Commit daaac441 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2405 from gabrieldemarmiesse/test_pure_12

Adding tests for "pure python mode" part 12
parents 373cc5de 7ddb28ca
# mymodule.pxd
# declare a C function as "cpdef" to export it to the module
cdef extern from "math.h":
cpdef double sin(double x)
# mymodule.py
import cython
# override with Python import if not in compiled code
if not cython.compiled:
from math import sin
# calls sin() from math.h when compiled with Cython and math.sin() in Python
print(sin(0))
......@@ -299,25 +299,11 @@ Calling C functions
Normally, it isn't possible to call C functions in pure Python mode as there
is no general way to support it in normal (uncompiled) Python. However, in
cases where an equivalent Python function exists, this can be achieved by
combining C function coercion with a conditional import as follows::
combining C function coercion with a conditional import as follows:
# in mymodule.pxd:
.. literalinclude:: ../../examples/tutorial/pure/mymodule.pxd
# declare a C function as "cpdef" to export it to the module
cdef extern from "math.h":
cpdef double sin(double x)
# in mymodule.py:
import cython
# override with Python import if not in compiled code
if not cython.compiled:
from math import sin
# calls sin() from math.h when compiled with Cython and math.sin() in Python
print(sin(0))
.. literalinclude:: ../../examples/tutorial/pure/mymodule.py
Note that the "sin" function will show up in the module namespace of "mymodule"
here (i.e. there will be a ``mymodule.sin()`` function). You can mark it as an
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment