blob: a7d4221019b47157d443515752e4c37739e891f9 [file] [log] [blame]
Guido van Rossum6938f061994-08-01 12:22:53 +00001\documentstyle[twoside,11pt,myformat]{report}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00002
Guido van Rossum6938f061994-08-01 12:22:53 +00003\title{Extending and Embedding the Python Interpreter}
Guido van Rossum7a2dba21993-11-05 14:45:11 +00004
5\author{
6 Guido van Rossum \\
Guido van Rossumdb65a6c1993-11-05 17:11:16 +00007 Dept. CST, CWI, P.O. Box 94079 \\
8 1090 GB Amsterdam, The Netherlands \\
Guido van Rossum7a2dba21993-11-05 14:45:11 +00009 E-mail: {\tt guido@cwi.nl}
10}
11
Guido van Rossum6938f061994-08-01 12:22:53 +000012\date{14 Jul 1994 \\ Release 1.0.3} % XXX update before release!
Guido van Rossum83eb9621993-11-23 16:28:45 +000013
Guido van Rossum7a2dba21993-11-05 14:45:11 +000014% 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 Rossum6f0132f1993-11-19 13:13:22 +000026This document describes how to write modules in C or C++ to extend the
27Python interpreter. It also describes how to use Python as an
28`embedded' language, and how extension modules can be loaded
29dynamically (at run time) into the interpreter, if the operating
30system supports this feature.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000031
32\end{abstract}
33
34\pagebreak
35
36{
37\parskip = 0mm
38\tableofcontents
39}
40
41\pagebreak
42
43\pagenumbering{arabic}
44
Guido van Rossumdb65a6c1993-11-05 17:11:16 +000045
Guido van Rossum7a2dba21993-11-05 14:45:11 +000046\chapter{Extending Python with C or C++ code}
47
Guido van Rossum6f0132f1993-11-19 13:13:22 +000048
49\section{Introduction}
50
Guido van Rossum7a2dba21993-11-05 14:45:11 +000051It is quite easy to add non-standard built-in modules to Python, if
52you know how to program in C. A built-in module known to the Python
Guido van Rossum6f0132f1993-11-19 13:13:22 +000053programmer as \code{foo} is generally implemented by a file called
Guido van Rossum6938f061994-08-01 12:22:53 +000054\file{foomodule.c}. All but the two most essential standard built-in
Guido van Rossum6f0132f1993-11-19 13:13:22 +000055modules also adhere to this convention, and in fact some of them form
56excellent examples of how to create an extension.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000057
58Extension modules can do two things that can't be done directly in
Guido van Rossum6938f061994-08-01 12:22:53 +000059Python: they can implement new data types (which are different from
60classes by the way), and they can make system calls or call C library
61functions. Since the latter is usually the most important reason for
62adding an extension, I'll concentrate on adding `wrappers' around C
63library functions; the concrete example uses the wrapper for
Guido van Rossum6f0132f1993-11-19 13:13:22 +000064\code{system()} in module \code{posix}, found in (of course) the file
Guido van Rossum6938f061994-08-01 12:22:53 +000065\file{Modules/posixmodule.c}.
66
67Note: unless otherwise mentioned, all file references in this
68document are relative to the toplevel directory of the Python
69distribution --- i.e. the directory that contains the \file{configure}
70script.
71
72The compilation of an extension module depends on your system setup
73and the intended use of the module; details are given in a later
74section.
75
76
77\section{A first look at the code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000078
79It is important not to be impressed by the size and complexity of
80the average extension module; much of this is straightforward
Guido van Rossum6f0132f1993-11-19 13:13:22 +000081`boilerplate' code (starting right with the copyright notice)!
Guido van Rossum7a2dba21993-11-05 14:45:11 +000082
Guido van Rossum6f0132f1993-11-19 13:13:22 +000083Let's skip the boilerplate and have a look at an interesting function
84in \file{posixmodule.c} first:
Guido van Rossum7a2dba21993-11-05 14:45:11 +000085
86\begin{verbatim}
87 static object *
88 posix_system(self, args)
89 object *self;
90 object *args;
91 {
92 char *command;
93 int sts;
94 if (!getargs(args, "s", &command))
95 return NULL;
96 sts = system(command);
Guido van Rossum6f0132f1993-11-19 13:13:22 +000097 return mkvalue("i", sts);
Guido van Rossum7a2dba21993-11-05 14:45:11 +000098 }
99\end{verbatim}
100
101This is the prototypical top-level function in an extension module.
Guido van Rossum6938f061994-08-01 12:22:53 +0000102It will be called (we'll see later how) when the Python program
103executes statements like
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000104
105\begin{verbatim}
106 >>> import posix
107 >>> sts = posix.system('ls -l')
108\end{verbatim}
109
110There is a straightforward translation from the arguments to the call
Guido van Rossum6938f061994-08-01 12:22:53 +0000111in Python (here the single expression \code{'ls -l'}) to the arguments that
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000112are passed to the C function. The C function always has two
Guido van Rossum6938f061994-08-01 12:22:53 +0000113parameters, conventionally named \var{self} and \var{args}. The
114\var{self} argument is used when the C function implements a builtin
115method --- this is advanced material and not covered in this document.
116In the example, \var{self} will always be a \code{NULL} pointer, since
117we are defining a function, not a method (this is done so that the
118interpreter doesn't have to understand two different types of C
119functions).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000120
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000121The \var{args} parameter will be a pointer to a Python object, or
122\code{NULL} if the Python function/method was called without
123arguments. It is necessary to do full argument type checking on each
124call, since otherwise the Python user would be able to cause the
Guido van Rossum6938f061994-08-01 12:22:53 +0000125Python interpreter to `dump core' by passing invalid arguments to a
126function in an extension module. Because argument checking and
127converting arguments to C are such common tasks, there's a general
128function in the Python interpreter that combines them:
129\code{getargs()}. It uses a template string to determine both the
130types of the Python argument and the types of the C variables into
131which it should store the converted values.\footnote{There are
132convenience macros \code{getnoarg()}, \code{getstrarg()},
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000133\code{getintarg()}, etc., for many common forms of \code{getargs()}
Guido van Rossum6938f061994-08-01 12:22:53 +0000134templates. These are relics from the past; the recommended practice
135is to call \code{getargs()} directly.} (More about this later.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000136
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000137If \code{getargs()} returns nonzero, the argument list has the right
138type and its components have been stored in the variables whose
139addresses are passed. If it returns zero, an error has occurred. In
Guido van Rossum6938f061994-08-01 12:22:53 +0000140the latter case it has already raised an appropriate exception by so
141the calling function should return \code{NULL} immediately --- see the
142next section.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000143
144
145\section{Intermezzo: errors and exceptions}
146
147An important convention throughout the Python interpreter is the
148following: when a function fails, it should set an exception condition
Guido van Rossum6938f061994-08-01 12:22:53 +0000149and return an error value (often a \code{NULL} pointer). Exceptions
150are stored in a static global variable in \file{Python/errors.c}; if
151this variable is \code{NULL} no exception has occurred. A second
152static global variable stores the `associated value' of the exception
153--- the second argument to \code{raise}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000154
Guido van Rossum6938f061994-08-01 12:22:53 +0000155The file \file{errors.h} declares a host of functions to set various
156types of exceptions. The most common one is \code{err_setstr()} ---
157its arguments are an exception object (e.g. \code{RuntimeError} ---
158actually it can be any string object) and a C string indicating the
159cause of the error (this is converted to a string object and stored as
160the `associated value' of the exception). Another useful function is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000161\code{err_errno()}, which only takes an exception argument and
162constructs the associated value by inspection of the (UNIX) global
Guido van Rossum6938f061994-08-01 12:22:53 +0000163variable errno. The most general function is \code{err_set()}, which
164takes two object arguments, the exception and its associated value.
165You don't need to \code{INCREF()} the objects passed to any of these
166functions.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000167
168You can test non-destructively whether an exception has been set with
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000169\code{err_occurred()}. However, most code never calls
170\code{err_occurred()} to see whether an error occurred or not, but
Guido van Rossum6938f061994-08-01 12:22:53 +0000171relies on error return values from the functions it calls instead.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000172
173When a function that calls another function detects that the called
Guido van Rossum6938f061994-08-01 12:22:53 +0000174function fails, it should return an error value (e.g. \code{NULL} or
175\code{-1}) but not call one of the \code{err_*} functions --- one has
176already been called. The caller is then supposed to also return an
177error indication to {\em its} caller, again {\em without} calling
178\code{err_*()}, and so on --- the most detailed cause of the error was
179already reported by the function that first detected it. Once the
180error has reached Python's interpreter main loop, this aborts the
181currently executing Python code and tries to find an exception handler
182specified by the Python programmer.
183
184(There are situations where a module can actually give a more detailed
185error message by calling another \code{err_*} function, and in such
186cases it is fine to do so. As a general rule, however, this is not
187necessary, and can cause information about the cause of the error to
188be lost: most operations can fail for a variety of reasons.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000189
190To ignore an exception set by a function call that failed, the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000191exception condition must be cleared explicitly by calling
192\code{err_clear()}. The only time C code should call
193\code{err_clear()} is if it doesn't want to pass the error on to the
194interpreter but wants to handle it completely by itself (e.g. by
195trying something else or pretending nothing happened).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000196
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000197Finally, the function \code{err_get()} gives you both error variables
Guido van Rossum6938f061994-08-01 12:22:53 +0000198{\em and clears them}. Note that even if an error occurred the second
199one may be \code{NULL}. You have to \code{XDECREF()} both when you
200are finished with them. I doubt you will need to use this function.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000201
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000202Note that a failing \code{malloc()} call must also be turned into an
203exception --- the direct caller of \code{malloc()} (or
204\code{realloc()}) must call \code{err_nomem()} and return a failure
205indicator itself. All the object-creating functions
206(\code{newintobject()} etc.) already do this, so only if you call
207\code{malloc()} directly this note is of importance.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000208
Guido van Rossum6938f061994-08-01 12:22:53 +0000209Also note that, with the important exception of \code{getargs()},
210functions that return an integer status usually return \code{0} or a
211positive value for success and \code{-1} for failure.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000212
Guido van Rossum6938f061994-08-01 12:22:53 +0000213Finally, be careful about cleaning up garbage (making \code{XDECREF()}
214or \code{DECREF()} calls for objects you have already created) when
215you return an error!
216
217The choice of which exception to raise is entirely yours. There are
218predeclared C objects corresponding to all built-in Python exceptions,
219e.g. \code{ZeroDevisionError} which you can use directly. Of course,
220you should chose exceptions wisely --- don't use \code{TypeError} to
221mean that a file couldn't be opened (that should probably be
222\code{IOError}). If anything's wrong with the argument list the
223\code{getargs()} function raises \code{TypeError}. If you have an
224argument whose value which must be in a particular range or must
225satisfy other conditions, \code{ValueError} is appropriate.
226
227You can also define a new exception that is unique to your module.
228For this, you usually declare a static object variable at the
229beginning of your file, e.g.
230
231\begin{verbatim}
232 static object *FooError;
233\end{verbatim}
234
235and initialize it in your module's initialization function
236(\code{initfoo()}) with a string object, e.g. (leaving out the error
237checking for simplicity):
238
239\begin{verbatim}
240 void
241 initfoo()
242 {
243 object *m, *d;
244 m = initmodule("foo", foo_methods);
245 d = getmoduledict(m);
246 FooError = newstringobject("foo.error");
247 dictinsert(d, "error", FooError);
248 }
249\end{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000250
251
252\section{Back to the example}
253
Guido van Rossum6938f061994-08-01 12:22:53 +0000254Going back to \code{posix_system()}, you should now be able to
255understand this bit:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000256
257\begin{verbatim}
258 if (!getargs(args, "s", &command))
259 return NULL;
260\end{verbatim}
261
Guido van Rossum6938f061994-08-01 12:22:53 +0000262It returns \code{NULL} (the error indicator for functions of this
263kind) if an error is detected in the argument list, relying on the
264exception set by \code{getargs()}. Otherwise the string value of the
265argument has been copied to the local variable \code{command} --- this
266is in fact just a pointer assignment and you are not supposed to
267modify the string to which it points.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000268
Guido van Rossum6938f061994-08-01 12:22:53 +0000269If a function is called with multiple arguments, the argument list
270(the argument \code{args}) is turned into a tuple. If it is called
271without arguments, \code{args} is \code{NULL}. \code{getargs()} knows
272about this; see later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000273
Guido van Rossum6938f061994-08-01 12:22:53 +0000274The next statement in \code{posix_system()} is a call to the C library
275function \code{system()}, passing it the string we just got from
276\code{getargs()}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000277
278\begin{verbatim}
279 sts = system(command);
280\end{verbatim}
281
Guido van Rossum6938f061994-08-01 12:22:53 +0000282Finally, \code{posix.system()} must return a value: the integer status
283returned by the C library \code{system()} function. This is done
284using the function \code{mkvalue()}, which is something like the
285inverse of \code{getargs()}: it takes a format string and a variable
286number of C values and returns a new Python object.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000287
288\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000289 return mkvalue("i", sts);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000290\end{verbatim}
291
Guido van Rossum6938f061994-08-01 12:22:53 +0000292In this case, it returns an integer object (yes, even integers are
293objects on the heap in Python!). More info on \code{mkvalue()} is
294given later.
295
296If you had a function that returned no useful argument (a.k.a. a
297procedure), you would need this idiom:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000298
299\begin{verbatim}
300 INCREF(None);
301 return None;
302\end{verbatim}
303
Guido van Rossum6938f061994-08-01 12:22:53 +0000304\code{None} is a unique Python object representing `no value'. It
305differs from \code{NULL}, which means `error' in most contexts.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000306
307
308\section{The module's function table}
309
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000310I promised to show how I made the function \code{posix_system()}
Guido van Rossum6938f061994-08-01 12:22:53 +0000311callable from Python programs. This is shown later in
312\file{Modules/posixmodule.c}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000313
314\begin{verbatim}
315 static struct methodlist posix_methods[] = {
316 ...
317 {"system", posix_system},
318 ...
319 {NULL, NULL} /* Sentinel */
320 };
321
322 void
323 initposix()
324 {
325 (void) initmodule("posix", posix_methods);
326 }
327\end{verbatim}
328
Guido van Rossum6938f061994-08-01 12:22:53 +0000329(The actual \code{initposix()} is somewhat more complicated, but many
330extension modules can be as simple as shown here.) When the Python
331program first imports module \code{posix}, \code{initposix()} is
332called, which calls \code{initmodule()} with specific parameters.
333This creates a `module object' (which is inserted in the table
334\code{sys.modules} under the key \code{'posix'}), and adds
335built-in-function objects to the newly created module based upon the
336table (of type struct methodlist) that was passed as its second
337parameter. The function \code{initmodule()} returns a pointer to the
338module object that it creates (which is unused here). It aborts with
339a fatal error if the module could not be initialized satisfactorily,
340so you don't need to check for errors.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000341
342
Guido van Rossum6938f061994-08-01 12:22:53 +0000343\section{Compilation and linkage}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000344
Guido van Rossum6938f061994-08-01 12:22:53 +0000345There are two more things to do before you can use your new extension
346module: compiling and linking it with the Python system. If you use
347dynamic loading, the details depend on the style of dynamic loading
348your system uses; see the chapter on Dynamic Loading for more info
349about this.
350
351If you can't use dynamic loading, or if you want to make your module a
352permanent part of the Python interpreter, you will have to change the
353configuration setup and rebuild the interpreter. Luckily, in the 1.0
354release this is very simple: just place your file (named
355\file{foomodule.c} for example) in the \file{Modules} directory, add a
356line to the file \file{Modules/Setup} describing your file:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000357
358\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000359 foo foomodule.o
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000360\end{verbatim}
361
Guido van Rossum6938f061994-08-01 12:22:53 +0000362and rebuild the interpreter by running \code{make} in the toplevel
363directory. You can also run \code{make} in the \file{Modules}
364subdirectory, but then you must first rebuilt the \file{Makefile}
365there by running \code{make Makefile}. (This is necessary each time
366you change the \file{Setup} file.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000367
368
369\section{Calling Python functions from C}
370
Guido van Rossum6938f061994-08-01 12:22:53 +0000371So far we have concentrated on making C functions callable from
372Python. The reverse is also useful: calling Python functions from C.
373This is especially the case for libraries that support so-called
374`callback' functions. If a C interface makes use of callbacks, the
375equivalent Python often needs to provide a callback mechanism to the
376Python programmer; the implementation will require calling the Python
377callback functions from a C callback. Other uses are also imaginable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000378
379Fortunately, the Python interpreter is easily called recursively, and
Guido van Rossum6938f061994-08-01 12:22:53 +0000380there is a standard interface to call a Python function. (I won't
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000381dwell on how to call the Python parser with a particular string as
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000382input --- if you're interested, have a look at the implementation of
Guido van Rossum6938f061994-08-01 12:22:53 +0000383the \samp{-c} command line option in \file{Python/pythonmain.c}.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000384
385Calling a Python function is easy. First, the Python program must
386somehow pass you the Python function object. You should provide a
387function (or some other interface) to do this. When this function is
388called, save a pointer to the Python function object (be careful to
Guido van Rossum6938f061994-08-01 12:22:53 +0000389\code{INCREF()} it!) in a global variable --- or whereever you see fit.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000390For example, the following function might be part of a module
391definition:
392
393\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000394 static object *my_callback = NULL;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000395
396 static object *
397 my_set_callback(dummy, arg)
398 object *dummy, *arg;
399 {
400 XDECREF(my_callback); /* Dispose of previous callback */
401 my_callback = arg;
402 XINCREF(my_callback); /* Remember new callback */
403 /* Boilerplate for "void" return */
404 INCREF(None);
405 return None;
406 }
407\end{verbatim}
408
Guido van Rossum6938f061994-08-01 12:22:53 +0000409This particular function doesn't do any typechecking on its argument
410--- that will be done by \code{call_object()}, which is a bit late but
411at least protects the Python interpreter from shooting itself in its
412foot. (The problem with typechecking functions is that there are at
413least five different Python object types that can be called, so the
414test would be somewhat cumbersome.)
415
416The macros \code{XINCREF()} and \code{XDECREF()} increment/decrement
417the reference count of an object and are safe in the presence of
418\code{NULL} pointers. More info on them in the section on Reference
419Counts below.
420
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000421Later, when it is time to call the function, you call the C function
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000422\code{call_object()}. This function has two arguments, both pointers
Guido van Rossum6938f061994-08-01 12:22:53 +0000423to arbitrary Python objects: the Python function, and the argument
424list. The argument list must always be a tuple object, whose length
425is the number of arguments. To call the Python function with no
426arguments, you must pass an empty tuple. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000427
428\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000429 object *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000430 object *result;
431 ...
432 /* Time to call the callback */
Guido van Rossum6938f061994-08-01 12:22:53 +0000433 arglist = mktuple(0);
434 result = call_object(my_callback, arglist);
435 DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000436\end{verbatim}
437
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000438\code{call_object()} returns a Python object pointer: this is
439the return value of the Python function. \code{call_object()} is
Guido van Rossum6938f061994-08-01 12:22:53 +0000440`reference-count-neutral' with respect to its arguments. In the
441example a new tuple was created to serve as the argument list, which
442is \code{DECREF()}-ed immediately after the call.
443
444The return value of \code{call_object()} is `new': either it is a
445brand new object, or it is an existing object whose reference count
446has been incremented. So, unless you want to save it in a global
447variable, you should somehow \code{DECREF()} the result, even
448(especially!) if you are not interested in its value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000449
450Before you do this, however, it is important to check that the return
Guido van Rossum6938f061994-08-01 12:22:53 +0000451value isn't \code{NULL}. If it is, the Python function terminated by raising
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000452an exception. If the C code that called \code{call_object()} is
453called from Python, it should now return an error indication to its
454Python caller, so the interpreter can print a stack trace, or the
455calling Python code can handle the exception. If this is not possible
456or desirable, the exception should be cleared by calling
457\code{err_clear()}. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000458
459\begin{verbatim}
460 if (result == NULL)
461 return NULL; /* Pass error back */
462 /* Here maybe use the result */
463 DECREF(result);
464\end{verbatim}
465
466Depending on the desired interface to the Python callback function,
Guido van Rossum6938f061994-08-01 12:22:53 +0000467you may also have to provide an argument list to \code{call_object()}.
468In some cases the argument list is also provided by the Python
469program, through the same interface that specified the callback
470function. It can then be saved and used in the same manner as the
471function object. In other cases, you may have to construct a new
472tuple to pass as the argument list. The simplest way to do this is to
473call \code{mkvalue()}. For example, if you want to pass an integral
474event code, you might use the following code:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000475
476\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000477 object *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000478 ...
Guido van Rossum6938f061994-08-01 12:22:53 +0000479 arglist = mkvalue("(l)", eventcode);
480 result = call_object(my_callback, arglist);
481 DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000482 if (result == NULL)
483 return NULL; /* Pass error back */
484 /* Here maybe use the result */
485 DECREF(result);
486\end{verbatim}
487
488Note the placement of DECREF(argument) immediately after the call,
489before the error check! Also note that strictly spoken this code is
Guido van Rossum6938f061994-08-01 12:22:53 +0000490not complete: \code{mkvalue()} may run out of memory, and this should
491be checked.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000492
493
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000494\section{Format strings for {\tt getargs()}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000495
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000496The \code{getargs()} function is declared in \file{modsupport.h} as
497follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000498
499\begin{verbatim}
500 int getargs(object *arg, char *format, ...);
501\end{verbatim}
502
503The remaining arguments must be addresses of variables whose type is
504determined by the format string. For the conversion to succeed, the
Guido van Rossum6938f061994-08-01 12:22:53 +0000505\var{arg} object must match the format and the format must be exhausted.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000506Note that while \code{getargs()} checks that the Python object really
Guido van Rossum6938f061994-08-01 12:22:53 +0000507is of the specified type, it cannot check the validity of the
508addresses of C variables provided in the call: if you make mistakes
509there, your code will probably dump core.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000510
Guido van Rossum6938f061994-08-01 12:22:53 +0000511A non-empty format string consists of a single `format unit'. A
512format unit describes one Python object; it is usually a single
513character or a parenthesized sequence of format units. The type of a
514format units is determined from its first character, the `format
515letter':
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000516
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000517\begin{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000518
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000519\item[\samp{s} (string)]
520The Python object must be a string object. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000521\code{(char**)} (i.e. the address of a character pointer), and a pointer
522to the C string contained in the Python object is stored into it. You
523must not provide storage to store the string; a pointer to an existing
524string is stored into the character pointer variable whose address you
525pass. If the next character in the format string is \samp{\#},
526another C argument of type \code{(int*)} must be present, and the
527length of the Python string (not counting the trailing zero byte) is
528stored into it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000529
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000530\item[\samp{z} (string or zero, i.e. \code{NULL})]
531Like \samp{s}, but the object may also be None. In this case the
Guido van Rossum6938f061994-08-01 12:22:53 +0000532string pointer is set to \code{NULL} and if a \samp{\#} is present the
533size is set to 0.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000534
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000535\item[\samp{b} (byte, i.e. char interpreted as tiny int)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000536The object must be a Python integer. The C argument must be a
537\code{(char*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000538
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000539\item[\samp{h} (half, i.e. short)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000540The object must be a Python integer. The C argument must be a
541\code{(short*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000542
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000543\item[\samp{i} (int)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000544The object must be a Python integer. The C argument must be an
545\code{(int*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000546
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000547\item[\samp{l} (long)]
548The object must be a (plain!) Python integer. The C argument must be
Guido van Rossum6938f061994-08-01 12:22:53 +0000549a \code{(long*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000550
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000551\item[\samp{c} (char)]
552The Python object must be a string of length 1. The C argument must
Guido van Rossum6938f061994-08-01 12:22:53 +0000553be a \code{(char*)}. (Don't pass an \code{(int*)}!)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000554
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000555\item[\samp{f} (float)]
556The object must be a Python int or float. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000557\code{(float*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000558
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000559\item[\samp{d} (double)]
560The object must be a Python int or float. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000561\code{(double*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000562
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000563\item[\samp{S} (string object)]
564The object must be a Python string. The C argument must be an
Guido van Rossum6938f061994-08-01 12:22:53 +0000565\code{(object**)} (i.e. the address of an object pointer). The C
566program thus gets back the actual string object that was passed, not
567just a pointer to its array of characters and its size as for format
568character \samp{s}. The reference count of the object has not been
569increased.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000570
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000571\item[\samp{O} (object)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000572The object can be any Python object, including None, but not
573\code{NULL}. The C argument must be an \code{(object**)}. This can be
574used if an argument list must contain objects of a type for which no
575format letter exist: the caller must then check that it has the right
576type. The reference count of the object has not been increased.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000577
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000578\item[\samp{(} (tuple)]
579The object must be a Python tuple. Following the \samp{(} character
580in the format string must come a number of format units describing the
581elements of the tuple, followed by a \samp{)} character. Tuple
582format units may be nested. (There are no exceptions for empty and
583singleton tuples; \samp{()} specifies an empty tuple and \samp{(i)} a
584singleton of one integer. Normally you don't want to use the latter,
Guido van Rossum6938f061994-08-01 12:22:53 +0000585since it is hard for the Python user to specify.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000586
587\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000588
589More format characters will probably be added as the need arises. It
Guido van Rossum6938f061994-08-01 12:22:53 +0000590should (but currently isn't) be allowed to use Python long integers
591whereever integers are expected, and perform a range check. (A range
592check is in fact always necessary for the \samp{b}, \samp{h} and
593\samp{i} format letters, but this is currently not implemented.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000594
595Some example calls:
596
597\begin{verbatim}
598 int ok;
599 int i, j;
600 long k, l;
601 char *s;
602 int size;
603
Guido van Rossum6938f061994-08-01 12:22:53 +0000604 ok = getargs(args, ""); /* No arguments */
605 /* Python call: f() */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000606
607 ok = getargs(args, "s", &s); /* A string */
608 /* Possible Python call: f('whoops!') */
609
Guido van Rossum6938f061994-08-01 12:22:53 +0000610 ok = getargs(args, "(lls)", &k, &l, &s); /* Two longs and a string */
611 /* Possible Python call: f(1, 2, 'three') */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000612
613 ok = getargs(args, "((ii)s#)", &i, &j, &s, &size);
614 /* A pair of ints and a string, whose size is also returned */
615 /* Possible Python call: f(1, 2, 'three') */
616
617 {
618 int left, top, right, bottom, h, v;
619 ok = getargs(args, "(((ii)(ii))(ii))",
620 &left, &top, &right, &bottom, &h, &v);
621 /* A rectangle and a point */
622 /* Possible Python call:
623 f( ((0, 0), (400, 300)), (10, 10)) */
624 }
625\end{verbatim}
626
Guido van Rossum6938f061994-08-01 12:22:53 +0000627Note that the `top level' of a non-empty format string must consist of
628a single unit; strings like \samp{is} and \samp{(ii)s\#} are not valid
629format strings. (But \samp{s\#} is.) If you have multiple arguments,
630the format must therefore always be enclosed in parentheses, as in the
631examples \samp{((ii)s\#)} and \samp{(((ii)(ii))(ii)}. (The current
632implementation does not complain when more than one unparenthesized
633format unit is given. Sorry.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000634
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000635The \code{getargs()} function does not support variable-length
636argument lists. In simple cases you can fake these by trying several
637calls to
638\code{getargs()} until one succeeds, but you must take care to call
639\code{err_clear()} before each retry. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000640
641\begin{verbatim}
642 static object *my_method(self, args) object *self, *args; {
643 int i, j, k;
644
645 if (getargs(args, "(ii)", &i, &j)) {
646 k = 0; /* Use default third argument */
647 }
648 else {
649 err_clear();
650 if (!getargs(args, "(iii)", &i, &j, &k))
651 return NULL;
652 }
653 /* ... use i, j and k here ... */
654 INCREF(None);
655 return None;
656 }
657\end{verbatim}
658
659(It is possible to think of an extension to the definition of format
Guido van Rossum6938f061994-08-01 12:22:53 +0000660strings to accommodate this directly, e.g. placing a \samp{|} in a
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000661tuple might specify that the remaining arguments are optional.
662\code{getargs()} should then return one more than the number of
663variables stored into.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000664
665Advanced users note: If you set the `varargs' flag in the method list
666for a function, the argument will always be a tuple (the `raw argument
667list'). In this case you must enclose single and empty argument lists
Guido van Rossum6938f061994-08-01 12:22:53 +0000668in parentheses, e.g. \samp{(s)} and \samp{()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000669
670
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000671\section{The {\tt mkvalue()} function}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000672
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000673This function is the counterpart to \code{getargs()}. It is declared
Guido van Rossum6938f061994-08-01 12:22:53 +0000674in \file{Include/modsupport.h} as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000675
676\begin{verbatim}
677 object *mkvalue(char *format, ...);
678\end{verbatim}
679
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000680It supports exactly the same format letters as \code{getargs()}, but
681the arguments (which are input to the function, not output) must not
682be pointers, just values. If a byte, short or float is passed to a
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000683varargs function, it is widened by the compiler to int or double, so
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000684\samp{b} and \samp{h} are treated as \samp{i} and \samp{f} is
685treated as \samp{d}. \samp{S} is treated as \samp{O}, \samp{s} is
686treated as \samp{z}. \samp{z\#} and \samp{s\#} are supported: a
687second argument specifies the length of the data (negative means use
688\code{strlen()}). \samp{S} and \samp{O} add a reference to their
689argument (so you should \code{DECREF()} it if you've just created it
690and aren't going to use it again).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000691
Guido van Rossum6938f061994-08-01 12:22:53 +0000692If the argument for \samp{O} or \samp{S} is a \code{NULL} pointer, it is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000693assumed that this was caused because the call producing the argument
694found an error and set an exception. Therefore, \code{mkvalue()} will
695return \code{NULL} but won't set an exception if one is already set.
696If no exception is set, \code{SystemError} is set.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000697
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000698If there is an error in the format string, the \code{SystemError}
699exception is set, since it is the calling C code's fault, not that of
700the Python user who sees the exception.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000701
702Example:
703
704\begin{verbatim}
705 return mkvalue("(ii)", 0, 0);
706\end{verbatim}
707
708returns a tuple containing two zeros. (Outer parentheses in the
709format string are actually superfluous, but you can use them for
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000710compatibility with \code{getargs()}, which requires them if more than
711one argument is expected.)
712
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000713
714\section{Reference counts}
715
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000716Here's a useful explanation of \code{INCREF()} and \code{DECREF()}
717(after an original by Sjoerd Mullender).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000718
Guido van Rossum6938f061994-08-01 12:22:53 +0000719Use \code{XINCREF()} or \code{XDECREF()} instead of \code{INCREF()} or
720\code{DECREF()} when the argument may be \code{NULL} --- the versions
721without \samp{X} are faster but wull dump core when they encounter a
722\code{NULL} pointer.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000723
724The basic idea is, if you create an extra reference to an object, you
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000725must \code{INCREF()} it, if you throw away a reference to an object,
726you must \code{DECREF()} it. Functions such as
727\code{newstringobject()}, \code{newsizedstringobject()},
728\code{newintobject()}, etc. create a reference to an object. If you
729want to throw away the object thus created, you must use
730\code{DECREF()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000731
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000732If you put an object into a tuple or list using \code{settupleitem()}
733or \code{setlistitem()}, the idea is that you usually don't want to
734keep a reference of your own around, so Python does not
735\code{INCREF()} the elements. It does \code{DECREF()} the old value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000736This means that if you put something into such an object using the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000737functions Python provides for this, you must \code{INCREF()} the
738object if you also want to keep a separate reference to the object around.
739Also, if you replace an element, you should \code{INCREF()} the old
740element first if you want to keep it. If you didn't \code{INCREF()}
741it before you replaced it, you are not allowed to look at it anymore,
742since it may have been freed.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000743
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000744Returning an object to Python (i.e. when your C function returns)
745creates a reference to an object, but it does not change the reference
746count. When your code does not keep another reference to the object,
747you should not \code{INCREF()} or \code{DECREF()} it (assuming it is a
748newly created object). When you do keep a reference around, you
749should \code{INCREF()} the object. Also, when you return a global
750object such as \code{None}, you should \code{INCREF()} it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000751
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000752If you want to return a tuple, you should consider using
753\code{mkvalue()}. This function creates a new tuple with a reference
754count of 1 which you can return. If any of the elements you put into
755the tuple are objects (format codes \samp{O} or \samp{S}), they
756are \code{INCREF()}'ed by \code{mkvalue()}. If you don't want to keep
757references to those elements around, you should \code{DECREF()} them
758after having called \code{mkvalue()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000759
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000760Usually you don't have to worry about arguments. They are
761\code{INCREF()}'ed before your function is called and
762\code{DECREF()}'ed after your function returns. When you keep a
763reference to an argument, you should \code{INCREF()} it and
764\code{DECREF()} when you throw it away. Also, when you return an
765argument, you should \code{INCREF()} it, because returning the
766argument creates an extra reference to it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000767
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000768If you use \code{getargs()} to parse the arguments, you can get a
769reference to an object (by using \samp{O} in the format string). This
770object was not \code{INCREF()}'ed, so you should not \code{DECREF()}
771it. If you want to keep the object, you must \code{INCREF()} it
772yourself.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000773
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000774If you create your own type of objects, you should use \code{NEWOBJ()}
775to create the object. This sets the reference count to 1. If you
776want to throw away the object, you should use \code{DECREF()}. When
777the reference count reaches zero, your type's \code{dealloc()}
778function is called. In it, you should \code{DECREF()} all object to
779which you keep references in your object, but you should not use
780\code{DECREF()} on your object. You should use \code{DEL()} instead.
781
782
Guido van Rossum6938f061994-08-01 12:22:53 +0000783\section{Writing extensions in C++}
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000784
785It is possible to write extension modules in C++. Some restrictions
786apply: since the main program (the Python interpreter) is compiled and
787linked by the C compiler, global or static objects with constructors
788cannot be used. All functions that will be called directly or
789indirectly (i.e. via function pointers) by the Python interpreter will
790have to be declared using \code{extern "C"}; this applies to all
791`methods' as well as to the module's initialization function.
792It is unnecessary to enclose the Python header files in
793\code{extern "C" \{...\}} --- they do this already.
794
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000795
796\chapter{Embedding Python in another application}
797
798Embedding Python is similar to extending it, but not quite. The
799difference is that when you extend Python, the main program of the
800application is still the Python interpreter, while of you embed
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000801Python, the main program may have nothing to do with Python ---
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000802instead, some parts of the application occasionally call the Python
803interpreter to run some Python code.
804
805So if you are embedding Python, you are providing your own main
806program. One of the things this main program has to do is initialize
807the Python interpreter. At the very least, you have to call the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000808function \code{initall()}. There are optional calls to pass command
809line arguments to Python. Then later you can call the interpreter
810from any part of the application.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000811
812There are several different ways to call the interpreter: you can pass
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000813a string containing Python statements to \code{run_command()}, or you
814can pass a stdio file pointer and a file name (for identification in
815error messages only) to \code{run_script()}. You can also call the
816lower-level operations described in the previous chapters to construct
817and use Python objects.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000818
819A simple demo of embedding Python can be found in the directory
Guido van Rossum6938f061994-08-01 12:22:53 +0000820\file{Demo/embed}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000821
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000822
Guido van Rossum6938f061994-08-01 12:22:53 +0000823\section{Embedding Python in C++}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000824
825It is also possible to embed Python in a C++ program; how this is done
826exactly will depend on the details of the C++ system used; in general
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000827you will need to write the main program in C++, and use the C++
828compiler to compile and link your program. There is no need to
829recompile Python itself with C++.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000830
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000831
832\chapter{Dynamic Loading}
833
Guido van Rossum6938f061994-08-01 12:22:53 +0000834On most modern systems it is possible to configure Python to support
835dynamic loading of extension modules implemented in C. When shared
836libraries are used dynamic loading is configured automatically;
837otherwise you have to select it as a build option (see below). Once
838configured, dynamic loading is trivial to use: when a Python program
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000839executes \code{import foo}, the search for modules tries to find a
Guido van Rossum6938f061994-08-01 12:22:53 +0000840file \file{foomodule.o} (\file{foomodule.so} when using shared
841libraries) in the module search path, and if one is found, it is
842loaded into the executing binary and executed. Once loaded, the
843module acts just like a built-in extension module.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000844
845The advantages of dynamic loading are twofold: the `core' Python
846binary gets smaller, and users can extend Python with their own
847modules implemented in C without having to build and maintain their
848own copy of the Python interpreter. There are also disadvantages:
849dynamic loading isn't available on all systems (this just means that
850on some systems you have to use static loading), and dynamically
851loading a module that was compiled for a different version of Python
Guido van Rossum6938f061994-08-01 12:22:53 +0000852(e.g. with a different representation of objects) may dump core.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000853
854
855\section{Configuring and building the interpreter for dynamic loading}
856
Guido van Rossum6938f061994-08-01 12:22:53 +0000857There are three styles of dynamic loading: one using shared libraries,
858one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
859loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000860
Guido van Rossum6938f061994-08-01 12:22:53 +0000861\subsection{Shared libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000862
Guido van Rossum6938f061994-08-01 12:22:53 +0000863The following systems supports dynamic loading using shared libraries:
864SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
865systems derived from SVR4, or at least those SVR4 derivatives that
866support shared libraries (are there any that don't?).
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000867
Guido van Rossum6938f061994-08-01 12:22:53 +0000868You don't need to do anything to configure dynamic loading on these
869systems --- the \file{configure} detects the presence of the
870\file{<dlfcn.h>} header file and automatically configures dynamic
871loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000872
Guido van Rossum6938f061994-08-01 12:22:53 +0000873\subsection{SGI dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000874
Guido van Rossum6938f061994-08-01 12:22:53 +0000875Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
876loading. (SGI IRIX 5 might also support it but it is inferior to
877using shared libraries so there is no reason to; a small test didn't
878work right away so I gave up trying to support it.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000879
Guido van Rossum6938f061994-08-01 12:22:53 +0000880Before you build Python, you first need to fetch and build the \code{dl}
881package written by Jack Jansen. This is available by anonymous ftp
882from host \file{ftp.cwi.nl}, directory \file{pub/dynload}, file
883\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
884instructions in the package's \file{README} file to build it.
885
886Once you have built \code{dl}, you can configure Python to use it. To
887this end, you run the \file{configure} script with the option
888\code{--with-dl=\var{directory}} where \var{directory} is the absolute
889pathname of the \code{dl} directory.
890
891Now build and install Python as you normally would (see the
892\file{README} file in the toplevel Python directory.)
893
894\subsection{GNU dynamic loading}
895
896GNU dynamic loading supports (according to its \file{README} file) the
897following hardware and software combinations: VAX (Ultrix), Sun 3
898(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
899Atari ST. There is no reason to use it on a Sparc; I haven't seen a
900Sun 3 for years so I don't know if these have shared libraries or not.
901
902You need to fetch and build two packages. One is GNU DLD 3.2.3,
903available by anonymous ftp from host \file{ftp.cwi.nl}, directory
904\file{pub/dynload}, file \file{dld-3.2.3.tar.Z}. (As far as I know,
905no further development on GNU DLD is being done.) The other is an
906emulation of Jack Jansen's \code{dl} package that I wrote on top of
907GNU DLD 3.2.3. This is available from the same host and directory,
908file dl-dld-1.1.tar.Z. (The version number may change --- but I doubt
909it will.) Follow the instructions in each package's \file{README}
910file to configure build them.
911
912Now configure Python. Run the \file{configure} script with the option
913\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
914\var{dl-directory} is the absolute pathname of the directory where you
915have built the \file{dl-dld} package, and \var{dld-directory} is that
916of the GNU DLD package. The Python interpreter you build hereafter
917will support GNU dynamic loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000918
919
920\section{Building a dynamically loadable module}
921
Guido van Rossum6938f061994-08-01 12:22:53 +0000922Since there are three styles of dynamic loading, there are also three
923groups of instructions for building a dynamically loadable module.
924Instructions common for all three styles are given first. Assuming
925your module is called \code{foo}, the source filename must be
926\file{foomodule.c}, so the object name is \file{foomodule.o}. The
927module must be written as a normal Python extension module (as
928described earlier).
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000929
Guido van Rossum6938f061994-08-01 12:22:53 +0000930Note that in all cases you will have to create your own Makefile that
931compiles your module file(s). This Makefile will have to pass two
932\samp{-I} arguments to the C compiler which will make it find the
933Python header files. If the Make variable \var{PYTHONTOP} points to
934the toplevel Python directory, your \var{CFLAGS} Make variable should
935contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
936(Most header files are in the \file{Include} subdirectory, but the
937\file{config.h} header lives in the toplevel directory.) You must
938also add \samp{-DHAVE_CONFIG_H} to the definition of \var{CFLAGS} to
939direct the Python headers to include \file{config.h}.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000940
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000941
Guido van Rossum6938f061994-08-01 12:22:53 +0000942\subsection{Shared libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000943
Guido van Rossum6938f061994-08-01 12:22:53 +0000944You must link the \samp{.o} file to produce a shared library. This is
945done using a special invocation of the \UNIX{} loader/linker, {\em
946ld}(1). Unfortunately the invocation differs slightly per system.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000947
Guido van Rossum6938f061994-08-01 12:22:53 +0000948On SunOS 4, use
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000949\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000950 ld foomodule.o -o foomodule.so
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000951\end{verbatim}
952
Guido van Rossum6938f061994-08-01 12:22:53 +0000953On Solaris 2, use
954\begin{verbatim}
955 ld -G foomodule.o -o foomodule.so
956\end{verbatim}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000957
Guido van Rossum6938f061994-08-01 12:22:53 +0000958On SGI IRIX 5, use
959\begin{verbatim}
960 ld -shared foomodule.o -o foomodule.so
961\end{verbatim}
962
963On other systems, consult the manual page for {\em ld}(1) to find what
964flags, if any, must be used.
965
966If your extension module uses system libraries that haven't already
967been linked with Python (e.g. a windowing system), these must be
968passed to the {\em ld} command as \samp{-l} options after the
969\samp{.o} file.
970
971The resulting file \file{foomodule.so} must be copied into a directory
972along the Python module search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000973
974
Guido van Rossum6938f061994-08-01 12:22:53 +0000975\subsection{SGI dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000976
Guido van Rossum6938f061994-08-01 12:22:53 +0000977{bf IMPORTANT:} You must compile your extension module with the
978additional C flag \samp{-G0} (or \samp{-G 0}). This instruct the
979assembler to generate position-independent code.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000980
Guido van Rossum6938f061994-08-01 12:22:53 +0000981You don't need to link the resulting \file{foomodule.o} file; just
982copy it into a directory along the Python module search path.
983
984The first time your extension is loaded, it takes some extra time and
985a few messages may be printed. This creates a file
986\file{foomodule.ld} which is an image that can be loaded quickly into
987the Python interpreter process. When a new Python interpreter is
988installed, the \code{dl} package detects this and rebuilds
989\file{foomodule.ld}. The file \file{foomodule.ld} is placed in the
990directory where \file{foomodule.o} was found, unless this directory is
991unwritable; in that case it is placed in a temporary
992directory.\footnote{Check the manual page of the \code{dl} package for
993details.}
994
995If your extension modules uses additional system libraries, you must
996create a file \file{foomodule.libs} in the same directory as the
997\file{foomodule.o}. This file should contain one or more lines with
998whitespace-separated options that will be passed to the linker ---
999normally only \samp{-l} options or absolute pathnames of libraries
1000(\samp{.a} files) should be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001001
1002
Guido van Rossum6938f061994-08-01 12:22:53 +00001003\subsection{GNU dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001004
Guido van Rossum6938f061994-08-01 12:22:53 +00001005Just copy \file{foomodule.o} into a directory along the Python module
1006search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001007
Guido van Rossum6938f061994-08-01 12:22:53 +00001008If your extension modules uses additional system libraries, you must
1009create a file \file{foomodule.libs} in the same directory as the
1010\file{foomodule.o}. This file should contain one or more lines with
1011whitespace-separated absolute pathnames of libraries (\samp{.a}
1012files). No \samp{-l} options can be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001013
1014
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001015\input{ext.ind}
1016
1017\end{document}