Skip to content

Commit

Permalink
[3.12] gh-105035: fix super() calls on unusual types (e.g. meta-types) (
Browse files Browse the repository at this point in the history
GH-105094) (#105117)

gh-105035: fix super() calls on unusual types (e.g. meta-types) (GH-105094)
(cherry picked from commit 68c75c3)

Co-authored-by: Carl Meyer <carl@oddbird.net>
  • Loading branch information
miss-islington and carljm authored May 30, 2023
1 parent 9ae49e3 commit 5fff491
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 221 deletions.
12 changes: 12 additions & 0 deletions Lib/test/test_super.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ def method(self):

self.assertEqual(C().method(), mysuper)

def test_unusual_getattro(self):
class MyType(type):
pass

def test(name):
mytype = MyType(name, (MyType,), {})
super(MyType, type(mytype)).__setattr__(mytype, "bar", 1)
self.assertEqual(mytype.bar, 1)

test("foo1")
test("foo2")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`super` calls on types with custom :attr:`tp_getattro`
implementation (e.g. meta-types.)
4 changes: 3 additions & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1660,8 +1660,10 @@ dummy_func(
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
STAT_INC(LOAD_SUPER_ATTR, hit);
PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 2);
PyTypeObject *cls = (PyTypeObject *)class;
int method_found = 0;
res2 = _PySuper_Lookup((PyTypeObject *)class, self, name, &method_found);
res2 = _PySuper_Lookup(cls, self, name,
cls->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
Py_DECREF(global_super);
Py_DECREF(class);
if (res2 == NULL) {
Expand Down
Loading

0 comments on commit 5fff491

Please sign in to comment.