Skip to content

Commit 77f0249

Browse files
pythongh-95196: Disable incorrect pickling of the C implemented classmethod descriptors (pythonGH-96383)
1 parent f8cbd79 commit 77f0249

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
@@ -2776,6 +2776,15 @@ def pie(self):
27762776
unpickled = self.loads(self.dumps(method, proto))
27772777
self.assertEqual(method(obj), unpickled(obj))
27782778

2779+
descriptors = (
2780+
PyMethodsTest.__dict__['cheese'], # static method descriptor
2781+
PyMethodsTest.__dict__['wine'], # class method descriptor
2782+
)
2783+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2784+
for descr in descriptors:
2785+
with self.subTest(proto=proto, descr=descr):
2786+
self.assertRaises(TypeError, self.dumps, descr, proto)
2787+
27792788
def test_c_methods(self):
27802789
global Subclass
27812790
class Subclass(tuple):
@@ -2811,6 +2820,15 @@ class Nested(str):
28112820
unpickled = self.loads(self.dumps(method, proto))
28122821
self.assertEqual(method(*args), unpickled(*args))
28132822

2823+
descriptors = (
2824+
bytearray.__dict__['maketrans'], # built-in static method descriptor
2825+
dict.__dict__['fromkeys'], # built-in class method descriptor
2826+
)
2827+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2828+
for descr in descriptors:
2829+
with self.subTest(proto=proto, descr=descr):
2830+
self.assertRaises(TypeError, self.dumps, descr, proto)
2831+
28142832
def test_compat_pickle(self):
28152833
tests = [
28162834
(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
@@ -776,7 +776,7 @@ PyTypeObject PyClassMethodDescr_Type = {
776776
0, /* tp_weaklistoffset */
777777
0, /* tp_iter */
778778
0, /* tp_iternext */
779-
descr_methods, /* tp_methods */
779+
0, /* tp_methods */
780780
descr_members, /* tp_members */
781781
method_getset, /* tp_getset */
782782
0, /* tp_base */

0 commit comments

Comments
 (0)