From: Stefan Behnel Date: Tue, 25 May 2010 07:30:19 +0000 (+0200) Subject: let generator expressions inherit type declarations from surrounding scope X-Git-Tag: 0.13.beta0~2^2~50 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=e0afd5f8877e235a0a7dab721f2fcbada5d6769b;p=cython.git let generator expressions inherit type declarations from surrounding scope --- diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index 3f33b966..b7b3d655 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -1275,6 +1275,11 @@ class GeneratorExpressionScope(LocalScope): def declare_var(self, name, type, pos, cname = None, visibility = 'private', is_cdef = True): + if type is unspecified_type: + # if the outer scope defines a type for this variable, inherit it + outer_entry = self.outer_scope.lookup(name) + if outer_entry and not outer_entry.is_builtin: + type = outer_entry.type # may still be 'unspecified_type' ! # 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)) diff --git a/tests/run/all.pyx b/tests/run/all.pyx index f87ea293..29b75614 100644 --- a/tests/run/all.pyx +++ b/tests/run/all.pyx @@ -166,9 +166,11 @@ def all_lower_case_characters(unicode ustring): return all(uchar.islower() for uchar in ustring) @cython.test_assert_path_exists("//ForInStatNode", - "//InlinedGeneratorExpressionNode") + "//InlinedGeneratorExpressionNode", + "//InlinedGeneratorExpressionNode//IfStatNode") @cython.test_fail_if_path_exists("//SimpleCallNode", - "//YieldExprNode") + "//YieldExprNode", + "//IfStatNode//CoerceToBooleanNode") def all_in_typed_gen(seq): """ >>> all_in_typed_gen([1,1,1]) @@ -192,15 +194,15 @@ def all_in_typed_gen(seq): 4 False """ - # FIXME: this isn't really supposed to work, but it currently does - # due to incorrect scoping - this should be fixed!! cdef int x return all(x for x in seq) @cython.test_assert_path_exists("//ForInStatNode", - "//InlinedGeneratorExpressionNode") + "//InlinedGeneratorExpressionNode", + "//InlinedGeneratorExpressionNode//IfStatNode") @cython.test_fail_if_path_exists("//SimpleCallNode", - "//YieldExprNode") + "//YieldExprNode", + "//IfStatNode//CoerceToBooleanNode") def all_in_nested_gen(seq): """ >>> all(x for L in [[1,1,1],[1,1,1],[1,1,1]] for x in L) @@ -243,7 +245,5 @@ def all_in_nested_gen(seq): 2 False """ - # FIXME: this isn't really supposed to work, but it currently does - # due to incorrect scoping - this should be fixed!! cdef int x return all(x for L in seq for x in L) diff --git a/tests/run/any.pyx b/tests/run/any.pyx index 1a1d5898..1621e66d 100644 --- a/tests/run/any.pyx +++ b/tests/run/any.pyx @@ -143,7 +143,8 @@ lower_ustring = mixed_ustring.lower() upper_ustring = mixed_ustring.upper() @cython.test_assert_path_exists('//PythonCapiCallNode', - '//ForFromStatNode') + '//ForFromStatNode', + "//InlinedGeneratorExpressionNode") @cython.test_fail_if_path_exists('//SimpleCallNode', '//ForInStatNode') def any_lower_case_characters(unicode ustring): @@ -158,9 +159,11 @@ def any_lower_case_characters(unicode ustring): return any(uchar.islower() for uchar in ustring) @cython.test_assert_path_exists("//ForInStatNode", - "//InlinedGeneratorExpressionNode") + "//InlinedGeneratorExpressionNode", + "//InlinedGeneratorExpressionNode//IfStatNode") @cython.test_fail_if_path_exists("//SimpleCallNode", - "//YieldExprNode") + "//YieldExprNode", + "//IfStatNode//CoerceToBooleanNode") def any_in_typed_gen(seq): """ >>> any_in_typed_gen([0,1,0]) @@ -182,15 +185,15 @@ def any_in_typed_gen(seq): 5 False """ - # FIXME: this isn't really supposed to work, but it currently does - # due to incorrect scoping - this should be fixed!! cdef int x return any(x for x in seq) @cython.test_assert_path_exists("//ForInStatNode", - "//InlinedGeneratorExpressionNode") + "//InlinedGeneratorExpressionNode", + "//InlinedGeneratorExpressionNode//IfStatNode") @cython.test_fail_if_path_exists("//SimpleCallNode", - "//YieldExprNode") + "//YieldExprNode", + "//IfStatNode//CoerceToBooleanNode") def any_in_nested_gen(seq): """ >>> any(x for L in [[0,0,0],[0,0,1],[0,0,0]] for x in L) @@ -226,7 +229,5 @@ def any_in_nested_gen(seq): 3 False """ - # FIXME: this isn't really supposed to work, but it currently does - # due to incorrect scoping - this should be fixed!! cdef int x return any(x for L in seq for x in L)