[Python-checkins] gh-95196: Disable incorrect pickling of the C implemented classmethod descriptors (GH-96383)

miss-islington webhook-mailer at python.org
Wed Oct 5 07:59:14 EDT 2022


https://github.com/python/cpython/commit/2b248b0ab58428ae9c6006c02808ae2241293da3
commit: 2b248b0ab58428ae9c6006c02808ae2241293da3
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-10-05T04:59:09-07:00
summary:

gh-95196: Disable incorrect pickling of the C implemented classmethod descriptors (GH-96383)

(cherry picked from commit 77f0249308de76401bf4f3c6a057789c92f862d1)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2022-08-29-13-06-58.gh-issue-95196.eGRR4b.rst
M Lib/test/pickletester.py
M Objects/descrobject.c

diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 21419e11c874..499f80a15f34 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -2776,6 +2776,15 @@ def pie(self):
                     unpickled = self.loads(self.dumps(method, proto))
                     self.assertEqual(method(obj), unpickled(obj))
 
+        descriptors = (
+            PyMethodsTest.__dict__['cheese'],  # static method descriptor
+            PyMethodsTest.__dict__['wine'],  # class method descriptor
+        )
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            for descr in descriptors:
+                with self.subTest(proto=proto, descr=descr):
+                    self.assertRaises(TypeError, self.dumps, descr, proto)
+
     def test_c_methods(self):
         global Subclass
         class Subclass(tuple):
@@ -2811,6 +2820,15 @@ class Nested(str):
                     unpickled = self.loads(self.dumps(method, proto))
                     self.assertEqual(method(*args), unpickled(*args))
 
+        descriptors = (
+            bytearray.__dict__['maketrans'],  # built-in static method descriptor
+            dict.__dict__['fromkeys'],  # built-in class method descriptor
+        )
+        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+            for descr in descriptors:
+                with self.subTest(proto=proto, descr=descr):
+                    self.assertRaises(TypeError, self.dumps, descr, proto)
+
     def test_compat_pickle(self):
         tests = [
             (range(1, 7), '__builtin__', 'xrange'),
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-08-29-13-06-58.gh-issue-95196.eGRR4b.rst b/Misc/NEWS.d/next/Core and Builtins/2022-08-29-13-06-58.gh-issue-95196.eGRR4b.rst
new file mode 100644
index 000000000000..37534fa17525
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-08-29-13-06-58.gh-issue-95196.eGRR4b.rst	
@@ -0,0 +1 @@
+Disable incorrect pickling of the C implemented classmethod descriptors.
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index c3c541bf3c32..73ac14d2a84b 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -775,7 +775,7 @@ PyTypeObject PyClassMethodDescr_Type = {
     0,                                          /* tp_weaklistoffset */
     0,                                          /* tp_iter */
     0,                                          /* tp_iternext */
-    descr_methods,                              /* tp_methods */
+    0,                                          /* tp_methods */
     descr_members,                              /* tp_members */
     method_getset,                              /* tp_getset */
     0,                                          /* tp_base */



More information about the Python-checkins mailing list