1 # cython: language_level=3
3 # tag: generators, python3
16 >>> items = list(locals_function(1).items())
18 >>> for item in items:
19 ... print('%s = %r' % item)
26 if sys.version_info[0] >= 3:
27 __doc__ = __doc__.replace(" u'", " '")
29 def locals_function(a, b=2):
34 def print_function(*args):
36 >>> print_function(1,2,3)
39 print(*args) # this isn't valid Py2 syntax
41 def exec3_function(cmd):
43 >>> exec3_function('a = 1+1')['a']
51 def exec2_function(cmd):
53 >>> exec2_function('a = 1+1')['a']
62 def unicode_literals():
64 >>> print( unicode_literals() )
68 print(isinstance(ustring, unicode) or type(ustring))
71 def str_type_is_unicode():
73 >>> str_type, s = str_type_is_unicode()
74 >>> isinstance(s, type(ustring)) or (s, str_type)
76 >>> isinstance(s, str_type) or (s, str_type)
78 >>> isinstance(ustring, str_type) or str_type
90 result = [x*2 for x in range(5) if x % 2 == 0]
91 assert x == 'abc' # don't leak in Py3 code
94 def list_comp_with_lambda():
96 >>> list_comp_with_lambda()
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
104 module_level_lc = [ module_level_loopvar*2 for module_level_loopvar in range(4) ]
105 def list_comp_module_level():
109 >>> module_level_loopvar
110 Traceback (most recent call last):
111 NameError: name 'module_level_loopvar' is not defined
114 module_level_list_genexp = list(module_level_genexp_loopvar*2 for module_level_genexp_loopvar in range(4))
115 def genexpr_module_level():
117 >>> module_level_list_genexp
119 >>> module_level_genexp_loopvar
120 Traceback (most recent call last):
121 NameError: name 'module_level_genexp_loopvar' is not defined
124 def list_comp_unknown_type(l):
126 >>> list_comp_unknown_type(range(5))
129 return [x*2 for x in l if x % 2 == 0]
131 def listcomp_as_condition(sequence):
133 >>> listcomp_as_condition(['a', 'b', '+'])
135 >>> listcomp_as_condition('ab+')
137 >>> listcomp_as_condition('abc')
140 if [1 for c in sequence if c in '+-*/<=>!%&|([^~,']:
146 >>> sorted(set_comp())
150 result = {x*2 for x in range(5) if x % 2 == 0}
151 assert x == 'abc' # don't leak
156 >>> sorted(dict_comp().items())
157 [(0, 0), (2, 4), (4, 8)]
160 result = {x:x*2 for x in range(5) if x % 2 == 0}
161 assert x == 'abc' # don't leak
164 # in Python 3, d.keys/values/items() are the iteration methods
165 @cython.test_assert_path_exists(
167 "//WhileStatNode/SimpleCallNode",
168 "//WhileStatNode/SimpleCallNode/NameNode")
169 @cython.test_fail_if_path_exists(
171 def dict_iter(dict d):
173 >>> d = {'a' : 1, 'b' : 2, 'c' : 3}
174 >>> keys, values, items = dict_iter(d)
180 [('a', 1), ('b', 2), ('c', 3)]
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
195 print(cython.typeof(1L))
196 print(cython.typeof(10000000000000L))
197 print(cython.typeof(1UL))
198 print(cython.typeof(10000000000000UL))