blob: be5c51c631e3c55f55178831c94443cb78529352 [file] [log] [blame]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001=======================
2Extending/Embedding FAQ
3=======================
4
5.. contents::
6
7.. highlight:: c
8
9
10Can I create my own functions in C?
11-----------------------------------
12
13Yes, you can create built-in modules containing functions, variables, exceptions
14and even new types in C. This is explained in the document
15:ref:`extending-index`.
16
17Most intermediate or advanced Python books will also cover this topic.
18
19
20Can I create my own functions in C++?
21-------------------------------------
22
23Yes, using the C compatibility features found in C++. Place ``extern "C" {
24... }`` around the Python include files and put ``extern "C"`` before each
25function that is going to be called by the Python interpreter. Global or static
26C++ objects with constructors are probably not a good idea.
27
28
Georg Brandle0289a32010-08-01 21:44:38 +000029.. _c-wrapper-software:
30
Georg Brandl6728c5a2009-10-11 18:31:23 +000031Writing C is hard; are there any alternatives?
32----------------------------------------------
33
34There are a number of alternatives to writing your own C extensions, depending
35on what you're trying to do.
36
37.. XXX make sure these all work; mention Cython
38
39If you need more speed, `Psyco <http://psyco.sourceforge.net/>`_ generates x86
40assembly code from Python bytecode. You can use Psyco to compile the most
41time-critical functions in your code, and gain a significant improvement with
42very little effort, as long as you're running on a machine with an
43x86-compatible processor.
44
45`Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_ is a compiler
46that accepts a slightly modified form of Python and generates the corresponding
47C code. Pyrex makes it possible to write an extension without having to learn
48Python's C API.
49
50If you need to interface to some C or C++ library for which no Python extension
51currently exists, you can try wrapping the library's data types and functions
52with a tool such as `SWIG <http://www.swig.org>`_. `SIP
Georg Brandl4ebf8072009-10-22 07:56:56 +000053<http://www.riverbankcomputing.co.uk/software/sip/>`__, `CXX
Georg Brandl6728c5a2009-10-11 18:31:23 +000054<http://cxx.sourceforge.net/>`_ `Boost
55<http://www.boost.org/libs/python/doc/index.html>`_, or `Weave
Ezio Melotti425aa2e2010-04-05 12:51:45 +000056<http://www.scipy.org/Weave>`_ are also alternatives for wrapping
Georg Brandl6728c5a2009-10-11 18:31:23 +000057C++ libraries.
58
59
60How can I execute arbitrary Python statements from C?
61-----------------------------------------------------
62
63The highest-level function to do this is :cfunc:`PyRun_SimpleString` which takes
64a single string argument to be executed in the context of the module
65``__main__`` and returns 0 for success and -1 when an exception occurred
66(including ``SyntaxError``). If you want more control, use
67:cfunc:`PyRun_String`; see the source for :cfunc:`PyRun_SimpleString` in
68``Python/pythonrun.c``.
69
70
71How can I evaluate an arbitrary Python expression from C?
72---------------------------------------------------------
73
74Call the function :cfunc:`PyRun_String` from the previous question with the
75start symbol :cdata:`Py_eval_input`; it parses an expression, evaluates it and
76returns its value.
77
78
79How do I extract C values from a Python object?
80-----------------------------------------------
81
82That depends on the object's type. If it's a tuple, :cfunc:`PyTuple_Size`
83returns its length and :cfunc:`PyTuple_GetItem` returns the item at a specified
84index. Lists have similar functions, :cfunc:`PyListSize` and
85:cfunc:`PyList_GetItem`.
86
87For strings, :cfunc:`PyString_Size` returns its length and
88:cfunc:`PyString_AsString` a pointer to its value. Note that Python strings may
89contain null bytes so C's :cfunc:`strlen` should not be used.
90
91To test the type of an object, first make sure it isn't *NULL*, and then use
92:cfunc:`PyString_Check`, :cfunc:`PyTuple_Check`, :cfunc:`PyList_Check`, etc.
93
94There is also a high-level API to Python objects which is provided by the
95so-called 'abstract' interface -- read ``Include/abstract.h`` for further
96details. It allows interfacing with any kind of Python sequence using calls
97like :cfunc:`PySequence_Length`, :cfunc:`PySequence_GetItem`, etc.) as well as
98many other useful protocols.
99
100
101How do I use Py_BuildValue() to create a tuple of arbitrary length?
102-------------------------------------------------------------------
103
104You can't. Use ``t = PyTuple_New(n)`` instead, and fill it with objects using
105``PyTuple_SetItem(t, i, o)`` -- note that this "eats" a reference count of
106``o``, so you have to :cfunc:`Py_INCREF` it. Lists have similar functions
107``PyList_New(n)`` and ``PyList_SetItem(l, i, o)``. Note that you *must* set all
108the tuple items to some value before you pass the tuple to Python code --
109``PyTuple_New(n)`` initializes them to NULL, which isn't a valid Python value.
110
111
112How do I call an object's method from C?
113----------------------------------------
114
115The :cfunc:`PyObject_CallMethod` function can be used to call an arbitrary
116method of an object. The parameters are the object, the name of the method to
117call, a format string like that used with :cfunc:`Py_BuildValue`, and the
118argument values::
119
120 PyObject *
121 PyObject_CallMethod(PyObject *object, char *method_name,
122 char *arg_format, ...);
123
124This works for any object that has methods -- whether built-in or user-defined.
125You are responsible for eventually :cfunc:`Py_DECREF`\ 'ing the return value.
126
127To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the
128file object pointer is "f")::
129
130 res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0);
131 if (res == NULL) {
132 ... an exception occurred ...
133 }
134 else {
135 Py_DECREF(res);
136 }
137
138Note that since :cfunc:`PyObject_CallObject` *always* wants a tuple for the
139argument list, to call a function without arguments, pass "()" for the format,
140and to call a function with one argument, surround the argument in parentheses,
141e.g. "(i)".
142
143
144How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?
145----------------------------------------------------------------------------------------
146
147In Python code, define an object that supports the ``write()`` method. Assign
148this object to :data:`sys.stdout` and :data:`sys.stderr`. Call print_error, or
149just allow the standard traceback mechanism to work. Then, the output will go
150wherever your ``write()`` method sends it.
151
152The easiest way to do this is to use the StringIO class in the standard library.
153
154Sample code and use for catching stdout:
155
156 >>> class StdoutCatcher:
157 ... def __init__(self):
158 ... self.data = ''
159 ... def write(self, stuff):
160 ... self.data = self.data + stuff
161 ...
162 >>> import sys
163 >>> sys.stdout = StdoutCatcher()
164 >>> print 'foo'
165 >>> print 'hello world!'
166 >>> sys.stderr.write(sys.stdout.data)
167 foo
168 hello world!
169
170
171How do I access a module written in Python from C?
172--------------------------------------------------
173
174You can get a pointer to the module object as follows::
175
176 module = PyImport_ImportModule("<modulename>");
177
178If the module hasn't been imported yet (i.e. it is not yet present in
179:data:`sys.modules`), this initializes the module; otherwise it simply returns
180the value of ``sys.modules["<modulename>"]``. Note that it doesn't enter the
181module into any namespace -- it only ensures it has been initialized and is
182stored in :data:`sys.modules`.
183
184You can then access the module's attributes (i.e. any name defined in the
185module) as follows::
186
187 attr = PyObject_GetAttrString(module, "<attrname>");
188
189Calling :cfunc:`PyObject_SetAttrString` to assign to variables in the module
190also works.
191
192
193How do I interface to C++ objects from Python?
194----------------------------------------------
195
196Depending on your requirements, there are many approaches. To do this manually,
197begin by reading :ref:`the "Extending and Embedding" document
198<extending-index>`. Realize that for the Python run-time system, there isn't a
199whole lot of difference between C and C++ -- so the strategy of building a new
200Python type around a C structure (pointer) type will also work for C++ objects.
201
Georg Brandle0289a32010-08-01 21:44:38 +0000202For C++ libraries, see :ref:`c-wrapper-software`.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000203
204
205I added a module using the Setup file and the make fails; why?
206--------------------------------------------------------------
207
208Setup must end in a newline, if there is no newline there, the build process
209fails. (Fixing this requires some ugly shell script hackery, and this bug is so
210minor that it doesn't seem worth the effort.)
211
212
213How do I debug an extension?
214----------------------------
215
216When using GDB with dynamically loaded extensions, you can't set a breakpoint in
217your extension until your extension is loaded.
218
219In your ``.gdbinit`` file (or interactively), add the command::
220
221 br _PyImport_LoadDynamicModule
222
223Then, when you run GDB::
224
225 $ gdb /local/bin/python
226 gdb) run myscript.py
227 gdb) continue # repeat until your extension is loaded
228 gdb) finish # so that your extension is loaded
229 gdb) br myfunction.c:50
230 gdb) continue
231
232I want to compile a Python module on my Linux system, but some files are missing. Why?
233--------------------------------------------------------------------------------------
234
235Most packaged versions of Python don't include the
236:file:`/usr/lib/python2.{x}/config/` directory, which contains various files
237required for compiling Python extensions.
238
239For Red Hat, install the python-devel RPM to get the necessary files.
240
241For Debian, run ``apt-get install python-dev``.
242
243
244What does "SystemError: _PyImport_FixupExtension: module yourmodule not loaded" mean?
245-------------------------------------------------------------------------------------
246
247This means that you have created an extension module named "yourmodule", but
248your module init function does not initialize with that name.
249
250Every module init function will have a line similar to::
251
252 module = Py_InitModule("yourmodule", yourmodule_functions);
253
254If the string passed to this function is not the same name as your extension
255module, the :exc:`SystemError` exception will be raised.
256
257
258How do I tell "incomplete input" from "invalid input"?
259------------------------------------------------------
260
261Sometimes you want to emulate the Python interactive interpreter's behavior,
262where it gives you a continuation prompt when the input is incomplete (e.g. you
263typed the start of an "if" statement or you didn't close your parentheses or
264triple string quotes), but it gives you a syntax error message immediately when
265the input is invalid.
266
267In Python you can use the :mod:`codeop` module, which approximates the parser's
268behavior sufficiently. IDLE uses this, for example.
269
270The easiest way to do it in C is to call :cfunc:`PyRun_InteractiveLoop` (perhaps
271in a separate thread) and let the Python interpreter handle the input for
272you. You can also set the :cfunc:`PyOS_ReadlineFunctionPointer` to point at your
273custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c``
274for more hints.
275
276However sometimes you have to run the embedded Python interpreter in the same
277thread as your rest application and you can't allow the
278:cfunc:`PyRun_InteractiveLoop` to stop while waiting for user input. The one
279solution then is to call :cfunc:`PyParser_ParseString` and test for ``e.error``
280equal to ``E_EOF``, which means the input is incomplete). Here's a sample code
281fragment, untested, inspired by code from Alex Farber::
282
283 #include <Python.h>
284 #include <node.h>
285 #include <errcode.h>
286 #include <grammar.h>
287 #include <parsetok.h>
288 #include <compile.h>
289
290 int testcomplete(char *code)
291 /* code should end in \n */
292 /* return -1 for error, 0 for incomplete, 1 for complete */
293 {
294 node *n;
295 perrdetail e;
296
297 n = PyParser_ParseString(code, &_PyParser_Grammar,
298 Py_file_input, &e);
299 if (n == NULL) {
300 if (e.error == E_EOF)
301 return 0;
302 return -1;
303 }
304
305 PyNode_Free(n);
306 return 1;
307 }
308
309Another solution is trying to compile the received string with
310:cfunc:`Py_CompileString`. If it compiles without errors, try to execute the
311returned code object by calling :cfunc:`PyEval_EvalCode`. Otherwise save the
312input for later. If the compilation fails, find out if it's an error or just
313more input is required - by extracting the message string from the exception
314tuple and comparing it to the string "unexpected EOF while parsing". Here is a
315complete example using the GNU readline library (you may want to ignore
316**SIGINT** while calling readline())::
317
318 #include <stdio.h>
319 #include <readline.h>
320
321 #include <Python.h>
322 #include <object.h>
323 #include <compile.h>
324 #include <eval.h>
325
326 int main (int argc, char* argv[])
327 {
328 int i, j, done = 0; /* lengths of line, code */
329 char ps1[] = ">>> ";
330 char ps2[] = "... ";
331 char *prompt = ps1;
332 char *msg, *line, *code = NULL;
333 PyObject *src, *glb, *loc;
334 PyObject *exc, *val, *trb, *obj, *dum;
335
336 Py_Initialize ();
337 loc = PyDict_New ();
338 glb = PyDict_New ();
339 PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());
340
341 while (!done)
342 {
343 line = readline (prompt);
344
345 if (NULL == line) /* CTRL-D pressed */
346 {
347 done = 1;
348 }
349 else
350 {
351 i = strlen (line);
352
353 if (i > 0)
354 add_history (line); /* save non-empty lines */
355
356 if (NULL == code) /* nothing in code yet */
357 j = 0;
358 else
359 j = strlen (code);
360
361 code = realloc (code, i + j + 2);
362 if (NULL == code) /* out of memory */
363 exit (1);
364
365 if (0 == j) /* code was empty, so */
366 code[0] = '\0'; /* keep strncat happy */
367
368 strncat (code, line, i); /* append line to code */
369 code[i + j] = '\n'; /* append '\n' to code */
370 code[i + j + 1] = '\0';
371
372 src = Py_CompileString (code, "<stdin>", Py_single_input);
373
374 if (NULL != src) /* compiled just fine - */
375 {
376 if (ps1 == prompt || /* ">>> " or */
377 '\n' == code[i + j - 1]) /* "... " and double '\n' */
378 { /* so execute it */
379 dum = PyEval_EvalCode ((PyCodeObject *)src, glb, loc);
380 Py_XDECREF (dum);
381 Py_XDECREF (src);
382 free (code);
383 code = NULL;
384 if (PyErr_Occurred ())
385 PyErr_Print ();
386 prompt = ps1;
387 }
388 } /* syntax error or E_EOF? */
389 else if (PyErr_ExceptionMatches (PyExc_SyntaxError))
390 {
391 PyErr_Fetch (&exc, &val, &trb); /* clears exception! */
392
393 if (PyArg_ParseTuple (val, "sO", &msg, &obj) &&
394 !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
395 {
396 Py_XDECREF (exc);
397 Py_XDECREF (val);
398 Py_XDECREF (trb);
399 prompt = ps2;
400 }
401 else /* some other syntax error */
402 {
403 PyErr_Restore (exc, val, trb);
404 PyErr_Print ();
405 free (code);
406 code = NULL;
407 prompt = ps1;
408 }
409 }
410 else /* some non-syntax error */
411 {
412 PyErr_Print ();
413 free (code);
414 code = NULL;
415 prompt = ps1;
416 }
417
418 free (line);
419 }
420 }
421
422 Py_XDECREF(glb);
423 Py_XDECREF(loc);
424 Py_Finalize();
425 exit(0);
426 }
427
428
429How do I find undefined g++ symbols __builtin_new or __pure_virtual?
430--------------------------------------------------------------------
431
432To dynamically load g++ extension modules, you must recompile Python, relink it
Ezio Melotti062d2b52009-12-19 22:41:49 +0000433using g++ (change LINKCC in the Python Modules Makefile), and link your
Georg Brandl6728c5a2009-10-11 18:31:23 +0000434extension module using g++ (e.g., ``g++ -shared -o mymodule.so mymodule.o``).
435
436
437Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?
438----------------------------------------------------------------------------------------------------------------
439
Georg Brandl6f82cd32010-02-06 18:44:44 +0000440In Python 2.2, you can inherit from built-in classes such as :class:`int`,
Georg Brandl6728c5a2009-10-11 18:31:23 +0000441:class:`list`, :class:`dict`, etc.
442
443The Boost Python Library (BPL, http://www.boost.org/libs/python/doc/index.html)
444provides a way of doing this from C++ (i.e. you can inherit from an extension
445class written in C++ using the BPL).
446
447
448When importing module X, why do I get "undefined symbol: PyUnicodeUCS2*"?
449-------------------------------------------------------------------------
450
451You are using a version of Python that uses a 4-byte representation for Unicode
452characters, but some C extension module you are importing was compiled using a
453Python that uses a 2-byte representation for Unicode characters (the default).
454
455If instead the name of the undefined symbol starts with ``PyUnicodeUCS4``, the
456problem is the reverse: Python was built using 2-byte Unicode characters, and
457the extension module was compiled using a Python with 4-byte Unicode characters.
458
459This can easily occur when using pre-built extension packages. RedHat Linux
4607.x, in particular, provided a "python2" binary that is compiled with 4-byte
461Unicode. This only causes the link failure if the extension uses any of the
462``PyUnicode_*()`` functions. It is also a problem if an extension uses any of
463the Unicode-related format specifiers for :cfunc:`Py_BuildValue` (or similar) or
464parameter specifications for :cfunc:`PyArg_ParseTuple`.
465
466You can check the size of the Unicode character a Python interpreter is using by
467checking the value of sys.maxunicode:
468
469 >>> import sys
470 >>> if sys.maxunicode > 65535:
471 ... print 'UCS4 build'
472 ... else:
473 ... print 'UCS2 build'
474
475The only way to solve this problem is to use extension modules compiled with a
476Python binary built using the same size for Unicode characters.
477
478
479