summaryrefslogtreecommitdiff
path: root/msgpack/unpack.h
diff options
context:
space:
mode:
authorJoel Nothman <joel.nothman@gmail.com>2012-09-23 19:37:28 +1000
committerJoel Nothman <joel.nothman@gmail.com>2012-09-23 19:37:28 +1000
commit77942514db0c5a80e9f3f9bcb1e1939ecc8705e6 (patch)
treed3fdec62e6250cf329284a309dd468f019583d05 /msgpack/unpack.h
parentb06ed8eb75563111ef88a119f9f7a45e67f61736 (diff)
downloadmsgpack-python-77942514db0c5a80e9f3f9bcb1e1939ecc8705e6.tar.gz
Implement object_pairs_hook
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;
}