Skip to content

Commit d108eeb

Browse files
gh-95196: Disable incorrect pickling of the C implemented classmethod descriptors (GH-96383)
(cherry picked from commit 77f0249) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3c09deb commit d108eeb

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Lib/test/pickletester.py

+18
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,15 @@ def pie(self):
27732773
unpickled = self.loads(self.dumps(method, proto))
27742774
self.assertEqual(method(obj), unpickled(obj))
27752775

2776+
descriptors = (
2777+
PyMethodsTest.__dict__['cheese'], # static method descriptor
2778+
PyMethodsTest.__dict__['wine'], # class method descriptor
2779+
)
2780+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2781+
for descr in descriptors:
2782+
with self.subTest(proto=proto, descr=descr):
2783+
self.assertRaises(TypeError, self.dumps, descr, proto)
2784+
27762785
def test_c_methods(self):
27772786
global Subclass
27782787
class Subclass(tuple):
@@ -2808,6 +2817,15 @@ class Nested(str):
28082817
unpickled = self.loads(self.dumps(method, proto))
28092818
self.assertEqual(method(*args), unpickled(*args))
28102819

2820+
descriptors = (
2821+
bytearray.__dict__['maketrans'], # built-in static method descriptor
2822+
dict.__dict__['fromkeys'], # built-in class method descriptor
2823+
)
2824+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2825+
for descr in descriptors:
2826+
with self.subTest(proto=proto, descr=descr):
2827+
self.assertRaises(TypeError, self.dumps, descr, proto)
2828+
28112829
def test_compat_pickle(self):
28122830
tests = [
28132831
(range(1, 7), '__builtin__', 'xrange'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Disable incorrect pickling of the C implemented classmethod descriptors.

Objects/descrobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ PyTypeObject PyClassMethodDescr_Type = {
755755
0, /* tp_weaklistoffset */
756756
0, /* tp_iter */
757757
0, /* tp_iternext */
758-
descr_methods, /* tp_methods */
758+
0, /* tp_methods */
759759
descr_members, /* tp_members */
760760
method_getset, /* tp_getset */
761761
0, /* tp_base */

0 commit comments

Comments
 (0)