Ticket #465 (closed defect: fixed)

Opened 5 years ago

Last modified 4 years ago

fix scoping for comprehensions and generator expressions

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

Description

The fix for the declaration analysis phase of comprehensions in

 http://hg.cython.org/cython-devel/rev/2646e35eeb7f

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

Changed 4 years ago by scoder

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

Changed 4 years ago by scoder

  • owner set to scoder
  • status changed from new to assigned

This was discussed on the mailing list:

 http://comments.gmane.org/gmane.comp.python.cython.devel/9588?set_lines=100000

 http://comments.gmane.org/gmane.comp.python.cython.devel/9601?set_lines=100000

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.

Changed 4 years ago by scoder

  • priority changed from major to minor
  • status changed from assigned to closed
  • resolution set to fixed
  • 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.