Skip to content

Commit f36f892

Browse files
llllllllllrhettinger
authored andcommitted
bpo-36068: Make _tuplegetter objects serializable (GH-11981)
1 parent 407c734 commit f36f892

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ Optimizations
368368
* Sped-up field lookups in :func:`collections.namedtuple`. They are now more
369369
than two times faster, making them the fastest form of instance variable
370370
lookup in Python. (Contributed by Raymond Hettinger, Pablo Galindo, and
371-
Serhiy Storchaka in :issue:`32492`.)
371+
Joe Jevnik, Serhiy Storchaka in :issue:`32492`.)
372372

373373
* The :class:`list` constructor does not overallocate the internal item buffer
374374
if the input iterable has a known length (the input implements ``__len__``).

Lib/test/test_collections.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import types
1414
import unittest
1515

16-
from collections import namedtuple, Counter, OrderedDict, _count_elements
16+
from collections import namedtuple, Counter, OrderedDict, _count_elements, _tuplegetter
1717
from collections import UserDict, UserString, UserList
1818
from collections import ChainMap
1919
from collections import deque
@@ -573,6 +573,15 @@ def test_field_descriptor(self):
573573
self.assertRaises(AttributeError, Point.x.__set__, p, 33)
574574
self.assertRaises(AttributeError, Point.x.__delete__, p)
575575

576+
class NewPoint(tuple):
577+
x = pickle.loads(pickle.dumps(Point.x))
578+
y = pickle.loads(pickle.dumps(Point.y))
579+
580+
np = NewPoint([1, 2])
581+
582+
self.assertEqual(np.x, 1)
583+
self.assertEqual(np.y, 2)
584+
576585

577586
################################################################################
578587
### Abstract Base Classes

Modules/_collectionsmodule.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2440,12 +2440,23 @@ tuplegetter_dealloc(_tuplegetterobject *self)
24402440
Py_TYPE(self)->tp_free((PyObject*)self);
24412441
}
24422442

2443+
static PyObject*
2444+
tuplegetter_reduce(_tuplegetterobject *self)
2445+
{
2446+
return Py_BuildValue("(O(nO))", (PyObject*) Py_TYPE(self), self->index, self->doc);
2447+
}
2448+
24432449

24442450
static PyMemberDef tuplegetter_members[] = {
24452451
{"__doc__", T_OBJECT, offsetof(_tuplegetterobject, doc), 0},
24462452
{0}
24472453
};
24482454

2455+
static PyMethodDef tuplegetter_methods[] = {
2456+
{"__reduce__", (PyCFunction) tuplegetter_reduce, METH_NOARGS, NULL},
2457+
{NULL},
2458+
};
2459+
24492460
static PyTypeObject tuplegetter_type = {
24502461
PyVarObject_HEAD_INIT(NULL, 0)
24512462
"_collections._tuplegetter", /* tp_name */
@@ -2475,7 +2486,7 @@ static PyTypeObject tuplegetter_type = {
24752486
0, /* tp_weaklistoffset */
24762487
0, /* tp_iter */
24772488
0, /* tp_iternext */
2478-
0, /* tp_methods */
2489+
tuplegetter_methods, /* tp_methods */
24792490
tuplegetter_members, /* tp_members */
24802491
0, /* tp_getset */
24812492
0, /* tp_base */

0 commit comments

Comments
 (0)