PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 27, 2022

[FIXED] How do you list all warning filters in Python?

 April 27, 2022     django, python, python-3.x, warnings     No comments   

Issue

I just spent the day trying to remove this warning from my output:

sys:1: ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/null' mode='w' encoding='UTF-8'>

I just finished a binary search of about 100k lines of code to try to figure this out. I still haven't found what's leaving /dev/null open, but I did find that some of our code says:

warnings.simplefilter("always")

Removing that removes the warning. Hooray! I plan to remove that bit of code, but I'm curious of two things:

  1. Could I have avoided doing a binary search for this somehow? Enabling TRACEMALLOC=1 was entirely ineffective (maybe because this is in third-party code we import?)

  2. Once I remove this, is there a way to list all of the warning filters that are applied at a particular point in the code? Seems like an important thing to be able to analyze, but I can't figure out how.


Solution

Just grep for import warnings (or the from variant). It's not as though that's a common thing to import.

After executing someone else's code, you can always

warnings.simplefilter("ignore")

to disable what they enabled, even without reviewing their source code.


You can view current filters in this way:

>>> import warnings
>>> import pprint
>>> pprint.pprint(warnings.filters)
[('default', None, <class 'DeprecationWarning'>, '__main__', 0),
 ('ignore', None, <class 'DeprecationWarning'>, None, 0),
 ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0),
 ('ignore', None, <class 'ImportWarning'>, None, 0),
 ('ignore', None, <class 'ResourceWarning'>, None, 0)]


Answered By - J_H
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing