How to remove python assertion when compiling in cython?

若如初见. 提交于 2019-12-04 05:18:31

Cython skips the assertions if you define the C preprocessor macro PYREX_WITHOUT_ASSERTIONS. So pass -DPYREX_WITHOUT_ASSERTIONS to the C compiler when compiling the generated C file. How to to this depends on your build process.

Use pypreprocessor

Which can also be found on PYPI (Python Package Index) and be fetched using pip.

Here's the implementation:

from pypreprocessor import pypreprocessor

pypreprocessor.parse()

#define debug

#ifdef debug
...place assert to be removed here...
#endif

This essentially works the same as the standard C preprocessor conditional compilation does.

SideNote: This module is compatible with both python2x and python3k.

Disclaimer: I'm the author of pypreprocessor.

This will make the initial load take longer due to the added preprocessor stage but the outputted bytecode (.pyc) will be optimized.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!