| 1 |
# mode: run |
| 2 |
# tag: cimport |
| 3 |
|
| 4 |
PYTHON setup.py build_ext --inplace |
| 5 |
PYTHON -c "from py_pkg.py_a import test; test()" |
| 6 |
PYTHON -c "from pkg.a import test; assert test() == (5, 7)" |
| 7 |
PYTHON -c "from pkg.b import test; assert test() == (1, 2)" |
| 8 |
PYTHON -c "from pkg.b_py2 import test; assert test() == (1, 2)" |
| 9 |
PYTHON -c "from pkg.sub.c import test; assert test() == (1, 2)" |
| 10 |
PYTHON -c "from pkg.sub.d import test; assert test() == 3" |
| 11 |
|
| 12 |
# File structure: |
| 13 |
# ├── setup.py |
| 14 |
# ├── py_pkg/ |
| 15 |
# | ├── __init__.py |
| 16 |
# | ├── py_a.pyx |
| 17 |
# | ├── py_b.pxd |
| 18 |
# | └── py_sub/ |
| 19 |
# | ├── __init__.py |
| 20 |
# | ├── py_c.pxd |
| 21 |
# | └── py_d.pxd |
| 22 |
# └── pkg/ |
| 23 |
# ├── __init__.py |
| 24 |
# ├── a.pyx |
| 25 |
# ├── a.pxd |
| 26 |
# ├── b.pyx |
| 27 |
# ├── b_py2.pyx |
| 28 |
# └── sub/ |
| 29 |
# ├── __init__.py |
| 30 |
# ├── __init__.pxd |
| 31 |
# ├── c.pyx |
| 32 |
# ├── c.pxd |
| 33 |
# ├── d.pyx |
| 34 |
# ├── tdef.pxd |
| 35 |
# └── reimport.pxd |
| 36 |
|
| 37 |
######## setup.py ######## |
| 38 |
|
| 39 |
from distutils.core import setup |
| 40 |
from Cython.Build import cythonize |
| 41 |
from Cython.Distutils.extension import Extension |
| 42 |
|
| 43 |
setup( |
| 44 |
ext_modules=cythonize('**/*.pyx'), |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
######## py_pkg/__init__.py ######## |
| 49 |
|
| 50 |
######## py_pkg/py_a.pyx ######## |
| 51 |
|
| 52 |
from .py_sub cimport py_c |
| 53 |
|
| 54 |
def test(): |
| 55 |
cdef py_c.py_b.py_b_int b = 1 |
| 56 |
assert b == 1 |
| 57 |
cdef py_c.py_c_int c = 2 |
| 58 |
assert c == 2 |
| 59 |
cdef py_c.py_d.py_d_int d = 3 |
| 60 |
assert d == 3 |
| 61 |
|
| 62 |
######## py_pkg/py_b.pxd ######## |
| 63 |
|
| 64 |
ctypedef int py_b_int |
| 65 |
|
| 66 |
######## py_pkg/py_sub/__init__.py ######## |
| 67 |
|
| 68 |
######## py_pkg/py_sub/py_c.pxd ######## |
| 69 |
|
| 70 |
from .. cimport py_b |
| 71 |
from . cimport py_d |
| 72 |
|
| 73 |
ctypedef int py_c_int |
| 74 |
|
| 75 |
######## py_pkg/py_sub/py_d.pxd ######## |
| 76 |
|
| 77 |
ctypedef int py_d_int |
| 78 |
|
| 79 |
######## pkg/__init__.py ######## |
| 80 |
|
| 81 |
######## pkg/sub/__init__.py ######## |
| 82 |
|
| 83 |
######## pkg/a.pyx ######## |
| 84 |
|
| 85 |
from .sub.reimport cimport myint |
| 86 |
from .sub cimport c |
| 87 |
|
| 88 |
cdef myint i = 5 |
| 89 |
assert i == 5 |
| 90 |
assert c.sum_func(1, 2) == 3 |
| 91 |
|
| 92 |
from .sub cimport myint as pkg_myint |
| 93 |
|
| 94 |
cdef pkg_myint pi = 7 |
| 95 |
assert pi == 7 |
| 96 |
|
| 97 |
cdef class test_pxd: |
| 98 |
pass |
| 99 |
|
| 100 |
|
| 101 |
def test(): |
| 102 |
return (i, pi) |
| 103 |
|
| 104 |
|
| 105 |
######## pkg/a.pxd ######## |
| 106 |
|
| 107 |
cdef class test_pxd: |
| 108 |
cdef public int x |
| 109 |
cdef public int y |
| 110 |
|
| 111 |
|
| 112 |
######## pkg/b.pyx ######## |
| 113 |
|
| 114 |
from . cimport a |
| 115 |
from .a cimport test_pxd |
| 116 |
|
| 117 |
assert a.test_pxd is test_pxd |
| 118 |
|
| 119 |
def test(): |
| 120 |
cdef test_pxd obj = test_pxd() |
| 121 |
obj.x = 1 |
| 122 |
obj.y = 2 |
| 123 |
return (obj.x, obj.y) |
| 124 |
|
| 125 |
|
| 126 |
######## pkg/b_py2.pyx ######## |
| 127 |
|
| 128 |
# cython: language_level=2 |
| 129 |
|
| 130 |
from . cimport a |
| 131 |
from .a cimport test_pxd |
| 132 |
cimport a as implicitly_relative_a # <-- Py2 "feature" |
| 133 |
|
| 134 |
assert a.test_pxd is test_pxd |
| 135 |
assert implicitly_relative_a.test_pxd is test_pxd |
| 136 |
|
| 137 |
def test(): |
| 138 |
cdef test_pxd obj = test_pxd() |
| 139 |
obj.x = 1 |
| 140 |
obj.y = 2 |
| 141 |
return (obj.x, obj.y) |
| 142 |
|
| 143 |
|
| 144 |
######## pkg/sub/c.pyx ######## |
| 145 |
|
| 146 |
from ..a cimport test_pxd |
| 147 |
|
| 148 |
|
| 149 |
def test(): |
| 150 |
cdef test_pxd obj = test_pxd() |
| 151 |
obj.x = 1 |
| 152 |
obj.y = 2 |
| 153 |
return (obj.x, obj.y) |
| 154 |
|
| 155 |
cdef int sum_func(int n, int m): |
| 156 |
return n + m |
| 157 |
|
| 158 |
######## pkg/sub/c.pxd ######## |
| 159 |
|
| 160 |
cdef int sum_func(int n, int m) |
| 161 |
|
| 162 |
######## pkg/sub/d.pyx ######## |
| 163 |
|
| 164 |
from . cimport c |
| 165 |
|
| 166 |
def test(): |
| 167 |
return c.sum_func(1, 2) |
| 168 |
|
| 169 |
######## pkg/sub/tdef.pxd ######## |
| 170 |
|
| 171 |
ctypedef int myint |
| 172 |
|
| 173 |
|
| 174 |
######## pkg/sub/reimport.pxd ######## |
| 175 |
|
| 176 |
from .tdef cimport myint |
| 177 |
|
| 178 |
|
| 179 |
######## pkg/sub/__init__.pxd ######## |
| 180 |
|
| 181 |
from .tdef cimport myint |
| 182 |
from . cimport c |