blob: 3dc3c45588d0acbf6d614f174c488477b798ccd0 [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
Guido van Rossum16cd7f91994-10-06 10:29:26 +00005\input{boilerplate}
Guido van Rossum83eb9621993-11-23 16:28:45 +00006
Guido van Rossum7a2dba21993-11-05 14:45:11 +00007% Tell \index to actually write the .idx file
8\makeindex
9
10\begin{document}
11
12\pagenumbering{roman}
13
14\maketitle
15
Guido van Rossum16cd7f91994-10-06 10:29:26 +000016\input{copyright}
17
Guido van Rossum7a2dba21993-11-05 14:45:11 +000018\begin{abstract}
19
20\noindent
Guido van Rossum16d6e711994-08-08 12:30:22 +000021This document describes how to write modules in C or \Cpp{} to extend the
Guido van Rossum6f0132f1993-11-19 13:13:22 +000022Python interpreter. It also describes how to use Python as an
23`embedded' language, and how extension modules can be loaded
24dynamically (at run time) into the interpreter, if the operating
25system supports this feature.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000026
27\end{abstract}
28
29\pagebreak
30
31{
32\parskip = 0mm
33\tableofcontents
34}
35
36\pagebreak
37
38\pagenumbering{arabic}
39
Guido van Rossumdb65a6c1993-11-05 17:11:16 +000040
Guido van Rossum16d6e711994-08-08 12:30:22 +000041\chapter{Extending Python with C or \Cpp{} code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000042
Guido van Rossum6f0132f1993-11-19 13:13:22 +000043
44\section{Introduction}
45
Guido van Rossum7a2dba21993-11-05 14:45:11 +000046It is quite easy to add non-standard built-in modules to Python, if
47you know how to program in C. A built-in module known to the Python
Guido van Rossum6f0132f1993-11-19 13:13:22 +000048programmer as \code{foo} is generally implemented by a file called
Guido van Rossum6938f061994-08-01 12:22:53 +000049\file{foomodule.c}. All but the two most essential standard built-in
Guido van Rossum6f0132f1993-11-19 13:13:22 +000050modules also adhere to this convention, and in fact some of them form
51excellent examples of how to create an extension.
Guido van Rossum7a2dba21993-11-05 14:45:11 +000052
53Extension modules can do two things that can't be done directly in
Guido van Rossum6938f061994-08-01 12:22:53 +000054Python: they can implement new data types (which are different from
Guido van Rossum16d6e711994-08-08 12:30:22 +000055classes, by the way), and they can make system calls or call C library
56functions. We'll see how both types of extension are implemented by
57examining the code for a Python curses interface.
Guido van Rossum6938f061994-08-01 12:22:53 +000058
59Note: unless otherwise mentioned, all file references in this
60document are relative to the toplevel directory of the Python
61distribution --- i.e. the directory that contains the \file{configure}
62script.
63
64The compilation of an extension module depends on your system setup
65and the intended use of the module; details are given in a later
66section.
67
68
69\section{A first look at the code}
Guido van Rossum7a2dba21993-11-05 14:45:11 +000070
71It is important not to be impressed by the size and complexity of
72the average extension module; much of this is straightforward
Guido van Rossum6f0132f1993-11-19 13:13:22 +000073`boilerplate' code (starting right with the copyright notice)!
Guido van Rossum7a2dba21993-11-05 14:45:11 +000074
Guido van Rossum6f0132f1993-11-19 13:13:22 +000075Let's skip the boilerplate and have a look at an interesting function
76in \file{posixmodule.c} first:
Guido van Rossum7a2dba21993-11-05 14:45:11 +000077
78\begin{verbatim}
79 static object *
80 posix_system(self, args)
81 object *self;
82 object *args;
83 {
84 char *command;
85 int sts;
86 if (!getargs(args, "s", &command))
87 return NULL;
88 sts = system(command);
Guido van Rossum6f0132f1993-11-19 13:13:22 +000089 return mkvalue("i", sts);
Guido van Rossum7a2dba21993-11-05 14:45:11 +000090 }
91\end{verbatim}
92
93This is the prototypical top-level function in an extension module.
Guido van Rossum6938f061994-08-01 12:22:53 +000094It will be called (we'll see later how) when the Python program
95executes statements like
Guido van Rossum7a2dba21993-11-05 14:45:11 +000096
97\begin{verbatim}
98 >>> import posix
99 >>> sts = posix.system('ls -l')
100\end{verbatim}
101
102There is a straightforward translation from the arguments to the call
Guido van Rossum6938f061994-08-01 12:22:53 +0000103in Python (here the single expression \code{'ls -l'}) to the arguments that
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000104are passed to the C function. The C function always has two
Guido van Rossum6938f061994-08-01 12:22:53 +0000105parameters, conventionally named \var{self} and \var{args}. The
106\var{self} argument is used when the C function implements a builtin
Guido van Rossum16d6e711994-08-08 12:30:22 +0000107method---this will be discussed later.
Guido van Rossum6938f061994-08-01 12:22:53 +0000108In the example, \var{self} will always be a \code{NULL} pointer, since
109we are defining a function, not a method (this is done so that the
110interpreter doesn't have to understand two different types of C
111functions).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000112
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000113The \var{args} parameter will be a pointer to a Python object, or
114\code{NULL} if the Python function/method was called without
115arguments. It is necessary to do full argument type checking on each
116call, since otherwise the Python user would be able to cause the
Guido van Rossum6938f061994-08-01 12:22:53 +0000117Python interpreter to `dump core' by passing invalid arguments to a
118function in an extension module. Because argument checking and
119converting arguments to C are such common tasks, there's a general
120function in the Python interpreter that combines them:
121\code{getargs()}. It uses a template string to determine both the
122types of the Python argument and the types of the C variables into
123which it should store the converted values.\footnote{There are
124convenience macros \code{getnoarg()}, \code{getstrarg()},
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000125\code{getintarg()}, etc., for many common forms of \code{getargs()}
Guido van Rossum6938f061994-08-01 12:22:53 +0000126templates. These are relics from the past; the recommended practice
127is to call \code{getargs()} directly.} (More about this later.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000128
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000129If \code{getargs()} returns nonzero, the argument list has the right
130type and its components have been stored in the variables whose
131addresses are passed. If it returns zero, an error has occurred. In
Guido van Rossum6938f061994-08-01 12:22:53 +0000132the latter case it has already raised an appropriate exception by so
133the calling function should return \code{NULL} immediately --- see the
134next section.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000135
136
137\section{Intermezzo: errors and exceptions}
138
139An important convention throughout the Python interpreter is the
140following: when a function fails, it should set an exception condition
Guido van Rossum6938f061994-08-01 12:22:53 +0000141and return an error value (often a \code{NULL} pointer). Exceptions
142are stored in a static global variable in \file{Python/errors.c}; if
143this variable is \code{NULL} no exception has occurred. A second
144static global variable stores the `associated value' of the exception
145--- the second argument to \code{raise}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000146
Guido van Rossum6938f061994-08-01 12:22:53 +0000147The file \file{errors.h} declares a host of functions to set various
148types of exceptions. The most common one is \code{err_setstr()} ---
149its arguments are an exception object (e.g. \code{RuntimeError} ---
150actually it can be any string object) and a C string indicating the
151cause of the error (this is converted to a string object and stored as
152the `associated value' of the exception). Another useful function is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000153\code{err_errno()}, which only takes an exception argument and
154constructs the associated value by inspection of the (UNIX) global
Guido van Rossum6938f061994-08-01 12:22:53 +0000155variable errno. The most general function is \code{err_set()}, which
156takes two object arguments, the exception and its associated value.
157You don't need to \code{INCREF()} the objects passed to any of these
158functions.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000159
160You can test non-destructively whether an exception has been set with
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000161\code{err_occurred()}. However, most code never calls
162\code{err_occurred()} to see whether an error occurred or not, but
Guido van Rossum6938f061994-08-01 12:22:53 +0000163relies on error return values from the functions it calls instead.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000164
165When a function that calls another function detects that the called
Guido van Rossum6938f061994-08-01 12:22:53 +0000166function fails, it should return an error value (e.g. \code{NULL} or
167\code{-1}) but not call one of the \code{err_*} functions --- one has
168already been called. The caller is then supposed to also return an
169error indication to {\em its} caller, again {\em without} calling
170\code{err_*()}, and so on --- the most detailed cause of the error was
171already reported by the function that first detected it. Once the
172error has reached Python's interpreter main loop, this aborts the
173currently executing Python code and tries to find an exception handler
174specified by the Python programmer.
175
176(There are situations where a module can actually give a more detailed
177error message by calling another \code{err_*} function, and in such
178cases it is fine to do so. As a general rule, however, this is not
179necessary, and can cause information about the cause of the error to
180be lost: most operations can fail for a variety of reasons.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000181
182To ignore an exception set by a function call that failed, the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000183exception condition must be cleared explicitly by calling
184\code{err_clear()}. The only time C code should call
185\code{err_clear()} is if it doesn't want to pass the error on to the
186interpreter but wants to handle it completely by itself (e.g. by
187trying something else or pretending nothing happened).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000188
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000189Finally, the function \code{err_get()} gives you both error variables
Guido van Rossum6938f061994-08-01 12:22:53 +0000190{\em and clears them}. Note that even if an error occurred the second
191one may be \code{NULL}. You have to \code{XDECREF()} both when you
192are finished with them. I doubt you will need to use this function.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000193
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000194Note that a failing \code{malloc()} call must also be turned into an
195exception --- the direct caller of \code{malloc()} (or
196\code{realloc()}) must call \code{err_nomem()} and return a failure
197indicator itself. All the object-creating functions
198(\code{newintobject()} etc.) already do this, so only if you call
199\code{malloc()} directly this note is of importance.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000200
Guido van Rossum6938f061994-08-01 12:22:53 +0000201Also note that, with the important exception of \code{getargs()},
202functions that return an integer status usually return \code{0} or a
203positive value for success and \code{-1} for failure.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000204
Guido van Rossum6938f061994-08-01 12:22:53 +0000205Finally, be careful about cleaning up garbage (making \code{XDECREF()}
206or \code{DECREF()} calls for objects you have already created) when
207you return an error!
208
209The choice of which exception to raise is entirely yours. There are
210predeclared C objects corresponding to all built-in Python exceptions,
211e.g. \code{ZeroDevisionError} which you can use directly. Of course,
212you should chose exceptions wisely --- don't use \code{TypeError} to
213mean that a file couldn't be opened (that should probably be
214\code{IOError}). If anything's wrong with the argument list the
215\code{getargs()} function raises \code{TypeError}. If you have an
216argument whose value which must be in a particular range or must
217satisfy other conditions, \code{ValueError} is appropriate.
218
219You can also define a new exception that is unique to your module.
220For this, you usually declare a static object variable at the
221beginning of your file, e.g.
222
223\begin{verbatim}
224 static object *FooError;
225\end{verbatim}
226
227and initialize it in your module's initialization function
228(\code{initfoo()}) with a string object, e.g. (leaving out the error
229checking for simplicity):
230
231\begin{verbatim}
232 void
233 initfoo()
234 {
235 object *m, *d;
236 m = initmodule("foo", foo_methods);
237 d = getmoduledict(m);
238 FooError = newstringobject("foo.error");
239 dictinsert(d, "error", FooError);
240 }
241\end{verbatim}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000242
243
244\section{Back to the example}
245
Guido van Rossum6938f061994-08-01 12:22:53 +0000246Going back to \code{posix_system()}, you should now be able to
247understand this bit:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000248
249\begin{verbatim}
250 if (!getargs(args, "s", &command))
251 return NULL;
252\end{verbatim}
253
Guido van Rossum6938f061994-08-01 12:22:53 +0000254It returns \code{NULL} (the error indicator for functions of this
255kind) if an error is detected in the argument list, relying on the
256exception set by \code{getargs()}. Otherwise the string value of the
257argument has been copied to the local variable \code{command} --- this
258is in fact just a pointer assignment and you are not supposed to
259modify the string to which it points.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000260
Guido van Rossum6938f061994-08-01 12:22:53 +0000261If a function is called with multiple arguments, the argument list
262(the argument \code{args}) is turned into a tuple. If it is called
263without arguments, \code{args} is \code{NULL}. \code{getargs()} knows
264about this; see later.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000265
Guido van Rossum6938f061994-08-01 12:22:53 +0000266The next statement in \code{posix_system()} is a call to the C library
267function \code{system()}, passing it the string we just got from
268\code{getargs()}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000269
270\begin{verbatim}
271 sts = system(command);
272\end{verbatim}
273
Guido van Rossum6938f061994-08-01 12:22:53 +0000274Finally, \code{posix.system()} must return a value: the integer status
275returned by the C library \code{system()} function. This is done
276using the function \code{mkvalue()}, which is something like the
277inverse of \code{getargs()}: it takes a format string and a variable
278number of C values and returns a new Python object.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000279
280\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000281 return mkvalue("i", sts);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000282\end{verbatim}
283
Guido van Rossum6938f061994-08-01 12:22:53 +0000284In this case, it returns an integer object (yes, even integers are
285objects on the heap in Python!). More info on \code{mkvalue()} is
286given later.
287
288If you had a function that returned no useful argument (a.k.a. a
289procedure), you would need this idiom:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000290
291\begin{verbatim}
292 INCREF(None);
293 return None;
294\end{verbatim}
295
Guido van Rossum6938f061994-08-01 12:22:53 +0000296\code{None} is a unique Python object representing `no value'. It
297differs from \code{NULL}, which means `error' in most contexts.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000298
299
300\section{The module's function table}
301
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000302I promised to show how I made the function \code{posix_system()}
Guido van Rossum6938f061994-08-01 12:22:53 +0000303callable from Python programs. This is shown later in
304\file{Modules/posixmodule.c}:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000305
306\begin{verbatim}
307 static struct methodlist posix_methods[] = {
308 ...
309 {"system", posix_system},
310 ...
311 {NULL, NULL} /* Sentinel */
312 };
313
314 void
315 initposix()
316 {
317 (void) initmodule("posix", posix_methods);
318 }
319\end{verbatim}
320
Guido van Rossum6938f061994-08-01 12:22:53 +0000321(The actual \code{initposix()} is somewhat more complicated, but many
322extension modules can be as simple as shown here.) When the Python
323program first imports module \code{posix}, \code{initposix()} is
324called, which calls \code{initmodule()} with specific parameters.
325This creates a `module object' (which is inserted in the table
326\code{sys.modules} under the key \code{'posix'}), and adds
327built-in-function objects to the newly created module based upon the
328table (of type struct methodlist) that was passed as its second
329parameter. The function \code{initmodule()} returns a pointer to the
330module object that it creates (which is unused here). It aborts with
331a fatal error if the module could not be initialized satisfactorily,
332so you don't need to check for errors.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000333
334
Guido van Rossum6938f061994-08-01 12:22:53 +0000335\section{Compilation and linkage}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000336
Guido van Rossum6938f061994-08-01 12:22:53 +0000337There are two more things to do before you can use your new extension
338module: compiling and linking it with the Python system. If you use
339dynamic loading, the details depend on the style of dynamic loading
340your system uses; see the chapter on Dynamic Loading for more info
341about this.
342
343If you can't use dynamic loading, or if you want to make your module a
344permanent part of the Python interpreter, you will have to change the
345configuration setup and rebuild the interpreter. Luckily, in the 1.0
346release this is very simple: just place your file (named
347\file{foomodule.c} for example) in the \file{Modules} directory, add a
348line to the file \file{Modules/Setup} describing your file:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000349
350\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000351 foo foomodule.o
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000352\end{verbatim}
353
Guido van Rossum6938f061994-08-01 12:22:53 +0000354and rebuild the interpreter by running \code{make} in the toplevel
355directory. You can also run \code{make} in the \file{Modules}
356subdirectory, but then you must first rebuilt the \file{Makefile}
357there by running \code{make Makefile}. (This is necessary each time
358you change the \file{Setup} file.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000359
360
361\section{Calling Python functions from C}
362
Guido van Rossum6938f061994-08-01 12:22:53 +0000363So far we have concentrated on making C functions callable from
364Python. The reverse is also useful: calling Python functions from C.
365This is especially the case for libraries that support so-called
366`callback' functions. If a C interface makes use of callbacks, the
367equivalent Python often needs to provide a callback mechanism to the
368Python programmer; the implementation will require calling the Python
369callback functions from a C callback. Other uses are also imaginable.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000370
371Fortunately, the Python interpreter is easily called recursively, and
Guido van Rossum6938f061994-08-01 12:22:53 +0000372there is a standard interface to call a Python function. (I won't
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000373dwell on how to call the Python parser with a particular string as
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000374input --- if you're interested, have a look at the implementation of
Guido van Rossum6938f061994-08-01 12:22:53 +0000375the \samp{-c} command line option in \file{Python/pythonmain.c}.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000376
377Calling a Python function is easy. First, the Python program must
378somehow pass you the Python function object. You should provide a
379function (or some other interface) to do this. When this function is
380called, save a pointer to the Python function object (be careful to
Guido van Rossum6938f061994-08-01 12:22:53 +0000381\code{INCREF()} it!) in a global variable --- or whereever you see fit.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000382For example, the following function might be part of a module
383definition:
384
385\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000386 static object *my_callback = NULL;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000387
388 static object *
389 my_set_callback(dummy, arg)
390 object *dummy, *arg;
391 {
392 XDECREF(my_callback); /* Dispose of previous callback */
393 my_callback = arg;
394 XINCREF(my_callback); /* Remember new callback */
395 /* Boilerplate for "void" return */
396 INCREF(None);
397 return None;
398 }
399\end{verbatim}
400
Guido van Rossum6938f061994-08-01 12:22:53 +0000401This particular function doesn't do any typechecking on its argument
402--- that will be done by \code{call_object()}, which is a bit late but
403at least protects the Python interpreter from shooting itself in its
404foot. (The problem with typechecking functions is that there are at
405least five different Python object types that can be called, so the
406test would be somewhat cumbersome.)
407
408The macros \code{XINCREF()} and \code{XDECREF()} increment/decrement
409the reference count of an object and are safe in the presence of
410\code{NULL} pointers. More info on them in the section on Reference
411Counts below.
412
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000413Later, when it is time to call the function, you call the C function
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000414\code{call_object()}. This function has two arguments, both pointers
Guido van Rossum6938f061994-08-01 12:22:53 +0000415to arbitrary Python objects: the Python function, and the argument
416list. The argument list must always be a tuple object, whose length
417is the number of arguments. To call the Python function with no
418arguments, you must pass an empty tuple. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000419
420\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000421 object *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000422 object *result;
423 ...
424 /* Time to call the callback */
Guido van Rossum16cd7f91994-10-06 10:29:26 +0000425 arglist = newtupleobject(0);
Guido van Rossum6938f061994-08-01 12:22:53 +0000426 result = call_object(my_callback, arglist);
427 DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000428\end{verbatim}
429
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000430\code{call_object()} returns a Python object pointer: this is
431the return value of the Python function. \code{call_object()} is
Guido van Rossum6938f061994-08-01 12:22:53 +0000432`reference-count-neutral' with respect to its arguments. In the
433example a new tuple was created to serve as the argument list, which
434is \code{DECREF()}-ed immediately after the call.
435
436The return value of \code{call_object()} is `new': either it is a
437brand new object, or it is an existing object whose reference count
438has been incremented. So, unless you want to save it in a global
439variable, you should somehow \code{DECREF()} the result, even
440(especially!) if you are not interested in its value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000441
442Before you do this, however, it is important to check that the return
Guido van Rossum6938f061994-08-01 12:22:53 +0000443value isn't \code{NULL}. If it is, the Python function terminated by raising
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000444an exception. If the C code that called \code{call_object()} is
445called from Python, it should now return an error indication to its
446Python caller, so the interpreter can print a stack trace, or the
447calling Python code can handle the exception. If this is not possible
448or desirable, the exception should be cleared by calling
449\code{err_clear()}. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000450
451\begin{verbatim}
452 if (result == NULL)
453 return NULL; /* Pass error back */
454 /* Here maybe use the result */
455 DECREF(result);
456\end{verbatim}
457
458Depending on the desired interface to the Python callback function,
Guido van Rossum6938f061994-08-01 12:22:53 +0000459you may also have to provide an argument list to \code{call_object()}.
460In some cases the argument list is also provided by the Python
461program, through the same interface that specified the callback
462function. It can then be saved and used in the same manner as the
463function object. In other cases, you may have to construct a new
464tuple to pass as the argument list. The simplest way to do this is to
465call \code{mkvalue()}. For example, if you want to pass an integral
466event code, you might use the following code:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000467
468\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000469 object *arglist;
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000470 ...
Guido van Rossum6938f061994-08-01 12:22:53 +0000471 arglist = mkvalue("(l)", eventcode);
472 result = call_object(my_callback, arglist);
473 DECREF(arglist);
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000474 if (result == NULL)
475 return NULL; /* Pass error back */
476 /* Here maybe use the result */
477 DECREF(result);
478\end{verbatim}
479
480Note the placement of DECREF(argument) immediately after the call,
481before the error check! Also note that strictly spoken this code is
Guido van Rossum6938f061994-08-01 12:22:53 +0000482not complete: \code{mkvalue()} may run out of memory, and this should
483be checked.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000484
485
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000486\section{Format strings for {\tt getargs()}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000487
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000488The \code{getargs()} function is declared in \file{modsupport.h} as
489follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000490
491\begin{verbatim}
492 int getargs(object *arg, char *format, ...);
493\end{verbatim}
494
495The remaining arguments must be addresses of variables whose type is
496determined by the format string. For the conversion to succeed, the
Guido van Rossum6938f061994-08-01 12:22:53 +0000497\var{arg} object must match the format and the format must be exhausted.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000498Note that while \code{getargs()} checks that the Python object really
Guido van Rossum6938f061994-08-01 12:22:53 +0000499is of the specified type, it cannot check the validity of the
500addresses of C variables provided in the call: if you make mistakes
501there, your code will probably dump core.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000502
Guido van Rossum6938f061994-08-01 12:22:53 +0000503A non-empty format string consists of a single `format unit'. A
504format unit describes one Python object; it is usually a single
505character or a parenthesized sequence of format units. The type of a
506format units is determined from its first character, the `format
507letter':
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000508
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000509\begin{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000510
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000511\item[\samp{s} (string)]
512The Python object must be a string object. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000513\code{(char**)} (i.e. the address of a character pointer), and a pointer
514to the C string contained in the Python object is stored into it. You
515must not provide storage to store the string; a pointer to an existing
516string is stored into the character pointer variable whose address you
517pass. If the next character in the format string is \samp{\#},
518another C argument of type \code{(int*)} must be present, and the
519length of the Python string (not counting the trailing zero byte) is
520stored into it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000521
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000522\item[\samp{z} (string or zero, i.e. \code{NULL})]
523Like \samp{s}, but the object may also be None. In this case the
Guido van Rossum6938f061994-08-01 12:22:53 +0000524string pointer is set to \code{NULL} and if a \samp{\#} is present the
525size is set to 0.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000526
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000527\item[\samp{b} (byte, i.e. char interpreted as tiny int)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000528The object must be a Python integer. The C argument must be a
529\code{(char*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000530
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000531\item[\samp{h} (half, i.e. short)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000532The object must be a Python integer. The C argument must be a
533\code{(short*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000534
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000535\item[\samp{i} (int)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000536The object must be a Python integer. The C argument must be an
537\code{(int*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000538
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000539\item[\samp{l} (long)]
540The object must be a (plain!) Python integer. The C argument must be
Guido van Rossum6938f061994-08-01 12:22:53 +0000541a \code{(long*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000542
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000543\item[\samp{c} (char)]
544The Python object must be a string of length 1. The C argument must
Guido van Rossum6938f061994-08-01 12:22:53 +0000545be a \code{(char*)}. (Don't pass an \code{(int*)}!)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000546
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000547\item[\samp{f} (float)]
548The object must be a Python int or float. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000549\code{(float*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000550
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000551\item[\samp{d} (double)]
552The object must be a Python int or float. The C argument must be a
Guido van Rossum6938f061994-08-01 12:22:53 +0000553\code{(double*)}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000554
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000555\item[\samp{S} (string object)]
556The object must be a Python string. The C argument must be an
Guido van Rossum6938f061994-08-01 12:22:53 +0000557\code{(object**)} (i.e. the address of an object pointer). The C
558program thus gets back the actual string object that was passed, not
559just a pointer to its array of characters and its size as for format
560character \samp{s}. The reference count of the object has not been
561increased.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000562
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000563\item[\samp{O} (object)]
Guido van Rossum6938f061994-08-01 12:22:53 +0000564The object can be any Python object, including None, but not
565\code{NULL}. The C argument must be an \code{(object**)}. This can be
566used if an argument list must contain objects of a type for which no
567format letter exist: the caller must then check that it has the right
568type. The reference count of the object has not been increased.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000569
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000570\item[\samp{(} (tuple)]
571The object must be a Python tuple. Following the \samp{(} character
572in the format string must come a number of format units describing the
573elements of the tuple, followed by a \samp{)} character. Tuple
574format units may be nested. (There are no exceptions for empty and
575singleton tuples; \samp{()} specifies an empty tuple and \samp{(i)} a
576singleton of one integer. Normally you don't want to use the latter,
Guido van Rossum6938f061994-08-01 12:22:53 +0000577since it is hard for the Python user to specify.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000578
579\end{description}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000580
581More format characters will probably be added as the need arises. It
Guido van Rossum6938f061994-08-01 12:22:53 +0000582should (but currently isn't) be allowed to use Python long integers
583whereever integers are expected, and perform a range check. (A range
584check is in fact always necessary for the \samp{b}, \samp{h} and
585\samp{i} format letters, but this is currently not implemented.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000586
587Some example calls:
588
589\begin{verbatim}
590 int ok;
591 int i, j;
592 long k, l;
593 char *s;
594 int size;
595
Guido van Rossum6938f061994-08-01 12:22:53 +0000596 ok = getargs(args, ""); /* No arguments */
597 /* Python call: f() */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000598
599 ok = getargs(args, "s", &s); /* A string */
600 /* Possible Python call: f('whoops!') */
601
Guido van Rossum6938f061994-08-01 12:22:53 +0000602 ok = getargs(args, "(lls)", &k, &l, &s); /* Two longs and a string */
603 /* Possible Python call: f(1, 2, 'three') */
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000604
605 ok = getargs(args, "((ii)s#)", &i, &j, &s, &size);
606 /* A pair of ints and a string, whose size is also returned */
607 /* Possible Python call: f(1, 2, 'three') */
608
609 {
610 int left, top, right, bottom, h, v;
611 ok = getargs(args, "(((ii)(ii))(ii))",
612 &left, &top, &right, &bottom, &h, &v);
613 /* A rectangle and a point */
614 /* Possible Python call:
615 f( ((0, 0), (400, 300)), (10, 10)) */
616 }
617\end{verbatim}
618
Guido van Rossum6938f061994-08-01 12:22:53 +0000619Note that the `top level' of a non-empty format string must consist of
620a single unit; strings like \samp{is} and \samp{(ii)s\#} are not valid
621format strings. (But \samp{s\#} is.) If you have multiple arguments,
622the format must therefore always be enclosed in parentheses, as in the
623examples \samp{((ii)s\#)} and \samp{(((ii)(ii))(ii)}. (The current
624implementation does not complain when more than one unparenthesized
625format unit is given. Sorry.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000626
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000627The \code{getargs()} function does not support variable-length
628argument lists. In simple cases you can fake these by trying several
629calls to
630\code{getargs()} until one succeeds, but you must take care to call
631\code{err_clear()} before each retry. For example:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000632
633\begin{verbatim}
634 static object *my_method(self, args) object *self, *args; {
635 int i, j, k;
636
637 if (getargs(args, "(ii)", &i, &j)) {
638 k = 0; /* Use default third argument */
639 }
640 else {
641 err_clear();
642 if (!getargs(args, "(iii)", &i, &j, &k))
643 return NULL;
644 }
645 /* ... use i, j and k here ... */
646 INCREF(None);
647 return None;
648 }
649\end{verbatim}
650
651(It is possible to think of an extension to the definition of format
Guido van Rossum6938f061994-08-01 12:22:53 +0000652strings to accommodate this directly, e.g. placing a \samp{|} in a
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000653tuple might specify that the remaining arguments are optional.
654\code{getargs()} should then return one more than the number of
655variables stored into.)
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000656
657Advanced users note: If you set the `varargs' flag in the method list
658for a function, the argument will always be a tuple (the `raw argument
659list'). In this case you must enclose single and empty argument lists
Guido van Rossum6938f061994-08-01 12:22:53 +0000660in parentheses, e.g. \samp{(s)} and \samp{()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000661
662
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000663\section{The {\tt mkvalue()} function}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000664
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000665This function is the counterpart to \code{getargs()}. It is declared
Guido van Rossum6938f061994-08-01 12:22:53 +0000666in \file{Include/modsupport.h} as follows:
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000667
668\begin{verbatim}
669 object *mkvalue(char *format, ...);
670\end{verbatim}
671
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000672It supports exactly the same format letters as \code{getargs()}, but
673the arguments (which are input to the function, not output) must not
674be pointers, just values. If a byte, short or float is passed to a
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000675varargs function, it is widened by the compiler to int or double, so
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000676\samp{b} and \samp{h} are treated as \samp{i} and \samp{f} is
677treated as \samp{d}. \samp{S} is treated as \samp{O}, \samp{s} is
678treated as \samp{z}. \samp{z\#} and \samp{s\#} are supported: a
679second argument specifies the length of the data (negative means use
680\code{strlen()}). \samp{S} and \samp{O} add a reference to their
681argument (so you should \code{DECREF()} it if you've just created it
682and aren't going to use it again).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000683
Guido van Rossum6938f061994-08-01 12:22:53 +0000684If the argument for \samp{O} or \samp{S} is a \code{NULL} pointer, it is
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000685assumed that this was caused because the call producing the argument
686found an error and set an exception. Therefore, \code{mkvalue()} will
687return \code{NULL} but won't set an exception if one is already set.
688If no exception is set, \code{SystemError} is set.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000689
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000690If there is an error in the format string, the \code{SystemError}
691exception is set, since it is the calling C code's fault, not that of
692the Python user who sees the exception.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000693
694Example:
695
696\begin{verbatim}
697 return mkvalue("(ii)", 0, 0);
698\end{verbatim}
699
700returns a tuple containing two zeros. (Outer parentheses in the
701format string are actually superfluous, but you can use them for
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000702compatibility with \code{getargs()}, which requires them if more than
703one argument is expected.)
704
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000705
706\section{Reference counts}
707
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000708Here's a useful explanation of \code{INCREF()} and \code{DECREF()}
709(after an original by Sjoerd Mullender).
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000710
Guido van Rossum6938f061994-08-01 12:22:53 +0000711Use \code{XINCREF()} or \code{XDECREF()} instead of \code{INCREF()} or
712\code{DECREF()} when the argument may be \code{NULL} --- the versions
713without \samp{X} are faster but wull dump core when they encounter a
714\code{NULL} pointer.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000715
716The basic idea is, if you create an extra reference to an object, you
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000717must \code{INCREF()} it, if you throw away a reference to an object,
718you must \code{DECREF()} it. Functions such as
719\code{newstringobject()}, \code{newsizedstringobject()},
720\code{newintobject()}, etc. create a reference to an object. If you
721want to throw away the object thus created, you must use
722\code{DECREF()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000723
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000724If you put an object into a tuple or list using \code{settupleitem()}
725or \code{setlistitem()}, the idea is that you usually don't want to
726keep a reference of your own around, so Python does not
727\code{INCREF()} the elements. It does \code{DECREF()} the old value.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000728This means that if you put something into such an object using the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000729functions Python provides for this, you must \code{INCREF()} the
730object if you also want to keep a separate reference to the object around.
731Also, if you replace an element, you should \code{INCREF()} the old
732element first if you want to keep it. If you didn't \code{INCREF()}
733it before you replaced it, you are not allowed to look at it anymore,
734since it may have been freed.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000735
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000736Returning an object to Python (i.e. when your C function returns)
737creates a reference to an object, but it does not change the reference
738count. When your code does not keep another reference to the object,
739you should not \code{INCREF()} or \code{DECREF()} it (assuming it is a
740newly created object). When you do keep a reference around, you
741should \code{INCREF()} the object. Also, when you return a global
742object such as \code{None}, you should \code{INCREF()} it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000743
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000744If you want to return a tuple, you should consider using
745\code{mkvalue()}. This function creates a new tuple with a reference
746count of 1 which you can return. If any of the elements you put into
747the tuple are objects (format codes \samp{O} or \samp{S}), they
748are \code{INCREF()}'ed by \code{mkvalue()}. If you don't want to keep
749references to those elements around, you should \code{DECREF()} them
750after having called \code{mkvalue()}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000751
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000752Usually you don't have to worry about arguments. They are
753\code{INCREF()}'ed before your function is called and
754\code{DECREF()}'ed after your function returns. When you keep a
755reference to an argument, you should \code{INCREF()} it and
756\code{DECREF()} when you throw it away. Also, when you return an
757argument, you should \code{INCREF()} it, because returning the
758argument creates an extra reference to it.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000759
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000760If you use \code{getargs()} to parse the arguments, you can get a
761reference to an object (by using \samp{O} in the format string). This
762object was not \code{INCREF()}'ed, so you should not \code{DECREF()}
763it. If you want to keep the object, you must \code{INCREF()} it
764yourself.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000765
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000766If you create your own type of objects, you should use \code{NEWOBJ()}
767to create the object. This sets the reference count to 1. If you
768want to throw away the object, you should use \code{DECREF()}. When
769the reference count reaches zero, your type's \code{dealloc()}
770function is called. In it, you should \code{DECREF()} all object to
771which you keep references in your object, but you should not use
772\code{DECREF()} on your object. You should use \code{DEL()} instead.
773
774
Guido van Rossum16d6e711994-08-08 12:30:22 +0000775\section{Writing extensions in \Cpp{}}
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000776
Guido van Rossum16d6e711994-08-08 12:30:22 +0000777It is possible to write extension modules in \Cpp{}. Some restrictions
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000778apply: since the main program (the Python interpreter) is compiled and
779linked by the C compiler, global or static objects with constructors
780cannot be used. All functions that will be called directly or
781indirectly (i.e. via function pointers) by the Python interpreter will
782have to be declared using \code{extern "C"}; this applies to all
783`methods' as well as to the module's initialization function.
784It is unnecessary to enclose the Python header files in
785\code{extern "C" \{...\}} --- they do this already.
786
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000787
788\chapter{Embedding Python in another application}
789
790Embedding Python is similar to extending it, but not quite. The
791difference is that when you extend Python, the main program of the
Guido van Rossum16d6e711994-08-08 12:30:22 +0000792application is still the Python interpreter, while if you embed
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000793Python, the main program may have nothing to do with Python ---
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000794instead, some parts of the application occasionally call the Python
795interpreter to run some Python code.
796
797So if you are embedding Python, you are providing your own main
798program. One of the things this main program has to do is initialize
799the Python interpreter. At the very least, you have to call the
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000800function \code{initall()}. There are optional calls to pass command
801line arguments to Python. Then later you can call the interpreter
802from any part of the application.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000803
804There are several different ways to call the interpreter: you can pass
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000805a string containing Python statements to \code{run_command()}, or you
806can pass a stdio file pointer and a file name (for identification in
807error messages only) to \code{run_script()}. You can also call the
808lower-level operations described in the previous chapters to construct
809and use Python objects.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000810
811A simple demo of embedding Python can be found in the directory
Guido van Rossum6938f061994-08-01 12:22:53 +0000812\file{Demo/embed}.
Guido van Rossumdb65a6c1993-11-05 17:11:16 +0000813
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000814
Guido van Rossum16d6e711994-08-08 12:30:22 +0000815\section{Embedding Python in \Cpp{}}
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000816
Guido van Rossum16d6e711994-08-08 12:30:22 +0000817It is also possible to embed Python in a \Cpp{} program; precisely how this
818is done will depend on the details of the \Cpp{} system used; in general you
819will need to write the main program in \Cpp{}, and use the \Cpp{} compiler
820to compile and link your program. There is no need to recompile Python
821itself using \Cpp{}.
Guido van Rossum7a2dba21993-11-05 14:45:11 +0000822
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000823
824\chapter{Dynamic Loading}
825
Guido van Rossum6938f061994-08-01 12:22:53 +0000826On most modern systems it is possible to configure Python to support
827dynamic loading of extension modules implemented in C. When shared
828libraries are used dynamic loading is configured automatically;
829otherwise you have to select it as a build option (see below). Once
830configured, dynamic loading is trivial to use: when a Python program
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000831executes \code{import foo}, the search for modules tries to find a
Guido van Rossum6938f061994-08-01 12:22:53 +0000832file \file{foomodule.o} (\file{foomodule.so} when using shared
833libraries) in the module search path, and if one is found, it is
834loaded into the executing binary and executed. Once loaded, the
835module acts just like a built-in extension module.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000836
837The advantages of dynamic loading are twofold: the `core' Python
838binary gets smaller, and users can extend Python with their own
839modules implemented in C without having to build and maintain their
840own copy of the Python interpreter. There are also disadvantages:
841dynamic loading isn't available on all systems (this just means that
842on some systems you have to use static loading), and dynamically
843loading a module that was compiled for a different version of Python
Guido van Rossum6938f061994-08-01 12:22:53 +0000844(e.g. with a different representation of objects) may dump core.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000845
846
847\section{Configuring and building the interpreter for dynamic loading}
848
Guido van Rossum6938f061994-08-01 12:22:53 +0000849There are three styles of dynamic loading: one using shared libraries,
850one using SGI IRIX 4 dynamic loading, and one using GNU dynamic
851loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000852
Guido van Rossum6938f061994-08-01 12:22:53 +0000853\subsection{Shared libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000854
Guido van Rossum16d6e711994-08-08 12:30:22 +0000855The following systems support dynamic loading using shared libraries:
Guido van Rossum6938f061994-08-01 12:22:53 +0000856SunOS 4; Solaris 2; SGI IRIX 5 (but not SGI IRIX 4!); and probably all
857systems derived from SVR4, or at least those SVR4 derivatives that
858support shared libraries (are there any that don't?).
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000859
Guido van Rossum6938f061994-08-01 12:22:53 +0000860You don't need to do anything to configure dynamic loading on these
861systems --- the \file{configure} detects the presence of the
862\file{<dlfcn.h>} header file and automatically configures dynamic
863loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000864
Guido van Rossum6938f061994-08-01 12:22:53 +0000865\subsection{SGI dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000866
Guido van Rossum6938f061994-08-01 12:22:53 +0000867Only SGI IRIX 4 supports dynamic loading of modules using SGI dynamic
868loading. (SGI IRIX 5 might also support it but it is inferior to
869using shared libraries so there is no reason to; a small test didn't
870work right away so I gave up trying to support it.)
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000871
Guido van Rossum6938f061994-08-01 12:22:53 +0000872Before you build Python, you first need to fetch and build the \code{dl}
873package written by Jack Jansen. This is available by anonymous ftp
874from host \file{ftp.cwi.nl}, directory \file{pub/dynload}, file
875\file{dl-1.6.tar.Z}. (The version number may change.) Follow the
876instructions in the package's \file{README} file to build it.
877
878Once you have built \code{dl}, you can configure Python to use it. To
879this end, you run the \file{configure} script with the option
880\code{--with-dl=\var{directory}} where \var{directory} is the absolute
881pathname of the \code{dl} directory.
882
883Now build and install Python as you normally would (see the
884\file{README} file in the toplevel Python directory.)
885
886\subsection{GNU dynamic loading}
887
888GNU dynamic loading supports (according to its \file{README} file) the
889following hardware and software combinations: VAX (Ultrix), Sun 3
890(SunOS 3.4 and 4.0), Sparc (SunOS 4.0), Sequent Symmetry (Dynix), and
891Atari ST. There is no reason to use it on a Sparc; I haven't seen a
892Sun 3 for years so I don't know if these have shared libraries or not.
893
894You need to fetch and build two packages. One is GNU DLD 3.2.3,
895available by anonymous ftp from host \file{ftp.cwi.nl}, directory
896\file{pub/dynload}, file \file{dld-3.2.3.tar.Z}. (As far as I know,
897no further development on GNU DLD is being done.) The other is an
898emulation of Jack Jansen's \code{dl} package that I wrote on top of
899GNU DLD 3.2.3. This is available from the same host and directory,
900file dl-dld-1.1.tar.Z. (The version number may change --- but I doubt
901it will.) Follow the instructions in each package's \file{README}
902file to configure build them.
903
904Now configure Python. Run the \file{configure} script with the option
905\code{--with-dl-dld=\var{dl-directory},\var{dld-directory}} where
906\var{dl-directory} is the absolute pathname of the directory where you
907have built the \file{dl-dld} package, and \var{dld-directory} is that
908of the GNU DLD package. The Python interpreter you build hereafter
909will support GNU dynamic loading.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000910
911
912\section{Building a dynamically loadable module}
913
Guido van Rossum6938f061994-08-01 12:22:53 +0000914Since there are three styles of dynamic loading, there are also three
915groups of instructions for building a dynamically loadable module.
916Instructions common for all three styles are given first. Assuming
917your module is called \code{foo}, the source filename must be
918\file{foomodule.c}, so the object name is \file{foomodule.o}. The
919module must be written as a normal Python extension module (as
920described earlier).
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000921
Guido van Rossum6938f061994-08-01 12:22:53 +0000922Note that in all cases you will have to create your own Makefile that
923compiles your module file(s). This Makefile will have to pass two
924\samp{-I} arguments to the C compiler which will make it find the
925Python header files. If the Make variable \var{PYTHONTOP} points to
926the toplevel Python directory, your \var{CFLAGS} Make variable should
927contain the options \samp{-I\$(PYTHONTOP) -I\$(PYTHONTOP)/Include}.
928(Most header files are in the \file{Include} subdirectory, but the
929\file{config.h} header lives in the toplevel directory.) You must
930also add \samp{-DHAVE_CONFIG_H} to the definition of \var{CFLAGS} to
931direct the Python headers to include \file{config.h}.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000932
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000933
Guido van Rossum6938f061994-08-01 12:22:53 +0000934\subsection{Shared libraries}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000935
Guido van Rossum6938f061994-08-01 12:22:53 +0000936You must link the \samp{.o} file to produce a shared library. This is
937done using a special invocation of the \UNIX{} loader/linker, {\em
938ld}(1). Unfortunately the invocation differs slightly per system.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000939
Guido van Rossum6938f061994-08-01 12:22:53 +0000940On SunOS 4, use
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000941\begin{verbatim}
Guido van Rossum6938f061994-08-01 12:22:53 +0000942 ld foomodule.o -o foomodule.so
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000943\end{verbatim}
944
Guido van Rossum6938f061994-08-01 12:22:53 +0000945On Solaris 2, use
946\begin{verbatim}
947 ld -G foomodule.o -o foomodule.so
948\end{verbatim}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000949
Guido van Rossum6938f061994-08-01 12:22:53 +0000950On SGI IRIX 5, use
951\begin{verbatim}
952 ld -shared foomodule.o -o foomodule.so
953\end{verbatim}
954
955On other systems, consult the manual page for {\em ld}(1) to find what
956flags, if any, must be used.
957
958If your extension module uses system libraries that haven't already
959been linked with Python (e.g. a windowing system), these must be
960passed to the {\em ld} command as \samp{-l} options after the
961\samp{.o} file.
962
963The resulting file \file{foomodule.so} must be copied into a directory
964along the Python module search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000965
966
Guido van Rossum6938f061994-08-01 12:22:53 +0000967\subsection{SGI dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000968
Guido van Rossum6938f061994-08-01 12:22:53 +0000969{bf IMPORTANT:} You must compile your extension module with the
970additional C flag \samp{-G0} (or \samp{-G 0}). This instruct the
971assembler to generate position-independent code.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000972
Guido van Rossum6938f061994-08-01 12:22:53 +0000973You don't need to link the resulting \file{foomodule.o} file; just
974copy it into a directory along the Python module search path.
975
976The first time your extension is loaded, it takes some extra time and
977a few messages may be printed. This creates a file
978\file{foomodule.ld} which is an image that can be loaded quickly into
979the Python interpreter process. When a new Python interpreter is
980installed, the \code{dl} package detects this and rebuilds
981\file{foomodule.ld}. The file \file{foomodule.ld} is placed in the
982directory where \file{foomodule.o} was found, unless this directory is
983unwritable; in that case it is placed in a temporary
984directory.\footnote{Check the manual page of the \code{dl} package for
985details.}
986
987If your extension modules uses additional system libraries, you must
988create a file \file{foomodule.libs} in the same directory as the
989\file{foomodule.o}. This file should contain one or more lines with
990whitespace-separated options that will be passed to the linker ---
991normally only \samp{-l} options or absolute pathnames of libraries
992(\samp{.a} files) should be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000993
994
Guido van Rossum6938f061994-08-01 12:22:53 +0000995\subsection{GNU dynamic loading}
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000996
Guido van Rossum6938f061994-08-01 12:22:53 +0000997Just copy \file{foomodule.o} into a directory along the Python module
998search path.
Guido van Rossum6f0132f1993-11-19 13:13:22 +0000999
Guido van Rossum6938f061994-08-01 12:22:53 +00001000If your extension modules uses additional system libraries, you must
1001create a file \file{foomodule.libs} in the same directory as the
1002\file{foomodule.o}. This file should contain one or more lines with
1003whitespace-separated absolute pathnames of libraries (\samp{.a}
1004files). No \samp{-l} options can be used.
Guido van Rossum6f0132f1993-11-19 13:13:22 +00001005
1006
Guido van Rossum7a2dba21993-11-05 14:45:11 +00001007\input{ext.ind}
1008
1009\end{document}