Support unpacking new types.

This commit is contained in:
INADA Naoki 2013-10-20 22:59:27 +09:00
parent 96bcd76f49
commit 822cce823c
4 changed files with 17 additions and 11 deletions

View file

@ -157,7 +157,7 @@ static inline int unpack_callback_array_item(unpack_user* u, unsigned int curren
static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
{
if (u->list_hook) {
PyObject *new_c = PyEval_CallFunction(u->list_hook, "(O)", *c);
PyObject *new_c = PyObject_CallFunction(u->list_hook, "(O)", *c);
if (!new_c)
return -1;
Py_DECREF(*c);
@ -203,7 +203,7 @@ static inline int unpack_callback_map_item(unpack_user* u, unsigned int current,
static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
{
if (u->object_hook) {
PyObject *new_c = PyEval_CallFunction(u->object_hook, "(O)", *c);
PyObject *new_c = PyObject_CallFunction(u->object_hook, "(O)", *c);
if (!new_c)
return -1;
@ -246,7 +246,11 @@ static inline int unpack_callback_ext(unpack_user* u, const char* base, const ch
return -1;
}
// length also includes the typecode, so the actual data is lenght-1
py = PyEval_CallFunction(u->ext_hook, "(is#)", typecode, pos, lenght-1);
#if PY_MAJOR_VERSION == 2
py = PyObject_CallFunction(u->ext_hook, "(is#)", typecode, pos, lenght-1);
#else
py = PyObject_CallFunction(u->ext_hook, "(iy#)", typecode, pos, lenght-1);
#endif
if (!py)
return -1;
*o = py;

View file

@ -93,4 +93,3 @@ typedef enum {
#endif
#endif /* msgpack/unpack_define.h */

View file

@ -178,15 +178,17 @@ static inline int unpack_execute(unpack_context* ctx, const char* data, size_t l
switch(*p) {
case 0xc0: // nil
push_simple_value(_nil);
//case 0xc1: // string
// again_terminal_trail(NEXT_CS(p), p+1);
//case 0xc1: // never used
case 0xc2: // false
push_simple_value(_false);
case 0xc3: // true
push_simple_value(_true);
//case 0xc4:
//case 0xc5:
//case 0xc6:
case 0xc4: // bin 8
again_fixed_trail(NEXT_CS(p), 1);
case 0xc5: // bin 16
again_fixed_trail(NEXT_CS(p), 2);
case 0xc6: // bin 32
again_fixed_trail(NEXT_CS(p), 4);
case 0xc7: // ext 8
again_fixed_trail(NEXT_CS(p), 1);
case 0xc8: // ext 16
@ -213,7 +215,8 @@ static inline int unpack_execute(unpack_context* ctx, const char* data, size_t l
_ext_zero);
case 0xd8: // fixext 16
again_fixed_trail_if_zero(ACS_EXT_VALUE, 16+1, _ext_zero);
//case 0xd9:
case 0xd9: // str 8
again_fixed_trail(NEXT_CS(p), 1);
case 0xda: // raw 16
case 0xdb: // raw 32
case 0xdc: // array 16

View file

@ -35,7 +35,7 @@ def test_only_one_obj_hook():
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
def test_bad_hook():
with raises(ValueError):
with raises(TypeError):
packed = packb([3, 1+2j], default=lambda o: o)
unpacked = unpackb(packed, use_list=1)