1

I'm writing a python tool that will be executed on an embedded platform, and whenever I import the "random" module on it I will receive the following error output:

ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512

Aside from this error message, the tool executes and does it' work as it should. After looking around a bit, it seems to be because the embedded OS lacks a specific library. It is not really an option to install it on the target, and what is more I'm okay with these hashtypes being unsupported as I'm not using them. But, I'd rather not have this error message and I cannot figure out how to suppress it. I could I guess just redirect stderr to /dev/null just when I import the module, but then I might also hide other errors. How would I best go about suppressing that error?

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Pierre Andersson
  • 320
  • 2
  • 12
  • This doesn't look like import error per se. Maybe some other module that uses `random` module functions that fails while trying to use them. Probable duplicate of https://stackoverflow.com/q/20399331/3029287 because error traceback looks very similar if not the same. – Ilija Feb 09 '18 at 15:17

1 Answers1

1

As you said, it seems to happen because those algorithms (sha1, etc.) are meant to always be supported on all platforms, but they aren't supported on the one you are on.

What happens in hashlib.py is this:

__always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
                  'blake2b', 'blake2s',
                  'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
                  'shake_128', 'shake_256')

...

for __func_name in __always_supported:
    # try them all, some may not work due to the OpenSSL
    # version not supporting that algorithm.
    try:
        globals()[__func_name] = __get_hash(__func_name)
    except ValueError:
        import logging
        logging.exception('code for hash %s was not found.', __func_name)

So what you see is the result of logging.exception() writing these exceptions and error messages to stderr.

Now, the logging module has filtering capabilities. So you can actually filter out those specific exceptions from ending up on stderr.

The code below imports random, while filtering away precisely those specific ValueError exceptions concerning "unsupported hash type". And it only does it while importing random, so if similar errors occur later in the code, they will be reported at that point.

import logging

class RemoveUnsupportedHashTypeErrorsFilter(logging.Filter):
    def filter(self, record): # record is of type logging.LogRecord.
        if record.exc_info:
            exctype, value = record.exc_info[:2]
            if (exctype==ValueError) and ("unsupported hash type" in str(value)):
                return False  # Do not log the record.
        return True  # Log the record.

filter = RemoveUnsupportedHashTypeErrorsFilter()
logging.getLogger().addFilter(filter)

try:
    import random
finally:
    logging.getLogger().removeFilter(filter)
Jesper
  • 1,611
  • 13
  • 10