[Cython] local variable handling in generators

Stefan Behnel stefan_ml at behnel.de
Mon May 23 11:57:20 CEST 2011


Stefan Behnel, 23.05.2011 11:29:
> Stefan Behnel, 23.05.2011 11:15:
>> Vitja Makarov, 23.05.2011 10:50:
>>> 2011/5/23 Stefan Behnel:
>>>> I'm fine with deallocating variables that are no longer used after the
>>>> user
>>>> explicitly assigned None to them (i.e. replace the None assignment by a
>>>> simple "DECREF + set to NULL" in that case). I don't think we should be
>>>> doing more than that.
>>>
>>> Hmm. Why should that be NULL if user sets it to None?
>>
>> Because there is no user visible difference. None will always be available,
>> even if the Cython code no longer holds a reference to it. So changing "x =
>> None" into "Py_DECREF(x); x=NULL" is just fine, as long as we can make sure
>> 'x' is never accessed after this point.
>
> The difference is clearer when I spell out the code for the first example,
> too:
>
> # x = None
> Py_INCREF(None)
> Py_DECREF(x)
> x = None
>
> would be optimised into
>
> # x = None
> Py_DECREF(x)
> x = NULL
>
> That's likely not a big difference, assuming that setting 'x' to None was
> worth it for the user, i.e. it will clean up the referenced object at that
> point. It may still be worth it inside of generators and on function
> return, which may now have one variable less to clean up or store away. A
> None value would still have to be DECREF-ed at least.

Then again, "del x" would be a more obvious way to spell this ...

Stefan


More information about the cython-devel mailing list