Skip to content

Commit 36a478f

Browse files
committed
BUG: Fix passing non-existant file to read_msgpack pandas-dev#15296
1 parent 7511388 commit 36a478f

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ I/O
346346
- Bug in :func:`read_csv` when called with a single-element list ``header`` would return a ``DataFrame`` of all NaN values (:issue:`7757`)
347347
- Bug in :func:`read_stata` where value labels could not be read when using an iterator (:issue:`16923`)
348348
- Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`)
349+
- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
349350

350351
Plotting
351352
^^^^^^^^

pandas/io/packers.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def read(fh):
193193

194194
# see if we have an actual file
195195
if isinstance(path_or_buf, compat.string_types):
196-
197196
try:
198197
exists = os.path.exists(path_or_buf)
199198
except (TypeError, ValueError):
@@ -203,18 +202,21 @@ def read(fh):
203202
with open(path_or_buf, 'rb') as fh:
204203
return read(fh)
205204

206-
# treat as a binary-like
207205
if isinstance(path_or_buf, compat.binary_type):
206+
# treat as a binary-like
208207
fh = None
209208
try:
210-
fh = compat.BytesIO(path_or_buf)
211-
return read(fh)
209+
# We can't distinguish between a path and a buffer of bytes in
210+
# Python 2 so instead assume the first byte of a valid path is
211+
# less than 0x80.
212+
if compat.PY3 or ord(path_or_buf[0]) >= 0x80:
213+
fh = compat.BytesIO(path_or_buf)
214+
return read(fh)
212215
finally:
213216
if fh is not None:
214217
fh.close()
215-
216-
# a buffer like
217-
if hasattr(path_or_buf, 'read') and compat.callable(path_or_buf.read):
218+
elif hasattr(path_or_buf, 'read') and compat.callable(path_or_buf.read):
219+
# treat as a buffer like
218220
return read(path_or_buf)
219221

220222
raise ValueError('path_or_buf needs to be a string file path or file-like')

0 commit comments

Comments
 (0)