Skip to content

Commit 92eb7b2

Browse files
twoertweinluckyvs1
authored andcommitted
BUG: read_csv does not close file during an error in _make_reader (pandas-dev#39029)
1 parent f443aac commit 92eb7b2

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

doc/source/whatsnew/v1.2.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Bug fixes
4040
~~~~~~~~~
4141

4242
- Bug in :meth:`read_csv` with ``float_precision="high"`` caused segfault or wrong parsing of long exponent strings. This resulted in a regression in some cases as the default for ``float_precision`` was changed in pandas 1.2.0 (:issue:`38753`)
43-
-
43+
- Bug in :func:`read_csv` not closing an opened file handle when a ``csv.Error`` or ``UnicodeDecodeError`` occurred while initializing (:issue:`39024`)
4444
-
4545

4646
.. ---------------------------------------------------------------------------

pandas/io/parsers.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,11 @@ def __init__(self, f: Union[FilePathOrBuffer, List], **kwds):
22972297
self._open_handles(f, kwds)
22982298
assert self.handles is not None
22992299
assert hasattr(self.handles.handle, "readline")
2300-
self._make_reader(self.handles.handle)
2300+
try:
2301+
self._make_reader(self.handles.handle)
2302+
except (csv.Error, UnicodeDecodeError):
2303+
self.close()
2304+
raise
23012305

23022306
# Get columns in two steps: infer from data, then
23032307
# infer column indices from self.usecols if it is specified.

pandas/tests/io/parser/common/test_read_errors.py

+23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
specific classification into the other test modules.
44
"""
55
import codecs
6+
import csv
67
from io import StringIO
78
import os
9+
from pathlib import Path
10+
import warnings
811

912
import numpy as np
1013
import pytest
1114

1215
from pandas.errors import EmptyDataError, ParserError
16+
import pandas.util._test_decorators as td
1317

1418
from pandas import DataFrame
1519
import pandas._testing as tm
@@ -208,3 +212,22 @@ def test_null_byte_char(all_parsers):
208212
msg = "NULL byte detected"
209213
with pytest.raises(ParserError, match=msg):
210214
parser.read_csv(StringIO(data), names=names)
215+
216+
217+
@td.check_file_leaks
218+
def test_open_file(all_parsers):
219+
# GH 39024
220+
parser = all_parsers
221+
if parser.engine == "c":
222+
pytest.skip()
223+
224+
with tm.ensure_clean() as path:
225+
file = Path(path)
226+
file.write_bytes(b"\xe4\na\n1")
227+
228+
# should not trigger a ResourceWarning
229+
warnings.simplefilter("always", category=ResourceWarning)
230+
with warnings.catch_warnings(record=True) as record:
231+
with pytest.raises(csv.Error, match="Could not determine delimiter"):
232+
parser.read_csv(file, sep=None)
233+
assert len(record) == 0, record[0].message

0 commit comments

Comments
 (0)