Skip to content

Commit 02b041c

Browse files
committed
BUG: Fix passing non-existant file to read_msgpack pandas-dev#15296
1 parent 06a70b1 commit 02b041c

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

doc/source/whatsnew/v0.22.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Documentation Changes
8686
Bug Fixes
8787
~~~~~~~~~
8888

89+
- Bug in ``pd.read_msgpack()`` with a non existent file is passed in Python 2 (:issue:`15296`)
90+
8991
Conversion
9092
^^^^^^^^^^
9193

pandas/io/packers.py

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

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

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

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

0 commit comments

Comments
 (0)