I need to generate a pyx file several times and run its recompilation and reload corresponding extension into the program in one runtime. Here is a simplified example:
from setuptools import Extension, setup
from Cython.Build import cythonize
import sys
pyxfile = "foo.pyx"
def write_pyx(incval):
with open(pyxfile, 'w') as f:
f.write('cpdef int foo(int x):\n return x+%i' % incval)
def ext_compile():
oldargv = sys.argv
sys.argv = ['thisfile.py', 'build_ext', '--inplace']
setup(
ext_modules=cythonize(
[ Extension("example", [pyxfile]) ],
compiler_directives={'language_level': 2}
)
)
sys.argv = oldargv
write_pyx(1)
ext_compile()
import example
print "foo(1) =", example.foo(1)
write_pyx(10)
ext_compile()
reload(example)
print "foo(1) =", example.foo(1)
However, when executed, despite the changing pyx file, I have only one compilation. This is the output in the console:
Compiling foo.pyx because it changed.
[1/1] Cythonizing foo.pyx
running build_ext
building 'example' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/artem/.pyenv/versions/2.7.16/include/python2.7 -c foo.c -o build/temp.linux-x86_64-2.7/foo.o
gcc -pthread -shared -L/home/artem/.pyenv/versions/2.7.16/lib build/temp.linux-x86_64-2.7/foo.o -o build/lib.linux-x86_64-2.7/example.so
copying build/lib.linux-x86_64-2.7/example.so ->
foo(1) = 2
running build_ext
copying build/lib.linux-x86_64-2.7/example.so ->
foo(1) = 2
Any idea how can I solve this?