Fred Drake | 6659c30 | 1998-03-03 22:02:19 +0000 | [diff] [blame] | 1 | \documentclass{manual} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 3 | % XXX PM Modulator |
| 4 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 5 | \title{Extending and Embedding the Python Interpreter} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 7 | \input{boilerplate} |
Guido van Rossum | 83eb962 | 1993-11-23 16:28:45 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 9 | % Tell \index to actually write the .idx file |
| 10 | \makeindex |
| 11 | |
| 12 | \begin{document} |
| 13 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 14 | \maketitle |
| 15 | |
Fred Drake | 9f86b66 | 1998-07-28 21:55:19 +0000 | [diff] [blame^] | 16 | \ifhtml |
| 17 | \chapter*{Front Matter\label{front}} |
| 18 | \fi |
| 19 | |
Guido van Rossum | 16cd7f9 | 1994-10-06 10:29:26 +0000 | [diff] [blame] | 20 | \input{copyright} |
| 21 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 22 | \begin{abstract} |
| 23 | |
| 24 | \noindent |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 25 | Python is an interpreted, object-oriented programming language. This |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 26 | document describes how to write modules in \C{} or \Cpp{} to extend the |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 27 | Python interpreter with new modules. Those modules can define new |
| 28 | functions but also new object types and their methods. The document |
| 29 | also describes how to embed the Python interpreter in another |
| 30 | application, for use as an extension language. Finally, it shows how |
| 31 | to compile and link extension modules so that they can be loaded |
| 32 | dynamically (at run time) into the interpreter, if the underlying |
| 33 | operating system supports this feature. |
| 34 | |
| 35 | This document assumes basic knowledge about Python. For an informal |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 36 | introduction to the language, see the Python Tutorial. The \emph{Python |
| 37 | Reference Manual} gives a more formal definition of the language. The |
| 38 | \emph{Python Library Reference} documents the existing object types, |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 39 | functions and modules (both built-in and written in Python) that give |
| 40 | the language its wide application range. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 41 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 42 | For a detailed description of the whole Python/\C{} API, see the separate |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 43 | \emph{Python/\C{} API Reference Manual}. \strong{Note:} While that |
| 44 | manual is still in a state of flux, it is safe to say that it is much |
| 45 | more up to date than the manual you're reading currently (which has |
| 46 | been in need for an upgrade for some time now). |
Guido van Rossum | fdacc58 | 1997-10-07 14:40:16 +0000 | [diff] [blame] | 47 | |
| 48 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 49 | \end{abstract} |
| 50 | |
Fred Drake | 4d4f9e7 | 1998-01-13 22:25:02 +0000 | [diff] [blame] | 51 | \tableofcontents |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 53 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 54 | \chapter{Extending Python with \C{} or \Cpp{} code} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 56 | |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 57 | %\section{Introduction} |
| 58 | \label{intro} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 60 | It is quite easy to add new built-in modules to Python, if you know |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 61 | how to program in \C{}. Such \dfn{extension modules} can do two things |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 62 | that can't be done directly in Python: they can implement new built-in |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 63 | object types, and they can call \C{} library functions and system calls. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 65 | To support extensions, the Python API (Application Programmers |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 66 | Interface) defines a set of functions, macros and variables that |
| 67 | provide access to most aspects of the Python run-time system. The |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 68 | Python API is incorporated in a \C{} source file by including the header |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 69 | \code{"Python.h"}. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 71 | The compilation of an extension module depends on its intended use as |
| 72 | well as on your system setup; details are given in a later section. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 75 | \section{A Simple Example} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 76 | \label{simpleExample} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 78 | Let's create an extension module called \samp{spam} (the favorite food |
| 79 | of Monty Python fans...) and let's say we want to create a Python |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 80 | interface to the \C{} library function \cfunction{system()}.\footnote{An |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 81 | interface for this function already exists in the standard module |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 82 | \module{os} --- it was chosen as a simple and straightfoward example.} |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 83 | This function takes a null-terminated character string as argument and |
| 84 | returns an integer. We want this function to be callable from Python |
| 85 | as follows: |
| 86 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 87 | \begin{verbatim} |
| 88 | >>> import spam |
| 89 | >>> status = spam.system("ls -l") |
| 90 | \end{verbatim} |
| 91 | |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 92 | Begin by creating a file \file{spammodule.c}. (In general, if a |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 93 | module is called \samp{spam}, the \C{} file containing its implementation |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 94 | is called \file{spammodule.c}; if the module name is very long, like |
| 95 | \samp{spammify}, the module name can be just \file{spammify.c}.) |
| 96 | |
| 97 | The first line of our file can be: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 98 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 99 | \begin{verbatim} |
| 100 | #include "Python.h" |
| 101 | \end{verbatim} |
| 102 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 103 | which pulls in the Python API (you can add a comment describing the |
| 104 | purpose of the module and a copyright notice if you like). |
| 105 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 106 | All user-visible symbols defined by \code{"Python.h"} have a prefix of |
| 107 | \samp{Py} or \samp{PY}, except those defined in standard header files. |
| 108 | For convenience, and since they are used extensively by the Python |
| 109 | interpreter, \code{"Python.h"} includes a few standard header files: |
| 110 | \code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and |
| 111 | \code{<stdlib.h>}. If the latter header file does not exist on your |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 112 | system, it declares the functions \cfunction{malloc()}, |
| 113 | \cfunction{free()} and \cfunction{realloc()} directly. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 114 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 115 | The next thing we add to our module file is the \C{} function that will |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 116 | be called when the Python expression \samp{spam.system(\var{string})} |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 117 | is evaluated (we'll see shortly how it ends up being called): |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 118 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 119 | \begin{verbatim} |
| 120 | static PyObject * |
| 121 | spam_system(self, args) |
| 122 | PyObject *self; |
| 123 | PyObject *args; |
| 124 | { |
| 125 | char *command; |
| 126 | int sts; |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 127 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 128 | if (!PyArg_ParseTuple(args, "s", &command)) |
| 129 | return NULL; |
| 130 | sts = system(command); |
| 131 | return Py_BuildValue("i", sts); |
| 132 | } |
| 133 | \end{verbatim} |
| 134 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 135 | There is a straightforward translation from the argument list in |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 136 | Python (e.g.\ the single expression \code{"ls -l"}) to the arguments |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 137 | passed to the \C{} function. The \C{} function always has two arguments, |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 138 | conventionally named \var{self} and \var{args}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 139 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 140 | The \var{self} argument is only used when the \C{} function implements a |
Fred Drake | dc40904 | 1998-04-02 18:54:54 +0000 | [diff] [blame] | 141 | built-in method. This will be discussed later. In the example, |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 142 | \var{self} will always be a \NULL{} pointer, since we are defining |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 143 | a function, not a method. (This is done so that the interpreter |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 144 | doesn't have to understand two different types of \C{} functions.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 146 | The \var{args} argument will be a pointer to a Python tuple object |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 147 | containing the arguments. Each item of the tuple corresponds to an |
| 148 | argument in the call's argument list. The arguments are Python |
Fred Drake | 1aedbd8 | 1998-02-16 14:47:27 +0000 | [diff] [blame] | 149 | objects --- in order to do anything with them in our \C{} function we have |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 150 | to convert them to \C{} values. The function \cfunction{PyArg_ParseTuple()} |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 151 | in the Python API checks the argument types and converts them to \C{} |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 152 | values. It uses a template string to determine the required types of |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 153 | the arguments as well as the types of the \C{} variables into which to |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 154 | store the converted values. More about this later. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 155 | |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 156 | \cfunction{PyArg_ParseTuple()} returns true (nonzero) if all arguments have |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 157 | the right type and its components have been stored in the variables |
| 158 | whose addresses are passed. It returns false (zero) if an invalid |
| 159 | argument list was passed. In the latter case it also raises an |
| 160 | appropriate exception by so the calling function can return |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 161 | \NULL{} immediately (as we saw in the example). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 162 | |
| 163 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 164 | \section{Intermezzo: Errors and Exceptions} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 165 | \label{errors} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 166 | |
| 167 | An important convention throughout the Python interpreter is the |
| 168 | following: when a function fails, it should set an exception condition |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 169 | and return an error value (usually a \NULL{} pointer). Exceptions |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 170 | are stored in a static global variable inside the interpreter; if this |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 171 | variable is \NULL{} no exception has occurred. A second global |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 172 | variable stores the ``associated value'' of the exception (the second |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 173 | argument to \keyword{raise}). A third variable contains the stack |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 174 | traceback in case the error originated in Python code. These three |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 175 | variables are the \C{} equivalents of the Python variables |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 176 | \code{sys.exc_type}, \code{sys.exc_value} and \code{sys.exc_traceback} |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 177 | (see the section on module \module{sys} in the \emph{Python Library |
| 178 | Reference}). It is important to know about them to understand how |
| 179 | errors are passed around. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 181 | The Python API defines a number of functions to set various types of |
| 182 | exceptions. |
| 183 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 184 | The most common one is \cfunction{PyErr_SetString()}. Its arguments |
| 185 | are an exception object and a \C{} string. The exception object is |
| 186 | usually a predefined object like \cdata{PyExc_ZeroDivisionError}. The |
| 187 | \C{} string indicates the cause of the error and is converted to a |
| 188 | Python string object and stored as the ``associated value'' of the |
| 189 | exception. |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 190 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 191 | Another useful function is \cfunction{PyErr_SetFromErrno()}, which only |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 192 | takes an exception argument and constructs the associated value by |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 193 | inspection of the (\UNIX{}) global variable \cdata{errno}. The most |
| 194 | general function is \cfunction{PyErr_SetObject()}, which takes two object |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 195 | arguments, the exception and its associated value. You don't need to |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 196 | \cfunction{Py_INCREF()} the objects passed to any of these functions. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 197 | |
| 198 | You can test non-destructively whether an exception has been set with |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 199 | \cfunction{PyErr_Occurred()}. This returns the current exception object, |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 200 | or \NULL{} if no exception has occurred. You normally don't need |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 201 | to call \cfunction{PyErr_Occurred()} to see whether an error occurred in a |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 202 | function call, since you should be able to tell from the return value. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 203 | |
Guido van Rossum | d16ddb6 | 1996-12-13 02:38:17 +0000 | [diff] [blame] | 204 | When a function \var{f} that calls another function \var{g} detects |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 205 | that the latter fails, \var{f} should itself return an error value |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 206 | (e.g. \NULL{} or \code{-1}). It should \emph{not} call one of the |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 207 | \cfunction{PyErr_*()} functions --- one has already been called by \var{g}. |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 208 | \var{f}'s caller is then supposed to also return an error indication |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 209 | to \emph{its} caller, again \emph{without} calling \cfunction{PyErr_*()}, |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 210 | and so on --- the most detailed cause of the error was already |
| 211 | reported by the function that first detected it. Once the error |
| 212 | reaches the Python interpreter's main loop, this aborts the currently |
| 213 | executing Python code and tries to find an exception handler specified |
| 214 | by the Python programmer. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 215 | |
| 216 | (There are situations where a module can actually give a more detailed |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 217 | error message by calling another \cfunction{PyErr_*()} function, and in |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 218 | such cases it is fine to do so. As a general rule, however, this is |
| 219 | not necessary, and can cause information about the cause of the error |
| 220 | to be lost: most operations can fail for a variety of reasons.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 222 | To ignore an exception set by a function call that failed, the exception |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 223 | condition must be cleared explicitly by calling \cfunction{PyErr_Clear()}. |
| 224 | The only time \C{} code should call \cfunction{PyErr_Clear()} is if it doesn't |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 225 | want to pass the error on to the interpreter but wants to handle it |
| 226 | completely by itself (e.g. by trying something else or pretending |
| 227 | nothing happened). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 228 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 229 | Note that a failing \cfunction{malloc()} call must be turned into an |
| 230 | exception --- the direct caller of \cfunction{malloc()} (or |
| 231 | \cfunction{realloc()}) must call \cfunction{PyErr_NoMemory()} and |
| 232 | return a failure indicator itself. All the object-creating functions |
| 233 | (\cfunction{PyInt_FromLong()} etc.) already do this, so only if you |
| 234 | call \cfunction{malloc()} directly this note is of importance. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 236 | Also note that, with the important exception of |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 237 | \cfunction{PyArg_ParseTuple()} and friends, functions that return an |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 238 | integer status usually return a positive value or zero for success and |
| 239 | \code{-1} for failure, like \UNIX{} system calls. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 240 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 241 | Finally, be careful to clean up garbage (by making |
| 242 | \cfunction{Py_XDECREF()} or \cfunction{Py_DECREF()} calls for objects |
| 243 | you have already created) when you return an error indicator! |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 244 | |
| 245 | The choice of which exception to raise is entirely yours. There are |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 246 | predeclared \C{} objects corresponding to all built-in Python exceptions, |
Fred Drake | b85fbec | 1998-04-13 00:50:04 +0000 | [diff] [blame] | 247 | e.g. \cdata{PyExc_ZeroDivisionError} which you can use directly. Of |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 248 | course, you should choose exceptions wisely --- don't use |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 249 | \cdata{PyExc_TypeError} to mean that a file couldn't be opened (that |
| 250 | should probably be \cdata{PyExc_IOError}). If something's wrong with |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 251 | the argument list, the \cfunction{PyArg_ParseTuple()} function usually |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 252 | raises \cdata{PyExc_TypeError}. If you have an argument whose value |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 253 | which must be in a particular range or must satisfy other conditions, |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 254 | \cdata{PyExc_ValueError} is appropriate. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 255 | |
| 256 | You can also define a new exception that is unique to your module. |
| 257 | For this, you usually declare a static object variable at the |
| 258 | beginning of your file, e.g. |
| 259 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 260 | \begin{verbatim} |
| 261 | static PyObject *SpamError; |
| 262 | \end{verbatim} |
| 263 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 264 | and initialize it in your module's initialization function |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 265 | (\cfunction{initspam()}) with an exception object, e.g. (leaving out |
| 266 | the error checking for now): |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 267 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 268 | \begin{verbatim} |
| 269 | void |
| 270 | initspam() |
| 271 | { |
| 272 | PyObject *m, *d; |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 273 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 274 | m = Py_InitModule("spam", SpamMethods); |
| 275 | d = PyModule_GetDict(m); |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 276 | SpamError = PyErr_NewException("spam.error", NULL, NULL); |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 277 | PyDict_SetItemString(d, "error", SpamError); |
| 278 | } |
| 279 | \end{verbatim} |
| 280 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 281 | Note that the Python name for the exception object is |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 282 | \exception{spam.error}. The \cfunction{PyErr_NewException()} function |
| 283 | may create either a string or class, depending on whether the |
| 284 | \samp{-X} flag was passed to the interpreter. If \samp{-X} was used, |
| 285 | \cdata{SpamError} will be a string object, otherwise it will be a |
| 286 | class object with the base class being \exception{Exception}, |
| 287 | described in the \emph{Python Library Reference} under ``Built-in |
| 288 | Exceptions.'' |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 291 | \section{Back to the Example} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 292 | \label{backToExample} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 293 | |
| 294 | Going back to our example function, you should now be able to |
| 295 | understand this statement: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 296 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 297 | \begin{verbatim} |
| 298 | if (!PyArg_ParseTuple(args, "s", &command)) |
| 299 | return NULL; |
| 300 | \end{verbatim} |
| 301 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 302 | It returns \NULL{} (the error indicator for functions returning |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 303 | object pointers) if an error is detected in the argument list, relying |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 304 | on the exception set by \cfunction{PyArg_ParseTuple()}. Otherwise the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 305 | string value of the argument has been copied to the local variable |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 306 | \cdata{command}. This is a pointer assignment and you are not supposed |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 307 | to modify the string to which it points (so in Standard \C{}, the variable |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 308 | \cdata{command} should properly be declared as \samp{const char |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 309 | *command}). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 310 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 311 | The next statement is a call to the \UNIX{} function |
| 312 | \cfunction{system()}, passing it the string we just got from |
| 313 | \cfunction{PyArg_ParseTuple()}: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 314 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 315 | \begin{verbatim} |
| 316 | sts = system(command); |
| 317 | \end{verbatim} |
| 318 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 319 | Our \function{spam.system()} function must return the value of |
| 320 | \cdata{sts} as a Python object. This is done using the function |
| 321 | \cfunction{Py_BuildValue()}, which is something like the inverse of |
| 322 | \cfunction{PyArg_ParseTuple()}: it takes a format string and an |
| 323 | arbitrary number of \C{} values, and returns a new Python object. |
| 324 | More info on \cfunction{Py_BuildValue()} is given later. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 325 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 326 | \begin{verbatim} |
| 327 | return Py_BuildValue("i", sts); |
| 328 | \end{verbatim} |
| 329 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 330 | In this case, it will return an integer object. (Yes, even integers |
| 331 | are objects on the heap in Python!) |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 332 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 333 | If you have a \C{} function that returns no useful argument (a function |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 334 | returning \ctype{void}), the corresponding Python function must return |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 335 | \code{None}. You need this idiom to do so: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 336 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 337 | \begin{verbatim} |
| 338 | Py_INCREF(Py_None); |
| 339 | return Py_None; |
| 340 | \end{verbatim} |
| 341 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 342 | \cdata{Py_None} is the \C{} name for the special Python object |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 343 | \code{None}. It is a genuine Python object rather than a \NULL{} |
| 344 | pointer, which means ``error'' in most contexts, as we have seen. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 345 | |
| 346 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 347 | \section{The Module's Method Table and Initialization Function} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 348 | \label{methodTable} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 349 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 350 | I promised to show how \cfunction{spam_system()} is called from Python |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 351 | programs. First, we need to list its name and address in a ``method |
| 352 | table'': |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 353 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 354 | \begin{verbatim} |
| 355 | static PyMethodDef SpamMethods[] = { |
| 356 | ... |
| 357 | {"system", spam_system, METH_VARARGS}, |
| 358 | ... |
| 359 | {NULL, NULL} /* Sentinel */ |
| 360 | }; |
| 361 | \end{verbatim} |
| 362 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 363 | Note the third entry (\samp{METH_VARARGS}). This is a flag telling |
| 364 | the interpreter the calling convention to be used for the \C{} |
| 365 | function. It should normally always be \samp{METH_VARARGS} or |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 366 | \samp{METH_VARARGS | METH_KEYWORDS}; a value of \code{0} means that an |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 367 | obsolete variant of \cfunction{PyArg_ParseTuple()} is used. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 368 | |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 369 | When using only \samp{METH_VARARGS}, the function should expect |
| 370 | the Python-level parameters to be passed in as a tuple acceptable for |
| 371 | parsing via \cfunction{PyArg_ParseTuple()}; more information on this |
| 372 | function is provided below. |
| 373 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 374 | The \constant{METH_KEYWORDS} bit may be set in the third field if keyword |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 375 | arguments should be passed to the function. In this case, the \C{} |
| 376 | function should accept a third \samp{PyObject *} parameter which will |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 377 | be a dictionary of keywords. Use \cfunction{PyArg_ParseTupleAndKeywords()} |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 378 | to parse the arguemts to such a function. |
| 379 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 380 | The method table must be passed to the interpreter in the module's |
| 381 | initialization function (which should be the only non-\code{static} |
| 382 | item defined in the module file): |
| 383 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 384 | \begin{verbatim} |
| 385 | void |
| 386 | initspam() |
| 387 | { |
| 388 | (void) Py_InitModule("spam", SpamMethods); |
| 389 | } |
| 390 | \end{verbatim} |
| 391 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 392 | When the Python program imports module \module{spam} for the first |
| 393 | time, \cfunction{initspam()} is called. It calls |
| 394 | \cfunction{Py_InitModule()}, which creates a ``module object'' (which |
| 395 | is inserted in the dictionary \code{sys.modules} under the key |
| 396 | \code{"spam"}), and inserts built-in function objects into the newly |
| 397 | created module based upon the table (an array of \ctype{PyMethodDef} |
| 398 | structures) that was passed as its second argument. |
| 399 | \cfunction{Py_InitModule()} returns a pointer to the module object |
| 400 | that it creates (which is unused here). It aborts with a fatal error |
| 401 | if the module could not be initialized satisfactorily, so the caller |
| 402 | doesn't need to check for errors. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 403 | |
| 404 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 405 | \section{Compilation and Linkage} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 406 | \label{compilation} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 408 | There are two more things to do before you can use your new extension: |
| 409 | compiling and linking it with the Python system. If you use dynamic |
| 410 | loading, the details depend on the style of dynamic loading your |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 411 | system uses; see the chapter ``Dynamic Loading'' for more information |
| 412 | about this. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 413 | |
| 414 | If you can't use dynamic loading, or if you want to make your module a |
| 415 | permanent part of the Python interpreter, you will have to change the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 416 | configuration setup and rebuild the interpreter. Luckily, this is |
| 417 | very simple: just place your file (\file{spammodule.c} for example) in |
| 418 | the \file{Modules} directory, add a line to the file |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 419 | \file{Modules/Setup.local} describing your file: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 420 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 421 | \begin{verbatim} |
| 422 | spam spammodule.o |
| 423 | \end{verbatim} |
| 424 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 425 | and rebuild the interpreter by running \program{make} in the toplevel |
| 426 | directory. You can also run \program{make} in the \file{Modules} |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 427 | subdirectory, but then you must first rebuild \file{Makefile} |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 428 | there by running `\program{make} Makefile'. (This is necessary each |
| 429 | time you change the \file{Setup} file.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 430 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 431 | If your module requires additional libraries to link with, these can |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 432 | be listed on the line in the configuration file as well, for instance: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 433 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 434 | \begin{verbatim} |
| 435 | spam spammodule.o -lX11 |
| 436 | \end{verbatim} |
| 437 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 438 | \section{Calling Python Functions From \C{}} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 439 | \label{callingPython} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 440 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 441 | So far we have concentrated on making \C{} functions callable from |
| 442 | Python. The reverse is also useful: calling Python functions from \C{}. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 443 | This is especially the case for libraries that support so-called |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 444 | ``callback'' functions. If a \C{} interface makes use of callbacks, the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 445 | equivalent Python often needs to provide a callback mechanism to the |
| 446 | Python programmer; the implementation will require calling the Python |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 447 | callback functions from a \C{} callback. Other uses are also imaginable. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 448 | |
| 449 | Fortunately, the Python interpreter is easily called recursively, and |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 450 | there is a standard interface to call a Python function. (I won't |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 451 | dwell on how to call the Python parser with a particular string as |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 452 | input --- if you're interested, have a look at the implementation of |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 453 | the \samp{-c} command line option in \file{Python/pythonmain.c}.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 454 | |
| 455 | Calling a Python function is easy. First, the Python program must |
| 456 | somehow pass you the Python function object. You should provide a |
| 457 | function (or some other interface) to do this. When this function is |
| 458 | called, save a pointer to the Python function object (be careful to |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 459 | \cfunction{Py_INCREF()} it!) in a global variable --- or whereever you |
| 460 | see fit. For example, the following function might be part of a module |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 461 | definition: |
| 462 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 463 | \begin{verbatim} |
| 464 | static PyObject *my_callback = NULL; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 465 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 466 | static PyObject * |
| 467 | my_set_callback(dummy, arg) |
| 468 | PyObject *dummy, *arg; |
| 469 | { |
| 470 | Py_XDECREF(my_callback); /* Dispose of previous callback */ |
| 471 | Py_XINCREF(arg); /* Add a reference to new callback */ |
| 472 | my_callback = arg; /* Remember new callback */ |
| 473 | /* Boilerplate to return "None" */ |
| 474 | Py_INCREF(Py_None); |
| 475 | return Py_None; |
| 476 | } |
| 477 | \end{verbatim} |
| 478 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 479 | The macros \cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()} |
| 480 | increment/decrement the reference count of an object and are safe in |
| 481 | the presence of \NULL{} pointers. More info on them in the section on |
| 482 | Reference Counts below. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 483 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 484 | Later, when it is time to call the function, you call the \C{} function |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 485 | \cfunction{PyEval_CallObject()}. This function has two arguments, both |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 486 | pointers to arbitrary Python objects: the Python function, and the |
| 487 | argument list. The argument list must always be a tuple object, whose |
| 488 | length is the number of arguments. To call the Python function with |
| 489 | no arguments, pass an empty tuple; to call it with one argument, pass |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 490 | a singleton tuple. \cfunction{Py_BuildValue()} returns a tuple when its |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 491 | format string consists of zero or more format codes between |
| 492 | parentheses. For example: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 493 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 494 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 495 | int arg; |
| 496 | PyObject *arglist; |
| 497 | PyObject *result; |
| 498 | ... |
| 499 | arg = 123; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 500 | ... |
| 501 | /* Time to call the callback */ |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 502 | arglist = Py_BuildValue("(i)", arg); |
| 503 | result = PyEval_CallObject(my_callback, arglist); |
| 504 | Py_DECREF(arglist); |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 505 | \end{verbatim} |
| 506 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 507 | \cfunction{PyEval_CallObject()} returns a Python object pointer: this is |
| 508 | the return value of the Python function. \cfunction{PyEval_CallObject()} is |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 509 | ``reference-count-neutral'' with respect to its arguments. In the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 510 | example a new tuple was created to serve as the argument list, which |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 511 | is \cfunction{Py_DECREF()}-ed immediately after the call. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 512 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 513 | The return value of \cfunction{PyEval_CallObject()} is ``new'': either it |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 514 | is a brand new object, or it is an existing object whose reference |
| 515 | count has been incremented. So, unless you want to save it in a |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 516 | global variable, you should somehow \cfunction{Py_DECREF()} the result, |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 517 | even (especially!) if you are not interested in its value. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 518 | |
| 519 | Before you do this, however, it is important to check that the return |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 520 | value isn't \NULL{}. If it is, the Python function terminated by |
| 521 | raising an exception. If the \C{} code that called |
| 522 | \cfunction{PyEval_CallObject()} is called from Python, it should now |
| 523 | return an error indication to its Python caller, so the interpreter |
| 524 | can print a stack trace, or the calling Python code can handle the |
| 525 | exception. If this is not possible or desirable, the exception should |
| 526 | be cleared by calling \cfunction{PyErr_Clear()}. For example: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 527 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 528 | \begin{verbatim} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 529 | if (result == NULL) |
| 530 | return NULL; /* Pass error back */ |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 531 | ...use result... |
| 532 | Py_DECREF(result); |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 533 | \end{verbatim} |
| 534 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 535 | Depending on the desired interface to the Python callback function, |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 536 | you may also have to provide an argument list to |
| 537 | \cfunction{PyEval_CallObject()}. In some cases the argument list is |
| 538 | also provided by the Python program, through the same interface that |
| 539 | specified the callback function. It can then be saved and used in the |
| 540 | same manner as the function object. In other cases, you may have to |
| 541 | construct a new tuple to pass as the argument list. The simplest way |
| 542 | to do this is to call \cfunction{Py_BuildValue()}. For example, if |
| 543 | you want to pass an integral event code, you might use the following |
| 544 | code: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 545 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 546 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 547 | PyObject *arglist; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 548 | ... |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 549 | arglist = Py_BuildValue("(l)", eventcode); |
| 550 | result = PyEval_CallObject(my_callback, arglist); |
| 551 | Py_DECREF(arglist); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 552 | if (result == NULL) |
| 553 | return NULL; /* Pass error back */ |
| 554 | /* Here maybe use the result */ |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 555 | Py_DECREF(result); |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 556 | \end{verbatim} |
| 557 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 558 | Note the placement of \samp{Py_DECREF(arglist)} immediately after the |
| 559 | call, before the error check! Also note that strictly spoken this |
| 560 | code is not complete: \cfunction{Py_BuildValue()} may run out of |
| 561 | memory, and this should be checked. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 562 | |
| 563 | |
Fred Drake | e795718 | 1998-04-04 07:17:47 +0000 | [diff] [blame] | 564 | \section{Format Strings for \cfunction{PyArg_ParseTuple()}} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 565 | \label{parseTuple} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 566 | |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 567 | The \cfunction{PyArg_ParseTuple()} function is declared as follows: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 568 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 569 | \begin{verbatim} |
| 570 | int PyArg_ParseTuple(PyObject *arg, char *format, ...); |
| 571 | \end{verbatim} |
| 572 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 573 | The \var{arg} argument must be a tuple object containing an argument |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 574 | list passed from Python to a \C{} function. The \var{format} argument |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 575 | must be a format string, whose syntax is explained below. The |
| 576 | remaining arguments must be addresses of variables whose type is |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 577 | determined by the format string. For the conversion to succeed, the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 578 | \var{arg} object must match the format and the format must be |
| 579 | exhausted. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 580 | |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 581 | Note that while \cfunction{PyArg_ParseTuple()} checks that the Python |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 582 | arguments have the required types, it cannot check the validity of the |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 583 | addresses of \C{} variables passed to the call: if you make mistakes |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 584 | there, your code will probably crash or at least overwrite random bits |
| 585 | in memory. So be careful! |
| 586 | |
| 587 | A format string consists of zero or more ``format units''. A format |
| 588 | unit describes one Python object; it is usually a single character or |
| 589 | a parenthesized sequence of format units. With a few exceptions, a |
| 590 | format unit that is not a parenthesized sequence normally corresponds |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 591 | to a single address argument to \cfunction{PyArg_ParseTuple()}. In the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 592 | following description, the quoted form is the format unit; the entry |
| 593 | in (round) parentheses is the Python object type that matches the |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 594 | format unit; and the entry in [square] brackets is the type of the \C{} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 595 | variable(s) whose address should be passed. (Use the \samp{\&} |
| 596 | operator to pass a variable's address.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 597 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 598 | \begin{description} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 599 | |
Fred Drake | 3fe985f | 1998-03-04 03:51:42 +0000 | [diff] [blame] | 600 | \item[\samp{s} (string) {[char *]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 601 | Convert a Python string to a \C{} pointer to a character string. You |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 602 | must not provide storage for the string itself; a pointer to an |
| 603 | existing string is stored into the character pointer variable whose |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 604 | address you pass. The \C{} string is null-terminated. The Python string |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 605 | must not contain embedded null bytes; if it does, a \exception{TypeError} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 606 | exception is raised. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 607 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 608 | \item[\samp{s\#} (string) {[char *, int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 609 | This variant on \samp{s} stores into two \C{} variables, the first one |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 610 | a pointer to a character string, the second one its length. In this |
| 611 | case the Python string may contain embedded null bytes. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 612 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 613 | \item[\samp{z} (string or \code{None}) {[char *]}] |
| 614 | Like \samp{s}, but the Python object may also be \code{None}, in which |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 615 | case the \C{} pointer is set to \NULL{}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 617 | \item[\samp{z\#} (string or \code{None}) {[char *, int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 618 | This is to \samp{s\#} as \samp{z} is to \samp{s}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 619 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 620 | \item[\samp{b} (integer) {[char]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 621 | Convert a Python integer to a tiny int, stored in a \C{} \ctype{char}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 622 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 623 | \item[\samp{h} (integer) {[short int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 624 | Convert a Python integer to a \C{} \ctype{short int}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 625 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 626 | \item[\samp{i} (integer) {[int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 627 | Convert a Python integer to a plain \C{} \ctype{int}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 628 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 629 | \item[\samp{l} (integer) {[long int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 630 | Convert a Python integer to a \C{} \ctype{long int}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 632 | \item[\samp{c} (string of length 1) {[char]}] |
| 633 | Convert a Python character, represented as a string of length 1, to a |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 634 | \C{} \ctype{char}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 635 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 636 | \item[\samp{f} (float) {[float]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 637 | Convert a Python floating point number to a \C{} \ctype{float}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 638 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 639 | \item[\samp{d} (float) {[double]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 640 | Convert a Python floating point number to a \C{} \ctype{double}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 641 | |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 642 | \item[\samp{D} (complex) {[Py_complex]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 643 | Convert a Python complex number to a \C{} \ctype{Py_complex} structure. |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 644 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 645 | \item[\samp{O} (object) {[PyObject *]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 646 | Store a Python object (without any conversion) in a \C{} object pointer. |
| 647 | The \C{} program thus receives the actual object that was passed. The |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 648 | object's reference count is not increased. The pointer stored is not |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 649 | \NULL{}. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 650 | |
Fred Drake | 3fe985f | 1998-03-04 03:51:42 +0000 | [diff] [blame] | 651 | \item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 652 | Store a Python object in a \C{} object pointer. This is similar to |
| 653 | \samp{O}, but takes two \C{} arguments: the first is the address of a |
| 654 | Python type object, the second is the address of the \C{} variable (of |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 655 | type \ctype{PyObject *}) into which the object pointer is stored. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 656 | If the Python object does not have the required type, a |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 657 | \exception{TypeError} exception is raised. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 658 | |
Fred Drake | 3fe985f | 1998-03-04 03:51:42 +0000 | [diff] [blame] | 659 | \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 660 | Convert a Python object to a \C{} variable through a \var{converter} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 661 | function. This takes two arguments: the first is a function, the |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 662 | second is the address of a \C{} variable (of arbitrary type), converted |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 663 | to \ctype{void *}. The \var{converter} function in turn is called as |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 664 | follows: |
| 665 | |
| 666 | \code{\var{status} = \var{converter}(\var{object}, \var{address});} |
| 667 | |
| 668 | where \var{object} is the Python object to be converted and |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 669 | \var{address} is the \ctype{void *} argument that was passed to |
| 670 | \cfunction{PyArg_ConvertTuple()}. The returned \var{status} should be |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 671 | \code{1} for a successful conversion and \code{0} if the conversion |
| 672 | has failed. When the conversion fails, the \var{converter} function |
| 673 | should raise an exception. |
| 674 | |
| 675 | \item[\samp{S} (string) {[PyStringObject *]}] |
Guido van Rossum | 2474d68 | 1998-02-26 17:07:11 +0000 | [diff] [blame] | 676 | Like \samp{O} but requires that the Python object is a string object. |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 677 | Raises a \exception{TypeError} exception if the object is not a string |
| 678 | object. The \C{} variable may also be declared as \ctype{PyObject *}. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 679 | |
Fred Drake | 3fe985f | 1998-03-04 03:51:42 +0000 | [diff] [blame] | 680 | \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}] |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 681 | The object must be a Python tuple whose length is the number of format |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 682 | units in \var{items}. The \C{} arguments must correspond to the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 683 | individual format units in \var{items}. Format units for tuples may |
| 684 | be nested. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 685 | |
| 686 | \end{description} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 687 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 688 | It is possible to pass Python long integers where integers are |
Fred Drake | 1aedbd8 | 1998-02-16 14:47:27 +0000 | [diff] [blame] | 689 | requested; however no proper range checking is done --- the most |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 690 | significant bits are silently truncated when the receiving field is |
| 691 | too small to receive the value (actually, the semantics are inherited |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 692 | from downcasts in \C{} --- your milage may vary). |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 693 | |
| 694 | A few other characters have a meaning in a format string. These may |
| 695 | not occur inside nested parentheses. They are: |
| 696 | |
| 697 | \begin{description} |
| 698 | |
| 699 | \item[\samp{|}] |
| 700 | Indicates that the remaining arguments in the Python argument list are |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 701 | optional. The \C{} variables corresponding to optional arguments should |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 702 | be initialized to their default value --- when an optional argument is |
Fred Drake | 40e72f7 | 1998-03-03 19:37:38 +0000 | [diff] [blame] | 703 | not specified, \cfunction{PyArg_ParseTuple()} does not touch the contents |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 704 | of the corresponding \C{} variable(s). |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 705 | |
| 706 | \item[\samp{:}] |
| 707 | The list of format units ends here; the string after the colon is used |
| 708 | as the function name in error messages (the ``associated value'' of |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 709 | the exceptions that \cfunction{PyArg_ParseTuple()} raises). |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 710 | |
| 711 | \item[\samp{;}] |
| 712 | The list of format units ends here; the string after the colon is used |
| 713 | as the error message \emph{instead} of the default error message. |
| 714 | Clearly, \samp{:} and \samp{;} mutually exclude each other. |
| 715 | |
| 716 | \end{description} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 717 | |
| 718 | Some example calls: |
| 719 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 720 | \begin{verbatim} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 721 | int ok; |
| 722 | int i, j; |
| 723 | long k, l; |
| 724 | char *s; |
| 725 | int size; |
| 726 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 727 | ok = PyArg_ParseTuple(args, ""); /* No arguments */ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 728 | /* Python call: f() */ |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 729 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 730 | ok = PyArg_ParseTuple(args, "s", &s); /* A string */ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 731 | /* Possible Python call: f('whoops!') */ |
| 732 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 733 | ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 734 | /* Possible Python call: f(1, 2, 'three') */ |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 735 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 736 | ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 737 | /* A pair of ints and a string, whose size is also returned */ |
Guido van Rossum | 7e924dd | 1997-02-10 16:51:52 +0000 | [diff] [blame] | 738 | /* Possible Python call: f((1, 2), 'three') */ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 739 | |
| 740 | { |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 741 | char *file; |
| 742 | char *mode = "r"; |
| 743 | int bufsize = 0; |
| 744 | ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize); |
| 745 | /* A string, and optionally another string and an integer */ |
| 746 | /* Possible Python calls: |
| 747 | f('spam') |
| 748 | f('spam', 'w') |
| 749 | f('spam', 'wb', 100000) */ |
| 750 | } |
| 751 | |
| 752 | { |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 753 | int left, top, right, bottom, h, v; |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 754 | ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)", |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 755 | &left, &top, &right, &bottom, &h, &v); |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 756 | /* A rectangle and a point */ |
| 757 | /* Possible Python call: |
| 758 | f(((0, 0), (400, 300)), (10, 10)) */ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 759 | } |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 760 | |
| 761 | { |
| 762 | Py_complex c; |
| 763 | ok = PyArg_ParseTuple(args, "D:myfunction", &c); |
| 764 | /* a complex, also providing a function name for errors */ |
| 765 | /* Possible Python call: myfunction(1+2j) */ |
| 766 | } |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 767 | \end{verbatim} |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 768 | |
| 769 | |
Fred Drake | e795718 | 1998-04-04 07:17:47 +0000 | [diff] [blame] | 770 | \section{Keyword Parsing with \cfunction{PyArg_ParseTupleAndKeywords()}} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 771 | \label{parseTupleAndKeywords} |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 772 | |
| 773 | The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as |
| 774 | follows: |
| 775 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 776 | \begin{verbatim} |
| 777 | int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict, |
| 778 | char *format, char **kwlist, ...); |
| 779 | \end{verbatim} |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 780 | |
| 781 | The \var{arg} and \var{format} parameters are identical to those of the |
| 782 | \cfunction{PyArg_ParseTuple()} function. The \var{kwdict} parameter |
| 783 | is the dictionary of keywords received as the third parameter from the |
| 784 | Python runtime. The \var{kwlist} parameter is a \NULL{}-terminated |
| 785 | list of strings which identify the parameters; the names are matched |
| 786 | with the type information from \var{format} from left to right. |
| 787 | |
| 788 | \strong{Note:} Nested tuples cannot be parsed when using keyword |
| 789 | arguments! Keyword parameters passed in which are not present in the |
Fred Drake | cd05ca9 | 1998-03-07 05:32:08 +0000 | [diff] [blame] | 790 | \var{kwlist} will cause \exception{TypeError} to be raised. |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 791 | |
| 792 | Here is an example module which uses keywords, based on an example by |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 793 | Geoff Philbrick (\email{philbrick@hks.com}):% |
| 794 | \index{Philbrick, Geoff} |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 795 | |
| 796 | \begin{verbatim} |
| 797 | #include <stdio.h> |
| 798 | #include "Python.h" |
| 799 | |
| 800 | static PyObject * |
| 801 | keywdarg_parrot(self, args, keywds) |
| 802 | PyObject *self; |
| 803 | PyObject *args; |
| 804 | PyObject *keywds; |
| 805 | { |
| 806 | int voltage; |
| 807 | char *state = "a stiff"; |
| 808 | char *action = "voom"; |
| 809 | char *type = "Norwegian Blue"; |
| 810 | |
| 811 | static char *kwlist[] = {"voltage", "state", "action", "type", NULL}; |
| 812 | |
| 813 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist, |
| 814 | &voltage, &state, &action, &type)) |
| 815 | return NULL; |
| 816 | |
| 817 | printf("-- This parrot wouldn't %s if you put %i Volts through it.\n", |
| 818 | action, voltage); |
| 819 | printf("-- Lovely plumage, the %s -- It's %s!\n", type, state); |
| 820 | |
| 821 | Py_INCREF(Py_None); |
| 822 | |
| 823 | return Py_None; |
| 824 | } |
| 825 | |
| 826 | static PyMethodDef keywdarg_methods[] = { |
| 827 | {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS|METH_KEYWORDS}, |
| 828 | {NULL, NULL} /* sentinel */ |
| 829 | }; |
| 830 | |
| 831 | void |
| 832 | initkeywdarg() |
| 833 | { |
| 834 | /* Create the module and add the functions */ |
Fred Drake | cd05ca9 | 1998-03-07 05:32:08 +0000 | [diff] [blame] | 835 | Py_InitModule("keywdarg", keywdarg_methods); |
Fred Drake | b6e5032 | 1998-02-04 20:26:31 +0000 | [diff] [blame] | 836 | } |
| 837 | \end{verbatim} |
| 838 | |
| 839 | |
Fred Drake | e795718 | 1998-04-04 07:17:47 +0000 | [diff] [blame] | 840 | \section{The \cfunction{Py_BuildValue()} Function} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 841 | \label{buildValue} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 842 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 843 | This function is the counterpart to \cfunction{PyArg_ParseTuple()}. It is |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 844 | declared as follows: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 845 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 846 | \begin{verbatim} |
| 847 | PyObject *Py_BuildValue(char *format, ...); |
| 848 | \end{verbatim} |
| 849 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 850 | It recognizes a set of format units similar to the ones recognized by |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 851 | \cfunction{PyArg_ParseTuple()}, but the arguments (which are input to the |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 852 | function, not output) must not be pointers, just values. It returns a |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 853 | new Python object, suitable for returning from a \C{} function called |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 854 | from Python. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 855 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 856 | One difference with \cfunction{PyArg_ParseTuple()}: while the latter |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 857 | requires its first argument to be a tuple (since Python argument lists |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 858 | are always represented as tuples internally), |
| 859 | \cfunction{Py_BuildValue()} does not always build a tuple. It builds |
| 860 | a tuple only if its format string contains two or more format units. |
| 861 | If the format string is empty, it returns \code{None}; if it contains |
| 862 | exactly one format unit, it returns whatever object is described by |
| 863 | that format unit. To force it to return a tuple of size 0 or one, |
| 864 | parenthesize the format string. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 865 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 866 | In the following description, the quoted form is the format unit; the |
| 867 | entry in (round) parentheses is the Python object type that the format |
| 868 | unit will return; and the entry in [square] brackets is the type of |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 869 | the \C{} value(s) to be passed. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 870 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 871 | The characters space, tab, colon and comma are ignored in format |
| 872 | strings (but not within format units such as \samp{s\#}). This can be |
| 873 | used to make long format strings a tad more readable. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 875 | \begin{description} |
| 876 | |
| 877 | \item[\samp{s} (string) {[char *]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 878 | Convert a null-terminated \C{} string to a Python object. If the \C{} |
| 879 | string pointer is \NULL{}, \code{None} is returned. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 880 | |
| 881 | \item[\samp{s\#} (string) {[char *, int]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 882 | Convert a \C{} string and its length to a Python object. If the \C{} string |
| 883 | pointer is \NULL{}, the length is ignored and \code{None} is |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 884 | returned. |
| 885 | |
| 886 | \item[\samp{z} (string or \code{None}) {[char *]}] |
| 887 | Same as \samp{s}. |
| 888 | |
| 889 | \item[\samp{z\#} (string or \code{None}) {[char *, int]}] |
| 890 | Same as \samp{s\#}. |
| 891 | |
| 892 | \item[\samp{i} (integer) {[int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 893 | Convert a plain \C{} \ctype{int} to a Python integer object. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 894 | |
| 895 | \item[\samp{b} (integer) {[char]}] |
| 896 | Same as \samp{i}. |
| 897 | |
| 898 | \item[\samp{h} (integer) {[short int]}] |
| 899 | Same as \samp{i}. |
| 900 | |
| 901 | \item[\samp{l} (integer) {[long int]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 902 | Convert a \C{} \ctype{long int} to a Python integer object. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 903 | |
| 904 | \item[\samp{c} (string of length 1) {[char]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 905 | Convert a \C{} \ctype{int} representing a character to a Python string of |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 906 | length 1. |
| 907 | |
| 908 | \item[\samp{d} (float) {[double]}] |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 909 | Convert a \C{} \ctype{double} to a Python floating point number. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 910 | |
| 911 | \item[\samp{f} (float) {[float]}] |
| 912 | Same as \samp{d}. |
| 913 | |
| 914 | \item[\samp{O} (object) {[PyObject *]}] |
| 915 | Pass a Python object untouched (except for its reference count, which |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 916 | is incremented by one). If the object passed in is a \NULL{} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 917 | pointer, it is assumed that this was caused because the call producing |
| 918 | the argument found an error and set an exception. Therefore, |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 919 | \cfunction{Py_BuildValue()} will return \NULL{} but won't raise an |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 920 | exception. If no exception has been raised yet, |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 921 | \cdata{PyExc_SystemError} is set. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 922 | |
| 923 | \item[\samp{S} (object) {[PyObject *]}] |
| 924 | Same as \samp{O}. |
| 925 | |
| 926 | \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}] |
| 927 | Convert \var{anything} to a Python object through a \var{converter} |
| 928 | function. The function is called with \var{anything} (which should be |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 929 | compatible with \ctype{void *}) as its argument and should return a |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 930 | ``new'' Python object, or \NULL{} if an error occurred. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 931 | |
| 932 | \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 933 | Convert a sequence of \C{} values to a Python tuple with the same number |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 934 | of items. |
| 935 | |
| 936 | \item[\samp{[\var{items}]} (list) {[\var{matching-items}]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 937 | Convert a sequence of \C{} values to a Python list with the same number |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 938 | of items. |
| 939 | |
| 940 | \item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}] |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 941 | Convert a sequence of \C{} values to a Python dictionary. Each pair of |
| 942 | consecutive \C{} values adds one item to the dictionary, serving as key |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 943 | and value, respectively. |
| 944 | |
| 945 | \end{description} |
| 946 | |
| 947 | If there is an error in the format string, the |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 948 | \cdata{PyExc_SystemError} exception is raised and \NULL{} returned. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 949 | |
| 950 | Examples (to the left the call, to the right the resulting Python value): |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 951 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 952 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 953 | Py_BuildValue("") None |
| 954 | Py_BuildValue("i", 123) 123 |
Guido van Rossum | f23e0fe | 1995-03-18 11:04:29 +0000 | [diff] [blame] | 955 | Py_BuildValue("iii", 123, 456, 789) (123, 456, 789) |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 956 | Py_BuildValue("s", "hello") 'hello' |
| 957 | Py_BuildValue("ss", "hello", "world") ('hello', 'world') |
| 958 | Py_BuildValue("s#", "hello", 4) 'hell' |
| 959 | Py_BuildValue("()") () |
| 960 | Py_BuildValue("(i)", 123) (123,) |
| 961 | Py_BuildValue("(ii)", 123, 456) (123, 456) |
| 962 | Py_BuildValue("(i,i)", 123, 456) (123, 456) |
| 963 | Py_BuildValue("[i,i]", 123, 456) [123, 456] |
Guido van Rossum | f23e0fe | 1995-03-18 11:04:29 +0000 | [diff] [blame] | 964 | Py_BuildValue("{s:i,s:i}", |
| 965 | "abc", 123, "def", 456) {'abc': 123, 'def': 456} |
| 966 | Py_BuildValue("((ii)(ii)) (ii)", |
| 967 | 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6)) |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 968 | \end{verbatim} |
| 969 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 970 | \section{Reference Counts} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 971 | \label{refcounts} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 972 | |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 973 | %\subsection{Introduction} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 974 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 975 | In languages like \C{} or \Cpp{}, the programmer is responsible for |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 976 | dynamic allocation and deallocation of memory on the heap. In \C{}, |
| 977 | this is done using the functions \cfunction{malloc()} and |
| 978 | \cfunction{free()}. In \Cpp{}, the operators \keyword{new} and |
| 979 | \keyword{delete} are used with essentially the same meaning; they are |
| 980 | actually implemented using \cfunction{malloc()} and |
| 981 | \cfunction{free()}, so we'll restrict the following discussion to the |
| 982 | latter. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 983 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 984 | Every block of memory allocated with \cfunction{malloc()} should |
| 985 | eventually be returned to the pool of available memory by exactly one |
| 986 | call to \cfunction{free()}. It is important to call |
| 987 | \cfunction{free()} at the right time. If a block's address is |
| 988 | forgotten but \cfunction{free()} is not called for it, the memory it |
| 989 | occupies cannot be reused until the program terminates. This is |
| 990 | called a \dfn{memory leak}. On the other hand, if a program calls |
| 991 | \cfunction{free()} for a block and then continues to use the block, it |
| 992 | creates a conflict with re-use of the block through another |
| 993 | \cfunction{malloc()} call. This is called \dfn{using freed memory}. |
| 994 | It has the same bad consequences as referencing uninitialized data --- |
| 995 | core dumps, wrong results, mysterious crashes. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 996 | |
| 997 | Common causes of memory leaks are unusual paths through the code. For |
| 998 | instance, a function may allocate a block of memory, do some |
| 999 | calculation, and then free the block again. Now a change in the |
| 1000 | requirements for the function may add a test to the calculation that |
| 1001 | detects an error condition and can return prematurely from the |
| 1002 | function. It's easy to forget to free the allocated memory block when |
| 1003 | taking this premature exit, especially when it is added later to the |
| 1004 | code. Such leaks, once introduced, often go undetected for a long |
| 1005 | time: the error exit is taken only in a small fraction of all calls, |
| 1006 | and most modern machines have plenty of virtual memory, so the leak |
| 1007 | only becomes apparent in a long-running process that uses the leaking |
| 1008 | function frequently. Therefore, it's important to prevent leaks from |
| 1009 | happening by having a coding convention or strategy that minimizes |
| 1010 | this kind of errors. |
| 1011 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1012 | Since Python makes heavy use of \cfunction{malloc()} and |
| 1013 | \cfunction{free()}, it needs a strategy to avoid memory leaks as well |
| 1014 | as the use of freed memory. The chosen method is called |
| 1015 | \dfn{reference counting}. The principle is simple: every object |
| 1016 | contains a counter, which is incremented when a reference to the |
| 1017 | object is stored somewhere, and which is decremented when a reference |
| 1018 | to it is deleted. When the counter reaches zero, the last reference |
| 1019 | to the object has been deleted and the object is freed. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1020 | |
| 1021 | An alternative strategy is called \dfn{automatic garbage collection}. |
| 1022 | (Sometimes, reference counting is also referred to as a garbage |
| 1023 | collection strategy, hence my use of ``automatic'' to distinguish the |
| 1024 | two.) The big advantage of automatic garbage collection is that the |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1025 | user doesn't need to call \cfunction{free()} explicitly. (Another claimed |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1026 | advantage is an improvement in speed or memory usage --- this is no |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1027 | hard fact however.) The disadvantage is that for \C{}, there is no |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1028 | truly portable automatic garbage collector, while reference counting |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1029 | can be implemented portably (as long as the functions \cfunction{malloc()} |
| 1030 | and \cfunction{free()} are available --- which the \C{} Standard guarantees). |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1031 | Maybe some day a sufficiently portable automatic garbage collector |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1032 | will be available for \C{}. Until then, we'll have to live with |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1033 | reference counts. |
| 1034 | |
| 1035 | \subsection{Reference Counting in Python} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1036 | \label{refcountsInPython} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1037 | |
| 1038 | There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)}, |
| 1039 | which handle the incrementing and decrementing of the reference count. |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1040 | \cfunction{Py_DECREF()} also frees the object when the count reaches zero. |
| 1041 | For flexibility, it doesn't call \cfunction{free()} directly --- rather, it |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1042 | makes a call through a function pointer in the object's \dfn{type |
| 1043 | object}. For this purpose (and others), every object also contains a |
| 1044 | pointer to its type object. |
| 1045 | |
| 1046 | The big question now remains: when to use \code{Py_INCREF(x)} and |
| 1047 | \code{Py_DECREF(x)}? Let's first introduce some terms. Nobody |
| 1048 | ``owns'' an object; however, you can \dfn{own a reference} to an |
| 1049 | object. An object's reference count is now defined as the number of |
| 1050 | owned references to it. The owner of a reference is responsible for |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1051 | calling \cfunction{Py_DECREF()} when the reference is no longer |
| 1052 | needed. Ownership of a reference can be transferred. There are three |
| 1053 | ways to dispose of an owned reference: pass it on, store it, or call |
| 1054 | \cfunction{Py_DECREF()}. Forgetting to dispose of an owned reference |
| 1055 | creates a memory leak. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1056 | |
| 1057 | It is also possible to \dfn{borrow}\footnote{The metaphor of |
| 1058 | ``borrowing'' a reference is not completely correct: the owner still |
| 1059 | has a copy of the reference.} a reference to an object. The borrower |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1060 | of a reference should not call \cfunction{Py_DECREF()}. The borrower must |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1061 | not hold on to the object longer than the owner from which it was |
| 1062 | borrowed. Using a borrowed reference after the owner has disposed of |
| 1063 | it risks using freed memory and should be avoided |
| 1064 | completely.\footnote{Checking that the reference count is at least 1 |
| 1065 | \strong{does not work} --- the reference count itself could be in |
| 1066 | freed memory and may thus be reused for another object!} |
| 1067 | |
| 1068 | The advantage of borrowing over owning a reference is that you don't |
| 1069 | need to take care of disposing of the reference on all possible paths |
| 1070 | through the code --- in other words, with a borrowed reference you |
| 1071 | don't run the risk of leaking when a premature exit is taken. The |
| 1072 | disadvantage of borrowing over leaking is that there are some subtle |
| 1073 | situations where in seemingly correct code a borrowed reference can be |
| 1074 | used after the owner from which it was borrowed has in fact disposed |
| 1075 | of it. |
| 1076 | |
| 1077 | A borrowed reference can be changed into an owned reference by calling |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1078 | \cfunction{Py_INCREF()}. This does not affect the status of the owner from |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1079 | which the reference was borrowed --- it creates a new owned reference, |
| 1080 | and gives full owner responsibilities (i.e., the new owner must |
| 1081 | dispose of the reference properly, as well as the previous owner). |
| 1082 | |
| 1083 | \subsection{Ownership Rules} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1084 | \label{ownershipRules} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1085 | |
| 1086 | Whenever an object reference is passed into or out of a function, it |
| 1087 | is part of the function's interface specification whether ownership is |
| 1088 | transferred with the reference or not. |
| 1089 | |
| 1090 | Most functions that return a reference to an object pass on ownership |
| 1091 | with the reference. In particular, all functions whose function it is |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1092 | to create a new object, e.g.\ \cfunction{PyInt_FromLong()} and |
| 1093 | \cfunction{Py_BuildValue()}, pass ownership to the receiver. Even if in |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1094 | fact, in some cases, you don't receive a reference to a brand new |
| 1095 | object, you still receive ownership of the reference. For instance, |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1096 | \cfunction{PyInt_FromLong()} maintains a cache of popular values and can |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1097 | return a reference to a cached item. |
| 1098 | |
| 1099 | Many functions that extract objects from other objects also transfer |
| 1100 | ownership with the reference, for instance |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1101 | \cfunction{PyObject_GetAttrString()}. The picture is less clear, here, |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1102 | however, since a few common routines are exceptions: |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1103 | \cfunction{PyTuple_GetItem()}, \cfunction{PyList_GetItem()}, |
| 1104 | \cfunction{PyDict_GetItem()}, and \cfunction{PyDict_GetItemString()} |
| 1105 | all return references that you borrow from the tuple, list or |
| 1106 | dictionary. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1107 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1108 | The function \cfunction{PyImport_AddModule()} also returns a borrowed |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1109 | reference, even though it may actually create the object it returns: |
| 1110 | this is possible because an owned reference to the object is stored in |
| 1111 | \code{sys.modules}. |
| 1112 | |
| 1113 | When you pass an object reference into another function, in general, |
| 1114 | the function borrows the reference from you --- if it needs to store |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1115 | it, it will use \cfunction{Py_INCREF()} to become an independent |
| 1116 | owner. There are exactly two important exceptions to this rule: |
| 1117 | \cfunction{PyTuple_SetItem()} and \cfunction{PyList_SetItem()}. These |
| 1118 | functions take over ownership of the item passed to them --- even if |
| 1119 | they fail! (Note that \cfunction{PyDict_SetItem()} and friends don't |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1120 | take over ownership --- they are ``normal.'') |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1121 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1122 | When a \C{} function is called from Python, it borrows references to its |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1123 | arguments from the caller. The caller owns a reference to the object, |
| 1124 | so the borrowed reference's lifetime is guaranteed until the function |
| 1125 | returns. Only when such a borrowed reference must be stored or passed |
| 1126 | on, it must be turned into an owned reference by calling |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1127 | \cfunction{Py_INCREF()}. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1128 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1129 | The object reference returned from a \C{} function that is called from |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1130 | Python must be an owned reference --- ownership is tranferred from the |
| 1131 | function to its caller. |
| 1132 | |
| 1133 | \subsection{Thin Ice} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1134 | \label{thinIce} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1135 | |
| 1136 | There are a few situations where seemingly harmless use of a borrowed |
| 1137 | reference can lead to problems. These all have to do with implicit |
| 1138 | invocations of the interpreter, which can cause the owner of a |
| 1139 | reference to dispose of it. |
| 1140 | |
| 1141 | The first and most important case to know about is using |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1142 | \cfunction{Py_DECREF()} on an unrelated object while borrowing a |
| 1143 | reference to a list item. For instance: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1144 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1145 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1146 | bug(PyObject *list) { |
| 1147 | PyObject *item = PyList_GetItem(list, 0); |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1148 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1149 | PyList_SetItem(list, 1, PyInt_FromLong(0L)); |
| 1150 | PyObject_Print(item, stdout, 0); /* BUG! */ |
| 1151 | } |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1152 | \end{verbatim} |
| 1153 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1154 | This function first borrows a reference to \code{list[0]}, then |
| 1155 | replaces \code{list[1]} with the value \code{0}, and finally prints |
| 1156 | the borrowed reference. Looks harmless, right? But it's not! |
| 1157 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1158 | Let's follow the control flow into \cfunction{PyList_SetItem()}. The list |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1159 | owns references to all its items, so when item 1 is replaced, it has |
| 1160 | to dispose of the original item 1. Now let's suppose the original |
| 1161 | item 1 was an instance of a user-defined class, and let's further |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1162 | suppose that the class defined a \method{__del__()} method. If this |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1163 | class instance has a reference count of 1, disposing of it will call |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1164 | its \method{__del__()} method. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1165 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1166 | Since it is written in Python, the \method{__del__()} method can execute |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1167 | arbitrary Python code. Could it perhaps do something to invalidate |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1168 | the reference to \code{item} in \cfunction{bug()}? You bet! Assuming |
| 1169 | that the list passed into \cfunction{bug()} is accessible to the |
| 1170 | \method{__del__()} method, it could execute a statement to the effect of |
| 1171 | \samp{del list[0]}, and assuming this was the last reference to that |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1172 | object, it would free the memory associated with it, thereby |
| 1173 | invalidating \code{item}. |
| 1174 | |
| 1175 | The solution, once you know the source of the problem, is easy: |
| 1176 | temporarily increment the reference count. The correct version of the |
| 1177 | function reads: |
| 1178 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1179 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1180 | no_bug(PyObject *list) { |
| 1181 | PyObject *item = PyList_GetItem(list, 0); |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1182 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1183 | Py_INCREF(item); |
| 1184 | PyList_SetItem(list, 1, PyInt_FromLong(0L)); |
| 1185 | PyObject_Print(item, stdout, 0); |
| 1186 | Py_DECREF(item); |
| 1187 | } |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1188 | \end{verbatim} |
| 1189 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1190 | This is a true story. An older version of Python contained variants |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1191 | of this bug and someone spent a considerable amount of time in a \C{} |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1192 | debugger to figure out why his \method{__del__()} methods would fail... |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1193 | |
| 1194 | The second case of problems with a borrowed reference is a variant |
| 1195 | involving threads. Normally, multiple threads in the Python |
| 1196 | interpreter can't get in each other's way, because there is a global |
| 1197 | lock protecting Python's entire object space. However, it is possible |
| 1198 | to temporarily release this lock using the macro |
| 1199 | \code{Py_BEGIN_ALLOW_THREADS}, and to re-acquire it using |
| 1200 | \code{Py_END_ALLOW_THREADS}. This is common around blocking I/O |
| 1201 | calls, to let other threads use the CPU while waiting for the I/O to |
| 1202 | complete. Obviously, the following function has the same problem as |
| 1203 | the previous one: |
| 1204 | |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1205 | \begin{verbatim} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1206 | bug(PyObject *list) { |
| 1207 | PyObject *item = PyList_GetItem(list, 0); |
| 1208 | Py_BEGIN_ALLOW_THREADS |
| 1209 | ...some blocking I/O call... |
| 1210 | Py_END_ALLOW_THREADS |
| 1211 | PyObject_Print(item, stdout, 0); /* BUG! */ |
| 1212 | } |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1213 | \end{verbatim} |
| 1214 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1215 | \subsection{NULL Pointers} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1216 | \label{nullPointers} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1217 | |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1218 | In general, functions that take object references as arguments do not |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1219 | expect you to pass them \NULL{} pointers, and will dump core (or |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1220 | cause later core dumps) if you do so. Functions that return object |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1221 | references generally return \NULL{} only to indicate that an |
| 1222 | exception occurred. The reason for not testing for \NULL{} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1223 | arguments is that functions often pass the objects they receive on to |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1224 | other function --- if each function were to test for \NULL{}, |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1225 | there would be a lot of redundant tests and the code would run slower. |
| 1226 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1227 | It is better to test for \NULL{} only at the ``source'', i.e.\ |
| 1228 | when a pointer that may be \NULL{} is received, e.g.\ from |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1229 | \cfunction{malloc()} or from a function that may raise an exception. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1230 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1231 | The macros \cfunction{Py_INCREF()} and \cfunction{Py_DECREF()} |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1232 | do not check for \NULL{} pointers --- however, their variants |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1233 | \cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()} do. |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1234 | |
| 1235 | The macros for checking for a particular object type |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1236 | (\code{Py\var{type}_Check()}) don't check for \NULL{} pointers --- |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1237 | again, there is much code that calls several of these in a row to test |
| 1238 | an object against various different expected types, and this would |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1239 | generate redundant tests. There are no variants with \NULL{} |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1240 | checking. |
| 1241 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1242 | The \C{} function calling mechanism guarantees that the argument list |
| 1243 | passed to \C{} functions (\code{args} in the examples) is never |
| 1244 | \NULL{} --- in fact it guarantees that it is always a tuple.% |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1245 | \footnote{These guarantees don't hold when you use the ``old'' style |
| 1246 | calling convention --- this is still found in much existing code.} |
| 1247 | |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1248 | It is a severe error to ever let a \NULL{} pointer ``escape'' to |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1249 | the Python user. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1251 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1252 | \section{Writing Extensions in \Cpp{}} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1253 | \label{cplusplus} |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 1254 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1255 | It is possible to write extension modules in \Cpp{}. Some restrictions |
Guido van Rossum | ed39cd0 | 1995-10-08 00:17:19 +0000 | [diff] [blame] | 1256 | apply. If the main program (the Python interpreter) is compiled and |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1257 | linked by the \C{} compiler, global or static objects with constructors |
Guido van Rossum | ed39cd0 | 1995-10-08 00:17:19 +0000 | [diff] [blame] | 1258 | cannot be used. This is not a problem if the main program is linked |
Guido van Rossum | afcd589 | 1998-02-05 19:59:39 +0000 | [diff] [blame] | 1259 | by the \Cpp{} compiler. Functions that will be called by the |
| 1260 | Python interpreter (in particular, module initalization functions) |
| 1261 | have to be declared using \code{extern "C"}. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 1262 | It is unnecessary to enclose the Python header files in |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1263 | \code{extern "C" \{...\}} --- they use this form already if the symbol |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1264 | \samp{__cplusplus} is defined (all recent \Cpp{} compilers define this |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1265 | symbol). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1266 | |
| 1267 | \chapter{Embedding Python in another application} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1268 | \label{embedding} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1269 | |
| 1270 | Embedding Python is similar to extending it, but not quite. The |
| 1271 | difference is that when you extend Python, the main program of the |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1272 | application is still the Python interpreter, while if you embed |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 1273 | Python, the main program may have nothing to do with Python --- |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1274 | instead, some parts of the application occasionally call the Python |
| 1275 | interpreter to run some Python code. |
| 1276 | |
| 1277 | So if you are embedding Python, you are providing your own main |
| 1278 | program. One of the things this main program has to do is initialize |
| 1279 | the Python interpreter. At the very least, you have to call the |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1280 | function \cfunction{Py_Initialize()}. There are optional calls to |
| 1281 | pass command line arguments to Python. Then later you can call the |
| 1282 | interpreter from any part of the application. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1283 | |
| 1284 | There are several different ways to call the interpreter: you can pass |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1285 | a string containing Python statements to |
| 1286 | \cfunction{PyRun_SimpleString()}, or you can pass a stdio file pointer |
| 1287 | and a file name (for identification in error messages only) to |
| 1288 | \cfunction{PyRun_SimpleFile()}. You can also call the lower-level |
| 1289 | operations described in the previous chapters to construct and use |
| 1290 | Python objects. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1291 | |
| 1292 | A simple demo of embedding Python can be found in the directory |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1293 | \file{Demo/embed}. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 1294 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1295 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1296 | \section{Embedding Python in \Cpp{}} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1297 | \label{embeddingInCplusplus} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1298 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1299 | It is also possible to embed Python in a \Cpp{} program; precisely how this |
| 1300 | is done will depend on the details of the \Cpp{} system used; in general you |
| 1301 | will need to write the main program in \Cpp{}, and use the \Cpp{} compiler |
| 1302 | to compile and link your program. There is no need to recompile Python |
| 1303 | itself using \Cpp{}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1304 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1305 | |
| 1306 | \chapter{Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1307 | \label{dynload} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1308 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1309 | On most modern systems it is possible to configure Python to support |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1310 | dynamic loading of extension modules implemented in \C{}. When shared |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1311 | libraries are used dynamic loading is configured automatically; |
| 1312 | otherwise you have to select it as a build option (see below). Once |
| 1313 | configured, dynamic loading is trivial to use: when a Python program |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1314 | executes \code{import spam}, the search for modules tries to find a |
| 1315 | file \file{spammodule.o} (\file{spammodule.so} when using shared |
Fred Drake | b789c70 | 1998-04-02 16:19:15 +0000 | [diff] [blame] | 1316 | libraries) in the module search path,% |
| 1317 | \indexiii{module}{search}{path} |
| 1318 | and if one is found, it is loaded into the executing binary and |
| 1319 | executed. Once loaded, the module acts just like a built-in extension |
| 1320 | module. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1321 | |
Guido van Rossum | b92112d | 1995-03-20 14:24:09 +0000 | [diff] [blame] | 1322 | The advantages of dynamic loading are twofold: the ``core'' Python |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1323 | binary gets smaller, and users can extend Python with their own |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1324 | modules implemented in \C{} without having to build and maintain their |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1325 | own copy of the Python interpreter. There are also disadvantages: |
| 1326 | dynamic loading isn't available on all systems (this just means that |
| 1327 | on some systems you have to use static loading), and dynamically |
| 1328 | loading a module that was compiled for a different version of Python |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1329 | (e.g. with a different representation of objects) may dump core. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1330 | |
| 1331 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1332 | \section{Configuring and Building the Interpreter for Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1333 | \label{dynloadConfig} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1335 | There are three styles of dynamic loading: one using shared libraries, |
| 1336 | one using SGI IRIX 4 dynamic loading, and one using GNU dynamic |
| 1337 | loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1338 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1339 | \subsection{Shared Libraries} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1340 | \label{sharedlibs} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1341 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 1342 | The following systems support dynamic loading using shared libraries: |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1343 | SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!), Linux, FreeBSD, |
| 1344 | NetBSD; and probably all systems derived from SVR4, or at least those |
| 1345 | SVR4 derivatives that support shared libraries (are there any that |
| 1346 | don't?). |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1347 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1348 | You don't need to do anything to configure dynamic loading on these |
| 1349 | systems --- the \file{configure} detects the presence of the |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1350 | \code{<dlfcn.h>} header file and automatically configures dynamic |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1351 | loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1352 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1353 | \subsection{SGI IRIX 4 Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1354 | \label{irixDynload} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1355 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1356 | Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic |
| 1357 | loading. (SGI IRIX 5 might also support it but it is inferior to |
| 1358 | using shared libraries so there is no reason to; a small test didn't |
| 1359 | work right away so I gave up trying to support it.) |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1360 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1361 | Before you build Python, you first need to fetch and build the |
| 1362 | \code{dl} package written by Jack Jansen. This is available by |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1363 | anonymous ftp from \url{ftp://ftp.cwi.nl/pub/dynload/}, file |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1364 | \file{dl-1.6.tar.Z}. (The version number may change.) Follow the |
| 1365 | instructions in the package's \file{README} file to build it. |
| 1366 | |
| 1367 | Once you have built \code{dl}, you can configure Python to use it. To |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1368 | this end, you run the \program{configure} script with the option |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1369 | \code{--with-dl=\var{directory}} where \var{directory} is the absolute |
| 1370 | pathname of the \code{dl} directory. |
| 1371 | |
| 1372 | Now build and install Python as you normally would (see the |
| 1373 | \file{README} file in the toplevel Python directory.) |
| 1374 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1375 | \subsection{GNU Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1376 | \label{gnuDynload} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1377 | |
| 1378 | GNU dynamic loading supports (according to its \file{README} file) the |
| 1379 | following hardware and software combinations: VAX (Ultrix), Sun 3 |
| 1380 | (SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and |
| 1381 | Atari ST. There is no reason to use it on a Sparc; I haven't seen a |
| 1382 | Sun 3 for years so I don't know if these have shared libraries or not. |
| 1383 | |
Guido van Rossum | 7e924dd | 1997-02-10 16:51:52 +0000 | [diff] [blame] | 1384 | You need to fetch and build two packages. |
| 1385 | One is GNU DLD. All development of this code has been done with DLD |
Fred Drake | ca6567f | 1998-01-22 20:44:18 +0000 | [diff] [blame] | 1386 | version 3.2.3, which is available by anonymous ftp from |
| 1387 | \url{ftp://ftp.cwi.nl/pub/dynload}, file |
Guido van Rossum | 7e924dd | 1997-02-10 16:51:52 +0000 | [diff] [blame] | 1388 | \file{dld-3.2.3.tar.Z}. (A more recent version of DLD is available |
Fred Drake | ca6567f | 1998-01-22 20:44:18 +0000 | [diff] [blame] | 1389 | via \url{http://www-swiss.ai.mit.edu/~jaffer/DLD.html} but this has |
Guido van Rossum | 7e924dd | 1997-02-10 16:51:52 +0000 | [diff] [blame] | 1390 | not been tested.) |
| 1391 | The other package needed is an |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1392 | emulation of Jack Jansen's \code{dl} package that I wrote on top of |
| 1393 | GNU DLD 3.2.3. This is available from the same host and directory, |
Guido van Rossum | 98046b9 | 1997-08-14 19:50:18 +0000 | [diff] [blame] | 1394 | file \file{dl-dld-1.1.tar.Z}. (The version number may change --- but I doubt |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1395 | it will.) Follow the instructions in each package's \file{README} |
Guido van Rossum | 98046b9 | 1997-08-14 19:50:18 +0000 | [diff] [blame] | 1396 | file to configure and build them. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1397 | |
| 1398 | Now configure Python. Run the \file{configure} script with the option |
| 1399 | \code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where |
| 1400 | \var{dl-directory} is the absolute pathname of the directory where you |
| 1401 | have built the \file{dl-dld} package, and \var{dld-directory} is that |
| 1402 | of the GNU DLD package. The Python interpreter you build hereafter |
| 1403 | will support GNU dynamic loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1404 | |
| 1405 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1406 | \section{Building a Dynamically Loadable Module} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1407 | \label{makedynload} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1408 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1409 | Since there are three styles of dynamic loading, there are also three |
| 1410 | groups of instructions for building a dynamically loadable module. |
| 1411 | Instructions common for all three styles are given first. Assuming |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1412 | your module is called \module{spam}, the source filename must be |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1413 | \file{spammodule.c}, so the object name is \file{spammodule.o}. The |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1414 | module must be written as a normal Python extension module (as |
| 1415 | described earlier). |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1416 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1417 | Note that in all cases you will have to create your own Makefile that |
| 1418 | compiles your module file(s). This Makefile will have to pass two |
Fred Drake | 0fd8268 | 1998-01-09 05:39:38 +0000 | [diff] [blame] | 1419 | \samp{-I} arguments to the \C{} compiler which will make it find the |
Fred Drake | b789c70 | 1998-04-02 16:19:15 +0000 | [diff] [blame] | 1420 | Python header files. If the Make variable \makevar{PYTHONTOP} points to |
| 1421 | the toplevel Python directory, your \makevar{CFLAGS} Make variable should |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1422 | contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}. |
Fred Drake | b789c70 | 1998-04-02 16:19:15 +0000 | [diff] [blame] | 1423 | (Most header files are in the \file{Include/} subdirectory, but the |
Guido van Rossum | 305ed11 | 1996-08-19 22:59:46 +0000 | [diff] [blame] | 1424 | \file{config.h} header lives in the toplevel directory.) |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1425 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1426 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1427 | \subsection{Shared Libraries} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1428 | \label{linking} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1429 | |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 1430 | You must link the \file{.o} file to produce a shared library. This is |
| 1431 | done using a special invocation of the \UNIX{} loader/linker, |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1432 | \manpage{ld}{1}. Unfortunately the invocation differs slightly per |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 1433 | system. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1434 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1435 | On SunOS 4, use |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1436 | \begin{verbatim} |
| 1437 | ld spammodule.o -o spammodule.so |
| 1438 | \end{verbatim} |
| 1439 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1440 | On Solaris 2, use |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1441 | \begin{verbatim} |
| 1442 | ld -G spammodule.o -o spammodule.so |
| 1443 | \end{verbatim} |
| 1444 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1445 | On SGI IRIX 5, use |
Fred Drake | 1e11a5c | 1998-02-13 07:11:32 +0000 | [diff] [blame] | 1446 | \begin{verbatim} |
| 1447 | ld -shared spammodule.o -o spammodule.so |
| 1448 | \end{verbatim} |
| 1449 | |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1450 | On other systems, consult the manual page for \manpage{ld}{1} to find |
| 1451 | what flags, if any, must be used. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1452 | |
| 1453 | If your extension module uses system libraries that haven't already |
| 1454 | been linked with Python (e.g. a windowing system), these must be |
Fred Drake | d7bb303 | 1998-03-03 17:52:07 +0000 | [diff] [blame] | 1455 | passed to the \program{ld} command as \samp{-l} options after the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1456 | \samp{.o} file. |
| 1457 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1458 | The resulting file \file{spammodule.so} must be copied into a directory |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1459 | along the Python module search path. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1460 | |
| 1461 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1462 | \subsection{SGI IRIX 4 Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1463 | \label{irixLinking} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1464 | |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 1465 | \strong{IMPORTANT:} You must compile your extension module with the |
Fred Drake | a0dbddf | 1998-04-02 06:50:02 +0000 | [diff] [blame] | 1466 | additional \C{} flag \samp{-G0} (or \samp{-G 0}). This instructs the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1467 | assembler to generate position-independent code. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1468 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1469 | You don't need to link the resulting \file{spammodule.o} file; just |
Fred Drake | b789c70 | 1998-04-02 16:19:15 +0000 | [diff] [blame] | 1470 | copy it into a directory along the Python module search path.% |
| 1471 | \indexiii{module}{search}{path} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1472 | |
| 1473 | The first time your extension is loaded, it takes some extra time and |
| 1474 | a few messages may be printed. This creates a file |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1475 | \file{spammodule.ld} which is an image that can be loaded quickly into |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1476 | the Python interpreter process. When a new Python interpreter is |
| 1477 | installed, the \code{dl} package detects this and rebuilds |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1478 | \file{spammodule.ld}. The file \file{spammodule.ld} is placed in the |
| 1479 | directory where \file{spammodule.o} was found, unless this directory is |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1480 | unwritable; in that case it is placed in a temporary |
| 1481 | directory.\footnote{Check the manual page of the \code{dl} package for |
| 1482 | details.} |
| 1483 | |
| 1484 | If your extension modules uses additional system libraries, you must |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1485 | create a file \file{spammodule.libs} in the same directory as the |
| 1486 | \file{spammodule.o}. This file should contain one or more lines with |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1487 | whitespace-separated options that will be passed to the linker --- |
| 1488 | normally only \samp{-l} options or absolute pathnames of libraries |
| 1489 | (\samp{.a} files) should be used. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1490 | |
| 1491 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1492 | \subsection{GNU Dynamic Loading} |
Fred Drake | 3da06a6 | 1998-02-26 18:49:12 +0000 | [diff] [blame] | 1493 | \label{gnuLinking} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1494 | |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1495 | Just copy \file{spammodule.o} into a directory along the Python module |
Fred Drake | b789c70 | 1998-04-02 16:19:15 +0000 | [diff] [blame] | 1496 | search path.% |
| 1497 | \indexiii{module}{search}{path} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1498 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1499 | If your extension modules uses additional system libraries, you must |
Guido van Rossum | 5049bcb | 1995-03-13 16:55:23 +0000 | [diff] [blame] | 1500 | create a file \file{spammodule.libs} in the same directory as the |
| 1501 | \file{spammodule.o}. This file should contain one or more lines with |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1502 | whitespace-separated absolute pathnames of libraries (\samp{.a} |
| 1503 | files). No \samp{-l} options can be used. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1504 | |
| 1505 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1506 | \end{document} |