Skip to content

gh-95196: Fixed instances of types.ClassMethodDescriptorType not being pickled correctly #96162

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ Other Language Changes
mapping is hashable.
(Contributed by Serhiy Storchaka in :gh:`87995`.)

* :class:`types.ClassMethodDescriptorType` instances are now pickled correctly.
(Contributed by Nir Schulman in :gh:`95196`.)


New Modules
===========
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(endpos)
STRUCT_FOR_ID(env)
STRUCT_FOR_ID(errors)
STRUCT_FOR_ID(eval)
STRUCT_FOR_ID(event)
STRUCT_FOR_ID(eventmask)
STRUCT_FOR_ID(exc_type)
Expand Down
7 changes: 7 additions & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Lib/test/pickletester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2795,8 +2795,10 @@ class Nested(str):
({1, 2}.__contains__, (2,)),
# unbound "coexist" method
(set.__contains__, ({1, 2}, 2)),
# built-in class method
# built-in bound class method
(dict.fromkeys, (("a", 1), ("b", 2))),
# built-in unbound class method
(dict.__dict__["fromkeys"], (dict, ("a", 1), ("b", 2))),
# built-in static method
(bytearray.maketrans, (b"abc", b"xyz")),
# subclass methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed :class:`types.ClassMethodDescriptorType` being pickled incorrectly.
20 changes: 19 additions & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,29 @@ descr_reduce(PyDescrObject *descr, PyObject *Py_UNUSED(ignored))
PyDescr_TYPE(descr), PyDescr_NAME(descr));
}


static PyObject*
classmethoddescr_reduce(PyDescrObject* descr, PyObject* Py_UNUSED(ignored))
{
/* Ideally, we would want to use a different callable than eval in
order to get the descriptor. However, we need to ensure the pickled
object will not cause errors upon unpickling in older versions. */
return Py_BuildValue("N(s, N, {s:O, s:O})",
_PyEval_GetBuiltin(&_Py_ID(eval)), "cls.__dict__[name]", Py_None,
"cls", PyDescr_TYPE(descr), "name", PyDescr_NAME(descr)
);
}

static PyMethodDef descr_methods[] = {
{"__reduce__", (PyCFunction)descr_reduce, METH_NOARGS, NULL},
{NULL, NULL}
};

static PyMethodDef classmethoddescr_methods[] = {
{"__reduce__", (PyCFunction)classmethoddescr_reduce, METH_NOARGS, NULL},
{NULL, NULL}
};

static PyMemberDef descr_members[] = {
{"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
{"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
Expand Down Expand Up @@ -776,7 +794,7 @@ PyTypeObject PyClassMethodDescr_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
descr_methods, /* tp_methods */
classmethoddescr_methods, /* tp_methods */
descr_members, /* tp_members */
method_getset, /* tp_getset */
0, /* tp_base */
Expand Down