Skip to content

ResourceWarning when using astropy with Python 3 #4795

Closed

Description

@weaverba137

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.

Activity

  1. astrofrog commented on Apr 20, 2016

    @astrofrog
    Member

    Could this be related to #4786?

  2. weaverba137 commented on Apr 20, 2016

    @weaverba137
    MemberAuthor

    Possibly, but in this case, the warning applies to the file astropy/extern/bundled/six.py, not to the FITS file.

  3. saimn commented on Apr 20, 2016

    @saimn
    Contributor

    The message <_io.TextIOWrapper name='.../astropy/extern/bundled/six.py' mode='r' encoding='utf-8'> is a bit weird. What does your test ?

  4. weaverba137 commented on Apr 20, 2016

    @weaverba137
    MemberAuthor

    @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.

  5. saimn commented on Apr 20, 2016

    @saimn
    Contributor

    @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.

  6. weaverba137 commented on Apr 20, 2016

    @weaverba137
    MemberAuthor

    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

  7. pllim commented on Apr 20, 2016

    @pllim
    Member

    If you use Python warnings filter to turn any ResourceWarning into exception, theoretically you would get a "nice" traceback.

  8. weaverba137 commented on Apr 20, 2016

    @weaverba137
    MemberAuthor

    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'>
    
  9. saimn commented on Apr 20, 2016

    @saimn
    Contributor

    @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.

  10. pllim commented on Apr 20, 2016

    @pllim
    Member

    Oh, didn't realize the filter does not work on this particular warning. Sorry for the noise.

  11. weaverba137 commented on Apr 20, 2016

    @weaverba137
    MemberAuthor

    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.

    ======================================================================
    FAIL: test_process_file (desidatamodel.test.test_stub.TestStub)
    Full test of parsing a FITS file.
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/Users/weaver/Documents/Code/git/desihub/desidatamodel/py/desidatamodel/test/test_stub.py", line 169, in test_process_file
        self.assertFalse(w and str(w[-1]))
    AssertionError: '{message : 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\'>",), category : \'ResourceWarning\', filename : \'/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/astropy/extern/bundled/six.py\', lineno : 60, line : None}' is not false
    
    ----------------------------------------------------------------------
    
  12. pllim commented on Apr 20, 2016

    @pllim
    Member

    That happens to be the last line in six.py:

    _import_six()

    Not very deterministic.

  13. pllim commented on Apr 20, 2016

    @pllim
    Member

    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.

  14. saimn commented on Apr 20, 2016

    @saimn
    Contributor

    Actually, after looking at the history of astropy/extern/bundled/six.py, it look more similar to this old issue: #3156

  15. pllim commented on Apr 21, 2016

    @pllim
    Member

    Theoretically, #3156 should have fixed it but I guess this must be an edge case? Hmm.

  16. saimn commented on May 1, 2018

    @saimn
    Contributor

    This one can probably be closed since six was removed from the code base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions