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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
# mode: run
# tag: cimport
PYTHON setup.py build_ext --inplace
PYTHON -c "from py_pkg.py_a import test; test()"
PYTHON -c "from pkg.a import test; assert test() == (5, 7)"
PYTHON -c "from pkg.b import test; assert test() == (1, 2)"
PYTHON -c "from pkg.b_py2 import test; assert test() == (1, 2)"
PYTHON -c "from pkg.sub.c import test; assert test() == (1, 2)"
PYTHON -c "from pkg.sub.d import test; assert test() == 3"
# File structure:
# ├── setup.py
# ├── py_pkg/
# | ├── __init__.py
# | ├── py_a.pyx
# | ├── py_b.pxd
# | └── py_sub/
# | ├── __init__.py
# | ├── py_c.pxd
# | └── py_d.pxd
# └── pkg/
# ├── __init__.py
# ├── a.pyx
# ├── a.pxd
# ├── b.pyx
# ├── b_py2.pyx
# └── sub/
# ├── __init__.py
# ├── __init__.pxd
# ├── c.pyx
# ├── c.pxd
# ├── d.pyx
# ├── tdef.pxd
# └── reimport.pxd
######## setup.py ########
from distutils.core import setup
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension
setup(
ext_modules=cythonize('**/*.pyx'),
)
######## py_pkg/__init__.py ########
######## py_pkg/py_a.pyx ########
from .py_sub cimport py_c
def test():
cdef py_c.py_b.py_b_int b = 1
assert b == 1
cdef py_c.py_c_int c = 2
assert c == 2
cdef py_c.py_d.py_d_int d = 3
assert d == 3
######## py_pkg/py_b.pxd ########
ctypedef int py_b_int
######## py_pkg/py_sub/__init__.py ########
######## py_pkg/py_sub/py_c.pxd ########
from .. cimport py_b
from . cimport py_d
ctypedef int py_c_int
######## py_pkg/py_sub/py_d.pxd ########
ctypedef int py_d_int
######## pkg/__init__.py ########
######## pkg/sub/__init__.py ########
######## pkg/a.pyx ########
from .sub.reimport cimport myint
from .sub cimport c
cdef myint i = 5
assert i == 5
assert c.sum_func(1, 2) == 3
from .sub cimport myint as pkg_myint
cdef pkg_myint pi = 7
assert pi == 7
cdef class test_pxd:
pass
def test():
return (i, pi)
######## pkg/a.pxd ########
cdef class test_pxd:
cdef public int x
cdef public int y
######## pkg/b.pyx ########
from . cimport a
from .a cimport test_pxd
assert a.test_pxd is test_pxd
def test():
cdef test_pxd obj = test_pxd()
obj.x = 1
obj.y = 2
return (obj.x, obj.y)
######## pkg/b_py2.pyx ########
# cython: language_level=2
from . cimport a
from .a cimport test_pxd
cimport a as implicitly_relative_a # <-- Py2 "feature"
assert a.test_pxd is test_pxd
assert implicitly_relative_a.test_pxd is test_pxd
def test():
cdef test_pxd obj = test_pxd()
obj.x = 1
obj.y = 2
return (obj.x, obj.y)
######## pkg/sub/c.pyx ########
from ..a cimport test_pxd
def test():
cdef test_pxd obj = test_pxd()
obj.x = 1
obj.y = 2
return (obj.x, obj.y)
cdef int sum_func(int n, int m):
return n + m
######## pkg/sub/c.pxd ########
cdef int sum_func(int n, int m)
######## pkg/sub/d.pyx ########
from . cimport c
def test():
return c.sum_func(1, 2)
######## pkg/sub/tdef.pxd ########
ctypedef int myint
######## pkg/sub/reimport.pxd ########
from .tdef cimport myint
######## pkg/sub/__init__.pxd ########
from .tdef cimport myint
from . cimport c
|