Visitar URL original
[3.12] gh-105035: fix super() calls on unusual types (e.g. meta-types) (GH-105094) by miss-islington · Pull Request #105117 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-105035: fix super() calls on unusual types (e.g. meta-types) (GH-1…
…05094)

(cherry picked from commit 68c75c3)

Co-authored-by: Carl Meyer <carl@oddbird.net>
  • Loading branch information
carljm authored and miss-islington committed May 30, 2023
commit 8eef8d4764ac6205c99a545ae30d5db9cf59e26d
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