mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.12] gh-105035: fix super() calls on unusual types (e.g. meta-types) (GH-105094) (#105117)
gh-105035: fix super() calls on unusual types (e.g. meta-types) (GH-105094)
(cherry picked from commit 68c75c3153
)
Co-authored-by: Carl Meyer <carl@oddbird.net>
This commit is contained in:
parent
9ae49e3f3b
commit
5fff491bd5
4 changed files with 240 additions and 222 deletions
|
@ -410,6 +410,18 @@ class TestSuper(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(C().method(), mysuper)
|
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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix :func:`super` calls on types with custom :attr:`tp_getattro`
|
||||||
|
implementation (e.g. meta-types.)
|
|
@ -1660,8 +1660,10 @@ dummy_func(
|
||||||
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
|
DEOPT_IF(!PyType_Check(class), LOAD_SUPER_ATTR);
|
||||||
STAT_INC(LOAD_SUPER_ATTR, hit);
|
STAT_INC(LOAD_SUPER_ATTR, hit);
|
||||||
PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 2);
|
PyObject *name = GETITEM(frame->f_code->co_names, oparg >> 2);
|
||||||
|
PyTypeObject *cls = (PyTypeObject *)class;
|
||||||
int method_found = 0;
|
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(global_super);
|
||||||
Py_DECREF(class);
|
Py_DECREF(class);
|
||||||
if (res2 == NULL) {
|
if (res2 == NULL) {
|
||||||
|
|
444
Python/generated_cases.c.h
generated
444
Python/generated_cases.c.h
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue