Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1 | \documentstyle[twoside,11pt,myformat]{report} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 3 | \title{Extending and Embedding the Python Interpreter} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 4 | |
| 5 | \author{ |
| 6 | Guido van Rossum \\ |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 7 | Dept. CST, CWI, P.O. Box 94079 \\ |
| 8 | 1090 GB Amsterdam, The Netherlands \\ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 9 | E-mail: {\tt guido@cwi.nl} |
| 10 | } |
| 11 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 12 | \date{14 July 1994 \\ Release 1.0.3} % XXX update before release! |
Guido van Rossum | 83eb962 | 1993-11-23 16:28:45 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 14 | % Tell \index to actually write the .idx file |
| 15 | \makeindex |
| 16 | |
| 17 | \begin{document} |
| 18 | |
| 19 | \pagenumbering{roman} |
| 20 | |
| 21 | \maketitle |
| 22 | |
| 23 | \begin{abstract} |
| 24 | |
| 25 | \noindent |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 26 | This document describes how to write modules in C or \Cpp{} to extend the |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 27 | Python interpreter. It also describes how to use Python as an |
| 28 | `embedded' language, and how extension modules can be loaded |
| 29 | dynamically (at run time) into the interpreter, if the operating |
| 30 | system supports this feature. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 31 | |
| 32 | \end{abstract} |
| 33 | |
| 34 | \pagebreak |
| 35 | |
| 36 | { |
| 37 | \parskip = 0mm |
| 38 | \tableofcontents |
| 39 | } |
| 40 | |
| 41 | \pagebreak |
| 42 | |
| 43 | \pagenumbering{arabic} |
| 44 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 46 | \chapter{Extending Python with C or \Cpp{} code} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 48 | |
| 49 | \section{Introduction} |
| 50 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 51 | It is quite easy to add non-standard built-in modules to Python, if |
| 52 | you know how to program in C. A built-in module known to the Python |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 53 | programmer as \code{foo} is generally implemented by a file called |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 54 | \file{foomodule.c}. All but the two most essential standard built-in |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 55 | modules also adhere to this convention, and in fact some of them form |
| 56 | excellent examples of how to create an extension. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 57 | |
| 58 | Extension modules can do two things that can't be done directly in |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 59 | Python: they can implement new data types (which are different from |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 60 | classes, by the way), and they can make system calls or call C library |
| 61 | functions. We'll see how both types of extension are implemented by |
| 62 | examining the code for a Python curses interface. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 63 | |
| 64 | Note: unless otherwise mentioned, all file references in this |
| 65 | document are relative to the toplevel directory of the Python |
| 66 | distribution --- i.e. the directory that contains the \file{configure} |
| 67 | script. |
| 68 | |
| 69 | The compilation of an extension module depends on your system setup |
| 70 | and the intended use of the module; details are given in a later |
| 71 | section. |
| 72 | |
| 73 | |
| 74 | \section{A first look at the code} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 75 | |
| 76 | It is important not to be impressed by the size and complexity of |
| 77 | the average extension module; much of this is straightforward |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 78 | `boilerplate' code (starting right with the copyright notice)! |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 80 | Let's skip the boilerplate and have a look at an interesting function |
| 81 | in \file{posixmodule.c} first: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 82 | |
| 83 | \begin{verbatim} |
| 84 | static object * |
| 85 | posix_system(self, args) |
| 86 | object *self; |
| 87 | object *args; |
| 88 | { |
| 89 | char *command; |
| 90 | int sts; |
| 91 | if (!getargs(args, "s", &command)) |
| 92 | return NULL; |
| 93 | sts = system(command); |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 94 | return mkvalue("i", sts); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 95 | } |
| 96 | \end{verbatim} |
| 97 | |
| 98 | This is the prototypical top-level function in an extension module. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 99 | It will be called (we'll see later how) when the Python program |
| 100 | executes statements like |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 101 | |
| 102 | \begin{verbatim} |
| 103 | >>> import posix |
| 104 | >>> sts = posix.system('ls -l') |
| 105 | \end{verbatim} |
| 106 | |
| 107 | There is a straightforward translation from the arguments to the call |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 108 | in Python (here the single expression \code{'ls -l'}) to the arguments that |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 109 | are passed to the C function. The C function always has two |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 110 | parameters, conventionally named \var{self} and \var{args}. The |
| 111 | \var{self} argument is used when the C function implements a builtin |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 112 | method---this will be discussed later. |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 113 | In the example, \var{self} will always be a \code{NULL} pointer, since |
| 114 | we are defining a function, not a method (this is done so that the |
| 115 | interpreter doesn't have to understand two different types of C |
| 116 | functions). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 118 | The \var{args} parameter will be a pointer to a Python object, or |
| 119 | \code{NULL} if the Python function/method was called without |
| 120 | arguments. It is necessary to do full argument type checking on each |
| 121 | call, since otherwise the Python user would be able to cause the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 122 | Python interpreter to `dump core' by passing invalid arguments to a |
| 123 | function in an extension module. Because argument checking and |
| 124 | converting arguments to C are such common tasks, there's a general |
| 125 | function in the Python interpreter that combines them: |
| 126 | \code{getargs()}. It uses a template string to determine both the |
| 127 | types of the Python argument and the types of the C variables into |
| 128 | which it should store the converted values.\footnote{There are |
| 129 | convenience macros \code{getnoarg()}, \code{getstrarg()}, |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 130 | \code{getintarg()}, etc., for many common forms of \code{getargs()} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 131 | templates. These are relics from the past; the recommended practice |
| 132 | is to call \code{getargs()} directly.} (More about this later.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 133 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 134 | If \code{getargs()} returns nonzero, the argument list has the right |
| 135 | type and its components have been stored in the variables whose |
| 136 | addresses are passed. If it returns zero, an error has occurred. In |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 137 | the latter case it has already raised an appropriate exception by so |
| 138 | the calling function should return \code{NULL} immediately --- see the |
| 139 | next section. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 140 | |
| 141 | |
| 142 | \section{Intermezzo: errors and exceptions} |
| 143 | |
| 144 | An important convention throughout the Python interpreter is the |
| 145 | following: when a function fails, it should set an exception condition |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 146 | and return an error value (often a \code{NULL} pointer). Exceptions |
| 147 | are stored in a static global variable in \file{Python/errors.c}; if |
| 148 | this variable is \code{NULL} no exception has occurred. A second |
| 149 | static global variable stores the `associated value' of the exception |
| 150 | --- the second argument to \code{raise}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 152 | The file \file{errors.h} declares a host of functions to set various |
| 153 | types of exceptions. The most common one is \code{err_setstr()} --- |
| 154 | its arguments are an exception object (e.g. \code{RuntimeError} --- |
| 155 | actually it can be any string object) and a C string indicating the |
| 156 | cause of the error (this is converted to a string object and stored as |
| 157 | the `associated value' of the exception). Another useful function is |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 158 | \code{err_errno()}, which only takes an exception argument and |
| 159 | constructs the associated value by inspection of the (UNIX) global |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 160 | variable errno. The most general function is \code{err_set()}, which |
| 161 | takes two object arguments, the exception and its associated value. |
| 162 | You don't need to \code{INCREF()} the objects passed to any of these |
| 163 | functions. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 164 | |
| 165 | You can test non-destructively whether an exception has been set with |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 166 | \code{err_occurred()}. However, most code never calls |
| 167 | \code{err_occurred()} to see whether an error occurred or not, but |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 168 | relies on error return values from the functions it calls instead. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 169 | |
| 170 | When a function that calls another function detects that the called |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 171 | function fails, it should return an error value (e.g. \code{NULL} or |
| 172 | \code{-1}) but not call one of the \code{err_*} functions --- one has |
| 173 | already been called. The caller is then supposed to also return an |
| 174 | error indication to {\em its} caller, again {\em without} calling |
| 175 | \code{err_*()}, and so on --- the most detailed cause of the error was |
| 176 | already reported by the function that first detected it. Once the |
| 177 | error has reached Python's interpreter main loop, this aborts the |
| 178 | currently executing Python code and tries to find an exception handler |
| 179 | specified by the Python programmer. |
| 180 | |
| 181 | (There are situations where a module can actually give a more detailed |
| 182 | error message by calling another \code{err_*} function, and in such |
| 183 | cases it is fine to do so. As a general rule, however, this is not |
| 184 | necessary, and can cause information about the cause of the error to |
| 185 | be lost: most operations can fail for a variety of reasons.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 186 | |
| 187 | To ignore an exception set by a function call that failed, the |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 188 | exception condition must be cleared explicitly by calling |
| 189 | \code{err_clear()}. The only time C code should call |
| 190 | \code{err_clear()} is if it doesn't want to pass the error on to the |
| 191 | interpreter but wants to handle it completely by itself (e.g. by |
| 192 | trying something else or pretending nothing happened). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 194 | Finally, the function \code{err_get()} gives you both error variables |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 195 | {\em and clears them}. Note that even if an error occurred the second |
| 196 | one may be \code{NULL}. You have to \code{XDECREF()} both when you |
| 197 | are finished with them. I doubt you will need to use this function. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 199 | Note that a failing \code{malloc()} call must also be turned into an |
| 200 | exception --- the direct caller of \code{malloc()} (or |
| 201 | \code{realloc()}) must call \code{err_nomem()} and return a failure |
| 202 | indicator itself. All the object-creating functions |
| 203 | (\code{newintobject()} etc.) already do this, so only if you call |
| 204 | \code{malloc()} directly this note is of importance. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 206 | Also note that, with the important exception of \code{getargs()}, |
| 207 | functions that return an integer status usually return \code{0} or a |
| 208 | positive value for success and \code{-1} for failure. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 210 | Finally, be careful about cleaning up garbage (making \code{XDECREF()} |
| 211 | or \code{DECREF()} calls for objects you have already created) when |
| 212 | you return an error! |
| 213 | |
| 214 | The choice of which exception to raise is entirely yours. There are |
| 215 | predeclared C objects corresponding to all built-in Python exceptions, |
| 216 | e.g. \code{ZeroDevisionError} which you can use directly. Of course, |
| 217 | you should chose exceptions wisely --- don't use \code{TypeError} to |
| 218 | mean that a file couldn't be opened (that should probably be |
| 219 | \code{IOError}). If anything's wrong with the argument list the |
| 220 | \code{getargs()} function raises \code{TypeError}. If you have an |
| 221 | argument whose value which must be in a particular range or must |
| 222 | satisfy other conditions, \code{ValueError} is appropriate. |
| 223 | |
| 224 | You can also define a new exception that is unique to your module. |
| 225 | For this, you usually declare a static object variable at the |
| 226 | beginning of your file, e.g. |
| 227 | |
| 228 | \begin{verbatim} |
| 229 | static object *FooError; |
| 230 | \end{verbatim} |
| 231 | |
| 232 | and initialize it in your module's initialization function |
| 233 | (\code{initfoo()}) with a string object, e.g. (leaving out the error |
| 234 | checking for simplicity): |
| 235 | |
| 236 | \begin{verbatim} |
| 237 | void |
| 238 | initfoo() |
| 239 | { |
| 240 | object *m, *d; |
| 241 | m = initmodule("foo", foo_methods); |
| 242 | d = getmoduledict(m); |
| 243 | FooError = newstringobject("foo.error"); |
| 244 | dictinsert(d, "error", FooError); |
| 245 | } |
| 246 | \end{verbatim} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 247 | |
| 248 | |
| 249 | \section{Back to the example} |
| 250 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 251 | Going back to \code{posix_system()}, you should now be able to |
| 252 | understand this bit: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 253 | |
| 254 | \begin{verbatim} |
| 255 | if (!getargs(args, "s", &command)) |
| 256 | return NULL; |
| 257 | \end{verbatim} |
| 258 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 259 | It returns \code{NULL} (the error indicator for functions of this |
| 260 | kind) if an error is detected in the argument list, relying on the |
| 261 | exception set by \code{getargs()}. Otherwise the string value of the |
| 262 | argument has been copied to the local variable \code{command} --- this |
| 263 | is in fact just a pointer assignment and you are not supposed to |
| 264 | modify the string to which it points. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 265 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 266 | If a function is called with multiple arguments, the argument list |
| 267 | (the argument \code{args}) is turned into a tuple. If it is called |
| 268 | without arguments, \code{args} is \code{NULL}. \code{getargs()} knows |
| 269 | about this; see later. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 271 | The next statement in \code{posix_system()} is a call to the C library |
| 272 | function \code{system()}, passing it the string we just got from |
| 273 | \code{getargs()}: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 274 | |
| 275 | \begin{verbatim} |
| 276 | sts = system(command); |
| 277 | \end{verbatim} |
| 278 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 279 | Finally, \code{posix.system()} must return a value: the integer status |
| 280 | returned by the C library \code{system()} function. This is done |
| 281 | using the function \code{mkvalue()}, which is something like the |
| 282 | inverse of \code{getargs()}: it takes a format string and a variable |
| 283 | number of C values and returns a new Python object. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 284 | |
| 285 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 286 | return mkvalue("i", sts); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 287 | \end{verbatim} |
| 288 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 289 | In this case, it returns an integer object (yes, even integers are |
| 290 | objects on the heap in Python!). More info on \code{mkvalue()} is |
| 291 | given later. |
| 292 | |
| 293 | If you had a function that returned no useful argument (a.k.a. a |
| 294 | procedure), you would need this idiom: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 295 | |
| 296 | \begin{verbatim} |
| 297 | INCREF(None); |
| 298 | return None; |
| 299 | \end{verbatim} |
| 300 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 301 | \code{None} is a unique Python object representing `no value'. It |
| 302 | differs from \code{NULL}, which means `error' in most contexts. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 303 | |
| 304 | |
| 305 | \section{The module's function table} |
| 306 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 307 | I promised to show how I made the function \code{posix_system()} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 308 | callable from Python programs. This is shown later in |
| 309 | \file{Modules/posixmodule.c}: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 310 | |
| 311 | \begin{verbatim} |
| 312 | static struct methodlist posix_methods[] = { |
| 313 | ... |
| 314 | {"system", posix_system}, |
| 315 | ... |
| 316 | {NULL, NULL} /* Sentinel */ |
| 317 | }; |
| 318 | |
| 319 | void |
| 320 | initposix() |
| 321 | { |
| 322 | (void) initmodule("posix", posix_methods); |
| 323 | } |
| 324 | \end{verbatim} |
| 325 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 326 | (The actual \code{initposix()} is somewhat more complicated, but many |
| 327 | extension modules can be as simple as shown here.) When the Python |
| 328 | program first imports module \code{posix}, \code{initposix()} is |
| 329 | called, which calls \code{initmodule()} with specific parameters. |
| 330 | This creates a `module object' (which is inserted in the table |
| 331 | \code{sys.modules} under the key \code{'posix'}), and adds |
| 332 | built-in-function objects to the newly created module based upon the |
| 333 | table (of type struct methodlist) that was passed as its second |
| 334 | parameter. The function \code{initmodule()} returns a pointer to the |
| 335 | module object that it creates (which is unused here). It aborts with |
| 336 | a fatal error if the module could not be initialized satisfactorily, |
| 337 | so you don't need to check for errors. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 338 | |
| 339 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 340 | \section{Compilation and linkage} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 342 | There are two more things to do before you can use your new extension |
| 343 | module: compiling and linking it with the Python system. If you use |
| 344 | dynamic loading, the details depend on the style of dynamic loading |
| 345 | your system uses; see the chapter on Dynamic Loading for more info |
| 346 | about this. |
| 347 | |
| 348 | If you can't use dynamic loading, or if you want to make your module a |
| 349 | permanent part of the Python interpreter, you will have to change the |
| 350 | configuration setup and rebuild the interpreter. Luckily, in the 1.0 |
| 351 | release this is very simple: just place your file (named |
| 352 | \file{foomodule.c} for example) in the \file{Modules} directory, add a |
| 353 | line to the file \file{Modules/Setup} describing your file: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 354 | |
| 355 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 356 | foo foomodule.o |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 357 | \end{verbatim} |
| 358 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 359 | and rebuild the interpreter by running \code{make} in the toplevel |
| 360 | directory. You can also run \code{make} in the \file{Modules} |
| 361 | subdirectory, but then you must first rebuilt the \file{Makefile} |
| 362 | there by running \code{make Makefile}. (This is necessary each time |
| 363 | you change the \file{Setup} file.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 364 | |
| 365 | |
| 366 | \section{Calling Python functions from C} |
| 367 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 368 | So far we have concentrated on making C functions callable from |
| 369 | Python. The reverse is also useful: calling Python functions from C. |
| 370 | This is especially the case for libraries that support so-called |
| 371 | `callback' functions. If a C interface makes use of callbacks, the |
| 372 | equivalent Python often needs to provide a callback mechanism to the |
| 373 | Python programmer; the implementation will require calling the Python |
| 374 | callback functions from a C callback. Other uses are also imaginable. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 375 | |
| 376 | Fortunately, the Python interpreter is easily called recursively, and |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 377 | 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] | 378 | 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] | 379 | 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] | 380 | the \samp{-c} command line option in \file{Python/pythonmain.c}.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 381 | |
| 382 | Calling a Python function is easy. First, the Python program must |
| 383 | somehow pass you the Python function object. You should provide a |
| 384 | function (or some other interface) to do this. When this function is |
| 385 | called, save a pointer to the Python function object (be careful to |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 386 | \code{INCREF()} it!) in a global variable --- or whereever you see fit. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 387 | For example, the following function might be part of a module |
| 388 | definition: |
| 389 | |
| 390 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 391 | static object *my_callback = NULL; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 392 | |
| 393 | static object * |
| 394 | my_set_callback(dummy, arg) |
| 395 | object *dummy, *arg; |
| 396 | { |
| 397 | XDECREF(my_callback); /* Dispose of previous callback */ |
| 398 | my_callback = arg; |
| 399 | XINCREF(my_callback); /* Remember new callback */ |
| 400 | /* Boilerplate for "void" return */ |
| 401 | INCREF(None); |
| 402 | return None; |
| 403 | } |
| 404 | \end{verbatim} |
| 405 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 406 | This particular function doesn't do any typechecking on its argument |
| 407 | --- that will be done by \code{call_object()}, which is a bit late but |
| 408 | at least protects the Python interpreter from shooting itself in its |
| 409 | foot. (The problem with typechecking functions is that there are at |
| 410 | least five different Python object types that can be called, so the |
| 411 | test would be somewhat cumbersome.) |
| 412 | |
| 413 | The macros \code{XINCREF()} and \code{XDECREF()} increment/decrement |
| 414 | the reference count of an object and are safe in the presence of |
| 415 | \code{NULL} pointers. More info on them in the section on Reference |
| 416 | Counts below. |
| 417 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 418 | Later, when it is time to call the function, you call the C function |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 419 | \code{call_object()}. This function has two arguments, both pointers |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 420 | to arbitrary Python objects: the Python function, and the argument |
| 421 | list. The argument list must always be a tuple object, whose length |
| 422 | is the number of arguments. To call the Python function with no |
| 423 | arguments, you must pass an empty tuple. For example: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 424 | |
| 425 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 426 | object *arglist; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 427 | object *result; |
| 428 | ... |
| 429 | /* Time to call the callback */ |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 430 | arglist = mktuple(0); |
| 431 | result = call_object(my_callback, arglist); |
| 432 | DECREF(arglist); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 433 | \end{verbatim} |
| 434 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 435 | \code{call_object()} returns a Python object pointer: this is |
| 436 | the return value of the Python function. \code{call_object()} is |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 437 | `reference-count-neutral' with respect to its arguments. In the |
| 438 | example a new tuple was created to serve as the argument list, which |
| 439 | is \code{DECREF()}-ed immediately after the call. |
| 440 | |
| 441 | The return value of \code{call_object()} is `new': either it is a |
| 442 | brand new object, or it is an existing object whose reference count |
| 443 | has been incremented. So, unless you want to save it in a global |
| 444 | variable, you should somehow \code{DECREF()} the result, even |
| 445 | (especially!) if you are not interested in its value. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 446 | |
| 447 | Before you do this, however, it is important to check that the return |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 448 | value isn't \code{NULL}. If it is, the Python function terminated by raising |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 449 | an exception. If the C code that called \code{call_object()} is |
| 450 | called from Python, it should now return an error indication to its |
| 451 | Python caller, so the interpreter can print a stack trace, or the |
| 452 | calling Python code can handle the exception. If this is not possible |
| 453 | or desirable, the exception should be cleared by calling |
| 454 | \code{err_clear()}. For example: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 455 | |
| 456 | \begin{verbatim} |
| 457 | if (result == NULL) |
| 458 | return NULL; /* Pass error back */ |
| 459 | /* Here maybe use the result */ |
| 460 | DECREF(result); |
| 461 | \end{verbatim} |
| 462 | |
| 463 | Depending on the desired interface to the Python callback function, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 464 | you may also have to provide an argument list to \code{call_object()}. |
| 465 | In some cases the argument list is also provided by the Python |
| 466 | program, through the same interface that specified the callback |
| 467 | function. It can then be saved and used in the same manner as the |
| 468 | function object. In other cases, you may have to construct a new |
| 469 | tuple to pass as the argument list. The simplest way to do this is to |
| 470 | call \code{mkvalue()}. For example, if you want to pass an integral |
| 471 | event code, you might use the following code: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 472 | |
| 473 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 474 | object *arglist; |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 475 | ... |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 476 | arglist = mkvalue("(l)", eventcode); |
| 477 | result = call_object(my_callback, arglist); |
| 478 | DECREF(arglist); |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 479 | if (result == NULL) |
| 480 | return NULL; /* Pass error back */ |
| 481 | /* Here maybe use the result */ |
| 482 | DECREF(result); |
| 483 | \end{verbatim} |
| 484 | |
| 485 | Note the placement of DECREF(argument) immediately after the call, |
| 486 | before the error check! Also note that strictly spoken this code is |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 487 | not complete: \code{mkvalue()} may run out of memory, and this should |
| 488 | be checked. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 489 | |
| 490 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 491 | \section{Format strings for {\tt getargs()}} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 492 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 493 | The \code{getargs()} function is declared in \file{modsupport.h} as |
| 494 | follows: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 495 | |
| 496 | \begin{verbatim} |
| 497 | int getargs(object *arg, char *format, ...); |
| 498 | \end{verbatim} |
| 499 | |
| 500 | The remaining arguments must be addresses of variables whose type is |
| 501 | determined by the format string. For the conversion to succeed, the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 502 | \var{arg} object must match the format and the format must be exhausted. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 503 | Note that while \code{getargs()} checks that the Python object really |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 504 | is of the specified type, it cannot check the validity of the |
| 505 | addresses of C variables provided in the call: if you make mistakes |
| 506 | there, your code will probably dump core. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 508 | A non-empty format string consists of a single `format unit'. A |
| 509 | format unit describes one Python object; it is usually a single |
| 510 | character or a parenthesized sequence of format units. The type of a |
| 511 | format units is determined from its first character, the `format |
| 512 | letter': |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 514 | \begin{description} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 515 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 516 | \item[\samp{s} (string)] |
| 517 | The Python object must be a string object. The C argument must be a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 518 | \code{(char**)} (i.e. the address of a character pointer), and a pointer |
| 519 | to the C string contained in the Python object is stored into it. You |
| 520 | must not provide storage to store the string; a pointer to an existing |
| 521 | string is stored into the character pointer variable whose address you |
| 522 | pass. If the next character in the format string is \samp{\#}, |
| 523 | another C argument of type \code{(int*)} must be present, and the |
| 524 | length of the Python string (not counting the trailing zero byte) is |
| 525 | stored into it. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 527 | \item[\samp{z} (string or zero, i.e. \code{NULL})] |
| 528 | Like \samp{s}, but the object may also be None. In this case the |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 529 | string pointer is set to \code{NULL} and if a \samp{\#} is present the |
| 530 | size is set to 0. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 531 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 532 | \item[\samp{b} (byte, i.e. char interpreted as tiny int)] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 533 | The object must be a Python integer. The C argument must be a |
| 534 | \code{(char*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 535 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 536 | \item[\samp{h} (half, i.e. short)] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 537 | The object must be a Python integer. The C argument must be a |
| 538 | \code{(short*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 539 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 540 | \item[\samp{i} (int)] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 541 | The object must be a Python integer. The C argument must be an |
| 542 | \code{(int*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 544 | \item[\samp{l} (long)] |
| 545 | The object must be a (plain!) Python integer. The C argument must be |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 546 | a \code{(long*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 547 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 548 | \item[\samp{c} (char)] |
| 549 | The Python object must be a string of length 1. The C argument must |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 550 | be a \code{(char*)}. (Don't pass an \code{(int*)}!) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 551 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 552 | \item[\samp{f} (float)] |
| 553 | The object must be a Python int or float. The C argument must be a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 554 | \code{(float*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 555 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 556 | \item[\samp{d} (double)] |
| 557 | The object must be a Python int or float. The C argument must be a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 558 | \code{(double*)}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 559 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 560 | \item[\samp{S} (string object)] |
| 561 | The object must be a Python string. The C argument must be an |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 562 | \code{(object**)} (i.e. the address of an object pointer). The C |
| 563 | program thus gets back the actual string object that was passed, not |
| 564 | just a pointer to its array of characters and its size as for format |
| 565 | character \samp{s}. The reference count of the object has not been |
| 566 | increased. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 567 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 568 | \item[\samp{O} (object)] |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 569 | The object can be any Python object, including None, but not |
| 570 | \code{NULL}. The C argument must be an \code{(object**)}. This can be |
| 571 | used if an argument list must contain objects of a type for which no |
| 572 | format letter exist: the caller must then check that it has the right |
| 573 | type. The reference count of the object has not been increased. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 574 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 575 | \item[\samp{(} (tuple)] |
| 576 | The object must be a Python tuple. Following the \samp{(} character |
| 577 | in the format string must come a number of format units describing the |
| 578 | elements of the tuple, followed by a \samp{)} character. Tuple |
| 579 | format units may be nested. (There are no exceptions for empty and |
| 580 | singleton tuples; \samp{()} specifies an empty tuple and \samp{(i)} a |
| 581 | singleton of one integer. Normally you don't want to use the latter, |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 582 | since it is hard for the Python user to specify. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 583 | |
| 584 | \end{description} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 585 | |
| 586 | More format characters will probably be added as the need arises. It |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 587 | should (but currently isn't) be allowed to use Python long integers |
| 588 | whereever integers are expected, and perform a range check. (A range |
| 589 | check is in fact always necessary for the \samp{b}, \samp{h} and |
| 590 | \samp{i} format letters, but this is currently not implemented.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 591 | |
| 592 | Some example calls: |
| 593 | |
| 594 | \begin{verbatim} |
| 595 | int ok; |
| 596 | int i, j; |
| 597 | long k, l; |
| 598 | char *s; |
| 599 | int size; |
| 600 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 601 | ok = getargs(args, ""); /* No arguments */ |
| 602 | /* Python call: f() */ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 603 | |
| 604 | ok = getargs(args, "s", &s); /* A string */ |
| 605 | /* Possible Python call: f('whoops!') */ |
| 606 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 607 | ok = getargs(args, "(lls)", &k, &l, &s); /* Two longs and a string */ |
| 608 | /* Possible Python call: f(1, 2, 'three') */ |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 609 | |
| 610 | ok = getargs(args, "((ii)s#)", &i, &j, &s, &size); |
| 611 | /* A pair of ints and a string, whose size is also returned */ |
| 612 | /* Possible Python call: f(1, 2, 'three') */ |
| 613 | |
| 614 | { |
| 615 | int left, top, right, bottom, h, v; |
| 616 | ok = getargs(args, "(((ii)(ii))(ii))", |
| 617 | &left, &top, &right, &bottom, &h, &v); |
| 618 | /* A rectangle and a point */ |
| 619 | /* Possible Python call: |
| 620 | f( ((0, 0), (400, 300)), (10, 10)) */ |
| 621 | } |
| 622 | \end{verbatim} |
| 623 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 624 | Note that the `top level' of a non-empty format string must consist of |
| 625 | a single unit; strings like \samp{is} and \samp{(ii)s\#} are not valid |
| 626 | format strings. (But \samp{s\#} is.) If you have multiple arguments, |
| 627 | the format must therefore always be enclosed in parentheses, as in the |
| 628 | examples \samp{((ii)s\#)} and \samp{(((ii)(ii))(ii)}. (The current |
| 629 | implementation does not complain when more than one unparenthesized |
| 630 | format unit is given. Sorry.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 631 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 632 | The \code{getargs()} function does not support variable-length |
| 633 | argument lists. In simple cases you can fake these by trying several |
| 634 | calls to |
| 635 | \code{getargs()} until one succeeds, but you must take care to call |
| 636 | \code{err_clear()} before each retry. For example: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 637 | |
| 638 | \begin{verbatim} |
| 639 | static object *my_method(self, args) object *self, *args; { |
| 640 | int i, j, k; |
| 641 | |
| 642 | if (getargs(args, "(ii)", &i, &j)) { |
| 643 | k = 0; /* Use default third argument */ |
| 644 | } |
| 645 | else { |
| 646 | err_clear(); |
| 647 | if (!getargs(args, "(iii)", &i, &j, &k)) |
| 648 | return NULL; |
| 649 | } |
| 650 | /* ... use i, j and k here ... */ |
| 651 | INCREF(None); |
| 652 | return None; |
| 653 | } |
| 654 | \end{verbatim} |
| 655 | |
| 656 | (It is possible to think of an extension to the definition of format |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 657 | strings to accommodate this directly, e.g. placing a \samp{|} in a |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 658 | tuple might specify that the remaining arguments are optional. |
| 659 | \code{getargs()} should then return one more than the number of |
| 660 | variables stored into.) |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 661 | |
| 662 | Advanced users note: If you set the `varargs' flag in the method list |
| 663 | for a function, the argument will always be a tuple (the `raw argument |
| 664 | list'). In this case you must enclose single and empty argument lists |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 665 | in parentheses, e.g. \samp{(s)} and \samp{()}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 666 | |
| 667 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 668 | \section{The {\tt mkvalue()} function} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 669 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 670 | This function is the counterpart to \code{getargs()}. It is declared |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 671 | in \file{Include/modsupport.h} as follows: |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 672 | |
| 673 | \begin{verbatim} |
| 674 | object *mkvalue(char *format, ...); |
| 675 | \end{verbatim} |
| 676 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 677 | It supports exactly the same format letters as \code{getargs()}, but |
| 678 | the arguments (which are input to the function, not output) must not |
| 679 | be pointers, just values. If a byte, short or float is passed to a |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 680 | varargs function, it is widened by the compiler to int or double, so |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 681 | \samp{b} and \samp{h} are treated as \samp{i} and \samp{f} is |
| 682 | treated as \samp{d}. \samp{S} is treated as \samp{O}, \samp{s} is |
| 683 | treated as \samp{z}. \samp{z\#} and \samp{s\#} are supported: a |
| 684 | second argument specifies the length of the data (negative means use |
| 685 | \code{strlen()}). \samp{S} and \samp{O} add a reference to their |
| 686 | argument (so you should \code{DECREF()} it if you've just created it |
| 687 | and aren't going to use it again). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 688 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 689 | If the argument for \samp{O} or \samp{S} is a \code{NULL} pointer, it is |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 690 | assumed that this was caused because the call producing the argument |
| 691 | found an error and set an exception. Therefore, \code{mkvalue()} will |
| 692 | return \code{NULL} but won't set an exception if one is already set. |
| 693 | If no exception is set, \code{SystemError} is set. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 694 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 695 | If there is an error in the format string, the \code{SystemError} |
| 696 | exception is set, since it is the calling C code's fault, not that of |
| 697 | the Python user who sees the exception. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 698 | |
| 699 | Example: |
| 700 | |
| 701 | \begin{verbatim} |
| 702 | return mkvalue("(ii)", 0, 0); |
| 703 | \end{verbatim} |
| 704 | |
| 705 | returns a tuple containing two zeros. (Outer parentheses in the |
| 706 | format string are actually superfluous, but you can use them for |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 707 | compatibility with \code{getargs()}, which requires them if more than |
| 708 | one argument is expected.) |
| 709 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 710 | |
| 711 | \section{Reference counts} |
| 712 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 713 | Here's a useful explanation of \code{INCREF()} and \code{DECREF()} |
| 714 | (after an original by Sjoerd Mullender). |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 715 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 716 | Use \code{XINCREF()} or \code{XDECREF()} instead of \code{INCREF()} or |
| 717 | \code{DECREF()} when the argument may be \code{NULL} --- the versions |
| 718 | without \samp{X} are faster but wull dump core when they encounter a |
| 719 | \code{NULL} pointer. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 720 | |
| 721 | The basic idea is, if you create an extra reference to an object, you |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 722 | must \code{INCREF()} it, if you throw away a reference to an object, |
| 723 | you must \code{DECREF()} it. Functions such as |
| 724 | \code{newstringobject()}, \code{newsizedstringobject()}, |
| 725 | \code{newintobject()}, etc. create a reference to an object. If you |
| 726 | want to throw away the object thus created, you must use |
| 727 | \code{DECREF()}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 729 | If you put an object into a tuple or list using \code{settupleitem()} |
| 730 | or \code{setlistitem()}, the idea is that you usually don't want to |
| 731 | keep a reference of your own around, so Python does not |
| 732 | \code{INCREF()} the elements. It does \code{DECREF()} the old value. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 733 | This means that if you put something into such an object using the |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 734 | functions Python provides for this, you must \code{INCREF()} the |
| 735 | object if you also want to keep a separate reference to the object around. |
| 736 | Also, if you replace an element, you should \code{INCREF()} the old |
| 737 | element first if you want to keep it. If you didn't \code{INCREF()} |
| 738 | it before you replaced it, you are not allowed to look at it anymore, |
| 739 | since it may have been freed. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 740 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 741 | Returning an object to Python (i.e. when your C function returns) |
| 742 | creates a reference to an object, but it does not change the reference |
| 743 | count. When your code does not keep another reference to the object, |
| 744 | you should not \code{INCREF()} or \code{DECREF()} it (assuming it is a |
| 745 | newly created object). When you do keep a reference around, you |
| 746 | should \code{INCREF()} the object. Also, when you return a global |
| 747 | object such as \code{None}, you should \code{INCREF()} it. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 748 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 749 | If you want to return a tuple, you should consider using |
| 750 | \code{mkvalue()}. This function creates a new tuple with a reference |
| 751 | count of 1 which you can return. If any of the elements you put into |
| 752 | the tuple are objects (format codes \samp{O} or \samp{S}), they |
| 753 | are \code{INCREF()}'ed by \code{mkvalue()}. If you don't want to keep |
| 754 | references to those elements around, you should \code{DECREF()} them |
| 755 | after having called \code{mkvalue()}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 756 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 757 | Usually you don't have to worry about arguments. They are |
| 758 | \code{INCREF()}'ed before your function is called and |
| 759 | \code{DECREF()}'ed after your function returns. When you keep a |
| 760 | reference to an argument, you should \code{INCREF()} it and |
| 761 | \code{DECREF()} when you throw it away. Also, when you return an |
| 762 | argument, you should \code{INCREF()} it, because returning the |
| 763 | argument creates an extra reference to it. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 764 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 765 | If you use \code{getargs()} to parse the arguments, you can get a |
| 766 | reference to an object (by using \samp{O} in the format string). This |
| 767 | object was not \code{INCREF()}'ed, so you should not \code{DECREF()} |
| 768 | it. If you want to keep the object, you must \code{INCREF()} it |
| 769 | yourself. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 770 | |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 771 | If you create your own type of objects, you should use \code{NEWOBJ()} |
| 772 | to create the object. This sets the reference count to 1. If you |
| 773 | want to throw away the object, you should use \code{DECREF()}. When |
| 774 | the reference count reaches zero, your type's \code{dealloc()} |
| 775 | function is called. In it, you should \code{DECREF()} all object to |
| 776 | which you keep references in your object, but you should not use |
| 777 | \code{DECREF()} on your object. You should use \code{DEL()} instead. |
| 778 | |
| 779 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 780 | \section{Writing extensions in \Cpp{}} |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 781 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 782 | It is possible to write extension modules in \Cpp{}. Some restrictions |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 783 | apply: since the main program (the Python interpreter) is compiled and |
| 784 | linked by the C compiler, global or static objects with constructors |
| 785 | cannot be used. All functions that will be called directly or |
| 786 | indirectly (i.e. via function pointers) by the Python interpreter will |
| 787 | have to be declared using \code{extern "C"}; this applies to all |
| 788 | `methods' as well as to the module's initialization function. |
| 789 | It is unnecessary to enclose the Python header files in |
| 790 | \code{extern "C" \{...\}} --- they do this already. |
| 791 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 792 | |
| 793 | \chapter{Embedding Python in another application} |
| 794 | |
| 795 | Embedding Python is similar to extending it, but not quite. The |
| 796 | 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] | 797 | application is still the Python interpreter, while if you embed |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 798 | Python, the main program may have nothing to do with Python --- |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 799 | instead, some parts of the application occasionally call the Python |
| 800 | interpreter to run some Python code. |
| 801 | |
| 802 | So if you are embedding Python, you are providing your own main |
| 803 | program. One of the things this main program has to do is initialize |
| 804 | the Python interpreter. At the very least, you have to call the |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 805 | function \code{initall()}. There are optional calls to pass command |
| 806 | line arguments to Python. Then later you can call the interpreter |
| 807 | from any part of the application. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 808 | |
| 809 | There are several different ways to call the interpreter: you can pass |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 810 | a string containing Python statements to \code{run_command()}, or you |
| 811 | can pass a stdio file pointer and a file name (for identification in |
| 812 | error messages only) to \code{run_script()}. You can also call the |
| 813 | lower-level operations described in the previous chapters to construct |
| 814 | and use Python objects. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 815 | |
| 816 | 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] | 817 | \file{Demo/embed}. |
Guido van Rossum | db65a6c | 1993-11-05 17:11:16 +0000 | [diff] [blame] | 818 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 820 | \section{Embedding Python in \Cpp{}} |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 821 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 822 | It is also possible to embed Python in a \Cpp{} program; precisely how this |
| 823 | is done will depend on the details of the \Cpp{} system used; in general you |
| 824 | will need to write the main program in \Cpp{}, and use the \Cpp{} compiler |
| 825 | to compile and link your program. There is no need to recompile Python |
| 826 | itself using \Cpp{}. |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 827 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 828 | |
| 829 | \chapter{Dynamic Loading} |
| 830 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 831 | On most modern systems it is possible to configure Python to support |
| 832 | dynamic loading of extension modules implemented in C. When shared |
| 833 | libraries are used dynamic loading is configured automatically; |
| 834 | otherwise you have to select it as a build option (see below). Once |
| 835 | configured, dynamic loading is trivial to use: when a Python program |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 836 | executes \code{import foo}, the search for modules tries to find a |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 837 | file \file{foomodule.o} (\file{foomodule.so} when using shared |
| 838 | libraries) in the module search path, and if one is found, it is |
| 839 | loaded into the executing binary and executed. Once loaded, the |
| 840 | module acts just like a built-in extension module. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 841 | |
| 842 | The advantages of dynamic loading are twofold: the `core' Python |
| 843 | binary gets smaller, and users can extend Python with their own |
| 844 | modules implemented in C without having to build and maintain their |
| 845 | own copy of the Python interpreter. There are also disadvantages: |
| 846 | dynamic loading isn't available on all systems (this just means that |
| 847 | on some systems you have to use static loading), and dynamically |
| 848 | 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] | 849 | (e.g. with a different representation of objects) may dump core. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 850 | |
| 851 | |
| 852 | \section{Configuring and building the interpreter for dynamic loading} |
| 853 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 854 | There are three styles of dynamic loading: one using shared libraries, |
| 855 | one using SGI IRIX 4 dynamic loading, and one using GNU dynamic |
| 856 | loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 857 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 858 | \subsection{Shared libraries} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 859 | |
Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 860 | The following systems support dynamic loading using shared libraries: |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 861 | SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all |
| 862 | systems derived from SVR4, or at least those SVR4 derivatives that |
| 863 | support shared libraries (are there any that don't?). |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 864 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 865 | You don't need to do anything to configure dynamic loading on these |
| 866 | systems --- the \file{configure} detects the presence of the |
| 867 | \file{<dlfcn.h>} header file and automatically configures dynamic |
| 868 | loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 869 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 870 | \subsection{SGI dynamic loading} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 871 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 872 | Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic |
| 873 | loading. (SGI IRIX 5 might also support it but it is inferior to |
| 874 | using shared libraries so there is no reason to; a small test didn't |
| 875 | work right away so I gave up trying to support it.) |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 876 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 877 | Before you build Python, you first need to fetch and build the \code{dl} |
| 878 | package written by Jack Jansen. This is available by anonymous ftp |
| 879 | from host \file{ftp.cwi.nl}, directory \file{pub/dynload}, file |
| 880 | \file{dl-1.6.tar.Z}. (The version number may change.) Follow the |
| 881 | instructions in the package's \file{README} file to build it. |
| 882 | |
| 883 | Once you have built \code{dl}, you can configure Python to use it. To |
| 884 | this end, you run the \file{configure} script with the option |
| 885 | \code{--with-dl=\var{directory}} where \var{directory} is the absolute |
| 886 | pathname of the \code{dl} directory. |
| 887 | |
| 888 | Now build and install Python as you normally would (see the |
| 889 | \file{README} file in the toplevel Python directory.) |
| 890 | |
| 891 | \subsection{GNU dynamic loading} |
| 892 | |
| 893 | GNU dynamic loading supports (according to its \file{README} file) the |
| 894 | following hardware and software combinations: VAX (Ultrix), Sun 3 |
| 895 | (SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and |
| 896 | Atari ST. There is no reason to use it on a Sparc; I haven't seen a |
| 897 | Sun 3 for years so I don't know if these have shared libraries or not. |
| 898 | |
| 899 | You need to fetch and build two packages. One is GNU DLD 3.2.3, |
| 900 | available by anonymous ftp from host \file{ftp.cwi.nl}, directory |
| 901 | \file{pub/dynload}, file \file{dld-3.2.3.tar.Z}. (As far as I know, |
| 902 | no further development on GNU DLD is being done.) The other is an |
| 903 | emulation of Jack Jansen's \code{dl} package that I wrote on top of |
| 904 | GNU DLD 3.2.3. This is available from the same host and directory, |
| 905 | file dl-dld-1.1.tar.Z. (The version number may change --- but I doubt |
| 906 | it will.) Follow the instructions in each package's \file{README} |
| 907 | file to configure build them. |
| 908 | |
| 909 | Now configure Python. Run the \file{configure} script with the option |
| 910 | \code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where |
| 911 | \var{dl-directory} is the absolute pathname of the directory where you |
| 912 | have built the \file{dl-dld} package, and \var{dld-directory} is that |
| 913 | of the GNU DLD package. The Python interpreter you build hereafter |
| 914 | will support GNU dynamic loading. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 915 | |
| 916 | |
| 917 | \section{Building a dynamically loadable module} |
| 918 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 919 | Since there are three styles of dynamic loading, there are also three |
| 920 | groups of instructions for building a dynamically loadable module. |
| 921 | Instructions common for all three styles are given first. Assuming |
| 922 | your module is called \code{foo}, the source filename must be |
| 923 | \file{foomodule.c}, so the object name is \file{foomodule.o}. The |
| 924 | module must be written as a normal Python extension module (as |
| 925 | described earlier). |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 927 | Note that in all cases you will have to create your own Makefile that |
| 928 | compiles your module file(s). This Makefile will have to pass two |
| 929 | \samp{-I} arguments to the C compiler which will make it find the |
| 930 | Python header files. If the Make variable \var{PYTHONTOP} points to |
| 931 | the toplevel Python directory, your \var{CFLAGS} Make variable should |
| 932 | contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}. |
| 933 | (Most header files are in the \file{Include} subdirectory, but the |
| 934 | \file{config.h} header lives in the toplevel directory.) You must |
| 935 | also add \samp{-DHAVE_CONFIG_H} to the definition of \var{CFLAGS} to |
| 936 | direct the Python headers to include \file{config.h}. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 937 | |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 938 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 939 | \subsection{Shared libraries} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 940 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 941 | You must link the \samp{.o} file to produce a shared library. This is |
| 942 | done using a special invocation of the \UNIX{} loader/linker, {\em |
| 943 | ld}(1). Unfortunately the invocation differs slightly per system. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 944 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 945 | On SunOS 4, use |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 946 | \begin{verbatim} |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 947 | ld foomodule.o -o foomodule.so |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 948 | \end{verbatim} |
| 949 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 950 | On Solaris 2, use |
| 951 | \begin{verbatim} |
| 952 | ld -G foomodule.o -o foomodule.so |
| 953 | \end{verbatim} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 955 | On SGI IRIX 5, use |
| 956 | \begin{verbatim} |
| 957 | ld -shared foomodule.o -o foomodule.so |
| 958 | \end{verbatim} |
| 959 | |
| 960 | On other systems, consult the manual page for {\em ld}(1) to find what |
| 961 | flags, if any, must be used. |
| 962 | |
| 963 | If your extension module uses system libraries that haven't already |
| 964 | been linked with Python (e.g. a windowing system), these must be |
| 965 | passed to the {\em ld} command as \samp{-l} options after the |
| 966 | \samp{.o} file. |
| 967 | |
| 968 | The resulting file \file{foomodule.so} must be copied into a directory |
| 969 | along the Python module search path. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 970 | |
| 971 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 972 | \subsection{SGI dynamic loading} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 973 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 974 | {bf IMPORTANT:} You must compile your extension module with the |
| 975 | additional C flag \samp{-G0} (or \samp{-G 0}). This instruct the |
| 976 | assembler to generate position-independent code. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 977 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 978 | You don't need to link the resulting \file{foomodule.o} file; just |
| 979 | copy it into a directory along the Python module search path. |
| 980 | |
| 981 | The first time your extension is loaded, it takes some extra time and |
| 982 | a few messages may be printed. This creates a file |
| 983 | \file{foomodule.ld} which is an image that can be loaded quickly into |
| 984 | the Python interpreter process. When a new Python interpreter is |
| 985 | installed, the \code{dl} package detects this and rebuilds |
| 986 | \file{foomodule.ld}. The file \file{foomodule.ld} is placed in the |
| 987 | directory where \file{foomodule.o} was found, unless this directory is |
| 988 | unwritable; in that case it is placed in a temporary |
| 989 | directory.\footnote{Check the manual page of the \code{dl} package for |
| 990 | details.} |
| 991 | |
| 992 | If your extension modules uses additional system libraries, you must |
| 993 | create a file \file{foomodule.libs} in the same directory as the |
| 994 | \file{foomodule.o}. This file should contain one or more lines with |
| 995 | whitespace-separated options that will be passed to the linker --- |
| 996 | normally only \samp{-l} options or absolute pathnames of libraries |
| 997 | (\samp{.a} files) should be used. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 998 | |
| 999 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1000 | \subsection{GNU dynamic loading} |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1001 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1002 | Just copy \file{foomodule.o} into a directory along the Python module |
| 1003 | search path. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1004 | |
Guido van Rossum | 6938f06 | 1994-08-01 12:22:53 +0000 | [diff] [blame] | 1005 | If your extension modules uses additional system libraries, you must |
| 1006 | create a file \file{foomodule.libs} in the same directory as the |
| 1007 | \file{foomodule.o}. This file should contain one or more lines with |
| 1008 | whitespace-separated absolute pathnames of libraries (\samp{.a} |
| 1009 | files). No \samp{-l} options can be used. |
Guido van Rossum | 6f0132f | 1993-11-19 13:13:22 +0000 | [diff] [blame] | 1010 | |
| 1011 | |
Guido van Rossum | 7a2dba2 | 1993-11-05 14:45:11 +0000 | [diff] [blame] | 1012 | \input{ext.ind} |
| 1013 | |
| 1014 | \end{document} |