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?
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).
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev