1# cython: auto_cpdef=True 2# mode:run 3# tag: directive,auto_cpdef 4 5import cython 6 7def str(arg): 8 """ 9 This is a bit evil - str gets mapped to a C-API function and is 10 being redefined here. 11 12 >>> print(str('TEST')) 13 STR 14 """ 15 return 'STR' 16 17@cython.test_assert_path_exists('//SimpleCallNode[@function.type.is_cfunction = True]') 18@cython.test_fail_if_path_exists('//SimpleCallNode[@function.type.is_builtin_type = True]') 19def call_str(arg): 20 """ 21 >>> print(call_str('TEST')) 22 STR 23 """ 24 return str(arg) 25 26 27def stararg_func(*args): 28 """ 29 >>> stararg_func(1, 2) 30 (1, 2) 31 """ 32 return args 33 34def starstararg_func(**kwargs): 35 """ 36 >>> starstararg_func(a=1) 37 1 38 """ 39 return kwargs['a'] 40 41l = lambda x: 1 42 43def test_lambda(): 44 """ 45 >>> l(1) 46 1 47 """ 48 49 50# test classical import fallbacks 51try: 52 from math import fabs 53except ImportError: 54 def fabs(x): 55 if x < 0: 56 return -x 57 else: 58 return x 59 60try: 61 from math import no_such_function 62except ImportError: 63 def no_such_function(x): 64 return x + 1.0 65 66 67def test_import_fallback(): 68 """ 69 >>> fabs(1.0) 70 1.0 71 >>> no_such_function(1.0) 72 2.0 73 >>> test_import_fallback() 74 (1.0, 2.0) 75 """ 76 return fabs(1.0), no_such_function(1.0) 77