From a2b46cae10a148a502260a7eb1cb356ab446743c Mon Sep 17 00:00:00 2001 From: "Michael J. Sullivan" Date: Tue, 15 Feb 2022 17:25:11 -0800 Subject: [PATCH 1/2] bpo-46764: Fix wrapping bound method with @classmethod Since #8405, @classmethod now has special case behavior to call its argument's tp_descr_get if it exists. This breaks wrapping a bound method with it, since bound method's have a do-nothing tp_descr_get. Drop that do-nothing tp_descr_get. --- Lib/test/test_decorators.py | 10 ++++++++++ Objects/classobject.c | 9 +-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_decorators.py b/Lib/test/test_decorators.py index 57a741ffd29742..a6baa3ad1dd6b3 100644 --- a/Lib/test/test_decorators.py +++ b/Lib/test/test_decorators.py @@ -330,6 +330,16 @@ def outer(cls): 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/Objects/classobject.c b/Objects/classobject.c index d7ccf31244e8b7..653922d39447e4 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -319,13 +319,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) "method", @@ -360,7 +353,7 @@ PyTypeObject PyMethod_Type = { method_getset, /* tp_getset */ 0, /* tp_base */ 0, /* tp_dict */ - method_descr_get, /* tp_descr_get */ + 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ 0, /* tp_init */ From 95815660cc27ff9512931e37edb5cecab2f1f005 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 16 Feb 2022 03:23:40 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst 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 index 00000000000000..f69793cd2d7b85 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-16-03-23-38.bpo-46764.wEY4bS.rst @@ -0,0 +1 @@ +Fix wrapping bound methods with @classmethod