Skip to content

Commit 541df17

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

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

pandas/io/packers.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
from pandas.core.sparse.array import BlockIndex, IntIndex
6262
from pandas.core.generic import NDFrame
6363
from pandas.errors import PerformanceWarning
64-
from pandas.io.common import get_filepath_or_buffer, _stringify_path
64+
from pandas.io.common import (get_filepath_or_buffer, _get_handle,
65+
_stringify_path)
6566
from pandas.core.internals import BlockManager, make_block, _safe_reshape
6667
import pandas.core.internals as internals
6768

@@ -191,34 +192,25 @@ def read(fh):
191192
return l[0]
192193
return l
193194

194-
# 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)
198+
199+
# if the filepath is too long will raise here
200+
# 5874
199201
except (TypeError, ValueError):
200202
exists = False
201203

202204
if exists:
203205
with open(path_or_buf, 'rb') as fh:
204206
return read(fh)
205-
206-
# treat as a binary-like
207-
if isinstance(path_or_buf, compat.binary_type):
208-
fh = None
209-
try:
210-
fh = compat.BytesIO(path_or_buf)
211-
return read(fh)
212-
finally:
213-
if fh is not None:
214-
fh.close()
215-
216-
# a buffer like
217-
if hasattr(path_or_buf, 'read') and compat.callable(path_or_buf.read):
207+
elif hasattr(path_or_buf, 'read'):
208+
return read(path_or_buf)
209+
else:
210+
raise ValueError('path_or_buf needs to be a string file path or file-like')
211+
else:
218212
return read(path_or_buf)
219213

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

223215
dtype_dict = {21: np.dtype('M8[ns]'),
224216
u('datetime64[ns]'): np.dtype('M8[ns]'),

0 commit comments

Comments
 (0)