Skip to content

Commit 1fc68b2

Browse files
committed
Fix get_filepath_or_buffer and _get_handle
Remove `maybe_read_encoded_stream` function
1 parent dce054e commit 1fc68b2

File tree

1 file changed

+7
-40
lines changed

1 file changed

+7
-40
lines changed

pandas/io/common.py

+7-40
Original file line numberDiff line numberDiff line change
@@ -138,39 +138,6 @@ def _is_s3_url(url):
138138
return False
139139

140140

141-
def maybe_read_encoded_stream(reader, encoding=None, compression=None):
142-
"""
143-
Read an encoded stream from the reader and transform the bytes to unicode if
144-
required based on the encoding.
145-
146-
Parameters
147-
----------
148-
reader : a streamable file-like object
149-
encoding : optional, the encoding to attempt to read
150-
151-
Returns
152-
-------
153-
a tuple of (a stream of decoded bytes, the encoding which was used)
154-
"""
155-
156-
if compat.PY3 or encoding is not None: # pragma: no cover
157-
if encoding:
158-
errors = 'strict'
159-
else:
160-
errors = 'replace'
161-
encoding = 'utf-8'
162-
163-
if compression == 'gzip':
164-
reader = BytesIO(reader.read())
165-
else:
166-
reader = StringIO(reader.read().decode(encoding, errors))
167-
else:
168-
if compression == 'gzip':
169-
reader = BytesIO(reader.read())
170-
encoding = None
171-
return reader, encoding
172-
173-
174141
def _expand_user(filepath_or_buffer):
175142
"""Return the argument with an initial component of ~ or ~user
176143
replaced by that user's home directory.
@@ -240,7 +207,7 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
240207
if content_encoding == 'gzip':
241208
# Override compression based on Content-Encoding header
242209
compression = 'gzip'
243-
reader, encoding = maybe_read_encoded_stream(req, encoding, compression)
210+
reader = BytesIO(req.read())
244211
return reader, encoding, compression
245212

246213
if _is_s3_url(filepath_or_buffer):
@@ -294,12 +261,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
294261
f = path_or_buf
295262
is_path = isinstance(path_or_buf, compat.string_types)
296263

297-
# in Python 3, convert BytesIO or fileobjects passed with an encoding
298-
if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):
299-
from io import TextIOWrapper
300-
return TextIOWrapper(path_or_buf, encoding=encoding)
301-
302-
elif compression:
264+
if compression:
303265
compression = compression.lower()
304266

305267
if compat.PY2 and not is_path and encoding:
@@ -369,6 +331,11 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
369331
# Python 3 and no explicit encoding
370332
f = open(path_or_buf, mode, errors='replace')
371333

334+
# in Python 3, convert BytesIO or fileobjects passed with an encoding
335+
if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):
336+
from io import TextIOWrapper
337+
f = TextIOWrapper(f, encoding=encoding)
338+
372339
if memory_map and hasattr(f, 'fileno'):
373340
try:
374341
g = MMapWrapper(f)

0 commit comments

Comments
 (0)