]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95196: Disable incorrect pickling of the C implemented classmethod descriptors...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 5 Oct 2022 11:53:38 +0000 (04:53 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Oct 2022 11:53:38 +0000 (04:53 -0700)
(cherry picked from commit 77f0249308de76401bf4f3c6a057789c92f862d1)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/pickletester.py
Misc/NEWS.d/next/Core and Builtins/2022-08-29-13-06-58.gh-issue-95196.eGRR4b.rst [new file with mode: 0644]
Objects/descrobject.c

index 4e8d4e4f6433f99a8012e6735ba3e621513fc376..18d7f52ecffd7f9f5410a426d108848b90050d7a 100644 (file)
@@ -2773,6 +2773,15 @@ class AbstractPickleTests:
                     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):
@@ -2808,6 +2817,15 @@ class AbstractPickleTests:
                     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 (file)
index 0000000..37534fa
--- /dev/null
@@ -0,0 +1 @@
+Disable incorrect pickling of the C implemented classmethod descriptors.
index 09b0f82c69909e331dbd9c6f26eb5c0d586873c7..ee3ad1b7a328b7df4c36a962f8a2d423d790bd49 100644 (file)
@@ -755,7 +755,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 */