summaryrefslogtreecommitdiff
path: root/msgpack/unpack.h
diff options
context:
space:
mode:
authorINADA Naoki <inada-n@klab.com>2012-09-24 03:10:37 +0900
committerINADA Naoki <inada-n@klab.com>2012-09-24 03:10:37 +0900
commit1526316a0803c233a6752aff1bab6e951447d12a (patch)
tree1705ccd3828c981791b2a0ce1debfbcc9f598a82 /msgpack/unpack.h
parentd13f10c02e06b0f3c138b1726cfa8b1a31529d64 (diff)
parent477d3b152f5d36a48a8083b3720def2dd1f5d1a7 (diff)
downloadmsgpack-python-1526316a0803c233a6752aff1bab6e951447d12a.tar.gz
Merge branch '0.2-maint'
Conflicts: msgpack/_msgpack.pyx
Diffstat (limited to 'msgpack/unpack.h')
-rw-r--r--msgpack/unpack.h30
1 files changed, 21 insertions, 9 deletions
diff --git a/msgpack/unpack.h b/msgpack/unpack.h
index a106f9c..7064a1b 100644
--- a/msgpack/unpack.h
+++ b/msgpack/unpack.h
@@ -22,6 +22,7 @@
typedef struct unpack_user {
int use_list;
PyObject *object_hook;
+ bool has_pairs_hook;
PyObject *list_hook;
const char *encoding;
const char *unicode_errors;
@@ -160,9 +161,7 @@ static inline int template_callback_array_item(unpack_user* u, unsigned int curr
static inline int template_callback_array_end(unpack_user* u, msgpack_unpack_object* c)
{
if (u->list_hook) {
- PyObject *arglist = Py_BuildValue("(O)", *c);
- PyObject *new_c = PyEval_CallObject(u->list_hook, arglist);
- Py_DECREF(arglist);
+ PyObject *new_c = PyEval_CallFunction(u->list_hook, "(O)", *c);
Py_DECREF(*c);
*c = new_c;
}
@@ -171,16 +170,31 @@ static inline int template_callback_array_end(unpack_user* u, msgpack_unpack_obj
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
{
- PyObject *p = PyDict_New();
+ PyObject *p;
+ if (u->has_pairs_hook) {
+ p = PyList_New(n); // Or use tuple?
+ }
+ else {
+ p = PyDict_New();
+ }
if (!p)
return -1;
*o = p;
return 0;
}
-static inline int template_callback_map_item(unpack_user* u, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v)
+static inline int template_callback_map_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object k, msgpack_unpack_object v)
{
- if (PyDict_SetItem(*c, k, v) == 0) {
+ if (u->has_pairs_hook) {
+ msgpack_unpack_object item = PyTuple_Pack(2, k, v);
+ if (!item)
+ return -1;
+ Py_DECREF(k);
+ Py_DECREF(v);
+ PyList_SET_ITEM(*c, current, item);
+ return 0;
+ }
+ else if (PyDict_SetItem(*c, k, v) == 0) {
Py_DECREF(k);
Py_DECREF(v);
return 0;
@@ -191,9 +205,7 @@ static inline int template_callback_map_item(unpack_user* u, msgpack_unpack_obje
static inline int template_callback_map_end(unpack_user* u, msgpack_unpack_object* c)
{
if (u->object_hook) {
- PyObject *arglist = Py_BuildValue("(O)", *c);
- PyObject *new_c = PyEval_CallObject(u->object_hook, arglist);
- Py_DECREF(arglist);
+ PyObject *new_c = PyEval_CallFunction(u->object_hook, "(O)", *c);
Py_DECREF(*c);
*c = new_c;
}