< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039196038240039088173205162105045222218071088187087116113144

Python/C API

The Application Programmer's Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is generally referred to as the Python/C API. There are two fundamentally different reasons for using the Python/C API. The first reason is to write extension modules for specific purposes; these are C modules that extend the Python interpreter. This is probably the most common use. The second reason is to use Python as a component in a larger application; this technique is generally referred to as embedding Python in an application.

Writing an extension module is a relatively well-understood process in which a "cookbook" approach works well. Several tools automate the process to some extent. Although people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

Many API functions are useful independent of whether you're embedding or extending Python; moreover, most applications that embed Python will need to provide a custom extension as well, so it's probably a good idea to become familiar with writing an extension before attempting to embed Python in a real application.

Python 1.5 introduces a number of new API functions as well as some changes to the build process that make embedding much simpler. This book describes the 1.5.2 state of affairs.

Include Files

All function, type, and macro definitions needed to use the Python/C API are included in your code by the following line:

						
#include "Python.h"

					

This implies inclusion of the following standard headers: <stdio.h>, <string.h>, <errno.h>, and <stdlib.h> (if available).

All user visible names defined by Python.h (except those defined by the included standard headers) have one of the prefixes Py or _Py. Names beginning with _Py are for internal use by the Python implementation and should not be used by extension writers. Structure member names do not have a reserved prefix.

Important: User code should never define names that begin with Py or _Py. This confuses the reader and jeopardizes the portability of the user code to future Python versions, which might define additional names beginning with one of these prefixes.

The header files are typically installed with Python. On UNIX, these are located in the directories $prefix/include/pythonversion/ and $exec_prefix/include/ pythonversion/, where $prefix and $exec_prefix are defined by the corresponding parameters to Python's configure script and the version is sys.version[:3]. On Windows, the headers are installed in $prefix/include, where $prefix is the installation directory specified to the installer.

To include the headers, place both directories (if different) on your compiler's search path for includes. Do not place the parent directories on the search path and then use #include <python1.5/Python.h>; this will break on multi-platform builds because the platform independent headers under $prefix include the platform specific headers from $exec_prefix.

Objects, Types, and Reference Counts

Most Python/C API functions have one or more arguments as well as a return value of type PyObject*. This type is a pointer to an opaque data type representing an arbitrary Python object. Because all Python object types are treated the same way by the Python language in most situations (for example, assignments, scope rules, and argument passing), it is only fitting that they should be represented by a single C type. Almost all Python objects live on the heap: You never declare an automatic or static variable of type PyObject; only pointer variables of type PyObject* can be declared. The sole exceptions are the type objects; because these must never be deallocated, they are typically static PyTypeObject objects.

All Python objects (even Python integers) have a type and a reference count. An object's type determines what kind of object it is (for example, an integer, a list, or a user-defined function; there are many more as explained in the Python Reference Manual). For each of the well-known types, there is a macro to check whether an object is of that type; for instance, PyList_Check(a) is true if (and only if) the object it points to is a Python list.

Reference Counts

The reference count is important because today's computers have a finite (and often severely limited) memory size; it counts how many different places there are that have a reference to an object. Such a place could be another object, a global (or static) C variable, or a local variable in some C function. When an object's reference count becomes zero, the object is deallocated. If it contains references to other objects, their reference count is decremented. Those other objects might be deallocated in turn, if this decrement makes their reference count become zero, and so on. (There's an obvious problem with objects that reference each other here; for now, the solution is don't do that.)

