4
# cython: language_level=3
8
#@cython.test_assert_path_exists('//ComprehensionNode')
9
#@cython.test_fail_if_path_exists('//SimpleCallNode')
10
def list_genexpr_iterable_lookup():
13
>>> [ x*2 for x in x if x % 2 == 0 ] # leaks in Py2 but finds the right 'x'
16
>>> list_genexpr_iterable_lookup()
20
result = list( x*2 for x in x if x % 2 == 0 )
21
assert x == (0,1,2,3,4,5)
25
#@cython.test_assert_path_exists('//ComprehensionNode')
26
#@cython.test_fail_if_path_exists('//SingleAssignmentNode//SimpleCallNode')
27
def genexpr_iterable_in_closure():
29
>>> genexpr_iterable_in_closure()
35
result = list( x*2 for x in x if x != 'b' )
36
assert x == 'abc' # don't leak in Py3 code
37
assert f() == 'abc' # don't leak in Py3 code
42
def genexpr_over_complex_arg(func, L):
44
>>> class wrapper(object):
46
>>> genexpr_over_complex_arg(list, wrapper())
49
return func(d for d in set([type(L).value, L.__class__.value, L.value]))
57
data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
58
data.sort(key=lambda r: r[1])
59
keys = [r[1] for r in data]
63
def genexpr_in_listcomp(L):
65
>>> genexpr_in_listcomp( [[1,2,3]]*2 )
66
[[1, 2, 3], [1, 2, 3]]
68
return list(d for d in [list(d for d in d) for d in L])
71
@cython.test_assert_path_exists('//ForFromStatNode')
72
def genexpr_range_in_listcomp(L):
74
>>> genexpr_range_in_listcomp( [1,2,3] )
75
[[0], [0, 1], [0, 1, 2]]
78
return [list(d for d in range(z)) for z in L]
81
@cython.test_fail_if_path_exists('//ForInStatNode')
82
def genexpr_in_dictcomp_dictiter():
84
>>> sorted(genexpr_in_dictcomp_dictiter())
88
return {k:d for k,d in d.iteritems() if d != 4}
91
def genexpr_over_array_slice():
93
>>> list(genexpr_over_array_slice())
94
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
100
return (n for n in x[:n+1])