From 45aab84447607060e84110c917dcc0487b4cd2c8 Mon Sep 17 00:00:00 2001 From: Michał Górny Date: Mon, 23 Oct 2017 08:07:16 +0200 Subject: hash: Avoid nested exceptions --- gemato/hash.py | 38 ++++++++++++++++++++------------------ 1 file 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): -- cgit v1.2.3