Skip to content

Commit 0b0cac5

Browse files
authored
CLN: memory_map do not silently fail (#44777)
1 parent 7669497 commit 0b0cac5

File tree

5 files changed

+26
-48
lines changed

5 files changed

+26
-48
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ I/O
748748
- Bug in :func:`read_csv` raising ``AttributeError`` when attempting to read a .csv file and infer index column dtype from an nullable integer type (:issue:`44079`)
749749
- :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` with ``compression`` set to ``'zip'`` no longer create a zip file containing a file ending with ".zip". Instead, they try to infer the inner file name more smartly. (:issue:`39465`)
750750
- Bug in :func:`read_csv` when passing simultaneously a parser in ``date_parser`` and ``parse_dates=False``, the parsing was still called (:issue:`44366`)
751+
- Bug in :func:`read_csv` silently ignoring errors when failling to create a memory-mapped file (:issue:`44766`)
752+
-
751753

752754
Period
753755
^^^^^^

pandas/io/common.py

+9-20
Original file line numberDiff line numberDiff line change
@@ -1040,26 +1040,15 @@ def _maybe_memory_map(
10401040
handle = open(handle, mode)
10411041
handles.append(handle)
10421042

1043-
try:
1044-
# error: Argument 1 to "_MMapWrapper" has incompatible type "Union[IO[Any],
1045-
# RawIOBase, BufferedIOBase, TextIOBase, mmap]"; expected "IO[Any]"
1046-
wrapped = cast(
1047-
BaseBuffer,
1048-
_MMapWrapper(handle, encoding, errors, decode), # type: ignore[arg-type]
1049-
)
1050-
# error: "BaseBuffer" has no attribute "close"
1051-
handle.close() # type: ignore[attr-defined]
1052-
handles.remove(handle)
1053-
handles.append(wrapped)
1054-
handle = wrapped
1055-
except Exception:
1056-
# we catch any errors that may have occurred
1057-
# because that is consistent with the lower-level
1058-
# functionality of the C engine (pd.read_csv), so
1059-
# leave the file handler as is then
1060-
memory_map = False
1061-
1062-
return handle, memory_map, handles
1043+
# error: Argument 1 to "_MMapWrapper" has incompatible type "Union[IO[Any],
1044+
# RawIOBase, BufferedIOBase, TextIOBase, mmap]"; expected "IO[Any]"
1045+
wrapped = cast(
1046+
BaseBuffer,
1047+
_MMapWrapper(handle, encoding, errors, decode), # type: ignore[arg-type]
1048+
)
1049+
handles.append(wrapped)
1050+
1051+
return wrapped, memory_map, handles
10631052

10641053

10651054
def file_exists(filepath_or_buffer: FilePath | BaseBuffer) -> bool:

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

-19
Original file line numberDiff line numberDiff line change
@@ -347,25 +347,6 @@ def test_read_csv_file_handle(all_parsers, io_class, encoding):
347347
assert not handle.closed
348348

349349

350-
def test_memory_map_file_handle_silent_fallback(all_parsers, compression):
351-
"""
352-
Do not fail for buffers with memory_map=True (cannot memory map BytesIO).
353-
354-
GH 37621
355-
"""
356-
parser = all_parsers
357-
expected = DataFrame({"a": [1], "b": [2]})
358-
359-
handle = BytesIO()
360-
expected.to_csv(handle, index=False, compression=compression, mode="wb")
361-
handle.seek(0)
362-
363-
tm.assert_frame_equal(
364-
parser.read_csv(handle, memory_map=True, compression=compression),
365-
expected,
366-
)
367-
368-
369350
def test_memory_map_compression(all_parsers, compression):
370351
"""
371352
Support memory map for compressed files.

pandas/tests/io/parser/test_read_fwf.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -699,15 +699,15 @@ def test_encoding_mmap(memory_map):
699699
GH 23254.
700700
"""
701701
encoding = "iso8859_1"
702-
data = BytesIO(" 1 A Ä 2\n".encode(encoding))
703-
df = read_fwf(
704-
data,
705-
header=None,
706-
widths=[2, 2, 2, 2],
707-
encoding=encoding,
708-
memory_map=memory_map,
709-
)
710-
data.seek(0)
702+
with tm.ensure_clean() as path:
703+
Path(path).write_bytes(" 1 A Ä 2\n".encode(encoding))
704+
df = read_fwf(
705+
path,
706+
header=None,
707+
widths=[2, 2, 2, 2],
708+
encoding=encoding,
709+
memory_map=memory_map,
710+
)
711711
df_reference = DataFrame([[1, "A", "Ä", 2]])
712712
tm.assert_frame_equal(df, df_reference)
713713

pandas/tests/io/test_common.py

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from io import (
88
BytesIO,
99
StringIO,
10+
UnsupportedOperation,
1011
)
1112
import mmap
1213
import os
@@ -602,3 +603,8 @@ def test_errno_attribute():
602603
with pytest.raises(FileNotFoundError, match="\\[Errno 2\\]") as err:
603604
pd.read_csv("doesnt_exist")
604605
assert err.errno == errno.ENOENT
606+
607+
608+
def test_fail_mmap():
609+
with pytest.raises(UnsupportedOperation, match="fileno"):
610+
icom.get_handle(BytesIO(), "rb", memory_map=True)

0 commit comments

Comments
 (0)