Skip to content

BLD/IO: Elusive Travis ResourceWarning & closing files by default  #22675

Closed
@mroeschke

Description

@mroeschke

Every once in a while Travis will raise a ResourceWarning

____________________ TestPythonParser.test_no_header_prefix ____________________
[gw0] darwin -- Python 3.5.6 /Users/travis/miniconda3/envs/pandas/bin/python
self = <pandas.tests.io.parser.test_parsers.TestPythonParser object at 0x11d030da0>
    def test_no_header_prefix(self):
        data = """1,2,3,4,5
    6,7,8,9,10
    11,12,13,14,15
    """
        df_pref = self.read_table(StringIO(data), sep=',', prefix='Field'
>                                 header=None)
pandas/tests/io/parser/header.py:48:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pandas/tests/io/parser/test_parsers.py:111: in read_table
    df = read_table(*args, **kwds)
../../../miniconda3/envs/pandas/lib/python3.5/contextlib.py:66: in __exit__
    next(self.gen)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
E           AssertionError: Caused unexpected warning(s): [('ResourceWarning', ResourceWarning("unclosed file <_io.BufferedReader name='/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/tmpp8reunuatest_file.zip'>",), '/Users/travis/miniconda3/envs/pandas/lib/python3.5/site-packages/py/_vendored_packages/apipkg.py', 146)].

And currently our file closing mechanism for parser functions is like so:

pandas/pandas/io/parsers.py

Lines 459 to 463 in 73dd6ec

if should_close:
try:
filepath_or_buffer.close()
except: # noqa: flake8
pass

It seems pointless having a generic try/except within an if statement (why have the if statement at all then?). More broadly, should we attempt to close all file-like objects anyways even if they are not necessarily needed (like StringIO in this Travis case)? Might help alleviate this Travis error.

Some examples:

Metadata

Metadata

Assignees

No one assigned

    Labels

    CIContinuous IntegrationUnreliable TestUnit tests that occasionally fail

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions