How to override the -DNDEBUG flag when creating a cython module

I have a Cython module that calls a C ++ function via cdef extern . The C ++ function has assert() , and I would like to verify these statements. However, when I create a module by calling python setup.py build_ext --inplace , gcc is always called using -DNDEBUG . Whenever code is executed, statements are not checked.

I cannot find a way to override -DNDEBUG with setup.py . Is it possible?

Currently, the only way I've come across is to manually call cython, gcc and g ++ with the parameters that python setup.py uses, but take out -DNDEBUG . But there should be an easier way.

+6
source share
2 answers

You can manually define NDEBUG , if one is defined, before including <cassert> . Add the following lines to the top of the cpp file that contains these statements. Make sure these are the very first statements in this file.

 #ifdef NDEBUG # define NDEBUG_DISABLED # undef NDEBUG #endif #include <cassert> #ifdef NDEBUG_DISABLED # define NDEBUG // re-enable NDEBUG if it was originally enabled #endif // rest of the file 

This ensures that NDEBUG not detected when the processor includes <cassert> , which will cause verification checks to be compiled into your code.

0
source

You can define NDEBUG in the setup.py file. Just use the undef_macros parameter when defining the extension:

 extensions = [ Extension ( "myprog", [ mysrc.c ], undef_macros = [ "NDEBUG" ] ) ] 

In the output of the assembly, you will see -DNDEBUG followed by -UNDEBUG, which overrides it. See the distutils documentation for more information on extension options.

Note, however, that a statement initiated in the extension module will lead to the exit of the python or ipython interpreter.

+6
source

Source: https://habr.com/ru/post/970978/


All Articles