Skip to content

Commit c4b43cc

Browse files
authored
REGR: SpooledTemporaryFile support in read_csv (#43447)
1 parent c03f49c commit c4b43cc

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Fixed regressions
2929
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
3030
- Fixed regression in :meth:`DataFrame.groupby` where aggregation on columns with object types dropped results on those columns (:issue:`42395`, :issue:`43108`)
3131
- Fixed regression in :meth:`Series.fillna` raising ``TypeError`` when filling ``float`` ``Series`` with list-like fill value having a dtype which couldn't cast lostlessly (like ``float32`` filled with ``float64``) (:issue:`43424`)
32+
- Fixed regression in :func:`read_csv` throwing an ``AttributeError`` when the file handle is an ``tempfile.SpooledTemporaryFile`` object (:issue:`43439`)
3233
-
3334

3435
.. ---------------------------------------------------------------------------

pandas/io/common.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818
import mmap
1919
import os
20+
import tempfile
2021
from typing import (
2122
IO,
2223
Any,
@@ -993,8 +994,15 @@ def _is_binary_mode(handle: FilePathOrBuffer, mode: str) -> bool:
993994
if "t" in mode or "b" in mode:
994995
return "b" in mode
995996

996-
# classes that expect string but have 'b' in mode
997-
text_classes = (codecs.StreamWriter, codecs.StreamReader, codecs.StreamReaderWriter)
997+
# exceptions
998+
text_classes = (
999+
# classes that expect string but have 'b' in mode
1000+
codecs.StreamWriter,
1001+
codecs.StreamReader,
1002+
codecs.StreamReaderWriter,
1003+
# cannot be wrapped in TextIOWrapper GH43439
1004+
tempfile.SpooledTemporaryFile,
1005+
)
9981006
if issubclass(type(handle), text_classes):
9991007
return False
10001008

pandas/tests/io/parser/test_encoding.py

+13
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,16 @@ def test_encoding_memory_map(all_parsers, encoding):
250250
expected.to_csv(file, index=False, encoding=encoding)
251251
df = parser.read_csv(file, encoding=encoding, memory_map=True)
252252
tm.assert_frame_equal(df, expected)
253+
254+
255+
def test_not_readable(all_parsers):
256+
# GH43439
257+
parser = all_parsers
258+
if parser.engine in ("python", "pyarrow"):
259+
pytest.skip("SpooledTemporaryFile does only work with the c-engine")
260+
with tempfile.SpooledTemporaryFile() as handle:
261+
handle.write(b"abcd")
262+
handle.seek(0)
263+
df = parser.read_csv(handle)
264+
expected = DataFrame([], columns=["abcd"])
265+
tm.assert_frame_equal(df, expected)

0 commit comments

Comments
 (0)