简体   繁体   English

如何通过warnings.warn抑制源上下文的打印?

[英]How to suppress the printing of source context by warnings.warn?

If I put the following snippet in a file annoying.py :如果我将以下代码段放在文件中,则annoying.py

import warnings
message = 'I know!  How about if we are just maximally annoying?!'
warnings.warn(message)

...and run it in a Unix shell, this is what I get: ...并在 Unix shell 中运行它,这就是我得到的:

% python annoying.py
annoying.py:3: UserWarning: I know!  How about if we are just maximally annoying?!
  warnings.warn(message)

(I get the same output with Python 3.7.3 and 2.7.16.) (我得到与 Python 3.7.3 和 2.7.16 相同的 output。)

I want only the first line of this output to be printed.我只想打印此 output 的第一行。

With respect to what I'm working on, the second line only adds visually distracting clutter.关于我正在做的事情,第二行只会增加视觉上分散注意力的混乱。 (This gratuitous clutter becomes particularly problematic when there are a lot of warnings.) (当有很多警告时,这种无缘无故的混乱变得特别成问题。)

How can I suppress the second/clutter line?如何抑制第二条/杂乱线?

From warnings.py source code it seems that msg.line controls if that extra line needs to printed or not.warnings.py源代码看来, msg.line 控制是否需要打印额外的行。 One could monkey patch the WarningMessage's init such that self.line is "" instead of None as show below可以猴子修补 WarningMessage 的初始化,这样 self.line 是 "" 而不是 None 如下所示

import warnings

#modify the __init__ so that self.line = "" instead of None
def new_init(self, message, category, filename, lineno, file=None,
                 line=None, source=None):
        self.message = message
        self.category = category
        self.filename = filename
        self.lineno = lineno
        self.file = file
        self.line = ""
        self.source = source
        self._category_name = category.__name__ if category else None

warnings.WarningMessage.__init__ = new_init

message = 'I know!  How about if we are just maximally annoying?!'
warnings.warn(message)

This results in:这导致:

annoying.py:19: UserWarning: I know!  How about if we are just maximally annoying?!

Another hacky way to do maybe is to make the warnings come as errors by using -W option:另一种 hacky 方法可能是使用 -W 选项将警告作为错误出现:

% python -Werror::UserWarning annoying.py

if you have following annoying.py如果您有以下烦恼.py

import warnings
message = 'I know!  How about if we are just maximally annoying?!'
try:
    warnings.warn(message)
except Exception as error:
    print(error) 

results in结果是

I know!  How about if we are just maximally annoying?!

To get the filename, linenumber, type of warning and warning message, I had to do the following要获取文件名、行号、警告类型和警告消息,我必须执行以下操作

import warnings
import sys, os
message = 'I know!  How about if we are just maximally annoying?!'
try:
    warnings.warn(message)
except Exception as error:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print('{}:{}: {}: {}'.format(fname, exc_tb.tb_lineno,exc_type.__name__,exc_obj))

which results in这导致

annoying.py:5: UserWarning: I know!  How about if we are just maximally annoying?!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM