1 | \chapter{Extending Python with \C{} or \Cpp{} \label{intro}}
|
---|
2 |
|
---|
3 |
|
---|
4 | It is quite easy to add new built-in modules to Python, if you know
|
---|
5 | how to program in C. Such \dfn{extension modules} can do two things
|
---|
6 | that can't be done directly in Python: they can implement new built-in
|
---|
7 | object types, and they can call C library functions and system calls.
|
---|
8 |
|
---|
9 | To support extensions, the Python API (Application Programmers
|
---|
10 | Interface) defines a set of functions, macros and variables that
|
---|
11 | provide access to most aspects of the Python run-time system. The
|
---|
12 | Python API is incorporated in a C source file by including the header
|
---|
13 | \code{"Python.h"}.
|
---|
14 |
|
---|
15 | The compilation of an extension module depends on its intended use as
|
---|
16 | well as on your system setup; details are given in later chapters.
|
---|
17 |
|
---|
18 |
|
---|
19 | \section{A Simple Example
|
---|
20 | \label{simpleExample}}
|
---|
21 |
|
---|
22 | Let's create an extension module called \samp{spam} (the favorite food
|
---|
23 | of Monty Python fans...) and let's say we want to create a Python
|
---|
24 | interface to the C library function \cfunction{system()}.\footnote{An
|
---|
25 | interface for this function already exists in the standard module
|
---|
26 | \module{os} --- it was chosen as a simple and straightforward example.}
|
---|
27 | This function takes a null-terminated character string as argument and
|
---|
28 | returns an integer. We want this function to be callable from Python
|
---|
29 | as follows:
|
---|
30 |
|
---|
31 | \begin{verbatim}
|
---|
32 | >>> import spam
|
---|
33 | >>> status = spam.system("ls -l")
|
---|
34 | \end{verbatim}
|
---|
35 |
|
---|
36 | Begin by creating a file \file{spammodule.c}. (Historically, if a
|
---|
37 | module is called \samp{spam}, the C file containing its implementation
|
---|
38 | is called \file{spammodule.c}; if the module name is very long, like
|
---|
39 | \samp{spammify}, the module name can be just \file{spammify.c}.)
|
---|
40 |
|
---|
41 | The first line of our file can be:
|
---|
42 |
|
---|
43 | \begin{verbatim}
|
---|
44 | #include <Python.h>
|
---|
45 | \end{verbatim}
|
---|
46 |
|
---|
47 | which pulls in the Python API (you can add a comment describing the
|
---|
48 | purpose of the module and a copyright notice if you like).
|
---|
49 |
|
---|
50 | \begin{notice}[warning]
|
---|
51 | Since Python may define some pre-processor definitions which affect
|
---|
52 | the standard headers on some systems, you \emph{must} include
|
---|
53 | \file{Python.h} before any standard headers are included.
|
---|
54 | \end{notice}
|
---|
55 |
|
---|
56 | All user-visible symbols defined by \file{Python.h} have a prefix of
|
---|
57 | \samp{Py} or \samp{PY}, except those defined in standard header files.
|
---|
58 | For convenience, and since they are used extensively by the Python
|
---|
59 | interpreter, \code{"Python.h"} includes a few standard header files:
|
---|
60 | \code{<stdio.h>}, \code{<string.h>}, \code{<errno.h>}, and
|
---|
61 | \code{<stdlib.h>}. If the latter header file does not exist on your
|
---|
62 | system, it declares the functions \cfunction{malloc()},
|
---|
63 | \cfunction{free()} and \cfunction{realloc()} directly.
|
---|
64 |
|
---|
65 | The next thing we add to our module file is the C function that will
|
---|
66 | be called when the Python expression \samp{spam.system(\var{string})}
|
---|
67 | is evaluated (we'll see shortly how it ends up being called):
|
---|
68 |
|
---|
69 | \begin{verbatim}
|
---|
70 | static PyObject *
|
---|
71 | spam_system(PyObject *self, PyObject *args)
|
---|
72 | {
|
---|
73 | const char *command;
|
---|
74 | int sts;
|
---|
75 |
|
---|
76 | if (!PyArg_ParseTuple(args, "s", &command))
|
---|
77 | return NULL;
|
---|
78 | sts = system(command);
|
---|
79 | return Py_BuildValue("i", sts);
|
---|
80 | }
|
---|
81 | \end{verbatim}
|
---|
82 |
|
---|
83 | There is a straightforward translation from the argument list in
|
---|
84 | Python (for example, the single expression \code{"ls -l"}) to the
|
---|
85 | arguments passed to the C function. The C function always has two
|
---|
86 | arguments, conventionally named \var{self} and \var{args}.
|
---|
87 |
|
---|
88 | The \var{self} argument is only used when the C function implements a
|
---|
89 | built-in method, not a function. In the example, \var{self} will
|
---|
90 | always be a \NULL{} pointer, since we are defining a function, not a
|
---|
91 | method. (This is done so that the interpreter doesn't have to
|
---|
92 | understand two different types of C functions.)
|
---|
93 |
|
---|
94 | The \var{args} argument will be a pointer to a Python tuple object
|
---|
95 | containing the arguments. Each item of the tuple corresponds to an
|
---|
96 | argument in the call's argument list. The arguments are Python
|
---|
97 | objects --- in order to do anything with them in our C function we have
|
---|
98 | to convert them to C values. The function \cfunction{PyArg_ParseTuple()}
|
---|
99 | in the Python API checks the argument types and converts them to C
|
---|
100 | values. It uses a template string to determine the required types of
|
---|
101 | the arguments as well as the types of the C variables into which to
|
---|
102 | store the converted values. More about this later.
|
---|
103 |
|
---|
104 | \cfunction{PyArg_ParseTuple()} returns true (nonzero) if all arguments have
|
---|
105 | the right type and its components have been stored in the variables
|
---|
106 | whose addresses are passed. It returns false (zero) if an invalid
|
---|
107 | argument list was passed. In the latter case it also raises an
|
---|
108 | appropriate exception so the calling function can return
|
---|
109 | \NULL{} immediately (as we saw in the example).
|
---|
110 |
|
---|
111 |
|
---|
112 | \section{Intermezzo: Errors and Exceptions
|
---|
113 | \label{errors}}
|
---|
114 |
|
---|
115 | An important convention throughout the Python interpreter is the
|
---|
116 | following: when a function fails, it should set an exception condition
|
---|
117 | and return an error value (usually a \NULL{} pointer). Exceptions
|
---|
118 | are stored in a static global variable inside the interpreter; if this
|
---|
119 | variable is \NULL{} no exception has occurred. A second global
|
---|
120 | variable stores the ``associated value'' of the exception (the second
|
---|
121 | argument to \keyword{raise}). A third variable contains the stack
|
---|
122 | traceback in case the error originated in Python code. These three
|
---|
123 | variables are the C equivalents of the Python variables
|
---|
124 | \code{sys.exc_type}, \code{sys.exc_value} and \code{sys.exc_traceback} (see
|
---|
125 | the section on module \module{sys} in the
|
---|
126 | \citetitle[../lib/lib.html]{Python Library Reference}). It is
|
---|
127 | important to know about them to understand how errors are passed
|
---|
128 | around.
|
---|
129 |
|
---|
130 | The Python API defines a number of functions to set various types of
|
---|
131 | exceptions.
|
---|
132 |
|
---|
133 | The most common one is \cfunction{PyErr_SetString()}. Its arguments
|
---|
134 | are an exception object and a C string. The exception object is
|
---|
135 | usually a predefined object like \cdata{PyExc_ZeroDivisionError}. The
|
---|
136 | C string indicates the cause of the error and is converted to a
|
---|
137 | Python string object and stored as the ``associated value'' of the
|
---|
138 | exception.
|
---|
139 |
|
---|
140 | Another useful function is \cfunction{PyErr_SetFromErrno()}, which only
|
---|
141 | takes an exception argument and constructs the associated value by
|
---|
142 | inspection of the global variable \cdata{errno}. The most
|
---|
143 | general function is \cfunction{PyErr_SetObject()}, which takes two object
|
---|
144 | arguments, the exception and its associated value. You don't need to
|
---|
145 | \cfunction{Py_INCREF()} the objects passed to any of these functions.
|
---|
146 |
|
---|
147 | You can test non-destructively whether an exception has been set with
|
---|
148 | \cfunction{PyErr_Occurred()}. This returns the current exception object,
|
---|
149 | or \NULL{} if no exception has occurred. You normally don't need
|
---|
150 | to call \cfunction{PyErr_Occurred()} to see whether an error occurred in a
|
---|
151 | function call, since you should be able to tell from the return value.
|
---|
152 |
|
---|
153 | When a function \var{f} that calls another function \var{g} detects
|
---|
154 | that the latter fails, \var{f} should itself return an error value
|
---|
155 | (usually \NULL{} or \code{-1}). It should \emph{not} call one of the
|
---|
156 | \cfunction{PyErr_*()} functions --- one has already been called by \var{g}.
|
---|
157 | \var{f}'s caller is then supposed to also return an error indication
|
---|
158 | to \emph{its} caller, again \emph{without} calling \cfunction{PyErr_*()},
|
---|
159 | and so on --- the most detailed cause of the error was already
|
---|
160 | reported by the function that first detected it. Once the error
|
---|
161 | reaches the Python interpreter's main loop, this aborts the currently
|
---|
162 | executing Python code and tries to find an exception handler specified
|
---|
163 | by the Python programmer.
|
---|
164 |
|
---|
165 | (There are situations where a module can actually give a more detailed
|
---|
166 | error message by calling another \cfunction{PyErr_*()} function, and in
|
---|
167 | such cases it is fine to do so. As a general rule, however, this is
|
---|
168 | not necessary, and can cause information about the cause of the error
|
---|
169 | to be lost: most operations can fail for a variety of reasons.)
|
---|
170 |
|
---|
171 | To ignore an exception set by a function call that failed, the exception
|
---|
172 | condition must be cleared explicitly by calling \cfunction{PyErr_Clear()}.
|
---|
173 | The only time C code should call \cfunction{PyErr_Clear()} is if it doesn't
|
---|
174 | want to pass the error on to the interpreter but wants to handle it
|
---|
175 | completely by itself (possibly by trying something else, or pretending
|
---|
176 | nothing went wrong).
|
---|
177 |
|
---|
178 | Every failing \cfunction{malloc()} call must be turned into an
|
---|
179 | exception --- the direct caller of \cfunction{malloc()} (or
|
---|
180 | \cfunction{realloc()}) must call \cfunction{PyErr_NoMemory()} and
|
---|
181 | return a failure indicator itself. All the object-creating functions
|
---|
182 | (for example, \cfunction{PyInt_FromLong()}) already do this, so this
|
---|
183 | note is only relevant to those who call \cfunction{malloc()} directly.
|
---|
184 |
|
---|
185 | Also note that, with the important exception of
|
---|
186 | \cfunction{PyArg_ParseTuple()} and friends, functions that return an
|
---|
187 | integer status usually return a positive value or zero for success and
|
---|
188 | \code{-1} for failure, like \UNIX{} system calls.
|
---|
189 |
|
---|
190 | Finally, be careful to clean up garbage (by making
|
---|
191 | \cfunction{Py_XDECREF()} or \cfunction{Py_DECREF()} calls for objects
|
---|
192 | you have already created) when you return an error indicator!
|
---|
193 |
|
---|
194 | The choice of which exception to raise is entirely yours. There are
|
---|
195 | predeclared C objects corresponding to all built-in Python exceptions,
|
---|
196 | such as \cdata{PyExc_ZeroDivisionError}, which you can use directly.
|
---|
197 | Of course, you should choose exceptions wisely --- don't use
|
---|
198 | \cdata{PyExc_TypeError} to mean that a file couldn't be opened (that
|
---|
199 | should probably be \cdata{PyExc_IOError}). If something's wrong with
|
---|
200 | the argument list, the \cfunction{PyArg_ParseTuple()} function usually
|
---|
201 | raises \cdata{PyExc_TypeError}. If you have an argument whose value
|
---|
202 | must be in a particular range or must satisfy other conditions,
|
---|
203 | \cdata{PyExc_ValueError} is appropriate.
|
---|
204 |
|
---|
205 | You can also define a new exception that is unique to your module.
|
---|
206 | For this, you usually declare a static object variable at the
|
---|
207 | beginning of your file:
|
---|
208 |
|
---|
209 | \begin{verbatim}
|
---|
210 | static PyObject *SpamError;
|
---|
211 | \end{verbatim}
|
---|
212 |
|
---|
213 | and initialize it in your module's initialization function
|
---|
214 | (\cfunction{initspam()}) with an exception object (leaving out
|
---|
215 | the error checking for now):
|
---|
216 |
|
---|
217 | \begin{verbatim}
|
---|
218 | PyMODINIT_FUNC
|
---|
219 | initspam(void)
|
---|
220 | {
|
---|
221 | PyObject *m;
|
---|
222 |
|
---|
223 | m = Py_InitModule("spam", SpamMethods);
|
---|
224 |
|
---|
225 | SpamError = PyErr_NewException("spam.error", NULL, NULL);
|
---|
226 | Py_INCREF(SpamError);
|
---|
227 | PyModule_AddObject(m, "error", SpamError);
|
---|
228 | }
|
---|
229 | \end{verbatim}
|
---|
230 |
|
---|
231 | Note that the Python name for the exception object is
|
---|
232 | \exception{spam.error}. The \cfunction{PyErr_NewException()} function
|
---|
233 | may create a class with the base class being \exception{Exception}
|
---|
234 | (unless another class is passed in instead of \NULL), described in the
|
---|
235 | \citetitle[../lib/lib.html]{Python Library Reference} under ``Built-in
|
---|
236 | Exceptions.''
|
---|
237 |
|
---|
238 | Note also that the \cdata{SpamError} variable retains a reference to
|
---|
239 | the newly created exception class; this is intentional! Since the
|
---|
240 | exception could be removed from the module by external code, an owned
|
---|
241 | reference to the class is needed to ensure that it will not be
|
---|
242 | discarded, causing \cdata{SpamError} to become a dangling pointer.
|
---|
243 | Should it become a dangling pointer, C code which raises the exception
|
---|
244 | could cause a core dump or other unintended side effects.
|
---|
245 |
|
---|
246 | We discuss the use of PyMODINIT_FUNC as a function return type later in this
|
---|
247 | sample.
|
---|
248 |
|
---|
249 | \section{Back to the Example
|
---|
250 | \label{backToExample}}
|
---|
251 |
|
---|
252 | Going back to our example function, you should now be able to
|
---|
253 | understand this statement:
|
---|
254 |
|
---|
255 | \begin{verbatim}
|
---|
256 | if (!PyArg_ParseTuple(args, "s", &command))
|
---|
257 | return NULL;
|
---|
258 | \end{verbatim}
|
---|
259 |
|
---|
260 | It returns \NULL{} (the error indicator for functions returning
|
---|
261 | object pointers) if an error is detected in the argument list, relying
|
---|
262 | on the exception set by \cfunction{PyArg_ParseTuple()}. Otherwise the
|
---|
263 | string value of the argument has been copied to the local variable
|
---|
264 | \cdata{command}. This is a pointer assignment and you are not supposed
|
---|
265 | to modify the string to which it points (so in Standard C, the variable
|
---|
266 | \cdata{command} should properly be declared as \samp{const char
|
---|
267 | *command}).
|
---|
268 |
|
---|
269 | The next statement is a call to the \UNIX{} function
|
---|
270 | \cfunction{system()}, passing it the string we just got from
|
---|
271 | \cfunction{PyArg_ParseTuple()}:
|
---|
272 |
|
---|
273 | \begin{verbatim}
|
---|
274 | sts = system(command);
|
---|
275 | \end{verbatim}
|
---|
276 |
|
---|
277 | Our \function{spam.system()} function must return the value of
|
---|
278 | \cdata{sts} as a Python object. This is done using the function
|
---|
279 | \cfunction{Py_BuildValue()}, which is something like the inverse of
|
---|
280 | \cfunction{PyArg_ParseTuple()}: it takes a format string and an
|
---|
281 | arbitrary number of C values, and returns a new Python object.
|
---|
282 | More info on \cfunction{Py_BuildValue()} is given later.
|
---|
283 |
|
---|
284 | \begin{verbatim}
|
---|
285 | return Py_BuildValue("i", sts);
|
---|
286 | \end{verbatim}
|
---|
287 |
|
---|
288 | In this case, it will return an integer object. (Yes, even integers
|
---|
289 | are objects on the heap in Python!)
|
---|
290 |
|
---|
291 | If you have a C function that returns no useful argument (a function
|
---|
292 | returning \ctype{void}), the corresponding Python function must return
|
---|
293 | \code{None}. You need this idiom to do so (which is implemented by the
|
---|
294 | \csimplemacro{Py_RETURN_NONE} macro):
|
---|
295 |
|
---|
296 | \begin{verbatim}
|
---|
297 | Py_INCREF(Py_None);
|
---|
298 | return Py_None;
|
---|
299 | \end{verbatim}
|
---|
300 |
|
---|
301 | \cdata{Py_None} is the C name for the special Python object
|
---|
302 | \code{None}. It is a genuine Python object rather than a \NULL{}
|
---|
303 | pointer, which means ``error'' in most contexts, as we have seen.
|
---|
304 |
|
---|
305 |
|
---|
306 | \section{The Module's Method Table and Initialization Function
|
---|
307 | \label{methodTable}}
|
---|
308 |
|
---|
309 | I promised to show how \cfunction{spam_system()} is called from Python
|
---|
310 | programs. First, we need to list its name and address in a ``method
|
---|
311 | table'':
|
---|
312 |
|
---|
313 | \begin{verbatim}
|
---|
314 | static PyMethodDef SpamMethods[] = {
|
---|
315 | ...
|
---|
316 | {"system", spam_system, METH_VARARGS,
|
---|
317 | "Execute a shell command."},
|
---|
318 | ...
|
---|
319 | {NULL, NULL, 0, NULL} /* Sentinel */
|
---|
320 | };
|
---|
321 | \end{verbatim}
|
---|
322 |
|
---|
323 | Note the third entry (\samp{METH_VARARGS}). This is a flag telling
|
---|
324 | the interpreter the calling convention to be used for the C
|
---|
325 | function. It should normally always be \samp{METH_VARARGS} or
|
---|
326 | \samp{METH_VARARGS | METH_KEYWORDS}; a value of \code{0} means that an
|
---|
327 | obsolete variant of \cfunction{PyArg_ParseTuple()} is used.
|
---|
328 |
|
---|
329 | When using only \samp{METH_VARARGS}, the function should expect
|
---|
330 | the Python-level parameters to be passed in as a tuple acceptable for
|
---|
331 | parsing via \cfunction{PyArg_ParseTuple()}; more information on this
|
---|
332 | function is provided below.
|
---|
333 |
|
---|
334 | The \constant{METH_KEYWORDS} bit may be set in the third field if
|
---|
335 | keyword arguments should be passed to the function. In this case, the
|
---|
336 | C function should accept a third \samp{PyObject *} parameter which
|
---|
337 | will be a dictionary of keywords. Use
|
---|
338 | \cfunction{PyArg_ParseTupleAndKeywords()} to parse the arguments to
|
---|
339 | such a function.
|
---|
340 |
|
---|
341 | The method table must be passed to the interpreter in the module's
|
---|
342 | initialization function. The initialization function must be named
|
---|
343 | \cfunction{init\var{name}()}, where \var{name} is the name of the
|
---|
344 | module, and should be the only non-\keyword{static} item defined in
|
---|
345 | the module file:
|
---|
346 |
|
---|
347 | \begin{verbatim}
|
---|
348 | PyMODINIT_FUNC
|
---|
349 | initspam(void)
|
---|
350 | {
|
---|
351 | (void) Py_InitModule("spam", SpamMethods);
|
---|
352 | }
|
---|
353 | \end{verbatim}
|
---|
354 |
|
---|
355 | Note that PyMODINIT_FUNC declares the function as \code{void} return type,
|
---|
356 | declares any special linkage declarations required by the platform, and for
|
---|
357 | \Cpp{} declares the function as \code{extern "C"}.
|
---|
358 |
|
---|
359 | When the Python program imports module \module{spam} for the first
|
---|
360 | time, \cfunction{initspam()} is called. (See below for comments about
|
---|
361 | embedding Python.) It calls
|
---|
362 | \cfunction{Py_InitModule()}, which creates a ``module object'' (which
|
---|
363 | is inserted in the dictionary \code{sys.modules} under the key
|
---|
364 | \code{"spam"}), and inserts built-in function objects into the newly
|
---|
365 | created module based upon the table (an array of \ctype{PyMethodDef}
|
---|
366 | structures) that was passed as its second argument.
|
---|
367 | \cfunction{Py_InitModule()} returns a pointer to the module object
|
---|
368 | that it creates (which is unused here). It aborts with a fatal error
|
---|
369 | if the module could not be initialized satisfactorily, so the caller
|
---|
370 | doesn't need to check for errors.
|
---|
371 |
|
---|
372 | When embedding Python, the \cfunction{initspam()} function is not
|
---|
373 | called automatically unless there's an entry in the
|
---|
374 | \cdata{_PyImport_Inittab} table. The easiest way to handle this is to
|
---|
375 | statically initialize your statically-linked modules by directly
|
---|
376 | calling \cfunction{initspam()} after the call to
|
---|
377 | \cfunction{Py_Initialize()}:
|
---|
378 |
|
---|
379 | \begin{verbatim}
|
---|
380 | int
|
---|
381 | main(int argc, char *argv[])
|
---|
382 | {
|
---|
383 | /* Pass argv[0] to the Python interpreter */
|
---|
384 | Py_SetProgramName(argv[0]);
|
---|
385 |
|
---|
386 | /* Initialize the Python interpreter. Required. */
|
---|
387 | Py_Initialize();
|
---|
388 |
|
---|
389 | /* Add a static module */
|
---|
390 | initspam();
|
---|
391 | \end{verbatim}
|
---|
392 |
|
---|
393 | An example may be found in the file \file{Demo/embed/demo.c} in the
|
---|
394 | Python source distribution.
|
---|
395 |
|
---|
396 | \note{Removing entries from \code{sys.modules} or importing
|
---|
397 | compiled modules into multiple interpreters within a process (or
|
---|
398 | following a \cfunction{fork()} without an intervening
|
---|
399 | \cfunction{exec()}) can create problems for some extension modules.
|
---|
400 | Extension module authors should exercise caution when initializing
|
---|
401 | internal data structures.
|
---|
402 | Note also that the \function{reload()} function can be used with
|
---|
403 | extension modules, and will call the module initialization function
|
---|
404 | (\cfunction{initspam()} in the example), but will not load the module
|
---|
405 | again if it was loaded from a dynamically loadable object file
|
---|
406 | (\file{.so} on \UNIX, \file{.dll} on Windows).}
|
---|
407 |
|
---|
408 | A more substantial example module is included in the Python source
|
---|
409 | distribution as \file{Modules/xxmodule.c}. This file may be used as a
|
---|
410 | template or simply read as an example. The \program{modulator.py}
|
---|
411 | script included in the source distribution or Windows install provides
|
---|
412 | a simple graphical user interface for declaring the functions and
|
---|
413 | objects which a module should implement, and can generate a template
|
---|
414 | which can be filled in. The script lives in the
|
---|
415 | \file{Tools/modulator/} directory; see the \file{README} file there
|
---|
416 | for more information.
|
---|
417 |
|
---|
418 |
|
---|
419 | \section{Compilation and Linkage
|
---|
420 | \label{compilation}}
|
---|
421 |
|
---|
422 | There are two more things to do before you can use your new extension:
|
---|
423 | compiling and linking it with the Python system. If you use dynamic
|
---|
424 | loading, the details may depend on the style of dynamic loading your
|
---|
425 | system uses; see the chapters about building extension modules
|
---|
426 | (chapter \ref{building}) and additional information that pertains only
|
---|
427 | to building on Windows (chapter \ref{building-on-windows}) for more
|
---|
428 | information about this.
|
---|
429 |
|
---|
430 | If you can't use dynamic loading, or if you want to make your module a
|
---|
431 | permanent part of the Python interpreter, you will have to change the
|
---|
432 | configuration setup and rebuild the interpreter. Luckily, this is
|
---|
433 | very simple on \UNIX: just place your file (\file{spammodule.c} for
|
---|
434 | example) in the \file{Modules/} directory of an unpacked source
|
---|
435 | distribution, add a line to the file \file{Modules/Setup.local}
|
---|
436 | describing your file:
|
---|
437 |
|
---|
438 | \begin{verbatim}
|
---|
439 | spam spammodule.o
|
---|
440 | \end{verbatim}
|
---|
441 |
|
---|
442 | and rebuild the interpreter by running \program{make} in the toplevel
|
---|
443 | directory. You can also run \program{make} in the \file{Modules/}
|
---|
444 | subdirectory, but then you must first rebuild \file{Makefile}
|
---|
445 | there by running `\program{make} Makefile'. (This is necessary each
|
---|
446 | time you change the \file{Setup} file.)
|
---|
447 |
|
---|
448 | If your module requires additional libraries to link with, these can
|
---|
449 | be listed on the line in the configuration file as well, for instance:
|
---|
450 |
|
---|
451 | \begin{verbatim}
|
---|
452 | spam spammodule.o -lX11
|
---|
453 | \end{verbatim}
|
---|
454 |
|
---|
455 | \section{Calling Python Functions from C
|
---|
456 | \label{callingPython}}
|
---|
457 |
|
---|
458 | So far we have concentrated on making C functions callable from
|
---|
459 | Python. The reverse is also useful: calling Python functions from C.
|
---|
460 | This is especially the case for libraries that support so-called
|
---|
461 | ``callback'' functions. If a C interface makes use of callbacks, the
|
---|
462 | equivalent Python often needs to provide a callback mechanism to the
|
---|
463 | Python programmer; the implementation will require calling the Python
|
---|
464 | callback functions from a C callback. Other uses are also imaginable.
|
---|
465 |
|
---|
466 | Fortunately, the Python interpreter is easily called recursively, and
|
---|
467 | there is a standard interface to call a Python function. (I won't
|
---|
468 | dwell on how to call the Python parser with a particular string as
|
---|
469 | input --- if you're interested, have a look at the implementation of
|
---|
470 | the \programopt{-c} command line option in \file{Python/pythonmain.c}
|
---|
471 | from the Python source code.)
|
---|
472 |
|
---|
473 | Calling a Python function is easy. First, the Python program must
|
---|
474 | somehow pass you the Python function object. You should provide a
|
---|
475 | function (or some other interface) to do this. When this function is
|
---|
476 | called, save a pointer to the Python function object (be careful to
|
---|
477 | \cfunction{Py_INCREF()} it!) in a global variable --- or wherever you
|
---|
478 | see fit. For example, the following function might be part of a module
|
---|
479 | definition:
|
---|
480 |
|
---|
481 | \begin{verbatim}
|
---|
482 | static PyObject *my_callback = NULL;
|
---|
483 |
|
---|
484 | static PyObject *
|
---|
485 | my_set_callback(PyObject *dummy, PyObject *args)
|
---|
486 | {
|
---|
487 | PyObject *result = NULL;
|
---|
488 | PyObject *temp;
|
---|
489 |
|
---|
490 | if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
|
---|
491 | if (!PyCallable_Check(temp)) {
|
---|
492 | PyErr_SetString(PyExc_TypeError, "parameter must be callable");
|
---|
493 | return NULL;
|
---|
494 | }
|
---|
495 | Py_XINCREF(temp); /* Add a reference to new callback */
|
---|
496 | Py_XDECREF(my_callback); /* Dispose of previous callback */
|
---|
497 | my_callback = temp; /* Remember new callback */
|
---|
498 | /* Boilerplate to return "None" */
|
---|
499 | Py_INCREF(Py_None);
|
---|
500 | result = Py_None;
|
---|
501 | }
|
---|
502 | return result;
|
---|
503 | }
|
---|
504 | \end{verbatim}
|
---|
505 |
|
---|
506 | This function must be registered with the interpreter using the
|
---|
507 | \constant{METH_VARARGS} flag; this is described in section
|
---|
508 | \ref{methodTable}, ``The Module's Method Table and Initialization
|
---|
509 | Function.'' The \cfunction{PyArg_ParseTuple()} function and its
|
---|
510 | arguments are documented in section~\ref{parseTuple}, ``Extracting
|
---|
511 | Parameters in Extension Functions.''
|
---|
512 |
|
---|
513 | The macros \cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()}
|
---|
514 | increment/decrement the reference count of an object and are safe in
|
---|
515 | the presence of \NULL{} pointers (but note that \var{temp} will not be
|
---|
516 | \NULL{} in this context). More info on them in
|
---|
517 | section~\ref{refcounts}, ``Reference Counts.''
|
---|
518 |
|
---|
519 | Later, when it is time to call the function, you call the C function
|
---|
520 | \cfunction{PyEval_CallObject()}.\ttindex{PyEval_CallObject()} This
|
---|
521 | function has two arguments, both pointers to arbitrary Python objects:
|
---|
522 | the Python function, and the argument list. The argument list must
|
---|
523 | always be a tuple object, whose length is the number of arguments. To
|
---|
524 | call the Python function with no arguments, pass an empty tuple; to
|
---|
525 | call it with one argument, pass a singleton tuple.
|
---|
526 | \cfunction{Py_BuildValue()} returns a tuple when its format string
|
---|
527 | consists of zero or more format codes between parentheses. For
|
---|
528 | example:
|
---|
529 |
|
---|
530 | \begin{verbatim}
|
---|
531 | int arg;
|
---|
532 | PyObject *arglist;
|
---|
533 | PyObject *result;
|
---|
534 | ...
|
---|
535 | arg = 123;
|
---|
536 | ...
|
---|
537 | /* Time to call the callback */
|
---|
538 | arglist = Py_BuildValue("(i)", arg);
|
---|
539 | result = PyEval_CallObject(my_callback, arglist);
|
---|
540 | Py_DECREF(arglist);
|
---|
541 | \end{verbatim}
|
---|
542 |
|
---|
543 | \cfunction{PyEval_CallObject()} returns a Python object pointer: this is
|
---|
544 | the return value of the Python function. \cfunction{PyEval_CallObject()} is
|
---|
545 | ``reference-count-neutral'' with respect to its arguments. In the
|
---|
546 | example a new tuple was created to serve as the argument list, which
|
---|
547 | is \cfunction{Py_DECREF()}-ed immediately after the call.
|
---|
548 |
|
---|
549 | The return value of \cfunction{PyEval_CallObject()} is ``new'': either it
|
---|
550 | is a brand new object, or it is an existing object whose reference
|
---|
551 | count has been incremented. So, unless you want to save it in a
|
---|
552 | global variable, you should somehow \cfunction{Py_DECREF()} the result,
|
---|
553 | even (especially!) if you are not interested in its value.
|
---|
554 |
|
---|
555 | Before you do this, however, it is important to check that the return
|
---|
556 | value isn't \NULL. If it is, the Python function terminated by
|
---|
557 | raising an exception. If the C code that called
|
---|
558 | \cfunction{PyEval_CallObject()} is called from Python, it should now
|
---|
559 | return an error indication to its Python caller, so the interpreter
|
---|
560 | can print a stack trace, or the calling Python code can handle the
|
---|
561 | exception. If this is not possible or desirable, the exception should
|
---|
562 | be cleared by calling \cfunction{PyErr_Clear()}. For example:
|
---|
563 |
|
---|
564 | \begin{verbatim}
|
---|
565 | if (result == NULL)
|
---|
566 | return NULL; /* Pass error back */
|
---|
567 | ...use result...
|
---|
568 | Py_DECREF(result);
|
---|
569 | \end{verbatim}
|
---|
570 |
|
---|
571 | Depending on the desired interface to the Python callback function,
|
---|
572 | you may also have to provide an argument list to
|
---|
573 | \cfunction{PyEval_CallObject()}. In some cases the argument list is
|
---|
574 | also provided by the Python program, through the same interface that
|
---|
575 | specified the callback function. It can then be saved and used in the
|
---|
576 | same manner as the function object. In other cases, you may have to
|
---|
577 | construct a new tuple to pass as the argument list. The simplest way
|
---|
578 | to do this is to call \cfunction{Py_BuildValue()}. For example, if
|
---|
579 | you want to pass an integral event code, you might use the following
|
---|
580 | code:
|
---|
581 |
|
---|
582 | \begin{verbatim}
|
---|
583 | PyObject *arglist;
|
---|
584 | ...
|
---|
585 | arglist = Py_BuildValue("(l)", eventcode);
|
---|
586 | result = PyEval_CallObject(my_callback, arglist);
|
---|
587 | Py_DECREF(arglist);
|
---|
588 | if (result == NULL)
|
---|
589 | return NULL; /* Pass error back */
|
---|
590 | /* Here maybe use the result */
|
---|
591 | Py_DECREF(result);
|
---|
592 | \end{verbatim}
|
---|
593 |
|
---|
594 | Note the placement of \samp{Py_DECREF(arglist)} immediately after the
|
---|
595 | call, before the error check! Also note that strictly spoken this
|
---|
596 | code is not complete: \cfunction{Py_BuildValue()} may run out of
|
---|
597 | memory, and this should be checked.
|
---|
598 |
|
---|
599 |
|
---|
600 | \section{Extracting Parameters in Extension Functions
|
---|
601 | \label{parseTuple}}
|
---|
602 |
|
---|
603 | \ttindex{PyArg_ParseTuple()}
|
---|
604 |
|
---|
605 | The \cfunction{PyArg_ParseTuple()} function is declared as follows:
|
---|
606 |
|
---|
607 | \begin{verbatim}
|
---|
608 | int PyArg_ParseTuple(PyObject *arg, char *format, ...);
|
---|
609 | \end{verbatim}
|
---|
610 |
|
---|
611 | The \var{arg} argument must be a tuple object containing an argument
|
---|
612 | list passed from Python to a C function. The \var{format} argument
|
---|
613 | must be a format string, whose syntax is explained in
|
---|
614 | ``\ulink{Parsing arguments and building
|
---|
615 | values}{../api/arg-parsing.html}'' in the
|
---|
616 | \citetitle[../api/api.html]{Python/C API Reference Manual}. The
|
---|
617 | remaining arguments must be addresses of variables whose type is
|
---|
618 | determined by the format string.
|
---|
619 |
|
---|
620 | Note that while \cfunction{PyArg_ParseTuple()} checks that the Python
|
---|
621 | arguments have the required types, it cannot check the validity of the
|
---|
622 | addresses of C variables passed to the call: if you make mistakes
|
---|
623 | there, your code will probably crash or at least overwrite random bits
|
---|
624 | in memory. So be careful!
|
---|
625 |
|
---|
626 | Note that any Python object references which are provided to the
|
---|
627 | caller are \emph{borrowed} references; do not decrement their
|
---|
628 | reference count!
|
---|
629 |
|
---|
630 | Some example calls:
|
---|
631 |
|
---|
632 | \begin{verbatim}
|
---|
633 | int ok;
|
---|
634 | int i, j;
|
---|
635 | long k, l;
|
---|
636 | const char *s;
|
---|
637 | int size;
|
---|
638 |
|
---|
639 | ok = PyArg_ParseTuple(args, ""); /* No arguments */
|
---|
640 | /* Python call: f() */
|
---|
641 | \end{verbatim}
|
---|
642 |
|
---|
643 | \begin{verbatim}
|
---|
644 | ok = PyArg_ParseTuple(args, "s", &s); /* A string */
|
---|
645 | /* Possible Python call: f('whoops!') */
|
---|
646 | \end{verbatim}
|
---|
647 |
|
---|
648 | \begin{verbatim}
|
---|
649 | ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
|
---|
650 | /* Possible Python call: f(1, 2, 'three') */
|
---|
651 | \end{verbatim}
|
---|
652 |
|
---|
653 | \begin{verbatim}
|
---|
654 | ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
|
---|
655 | /* A pair of ints and a string, whose size is also returned */
|
---|
656 | /* Possible Python call: f((1, 2), 'three') */
|
---|
657 | \end{verbatim}
|
---|
658 |
|
---|
659 | \begin{verbatim}
|
---|
660 | {
|
---|
661 | const char *file;
|
---|
662 | const char *mode = "r";
|
---|
663 | int bufsize = 0;
|
---|
664 | ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
|
---|
665 | /* A string, and optionally another string and an integer */
|
---|
666 | /* Possible Python calls:
|
---|
667 | f('spam')
|
---|
668 | f('spam', 'w')
|
---|
669 | f('spam', 'wb', 100000) */
|
---|
670 | }
|
---|
671 | \end{verbatim}
|
---|
672 |
|
---|
673 | \begin{verbatim}
|
---|
674 | {
|
---|
675 | int left, top, right, bottom, h, v;
|
---|
676 | ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
|
---|
677 | &left, &top, &right, &bottom, &h, &v);
|
---|
678 | /* A rectangle and a point */
|
---|
679 | /* Possible Python call:
|
---|
680 | f(((0, 0), (400, 300)), (10, 10)) */
|
---|
681 | }
|
---|
682 | \end{verbatim}
|
---|
683 |
|
---|
684 | \begin{verbatim}
|
---|
685 | {
|
---|
686 | Py_complex c;
|
---|
687 | ok = PyArg_ParseTuple(args, "D:myfunction", &c);
|
---|
688 | /* a complex, also providing a function name for errors */
|
---|
689 | /* Possible Python call: myfunction(1+2j) */
|
---|
690 | }
|
---|
691 | \end{verbatim}
|
---|
692 |
|
---|
693 |
|
---|
694 | \section{Keyword Parameters for Extension Functions
|
---|
695 | \label{parseTupleAndKeywords}}
|
---|
696 |
|
---|
697 | \ttindex{PyArg_ParseTupleAndKeywords()}
|
---|
698 |
|
---|
699 | The \cfunction{PyArg_ParseTupleAndKeywords()} function is declared as
|
---|
700 | follows:
|
---|
701 |
|
---|
702 | \begin{verbatim}
|
---|
703 | int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
|
---|
704 | char *format, char *kwlist[], ...);
|
---|
705 | \end{verbatim}
|
---|
706 |
|
---|
707 | The \var{arg} and \var{format} parameters are identical to those of the
|
---|
708 | \cfunction{PyArg_ParseTuple()} function. The \var{kwdict} parameter
|
---|
709 | is the dictionary of keywords received as the third parameter from the
|
---|
710 | Python runtime. The \var{kwlist} parameter is a \NULL-terminated
|
---|
711 | list of strings which identify the parameters; the names are matched
|
---|
712 | with the type information from \var{format} from left to right. On
|
---|
713 | success, \cfunction{PyArg_ParseTupleAndKeywords()} returns true,
|
---|
714 | otherwise it returns false and raises an appropriate exception.
|
---|
715 |
|
---|
716 | \note{Nested tuples cannot be parsed when using keyword
|
---|
717 | arguments! Keyword parameters passed in which are not present in the
|
---|
718 | \var{kwlist} will cause \exception{TypeError} to be raised.}
|
---|
719 |
|
---|
720 | Here is an example module which uses keywords, based on an example by
|
---|
721 | Geoff Philbrick (\email{philbrick@hks.com}):%
|
---|
722 | \index{Philbrick, Geoff}
|
---|
723 |
|
---|
724 | \begin{verbatim}
|
---|
725 | #include "Python.h"
|
---|
726 |
|
---|
727 | static PyObject *
|
---|
728 | keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
|
---|
729 | {
|
---|
730 | int voltage;
|
---|
731 | char *state = "a stiff";
|
---|
732 | char *action = "voom";
|
---|
733 | char *type = "Norwegian Blue";
|
---|
734 |
|
---|
735 | static char *kwlist[] = {"voltage", "state", "action", "type", NULL};
|
---|
736 |
|
---|
737 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
|
---|
738 | &voltage, &state, &action, &type))
|
---|
739 | return NULL;
|
---|
740 |
|
---|
741 | printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
|
---|
742 | action, voltage);
|
---|
743 | printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);
|
---|
744 |
|
---|
745 | Py_INCREF(Py_None);
|
---|
746 |
|
---|
747 | return Py_None;
|
---|
748 | }
|
---|
749 |
|
---|
750 | static PyMethodDef keywdarg_methods[] = {
|
---|
751 | /* The cast of the function is necessary since PyCFunction values
|
---|
752 | * only take two PyObject* parameters, and keywdarg_parrot() takes
|
---|
753 | * three.
|
---|
754 | */
|
---|
755 | {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS | METH_KEYWORDS,
|
---|
756 | "Print a lovely skit to standard output."},
|
---|
757 | {NULL, NULL, 0, NULL} /* sentinel */
|
---|
758 | };
|
---|
759 | \end{verbatim}
|
---|
760 |
|
---|
761 | \begin{verbatim}
|
---|
762 | void
|
---|
763 | initkeywdarg(void)
|
---|
764 | {
|
---|
765 | /* Create the module and add the functions */
|
---|
766 | Py_InitModule("keywdarg", keywdarg_methods);
|
---|
767 | }
|
---|
768 | \end{verbatim}
|
---|
769 |
|
---|
770 |
|
---|
771 | \section{Building Arbitrary Values
|
---|
772 | \label{buildValue}}
|
---|
773 |
|
---|
774 | This function is the counterpart to \cfunction{PyArg_ParseTuple()}. It is
|
---|
775 | declared as follows:
|
---|
776 |
|
---|
777 | \begin{verbatim}
|
---|
778 | PyObject *Py_BuildValue(char *format, ...);
|
---|
779 | \end{verbatim}
|
---|
780 |
|
---|
781 | It recognizes a set of format units similar to the ones recognized by
|
---|
782 | \cfunction{PyArg_ParseTuple()}, but the arguments (which are input to the
|
---|
783 | function, not output) must not be pointers, just values. It returns a
|
---|
784 | new Python object, suitable for returning from a C function called
|
---|
785 | from Python.
|
---|
786 |
|
---|
787 | One difference with \cfunction{PyArg_ParseTuple()}: while the latter
|
---|
788 | requires its first argument to be a tuple (since Python argument lists
|
---|
789 | are always represented as tuples internally),
|
---|
790 | \cfunction{Py_BuildValue()} does not always build a tuple. It builds
|
---|
791 | a tuple only if its format string contains two or more format units.
|
---|
792 | If the format string is empty, it returns \code{None}; if it contains
|
---|
793 | exactly one format unit, it returns whatever object is described by
|
---|
794 | that format unit. To force it to return a tuple of size 0 or one,
|
---|
795 | parenthesize the format string.
|
---|
796 |
|
---|
797 | Examples (to the left the call, to the right the resulting Python value):
|
---|
798 |
|
---|
799 | \begin{verbatim}
|
---|
800 | Py_BuildValue("") None
|
---|
801 | Py_BuildValue("i", 123) 123
|
---|
802 | Py_BuildValue("iii", 123, 456, 789) (123, 456, 789)
|
---|
803 | Py_BuildValue("s", "hello") 'hello'
|
---|
804 | Py_BuildValue("ss", "hello", "world") ('hello', 'world')
|
---|
805 | Py_BuildValue("s#", "hello", 4) 'hell'
|
---|
806 | Py_BuildValue("()") ()
|
---|
807 | Py_BuildValue("(i)", 123) (123,)
|
---|
808 | Py_BuildValue("(ii)", 123, 456) (123, 456)
|
---|
809 | Py_BuildValue("(i,i)", 123, 456) (123, 456)
|
---|
810 | Py_BuildValue("[i,i]", 123, 456) [123, 456]
|
---|
811 | Py_BuildValue("{s:i,s:i}",
|
---|
812 | "abc", 123, "def", 456) {'abc': 123, 'def': 456}
|
---|
813 | Py_BuildValue("((ii)(ii)) (ii)",
|
---|
814 | 1, 2, 3, 4, 5, 6) (((1, 2), (3, 4)), (5, 6))
|
---|
815 | \end{verbatim}
|
---|
816 |
|
---|
817 |
|
---|
818 | \section{Reference Counts
|
---|
819 | \label{refcounts}}
|
---|
820 |
|
---|
821 | In languages like C or \Cpp, the programmer is responsible for
|
---|
822 | dynamic allocation and deallocation of memory on the heap. In C,
|
---|
823 | this is done using the functions \cfunction{malloc()} and
|
---|
824 | \cfunction{free()}. In \Cpp, the operators \keyword{new} and
|
---|
825 | \keyword{delete} are used with essentially the same meaning and
|
---|
826 | we'll restrict the following discussion to the C case.
|
---|
827 |
|
---|
828 | Every block of memory allocated with \cfunction{malloc()} should
|
---|
829 | eventually be returned to the pool of available memory by exactly one
|
---|
830 | call to \cfunction{free()}. It is important to call
|
---|
831 | \cfunction{free()} at the right time. If a block's address is
|
---|
832 | forgotten but \cfunction{free()} is not called for it, the memory it
|
---|
833 | occupies cannot be reused until the program terminates. This is
|
---|
834 | called a \dfn{memory leak}. On the other hand, if a program calls
|
---|
835 | \cfunction{free()} for a block and then continues to use the block, it
|
---|
836 | creates a conflict with re-use of the block through another
|
---|
837 | \cfunction{malloc()} call. This is called \dfn{using freed memory}.
|
---|
838 | It has the same bad consequences as referencing uninitialized data ---
|
---|
839 | core dumps, wrong results, mysterious crashes.
|
---|
840 |
|
---|
841 | Common causes of memory leaks are unusual paths through the code. For
|
---|
842 | instance, a function may allocate a block of memory, do some
|
---|
843 | calculation, and then free the block again. Now a change in the
|
---|
844 | requirements for the function may add a test to the calculation that
|
---|
845 | detects an error condition and can return prematurely from the
|
---|
846 | function. It's easy to forget to free the allocated memory block when
|
---|
847 | taking this premature exit, especially when it is added later to the
|
---|
848 | code. Such leaks, once introduced, often go undetected for a long
|
---|
849 | time: the error exit is taken only in a small fraction of all calls,
|
---|
850 | and most modern machines have plenty of virtual memory, so the leak
|
---|
851 | only becomes apparent in a long-running process that uses the leaking
|
---|
852 | function frequently. Therefore, it's important to prevent leaks from
|
---|
853 | happening by having a coding convention or strategy that minimizes
|
---|
854 | this kind of errors.
|
---|
855 |
|
---|
856 | Since Python makes heavy use of \cfunction{malloc()} and
|
---|
857 | \cfunction{free()}, it needs a strategy to avoid memory leaks as well
|
---|
858 | as the use of freed memory. The chosen method is called
|
---|
859 | \dfn{reference counting}. The principle is simple: every object
|
---|
860 | contains a counter, which is incremented when a reference to the
|
---|
861 | object is stored somewhere, and which is decremented when a reference
|
---|
862 | to it is deleted. When the counter reaches zero, the last reference
|
---|
863 | to the object has been deleted and the object is freed.
|
---|
864 |
|
---|
865 | An alternative strategy is called \dfn{automatic garbage collection}.
|
---|
866 | (Sometimes, reference counting is also referred to as a garbage
|
---|
867 | collection strategy, hence my use of ``automatic'' to distinguish the
|
---|
868 | two.) The big advantage of automatic garbage collection is that the
|
---|
869 | user doesn't need to call \cfunction{free()} explicitly. (Another claimed
|
---|
870 | advantage is an improvement in speed or memory usage --- this is no
|
---|
871 | hard fact however.) The disadvantage is that for C, there is no
|
---|
872 | truly portable automatic garbage collector, while reference counting
|
---|
873 | can be implemented portably (as long as the functions \cfunction{malloc()}
|
---|
874 | and \cfunction{free()} are available --- which the C Standard guarantees).
|
---|
875 | Maybe some day a sufficiently portable automatic garbage collector
|
---|
876 | will be available for C. Until then, we'll have to live with
|
---|
877 | reference counts.
|
---|
878 |
|
---|
879 | While Python uses the traditional reference counting implementation,
|
---|
880 | it also offers a cycle detector that works to detect reference
|
---|
881 | cycles. This allows applications to not worry about creating direct
|
---|
882 | or indirect circular references; these are the weakness of garbage
|
---|
883 | collection implemented using only reference counting. Reference
|
---|
884 | cycles consist of objects which contain (possibly indirect) references
|
---|
885 | to themselves, so that each object in the cycle has a reference count
|
---|
886 | which is non-zero. Typical reference counting implementations are not
|
---|
887 | able to reclaim the memory belonging to any objects in a reference
|
---|
888 | cycle, or referenced from the objects in the cycle, even though there
|
---|
889 | are no further references to the cycle itself.
|
---|
890 |
|
---|
891 | The cycle detector is able to detect garbage cycles and can reclaim
|
---|
892 | them so long as there are no finalizers implemented in Python
|
---|
893 | (\method{__del__()} methods). When there are such finalizers, the
|
---|
894 | detector exposes the cycles through the \ulink{\module{gc}
|
---|
895 | module}{../lib/module-gc.html} (specifically, the \code{garbage}
|
---|
896 | variable in that module). The \module{gc} module also exposes a way
|
---|
897 | to run the detector (the \function{collect()} function), as well as
|
---|
898 | configuration interfaces and the ability to disable the detector at
|
---|
899 | runtime. The cycle detector is considered an optional component;
|
---|
900 | though it is included by default, it can be disabled at build time
|
---|
901 | using the \longprogramopt{without-cycle-gc} option to the
|
---|
902 | \program{configure} script on \UNIX{} platforms (including Mac OS X)
|
---|
903 | or by removing the definition of \code{WITH_CYCLE_GC} in the
|
---|
904 | \file{pyconfig.h} header on other platforms. If the cycle detector is
|
---|
905 | disabled in this way, the \module{gc} module will not be available.
|
---|
906 |
|
---|
907 |
|
---|
908 | \subsection{Reference Counting in Python
|
---|
909 | \label{refcountsInPython}}
|
---|
910 |
|
---|
911 | There are two macros, \code{Py_INCREF(x)} and \code{Py_DECREF(x)},
|
---|
912 | which handle the incrementing and decrementing of the reference count.
|
---|
913 | \cfunction{Py_DECREF()} also frees the object when the count reaches zero.
|
---|
914 | For flexibility, it doesn't call \cfunction{free()} directly --- rather, it
|
---|
915 | makes a call through a function pointer in the object's \dfn{type
|
---|
916 | object}. For this purpose (and others), every object also contains a
|
---|
917 | pointer to its type object.
|
---|
918 |
|
---|
919 | The big question now remains: when to use \code{Py_INCREF(x)} and
|
---|
920 | \code{Py_DECREF(x)}? Let's first introduce some terms. Nobody
|
---|
921 | ``owns'' an object; however, you can \dfn{own a reference} to an
|
---|
922 | object. An object's reference count is now defined as the number of
|
---|
923 | owned references to it. The owner of a reference is responsible for
|
---|
924 | calling \cfunction{Py_DECREF()} when the reference is no longer
|
---|
925 | needed. Ownership of a reference can be transferred. There are three
|
---|
926 | ways to dispose of an owned reference: pass it on, store it, or call
|
---|
927 | \cfunction{Py_DECREF()}. Forgetting to dispose of an owned reference
|
---|
928 | creates a memory leak.
|
---|
929 |
|
---|
930 | It is also possible to \dfn{borrow}\footnote{The metaphor of
|
---|
931 | ``borrowing'' a reference is not completely correct: the owner still
|
---|
932 | has a copy of the reference.} a reference to an object. The borrower
|
---|
933 | of a reference should not call \cfunction{Py_DECREF()}. The borrower must
|
---|
934 | not hold on to the object longer than the owner from which it was
|
---|
935 | borrowed. Using a borrowed reference after the owner has disposed of
|
---|
936 | it risks using freed memory and should be avoided
|
---|
937 | completely.\footnote{Checking that the reference count is at least 1
|
---|
938 | \strong{does not work} --- the reference count itself could be in
|
---|
939 | freed memory and may thus be reused for another object!}
|
---|
940 |
|
---|
941 | The advantage of borrowing over owning a reference is that you don't
|
---|
942 | need to take care of disposing of the reference on all possible paths
|
---|
943 | through the code --- in other words, with a borrowed reference you
|
---|
944 | don't run the risk of leaking when a premature exit is taken. The
|
---|
945 | disadvantage of borrowing over leaking is that there are some subtle
|
---|
946 | situations where in seemingly correct code a borrowed reference can be
|
---|
947 | used after the owner from which it was borrowed has in fact disposed
|
---|
948 | of it.
|
---|
949 |
|
---|
950 | A borrowed reference can be changed into an owned reference by calling
|
---|
951 | \cfunction{Py_INCREF()}. This does not affect the status of the owner from
|
---|
952 | which the reference was borrowed --- it creates a new owned reference,
|
---|
953 | and gives full owner responsibilities (the new owner must
|
---|
954 | dispose of the reference properly, as well as the previous owner).
|
---|
955 |
|
---|
956 |
|
---|
957 | \subsection{Ownership Rules
|
---|
958 | \label{ownershipRules}}
|
---|
959 |
|
---|
960 | Whenever an object reference is passed into or out of a function, it
|
---|
961 | is part of the function's interface specification whether ownership is
|
---|
962 | transferred with the reference or not.
|
---|
963 |
|
---|
964 | Most functions that return a reference to an object pass on ownership
|
---|
965 | with the reference. In particular, all functions whose function it is
|
---|
966 | to create a new object, such as \cfunction{PyInt_FromLong()} and
|
---|
967 | \cfunction{Py_BuildValue()}, pass ownership to the receiver. Even if
|
---|
968 | the object is not actually new, you still receive ownership of a new
|
---|
969 | reference to that object. For instance, \cfunction{PyInt_FromLong()}
|
---|
970 | maintains a cache of popular values and can return a reference to a
|
---|
971 | cached item.
|
---|
972 |
|
---|
973 | Many functions that extract objects from other objects also transfer
|
---|
974 | ownership with the reference, for instance
|
---|
975 | \cfunction{PyObject_GetAttrString()}. The picture is less clear, here,
|
---|
976 | however, since a few common routines are exceptions:
|
---|
977 | \cfunction{PyTuple_GetItem()}, \cfunction{PyList_GetItem()},
|
---|
978 | \cfunction{PyDict_GetItem()}, and \cfunction{PyDict_GetItemString()}
|
---|
979 | all return references that you borrow from the tuple, list or
|
---|
980 | dictionary.
|
---|
981 |
|
---|
982 | The function \cfunction{PyImport_AddModule()} also returns a borrowed
|
---|
983 | reference, even though it may actually create the object it returns:
|
---|
984 | this is possible because an owned reference to the object is stored in
|
---|
985 | \code{sys.modules}.
|
---|
986 |
|
---|
987 | When you pass an object reference into another function, in general,
|
---|
988 | the function borrows the reference from you --- if it needs to store
|
---|
989 | it, it will use \cfunction{Py_INCREF()} to become an independent
|
---|
990 | owner. There are exactly two important exceptions to this rule:
|
---|
991 | \cfunction{PyTuple_SetItem()} and \cfunction{PyList_SetItem()}. These
|
---|
992 | functions take over ownership of the item passed to them --- even if
|
---|
993 | they fail! (Note that \cfunction{PyDict_SetItem()} and friends don't
|
---|
994 | take over ownership --- they are ``normal.'')
|
---|
995 |
|
---|
996 | When a C function is called from Python, it borrows references to its
|
---|
997 | arguments from the caller. The caller owns a reference to the object,
|
---|
998 | so the borrowed reference's lifetime is guaranteed until the function
|
---|
999 | returns. Only when such a borrowed reference must be stored or passed
|
---|
1000 | on, it must be turned into an owned reference by calling
|
---|
1001 | \cfunction{Py_INCREF()}.
|
---|
1002 |
|
---|
1003 | The object reference returned from a C function that is called from
|
---|
1004 | Python must be an owned reference --- ownership is transferred from
|
---|
1005 | the function to its caller.
|
---|
1006 |
|
---|
1007 |
|
---|
1008 | \subsection{Thin Ice
|
---|
1009 | \label{thinIce}}
|
---|
1010 |
|
---|
1011 | There are a few situations where seemingly harmless use of a borrowed
|
---|
1012 | reference can lead to problems. These all have to do with implicit
|
---|
1013 | invocations of the interpreter, which can cause the owner of a
|
---|
1014 | reference to dispose of it.
|
---|
1015 |
|
---|
1016 | The first and most important case to know about is using
|
---|
1017 | \cfunction{Py_DECREF()} on an unrelated object while borrowing a
|
---|
1018 | reference to a list item. For instance:
|
---|
1019 |
|
---|
1020 | \begin{verbatim}
|
---|
1021 | void
|
---|
1022 | bug(PyObject *list)
|
---|
1023 | {
|
---|
1024 | PyObject *item = PyList_GetItem(list, 0);
|
---|
1025 |
|
---|
1026 | PyList_SetItem(list, 1, PyInt_FromLong(0L));
|
---|
1027 | PyObject_Print(item, stdout, 0); /* BUG! */
|
---|
1028 | }
|
---|
1029 | \end{verbatim}
|
---|
1030 |
|
---|
1031 | This function first borrows a reference to \code{list[0]}, then
|
---|
1032 | replaces \code{list[1]} with the value \code{0}, and finally prints
|
---|
1033 | the borrowed reference. Looks harmless, right? But it's not!
|
---|
1034 |
|
---|
1035 | Let's follow the control flow into \cfunction{PyList_SetItem()}. The list
|
---|
1036 | owns references to all its items, so when item 1 is replaced, it has
|
---|
1037 | to dispose of the original item 1. Now let's suppose the original
|
---|
1038 | item 1 was an instance of a user-defined class, and let's further
|
---|
1039 | suppose that the class defined a \method{__del__()} method. If this
|
---|
1040 | class instance has a reference count of 1, disposing of it will call
|
---|
1041 | its \method{__del__()} method.
|
---|
1042 |
|
---|
1043 | Since it is written in Python, the \method{__del__()} method can execute
|
---|
1044 | arbitrary Python code. Could it perhaps do something to invalidate
|
---|
1045 | the reference to \code{item} in \cfunction{bug()}? You bet! Assuming
|
---|
1046 | that the list passed into \cfunction{bug()} is accessible to the
|
---|
1047 | \method{__del__()} method, it could execute a statement to the effect of
|
---|
1048 | \samp{del list[0]}, and assuming this was the last reference to that
|
---|
1049 | object, it would free the memory associated with it, thereby
|
---|
1050 | invalidating \code{item}.
|
---|
1051 |
|
---|
1052 | The solution, once you know the source of the problem, is easy:
|
---|
1053 | temporarily increment the reference count. The correct version of the
|
---|
1054 | function reads:
|
---|
1055 |
|
---|
1056 | \begin{verbatim}
|
---|
1057 | void
|
---|
1058 | no_bug(PyObject *list)
|
---|
1059 | {
|
---|
1060 | PyObject *item = PyList_GetItem(list, 0);
|
---|
1061 |
|
---|
1062 | Py_INCREF(item);
|
---|
1063 | PyList_SetItem(list, 1, PyInt_FromLong(0L));
|
---|
1064 | PyObject_Print(item, stdout, 0);
|
---|
1065 | Py_DECREF(item);
|
---|
1066 | }
|
---|
1067 | \end{verbatim}
|
---|
1068 |
|
---|
1069 | This is a true story. An older version of Python contained variants
|
---|
1070 | of this bug and someone spent a considerable amount of time in a C
|
---|
1071 | debugger to figure out why his \method{__del__()} methods would fail...
|
---|
1072 |
|
---|
1073 | The second case of problems with a borrowed reference is a variant
|
---|
1074 | involving threads. Normally, multiple threads in the Python
|
---|
1075 | interpreter can't get in each other's way, because there is a global
|
---|
1076 | lock protecting Python's entire object space. However, it is possible
|
---|
1077 | to temporarily release this lock using the macro
|
---|
1078 | \csimplemacro{Py_BEGIN_ALLOW_THREADS}, and to re-acquire it using
|
---|
1079 | \csimplemacro{Py_END_ALLOW_THREADS}. This is common around blocking
|
---|
1080 | I/O calls, to let other threads use the processor while waiting for
|
---|
1081 | the I/O to complete. Obviously, the following function has the same
|
---|
1082 | problem as the previous one:
|
---|
1083 |
|
---|
1084 | \begin{verbatim}
|
---|
1085 | void
|
---|
1086 | bug(PyObject *list)
|
---|
1087 | {
|
---|
1088 | PyObject *item = PyList_GetItem(list, 0);
|
---|
1089 | Py_BEGIN_ALLOW_THREADS
|
---|
1090 | ...some blocking I/O call...
|
---|
1091 | Py_END_ALLOW_THREADS
|
---|
1092 | PyObject_Print(item, stdout, 0); /* BUG! */
|
---|
1093 | }
|
---|
1094 | \end{verbatim}
|
---|
1095 |
|
---|
1096 |
|
---|
1097 | \subsection{NULL Pointers
|
---|
1098 | \label{nullPointers}}
|
---|
1099 |
|
---|
1100 | In general, functions that take object references as arguments do not
|
---|
1101 | expect you to pass them \NULL{} pointers, and will dump core (or
|
---|
1102 | cause later core dumps) if you do so. Functions that return object
|
---|
1103 | references generally return \NULL{} only to indicate that an
|
---|
1104 | exception occurred. The reason for not testing for \NULL{}
|
---|
1105 | arguments is that functions often pass the objects they receive on to
|
---|
1106 | other function --- if each function were to test for \NULL,
|
---|
1107 | there would be a lot of redundant tests and the code would run more
|
---|
1108 | slowly.
|
---|
1109 |
|
---|
1110 | It is better to test for \NULL{} only at the ``source:'' when a
|
---|
1111 | pointer that may be \NULL{} is received, for example, from
|
---|
1112 | \cfunction{malloc()} or from a function that may raise an exception.
|
---|
1113 |
|
---|
1114 | The macros \cfunction{Py_INCREF()} and \cfunction{Py_DECREF()}
|
---|
1115 | do not check for \NULL{} pointers --- however, their variants
|
---|
1116 | \cfunction{Py_XINCREF()} and \cfunction{Py_XDECREF()} do.
|
---|
1117 |
|
---|
1118 | The macros for checking for a particular object type
|
---|
1119 | (\code{Py\var{type}_Check()}) don't check for \NULL{} pointers ---
|
---|
1120 | again, there is much code that calls several of these in a row to test
|
---|
1121 | an object against various different expected types, and this would
|
---|
1122 | generate redundant tests. There are no variants with \NULL{}
|
---|
1123 | checking.
|
---|
1124 |
|
---|
1125 | The C function calling mechanism guarantees that the argument list
|
---|
1126 | passed to C functions (\code{args} in the examples) is never
|
---|
1127 | \NULL{} --- in fact it guarantees that it is always a tuple.\footnote{
|
---|
1128 | These guarantees don't hold when you use the ``old'' style
|
---|
1129 | calling convention --- this is still found in much existing code.}
|
---|
1130 |
|
---|
1131 | It is a severe error to ever let a \NULL{} pointer ``escape'' to
|
---|
1132 | the Python user.
|
---|
1133 |
|
---|
1134 | % Frank Stajano:
|
---|
1135 | % A pedagogically buggy example, along the lines of the previous listing,
|
---|
1136 | % would be helpful here -- showing in more concrete terms what sort of
|
---|
1137 | % actions could cause the problem. I can't very well imagine it from the
|
---|
1138 | % description.
|
---|
1139 |
|
---|
1140 |
|
---|
1141 | \section{Writing Extensions in \Cpp
|
---|
1142 | \label{cplusplus}}
|
---|
1143 |
|
---|
1144 | It is possible to write extension modules in \Cpp. Some restrictions
|
---|
1145 | apply. If the main program (the Python interpreter) is compiled and
|
---|
1146 | linked by the C compiler, global or static objects with constructors
|
---|
1147 | cannot be used. This is not a problem if the main program is linked
|
---|
1148 | by the \Cpp{} compiler. Functions that will be called by the
|
---|
1149 | Python interpreter (in particular, module initialization functions)
|
---|
1150 | have to be declared using \code{extern "C"}.
|
---|
1151 | It is unnecessary to enclose the Python header files in
|
---|
1152 | \code{extern "C" \{...\}} --- they use this form already if the symbol
|
---|
1153 | \samp{__cplusplus} is defined (all recent \Cpp{} compilers define this
|
---|
1154 | symbol).
|
---|
1155 |
|
---|
1156 |
|
---|
1157 | \section{Providing a C API for an Extension Module
|
---|
1158 | \label{using-cobjects}}
|
---|
1159 | \sectionauthor{Konrad Hinsen}{hinsen@cnrs-orleans.fr}
|
---|
1160 |
|
---|
1161 | Many extension modules just provide new functions and types to be
|
---|
1162 | used from Python, but sometimes the code in an extension module can
|
---|
1163 | be useful for other extension modules. For example, an extension
|
---|
1164 | module could implement a type ``collection'' which works like lists
|
---|
1165 | without order. Just like the standard Python list type has a C API
|
---|
1166 | which permits extension modules to create and manipulate lists, this
|
---|
1167 | new collection type should have a set of C functions for direct
|
---|
1168 | manipulation from other extension modules.
|
---|
1169 |
|
---|
1170 | At first sight this seems easy: just write the functions (without
|
---|
1171 | declaring them \keyword{static}, of course), provide an appropriate
|
---|
1172 | header file, and document the C API. And in fact this would work if
|
---|
1173 | all extension modules were always linked statically with the Python
|
---|
1174 | interpreter. When modules are used as shared libraries, however, the
|
---|
1175 | symbols defined in one module may not be visible to another module.
|
---|
1176 | The details of visibility depend on the operating system; some systems
|
---|
1177 | use one global namespace for the Python interpreter and all extension
|
---|
1178 | modules (Windows, for example), whereas others require an explicit
|
---|
1179 | list of imported symbols at module link time (AIX is one example), or
|
---|
1180 | offer a choice of different strategies (most Unices). And even if
|
---|
1181 | symbols are globally visible, the module whose functions one wishes to
|
---|
1182 | call might not have been loaded yet!
|
---|
1183 |
|
---|
1184 | Portability therefore requires not to make any assumptions about
|
---|
1185 | symbol visibility. This means that all symbols in extension modules
|
---|
1186 | should be declared \keyword{static}, except for the module's
|
---|
1187 | initialization function, in order to avoid name clashes with other
|
---|
1188 | extension modules (as discussed in section~\ref{methodTable}). And it
|
---|
1189 | means that symbols that \emph{should} be accessible from other
|
---|
1190 | extension modules must be exported in a different way.
|
---|
1191 |
|
---|
1192 | Python provides a special mechanism to pass C-level information
|
---|
1193 | (pointers) from one extension module to another one: CObjects.
|
---|
1194 | A CObject is a Python data type which stores a pointer (\ctype{void
|
---|
1195 | *}). CObjects can only be created and accessed via their C API, but
|
---|
1196 | they can be passed around like any other Python object. In particular,
|
---|
1197 | they can be assigned to a name in an extension module's namespace.
|
---|
1198 | Other extension modules can then import this module, retrieve the
|
---|
1199 | value of this name, and then retrieve the pointer from the CObject.
|
---|
1200 |
|
---|
1201 | There are many ways in which CObjects can be used to export the C API
|
---|
1202 | of an extension module. Each name could get its own CObject, or all C
|
---|
1203 | API pointers could be stored in an array whose address is published in
|
---|
1204 | a CObject. And the various tasks of storing and retrieving the pointers
|
---|
1205 | can be distributed in different ways between the module providing the
|
---|
1206 | code and the client modules.
|
---|
1207 |
|
---|
1208 | The following example demonstrates an approach that puts most of the
|
---|
1209 | burden on the writer of the exporting module, which is appropriate
|
---|
1210 | for commonly used library modules. It stores all C API pointers
|
---|
1211 | (just one in the example!) in an array of \ctype{void} pointers which
|
---|
1212 | becomes the value of a CObject. The header file corresponding to
|
---|
1213 | the module provides a macro that takes care of importing the module
|
---|
1214 | and retrieving its C API pointers; client modules only have to call
|
---|
1215 | this macro before accessing the C API.
|
---|
1216 |
|
---|
1217 | The exporting module is a modification of the \module{spam} module from
|
---|
1218 | section~\ref{simpleExample}. The function \function{spam.system()}
|
---|
1219 | does not call the C library function \cfunction{system()} directly,
|
---|
1220 | but a function \cfunction{PySpam_System()}, which would of course do
|
---|
1221 | something more complicated in reality (such as adding ``spam'' to
|
---|
1222 | every command). This function \cfunction{PySpam_System()} is also
|
---|
1223 | exported to other extension modules.
|
---|
1224 |
|
---|
1225 | The function \cfunction{PySpam_System()} is a plain C function,
|
---|
1226 | declared \keyword{static} like everything else:
|
---|
1227 |
|
---|
1228 | \begin{verbatim}
|
---|
1229 | static int
|
---|
1230 | PySpam_System(const char *command)
|
---|
1231 | {
|
---|
1232 | return system(command);
|
---|
1233 | }
|
---|
1234 | \end{verbatim}
|
---|
1235 |
|
---|
1236 | The function \cfunction{spam_system()} is modified in a trivial way:
|
---|
1237 |
|
---|
1238 | \begin{verbatim}
|
---|
1239 | static PyObject *
|
---|
1240 | spam_system(PyObject *self, PyObject *args)
|
---|
1241 | {
|
---|
1242 | const char *command;
|
---|
1243 | int sts;
|
---|
1244 |
|
---|
1245 | if (!PyArg_ParseTuple(args, "s", &command))
|
---|
1246 | return NULL;
|
---|
1247 | sts = PySpam_System(command);
|
---|
1248 | return Py_BuildValue("i", sts);
|
---|
1249 | }
|
---|
1250 | \end{verbatim}
|
---|
1251 |
|
---|
1252 | In the beginning of the module, right after the line
|
---|
1253 |
|
---|
1254 | \begin{verbatim}
|
---|
1255 | #include "Python.h"
|
---|
1256 | \end{verbatim}
|
---|
1257 |
|
---|
1258 | two more lines must be added:
|
---|
1259 |
|
---|
1260 | \begin{verbatim}
|
---|
1261 | #define SPAM_MODULE
|
---|
1262 | #include "spammodule.h"
|
---|
1263 | \end{verbatim}
|
---|
1264 |
|
---|
1265 | The \code{\#define} is used to tell the header file that it is being
|
---|
1266 | included in the exporting module, not a client module. Finally,
|
---|
1267 | the module's initialization function must take care of initializing
|
---|
1268 | the C API pointer array:
|
---|
1269 |
|
---|
1270 | \begin{verbatim}
|
---|
1271 | PyMODINIT_FUNC
|
---|
1272 | initspam(void)
|
---|
1273 | {
|
---|
1274 | PyObject *m;
|
---|
1275 | static void *PySpam_API[PySpam_API_pointers];
|
---|
1276 | PyObject *c_api_object;
|
---|
1277 |
|
---|
1278 | m = Py_InitModule("spam", SpamMethods);
|
---|
1279 |
|
---|
1280 | /* Initialize the C API pointer array */
|
---|
1281 | PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;
|
---|
1282 |
|
---|
1283 | /* Create a CObject containing the API pointer array's address */
|
---|
1284 | c_api_object = PyCObject_FromVoidPtr((void *)PySpam_API, NULL);
|
---|
1285 |
|
---|
1286 | if (c_api_object != NULL)
|
---|
1287 | PyModule_AddObject(m, "_C_API", c_api_object);
|
---|
1288 | }
|
---|
1289 | \end{verbatim}
|
---|
1290 |
|
---|
1291 | Note that \code{PySpam_API} is declared \keyword{static}; otherwise
|
---|
1292 | the pointer array would disappear when \function{initspam()} terminates!
|
---|
1293 |
|
---|
1294 | The bulk of the work is in the header file \file{spammodule.h},
|
---|
1295 | which looks like this:
|
---|
1296 |
|
---|
1297 | \begin{verbatim}
|
---|
1298 | #ifndef Py_SPAMMODULE_H
|
---|
1299 | #define Py_SPAMMODULE_H
|
---|
1300 | #ifdef __cplusplus
|
---|
1301 | extern "C" {
|
---|
1302 | #endif
|
---|
1303 |
|
---|
1304 | /* Header file for spammodule */
|
---|
1305 |
|
---|
1306 | /* C API functions */
|
---|
1307 | #define PySpam_System_NUM 0
|
---|
1308 | #define PySpam_System_RETURN int
|
---|
1309 | #define PySpam_System_PROTO (const char *command)
|
---|
1310 |
|
---|
1311 | /* Total number of C API pointers */
|
---|
1312 | #define PySpam_API_pointers 1
|
---|
1313 |
|
---|
1314 |
|
---|
1315 | #ifdef SPAM_MODULE
|
---|
1316 | /* This section is used when compiling spammodule.c */
|
---|
1317 |
|
---|
1318 | static PySpam_System_RETURN PySpam_System PySpam_System_PROTO;
|
---|
1319 |
|
---|
1320 | #else
|
---|
1321 | /* This section is used in modules that use spammodule's API */
|
---|
1322 |
|
---|
1323 | static void **PySpam_API;
|
---|
1324 |
|
---|
1325 | #define PySpam_System \
|
---|
1326 | (*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])
|
---|
1327 |
|
---|
1328 | /* Return -1 and set exception on error, 0 on success. */
|
---|
1329 | static int
|
---|
1330 | import_spam(void)
|
---|
1331 | {
|
---|
1332 | PyObject *module = PyImport_ImportModule("spam");
|
---|
1333 |
|
---|
1334 | if (module != NULL) {
|
---|
1335 | PyObject *c_api_object = PyObject_GetAttrString(module, "_C_API");
|
---|
1336 | if (c_api_object == NULL)
|
---|
1337 | return -1;
|
---|
1338 | if (PyCObject_Check(c_api_object))
|
---|
1339 | PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object);
|
---|
1340 | Py_DECREF(c_api_object);
|
---|
1341 | }
|
---|
1342 | return 0;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | #endif
|
---|
1346 |
|
---|
1347 | #ifdef __cplusplus
|
---|
1348 | }
|
---|
1349 | #endif
|
---|
1350 |
|
---|
1351 | #endif /* !defined(Py_SPAMMODULE_H) */
|
---|
1352 | \end{verbatim}
|
---|
1353 |
|
---|
1354 | All that a client module must do in order to have access to the
|
---|
1355 | function \cfunction{PySpam_System()} is to call the function (or
|
---|
1356 | rather macro) \cfunction{import_spam()} in its initialization
|
---|
1357 | function:
|
---|
1358 |
|
---|
1359 | \begin{verbatim}
|
---|
1360 | PyMODINIT_FUNC
|
---|
1361 | initclient(void)
|
---|
1362 | {
|
---|
1363 | PyObject *m;
|
---|
1364 |
|
---|
1365 | Py_InitModule("client", ClientMethods);
|
---|
1366 | if (import_spam() < 0)
|
---|
1367 | return;
|
---|
1368 | /* additional initialization can happen here */
|
---|
1369 | }
|
---|
1370 | \end{verbatim}
|
---|
1371 |
|
---|
1372 | The main disadvantage of this approach is that the file
|
---|
1373 | \file{spammodule.h} is rather complicated. However, the
|
---|
1374 | basic structure is the same for each function that is
|
---|
1375 | exported, so it has to be learned only once.
|
---|
1376 |
|
---|
1377 | Finally it should be mentioned that CObjects offer additional
|
---|
1378 | functionality, which is especially useful for memory allocation and
|
---|
1379 | deallocation of the pointer stored in a CObject. The details
|
---|
1380 | are described in the \citetitle[../api/api.html]{Python/C API
|
---|
1381 | Reference Manual} in the section
|
---|
1382 | ``\ulink{CObjects}{../api/cObjects.html}'' and in the implementation
|
---|
1383 | of CObjects (files \file{Include/cobject.h} and
|
---|
1384 | \file{Objects/cobject.c} in the Python source code distribution).
|
---|