Rev 4768: (Matt Nordhoff) Add Pickle support to the StaticTuple class. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Fri Oct 23 19:40:28 BST 2009
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 4768 [merge]
revision-id: pqm at pqm.ubuntu.com-20091023184024-igfxawbh4tkag0ui
parent: pqm at pqm.ubuntu.com-20091023173042-6bbukn561ie0a6hb
parent: mnordhoff at mattnordhoff.com-20091021175433-qxa3fvplnw67aynk
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2009-10-23 19:40:24 +0100
message:
(Matt Nordhoff) Add Pickle support to the StaticTuple class.
modified:
bzrlib/_static_tuple_c.c _keys_type_c.c-20090908204220-aa346ccw4l37jzt7-1
bzrlib/_static_tuple_py.py _keys_type_py.py-20090908213415-o1ww98k9a8aqm0bm-1
bzrlib/tests/test__static_tuple.py test__keys_type.py-20090908204220-aa346ccw4l37jzt7-2
=== modified file 'bzrlib/_static_tuple_c.c'
--- a/bzrlib/_static_tuple_c.c 2009-10-21 14:27:00 +0000
+++ b/bzrlib/_static_tuple_c.c 2009-10-21 17:54:33 +0000
@@ -304,8 +304,8 @@
if (tuple_repr == NULL) {
return NULL;
}
- result = PyString_FromFormat("%s%s", Py_TYPE(self)->tp_name,
- PyString_AsString(tuple_repr));
+ result = PyString_FromFormat("StaticTuple%s",
+ PyString_AsString(tuple_repr));
return result;
}
@@ -574,6 +574,29 @@
static PyObject *
+StaticTuple_reduce(StaticTuple *self)
+{
+ PyObject *result = NULL, *as_tuple = NULL;
+
+ result = PyTuple_New(2);
+ if (!result) {
+ return NULL;
+ }
+ as_tuple = StaticTuple_as_tuple(self);
+ if (as_tuple == NULL) {
+ Py_DECREF(result);
+ return NULL;
+ }
+ Py_INCREF(&StaticTuple_Type);
+ PyTuple_SET_ITEM(result, 0, (PyObject *)&StaticTuple_Type);
+ PyTuple_SET_ITEM(result, 1, as_tuple);
+ return result;
+}
+
+static char StaticTuple_reduce_doc[] = "__reduce__() => tuple\n";
+
+
+static PyObject *
StaticTuple_add(PyObject *v, PyObject *w)
{
Py_ssize_t i, len_v, len_w;
@@ -688,6 +711,7 @@
METH_STATIC | METH_VARARGS,
"Create a StaticTuple from a given sequence. This functions"
" the same as the tuple() constructor."},
+ {"__reduce__", (PyCFunction)StaticTuple_reduce, METH_NOARGS, StaticTuple_reduce_doc},
{NULL, NULL} /* sentinel */
};
@@ -735,7 +759,7 @@
PyTypeObject StaticTuple_Type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
- "StaticTuple", /* tp_name */
+ "bzrlib._static_tuple_c.StaticTuple", /* tp_name */
sizeof(StaticTuple), /* tp_basicsize */
sizeof(PyObject *), /* tp_itemsize */
(destructor)StaticTuple_dealloc, /* tp_dealloc */
=== modified file 'bzrlib/_static_tuple_py.py'
--- a/bzrlib/_static_tuple_py.py 2009-10-21 05:02:35 +0000
+++ b/bzrlib/_static_tuple_py.py 2009-10-21 16:04:03 +0000
@@ -48,6 +48,9 @@
def __repr__(self):
return '%s%s' % (self.__class__.__name__, tuple.__repr__(self))
+ def __reduce__(self):
+ return (StaticTuple, tuple(self))
+
def __add__(self, other):
"""Concatenate self with other"""
return StaticTuple.from_sequence(tuple.__add__(self,other))
=== modified file 'bzrlib/tests/test__static_tuple.py'
--- a/bzrlib/tests/test__static_tuple.py 2009-10-21 14:27:00 +0000
+++ b/bzrlib/tests/test__static_tuple.py 2009-10-21 17:54:33 +0000
@@ -16,6 +16,7 @@
"""Tests for the StaticTuple type."""
+import cPickle
import gc
import sys
@@ -577,6 +578,24 @@
self.assertRaises(TypeError,
self.module.StaticTuple.from_sequence, foo='a')
+ def test_pickle(self):
+ st = self.module.StaticTuple('foo', 'bar')
+ pickled = cPickle.dumps(st)
+ unpickled = cPickle.loads(pickled)
+ self.assertEqual(unpickled, st)
+
+ def test_pickle_empty(self):
+ st = self.module.StaticTuple()
+ pickled = cPickle.dumps(st)
+ unpickled = cPickle.loads(pickled)
+ self.assertIs(st, unpickled)
+
+ def test_pickle_nested(self):
+ st = self.module.StaticTuple('foo', self.module.StaticTuple('bar'))
+ pickled = cPickle.dumps(st)
+ unpickled = cPickle.loads(pickled)
+ self.assertEqual(unpickled, st)
+
def test_static_tuple_thunk(self):
# Make sure the right implementation is available from
# bzrlib.static_tuple.StaticTuple.
More information about the bazaar-commits
mailing list