blob: c63c6cbf8084591f4ab434defdc3f535f174d336 [file] [log] [blame]
Fred Drake6659c301998-03-03 22:02:19 +00001\documentclass{manual}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002
Guido van Rossum5049bcb1995-03-13 16:55:23 +00003% XXX PM Modulator
4
Guido van Rossum6938f061994-08-01 12:22:53 +00005\title{Extending and Embedding the Python Interpreter}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00006
Guido van Rossum16cd7f91994-10-06 10:29:26 +00007\input{boilerplate}
Guido van Rossum83eb9621993-11-23 16:28:45 +00008
Guido van Rossum7a2dba21993-11-05 14:45:11 +00009% Tell \index to actually write the .idx file
10\makeindex
11
12\begin{document}
13
Guido van Rossum7a2dba21993-11-05 14:45:11 +000014\maketitle
15
Fred Drake9f86b661998-07-28 21:55:19 +000016\ifhtml
17\chapter*{Front Matter\label{front}}
18\fi
19
Guido van Rossum16cd7f91994-10-06 10:29:26 +000020\input{copyright}
21
Guido van Rossum7a2dba21993-11-05 14:45:11 +000022\begin{abstract}
23
24\noindent
Guido van Rossumb92112d1995-03-20 14:24:09 +000025Python is an interpreted, object-oriented programming language. This
Fred Drake0fd82681998-01-09 05:39:38 +000026document describes how to write modules in \C{} or \Cpp{} to extend the
Guido van Rossumb92112d1995-03-20 14:24:09 +000027Python interpreter with new modules. Those modules can define new
28functions but also new object types and their methods. The document
29also describes how to embed the Python interpreter in another
30application, for use as an extension language. Finally, it shows how
31to compile and link extension modules so that they can be loaded
32dynamically (at run time) into the interpreter, if the underlying
33operating system supports this feature.
34
35This document assumes basic knowledge about Python. For an informal
Fred Drake3da06a61998-02-26 18:49:12 +000036introduction to the language, see the Python Tutorial. The \emph{Python
37Reference Manual} gives a more formal definition of the language. The
38\emph{Python Library Reference} documents the existing object types,
Guido van Rossumb92112d1995-03-20 14:24:09 +000039functions and modules (both built-in and written in Python) that give
40the language its wide application range.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000041
Fred Drake0fd82681998-01-09 05:39:38 +000042For a detailed description of the whole Python/\C{} API, see the separate
Fred Drake3da06a61998-02-26 18:49:12 +000043\emph{Python/\C{} API Reference Manual}. \strong{Note:} While that
44manual is still in a state of flux, it is safe to say that it is much
45more up to date than the manual you're reading currently (which has
46been in need for an upgrade for some time now).
Guido van Rossumfdacc581997-10-07 14:40:16 +000047
48
Guido van Rossum7a2dba21993-11-05 14:45:11 +000049\end{abstract}
50
Fred Drake4d4f9e71998-01-13 22:25:02 +000051\tableofcontents
Guido van Rossum7a2dba21993-11-05 14:45:11 +000052
Guido van Rossumdb65a6c1993-11-05 17:11:16 +000053
Fred Drake0fd82681998-01-09 05:39:38 +000054\chapter{Extending Python with \C{} or \Cpp{} code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000055
Guido van Rossum6f0132f1993-11-19 13:13:22 +000056
Fred Drake3da06a61998-02-26 18:49:12 +000057%\section{Introduction}
58\label{intro}
Guido van Rossum6f0132f1993-11-19 13:13:22 +000059
Guido van Rossumb92112d1995-03-20 14:24:09 +000060It is quite easy to add new built-in modules to Python, if you know
Fred Drake0fd82681998-01-09 05:39:38 +000061how to program in \C{}. Such \dfn{extension modules} can do two things
Guido van Rossumb92112d1995-03-20 14:24:09 +000062that can't be done directly in Python: they can implement new built-in
Fred Drake0fd82681998-01-09 05:39:38 +000063object types, and they can call \C{} library functions and system calls.
Guido van Rossum6938f061994-08-01 12:22:53 +000064
Guido van Rossum5049bcb1995-03-13 16:55:23 +000065To support extensions, the Python API (Application Programmers
Guido van Rossumb92112d1995-03-20 14:24:09 +000066Interface) defines a set of functions, macros and variables that
67provide access to most aspects of the Python run-time system. The
Fred Drake0fd82681998-01-09 05:39:38 +000068Python API is incorporated in a \C{} source file by including the header
Guido van Rossumb92112d1995-03-20 14:24:09 +000069\code{"Python.h"}.
Guido van Rossum6938f061994-08-01 12:22:53 +000070
Guido van Rossumb92112d1995-03-20 14:24:09 +000071The compilation of an extension module depends on its intended use as
72well as on your system setup; details are given in a later section.
Guido van Rossum6938f061994-08-01 12:22:53 +000073
Guido van Rossum7a2dba21993-11-05 14:45:11 +000074
Fred Drake5e8aa541998-11-16 18:34:07 +000075\section{A Simple Example
76 \label{simpleExample}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000077
Guido van Rossumb92112d1995-03-20 14:24:09 +000078Let's create an extension module called \samp{spam} (the favorite food
79of Monty Python fans...) and let's say we want to create a Python
Fred Draked7bb3031998-03-03 17:52:07 +000080interface to the \C{} library function \cfunction{system()}.\footnote{An
Guido van Rossumb92112d1995-03-20 14:24:09 +000081interface for this function already exists in the standard module
Fred Draked7bb3031998-03-03 17:52:07 +000082\module{os} --- it was chosen as a simple and straightfoward example.}
Guido van Rossumb92112d1995-03-20 14:24:09 +000083This function takes a null-terminated character string as argument and
84returns an integer. We want this function to be callable from Python
85as follows:
86
Fred Drake1e11a5c1998-02-13 07:11:32 +000087\begin{verbatim}
88>>> import spam
89>>> status = spam.system("ls -l")
90\end{verbatim}
91
Fred Drakea0dbddf1998-04-02 06:50:02 +000092Begin by creating a file \file{spammodule.c}. (In general, if a
Fred Drake0fd82681998-01-09 05:39:38 +000093module is called \samp{spam}, the \C{} file containing its implementation
Guido van Rossumb92112d1995-03-20 14:24:09 +000094is called \file{spammodule.c}; if the module name is very long, like
95\samp{spammify}, the module name can be just \file{spammify.c}.)
96
97The first line of our file can be:
Guido van Rossum7a2dba21993-11-05 14:45:11 +000098
Fred Drake1e11a5c1998-02-13 07:11:32 +000099\begin{verbatim}
100#include "Python.h"
101\end{verbatim}
102
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000103which pulls in the Python API (you can add a comment describing the
104purpose of the module and a copyright notice if you like).
105
Guido van Rossumb92112d1995-03-20 14:24:09 +0000106All user-visible symbols defined by \code{"Python.h"} have a prefix of
107\samp{Py} or \samp{PY}, except those defined in standard header files.
108For convenience, and since they are used extensively by the Python
109interpreter, \code{"Python.h"} includes a few standard header files:
110\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
111\code{<stdlib.h>}. If the latter header file does not exist on your
Fred Draked7bb3031998-03-03 17:52:07 +0000112system, it declares the functions \cfunction{malloc()},
113\cfunction{free()} and \cfunction{realloc()} directly.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000114
Fred Drake0fd82681998-01-09 05:39:38 +0000115The next thing we add to our module file is the \C{} function that will
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000116be called when the Python expression \samp{spam.system(\var{string})}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000117is evaluated (we'll see shortly how it ends up being called):
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000118
Fred Drake1e11a5c1998-02-13 07:11:32 +0000119\begin{verbatim}
120static PyObject *
121spam_system(self, args)
122 PyObject *self;
123 PyObject *args;
124{
125 char *command;
126 int sts;
Fred Drakea0dbddf1998-04-02 06:50:02 +0000127
Fred Drake1e11a5c1998-02-13 07:11:32 +0000128 if (!PyArg_ParseTuple(args, "s", &command))
129 return NULL;
130 sts = system(command);
131 return Py_BuildValue("i", sts);
132}
133\end{verbatim}
134
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000135There is a straightforward translation from the argument list in
Guido van Rossumb92112d1995-03-20 14:24:09 +0000136Python (e.g.\ the single expression \code{"ls -l"}) to the arguments
Fred Drake0fd82681998-01-09 05:39:38 +0000137passed to the \C{} function. The \C{} function always has two arguments,
Guido van Rossumb92112d1995-03-20 14:24:09 +0000138conventionally named \var{self} and \var{args}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000139
Fred Drake0fd82681998-01-09 05:39:38 +0000140The \var{self} argument is only used when the \C{} function implements a
Fred Drakedc409041998-04-02 18:54:54 +0000141built-in method. This will be discussed later. In the example,
Fred Drake0fd82681998-01-09 05:39:38 +0000142\var{self} will always be a \NULL{} pointer, since we are defining
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000143a function, not a method. (This is done so that the interpreter
Fred Drake0fd82681998-01-09 05:39:38 +0000144doesn't have to understand two different types of \C{} functions.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000145
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000146The \var{args} argument will be a pointer to a Python tuple object
Guido van Rossumb92112d1995-03-20 14:24:09 +0000147containing the arguments. Each item of the tuple corresponds to an
148argument in the call's argument list. The arguments are Python
Fred Drake1aedbd81998-02-16 14:47:27 +0000149objects --- in order to do anything with them in our \C{} function we have
Fred Drake3da06a61998-02-26 18:49:12 +0000150to convert them to \C{} values. The function \cfunction{PyArg_ParseTuple()}
Fred Drake0fd82681998-01-09 05:39:38 +0000151in the Python API checks the argument types and converts them to \C{}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000152values. It uses a template string to determine the required types of
Fred Drake0fd82681998-01-09 05:39:38 +0000153the arguments as well as the types of the \C{} variables into which to
Guido van Rossumb92112d1995-03-20 14:24:09 +0000154store the converted values. More about this later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000155
Fred Drake3da06a61998-02-26 18:49:12 +0000156\cfunction{PyArg_ParseTuple()} returns true (nonzero) if all arguments have
Guido van Rossumb92112d1995-03-20 14:24:09 +0000157the right type and its components have been stored in the variables
158whose addresses are passed. It returns false (zero) if an invalid
159argument list was passed. In the latter case it also raises an
160appropriate exception by so the calling function can return
Fred Drake0fd82681998-01-09 05:39:38 +0000161\NULL{} immediately (as we saw in the example).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000162
163
Fred Drake5e8aa541998-11-16 18:34:07 +0000164\section{Intermezzo: Errors and Exceptions
165 \label{errors}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000166
167An important convention throughout the Python interpreter is the
168following: when a function fails, it should set an exception condition
Fred Drake0fd82681998-01-09 05:39:38 +0000169and return an error value (usually a \NULL{} pointer). Exceptions
Guido van Rossumb92112d1995-03-20 14:24:09 +0000170are stored in a static global variable inside the interpreter; if this
Fred Drake0fd82681998-01-09 05:39:38 +0000171variable is \NULL{} no exception has occurred. A second global
Guido van Rossumb92112d1995-03-20 14:24:09 +0000172variable stores the ``associated value'' of the exception (the second
Fred Draked7bb3031998-03-03 17:52:07 +0000173argument to \keyword{raise}). A third variable contains the stack
Guido van Rossumb92112d1995-03-20 14:24:09 +0000174traceback in case the error originated in Python code. These three
Fred Drake0fd82681998-01-09 05:39:38 +0000175variables are the \C{} equivalents of the Python variables
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000176\code{sys.exc_type}, \code{sys.exc_value} and \code{sys.exc_traceback}
Fred Draked7bb3031998-03-03 17:52:07 +0000177(see the section on module \module{sys} in the \emph{Python Library
178Reference}). It is important to know about them to understand how
179errors are passed around.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000180
Guido van Rossumb92112d1995-03-20 14:24:09 +0000181The Python API defines a number of functions to set various types of
182exceptions.
183
Fred Draked7bb3031998-03-03 17:52:07 +0000184The most common one is \cfunction{PyErr_SetString()}. Its arguments
185are an exception object and a \C{} string. The exception object is
186usually a predefined object like \cdata{PyExc_ZeroDivisionError}. The
187\C{} string indicates the cause of the error and is converted to a
188Python string object and stored as the ``associated value'' of the
189exception.
Guido van Rossumb92112d1995-03-20 14:24:09 +0000190
Fred Draked7bb3031998-03-03 17:52:07 +0000191Another useful function is \cfunction{PyErr_SetFromErrno()}, which only
Guido van Rossumb92112d1995-03-20 14:24:09 +0000192takes an exception argument and constructs the associated value by
Fred Draked7bb3031998-03-03 17:52:07 +0000193inspection of the (\UNIX{}) global variable \cdata{errno}. The most
194general function is \cfunction{PyErr_SetObject()}, which takes two object
Guido van Rossumb92112d1995-03-20 14:24:09 +0000195arguments, the exception and its associated value. You don't need to
Fred Draked7bb3031998-03-03 17:52:07 +0000196\cfunction{Py_INCREF()} the objects passed to any of these functions.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000197
198You can test non-destructively whether an exception has been set with
Fred Draked7bb3031998-03-03 17:52:07 +0000199\cfunction{PyErr_Occurred()}. This returns the current exception object,
Fred Drake0fd82681998-01-09 05:39:38 +0000200or \NULL{} if no exception has occurred. You normally don't need
Fred Draked7bb3031998-03-03 17:52:07 +0000201to call \cfunction{PyErr_Occurred()} to see whether an error occurred in a
Guido van Rossumb92112d1995-03-20 14:24:09 +0000202function call, since you should be able to tell from the return value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000203
Guido van Rossumd16ddb61996-12-13 02:38:17 +0000204When a function \var{f} that calls another function \var{g} detects
Guido van Rossumb92112d1995-03-20 14:24:09 +0000205that the latter fails, \var{f} should itself return an error value
Fred Drake0fd82681998-01-09 05:39:38 +0000206(e.g. \NULL{} or \code{-1}). It should \emph{not} call one of the
Fred Draked7bb3031998-03-03 17:52:07 +0000207\cfunction{PyErr_*()} functions --- one has already been called by \var{g}.
Guido van Rossumb92112d1995-03-20 14:24:09 +0000208\var{f}'s caller is then supposed to also return an error indication
Fred Draked7bb3031998-03-03 17:52:07 +0000209to \emph{its} caller, again \emph{without} calling \cfunction{PyErr_*()},
Guido van Rossumb92112d1995-03-20 14:24:09 +0000210and so on --- the most detailed cause of the error was already
211reported by the function that first detected it. Once the error
212reaches the Python interpreter's main loop, this aborts the currently
213executing Python code and tries to find an exception handler specified
214by the Python programmer.
Guido van Rossum6938f061994-08-01 12:22:53 +0000215
216(There are situations where a module can actually give a more detailed
Fred Draked7bb3031998-03-03 17:52:07 +0000217error message by calling another \cfunction{PyErr_*()} function, and in
Guido van Rossumb92112d1995-03-20 14:24:09 +0000218such cases it is fine to do so. As a general rule, however, this is
219not necessary, and can cause information about the cause of the error
220to be lost: most operations can fail for a variety of reasons.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000221
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000222To ignore an exception set by a function call that failed, the exception
Fred Draked7bb3031998-03-03 17:52:07 +0000223condition must be cleared explicitly by calling \cfunction{PyErr_Clear()}.
224The only time \C{} code should call \cfunction{PyErr_Clear()} is if it doesn't
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000225want to pass the error on to the interpreter but wants to handle it
226completely by itself (e.g. by trying something else or pretending
227nothing happened).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000228
Fred Draked7bb3031998-03-03 17:52:07 +0000229Note that a failing \cfunction{malloc()} call must be turned into an
230exception --- the direct caller of \cfunction{malloc()} (or
231\cfunction{realloc()}) must call \cfunction{PyErr_NoMemory()} and
232return a failure indicator itself. All the object-creating functions
233(\cfunction{PyInt_FromLong()} etc.) already do this, so only if you
234call \cfunction{malloc()} directly this note is of importance.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000235
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000236Also note that, with the important exception of
Fred Drake3da06a61998-02-26 18:49:12 +0000237\cfunction{PyArg_ParseTuple()} and friends, functions that return an
Guido van Rossumb92112d1995-03-20 14:24:09 +0000238integer status usually return a positive value or zero for success and
239\code{-1} for failure, like \UNIX{} system calls.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000240
Fred Draked7bb3031998-03-03 17:52:07 +0000241Finally, be careful to clean up garbage (by making
242\cfunction{Py_XDECREF()} or \cfunction{Py_DECREF()} calls for objects
243you have already created) when you return an error indicator!
Guido van Rossum6938f061994-08-01 12:22:53 +0000244
245The choice of which exception to raise is entirely yours. There are
Fred Drake0fd82681998-01-09 05:39:38 +0000246predeclared \C{} objects corresponding to all built-in Python exceptions,
Fred Drakeb85fbec1998-04-13 00:50:04 +0000247e.g. \cdata{PyExc_ZeroDivisionError} which you can use directly. Of
Guido van Rossumb92112d1995-03-20 14:24:09 +0000248course, you should choose exceptions wisely --- don't use
Fred Draked7bb3031998-03-03 17:52:07 +0000249\cdata{PyExc_TypeError} to mean that a file couldn't be opened (that
250should probably be \cdata{PyExc_IOError}). If something's wrong with
Fred Drake3da06a61998-02-26 18:49:12 +0000251the argument list, the \cfunction{PyArg_ParseTuple()} function usually
Fred Draked7bb3031998-03-03 17:52:07 +0000252raises \cdata{PyExc_TypeError}. If you have an argument whose value
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000253which must be in a particular range or must satisfy other conditions,
Fred Draked7bb3031998-03-03 17:52:07 +0000254\cdata{PyExc_ValueError} is appropriate.
Guido van Rossum6938f061994-08-01 12:22:53 +0000255
256You can also define a new exception that is unique to your module.
257For this, you usually declare a static object variable at the
258beginning of your file, e.g.
259
Fred Drake1e11a5c1998-02-13 07:11:32 +0000260\begin{verbatim}
261static PyObject *SpamError;
262\end{verbatim}
263
Guido van Rossum6938f061994-08-01 12:22:53 +0000264and initialize it in your module's initialization function
Fred Draked7bb3031998-03-03 17:52:07 +0000265(\cfunction{initspam()}) with an exception object, e.g. (leaving out
266the error checking for now):
Guido van Rossum6938f061994-08-01 12:22:53 +0000267
Fred Drake1e11a5c1998-02-13 07:11:32 +0000268\begin{verbatim}
269void
270initspam()
271{
272 PyObject *m, *d;
Fred Drakea0dbddf1998-04-02 06:50:02 +0000273
Fred Drake1e11a5c1998-02-13 07:11:32 +0000274 m = Py_InitModule("spam", SpamMethods);
275 d = PyModule_GetDict(m);
Fred Draked7bb3031998-03-03 17:52:07 +0000276 SpamError = PyErr_NewException("spam.error", NULL, NULL);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000277 PyDict_SetItemString(d, "error", SpamError);
278}
279\end{verbatim}
280
Guido van Rossumb92112d1995-03-20 14:24:09 +0000281Note that the Python name for the exception object is
Fred Draked7bb3031998-03-03 17:52:07 +0000282\exception{spam.error}. The \cfunction{PyErr_NewException()} function
283may create either a string or class, depending on whether the
284\samp{-X} flag was passed to the interpreter. If \samp{-X} was used,
285\cdata{SpamError} will be a string object, otherwise it will be a
286class object with the base class being \exception{Exception},
287described in the \emph{Python Library Reference} under ``Built-in
288Exceptions.''
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000289
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000290
Fred Drake5e8aa541998-11-16 18:34:07 +0000291\section{Back to the Example
292 \label{backToExample}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000293
294Going back to our example function, you should now be able to
295understand this statement:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000296
Fred Drake1e11a5c1998-02-13 07:11:32 +0000297\begin{verbatim}
298 if (!PyArg_ParseTuple(args, "s", &command))
299 return NULL;
300\end{verbatim}
301
Fred Drake0fd82681998-01-09 05:39:38 +0000302It returns \NULL{} (the error indicator for functions returning
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000303object pointers) if an error is detected in the argument list, relying
Fred Drake3da06a61998-02-26 18:49:12 +0000304on the exception set by \cfunction{PyArg_ParseTuple()}. Otherwise the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000305string value of the argument has been copied to the local variable
Fred Draked7bb3031998-03-03 17:52:07 +0000306\cdata{command}. This is a pointer assignment and you are not supposed
Fred Drake0fd82681998-01-09 05:39:38 +0000307to modify the string to which it points (so in Standard \C{}, the variable
Fred Draked7bb3031998-03-03 17:52:07 +0000308\cdata{command} should properly be declared as \samp{const char
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000309*command}).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000310
Fred Draked7bb3031998-03-03 17:52:07 +0000311The next statement is a call to the \UNIX{} function
312\cfunction{system()}, passing it the string we just got from
313\cfunction{PyArg_ParseTuple()}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000314
Fred Drake1e11a5c1998-02-13 07:11:32 +0000315\begin{verbatim}
316 sts = system(command);
317\end{verbatim}
318
Fred Draked7bb3031998-03-03 17:52:07 +0000319Our \function{spam.system()} function must return the value of
320\cdata{sts} as a Python object. This is done using the function
321\cfunction{Py_BuildValue()}, which is something like the inverse of
322\cfunction{PyArg_ParseTuple()}: it takes a format string and an
323arbitrary number of \C{} values, and returns a new Python object.
324More info on \cfunction{Py_BuildValue()} is given later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000325
Fred Drake1e11a5c1998-02-13 07:11:32 +0000326\begin{verbatim}
327 return Py_BuildValue("i", sts);
328\end{verbatim}
329
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000330In this case, it will return an integer object. (Yes, even integers
331are objects on the heap in Python!)
Guido van Rossum6938f061994-08-01 12:22:53 +0000332
Fred Drake0fd82681998-01-09 05:39:38 +0000333If you have a \C{} function that returns no useful argument (a function
Fred Draked7bb3031998-03-03 17:52:07 +0000334returning \ctype{void}), the corresponding Python function must return
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000335\code{None}. You need this idiom to do so:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000336
Fred Drake1e11a5c1998-02-13 07:11:32 +0000337\begin{verbatim}
338 Py_INCREF(Py_None);
339 return Py_None;
340\end{verbatim}
341
Fred Draked7bb3031998-03-03 17:52:07 +0000342\cdata{Py_None} is the \C{} name for the special Python object
Fred Drakea0dbddf1998-04-02 06:50:02 +0000343\code{None}. It is a genuine Python object rather than a \NULL{}
344pointer, which means ``error'' in most contexts, as we have seen.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000345
346
Fred Drake5e8aa541998-11-16 18:34:07 +0000347\section{The Module's Method Table and Initialization Function
348 \label{methodTable}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000349
Fred Draked7bb3031998-03-03 17:52:07 +0000350I promised to show how \cfunction{spam_system()} is called from Python
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000351programs. First, we need to list its name and address in a ``method
352table'':
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000353
Fred Drake1e11a5c1998-02-13 07:11:32 +0000354\begin{verbatim}
355static PyMethodDef SpamMethods[] = {
356 ...
357 {"system", spam_system, METH_VARARGS},
358 ...
359 {NULL, NULL} /* Sentinel */
360};
361\end{verbatim}
362
Fred Drake0fd82681998-01-09 05:39:38 +0000363Note the third entry (\samp{METH_VARARGS}). This is a flag telling
364the interpreter the calling convention to be used for the \C{}
365function. It should normally always be \samp{METH_VARARGS} or
Fred Drakea0dbddf1998-04-02 06:50:02 +0000366\samp{METH_VARARGS | METH_KEYWORDS}; a value of \code{0} means that an
Fred Drake3da06a61998-02-26 18:49:12 +0000367obsolete variant of \cfunction{PyArg_ParseTuple()} is used.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000368
Fred Drakeb6e50321998-02-04 20:26:31 +0000369When using only \samp{METH_VARARGS}, the function should expect
370the Python-level parameters to be passed in as a tuple acceptable for
371parsing via \cfunction{PyArg_ParseTuple()}; more information on this
372function is provided below.
373
Fred Draked7bb3031998-03-03 17:52:07 +0000374The \constant{METH_KEYWORDS} bit may be set in the third field if keyword
Fred Drake0fd82681998-01-09 05:39:38 +0000375arguments should be passed to the function. In this case, the \C{}
376function should accept a third \samp{PyObject *} parameter which will
Fred Drake3da06a61998-02-26 18:49:12 +0000377be a dictionary of keywords. Use \cfunction{PyArg_ParseTupleAndKeywords()}
Fred Drake0fd82681998-01-09 05:39:38 +0000378to parse the arguemts to such a function.
379
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000380The method table must be passed to the interpreter in the module's
381initialization function (which should be the only non-\code{static}
382item defined in the module file):
383
Fred Drake1e11a5c1998-02-13 07:11:32 +0000384\begin{verbatim}
385void
386initspam()
387{
388 (void) Py_InitModule("spam", SpamMethods);
389}
390\end{verbatim}
391
Fred Draked7bb3031998-03-03 17:52:07 +0000392When the Python program imports module \module{spam} for the first
393time, \cfunction{initspam()} is called. It calls
394\cfunction{Py_InitModule()}, which creates a ``module object'' (which
395is inserted in the dictionary \code{sys.modules} under the key
396\code{"spam"}), and inserts built-in function objects into the newly
397created module based upon the table (an array of \ctype{PyMethodDef}
398structures) that was passed as its second argument.
399\cfunction{Py_InitModule()} returns a pointer to the module object
400that it creates (which is unused here). It aborts with a fatal error
401if the module could not be initialized satisfactorily, so the caller
402doesn't need to check for errors.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000403
404
Fred Drake5e8aa541998-11-16 18:34:07 +0000405\section{Compilation and Linkage
406 \label{compilation}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000407
Guido van Rossumb92112d1995-03-20 14:24:09 +0000408There are two more things to do before you can use your new extension:
409compiling and linking it with the Python system. If you use dynamic
410loading, the details depend on the style of dynamic loading your
Fred Drakea0dbddf1998-04-02 06:50:02 +0000411system uses; see the chapter ``Dynamic Loading'' for more information
412about this.
Guido van Rossum6938f061994-08-01 12:22:53 +0000413
414If you can't use dynamic loading, or if you want to make your module a
415permanent part of the Python interpreter, you will have to change the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000416configuration setup and rebuild the interpreter. Luckily, this is
417very simple: just place your file (\file{spammodule.c} for example) in
418the \file{Modules} directory, add a line to the file
Fred Drakea0dbddf1998-04-02 06:50:02 +0000419\file{Modules/Setup.local} describing your file:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000420
Fred Drake1e11a5c1998-02-13 07:11:32 +0000421\begin{verbatim}
422spam spammodule.o
423\end{verbatim}
424
Fred Draked7bb3031998-03-03 17:52:07 +0000425and rebuild the interpreter by running \program{make} in the toplevel
426directory. You can also run \program{make} in the \file{Modules}
Fred Drakea0dbddf1998-04-02 06:50:02 +0000427subdirectory, but then you must first rebuild \file{Makefile}
Fred Draked7bb3031998-03-03 17:52:07 +0000428there by running `\program{make} Makefile'. (This is necessary each
429time you change the \file{Setup} file.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000430
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000431If your module requires additional libraries to link with, these can
Fred Drakea0dbddf1998-04-02 06:50:02 +0000432be listed on the line in the configuration file as well, for instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000433
Fred Drake1e11a5c1998-02-13 07:11:32 +0000434\begin{verbatim}
435spam spammodule.o -lX11
436\end{verbatim}
437
Fred Drake5e8aa541998-11-16 18:34:07 +0000438\section{Calling Python Functions from \C{}
439 \label{callingPython}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000440
Fred Drake0fd82681998-01-09 05:39:38 +0000441So far we have concentrated on making \C{} functions callable from
442Python. The reverse is also useful: calling Python functions from \C{}.
Guido van Rossum6938f061994-08-01 12:22:53 +0000443This is especially the case for libraries that support so-called
Fred Drake0fd82681998-01-09 05:39:38 +0000444``callback'' functions. If a \C{} interface makes use of callbacks, the
Guido van Rossum6938f061994-08-01 12:22:53 +0000445equivalent Python often needs to provide a callback mechanism to the
446Python programmer; the implementation will require calling the Python
Fred Drake0fd82681998-01-09 05:39:38 +0000447callback functions from a \C{} callback. Other uses are also imaginable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000448
449Fortunately, the Python interpreter is easily called recursively, and
Guido van Rossum6938f061994-08-01 12:22:53 +0000450there is a standard interface to call a Python function. (I won't
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000451dwell on how to call the Python parser with a particular string as
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000452input --- if you're interested, have a look at the implementation of
Fred Drake5e8aa541998-11-16 18:34:07 +0000453the \samp{-c} command line option in \file{Python/pythonmain.c} from
454the Python source code.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000455
456Calling a Python function is easy. First, the Python program must
457somehow pass you the Python function object. You should provide a
458function (or some other interface) to do this. When this function is
459called, save a pointer to the Python function object (be careful to
Fred Draked7bb3031998-03-03 17:52:07 +0000460\cfunction{Py_INCREF()} it!) in a global variable --- or whereever you
461see fit. For example, the following function might be part of a module
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000462definition:
463
Fred Drake1e11a5c1998-02-13 07:11:32 +0000464\begin{verbatim}
465static PyObject *my_callback = NULL;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000466
Fred Drake1e11a5c1998-02-13 07:11:32 +0000467static PyObject *
468my_set_callback(dummy, arg)
469 PyObject *dummy, *arg;
470{
Fred Drake5e8aa541998-11-16 18:34:07 +0000471 PyObject *result = NULL;
472 PyObject *temp;
473
474 if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
475 if (!PyCallable_Check(temp)) {
476 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
477 return NULL;
478 }
479 Py_XINCREF(temp); /* Add a reference to new callback */
480 Py_XDECREF(my_callback); /* Dispose of previous callback */
481 my_callback = temp; /* Remember new callback */
482 /* Boilerplate to return "None" */
483 Py_INCREF(Py_None);
484 result = Py_None;
485 }
486 return result;
Fred Drake1e11a5c1998-02-13 07:11:32 +0000487}
488\end{verbatim}
489
Fred Drake5e8aa541998-11-16 18:34:07 +0000490This function must be registered with the interpreter using the
491\constant{METH_VARARGS} flag; this is described in Section
492\ref{methodTable}, ``The Module's Method Table and Initialization
493Function.'' The \cfunction{PyArg_ParseTuple()} function and its
494arguments are documented in Section \ref{parseTuple}, ``Format Strings
495for \cfunction{PyArg_ParseTuple()}.''
496
Fred Draked7bb3031998-03-03 17:52:07 +0000497The macros \cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()}
498increment/decrement the reference count of an object and are safe in
Fred Drake5e8aa541998-11-16 18:34:07 +0000499the presence of \NULL{} pointers (but note that \var{temp} will not be
500\NULL{} in this context). More info on them in Section
501\ref{refcounts}, ``Reference Counts.''
Guido van Rossum6938f061994-08-01 12:22:53 +0000502
Fred Drake0fd82681998-01-09 05:39:38 +0000503Later, when it is time to call the function, you call the \C{} function
Fred Draked7bb3031998-03-03 17:52:07 +0000504\cfunction{PyEval_CallObject()}. This function has two arguments, both
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000505pointers to arbitrary Python objects: the Python function, and the
506argument list. The argument list must always be a tuple object, whose
507length is the number of arguments. To call the Python function with
508no arguments, pass an empty tuple; to call it with one argument, pass
Fred Draked7bb3031998-03-03 17:52:07 +0000509a singleton tuple. \cfunction{Py_BuildValue()} returns a tuple when its
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000510format string consists of zero or more format codes between
511parentheses. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000512
Fred Drake1e11a5c1998-02-13 07:11:32 +0000513\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000514 int arg;
515 PyObject *arglist;
516 PyObject *result;
517 ...
518 arg = 123;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000519 ...
520 /* Time to call the callback */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000521 arglist = Py_BuildValue("(i)", arg);
522 result = PyEval_CallObject(my_callback, arglist);
523 Py_DECREF(arglist);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000524\end{verbatim}
525
Fred Draked7bb3031998-03-03 17:52:07 +0000526\cfunction{PyEval_CallObject()} returns a Python object pointer: this is
527the return value of the Python function. \cfunction{PyEval_CallObject()} is
Guido van Rossumb92112d1995-03-20 14:24:09 +0000528``reference-count-neutral'' with respect to its arguments. In the
Guido van Rossum6938f061994-08-01 12:22:53 +0000529example a new tuple was created to serve as the argument list, which
Fred Draked7bb3031998-03-03 17:52:07 +0000530is \cfunction{Py_DECREF()}-ed immediately after the call.
Guido van Rossum6938f061994-08-01 12:22:53 +0000531
Fred Draked7bb3031998-03-03 17:52:07 +0000532The return value of \cfunction{PyEval_CallObject()} is ``new'': either it
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000533is a brand new object, or it is an existing object whose reference
534count has been incremented. So, unless you want to save it in a
Fred Draked7bb3031998-03-03 17:52:07 +0000535global variable, you should somehow \cfunction{Py_DECREF()} the result,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000536even (especially!) if you are not interested in its value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000537
538Before you do this, however, it is important to check that the return
Fred Draked7bb3031998-03-03 17:52:07 +0000539value isn't \NULL{}. If it is, the Python function terminated by
540raising an exception. If the \C{} code that called
541\cfunction{PyEval_CallObject()} is called from Python, it should now
542return an error indication to its Python caller, so the interpreter
543can print a stack trace, or the calling Python code can handle the
544exception. If this is not possible or desirable, the exception should
545be cleared by calling \cfunction{PyErr_Clear()}. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000546
Fred Drake1e11a5c1998-02-13 07:11:32 +0000547\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000548 if (result == NULL)
549 return NULL; /* Pass error back */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000550 ...use result...
551 Py_DECREF(result);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000552\end{verbatim}
553
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000554Depending on the desired interface to the Python callback function,
Fred Draked7bb3031998-03-03 17:52:07 +0000555you may also have to provide an argument list to
556\cfunction{PyEval_CallObject()}. In some cases the argument list is
557also provided by the Python program, through the same interface that
558specified the callback function. It can then be saved and used in the
559same manner as the function object. In other cases, you may have to
560construct a new tuple to pass as the argument list. The simplest way
561to do this is to call \cfunction{Py_BuildValue()}. For example, if
562you want to pass an integral event code, you might use the following
563code:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000564
Fred Drake1e11a5c1998-02-13 07:11:32 +0000565\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000566 PyObject *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000567 ...
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000568 arglist = Py_BuildValue("(l)", eventcode);
569 result = PyEval_CallObject(my_callback, arglist);
570 Py_DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000571 if (result == NULL)
572 return NULL; /* Pass error back */
573 /* Here maybe use the result */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000574 Py_DECREF(result);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000575\end{verbatim}
576
Fred Draked7bb3031998-03-03 17:52:07 +0000577Note the placement of \samp{Py_DECREF(arglist)} immediately after the
578call, before the error check! Also note that strictly spoken this
579code is not complete: \cfunction{Py_BuildValue()} may run out of
580memory, and this should be checked.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000581
582
Fred Drake5e8aa541998-11-16 18:34:07 +0000583\section{Format Strings for \cfunction{PyArg_ParseTuple()}
584 \label{parseTuple}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000585
Fred Drake3da06a61998-02-26 18:49:12 +0000586The \cfunction{PyArg_ParseTuple()} function is declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000587
Fred Drake1e11a5c1998-02-13 07:11:32 +0000588\begin{verbatim}
589int PyArg_ParseTuple(PyObject *arg, char *format, ...);
590\end{verbatim}
591
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000592The \var{arg} argument must be a tuple object containing an argument
Fred Drake0fd82681998-01-09 05:39:38 +0000593list passed from Python to a \C{} function. The \var{format} argument
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000594must be a format string, whose syntax is explained below. The
595remaining arguments must be addresses of variables whose type is
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000596determined by the format string. For the conversion to succeed, the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000597\var{arg} object must match the format and the format must be
598exhausted.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000599
Fred Drake3da06a61998-02-26 18:49:12 +0000600Note that while \cfunction{PyArg_ParseTuple()} checks that the Python
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000601arguments have the required types, it cannot check the validity of the
Fred Drake0fd82681998-01-09 05:39:38 +0000602addresses of \C{} variables passed to the call: if you make mistakes
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000603there, your code will probably crash or at least overwrite random bits
604in memory. So be careful!
605
606A format string consists of zero or more ``format units''. A format
607unit describes one Python object; it is usually a single character or
608a parenthesized sequence of format units. With a few exceptions, a
609format unit that is not a parenthesized sequence normally corresponds
Fred Drake3da06a61998-02-26 18:49:12 +0000610to a single address argument to \cfunction{PyArg_ParseTuple()}. In the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000611following description, the quoted form is the format unit; the entry
612in (round) parentheses is the Python object type that matches the
Fred Drake0fd82681998-01-09 05:39:38 +0000613format unit; and the entry in [square] brackets is the type of the \C{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000614variable(s) whose address should be passed. (Use the \samp{\&}
615operator to pass a variable's address.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000616
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000617\begin{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000618
Fred Drake3fe985f1998-03-04 03:51:42 +0000619\item[\samp{s} (string) {[char *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000620Convert a Python string to a \C{} pointer to a character string. You
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000621must not provide storage for the string itself; a pointer to an
622existing string is stored into the character pointer variable whose
Fred Drake0fd82681998-01-09 05:39:38 +0000623address you pass. The \C{} string is null-terminated. The Python string
Fred Drake3da06a61998-02-26 18:49:12 +0000624must not contain embedded null bytes; if it does, a \exception{TypeError}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000625exception is raised.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000626
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000627\item[\samp{s\#} (string) {[char *, int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000628This variant on \samp{s} stores into two \C{} variables, the first one
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000629a pointer to a character string, the second one its length. In this
630case the Python string may contain embedded null bytes.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000631
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000632\item[\samp{z} (string or \code{None}) {[char *]}]
633Like \samp{s}, but the Python object may also be \code{None}, in which
Fred Drake0fd82681998-01-09 05:39:38 +0000634case the \C{} pointer is set to \NULL{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000635
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000636\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000637This is to \samp{s\#} as \samp{z} is to \samp{s}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000638
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000639\item[\samp{b} (integer) {[char]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000640Convert a Python integer to a tiny int, stored in a \C{} \ctype{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000641
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000642\item[\samp{h} (integer) {[short int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000643Convert a Python integer to a \C{} \ctype{short int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000644
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000645\item[\samp{i} (integer) {[int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000646Convert a Python integer to a plain \C{} \ctype{int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000647
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000648\item[\samp{l} (integer) {[long int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000649Convert a Python integer to a \C{} \ctype{long int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000650
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000651\item[\samp{c} (string of length 1) {[char]}]
652Convert a Python character, represented as a string of length 1, to a
Fred Draked7bb3031998-03-03 17:52:07 +0000653\C{} \ctype{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000654
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000655\item[\samp{f} (float) {[float]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000656Convert a Python floating point number to a \C{} \ctype{float}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000657
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000658\item[\samp{d} (float) {[double]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000659Convert a Python floating point number to a \C{} \ctype{double}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000660
Fred Drakeb6e50321998-02-04 20:26:31 +0000661\item[\samp{D} (complex) {[Py_complex]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000662Convert a Python complex number to a \C{} \ctype{Py_complex} structure.
Fred Drakeb6e50321998-02-04 20:26:31 +0000663
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000664\item[\samp{O} (object) {[PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000665Store a Python object (without any conversion) in a \C{} object pointer.
666The \C{} program thus receives the actual object that was passed. The
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000667object's reference count is not increased. The pointer stored is not
Fred Drake0fd82681998-01-09 05:39:38 +0000668\NULL{}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000669
Fred Drake3fe985f1998-03-04 03:51:42 +0000670\item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000671Store a Python object in a \C{} object pointer. This is similar to
672\samp{O}, but takes two \C{} arguments: the first is the address of a
673Python type object, the second is the address of the \C{} variable (of
Fred Draked7bb3031998-03-03 17:52:07 +0000674type \ctype{PyObject *}) into which the object pointer is stored.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000675If the Python object does not have the required type, a
Fred Draked7bb3031998-03-03 17:52:07 +0000676\exception{TypeError} exception is raised.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000677
Fred Drake3fe985f1998-03-04 03:51:42 +0000678\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000679Convert a Python object to a \C{} variable through a \var{converter}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000680function. This takes two arguments: the first is a function, the
Fred Drake0fd82681998-01-09 05:39:38 +0000681second is the address of a \C{} variable (of arbitrary type), converted
Fred Draked7bb3031998-03-03 17:52:07 +0000682to \ctype{void *}. The \var{converter} function in turn is called as
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000683follows:
684
685\code{\var{status} = \var{converter}(\var{object}, \var{address});}
686
687where \var{object} is the Python object to be converted and
Fred Draked7bb3031998-03-03 17:52:07 +0000688\var{address} is the \ctype{void *} argument that was passed to
689\cfunction{PyArg_ConvertTuple()}. The returned \var{status} should be
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000690\code{1} for a successful conversion and \code{0} if the conversion
691has failed. When the conversion fails, the \var{converter} function
692should raise an exception.
693
694\item[\samp{S} (string) {[PyStringObject *]}]
Guido van Rossum2474d681998-02-26 17:07:11 +0000695Like \samp{O} but requires that the Python object is a string object.
Fred Draked7bb3031998-03-03 17:52:07 +0000696Raises a \exception{TypeError} exception if the object is not a string
697object. The \C{} variable may also be declared as \ctype{PyObject *}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000698
Fred Drake3fe985f1998-03-04 03:51:42 +0000699\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000700The object must be a Python tuple whose length is the number of format
Fred Drake0fd82681998-01-09 05:39:38 +0000701units in \var{items}. The \C{} arguments must correspond to the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000702individual format units in \var{items}. Format units for tuples may
703be nested.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000704
705\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000706
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000707It is possible to pass Python long integers where integers are
Fred Drake1aedbd81998-02-16 14:47:27 +0000708requested; however no proper range checking is done --- the most
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000709significant bits are silently truncated when the receiving field is
710too small to receive the value (actually, the semantics are inherited
Fred Drake0fd82681998-01-09 05:39:38 +0000711from downcasts in \C{} --- your milage may vary).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000712
713A few other characters have a meaning in a format string. These may
714not occur inside nested parentheses. They are:
715
716\begin{description}
717
718\item[\samp{|}]
719Indicates that the remaining arguments in the Python argument list are
Fred Drake0fd82681998-01-09 05:39:38 +0000720optional. The \C{} variables corresponding to optional arguments should
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000721be initialized to their default value --- when an optional argument is
Fred Drake40e72f71998-03-03 19:37:38 +0000722not specified, \cfunction{PyArg_ParseTuple()} does not touch the contents
Fred Drake0fd82681998-01-09 05:39:38 +0000723of the corresponding \C{} variable(s).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000724
725\item[\samp{:}]
726The list of format units ends here; the string after the colon is used
727as the function name in error messages (the ``associated value'' of
Fred Draked7bb3031998-03-03 17:52:07 +0000728the exceptions that \cfunction{PyArg_ParseTuple()} raises).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000729
730\item[\samp{;}]
731The list of format units ends here; the string after the colon is used
732as the error message \emph{instead} of the default error message.
733Clearly, \samp{:} and \samp{;} mutually exclude each other.
734
735\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000736
737Some example calls:
738
Fred Drake0fd82681998-01-09 05:39:38 +0000739\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000740 int ok;
741 int i, j;
742 long k, l;
743 char *s;
744 int size;
745
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000746 ok = PyArg_ParseTuple(args, ""); /* No arguments */
Guido van Rossum6938f061994-08-01 12:22:53 +0000747 /* Python call: f() */
Fred Drake0fd82681998-01-09 05:39:38 +0000748
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000749 ok = PyArg_ParseTuple(args, "s", &s); /* A string */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000750 /* Possible Python call: f('whoops!') */
751
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000752 ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
Guido van Rossum6938f061994-08-01 12:22:53 +0000753 /* Possible Python call: f(1, 2, 'three') */
Fred Drake0fd82681998-01-09 05:39:38 +0000754
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000755 ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000756 /* A pair of ints and a string, whose size is also returned */
Guido van Rossum7e924dd1997-02-10 16:51:52 +0000757 /* Possible Python call: f((1, 2), 'three') */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000758
759 {
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000760 char *file;
761 char *mode = "r";
762 int bufsize = 0;
763 ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
764 /* A string, and optionally another string and an integer */
765 /* Possible Python calls:
766 f('spam')
767 f('spam', 'w')
768 f('spam', 'wb', 100000) */
769 }
770
771 {
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000772 int left, top, right, bottom, h, v;
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000773 ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000774 &left, &top, &right, &bottom, &h, &v);
Fred Drakea0dbddf1998-04-02 06:50:02 +0000775 /* A rectangle and a point */
776 /* Possible Python call:
777 f(((0, 0), (400, 300)), (10, 10)) */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000778 }
Fred Drakeb6e50321998-02-04 20:26:31 +0000779
780 {
781 Py_complex c;
782 ok = PyArg_ParseTuple(args, "D:myfunction", &c);
783 /* a complex, also providing a function name for errors */
784 /* Possible Python call: myfunction(1+2j) */
785 }
Fred Drake0fd82681998-01-09 05:39:38 +0000786\end{verbatim}
Fred Drakeb6e50321998-02-04 20:26:31 +0000787
788
Fred Drake5e8aa541998-11-16 18:34:07 +0000789\section{Keyword Parsing with \cfunction{PyArg_ParseTupleAndKeywords()}
790 \label{parseTupleAndKeywords}}
Fred Drakeb6e50321998-02-04 20:26:31 +0000791
792The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as
793follows:
794
Fred Drake1e11a5c1998-02-13 07:11:32 +0000795\begin{verbatim}
796int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
797 char *format, char **kwlist, ...);
798\end{verbatim}
Fred Drakeb6e50321998-02-04 20:26:31 +0000799
800The \var{arg} and \var{format} parameters are identical to those of the
801\cfunction{PyArg_ParseTuple()} function. The \var{kwdict} parameter
802is the dictionary of keywords received as the third parameter from the
803Python runtime. The \var{kwlist} parameter is a \NULL{}-terminated
804list of strings which identify the parameters; the names are matched
805with the type information from \var{format} from left to right.
806
807\strong{Note:} Nested tuples cannot be parsed when using keyword
808arguments! Keyword parameters passed in which are not present in the
Fred Drakecd05ca91998-03-07 05:32:08 +0000809\var{kwlist} will cause \exception{TypeError} to be raised.
Fred Drakeb6e50321998-02-04 20:26:31 +0000810
811Here is an example module which uses keywords, based on an example by
Fred Drakea0dbddf1998-04-02 06:50:02 +0000812Geoff Philbrick (\email{philbrick@hks.com}):%
813\index{Philbrick, Geoff}
Fred Drakeb6e50321998-02-04 20:26:31 +0000814
815\begin{verbatim}
816#include <stdio.h>
817#include "Python.h"
818
819static PyObject *
820keywdarg_parrot(self, args, keywds)
821 PyObject *self;
822 PyObject *args;
823 PyObject *keywds;
824{
825 int voltage;
826 char *state = "a stiff";
827 char *action = "voom";
828 char *type = "Norwegian Blue";
829
830 static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
831
832 if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
833 &voltage, &state, &action, &type))
834 return NULL;
835
836 printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
837 action, voltage);
838 printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
839
840 Py_INCREF(Py_None);
841
842 return Py_None;
843}
844
845static PyMethodDef keywdarg_methods[] = {
846 {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS|METH_KEYWORDS},
847 {NULL, NULL} /* sentinel */
848};
849
850void
851initkeywdarg()
852{
853 /* Create the module and add the functions */
Fred Drakecd05ca91998-03-07 05:32:08 +0000854 Py_InitModule("keywdarg", keywdarg_methods);
Fred Drakeb6e50321998-02-04 20:26:31 +0000855}
856\end{verbatim}
857
858
Fred Drake5e8aa541998-11-16 18:34:07 +0000859\section{The \cfunction{Py_BuildValue()} Function
860 \label{buildValue}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000861
Fred Draked7bb3031998-03-03 17:52:07 +0000862This function is the counterpart to \cfunction{PyArg_ParseTuple()}. It is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000863declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000864
Fred Drake1e11a5c1998-02-13 07:11:32 +0000865\begin{verbatim}
866PyObject *Py_BuildValue(char *format, ...);
867\end{verbatim}
868
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000869It recognizes a set of format units similar to the ones recognized by
Fred Draked7bb3031998-03-03 17:52:07 +0000870\cfunction{PyArg_ParseTuple()}, but the arguments (which are input to the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000871function, not output) must not be pointers, just values. It returns a
Fred Drake0fd82681998-01-09 05:39:38 +0000872new Python object, suitable for returning from a \C{} function called
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000873from Python.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000874
Fred Draked7bb3031998-03-03 17:52:07 +0000875One difference with \cfunction{PyArg_ParseTuple()}: while the latter
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000876requires its first argument to be a tuple (since Python argument lists
Fred Draked7bb3031998-03-03 17:52:07 +0000877are always represented as tuples internally),
878\cfunction{Py_BuildValue()} does not always build a tuple. It builds
879a tuple only if its format string contains two or more format units.
880If the format string is empty, it returns \code{None}; if it contains
881exactly one format unit, it returns whatever object is described by
882that format unit. To force it to return a tuple of size 0 or one,
883parenthesize the format string.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000884
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000885In the following description, the quoted form is the format unit; the
886entry in (round) parentheses is the Python object type that the format
887unit will return; and the entry in [square] brackets is the type of
Fred Drake0fd82681998-01-09 05:39:38 +0000888the \C{} value(s) to be passed.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000889
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000890The characters space, tab, colon and comma are ignored in format
891strings (but not within format units such as \samp{s\#}). This can be
892used to make long format strings a tad more readable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000893
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000894\begin{description}
895
896\item[\samp{s} (string) {[char *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000897Convert a null-terminated \C{} string to a Python object. If the \C{}
898string pointer is \NULL{}, \code{None} is returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000899
900\item[\samp{s\#} (string) {[char *, int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000901Convert a \C{} string and its length to a Python object. If the \C{} string
902pointer is \NULL{}, the length is ignored and \code{None} is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000903returned.
904
905\item[\samp{z} (string or \code{None}) {[char *]}]
906Same as \samp{s}.
907
908\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
909Same as \samp{s\#}.
910
911\item[\samp{i} (integer) {[int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000912Convert a plain \C{} \ctype{int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000913
914\item[\samp{b} (integer) {[char]}]
915Same as \samp{i}.
916
917\item[\samp{h} (integer) {[short int]}]
918Same as \samp{i}.
919
920\item[\samp{l} (integer) {[long int]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000921Convert a \C{} \ctype{long int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000922
923\item[\samp{c} (string of length 1) {[char]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000924Convert a \C{} \ctype{int} representing a character to a Python string of
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000925length 1.
926
927\item[\samp{d} (float) {[double]}]
Fred Draked7bb3031998-03-03 17:52:07 +0000928Convert a \C{} \ctype{double} to a Python floating point number.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000929
930\item[\samp{f} (float) {[float]}]
931Same as \samp{d}.
932
933\item[\samp{O} (object) {[PyObject *]}]
934Pass a Python object untouched (except for its reference count, which
Fred Drake0fd82681998-01-09 05:39:38 +0000935is incremented by one). If the object passed in is a \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000936pointer, it is assumed that this was caused because the call producing
937the argument found an error and set an exception. Therefore,
Fred Draked7bb3031998-03-03 17:52:07 +0000938\cfunction{Py_BuildValue()} will return \NULL{} but won't raise an
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000939exception. If no exception has been raised yet,
Fred Draked7bb3031998-03-03 17:52:07 +0000940\cdata{PyExc_SystemError} is set.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000941
942\item[\samp{S} (object) {[PyObject *]}]
943Same as \samp{O}.
944
945\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
946Convert \var{anything} to a Python object through a \var{converter}
947function. The function is called with \var{anything} (which should be
Fred Draked7bb3031998-03-03 17:52:07 +0000948compatible with \ctype{void *}) as its argument and should return a
Fred Drake0fd82681998-01-09 05:39:38 +0000949``new'' Python object, or \NULL{} if an error occurred.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000950
951\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000952Convert a sequence of \C{} values to a Python tuple with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000953of items.
954
955\item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000956Convert a sequence of \C{} values to a Python list with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000957of items.
958
959\item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000960Convert a sequence of \C{} values to a Python dictionary. Each pair of
961consecutive \C{} values adds one item to the dictionary, serving as key
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000962and value, respectively.
963
964\end{description}
965
966If there is an error in the format string, the
Fred Draked7bb3031998-03-03 17:52:07 +0000967\cdata{PyExc_SystemError} exception is raised and \NULL{} returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000968
969Examples (to the left the call, to the right the resulting Python value):
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000970
Fred Drake1e11a5c1998-02-13 07:11:32 +0000971\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000972 Py_BuildValue("") None
973 Py_BuildValue("i", 123) 123
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000974 Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000975 Py_BuildValue("s", "hello") 'hello'
976 Py_BuildValue("ss", "hello", "world") ('hello', 'world')
977 Py_BuildValue("s#", "hello", 4) 'hell'
978 Py_BuildValue("()") ()
979 Py_BuildValue("(i)", 123) (123,)
980 Py_BuildValue("(ii)", 123, 456) (123, 456)
981 Py_BuildValue("(i,i)", 123, 456) (123, 456)
982 Py_BuildValue("[i,i]", 123, 456) [123, 456]
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000983 Py_BuildValue("{s:i,s:i}",
984 "abc", 123, "def", 456) {'abc': 123, 'def': 456}
985 Py_BuildValue("((ii)(ii)) (ii)",
986 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))
Fred Drake1e11a5c1998-02-13 07:11:32 +0000987\end{verbatim}
988
Fred Drake5e8aa541998-11-16 18:34:07 +0000989\section{Reference Counts
990 \label{refcounts}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000991
Fred Drake3da06a61998-02-26 18:49:12 +0000992%\subsection{Introduction}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000993
Fred Drake0fd82681998-01-09 05:39:38 +0000994In languages like \C{} or \Cpp{}, the programmer is responsible for
Fred Draked7bb3031998-03-03 17:52:07 +0000995dynamic allocation and deallocation of memory on the heap. In \C{},
996this is done using the functions \cfunction{malloc()} and
997\cfunction{free()}. In \Cpp{}, the operators \keyword{new} and
998\keyword{delete} are used with essentially the same meaning; they are
999actually implemented using \cfunction{malloc()} and
1000\cfunction{free()}, so we'll restrict the following discussion to the
1001latter.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001002
Fred Draked7bb3031998-03-03 17:52:07 +00001003Every block of memory allocated with \cfunction{malloc()} should
1004eventually be returned to the pool of available memory by exactly one
1005call to \cfunction{free()}. It is important to call
1006\cfunction{free()} at the right time. If a block's address is
1007forgotten but \cfunction{free()} is not called for it, the memory it
1008occupies cannot be reused until the program terminates. This is
1009called a \dfn{memory leak}. On the other hand, if a program calls
1010\cfunction{free()} for a block and then continues to use the block, it
1011creates a conflict with re-use of the block through another
1012\cfunction{malloc()} call. This is called \dfn{using freed memory}.
1013It has the same bad consequences as referencing uninitialized data ---
1014core dumps, wrong results, mysterious crashes.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001015
1016Common causes of memory leaks are unusual paths through the code. For
1017instance, a function may allocate a block of memory, do some
1018calculation, and then free the block again. Now a change in the
1019requirements for the function may add a test to the calculation that
1020detects an error condition and can return prematurely from the
1021function. It's easy to forget to free the allocated memory block when
1022taking this premature exit, especially when it is added later to the
1023code. Such leaks, once introduced, often go undetected for a long
1024time: the error exit is taken only in a small fraction of all calls,
1025and most modern machines have plenty of virtual memory, so the leak
1026only becomes apparent in a long-running process that uses the leaking
1027function frequently. Therefore, it's important to prevent leaks from
1028happening by having a coding convention or strategy that minimizes
1029this kind of errors.
1030
Fred Draked7bb3031998-03-03 17:52:07 +00001031Since Python makes heavy use of \cfunction{malloc()} and
1032\cfunction{free()}, it needs a strategy to avoid memory leaks as well
1033as the use of freed memory. The chosen method is called
1034\dfn{reference counting}. The principle is simple: every object
1035contains a counter, which is incremented when a reference to the
1036object is stored somewhere, and which is decremented when a reference
1037to it is deleted. When the counter reaches zero, the last reference
1038to the object has been deleted and the object is freed.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001039
1040An alternative strategy is called \dfn{automatic garbage collection}.
1041(Sometimes, reference counting is also referred to as a garbage
1042collection strategy, hence my use of ``automatic'' to distinguish the
1043two.) The big advantage of automatic garbage collection is that the
Fred Draked7bb3031998-03-03 17:52:07 +00001044user doesn't need to call \cfunction{free()} explicitly. (Another claimed
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001045advantage is an improvement in speed or memory usage --- this is no
Fred Drake0fd82681998-01-09 05:39:38 +00001046hard fact however.) The disadvantage is that for \C{}, there is no
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001047truly portable automatic garbage collector, while reference counting
Fred Draked7bb3031998-03-03 17:52:07 +00001048can be implemented portably (as long as the functions \cfunction{malloc()}
1049and \cfunction{free()} are available --- which the \C{} Standard guarantees).
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001050Maybe some day a sufficiently portable automatic garbage collector
Fred Drake0fd82681998-01-09 05:39:38 +00001051will be available for \C{}. Until then, we'll have to live with
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001052reference counts.
1053
Fred Drake5e8aa541998-11-16 18:34:07 +00001054\subsection{Reference Counting in Python
1055 \label{refcountsInPython}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001056
1057There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)},
1058which handle the incrementing and decrementing of the reference count.
Fred Draked7bb3031998-03-03 17:52:07 +00001059\cfunction{Py_DECREF()} also frees the object when the count reaches zero.
1060For flexibility, it doesn't call \cfunction{free()} directly --- rather, it
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001061makes a call through a function pointer in the object's \dfn{type
1062object}. For this purpose (and others), every object also contains a
1063pointer to its type object.
1064
1065The big question now remains: when to use \code{Py_INCREF(x)} and
1066\code{Py_DECREF(x)}? Let's first introduce some terms. Nobody
1067``owns'' an object; however, you can \dfn{own a reference} to an
1068object. An object's reference count is now defined as the number of
1069owned references to it. The owner of a reference is responsible for
Fred Draked7bb3031998-03-03 17:52:07 +00001070calling \cfunction{Py_DECREF()} when the reference is no longer
1071needed. Ownership of a reference can be transferred. There are three
1072ways to dispose of an owned reference: pass it on, store it, or call
1073\cfunction{Py_DECREF()}. Forgetting to dispose of an owned reference
1074creates a memory leak.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001075
1076It is also possible to \dfn{borrow}\footnote{The metaphor of
1077``borrowing'' a reference is not completely correct: the owner still
1078has a copy of the reference.} a reference to an object. The borrower
Fred Draked7bb3031998-03-03 17:52:07 +00001079of a reference should not call \cfunction{Py_DECREF()}. The borrower must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001080not hold on to the object longer than the owner from which it was
1081borrowed. Using a borrowed reference after the owner has disposed of
1082it risks using freed memory and should be avoided
1083completely.\footnote{Checking that the reference count is at least 1
1084\strong{does not work} --- the reference count itself could be in
1085freed memory and may thus be reused for another object!}
1086
1087The advantage of borrowing over owning a reference is that you don't
1088need to take care of disposing of the reference on all possible paths
1089through the code --- in other words, with a borrowed reference you
1090don't run the risk of leaking when a premature exit is taken. The
1091disadvantage of borrowing over leaking is that there are some subtle
1092situations where in seemingly correct code a borrowed reference can be
1093used after the owner from which it was borrowed has in fact disposed
1094of it.
1095
1096A borrowed reference can be changed into an owned reference by calling
Fred Draked7bb3031998-03-03 17:52:07 +00001097\cfunction{Py_INCREF()}. This does not affect the status of the owner from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001098which the reference was borrowed --- it creates a new owned reference,
1099and gives full owner responsibilities (i.e., the new owner must
1100dispose of the reference properly, as well as the previous owner).
1101
Fred Drake5e8aa541998-11-16 18:34:07 +00001102\subsection{Ownership Rules
1103 \label{ownershipRules}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001104
1105Whenever an object reference is passed into or out of a function, it
1106is part of the function's interface specification whether ownership is
1107transferred with the reference or not.
1108
1109Most functions that return a reference to an object pass on ownership
1110with the reference. In particular, all functions whose function it is
Fred Draked7bb3031998-03-03 17:52:07 +00001111to create a new object, e.g.\ \cfunction{PyInt_FromLong()} and
1112\cfunction{Py_BuildValue()}, pass ownership to the receiver. Even if in
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001113fact, in some cases, you don't receive a reference to a brand new
1114object, you still receive ownership of the reference. For instance,
Fred Draked7bb3031998-03-03 17:52:07 +00001115\cfunction{PyInt_FromLong()} maintains a cache of popular values and can
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001116return a reference to a cached item.
1117
1118Many functions that extract objects from other objects also transfer
1119ownership with the reference, for instance
Fred Draked7bb3031998-03-03 17:52:07 +00001120\cfunction{PyObject_GetAttrString()}. The picture is less clear, here,
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001121however, since a few common routines are exceptions:
Fred Draked7bb3031998-03-03 17:52:07 +00001122\cfunction{PyTuple_GetItem()}, \cfunction{PyList_GetItem()},
1123\cfunction{PyDict_GetItem()}, and \cfunction{PyDict_GetItemString()}
1124all return references that you borrow from the tuple, list or
1125dictionary.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001126
Fred Draked7bb3031998-03-03 17:52:07 +00001127The function \cfunction{PyImport_AddModule()} also returns a borrowed
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001128reference, even though it may actually create the object it returns:
1129this is possible because an owned reference to the object is stored in
1130\code{sys.modules}.
1131
1132When you pass an object reference into another function, in general,
1133the function borrows the reference from you --- if it needs to store
Fred Draked7bb3031998-03-03 17:52:07 +00001134it, it will use \cfunction{Py_INCREF()} to become an independent
1135owner. There are exactly two important exceptions to this rule:
1136\cfunction{PyTuple_SetItem()} and \cfunction{PyList_SetItem()}. These
1137functions take over ownership of the item passed to them --- even if
1138they fail! (Note that \cfunction{PyDict_SetItem()} and friends don't
Fred Drakea0dbddf1998-04-02 06:50:02 +00001139take over ownership --- they are ``normal.'')
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001140
Fred Drake0fd82681998-01-09 05:39:38 +00001141When a \C{} function is called from Python, it borrows references to its
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001142arguments from the caller. The caller owns a reference to the object,
1143so the borrowed reference's lifetime is guaranteed until the function
1144returns. Only when such a borrowed reference must be stored or passed
1145on, it must be turned into an owned reference by calling
Fred Draked7bb3031998-03-03 17:52:07 +00001146\cfunction{Py_INCREF()}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001147
Fred Drake0fd82681998-01-09 05:39:38 +00001148The object reference returned from a \C{} function that is called from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001149Python must be an owned reference --- ownership is tranferred from the
1150function to its caller.
1151
Fred Drake5e8aa541998-11-16 18:34:07 +00001152\subsection{Thin Ice
1153 \label{thinIce}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001154
1155There are a few situations where seemingly harmless use of a borrowed
1156reference can lead to problems. These all have to do with implicit
1157invocations of the interpreter, which can cause the owner of a
1158reference to dispose of it.
1159
1160The first and most important case to know about is using
Fred Draked7bb3031998-03-03 17:52:07 +00001161\cfunction{Py_DECREF()} on an unrelated object while borrowing a
1162reference to a list item. For instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001163
Fred Drake1e11a5c1998-02-13 07:11:32 +00001164\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001165bug(PyObject *list) {
1166 PyObject *item = PyList_GetItem(list, 0);
Fred Drakea0dbddf1998-04-02 06:50:02 +00001167
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001168 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1169 PyObject_Print(item, stdout, 0); /* BUG! */
1170}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001171\end{verbatim}
1172
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001173This function first borrows a reference to \code{list[0]}, then
1174replaces \code{list[1]} with the value \code{0}, and finally prints
1175the borrowed reference. Looks harmless, right? But it's not!
1176
Fred Draked7bb3031998-03-03 17:52:07 +00001177Let's follow the control flow into \cfunction{PyList_SetItem()}. The list
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001178owns references to all its items, so when item 1 is replaced, it has
1179to dispose of the original item 1. Now let's suppose the original
1180item 1 was an instance of a user-defined class, and let's further
Fred Draked7bb3031998-03-03 17:52:07 +00001181suppose that the class defined a \method{__del__()} method. If this
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001182class instance has a reference count of 1, disposing of it will call
Fred Draked7bb3031998-03-03 17:52:07 +00001183its \method{__del__()} method.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001184
Fred Draked7bb3031998-03-03 17:52:07 +00001185Since it is written in Python, the \method{__del__()} method can execute
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001186arbitrary Python code. Could it perhaps do something to invalidate
Fred Draked7bb3031998-03-03 17:52:07 +00001187the reference to \code{item} in \cfunction{bug()}? You bet! Assuming
1188that the list passed into \cfunction{bug()} is accessible to the
1189\method{__del__()} method, it could execute a statement to the effect of
1190\samp{del list[0]}, and assuming this was the last reference to that
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001191object, it would free the memory associated with it, thereby
1192invalidating \code{item}.
1193
1194The solution, once you know the source of the problem, is easy:
1195temporarily increment the reference count. The correct version of the
1196function reads:
1197
Fred Drake1e11a5c1998-02-13 07:11:32 +00001198\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001199no_bug(PyObject *list) {
1200 PyObject *item = PyList_GetItem(list, 0);
Fred Drakea0dbddf1998-04-02 06:50:02 +00001201
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001202 Py_INCREF(item);
1203 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1204 PyObject_Print(item, stdout, 0);
1205 Py_DECREF(item);
1206}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001207\end{verbatim}
1208
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001209This is a true story. An older version of Python contained variants
Fred Drake0fd82681998-01-09 05:39:38 +00001210of this bug and someone spent a considerable amount of time in a \C{}
Fred Draked7bb3031998-03-03 17:52:07 +00001211debugger to figure out why his \method{__del__()} methods would fail...
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001212
1213The second case of problems with a borrowed reference is a variant
1214involving threads. Normally, multiple threads in the Python
1215interpreter can't get in each other's way, because there is a global
1216lock protecting Python's entire object space. However, it is possible
1217to temporarily release this lock using the macro
1218\code{Py_BEGIN_ALLOW_THREADS}, and to re-acquire it using
1219\code{Py_END_ALLOW_THREADS}. This is common around blocking I/O
1220calls, to let other threads use the CPU while waiting for the I/O to
1221complete. Obviously, the following function has the same problem as
1222the previous one:
1223
Fred Drake1e11a5c1998-02-13 07:11:32 +00001224\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001225bug(PyObject *list) {
1226 PyObject *item = PyList_GetItem(list, 0);
1227 Py_BEGIN_ALLOW_THREADS
1228 ...some blocking I/O call...
1229 Py_END_ALLOW_THREADS
1230 PyObject_Print(item, stdout, 0); /* BUG! */
1231}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001232\end{verbatim}
1233
Fred Drake5e8aa541998-11-16 18:34:07 +00001234\subsection{NULL Pointers
1235 \label{nullPointers}}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001236
Fred Drakea0dbddf1998-04-02 06:50:02 +00001237In general, functions that take object references as arguments do not
Fred Drake0fd82681998-01-09 05:39:38 +00001238expect you to pass them \NULL{} pointers, and will dump core (or
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001239cause later core dumps) if you do so. Functions that return object
Fred Drake0fd82681998-01-09 05:39:38 +00001240references generally return \NULL{} only to indicate that an
1241exception occurred. The reason for not testing for \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001242arguments is that functions often pass the objects they receive on to
Fred Drake0fd82681998-01-09 05:39:38 +00001243other function --- if each function were to test for \NULL{},
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001244there would be a lot of redundant tests and the code would run slower.
1245
Fred Drakee743fd01998-11-24 17:07:29 +00001246It is better to test for \NULL{} only at the ``source'', i.e.\ when a
1247pointer that may be \NULL{} is received, e.g.\ from
Fred Draked7bb3031998-03-03 17:52:07 +00001248\cfunction{malloc()} or from a function that may raise an exception.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001249
Fred Draked7bb3031998-03-03 17:52:07 +00001250The macros \cfunction{Py_INCREF()} and \cfunction{Py_DECREF()}
Fred Drakea0dbddf1998-04-02 06:50:02 +00001251do not check for \NULL{} pointers --- however, their variants
Fred Draked7bb3031998-03-03 17:52:07 +00001252\cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()} do.
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001253
1254The macros for checking for a particular object type
Fred Drake0fd82681998-01-09 05:39:38 +00001255(\code{Py\var{type}_Check()}) don't check for \NULL{} pointers ---
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001256again, there is much code that calls several of these in a row to test
1257an object against various different expected types, and this would
Fred Drake0fd82681998-01-09 05:39:38 +00001258generate redundant tests. There are no variants with \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001259checking.
1260
Fred Drake0fd82681998-01-09 05:39:38 +00001261The \C{} function calling mechanism guarantees that the argument list
1262passed to \C{} functions (\code{args} in the examples) is never
1263\NULL{} --- in fact it guarantees that it is always a tuple.%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001264\footnote{These guarantees don't hold when you use the ``old'' style
1265calling convention --- this is still found in much existing code.}
1266
Fred Drake0fd82681998-01-09 05:39:38 +00001267It is a severe error to ever let a \NULL{} pointer ``escape'' to
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001268the Python user.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001269
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001270
Fred Drake5e8aa541998-11-16 18:34:07 +00001271\section{Writing Extensions in \Cpp{}
1272 \label{cplusplus}}
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001273
Guido van Rossum16d6e711994-08-08 12:30:22 +00001274It is possible to write extension modules in \Cpp{}. Some restrictions
Guido van Rossumed39cd01995-10-08 00:17:19 +00001275apply. If the main program (the Python interpreter) is compiled and
Fred Drake0fd82681998-01-09 05:39:38 +00001276linked by the \C{} compiler, global or static objects with constructors
Guido van Rossumed39cd01995-10-08 00:17:19 +00001277cannot be used. This is not a problem if the main program is linked
Guido van Rossumafcd5891998-02-05 19:59:39 +00001278by the \Cpp{} compiler. Functions that will be called by the
1279Python interpreter (in particular, module initalization functions)
1280have to be declared using \code{extern "C"}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001281It is unnecessary to enclose the Python header files in
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001282\code{extern "C" \{...\}} --- they use this form already if the symbol
Fred Drake0fd82681998-01-09 05:39:38 +00001283\samp{__cplusplus} is defined (all recent \Cpp{} compilers define this
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001284symbol).
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001285
Fred Drakee743fd01998-11-24 17:07:29 +00001286
1287
1288\chapter{Building \C{} and \Cpp{} Extensions on \UNIX{}}
1289
1290\sectionauthor{Fim Fulton}{jim@Digicool.com}
1291
1292
1293%The make file make file, building C extensions on Unix
1294
1295
1296Starting in Python 1.4, Python provides a special make file for
1297building make files for building dynamically-linked extensions and
1298custom interpreters. The make file make file builds a make file
1299that reflects various system variables determined by configure when
1300the Python interpreter was built, so people building module's don't
1301have to resupply these settings. This vastly simplifies the process
1302of building extensions and custom interpreters on Unix systems.
1303
1304The make file make file is distributed as the file
1305\file{Misc/Makefile.pre.in} in the Python source distribution. The
1306first step in building extensions or custom interpreters is to copy
1307this make file to a development directory containing extension module
1308source.
1309
1310The make file make file, \file{Makefile.pre.in} uses metadata
1311provided in a file named \file{Setup}. The format of the \file{Setup}
1312file is the same as the \file{Setup} (or \file{Setup.in}) file
1313provided in the \file{Modules/} directory of the Python source
1314distribution. The \file{Setup} file contains variable definitions::
1315
1316\begin{verbatim}
1317EC=/projects/ExtensionClass
1318\end{verbatim}
1319
1320and module description lines. It can also contain blank lines and
1321comment lines that start with \character{\#}.
1322
1323A module description line includes a module name, source files,
1324options, variable references, and other input files, such
1325as libraries or object files. Consider a simple example::
1326
1327\begin{verbatim}
1328ExtensionClass ExtensionClass.c
1329\end{verbatim}
1330
1331This is the simplest form of a module definition line. It defines a
1332dule, \module{ExtensionClass}, which has a single source file,
1333\file{ExtensionClass.c}.
1334
1335Here is a slightly more complex example that uses an \strong{-I}
1336option to specify an include directory:
1337
1338\begin{verbatim}
1339cPersistence cPersistence.c -I$(EC)
1340\end{verbatim}
1341
1342This example also illustrates the format for variable references.
1343
1344For systems that support dynamic linking, the \file{Setup} file should
1345begin:
1346
1347\begin{verbatim}
1348*shared*
1349\end{verbatim}
1350
1351to indicate that the modules defined in \file{Setup} are to be built
1352as dynamically-linked linked modules.
1353
1354Here is a complete \file{Setup} file for building a
1355\module{cPersistent} module:
1356
1357\begin{verbatim}
1358# Set-up file to build the cPersistence module.
1359# Note that the text should begin in the first column.
1360*shared*
1361
1362# We need the path to the directory containing the ExtensionClass
1363# include file.
1364EC=/projects/ExtensionClass
1365cPersistence cPersistence.c -I$(EC)
1366\end{verbatim}
1367
1368After the \file{Setup} file has been created, \file{Makefile.pre.in}
1369is run with the \samp{boot} target to create a make file:
1370
1371\begin{verbatim}
1372make -f Makefile.pre.in boot
1373\end{verbatim}
1374
1375This creates the file, Makefile. To build the extensions, simply
1376run the created make file:
1377
1378\begin{verbatim}
1379make
1380\end{verbatim}
1381
1382It's not necessary to re-run \file{Makefile.pre.in} if the
1383\file{Setup} file is changed. The make file automatically rebuilds
1384itself if the \file{Setup} file changes.
1385
1386\section{Building Custom Interpreters}
1387
1388The make file built by \file{Makefile.pre.in} can be run with the
1389\samp{static} target to build an interpreter:
1390
1391\begin{verbatim}
1392make static
1393\end{verbatim}
1394
1395Any modules defined in the Setup file before the \samp{*shared*} line
1396will be statically linked into the interpreter. Typically, a
1397\samp{*shared*} line is omitted from the Setup file when a custom
1398interpreter is desired.
1399
1400\section{Module Definition Options}
1401
1402Several compiler options are supported:
1403
1404\begin{tableii}{l|l}{}{Option}{Meaning}
1405 \lineii{-C}{Tell the C pre-processor not to discard comments}
1406 \lineii{-D\var{name}=\var{value}}{Define a macro}
1407 \lineii{-I\var{dir}}{Specify an include directory, \var{dir}}
1408 \lineii{-L\var{dir}}{Specify a library directory, \var{dir}}
1409 \lineii{-l\var{lib}}{Link a library, \var{lib}}
1410 \lineii{-U\var{name}}{Undefine a macro}
1411\end{tableii}
1412
1413Other compiler options can be included (snuck in) by putting them
1414in variable variables.
1415
1416Source files can include files with \file{.c}, \file{.C}, \file{.cc},
1417and \file{.c++} extensions.
1418
1419Other input files include files with \file{.o} or \file{.a}
1420extensions.
1421
1422
1423\section{Example}
1424
1425Here is a more complicated example from \file{Modules/Setup.in}:
1426
1427\begin{verbatim}
1428GMP=/ufs/guido/src/gmp
1429mpz mpzmodule.c -I$(GMP) $(GMP)/libgmp.a
1430\end{verbatim}
1431
1432which could also be written as:
1433
1434\begin{verbatim}
1435mpz mpzmodule.c -I$(GMP) -L$(GMP) -lgmp
1436\end{verbatim}
1437
1438
1439\section{Distributing your extension modules
1440 \label{distributing}}
1441
1442When distributing your extension modules in source form, make sure to
1443include a \file{Setup} file. The \file{Setup} file should be named
1444\file{Setup.in} in the distribution. The make file make file,
1445\file{Makefile.pre.in}, will copy \file{Setup.in} to \file{Setup}.
1446Distributing a \file{Setup.in} file makes it easy for people to
1447customize the \file{Setup} file while keeping the original in
1448\file{Setup.in}.
1449
1450It is a good idea to include a copy of \file{Makefile.pre.in} for
1451people who do not have a source distribution of Python.
1452
1453Do not distribute a make file. People building your modules
1454should use \file{Makefile.pre.in} to build their own make file.
1455
1456
Fred Drake5e8aa541998-11-16 18:34:07 +00001457\chapter{Embedding Python in Another Application
1458 \label{embedding}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001459
1460Embedding Python is similar to extending it, but not quite. The
1461difference is that when you extend Python, the main program of the
Guido van Rossum16d6e711994-08-08 12:30:22 +00001462application is still the Python interpreter, while if you embed
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001463Python, the main program may have nothing to do with Python ---
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001464instead, some parts of the application occasionally call the Python
1465interpreter to run some Python code.
1466
1467So if you are embedding Python, you are providing your own main
1468program. One of the things this main program has to do is initialize
1469the Python interpreter. At the very least, you have to call the
Fred Draked7bb3031998-03-03 17:52:07 +00001470function \cfunction{Py_Initialize()}. There are optional calls to
1471pass command line arguments to Python. Then later you can call the
1472interpreter from any part of the application.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001473
1474There are several different ways to call the interpreter: you can pass
Fred Draked7bb3031998-03-03 17:52:07 +00001475a string containing Python statements to
1476\cfunction{PyRun_SimpleString()}, or you can pass a stdio file pointer
1477and a file name (for identification in error messages only) to
1478\cfunction{PyRun_SimpleFile()}. You can also call the lower-level
1479operations described in the previous chapters to construct and use
1480Python objects.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001481
1482A simple demo of embedding Python can be found in the directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001483\file{Demo/embed}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001484
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001485
Fred Drake5e8aa541998-11-16 18:34:07 +00001486\section{Embedding Python in \Cpp{}
1487 \label{embeddingInCplusplus}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001488
Guido van Rossum16d6e711994-08-08 12:30:22 +00001489It is also possible to embed Python in a \Cpp{} program; precisely how this
1490is done will depend on the details of the \Cpp{} system used; in general you
1491will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
1492to compile and link your program. There is no need to recompile Python
1493itself using \Cpp{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001494
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001495
Fred Drake5e8aa541998-11-16 18:34:07 +00001496\chapter{Dynamic Loading
1497 \label{dynload}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001498
Guido van Rossum6938f061994-08-01 12:22:53 +00001499On most modern systems it is possible to configure Python to support
Fred Drake0fd82681998-01-09 05:39:38 +00001500dynamic loading of extension modules implemented in \C{}. When shared
Guido van Rossum6938f061994-08-01 12:22:53 +00001501libraries are used dynamic loading is configured automatically;
1502otherwise you have to select it as a build option (see below). Once
1503configured, dynamic loading is trivial to use: when a Python program
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001504executes \code{import spam}, the search for modules tries to find a
1505file \file{spammodule.o} (\file{spammodule.so} when using shared
Fred Drakeb789c701998-04-02 16:19:15 +00001506libraries) in the module search path,%
1507\indexiii{module}{search}{path}
1508and if one is found, it is loaded into the executing binary and
1509executed. Once loaded, the module acts just like a built-in extension
1510module.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001511
Guido van Rossumb92112d1995-03-20 14:24:09 +00001512The advantages of dynamic loading are twofold: the ``core'' Python
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001513binary gets smaller, and users can extend Python with their own
Fred Drake0fd82681998-01-09 05:39:38 +00001514modules implemented in \C{} without having to build and maintain their
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001515own copy of the Python interpreter. There are also disadvantages:
1516dynamic loading isn't available on all systems (this just means that
1517on some systems you have to use static loading), and dynamically
1518loading a module that was compiled for a different version of Python
Guido van Rossum6938f061994-08-01 12:22:53 +00001519(e.g. with a different representation of objects) may dump core.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001520
1521
Fred Drake5e8aa541998-11-16 18:34:07 +00001522\section{Configuring and Building the Interpreter for Dynamic Loading
1523 \label{dynloadConfig}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001524
Guido van Rossum6938f061994-08-01 12:22:53 +00001525There are three styles of dynamic loading: one using shared libraries,
1526one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
1527loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001528
Fred Drake5e8aa541998-11-16 18:34:07 +00001529\subsection{Shared Libraries
1530 \label{sharedlibs}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001531
Guido van Rossum16d6e711994-08-08 12:30:22 +00001532The following systems support dynamic loading using shared libraries:
Fred Drakea0dbddf1998-04-02 06:50:02 +00001533SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!), Linux, FreeBSD,
1534NetBSD; and probably all systems derived from SVR4, or at least those
1535SVR4 derivatives that support shared libraries (are there any that
1536don't?).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001537
Guido van Rossum6938f061994-08-01 12:22:53 +00001538You don't need to do anything to configure dynamic loading on these
1539systems --- the \file{configure} detects the presence of the
Fred Drakea0dbddf1998-04-02 06:50:02 +00001540\code{<dlfcn.h>} header file and automatically configures dynamic
Guido van Rossum6938f061994-08-01 12:22:53 +00001541loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001542
Fred Drake5e8aa541998-11-16 18:34:07 +00001543\subsection{SGI IRIX 4 Dynamic Loading
1544 \label{irixDynload}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001545
Guido van Rossum6938f061994-08-01 12:22:53 +00001546Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
1547loading. (SGI IRIX 5 might also support it but it is inferior to
1548using shared libraries so there is no reason to; a small test didn't
1549work right away so I gave up trying to support it.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001550
Fred Draked7bb3031998-03-03 17:52:07 +00001551Before you build Python, you first need to fetch and build the
1552\code{dl} package written by Jack Jansen. This is available by
Fred Drakea0dbddf1998-04-02 06:50:02 +00001553anonymous ftp from \url{ftp://ftp.cwi.nl/pub/dynload/}, file
Guido van Rossum6938f061994-08-01 12:22:53 +00001554\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
1555instructions in the package's \file{README} file to build it.
1556
1557Once you have built \code{dl}, you can configure Python to use it. To
Fred Drakea0dbddf1998-04-02 06:50:02 +00001558this end, you run the \program{configure} script with the option
Guido van Rossum6938f061994-08-01 12:22:53 +00001559\code{--with-dl=\var{directory}} where \var{directory} is the absolute
1560pathname of the \code{dl} directory.
1561
1562Now build and install Python as you normally would (see the
1563\file{README} file in the toplevel Python directory.)
1564
Fred Drake5e8aa541998-11-16 18:34:07 +00001565\subsection{GNU Dynamic Loading
1566 \label{gnuDynload}}
Guido van Rossum6938f061994-08-01 12:22:53 +00001567
1568GNU dynamic loading supports (according to its \file{README} file) the
1569following hardware and software combinations: VAX (Ultrix), Sun 3
1570(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
1571Atari ST. There is no reason to use it on a Sparc; I haven't seen a
1572Sun 3 for years so I don't know if these have shared libraries or not.
1573
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001574You need to fetch and build two packages.
1575One is GNU DLD. All development of this code has been done with DLD
Fred Drakeca6567f1998-01-22 20:44:18 +00001576version 3.2.3, which is available by anonymous ftp from
1577\url{ftp://ftp.cwi.nl/pub/dynload}, file
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001578\file{dld-3.2.3.tar.Z}. (A more recent version of DLD is available
Fred Drakeca6567f1998-01-22 20:44:18 +00001579via \url{http://www-swiss.ai.mit.edu/~jaffer/DLD.html} but this has
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001580not been tested.)
1581The other package needed is an
Guido van Rossum6938f061994-08-01 12:22:53 +00001582emulation of Jack Jansen's \code{dl} package that I wrote on top of
1583GNU DLD 3.2.3. This is available from the same host and directory,
Guido van Rossum98046b91997-08-14 19:50:18 +00001584file \file{dl-dld-1.1.tar.Z}. (The version number may change --- but I doubt
Guido van Rossum6938f061994-08-01 12:22:53 +00001585it will.) Follow the instructions in each package's \file{README}
Guido van Rossum98046b91997-08-14 19:50:18 +00001586file to configure and build them.
Guido van Rossum6938f061994-08-01 12:22:53 +00001587
1588Now configure Python. Run the \file{configure} script with the option
1589\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
1590\var{dl-directory} is the absolute pathname of the directory where you
1591have built the \file{dl-dld} package, and \var{dld-directory} is that
1592of the GNU DLD package. The Python interpreter you build hereafter
1593will support GNU dynamic loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001594
1595
Fred Drake5e8aa541998-11-16 18:34:07 +00001596\section{Building a Dynamically Loadable Module
1597 \label{makedynload}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001598
Guido van Rossum6938f061994-08-01 12:22:53 +00001599Since there are three styles of dynamic loading, there are also three
1600groups of instructions for building a dynamically loadable module.
1601Instructions common for all three styles are given first. Assuming
Fred Draked7bb3031998-03-03 17:52:07 +00001602your module is called \module{spam}, the source filename must be
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001603\file{spammodule.c}, so the object name is \file{spammodule.o}. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001604module must be written as a normal Python extension module (as
1605described earlier).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001606
Guido van Rossum6938f061994-08-01 12:22:53 +00001607Note that in all cases you will have to create your own Makefile that
1608compiles your module file(s). This Makefile will have to pass two
Fred Drake0fd82681998-01-09 05:39:38 +00001609\samp{-I} arguments to the \C{} compiler which will make it find the
Fred Drakeb789c701998-04-02 16:19:15 +00001610Python header files. If the Make variable \makevar{PYTHONTOP} points to
1611the toplevel Python directory, your \makevar{CFLAGS} Make variable should
Guido van Rossum6938f061994-08-01 12:22:53 +00001612contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
Fred Drakeb789c701998-04-02 16:19:15 +00001613(Most header files are in the \file{Include/} subdirectory, but the
Guido van Rossum305ed111996-08-19 22:59:46 +00001614\file{config.h} header lives in the toplevel directory.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001615
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001616
Fred Drake5e8aa541998-11-16 18:34:07 +00001617\subsection{Shared Libraries
1618 \label{linking}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001619
Fred Drakeaf8a0151998-01-14 14:51:31 +00001620You must link the \file{.o} file to produce a shared library. This is
1621done using a special invocation of the \UNIX{} loader/linker,
Fred Drakea0dbddf1998-04-02 06:50:02 +00001622\manpage{ld}{1}. Unfortunately the invocation differs slightly per
Fred Drakeaf8a0151998-01-14 14:51:31 +00001623system.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001624
Guido van Rossum6938f061994-08-01 12:22:53 +00001625On SunOS 4, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001626\begin{verbatim}
1627ld spammodule.o -o spammodule.so
1628\end{verbatim}
1629
Guido van Rossum6938f061994-08-01 12:22:53 +00001630On Solaris 2, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001631\begin{verbatim}
1632ld -G spammodule.o -o spammodule.so
1633\end{verbatim}
1634
Guido van Rossum6938f061994-08-01 12:22:53 +00001635On SGI IRIX 5, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001636\begin{verbatim}
1637ld -shared spammodule.o -o spammodule.so
1638\end{verbatim}
1639
Fred Draked7bb3031998-03-03 17:52:07 +00001640On other systems, consult the manual page for \manpage{ld}{1} to find
1641what flags, if any, must be used.
Guido van Rossum6938f061994-08-01 12:22:53 +00001642
1643If your extension module uses system libraries that haven't already
1644been linked with Python (e.g. a windowing system), these must be
Fred Draked7bb3031998-03-03 17:52:07 +00001645passed to the \program{ld} command as \samp{-l} options after the
Guido van Rossum6938f061994-08-01 12:22:53 +00001646\samp{.o} file.
1647
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001648The resulting file \file{spammodule.so} must be copied into a directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001649along the Python module search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001650
1651
Fred Drake5e8aa541998-11-16 18:34:07 +00001652\subsection{SGI IRIX 4 Dynamic Loading
1653 \label{irixLinking}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001654
Fred Drakeaf8a0151998-01-14 14:51:31 +00001655\strong{IMPORTANT:} You must compile your extension module with the
Fred Drakea0dbddf1998-04-02 06:50:02 +00001656additional \C{} flag \samp{-G0} (or \samp{-G 0}). This instructs the
Guido van Rossum6938f061994-08-01 12:22:53 +00001657assembler to generate position-independent code.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001658
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001659You don't need to link the resulting \file{spammodule.o} file; just
Fred Drakeb789c701998-04-02 16:19:15 +00001660copy it into a directory along the Python module search path.%
1661\indexiii{module}{search}{path}
Guido van Rossum6938f061994-08-01 12:22:53 +00001662
1663The first time your extension is loaded, it takes some extra time and
1664a few messages may be printed. This creates a file
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001665\file{spammodule.ld} which is an image that can be loaded quickly into
Guido van Rossum6938f061994-08-01 12:22:53 +00001666the Python interpreter process. When a new Python interpreter is
1667installed, the \code{dl} package detects this and rebuilds
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001668\file{spammodule.ld}. The file \file{spammodule.ld} is placed in the
1669directory where \file{spammodule.o} was found, unless this directory is
Guido van Rossum6938f061994-08-01 12:22:53 +00001670unwritable; in that case it is placed in a temporary
1671directory.\footnote{Check the manual page of the \code{dl} package for
1672details.}
1673
1674If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001675create a file \file{spammodule.libs} in the same directory as the
1676\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001677whitespace-separated options that will be passed to the linker ---
1678normally only \samp{-l} options or absolute pathnames of libraries
1679(\samp{.a} files) should be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001680
1681
Fred Drake5e8aa541998-11-16 18:34:07 +00001682\subsection{GNU Dynamic Loading
1683 \label{gnuLinking}}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001684
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001685Just copy \file{spammodule.o} into a directory along the Python module
Fred Drakeb789c701998-04-02 16:19:15 +00001686search path.%
1687\indexiii{module}{search}{path}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001688
Guido van Rossum6938f061994-08-01 12:22:53 +00001689If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001690create a file \file{spammodule.libs} in the same directory as the
1691\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001692whitespace-separated absolute pathnames of libraries (\samp{.a}
1693files). No \samp{-l} options can be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001694
1695
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001696\end{document}