summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog.rst11
-rw-r--r--msgpack/_msgpack.pyx16
-rw-r--r--test/test_pack.py26
3 files changed, 50 insertions, 3 deletions
diff --git a/ChangeLog.rst b/ChangeLog.rst
index 4fd5cbc..46b83ee 100644
--- a/ChangeLog.rst
+++ b/ChangeLog.rst
@@ -1,3 +1,14 @@
+0.2.3
+=======
+:release date: in development
+
+Changes
+-------
+
+Bugs fixed
+-----------
+* Can't pack subclass of dict.
+
0.2.2
=======
:release date: 2012-09-21
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx
index c9f5e31..976871e 100644
--- a/msgpack/_msgpack.pyx
+++ b/msgpack/_msgpack.pyx
@@ -139,11 +139,19 @@ cdef class Packer(object):
ret = msgpack_pack_raw(&self.pk, len(o))
if ret == 0:
ret = msgpack_pack_raw_body(&self.pk, rawval, len(o))
- elif PyDict_Check(o):
+ elif PyDict_CheckExact(o):
d = <dict>o
ret = msgpack_pack_map(&self.pk, len(d))
if ret == 0:
- for k,v in d.iteritems():
+ for k, v in d.iteritems():
+ ret = self._pack(k, nest_limit-1)
+ if ret != 0: break
+ ret = self._pack(v, nest_limit-1)
+ if ret != 0: break
+ elif PyDict_Check(o):
+ ret = msgpack_pack_map(&self.pk, len(o))
+ if ret == 0:
+ for k, v in o.items():
ret = self._pack(k, nest_limit-1)
if ret != 0: break
ret = self._pack(v, nest_limit-1)
@@ -332,7 +340,9 @@ cdef class Unpacker(object):
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
object object_hook=None, object list_hook=None,
- encoding=None, unicode_errors='strict', int max_buffer_size=0):
+ encoding=None, unicode_errors='strict', int max_buffer_size=0,
+ object object_pairs_hook=None,
+ ):
self.use_list = use_list
self.file_like = file_like
if file_like:
diff --git a/test/test_pack.py b/test/test_pack.py
index 85d11a0..b216c46 100644
--- a/test/test_pack.py
+++ b/test/test_pack.py
@@ -91,5 +91,31 @@ def testPackFloat():
assert_equal(packb(1.0, use_single_float=True), b'\xca' + struct.pack('>f', 1.0))
assert_equal(packb(1.0, use_single_float=False), b'\xcb' + struct.pack('>d', 1.0))
+
+class odict(dict):
+ '''Reimplement OrderedDict to run test on Python 2.6'''
+ def __init__(self, seq):
+ self._seq = seq
+ dict.__init__(self, seq)
+
+ def items(self):
+ return self._seq[:]
+
+ def iteritems(self):
+ return iter(self._seq)
+
+ def keys(self):
+ return [x[0] for x in self._seq]
+
+def test_odict():
+ seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
+ od = odict(seq)
+ assert_equal(unpackb(packb(od)), dict(seq))
+ # After object_pairs_hook is implemented.
+ #def pair_hook(seq):
+ # return seq
+ #assert_equal(unpackb(packb(od), object_pairs_hook=pair_hook), seq)
+
+
if __name__ == '__main__':
main()