[Cython] prevent comprehension variables from leaking
Stefan Behnel
stefan_ml at behnel.de
Fri Dec 19 15:44:06 CET 2008
Hi,
Dag Sverre Seljebotn wrote:
> Stefan Behnel wrote:
>> I think this change might be a bit controversial, so I would like to hear
>> some comments on it. I added a transform that keeps the run variable in
>> comprehensions from leaking by replacing it with a temp variable.
>
> I think either a) leave it like it is, or b) the [] sets up an entirely
> new scope (if this is how Python 3 does it, I don't know myself).
> Anything in-between seems only confusing.
I agree that a local scope would be the right way to do it. The problem of
leaking comprehension run variables was fixed in Py3, and the current
support for set comprehensions and dict comprehensions in Cython fails to
comply here. Having list comprehensions leak is bad enough, and I think it
should be fixed in Cython even if that means a slight incompatibility with
Py2.x.
> This means that if this change is done, it must be OK to do
>
> cdef char* x
> print [x for x in range(10)]
That would be expected, yes. However, type inference will not work in most
cases, as there is no way to specify what an iterator will return. And most
use cases will not involve range() but an arbitrary list or dict.
And I agree that a suitable syntax is hard to come up with.
Stefan
More information about the Cython-dev
mailing list