Opened 5 years ago

Closed 4 years ago

#465 closed defect (fixed)

fix scoping for comprehensions and generator expressions

Reported by: scoder Owned by: scoder
Priority: minor Milestone: 0.13
Component: Python Semantics Keywords:


The fix for the declaration analysis phase of comprehensions in

lets the comprehension variable appear in locals(). This diverges from Python's own behaviour.

The best way to fix this would be to follow Py3 and move the variable out of the surrounding scope (actually, in Py3, it becomes a generator expression, but the result is the same).

This breaks compatibility with Py2 (and also rather rare code that relies on the variable holding the last value after running the comprehension), but it's certainly worth it.

Change History (3)

comment:1 Changed 4 years ago by scoder

The same fix is required for inlined generator expressions, as implemented in ticket #534.

comment:2 Changed 4 years ago by scoder

  • Owner set to scoder
  • Status changed from new to assigned

This was discussed on the mailing list:

Essence: comprehensions will continue to leak their loop variables when compiling Cython code or Python 2 code, but will not leak them when compiling Python 3 code (i.e. when using the future -3 option). Generator expressions will not leak their variables, thus following the behaviour they have in Python 2 and Python 3. The same applies to inlined generator expressions.

In the current cython-closures branch, the behaviour is a configurable option on the ComprehensionNode, and both comprehensions and generator expressions behave as in Python 2.

comment:3 Changed 4 years ago by scoder

  • Priority changed from major to minor
  • Resolution set to fixed
  • Status changed from assigned to closed
  • Summary changed from fix scoping for comprehensions to fix scoping for comprehensions and generator expressions

The above described locals() problem is not due to the variable not appearing in the locals() at all, but rather because the test evaluates locals() before assigning to the variable. This conflicts with Cython's function local variable scoping rules and is not related to comprehension scoping.

Closing this bug as 'fixed' as the underlying problem is gone, although the behaviour stays the same.

Note: See TracTickets for help on using tickets.