Interfacing with External C Code

One of the main uses of Pyrex is wrapping existing libraries of C code. This is achieved by using external declarations to declare the C functions and variables from the library that you want to use.

You can also use public declarations to make C functions and variables defined in a Pyrex module available to external C code. The need for this is expected to be less frequent, but you might want to do it, for example, if you are embedding Python in another application as a scripting language. Just as a Pyrex module can be used as a bridge to allow Python code to call C code, it can 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 local to the module (i.e. they have the C static storage class). They can also be declared extern to specify that they are defined elsewhere, for example:
cdef extern int spam_counter
cdef extern void order_spam(int tons)

Referencing C header files

When you use an extern definition on its own as in the examples above, Pyrex includes a declaration for it in the generated C file. This can cause problems if the declaration doesn't exactly match the declaration that will be seen by other C code. If you're wrapping an existing C library, for example, it's important that the generated C code is compiled with exactly the same declarations as the rest of the library.

To achieve this, you can tell Pyrex that the declarations are to be found in a C header file, like this:

cdef extern from "spam.h":
    int spam_counter
    void order_spam(int tons)
The cdef extern from clause does three things:
  1. It directs Pyrex to place a #include statement for the named header file in the generated C code.
  2.  
  3. It prevents Pyrex from generating any C code for the declarations found in the associated block.
  4.  
  5. It treats all declarations within the block as though they started with cdef extern.
It's important to understand that Pyrex does not itself read the C header file, so you still need to provide Pyrex versions of any declarations from it that you use. However, the Pyrex declarations don't always have to exactly match the C ones, and in some cases they shouldn't or can't. In particular:
  1. Don't use const. Pyrex doesn't know anything about const, so just leave it out. Most of the time this shouldn't cause any problem, although on rare occasions you might have to use a cast. 1
  2.  
  3. Leave out any platform-specific extensions to C declarations such as __declspec().
  4.  
  5. If the header file declares a big struct and you only want to use a few members, you only need to declare the members you're interested in. Leaving the rest out doesn't do any harm, because the C compiler will use the full definition from the header file.

    In some cases, you might not need any of the struct's members, in which case you can just put pass in the body of the struct declaration, e.g.

        cdef extern from "foo.h":
            struct spam:
                pass


    Note that you can only do this inside a cdef extern from block; struct declarations anywhere else must be non-empty.

  6. If the header file uses typedef names such as size_t to refer to platform-dependent flavours of numeric types, you will need a corresponding ctypedef statement, but you don't need to match the type exactly, just use something of the right general kind (int, float, etc). For example,
    1. ctypedef int size_t
    will work okay whatever the actual size of a size_t is (provided the header file defines it correctly).
     
  7. If the header file uses macros to define constants, translate them into a dummy enum declaration.
  8.  
  9. If the header file defines a function using a macro, declare it as though it were an ordinary function, with appropriate argument and result types.
A few more tricks and tips:
cdef extern from *:
    ...

Styles of struct, union and enum declaration

There are two main ways that structs, unions and enums can be declared in C header files: using a tag name, or using a typedef. There are also some variations based on various combinations of these.

It's important to make the Pyrex declarations match the style used in the header file, so that Pyrex can emit the right sort of references to the type in the code it generates. To make this possible, Pyrex provides two different syntaxes for declaring a struct, union or enum type. The style introduced above corresponds to the use of a tag name. To get the other style, you prefix the declaration with ctypedef, as illustrated below.

The following table shows the various possible styles that can be found in a header file, and the corresponding Pyrex declaration that you should put in the cdef exern from block. Struct declarations are used as an example; the same applies equally to union and enum declarations.

Note that in all the cases below, you refer to the type in Pyrex code simply as Foo, not struct Foo.

  C code Possibilities for corresponding Pyrex code Comments
1 struct Foo {
  ...
};
cdef struct Foo:
  ...
Pyrex will refer to the type as struct Foo in the generated C code.
2 typedef struct {
  ...
} Foo;
ctypedef struct Foo:
  ...
