]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46764: Fix wrapping bound method with @classmethod (#31367)
authorMichael J. Sullivan <sully@msully.net>
Thu, 5 May 2022 04:00:21 +0000 (21:00 -0700)
committerGitHub <noreply@github.com>
Thu, 5 May 2022 04:00:21 +0000 (23:00 -0500)
Lib/test/test_decorators.py
Misc/NEWS.d/next/Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst [new file with mode: 0644]
Objects/classobject.c

index 57a741ffd29742d1e80ba18ae57192ee5b3ac2d8..a6baa3ad1dd6b31ed515b8747895ae42114bef40 100644 (file)
@@ -330,6 +330,16 @@ class TestDecorators(unittest.TestCase):
         self.assertEqual(Class().inner(), 'spam')
         self.assertEqual(Class().outer(), 'eggs')
 
+    def test_bound_function_inside_classmethod(self):
+        class A:
+            def foo(self, cls):
+                return 'spam'
+
+        class B:
+            bar = classmethod(A().foo)
+
+        self.assertEqual(B.bar(), 'spam')
+
     def test_wrapped_classmethod_inside_classmethod(self):
         class MyClassMethod1:
             def __init__(self, func):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst
new file mode 100644 (file)
index 0000000..f69793c
--- /dev/null
@@ -0,0 +1 @@
+Fix wrapping bound methods with @classmethod
index cf731358e41c447ffc43de88611d71438a7e90f6..b9708ba0e41a3b8c74bc8a5cceb117433b82f1b1 100644 (file)
@@ -321,13 +321,6 @@ method_traverse(PyMethodObject *im, visitproc visit, void *arg)
     return 0;
 }
 
-static PyObject *
-method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
-{
-    Py_INCREF(meth);
-    return meth;
-}
-
 PyTypeObject PyMethod_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     .tp_name = "method",
@@ -348,7 +341,6 @@ PyTypeObject PyMethod_Type = {
     .tp_methods = method_methods,
     .tp_members = method_memberlist,
     .tp_getset = method_getset,
-    .tp_descr_get = method_descr_get,
     .tp_new = method_new,
 };