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