[Cython] PEP 3135 -- New Super

Vitja Makarov vitja.makarov at gmail.com
Thu Jul 7 17:09:08 CEST 2011


2011/7/6 Vitja Makarov <vitja.makarov at gmail.com>:
> 2011/7/6 Stefan Behnel <stefan_ml at behnel.de>:
>> Vitja Makarov, 06.07.2011 09:05:
>>>
>>> 2011/7/6 Stefan Behnel<stefan_ml at behnel.de>:
>>>>
>>>> Stefan Behnel, 05.07.2011 10:04:
>>>>>
>>>>> Vitja Makarov, 05.07.2011 09:17:
>>>>>>
>>>>>> 2011/7/5 Stefan Behnel:
>>>>>>>
>>>>>>> Vitja Makarov, 05.07.2011 08:21:
>>>>>>>>
>>>>>>>> I was thinking about implementing new super() with no arguments.
>>>>>>>
>>>>>>> http://trac.cython.org/cython_trac/ticket/696
>>>>>>>
>>>>>>>> The problem is where to store __class__, I see two options here:
>>>>>>>>
>>>>>>>> 1. Add func_class member to CyFunction, this way __class__ will be
>>>>>>>> private and not visible for inner functions:
>>>>>>>> 2. Put it into closure
>>>>>>>
>>>>>>> The second option has the advantage of requiring the field only when
>>>>>>> super()
>>>>>>> is used, whereas the first impacts all functions.
>>>>>>>
>>>>>>> I would expect that programs commonly have a lot more functions than
>>>>>>> specifically methods that use a no-argument call to super(), so this
>>>>>>> may
>>>>>>> make a difference.
>>>>>>>
>>>>>>
>>>>>> So, now classes are created the following way:
>>>>>>
>>>>>> class_dict = {}
>>>>>> class_dict.foo = foo_func
>>>>>> class = CreateClass(class_dict)
>>>>>>
>>>>>> So after class is created I should check its dict for CyFunction
>>>>>> members (maybe only ones that actually require __class__)
>>>>>> and set __class__:
>>>>>>
>>>>>> for value in class.__dict__.itervalues():
>>>>>> if isinstance(value, CyFunction) and value.func_class is WantClass:
>>>>>> value.func_class = class
>>>>>>
>>>>>> Btw, first way requires cyfunction signature change, it would accept
>>>>>> cyfunction object as first argument.
>>>>>
>>>>> We currently pass the binding (i.e. owning) object, right?
>>>>
>>>> So, how would this work for methods? We need to pass the 'self' object
>>>> there, which the CyFunction doesn't know. If anything, it only knows the
>>>> class it was defined in, which doesn't help here.
>>>
>>> From PEP: "super() is equivalent to: super(__class__,<firstarg>)"
>>
>> I wasn't speaking of super(). What I meant, was: how do we pass 'self' when
>> we pass the CyFunction object as the first argument?
>>
>
>
> Oh, ok. Now we pass closure or nothing in self. So method's self is
> passed via tuple.
> Instancemethod do this for us. Now CyFucntion uses PyCFunction_Call we
> can override this and change signature of cyfunction to:
>
> PyObject func(CyFunction *func, PyObject *self, PyObject *args,
> PyObject *kwargs);
>
> This way we should implement new instancemethod type.
>

I've add support for new super() for cdef classes here:
https://github.com/vitek/cython/compare/_new_super

Pure python classes would require my _bindings branch.


-- 
vitja.


More information about the cython-devel mailing list