Cpython and Cpython hook -1

test.pyx

1
2
3
4
5
6
def a(arg):
print(arg)


def b(arg):
print(arg)

setup.py

1
2
3
4
5
6
from setuptools import setup
from Cython.Build import cythonize

setup(
ext_modules=cythonize("./test.pyx")
)

编译指令:

1
python .\setup.py build_ext --inplace

这里–inplace可不要。添加该选项可让编译的库输出到工作目录下
test_import.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import test
print(dir(test))

a = test.a


def hook_a(arg):
print("hook a")
a(arg)


test.a = hook_a
test.a(1)

1
2
3
['__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__test__', 'a', 'b']
hook a
1