diff options
author | Michał Górny <mgorny@gentoo.org> | 2017-10-23 08:07:16 +0200 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2017-10-23 08:07:16 +0200 |
commit | 45aab84447607060e84110c917dcc0487b4cd2c8 (patch) | |
tree | ed501d54dd76597d503db755c1ba7a35a062f645 | |
parent | 0dff55fb5bdebd6373b68b413b5104077d154fc8 (diff) | |
download | gemato-45aab84447607060e84110c917dcc0487b4cd2c8.tar.gz |
hash: Avoid nested exceptions
-rw-r--r-- | gemato/hash.py | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/gemato/hash.py b/gemato/hash.py index 6a1c854..18313f7 100644 --- a/gemato/hash.py +++ b/gemato/hash.py @@ -24,29 +24,31 @@ def get_hash_by_name(name): try: return hashlib.new(name) except ValueError: - # fallback support - if name.startswith('sha3_'): + pass + + # fallback support + if name.startswith('sha3_'): + try: + import sha3 + except ImportError: + pass + else: try: - import sha3 - except ImportError: + return getattr(sha3, name)() + except AttributeError: pass - else: - try: - return getattr(sha3, name)() - except AttributeError: - pass - elif name.startswith('blake2'): + elif name.startswith('blake2'): + try: + import pyblake2 + except ImportError: + pass + else: try: - import pyblake2 - except ImportError: + return getattr(pyblake2, name)() + except AttributeError: pass - else: - try: - return getattr(pyblake2, name)() - except AttributeError: - pass - raise UnsupportedHash(name) + raise UnsupportedHash(name) def hash_file(f, hash_names): |