Skip to content

Commit 1cd0704

Browse files
Fix file descriptor leak
1 parent e734449 commit 1cd0704

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

pandas/io/parsers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,11 +2273,15 @@ def __init__(self, f, **kwds):
22732273
# Get columns in two steps: infer from data, then
22742274
# infer column indices from self.usecols if it is specified.
22752275
self._col_indices = None
2276-
(
2277-
self.columns,
2278-
self.num_original_columns,
2279-
self.unnamed_cols,
2280-
) = self._infer_columns()
2276+
try:
2277+
(
2278+
self.columns,
2279+
self.num_original_columns,
2280+
self.unnamed_cols,
2281+
) = self._infer_columns()
2282+
except EmptyDataError:
2283+
self.close()
2284+
raise
22812285

22822286
# Now self.columns has the set of columns that we will process.
22832287
# The original set is stored in self.original_columns.

pandas/tests/io/parser/test_python_parser_only.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
import pytest
1212

13-
from pandas.errors import ParserError
13+
from pandas.errors import ParserError, EmptyDataError
1414

1515
from pandas import DataFrame, Index, MultiIndex
1616
import pandas._testing as tm
1717

18+
import psutil
19+
1820

1921
def test_default_separator(python_parser_only):
2022
# see gh-17333
@@ -314,3 +316,15 @@ def test_malformed_skipfooter(python_parser_only):
314316
msg = "Expected 3 fields in line 4, saw 5"
315317
with pytest.raises(ParserError, match=msg):
316318
parser.read_csv(StringIO(data), header=1, comment="#", skipfooter=1)
319+
320+
321+
def test_file_descriptor_leak(python_parser_only):
322+
# GH 31488
323+
parser = python_parser_only
324+
with open("empty.csv", "w"):
325+
pass
326+
327+
proc = psutil.Process()
328+
with pytest.raises(EmptyDataError):
329+
parser.read_csv("empty.csv")
330+
assert not proc.open_files()

0 commit comments

Comments
 (0)