summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWouter Bolsterlee <uws@xs4all.nl>2014-02-11 20:34:23 +0100
committerWouter Bolsterlee <uws@xs4all.nl>2014-02-11 20:42:58 +0100
commit77046b839da20feb528d303c8858ab6f34013ce7 (patch)
tree1ee52a06c697ba160101a756bbf6a5f55af7c26c
parentd5436c28197f3ccdd473e1c54838133574948dab (diff)
downloadmsgpack-python-77046b839da20feb528d303c8858ab6f34013ce7.tar.gz
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).
-rw-r--r--msgpack/_unpacker.pyx4
-rw-r--r--msgpack/fallback.py14
-rw-r--r--test/test_obj.py2
3 files changed, 10 insertions, 10 deletions
diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx
index d5aa46e..18592f4 100644
--- a/msgpack/_unpacker.pyx
+++ b/msgpack/_unpacker.pyx
@@ -52,7 +52,7 @@ cdef inline init_ctx(unpack_context *ctx,
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
if object_hook is not None and object_pairs_hook is not None:
- raise ValueError("object_pairs_hook and object_hook are mutually exclusive.")
+ raise TypeError("object_pairs_hook and object_hook are mutually exclusive.")
if object_hook is not None:
if not PyCallable_Check(object_hook):
@@ -227,7 +227,7 @@ cdef class Unpacker(object):
if file_like:
self.file_like_read = file_like.read
if not PyCallable_Check(self.file_like_read):
- raise ValueError("`file_like.read` must be a callable.")
+ raise TypeError("`file_like.read` must be a callable.")
if not max_buffer_size:
max_buffer_size = INT_MAX
if read_size > max_buffer_size:
diff --git a/msgpack/fallback.py b/msgpack/fallback.py
index bf5b1c2..cf61023 100644
--- a/msgpack/fallback.py
+++ b/msgpack/fallback.py
@@ -159,7 +159,7 @@ class Unpacker(object):
self._fb_feeding = True
else:
if not callable(file_like.read):
- raise ValueError("`file_like.read` must be callable")
+ raise TypeError("`file_like.read` must be callable")
self.file_like = file_like
self._fb_feeding = False
self._fb_buffers = []
@@ -179,16 +179,16 @@ class Unpacker(object):
self._ext_hook = ext_hook
if list_hook is not None and not callable(list_hook):
- raise ValueError('`list_hook` is not callable')
+ raise TypeError('`list_hook` is not callable')
if object_hook is not None and not callable(object_hook):
- raise ValueError('`object_hook` is not callable')
+ raise TypeError('`object_hook` is not callable')
if object_pairs_hook is not None and not callable(object_pairs_hook):
- raise ValueError('`object_pairs_hook` is not callable')
+ raise TypeError('`object_pairs_hook` is not callable')
if object_hook is not None and object_pairs_hook is not None:
- raise ValueError("object_pairs_hook and object_hook are mutually "
- "exclusive")
+ raise TypeError("object_pairs_hook and object_hook are mutually "
+ "exclusive")
if not callable(ext_hook):
- raise ValueError("`ext_hook` is not callable")
+ raise TypeError("`ext_hook` is not callable")
def feed(self, next_bytes):
if isinstance(next_bytes, array.array):
diff --git a/test/test_obj.py b/test/test_obj.py
index 9083218..390c1b6 100644
--- a/test/test_obj.py
+++ b/test/test_obj.py
@@ -31,7 +31,7 @@ def test_decode_pairs_hook():
assert unpacked[1] == prod_sum
def test_only_one_obj_hook():
- with raises(ValueError):
+ with raises(TypeError):
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
def test_bad_hook():