1 ======================= 2 Extending/Embedding FAQ 3 ======================= 4 5 .. only:: html 6 7 .. contents:: 8 9 .. highlight:: c 10 11 12 .. XXX need review for Python 3. 13 14 15 Can I create my own functions in C? 16 ----------------------------------- 17 18 Yes, you can create built-in modules containing functions, variables, exceptions 19 and even new types in C. This is explained in the document 20 :ref:`extending-index`. 21 22 Most intermediate or advanced Python books will also cover this topic. 23 24 25 Can I create my own functions in C++? 26 ------------------------------------- 27 28 Yes, using the C compatibility features found in C++. Place ``extern "C" { 29 ... }`` around the Python include files and put ``extern "C"`` before each 30 function that is going to be called by the Python interpreter. Global or static 31 C++ objects with constructors are probably not a good idea. 32 33 34 .. _c-wrapper-software: 35 36 Writing C is hard; are there any alternatives? 37 ---------------------------------------------- 38 39 There are a number of alternatives to writing your own C extensions, depending 40 on what you're trying to do. 41 42 .. XXX make sure these all work 43 44 `Cython <http://cython.org>`_ and its relative `Pyrex 45 <https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/>`_ are compilers 46 that accept a slightly modified form of Python and generate the corresponding 47 C code. Cython and Pyrex make it possible to write an extension without having 48 to learn Python's C API. 49 50 If you need to interface to some C or C++ library for which no Python extension 51 currently exists, you can try wrapping the library's data types and functions 52 with a tool such as `SWIG <http://www.swig.org>`_. `SIP 53 <https://riverbankcomputing.com/software/sip/intro>`__, `CXX 54 <http://cxx.sourceforge.net/>`_ `Boost 55 <http://www.boost.org/libs/python/doc/index.html>`_, or `Weave 56 <https://github.com/scipy/weave>`_ are also 57 alternatives for wrapping C++ libraries. 58 59 60 How can I execute arbitrary Python statements from C? 61 ----------------------------------------------------- 62 63 The highest-level function to do this is :c:func:`PyRun_SimpleString` which takes 64 a 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 :exc:`SyntaxError`). If you want more control, use 67 :c:func:`PyRun_String`; see the source for :c:func:`PyRun_SimpleString` in 68 ``Python/pythonrun.c``. 69 70 71 How can I evaluate an arbitrary Python expression from C? 72 --------------------------------------------------------- 73 74 Call the function :c:func:`PyRun_String` from the previous question with the 75 start symbol :c:data:`Py_eval_input`; it parses an expression, evaluates it and 76 returns its value. 77 78 79 How do I extract C values from a Python object? 80 ----------------------------------------------- 81 82 That depends on the object's type. If it's a tuple, :c:func:`PyTuple_Size` 83 returns its length and :c:func:`PyTuple_GetItem` returns the item at a specified 84 index. Lists have similar functions, :c:func:`PyListSize` and 85 :c:func:`PyList_GetItem`. 86 87 For bytes, :c:func:`PyBytes_Size` returns its length and 88 :c:func:`PyBytes_AsStringAndSize` provides a pointer to its value and its 89 length. Note that Python bytes objects may contain null bytes so C's 90 :c:func:`strlen` should not be used. 91 92 To test the type of an object, first make sure it isn't *NULL*, and then use 93 :c:func:`PyBytes_Check`, :c:func:`PyTuple_Check`, :c:func:`PyList_Check`, etc. 94 95 There is also a high-level API to Python objects which is provided by the 96 so-called 'abstract' interface -- read ``Include/abstract.h`` for further 97 details. It allows interfacing with any kind of Python sequence using calls 98 like :c:func:`PySequence_Length`, :c:func:`PySequence_GetItem`, etc. as well 99 as many other useful protocols such as numbers (:c:func:`PyNumber_Index` et 100 al.) and mappings in the PyMapping APIs. 101 102 103 How do I use Py_BuildValue() to create a tuple of arbitrary length? 104 ------------------------------------------------------------------- 105 106 You can't. Use :c:func:`PyTuple_Pack` instead. 107 108 109 How do I call an object's method from C? 110 ---------------------------------------- 111 112 The :c:func:`PyObject_CallMethod` function can be used to call an arbitrary 113 method of an object. The parameters are the object, the name of the method to 114 call, a format string like that used with :c:func:`Py_BuildValue`, and the 115 argument values:: 116 117 PyObject * 118 PyObject_CallMethod(PyObject *object, const char *method_name, 119 const char *arg_format, ...); 120 121 This works for any object that has methods -- whether built-in or user-defined. 122 You are responsible for eventually :c:func:`Py_DECREF`\ 'ing the return value. 123 124 To call, e.g., a file object's "seek" method with arguments 10, 0 (assuming the 125 file object pointer is "f"):: 126 127 res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0); 128 if (res == NULL) { 129 ... an exception occurred ... 130 } 131 else { 132 Py_DECREF(res); 133 } 134 135 Note that since :c:func:`PyObject_CallObject` *always* wants a tuple for the 136 argument list, to call a function without arguments, pass "()" for the format, 137 and to call a function with one argument, surround the argument in parentheses, 138 e.g. "(i)". 139 140 141 How do I catch the output from PyErr_Print() (or anything that prints to stdout/stderr)? 142 ---------------------------------------------------------------------------------------- 143 144 In Python code, define an object that supports the ``write()`` method. Assign 145 this object to :data:`sys.stdout` and :data:`sys.stderr`. Call print_error, or 146 just allow the standard traceback mechanism to work. Then, the output will go 147 wherever your ``write()`` method sends it. 148 149 The easiest way to do this is to use the :class:`io.StringIO` class: 150 151 .. code-block:: pycon 152 153 >>> import io, sys 154 >>> sys.stdout = io.StringIO() 155 >>> print('foo') 156 >>> print('hello world!') 157 >>> sys.stderr.write(sys.stdout.getvalue()) 158 foo 159 hello world! 160 161 A custom object to do the same would look like this: 162 163 .. code-block:: pycon 164 165 >>> import io, sys 166 >>> class StdoutCatcher(io.TextIOBase): 167 ... def __init__(self): 168 ... self.data = [] 169 ... def write(self, stuff): 170 ... self.data.append(stuff) 171 ... 172 >>> import sys 173 >>> sys.stdout = StdoutCatcher() 174 >>> print('foo') 175 >>> print('hello world!') 176 >>> sys.stderr.write(''.join(sys.stdout.data)) 177 foo 178 hello world! 179 180 181 How do I access a module written in Python from C? 182 -------------------------------------------------- 183 184 You can get a pointer to the module object as follows:: 185 186 module = PyImport_ImportModule("<modulename>"); 187 188 If the module hasn't been imported yet (i.e. it is not yet present in 189 :data:`sys.modules`), this initializes the module; otherwise it simply returns 190 the value of ``sys.modules["<modulename>"]``. Note that it doesn't enter the 191 module into any namespace -- it only ensures it has been initialized and is 192 stored in :data:`sys.modules`. 193 194 You can then access the module's attributes (i.e. any name defined in the 195 module) as follows:: 196 197 attr = PyObject_GetAttrString(module, "<attrname>"); 198 199 Calling :c:func:`PyObject_SetAttrString` to assign to variables in the module 200 also works. 201 202 203 How do I interface to C++ objects from Python? 204 ---------------------------------------------- 205 206 Depending on your requirements, there are many approaches. To do this manually, 207 begin by reading :ref:`the "Extending and Embedding" document 208 <extending-index>`. Realize that for the Python run-time system, there isn't a 209 whole lot of difference between C and C++ -- so the strategy of building a new 210 Python type around a C structure (pointer) type will also work for C++ objects. 211 212 For C++ libraries, see :ref:`c-wrapper-software`. 213 214 215 I added a module using the Setup file and the make fails; why? 216 -------------------------------------------------------------- 217 218 Setup must end in a newline, if there is no newline there, the build process 219 fails. (Fixing this requires some ugly shell script hackery, and this bug is so 220 minor that it doesn't seem worth the effort.) 221 222 223 How do I debug an extension? 224 ---------------------------- 225 226 When using GDB with dynamically loaded extensions, you can't set a breakpoint in 227 your extension until your extension is loaded. 228 229 In your ``.gdbinit`` file (or interactively), add the command: 230 231 .. code-block:: none 232 233 br _PyImport_LoadDynamicModule 234 235 Then, when you run GDB: 236 237 .. code-block:: shell-session 238 239 $ gdb /local/bin/python 240 gdb) run myscript.py 241 gdb) continue # repeat until your extension is loaded 242 gdb) finish # so that your extension is loaded 243 gdb) br myfunction.c:50 244 gdb) continue 245 246 I want to compile a Python module on my Linux system, but some files are missing. Why? 247 -------------------------------------------------------------------------------------- 248 249 Most packaged versions of Python don't include the 250 :file:`/usr/lib/python2.{x}/config/` directory, which contains various files 251 required for compiling Python extensions. 252 253 For Red Hat, install the python-devel RPM to get the necessary files. 254 255 For Debian, run ``apt-get install python-dev``. 256 257 258 How do I tell "incomplete input" from "invalid input"? 259 ------------------------------------------------------ 260 261 Sometimes you want to emulate the Python interactive interpreter's behavior, 262 where it gives you a continuation prompt when the input is incomplete (e.g. you 263 typed the start of an "if" statement or you didn't close your parentheses or 264 triple string quotes), but it gives you a syntax error message immediately when 265 the input is invalid. 266 267 In Python you can use the :mod:`codeop` module, which approximates the parser's 268 behavior sufficiently. IDLE uses this, for example. 269 270 The easiest way to do it in C is to call :c:func:`PyRun_InteractiveLoop` (perhaps 271 in a separate thread) and let the Python interpreter handle the input for 272 you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at your 273 custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c`` 274 for more hints. 275 276 However sometimes you have to run the embedded Python interpreter in the same 277 thread as your rest application and you can't allow the 278 :c:func:`PyRun_InteractiveLoop` to stop while waiting for user input. The one 279 solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error`` 280 equal to ``E_EOF``, which means the input is incomplete. Here's a sample code 281 fragment, 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 309 Another solution is trying to compile the received string with 310 :c:func:`Py_CompileString`. If it compiles without errors, try to execute the 311 returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the 312 input for later. If the compilation fails, find out if it's an error or just 313 more input is required - by extracting the message string from the exception 314 tuple and comparing it to the string "unexpected EOF while parsing". Here is a 315 complete 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 (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 429 How do I find undefined g++ symbols __builtin_new or __pure_virtual? 430 -------------------------------------------------------------------- 431 432 To dynamically load g++ extension modules, you must recompile Python, relink it 433 using g++ (change LINKCC in the Python Modules Makefile), and link your 434 extension module using g++ (e.g., ``g++ -shared -o mymodule.so mymodule.o``). 435 436 437 Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? 438 ---------------------------------------------------------------------------------------------------------------- 439 440 Yes, you can inherit from built-in classes such as :class:`int`, :class:`list`, 441 :class:`dict`, etc. 442 443 The Boost Python Library (BPL, http://www.boost.org/libs/python/doc/index.html) 444 provides a way of doing this from C++ (i.e. you can inherit from an extension 445 class written in C++ using the BPL). 446