Interfacing with External C Code¶
One of the main uses of Cython is wrapping existing libraries of C code. Thisis achieved by using external declarations to declare the C functions andvariables from the library that you want to use.
You can also use public declarations to make C functions and variables definedin a Cython module available to external C code. The need for this is expectedto be less frequent, but you might want to do it, for example, if you areembedding Python in another application as a scripting language. Just as aCython module can be used as a bridge to allow Python code to call C code, itcan also be used to allow C code to call Python code.
External declarations¶
By default, C functions and variables declared at the module level are localto the module (i.e. they have the C static storage class). They can also bedeclared extern to specify that they are defined elsewhere, for example,:
cdefexternintspam_countercdefexternvoidorder_spam(inttons)
Referencing C header files¶
When you use an extern definition on its own as in the examples above, Cythonincludes a declaration for it in the generated C file. This can cause problemsif the declaration doesn’t exactly match the declaration that will be seen byother C code. If you’re wrapping an existing C library, for example, it’simportant that the generated C code is compiled with exactly the samedeclarations as the rest of the library.
To achieve this, you can tell Cython that the declarations are to be found in aC header file, like this:
cdefexternfrom"spam.h":intspam_countervoidorder_spam(inttons)
Thecdefextern
from clause does three things:
It directs Cython to place a
#include
statement for the named header file inthe generated C code.It prevents Cython from generating any C codefor the declarations found in the associated block.
It treats all declarations within the block as though they started with
cdefextern
.
It’s important to understand that Cython does not itself read the C headerfile, so you still need to provide Cython versions of any declarations from itthat you use. However, the Cython declarations don’t always have to exactlymatch the C ones, and in some cases they shouldn’t or can’t. In particular:
Leave out any platform-specific extensions to C declarations such as
__declspec()
.If the header file declares a big struct and you only want to use a fewmembers, you only need to declare the members you’re interested in. Leavingthe rest out doesn’t do any harm, because the C compiler will use the fulldefinition from the header file.
In some cases, you might not need any of the struct’s members, in whichcase you can just put pass in the body of the struct declaration, e.g.:
cdefexternfrom"foo.h":structspam:pass
Note
you can only do this inside a
cdefexternfrom
block; structdeclarations anywhere else must be non-empty.If the header file uses
typedef
names such asword
to referto platform-dependent flavours of numeric types, you will need acorrespondingctypedef
statement, but you don’t need to matchthe type exactly, just use something of the right general kind (int, float,etc). For example,:ctypedefintword
will work okay whatever the actual size of a
word
is (provided the headerfile defines it correctly). Conversion to and from Python types, if any, will alsobe used for this new type.If the header file uses macros to define constants, translate them into anormal external variable declaration. You can also declare them as an
enum
if they contain normalint values. Note thatCython considersenum
to be equivalent toint, so donot do this for non-int values.If the header file defines a function using a macro, declare it as thoughit were an ordinary function, with appropriate argument and result types.
For archaic reasons C uses the keyword
void
to declare a functiontaking no parameters. In Cython as in Python, simply declare such functionsasfoo()
.
A few more tricks and tips:
If you want to include a C header because it’s needed by another header, butdon’t want to use any declarations from it, put pass in the extern-fromblock:
cdefexternfrom"spam.h":pass
If you want to include a system header, put angle brackets inside the quotes:
cdefexternfrom"<sysheader.h>":...
If you want to include some external declarations, but don’t want to specifya header file (because it’s included by some other header that you’vealready included) you can put
*
in place of the header file name:cdefexternfrom*:...
If a
cdefexternfrom"inc.h"
block is not empty and contains onlyfunction or variable declarations (and no type declarations of any kind),Cython will put the#include"inc.h"
statement after alldeclarations generated by Cython. This means that the included filehas access to the variables, functions, structures, … which aredeclared by Cython.
Implementing functions in C¶
When you want to call C code from a Cython module, usually that codewill be in some external library that you link your extension against.However, you can also directly compile C (or C++) code as part of yourCython module. In the.pyx
file, you can put something like:
cdefexternfrom"spam.c":voidorder_spam(inttons)
Cython will assume that the functionorder_spam()
is defined in thefilespam.c
. If you also want to cimport this function from anothermodule, it must be declared (not extern!) in the.pxd
file:
cdefvoidorder_spam(inttons)
For this to work, the signature oforder_spam()
inspam.c
mustmatch the signature that Cython uses, in particular the function mustbe static:
staticvoidorder_spam(inttons){printf("Ordered %i tons of spam!\n",tons);}
Styles of struct, union and enum declaration¶
There are two main ways that structs, unions and enums can be declared in Cheader files: using a tag name, or using a typedef. There are also somevariations based on various combinations of these.
It’s important to make the Cython declarations match the style used in theheader file, so that Cython can emit the right sort of references to the typein the code it generates. To make this possible, Cython provides two differentsyntaxes for declaring a struct, union or enum type. The style introducedabove corresponds to the use of a tag name. To get the other style, you prefixthe declaration withctypedef
, as illustrated below.
The following table shows the various possible styles that can be found in aheader file, and the corresponding Cython declaration that you should put inthecdefextern
from block. Struct declarations are used as an example; thesame applies equally to union and enum declarations.
C code | Possibilities for corresponding Cython Code | Comments |
---|---|---|
structFoo{...}; | cdefstructFoo:... | Cython will refer to the type as |
typedefstruct{...}Foo; | ctypedefstructFoo:... | Cython will refer to the type simply as |
typedefstructfoo{...}Foo; | cdefstructfoo:...ctypedeffooFoo#optional or: ctypedefstructFoo:... | If the C header uses both a tag and a typedef withdifferentnames, you can use either form of declaration in Cython(although if you need to forward reference the type,you’ll have to use the first form). |
typedefstructFoo{...}Foo; | cdefstructFoo:... | If the header uses thesame name for the tag and typedef, youwon’t be able to include a |
See also use ofExternal extension types.Note that in all the cases below, you refer to the type in Cython code simplyasFoo
, notstructFoo
.
Pointers¶
When interacting with a C-api there may be functions that require pointers as arguments.Pointers are variables that contain a memory address to another variable.
For example:
cdefexternfrom"<my_lib.h>":cdefvoidincrease_by_one(int*my_var)
This function takes a pointer to an integer as argument. Knowing the address of theinteger allows the function to modify the value in place, so that the caller can seethe changes afterwards. In order to get the address from an existing variable,use the&
operator:
cdefintsome_int=42cdefint *some_int_pointer=&some_intincrease_by_one(some_int_pointer)# Or without creating the extra variableincrease_by_one(&some_int)print(some_int)# prints 44 (== 42+1+1)
If you want to manipulate the variable the pointer points to, you can access it byreferencing its first element like you would in pythonmy_pointer[0]
. For example:
cdefvoidincrease_by_one(int*my_var):my_var[0]+=1
For a deeper introduction to pointers, you can readthis tutorial at tutorialspoint. For differences betweenCython and C syntax for manipulating pointers, seeStatements and expressions.
Accessing Python/C API routines¶
One particular use of thecdefexternfrom
statement is for gaining access toroutines in the Python/C API. For example,:
cdefexternfrom"Python.h":objectPyString_FromStringAndSize(char*s,Py_ssize_tlen)
will allow you to create Python strings containing null bytes.
Note that Cython comes with ready-to-use declarations of (almost) all C-API functionsin the cimportablecpython.*
modules. See the list inhttps://github.com/cython/cython/tree/master/Cython/Includes/cpython
You should always use submodules (e.g.cpython.object
,cpython.list
) toaccess these functions. Historically Cython has made some of the C-API functionsavailable under directly under thecpython
module. However, this isdeprecated, will be removed eventually, and any new additions will not be addedthere.
Special Types¶
Cython predefines the namePy_ssize_t
for use with Python/C API routines. Tomake your extensions compatible with 64-bit systems, you should always usethis type where it is specified in the documentation of Python/C API routines.
Windows Calling Conventions¶
The__stdcall
and__cdecl
calling convention specifiers can be used inCython, with the same syntax as used by C compilers on Windows, for example,:
cdefexternint__stdcallFrobnicateWindow(longhandle)cdefvoid(__stdcall*callback)(void*)
If__stdcall
is used, the function is only considered compatible withother__stdcall
functions of the same signature.
Resolving naming conflicts - C name specifications¶
Each Cython module has a single module-level namespace for both Python and Cnames. This can be inconvenient if you want to wrap some external C functionsand provide the Python user with Python functions of the same names.
Cython provides a couple of different ways of solving this problem. The bestway, especially if you have many C functions to wrap, is to put the externC function declarations into a.pxd
file and thus a different namespace,using the facilities described insharing declarations between Cythonmodules. Writing them into a.pxd
file allowstheir reuse across modules, avoids naming collisions in the normal Python wayand even makes it easy to rename them on cimport. For example, if yourdecl.pxd
file declared a C functioneject_tomato
:
cdefexternfrom"myheader.h":voideject_tomato(floatspeed)
then you can cimport and wrap it in a.pyx
file as follows:
fromdeclcimporteject_tomatoasc_eject_tomatodefeject_tomato(speed):c_eject_tomato(speed)
or simply cimport the.pxd
file and use it as prefix:
cimportdecldefeject_tomato(speed):decl.eject_tomato(speed)
Note that this has no runtime lookup overhead, as it would in Python.Cython resolves the names in the.pxd
file at compile time.
For special cases where namespacing or renaming on import is not enough,e.g. when a name in C conflicts with a Python keyword, you can use a C namespecification to give different Cython and C names to the C function atdeclaration time. Suppose, for example, that you want to wrap an externalC function calledyield()
. If you declare it as:
cdefexternfrom"myheader.h":voidc_yield"yield"(floatspeed)
then its Cython visible name will bec_yield
, whereas its name in Cwill beyield
. You can then wrap it with:
defcall_yield(speed):c_yield(speed)
As for functions, C names can be specified for variables, structs, unions,enums, struct and union members, and enum values. For example:
cdefexternintone"eins",two"zwei"cdefexternfloatthree"drei"cdefstructspam"SPAM":inti"eye"cdefenumsurprise"inquisition":first"alpha"second"beta"=3
Note that Cython will not do any validation or name mangling on the stringyou provide. It will inject the bare text into the C code unmodified, so youare entirely on your own with this feature. If you want to declare a namexyz
and have Cython inject the text “make the C compiler fail here” intothe C file for it, you can do this using a C name declaration. Consider thisan advanced feature, only for the rare cases where everything else fails.
Including verbatim C code¶
For advanced use cases, Cython allows you to directly write C codeas “docstring” of acdefexternfrom
block:
cdefexternfrom*:""" /* This is C code which will be put * in the .c file output by Cython */ static long c_square(long x) {return x * x;} #define c_assign(x, y) ((x) = (y)) """longc_square(longx)voidc_assign(long&x,longy)
The above is essentially equivalent to having the C code in a fileheader.h
and writing
cdefexternfrom"header.h":longc_square(longx)voidc_assign(long&x,longy)
This feature is commonly used for platform specific adaptations atcompile time, for example:
cdefexternfrom*:""" #if defined(_WIN32) || defined(MS_WINDOWS) || defined(_MSC_VER) #include "stdlib.h" #define myapp_sleep(m) _sleep(m) #else #include <unistd.h> #define myapp_sleep(m) ((void) usleep((m) * 1000)) #endif """# using "myapp_" prefix in the C code to prevent C naming conflictsvoidmsleep"myapp_sleep"(intmilliseconds)nogilmsleep(milliseconds=1)
It is also possible to combine a header file and verbatim C code:
cdefexternfrom"badheader.h":""" /* This macro breaks stuff */ #undef int """# Stuff from badheader.h
In this case, the C code#undefint
is put right after#include"badheader.h"
in the C code generated by Cython.
Verbatim C code can also be used for version specific adaptations, e.g. whena struct field was added to a library but is not available in older versions:
cdefexternfrom"struct_field_adaptation.h":""" #define HAS_NEWLY_ADDED_FIELD (C_LIB_VERSION >= 20) #if HAS_NEWLY_ADDED_FIELD #define _mylib_get_newly_added_field(a_struct_ptr) ((a_struct_ptr)->newly_added_field) #define _mylib_set_newly_added_field(a_struct_ptr, value) ((a_struct_ptr)->newly_added_field) = (value) #else #define _mylib_get_newly_added_field(a_struct_ptr) (0) #define _mylib_set_newly_added_field(a_struct_ptr, value) ((void) (value)) #endif """# Normal declarations provided by the C header file:ctypedefstructStructType:intfield1intfield2StructType*get_struct_ptr()# Special declarations conditionally provided above:bintHAS_NEWLY_ADDED_FIELDintget_newly_added_field"_mylib_get_newly_added_field"(StructType*struct_ptr)voidset_newly_added_field"_mylib_set_newly_added_field"(StructType*struct_ptr,intvalue)cdefStructType *some_struct_ptr=get_struct_ptr()print(some_struct_ptr.field1)ifHAS_NEWLY_ADDED_FIELD:print(get_newly_added_field(some_struct_ptr))
Note that the string is parsed like any other docstring in Python.If you require character escapes to be passed into the C code file,use a raw docstring, i.e.r"""..."""
.
Using Cython Declarations from C¶
Cython provides two methods for making C declarations from a Cython moduleavailable for use by external C code—public declarations and C APIdeclarations.
Note
You do not need to use either of these to make declarations from oneCython module available to another Cython module – you should use thecimport
statement for that. Sharing Declarations Between Cython Modules.
Public Declarations¶
You can make C types, variables and functions defined in a Cython moduleaccessible to C code that is linked together with the Cython-generated C file,by declaring them with the public keyword:
cdefpublicstructBunny:# a public type declarationintvorpalnesscdefpublicintspam# a public variable declarationcdefpublicvoidgrail(Bunny*)# a public function declaration
If there are any public declarations in a Cython module, a header file calledmodulename.h
file is generated containing equivalent C declarations forinclusion in other C code.
A typical use case for this is building an extension module from multipleC sources, one of them being Cython generated (i.e. with something likeExtension("grail",sources=["grail.pyx","grail_helper.c"])
insetup.py
.In this case, the filegrail_helper.c
just needs to add#include"grail.h"
in order to access the public Cython variables.
A more advanced use case is embedding Python in C using Cython.In this case, make sure to callPy_Initialize()
andPy_Finalize()
.For example, in the following snippet that includesgrail.h
:
#include<Python.h>#include"grail.h"intmain(){Py_Initialize();initgrail();/* Python 2.x only ! */Bunnyb;grail(b);Py_Finalize();}
This C code can then be built together with the Cython-generated C codein a single program (or library). Be aware that this program will not includeany external dependencies that your module uses. Therefore typically this willnot generate a truly portable application for most cases.
In Python 3.x, calling the module init function directly should be avoided. Instead,use theinittab mechanismto link Cython modules into a single shared library or program.
err=PyImport_AppendInittab("grail",PyInit_grail);Py_Initialize();grail_module=PyImport_ImportModule("grail");
If the Cython module resides within a package, then the name of the.h
file consists of the full dotted name of the module, e.g. a module calledfoo.spam
would have a header file calledfoo.spam.h
.
Note
On some operating systems like Linux, it is also possible to firstbuild the Cython extension in the usual way and then link againstthe resulting.so
file like a dynamic library.Beware that this is not portable, so it should be avoided.
C++ public declarations¶
When a file is compiled as C++, its public functions are declared as C++ API (usingextern"C++"
) by default.This disallows to call the functions from C code. If the functions are really meant as a plain C API,theextern
declaration needs to be manually specified by the user.This can be done by setting theCYTHON_EXTERN_C
C macro toextern"C"
during the compilation of the generated C++ file:
fromsetuptoolsimportExtension,setupfromCython.Buildimportcythonizeextensions=[Extension("module",["module.pyx"],define_macros=[("CYTHON_EXTERN_C",'extern "C"')],language="c++",)]setup(name="My hello app",ext_modules=cythonize(extensions),)
C API Declarations¶
The other way of making declarations available to C code is to declare themwith theapi
keyword. You can use this keyword with C functions andextension types. A header file calledmodulename_api.h
is producedcontaining declarations of the functions and extension types, and a functioncalledimport_modulename()
.
C code wanting to use these functions or extension types needs to include theheader and call theimport_modulename()
function. The other functionscan then be called and the extension types used as usual.
If the C code wanting to use these functions is part of more than one sharedlibrary or executable, thenimport_modulename()
function needs to becalled in each of the shared libraries which use these functions. If youcrash with a segmentation fault (SIGSEGV on linux) when calling into one ofthese api calls, this is likely an indication that the shared library whichcontains the api call which is generating the segmentation fault does not calltheimport_modulename()
function before the api call which crashes.
Any public C type or extension type declarations in the Cython module are alsomade available when you includemodulename_api.h
.:
# delorean.pyxcdefpublicstructVehicle:intspeedfloatpowercdefapivoidactivate(Vehicle*v)except*:ifv.speed>=88andv.power>=1.21:print("Time travel achieved")
# marty.c#include"delorean_api.h"Vehiclecar;intmain(intargc,char*argv[]){Py_Initialize();import_delorean();car.speed=atoi(argv[1]);car.power=atof(argv[2]);activate(&car);/* Error handling left out - call PyErr_Occurred() to test for Python exceptions. */Py_Finalize();}
Note
Any types defined in the Cython module that are used as argument orreturn types of the exported functions will need to be declared public,otherwise they won’t be included in the generated header file, and you willget errors when you try to compile a C file that uses the header.
Using theapi
method does not require the C code using thedeclarations to be linked with the extension module in any way, as the Pythonimport machinery is used to make the connection dynamically. However, onlyfunctions can be accessed this way, not variables. Note also that for themodule import mechanism to be set up correctly, the user must callPy_Initialize()
andPy_Finalize()
; if you experience a segmentation fault inthe call toimport_modulename()
, it is likely that this wasn’t done.
You can use bothpublic
andapi
on the same function tomake it available by both methods, e.g.:
cdefpublicapivoidbelt_and_braces()except*:...
However, note that you should include eithermodulename.h
ormodulename_api.h
in a given C file, not both, otherwise you may getconflicting dual definitions.
If the Cython module resides within a package, then:
The name of the header file contains of the full dotted name of the module.
The name of the importing function contains the full name with dots replacedby double underscores.
E.g. a module calledfoo.spam
would have an API header file calledfoo.spam_api.h
and an importing function calledimport_foo__spam()
.
Multiple public and API declarations¶
You can declare a whole group of items aspublic
and/orapi
all at once by enclosing them in acdef
block, forexample,:
cdefpublicapi:voidorder_spam(inttons)except*char*get_lunch(floattomato_size)exceptNULL
This can be a useful thing to do in a.pxd
file (seeSharing Declarations Between Cython Modules) to make the module’s public interfaceavailable by all three methods.
Acquiring and Releasing the GIL¶
Cython provides facilities for acquiring and releasing theGlobal Interpreter Lock (GIL) (seeour glossary orexternal documentation).This may be useful when calling from multi-threaded code into(external C) code that may block, or when wanting to use Pythonfrom a (native) C thread callback. Releasing the GIL shouldobviously only be done for thread-safe code or for code thatuses other means of protection against race conditions andconcurrency issues.
Note that acquiring the GIL is a blocking thread-synchronisingoperation, and therefore potentially costly. It might not beworth releasing the GIL for minor calculations. Usually, I/Ooperations and substantial computations in parallel code willbenefit from it.
Releasing the GIL¶
You can release the GIL around a section of code using thewithnogil
statement:
withnogil:<codetobeexecutedwiththeGILreleased>
Code in the body of the with-statement must not manipulate Python objectsin any way, and must not call anything that manipulates Python objects withoutfirst re-acquiring the GIL. Cython validates these operations at compile time,but cannot look into external C functions, for example. They must be correctlydeclared as requiring or not requiring the GIL (see below) in order to makeCython’s checks effective.
Since Cython 3.0, some simple Python statements can be used inside ofnogil
sections:raise
,assert
andprint
(the Py2 statement, not the function).Since they tend to be lone Python statements, Cython will automatically acquireand release the GIL around them for convenience.
Acquiring the GIL¶
A C function that is to be used as a callback from C code that is executedwithout the GIL needs to acquire the GIL before it can manipulate Pythonobjects. This can be done by specifyingwithgil
in the functionheader:
cdefvoidmy_callback(void*data)withgil:...
If the callback may be called from another non-Python thread,care must be taken to initialize the GIL first, through a call toPyEval_InitThreads().If you’re already usingcython.parallel in your module, this will already have been taken care of.
The GIL may also be acquired through thewithgil
statement:
withgil:<executethisblockwiththeGILacquired>
Conditional Acquiring / Releasing the GIL¶
Sometimes it is helpful to use a condition to decide whether to run acertain piece of code with or without the GIL. This code would run anyway,the difference is whether the GIL will be held or released.The condition must be constant (at compile time).
This could be useful for profiling, debugging, performance testing, andfor fused types (seeConditional GIL Acquiring / Releasing).:
DEFFREE_GIL=Truewithnogil(FREE_GIL):<codetobeexecutedwiththeGILreleased>withgil(False):<GILisstillreleased>
Declaring a function as callable without the GIL¶
You can specifynogil
in a C function header or function type todeclare that it is safe to call without the GIL.:
cdefvoidmy_gil_free_func(intspam)nogil:...
When you implement such a function in Cython, it cannot have any Pythonarguments or Python object return type. Furthermore, any operationthat involves Python objects (including calling Python functions) mustexplicitly acquire the GIL first, e.g. by using awithgil
blockor by calling a function that has been definedwithgil
. Theserestrictions are checked by Cython and you will get a compile errorif it finds any Python interaction inside of anogil
code section.
Note
Thenogil
function annotation declares that it is safeto call the function without the GIL. It is perfectly allowedto execute it while holding the GIL. The function does not initself release the GIL if it is held by the caller.
Declaring a functionwithgil
(i.e. as acquiring the GIL on entry) alsoimplicitly makes its signaturenogil
.