Skip to content

Commit 2344ff2

Browse files
committed
TST/ERR: GH10369 read_msgpack checks argument type
1 parent 9ed13a1 commit 2344ff2

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,4 @@ Bug Fixes
131131
- Bug in ``DatetimeIndex`` and ``PeriodIndex.value_counts`` resets name from its result, but retains in result's ``Index``. (:issue:`10150`)
132132

133133
- Bug in `pandas.concat` with ``axis=0`` when column is of dtype ``category`` (:issue:`10177`)
134+
- Bug in ``read_msgpack`` where input type is not always checked (:issue:`10369`)

pandas/io/packers.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -141,24 +141,28 @@ def read(fh):
141141

142142
try:
143143
exists = os.path.exists(path_or_buf)
144-
except (TypeError,ValueError):
144+
except (TypeError, ValueError):
145145
exists = False
146146

147147
if exists:
148148
with open(path_or_buf, 'rb') as fh:
149149
return read(fh)
150150

151-
# treat as a string-like
152-
if not hasattr(path_or_buf, 'read'):
153-
151+
# treat as a binary-like
152+
if isinstance(path_or_buf, compat.binary_type):
153+
fh = None
154154
try:
155155
fh = compat.BytesIO(path_or_buf)
156156
return read(fh)
157157
finally:
158-
fh.close()
158+
if fh is not None:
159+
fh.close()
159160

160161
# a buffer like
161-
return read(path_or_buf)
162+
if hasattr(path_or_buf, 'read') and compat.callable(path_or_buf.read):
163+
return read(path_or_buf)
164+
165+
raise ValueError('path_or_buf needs to be a string file path or file-like')
162166

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

pandas/io/tests/test_packers.py

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ def test_iterator_with_string_io(self):
9393
for i, result in enumerate(read_msgpack(s,iterator=True)):
9494
tm.assert_frame_equal(result,dfs[i])
9595

96+
def test_invalid_arg(self):
97+
#GH10369
98+
class A(object):
99+
def __init__(self):
100+
self.read = 0
101+
102+
tm.assertRaises(ValueError, read_msgpack, path_or_buf=None)
103+
tm.assertRaises(ValueError, read_msgpack, path_or_buf={})
104+
tm.assertRaises(ValueError, read_msgpack, path_or_buf=A())
105+
106+
96107
class TestNumpy(TestPackers):
97108

98109
def test_numpy_scalar_float(self):

0 commit comments

Comments
 (0)