1 # cython: infer_types = True
5 from cython cimport typeof, infer_types
7 from cpython cimport bool
9 ##################################################
10 # type inference tests in 'full' mode
20 assert typeof(i) == "long", typeof(i)
22 assert typeof(x) == "double", typeof(x)
24 assert typeof(xptr) == "double *", typeof(xptr)
26 assert typeof(xptrptr) == "double **", typeof(xptrptr)
28 assert typeof(b) == "bytes object", typeof(b)
30 assert typeof(s) == "str object", typeof(s)
32 assert typeof(u) == "unicode object", typeof(u)
34 assert typeof(L) == "list object", typeof(L)
36 assert typeof(t) == "tuple object", typeof(t)
43 assert typeof(b) == "bytes object", typeof(b)
45 assert typeof(u) == "unicode object", typeof(u)
47 assert typeof(L) == "list object", typeof(L)
49 assert typeof(t) == "tuple object", typeof(t)
51 assert typeof(d) == "dict object", typeof(d)
53 assert typeof(B) == "bool", typeof(B)
60 assert typeof(b) == "bytes object", typeof(b)
62 assert typeof(b1) == "bytes object", typeof(b1)
64 assert typeof(b2) == "bytes object", typeof(b2)
66 assert typeof(u) == "unicode object", typeof(u)
68 assert typeof(u1) == "unicode object", typeof(u1)
70 assert typeof(u2) == "unicode object", typeof(u2)
72 assert typeof(s) == "str object", typeof(s)
74 assert typeof(s1) == "str object", typeof(s1)
76 assert typeof(s2) == "str object", typeof(s2)
78 assert typeof(L) == "list object", typeof(L)
80 assert typeof(L1) == "list object", typeof(L1)
82 assert typeof(L2) == "list object", typeof(L2)
84 assert typeof(t) == "tuple object", typeof(t)
86 assert typeof(t1) == "tuple object", typeof(t1)
88 assert typeof(t2) == "tuple object", typeof(t2)
95 assert typeof(b) == "bytes object", typeof(b)
97 assert typeof(b1) == "Python object", typeof(b1)
99 assert typeof(u) == "unicode object", typeof(u)
101 assert typeof(u1) == "Py_UNICODE", typeof(u1)
103 assert typeof(s) == "str object", typeof(s)
105 assert typeof(s1) == "Python object", typeof(s1)
107 assert typeof(L) == "list object", typeof(L)
109 assert typeof(L1) == "Python object", typeof(L1)
111 assert typeof(t) == "tuple object", typeof(t)
113 assert typeof(t1) == "Python object", typeof(t1)
115 def multiple_assignments():
117 >>> multiple_assignments()
122 assert typeof(a) == "long"
126 assert typeof(b) == "double"
130 assert typeof(c) == "Python object"
137 assert typeof(a) == "long", typeof(a)
139 assert typeof(b) == "double", typeof(b)
141 assert typeof(c) == "Python object", typeof(c)
143 assert typeof(d) == "double", typeof(d)
145 cdef class some_class:
148 def unary_operators():
150 >>> unary_operators()
153 assert typeof(~x) == "int", typeof(~x)
155 assert typeof(~obj) == "Python object", typeof(~obj)
157 assert typeof(a) == "Python object", typeof(a)
159 assert typeof(b) == "int", typeof(b)
161 assert typeof(c) == "Python object", typeof(c)
163 assert typeof(d) == "Python object", typeof(d)
166 def builtin_type_operations():
168 >>> builtin_type_operations()
173 assert typeof(b1) == "bytes object", typeof(b1)
175 assert typeof(b2) == "bytes object", typeof(b2)
178 assert typeof(u1) == "unicode object", typeof(u1)
180 assert typeof(u2) == "unicode object", typeof(u2)
183 assert typeof(u3) == "unicode object", typeof(u3)
186 assert typeof(s1) == "str object", typeof(s1)
188 assert typeof(s2) == "str object", typeof(s2)
190 s3 = "abc %s" * 10 * 10
191 s3 = 10 * "abc %s" * 10
192 assert typeof(s3) == "str object", typeof(s3)
194 assert typeof(L1) == "list object", typeof(L1)
196 assert typeof(L2) == "list object", typeof(L2)
198 assert typeof(T1) == "tuple object", typeof(T1)
200 assert typeof(T2) == "tuple object", typeof(T2)
210 assert typeof(d) == "double"
212 assert typeof(e) == "double"
214 def cascaded_assignment():
216 assert typeof(a) == "double"
217 assert typeof(b) == "double"
218 assert typeof(c) == "double"
219 assert typeof(d) == "double"
221 assert typeof(e) == "double"
229 assert typeof(a) == "long"
237 assert typeof(a) == "long"
242 assert typeof(b) == "double"
244 for c from 0 <= c < 10 by .5:
246 assert typeof(c) == "double"
248 for d in range(0, 10L, 2):
250 assert typeof(a) == "long"
252 def loop_over_charptr():
254 >>> print( loop_over_charptr() )
257 cdef char* char_ptr_string = 'abcdefg'
258 for c in char_ptr_string:
262 def loop_over_bytes_literal():
264 >>> print( loop_over_bytes_literal() )
271 def loop_over_bytes():
273 >>> print( loop_over_bytes() )
276 cdef bytes bytes_string = b'abcdefg'
277 for c in bytes_string:
283 >>> print( loop_over_str() )
286 cdef str string = 'abcdefg'
291 def loop_over_unicode():
293 >>> print( loop_over_unicode() )
296 cdef unicode ustring = u'abcdefg'
297 for uchar in ustring:
301 def loop_over_int_array():
303 >>> print( loop_over_int_array() )
306 cdef int[10] int_array
319 >>> conditional(True)
320 (True, 'Python object')
321 >>> conditional(False)
322 (False, 'Python object')
328 return type(a) is unicode, typeof(a)
330 ##################################################
331 # type inference tests that work in 'safe' mode
334 def double_inference():
336 >>> values, types = double_inference()
337 >>> values == (1.0, 1.0*2, 1.0*2.0+2.0*2.0, 1.0*2.0)
340 ('double', 'double', 'double', 'Python object')
344 d_c = d_a * float(some_float_value()) + d_b * float(some_float_value())
345 o_d = d_a * some_float_value()
346 return (d_a,d_b,d_c,o_d), (typeof(d_a), typeof(d_b), typeof(d_c), typeof(o_d))
348 cdef object some_float_value():
352 @cython.test_fail_if_path_exists('//NameNode[@type.is_pyobject = True]')
353 @cython.test_assert_path_exists('//NameNode[@type.is_pyobject]',
354 '//NameNode[@type.is_pyobject = False]')
358 >>> double_loop() == 1.0 * 10
373 assert typeof(a) == "double", typeof(c)
375 assert typeof(b) == "long", typeof(b)
377 assert typeof(c) == "MyType", typeof(c)
378 for i in range(10): pass
379 assert typeof(i) == "long", typeof(i)
382 assert typeof(d) == "long", typeof(d)
384 # we special-case inference to type str, see
387 assert typeof(s) == "Python object", typeof(s)
389 assert typeof(t) == "str object", typeof(t)
391 # potentially overflowing arithmetic
394 assert typeof(e) == "Python object", typeof(e)
397 assert typeof(f) == "Python object", typeof(f)
400 assert typeof(g) == "Python object", typeof(g)
403 assert typeof(j) == "Python object", typeof(j)
406 def args_tuple_keywords(*args, **kwargs):
408 >>> args_tuple_keywords(1,2,3, a=1, b=2)
410 assert typeof(args) == "tuple object", typeof(args)
411 assert typeof(kwargs) == "dict object", typeof(kwargs)
414 def args_tuple_keywords_reassign_same(*args, **kwargs):
416 >>> args_tuple_keywords_reassign_same(1,2,3, a=1, b=2)
418 assert typeof(args) == "tuple object", typeof(args)
419 assert typeof(kwargs) == "dict object", typeof(kwargs)
425 def args_tuple_keywords_reassign_pyobjects(*args, **kwargs):
427 >>> args_tuple_keywords_reassign_pyobjects(1,2,3, a=1, b=2)
429 assert typeof(args) == "Python object", typeof(args)
430 assert typeof(kwargs) == "Python object", typeof(kwargs)
440 cdef class Base0: pass
441 cdef class Base(Base0): pass
442 cdef class A(Base): pass
443 cdef class AA(A): pass
444 cdef class AAA(AA): pass
445 cdef class B(Base): pass
446 cdef class BB(B): pass
448 cdef class CC(C): pass
451 def common_extension_type_base():
453 >>> common_extension_type_base()
457 assert typeof(x) == "A", typeof(x)
460 assert typeof(y) == "Base", typeof(y)
463 assert typeof(z) == "Base", typeof(z)
466 assert typeof(w) == "Python object", typeof(w)
470 def large_literals():
474 # It's only safe to infer small integer literals.
476 b = 100000000000000000000000000000000
477 assert typeof(a) == "long", typeof(a)
478 assert typeof(b) == "Python object", typeof(b)
479 c, d = 10, 100000000000000000000000000000000
480 assert typeof(c) == "long", typeof(c)
481 assert typeof(d) == "Python object", typeof(d)