Home | History | Annotate | Download | only in faq
      1 =======================
      2 Extending/Embedding FAQ
      3 =======================
      4 
      5 .. only:: html
      6 
      7    .. contents::
      8 
      9 .. highlight:: c
     10 
     11 
     12 Can I create my own functions in C?
     13 -----------------------------------
     14 
     15 Yes, you can create built-in modules containing functions, variables, exceptions
     16 and even new types in C.  This is explained in the document
     17 :ref:`extending-index`.
     18 
     19 Most intermediate or advanced Python books will also cover this topic.
     20 
     21 
     22 Can I create my own functions in C++?
     23 -------------------------------------
     24 
     25 Yes, using the C compatibility features found in C++.  Place ``extern "C" {
     26 ... }`` around the Python include files and put ``extern "C"`` before each
     27 function that is going to be called by the Python interpreter.  Global or static
     28 C++ objects with constructors are probably not a good idea.
     29 
     30 
     31 .. _c-wrapper-software:
     32 
     33 Writing C is hard; are there any alternatives?
     34 ----------------------------------------------
     35 
     36 There are a number of alternatives to writing your own C extensions, depending
     37 on what you're trying to do.
     38 
     39 .. XXX make sure these all work; mention Cython
     40 
     41 If you need more speed, `Psyco <http://psyco.sourceforge.net/>`_ generates x86
     42 assembly code from Python bytecode.  You can use Psyco to compile the most
     43 time-critical functions in your code, and gain a significant improvement with
     44 very little effort, as long as you're running on a machine with an
     45 x86-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
     49 that accept a slightly modified form of Python and generate the corresponding
     50 C code.  Pyrex makes it possible to write an extension without having to learn
     51 Python's C API.
     52 
     53 If you need to interface to some C or C++ library for which no Python extension
     54 currently exists, you can try wrapping the library's data types and functions
     55 with 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
     60 alternatives for wrapping C++ libraries.
     61 
     62 
     63 How can I execute arbitrary Python statements from C?
     64 -----------------------------------------------------
     65 
     66 The highest-level function to do this is :c:func:`PyRun_SimpleString` which takes
     67 a 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 
     74 How can I evaluate an arbitrary Python expression from C?
     75 ---------------------------------------------------------
     76 
     77 Call the function :c:func:`PyRun_String` from the previous question with the
     78 start symbol :c:data:`Py_eval_input`; it parses an expression, evaluates it and
     79 returns its value.
     80 
     81 
     82 How do I extract C values from a Python object?
     83 -----------------------------------------------
     84 
     85 That depends on the object's type.  If it's a tuple, :c:func:`PyTuple_Size`
     86 returns its length and :c:func:`PyTuple_GetItem` returns the item at a specified
     87 index.  Lists have similar functions, :c:func:`PyListSize` and
     88 :c:func:`PyList_GetItem`.
     89 
     90 For strings, :c:func:`PyString_Size` returns its length and
     91 :c:func:`PyString_AsString` a pointer to its value.  Note that Python strings may
     92 contain null bytes so C's :c:func:`strlen` should not be used.
     93 
     94 To 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 
     97 There is also a high-level API to Python objects which is provided by the
     98 so-called 'abstract' interface -- read ``Include/abstract.h`` for further
     99 details.  It allows interfacing with any kind of Python sequence using calls
    100 like :c:func:`PySequence_Length`, :c:func:`PySequence_GetItem`, etc.)  as well as
    101 many other useful protocols.
    102 
    103 
    104 How do I use Py_BuildValue() to create a tuple of arbitrary length?
    105 -------------------------------------------------------------------
    106 
    107 You 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
    111 the 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 
    115 How do I call an object's method from C?
    116 ----------------------------------------
    117 
    118 The :c:func:`PyObject_CallMethod` function can be used to call an arbitrary
    119 method of an object.  The parameters are the object, the name of the method to
    120 call, a format string like that used with :c:func:`Py_BuildValue`, and the
    121 argument values::
    122 
    123    PyObject *
    124    PyObject_CallMethod(PyObject *object, char *method_name,
    125                        char *arg_format, ...);
    126 
    127 This works for any object that has methods -- whether built-in or user-defined.
    128 You are responsible for eventually :c:func:`Py_DECREF`\ 'ing the return value.
    129 
    130 To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the
    131 file 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 
    141 Note that since :c:func:`PyObject_CallObject` *always* wants a tuple for the
    142 argument list, to call a function without arguments, pass "()" for the format,
    143 and to call a function with one argument, surround the argument in parentheses,
    144 e.g. "(i)".
    145 
    146 
    147 How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)?
    148 ----------------------------------------------------------------------------------------
    149 
    150 In Python code, define an object that supports the ``write()`` method.  Assign
    151 this object to :data:`sys.stdout` and :data:`sys.stderr`.  Call print_error, or
    152 just allow the standard traceback mechanism to work. Then, the output will go
    153 wherever your ``write()`` method sends it.
    154 
    155 The easiest way to do this is to use the StringIO class in the standard library.
    156 
    157 Sample 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 
    176 How do I access a module written in Python from C?
    177 --------------------------------------------------
    178 
    179 You can get a pointer to the module object as follows::
    180 
    181    module = PyImport_ImportModule("<modulename>");
    182 
    183 If 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
    185 the value of ``sys.modules["<modulename>"]``.  Note that it doesn't enter the
    186 module into any namespace -- it only ensures it has been initialized and is
    187 stored in :data:`sys.modules`.
    188 
    189 You can then access the module's attributes (i.e. any name defined in the
    190 module) as follows::
    191 
    192    attr = PyObject_GetAttrString(module, "<attrname>");
    193 
    194 Calling :c:func:`PyObject_SetAttrString` to assign to variables in the module
    195 also works.
    196 
    197 
    198 How do I interface to C++ objects from Python?
    199 ----------------------------------------------
    200 
    201 Depending on your requirements, there are many approaches.  To do this manually,
    202 begin by reading :ref:`the "Extending and Embedding" document
    203 <extending-index>`.  Realize that for the Python run-time system, there isn't a
    204 whole lot of difference between C and C++ -- so the strategy of building a new
    205 Python type around a C structure (pointer) type will also work for C++ objects.
    206 
    207 For C++ libraries, see :ref:`c-wrapper-software`.
    208 
    209 
    210 I added a module using the Setup file and the make fails; why?
    211 --------------------------------------------------------------
    212 
    213 Setup must end in a newline, if there is no newline there, the build process
    214 fails.  (Fixing this requires some ugly shell script hackery, and this bug is so
    215 minor that it doesn't seem worth the effort.)
    216 
    217 
    218 How do I debug an extension?
    219 ----------------------------
    220 
    221 When using GDB with dynamically loaded extensions, you can't set a breakpoint in
    222 your extension until your extension is loaded.
    223 
    224 In your ``.gdbinit`` file (or interactively), add the command:
    225 
    226 .. code-block:: none
    227 
    228    br _PyImport_LoadDynamicModule
    229 
    230 Then, 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 
    241 I want to compile a Python module on my Linux system, but some files are missing. Why?
    242 --------------------------------------------------------------------------------------
    243 
    244 Most packaged versions of Python don't include the
    245 :file:`/usr/lib/python2.{x}/config/` directory, which contains various files
    246 required for compiling Python extensions.
    247 
    248 For Red Hat, install the python-devel RPM to get the necessary files.
    249 
    250 For Debian, run ``apt-get install python-dev``.
    251 
    252 
    253 What does "SystemError: _PyImport_FixupExtension: module yourmodule not loaded" mean?
    254 -------------------------------------------------------------------------------------
    255 
    256 This means that you have created an extension module named "yourmodule", but
    257 your module init function does not initialize with that name.
    258 
    259 Every module init function will have a line similar to::
    260 
    261    module = Py_InitModule("yourmodule", yourmodule_functions);
    262 
    263 If the string passed to this function is not the same name as your extension
    264 module, the :exc:`SystemError` exception will be raised.
    265 
    266 
    267 How do I tell "incomplete input" from "invalid input"?
    268 ------------------------------------------------------
    269 
    270 Sometimes you want to emulate the Python interactive interpreter's behavior,
    271 where it gives you a continuation prompt when the input is incomplete (e.g. you
    272 typed the start of an "if" statement or you didn't close your parentheses or
    273 triple string quotes), but it gives you a syntax error message immediately when
    274 the input is invalid.
    275 
    276 In Python you can use the :mod:`codeop` module, which approximates the parser's
    277 behavior sufficiently.  IDLE uses this, for example.
    278 
    279 The easiest way to do it in C is to call :c:func:`PyRun_InteractiveLoop` (perhaps
    280 in a separate thread) and let the Python interpreter handle the input for
    281 you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at your
    282 custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c``
    283 for more hints.
    284 
    285 However sometimes you have to run the embedded Python interpreter in the same
    286 thread as your rest application and you can't allow the
    287 :c:func:`PyRun_InteractiveLoop` to stop while waiting for user input.  The one
    288 solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error``
    289 equal to ``E_EOF``, which means the input is incomplete).  Here's a sample code
    290 fragment, 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 
    318 Another solution is trying to compile the received string with
    319 :c:func:`Py_CompileString`. If it compiles without errors, try to execute the
    320 returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the
    321 input for later. If the compilation fails, find out if it's an error or just
    322 more input is required - by extracting the message string from the exception
    323 tuple and comparing it to the string "unexpected EOF while parsing".  Here is a
    324 complete 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 
    438 How do I find undefined g++ symbols __builtin_new or __pure_virtual?
    439 --------------------------------------------------------------------
    440 
    441 To dynamically load g++ extension modules, you must recompile Python, relink it
    442 using g++ (change LINKCC in the Python Modules Makefile), and link your
    443 extension module using g++ (e.g., ``g++ -shared -o mymodule.so mymodule.o``).
    444 
    445 
    446 Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)?
    447 ----------------------------------------------------------------------------------------------------------------
    448 
    449 Yes, you can inherit from built-in classes such as :class:`int`, :class:`list`,
    450 :class:`dict`, etc.
    451 
    452 The Boost Python Library (BPL, http://www.boost.org/libs/python/doc/index.html)
    453 provides a way of doing this from C++ (i.e. you can inherit from an extension
    454 class written in C++ using the BPL).
    455 
    456 
    457 When importing module X, why do I get "undefined symbol: PyUnicodeUCS2*"?
    458 -------------------------------------------------------------------------
    459 
    460 You are using a version of Python that uses a 4-byte representation for Unicode
    461 characters, but some C extension module you are importing was compiled using a
    462 Python that uses a 2-byte representation for Unicode characters (the default).
    463 
    464 If instead the name of the undefined symbol starts with ``PyUnicodeUCS4``, the
    465 problem is the reverse: Python was built using 2-byte Unicode characters, and
    466 the extension module was compiled using a Python with 4-byte Unicode characters.
    467 
    468 This can easily occur when using pre-built extension packages.  RedHat Linux
    469 7.x, in particular, provided a "python2" binary that is compiled with 4-byte
    470 Unicode.  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
    472 the Unicode-related format specifiers for :c:func:`Py_BuildValue` (or similar) or
    473 parameter specifications for :c:func:`PyArg_ParseTuple`.
    474 
    475 You can check the size of the Unicode character a Python interpreter is using by
    476 checking 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 
    486 The only way to solve this problem is to use extension modules compiled with a
    487 Python binary built using the same size for Unicode characters.
    488 
    489 
    490 
    491