]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36068: Make _tuplegetter objects serializable (GH-11981)
authorJoe Jevnik <JoeJev@gmail.com>
Thu, 21 Feb 2019 21:00:40 +0000 (16:00 -0500)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 21 Feb 2019 21:00:40 +0000 (13:00 -0800)
Doc/whatsnew/3.8.rst
Lib/test/test_collections.py
Modules/_collectionsmodule.c

index 66e1c13cccce71884f379a5738d9bb0d487754a2..2f759f3454ea3eecfd3016e2ee895ed933c9954b 100644 (file)
@@ -368,7 +368,7 @@ Optimizations
 * Sped-up field lookups in :func:`collections.namedtuple`.  They are now more
   than two times faster, making them the fastest form of instance variable
   lookup in Python. (Contributed by Raymond Hettinger, Pablo Galindo, and
-  Serhiy Storchaka in :issue:`32492`.)
+  Joe Jevnik, Serhiy Storchaka in :issue:`32492`.)
 
 * The :class:`list` constructor does not overallocate the internal item buffer
   if the input iterable has a known length (the input implements ``__len__``).
index 4f8a841e7577e10673348245f3a1f0aaf046261a..dad1b6cd7fb385c799d954afb2fdb7752b5937f5 100644 (file)
@@ -13,7 +13,7 @@ from test import support
 import types
 import unittest
 
-from collections import namedtuple, Counter, OrderedDict, _count_elements
+from collections import namedtuple, Counter, OrderedDict, _count_elements, _tuplegetter
 from collections import UserDict, UserString, UserList
 from collections import ChainMap
 from collections import deque
@@ -573,6 +573,15 @@ class TestNamedTuple(unittest.TestCase):
         self.assertRaises(AttributeError, Point.x.__set__, p, 33)
         self.assertRaises(AttributeError, Point.x.__delete__, p)
 
+        class NewPoint(tuple):
+            x = pickle.loads(pickle.dumps(Point.x))
+            y = pickle.loads(pickle.dumps(Point.y))
+
+        np = NewPoint([1, 2])
+
+        self.assertEqual(np.x, 1)
+        self.assertEqual(np.y, 2)
+
 
 ################################################################################
 ### Abstract Base Classes
index 280b15d73b1ff2f61435a2adbd0644fca5c2af05..1c9e866e62f04891cb87fcfb298c39d0db4aee5f 100644 (file)
@@ -2440,12 +2440,23 @@ tuplegetter_dealloc(_tuplegetterobject *self)
     Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
+static PyObject*
+tuplegetter_reduce(_tuplegetterobject *self)
+{
+    return Py_BuildValue("(O(nO))", (PyObject*) Py_TYPE(self), self->index, self->doc);
+}
+
 
 static PyMemberDef tuplegetter_members[] = {
     {"__doc__",  T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
     {0}
 };
 
+static PyMethodDef tuplegetter_methods[] = {
+    {"__reduce__", (PyCFunction) tuplegetter_reduce, METH_NOARGS, NULL},
+    {NULL},
+};
+
 static PyTypeObject tuplegetter_type = {
     PyVarObject_HEAD_INIT(NULL, 0)
     "_collections._tuplegetter",                /* tp_name */
@@ -2475,7 +2486,7 @@ static PyTypeObject tuplegetter_type = {
     0,                                          /* tp_weaklistoffset */
     0,                                          /* tp_iter */
     0,                                          /* tp_iternext */
-    0,                                          /* tp_methods */
+    tuplegetter_methods,                        /* tp_methods */
     tuplegetter_members,                        /* tp_members */
     0,                                          /* tp_getset */
     0,                                          /* tp_base */