Pyrex will refer to the type simply as Foo in the generated C code.
3 typedef struct foo {
  ...
} Foo;
cdef struct foo:
  ...
ctypedef foo Foo #optional
If the C header uses both a tag and a typedef with different names, you can use either form of declaration in Pyrex (although if you need to forward reference the type, you'll have to use the first form).
ctypedef struct Foo:
  ...
4 typedef struct Foo {
  ...
} Foo;
cdef struct Foo:
  ...
If the header uses the same name for the tag and the typedef, you won't be able to include a ctypedef for it -- but then, it's not necessary.

Accessing Python/C API routines

One particular use of the cdef extern from statement is for gaining access to routines in the Python/C API. For example,
cdef extern from "Python.h":
    object PyString_FromStringAndSize(char *s, Py_ssize_t len)
will allow you to create Python strings containing null bytes.

Special Types

Pyrex predefines the name Py_ssize_t for use with Python/C API routines. To make your extensions compatible with 64-bit systems, you should always use this type where it is specified in the documentation of Python/C API routines.

Windows Calling Conventions

The __stdcall, __fastcall and __cdecl calling convention specifiers can be used in Pyrex, with the same syntax as used by C compilers on Windows, for example,

cdef extern int __stdcall FrobnicateWindow(long handle)

cdef void (__stdcall *callback)(void *)
If __stdcall is used, the function is only considered compatible with other __stdcall functions of the same signature.


Resolving naming conflicts - C name specifications

Each Pyrex module has a single module-level namespace for both Python and C names. This can be inconvenient if you want to wrap some external C functions and provide the Python user with Python functions of the same names.

Pyrex 0.8 provides a couple of different ways of solving this problem. The best way, especially if you have many C functions to wrap, is probably to put the extern C function declarations into a different namespace using the facilities described in the section on sharing declarations between Pyrex modules.

The other way is to use a c name specification to give different Pyrex and C names to the C function. Suppose, for example, that you want to wrap an external function called eject_tomato. If you declare it as

cdef extern void c_eject_tomato "eject_tomato" (float speed)
then its name inside the Pyrex module will be c_eject_tomato, whereas its name in C will be eject_tomato. You can then wrap it with
def eject_tomato(speed):
  c_eject_tomato(speed)
so that users of your module can refer to it as eject_tomato.

Another use for this feature is referring to external names that happen to be Pyrex keywords. For example, if you want to call an external function called print, you can rename it to something else in your Pyrex module.

As well as functions, C names can be specified for variables, structs, unions, enums, struct and union members, and enum values. For example,

cdef extern int one "ein", two "zwei"
cdef extern float three "drei"

cdef struct spam "SPAM":
  int i "eye"
cdef enum surprise "inquisition":
  first "alpha"
  second "beta" = 3

Using Pyrex Declarations from C

Pyrex provides two methods for making C declarations from a Pyrex module available for use by external C code – public declarations and C API declarations.

NOTE: You do not need to use either of these to make declarations from one Pyrex module available to another Pyrex module – you should use the cimport statement for that. Sharing Declarations Between Pyrex Modules.

Public Declarations

You can make C types, variables and functions defined in a Pyrex module accessible to C code that is linked with the module, by declaring them with the public keyword:
cdef public struct Bunny: # public type declaration
    int vorpalness

cdef public int spam # public variable declaration

cdef public void grail(Bunny *): # public function declaration
    ...

If there are any public declarations in a Pyrex module, a header file called modulename.h file is generated containing equivalent C declarations for inclusion in other C code.

Any C code wanting to make use of these declarations will need to be linked, either statically or dynamically, with the extension module.

If the Pyrex 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 called foo.spam would have a header file called foo.spam.h.

C API Declarations

The other way of making declarations available to C code is to declare them with the api keyword. You can use this keyword with C functions and extension types. A header file called "modulename_api.h" is produced containing declarations of the functions and extension types, and a function called import_modulename().

C code wanting to use these functions or extension types needs to include the header and call the import_modulename() function. The other functions can then be called and the extension types used as usual.

Any public C type or extension type declarations in the Pyrex module are also made available when you include modulename_api.h.

delorean.pyx
marty.c
cdef public struct Vehicle:
int speed
float power

cdef api void activate(Vehicle *v):
if v.speed >= 88 \
and v.power >= 1.21:
print "Time travel achieved"
#include "delorean_api.h"

Vehicle car;

int main(int argc, char *argv[]) {
import_delorean();
car.speed = atoi(argv[1]);
car.power = atof(argv[2]); 
activate(&car);
}

Note that any types defined in the Pyrex module that are used as argument or return types of the exported functions will need to be declared public, otherwise they won't be included in the generated header file, and you will get errors when you try to compile a C file that uses the header.

Using the api method does not require the C code using the declarations to be linked with the extension module in any way, as the Python import machinery is used to make the connection dynamically. However, only functions can be accessed this way, not variables.

You can use both public and api on the same function to make it available by both methods, e.g.
cdef public api void belt_and_braces():
...
However, note that you should include either modulename.h or modulename_api.h in a given C file, not both, otherwise you may get conflicting dual definitions.

If the Pyrex module resides within a package, then:
E.g. a module called foo.spam would have an API header file called foo.spam_api.h and an importing function called import_foo__spam().

Multiple public and api declarations

You can declare a whole group of items as public and/or api all at once by enclosing them in a cdef block, for example,
cdef public api:
void order_spam(int tons)
char *get_lunch(float tomato_size)
This can be a useful thing to do in a .pxd file (see Sharing Declarations Between Pyrex Modules) to make the module's public interface available by all three methods.


Acquiring and Releasing the GIL

Pyrex provides facilities for releasing the Global Interpreter Lock (GIL) before calling C code, and for acquiring the GIL in functions that are to be called back from C code that is executed without the GIL.

Releasing the GIL

You can release the GIL around a section of code using the with nogil statement:
with nogil:
<code to be executed with the GIL released>
Code in the body of the statement must not manipulate Python objects, and must not call anything that manipulates Python objects without first re-acquiring the GIL. Pyrex attempts to check that these restrictions are being followed as far as it can, but it may not catch all possible forms of violation.

Any external C functions called inside the block must be declared as nogil (see below).

Note: It may be safe to do some things with Python objects under some circumstances. Provided steps are taken (such as adequate locking) to ensure that the objects involved cannot be deallocated by Python code running in another thread, it is probably safe to access non-Python C attributes of an extension type, and to pass references to Python objects to another function that is safe to call with the GIL released.

However, in the absence of such locking, it is not safe to do anything with Python objects with the GIL released -- not even look at them.

Acquiring the GIL

A C function that is to be used as a callback from C code that is executed without the GIL needs to acquire the GIL before it can manipulate Python objects. This can be done by specifying with gil in the function header:
cdef void my_callback(void *data) with gil:
...

Declaring a function as callable without the GIL

You can specify nogil in a C function header or function type to declare that it is safe to call without the GIL.

cdef extern int swizzle_the_knob() nogil

A block of external functions can be declared nogil at once.

cdef extern from "somewhere.h" nogil:
...

Note that declaring a function nogil does not cause the GIL to be released before calling the function. It simply allows the function to be called in situations where the GIL is not held.

You can also declare a function implemented in Pyrex as nogil.
cdef void my_gil_free_func(int spam) nogil:
...
Such a function cannot have any Python local variables, it cannot return a Python type, and the same restrictions apply to the body of the function as for a with nogil block.

Declaring a function with gil also implicitly makes its signature nogil.


Footnotes
1. A problem with const could arise if you have something like
cdef extern from "grail.h":
  char *nun
where grail.h actually contains
extern const char *nun;
and you do
cdef void languissement(char *s):
  #something that doesn't change s
...
languissement(nun)
which will cause the C compiler to complain. You can work around it by casting away the constness:
languissement(<char *>nun)  
---