隐藏回溯,除非设置了调试标志 [英] Hide traceback unless a debug flag is set
问题描述
示例代码:
them_md5 ='c38f03d2b7160f891fc36ec776ca4685'
my_md5 ='c64e53bbb108a1c65e31eb4d1bb8e3b7'
如果them_md5!= my_md5:
raise ValueError('md5 sum not match!')
现在输出,但只有在使用 foo.py --debug
调用时才需要:
追溯(最近的最后一次呼叫):
文件b:\code\apt\apt.py,第1647行,模块>
__main __.__ dict __ [command](packages)
文件b:\code\apt\apt.py,第399行,md5
raise ValueError('md5 sum不匹配!')
ValueError:md5 sum不匹配!
期望的正常输出:
ValueError:md5 sum不匹配!
这是一个测试脚本: https://gist.github.com/maphew/e3a75c147cca98019cd8
简单的方法是使用 sys
模块并使用以下命令:
sys.tracebacklimit = 0
使用你的标志来确定行为。
示例:
>>>导入sys
>>>> sys.tracebacklimit = 0
>>> int('a')
ValueError:对于基数为10的int()的无效字面值:'a'
更好的方法是使用异常钩子 :
def exception_handler(exception_type,exception,traceback):
#所有的跟踪属于我们!
#你的格式
打印%s:%s%(exception_type .__ name__,exception)
sys.excepthook = exception_handler
编辑:
如果您仍然需要退回原始挂钩的选项:
def exception_handler(exception_type,exception,traceback,debug_hook = sys.excepthook):
如果_your_debug_flag_here:
debug_hook(exception_type,exception,traceback)
else:
print%s:%s%(exception_type .__ name__,exception)
现在您可以将调试钩子传递给处理程序,但您最有可能希望始终使用源自 sys.excepthook
(所以在 debug_hook
中不传递)。在定义时间(常见的陷阱...)中,Python绑定默认参数一次,这使得它在替换之前始终可以使用相同的原始处理程序。
What is the idiomatic python way to hide traceback errors unless a verbose or debug flag is set?
Example code:
their_md5 = 'c38f03d2b7160f891fc36ec776ca4685'
my_md5 = 'c64e53bbb108a1c65e31eb4d1bb8e3b7'
if their_md5 != my_md5:
raise ValueError('md5 sum does not match!')
Existing output now, but only desired when called with foo.py --debug
:
Traceback (most recent call last):
File "b:\code\apt\apt.py", line 1647, in <module>
__main__.__dict__[command] (packages)
File "b:\code\apt\apt.py", line 399, in md5
raise ValueError('md5 sum does not match!')
ValueError: md5 sum does not match!
Desired normal output:
ValueError: md5 sum does not match!
Here's a test script: https://gist.github.com/maphew/e3a75c147cca98019cd8
The short way is using the sys
module and use this command:
sys.tracebacklimit = 0
Use your flag to determine the behaviour.
Example:
>>> import sys
>>> sys.tracebacklimit=0
>>> int('a')
ValueError: invalid literal for int() with base 10: 'a'
The nicer way is to use and exception hook:
def exception_handler(exception_type, exception, traceback):
# All your trace are belong to us!
# your format
print "%s: %s" % (exception_type.__name__, exception)
sys.excepthook = exception_handler
Edit:
If you still need the option of falling back to the original hook:
def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):
if _your_debug_flag_here:
debug_hook(exception_type, exception, traceback)
else:
print "%s: %s" % (exception_type.__name__, exception)
Now you can pass a debug hook to the handler, but you'll most likely want to always use the one originated in sys.excepthook
(so pass nothing in debug_hook
). Python binds default arguments once in definition time (common pitfall...) which makes this always work with the same original handler, before replaced.
这篇关于隐藏回溯,除非设置了调试标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!