Skip to content

Commit 2e114ce

Browse files
BUG: Fix file descriptor leak (#32598)
1 parent 7a24c35 commit 2e114ce

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ I/O
339339
- Bug in :meth:`read_csv` was raising `TypeError` when `sep=None` was used in combination with `comment` keyword (:issue:`31396`)
340340
- Bug in :class:`HDFStore` that caused it to set to ``int64`` the dtype of a ``datetime64`` column when reading a DataFrame in Python 3 from fixed format written in Python 2 (:issue:`31750`)
341341
- Bug in :meth:`read_excel` where a UTF-8 string with a high surrogate would cause a segmentation violation (:issue:`23809`)
342+
- Bug in :meth:`read_csv` was causing a file descriptor leak on an empty file (:issue:`31488`)
342343

343344

344345
Plotting

pandas/io/parsers.py

+9-5
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 (TypeError, ValueError):
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_common.py

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from pandas._libs.tslib import Timestamp
1717
from pandas.errors import DtypeWarning, EmptyDataError, ParserError
18+
import pandas.util._test_decorators as td
1819

1920
from pandas import DataFrame, Index, MultiIndex, Series, compat, concat
2021
import pandas._testing as tm
@@ -2079,3 +2080,16 @@ def test_integer_precision(all_parsers):
20792080
result = parser.read_csv(StringIO(s), header=None)[4]
20802081
expected = Series([4321583677327450765, 4321113141090630389], name=4)
20812082
tm.assert_series_equal(result, expected)
2083+
2084+
2085+
def test_file_descriptor_leak(all_parsers):
2086+
# GH 31488
2087+
2088+
parser = all_parsers
2089+
with tm.ensure_clean() as path:
2090+
2091+
def test():
2092+
with pytest.raises(EmptyDataError, match="No columns to parse from file"):
2093+
parser.read_csv(path)
2094+
2095+
td.check_file_leaks(test)()

0 commit comments

Comments
 (0)