cython

Форк
0
/
pure_pxd.srctree 
111 строк · 2.1 Кб
1
PYTHON setup.py build_ext --inplace
2
PYTHON -c "import a; a.test(a)"
3

4
######## setup.py ########
5

6
from Cython.Build.Dependencies import cythonize
7

8
from distutils.core import setup
9

10
setup(
11
    ext_modules=cythonize("a.py"),
12
)
13

14

15
######## a.py ########
16

17
class ExtTypePass(object):
18
    pass
19

20

21
class ExtTypePxdDocstring(object):
22
    pass
23

24

25
class ExtTypeDocstring(object):
26
    """huhu!"""  # this should override the .pxd docstring
27

28

29
class 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

40
class 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

51
def func(a, b, c):
52
    """
53
    >>> func(1, 2, 3)
54
    6
55
    """
56
    return a + b + c
57

58
def sum_generator_expression(a):
59
    # GH-3477 - closure variables incorrectly captured in functions transformed to cdef
60
    return sum(i for i in range(a))
61

62
def run_sum_generator_expression(a):
63
    """
64
    >>> run_sum_generator_expression(5)
65
    10
66
    """
67
    return sum_generator_expression(a)
68

69

70
def test(module):
71
    import os.path
72
    assert not os.path.basename(__file__).endswith('.py'), __file__
73
    assert not os.path.basename(__file__).endswith('.pyc'), __file__
74
    assert not os.path.basename(__file__).endswith('.pyo'), __file__
75

76
    assert not ExtTypePass().__doc__, ExtTypePass().__doc__
77
    assert ExtTypeDocstring().__doc__ == "huhu!", ExtTypeDocstring().__doc__
78
    assert ExtTypePxdDocstring().__doc__ == "ho, ho, ho!", ExtTypePxdDocstring().__doc__
79
    assert '>>> ' in func.__doc__
80

81
    import doctest
82
    result = doctest.testmod(module, verbose=True)
83
    assert not result.failed, result.failed
84

85

86
######## a.pxd ########
87

88
cimport cython
89

90
cdef class ExtTypePass:
91
    pass
92

93

94
cdef class ExtTypePxdDocstring:
95
    """ho, ho, ho!"""
96

97

98
cdef class ExtTypeAttributes:
99
    cdef int a
100
    cdef readonly list b
101

102

103
cdef class TypedMethod:
104
    @cython.locals(x='char[:]')
105
    cpdef meth(self)
106

107

108
cpdef int func(x, int y, z) except? -1  # argument names should not matter, types should
109

110

111
cdef int sum_generator_expression(int a)
112

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.