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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
PYTHON setup.py build_ext --inplace
PYTHON -c "import a; a.test(a)"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules=cythonize("a.py"),
)
######## a.py ########
class ExtTypePass(object):
pass
class ExtTypePxdDocstring(object):
pass
class ExtTypeDocstring(object):
"""huhu!""" # this should override the .pxd docstring
class ExtTypeAttributes(object):
"""
>>> x = ExtTypeAttributes()
>>> x.b
[1, 2, 3]
"""
def __init__(self):
self.a = 123
self.b = [1, 2, 3]
class TypedMethod():
"""
>>> t = TypedMethod()
>>> t.meth()
97
"""
def meth(self):
x = bytearray(b'abcdefg')
return x[0]
def func(a, b, c):
"""
>>> func(1, 2, 3)
6
"""
return a + b + c
def sum_generator_expression(a):
# GH-3477 - closure variables incorrectly captured in functions transformed to cdef
return sum(i for i in range(a))
def run_sum_generator_expression(a):
"""
>>> run_sum_generator_expression(5)
10
"""
return sum_generator_expression(a)
def test(module):
import os.path
assert not os.path.basename(__file__).endswith('.py'), __file__
assert not os.path.basename(__file__).endswith('.pyc'), __file__
assert not os.path.basename(__file__).endswith('.pyo'), __file__
assert not ExtTypePass().__doc__, ExtTypePass().__doc__
assert ExtTypeDocstring().__doc__ == "huhu!", ExtTypeDocstring().__doc__
assert ExtTypePxdDocstring().__doc__ == "ho, ho, ho!", ExtTypePxdDocstring().__doc__
assert '>>> ' in func.__doc__
import doctest
result = doctest.testmod(module, verbose=True)
assert not result.failed, result.failed
######## a.pxd ########
cimport cython
cdef class ExtTypePass:
pass
cdef class ExtTypePxdDocstring:
"""ho, ho, ho!"""
cdef class ExtTypeAttributes:
cdef int a
cdef readonly list b
cdef class TypedMethod:
@cython.locals(x='char[:]')
cpdef meth(self)
cpdef int func(x, int y, z) except? -1 # argument names should not matter, types should
cdef int sum_generator_expression(int a)
|