JaviMerino
JaviMerino

Reputation: 619

Regenerate cython extension with distutils

I have a cython file that generates a different .c file depending on whether it's been compiled for python 2 or python 3. That is:

from .mp_utils import PY3

if PY3:
    builtin = (int, float, str, complex)
else:
    builtin = (int, float, str, long, complex, file)

with mp_utils having this:

PY3 = sys.version > '3'

This extension is built by distutils. setup.py has:

getsize = Extension(
    'memprof.getsize',
    sources=['memprof/getsize.pyx']
)

setup(
    # [...]
    cmdclass={'build_ext': build_ext},
    ext_modules=[getsize],
)

However, cython is not aware that it has to rebuild the .c on every invocation of python setup.py install:

# python3 setup.py test
running test
running egg_info
writing dependency_links to memprof.egg-info/dependency_links.txt
writing requirements to memprof.egg-info/requires.txt
writing memprof.egg-info/PKG-INFO
writing top-level names to memprof.egg-info/top_level.txt
reading manifest file 'memprof.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'memprof.egg-info/SOURCES.txt'
running build_ext
skipping 'memprof/getsize.c' Cython extension (up-to-date)
building 'memprof.getsize' extension
[...]

This is wrong, memprof/getsize.c is not up to date, it's the getsize.c built for the python 2 version. It has to be rebuilt. How can I tell distutils (or cython) that it has to regenerate the .c file on every invocation?

I know that I can touch memprof/getsize.pyx or just delete memprof/getsize.c by hand but you have to remember to do that and it's not what I'm looking for. I want something that when I do python3 setup.py install it installs the right thing no matter what I built before.

Upvotes: 3

Views: 1249

Answers (1)

jmdana
jmdana

Reputation: 439

setup.py options can be defined in a setup.cfg file.

Therefore, something like this in your setup.cfg:

[build_ext]
force=1

will force the compilation each time.

Upvotes: 2

Related Questions