Add tests and bugfix.

This commit is contained in:
INADA Naoki 2013-10-21 00:29:05 +09:00
parent cb78959678
commit 37c2ad63af
3 changed files with 25 additions and 5 deletions

View file

@ -705,7 +705,8 @@ static inline int msgpack_pack_bin(msgpack_packer *x, size_t l)
static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t l) static inline int msgpack_pack_raw_body(msgpack_packer* x, const void* b, size_t l)
{ {
msgpack_pack_append_buffer(x, (const unsigned char*)b, l); if (l > 0) msgpack_pack_append_buffer(x, (const unsigned char*)b, l);
return 0;
} }
/* /*

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) static inline int unpack_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
{ {
if (u->list_hook) { if (u->list_hook) {
PyObject *new_c = PyObject_CallFunction(u->list_hook, "(O)", *c); PyObject *new_c = PyObject_CallFunctionObjArgs(u->list_hook, *c, NULL);
if (!new_c) if (!new_c)
return -1; return -1;
Py_DECREF(*c); 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) static inline int unpack_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
{ {
if (u->object_hook) { if (u->object_hook) {
PyObject *new_c = PyObject_CallFunction(u->object_hook, "(O)", *c); PyObject *new_c = PyObject_CallFunctionObjArgs(u->object_hook, *c, NULL);
if (!new_c) if (!new_c)
return -1; return -1;

View file

@ -1,6 +1,6 @@
# coding: utf-8 # coding: utf-8
from msgpack import packb, unpackb from msgpack import packb, unpackb, ExtType
def test_str8(): def test_str8():
@ -66,4 +66,23 @@ def test_bin32():
assert b[5:] == data assert b[5:] == data
assert unpackb(b) == data assert unpackb(b) == data
def test_ext():
def check(ext, packed):
assert packb(ext) == packed
assert unpackb(packed) == ext
check(ExtType(0x42, b'Z'), b'\xd4\x42Z') # fixext 1
check(ExtType(0x42, b'ZZ'), b'\xd5\x42ZZ') # fixext 2
check(ExtType(0x42, b'Z'*4), b'\xd6\x42' + b'Z'*4) # fixext 4
check(ExtType(0x42, b'Z'*8), b'\xd7\x42' + b'Z'*8) # fixext 8
check(ExtType(0x42, b'Z'*16), b'\xd8\x42' + b'Z'*16) # fixext 16
# ext 8
check(ExtType(0x42, b''), b'\xc7\x00\x42')
check(ExtType(0x42, b'Z'*255), b'\xc7\xff\x42' + b'Z'*255)
# ext 16
check(ExtType(0x42, b'Z'*256), b'\xc8\x01\x00\x42' + b'Z'*256)
check(ExtType(0x42, b'Z'*0xffff), b'\xc8\xff\xff\x42' + b'Z'*0xffff)
# ext 32
check(ExtType(0x42, b'Z'*0x10000), b'\xc9\x00\x01\x00\x00\x42' + b'Z'*0x10000)
# needs large memory
#check(ExtType(0x42, b'Z'*0xffffffff),
# b'\xc9\xff\xff\xff\xff\x42' + b'Z'*0xffffffff)