Alex Rivera | Logout

Print only the message on warnings

Asked 2010-02-02T20:08:10.153
63

I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to warnings.warn().

I.e., now I see this:

./file.py:123: UserWarning: My looong warning message
some Python code

I'd like to see this:

My looong warning message
Edit
Report

1 Answer

26

Monkeypatch warnings.showwarning() with your own custom function, e.g. like this:

import warnings

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

warnings.showwarning = my_warning

Note that the manual itself recommends monkeypatching this:

You may replace this function with any callable by assigning to warnings.showwarning.

answered 2010-02-02T20:14:34.997

Your Answer