Skip to content

Commit d058eae

Browse files
authored
gh-113157 gh-89519: Fix method descriptors (gh-113233)
Restore behaviors before classmethod descriptor chaining was introduced.
1 parent 6a5b473 commit d058eae

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Diff for: Lib/test/test_descr.py

+15
Original file line numberDiff line numberDiff line change
@@ -5004,6 +5004,21 @@ class Child(Parent):
50045004
gc.collect()
50055005
self.assertEqual(Parent.__subclasses__(), [])
50065006

5007+
def test_instance_method_get_behavior(self):
5008+
# test case for gh-113157
5009+
5010+
class A:
5011+
def meth(self):
5012+
return self
5013+
5014+
class B:
5015+
pass
5016+
5017+
a = A()
5018+
b = B()
5019+
b.meth = a.meth.__get__(b, B)
5020+
self.assertEqual(b.meth(), a)
5021+
50075022
def test_attr_raise_through_property(self):
50085023
# test case for gh-103272
50095024
class A:

Diff for: Objects/classobject.c

+8
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ method_traverse(PyMethodObject *im, visitproc visit, void *arg)
319319
return 0;
320320
}
321321

322+
static PyObject *
323+
method_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
324+
{
325+
Py_INCREF(meth);
326+
return meth;
327+
}
328+
322329
PyTypeObject PyMethod_Type = {
323330
PyVarObject_HEAD_INIT(&PyType_Type, 0)
324331
.tp_name = "method",
@@ -339,6 +346,7 @@ PyTypeObject PyMethod_Type = {
339346
.tp_methods = method_methods,
340347
.tp_members = method_memberlist,
341348
.tp_getset = method_getset,
349+
.tp_descr_get = method_descr_get,
342350
.tp_new = method_new,
343351
};
344352

0 commit comments

Comments
 (0)