1cimport cython 2 3cy = __import__("cython") 4 5cpdef func1(self, cython.integral x): 6 print "%s," % (self,), 7 if cython.integral is int: 8 print 'x is int', x, cython.typeof(x) 9 else: 10 print 'x is long', x, cython.typeof(x) 11 12 13class A(object): 14 meth = func1 15 16 def __str__(self): 17 return "A" 18 19pyfunc = func1 20 21def test_fused_cpdef(): 22 """ 23 >>> test_fused_cpdef() 24 None, x is int 2 int 25 None, x is long 2 long 26 None, x is long 2 long 27 <BLANKLINE> 28 None, x is int 2 int 29 None, x is long 2 long 30 <BLANKLINE> 31 A, x is int 2 int 32 A, x is long 2 long 33 A, x is long 2 long 34 A, x is long 2 long 35 """ 36 func1[int](None, 2) 37 func1[long](None, 2) 38 func1(None, 2) 39 40 print 41 42 pyfunc[cy.int](None, 2) 43 pyfunc(None, 2) 44 45 print 46 47 A.meth[cy.int](A(), 2) 48 A.meth(A(), 2) 49 A().meth[cy.long](2) 50 A().meth(2) 51 52 53def assert_raise(func, *args): 54 try: 55 func(*args) 56 except TypeError: 57 pass 58 else: 59 assert False, "Function call did not raise TypeError" 60 61def test_badcall(): 62 """ 63 >>> test_badcall() 64 """ 65 assert_raise(pyfunc) 66 assert_raise(pyfunc, 1, 2, 3) 67 assert_raise(pyfunc[cy.int], 10, 11, 12) 68 assert_raise(pyfunc, None, object()) 69 assert_raise(A().meth) 70 assert_raise(A.meth) 71 assert_raise(A().meth[cy.int]) 72 assert_raise(A.meth[cy.int]) 73 74ctypedef long double long_double 75 76cpdef multiarg(cython.integral x, cython.floating y): 77 if cython.integral is int: 78 print "x is an int,", 79 else: 80 print "x is a long,", 81 82 if cython.floating is long_double: 83 print "y is a long double:", 84 elif float is cython.floating: 85 print "y is a float:", 86 else: 87 print "y is a double:", 88 89 print x, y 90 91def test_multiarg(): 92 """ 93 >>> test_multiarg() 94 x is an int, y is a float: 1 2.0 95 x is an int, y is a float: 1 2.0 96 x is a long, y is a double: 4 5.0 97 >>> multiarg() 98 Traceback (most recent call last): 99 TypeError: Expected at least 2 arguments, got 0 100 >>> multiarg(1, 2.0, 3) # doctest: +ELLIPSIS 101 Traceback (most recent call last): 102 TypeError: ...2...arg...3... 103 """ 104 multiarg[int, float](1, 2.0) 105 multiarg[cy.int, cy.float](1, 2.0) 106 multiarg(4, 5.0) 107