8000 Always raise TypeError for wrong argument types · popravich/msgpack-python@77046b8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 77046b8

Browse files
committed
Always raise TypeError for wrong argument types
The code that checks whether hooks are callable() (and some other type checks) should always raise TypeError on failure. Before this change, both ValueError and TypeError were used in an inconsistent way (C extension and Python implementation were not the same).
1 parent d5436c2 commit 77046b8

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

msgpack/_unpacker.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ cdef inline init_ctx(unpack_context *ctx,
5252
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
5353

5454
if object_hook is not None and object_pairs_hook is not None:
55-
raise ValueError("object_pairs_hook and object_hook are mutually exclusive.")
55+
raise TypeError("object_pairs_hook and object_hook are mutually exclusive.")
5656

5757
if object_hook is not None:
5858
if not PyCallable_Check(object_hook):
@@ -227,7 +227,7 @@ cdef class Unpacker(object):
227227
if file_like:
228228
self.file_like_read = file_like.read
229229
if not PyCallable_Check(self.file_like_read):
230-
raise ValueError("`file_like.read` must be a callable.")
230+
raise TypeError("`file_like.read` must be a callable.")
231231
if not max_buffer_size:
232232
max_buffer_size = INT_MAX
233233
if read_size > max_buffer_size:

msgpack/fallback.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def __init__(self, file_like=None, read_size=0, use_list=True,
159159
self._fb_feeding = True
160160
else:
161161
if not callable(file_like.read):
162-
raise ValueError("`file_like.read` must be callable")
162+
raise TypeError("`file_like.read` must be callable")
163163
self.file_like = file_like
164164
self._fb_feeding = False
165165
self._fb_buffers = []
@@ -179,16 +179,16 @@ def __init__(self, file_like=None, read_size=0, use_list=True,
179179
self._ext_hook = ext_hook
180180

181181
if list_hook is not None and not callable(list_hook):
182-
raise ValueError('`list_hook` is not callable')
182+
raise TypeError('`list_hook` is not callable')
183183
if object_hook is not None and not callable(object_hook):
184-
raise ValueError('`object_hook` is not callable')
184+
raise TypeError('`object_hook` is not callable')
185185
if object_pairs_hook is not None and not callable(object_pairs_hook):
186-
raise ValueError('`object_pairs_hook` is not callable')
186+
raise TypeError('`object_pairs_hook` is not callable')
187187
if object_hook is not None and object_pairs_hook is not None:
188-
raise ValueError("object_pairs_hook and object_hook are mutually "
189-
"exclusive")
188+
raise TypeError("object_pairs_hook and object_hook are mutually "
189+
"exclusive")
190190
if not callable(ext_hook):
191-
raise ValueError("`ext_hook` is not callable")
191+
raise TypeError("`ext_hook` is not callable")
192192

193193
def feed(self, next_bytes):
194194
if isinstance(next_bytes, array.array):

test/test_obj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_decode_pairs_hook():
3131
assert unpacked[1] == prod_sum
3232

3333
def test_only_one_obj_hook():
34-
with raises(ValueError):
34+
with raises(TypeError):
3535
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
3636

3737
def test_bad_hook():

0 commit comments

Comments
 (0)
0