Python,压缩ERROR:root:导入随机时的代码

时间:2018-02-09 15:10:35

标签: python random

我正在编写一个将在嵌入式平台上执行的python工具,每当我导入"随机"我将收到以下错误输出:

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

除了此错误消息之外,该工具还会执行并执行此操作。尽职尽责。看了一下之后,似乎是因为嵌入式操作系统缺少特定的库。在目标上安装它并不是一个真正的选择,而且由于我没有使用这些哈希类型,所以我更加不受支持。但是,我宁愿没有这个错误信息,也无法弄清楚如何抑制它。我可以猜测只是在导入模块时将stderr重定向到/ dev / null,但我可能还会隐藏其他错误。我怎样才能最好地压制这个错误?

1 个答案:

答案 0 :(得分:1)

正如你所说,似乎是因为那些算法(sha1等)在所有平台上都是支持的,但是你所使用的那些算法不支持它们。

hashlib.py会发生什么:

__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)

所以你看到的是logging.exception()将这些异常和错误消息写入stderr的结果。

现在,logging模块已filtering capabilities。因此,您实际上可以过滤掉stderr上的特定异常。

以下代码导入random,同时精确过滤掉与“不支持的散列类型”相关的特定ValueError异常。并且它只在导入random时执行,因此如果代码中稍后出现类似错误,则会在此时报告它们。

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)