Skip to content
Closed
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
20 changes: 20 additions & 0 deletions Lib/test/test_super.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,26 @@ def test_super_init_leaks(self):
for i in range(1000):
super.__init__(sp, int, i)

def test_unbound_super_binding(self):
class A:
def f(self):
return 'A.f'
@classmethod
def g(cls):
return 'A.g'

class B(A):
def f(self):
return 'B.f ' + self.__super.f()
@classmethod
def g(cls):
return 'B.g ' + cls.__super.g()

B._B__super = super(B)
b = B()
self.assertEqual(b.f(), 'B.f A.f') # instance binding
self.assertEqual(b.g(), 'B.g A.g') # class binding


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add class binding to unbound super objects for allowing autosuper with class
methods. Patch by Géry Ogam.
30 changes: 26 additions & 4 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -8798,23 +8798,45 @@ super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
superobject *su = (superobject *)self;
superobject *newobj;

if (obj == NULL || obj == Py_None || su->obj != NULL) {
/* Not binding to an object, or already bound */
if (su->obj != NULL) {
/* Already bound */
Py_INCREF(self);
return self;
}
if (!Py_IS_TYPE(su, &PySuper_Type))
if (!Py_IS_TYPE(su, &PySuper_Type)) {
/* If su is an instance of a (strict) subclass of super,
call its type */
if (obj == NULL || obj == Py_None) {
/* No instance to bind to */
return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
su->type, type, NULL);
}
return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
su->type, obj, NULL);
}
else {
/* Inline the common case */
if (obj == NULL || obj == Py_None) {
/* No instance to bind to */
PyTypeObject *obj_type = supercheck(su->type, type);
if (obj_type == NULL)
return NULL;
newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
NULL, NULL);
if (newobj == NULL)
return NULL;
Py_INCREF(su->type);
Py_INCREF(type);
newobj->type = su->type;
newobj->obj = type;
newobj->obj_type = obj_type;
return (PyObject *)newobj;
}
PyTypeObject *obj_type = supercheck(su->type, obj);
if (obj_type == NULL)
return NULL;
newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
NULL, NULL);
NULL, NULL);
if (newobj == NULL)
return NULL;
Py_INCREF(su->type);
Expand Down