blob: af745f194583a8ccd3ae8bc911e8f21cfed4f06e [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
Guido van Rossum7a2dba21993-11-05 14:45:11 +000015\maketitle
16
Guido van Rossum16cd7f91994-10-06 10:29:26 +000017\input{copyright}
18
Guido van Rossum7a2dba21993-11-05 14:45:11 +000019\begin{abstract}
20
21\noindent
Guido van Rossumb92112d1995-03-20 14:24:09 +000022Python is an interpreted, object-oriented programming language. This
Fred Drake0fd82681998-01-09 05:39:38 +000023document describes how to write modules in \C{} or \Cpp{} to extend the
Guido van Rossumb92112d1995-03-20 14:24:09 +000024Python interpreter with new modules. Those modules can define new
25functions but also new object types and their methods. The document
26also describes how to embed the Python interpreter in another
27application, for use as an extension language. Finally, it shows how
28to compile and link extension modules so that they can be loaded
29dynamically (at run time) into the interpreter, if the underlying
30operating system supports this feature.
31
32This document assumes basic knowledge about Python. For an informal
Fred Drake3da06a61998-02-26 18:49:12 +000033introduction to the language, see the Python Tutorial. The \emph{Python
34Reference Manual} gives a more formal definition of the language. The
35\emph{Python Library Reference} documents the existing object types,
Guido van Rossumb92112d1995-03-20 14:24:09 +000036functions and modules (both built-in and written in Python) that give
37the language its wide application range.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000038
Fred Drake0fd82681998-01-09 05:39:38 +000039For a detailed description of the whole Python/\C{} API, see the separate
Fred Drake3da06a61998-02-26 18:49:12 +000040\emph{Python/\C{} API Reference Manual}. \strong{Note:} While that
41manual is still in a state of flux, it is safe to say that it is much
42more up to date than the manual you're reading currently (which has
43been in need for an upgrade for some time now).
Guido van Rossumfdacc581997-10-07 14:40:16 +000044
45
Guido van Rossum7a2dba21993-11-05 14:45:11 +000046\end{abstract}
47
Fred Drake4d4f9e71998-01-13 22:25:02 +000048\tableofcontents
Guido van Rossum7a2dba21993-11-05 14:45:11 +000049
Guido van Rossumdb65a6c1993-11-05 17:11:16 +000050
Fred Drake0fd82681998-01-09 05:39:38 +000051\chapter{Extending Python with \C{} or \Cpp{} code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000052
Guido van Rossum6f0132f1993-11-19 13:13:22 +000053
Fred Drake3da06a61998-02-26 18:49:12 +000054%\section{Introduction}
55\label{intro}
Guido van Rossum6f0132f1993-11-19 13:13:22 +000056
Guido van Rossumb92112d1995-03-20 14:24:09 +000057It is quite easy to add new built-in modules to Python, if you know
Fred Drake0fd82681998-01-09 05:39:38 +000058how to program in \C{}. Such \dfn{extension modules} can do two things
Guido van Rossumb92112d1995-03-20 14:24:09 +000059that can't be done directly in Python: they can implement new built-in
Fred Drake0fd82681998-01-09 05:39:38 +000060object types, and they can call \C{} library functions and system calls.
Guido van Rossum6938f061994-08-01 12:22:53 +000061
Guido van Rossum5049bcb1995-03-13 16:55:23 +000062To support extensions, the Python API (Application Programmers
Guido van Rossumb92112d1995-03-20 14:24:09 +000063Interface) defines a set of functions, macros and variables that
64provide access to most aspects of the Python run-time system. The
Fred Drake0fd82681998-01-09 05:39:38 +000065Python API is incorporated in a \C{} source file by including the header
Guido van Rossumb92112d1995-03-20 14:24:09 +000066\code{"Python.h"}.
Guido van Rossum6938f061994-08-01 12:22:53 +000067
Guido van Rossumb92112d1995-03-20 14:24:09 +000068The compilation of an extension module depends on its intended use as
69well as on your system setup; details are given in a later section.
Guido van Rossum6938f061994-08-01 12:22:53 +000070
Guido van Rossum7a2dba21993-11-05 14:45:11 +000071
Guido van Rossum5049bcb1995-03-13 16:55:23 +000072\section{A Simple Example}
Fred Drake3da06a61998-02-26 18:49:12 +000073\label{simpleExample}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000074
Guido van Rossumb92112d1995-03-20 14:24:09 +000075Let's create an extension module called \samp{spam} (the favorite food
76of Monty Python fans...) and let's say we want to create a Python
Fred Drake0fd82681998-01-09 05:39:38 +000077interface to the \C{} library function \code{system()}.\footnote{An
Guido van Rossumb92112d1995-03-20 14:24:09 +000078interface for this function already exists in the standard module
79\code{os} --- it was chosen as a simple and straightfoward example.}
80This function takes a null-terminated character string as argument and
81returns an integer. We want this function to be callable from Python
82as follows:
83
Fred Drake1e11a5c1998-02-13 07:11:32 +000084\begin{verbatim}
85>>> import spam
86>>> status = spam.system("ls -l")
87\end{verbatim}
88
Guido van Rossumb92112d1995-03-20 14:24:09 +000089Begin by creating a file \samp{spammodule.c}. (In general, if a
Fred Drake0fd82681998-01-09 05:39:38 +000090module is called \samp{spam}, the \C{} file containing its implementation
Guido van Rossumb92112d1995-03-20 14:24:09 +000091is called \file{spammodule.c}; if the module name is very long, like
92\samp{spammify}, the module name can be just \file{spammify.c}.)
93
94The first line of our file can be:
Guido van Rossum7a2dba21993-11-05 14:45:11 +000095
Fred Drake1e11a5c1998-02-13 07:11:32 +000096\begin{verbatim}
97#include "Python.h"
98\end{verbatim}
99
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000100which pulls in the Python API (you can add a comment describing the
101purpose of the module and a copyright notice if you like).
102
Guido van Rossumb92112d1995-03-20 14:24:09 +0000103All user-visible symbols defined by \code{"Python.h"} have a prefix of
104\samp{Py} or \samp{PY}, except those defined in standard header files.
105For convenience, and since they are used extensively by the Python
106interpreter, \code{"Python.h"} includes a few standard header files:
107\code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
108\code{<stdlib.h>}. If the latter header file does not exist on your
109system, it declares the functions \code{malloc()}, \code{free()} and
110\code{realloc()} directly.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000111
Fred Drake0fd82681998-01-09 05:39:38 +0000112The next thing we add to our module file is the \C{} function that will
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000113be called when the Python expression \samp{spam.system(\var{string})}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000114is evaluated (we'll see shortly how it ends up being called):
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000115
Fred Drake1e11a5c1998-02-13 07:11:32 +0000116\begin{verbatim}
117static PyObject *
118spam_system(self, args)
119 PyObject *self;
120 PyObject *args;
121{
122 char *command;
123 int sts;
124 if (!PyArg_ParseTuple(args, "s", &command))
125 return NULL;
126 sts = system(command);
127 return Py_BuildValue("i", sts);
128}
129\end{verbatim}
130
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000131There is a straightforward translation from the argument list in
Guido van Rossumb92112d1995-03-20 14:24:09 +0000132Python (e.g.\ the single expression \code{"ls -l"}) to the arguments
Fred Drake0fd82681998-01-09 05:39:38 +0000133passed to the \C{} function. The \C{} function always has two arguments,
Guido van Rossumb92112d1995-03-20 14:24:09 +0000134conventionally named \var{self} and \var{args}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000135
Fred Drake0fd82681998-01-09 05:39:38 +0000136The \var{self} argument is only used when the \C{} function implements a
Guido van Rossumb92112d1995-03-20 14:24:09 +0000137builtin method. This will be discussed later. In the example,
Fred Drake0fd82681998-01-09 05:39:38 +0000138\var{self} will always be a \NULL{} pointer, since we are defining
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000139a function, not a method. (This is done so that the interpreter
Fred Drake0fd82681998-01-09 05:39:38 +0000140doesn't have to understand two different types of \C{} functions.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000141
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000142The \var{args} argument will be a pointer to a Python tuple object
Guido van Rossumb92112d1995-03-20 14:24:09 +0000143containing the arguments. Each item of the tuple corresponds to an
144argument in the call's argument list. The arguments are Python
Fred Drake1aedbd81998-02-16 14:47:27 +0000145objects --- in order to do anything with them in our \C{} function we have
Fred Drake3da06a61998-02-26 18:49:12 +0000146to convert them to \C{} values. The function \cfunction{PyArg_ParseTuple()}
Fred Drake0fd82681998-01-09 05:39:38 +0000147in the Python API checks the argument types and converts them to \C{}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000148values. It uses a template string to determine the required types of
Fred Drake0fd82681998-01-09 05:39:38 +0000149the arguments as well as the types of the \C{} variables into which to
Guido van Rossumb92112d1995-03-20 14:24:09 +0000150store the converted values. More about this later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000151
Fred Drake3da06a61998-02-26 18:49:12 +0000152\cfunction{PyArg_ParseTuple()} returns true (nonzero) if all arguments have
Guido van Rossumb92112d1995-03-20 14:24:09 +0000153the right type and its components have been stored in the variables
154whose addresses are passed. It returns false (zero) if an invalid
155argument list was passed. In the latter case it also raises an
156appropriate exception by so the calling function can return
Fred Drake0fd82681998-01-09 05:39:38 +0000157\NULL{} immediately (as we saw in the example).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000158
159
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000160\section{Intermezzo: Errors and Exceptions}
Fred Drake3da06a61998-02-26 18:49:12 +0000161\label{errors}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000162
163An important convention throughout the Python interpreter is the
164following: when a function fails, it should set an exception condition
Fred Drake0fd82681998-01-09 05:39:38 +0000165and return an error value (usually a \NULL{} pointer). Exceptions
Guido van Rossumb92112d1995-03-20 14:24:09 +0000166are stored in a static global variable inside the interpreter; if this
Fred Drake0fd82681998-01-09 05:39:38 +0000167variable is \NULL{} no exception has occurred. A second global
Guido van Rossumb92112d1995-03-20 14:24:09 +0000168variable stores the ``associated value'' of the exception (the second
169argument to \code{raise}). A third variable contains the stack
170traceback in case the error originated in Python code. These three
Fred Drake0fd82681998-01-09 05:39:38 +0000171variables are the \C{} equivalents of the Python variables
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000172\code{sys.exc_type}, \code{sys.exc_value} and \code{sys.exc_traceback}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000173(see the section on module \code{sys} in the Library Reference
174Manual). It is important to know about them to understand how errors
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000175are passed around.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000176
Guido van Rossumb92112d1995-03-20 14:24:09 +0000177The Python API defines a number of functions to set various types of
178exceptions.
179
180The most common one is \code{PyErr_SetString()}. Its arguments are an
Fred Drake0fd82681998-01-09 05:39:38 +0000181exception object and a \C{} string. The exception object is usually a
182predefined object like \code{PyExc_ZeroDivisionError}. The \C{} string
Guido van Rossumb92112d1995-03-20 14:24:09 +0000183indicates the cause of the error and is converted to a Python string
184object and stored as the ``associated value'' of the exception.
185
186Another useful function is \code{PyErr_SetFromErrno()}, which only
187takes an exception argument and constructs the associated value by
188inspection of the (\UNIX{}) global variable \code{errno}. The most
189general function is \code{PyErr_SetObject()}, which takes two object
190arguments, the exception and its associated value. You don't need to
191\code{Py_INCREF()} the objects passed to any of these functions.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000192
193You can test non-destructively whether an exception has been set with
Guido van Rossumb92112d1995-03-20 14:24:09 +0000194\code{PyErr_Occurred()}. This returns the current exception object,
Fred Drake0fd82681998-01-09 05:39:38 +0000195or \NULL{} if no exception has occurred. You normally don't need
Guido van Rossumb92112d1995-03-20 14:24:09 +0000196to call \code{PyErr_Occurred()} to see whether an error occurred in a
197function call, since you should be able to tell from the return value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000198
Guido van Rossumd16ddb61996-12-13 02:38:17 +0000199When a function \var{f} that calls another function \var{g} detects
Guido van Rossumb92112d1995-03-20 14:24:09 +0000200that the latter fails, \var{f} should itself return an error value
Fred Drake0fd82681998-01-09 05:39:38 +0000201(e.g. \NULL{} or \code{-1}). It should \emph{not} call one of the
Guido van Rossumb92112d1995-03-20 14:24:09 +0000202\code{PyErr_*()} functions --- one has already been called by \var{g}.
203\var{f}'s caller is then supposed to also return an error indication
204to \emph{its} caller, again \emph{without} calling \code{PyErr_*()},
205and so on --- the most detailed cause of the error was already
206reported by the function that first detected it. Once the error
207reaches the Python interpreter's main loop, this aborts the currently
208executing Python code and tries to find an exception handler specified
209by the Python programmer.
Guido van Rossum6938f061994-08-01 12:22:53 +0000210
211(There are situations where a module can actually give a more detailed
Guido van Rossumb92112d1995-03-20 14:24:09 +0000212error message by calling another \code{PyErr_*()} function, and in
213such cases it is fine to do so. As a general rule, however, this is
214not necessary, and can cause information about the cause of the error
215to be lost: most operations can fail for a variety of reasons.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000216
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000217To ignore an exception set by a function call that failed, the exception
218condition must be cleared explicitly by calling \code{PyErr_Clear()}.
Fred Drake0fd82681998-01-09 05:39:38 +0000219The only time \C{} code should call \code{PyErr_Clear()} is if it doesn't
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000220want to pass the error on to the interpreter but wants to handle it
221completely by itself (e.g. by trying something else or pretending
222nothing happened).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000223
Guido van Rossumb92112d1995-03-20 14:24:09 +0000224Note that a failing \code{malloc()} call must be turned into an
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000225exception --- the direct caller of \code{malloc()} (or
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000226\code{realloc()}) must call \code{PyErr_NoMemory()} and return a
227failure indicator itself. All the object-creating functions
228(\code{PyInt_FromLong()} etc.) already do this, so only if you call
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000229\code{malloc()} directly this note is of importance.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000230
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000231Also note that, with the important exception of
Fred Drake3da06a61998-02-26 18:49:12 +0000232\cfunction{PyArg_ParseTuple()} and friends, functions that return an
Guido van Rossumb92112d1995-03-20 14:24:09 +0000233integer status usually return a positive value or zero for success and
234\code{-1} for failure, like \UNIX{} system calls.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000235
Guido van Rossumb92112d1995-03-20 14:24:09 +0000236Finally, be careful to clean up garbage (by making \code{Py_XDECREF()}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000237or \code{Py_DECREF()} calls for objects you have already created) when
Guido van Rossumb92112d1995-03-20 14:24:09 +0000238you return an error indicator!
Guido van Rossum6938f061994-08-01 12:22:53 +0000239
240The choice of which exception to raise is entirely yours. There are
Fred Drake0fd82681998-01-09 05:39:38 +0000241predeclared \C{} objects corresponding to all built-in Python exceptions,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000242e.g. \code{PyExc_ZeroDevisionError} which you can use directly. Of
Guido van Rossumb92112d1995-03-20 14:24:09 +0000243course, you should choose exceptions wisely --- don't use
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000244\code{PyExc_TypeError} to mean that a file couldn't be opened (that
245should probably be \code{PyExc_IOError}). If something's wrong with
Fred Drake3da06a61998-02-26 18:49:12 +0000246the argument list, the \cfunction{PyArg_ParseTuple()} function usually
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000247raises \code{PyExc_TypeError}. If you have an argument whose value
248which must be in a particular range or must satisfy other conditions,
249\code{PyExc_ValueError} is appropriate.
Guido van Rossum6938f061994-08-01 12:22:53 +0000250
251You can also define a new exception that is unique to your module.
252For this, you usually declare a static object variable at the
253beginning of your file, e.g.
254
Fred Drake1e11a5c1998-02-13 07:11:32 +0000255\begin{verbatim}
256static PyObject *SpamError;
257\end{verbatim}
258
Guido van Rossum6938f061994-08-01 12:22:53 +0000259and initialize it in your module's initialization function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000260(\code{initspam()}) with a string object, e.g. (leaving out the error
Guido van Rossumb92112d1995-03-20 14:24:09 +0000261checking for now):
Guido van Rossum6938f061994-08-01 12:22:53 +0000262
Fred Drake1e11a5c1998-02-13 07:11:32 +0000263\begin{verbatim}
264void
265initspam()
266{
267 PyObject *m, *d;
268 m = Py_InitModule("spam", SpamMethods);
269 d = PyModule_GetDict(m);
270 SpamError = PyString_FromString("spam.error");
271 PyDict_SetItemString(d, "error", SpamError);
272}
273\end{verbatim}
274
Guido van Rossumb92112d1995-03-20 14:24:09 +0000275Note that the Python name for the exception object is
276\code{spam.error}. It is conventional for module and exception names
277to be spelled in lower case. It is also conventional that the
278\emph{value} of the exception object is the same as its name, e.g.\
279the string \code{"spam.error"}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000280
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000281
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000282\section{Back to the Example}
Fred Drake3da06a61998-02-26 18:49:12 +0000283\label{backToExample}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000284
285Going back to our example function, you should now be able to
286understand this statement:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000287
Fred Drake1e11a5c1998-02-13 07:11:32 +0000288\begin{verbatim}
289 if (!PyArg_ParseTuple(args, "s", &command))
290 return NULL;
291\end{verbatim}
292
Fred Drake0fd82681998-01-09 05:39:38 +0000293It returns \NULL{} (the error indicator for functions returning
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000294object pointers) if an error is detected in the argument list, relying
Fred Drake3da06a61998-02-26 18:49:12 +0000295on the exception set by \cfunction{PyArg_ParseTuple()}. Otherwise the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000296string value of the argument has been copied to the local variable
297\code{command}. This is a pointer assignment and you are not supposed
Fred Drake0fd82681998-01-09 05:39:38 +0000298to modify the string to which it points (so in Standard \C{}, the variable
Guido van Rossumb92112d1995-03-20 14:24:09 +0000299\code{command} should properly be declared as \samp{const char
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000300*command}).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000301
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000302The next statement is a call to the \UNIX{} function \code{system()},
Fred Drake3da06a61998-02-26 18:49:12 +0000303passing it the string we just got from \cfunction{PyArg_ParseTuple()}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000304
Fred Drake1e11a5c1998-02-13 07:11:32 +0000305\begin{verbatim}
306 sts = system(command);
307\end{verbatim}
308
Guido van Rossumd16ddb61996-12-13 02:38:17 +0000309Our \code{spam.system()} function must return the value of \code{sts}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000310as a Python object. This is done using the function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000311\code{Py_BuildValue()}, which is something like the inverse of
Fred Drake3da06a61998-02-26 18:49:12 +0000312\cfunction{PyArg_ParseTuple()}: it takes a format string and an arbitrary
Fred Drake0fd82681998-01-09 05:39:38 +0000313number of \C{} values, and returns a new Python object. More info on
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000314\code{Py_BuildValue()} is given later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000315
Fred Drake1e11a5c1998-02-13 07:11:32 +0000316\begin{verbatim}
317 return Py_BuildValue("i", sts);
318\end{verbatim}
319
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000320In this case, it will return an integer object. (Yes, even integers
321are objects on the heap in Python!)
Guido van Rossum6938f061994-08-01 12:22:53 +0000322
Fred Drake0fd82681998-01-09 05:39:38 +0000323If you have a \C{} function that returns no useful argument (a function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000324returning \code{void}), the corresponding Python function must return
325\code{None}. You need this idiom to do so:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000326
Fred Drake1e11a5c1998-02-13 07:11:32 +0000327\begin{verbatim}
328 Py_INCREF(Py_None);
329 return Py_None;
330\end{verbatim}
331
Fred Drake0fd82681998-01-09 05:39:38 +0000332\code{Py_None} is the \C{} name for the special Python object
333\code{None}. It is a genuine Python object (not a \NULL{}
Guido van Rossumb92112d1995-03-20 14:24:09 +0000334pointer, which means ``error'' in most contexts, as we have seen).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000335
336
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000337\section{The Module's Method Table and Initialization Function}
Fred Drake3da06a61998-02-26 18:49:12 +0000338\label{methodTable}
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
Fred Drake1e11a5c1998-02-13 07:11:32 +0000344\begin{verbatim}
345static PyMethodDef SpamMethods[] = {
346 ...
347 {"system", spam_system, METH_VARARGS},
348 ...
349 {NULL, NULL} /* Sentinel */
350};
351\end{verbatim}
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
Fred Drake3da06a61998-02-26 18:49:12 +0000357obsolete variant of \cfunction{PyArg_ParseTuple()} is used.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000358
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
Fred Drake3da06a61998-02-26 18:49:12 +0000367be a dictionary of keywords. Use \cfunction{PyArg_ParseTupleAndKeywords()}
Fred Drake0fd82681998-01-09 05:39:38 +0000368to 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
Fred Drake1e11a5c1998-02-13 07:11:32 +0000374\begin{verbatim}
375void
376initspam()
377{
378 (void) Py_InitModule("spam", SpamMethods);
379}
380\end{verbatim}
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}
Fred Drake3da06a61998-02-26 18:49:12 +0000395\label{compilation}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000396
Guido van Rossumb92112d1995-03-20 14:24:09 +0000397There are two more things to do before you can use your new extension:
398compiling and linking it with the Python system. If you use dynamic
399loading, the details depend on the style of dynamic loading your
400system uses; see the chapter on Dynamic Loading for more info about
401this.
Guido van Rossum6938f061994-08-01 12:22:53 +0000402
403If you can't use dynamic loading, or if you want to make your module a
404permanent part of the Python interpreter, you will have to change the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000405configuration setup and rebuild the interpreter. Luckily, this is
406very simple: just place your file (\file{spammodule.c} for example) in
407the \file{Modules} directory, add a line to the file
408\file{Modules/Setup} describing your file:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000409
Fred Drake1e11a5c1998-02-13 07:11:32 +0000410\begin{verbatim}
411spam spammodule.o
412\end{verbatim}
413
Guido van Rossum6938f061994-08-01 12:22:53 +0000414and rebuild the interpreter by running \code{make} in the toplevel
415directory. You can also run \code{make} in the \file{Modules}
416subdirectory, but then you must first rebuilt the \file{Makefile}
417there by running \code{make Makefile}. (This is necessary each time
418you change the \file{Setup} file.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000419
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000420If your module requires additional libraries to link with, these can
421be listed on the line in the \file{Setup} file as well, for instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000422
Fred Drake1e11a5c1998-02-13 07:11:32 +0000423\begin{verbatim}
424spam spammodule.o -lX11
425\end{verbatim}
426
Fred Drake0fd82681998-01-09 05:39:38 +0000427\section{Calling Python Functions From \C{}}
Fred Drake3da06a61998-02-26 18:49:12 +0000428\label{callingPython}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000429
Fred Drake0fd82681998-01-09 05:39:38 +0000430So far we have concentrated on making \C{} functions callable from
431Python. The reverse is also useful: calling Python functions from \C{}.
Guido van Rossum6938f061994-08-01 12:22:53 +0000432This is especially the case for libraries that support so-called
Fred Drake0fd82681998-01-09 05:39:38 +0000433``callback'' functions. If a \C{} interface makes use of callbacks, the
Guido van Rossum6938f061994-08-01 12:22:53 +0000434equivalent Python often needs to provide a callback mechanism to the
435Python programmer; the implementation will require calling the Python
Fred Drake0fd82681998-01-09 05:39:38 +0000436callback functions from a \C{} callback. Other uses are also imaginable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000437
438Fortunately, the Python interpreter is easily called recursively, and
Guido van Rossum6938f061994-08-01 12:22:53 +0000439there is a standard interface to call a Python function. (I won't
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000440dwell on how to call the Python parser with a particular string as
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000441input --- if you're interested, have a look at the implementation of
Guido van Rossum6938f061994-08-01 12:22:53 +0000442the \samp{-c} command line option in \file{Python/pythonmain.c}.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000443
444Calling a Python function is easy. First, the Python program must
445somehow pass you the Python function object. You should provide a
446function (or some other interface) to do this. When this function is
447called, save a pointer to the Python function object (be careful to
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000448\code{Py_INCREF()} it!) in a global variable --- or whereever you see fit.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000449For example, the following function might be part of a module
450definition:
451
Fred Drake1e11a5c1998-02-13 07:11:32 +0000452\begin{verbatim}
453static PyObject *my_callback = NULL;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000454
Fred Drake1e11a5c1998-02-13 07:11:32 +0000455static PyObject *
456my_set_callback(dummy, arg)
457 PyObject *dummy, *arg;
458{
459 Py_XDECREF(my_callback); /* Dispose of previous callback */
460 Py_XINCREF(arg); /* Add a reference to new callback */
461 my_callback = arg; /* Remember new callback */
462 /* Boilerplate to return "None" */
463 Py_INCREF(Py_None);
464 return Py_None;
465}
466\end{verbatim}
467
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000468The macros \code{Py_XINCREF()} and \code{Py_XDECREF()} increment/decrement
Guido van Rossum6938f061994-08-01 12:22:53 +0000469the reference count of an object and are safe in the presence of
Fred Drake0fd82681998-01-09 05:39:38 +0000470\NULL{} pointers. More info on them in the section on Reference
Guido van Rossum6938f061994-08-01 12:22:53 +0000471Counts below.
472
Fred Drake0fd82681998-01-09 05:39:38 +0000473Later, when it is time to call the function, you call the \C{} function
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000474\code{PyEval_CallObject()}. This function has two arguments, both
475pointers to arbitrary Python objects: the Python function, and the
476argument list. The argument list must always be a tuple object, whose
477length is the number of arguments. To call the Python function with
478no arguments, pass an empty tuple; to call it with one argument, pass
479a singleton tuple. \code{Py_BuildValue()} returns a tuple when its
480format string consists of zero or more format codes between
481parentheses. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000482
Fred Drake1e11a5c1998-02-13 07:11:32 +0000483\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000484 int arg;
485 PyObject *arglist;
486 PyObject *result;
487 ...
488 arg = 123;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000489 ...
490 /* Time to call the callback */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000491 arglist = Py_BuildValue("(i)", arg);
492 result = PyEval_CallObject(my_callback, arglist);
493 Py_DECREF(arglist);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000494\end{verbatim}
495
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000496\code{PyEval_CallObject()} returns a Python object pointer: this is
497the return value of the Python function. \code{PyEval_CallObject()} is
Guido van Rossumb92112d1995-03-20 14:24:09 +0000498``reference-count-neutral'' with respect to its arguments. In the
Guido van Rossum6938f061994-08-01 12:22:53 +0000499example a new tuple was created to serve as the argument list, which
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000500is \code{Py_DECREF()}-ed immediately after the call.
Guido van Rossum6938f061994-08-01 12:22:53 +0000501
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000502The return value of \code{PyEval_CallObject()} is ``new'': either it
503is a brand new object, or it is an existing object whose reference
504count has been incremented. So, unless you want to save it in a
505global variable, you should somehow \code{Py_DECREF()} the result,
506even (especially!) if you are not interested in its value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000507
508Before you do this, however, it is important to check that the return
Fred Drake0fd82681998-01-09 05:39:38 +0000509value isn't \NULL{}. If it is, the Python function terminated by raising
510an exception. If the \C{} code that called \code{PyEval_CallObject()} is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000511called from Python, it should now return an error indication to its
512Python caller, so the interpreter can print a stack trace, or the
513calling Python code can handle the exception. If this is not possible
514or desirable, the exception should be cleared by calling
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000515\code{PyErr_Clear()}. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000516
Fred Drake1e11a5c1998-02-13 07:11:32 +0000517\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000518 if (result == NULL)
519 return NULL; /* Pass error back */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000520 ...use result...
521 Py_DECREF(result);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000522\end{verbatim}
523
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000524Depending on the desired interface to the Python callback function,
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000525you may also have to provide an argument list to \code{PyEval_CallObject()}.
Guido van Rossum6938f061994-08-01 12:22:53 +0000526In some cases the argument list is also provided by the Python
527program, through the same interface that specified the callback
528function. It can then be saved and used in the same manner as the
529function object. In other cases, you may have to construct a new
530tuple to pass as the argument list. The simplest way to do this is to
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000531call \code{Py_BuildValue()}. For example, if you want to pass an integral
Guido van Rossum6938f061994-08-01 12:22:53 +0000532event code, you might use the following code:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000533
Fred Drake1e11a5c1998-02-13 07:11:32 +0000534\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000535 PyObject *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000536 ...
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000537 arglist = Py_BuildValue("(l)", eventcode);
538 result = PyEval_CallObject(my_callback, arglist);
539 Py_DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000540 if (result == NULL)
541 return NULL; /* Pass error back */
542 /* Here maybe use the result */
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000543 Py_DECREF(result);
Fred Drake1e11a5c1998-02-13 07:11:32 +0000544\end{verbatim}
545
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000546Note the placement of \code{Py_DECREF(argument)} immediately after the call,
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000547before the error check! Also note that strictly spoken this code is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000548not complete: \code{Py_BuildValue()} may run out of memory, and this should
Guido van Rossum6938f061994-08-01 12:22:53 +0000549be checked.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000550
551
Fred Drake53396f61998-01-19 02:48:37 +0000552\section{Format Strings for \sectcode{PyArg_ParseTuple()}}
Fred Drake3da06a61998-02-26 18:49:12 +0000553\label{parseTuple}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000554
Fred Drake3da06a61998-02-26 18:49:12 +0000555The \cfunction{PyArg_ParseTuple()} function is declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000556
Fred Drake1e11a5c1998-02-13 07:11:32 +0000557\begin{verbatim}
558int PyArg_ParseTuple(PyObject *arg, char *format, ...);
559\end{verbatim}
560
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000561The \var{arg} argument must be a tuple object containing an argument
Fred Drake0fd82681998-01-09 05:39:38 +0000562list passed from Python to a \C{} function. The \var{format} argument
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000563must be a format string, whose syntax is explained below. The
564remaining arguments must be addresses of variables whose type is
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000565determined by the format string. For the conversion to succeed, the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000566\var{arg} object must match the format and the format must be
567exhausted.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000568
Fred Drake3da06a61998-02-26 18:49:12 +0000569Note that while \cfunction{PyArg_ParseTuple()} checks that the Python
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000570arguments have the required types, it cannot check the validity of the
Fred Drake0fd82681998-01-09 05:39:38 +0000571addresses of \C{} variables passed to the call: if you make mistakes
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000572there, your code will probably crash or at least overwrite random bits
573in memory. So be careful!
574
575A format string consists of zero or more ``format units''. A format
576unit describes one Python object; it is usually a single character or
577a parenthesized sequence of format units. With a few exceptions, a
578format unit that is not a parenthesized sequence normally corresponds
Fred Drake3da06a61998-02-26 18:49:12 +0000579to a single address argument to \cfunction{PyArg_ParseTuple()}. In the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000580following description, the quoted form is the format unit; the entry
581in (round) parentheses is the Python object type that matches the
Fred Drake0fd82681998-01-09 05:39:38 +0000582format unit; and the entry in [square] brackets is the type of the \C{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000583variable(s) whose address should be passed. (Use the \samp{\&}
584operator to pass a variable's address.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000585
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000586\begin{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000587
Fred Drake628f5981998-02-25 15:48:16 +0000588\item[\samp{s} (string) [char *{]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000589Convert a Python string to a \C{} pointer to a character string. You
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000590must not provide storage for the string itself; a pointer to an
591existing string is stored into the character pointer variable whose
Fred Drake0fd82681998-01-09 05:39:38 +0000592address you pass. The \C{} string is null-terminated. The Python string
Fred Drake3da06a61998-02-26 18:49:12 +0000593must not contain embedded null bytes; if it does, a \exception{TypeError}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000594exception is raised.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000595
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000596\item[\samp{s\#} (string) {[char *, int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000597This variant on \code{'s'} stores into two \C{} variables, the first one
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000598a pointer to a character string, the second one its length. In this
599case the Python string may contain embedded null bytes.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000600
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000601\item[\samp{z} (string or \code{None}) {[char *]}]
602Like \samp{s}, but the Python object may also be \code{None}, in which
Fred Drake0fd82681998-01-09 05:39:38 +0000603case the \C{} pointer is set to \NULL{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000604
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000605\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
606This is to \code{'s\#'} as \code{'z'} is to \code{'s'}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000607
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000608\item[\samp{b} (integer) {[char]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000609Convert a Python integer to a tiny int, stored in a \C{} \code{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000610
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000611\item[\samp{h} (integer) {[short int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000612Convert a Python integer to a \C{} \code{short int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000613
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000614\item[\samp{i} (integer) {[int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000615Convert a Python integer to a plain \C{} \code{int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000616
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000617\item[\samp{l} (integer) {[long int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000618Convert a Python integer to a \C{} \code{long int}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000619
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000620\item[\samp{c} (string of length 1) {[char]}]
621Convert a Python character, represented as a string of length 1, to a
Fred Drake0fd82681998-01-09 05:39:38 +0000622\C{} \code{char}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000623
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000624\item[\samp{f} (float) {[float]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000625Convert a Python floating point number to a \C{} \code{float}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000626
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000627\item[\samp{d} (float) {[double]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000628Convert a Python floating point number to a \C{} \code{double}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000629
Fred Drakeb6e50321998-02-04 20:26:31 +0000630\item[\samp{D} (complex) {[Py_complex]}]
631Convert a Python complex number to a \C{} \code{Py_complex} structure.
632
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000633\item[\samp{O} (object) {[PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000634Store a Python object (without any conversion) in a \C{} object pointer.
635The \C{} program thus receives the actual object that was passed. The
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000636object's reference count is not increased. The pointer stored is not
Fred Drake0fd82681998-01-09 05:39:38 +0000637\NULL{}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000638
639\item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000640Store a Python object in a \C{} object pointer. This is similar to
641\samp{O}, but takes two \C{} arguments: the first is the address of a
642Python type object, the second is the address of the \C{} variable (of
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000643type \code{PyObject *}) into which the object pointer is stored.
644If the Python object does not have the required type, a
645\code{TypeError} exception is raised.
646
647\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000648Convert a Python object to a \C{} variable through a \var{converter}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000649function. This takes two arguments: the first is a function, the
Fred Drake0fd82681998-01-09 05:39:38 +0000650second is the address of a \C{} variable (of arbitrary type), converted
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000651to \code{void *}. The \var{converter} function in turn is called as
652follows:
653
654\code{\var{status} = \var{converter}(\var{object}, \var{address});}
655
656where \var{object} is the Python object to be converted and
657\var{address} is the \code{void *} argument that was passed to
658\code{PyArg_ConvertTuple()}. The returned \var{status} should be
659\code{1} for a successful conversion and \code{0} if the conversion
660has failed. When the conversion fails, the \var{converter} function
661should raise an exception.
662
663\item[\samp{S} (string) {[PyStringObject *]}]
Guido van Rossum2474d681998-02-26 17:07:11 +0000664Like \samp{O} but requires that the Python object is a string object.
665Raises a \code{TypeError} exception if the object is not a string
666object. The \C{} variable may also be declared as \code{PyObject *}.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000667
668\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
669The object must be a Python tuple whose length is the number of format
Fred Drake0fd82681998-01-09 05:39:38 +0000670units in \var{items}. The \C{} arguments must correspond to the
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000671individual format units in \var{items}. Format units for tuples may
672be nested.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000673
674\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000675
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000676It is possible to pass Python long integers where integers are
Fred Drake1aedbd81998-02-16 14:47:27 +0000677requested; however no proper range checking is done --- the most
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000678significant bits are silently truncated when the receiving field is
679too small to receive the value (actually, the semantics are inherited
Fred Drake0fd82681998-01-09 05:39:38 +0000680from downcasts in \C{} --- your milage may vary).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000681
682A few other characters have a meaning in a format string. These may
683not occur inside nested parentheses. They are:
684
685\begin{description}
686
687\item[\samp{|}]
688Indicates that the remaining arguments in the Python argument list are
Fred Drake0fd82681998-01-09 05:39:38 +0000689optional. The \C{} variables corresponding to optional arguments should
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000690be initialized to their default value --- when an optional argument is
691not specified, the \code{PyArg_ParseTuple} does not touch the contents
Fred Drake0fd82681998-01-09 05:39:38 +0000692of the corresponding \C{} variable(s).
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000693
694\item[\samp{:}]
695The list of format units ends here; the string after the colon is used
696as the function name in error messages (the ``associated value'' of
697the exceptions that \code{PyArg_ParseTuple} raises).
698
699\item[\samp{;}]
700The list of format units ends here; the string after the colon is used
701as the error message \emph{instead} of the default error message.
702Clearly, \samp{:} and \samp{;} mutually exclude each other.
703
704\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000705
706Some example calls:
707
Fred Drake0fd82681998-01-09 05:39:38 +0000708\begin{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000709 int ok;
710 int i, j;
711 long k, l;
712 char *s;
713 int size;
714
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000715 ok = PyArg_ParseTuple(args, ""); /* No arguments */
Guido van Rossum6938f061994-08-01 12:22:53 +0000716 /* Python call: f() */
Fred Drake0fd82681998-01-09 05:39:38 +0000717
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000718 ok = PyArg_ParseTuple(args, "s", &s); /* A string */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000719 /* Possible Python call: f('whoops!') */
720
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000721 ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
Guido van Rossum6938f061994-08-01 12:22:53 +0000722 /* Possible Python call: f(1, 2, 'three') */
Fred Drake0fd82681998-01-09 05:39:38 +0000723
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000724 ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000725 /* A pair of ints and a string, whose size is also returned */
Guido van Rossum7e924dd1997-02-10 16:51:52 +0000726 /* Possible Python call: f((1, 2), 'three') */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000727
728 {
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000729 char *file;
730 char *mode = "r";
731 int bufsize = 0;
732 ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
733 /* A string, and optionally another string and an integer */
734 /* Possible Python calls:
735 f('spam')
736 f('spam', 'w')
737 f('spam', 'wb', 100000) */
738 }
739
740 {
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000741 int left, top, right, bottom, h, v;
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000742 ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000743 &left, &top, &right, &bottom, &h, &v);
744 /* A rectangle and a point */
745 /* Possible Python call:
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000746 f(((0, 0), (400, 300)), (10, 10)) */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000747 }
Fred Drakeb6e50321998-02-04 20:26:31 +0000748
749 {
750 Py_complex c;
751 ok = PyArg_ParseTuple(args, "D:myfunction", &c);
752 /* a complex, also providing a function name for errors */
753 /* Possible Python call: myfunction(1+2j) */
754 }
Fred Drake0fd82681998-01-09 05:39:38 +0000755\end{verbatim}
Fred Drakeb6e50321998-02-04 20:26:31 +0000756
757
758\section{Keyword Parsing with \sectcode{PyArg_ParseTupleAndKeywords()}}
Fred Drake3da06a61998-02-26 18:49:12 +0000759\label{parseTupleAndKeywords}
Fred Drakeb6e50321998-02-04 20:26:31 +0000760
761The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as
762follows:
763
Fred Drake1e11a5c1998-02-13 07:11:32 +0000764\begin{verbatim}
765int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
766 char *format, char **kwlist, ...);
767\end{verbatim}
Fred Drakeb6e50321998-02-04 20:26:31 +0000768
769The \var{arg} and \var{format} parameters are identical to those of the
770\cfunction{PyArg_ParseTuple()} function. The \var{kwdict} parameter
771is the dictionary of keywords received as the third parameter from the
772Python runtime. The \var{kwlist} parameter is a \NULL{}-terminated
773list of strings which identify the parameters; the names are matched
774with the type information from \var{format} from left to right.
775
776\strong{Note:} Nested tuples cannot be parsed when using keyword
777arguments! Keyword parameters passed in which are not present in the
778\var{kwlist} will cause a \exception{TypeError} to be raised.
779
780Here is an example module which uses keywords, based on an example by
781Geoff Philbrick (\email{philbrick@hks.com}):
782
783\begin{verbatim}
784#include <stdio.h>
785#include "Python.h"
786
787static PyObject *
788keywdarg_parrot(self, args, keywds)
789 PyObject *self;
790 PyObject *args;
791 PyObject *keywds;
792{
793 int voltage;
794 char *state = "a stiff";
795 char *action = "voom";
796 char *type = "Norwegian Blue";
797
798 static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
799
800 if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
801 &voltage, &state, &action, &type))
802 return NULL;
803
804 printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
805 action, voltage);
806 printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
807
808 Py_INCREF(Py_None);
809
810 return Py_None;
811}
812
813static PyMethodDef keywdarg_methods[] = {
814 {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS|METH_KEYWORDS},
815 {NULL, NULL} /* sentinel */
816};
817
818void
819initkeywdarg()
820{
821 /* Create the module and add the functions */
822 Py_InitModule("keywdarg", keywdarg_methods);
823
824}
825\end{verbatim}
826
827
Fred Drake53396f61998-01-19 02:48:37 +0000828\section{The \sectcode{Py_BuildValue()} Function}
Fred Drake3da06a61998-02-26 18:49:12 +0000829\label{buildValue}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000830
831This function is the counterpart to \code{PyArg_ParseTuple()}. It is
832declared as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000833
Fred Drake1e11a5c1998-02-13 07:11:32 +0000834\begin{verbatim}
835PyObject *Py_BuildValue(char *format, ...);
836\end{verbatim}
837
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000838It recognizes a set of format units similar to the ones recognized by
839\code{PyArg_ParseTuple()}, but the arguments (which are input to the
840function, not output) must not be pointers, just values. It returns a
Fred Drake0fd82681998-01-09 05:39:38 +0000841new Python object, suitable for returning from a \C{} function called
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000842from Python.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000843
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000844One difference with \code{PyArg_ParseTuple()}: while the latter
845requires its first argument to be a tuple (since Python argument lists
846are always represented as tuples internally), \code{BuildValue()} does
847not always build a tuple. It builds a tuple only if its format string
848contains two or more format units. If the format string is empty, it
849returns \code{None}; if it contains exactly one format unit, it
850returns whatever object is described by that format unit. To force it
851to return a tuple of size 0 or one, parenthesize the format string.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000852
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000853In the following description, the quoted form is the format unit; the
854entry in (round) parentheses is the Python object type that the format
855unit will return; and the entry in [square] brackets is the type of
Fred Drake0fd82681998-01-09 05:39:38 +0000856the \C{} value(s) to be passed.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000857
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000858The characters space, tab, colon and comma are ignored in format
859strings (but not within format units such as \samp{s\#}). This can be
860used to make long format strings a tad more readable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000861
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000862\begin{description}
863
864\item[\samp{s} (string) {[char *]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000865Convert a null-terminated \C{} string to a Python object. If the \C{}
866string pointer is \NULL{}, \code{None} is returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000867
868\item[\samp{s\#} (string) {[char *, int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000869Convert a \C{} string and its length to a Python object. If the \C{} string
870pointer is \NULL{}, the length is ignored and \code{None} is
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000871returned.
872
873\item[\samp{z} (string or \code{None}) {[char *]}]
874Same as \samp{s}.
875
876\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
877Same as \samp{s\#}.
878
879\item[\samp{i} (integer) {[int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000880Convert a plain \C{} \code{int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000881
882\item[\samp{b} (integer) {[char]}]
883Same as \samp{i}.
884
885\item[\samp{h} (integer) {[short int]}]
886Same as \samp{i}.
887
888\item[\samp{l} (integer) {[long int]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000889Convert a \C{} \code{long int} to a Python integer object.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000890
891\item[\samp{c} (string of length 1) {[char]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000892Convert a \C{} \code{int} representing a character to a Python string of
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000893length 1.
894
895\item[\samp{d} (float) {[double]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000896Convert a \C{} \code{double} to a Python floating point number.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000897
898\item[\samp{f} (float) {[float]}]
899Same as \samp{d}.
900
901\item[\samp{O} (object) {[PyObject *]}]
902Pass a Python object untouched (except for its reference count, which
Fred Drake0fd82681998-01-09 05:39:38 +0000903is incremented by one). If the object passed in is a \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000904pointer, it is assumed that this was caused because the call producing
905the argument found an error and set an exception. Therefore,
Fred Drake0fd82681998-01-09 05:39:38 +0000906\code{Py_BuildValue()} will return \NULL{} but won't raise an
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000907exception. If no exception has been raised yet,
908\code{PyExc_SystemError} is set.
909
910\item[\samp{S} (object) {[PyObject *]}]
911Same as \samp{O}.
912
913\item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
914Convert \var{anything} to a Python object through a \var{converter}
915function. The function is called with \var{anything} (which should be
916compatible with \code{void *}) as its argument and should return a
Fred Drake0fd82681998-01-09 05:39:38 +0000917``new'' Python object, or \NULL{} if an error occurred.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000918
919\item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000920Convert a sequence of \C{} values to a Python tuple with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000921of items.
922
923\item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000924Convert a sequence of \C{} values to a Python list with the same number
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000925of items.
926
927\item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
Fred Drake0fd82681998-01-09 05:39:38 +0000928Convert a sequence of \C{} values to a Python dictionary. Each pair of
929consecutive \C{} values adds one item to the dictionary, serving as key
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000930and value, respectively.
931
932\end{description}
933
934If there is an error in the format string, the
Fred Drake0fd82681998-01-09 05:39:38 +0000935\code{PyExc_SystemError} exception is raised and \NULL{} returned.
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000936
937Examples (to the left the call, to the right the resulting Python value):
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000938
Fred Drake1e11a5c1998-02-13 07:11:32 +0000939\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000940 Py_BuildValue("") None
941 Py_BuildValue("i", 123) 123
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000942 Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000943 Py_BuildValue("s", "hello") 'hello'
944 Py_BuildValue("ss", "hello", "world") ('hello', 'world')
945 Py_BuildValue("s#", "hello", 4) 'hell'
946 Py_BuildValue("()") ()
947 Py_BuildValue("(i)", 123) (123,)
948 Py_BuildValue("(ii)", 123, 456) (123, 456)
949 Py_BuildValue("(i,i)", 123, 456) (123, 456)
950 Py_BuildValue("[i,i]", 123, 456) [123, 456]
Guido van Rossumf23e0fe1995-03-18 11:04:29 +0000951 Py_BuildValue("{s:i,s:i}",
952 "abc", 123, "def", 456) {'abc': 123, 'def': 456}
953 Py_BuildValue("((ii)(ii)) (ii)",
954 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))
Fred Drake1e11a5c1998-02-13 07:11:32 +0000955\end{verbatim}
956
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000957\section{Reference Counts}
Fred Drake3da06a61998-02-26 18:49:12 +0000958\label{refcounts}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000959
Fred Drake3da06a61998-02-26 18:49:12 +0000960%\subsection{Introduction}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000961
Fred Drake0fd82681998-01-09 05:39:38 +0000962In languages like \C{} or \Cpp{}, the programmer is responsible for
963dynamic allocation and deallocation of memory on the heap. In \C{}, this
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000964is done using the functions \code{malloc()} and \code{free()}. In
965\Cpp{}, the operators \code{new} and \code{delete} are used with
966essentially the same meaning; they are actually implemented using
967\code{malloc()} and \code{free()}, so we'll restrict the following
968discussion to the latter.
969
970Every block of memory allocated with \code{malloc()} should eventually
971be returned to the pool of available memory by exactly one call to
972\code{free()}. It is important to call \code{free()} at the right
973time. If a block's address is forgotten but \code{free()} is not
974called for it, the memory it occupies cannot be reused until the
975program terminates. This is called a \dfn{memory leak}. On the other
976hand, if a program calls \code{free()} for a block and then continues
977to use the block, it creates a conflict with re-use of the block
978through another \code{malloc()} call. This is called \dfn{using freed
Guido van Rossumdebf2e81997-07-17 15:58:43 +0000979memory}. It has the same bad consequences as referencing uninitialized
Guido van Rossum5049bcb1995-03-13 16:55:23 +0000980data --- core dumps, wrong results, mysterious crashes.
981
982Common causes of memory leaks are unusual paths through the code. For
983instance, a function may allocate a block of memory, do some
984calculation, and then free the block again. Now a change in the
985requirements for the function may add a test to the calculation that
986detects an error condition and can return prematurely from the
987function. It's easy to forget to free the allocated memory block when
988taking this premature exit, especially when it is added later to the
989code. Such leaks, once introduced, often go undetected for a long
990time: the error exit is taken only in a small fraction of all calls,
991and most modern machines have plenty of virtual memory, so the leak
992only becomes apparent in a long-running process that uses the leaking
993function frequently. Therefore, it's important to prevent leaks from
994happening by having a coding convention or strategy that minimizes
995this kind of errors.
996
997Since Python makes heavy use of \code{malloc()} and \code{free()}, it
998needs a strategy to avoid memory leaks as well as the use of freed
999memory. The chosen method is called \dfn{reference counting}. The
1000principle is simple: every object contains a counter, which is
1001incremented when a reference to the object is stored somewhere, and
1002which is decremented when a reference to it is deleted. When the
1003counter reaches zero, the last reference to the object has been
1004deleted and the object is freed.
1005
1006An alternative strategy is called \dfn{automatic garbage collection}.
1007(Sometimes, reference counting is also referred to as a garbage
1008collection strategy, hence my use of ``automatic'' to distinguish the
1009two.) The big advantage of automatic garbage collection is that the
1010user doesn't need to call \code{free()} explicitly. (Another claimed
1011advantage is an improvement in speed or memory usage --- this is no
Fred Drake0fd82681998-01-09 05:39:38 +00001012hard fact however.) The disadvantage is that for \C{}, there is no
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001013truly portable automatic garbage collector, while reference counting
1014can be implemented portably (as long as the functions \code{malloc()}
Fred Drake0fd82681998-01-09 05:39:38 +00001015and \code{free()} are available --- which the \C{} Standard guarantees).
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001016Maybe some day a sufficiently portable automatic garbage collector
Fred Drake0fd82681998-01-09 05:39:38 +00001017will be available for \C{}. Until then, we'll have to live with
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001018reference counts.
1019
1020\subsection{Reference Counting in Python}
Fred Drake3da06a61998-02-26 18:49:12 +00001021\label{refcountsInPython}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001022
1023There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)},
1024which handle the incrementing and decrementing of the reference count.
1025\code{Py_DECREF()} also frees the object when the count reaches zero.
1026For flexibility, it doesn't call \code{free()} directly --- rather, it
1027makes a call through a function pointer in the object's \dfn{type
1028object}. For this purpose (and others), every object also contains a
1029pointer to its type object.
1030
1031The big question now remains: when to use \code{Py_INCREF(x)} and
1032\code{Py_DECREF(x)}? Let's first introduce some terms. Nobody
1033``owns'' an object; however, you can \dfn{own a reference} to an
1034object. An object's reference count is now defined as the number of
1035owned references to it. The owner of a reference is responsible for
1036calling \code{Py_DECREF()} when the reference is no longer needed.
1037Ownership of a reference can be transferred. There are three ways to
1038dispose of an owned reference: pass it on, store it, or call
1039\code{Py_DECREF()}. Forgetting to dispose of an owned reference creates
1040a memory leak.
1041
1042It is also possible to \dfn{borrow}\footnote{The metaphor of
1043``borrowing'' a reference is not completely correct: the owner still
1044has a copy of the reference.} a reference to an object. The borrower
1045of a reference should not call \code{Py_DECREF()}. The borrower must
1046not hold on to the object longer than the owner from which it was
1047borrowed. Using a borrowed reference after the owner has disposed of
1048it risks using freed memory and should be avoided
1049completely.\footnote{Checking that the reference count is at least 1
1050\strong{does not work} --- the reference count itself could be in
1051freed memory and may thus be reused for another object!}
1052
1053The advantage of borrowing over owning a reference is that you don't
1054need to take care of disposing of the reference on all possible paths
1055through the code --- in other words, with a borrowed reference you
1056don't run the risk of leaking when a premature exit is taken. The
1057disadvantage of borrowing over leaking is that there are some subtle
1058situations where in seemingly correct code a borrowed reference can be
1059used after the owner from which it was borrowed has in fact disposed
1060of it.
1061
1062A borrowed reference can be changed into an owned reference by calling
1063\code{Py_INCREF()}. This does not affect the status of the owner from
1064which the reference was borrowed --- it creates a new owned reference,
1065and gives full owner responsibilities (i.e., the new owner must
1066dispose of the reference properly, as well as the previous owner).
1067
1068\subsection{Ownership Rules}
Fred Drake3da06a61998-02-26 18:49:12 +00001069\label{ownershipRules}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001070
1071Whenever an object reference is passed into or out of a function, it
1072is part of the function's interface specification whether ownership is
1073transferred with the reference or not.
1074
1075Most functions that return a reference to an object pass on ownership
1076with the reference. In particular, all functions whose function it is
1077to create a new object, e.g.\ \code{PyInt_FromLong()} and
1078\code{Py_BuildValue()}, pass ownership to the receiver. Even if in
1079fact, in some cases, you don't receive a reference to a brand new
1080object, you still receive ownership of the reference. For instance,
1081\code{PyInt_FromLong()} maintains a cache of popular values and can
1082return a reference to a cached item.
1083
1084Many functions that extract objects from other objects also transfer
1085ownership with the reference, for instance
1086\code{PyObject_GetAttrString()}. The picture is less clear, here,
1087however, since a few common routines are exceptions:
1088\code{PyTuple_GetItem()}, \code{PyList_GetItem()} and
1089\code{PyDict_GetItem()} (and \code{PyDict_GetItemString()}) all return
1090references that you borrow from the tuple, list or dictionary.
1091
1092The function \code{PyImport_AddModule()} also returns a borrowed
1093reference, even though it may actually create the object it returns:
1094this is possible because an owned reference to the object is stored in
1095\code{sys.modules}.
1096
1097When you pass an object reference into another function, in general,
1098the function borrows the reference from you --- if it needs to store
1099it, it will use \code{Py_INCREF()} to become an independent owner.
1100There are exactly two important exceptions to this rule:
1101\code{PyTuple_SetItem()} and \code{PyList_SetItem()}. These functions
1102take over ownership of the item passed to them --- even if they fail!
1103(Note that \code{PyDict_SetItem()} and friends don't take over
1104ownership --- they are ``normal''.)
1105
Fred Drake0fd82681998-01-09 05:39:38 +00001106When a \C{} function is called from Python, it borrows references to its
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001107arguments from the caller. The caller owns a reference to the object,
1108so the borrowed reference's lifetime is guaranteed until the function
1109returns. Only when such a borrowed reference must be stored or passed
1110on, it must be turned into an owned reference by calling
1111\code{Py_INCREF()}.
1112
Fred Drake0fd82681998-01-09 05:39:38 +00001113The object reference returned from a \C{} function that is called from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001114Python must be an owned reference --- ownership is tranferred from the
1115function to its caller.
1116
1117\subsection{Thin Ice}
Fred Drake3da06a61998-02-26 18:49:12 +00001118\label{thinIce}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001119
1120There are a few situations where seemingly harmless use of a borrowed
1121reference can lead to problems. These all have to do with implicit
1122invocations of the interpreter, which can cause the owner of a
1123reference to dispose of it.
1124
1125The first and most important case to know about is using
1126\code{Py_DECREF()} on an unrelated object while borrowing a reference
1127to a list item. For instance:
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001128
Fred Drake1e11a5c1998-02-13 07:11:32 +00001129\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001130bug(PyObject *list) {
1131 PyObject *item = PyList_GetItem(list, 0);
1132 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1133 PyObject_Print(item, stdout, 0); /* BUG! */
1134}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001135\end{verbatim}
1136
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001137This function first borrows a reference to \code{list[0]}, then
1138replaces \code{list[1]} with the value \code{0}, and finally prints
1139the borrowed reference. Looks harmless, right? But it's not!
1140
1141Let's follow the control flow into \code{PyList_SetItem()}. The list
1142owns references to all its items, so when item 1 is replaced, it has
1143to dispose of the original item 1. Now let's suppose the original
1144item 1 was an instance of a user-defined class, and let's further
1145suppose that the class defined a \code{__del__()} method. If this
1146class instance has a reference count of 1, disposing of it will call
1147its \code{__del__()} method.
1148
1149Since it is written in Python, the \code{__del__()} method can execute
1150arbitrary Python code. Could it perhaps do something to invalidate
1151the reference to \code{item} in \code{bug()}? You bet! Assuming that
1152the list passed into \code{bug()} is accessible to the
1153\code{__del__()} method, it could execute a statement to the effect of
1154\code{del list[0]}, and assuming this was the last reference to that
1155object, it would free the memory associated with it, thereby
1156invalidating \code{item}.
1157
1158The solution, once you know the source of the problem, is easy:
1159temporarily increment the reference count. The correct version of the
1160function reads:
1161
Fred Drake1e11a5c1998-02-13 07:11:32 +00001162\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001163no_bug(PyObject *list) {
1164 PyObject *item = PyList_GetItem(list, 0);
1165 Py_INCREF(item);
1166 PyList_SetItem(list, 1, PyInt_FromLong(0L));
1167 PyObject_Print(item, stdout, 0);
1168 Py_DECREF(item);
1169}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001170\end{verbatim}
1171
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001172This is a true story. An older version of Python contained variants
Fred Drake0fd82681998-01-09 05:39:38 +00001173of this bug and someone spent a considerable amount of time in a \C{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001174debugger to figure out why his \code{__del__()} methods would fail...
1175
1176The second case of problems with a borrowed reference is a variant
1177involving threads. Normally, multiple threads in the Python
1178interpreter can't get in each other's way, because there is a global
1179lock protecting Python's entire object space. However, it is possible
1180to temporarily release this lock using the macro
1181\code{Py_BEGIN_ALLOW_THREADS}, and to re-acquire it using
1182\code{Py_END_ALLOW_THREADS}. This is common around blocking I/O
1183calls, to let other threads use the CPU while waiting for the I/O to
1184complete. Obviously, the following function has the same problem as
1185the previous one:
1186
Fred Drake1e11a5c1998-02-13 07:11:32 +00001187\begin{verbatim}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001188bug(PyObject *list) {
1189 PyObject *item = PyList_GetItem(list, 0);
1190 Py_BEGIN_ALLOW_THREADS
1191 ...some blocking I/O call...
1192 Py_END_ALLOW_THREADS
1193 PyObject_Print(item, stdout, 0); /* BUG! */
1194}
Fred Drake1e11a5c1998-02-13 07:11:32 +00001195\end{verbatim}
1196
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001197\subsection{NULL Pointers}
Fred Drake3da06a61998-02-26 18:49:12 +00001198\label{nullPointers}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001199
1200In general, functions that take object references as arguments don't
Fred Drake0fd82681998-01-09 05:39:38 +00001201expect you to pass them \NULL{} pointers, and will dump core (or
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001202cause later core dumps) if you do so. Functions that return object
Fred Drake0fd82681998-01-09 05:39:38 +00001203references generally return \NULL{} only to indicate that an
1204exception occurred. The reason for not testing for \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001205arguments is that functions often pass the objects they receive on to
Fred Drake0fd82681998-01-09 05:39:38 +00001206other function --- if each function were to test for \NULL{},
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001207there would be a lot of redundant tests and the code would run slower.
1208
Fred Drake0fd82681998-01-09 05:39:38 +00001209It is better to test for \NULL{} only at the ``source'', i.e.\
1210when a pointer that may be \NULL{} is received, e.g.\ from
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001211\code{malloc()} or from a function that may raise an exception.
1212
1213The macros \code{Py_INCREF()} and \code{Py_DECREF()}
Fred Drake0fd82681998-01-09 05:39:38 +00001214don't check for \NULL{} pointers --- however, their variants
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001215\code{Py_XINCREF()} and \code{Py_XDECREF()} do.
1216
1217The macros for checking for a particular object type
Fred Drake0fd82681998-01-09 05:39:38 +00001218(\code{Py\var{type}_Check()}) don't check for \NULL{} pointers ---
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001219again, there is much code that calls several of these in a row to test
1220an object against various different expected types, and this would
Fred Drake0fd82681998-01-09 05:39:38 +00001221generate redundant tests. There are no variants with \NULL{}
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001222checking.
1223
Fred Drake0fd82681998-01-09 05:39:38 +00001224The \C{} function calling mechanism guarantees that the argument list
1225passed to \C{} functions (\code{args} in the examples) is never
1226\NULL{} --- in fact it guarantees that it is always a tuple.%
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001227\footnote{These guarantees don't hold when you use the ``old'' style
1228calling convention --- this is still found in much existing code.}
1229
Fred Drake0fd82681998-01-09 05:39:38 +00001230It is a severe error to ever let a \NULL{} pointer ``escape'' to
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001231the Python user.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001232
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001233
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001234\section{Writing Extensions in \Cpp{}}
Fred Drake3da06a61998-02-26 18:49:12 +00001235\label{cplusplus}
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001236
Guido van Rossum16d6e711994-08-08 12:30:22 +00001237It is possible to write extension modules in \Cpp{}. Some restrictions
Guido van Rossumed39cd01995-10-08 00:17:19 +00001238apply. If the main program (the Python interpreter) is compiled and
Fred Drake0fd82681998-01-09 05:39:38 +00001239linked by the \C{} compiler, global or static objects with constructors
Guido van Rossumed39cd01995-10-08 00:17:19 +00001240cannot be used. This is not a problem if the main program is linked
Guido van Rossumafcd5891998-02-05 19:59:39 +00001241by the \Cpp{} compiler. Functions that will be called by the
1242Python interpreter (in particular, module initalization functions)
1243have to be declared using \code{extern "C"}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001244It is unnecessary to enclose the Python header files in
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001245\code{extern "C" \{...\}} --- they use this form already if the symbol
Fred Drake0fd82681998-01-09 05:39:38 +00001246\samp{__cplusplus} is defined (all recent \Cpp{} compilers define this
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001247symbol).
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001248
1249\chapter{Embedding Python in another application}
Fred Drake3da06a61998-02-26 18:49:12 +00001250\label{embedding}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001251
1252Embedding Python is similar to extending it, but not quite. The
1253difference is that when you extend Python, the main program of the
Guido van Rossum16d6e711994-08-08 12:30:22 +00001254application is still the Python interpreter, while if you embed
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001255Python, the main program may have nothing to do with Python ---
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001256instead, some parts of the application occasionally call the Python
1257interpreter to run some Python code.
1258
1259So if you are embedding Python, you are providing your own main
1260program. One of the things this main program has to do is initialize
1261the Python interpreter. At the very least, you have to call the
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001262function \code{Py_Initialize()}. There are optional calls to pass command
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001263line arguments to Python. Then later you can call the interpreter
1264from any part of the application.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001265
1266There are several different ways to call the interpreter: you can pass
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001267a string containing Python statements to \code{PyRun_SimpleString()},
1268or you can pass a stdio file pointer and a file name (for
1269identification in error messages only) to \code{PyRun_SimpleFile()}. You
1270can also call the lower-level operations described in the previous
1271chapters to construct and use Python objects.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001272
1273A simple demo of embedding Python can be found in the directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001274\file{Demo/embed}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00001275
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001276
Guido van Rossum16d6e711994-08-08 12:30:22 +00001277\section{Embedding Python in \Cpp{}}
Fred Drake3da06a61998-02-26 18:49:12 +00001278\label{embeddingInCplusplus}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001279
Guido van Rossum16d6e711994-08-08 12:30:22 +00001280It is also possible to embed Python in a \Cpp{} program; precisely how this
1281is done will depend on the details of the \Cpp{} system used; in general you
1282will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
1283to compile and link your program. There is no need to recompile Python
1284itself using \Cpp{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001285
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001286
1287\chapter{Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001288\label{dynload}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001289
Guido van Rossum6938f061994-08-01 12:22:53 +00001290On most modern systems it is possible to configure Python to support
Fred Drake0fd82681998-01-09 05:39:38 +00001291dynamic loading of extension modules implemented in \C{}. When shared
Guido van Rossum6938f061994-08-01 12:22:53 +00001292libraries are used dynamic loading is configured automatically;
1293otherwise you have to select it as a build option (see below). Once
1294configured, dynamic loading is trivial to use: when a Python program
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001295executes \code{import spam}, the search for modules tries to find a
1296file \file{spammodule.o} (\file{spammodule.so} when using shared
Guido van Rossum6938f061994-08-01 12:22:53 +00001297libraries) in the module search path, and if one is found, it is
1298loaded into the executing binary and executed. Once loaded, the
1299module acts just like a built-in extension module.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001300
Guido van Rossumb92112d1995-03-20 14:24:09 +00001301The advantages of dynamic loading are twofold: the ``core'' Python
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001302binary gets smaller, and users can extend Python with their own
Fred Drake0fd82681998-01-09 05:39:38 +00001303modules implemented in \C{} without having to build and maintain their
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001304own copy of the Python interpreter. There are also disadvantages:
1305dynamic loading isn't available on all systems (this just means that
1306on some systems you have to use static loading), and dynamically
1307loading a module that was compiled for a different version of Python
Guido van Rossum6938f061994-08-01 12:22:53 +00001308(e.g. with a different representation of objects) may dump core.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001309
1310
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001311\section{Configuring and Building the Interpreter for Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001312\label{dynloadConfig}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001313
Guido van Rossum6938f061994-08-01 12:22:53 +00001314There are three styles of dynamic loading: one using shared libraries,
1315one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
1316loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001317
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001318\subsection{Shared Libraries}
Fred Drake3da06a61998-02-26 18:49:12 +00001319\label{sharedlibs}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001320
Guido van Rossum16d6e711994-08-08 12:30:22 +00001321The following systems support dynamic loading using shared libraries:
Guido van Rossum6938f061994-08-01 12:22:53 +00001322SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
1323systems derived from SVR4, or at least those SVR4 derivatives that
1324support shared libraries (are there any that don't?).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001325
Guido van Rossum6938f061994-08-01 12:22:53 +00001326You don't need to do anything to configure dynamic loading on these
1327systems --- the \file{configure} detects the presence of the
1328\file{<dlfcn.h>} header file and automatically configures dynamic
1329loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001330
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001331\subsection{SGI IRIX 4 Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001332\label{irixDynload}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001333
Guido van Rossum6938f061994-08-01 12:22:53 +00001334Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
1335loading. (SGI IRIX 5 might also support it but it is inferior to
1336using shared libraries so there is no reason to; a small test didn't
1337work right away so I gave up trying to support it.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001338
Guido van Rossum6938f061994-08-01 12:22:53 +00001339Before you build Python, you first need to fetch and build the \code{dl}
1340package written by Jack Jansen. This is available by anonymous ftp
Fred Drakeca6567f1998-01-22 20:44:18 +00001341from \url{ftp://ftp.cwi.nl/pub/dynload}, file
Guido van Rossum6938f061994-08-01 12:22:53 +00001342\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
1343instructions in the package's \file{README} file to build it.
1344
1345Once you have built \code{dl}, you can configure Python to use it. To
1346this end, you run the \file{configure} script with the option
1347\code{--with-dl=\var{directory}} where \var{directory} is the absolute
1348pathname of the \code{dl} directory.
1349
1350Now build and install Python as you normally would (see the
1351\file{README} file in the toplevel Python directory.)
1352
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001353\subsection{GNU Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001354\label{gnuDynload}
Guido van Rossum6938f061994-08-01 12:22:53 +00001355
1356GNU dynamic loading supports (according to its \file{README} file) the
1357following hardware and software combinations: VAX (Ultrix), Sun 3
1358(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
1359Atari ST. There is no reason to use it on a Sparc; I haven't seen a
1360Sun 3 for years so I don't know if these have shared libraries or not.
1361
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001362You need to fetch and build two packages.
1363One is GNU DLD. All development of this code has been done with DLD
Fred Drakeca6567f1998-01-22 20:44:18 +00001364version 3.2.3, which is available by anonymous ftp from
1365\url{ftp://ftp.cwi.nl/pub/dynload}, file
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001366\file{dld-3.2.3.tar.Z}. (A more recent version of DLD is available
Fred Drakeca6567f1998-01-22 20:44:18 +00001367via \url{http://www-swiss.ai.mit.edu/~jaffer/DLD.html} but this has
Guido van Rossum7e924dd1997-02-10 16:51:52 +00001368not been tested.)
1369The other package needed is an
Guido van Rossum6938f061994-08-01 12:22:53 +00001370emulation of Jack Jansen's \code{dl} package that I wrote on top of
1371GNU DLD 3.2.3. This is available from the same host and directory,
Guido van Rossum98046b91997-08-14 19:50:18 +00001372file \file{dl-dld-1.1.tar.Z}. (The version number may change --- but I doubt
Guido van Rossum6938f061994-08-01 12:22:53 +00001373it will.) Follow the instructions in each package's \file{README}
Guido van Rossum98046b91997-08-14 19:50:18 +00001374file to configure and build them.
Guido van Rossum6938f061994-08-01 12:22:53 +00001375
1376Now configure Python. Run the \file{configure} script with the option
1377\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
1378\var{dl-directory} is the absolute pathname of the directory where you
1379have built the \file{dl-dld} package, and \var{dld-directory} is that
1380of the GNU DLD package. The Python interpreter you build hereafter
1381will support GNU dynamic loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001382
1383
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001384\section{Building a Dynamically Loadable Module}
Fred Drake3da06a61998-02-26 18:49:12 +00001385\label{makedynload}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001386
Guido van Rossum6938f061994-08-01 12:22:53 +00001387Since there are three styles of dynamic loading, there are also three
1388groups of instructions for building a dynamically loadable module.
1389Instructions common for all three styles are given first. Assuming
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001390your module is called \code{spam}, the source filename must be
1391\file{spammodule.c}, so the object name is \file{spammodule.o}. The
Guido van Rossum6938f061994-08-01 12:22:53 +00001392module must be written as a normal Python extension module (as
1393described earlier).
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001394
Guido van Rossum6938f061994-08-01 12:22:53 +00001395Note that in all cases you will have to create your own Makefile that
1396compiles your module file(s). This Makefile will have to pass two
Fred Drake0fd82681998-01-09 05:39:38 +00001397\samp{-I} arguments to the \C{} compiler which will make it find the
Guido van Rossum6938f061994-08-01 12:22:53 +00001398Python header files. If the Make variable \var{PYTHONTOP} points to
1399the toplevel Python directory, your \var{CFLAGS} Make variable should
1400contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
1401(Most header files are in the \file{Include} subdirectory, but the
Guido van Rossum305ed111996-08-19 22:59:46 +00001402\file{config.h} header lives in the toplevel directory.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001403
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001404
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001405\subsection{Shared Libraries}
Fred Drake3da06a61998-02-26 18:49:12 +00001406\label{linking}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001407
Fred Drakeaf8a0151998-01-14 14:51:31 +00001408You must link the \file{.o} file to produce a shared library. This is
1409done using a special invocation of the \UNIX{} loader/linker,
1410\emph{ld}(1). Unfortunately the invocation differs slightly per
1411system.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001412
Guido van Rossum6938f061994-08-01 12:22:53 +00001413On SunOS 4, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001414\begin{verbatim}
1415ld spammodule.o -o spammodule.so
1416\end{verbatim}
1417
Guido van Rossum6938f061994-08-01 12:22:53 +00001418On Solaris 2, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001419\begin{verbatim}
1420ld -G spammodule.o -o spammodule.so
1421\end{verbatim}
1422
Guido van Rossum6938f061994-08-01 12:22:53 +00001423On SGI IRIX 5, use
Fred Drake1e11a5c1998-02-13 07:11:32 +00001424\begin{verbatim}
1425ld -shared spammodule.o -o spammodule.so
1426\end{verbatim}
1427
Guido van Rossumb92112d1995-03-20 14:24:09 +00001428On other systems, consult the manual page for \code{ld}(1) to find what
Guido van Rossum6938f061994-08-01 12:22:53 +00001429flags, if any, must be used.
1430
1431If your extension module uses system libraries that haven't already
1432been linked with Python (e.g. a windowing system), these must be
Guido van Rossumb92112d1995-03-20 14:24:09 +00001433passed to the \code{ld} command as \samp{-l} options after the
Guido van Rossum6938f061994-08-01 12:22:53 +00001434\samp{.o} file.
1435
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001436The resulting file \file{spammodule.so} must be copied into a directory
Guido van Rossum6938f061994-08-01 12:22:53 +00001437along the Python module search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001438
1439
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001440\subsection{SGI IRIX 4 Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001441\label{irixLinking}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001442
Fred Drakeaf8a0151998-01-14 14:51:31 +00001443\strong{IMPORTANT:} You must compile your extension module with the
Fred Drake0fd82681998-01-09 05:39:38 +00001444additional \C{} flag \samp{-G0} (or \samp{-G 0}). This instruct the
Guido van Rossum6938f061994-08-01 12:22:53 +00001445assembler to generate position-independent code.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001446
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001447You don't need to link the resulting \file{spammodule.o} file; just
Guido van Rossum6938f061994-08-01 12:22:53 +00001448copy it into a directory along the Python module search path.
1449
1450The first time your extension is loaded, it takes some extra time and
1451a few messages may be printed. This creates a file
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001452\file{spammodule.ld} which is an image that can be loaded quickly into
Guido van Rossum6938f061994-08-01 12:22:53 +00001453the Python interpreter process. When a new Python interpreter is
1454installed, the \code{dl} package detects this and rebuilds
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001455\file{spammodule.ld}. The file \file{spammodule.ld} is placed in the
1456directory where \file{spammodule.o} was found, unless this directory is
Guido van Rossum6938f061994-08-01 12:22:53 +00001457unwritable; in that case it is placed in a temporary
1458directory.\footnote{Check the manual page of the \code{dl} package for
1459details.}
1460
1461If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001462create a file \file{spammodule.libs} in the same directory as the
1463\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001464whitespace-separated options that will be passed to the linker ---
1465normally only \samp{-l} options or absolute pathnames of libraries
1466(\samp{.a} files) should be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001467
1468
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001469\subsection{GNU Dynamic Loading}
Fred Drake3da06a61998-02-26 18:49:12 +00001470\label{gnuLinking}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001471
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001472Just copy \file{spammodule.o} into a directory along the Python module
Guido van Rossum6938f061994-08-01 12:22:53 +00001473search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001474
Guido van Rossum6938f061994-08-01 12:22:53 +00001475If your extension modules uses additional system libraries, you must
Guido van Rossum5049bcb1995-03-13 16:55:23 +00001476create a file \file{spammodule.libs} in the same directory as the
1477\file{spammodule.o}. This file should contain one or more lines with
Guido van Rossum6938f061994-08-01 12:22:53 +00001478whitespace-separated absolute pathnames of libraries (\samp{.a}
1479files). No \samp{-l} options can be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001480
1481
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001482%\input{extref}
Guido van Rossum267e80d1996-08-09 21:01:07 +00001483
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001484\input{ext.ind}
1485
1486\end{document}