Removing a warnings filter?

  • Thread starter Torsten Bronger
  • Start date
T

Torsten Bronger

Hallöchen!

When I add a warning filter with warnings.filterwarnings, how can I
get rid of it? I've read about resetwarnings(), but it removes all
filters, even those that I didn't install in a certain function.

In particular, I want to transform one special form of warning in an
exception to keep track of it more easily. However, before leaving
my function I want to restore the old status.

How can this be achieved? Thank you!

Tschö,
Torsten.
 
D

Dave Benjamin

Torsten said:
When I add a warning filter with warnings.filterwarnings, how can I
get rid of it? I've read about resetwarnings(), but it removes all
filters, even those that I didn't install in a certain function.

I have never used this module, but judging by a quick glance of the
source to "warnings.py", it appears that warning filters are added to a
list called "warnings.filters". They are added to the beginning of the
list unless you pass a true value for the "append" parameter of
"filterwarnings", in which case they are added to the end. The usual way
to remove items from a list apply, ie.:

del warnings.filters[0] # if append was false
del warnings.filters[-1] # if append was true

Note that all of these operations modify module data, which could have
implications in multithreaded programs. If your program is
multithreaded, you may want to consider using locks.

Hope this helps,
Dave
 
T

Torsten Bronger

Hallöchen!

Dave Benjamin said:
I have never used this module, but judging by a quick glance of
the source to "warnings.py", it appears that warning filters are
added to a list called "warnings.filters".

Thank you, I'll use it in my program. However, I don't like it very
much, because warnings.filters isn't mentioned in the reference, and
in other languages that I've used, internal features are not
guaranteed to work across different versions.

Tschö,
Torsten.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,241
Messages
2,571,219
Members
47,849
Latest member
RoseannKoz

Latest Threads

Top