Skip to content

Commit

Permalink
pythongh-106917: fix super classmethod calls to non-classmethods
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Jul 21, 2023
1 parent 0ba07b2 commit c603b7e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
39 changes: 39 additions & 0 deletions Lib/test/test_super.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,45 @@ def test(name):
test("foo1")
test("foo2")

def test_reassigned_new(self):
class A:
def __new__(cls):
pass

def __init_subclass__(cls):
if "__new__" not in cls.__dict__:
cls.__new__ = cls.__new__

class B(A):
pass

class C(B):
def __new__(cls):
return super().__new__(cls)

C()
C()

def test_mixed_staticmethod_hierarchy(self):
# This test is just a desugared version of `test_reassigned_new`
class A:
@staticmethod
def some(cls, *args, **kwargs):
self.assertFalse(args)
self.assertFalse(kwargs)

class B(A):
def some(cls, *args, **kwargs):
return super().some(cls, *args, **kwargs)

class C(B):
@staticmethod
def some(cls):
return super().some(cls)

C.some(C)
C.some(C)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix classmethod-style :func:`super` method calls (i.e., where the second
argument to :func:`super`, or the implied second argument drawn from
``self/cls`` in the case of zero-arg super, is a type) when the target of
the call is not a classmethod.
2 changes: 1 addition & 1 deletion Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ dummy_func(
PyTypeObject *cls = (PyTypeObject *)class;
int method_found = 0;
res2 = _PySuper_Lookup(cls, self, name,
cls->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
Py_TYPE(self)->tp_getattro == PyObject_GenericGetAttr ? &method_found : NULL);
Py_DECREF(global_super);
Py_DECREF(class);
if (res2 == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c603b7e

Please sign in to comment.