[Cython] local variable handling in generators

Dag Sverre Seljebotn d.s.seljebotn at astro.uio.no
Mon May 23 11:04:52 CEST 2011


On 05/23/2011 10:50 AM, Vitja Makarov wrote:
> 2011/5/23 Stefan Behnel<stefan_ml at behnel.de>:
>> Vitja Makarov, 23.05.2011 10:13:
>>>
>>> With live variable analysis that should be easy to save/restore only
>>> active variables at the yield point.
>>
>> "Active" in the sense of "modified", I suppose? That's what I was expecting.
>>
>
> Active means that variable value will be used. In my example after
> 'print a' a isn't used anymore.
>
>>
>>> Btw now only reaching definitions analysis is implemented. I'm going
>>> to optimize by replacing sets with bitsets. And then try to implement
>>> live varaiables.
>>>
>>> I'm going to delete variable reference using active variable info, but
>>> that could introduce small incompatiblity with CPython:
>>> a = X
>>> print a #<- a will be decrefed here
>>> print 'the end'
>>
>> That incompatibility is not small at all. It breaks this code:
>>
>>     x = b'abc'
>>     cdef char* c = x
>>
>> Even if 'x' is no longer used after this point, it *must not* get freed
>> before 'c' is going away as well. That's basically impossible to decide, as
>> users may pass 'c' into a function that stores it away for alter use.
>>
>
> Yeah. That's hard to detect. But x could be marked as "don't decref
> when not-active"


def f(object o):
     cdef char* buf
     buf = get_buffer_of_obj(o)
     call_c_func(buf)

So there's a lot of variables that would have to be marked this way (but 
not all, I can see that).

Dag Sverre


More information about the cython-devel mailing list