Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Cython assertion option in setup.py

My Cython (.pyx) file contains assert and I would like to remove it when I compile the file. I found this post and edited my setup.py as follows.

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

# Before edit
compiler_args = ["-O3", "-ffast-math"]

# Both did not work
# compiler_args = ["-O3", "-ffast-math", "-DPYREX_WITHOUT_ASSERTIONS"]
# compiler_args = ["-O3", "-ffast-math", "-CYTHON_WITHOUT_ASSERTIONS"]

ext_modules = [
               Extension("mycython", sources=["mycython.pyx"],
               extra_compile_args=compiler_args)
              ]

setup(
      name="Test",
      cmdclass={'build_ext': build_ext},
      ext_modules=ext_modules
     )

The error says:

clang: error: unknown argument: '-CYTHON_WITHOUT_ASSERTIONS'

How can I fix it?

like image 494
user51966 Avatar asked Mar 23 '26 11:03

user51966


1 Answers

CYTHON_WITHOUT_ASSERTIONS is a preprocessor macro, so you have to pass it to clang with the -D flag (just like gcc). The first variable's name is actually PYREX_WITHOUT_ASSERTIONS, but to pass it to the preprocessor as a macro (i.e. a part of your clang compiler) you need to add a -D in front of the variable name.

Try compiler_args = ["-O3", "-ffast-math", "-DCYTHON_WITHOUT_ASSERTIONS"] instead (note the D in front of CYTHON_WITHOUT_ASSERTIONS).

HTH.

like image 150
Matt Messersmith Avatar answered Mar 26 '26 10:03

Matt Messersmith