Skip to content

Commit

Permalink
pythongh-113157 pythongh-89519: Fix method descriptors (pythongh-113233)
Browse files Browse the repository at this point in the history
Restore behaviors before classmethod descriptor chaining was introduced.
  • Loading branch information
rhettinger authored and kulikjak committed Jan 22, 2024
1 parent 1e6a242 commit b9f2300
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5004,6 +5004,21 @@ class Child(Parent):
gc.collect()
self.assertEqual(Parent.__subclasses__(), [])

def test_instance_method_get_behavior(self):
# test case for gh-113157

class A:
def meth(self):
return self

class B:
pass

a = A()
b = B()
b.meth = a.meth.__get__(b, B)
self.assertEqual(b.meth(), a)

def test_attr_raise_through_property(self):
# test case for gh-103272
class A:
Expand Down
8 changes: 8 additions & 0 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ 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",
Expand All @@ -339,6 +346,7 @@ 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,
};

Expand Down

0 comments on commit b9f2300

Please sign in to comment.