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