Skip to content

Commit 76659e5

Browse files
committed
BUG: Fix passing non-existant file to read_msgpack pandas-dev#15296
1 parent 94a86f2 commit 76659e5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Bug Fixes
3939

4040
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
4141
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
42+
- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
4243

4344

4445

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)