Commit acd03a26 authored by Stefan Behnel's avatar Stefan Behnel

Fix Py3 list comprehensions in (C-)class bodies: loop variable was declared in...

Fix Py3 list comprehensions in (C-)class bodies: loop variable was declared in the object struct instead of the scope that declared the class (usually the module init function).
parent da602065
......@@ -14,6 +14,9 @@ Features added
Bugs fixed
----------
* Py3 list comprehensions in class bodies resulted in invalid C code.
(Github issue #1889)
* Modules built for later CPython 3.5.x versions failed to import in 3.5.1.
(Github issue #1880)
......
......@@ -1750,12 +1750,14 @@ class GeneratorExpressionScope(Scope):
def __init__(self, outer_scope):
name = outer_scope.global_scope().next_id(Naming.genexpr_id_ref)
Scope.__init__(self, name, outer_scope, outer_scope)
self.var_entries = outer_scope.var_entries # keep declarations outside
self.directives = outer_scope.directives
self.genexp_prefix = "%s%d%s" % (Naming.pyrex_prefix, len(name), name)
while outer_scope.is_genexpr_scope:
# Class/ExtType scopes are filled at class creation time, i.e. from the
# module init function or surrounding function.
while outer_scope.is_genexpr_scope or outer_scope.is_c_class_scope or outer_scope.is_py_class_scope:
outer_scope = outer_scope.outer_scope
self.var_entries = outer_scope.var_entries # keep declarations outside
outer_scope.subscopes.add(self)
def mangle(self, prefix, name):
......
......@@ -378,6 +378,24 @@ def list_comp_with_lambda():
return result
class ListCompInClass(object):
"""
>>> x = ListCompInClass()
>>> x.listcomp
[1, 2, 3]
"""
listcomp = [i+1 for i in range(3)]
cdef class ListCompInCClass:
"""
>>> x = ListCompInCClass()
>>> x.listcomp
[1, 2, 3]
"""
listcomp = [i+1 for i in range(3)]
module_level_lc = [ module_level_loopvar*2 for module_level_loopvar in range(4) ]
def list_comp_module_level():
"""
......
......@@ -84,6 +84,24 @@ def global_listcomp():
"""
class ListCompInClass(object):
"""
>>> x = ListCompInClass()
>>> x.listcomp
[1, 2, 3]
"""
listcomp = [i+1 for i in range(3)]
cdef class ListCompInCClass:
"""
>>> x = ListCompInCClass()
>>> x.listcomp
[1, 2, 3]
"""
listcomp = [i+1 for i in range(3)]
def nested_result():
"""
>>> nested_result()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment