[Cython] Conditional import in pure Python mode

Stefan Behnel stefan_ml at behnel.de
Wed May 2 15:49:44 CEST 2012

Stefan Behnel, 01.05.2012 21:14:
> 1) Write up a math.pxd that contains declarations equivalent to Python's
> math module. Note that this may not be entirely trivial because the math
> module does some error handling and type special casing under the hood.

Having taken a slightly deeper look at this now, I think it would make
sense to start with this part. An equivalent implementation will need to do
the same kind of error handling, so there will be more than just libm
function declarations in the file. Inline functions work in .pxd files
(although I'm not sure inlining is really a good idea here).

> Some of this may still be required for the C level equivalents, although
> the type special casing would better be done by overriding function
> signatures using this feature:
> http://docs.cython.org/src/userguide/external_C_code.html#resolving-naming-conflicts-c-name-specifications
> Basically, you would declare two (or more) function signatures under the
> same name, but with different C names.

Given that most functions work on double values and return double, this
won't be an issue. The functions that return integers are an issue, though,
because Python can easily handle this under the hood by simply returning
arbitrary sized integer objects. A C version cannot safely return C
integers without risking overflows. We should leave those out entirely for
now and just fall back to the normal Python implementation.

Ian, could you give the .pxd file a try? You should be able to test this by
importing math, followed by a cimport of your new math module.


More information about the cython-devel mailing list