Commit 696a1959 authored by Matus Valo's avatar Matus Valo Committed by GitHub

docs: Add Pure Python mode to "Calling C functions" tutorial (GH-4247)

parent 8552e3d3
from cython.cimports.libc.stdlib import atoi
@cython.cfunc
def parse_charptr_to_py_int(s: cython.p_char):
assert s is not cython.NULL, "byte string value is NULL"
return atoi(s) # note: atoi() has no error detection!
from libc.stdlib cimport atoi
cdef parse_charptr_to_py_int(char* s):
assert s is not NULL, "byte string value is NULL"
return atoi(s) # note: atoi() has no error detection!
import cython
from cython.cimports.strstr import strstr
def main():
data: p_char = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
pos: p_char = strstr(needle='akd', haystack=data)
print(pos is not cython.NULL)
cdef extern from "string.h":
char* strstr(const char *haystack, const char *needle)
cdef char* data = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
cdef char* pos = strstr(needle='akd', haystack=data)
......
from cython.cimports.libc.math import sin
@cython.cfunc
def f(x: cython.double) -> cython.double:
return sin(x * x)
from libc.math cimport sin
cdef double f(double x):
return sin(x * x)
from cython.cimports.cpython.version import PY_VERSION_HEX
# Python version >= 3.2 final ?
print(PY_VERSION_HEX >= 0x030200F0)
cdef extern from "string.h":
char* strstr(const char *haystack, const char *needle)
Calling C functions
====================
.. include::
../two-syntax-variants-used
This tutorial describes shortly what you need to know in order to call
C library functions from Cython code. For a longer and more
comprehensive tutorial about using external C libraries, wrapping them
......@@ -15,7 +18,17 @@ For example, let's say you need a low-level way to parse a number from
a ``char*`` value. You could use the ``atoi()`` function, as defined
by the ``stdlib.h`` header file. This can be done as follows:
.. literalinclude:: ../../examples/tutorial/external/atoi.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/atoi.py
:caption: atoi.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/atoi.pyx
:caption: atoi.pyx
You can find a complete list of these standard cimport files in
Cython's source package
......@@ -28,14 +41,33 @@ Cython also has a complete set of declarations for CPython's C-API.
For example, to test at C compilation time which CPython version
your code is being compiled with, you can do this:
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.py
:caption: py_version_hex.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/py_version_hex.pyx
:caption: py_version_hex.pyx
.. _libc.math:
Cython also provides declarations for the C math library:
.. literalinclude:: ../../examples/tutorial/external/libc_sin.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/libc_sin.py
:caption: libc_sin.py
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/libc_sin.pyx
:caption: libc_sin.pyx
Dynamic linking
---------------
......@@ -83,6 +115,9 @@ This allows the C declaration to be reused in other Cython modules,
while still providing an automatically generated Python wrapper in
this specific module.
.. note:: External declarations must be placed in a ``.pxd`` file in Pure
Python mode.
Naming parameters
-----------------
......@@ -103,7 +138,19 @@ You can now make it clear which of the two arguments does what in
your call, thus avoiding any ambiguities and often making your code
more readable:
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.pyx
.. tabs::
.. group-tab:: Pure Python
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.py
:caption: keyword_args_call.py
.. literalinclude:: ../../examples/tutorial/external/strstr.pxd
:caption: strstr.pxd
.. group-tab:: Cython
.. literalinclude:: ../../examples/tutorial/external/keyword_args_call.pyx
:caption: keyword_args_call.pyx
Note that changing existing parameter names later is a backwards
incompatible API modification, just as for Python code. Thus, if
......
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