1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
cimport cython
@cython.test_assert_path_exists(
'//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]')
def get_type_of(a):
"""
>>> get_type_of(object()) is object
True
"""
return type(a)
@cython.test_assert_path_exists(
'//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]')
def get_type_through_local(a):
"""
>>> get_type_of(object()) is object
True
"""
t = type(a)
return t
@cython.test_assert_path_exists(
'//PythonCapiCallNode/PythonCapiFunctionNode[@cname="Py_TYPE"]')
@cython.test_fail_if_path_exists(
'//PythonCapiCallNode/PythonCapiFunctionNode[@cname="__Pyx_Type"]',
'//NameNode[@name="type"]')
def test_type(a, t):
"""
>>> test_type(object(), object)
True
"""
return type(a) and type(a) is t and type(a) == t
@cython.test_assert_path_exists('//NameNode[@name="type"]')
def type_type():
"""
>>> type_type()(object()) is object
True
"""
return type
cpdef type pass_type(type x):
"""
>>> pass_type(int) == int
True
>>> class MyType(object): pass
>>> pass_type(MyType) == MyType
True
>>> pass_type(object())
Traceback (most recent call last):
TypeError: Argument 'x' has incorrect type (expected type, got object)
"""
return x
|