On May 24, 2010, at 4:50 AM, Stefan Behnel wrote:

> Hi,
>
> the current closures branch has limited support for inlined generator
> expressions, i.e. you can write
>
>     any(x == 5 for x in some_seq)
>
> and Cython will generate an inlined loop for it that short circuits
> properly on the first hit. To get this working correctly, I fixed the
> scoping behaviour of the loop variable by giving generator expressions
> their own scope, so that x won't leak into the surrounding scope in  
> the
> above example. However, this introduces a pretty annoying problem: you
> can't type the loop variable any more. Any cdef declaration will  
> only apply
> to the surrounding scope, not to the scope of the generator  
> expression.
>
> This may be acceptable in some cases where Cython can properly infer  
> a type
> for it, but in other cases, explicit typing is required to make the
> expression work correctly and/or efficiently.
>
> Does anyone have an idea how we can let users provide an explicit  
> type for
> x in the above example? Maybe with a cast like this?
>
>     any(x == 5 for <int>x in some_seq)
>
> Any other ideas?

That seems reasonable to me, and also goes well with, e.g., the  
proposed syntax for typing varargs.

> Second question: should this be enabled for list/set/dict  
> comprehensions in
> Cython 0.13 as well? I think we already agreed that the leaking loop
> variables should be considered a bug that rarely has an impact on  
> existing
> code. In Py2, only list comprehensions leak their variable, whereas  
> genexps
> have their own scope. In Py3, all comprehensions and genexps have  
> their own
> scope. I would prefer unifying the scoping rules in Cython 0.13 as  
> well.
> (It's already implemented in cython-closures, although disabling it  
> would
> be trivial).


I think we should strive for Py2 compatibility, even in corner cases.  
With the -3 flag, we should behave as Python 3 (i.e. not leaking  
variables as you suggest).

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to