Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (29 sloc) 1.98 KB

This is Cython and Unifdef wrapper that adds some (very basic) preprocessor support to Cython.

cython_ifdef.py makes it possible to use preprocessor directives right in your .pyx source:

class X:

#ifdef _WIN32
    def windows_only(self):
        return 5
#else
    def unix_only(self):
        return 6
#endif

Running cython_ifdef.py test.pyx will produce a test.c file that includes appropriate #ifdef _WIN32 to produce a class that has windows_only method when _WIN32 is defined and unix_only when it's not.

Here's an example of what cython_ifdef.py can process: gevent/core_.pyx.

Installation

In addition to Cython, you need to have unifdef installed in order to run this script. On Debian/Ubuntu, do apt-get install unifdef.

Download the script from github and put it somewhere on the run path.

How does it work

  • It gets lists of all preprocessor symbols used in the source file using unifdef -t -s
  • It runs unifdef for all possible configurations on the original source.
  • For each generated source, it runs cython and stores the result in memory.
  • It then merges all the resulting .c files into one with the appropriate #ifdef in place.

Limitations

  • It only supports symbols that are either defined or undefined. Processing expressions such as #if SYMBOL == 5 is not implemented.
  • It does not look into included .pxi.
  • The amount of time it takes is exponential: 2^(Number of symbols), so it does not support arbitrary amount of preprocessor symbols.

It was written to support gevent's use case rather than a generic tool, so be warned.

Author

cython_ifdef.py is written by Denis Bilenko for gevent project and is licensed under MIT license.