blob: 5603fde219838278a74cd89152489c288526cfb2 [file] [log] [blame]
Fred Drakedca87921998-01-13 16:53:23 +00001\documentclass[twoside,openright]{report}
Fred Drake0fd82681998-01-09 05:39:38 +00002\usepackage{myformat}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00003
Guido van Rossum5049bcb1995-03-13 16:55:23 +00004% XXX PM Modulator
5
Guido van Rossum6938f061994-08-01 12:22:53 +00006\title{Extending and Embedding the Python Interpreter}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00007
Guido van Rossum16cd7f91994-10-06 10:29:26 +00008\input{boilerplate}
Guido van Rossum83eb9621993-11-23 16:28:45 +00009
Guido van Rossum7a2dba21993-11-05 14:45:11 +000010% Tell \index to actually write the .idx file
11\makeindex
12
13\begin{document}
14
Fred Drake0fd82681998-01-09 05:39:38 +000015\pagestyle{empty}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000016\pagenumbering{roman}
17
18\maketitle
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
36introduction to the language, see the Python Tutorial. The Python
37Reference Manual gives a more formal definition of the language. The
38Python Library Reference documents the existing object types,
39functions 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
43Python/\C{} API Reference Manual. \strong{Note:} While that manual is
Guido van Rossumfdacc581997-10-07 14:40:16 +000044still in a state of flux, it is safe to say that it is much more up to
45date than the manual you're reading currently (which has been in need
46for an upgrade for some time now).
47
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
53\pagenumbering{arabic}
54
Guido van Rossumdb65a6c1993-11-05 17:11:16 +000055
Fred Drake0fd82681998-01-09 05:39:38 +000056\chapter{Extending Python with \C{} or \Cpp{} code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000057
Guido van Rossum6f0132f1993-11-19 13:13:22 +000058
59\section{Introduction}
60
Guido van Rossumb92112d1995-03-20 14:24:09 +000061It is quite easy to add new built-in modules to Python, if you know
Fred Drake0fd82681998-01-09 05:39:38 +000062how to program in \C{}. Such \dfn{extension modules} can do two things
Guido van Rossumb92112d1995-03-20 14:24:09 +000063that can't be done directly in Python: they can implement new built-in
Fred Drake0fd82681998-01-09 05:39:38 +000064object types, and they can call \C{} library functions and system calls.
Guido van Rossum6938f061994-08-01 12:22:53 +000065
Guido van Rossum5049bcb1995-03-13 16:55:23 +000066To support extensions, the Python API (Application Programmers
Guido van Rossumb92112d1995-03-20 14:24:09 +000067Interface) defines a set of functions, macros and variables that
68provide access to most aspects of the Python run-time system. The
Fred Drake0fd82681998-01-09 05:39:38 +000069Python API is incorporated in a \C{} source file by including the header
Guido van Rossumb92112d1995-03-20 14:24:09 +000070\code{"Python.h"}.
Guido van Rossum6938f061994-08-01 12:22:53 +000071
Guido van Rossumb92112d1995-03-20 14:24:09 +000072The compilation of an extension module depends on its intended use as
73well as on your system setup; details are given in a later section.
Guido van Rossum6938f061994-08-01 12:22:53 +000074
Guido van Rossum7a2dba21993-11-05 14:45:11 +000075
Guido van Rossum5049bcb1995-03-13 16:55:23 +000076\section{A Simple Example}
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 Drake0fd82681998-01-09 05:39:38 +000080interface to the \C{} library function \code{system()}.\footnote{An
Guido van Rossumb92112d1995-03-20 14:24:09 +000081interface for this function already exists in the standard module
82\code{os} --- it was chosen as a simple and straightfoward example.}
83This 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
Guido van Rossume47da0a1997-07-17 16:34:52 +000087\bcode\begin{verbatim}
Guido van Rossumb92112d1995-03-20 14:24:09 +000088 >>> import spam
89 >>> status = spam.system("ls -l")
Guido van Rossume47da0a1997-07-17 16:34:52 +000090\end{verbatim}\ecode
91%
Guido van Rossumb92112d1995-03-20 14:24:09 +000092Begin by creating a file \samp{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
Guido van Rossume47da0a1997-07-17 16:34:52 +000099\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000100 #include "Python.h"
Guido van Rossume47da0a1997-07-17 16:34:52 +0000101\end{verbatim}\ecode
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
112system, it declares the functions \code{malloc()}, \code{free()} and
113\code{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
Guido van Rossume47da0a1997-07-17 16:34:52 +0000119\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000120 static PyObject *
121 spam_system(self, args)
122 PyObject *self;
123 PyObject *args;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000124 {
125 char *command;
126 int sts;
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000127 if (!PyArg_ParseTuple(args, "s", &command))
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000128 return NULL;
129 sts = system(command);
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000130 return Py_BuildValue("i", sts);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000131 }
Guido van Rossume47da0a1997-07-17 16:34:52 +0000132\end{verbatim}\ecode
133%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000134There is a straightforward translation from the argument list in
Guido van Rossumb92112d1995-03-20 14:24:09 +0000135Python (e.g.\ the single expression \code{"ls -l"}) to the arguments
Fred Drake0fd82681998-01-09 05:39:38 +0000136passed to the \C{} function. The \C{} function always has two arguments,
Guido van Rossumb92112d1995-03-20 14:24:09 +0000137conventionally named \var{self} and \var{args}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000138
Fred Drake0fd82681998-01-09 05:39:38 +0000139The \var{self} argument is only used when the \C{} function implements a
Guido van Rossumb92112d1995-03-20 14:24:09 +0000140builtin method. This will be discussed later. In the example,
Fred Drake0fd82681998-01-09 05:39:38 +0000141\var{self} will always be a \NULL{} pointer, since we are defining
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000142a function, not a method. (This is done so that the interpreter
Fred Drake0fd82681998-01-09 05:39:38 +0000143doesn't have to understand two different types of \C{} functions.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000144
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000145The \var{args} argument will be a pointer to a Python tuple object
Guido van Rossumb92112d1995-03-20 14:24:09 +0000146containing the arguments. Each item of the tuple corresponds to an
147argument in the call's argument list. The arguments are Python
Fred Drake0fd82681998-01-09 05:39:38 +0000148objects -- in order to do anything with them in our \C{} function we have
149to convert them to \C{} values. The function \code{PyArg_ParseTuple()}
150in the Python API checks the argument types and converts them to \C{}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000151values. It uses a template string to determine the required types of
Fred Drake0fd82681998-01-09 05:39:38 +0000152the arguments as well as the types of the \C{} variables into which to
Guido van Rossumb92112d1995-03-20 14:24:09 +0000153store the converted values. More about this later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000154
Guido van Rossumb92112d1995-03-20 14:24:09 +0000155\code{PyArg_ParseTuple()} returns true (nonzero) if all arguments have
156the right type and its components have been stored in the variables
157whose addresses are passed. It returns false (zero) if an invalid
158argument list was passed. In the latter case it also raises an
159appropriate exception by so the calling function can return
Fred Drake0fd82681998-01-09 05:39:38 +0000160\NULL{} immediately (as we saw in the example).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000161
162
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000163\section{Intermezzo: Errors and Exceptions}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000164
165An important convention throughout the Python interpreter is the
166following: when a function fails, it should set an exception condition
Fred Drake0fd82681998-01-09 05:39:38 +0000167and return an error value (usually a \NULL{} pointer). Exceptions
Guido van Rossumb92112d1995-03-20 14:24:09 +0000168are stored in a static global variable inside the interpreter; if this
Fred Drake0fd82681998-01-09 05:39:38 +0000169variable is \NULL{} no exception has occurred. A second global
Guido van Rossumb92112d1995-03-20 14:24:09 +0000170variable stores the ``associated value'' of the exception (the second
171argument to \code{raise}). A third variable contains the stack
172traceback in case the error originated in Python code. These three
Fred Drake0fd82681998-01-09 05:39:38 +0000173variables are the \C{} equivalents of the Python variables
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000174\code{sys.exc_type}, \code{sys.exc_value} and \code{sys.exc_traceback}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000175(see the section on module \code{sys} in the Library Reference
176Manual). It is important to know about them to understand how errors
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000177are passed around.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000178
Guido van Rossumb92112d1995-03-20 14:24:09 +0000179The Python API defines a number of functions to set various types of
180exceptions.
181
182The most common one is \code{PyErr_SetString()}. Its arguments are an
Fred Drake0fd82681998-01-09 05:39:38 +0000183exception object and a \C{} string. The exception object is usually a
184predefined object like \code{PyExc_ZeroDivisionError}. The \C{} string
Guido van Rossumb92112d1995-03-20 14:24:09 +0000185indicates the cause of the error and is converted to a Python string
186object and stored as the ``associated value'' of the exception.
187
188Another useful function is \code{PyErr_SetFromErrno()}, which only
189takes an exception argument and constructs the associated value by
190inspection of the (\UNIX{}) global variable \code{errno}. The most
191general function is \code{PyErr_SetObject()}, which takes two object
192arguments, the exception and its associated value. You don't need to
193\code{Py_INCREF()} the objects passed to any of these functions.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000194
195You can test non-destructively whether an exception has been set with
Guido van Rossumb92112d1995-03-20 14:24:09 +0000196\code{PyErr_Occurred()}. This returns the current exception object,
Fred Drake0fd82681998-01-09 05:39:38 +0000197or \NULL{} if no exception has occurred. You normally don't need
Guido van Rossumb92112d1995-03-20 14:24:09 +0000198to call \code{PyErr_Occurred()} to see whether an error occurred in a
199function call, since you should be able to tell from the return value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000200
Guido van Rossumd16ddb61996-12-13 02:38:17 +0000201When a function \var{f} that calls another function \var{g} detects
Guido van Rossumb92112d1995-03-20 14:24:09 +0000202that the latter fails, \var{f} should itself return an error value
Fred Drake0fd82681998-01-09 05:39:38 +0000203(e.g. \NULL{} or \code{-1}). It should \emph{not} call one of the
Guido van Rossumb92112d1995-03-20 14:24:09 +0000204\code{PyErr_*()} functions --- one has already been called by \var{g}.
205\var{f}'s caller is then supposed to also return an error indication
206to \emph{its} caller, again \emph{without} calling \code{PyErr_*()},
207and so on --- the most detailed cause of the error was already
208reported by the function that first detected it. Once the error
209reaches the Python interpreter's main loop, this aborts the currently
210executing Python code and tries to find an exception handler specified
211by the Python programmer.
Guido van Rossum6938f061994-08-01 12:22:53 +0000212
213(There are situations where a module can actually give a more detailed
Guido van Rossumb92112d1995-03-20 14:24:09 +0000214error message by calling another \code{PyErr_*()} function, and in
215such cases it is fine to do so. As a general rule, however, this is
216not necessary, and can cause information about the cause of the error
217to be lost: most operations can fail for a variety of reasons.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000218
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000219To ignore an exception set by a function call that failed, the exception
220condition must be cleared explicitly by calling \code{PyErr_Clear()}.
Fred Drake0fd82681998-01-09 05:39:38 +0000221The only time \C{} code should call \code{PyErr_Clear()} is if it doesn't
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000222want to pass the error on to the interpreter but wants to handle it
223completely by itself (e.g. by trying something else or pretending
224nothing happened).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000225
Guido van Rossumb92112d1995-03-20 14:24:09 +0000226Note that a failing \code{malloc()} call must be turned into an
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000227exception --- the direct caller of \code{malloc()} (or
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000228\code{realloc()}) must call \code{PyErr_NoMemory()} and return a
229failure indicator itself. All the object-creating functions
230(\code{PyInt_FromLong()} etc.) already do this, so only if you call
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000231\code{malloc()} directly this note is of importance.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000232
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000233Also note that, with the important exception of
Guido van Rossumb92112d1995-03-20 14:24:09 +0000234\code{PyArg_ParseTuple()} and friends, functions that return an
235integer status usually return a positive value or zero for success and
236\code{-1} for failure, like \UNIX{} system calls.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000237
Guido van Rossumb92112d1995-03-20 14:24:09 +0000238Finally, be careful to clean up garbage (by making \code{Py_XDECREF()}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000239or \code{Py_DECREF()} calls for objects you have already created) when
Guido van Rossumb92112d1995-03-20 14:24:09 +0000240you return an error indicator!
Guido van Rossum6938f061994-08-01 12:22:53 +0000241
242The choice of which exception to raise is entirely yours. There are
Fred Drake0fd82681998-01-09 05:39:38 +0000243predeclared \C{} objects corresponding to all built-in Python exceptions,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000244e.g. \code{PyExc_ZeroDevisionError} which you can use directly. Of
Guido van Rossumb92112d1995-03-20 14:24:09 +0000245course, you should choose exceptions wisely --- don't use
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000246\code{PyExc_TypeError} to mean that a file couldn't be opened (that
247should probably be \code{PyExc_IOError}). If something's wrong with
248the argument list, the \code{PyArg_ParseTuple()} function usually
249raises \code{PyExc_TypeError}. If you have an argument whose value
250which must be in a particular range or must satisfy other conditions,
251\code{PyExc_ValueError} is appropriate.
Guido van Rossum6938f061994-08-01 12:22:53 +0000252
253You can also define a new exception that is unique to your module.
254For this, you usually declare a static object variable at the
255beginning of your file, e.g.
256
Guido van Rossume47da0a1997-07-17 16:34:52 +0000257\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000258 static PyObject *SpamError;
Guido van Rossume47da0a1997-07-17 16:34:52 +0000259\end{verbatim}\ecode
260%
Guido van Rossum6938f061994-08-01 12:22:53 +0000261and initialize it in your module's initialization function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000262(\code{initspam()}) with a string object, e.g. (leaving out the error
Guido van Rossumb92112d1995-03-20 14:24:09 +0000263checking for now):
Guido van Rossum6938f061994-08-01 12:22:53 +0000264
Guido van Rossume47da0a1997-07-17 16:34:52 +0000265\bcode\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000266 void
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000267 initspam()
Guido van Rossum6938f061994-08-01 12:22:53 +0000268 {
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000269 PyObject *m, *d;
Guido van Rossumb92112d1995-03-20 14:24:09 +0000270 m = Py_InitModule("spam", SpamMethods);
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000271 d = PyModule_GetDict(m);
272 SpamError = PyString_FromString("spam.error");
273 PyDict_SetItemString(d, "error", SpamError);
Guido van Rossum6938f061994-08-01 12:22:53 +0000274 }
Guido van Rossume47da0a1997-07-17 16:34:52 +0000275\end{verbatim}\ecode
276%
Guido van Rossumb92112d1995-03-20 14:24:09 +0000277Note that the Python name for the exception object is
278\code{spam.error}. It is conventional for module and exception names
279to be spelled in lower case. It is also conventional that the
280\emph{value} of the exception object is the same as its name, e.g.\
281the string \code{"spam.error"}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000282
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000283
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000284\section{Back to the Example}
285
286Going back to our example function, you should now be able to
287understand this statement:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000288
Guido van Rossume47da0a1997-07-17 16:34:52 +0000289\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000290 if (!PyArg_ParseTuple(args, "s", &command))
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000291 return NULL;
Guido van Rossume47da0a1997-07-17 16:34:52 +0000292\end{verbatim}\ecode
293%
Fred Drake0fd82681998-01-09 05:39:38 +0000294It returns \NULL{} (the error indicator for functions returning
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000295object pointers) if an error is detected in the argument list, relying
296on the exception set by \code{PyArg_ParseTuple()}. Otherwise the
297string value of the argument has been copied to the local variable
298\code{command}. This is a pointer assignment and you are not supposed
Fred Drake0fd82681998-01-09 05:39:38 +0000299to modify the string to which it points (so in Standard \C{}, the variable
Guido van Rossumb92112d1995-03-20 14:24:09 +0000300\code{command} should properly be declared as \samp{const char
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000301*command}).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000302
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000303The next statement is a call to the \UNIX{} function \code{system()},
304passing it the string we just got from \code{PyArg_ParseTuple()}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000305
Guido van Rossume47da0a1997-07-17 16:34:52 +0000306\bcode\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000307 sts = system(command);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000308\end{verbatim}\ecode
309%
Guido van Rossumd16ddb61996-12-13 02:38:17 +0000310Our \code{spam.system()} function must return the value of \code{sts}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000311as a Python object. This is done using the function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000312\code{Py_BuildValue()}, which is something like the inverse of
313\code{PyArg_ParseTuple()}: it takes a format string and an arbitrary
Fred Drake0fd82681998-01-09 05:39:38 +0000314number of \C{} values, and returns a new Python object. More info on
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000315\code{Py_BuildValue()} is given later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000316
Guido van Rossume47da0a1997-07-17 16:34:52 +0000317\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000318 return Py_BuildValue("i", sts);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000319\end{verbatim}\ecode
320%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000321In this case, it will return an integer object. (Yes, even integers
322are objects on the heap in Python!)
Guido van Rossum6938f061994-08-01 12:22:53 +0000323
Fred Drake0fd82681998-01-09 05:39:38 +0000324If you have a \C{} function that returns no useful argument (a function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000325returning \code{void}), the corresponding Python function must return
326\code{None}. You need this idiom to do so:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000327
Guido van Rossume47da0a1997-07-17 16:34:52 +0000328\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000329 Py_INCREF(Py_None);
330 return Py_None;
Guido van Rossume47da0a1997-07-17 16:34:52 +0000331\end{verbatim}\ecode
332%
Fred Drake0fd82681998-01-09 05:39:38 +0000333\code{Py_None} is the \C{} name for the special Python object
334\code{None}. It is a genuine Python object (not a \NULL{}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000335pointer, which means ``error'' in most contexts, as we have seen).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000336
337
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000338\section{The Module's Method Table and Initialization Function}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000339
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000340I promised to show how \code{spam_system()} is called from Python
341programs. First, we need to list its name and address in a ``method
342table'':
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000343
Guido van Rossume47da0a1997-07-17 16:34:52 +0000344\bcode\begin{verbatim}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000345 static PyMethodDef SpamMethods[] = {
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000346 ...
Fred Drake0fd82681998-01-09 05:39:38 +0000347 {"system", spam_system, METH_VARARGS},
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000348 ...
349 {NULL, NULL} /* Sentinel */
350 };
Guido van Rossume47da0a1997-07-17 16:34:52 +0000351\end{verbatim}\ecode
352%
Fred Drake0fd82681998-01-09 05:39:38 +0000353Note the third entry (\samp{METH_VARARGS}). This is a flag telling
354the interpreter the calling convention to be used for the \C{}
355function. It should normally always be \samp{METH_VARARGS} or
356\samp{METH_VARARGS | METH_KEYWORDS}; a value of \samp{0} means that an
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000357obsolete variant of \code{PyArg_ParseTuple()} is used.
358
Fred Drakeb6e50321998-02-04 20:26:31 +0000359When using only \samp{METH_VARARGS}, the function should expect
360the Python-level parameters to be passed in as a tuple acceptable for
361parsing via \cfunction{PyArg_ParseTuple()}; more information on this
362function is provided below.
363
Fred Drake0fd82681998-01-09 05:39:38 +0000364The \code{METH_KEYWORDS} bit may be set in the third field if keyword
365arguments should be passed to the function. In this case, the \C{}
366function should accept a third \samp{PyObject *} parameter which will
367be a dictionary of keywords. Use \code{PyArg_ParseTupleAndKeywords()}
368to parse the arguemts to such a function.
369
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000370The method table must be passed to the interpreter in the module's
371initialization function (which should be the only non-\code{static}
372item defined in the module file):
373
Guido van Rossume47da0a1997-07-17 16:34:52 +0000374\bcode\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000375 void
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000376 initspam()
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000377 {
Guido van Rossumb92112d1995-03-20 14:24:09 +0000378 (void) Py_InitModule("spam", SpamMethods);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000379 }
Guido van Rossume47da0a1997-07-17 16:34:52 +0000380\end{verbatim}\ecode
381%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000382When the Python program imports module \code{spam} for the first time,
383\code{initspam()} is called. It calls \code{Py_InitModule()}, which
384creates a ``module object'' (which is inserted in the dictionary
385\code{sys.modules} under the key \code{"spam"}), and inserts built-in
386function objects into the newly created module based upon the table
387(an array of \code{PyMethodDef} structures) that was passed as its
388second argument. \code{Py_InitModule()} returns a pointer to the
Guido van Rossum6938f061994-08-01 12:22:53 +0000389module object that it creates (which is unused here). It aborts with
390a fatal error if the module could not be initialized satisfactorily,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000391so the caller doesn't need to check for errors.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000392
393
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000394\section{Compilation and Linkage}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000395
Guido van Rossumb92112d1995-03-20 14:24:09 +0000396There are two more things to do before you can use your new extension:
397compiling and linking it with the Python system. If you use dynamic
398loading, the details depend on the style of dynamic loading your
399system uses; see the chapter on Dynamic Loading for more info about
400this.
Guido van Rossum6938f061994-08-01 12:22:53 +0000401
402If you can't use dynamic loading, or if you want to make your module a
403permanent part of the Python interpreter, you will have to change the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000404configuration setup and rebuild the interpreter. Luckily, this is
405very simple: just place your file (\file{spammodule.c} for example) in
406the \file{Modules} directory, add a line to the file
407\file{Modules/Setup} describing your file:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000408
Guido van Rossume47da0a1997-07-17 16:34:52 +0000409\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000410 spam spammodule.o
Guido van Rossume47da0a1997-07-17 16:34:52 +0000411\end{verbatim}\ecode
412%
Guido van Rossum6938f061994-08-01 12:22:53 +0000413and rebuild the interpreter by running \code{make} in the toplevel
414directory. You can also run \code{make} in the \file{Modules}
415subdirectory, but then you must first rebuilt the \file{Makefile}
416there by running \code{make Makefile}. (This is necessary each time
417you change the \file{Setup} file.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000418
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000419If your module requires additional libraries to link with, these can
420be listed on the line in the \file{Setup} file as well, for instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000421
Guido van Rossume47da0a1997-07-17 16:34:52 +0000422\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000423 spam spammodule.o -lX11
Guido van Rossume47da0a1997-07-17 16:34:52 +0000424\end{verbatim}\ecode
425%
Fred Drake0fd82681998-01-09 05:39:38 +0000426\section{Calling Python Functions From \C{}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000427
Fred Drake0fd82681998-01-09 05:39:38 +0000428So far we have concentrated on making \C{} functions callable from
429Python. The reverse is also useful: calling Python functions from \C{}.
Guido van Rossum6938f061994-08-01 12:22:53 +0000430This is especially the case for libraries that support so-called
Fred Drake0fd82681998-01-09 05:39:38 +0000431``callback'' functions. If a \C{} interface makes use of callbacks, the
Guido van Rossum6938f061994-08-01 12:22:53 +0000432equivalent Python often needs to provide a callback mechanism to the
433Python programmer; the implementation will require calling the Python
Fred Drake0fd82681998-01-09 05:39:38 +0000434callback functions from a \C{} callback. Other uses are also imaginable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000435
436Fortunately, the Python interpreter is easily called recursively, and
Guido van Rossum6938f061994-08-01 12:22:53 +0000437there is a standard interface to call a Python function. (I won't
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000438dwell on how to call the Python parser with a particular string as
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000439input --- if you're interested, have a look at the implementation of
Guido van Rossum6938f061994-08-01 12:22:53 +0000440the \samp{-c} command line option in \file{Python/pythonmain.c}.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000441
442Calling a Python function is easy. First, the Python program must
443somehow pass you the Python function object. You should provide a
444function (or some other interface) to do this. When this function is
445called, save a pointer to the Python function object (be careful to
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000446\code{Py_INCREF()} it!) in a global variable --- or whereever you see fit.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000447For example, the following function might be part of a module
448definition:
449
Guido van Rossume47da0a1997-07-17 16:34:52 +0000450\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000451 static PyObject *my_callback = NULL;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000452
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000453 static PyObject *
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000454 my_set_callback(dummy, arg)
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000455 PyObject *dummy, *arg;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000456 {
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000457 Py_XDECREF(my_callback); /* Dispose of previous callback */
458 Py_XINCREF(arg); /* Add a reference to new callback */
459 my_callback = arg; /* Remember new callback */
460 /* Boilerplate to return "None" */
461 Py_INCREF(Py_None);
462 return Py_None;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000463 }
Guido van Rossume47da0a1997-07-17 16:34:52 +0000464\end{verbatim}\ecode
465%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000466The macros \code{Py_XINCREF()} and \code{Py_XDECREF()} increment/decrement
Guido van Rossum6938f061994-08-01 12:22:53 +0000467the reference count of an object and are safe in the presence of
Fred Drake0fd82681998-01-09 05:39:38 +0000468\NULL{} pointers. More info on them in the section on Reference
Guido van Rossum6938f061994-08-01 12:22:53 +0000469Counts below.
470
Fred Drake0fd82681998-01-09 05:39:38 +0000471Later, when it is time to call the function, you call the \C{} function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000472\code{PyEval_CallObject()}. This function has two arguments, both
473pointers to arbitrary Python objects: the Python function, and the
474argument list. The argument list must always be a tuple object, whose
475length is the number of arguments. To call the Python function with
476no arguments, pass an empty tuple; to call it with one argument, pass
477a singleton tuple. \code{Py_BuildValue()} returns a tuple when its
478format string consists of zero or more format codes between
479parentheses. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000480
Guido van Rossume47da0a1997-07-17 16:34:52 +0000481\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000482 int arg;
483 PyObject *arglist;
484 PyObject *result;
485 ...
486 arg = 123;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000487 ...
488 /* Time to call the callback */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000489 arglist = Py_BuildValue("(i)", arg);
490 result = PyEval_CallObject(my_callback, arglist);
491 Py_DECREF(arglist);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000492\end{verbatim}\ecode
493%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000494\code{PyEval_CallObject()} returns a Python object pointer: this is
495the return value of the Python function. \code{PyEval_CallObject()} is
Guido van Rossumb92112d1995-03-20 14:24:09 +0000496``reference-count-neutral'' with respect to its arguments. In the
Guido van Rossum6938f061994-08-01 12:22:53 +0000497example a new tuple was created to serve as the argument list, which
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000498is \code{Py_DECREF()}-ed immediately after the call.
Guido van Rossum6938f061994-08-01 12:22:53 +0000499
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000500The return value of \code{PyEval_CallObject()} is ``new'': either it
501is a brand new object, or it is an existing object whose reference
502count has been incremented. So, unless you want to save it in a
503global variable, you should somehow \code{Py_DECREF()} the result,
504even (especially!) if you are not interested in its value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000505
506Before you do this, however, it is important to check that the return
Fred Drake0fd82681998-01-09 05:39:38 +0000507value isn't \NULL{}. If it is, the Python function terminated by raising
508an exception. If the \C{} code that called \code{PyEval_CallObject()} is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000509called from Python, it should now return an error indication to its
510Python caller, so the interpreter can print a stack trace, or the
511calling Python code can handle the exception. If this is not possible
512or desirable, the exception should be cleared by calling
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000513\code{PyErr_Clear()}. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000514
Guido van Rossume47da0a1997-07-17 16:34:52 +0000515\bcode\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000516 if (result == NULL)
517 return NULL; /* Pass error back */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000518 ...use result...
519 Py_DECREF(result);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000520\end{verbatim}\ecode
521%
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000522Depending on the desired interface to the Python callback function,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000523you may also have to provide an argument list to \code{PyEval_CallObject()}.
Guido van Rossum6938f061994-08-01 12:22:53 +0000524In some cases the argument list is also provided by the Python
525program, through the same interface that specified the callback
526function. It can then be saved and used in the same manner as the
527function object. In other cases, you may have to construct a new
528tuple to pass as the argument list. The simplest way to do this is to
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000529call \code{Py_BuildValue()}. For example, if you want to pass an integral
Guido van Rossum6938f061994-08-01 12:22:53 +0000530event code, you might use the following code:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000531
Guido van Rossume47da0a1997-07-17 16:34:52 +0000532\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000533 PyObject *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000534 ...
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000535 arglist = Py_BuildValue("(l)", eventcode);
536 result = PyEval_CallObject(my_callback, arglist);
537 Py_DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000538 if (result == NULL)
539 return NULL; /* Pass error back */
540 /* Here maybe use the result */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000541 Py_DECREF(result);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000542\end{verbatim}\ecode
543%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000544Note the placement of \code{Py_DECREF(argument)} immediately after the call,
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000545before the error check! Also note that strictly spoken this code is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000546not complete: \code{Py_BuildValue()} may run out of memory, and this should
Guido van Rossum6938f061994-08-01 12:22:53 +0000547be checked.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000548
549
Fred Drake53396f61998-01-19 02:48:37 +0000550\section{Format Strings for \sectcode{PyArg_ParseTuple()}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000551
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000552The \code{PyArg_ParseTuple()} function is declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000553
Guido van Rossume47da0a1997-07-17 16:34:52 +0000554\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000555 int PyArg_ParseTuple(PyObject *arg, char *format, ...);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000556\end{verbatim}\ecode
557%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000558The \var{arg} argument must be a tuple object containing an argument
Fred Drake0fd82681998-01-09 05:39:38 +0000559list passed from Python to a \C{} function. The \var{format} argument
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000560must be a format string, whose syntax is explained below. The
561remaining arguments must be addresses of variables whose type is
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000562determined by the format string. For the conversion to succeed, the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000563\var{arg} object must match the format and the format must be
564exhausted.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000565
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000566Note that while \code{PyArg_ParseTuple()} checks that the Python
567arguments have the required types, it cannot check the validity of the
Fred Drake0fd82681998-01-09 05:39:38 +0000568addresses of \C{} variables passed to the call: if you make mistakes
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000569there, your code will probably crash or at least overwrite random bits
570in memory. So be careful!
571
572A format string consists of zero or more ``format units''. A format
573unit describes one Python object; it is usually a single character or
574a parenthesized sequence of format units. With a few exceptions, a
575format unit that is not a parenthesized sequence normally corresponds
576to a single address argument to \code{PyArg_ParseTuple()}. In the
577following description, the quoted form is the format unit; the entry
578in (round) parentheses is the Python object type that matches the
Fred Drake0fd82681998-01-09 05:39:38 +0000579format unit; and the entry in [square] brackets is the type of the \C{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000580variable(s) whose address should be passed. (Use the \samp{\&}
581operator to pass a variable's address.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000582
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000583\begin{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000584
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000585\item[\samp{s} (string) [char *]]
Fred Drake0fd82681998-01-09 05:39:38 +0000586Convert a Python string to a \C{} pointer to a character string. You
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000587must not provide storage for the string itself; a pointer to an
588existing string is stored into the character pointer variable whose
Fred Drake0fd82681998-01-09 05:39:38 +0000589address you pass. The \C{} string is null-terminated. The Python string
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000590must not contain embedded null bytes; if it does, a \code{TypeError}
591exception is raised.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000592
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000593\item[\samp{s\#} (string) {[char *, int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000594This variant on \code{'s'} stores into two \C{} variables, the first one
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000595a pointer to a character string, the second one its length. In this
596case the Python string may contain embedded null bytes.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000597
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000598\item[\samp{z} (string or \code{None}) {[char *]}]
599Like \samp{s}, but the Python object may also be \code{None}, in which
Fred Drake0fd82681998-01-09 05:39:38 +0000600case the \C{} pointer is set to \NULL{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000601
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000602\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
603This is to \code{'s\#'} as \code{'z'} is to \code{'s'}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000604
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000605\item[\samp{b} (integer) {[char]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000606Convert a Python integer to a tiny int, stored in a \C{} \code{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000607
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000608\item[\samp{h} (integer) {[short int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000609Convert a Python integer to a \C{} \code{short int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000610
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000611\item[\samp{i} (integer) {[int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000612Convert a Python integer to a plain \C{} \code{int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000613
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000614\item[\samp{l} (integer) {[long int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000615Convert a Python integer to a \C{} \code{long int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000616
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000617\item[\samp{c} (string of length 1) {[char]}]
618Convert a Python character, represented as a string of length 1, to a
Fred Drake0fd82681998-01-09 05:39:38 +0000619\C{} \code{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000620
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000621\item[\samp{f} (float) {[float]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000622Convert a Python floating point number to a \C{} \code{float}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000623
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000624\item[\samp{d} (float) {[double]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000625Convert a Python floating point number to a \C{} \code{double}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000626
Fred Drakeb6e50321998-02-04 20:26:31 +0000627\item[\samp{D} (complex) {[Py_complex]}]
628Convert a Python complex number to a \C{} \code{Py_complex} structure.
629
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000630\item[\samp{O} (object) {[PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000631Store a Python object (without any conversion) in a \C{} object pointer.
632The \C{} program thus receives the actual object that was passed. The
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000633object's reference count is not increased. The pointer stored is not
Fred Drake0fd82681998-01-09 05:39:38 +0000634\NULL{}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000635
636\item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000637Store a Python object in a \C{} object pointer. This is similar to
638\samp{O}, but takes two \C{} arguments: the first is the address of a
639Python type object, the second is the address of the \C{} variable (of
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000640type \code{PyObject *}) into which the object pointer is stored.
641If the Python object does not have the required type, a
642\code{TypeError} exception is raised.
643
644\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000645Convert a Python object to a \C{} variable through a \var{converter}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000646function. This takes two arguments: the first is a function, the
Fred Drake0fd82681998-01-09 05:39:38 +0000647second is the address of a \C{} variable (of arbitrary type), converted
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000648to \code{void *}. The \var{converter} function in turn is called as
649follows:
650
651\code{\var{status} = \var{converter}(\var{object}, \var{address});}
652
653where \var{object} is the Python object to be converted and
654\var{address} is the \code{void *} argument that was passed to
655\code{PyArg_ConvertTuple()}. The returned \var{status} should be
656\code{1} for a successful conversion and \code{0} if the conversion
657has failed. When the conversion fails, the \var{converter} function
658should raise an exception.
659
660\item[\samp{S} (string) {[PyStringObject *]}]
661Like \samp{O} but raises a \code{TypeError} exception that the object
Fred Drake0fd82681998-01-09 05:39:38 +0000662is a string object. The \C{} variable may also be declared as
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000663\code{PyObject *}.
664
665\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
666The object must be a Python tuple whose length is the number of format
Fred Drake0fd82681998-01-09 05:39:38 +0000667units in \var{items}. The \C{} arguments must correspond to the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000668individual format units in \var{items}. Format units for tuples may
669be nested.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000670
671\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000672
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000673It is possible to pass Python long integers where integers are
674requested; however no proper range checking is done -- the most
675significant bits are silently truncated when the receiving field is
676too small to receive the value (actually, the semantics are inherited
Fred Drake0fd82681998-01-09 05:39:38 +0000677from downcasts in \C{} --- your milage may vary).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000678
679A few other characters have a meaning in a format string. These may
680not occur inside nested parentheses. They are:
681
682\begin{description}
683
684\item[\samp{|}]
685Indicates that the remaining arguments in the Python argument list are
Fred Drake0fd82681998-01-09 05:39:38 +0000686optional. The \C{} variables corresponding to optional arguments should
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000687be initialized to their default value --- when an optional argument is
688not specified, the \code{PyArg_ParseTuple} does not touch the contents
Fred Drake0fd82681998-01-09 05:39:38 +0000689of the corresponding \C{} variable(s).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000690
691\item[\samp{:}]
692The list of format units ends here; the string after the colon is used
693as the function name in error messages (the ``associated value'' of
694the exceptions that \code{PyArg_ParseTuple} raises).
695
696\item[\samp{;}]
697The list of format units ends here; the string after the colon is used
698as the error message \emph{instead} of the default error message.
699Clearly, \samp{:} and \samp{;} mutually exclude each other.
700
701\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000702
703Some example calls:
704
Fred Drake0fd82681998-01-09 05:39:38 +0000705\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000706 int ok;
707 int i, j;
708 long k, l;
709 char *s;
710 int size;
711
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000712 ok = PyArg_ParseTuple(args, ""); /* No arguments */
Guido van Rossum6938f061994-08-01 12:22:53 +0000713 /* Python call: f() */
Fred Drake0fd82681998-01-09 05:39:38 +0000714
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000715 ok = PyArg_ParseTuple(args, "s", &s); /* A string */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000716 /* Possible Python call: f('whoops!') */
717
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000718 ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
Guido van Rossum6938f061994-08-01 12:22:53 +0000719 /* Possible Python call: f(1, 2, 'three') */
Fred Drake0fd82681998-01-09 05:39:38 +0000720
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000721 ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000722 /* A pair of ints and a string, whose size is also returned */
Guido van Rossum7e924dd1997-02-10 16:51:52 +0000723 /* Possible Python call: f((1, 2), 'three') */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000724
725 {
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000726 char *file;
727 char *mode = "r";
728 int bufsize = 0;
729 ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
730 /* A string, and optionally another string and an integer */
731 /* Possible Python calls:
732 f('spam')
733 f('spam', 'w')
734 f('spam', 'wb', 100000) */
735 }
736
737 {
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000738 int left, top, right, bottom, h, v;
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000739 ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000740 &left, &top, &right, &bottom, &h, &v);
741 /* A rectangle and a point */
742 /* Possible Python call:
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000743 f(((0, 0), (400, 300)), (10, 10)) */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000744 }
Fred Drakeb6e50321998-02-04 20:26:31 +0000745
746 {
747 Py_complex c;
748 ok = PyArg_ParseTuple(args, "D:myfunction", &c);
749 /* a complex, also providing a function name for errors */
750 /* Possible Python call: myfunction(1+2j) */
751 }
Fred Drake0fd82681998-01-09 05:39:38 +0000752\end{verbatim}
Fred Drakeb6e50321998-02-04 20:26:31 +0000753
754
755\section{Keyword Parsing with \sectcode{PyArg_ParseTupleAndKeywords()}}
756
757The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as
758follows:
759
760\bcode\begin{verbatim}
761 int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
762 char *format, char **kwlist, ...);
763\end{verbatim}\ecode
764
765The \var{arg} and \var{format} parameters are identical to those of the
766\cfunction{PyArg_ParseTuple()} function. The \var{kwdict} parameter
767is the dictionary of keywords received as the third parameter from the
768Python runtime. The \var{kwlist} parameter is a \NULL{}-terminated
769list of strings which identify the parameters; the names are matched
770with the type information from \var{format} from left to right.
771
772\strong{Note:} Nested tuples cannot be parsed when using keyword
773arguments! Keyword parameters passed in which are not present in the
774\var{kwlist} will cause a \exception{TypeError} to be raised.
775
776Here is an example module which uses keywords, based on an example by
777Geoff Philbrick (\email{philbrick@hks.com}):
778
779\begin{verbatim}
780#include <stdio.h>
781#include "Python.h"
782
783static PyObject *
784keywdarg_parrot(self, args, keywds)
785 PyObject *self;
786 PyObject *args;
787 PyObject *keywds;
788{
789 int voltage;
790 char *state = "a stiff";
791 char *action = "voom";
792 char *type = "Norwegian Blue";
793
794 static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
795
796 if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
797 &voltage, &state, &action, &type))
798 return NULL;
799
800 printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
801 action, voltage);
802 printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
803
804 Py_INCREF(Py_None);
805
806 return Py_None;
807}
808
809static PyMethodDef keywdarg_methods[] = {
810 {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS|METH_KEYWORDS},
811 {NULL, NULL} /* sentinel */
812};
813
814void
815initkeywdarg()
816{
817 /* Create the module and add the functions */
818 Py_InitModule("keywdarg", keywdarg_methods);
819
820}
821\end{verbatim}
822
823
Fred Drake53396f61998-01-19 02:48:37 +0000824\section{The \sectcode{Py_BuildValue()} Function}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000825
826This function is the counterpart to \code{PyArg_ParseTuple()}. It is
827declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000828
Guido van Rossume47da0a1997-07-17 16:34:52 +0000829\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000830 PyObject *Py_BuildValue(char *format, ...);
Guido van Rossume47da0a1997-07-17 16:34:52 +0000831\end{verbatim}\ecode
832%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000833It recognizes a set of format units similar to the ones recognized by
834\code{PyArg_ParseTuple()}, but the arguments (which are input to the
835function, not output) must not be pointers, just values. It returns a
Fred Drake0fd82681998-01-09 05:39:38 +0000836new Python object, suitable for returning from a \C{} function called
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000837from Python.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000838
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000839One difference with \code{PyArg_ParseTuple()}: while the latter
840requires its first argument to be a tuple (since Python argument lists
841are always represented as tuples internally), \code{BuildValue()} does
842not always build a tuple. It builds a tuple only if its format string
843contains two or more format units. If the format string is empty, it
844returns \code{None}; if it contains exactly one format unit, it
845returns whatever object is described by that format unit. To force it
846to return a tuple of size 0 or one, parenthesize the format string.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000847
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000848In the following description, the quoted form is the format unit; the
849entry in (round) parentheses is the Python object type that the format
850unit will return; and the entry in [square] brackets is the type of
Fred Drake0fd82681998-01-09 05:39:38 +0000851the \C{} value(s) to be passed.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000852
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000853The characters space, tab, colon and comma are ignored in format
854strings (but not within format units such as \samp{s\#}). This can be
855used to make long format strings a tad more readable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000856
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000857\begin{description}
858
859\item[\samp{s} (string) {[char *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000860Convert a null-terminated \C{} string to a Python object. If the \C{}
861string pointer is \NULL{}, \code{None} is returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000862
863\item[\samp{s\#} (string) {[char *, int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000864Convert a \C{} string and its length to a Python object. If the \C{} string
865pointer is \NULL{}, the length is ignored and \code{None} is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000866returned.
867
868\item[\samp{z} (string or \code{None}) {[char *]}]
869Same as \samp{s}.
870
871\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
872Same as \samp{s\#}.
873
874\item[\samp{i} (integer) {[int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000875Convert a plain \C{} \code{int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000876
877\item[\samp{b} (integer) {[char]}]
878Same as \samp{i}.
879
880\item[\samp{h} (integer) {[short int]}]
881Same as \samp{i}.
882
883\item[\samp{l} (integer) {[long int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000884Convert a \C{} \code{long int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000885
886\item[\samp{c} (string of length 1) {[char]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000887Convert a \C{} \code{int} representing a character to a Python string of
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000888length 1.
889
890\item[\samp{d} (float) {[double]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000891Convert a \C{} \code{double} to a Python floating point number.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000892
893\item[\samp{f} (float) {[float]}]
894Same as \samp{d}.
895
896\item[\samp{O} (object) {[PyObject *]}]
897Pass a Python object untouched (except for its reference count, which
Fred Drake0fd82681998-01-09 05:39:38 +0000898is incremented by one). If the object passed in is a \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000899pointer, it is assumed that this was caused because the call producing
900the argument found an error and set an exception. Therefore,
Fred Drake0fd82681998-01-09 05:39:38 +0000901\code{Py_BuildValue()} will return \NULL{} but won't raise an
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000902exception. If no exception has been raised yet,
903\code{PyExc_SystemError} is set.
904
905\item[\samp{S} (object) {[PyObject *]}]
906Same as \samp{O}.
907
908\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
909Convert \var{anything} to a Python object through a \var{converter}
910function. The function is called with \var{anything} (which should be
911compatible with \code{void *}) as its argument and should return a
Fred Drake0fd82681998-01-09 05:39:38 +0000912``new'' Python object, or \NULL{} if an error occurred.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000913
914\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000915Convert a sequence of \C{} values to a Python tuple with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000916of items.
917
918\item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000919Convert a sequence of \C{} values to a Python list with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000920of items.
921
922\item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000923Convert a sequence of \C{} values to a Python dictionary. Each pair of
924consecutive \C{} values adds one item to the dictionary, serving as key
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000925and value, respectively.
926
927\end{description}
928
929If there is an error in the format string, the
Fred Drake0fd82681998-01-09 05:39:38 +0000930\code{PyExc_SystemError} exception is raised and \NULL{} returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000931
932Examples (to the left the call, to the right the resulting Python value):
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000933
Guido van Rossume47da0a1997-07-17 16:34:52 +0000934\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000935 Py_BuildValue("") None
936 Py_BuildValue("i", 123) 123
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000937 Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000938 Py_BuildValue("s", "hello") 'hello'
939 Py_BuildValue("ss", "hello", "world") ('hello', 'world')
940 Py_BuildValue("s#", "hello", 4) 'hell'
941 Py_BuildValue("()") ()
942 Py_BuildValue("(i)", 123) (123,)
943 Py_BuildValue("(ii)", 123, 456) (123, 456)
944 Py_BuildValue("(i,i)", 123, 456) (123, 456)
945 Py_BuildValue("[i,i]", 123, 456) [123, 456]
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000946 Py_BuildValue("{s:i,s:i}",
947 "abc", 123, "def", 456) {'abc': 123, 'def': 456}
948 Py_BuildValue("((ii)(ii)) (ii)",
949 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))
Guido van Rossume47da0a1997-07-17 16:34:52 +0000950\end{verbatim}\ecode
951%
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000952\section{Reference Counts}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000953
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000954\subsection{Introduction}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000955
Fred Drake0fd82681998-01-09 05:39:38 +0000956In languages like \C{} or \Cpp{}, the programmer is responsible for
957dynamic allocation and deallocation of memory on the heap. In \C{}, this
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000958is done using the functions \code{malloc()} and \code{free()}. In
959\Cpp{}, the operators \code{new} and \code{delete} are used with
960essentially the same meaning; they are actually implemented using
961\code{malloc()} and \code{free()}, so we'll restrict the following
962discussion to the latter.
963
964Every block of memory allocated with \code{malloc()} should eventually
965be returned to the pool of available memory by exactly one call to
966\code{free()}. It is important to call \code{free()} at the right
967time. If a block's address is forgotten but \code{free()} is not
968called for it, the memory it occupies cannot be reused until the
969program terminates. This is called a \dfn{memory leak}. On the other
970hand, if a program calls \code{free()} for a block and then continues
971to use the block, it creates a conflict with re-use of the block
972through another \code{malloc()} call. This is called \dfn{using freed
Guido van Rossumdebf2e81997-07-17 15:58:43 +0000973memory}. It has the same bad consequences as referencing uninitialized
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000974data --- core dumps, wrong results, mysterious crashes.
975
976Common causes of memory leaks are unusual paths through the code. For
977instance, a function may allocate a block of memory, do some
978calculation, and then free the block again. Now a change in the
979requirements for the function may add a test to the calculation that
980detects an error condition and can return prematurely from the
981function. It's easy to forget to free the allocated memory block when
982taking this premature exit, especially when it is added later to the
983code. Such leaks, once introduced, often go undetected for a long
984time: the error exit is taken only in a small fraction of all calls,
985and most modern machines have plenty of virtual memory, so the leak
986only becomes apparent in a long-running process that uses the leaking
987function frequently. Therefore, it's important to prevent leaks from
988happening by having a coding convention or strategy that minimizes
989this kind of errors.
990
991Since Python makes heavy use of \code{malloc()} and \code{free()}, it
992needs a strategy to avoid memory leaks as well as the use of freed
993memory. The chosen method is called \dfn{reference counting}. The
994principle is simple: every object contains a counter, which is
995incremented when a reference to the object is stored somewhere, and
996which is decremented when a reference to it is deleted. When the
997counter reaches zero, the last reference to the object has been
998deleted and the object is freed.
999
1000An alternative strategy is called \dfn{automatic garbage collection}.
1001(Sometimes, reference counting is also referred to as a garbage
1002collection strategy, hence my use of ``automatic'' to distinguish the
1003two.) The big advantage of automatic garbage collection is that the
1004user doesn't need to call \code{free()} explicitly. (Another claimed
1005advantage is an improvement in speed or memory usage --- this is no
Fred Drake0fd82681998-01-09 05:39:38 +00001006hard fact however.) The disadvantage is that for \C{}, there is no
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001007truly portable automatic garbage collector, while reference counting
1008can be implemented portably (as long as the functions \code{malloc()}
Fred Drake0fd82681998-01-09 05:39:38 +00001009and \code{free()} are available --- which the \C{} Standard guarantees).
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001010Maybe some day a sufficiently portable automatic garbage collector
Fred Drake0fd82681998-01-09 05:39:38 +00001011will be available for \C{}. Until then, we'll have to live with
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001012reference counts.
1013
1014\subsection{Reference Counting in Python}
1015
1016There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)},
1017which handle the incrementing and decrementing of the reference count.
1018\code{Py_DECREF()} also frees the object when the count reaches zero.
1019For flexibility, it doesn't call \code{free()} directly --- rather, it
1020makes a call through a function pointer in the object's \dfn{type
1021object}. For this purpose (and others), every object also contains a
1022pointer to its type object.
1023
1024The big question now remains: when to use \code{Py_INCREF(x)} and
1025\code{Py_DECREF(x)}? Let's first introduce some terms. Nobody
1026``owns'' an object; however, you can \dfn{own a reference} to an
1027object. An object's reference count is now defined as the number of
1028owned references to it. The owner of a reference is responsible for
1029calling \code{Py_DECREF()} when the reference is no longer needed.
1030Ownership of a reference can be transferred. There are three ways to
1031dispose of an owned reference: pass it on, store it, or call
1032\code{Py_DECREF()}. Forgetting to dispose of an owned reference creates
1033a memory leak.
1034
1035It is also possible to \dfn{borrow}\footnote{The metaphor of
1036``borrowing'' a reference is not completely correct: the owner still
1037has a copy of the reference.} a reference to an object. The borrower
1038of a reference should not call \code{Py_DECREF()}. The borrower must
1039not hold on to the object longer than the owner from which it was
1040borrowed. Using a borrowed reference after the owner has disposed of
1041it risks using freed memory and should be avoided
1042completely.\footnote{Checking that the reference count is at least 1
1043\strong{does not work} --- the reference count itself could be in
1044freed memory and may thus be reused for another object!}
1045
1046The advantage of borrowing over owning a reference is that you don't
1047need to take care of disposing of the reference on all possible paths
1048through the code --- in other words, with a borrowed reference you
1049don't run the risk of leaking when a premature exit is taken. The
1050disadvantage of borrowing over leaking is that there are some subtle
1051situations where in seemingly correct code a borrowed reference can be
1052used after the owner from which it was borrowed has in fact disposed
1053of it.
1054
1055A borrowed reference can be changed into an owned reference by calling
1056\code{Py_INCREF()}. This does not affect the status of the owner from
1057which the reference was borrowed --- it creates a new owned reference,
1058and gives full owner responsibilities (i.e., the new owner must
1059dispose of the reference properly, as well as the previous owner).
1060
1061\subsection{Ownership Rules}
1062
1063Whenever an object reference is passed into or out of a function, it
1064is part of the function's interface specification whether ownership is
1065transferred with the reference or not.
1066
1067Most functions that return a reference to an object pass on ownership
1068with the reference. In particular, all functions whose function it is
1069to create a new object, e.g.\ \code{PyInt_FromLong()} and
1070\code{Py_BuildValue()}, pass ownership to the receiver. Even if in
1071fact, in some cases, you don't receive a reference to a brand new
1072object, you still receive ownership of the reference. For instance,
1073\code{PyInt_FromLong()} maintains a cache of popular values and can
1074return a reference to a cached item.
1075
1076Many functions that extract objects from other objects also transfer
1077ownership with the reference, for instance
1078\code{PyObject_GetAttrString()}. The picture is less clear, here,
1079however, since a few common routines are exceptions:
1080\code{PyTuple_GetItem()}, \code{PyList_GetItem()} and
1081\code{PyDict_GetItem()} (and \code{PyDict_GetItemString()}) all return
1082references that you borrow from the tuple, list or dictionary.
1083
1084The function \code{PyImport_AddModule()} also returns a borrowed
1085reference, even though it may actually create the object it returns:
1086this is possible because an owned reference to the object is stored in
1087\code{sys.modules}.
1088
1089When you pass an object reference into another function, in general,
1090the function borrows the reference from you --- if it needs to store
1091it, it will use \code{Py_INCREF()} to become an independent owner.
1092There are exactly two important exceptions to this rule:
1093\code{PyTuple_SetItem()} and \code{PyList_SetItem()}. These functions
1094take over ownership of the item passed to them --- even if they fail!
1095(Note that \code{PyDict_SetItem()} and friends don't take over
1096ownership --- they are ``normal''.)
1097
Fred Drake0fd82681998-01-09 05:39:38 +00001098When a \C{} function is called from Python, it borrows references to its
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001099arguments from the caller. The caller owns a reference to the object,
1100so the borrowed reference's lifetime is guaranteed until the function
1101returns. Only when such a borrowed reference must be stored or passed
1102on, it must be turned into an owned reference by calling
1103\code{Py_INCREF()}.
1104
Fred Drake0fd82681998-01-09 05:39:38 +00001105The object reference returned from a \C{} function that is called from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001106Python must be an owned reference --- ownership is tranferred from the
1107function to its caller.
1108
1109\subsection{Thin Ice}
1110
1111There are a few situations where seemingly harmless use of a borrowed
1112reference can lead to problems. These all have to do with implicit
1113invocations of the interpreter, which can cause the owner of a
1114reference to dispose of it.
1115
1116The first and most important case to know about is using
1117\code{Py_DECREF()} on an unrelated object while borrowing a reference
1118to a list item. For instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001119
Guido van Rossume47da0a1997-07-17 16:34:52 +00001120\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001121bug(PyObject *list) {
1122 PyObject *item = PyList_GetItem(list, 0);
1123 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1124 PyObject_Print(item, stdout, 0); /* BUG! */
1125}
Guido van Rossume47da0a1997-07-17 16:34:52 +00001126\end{verbatim}\ecode
1127%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001128This function first borrows a reference to \code{list[0]}, then
1129replaces \code{list[1]} with the value \code{0}, and finally prints
1130the borrowed reference. Looks harmless, right? But it's not!
1131
1132Let's follow the control flow into \code{PyList_SetItem()}. The list
1133owns references to all its items, so when item 1 is replaced, it has
1134to dispose of the original item 1. Now let's suppose the original
1135item 1 was an instance of a user-defined class, and let's further
1136suppose that the class defined a \code{__del__()} method. If this
1137class instance has a reference count of 1, disposing of it will call
1138its \code{__del__()} method.
1139
1140Since it is written in Python, the \code{__del__()} method can execute
1141arbitrary Python code. Could it perhaps do something to invalidate
1142the reference to \code{item} in \code{bug()}? You bet! Assuming that
1143the list passed into \code{bug()} is accessible to the
1144\code{__del__()} method, it could execute a statement to the effect of
1145\code{del list[0]}, and assuming this was the last reference to that
1146object, it would free the memory associated with it, thereby
1147invalidating \code{item}.
1148
1149The solution, once you know the source of the problem, is easy:
1150temporarily increment the reference count. The correct version of the
1151function reads:
1152
Guido van Rossume47da0a1997-07-17 16:34:52 +00001153\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001154no_bug(PyObject *list) {
1155 PyObject *item = PyList_GetItem(list, 0);
1156 Py_INCREF(item);
1157 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1158 PyObject_Print(item, stdout, 0);
1159 Py_DECREF(item);
1160}
Guido van Rossume47da0a1997-07-17 16:34:52 +00001161\end{verbatim}\ecode
1162%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001163This is a true story. An older version of Python contained variants
Fred Drake0fd82681998-01-09 05:39:38 +00001164of this bug and someone spent a considerable amount of time in a \C{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001165debugger to figure out why his \code{__del__()} methods would fail...
1166
1167The second case of problems with a borrowed reference is a variant
1168involving threads. Normally, multiple threads in the Python
1169interpreter can't get in each other's way, because there is a global
1170lock protecting Python's entire object space. However, it is possible
1171to temporarily release this lock using the macro
1172\code{Py_BEGIN_ALLOW_THREADS}, and to re-acquire it using
1173\code{Py_END_ALLOW_THREADS}. This is common around blocking I/O
1174calls, to let other threads use the CPU while waiting for the I/O to
1175complete. Obviously, the following function has the same problem as
1176the previous one:
1177
Guido van Rossume47da0a1997-07-17 16:34:52 +00001178\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001179bug(PyObject *list) {
1180 PyObject *item = PyList_GetItem(list, 0);
1181 Py_BEGIN_ALLOW_THREADS
1182 ...some blocking I/O call...
1183 Py_END_ALLOW_THREADS
1184 PyObject_Print(item, stdout, 0); /* BUG! */
1185}
Guido van Rossume47da0a1997-07-17 16:34:52 +00001186\end{verbatim}\ecode
1187%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001188\subsection{NULL Pointers}
1189
1190In general, functions that take object references as arguments don't
Fred Drake0fd82681998-01-09 05:39:38 +00001191expect you to pass them \NULL{} pointers, and will dump core (or
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001192cause later core dumps) if you do so. Functions that return object
Fred Drake0fd82681998-01-09 05:39:38 +00001193references generally return \NULL{} only to indicate that an
1194exception occurred. The reason for not testing for \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001195arguments is that functions often pass the objects they receive on to
Fred Drake0fd82681998-01-09 05:39:38 +00001196other function --- if each function were to test for \NULL{},
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001197there would be a lot of redundant tests and the code would run slower.
1198
Fred Drake0fd82681998-01-09 05:39:38 +00001199It is better to test for \NULL{} only at the ``source'', i.e.\
1200when a pointer that may be \NULL{} is received, e.g.\ from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001201\code{malloc()} or from a function that may raise an exception.
1202
1203The macros \code{Py_INCREF()} and \code{Py_DECREF()}
Fred Drake0fd82681998-01-09 05:39:38 +00001204don't check for \NULL{} pointers --- however, their variants
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001205\code{Py_XINCREF()} and \code{Py_XDECREF()} do.
1206
1207The macros for checking for a particular object type
Fred Drake0fd82681998-01-09 05:39:38 +00001208(\code{Py\var{type}_Check()}) don't check for \NULL{} pointers ---
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001209again, there is much code that calls several of these in a row to test
1210an object against various different expected types, and this would
Fred Drake0fd82681998-01-09 05:39:38 +00001211generate redundant tests. There are no variants with \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001212checking.
1213
Fred Drake0fd82681998-01-09 05:39:38 +00001214The \C{} function calling mechanism guarantees that the argument list
1215passed to \C{} functions (\code{args} in the examples) is never
1216\NULL{} --- in fact it guarantees that it is always a tuple.%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001217\footnote{These guarantees don't hold when you use the ``old'' style
1218calling convention --- this is still found in much existing code.}
1219
Fred Drake0fd82681998-01-09 05:39:38 +00001220It is a severe error to ever let a \NULL{} pointer ``escape'' to
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001221the Python user.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001222
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001223
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001224\section{Writing Extensions in \Cpp{}}
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001225
Guido van Rossum16d6e711994-08-08 12:30:22 +00001226It is possible to write extension modules in \Cpp{}. Some restrictions
Guido van Rossumed39cd01995-10-08 00:17:19 +00001227apply. If the main program (the Python interpreter) is compiled and
Fred Drake0fd82681998-01-09 05:39:38 +00001228linked by the \C{} compiler, global or static objects with constructors
Guido van Rossumed39cd01995-10-08 00:17:19 +00001229cannot be used. This is not a problem if the main program is linked
Guido van Rossumafcd5891998-02-05 19:59:39 +00001230by the \Cpp{} compiler. Functions that will be called by the
1231Python interpreter (in particular, module initalization functions)
1232have to be declared using \code{extern "C"}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001233It is unnecessary to enclose the Python header files in
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001234\code{extern "C" \{...\}} --- they use this form already if the symbol
Fred Drake0fd82681998-01-09 05:39:38 +00001235\samp{__cplusplus} is defined (all recent \Cpp{} compilers define this
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001236symbol).
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001237
1238\chapter{Embedding Python in another application}
1239
1240Embedding Python is similar to extending it, but not quite. The
1241difference is that when you extend Python, the main program of the
Guido van Rossum16d6e711994-08-08 12:30:22 +00001242application is still the Python interpreter, while if you embed
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001243Python, the main program may have nothing to do with Python ---
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001244instead, some parts of the application occasionally call the Python
1245interpreter to run some Python code.
1246
1247So if you are embedding Python, you are providing your own main
1248program. One of the things this main program has to do is initialize
1249the Python interpreter. At the very least, you have to call the
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001250function \code{Py_Initialize()}. There are optional calls to pass command
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001251line arguments to Python. Then later you can call the interpreter
1252from any part of the application.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001253
1254There are several different ways to call the interpreter: you can pass
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001255a string containing Python statements to \code{PyRun_SimpleString()},
1256or you can pass a stdio file pointer and a file name (for
1257identification in error messages only) to \code{PyRun_SimpleFile()}. You
1258can also call the lower-level operations described in the previous
1259chapters to construct and use Python objects.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001260
1261A simple demo of embedding Python can be found in the directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001262\file{Demo/embed}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001263
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001264
Guido van Rossum16d6e711994-08-08 12:30:22 +00001265\section{Embedding Python in \Cpp{}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001266
Guido van Rossum16d6e711994-08-08 12:30:22 +00001267It is also possible to embed Python in a \Cpp{} program; precisely how this
1268is done will depend on the details of the \Cpp{} system used; in general you
1269will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
1270to compile and link your program. There is no need to recompile Python
1271itself using \Cpp{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001272
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001273
1274\chapter{Dynamic Loading}
1275
Guido van Rossum6938f061994-08-01 12:22:53 +00001276On most modern systems it is possible to configure Python to support
Fred Drake0fd82681998-01-09 05:39:38 +00001277dynamic loading of extension modules implemented in \C{}. When shared
Guido van Rossum6938f061994-08-01 12:22:53 +00001278libraries are used dynamic loading is configured automatically;
1279otherwise you have to select it as a build option (see below). Once
1280configured, dynamic loading is trivial to use: when a Python program
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001281executes \code{import spam}, the search for modules tries to find a
1282file \file{spammodule.o} (\file{spammodule.so} when using shared
Guido van Rossum6938f061994-08-01 12:22:53 +00001283libraries) in the module search path, and if one is found, it is
1284loaded into the executing binary and executed. Once loaded, the
1285module acts just like a built-in extension module.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001286
Guido van Rossumb92112d1995-03-20 14:24:09 +00001287The advantages of dynamic loading are twofold: the ``core'' Python
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001288binary gets smaller, and users can extend Python with their own
Fred Drake0fd82681998-01-09 05:39:38 +00001289modules implemented in \C{} without having to build and maintain their
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001290own copy of the Python interpreter. There are also disadvantages:
1291dynamic loading isn't available on all systems (this just means that
1292on some systems you have to use static loading), and dynamically
1293loading a module that was compiled for a different version of Python
Guido van Rossum6938f061994-08-01 12:22:53 +00001294(e.g. with a different representation of objects) may dump core.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001295
1296
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001297\section{Configuring and Building the Interpreter for Dynamic Loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001298
Guido van Rossum6938f061994-08-01 12:22:53 +00001299There are three styles of dynamic loading: one using shared libraries,
1300one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
1301loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001302
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001303\subsection{Shared Libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001304
Guido van Rossum16d6e711994-08-08 12:30:22 +00001305The following systems support dynamic loading using shared libraries:
Guido van Rossum6938f061994-08-01 12:22:53 +00001306SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
1307systems derived from SVR4, or at least those SVR4 derivatives that
1308support shared libraries (are there any that don't?).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001309
Guido van Rossum6938f061994-08-01 12:22:53 +00001310You don't need to do anything to configure dynamic loading on these
1311systems --- the \file{configure} detects the presence of the
1312\file{<dlfcn.h>} header file and automatically configures dynamic
1313loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001314
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001315\subsection{SGI IRIX 4 Dynamic Loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001316
Guido van Rossum6938f061994-08-01 12:22:53 +00001317Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
1318loading. (SGI IRIX 5 might also support it but it is inferior to
1319using shared libraries so there is no reason to; a small test didn't
1320work right away so I gave up trying to support it.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001321
Guido van Rossum6938f061994-08-01 12:22:53 +00001322Before you build Python, you first need to fetch and build the \code{dl}
1323package written by Jack Jansen. This is available by anonymous ftp
Fred Drakeca6567f1998-01-22 20:44:18 +00001324from \url{ftp://ftp.cwi.nl/pub/dynload}, file
Guido van Rossum6938f061994-08-01 12:22:53 +00001325\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
1326instructions in the package's \file{README} file to build it.
1327
1328Once you have built \code{dl}, you can configure Python to use it. To
1329this end, you run the \file{configure} script with the option
1330\code{--with-dl=\var{directory}} where \var{directory} is the absolute
1331pathname of the \code{dl} directory.
1332
1333Now build and install Python as you normally would (see the
1334\file{README} file in the toplevel Python directory.)
1335
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001336\subsection{GNU Dynamic Loading}
Guido van Rossum6938f061994-08-01 12:22:53 +00001337
1338GNU dynamic loading supports (according to its \file{README} file) the
1339following hardware and software combinations: VAX (Ultrix), Sun 3
1340(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
1341Atari ST. There is no reason to use it on a Sparc; I haven't seen a
1342Sun 3 for years so I don't know if these have shared libraries or not.
1343
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001344You need to fetch and build two packages.
1345One is GNU DLD. All development of this code has been done with DLD
Fred Drakeca6567f1998-01-22 20:44:18 +00001346version 3.2.3, which is available by anonymous ftp from
1347\url{ftp://ftp.cwi.nl/pub/dynload}, file
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001348\file{dld-3.2.3.tar.Z}. (A more recent version of DLD is available
Fred Drakeca6567f1998-01-22 20:44:18 +00001349via \url{http://www-swiss.ai.mit.edu/~jaffer/DLD.html} but this has
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001350not been tested.)
1351The other package needed is an
Guido van Rossum6938f061994-08-01 12:22:53 +00001352emulation of Jack Jansen's \code{dl} package that I wrote on top of
1353GNU DLD 3.2.3. This is available from the same host and directory,
Guido van Rossum98046b91997-08-14 19:50:18 +00001354file \file{dl-dld-1.1.tar.Z}. (The version number may change --- but I doubt
Guido van Rossum6938f061994-08-01 12:22:53 +00001355it will.) Follow the instructions in each package's \file{README}
Guido van Rossum98046b91997-08-14 19:50:18 +00001356file to configure and build them.
Guido van Rossum6938f061994-08-01 12:22:53 +00001357
1358Now configure Python. Run the \file{configure} script with the option
1359\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
1360\var{dl-directory} is the absolute pathname of the directory where you
1361have built the \file{dl-dld} package, and \var{dld-directory} is that
1362of the GNU DLD package. The Python interpreter you build hereafter
1363will support GNU dynamic loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001364
1365
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001366\section{Building a Dynamically Loadable Module}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001367
Guido van Rossum6938f061994-08-01 12:22:53 +00001368Since there are three styles of dynamic loading, there are also three
1369groups of instructions for building a dynamically loadable module.
1370Instructions common for all three styles are given first. Assuming
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001371your module is called \code{spam}, the source filename must be
1372\file{spammodule.c}, so the object name is \file{spammodule.o}. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001373module must be written as a normal Python extension module (as
1374described earlier).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001375
Guido van Rossum6938f061994-08-01 12:22:53 +00001376Note that in all cases you will have to create your own Makefile that
1377compiles your module file(s). This Makefile will have to pass two
Fred Drake0fd82681998-01-09 05:39:38 +00001378\samp{-I} arguments to the \C{} compiler which will make it find the
Guido van Rossum6938f061994-08-01 12:22:53 +00001379Python header files. If the Make variable \var{PYTHONTOP} points to
1380the toplevel Python directory, your \var{CFLAGS} Make variable should
1381contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
1382(Most header files are in the \file{Include} subdirectory, but the
Guido van Rossum305ed111996-08-19 22:59:46 +00001383\file{config.h} header lives in the toplevel directory.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001384
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001385
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001386\subsection{Shared Libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001387
Fred Drakeaf8a0151998-01-14 14:51:31 +00001388You must link the \file{.o} file to produce a shared library. This is
1389done using a special invocation of the \UNIX{} loader/linker,
1390\emph{ld}(1). Unfortunately the invocation differs slightly per
1391system.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001392
Guido van Rossum6938f061994-08-01 12:22:53 +00001393On SunOS 4, use
Guido van Rossume47da0a1997-07-17 16:34:52 +00001394\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001395 ld spammodule.o -o spammodule.so
Guido van Rossume47da0a1997-07-17 16:34:52 +00001396\end{verbatim}\ecode
1397%
Guido van Rossum6938f061994-08-01 12:22:53 +00001398On Solaris 2, use
Guido van Rossume47da0a1997-07-17 16:34:52 +00001399\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001400 ld -G spammodule.o -o spammodule.so
Guido van Rossume47da0a1997-07-17 16:34:52 +00001401\end{verbatim}\ecode
1402%
Guido van Rossum6938f061994-08-01 12:22:53 +00001403On SGI IRIX 5, use
Guido van Rossume47da0a1997-07-17 16:34:52 +00001404\bcode\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001405 ld -shared spammodule.o -o spammodule.so
Guido van Rossume47da0a1997-07-17 16:34:52 +00001406\end{verbatim}\ecode
1407%
Guido van Rossumb92112d1995-03-20 14:24:09 +00001408On other systems, consult the manual page for \code{ld}(1) to find what
Guido van Rossum6938f061994-08-01 12:22:53 +00001409flags, if any, must be used.
1410
1411If your extension module uses system libraries that haven't already
1412been linked with Python (e.g. a windowing system), these must be
Guido van Rossumb92112d1995-03-20 14:24:09 +00001413passed to the \code{ld} command as \samp{-l} options after the
Guido van Rossum6938f061994-08-01 12:22:53 +00001414\samp{.o} file.
1415
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001416The resulting file \file{spammodule.so} must be copied into a directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001417along the Python module search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001418
1419
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001420\subsection{SGI IRIX 4 Dynamic Loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001421
Fred Drakeaf8a0151998-01-14 14:51:31 +00001422\strong{IMPORTANT:} You must compile your extension module with the
Fred Drake0fd82681998-01-09 05:39:38 +00001423additional \C{} flag \samp{-G0} (or \samp{-G 0}). This instruct the
Guido van Rossum6938f061994-08-01 12:22:53 +00001424assembler to generate position-independent code.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001425
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001426You don't need to link the resulting \file{spammodule.o} file; just
Guido van Rossum6938f061994-08-01 12:22:53 +00001427copy it into a directory along the Python module search path.
1428
1429The first time your extension is loaded, it takes some extra time and
1430a few messages may be printed. This creates a file
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001431\file{spammodule.ld} which is an image that can be loaded quickly into
Guido van Rossum6938f061994-08-01 12:22:53 +00001432the Python interpreter process. When a new Python interpreter is
1433installed, the \code{dl} package detects this and rebuilds
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001434\file{spammodule.ld}. The file \file{spammodule.ld} is placed in the
1435directory where \file{spammodule.o} was found, unless this directory is
Guido van Rossum6938f061994-08-01 12:22:53 +00001436unwritable; in that case it is placed in a temporary
1437directory.\footnote{Check the manual page of the \code{dl} package for
1438details.}
1439
1440If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001441create a file \file{spammodule.libs} in the same directory as the
1442\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001443whitespace-separated options that will be passed to the linker ---
1444normally only \samp{-l} options or absolute pathnames of libraries
1445(\samp{.a} files) should be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001446
1447
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001448\subsection{GNU Dynamic Loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001449
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001450Just copy \file{spammodule.o} into a directory along the Python module
Guido van Rossum6938f061994-08-01 12:22:53 +00001451search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001452
Guido van Rossum6938f061994-08-01 12:22:53 +00001453If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001454create a file \file{spammodule.libs} in the same directory as the
1455\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001456whitespace-separated absolute pathnames of libraries (\samp{.a}
1457files). No \samp{-l} options can be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001458
1459
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001460%\input{extref}
Guido van Rossum267e80d1996-08-09 21:01:07 +00001461
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001462\input{ext.ind}
1463
1464\end{document}