Skip to content

Commit f97c2af

Browse files
authored
BUG: close file handles in mmap (#35748)
1 parent 8f0d038 commit f97c2af

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

pandas/io/common.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Optional,
1919
Tuple,
2020
Type,
21+
Union,
2122
)
2223
from urllib.parse import (
2324
urljoin,
@@ -452,7 +453,7 @@ def get_handle(
452453
except ImportError:
453454
need_text_wrapping = (BufferedIOBase, RawIOBase)
454455

455-
handles: List[IO] = list()
456+
handles: List[Union[IO, _MMapWrapper]] = list()
456457
f = path_or_buf
457458

458459
# Convert pathlib.Path/py.path.local or string
@@ -535,6 +536,8 @@ def get_handle(
535536
try:
536537
wrapped = _MMapWrapper(f)
537538
f.close()
539+
handles.remove(f)
540+
handles.append(wrapped)
538541
f = wrapped
539542
except Exception:
540543
# we catch any errors that may have occurred

pandas/tests/io/parser/test_common.py

+1
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,7 @@ def test_raise_on_no_columns(all_parsers, nrows):
18361836
parser.read_csv(StringIO(data))
18371837

18381838

1839+
@td.check_file_leaks
18391840
def test_memory_map(all_parsers, csv_dir_path):
18401841
mmap_file = os.path.join(csv_dir_path, "test_mmap.csv")
18411842
parser = all_parsers

0 commit comments

Comments
 (0)