From b3604d3bcedc91f70ecb973cfd542829205885d2 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 24 May 2010 11:48:36 +0200 Subject: [PATCH] fix type inference and code generation for generator expressions at module level --- Cython/Compiler/Symtab.py | 12 +++++++----- tests/run/listcomp.pyx | 9 +++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index b21ea071..3f33b966 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -1268,15 +1268,17 @@ class GeneratorExpressionScope(LocalScope): name = outer_scope.global_scope().next_id(Naming.genexpr_id_ref) LocalScope.__init__(self, name, outer_scope) self.directives = outer_scope.directives - self.genexp_prefix = "%s%s" % (Naming.pyrex_prefix, name) - + self.genexp_prefix = "%s%d%s" % (Naming.pyrex_prefix, len(name), name) + def mangle(self, prefix, name): - return '%s%s' % (self.genexp_prefix, LocalScope.mangle(self, prefix, name)) + return '%s%s' % (self.genexp_prefix, self.outer_scope.mangle(self, prefix, name)) def declare_var(self, name, type, pos, - cname = None, visibility = 'private', is_cdef = 0): + cname = None, visibility = 'private', is_cdef = True): + # the outer scope needs to generate code for the variable, but + # this scope must hold its name exclusively cname = '%s%s' % (self.genexp_prefix, self.outer_scope.mangle(Naming.var_prefix, name)) - entry = self.outer_scope.declare_var(None, type, pos, cname, visibility, is_cdef) + entry = self.outer_scope.declare_var(None, type, pos, cname, visibility, is_cdef = True) self.entries[name] = entry return entry diff --git a/tests/run/listcomp.pyx b/tests/run/listcomp.pyx index 1caffa00..47197121 100644 --- a/tests/run/listcomp.pyx +++ b/tests/run/listcomp.pyx @@ -33,3 +33,12 @@ def iterdict(): l = [d[key] for key in d] l.sort() print l + +listcomp_result = [ i*i for i in range(5) ] +def global_listcomp(): + """ + >>> [ i*i for i in range(5) ] + [0, 1, 4, 9, 16] + >>> listcomp_result + [0, 1, 4, 9, 16] + """ -- 2.26.2