除非设置了调试标志,否则隐藏回溯 [英] Hide traceback unless a debug flag is set

查看:26
本文介绍了除非设置了调试标志,否则隐藏回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非设置了详细或调试标志,否则隐藏回溯错误的惯用 Python 方法是什么?

示例代码:

their_md5 = 'c38f03d2b7160f891fc36ec776ca4685'my_md5 = 'c64e53bbb108a1c65e31eb4d1bb8e3b7'如果他们的_md5 != my_md5:raise ValueError('md5 sum 不匹配!')

现有输出,但仅在使用 foo.py --debug 调用时才需要:

回溯(最近一次调用最后一次):文件b:codeaptapt.py",第 1647 行,在 <module> 中__main__.__dict__[command](包)文件b:codeaptapt.py",第 399 行,在 md5 中raise ValueError('md5 sum 不匹配!')ValueError: md5 sum 不匹配!

所需的正常输出:

ValueError: md5 sum 不匹配!

这是一个测试脚本:https://gist.github.com/maphew/e3a75c147cca98019cd8

解决方案

简单的方法是使用 sys 模块并使用这个命令:

sys.tracebacklimit = 0

使用您的标志来确定行为.

示例:

<预><代码>>>>导入系统>>>sys.tracebacklimit=0>>>int('a')ValueError:int() 的无效文字,基数为 10:'a'

更好的方法是使用和异常钩子:

def exception_handler(exception_type, exception, traceback):#你的踪迹都是我们的!#你的格式打印 "%s: %s" % (exception_type.__name__, 异常)sys.excepthook = 异常处理程序

如果你还需要回退到原来的钩子的选项:

def exception_handler(exception_type, exception, traceback, debug_hook=sys.excepthook):如果_your_debug_flag_here:debug_hook(异常类型,异常,回溯)别的:打印 "%s: %s" % (exception_type.__name__, 异常)

现在您可以将调试钩子传递给处理程序,但您很可能希望始终使用源自 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:codeaptapt.py", line 1647, in <module>
    __main__.__dict__[command] (packages)
  File "b:codeaptapt.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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