[Pyrex] Conditional compilation

David M. Cooke cookedm at physics.mcmaster.ca
Tue Apr 5 02:06:31 CEST 2005


khinsen at cea.fr writes:

> Is there any way to get conditional compilation in Pyrex-generated
> modules? For example, insert code only when WITH_THREAD is defined?
>
> Konrad.

AFAIK, there's no way to do the equivalent of #ifdef. You can try to
fake it with header files and compile-time optimization, though. If
you've got, say, threaded_func() and nonthreaded_func(), only one of
which will exist depending on WITH_THREAD, you could put some magic in
a header file,

someheader.h:

#ifdef WITH_THREAD
# define have_threads 1
int threaded_func(int x, int y, int z);
# define nonthreaded_func(x) 1
#else
# define have_threads 0
# define threaded_func(x,y,z) 1
int nonthreaded_func(int z);
#endif

Then, in your .pyx file:

cdef extern from "someheader.h":
    int have_threads
    int threaded_func(int x, int y, int z)
    int nonthreaded_func(int z)

def a_function():
    if have_threads:
        return threaded_func(x,y,z)
    else:
        return nonthread_func(z)

Not tested, but you get the idea. It's kind of hacky, but it might
work in simple cases. As long as you have the names for all the
functions used defined in both cases, Pyrex will pick them up and use
them without qualms (it doesn't care if they actually exist).

-- 
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke                      http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca



More information about the Pyrex mailing list