Python,正在抑制错误:根:导入随机代码时的代码
我正在编写一个将在嵌入式平台上执行的python工具,每当我在其上导入“随机”模块时,我都会收到以下错误输出:Python,正在抑制错误:根:导入随机代码时的代码,python,random,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
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
错误:根:未找到哈希sha1的代码。
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/hashlib.py”,第147行,在
globals()[\uuuuu func\u name]=\uuuu get\u散列(\uuuu func\u name)
文件“/usr/lib/python2.7/hashlib.py”,第97行,在内置构造函数中
raise VALUERROR('不支持的哈希类型'+名称)
ValueError:不支持的哈希类型sha1
错误:根:未找到哈希sha224的代码。
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/hashlib.py”,第147行,在
globals()[\uuuuu func\u name]=\uuuu get\u散列(\uuuu func\u name)
文件“/usr/lib/python2.7/hashlib.py”,第97行,在内置构造函数中
raise VALUERROR('不支持的哈希类型'+名称)
ValueError:不支持的哈希类型sha224
错误:根:未找到哈希sha256的代码。
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/hashlib.py”,第147行,在
globals()[\uuuuu func\u name]=\uuuu get\u散列(\uuuu func\u name)
文件“/usr/lib/python2.7/hashlib.py”,第97行,在内置构造函数中
raise VALUERROR('不支持的哈希类型'+名称)
ValueError:不支持的哈希类型sha256
错误:根:未找到哈希sha384的代码。
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/hashlib.py”,第147行,在
globals()[\uuuuu func\u name]=\uuuu get\u散列(\uuuu func\u name)
文件“/usr/lib/python2.7/hashlib.py”,第97行,在内置构造函数中
raise VALUERROR('不支持的哈希类型'+名称)
ValueError:不支持的哈希类型sha384
错误:根:未找到哈希sha512的代码。
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python2.7/hashlib.py”,第147行,在
globals()[\uuuuu func\u name]=\uuuu get\u散列(\uuuu func\u name)
文件“/usr/lib/python2.7/hashlib.py”,第97行,在内置构造函数中
raise VALUERROR('不支持的哈希类型'+名称)
ValueError:不支持的哈希类型sha512
除了此错误消息外,该工具执行并按其应做的工作。环顾四周,这似乎是因为嵌入式操作系统缺少特定的库。在目标上安装它并不是一个真正的选项,更重要的是,我同意这些hashtype不受支持,因为我没有使用它们。但是,我宁愿没有这个错误消息,也不知道如何抑制它。我想我可能只是在导入模块时将stderr重定向到/dev/null,但是我也可能隐藏其他错误。我怎样才能最好地抑制这种错误呢?正如您所说,这种情况似乎发生了,因为这些算法(
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的结果
现在,日志记录
模块已经完成。因此,您实际上可以过滤掉那些特定的异常,使其不会出现在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)
这看起来不像导入错误本身。可能是使用
随机
模块函数的其他模块在尝试使用时失败。错误的可能重复,因为错误回溯看起来非常相似(如果不相同的话)。