How to remove python statement when compiling in cython?

So here is my problem: I have code in python, but I need to improve performance in some parts of my code that are too slow. A good (and simple) solution seems to use cython; I tried and got good results. The problem is that I am using the assert statement in my python code. Before using cython, I could compile my python code with the -OO option so that I can deliver a version that does not perform any approval test and still have assert for debugging. But files that are compiled in cython seem to always fulfill statements. Are there any options that can be passed to cython compilation to remove (or not delete) statements?

+5
source share
2 answers

Cython skips statements if you define a C preprocessor macro PYREX_WITHOUT_ASSERTIONS. Therefore, pass -DPYREX_WITHOUT_ASSERTIONSC to the compiler when compiling the generated file C. How it depends on your build process.

+7
source

Use pypreprocessor

Which can also be found on PYPI (the Python package index) and obtained 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 way as conditional compilation of the standard C preprocessor.

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

Disclaimer: I am the author of pypreprocessor.

, - , - (.pyc) .

0

All Articles