def test_struct(n: cython.int, x: cython.double) -> MyStruct2: """ >>> test_struct(389, 1.64493) (389, 1.64493) >>> d = test_struct.__annotations__ >>> sorted(d) ['n', 'return', 'x'] """ assert cython.typeof(n) == 'int', cython.typeof(n) if is_compiled: assert cython.typeof(x) == 'double', cython.typeof(x) # C double else: assert cython.typeof(x) == 'float', cython.typeof(x) # Python float a = cython.declare(MyStruct2) a[0] = MyStruct(is_integral=True, data=MyUnion(n=n)) a[1] = MyStruct(is_integral=False, data={'x': x}) return a[0].data.n, a[1].data.x
def test_return_type(n: cython.int) -> cython.double: """ >>> test_return_type(389) 389.0 """ assert cython.typeof(n) == 'int', cython.typeof(n) return n if is_compiled else float(n)
def test_subscripted_types(): """ >>> test_subscripted_types() dict object list object set object """ a: typing.Dict[int, float] = {} b: List[int] = [] c: _SET_[object] = set() print(cython.typeof(a) + (" object" if not cython.compiled else "")) print(cython.typeof(b) + (" object" if not cython.compiled else "")) print(cython.typeof(c) + (" object" if not cython.compiled else ""))
def func2_inpy(self, arg): """ >>> TestCls().func2_inpy(1.0) 'float' >>> TestCls().func2_inpy(2) 'int' """ return cython.typeof(arg)
def cpfunc(self, arg): """ >>> TestCls().cpfunc(1.0) 'float' >>> TestCls().cpfunc(2) 'int' """ return cython.typeof(arg)
def func1(self, arg: 'NotInPy'): """ >>> TestCls().func1(1.0) 'float' >>> TestCls().func1(2) 'int' """ return cython.typeof(arg)
def test_tuple(a: typing.Tuple[int, float], b: typing.Tuple[int, ...], c: Tuple[int, object] # cannot be a ctuple ): """ >>> test_tuple((1, 1.0), (1, 1.0), (1, 1.0)) int int tuple object tuple object """ x: typing.Tuple[int, float] = (a[0], a[1]) y: Tuple[int, ...] = (1,2.) z = a[0] # should infer to int print(cython.typeof(z)) print(cython.typeof(x[0])) print(cython.typeof(y) + (" object" if not cython.compiled else "")) print(cython.typeof(c) + (" object" if not cython.compiled else ""))
def fused_func_0(x: IntOrFloat = 0): """ Fused functions can legitimately take 0 arguments >>> fused_func_0() ('int', 'int') # subscripted in module __doc__ conditionally """ return (type(x).__name__, cython.typeof(x))
def call_cdef_inline(x): """ >>> result, return_type = call_cdef_inline(1) >>> (not is_compiled and 'float') or type(result).__name__ 'float' >>> (not is_compiled and 'double') or return_type 'double' >>> (is_compiled and 'int') or return_type 'int' >>> result == 2.0 or result True """ ret = cdef_inline(x) return ret, cython.typeof(ret)
def call_cdef_inline(x): """ >>> result, return_type = call_cdef_inline(1) >>> (not is_compiled and 'float') or type(result).__name__ 'float' >>> (not is_compiled and 'double') or return_type 'double' >>> (is_compiled and 'int') or return_type 'int' >>> result == 2.0 or result True >>> call_cdef_inline(-2) Traceback (most recent call last): RuntimeError: huhu! """ ret = cdef_inline(x) return ret, cython.typeof(ret)
def call_ccall(x): """ Test that a declared return type is honoured when compiled. >>> result, return_type = call_ccall(1) >>> (not is_compiled and 'double') or return_type 'double' >>> (is_compiled and 'int') or return_type 'int' >>> (not is_compiled and 1.0) or result 1.0 >>> (is_compiled and 1) or result 1 """ ret = c_call(x) return ret, cython.typeof(ret)
def call_ccall(x): """ Test that a declared return type is honoured when compiled. >>> result, return_type = call_ccall(1) >>> (not is_compiled and 'double') or return_type 'double' >>> (is_compiled and 'int') or return_type 'int' >>> (not is_compiled and 1.0) or result 1.0 >>> (is_compiled and 1) or result 1 >>> call_ccall(-2) Traceback (most recent call last): RuntimeError: huhu! """ ret = c_call(x) return ret, cython.typeof(ret)
def regular_func(x): return (type(x).__name__, cython.typeof(x))
def fused_func(x: MyFusedClass): return (type(x).__name__, cython.typeof(x))
def fused_in_class(self, x: MyFusedClass): return (type(x).__name__, cython.typeof(x))
def call_cdef_inline(x): ret = cdef_inline(x) return ret, cython.typeof(ret)
def call_ccall(x): ret = c_call(x) return ret, cython.typeof(ret)
def get_attr(self): print(cython.typeof(self.attr)) return self.attr
def gen(con, symbol_table): func = formatter = None # class type if con is None: def func(value): return value is None elif isinstance(con, tuple): classes = tuple([(cls if cls is not None else type(None)) for cls in con]) def func(value): return isinstance(value, classes) elif _inspect.isclass(con): def func(value): return isinstance(value, con) # in elif isinstance(con, list): # 引数がジェネレータなら常に真を返す def func(value): if isinstance(value, _types.GeneratorType): return True else: return value in con # set in elif isinstance(con, set): # 引数がジェネレータなら常に真を、strなら偽を返す def func(value): # list, tuple, set, dictを想定 if isinstance(value, _types.GeneratorType): return True elif isinstance(value, str): return False else: return all(((ele in con) for ele in value)) # **kwargs elif isinstance(con, dict): d = {} for name, prop_cons in con.items(): if not isinstance(prop_cons, tuple): prop_cons = (prop_cons,) d[name] = self._gen_arg_condition(prop_cons, symbol_table) def func(value): result_all = [] for name, val in value.items(): if name in d: ls = d[name] results = [f(val) for f in ls] if ls.formatter: r = eval(ls.formatter.format(*results), symbol_table) else: r = all(results) result_all.append(r) return all(result_all) # function(str) / formatter elif isinstance(con, str): if con.startswith('lambda '): func = eval(con, symbol_table) elif con.startswith('def '): local_dict = {} exec(con, symbol_table, local_dict) if local_dict: func = next(iter(local_dict.values())) else: msg = 'Cannot convert str -> Function' + con raise InvalidConditionError(msg) else: formatter = con # function elif (isinstance(con, function_types) or _cython and _cython.typeof(con) == 'cython_function_or_method'): func = con return func, formatter