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