[Python-checkins] cpython: Issue #15022: Ensure all pickle protocols are supported.

eric.snow python-checkins at python.org
Sun Feb 17 02:25:49 CET 2013


http://hg.python.org/cpython/rev/e4c065b2db49
changeset:   82235:e4c065b2db49
user:        Eric Snow <ericsnowcurrently at gmail.com>
date:        Sat Feb 16 18:20:32 2013 -0700
summary:
  Issue #15022: Ensure all pickle protocols are supported.

files:
  Lib/test/test_types.py    |  11 ++++++++---
  Objects/namespaceobject.c |  25 ++++++++++++++++++++++++-
  2 files changed, 32 insertions(+), 4 deletions(-)


diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1159,10 +1159,15 @@
     def test_pickle(self):
         ns = types.SimpleNamespace(breakfast="spam", lunch="spam")
 
-        ns_pickled = pickle.dumps(ns)
-        ns_roundtrip = pickle.loads(ns_pickled)
+        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
+            pname = "protocol {}".format(protocol)
+            try:
+                ns_pickled = pickle.dumps(ns, protocol)
+            except TypeError as e:
+                raise TypeError(pname) from e
+            ns_roundtrip = pickle.loads(ns_pickled)
 
-        self.assertEqual(ns, ns_roundtrip)
+            self.assertEqual(ns, ns_roundtrip, pname)
 
 
 def test_main():
diff --git a/Objects/namespaceobject.c b/Objects/namespaceobject.c
--- a/Objects/namespaceobject.c
+++ b/Objects/namespaceobject.c
@@ -173,6 +173,29 @@
 }
 
 
+PyDoc_STRVAR(namespace_reduce__doc__, "Return state information for pickling");
+
+static PyObject *
+namespace_reduce(register _PyNamespaceObject *ns)
+{
+    PyObject *result, *args = PyTuple_New(0);
+
+    if (!args)
+        return NULL;
+
+    result = PyTuple_Pack(3, (PyObject *)Py_TYPE(ns), args, ns->ns_dict);
+    Py_DECREF(args);
+    return result;
+}
+
+
+static PyMethodDef namespace_methods[] = {
+    {"__reduce__", (PyCFunction)namespace_reduce, METH_NOARGS,
+     namespace_reduce__doc__},
+    {NULL,         NULL}  /* sentinel */
+};
+
+
 PyDoc_STRVAR(namespace_doc,
 "A simple attribute-based namespace.\n\
 \n\
@@ -207,7 +230,7 @@
     0,                                          /* tp_weaklistoffset */
     0,                                          /* tp_iter */
     0,                                          /* tp_iternext */
-    0,                                          /* tp_methods */
+    namespace_methods,                          /* tp_methods */
     namespace_members,                          /* tp_members */
     0,                                          /* tp_getset */
     0,                                          /* tp_base */

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list