You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ResourceWarning when using astropy with Python 3 #4795
I'm using astropy.io.fits with a non-affiliated package. During unit testing on Python 3, I get this warning:
Full test of parsing a FITS file. ... /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/astropy/extern/bundled/six.py:60: ResourceWarning: unclosed file <_io.TextIOWrapper name='/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/astropy/extern/bundled/six.py' mode='r' encoding='utf-8'>
I'm using py34-astropy @1.1.1_2 with MacPorts. I also see similar warnings with Travis tests, where astropy is simply installed with pip install astropy.
@weaverba137 - I don't see why some code would be opening the six.py file, and as you're running a unit test on a FITS file and I found a this ResourceWarning issue in #4786, I think it could be useful to know which line in your code throws the warning.
It's not quite that simple, because ResourceWarning is ignored by default. Can you point me to where the exact instructions are for forcing ResourceWarning to always be an error in unittest test cases?
Just adding warnings.filterwarnings('error') doesn't actually turn ResourceWarning into an error:
Full test of parsing a FITS file. ... Exception ignored in: <_io.FileIO name='/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/astropy/extern/bundled/six.py' mode='rb'>
ResourceWarning: unclosed file <_io.TextIOWrapper name='/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/astropy/extern/bundled/six.py' mode='r' encoding='utf-8'>
@weaverba137 - I get the same issue, using warnings.simplefilter('error', ResourceWarning). I guess some py.test magic ... I try to add this before each astropy.io.fits test to see if they are more unclosed files cases.
Activity
astrofrog commentedon Apr 20, 2016
Could this be related to #4786?
weaverba137 commentedon Apr 20, 2016
Possibly, but in this case, the warning applies to the file astropy/extern/bundled/six.py, not to the FITS file.
saimn commentedon Apr 20, 2016
The message
<_io.TextIOWrapper name='.../astropy/extern/bundled/six.py' mode='r' encoding='utf-8'>is a bit weird. What does your test ?weaverba137 commentedon Apr 20, 2016
@saimn I don't understand your question. The warning is thrown by opening a FITS file, but the warning isn't about a FITS file, it's about six.py.
saimn commentedon Apr 20, 2016
@weaverba137 - I don't see why some code would be opening the six.py file, and as you're running a unit test on a FITS file and I found a this
ResourceWarningissue in #4786, I think it could be useful to know which line in your code throws the warning.weaverba137 commentedon Apr 20, 2016
Maybe you can advise me on how to extract a line number from a ResourceWarning, but I can say that it would be raised by this function: https://github.com/desihub/desidatamodel/blob/master/py/desidatamodel/stub.py#L331
pllim commentedon Apr 20, 2016
If you use Python warnings filter to turn any
ResourceWarninginto exception, theoretically you would get a "nice" traceback.weaverba137 commentedon Apr 20, 2016
It's not quite that simple, because ResourceWarning is ignored by default. Can you point me to where the exact instructions are for forcing ResourceWarning to always be an error in unittest test cases?
Just adding
warnings.filterwarnings('error')doesn't actually turn ResourceWarning into an error:saimn commentedon Apr 20, 2016
@weaverba137 - I get the same issue, using
warnings.simplefilter('error', ResourceWarning). I guess some py.test magic ... I try to add this before eachastropy.io.fitstest to see if they are more unclosed files cases.pllim commentedon Apr 20, 2016
Oh, didn't realize the filter does not work on this particular warning. Sorry for the noise.
weaverba137 commentedon Apr 20, 2016
By following the instructions here: http://stackoverflow.com/questions/24717027/convert-python-3-resourcewarnings-into-exception, I at least got the ResourceWarning to print, although the warning itself does not contain enough information to say where it is raised in my code. It does appear to be raised in line 60 of six.py.
pllim commentedon Apr 20, 2016
That happens to be the last line in
six.py:_import_six()Not very deterministic.
pllim commentedon Apr 20, 2016
Another way to go about this, in case it is related to #4786, is to use that PR and see if this warning goes away.
saimn commentedon Apr 20, 2016
Actually, after looking at the history of
astropy/extern/bundled/six.py, it look more similar to this old issue: #3156pllim commentedon Apr 21, 2016
Theoretically, #3156 should have fixed it but I guess this must be an edge case? Hmm.
saimn commentedon May 1, 2018
This one can probably be closed since six was removed from the code base.