Skip to content

Commit dd34eef

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

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pandas/io/packers.py

+11-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

@@ -186,39 +187,31 @@ def read_msgpack(path_or_buf, encoding='utf-8', iterator=False, **kwargs):
186187
return Iterator(path_or_buf)
187188

188189
def read(fh):
190+
print(fh)
189191
l = list(unpack(fh, encoding=encoding, **kwargs))
190192
if len(l) == 1:
191193
return l[0]
192194
return l
193195

194-
# see if we have an actual file
195196
if isinstance(path_or_buf, compat.string_types):
196-
197197
try:
198198
exists = os.path.exists(path_or_buf)
199+
200+
# if the filepath is too long will raise here
201+
# 5874
199202
except (TypeError, ValueError):
200203
exists = False
201204

202205
if exists:
203206
with open(path_or_buf, 'rb') as fh:
204207
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):
208+
elif hasattr(path_or_buf, 'read'):
209+
return read(path_or_buf)
210+
else:
211+
raise ValueError('path_or_buf needs to be a string file path or file-like')
212+
else:
218213
return read(path_or_buf)
219214

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

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

0 commit comments

Comments
 (0)