Skip to content

Backport PR #40994: REGR: memory_map with non-UTF8 encoding #41257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Regression in :func:`concat` between two :class:`DataFrames` where one has an :class:`Index` that is all-None and the other is :class:`DatetimeIndex` incorrectly raising (:issue:`40841`)
-
- Regression in :func:`read_csv` when using ``memory_map=True`` with an non-UTF8 encoding (:issue:`40986`)
-

.. ---------------------------------------------------------------------------
Expand Down
42 changes: 37 additions & 5 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,12 @@ def get_handle(

# memory mapping needs to be the first step
handle, memory_map, handles = _maybe_memory_map(
handle, memory_map, ioargs.encoding, ioargs.mode, errors
handle,
memory_map,
ioargs.encoding,
ioargs.mode,
errors,
ioargs.compression["method"] not in _compression_to_extension,
)

is_path = isinstance(handle, str)
Expand Down Expand Up @@ -759,7 +764,18 @@ class _MMapWrapper(abc.Iterator):
"""

def __init__(self, f: IO):
def __init__(
self,
f: IO,
encoding: str = "utf-8",
errors: str = "strict",
decode: bool = True,
):
self.encoding = encoding
self.errors = errors
self.decoder = codecs.getincrementaldecoder(encoding)(errors=errors)
self.decode = decode

self.attributes = {}
for attribute in ("seekable", "readable", "writeable"):
if not hasattr(f, attribute):
Expand All @@ -775,19 +791,31 @@ def __getattr__(self, name: str):
def __iter__(self) -> "_MMapWrapper":
return self

def read(self, size: int = -1) -> Union[str, bytes]:
# CSV c-engine uses read instead of iterating
content: bytes = self.mmap.read(size)
if self.decode:
errors = self.errors if self.errors is not None else "strict"
# memory mapping is applied before compression. Encoding should
# be applied to the de-compressed data.
return content.decode(self.encoding, errors=errors)
return content

def __next__(self) -> str:
newbytes = self.mmap.readline()

# readline returns bytes, not str, but Python's CSV reader
# expects str, so convert the output to str before continuing
newline = newbytes.decode("utf-8")
newline = self.decoder.decode(newbytes)

# mmap doesn't raise if reading past the allocated
# data but instead returns an empty string, so raise
# if that is returned
if newline == "":
raise StopIteration
return newline

# IncrementalDecoder seems to push newline to the next line
return newline.lstrip("\n")


def _maybe_memory_map(
Expand All @@ -796,6 +824,7 @@ def _maybe_memory_map(
encoding: str,
mode: str,
errors: Optional[str],
decode: bool,
) -> Tuple[FileOrBuffer, bool, List[Buffer]]:
"""Try to memory map file/buffer."""
handles: List[Buffer] = []
Expand All @@ -814,7 +843,10 @@ def _maybe_memory_map(
handles.append(handle)

try:
wrapped = cast(mmap.mmap, _MMapWrapper(handle)) # type: ignore[arg-type]
wrapped = cast(
mmap.mmap,
_MMapWrapper(handle, encoding, errors, decode), # type: ignore[arg-type]
)
handle.close()
handles.remove(handle)
handles.append(wrapped)
Expand Down
25 changes: 0 additions & 25 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1868,31 +1868,6 @@ def __init__(self, src: FilePathOrBuffer, **kwds):
assert self.handles is not None
for key in ("storage_options", "encoding", "memory_map", "compression"):
kwds.pop(key, None)
if self.handles.is_mmap and hasattr(self.handles.handle, "mmap"):
# pandas\io\parsers.py:1861: error: Item "IO[Any]" of
# "Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]" has no attribute "mmap" [union-attr]

# pandas\io\parsers.py:1861: error: Item "RawIOBase" of
# "Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]" has no attribute "mmap" [union-attr]

# pandas\io\parsers.py:1861: error: Item "BufferedIOBase" of
# "Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]" has no attribute "mmap" [union-attr]

# pandas\io\parsers.py:1861: error: Item "TextIOBase" of
# "Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]" has no attribute "mmap" [union-attr]

# pandas\io\parsers.py:1861: error: Item "TextIOWrapper" of
# "Union[IO[Any], RawIOBase, BufferedIOBase, TextIOBase,
# TextIOWrapper, mmap]" has no attribute "mmap" [union-attr]

# pandas\io\parsers.py:1861: error: Item "mmap" of "Union[IO[Any],
# RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap]" has
# no attribute "mmap" [union-attr]
self.handles.handle = self.handles.handle.mmap # type: ignore[union-attr]

try:
self._reader = parsers.TextReader(self.handles.handle, **kwds)
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/io/parser/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,20 @@ def test_parse_encoded_special_characters(encoding):

expected = DataFrame(data=[[":foo", 0], ["bar", 1], ["baz", 2]], columns=["a", "b"])
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("encoding", ["utf-8", None, "utf-16", "cp1255", "latin-1"])
def test_encoding_memory_map(all_parsers, encoding):
# GH40986
parser = all_parsers
expected = DataFrame(
{
"name": ["Raphael", "Donatello", "Miguel Angel", "Leonardo"],
"mask": ["red", "purple", "orange", "blue"],
"weapon": ["sai", "bo staff", "nunchunk", "katana"],
}
)
with tm.ensure_clean() as file:
expected.to_csv(file, index=False, encoding=encoding)
df = parser.read_csv(file, encoding=encoding, memory_map=True)
tm.assert_frame_equal(df, expected)