support for lambda inside of generator expressions
[cython.git] / tests / run / cython3.pyx
1 # cython: language_level=3
2 # mode: run
3 # tag: generators, python3
4
5 cimport cython
6
7 try:
8     sorted
9 except NameError:
10     def sorted(seq):
11         seq = list(seq)
12         seq.sort()
13         return seq
14
15 __doc__ = """
16 >>> items = list(locals_function(1).items())
17 >>> items.sort()
18 >>> for item in items:
19 ...     print('%s = %r' % item)
20 a = 1
21 b = 2
22 x = u'abc'
23 """
24
25 import sys
26 if sys.version_info[0] >= 3:
27     __doc__ = __doc__.replace(" u'", " '")
28
29 def locals_function(a, b=2):
30     x = 'abc'
31     return locals()
32
33
34 def print_function(*args):
35     """
36     >>> print_function(1,2,3)
37     1 2 3
38     """
39     print(*args) # this isn't valid Py2 syntax
40
41 def exec3_function(cmd):
42     """
43     >>> exec3_function('a = 1+1')['a']
44     2
45     """
46     g = {}
47     l = {}
48     exec(cmd, g, l)
49     return l
50
51 def exec2_function(cmd):
52     """
53     >>> exec2_function('a = 1+1')['a']
54     2
55     """
56     g = {}
57     exec(cmd, g)
58     return g
59
60 ustring = "abcdefg"
61
62 def unicode_literals():
63     """
64     >>> print( unicode_literals() )
65     True
66     abcdefg
67     """
68     print(isinstance(ustring, unicode) or type(ustring))
69     return ustring
70
71 def str_type_is_unicode():
72     """
73     >>> str_type, s = str_type_is_unicode()
74     >>> isinstance(s, type(ustring)) or (s, str_type)
75     True
76     >>> isinstance(s, str_type) or (s, str_type)
77     True
78     >>> isinstance(ustring, str_type) or str_type
79     True
80     """
81     cdef str s = 'abc'
82     return str, s
83
84 def list_comp():
85     """
86     >>> list_comp()
87     [0, 4, 8]
88     """
89     x = 'abc'
90     result = [x*2 for x in range(5) if x % 2 == 0]
91     assert x == 'abc' # don't leak in Py3 code
92     return result
93
94 def list_comp_with_lambda():
95     """
96     >>> list_comp_with_lambda()
97     [0, 4, 8]
98     """
99     x = 'abc'
100     result = [x*2 for x in range(5) if (lambda x:x % 2)(x) == 0]
101     assert x == 'abc' # don't leak in Py3 code
102     return result
103
104 module_level_lc = [ module_level_loopvar*2 for module_level_loopvar in range(4) ]
105 def list_comp_module_level():
106     """
107     >>> module_level_lc
108     [0, 2, 4, 6]
109     >>> module_level_loopvar
110     Traceback (most recent call last):
111     NameError: name 'module_level_loopvar' is not defined
112     """
113
114 module_level_list_genexp = list(module_level_genexp_loopvar*2 for module_level_genexp_loopvar in range(4))
115 def genexpr_module_level():
116     """
117     >>> module_level_list_genexp
118     [0, 2, 4, 6]
119     >>> module_level_genexp_loopvar
120     Traceback (most recent call last):
121     NameError: name 'module_level_genexp_loopvar' is not defined
122     """
123
124 def list_comp_unknown_type(l):
125     """
126     >>> list_comp_unknown_type(range(5))
127     [0, 4, 8]
128     """
129     return [x*2 for x in l if x % 2 == 0]
130
131 def listcomp_as_condition(sequence):
132     """
133     >>> listcomp_as_condition(['a', 'b', '+'])
134     True
135     >>> listcomp_as_condition('ab+')
136     True
137     >>> listcomp_as_condition('abc')
138     False
139     """
140     if [1 for c in sequence if c in '+-*/<=>!%&|([^~,']:
141         return True
142     return False
143
144 def set_comp():
145     """
146     >>> sorted(set_comp())
147     [0, 4, 8]
148     """
149     x = 'abc'
150     result = {x*2 for x in range(5) if x % 2 == 0}
151     assert x == 'abc' # don't leak
152     return result
153
154 def dict_comp():
155     """
156     >>> sorted(dict_comp().items())
157     [(0, 0), (2, 4), (4, 8)]
158     """
159     x = 'abc'
160     result = {x:x*2 for x in range(5) if x % 2 == 0}
161     assert x == 'abc' # don't leak
162     return result
163
164 # in Python 3, d.keys/values/items() are the iteration methods
165 @cython.test_assert_path_exists(
166     "//WhileStatNode",
167     "//WhileStatNode/SimpleCallNode",
168     "//WhileStatNode/SimpleCallNode/NameNode")
169 @cython.test_fail_if_path_exists(
170     "//ForInStatNode")
171 def dict_iter(dict d):
172     """
173     >>> d = {'a' : 1, 'b' : 2, 'c' : 3}
174     >>> keys, values, items = dict_iter(d)
175     >>> sorted(keys)
176     ['a', 'b', 'c']
177     >>> sorted(values)
178     [1, 2, 3]
179     >>> sorted(items)
180     [('a', 1), ('b', 2), ('c', 3)]
181     """
182     keys = [ key for key in d.keys() ]
183     values = [ value for value in d.values() ]
184     items = [ item for item in d.items() ]
185     return keys, values, items
186
187 def int_literals():
188     """
189     >>> int_literals()
190     long
191     long
192     unsigned long
193     unsigned long
194     """
195     print(cython.typeof(1L))
196     print(cython.typeof(10000000000000L))
197     print(cython.typeof(1UL))
198     print(cython.typeof(10000000000000UL))