Adds some preprocessor capabilities to cython
Python Shell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is Cython and Unifdef wrapper that adds some (very basic) preprocessor support to Cython. makes it possible to use preprocessor directives right in your .pyx source:

class X:

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

Running 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 can process: gevent/core_.pyx.


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.


  • 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 is written by Denis Bilenko for gevent project and is licensed under MIT license.