Nathaniel Smith added the comment:

There isn't really any magic in how warnings work. Basically someone calls 
warnings.warn(...), which is a regular Python function, and it gathers up some 
information about the specific warning message and calling context, checks a 
global variable ("warnings.filters") to decide how this message in this context 
should be handled, and then either does nothing, prints something to stderr, or 
raises an exception.

There are lots of warnings that are printed by default, and I'm sure IDLE is 
handling them fine already. It's just (Pending)DeprecationWarnings in 
particular that have an entry stuck into warnings.filters saying "please ignore 
these" (unless it gets overridden by something else).

So you just need to make sure that a filter is added to warnings.filters that 
says to treat DeprecationWarnings generated by the __main__ module using the 
"default" action. ("default" is the name of a specific way of handling 
warnings; for most types of warnings, the default handler is the one named 
"default", but for DeprecationWarning, the default handler is the one named 
"ignore". Obvious, right?)

So you just need to make sure to run the following line of code somewhere in 
the user process:

warnings.filterwarnings("default", category=DeprecationWarning, 
module="__main__")

Adjust as necessary if (a) you want to apply similar handling to 
PendingDeprecationWarning, (b) your user namespace has some value of __name__ 
that is different from "__main__".

Then to test, you can just type

warnings.warn("foo", DeprecationWarning)

at the prompt, and it should be printed.

Note that because the warnings module tries to suppress duplicate warnings 
(which is good), and it has a bug where it can't tell the difference between 
different lines of code at the REPL (this is bad -- see 
https://github.com/ipython/ipython/issues/6611 , and there should probably be a 
python bug too but I haven't gotten around to filing it), then the *second* 
time you run that line of code in the same REPL, nothing will be printed. So 
the moral is just, when testing this, make sure you use a different warning 
message each time, or you'll get very confused.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue24294>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to