获取警告的回溯 [英] Get Traceback of warnings

查看:53
本文介绍了获取警告的回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 numpy 中我们可以做 np.seterr(invalid='raise') 以获取对引发错误的警告的回溯(请参阅 这篇文章).

In numpy we can do np.seterr(invalid='raise') to get a traceback for warnings raising an error instead (see this post).

  • 是否有跟踪警告的通用方法?
  • 当出现警告时,我可以让 python 进行回溯吗?

推荐答案

您可以通过分配给 warnings.showwarning 来获得您想要的东西.警告模块文档 本身建议您这样做,所以它是并不是说您受到了源头的阴暗面的诱惑.:)

You can get what you want by assigning to warnings.showwarning. The warnings module documentation itself recommends that you do that, so it's not that you're being tempted by the dark side of the source. :)

您可以通过分配给 warnings.showwarning 来使用替代实现替换此函数.

You may replace this function with an alternative implementation by assigning to warnings.showwarning.

您可以定义一个新函数来执行warning.showwarning 通常所做的事情,此外它还打印堆栈.然后你放置它而不是原来的:

You can define a new function that does what warning.showwarning normaly does and additionally it prints the stack. Then you place it instead of the original:

import traceback
import warnings
import sys

def warn_with_traceback(message, category, filename, lineno, file=None, line=None):

    log = file if hasattr(file,'write') else sys.stderr
    traceback.print_stack(file=log)
    log.write(warnings.formatwarning(message, category, filename, lineno, line))

warnings.showwarning = warn_with_traceback

此后,每个警告都会打印堆栈跟踪以及警告消息.但是请注意,如果警告因为不是第一个而被忽略,则不会发生任何事情,因此您仍然需要执行:

After this, every warning will print the stack trace as well as the warning message. Take into account, however, that if the warning is ignored because it is not the first one, nothing will happen, so you still need to execute:

warnings.simplefilter("always")

<小时>

您可以通过 warning 模块的过滤器获得与 numpy.seterr 类似的控制


You can get a similar control that the one numpy.seterr gives through the warning module's filters

如果您想要的是 python 在每次触发时报告每个警告,而不仅仅是第一次,您可以包含以下内容:

If what you want is python to report every a warning every time it is triggered and not only the first time, you can include something like:

import warnings
warnings.simplefilter("always")

您可以通过将不同的字符串作为参数传递来获得其他行为.使用相同的函数,您还可以根据引发警告的模块、警告提供的消息、警告类、导致警告的代码行等为警告指定不同的行为...

You can get other behaviours by passing different strings as arguments. Using the same function you can also specify different behaviours for warnings depending on the module that raised them, the message they provide, the warning class, the line of code that is causing it and so on...

您可以查看模块文档

例如,您可以将所有警告设置为引发异常,除了应该完全忽略的DeprecationWarnings:

As an example, you can set all the warnings to raise exceptions, except the DeprecationWarnings that should be ignored completely:

import warnings
warnings.simplefilter("error")
warnings.simplefilter("ignore", DeprecationWarning)

通过这种方式,您可以获得作为错误引发的每个警告的完整回溯(只有第一个,因为执行将停止......但您可以一一解决它们,并创建一个过滤器以忽略您不知道的那些想再听一遍...

This way you get the full traceback for each warning raised as error (only the first one, since execution will stop... but you can address them one by one, and create a filter to ignore the ones you don't want to hear about again...

这篇关于获取警告的回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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