diff options
| author | Eli Collins <elic@assurancetechnologies.com> | 2020-10-05 21:26:25 -0400 |
|---|---|---|
| committer | Eli Collins <elic@assurancetechnologies.com> | 2020-10-05 21:26:25 -0400 |
| commit | 6badf316805b188c92165e342ca88ce3be9a96db (patch) | |
| tree | 735fe346eb1140c858fc3e57a08ac3eb5292905d /passlib/utils | |
| parent | 3f1ad211d9499f5c36e9cc90c888c6e0da50ab3c (diff) | |
| download | passlib-6badf316805b188c92165e342ca88ce3be9a96db.tar.gz | |
passlib.utils: add thread lock to fix some UT failures.
Diffstat (limited to 'passlib/utils')
| -rw-r--r-- | passlib/utils/__init__.py | 27 | ||||
| -rw-r--r-- | passlib/utils/compat/__init__.py | 25 |
2 files changed, 49 insertions, 3 deletions
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index fd30b3e..e3df5d5 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -36,6 +36,11 @@ else: import time if stringprep: import unicodedata +try: + import threading +except ImportError: + # module optional before py37 + threading = None import timeit import types from warnings import warn @@ -60,7 +65,7 @@ from passlib.utils.compat import (add_doc, join_bytes, join_byte_values, join_byte_elems, irange, imap, PY3, u, join_unicode, unicode, byte_elem_value, nextgetter, unicode_or_str, unicode_or_bytes_types, - get_method_function, suppress_cause) + get_method_function, suppress_cause, PYPY) # local __all__ = [ # constants @@ -856,6 +861,20 @@ else: has_crypt = True _NULL = '\x00' + # XXX: replace this with lazy-evaluated bug detection? + if threading and PYPY and (7, 2, 0) <= sys.pypy_version_info <= (7, 3, 3): + #: internal lock used to wrap crypt() calls. + #: WARNING: if non-passlib code invokes crypt(), this lock won't be enough! + _safe_crypt_lock = threading.Lock() + + #: detect if crypt.crypt() needs a thread lock around calls. + crypt_needs_lock = True + + else: + from passlib.utils.compat import nullcontext + _safe_crypt_lock = nullcontext() + crypt_needs_lock = False + # some crypt() variants will return various constant strings when # an invalid/unrecognized config string is passed in; instead of # returning NULL / None. examples include ":", ":0", "*0", etc. @@ -908,7 +927,8 @@ else: if isinstance(hash, bytes): hash = hash.decode("ascii") try: - result = _crypt(secret, hash) + with _safe_crypt_lock: + result = _crypt(secret, hash) except OSError: # new in py39 -- per https://bugs.python.org/issue39289, # crypt() now throws OSError for various things, mainly unknown hash formats @@ -935,7 +955,8 @@ else: raise ValueError("null character in secret") if isinstance(hash, unicode): hash = hash.encode("ascii") - result = _crypt(secret, hash) + with _safe_crypt_lock: + result = _crypt(secret, hash) if not result: return None result = result.decode("ascii") diff --git a/passlib/utils/compat/__init__.py b/passlib/utils/compat/__init__.py index 1d6cf4d..f6ead24 100644 --- a/passlib/utils/compat/__init__.py +++ b/passlib/utils/compat/__init__.py @@ -80,6 +80,9 @@ __all__ = [ # collections 'OrderedDict', + # context helpers + 'nullcontext', + # introspection 'get_method_function', 'add_doc', ] @@ -374,6 +377,28 @@ else: _lazy_attrs['OrderedDict'] = 'collections.OrderedDict' #============================================================================= +# context managers +#============================================================================= + +try: + # new in py37 + from contextlib import nullcontext +except ImportError: + + class nullcontext(object): + """ + Context manager that does no additional processing. + """ + def __init__(self, enter_result=None): + self.enter_result = enter_result + + def __enter__(self): + return self.enter_result + + def __exit__(self, *exc_info): + pass + +#============================================================================= # lazy overlay module #============================================================================= from types import ModuleType |