Reference counts are always manipulated explicitly. The normal way is to use the macro Py_INCREF() to increment an object's reference count by one, and Py_DECREF() to decrement it by one. The decref macro is considerably more complex than the incref one because it must check whether the reference count becomes zero, and then causes the object's deallocation by calling a function contained in the object's type structure. The type-specific deallocator takes care of decrementing the reference counts for other objects contained in the object, and so on, if this is a compound object type such as a list. There's no chance that the reference count can overflow; at least as many bits are used to hold the reference count as there are distinct memory locations in virtual memory (assuming sizeof(long) >= sizeof(char *)). Thus, the reference count increment is a simple operation. You should only pass a variable to Py_DECREF or Py_XDECREF. If you pass an expression, it will be evaluated multiple times (so don't use Py_XDECREF(func(...)) to ignore the return value of a function).

It is not necessary to increment an object's reference count for every local variable that contains a pointer to an object. In theory, the object's reference count goes up by one when the variable is made to point to it, and it goes down by one when the variable goes out of scope. However, these two cancel each other out, so at the end, the reference count hasn't changed. The only real reason to use the reference count is to prevent the object from being deallocated as long as our variable is pointing to it. If we know that there is at least one other reference to the object that lives at least as long as our variable, there is no need to increment the reference count temporarily. An important situation in which this arises is in objects that are passed as arguments to C functions in an extension module that are called from Python; the call mechanism guarantees to hold a reference to every argument for the duration of the call.

However, a common pitfall is to extract an object from a list and hold on to it for a while without incrementing its reference count. Some other operation might conceivably remove the object from the list, decrementing its reference count, and possibly deallocating it. The real danger is that innocent-looking operations might invoke arbitrary Python code that could do this; there is a code path that allows control to flow back to the user from a Py_DECREF(), so almost any operation is potentially dangerous.

A safe approach is to always use the generic operations (functions whose name begins with PyObject_, PyNumber_, PySequence_, or PyMapping_). These operations always increment the reference count of the object they return. This leaves the caller with the responsibility to call Py_DECREF() when they are done with the result; this soon becomes second nature.

Reference Count Details

The reference count behavior of functions in the Python/C API is best explained in terms of ownership of references. Note that we talk of owning references, never of owning objects; objects are always shared. When a function owns a reference, it has to dispose of it properly—either by passing ownership on (usually to its caller) or by calling Py_DECREF() or Py_XDECREF(). When a function passes ownership of a reference on to its caller, the caller is said to receive a new reference. When no ownership is transferred, the caller is said to borrow the reference. Nothing needs to be done for a borrowed reference.

Conversely, when calling a function passes it a reference to an object, there are two possibilities: The function steals a reference to the object, or it does not. Few functions steal references; the two notable exceptions are PyList_SetItem() and PyTuple_SetItem(), which steal a reference to the item (but not to the tuple or list into which the item is put). These functions were designed to steal a reference because of a common idiom for populating a tuple or list with newly created objects; for example, the code to create the tuple (1, 2, "three") could look similar to this (forget about error handling for the moment):

								
PyObject *t;
t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
PyTuple_SetItem(t, 2, PyString_FromString("three"));

							

Incidentally, PyTuple_SetItem() is the only way to set tuple items; PySequence_SetItem() and PyObject_SetItem() refuse to do this because tuples are an immutable data type. You should only use PyTuple_SetItem() for tuples that you are creating yourself.

Equivalent code for populating a list can be written using PyList_New() and PyList_SetItem(). Such code can also use PySequence_SetItem(); this illustrates the difference between the two (the extra Py_DECREF() calls):

								
PyObject *l, *x;
l = PyList_New(3);
x = PyInt_FromLong(1L);
PySequence_SetItem(l, 0, x); Py_DECREF(x);
x = PyInt_FromLong(2L);
PySequence_SetItem(l, 1, x); Py_DECREF(x);
x = PyString_FromString("three");
PySequence_SetItem(l, 2, x); Py_DECREF(x);

							

You might find it strange that the recommended approach takes more code. However, in practice, you will rarely use these ways of creating and populating a tuple or list. There's a generic function, Py_BuildValue(), that can create most common objects from C values, directed by a format string. For example, the previous two blocks of code could be replaced by the following (which also takes care of the error checking):

								
PyObject *t, *l;
t = Py_BuildValue("(iis)", 1, 2, "three");
l = Py_BuildValue("[iis]", 1, 2, "three");

							

It is more common to use PyObject_SetItem() and become friends with items whose references you are only borrowing, like arguments that were passed in to the function you are writing. In that case, their behavior regarding reference counts is much saner because you don't have to increment a reference count so you can give a reference away (have it be stolen). For example, this function sets all items of a list (actually, any mutable sequence) to a given item:

								
int set_all(PyObject *target, PyObject *item)
{
    int i, n;
    n = PyObject_Length(target);
    if (n < 0)
        return -1;
    for (i = 0; i < n; i++) {
        if (PyObject_SetItem(target, i, item) < 0)
            return -1;
    }
    return 0;
}

							

The situation is slightly different for function return values. Although passing a reference to most functions does not change your ownership responsibilities for that reference, many functions that return a reference to an object give you ownership of the reference. The reason is simple: In many cases, the returned object is created on-the-fly, and the reference you get is the only reference to the object. Therefore, the generic functions that return object references, such as PyObject_GetItem() and PySequence_GetItem(), always return a new reference (that is, the caller becomes the owner of the reference).

It is important to realize that whether you own a reference returned by a function only depends on which function you call—the plumage (that is, the type of the object passed as an argument to the function) doesn't enter into it. Thus, if you extract an item from a list using PyList_GetItem(), you don't own the reference—but if you obtain the same item from the same list using PySequence_GetItem() (which happens to take exactly the same arguments), you do own a reference to the returned object.

Here is an example of how you could write a function that computes the sum of the items in a list of integers; once using PyList_GetItem(), once using PySequence_GetItem():

								
long sum_list(PyObject *list)
{
    int i, n;
    long total = 0;



    PyObject *item;

   n = PyList_Size(list);
    if (n < 0)
        return -1; /* Not a list */
    for (i = 0; i < n; i++) {
        item = PyList_GetItem(list, i); /* Can't fail */
        if (!PyInt_Check(item)) continue; /* Skip non-integers */
        total += PyInt_AsLong(item);
    }
    return total;

}
long sum_sequence(PyObject *sequence)
{
    int i, n;
    long total = 0;
    PyObject *item;
    n = PyObject_Size(list);
    if (n < 0)
        return -1; /* Has no length */
    for (i = 0; i < n; i++) {
        item = PySequence_GetItem(list, i);
        if (item == NULL)
            return -1; /* Not a sequence, or other failure */
        if (PyInt_Check(item))
            total += PyInt_AsLong(item);
        Py_DECREF(item); /* Discard reference ownership */
    }
    return total;
}

							
Types

Few other data types play a significant role in the Python/C API; most are simple C types such as int, long, double, and char *. A few structure types are used to describe static tables used to list the functions exported by a module or the data attributes of a new object type. These will be discussed together with the functions that use them.

Exceptions

The Python programmer only needs to deal with exceptions if specific error handling is required; unhandled exceptions are automatically propagated to the caller, and then to the caller's caller, and so on, until they reach the top-level interpreter, where they are reported to the user accompanied by a stack traceback.

For C programmers, however, error checking always has to be explicit. All functions in the Python/C API can raise exceptions, unless an explicit claim is made otherwise in a function's documentation. In general, when a function encounters an error, it sets an exception, discards any object references that it owns, and returns an error indicator—usually NULL or -1. A few functions return a Boolean true/false result, with false indicating an error. Very few functions return no explicit error indicator or have an ambiguous return value and require explicit testing for errors with PyErr_Occurred()

Exception state is maintained in per-thread storage (this is equivalent to using global storage in an unthreaded application). A thread can be in one of two states: An exception has occurred, or it hasn't. The function PyErr_Occurred() can be used to check for this: It returns a borrowed reference to the exception type object when an exception has occurred, and NULL otherwise. There are a number of functions to set the exception state: PyErr_SetString() is the most common (though not the most general) function to set the exception state, and PyErr_Clear() clears the exception state.

The full exception state consists of three objects (all of which can be NULL): the exception type, the corresponding exception value, and the traceback. These have the same meanings as the Python object sys.exc_type, sys.exc_value, sys.exc_traceback; however, they are not the same: The Python objects represent the last exception being handled by a Python try ... except statement, whereas the C level exception state only exists while an exception is being passed on between C functions until it reaches the Python interpreter, which takes care of transferring it to sys.exc_type and friends.

Note that starting with Python 1.5, the preferred, thread-safe way to access the exception state from Python code is to call the function sys.exc_info(), which returns the per-thread exception state for Python code. Also, the semantics of both ways to access the exception state have changed so that a function which catches an exception will save and restore its thread exception state to preserve the exception state of its caller. This prevents common bugs in exception handling code caused by an innocent- looking function overwriting the exception being handled; it also reduces the often unwanted lifetime extension for objects that are referenced by the stack frames in the traceback.

As a general principle, a function that calls another function to perform some task should check whether the called function raised an exception, and if so, pass the exception state on to its caller. It should discard any object references that it owns, and returns an error indicator, but it should not set another exception—that would overwrite the exception just raised and lose important information about the exact cause of the error.

A simple example of detecting exceptions and passing them on is shown in the previous sum_sequence() example. It so happens that the example doesn't need to clean up any owned references when it detects an error. The following example function shows some error cleanup. First, to remind you why you like Python, we show the equivalent Python code:

						
def incr_item(dict, key):
    try:
        item = dict[key]
    except KeyError:
        item = 0
    return item + 1

					

Here is the corresponding C code, in all its glory:

						
int incr_item(PyObject *dict, PyObject *key)
{
    /* Objects all initialized to NULL for Py_XDECREF */
    PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
    int rv = -1; /* Return value initialized to -1 (failure) */
    item = PyObject_GetItem(dict, key);
    if (item == NULL) {
        /* Handle KeyError only: */
        if (!PyErr_ExceptionMatches(PyExc_KeyError)) goto error;
        /* Clear the error and use zero: */
        PyErr_Clear();
        item = PyInt_FromLong(0L);
        if (item == NULL) goto error;
    }
    const_one = PyInt_FromLong(1L);
    if (const_one == NULL) goto error;
    incremented_item = PyNumber_Add(item, const_one);
    if (incremented_item == NULL) goto error;

    if (PyObject_SetItem(dict, key, incremented_item) < 0) goto error;
    rv = 0; /* Success */
    /* Continue with cleanup code */
 error:
    /* Cleanup code, shared by success and failure path */
    /* Use Py_XDECREF() to ignore NULL references */
    Py_XDECREF(item);
    Py_XDECREF(const_one);
    Py_XDECREF(incremented_item);
    return rv; /* -1 for error, 0 for success */
}

					

This example represents an endorsed use of the goto statement in C. It illustrates the use of PyErr_ExceptionMatches() and PyErr_Clear() to handle specific exceptions, and the use of Py_XDECREF() to dispose of owned references that might be NULL (note the X in the name; Py_DECREF() would crash when confronted with a NULL reference). It is important that the variables used to hold owned references are initialized to NULL for this to work; likewise, the proposed return value is initialized to -1 (failure) and only set to success after the final call made is successful.

Embedding Python

The one important task that only embedders (as opposed to extension writers) of the Python interpreter have to worry about is the initialization, and possibly the finalization, of the Python interpreter. Most functionality of the interpreter can only be used after the interpreter has been initialized.

The basic initialization function is Py_Initialize(). This initializes the table of loaded modules and creates the fundamental modules __builtin__, __main__, and sys. It also initializes the module search path (sys.path).

Py_Initialize() does not set the script argument list (sys.argv). If this variable is needed by Python code that will be executed later, it must be set explicitly with a call to PySys_SetArgv(argc, argv) subsequent to the call to Py_Initialize().

On most systems (in particular, on UNIX and Windows, although the details are slightly different), Py_Initialize() calculates the module search path based on its best guess for the location of the standard Python interpreter executable, assuming that the Python library is found in a fixed location relative to the Python interpreter executable. In particular, it looks for a directory named lib/python1.5 (replacing 1.5 with the current interpreter version) relative to the parent directory where the executable named "python" is found on the shell command search path (the environment variable $PATH).

For instance, if the Python executable is found in /usr/local/bin/python, it will assume that the libraries are in /usr/local/lib/python1.5. (In fact, this particular path is also the fallback location, used when no executable file named "python" is found along $PATH, unless some other prefix is set when configure is called.) The user can override this behavior by setting the environment variable $PYTHONHOME, or insert additional directories in front of the standard path by setting $PYTHONPATH.

The embedding application can steer the search by calling Py_SetProgramName(file) before calling Py_Initialize(). Note that $PYTHONHOME still overrides this and $PYTHONPATH is still inserted in front of the standard path. An application that requires total control has to provide its own implementation of Py_GetPath(), Py_GetPrefix(),Py_GetExecPrefix(), and Py_GetProgramFullPath() (all defined in Modules/ getpath.c).

Sometimes, it is desirable to uninitialize Python. For instance, the application might want to start over (make another call to Py_Initialize()) or the application is simply done with its use of Python and wants to free all memory allocated by Python. This can be accomplished by calling Py_Finalize(). The function Py_IsInitialized() returns true if Python is currently in the initialized state. More information about these functions is given in a later section.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

__builtin__ module
__main__ module
accessing
      exception states
Application Programmers Interface (API)
      Python/C 2nd 3rd 4th 5th 6th 7th 8th 9th
borrowed references
checking
      error 2nd
configuring
      items in lists
      tuples
counts
     reference
            Python/C Application Programmers (API) interface 2nd 3rd 4th 5th
decrementing
      reference counts
embedding
     Python
            Python/C Application Programmers Interface (API)
error checking 2nd
exceptions
      finding 2nd
      Python/C Application Programmers Interface (API) 2nd 3rd
fallback location
files
      header
finding
      exceptions 2nd
functions
      embedding Python
      handling exceptions 2nd 3rd
      passing references to
      Py_BuildValue()
      Py_Finalize()
      Py_GetExecPrefix()
      Py_GetPath()
      Py_GetPrefix()
      Py_GetProgramFullPath()
      Py_Initialize() 2nd
      Py_IsInitialized()
      Py_SetProgramName(file)
      PyErr_Clear()
      PyErr_SetString()
      PyList_GetItem()
      PyList_New()
      PyList_SetItem()
      PyObject_GetItem()
      PyObject_SetItem()
      PySequence_GetItem() 2nd
      PySequence_SetItem()
      PyTuple_SetItem()
      reference counts 2nd 3rd
      sum_sequence() 2nd
      sys.exec_info()
header files
incrementing
      reference counts 2nd
interfaces
      Python/C Application Programmers (API) 2nd 3rd 4th 5th 6th 7th 8th 9th
lists
      populating
      setting items in
macros
      Py_DECREF() 2nd 3rd
      Py_INCREF()
      Py_XDECREF() 2nd
      reference counts 2nd 3rd
modules
      __builtin__
      __main__
      sys
object types
      Python/C Application Programmers Interface (API) 2nd 3rd 4th 5th
ownership, references
passing
      exceptions 2nd
      references to functions
populating
      lists
Py_BuildValue() function
Py_DECREF() macro 2nd 3rd
Py_Finalize() function
Py_GetExecPrefix() function
Py_GetPath() function
Py_GetPrefix() function
Py_GetProgramFullPath() function
Py_INCREF() macro
Py_Initialize() function 2nd
Py_IsInitialized() function
Py_SetProgramName(file) function
Py_XDECREF() macro 2nd
PyErr_Clear() function
PyErr_Occurred() function
PyErr_SetString() function
PyList_GetItem() function
PyList_New() function
PyList_SetItem() function
PyObject_GetItem() function
PyObject_SetItem() function
PySequence_GetItem() function 2nd
PySequence_SetItem() function
Python/C Application Programmers Interface (API) 2nd 3rd 4th 5th 6th 7th 8th 9th
PyTuple_SetItem() function
reference counts
      Python/C Application Programmers Interface (API) 2nd 3rd 4th 5th
searching
      exceptions 2nd
setting
      items in lists
      typles
state
      exceptions
states
      thread
stealing references
sum_sequence() function 2nd
sys module
sys.exec_info() function
threads
      states
tuples
      setting

© 2002, O'Reilly & Associates, Inc.