summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2012-08-02 14:07:16 -0400
committerEli Collins <elic@assurancetechnologies.com>2012-08-02 14:07:16 -0400
commit2754f78bc1bd5aa826d0385aef85fdb4005a2ffb (patch)
tree2935a3da12d4748649e91086c9e929dcf36180c1
parentcbd6cf1639ca34bf57046269d09bfc080a2e177a (diff)
downloadpasslib-2754f78bc1bd5aa826d0385aef85fdb4005a2ffb.tar.gz
bugfix for py3.3: passlib registry methods would erroneously return private module attrs
-rw-r--r--CHANGES2
-rw-r--r--passlib/registry.py12
-rw-r--r--passlib/tests/test_registry.py17
3 files changed, 30 insertions, 1 deletions
diff --git a/CHANGES b/CHANGES
index dde2429..689786a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -31,6 +31,8 @@ Release History
* *bugfix:* Fixed bug which caused passlib.apache unittest to fail
if mtime resolution >= 1 second (:issue:`35`).
+ * Various bugfixes for Python 3.3 compatibility.
+
* Various documentation updates and corrections.
**1.6** (2012-05-01)
diff --git a/passlib/registry.py b/passlib/registry.py
index eb0db9f..2616b8c 100644
--- a/passlib/registry.py
+++ b/passlib/registry.py
@@ -293,6 +293,14 @@ def get_crypt_handler(name, default=_UNSET):
:returns: handler attached to name, or default value (if specified).
"""
+ # catch invalid names before we check _handlers,
+ # since it's a module dict, and exposes things like __package__, etc.
+ if name.startswith("_"):
+ if default is _UNSET:
+ raise KeyError("invalid handler name: %r" % (name,))
+ else:
+ return default
+
# check if handler is already loaded
try:
return _handlers[name]
@@ -356,7 +364,9 @@ def list_crypt_handlers(loaded_only=False):
names = set(_handlers)
if not loaded_only:
names.update(_locations)
- return sorted(names)
+ # strip private attrs out of namespace and sort.
+ # TODO: make _handlers a separate list, so we don't have module namespace mixed in.
+ return sorted(name for name in names if not name.startswith("_"))
# NOTE: these two functions mainly exist just for the unittests...
diff --git a/passlib/tests/test_registry.py b/passlib/tests/test_registry.py
index bc910b5..306e95a 100644
--- a/passlib/tests/test_registry.py
+++ b/passlib/tests/test_registry.py
@@ -181,6 +181,23 @@ class RegistryTest(TestCase):
register_crypt_handler_path('dummy_0', __name__)
self.assertIs(get_crypt_handler("DUMMY-0"), dummy_0)
+ # check system & private names aren't returned
+ import passlib.hash # ensure module imported, so py3.3 sets __package__
+ passlib.hash.__dict__["_fake"] = "dummy" # so behavior seen under py2x also
+ for name in ["_fake", "__package__"]:
+ self.assertRaises(KeyError, get_crypt_handler, name)
+ self.assertIs(get_crypt_handler(name, None), None)
+
+ def test_list_crypt_handlers(self):
+ "test list_crypt_handlers()"
+ from passlib.registry import list_crypt_handlers
+
+ # check system & private names aren't returned
+ import passlib.hash # ensure module imported, so py3.3 sets __package__
+ passlib.hash.__dict__["_fake"] = "dummy" # so behavior seen under py2x also
+ for name in list_crypt_handlers():
+ self.assertFalse(name.startswith("_"), "%r: " % name)
+
#=============================================================================
# eof
#=============================================================================