diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2020-10-06 14:49:50 -0400 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2020-10-06 14:49:50 -0400 |
commit | e82f75c13f9815f82b0cf046b2a502925f194703 (patch) | |
tree | 19e9bd2034b27e1fbcdfbfb93bfff29e6cbb848d | |
parent | 2cabd30db6306316c7a193585ea1cec5235d5209 (diff) | |
download | passlib-e82f75c13f9815f82b0cf046b2a502925f194703.tar.gz |
cleanup old python compat -- removed "unicode_or_str" and "native_string_types" aliases
-rw-r--r-- | passlib/context.py | 22 | ||||
-rw-r--r-- | passlib/crypto/_blowfish/__init__.py | 4 | ||||
-rw-r--r-- | passlib/handlers/argon2.py | 6 | ||||
-rw-r--r-- | passlib/handlers/scram.py | 4 | ||||
-rw-r--r-- | passlib/handlers/scrypt.py | 2 | ||||
-rw-r--r-- | passlib/registry.py | 7 | ||||
-rw-r--r-- | passlib/totp.py | 8 | ||||
-rw-r--r-- | passlib/utils/__init__.py | 8 | ||||
-rw-r--r-- | passlib/utils/compat/__init__.py | 11 | ||||
-rw-r--r-- | passlib/utils/handlers.py | 14 | ||||
-rw-r--r-- | passlib/utils/pbkdf2.py | 5 |
11 files changed, 40 insertions, 51 deletions
diff --git a/passlib/context.py b/passlib/context.py index bafb4f4..6195d1b 100644 --- a/passlib/context.py +++ b/passlib/context.py @@ -21,7 +21,7 @@ from passlib.utils import (handlers as uh, to_bytes, from passlib.utils.binary import BASE64_CHARS from passlib.utils.compat import (num_types, unicode, - unicode_or_bytes_types, native_string_types, + unicode_or_bytes_types, ) from passlib.utils.decor import deprecated_method, memoized_property # local @@ -137,7 +137,7 @@ class _CryptConfig(object): """initialize .handlers and .schemes attributes""" handlers = [] schemes = [] - if isinstance(data, native_string_types): + if isinstance(data, str): data = splitcomma(data) for elem in data or (): # resolve elem -> handler & scheme @@ -145,7 +145,7 @@ class _CryptConfig(object): handler = elem scheme = handler.name _validate_handler_name(scheme) - elif isinstance(elem, native_string_types): + elif isinstance(elem, str): handler = get_crypt_handler(elem) scheme = handler.name else: @@ -248,7 +248,7 @@ class _CryptConfig(object): raise KeyError("%r option not allowed in CryptContext " "configuration" % (key,)) # coerce strings for certain fields (e.g. min_rounds uses ints) - if isinstance(value, native_string_types): + if isinstance(value, str): func = _coerce_scheme_options.get(key) if func: value = func(value) @@ -259,12 +259,12 @@ class _CryptConfig(object): if key == "default": if hasattr(value, "name"): value = value.name - elif not isinstance(value, native_string_types): + elif not isinstance(value, str): raise ExpectedTypeError(value, "str", "default") if schemes and value not in schemes: raise KeyError("default scheme not found in policy") elif key == "deprecated": - if isinstance(value, native_string_types): + if isinstance(value, str): value = splitcomma(value) elif not isinstance(value, (list,tuple)): raise ExpectedTypeError(value, "str or seq", "deprecated") @@ -277,7 +277,7 @@ class _CryptConfig(object): elif schemes: # make sure list of deprecated schemes is subset of configured schemes for scheme in value: - if not isinstance(scheme, native_string_types): + if not isinstance(scheme, str): raise ExpectedTypeError(value, "str", "deprecated element") if scheme not in schemes: raise KeyError("deprecated scheme not found " @@ -545,9 +545,9 @@ class _CryptConfig(object): pass # type check - if category is not None and not isinstance(category, native_string_types): + if category is not None and not isinstance(category, str): raise ExpectedTypeError(category, "str or None", "category") - if scheme is not None and not isinstance(scheme, native_string_types): + if scheme is not None and not isinstance(scheme, str): raise ExpectedTypeError(scheme, "str or None", "scheme") # if scheme=None, @@ -1014,7 +1014,7 @@ class CryptContext(object): def _parse_config_key(ckey): """helper used to parse ``cat__scheme__option`` keys into a tuple""" # split string into 1-3 parts - assert isinstance(ckey, native_string_types) + assert isinstance(ckey, str) parts = ckey.replace(".", "__").split("__") count = len(parts) if count == 1: @@ -1284,7 +1284,7 @@ class CryptContext(object): else: value = str(value) - assert isinstance(value, native_string_types), \ + assert isinstance(value, str), \ "expected string for key: %r %r" % (key, value) # escape any percent signs. diff --git a/passlib/crypto/_blowfish/__init__.py b/passlib/crypto/_blowfish/__init__.py index 988918b..f1e84e9 100644 --- a/passlib/crypto/_blowfish/__init__.py +++ b/passlib/crypto/_blowfish/__init__.py @@ -56,7 +56,7 @@ import struct # pkg from passlib.utils import getrandbytes, rng from passlib.utils.binary import bcrypt64 -from passlib.utils.compat import unicode, native_string_types +from passlib.utils.compat import unicode from passlib.crypto._blowfish.unrolled import BlowfishEngine # local __all__ = [ @@ -99,7 +99,7 @@ def raw_bcrypt(password, ident, salt, log_rounds): #=================================================================== # parse ident - assert isinstance(ident, native_string_types) + assert isinstance(ident, str) add_null_padding = True if ident == u'2a' or ident == u'2y' or ident == u'2b': pass diff --git a/passlib/handlers/argon2.py b/passlib/handlers/argon2.py index 9bb4005..6a38153 100644 --- a/passlib/handlers/argon2.py +++ b/passlib/handlers/argon2.py @@ -292,7 +292,7 @@ class _Argon2Common(uh.SubclassBackendMixin, uh.ParallelismMixin, # set checksum size relaxed = kwds.get("relaxed") if digest_size is not None: - if isinstance(digest_size, uh.native_string_types): + if isinstance(digest_size, str): digest_size = int(digest_size) # NOTE: this isn't *really* digest size minimum, but want to enforce secure minimum. subcls.checksum_size = uh.norm_integer(subcls, digest_size, min=16, max=MAX_UINT32, @@ -300,7 +300,7 @@ class _Argon2Common(uh.SubclassBackendMixin, uh.ParallelismMixin, # set memory cost if memory_cost is not None: - if isinstance(memory_cost, uh.native_string_types): + if isinstance(memory_cost, str): memory_cost = int(memory_cost) subcls.memory_cost = subcls._norm_memory_cost(memory_cost, relaxed=relaxed) @@ -309,7 +309,7 @@ class _Argon2Common(uh.SubclassBackendMixin, uh.ParallelismMixin, # set max threads if max_threads is not None: - if isinstance(max_threads, uh.native_string_types): + if isinstance(max_threads, str): max_threads = int(max_threads) if max_threads < 1 and max_threads != -1: raise ValueError("max_threads (%d) must be -1 (unlimited), or at least 1." % diff --git a/passlib/handlers/scram.py b/passlib/handlers/scram.py index dbf8b88..4486e15 100644 --- a/passlib/handlers/scram.py +++ b/passlib/handlers/scram.py @@ -8,7 +8,7 @@ import logging; log = logging.getLogger(__name__) # pkg from passlib.utils import consteq, saslprep, to_native_str, splitcomma from passlib.utils.binary import ab64_decode, ab64_encode -from passlib.utils.compat import bascii_to_str, native_string_types +from passlib.utils.compat import bascii_to_str from passlib.crypto.digest import pbkdf2_hmac, norm_hash_name import passlib.utils.handlers as uh # local @@ -336,7 +336,7 @@ class scram(uh.HasRounds, uh.HasRawSalt, uh.HasRawChecksum, uh.GenericHandler): @classmethod def _norm_algs(cls, algs): """normalize algs parameter""" - if isinstance(algs, native_string_types): + if isinstance(algs, str): algs = splitcomma(algs) algs = sorted(norm_hash_name(alg, 'iana') for alg in algs) if any(len(alg)>9 for alg in algs): diff --git a/passlib/handlers/scrypt.py b/passlib/handlers/scrypt.py index f3113dd..f6131f5 100644 --- a/passlib/handlers/scrypt.py +++ b/passlib/handlers/scrypt.py @@ -155,7 +155,7 @@ class scrypt(uh.ParallelismMixin, uh.HasRounds, uh.HasRawSalt, uh.HasRawChecksum def using(cls, block_size=None, **kwds): subcls = super(scrypt, cls).using(**kwds) if block_size is not None: - if isinstance(block_size, uh.native_string_types): + if isinstance(block_size, str): block_size = int(block_size) subcls.block_size = subcls._norm_block_size(block_size, relaxed=kwds.get("relaxed")) diff --git a/passlib/registry.py b/passlib/registry.py index e1161ad..3d2976b 100644 --- a/passlib/registry.py +++ b/passlib/registry.py @@ -14,7 +14,6 @@ from passlib.utils import ( is_crypt_handler, has_crypt as os_crypt_present, unix_crypt_schemes as os_crypt_schemes, ) -from passlib.utils.compat import unicode_or_str from passlib.utils.decor import memoize_single_value # local __all__ = [ @@ -323,7 +322,7 @@ def get_crypt_handler(name, default=_UNSET): pass # normalize name (and if changed, check dict again) - assert isinstance(name, unicode_or_str), "name must be string instance" + assert isinstance(name, str), "name must be string instance" alt = name.replace("-","_").lower() if alt != name: warn("handler names should be lower-case, and use underscores instead " @@ -434,10 +433,10 @@ def _resolve(hasher, param="value"): """ if is_crypt_handler(hasher): return hasher - elif isinstance(hasher, unicode_or_str): + elif isinstance(hasher, str): return get_crypt_handler(hasher) else: - raise exc.ExpectedTypeError(hasher, unicode_or_str, param) + raise exc.ExpectedTypeError(hasher, str, param) #: backend aliases diff --git a/passlib/totp.py b/passlib/totp.py index 480abe7..c0b0342 100644 --- a/passlib/totp.py +++ b/passlib/totp.py @@ -31,7 +31,7 @@ from passlib.exc import TokenError, MalformedTokenError, InvalidTokenError, Used from passlib.utils import (to_unicode, to_bytes, consteq, getrandbytes, rng, SequenceMixin, xor_bytes, getrandstr) from passlib.utils.binary import BASE64_CHARS, b32encode, b32decode -from passlib.utils.compat import (u, unicode, native_string_types, bascii_to_str, int_types, num_types, +from passlib.utils.compat import (u, unicode, bascii_to_str, int_types, num_types, byte_elem_value) from passlib.utils.decor import hybrid_method, memoized_property from passlib.crypto.digest import lookup_hash, compile_hmac, pbkdf2_hmac @@ -249,7 +249,7 @@ class AppWallet(object): # init cost # if encrypt_cost is not None: - if isinstance(encrypt_cost, native_string_types): + if isinstance(encrypt_cost, str): encrypt_cost = int(encrypt_cost) assert encrypt_cost >= 0 self.encrypt_cost = encrypt_cost @@ -291,7 +291,7 @@ class AppWallet(object): # to make this easy to pass in configuration from a separate file, # 'secrets' can be string using two formats -- json & "tag:value\n" check_type = True - if isinstance(source, native_string_types): + if isinstance(source, str): if source.lstrip().startswith(("[", "{")): # json list / dict source = json.loads(source) @@ -324,7 +324,7 @@ class AppWallet(object): for tag, value in source) def _parse_secret_pair(self, tag, value): - if isinstance(tag, native_string_types): + if isinstance(tag, str): pass elif isinstance(tag, int): tag = str(tag) diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index d6c61a0..9ad8f00 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -64,7 +64,7 @@ from passlib.exc import ExpectedStringError, ExpectedTypeError from passlib.utils.compat import (add_doc, join_bytes, join_byte_values, join_byte_elems, join_unicode, unicode, byte_elem_value, - unicode_or_str, unicode_or_bytes_types, + unicode_or_bytes_types, get_method_function, PYPY) # local __all__ = [ @@ -960,9 +960,9 @@ def test_crypt(secret, hash): # safe_crypt() always returns unicode, which means that for py3, # 'hash' can't be bytes, or "== hash" will never be True. # under py2 unicode & str(bytes) will compare fine; - # so just enforcing "unicode_or_str" limitation - assert isinstance(hash, unicode_or_str), \ - "hash must be unicode_or_str, got %s" % type(hash) + # so just enforcing "str" limitation + assert isinstance(hash, str), \ + "hash must be str, got %s" % type(hash) assert hash, "hash must be non-empty" return safe_crypt(secret, hash) == hash diff --git a/passlib/utils/compat/__init__.py b/passlib/utils/compat/__init__.py index 1a206e8..2b00d89 100644 --- a/passlib/utils/compat/__init__.py +++ b/passlib/utils/compat/__init__.py @@ -22,15 +22,10 @@ PYPY = hasattr(sys, "pypy_version_info") if PYPY and sys.pypy_version_info < (2,0): raise RuntimeError("passlib requires pypy >= 2.0 (as of passlib 1.7)") -# e.g. '2.7.7\n[Pyston 0.5.1]' -# NOTE: deprecated support 2019-11 -PYSTON = "Pyston" in sys.version - #============================================================================= # common imports #============================================================================= import logging; log = logging.getLogger(__name__) -import builtins def add_doc(obj, doc): """add docstring to an object""" @@ -45,7 +40,7 @@ __all__ = [ 'int_types', 'num_types', 'unicode_or_bytes_types', - 'native_string_types', + 'str', # unicode/bytes types & helpers 'u', @@ -82,18 +77,15 @@ if True: # legacy PY3 indent return s unicode_or_bytes_types = (str, bytes) - native_string_types = (unicode,) # shorter preferred aliases # TODO: align compat module w/ crowbar.compat naming unicode_or_bytes = unicode_or_bytes_types -unicode_or_str = native_string_types # unicode -- unicode type, regardless of python version # bytes -- bytes type, regardless of python version # unicode_or_bytes_types -- types that text can occur in, whether encoded or not -# native_string_types -- types that native python strings (dict keys etc) can occur in. #============================================================================= # unicode & bytes helpers @@ -266,7 +258,6 @@ class _LazyOverlayModule(ModuleType): return list(attrs) # replace this module with overlay that will lazily import attributes. -_lazy_attrs = dict() _LazyOverlayModule.replace_module(__name__, _lazy_attrs) #============================================================================= diff --git a/passlib/utils/handlers.py b/passlib/utils/handlers.py index 8fa797d..7703683 100644 --- a/passlib/utils/handlers.py +++ b/passlib/utils/handlers.py @@ -26,7 +26,7 @@ from passlib.utils.binary import ( HEX_CHARS, UPPER_HEX_CHARS, LOWER_HEX_CHARS, ALL_BYTE_VALUES, ) -from passlib.utils.compat import join_byte_values, native_string_types, \ +from passlib.utils.compat import join_byte_values, \ uascii_to_str, join_unicode, unicode, str_to_uascii, \ join_unicode, unicode_or_bytes_types, int_types from passlib.utils.decor import classproperty, deprecated_method @@ -1298,7 +1298,7 @@ class HasSalt(GenericHandler): # replace default_rounds relaxed = kwds.get("relaxed") if default_salt_size is not None: - if isinstance(default_salt_size, native_string_types): + if isinstance(default_salt_size, str): default_salt_size = int(default_salt_size) subcls.default_salt_size = subcls._clip_to_valid_salt_size(default_salt_size, param="salt_size", @@ -1619,7 +1619,7 @@ class HasRounds(GenericHandler): min_desired_rounds = cls.min_desired_rounds else: explicit_min_rounds = True - if isinstance(min_desired_rounds, native_string_types): + if isinstance(min_desired_rounds, str): min_desired_rounds = int(min_desired_rounds) subcls.min_desired_rounds = subcls._norm_rounds(min_desired_rounds, param="min_desired_rounds", @@ -1629,7 +1629,7 @@ class HasRounds(GenericHandler): if max_desired_rounds is None: max_desired_rounds = cls.max_desired_rounds else: - if isinstance(max_desired_rounds, native_string_types): + if isinstance(max_desired_rounds, str): max_desired_rounds = int(max_desired_rounds) if min_desired_rounds and max_desired_rounds < min_desired_rounds: msg = "%s: max_desired_rounds (%r) below min_desired_rounds (%r)" % \ @@ -1645,7 +1645,7 @@ class HasRounds(GenericHandler): # replace default_rounds if default_rounds is not None: - if isinstance(default_rounds, native_string_types): + if isinstance(default_rounds, str): default_rounds = int(default_rounds) if min_desired_rounds and default_rounds < min_desired_rounds: raise ValueError("%s: default_rounds (%r) below min_desired_rounds (%r)" % @@ -1663,7 +1663,7 @@ class HasRounds(GenericHandler): # replace / set vary_rounds if vary_rounds is not None: - if isinstance(vary_rounds, native_string_types): + if isinstance(vary_rounds, str): if vary_rounds.endswith("%"): vary_rounds = float(vary_rounds[:-1]) * 0.01 elif "." in vary_rounds: @@ -1892,7 +1892,7 @@ class ParallelismMixin(GenericHandler): def using(cls, parallelism=None, **kwds): subcls = super(ParallelismMixin, cls).using(**kwds) if parallelism is not None: - if isinstance(parallelism, native_string_types): + if isinstance(parallelism, str): parallelism = int(parallelism) subcls.parallelism = subcls._norm_parallelism(parallelism, relaxed=kwds.get("relaxed")) return subcls diff --git a/passlib/utils/pbkdf2.py b/passlib/utils/pbkdf2.py index e490af5..d9bd083 100644 --- a/passlib/utils/pbkdf2.py +++ b/passlib/utils/pbkdf2.py @@ -11,7 +11,6 @@ import logging; log = logging.getLogger(__name__) # site # pkg from passlib.exc import ExpectedTypeError -from passlib.utils.compat import native_string_types from passlib.crypto.digest import lookup_hash, pbkdf1 as _pbkdf1, pbkdf2_hmac, compile_hmac # local __all__ = [ @@ -85,7 +84,7 @@ def get_prf(name): global _prf_cache if name in _prf_cache: return _prf_cache[name] - if isinstance(name, native_string_types): + if isinstance(name, str): if not name.startswith(_HMAC_PREFIXES): raise ValueError("unknown prf algorithm: %r" % (name,)) digest = lookup_hash(name[5:]).name @@ -171,7 +170,7 @@ def pbkdf2(secret, salt, rounds, keylen=None, prf="hmac-sha1"): This has been deprecated in favor of :func:`passlib.crypto.digest.pbkdf2_hmac`, and will be removed in Passlib 2.0. *Note the call signature has changed.* """ - if callable(prf) or (isinstance(prf, native_string_types) and not prf.startswith(_HMAC_PREFIXES)): + if callable(prf) or (isinstance(prf, str) and not prf.startswith(_HMAC_PREFIXES)): raise NotImplementedError("non-HMAC prfs are not supported as of Passlib 1.7") digest = prf[5:] return pbkdf2_hmac(digest, secret, salt, rounds, keylen) |