cpython/Doc/ext/ext.tex

1010 lines
41 KiB
TeX

\documentstyle[twoside,11pt,myformat]{report}
\title{Extending and Embedding the Python Interpreter}
\input{boilerplate}
% Tell \index to actually write the .idx file
\makeindex
\begin{document}
\pagenumbering{roman}
\maketitle
\input{copyright}
\begin{abstract}
\noindent
This document describes how to write modules in C or \Cpp{} to extend the
Python interpreter. It also describes how to use Python as an
`embedded' language, and how extension modules can be loaded
dynamically (at run time) into the interpreter, if the operating
system supports this feature.
\end{abstract}
\pagebreak
{
\parskip = 0mm
\tableofcontents
}
\pagebreak
\pagenumbering{arabic}
\chapter{Extending Python with C or \Cpp{} code}
\section{Introduction}
It is quite easy to add non-standard built-in modules to Python, if
you know how to program in C. A built-in module known to the Python
programmer as \code{foo} is generally implemented by a file called
\file{foomodule.c}. All but the two most essential standard built-in
modules also adhere to this convention, and in fact some of them form
excellent examples of how to create an extension.
Extension modules can do two things that can't be done directly in
Python: they can implement new data types (which are different from
classes, by the way), and they can make system calls or call C library
functions. We'll see how both types of extension are implemented by
examining the code for a Python curses interface.
Note: unless otherwise mentioned, all file references in this
document are relative to the toplevel directory of the Python
distribution --- i.e. the directory that contains the \file{configure}
script.
The compilation of an extension module depends on your system setup
and the intended use of the module; details are given in a later
section.
\section{A first look at the code}
It is important not to be impressed by the size and complexity of
the average extension module; much of this is straightforward
`boilerplate' code (starting right with the copyright notice)!
Let's skip the boilerplate and have a look at an interesting function
in \file{posixmodule.c} first:
\begin{verbatim}
static object *
posix_system(self, args)
object *self;
object *args;
{
char *command;
int sts;
if (!getargs(args, "s", &command))
return NULL;
sts = system(command);
return mkvalue("i", sts);
}
\end{verbatim}
This is the prototypical top-level function in an extension module.
It will be called (we'll see later how) when the Python program
executes statements like
\begin{verbatim}
>>> import posix
>>> sts = posix.system('ls -l')
\end{verbatim}
There is a straightforward translation from the arguments to the call
in Python (here the single expression \code{'ls -l'}) to the arguments that
are passed to the C function. The C function always has two
parameters, conventionally named \var{self} and \var{args}. The
\var{self} argument is used when the C function implements a builtin
method---this will be discussed later.
In the example, \var{self} will always be a \code{NULL} pointer, since
we are defining a function, not a method (this is done so that the
interpreter doesn't have to understand two different types of C
functions).
The \var{args} parameter will be a pointer to a Python object, or
\code{NULL} if the Python function/method was called without
arguments. It is necessary to do full argument type checking on each
call, since otherwise the Python user would be able to cause the
Python interpreter to `dump core' by passing invalid arguments to a
function in an extension module. Because argument checking and
converting arguments to C are such common tasks, there's a general
function in the Python interpreter that combines them:
\code{getargs()}. It uses a template string to determine both the
types of the Python argument and the types of the C variables into
which it should store the converted values.\footnote{There are
convenience macros \code{getnoarg()}, \code{getstrarg()},
\code{getintarg()}, etc., for many common forms of \code{getargs()}
templates. These are relics from the past; the recommended practice
is to call \code{getargs()} directly.} (More about this later.)
If \code{getargs()} returns nonzero, the argument list has the right
type and its components have been stored in the variables whose
addresses are passed. If it returns zero, an error has occurred. In
the latter case it has already raised an appropriate exception by so
the calling function should return \code{NULL} immediately --- see the
next section.
\section{Intermezzo: errors and exceptions}
An important convention throughout the Python interpreter is the
following: when a function fails, it should set an exception condition
and return an error value (often a \code{NULL} pointer). Exceptions
are stored in a static global variable in \file{Python/errors.c}; if
this variable is \code{NULL} no exception has occurred. A second
static global variable stores the `associated value' of the exception
--- the second argument to \code{raise}.
The file \file{errors.h} declares a host of functions to set various
types of exceptions. The most common one is \code{err_setstr()} ---
its arguments are an exception object (e.g. \code{RuntimeError} ---
actually it can be any string object) and a C string indicating the
cause of the error (this is converted to a string object and stored as
the `associated value' of the exception). Another useful function is
\code{err_errno()}, which only takes an exception argument and
constructs the associated value by inspection of the (UNIX) global
variable errno. The most general function is \code{err_set()}, which
takes two object arguments, the exception and its associated value.
You don't need to \code{INCREF()} the objects passed to any of these
functions.
You can test non-destructively whether an exception has been set with
\code{err_occurred()}. However, most code never calls
\code{err_occurred()} to see whether an error occurred or not, but
relies on error return values from the functions it calls instead.
When a function that calls another function detects that the called
function fails, it should return an error value (e.g. \code{NULL} or
\code{-1}) but not call one of the \code{err_*} functions --- one has
already been called. The caller is then supposed to also return an
error indication to {\em its} caller, again {\em without} calling
\code{err_*()}, and so on --- the most detailed cause of the error was
already reported by the function that first detected it. Once the
error has reached Python's interpreter main loop, this aborts the
currently executing Python code and tries to find an exception handler
specified by the Python programmer.
(There are situations where a module can actually give a more detailed
error message by calling another \code{err_*} function, and in such
cases it is fine to do so. As a general rule, however, this is not
necessary, and can cause information about the cause of the error to
be lost: most operations can fail for a variety of reasons.)
To ignore an exception set by a function call that failed, the
exception condition must be cleared explicitly by calling
\code{err_clear()}. The only time C code should call
\code{err_clear()} is if it doesn't want to pass the error on to the
interpreter but wants to handle it completely by itself (e.g. by
trying something else or pretending nothing happened).
Finally, the function \code{err_get()} gives you both error variables
{\em and clears them}. Note that even if an error occurred the second
one may be \code{NULL}. You have to \code{XDECREF()} both when you
are finished with them. I doubt you will need to use this function.
Note that a failing \code{malloc()} call must also be turned into an
exception --- the direct caller of \code{malloc()} (or
\code{realloc()}) must call \code{err_nomem()} and return a failure
indicator itself. All the object-creating functions
(\code{newintobject()} etc.) already do this, so only if you call
\code{malloc()} directly this note is of importance.
Also note that, with the important exception of \code{getargs()},
functions that return an integer status usually return \code{0} or a
positive value for success and \code{-1} for failure.
Finally, be careful about cleaning up garbage (making \code{XDECREF()}
or \code{DECREF()} calls for objects you have already created) when
you return an error!
The choice of which exception to raise is entirely yours. There are
predeclared C objects corresponding to all built-in Python exceptions,
e.g. \code{ZeroDevisionError} which you can use directly. Of course,
you should chose exceptions wisely --- don't use \code{TypeError} to
mean that a file couldn't be opened (that should probably be
\code{IOError}). If anything's wrong with the argument list the
\code{getargs()} function raises \code{TypeError}. If you have an
argument whose value which must be in a particular range or must
satisfy other conditions, \code{ValueError} is appropriate.
You can also define a new exception that is unique to your module.
For this, you usually declare a static object variable at the
beginning of your file, e.g.
\begin{verbatim}
static object *FooError;
\end{verbatim}
and initialize it in your module's initialization function
(\code{initfoo()}) with a string object, e.g. (leaving out the error
checking for simplicity):
\begin{verbatim}
void
initfoo()
{
object *m, *d;
m = initmodule("foo", foo_methods);
d = getmoduledict(m);
FooError = newstringobject("foo.error");
dictinsert(d, "error", FooError);
}
\end{verbatim}
\section{Back to the example}
Going back to \code{posix_system()}, you should now be able to
understand this bit:
\begin{verbatim}
if (!getargs(args, "s", &command))
return NULL;
\end{verbatim}
It returns \code{NULL} (the error indicator for functions of this
kind) if an error is detected in the argument list, relying on the
exception set by \code{getargs()}. Otherwise the string value of the
argument has been copied to the local variable \code{command} --- this
is in fact just a pointer assignment and you are not supposed to
modify the string to which it points.
If a function is called with multiple arguments, the argument list
(the argument \code{args}) is turned into a tuple. If it is called
without arguments, \code{args} is \code{NULL}. \code{getargs()} knows
about this; see later.
The next statement in \code{posix_system()} is a call to the C library
function \code{system()}, passing it the string we just got from
\code{getargs()}:
\begin{verbatim}
sts = system(command);
\end{verbatim}
Finally, \code{posix.system()} must return a value: the integer status
returned by the C library \code{system()} function. This is done
using the function \code{mkvalue()}, which is something like the
inverse of \code{getargs()}: it takes a format string and a variable
number of C values and returns a new Python object.
\begin{verbatim}
return mkvalue("i", sts);
\end{verbatim}
In this case, it returns an integer object (yes, even integers are
objects on the heap in Python!). More info on \code{mkvalue()} is
given later.
If you had a function that returned no useful argument (a.k.a. a
procedure), you would need this idiom:
\begin{verbatim}
INCREF(None);
return None;
\end{verbatim}
\code{None} is a unique Python object representing `no value'. It
differs from \code{NULL}, which means `error' in most contexts.
\section{The module's function table}
I promised to show how I made the function \code{posix_system()}
callable from Python programs. This is shown later in
\file{Modules/posixmodule.c}:
\begin{verbatim}
static struct methodlist posix_methods[] = {
...
{"system", posix_system},
...
{NULL, NULL} /* Sentinel */
};
void
initposix()
{
(void) initmodule("posix", posix_methods);
}
\end{verbatim}
(The actual \code{initposix()} is somewhat more complicated, but many
extension modules can be as simple as shown here.) When the Python
program first imports module \code{posix}, \code{initposix()} is
called, which calls \code{initmodule()} with specific parameters.
This creates a `module object' (which is inserted in the table
\code{sys.modules} under the key \code{'posix'}), and adds
built-in-function objects to the newly created module based upon the
table (of type struct methodlist) that was passed as its second
parameter. The function \code{initmodule()} returns a pointer to the
module object that it creates (which is unused here). It aborts with
a fatal error if the module could not be initialized satisfactorily,
so you don't need to check for errors.
\section{Compilation and linkage}
There are two more things to do before you can use your new extension
module: compiling and linking it with the Python system. If you use
dynamic loading, the details depend on the style of dynamic loading
your system uses; see the chapter on Dynamic Loading for more info
about this.
If you can't use dynamic loading, or if you want to make your module a
permanent part of the Python interpreter, you will have to change the
configuration setup and rebuild the interpreter. Luckily, in the 1.0
release this is very simple: just place your file (named
\file{foomodule.c} for example) in the \file{Modules} directory, add a
line to the file \file{Modules/Setup} describing your file:
\begin{verbatim}
foo foomodule.o
\end{verbatim}
and rebuild the interpreter by running \code{make} in the toplevel
directory. You can also run \code{make} in the \file{Modules}
subdirectory, but then you must first rebuilt the \file{Makefile}
there by running \code{make Makefile}. (This is necessary each time
you change the \file{Setup} file.)
\section{Calling Python functions from C}
So far we have concentrated on making C functions callable from
Python. The reverse is also useful: calling Python functions from C.
This is especially the case for libraries that support so-called
`callback' functions. If a C interface makes use of callbacks, the
equivalent Python often needs to provide a callback mechanism to the
Python programmer; the implementation will require calling the Python
callback functions from a C callback. Other uses are also imaginable.
Fortunately, the Python interpreter is easily called recursively, and
there is a standard interface to call a Python function. (I won't
dwell on how to call the Python parser with a particular string as
input --- if you're interested, have a look at the implementation of
the \samp{-c} command line option in \file{Python/pythonmain.c}.)
Calling a Python function is easy. First, the Python program must
somehow pass you the Python function object. You should provide a
function (or some other interface) to do this. When this function is
called, save a pointer to the Python function object (be careful to
\code{INCREF()} it!) in a global variable --- or whereever you see fit.
For example, the following function might be part of a module
definition:
\begin{verbatim}
static object *my_callback = NULL;
static object *
my_set_callback(dummy, arg)
object *dummy, *arg;
{
XDECREF(my_callback); /* Dispose of previous callback */
my_callback = arg;
XINCREF(my_callback); /* Remember new callback */
/* Boilerplate for "void" return */
INCREF(None);
return None;
}
\end{verbatim}
This particular function doesn't do any typechecking on its argument
--- that will be done by \code{call_object()}, which is a bit late but
at least protects the Python interpreter from shooting itself in its
foot. (The problem with typechecking functions is that there are at
least five different Python object types that can be called, so the
test would be somewhat cumbersome.)
The macros \code{XINCREF()} and \code{XDECREF()} increment/decrement
the reference count of an object and are safe in the presence of
\code{NULL} pointers. More info on them in the section on Reference
Counts below.
Later, when it is time to call the function, you call the C function
\code{call_object()}. This function has two arguments, both pointers
to arbitrary Python objects: the Python function, and the argument
list. The argument list must always be a tuple object, whose length
is the number of arguments. To call the Python function with no
arguments, you must pass an empty tuple. For example:
\begin{verbatim}
object *arglist;
object *result;
...
/* Time to call the callback */
arglist = newtupleobject(0);
result = call_object(my_callback, arglist);
DECREF(arglist);
\end{verbatim}
\code{call_object()} returns a Python object pointer: this is
the return value of the Python function. \code{call_object()} is
`reference-count-neutral' with respect to its arguments. In the
example a new tuple was created to serve as the argument list, which
is \code{DECREF()}-ed immediately after the call.
The return value of \code{call_object()} is `new': either it is a
brand new object, or it is an existing object whose reference count
has been incremented. So, unless you want to save it in a global
variable, you should somehow \code{DECREF()} the result, even
(especially!) if you are not interested in its value.
Before you do this, however, it is important to check that the return
value isn't \code{NULL}. If it is, the Python function terminated by raising
an exception. If the C code that called \code{call_object()} is
called from Python, it should now return an error indication to its
Python caller, so the interpreter can print a stack trace, or the
calling Python code can handle the exception. If this is not possible
or desirable, the exception should be cleared by calling
\code{err_clear()}. For example:
\begin{verbatim}
if (result == NULL)
return NULL; /* Pass error back */
/* Here maybe use the result */
DECREF(result);
\end{verbatim}
Depending on the desired interface to the Python callback function,
you may also have to provide an argument list to \code{call_object()}.
In some cases the argument list is also provided by the Python
program, through the same interface that specified the callback
function. It can then be saved and used in the same manner as the
function object. In other cases, you may have to construct a new
tuple to pass as the argument list. The simplest way to do this is to
call \code{mkvalue()}. For example, if you want to pass an integral
event code, you might use the following code:
\begin{verbatim}
object *arglist;
...
arglist = mkvalue("(l)", eventcode);
result = call_object(my_callback, arglist);
DECREF(arglist);
if (result == NULL)
return NULL; /* Pass error back */
/* Here maybe use the result */
DECREF(result);
\end{verbatim}
Note the placement of DECREF(argument) immediately after the call,
before the error check! Also note that strictly spoken this code is
not complete: \code{mkvalue()} may run out of memory, and this should
be checked.
\section{Format strings for {\tt getargs()}}
The \code{getargs()} function is declared in \file{modsupport.h} as
follows:
\begin{verbatim}
int getargs(object *arg, char *format, ...);
\end{verbatim}
The remaining arguments must be addresses of variables whose type is
determined by the format string. For the conversion to succeed, the
\var{arg} object must match the format and the format must be exhausted.
Note that while \code{getargs()} checks that the Python object really
is of the specified type, it cannot check the validity of the
addresses of C variables provided in the call: if you make mistakes
there, your code will probably dump core.
A non-empty format string consists of a single `format unit'. A
format unit describes one Python object; it is usually a single
character or a parenthesized sequence of format units. The type of a
format units is determined from its first character, the `format
letter':
\begin{description}
\item[\samp{s} (string)]
The Python object must be a string object. The C argument must be a
\code{(char**)} (i.e. the address of a character pointer), and a pointer
to the C string contained in the Python object is stored into it. You
must not provide storage to store the string; a pointer to an existing
string is stored into the character pointer variable whose address you
pass. If the next character in the format string is \samp{\#},
another C argument of type \code{(int*)} must be present, and the
length of the Python string (not counting the trailing zero byte) is
stored into it.
\item[\samp{z} (string or zero, i.e. \code{NULL})]
Like \samp{s}, but the object may also be None. In this case the
string pointer is set to \code{NULL} and if a \samp{\#} is present the
size is set to 0.
\item[\samp{b} (byte, i.e. char interpreted as tiny int)]
The object must be a Python integer. The C argument must be a
\code{(char*)}.
\item[\samp{h} (half, i.e. short)]
The object must be a Python integer. The C argument must be a
\code{(short*)}.
\item[\samp{i} (int)]
The object must be a Python integer. The C argument must be an
\code{(int*)}.
\item[\samp{l} (long)]
The object must be a (plain!) Python integer. The C argument must be
a \code{(long*)}.
\item[\samp{c} (char)]
The Python object must be a string of length 1. The C argument must
be a \code{(char*)}. (Don't pass an \code{(int*)}!)
\item[\samp{f} (float)]
The object must be a Python int or float. The C argument must be a
\code{(float*)}.
\item[\samp{d} (double)]
The object must be a Python int or float. The C argument must be a
\code{(double*)}.
\item[\samp{S} (string object)]
The object must be a Python string. The C argument must be an
\code{(object**)} (i.e. the address of an object pointer). The C
program thus gets back the actual string object that was passed, not
just a pointer to its array of characters and its size as for format
character \samp{s}. The reference count of the object has not been
increased.
\item[\samp{O} (object)]
The object can be any Python object, including None, but not
\code{NULL}. The C argument must be an \code{(object**)}. This can be
used if an argument list must contain objects of a type for which no
format letter exist: the caller must then check that it has the right
type. The reference count of the object has not been increased.
\item[\samp{(} (tuple)]
The object must be a Python tuple. Following the \samp{(} character
in the format string must come a number of format units describing the
elements of the tuple, followed by a \samp{)} character. Tuple
format units may be nested. (There are no exceptions for empty and
singleton tuples; \samp{()} specifies an empty tuple and \samp{(i)} a
singleton of one integer. Normally you don't want to use the latter,
since it is hard for the Python user to specify.
\end{description}
More format characters will probably be added as the need arises. It
should (but currently isn't) be allowed to use Python long integers
whereever integers are expected, and perform a range check. (A range
check is in fact always necessary for the \samp{b}, \samp{h} and
\samp{i} format letters, but this is currently not implemented.)
Some example calls:
\begin{verbatim}
int ok;
int i, j;
long k, l;
char *s;
int size;
ok = getargs(args, ""); /* No arguments */
/* Python call: f() */
ok = getargs(args, "s", &s); /* A string */
/* Possible Python call: f('whoops!') */
ok = getargs(args, "(lls)", &k, &l, &s); /* Two longs and a string */
/* Possible Python call: f(1, 2, 'three') */
ok = getargs(args, "((ii)s#)", &i, &j, &s, &size);
/* A pair of ints and a string, whose size is also returned */
/* Possible Python call: f(1, 2, 'three') */
{
int left, top, right, bottom, h, v;
ok = getargs(args, "(((ii)(ii))(ii))",
&left, &top, &right, &bottom, &h, &v);
/* A rectangle and a point */
/* Possible Python call:
f( ((0, 0), (400, 300)), (10, 10)) */
}
\end{verbatim}
Note that the `top level' of a non-empty format string must consist of
a single unit; strings like \samp{is} and \samp{(ii)s\#} are not valid
format strings. (But \samp{s\#} is.) If you have multiple arguments,
the format must therefore always be enclosed in parentheses, as in the
examples \samp{((ii)s\#)} and \samp{(((ii)(ii))(ii)}. (The current
implementation does not complain when more than one unparenthesized
format unit is given. Sorry.)
The \code{getargs()} function does not support variable-length
argument lists. In simple cases you can fake these by trying several
calls to
\code{getargs()} until one succeeds, but you must take care to call
\code{err_clear()} before each retry. For example:
\begin{verbatim}
static object *my_method(self, args) object *self, *args; {
int i, j, k;
if (getargs(args, "(ii)", &i, &j)) {
k = 0; /* Use default third argument */
}
else {
err_clear();
if (!getargs(args, "(iii)", &i, &j, &k))
return NULL;
}
/* ... use i, j and k here ... */
INCREF(None);
return None;
}
\end{verbatim}
(It is possible to think of an extension to the definition of format
strings to accommodate this directly, e.g. placing a \samp{|} in a
tuple might specify that the remaining arguments are optional.
\code{getargs()} should then return one more than the number of
variables stored into.)
Advanced users note: If you set the `varargs' flag in the method list
for a function, the argument will always be a tuple (the `raw argument
list'). In this case you must enclose single and empty argument lists
in parentheses, e.g. \samp{(s)} and \samp{()}.
\section{The {\tt mkvalue()} function}
This function is the counterpart to \code{getargs()}. It is declared
in \file{Include/modsupport.h} as follows:
\begin{verbatim}
object *mkvalue(char *format, ...);
\end{verbatim}
It supports exactly the same format letters as \code{getargs()}, but
the arguments (which are input to the function, not output) must not
be pointers, just values. If a byte, short or float is passed to a
varargs function, it is widened by the compiler to int or double, so
\samp{b} and \samp{h} are treated as \samp{i} and \samp{f} is
treated as \samp{d}. \samp{S} is treated as \samp{O}, \samp{s} is
treated as \samp{z}. \samp{z\#} and \samp{s\#} are supported: a
second argument specifies the length of the data (negative means use
\code{strlen()}). \samp{S} and \samp{O} add a reference to their
argument (so you should \code{DECREF()} it if you've just created it
and aren't going to use it again).
If the argument for \samp{O} or \samp{S} is a \code{NULL} pointer, it is
assumed that this was caused because the call producing the argument
found an error and set an exception. Therefore, \code{mkvalue()} will
return \code{NULL} but won't set an exception if one is already set.
If no exception is set, \code{SystemError} is set.
If there is an error in the format string, the \code{SystemError}
exception is set, since it is the calling C code's fault, not that of
the Python user who sees the exception.
Example:
\begin{verbatim}
return mkvalue("(ii)", 0, 0);
\end{verbatim}
returns a tuple containing two zeros. (Outer parentheses in the
format string are actually superfluous, but you can use them for
compatibility with \code{getargs()}, which requires them if more than
one argument is expected.)
\section{Reference counts}
Here's a useful explanation of \code{INCREF()} and \code{DECREF()}
(after an original by Sjoerd Mullender).
Use \code{XINCREF()} or \code{XDECREF()} instead of \code{INCREF()} or
\code{DECREF()} when the argument may be \code{NULL} --- the versions
without \samp{X} are faster but wull dump core when they encounter a
\code{NULL} pointer.
The basic idea is, if you create an extra reference to an object, you
must \code{INCREF()} it, if you throw away a reference to an object,
you must \code{DECREF()} it. Functions such as
\code{newstringobject()}, \code{newsizedstringobject()},
\code{newintobject()}, etc. create a reference to an object. If you
want to throw away the object thus created, you must use
\code{DECREF()}.
If you put an object into a tuple or list using \code{settupleitem()}
or \code{setlistitem()}, the idea is that you usually don't want to
keep a reference of your own around, so Python does not
\code{INCREF()} the elements. It does \code{DECREF()} the old value.
This means that if you put something into such an object using the
functions Python provides for this, you must \code{INCREF()} the
object if you also want to keep a separate reference to the object around.
Also, if you replace an element, you should \code{INCREF()} the old
element first if you want to keep it. If you didn't \code{INCREF()}
it before you replaced it, you are not allowed to look at it anymore,
since it may have been freed.
Returning an object to Python (i.e. when your C function returns)
creates a reference to an object, but it does not change the reference
count. When your code does not keep another reference to the object,
you should not \code{INCREF()} or \code{DECREF()} it (assuming it is a
newly created object). When you do keep a reference around, you
should \code{INCREF()} the object. Also, when you return a global
object such as \code{None}, you should \code{INCREF()} it.
If you want to return a tuple, you should consider using
\code{mkvalue()}. This function creates a new tuple with a reference
count of 1 which you can return. If any of the elements you put into
the tuple are objects (format codes \samp{O} or \samp{S}), they
are \code{INCREF()}'ed by \code{mkvalue()}. If you don't want to keep
references to those elements around, you should \code{DECREF()} them
after having called \code{mkvalue()}.
Usually you don't have to worry about arguments. They are
\code{INCREF()}'ed before your function is called and
\code{DECREF()}'ed after your function returns. When you keep a
reference to an argument, you should \code{INCREF()} it and
\code{DECREF()} when you throw it away. Also, when you return an
argument, you should \code{INCREF()} it, because returning the
argument creates an extra reference to it.
If you use \code{getargs()} to parse the arguments, you can get a
reference to an object (by using \samp{O} in the format string). This
object was not \code{INCREF()}'ed, so you should not \code{DECREF()}
it. If you want to keep the object, you must \code{INCREF()} it
yourself.
If you create your own type of objects, you should use \code{NEWOBJ()}
to create the object. This sets the reference count to 1. If you
want to throw away the object, you should use \code{DECREF()}. When
the reference count reaches zero, your type's \code{dealloc()}
function is called. In it, you should \code{DECREF()} all object to
which you keep references in your object, but you should not use
\code{DECREF()} on your object. You should use \code{DEL()} instead.
\section{Writing extensions in \Cpp{}}
It is possible to write extension modules in \Cpp{}. Some restrictions
apply: since the main program (the Python interpreter) is compiled and
linked by the C compiler, global or static objects with constructors
cannot be used. All functions that will be called directly or
indirectly (i.e. via function pointers) by the Python interpreter will
have to be declared using \code{extern "C"}; this applies to all
`methods' as well as to the module's initialization function.
It is unnecessary to enclose the Python header files in
\code{extern "C" \{...\}} --- they do this already.
\chapter{Embedding Python in another application}
Embedding Python is similar to extending it, but not quite. The
difference is that when you extend Python, the main program of the
application is still the Python interpreter, while if you embed
Python, the main program may have nothing to do with Python ---
instead, some parts of the application occasionally call the Python
interpreter to run some Python code.
So if you are embedding Python, you are providing your own main
program. One of the things this main program has to do is initialize
the Python interpreter. At the very least, you have to call the
function \code{initall()}. There are optional calls to pass command
line arguments to Python. Then later you can call the interpreter
from any part of the application.
There are several different ways to call the interpreter: you can pass
a string containing Python statements to \code{run_command()}, or you
can pass a stdio file pointer and a file name (for identification in
error messages only) to \code{run_script()}. You can also call the
lower-level operations described in the previous chapters to construct
and use Python objects.
A simple demo of embedding Python can be found in the directory
\file{Demo/embed}.
\section{Embedding Python in \Cpp{}}
It is also possible to embed Python in a \Cpp{} program; precisely how this
is done will depend on the details of the \Cpp{} system used; in general you
will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
to compile and link your program. There is no need to recompile Python
itself using \Cpp{}.
\chapter{Dynamic Loading}
On most modern systems it is possible to configure Python to support
dynamic loading of extension modules implemented in C. When shared
libraries are used dynamic loading is configured automatically;
otherwise you have to select it as a build option (see below). Once
configured, dynamic loading is trivial to use: when a Python program
executes \code{import foo}, the search for modules tries to find a
file \file{foomodule.o} (\file{foomodule.so} when using shared
libraries) in the module search path, and if one is found, it is
loaded into the executing binary and executed. Once loaded, the
module acts just like a built-in extension module.
The advantages of dynamic loading are twofold: the `core' Python
binary gets smaller, and users can extend Python with their own
modules implemented in C without having to build and maintain their
own copy of the Python interpreter. There are also disadvantages:
dynamic loading isn't available on all systems (this just means that
on some systems you have to use static loading), and dynamically
loading a module that was compiled for a different version of Python
(e.g. with a different representation of objects) may dump core.
\section{Configuring and building the interpreter for dynamic loading}
There are three styles of dynamic loading: one using shared libraries,
one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
loading.
\subsection{Shared libraries}
The following systems support dynamic loading using shared libraries:
SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
systems derived from SVR4, or at least those SVR4 derivatives that
support shared libraries (are there any that don't?).
You don't need to do anything to configure dynamic loading on these
systems --- the \file{configure} detects the presence of the
\file{<dlfcn.h>} header file and automatically configures dynamic
loading.
\subsection{SGI dynamic loading}
Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
loading. (SGI IRIX 5 might also support it but it is inferior to
using shared libraries so there is no reason to; a small test didn't
work right away so I gave up trying to support it.)
Before you build Python, you first need to fetch and build the \code{dl}
package written by Jack Jansen. This is available by anonymous ftp
from host \file{ftp.cwi.nl}, directory \file{pub/dynload}, file
\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
instructions in the package's \file{README} file to build it.
Once you have built \code{dl}, you can configure Python to use it. To
this end, you run the \file{configure} script with the option
\code{--with-dl=\var{directory}} where \var{directory} is the absolute
pathname of the \code{dl} directory.
Now build and install Python as you normally would (see the
\file{README} file in the toplevel Python directory.)
\subsection{GNU dynamic loading}
GNU dynamic loading supports (according to its \file{README} file) the
following hardware and software combinations: VAX (Ultrix), Sun 3
(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
Atari ST. There is no reason to use it on a Sparc; I haven't seen a
Sun 3 for years so I don't know if these have shared libraries or not.
You need to fetch and build two packages. One is GNU DLD 3.2.3,
available by anonymous ftp from host \file{ftp.cwi.nl}, directory
\file{pub/dynload}, file \file{dld-3.2.3.tar.Z}. (As far as I know,
no further development on GNU DLD is being done.) The other is an
emulation of Jack Jansen's \code{dl} package that I wrote on top of
GNU DLD 3.2.3. This is available from the same host and directory,
file dl-dld-1.1.tar.Z. (The version number may change --- but I doubt
it will.) Follow the instructions in each package's \file{README}
file to configure build them.
Now configure Python. Run the \file{configure} script with the option
\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
\var{dl-directory} is the absolute pathname of the directory where you
have built the \file{dl-dld} package, and \var{dld-directory} is that
of the GNU DLD package. The Python interpreter you build hereafter
will support GNU dynamic loading.
\section{Building a dynamically loadable module}
Since there are three styles of dynamic loading, there are also three
groups of instructions for building a dynamically loadable module.
Instructions common for all three styles are given first. Assuming
your module is called \code{foo}, the source filename must be
\file{foomodule.c}, so the object name is \file{foomodule.o}. The
module must be written as a normal Python extension module (as
described earlier).
Note that in all cases you will have to create your own Makefile that
compiles your module file(s). This Makefile will have to pass two
\samp{-I} arguments to the C compiler which will make it find the
Python header files. If the Make variable \var{PYTHONTOP} points to
the toplevel Python directory, your \var{CFLAGS} Make variable should
contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
(Most header files are in the \file{Include} subdirectory, but the
\file{config.h} header lives in the toplevel directory.) You must
also add \samp{-DHAVE_CONFIG_H} to the definition of \var{CFLAGS} to
direct the Python headers to include \file{config.h}.
\subsection{Shared libraries}
You must link the \samp{.o} file to produce a shared library. This is
done using a special invocation of the \UNIX{} loader/linker, {\em
ld}(1). Unfortunately the invocation differs slightly per system.
On SunOS 4, use
\begin{verbatim}
ld foomodule.o -o foomodule.so
\end{verbatim}
On Solaris 2, use
\begin{verbatim}
ld -G foomodule.o -o foomodule.so
\end{verbatim}
On SGI IRIX 5, use
\begin{verbatim}
ld -shared foomodule.o -o foomodule.so
\end{verbatim}
On other systems, consult the manual page for {\em ld}(1) to find what
flags, if any, must be used.
If your extension module uses system libraries that haven't already
been linked with Python (e.g. a windowing system), these must be
passed to the {\em ld} command as \samp{-l} options after the
\samp{.o} file.
The resulting file \file{foomodule.so} must be copied into a directory
along the Python module search path.
\subsection{SGI dynamic loading}
{bf IMPORTANT:} You must compile your extension module with the
additional C flag \samp{-G0} (or \samp{-G 0}). This instruct the
assembler to generate position-independent code.
You don't need to link the resulting \file{foomodule.o} file; just
copy it into a directory along the Python module search path.
The first time your extension is loaded, it takes some extra time and
a few messages may be printed. This creates a file
\file{foomodule.ld} which is an image that can be loaded quickly into
the Python interpreter process. When a new Python interpreter is
installed, the \code{dl} package detects this and rebuilds
\file{foomodule.ld}. The file \file{foomodule.ld} is placed in the
directory where \file{foomodule.o} was found, unless this directory is
unwritable; in that case it is placed in a temporary
directory.\footnote{Check the manual page of the \code{dl} package for
details.}
If your extension modules uses additional system libraries, you must
create a file \file{foomodule.libs} in the same directory as the
\file{foomodule.o}. This file should contain one or more lines with
whitespace-separated options that will be passed to the linker ---
normally only \samp{-l} options or absolute pathnames of libraries
(\samp{.a} files) should be used.
\subsection{GNU dynamic loading}
Just copy \file{foomodule.o} into a directory along the Python module
search path.
If your extension modules uses additional system libraries, you must
create a file \file{foomodule.libs} in the same directory as the
\file{foomodule.o}. This file should contain one or more lines with
whitespace-separated absolute pathnames of libraries (\samp{.a}
files). No \samp{-l} options can be used.
\input{ext.ind}
\end{document}