File tree Expand file tree Collapse file tree 3 files changed +22
-6
lines changed Expand file tree Collapse file tree 3 files changed +22
-6
lines changed Original file line number Diff line number Diff 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`)
Original file line number Diff line number Diff 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
163167dtype_dict = {21 : np .dtype ('M8[ns]' ),
164168 u ('datetime64[ns]' ): np .dtype ('M8[ns]' ),
Original file line number Diff line number Diff 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+
96107class TestNumpy (TestPackers ):
97108
98109 def test_numpy_scalar_float (self ):
You can’t perform that action at this time.
0 commit comments