Skip to content

Commit a440e84

Browse files
committed
pythongh-106917: don't wrongly optimize super 'method' loads off types
1 parent 0ba07b2 commit a440e84

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

Lib/test/test_super.py

+39
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,45 @@ def test(name):
422422
test("foo1")
423423
test("foo2")
424424

425+
def test_reassigned_new(self):
426+
class A:
427+
def __new__(cls):
428+
pass
429+
430+
def __init_subclass__(cls):
431+
if "__new__" not in cls.__dict__:
432+
cls.__new__ = cls.__new__
433+
434+
class B(A):
435+
pass
436+
437+
class C(B):
438+
def __new__(cls):
439+
return super().__new__(cls)
440+
441+
C()
442+
C()
443+
444+
def test_mixed_staticmethod_hierarchy(self):
445+
# This test is just a desugared version of `test_reassigned_new`
446+
class A:
447+
@staticmethod
448+
def some(cls, *args, **kwargs):
449+
self.assertFalse(args)
450+
self.assertFalse(kwargs)
451+
452+
class B(A):
453+
def some(cls, *args, **kwargs):
454+
return super().some(cls, *args, **kwargs)
455+
456+
class C(B):
457+
@staticmethod
458+
def some(cls):
459+
return super().some(cls)
460+
461+
C.some(C)
462+
C.some(C)
463+
425464

426465
if __name__ == "__main__":
427466
unittest.main()

Python/bytecodes.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ dummy_func(
17201720
PyTypeObject *cls = (PyTypeObject *)class;
17211721
int method_found = 0;
17221722
res2 = _PySuper_Lookup(cls, self, name,
1723-
cls->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
1723+
Py_TYPE(self)->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
17241724
Py_DECREF(global_super);
17251725
Py_DECREF(class);
17261726
if (res2 == NULL) {

Python/executor_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)