1PYTHON setup.py build_ext --inplace 2PYTHON -c "import a; a.test(a)" 3 4######## setup.py ######## 5 6from Cython.Build.Dependencies import cythonize 7 8from distutils.core import setup 9 10setup( 11 ext_modules=cythonize("a.py"), 12) 13 14 15######## a.py ######## 16 17class ExtTypePass(object): 18 pass 19 20 21class ExtTypePxdDocstring(object): 22 pass 23 24 25class ExtTypeDocstring(object): 26 """huhu!""" # this should override the .pxd docstring 27 28 29class ExtTypeAttributes(object): 30 """ 31 >>> x = ExtTypeAttributes() 32 >>> x.b 33 [1, 2, 3] 34 """ 35 def __init__(self): 36 self.a = 123 37 self.b = [1, 2, 3] 38 39 40class TypedMethod(): 41 """ 42 >>> t = TypedMethod() 43 >>> t.meth() 44 97 45 """ 46 def meth(self): 47 x = bytearray(b'abcdefg') 48 return x[0] 49 50 51def func(a, b, c): 52 """ 53 >>> func(1, 2, 3) 54 6 55 """ 56 return a + b + c 57 58 59def test(module): 60 import os.path 61 assert not os.path.basename(__file__).endswith('.py'), __file__ 62 assert not os.path.basename(__file__).endswith('.pyc'), __file__ 63 assert not os.path.basename(__file__).endswith('.pyo'), __file__ 64 65 assert not ExtTypePass().__doc__, ExtTypePass().__doc__ 66 assert ExtTypeDocstring().__doc__ == "huhu!", ExtTypeDocstring().__doc__ 67 assert ExtTypePxdDocstring().__doc__ == "ho, ho, ho!", ExtTypePxdDocstring().__doc__ 68 assert '>>> ' in func.__doc__ 69 70 import doctest 71 result = doctest.testmod(module, verbose=True) 72 assert not result.failed, result.failed 73 74 75######## a.pxd ######## 76 77cimport cython 78 79cdef class ExtTypePass: 80 pass 81 82 83cdef class ExtTypePxdDocstring: 84 """ho, ho, ho!""" 85 86 87cdef class ExtTypeAttributes: 88 cdef int a 89 cdef readonly list b 90 91 92cdef class TypedMethod: 93 @cython.locals(x='char[:]') 94 cpdef meth(self) 95 96 97cpdef int func(x, int y, z) except? -1 # argument names should not matter, types should 98