Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 1 | ======================= |
| 2 | Extending/Embedding FAQ |
| 3 | ======================= |
| 4 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 5 | .. only:: html |
| 6 | |
| 7 | .. contents:: |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 8 | |
| 9 | .. highlight:: c |
| 10 | |
| 11 | |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 12 | .. XXX need review for Python 3. |
| 13 | |
| 14 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 4abda54 | 2010-07-12 09:00:29 +0000 | [diff] [blame] | 34 | .. _c-wrapper-software: |
| 35 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | 9cb41df | 2011-12-03 21:21:36 +0100 | [diff] [blame] | 42 | .. XXX make sure these all work |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 43 | |
Antoine Pitrou | 09264b6 | 2011-02-05 10:57:17 +0000 | [diff] [blame] | 44 | `Cython <http://cython.org>`_ and its relative `Pyrex |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame^] | 45 | <https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/>`_ are compilers |
Antoine Pitrou | 09264b6 | 2011-02-05 10:57:17 +0000 | [diff] [blame] | 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. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 53 | <https://riverbankcomputing.com/software/sip/intro>`__, `CXX |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 54 | <http://cxx.sourceforge.net/>`_ `Boost |
| 55 | <http://www.boost.org/libs/python/doc/index.html>`_, or `Weave |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 56 | <https://scipy.github.io/devdocs/tutorial/weave.html>`_ are also |
Georg Brandl | 77fe77d | 2014-10-29 09:24:54 +0100 | [diff] [blame] | 57 | alternatives for wrapping C++ libraries. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 58 | |
| 59 | |
| 60 | How can I execute arbitrary Python statements from C? |
| 61 | ----------------------------------------------------- |
| 62 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 63 | The highest-level function to do this is :c:func:`PyRun_SimpleString` which takes |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 ``SyntaxError``). If you want more control, use |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 67 | :c:func:`PyRun_String`; see the source for :c:func:`PyRun_SimpleString` in |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 68 | ``Python/pythonrun.c``. |
| 69 | |
| 70 | |
| 71 | How can I evaluate an arbitrary Python expression from C? |
| 72 | --------------------------------------------------------- |
| 73 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 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 |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 76 | returns its value. |
| 77 | |
| 78 | |
| 79 | How do I extract C values from a Python object? |
| 80 | ----------------------------------------------- |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 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`. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 86 | |
Gregory P. Smith | 4b52ae8 | 2013-03-22 13:43:30 -0700 | [diff] [blame] | 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. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 91 | |
| 92 | To test the type of an object, first make sure it isn't *NULL*, and then use |
Gregory P. Smith | 4b52ae8 | 2013-03-22 13:43:30 -0700 | [diff] [blame] | 93 | :c:func:`PyBytes_Check`, :c:func:`PyTuple_Check`, :c:func:`PyList_Check`, etc. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 |
Zachary Ware | 2f31b4b | 2014-03-20 10:16:09 -0500 | [diff] [blame] | 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 |
Gregory P. Smith | 4b52ae8 | 2013-03-22 13:43:30 -0700 | [diff] [blame] | 100 | al.) and mappings in the PyMapping APIs. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 101 | |
| 102 | |
| 103 | How do I use Py_BuildValue() to create a tuple of arbitrary length? |
| 104 | ------------------------------------------------------------------- |
| 105 | |
Antoine Pitrou | 48383bf | 2011-12-03 22:30:19 +0100 | [diff] [blame] | 106 | You can't. Use :c:func:`PyTuple_Pack` instead. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 107 | |
| 108 | |
| 109 | How do I call an object's method from C? |
| 110 | ---------------------------------------- |
| 111 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 112 | The :c:func:`PyObject_CallMethod` function can be used to call an arbitrary |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 113 | method of an object. The parameters are the object, the name of the method to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | call, a format string like that used with :c:func:`Py_BuildValue`, and the |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 115 | argument values:: |
| 116 | |
| 117 | PyObject * |
Serhiy Storchaka | 03863d2 | 2015-06-21 17:11:21 +0300 | [diff] [blame] | 118 | PyObject_CallMethod(PyObject *object, const char *method_name, |
| 119 | const char *arg_format, ...); |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 120 | |
| 121 | This works for any object that has methods -- whether built-in or user-defined. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 122 | You are responsible for eventually :c:func:`Py_DECREF`\ 'ing the return value. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | Note that since :c:func:`PyObject_CallObject` *always* wants a tuple for the |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 | |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 149 | The easiest way to do this is to use the :class:`io.StringIO` class:: |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 150 | |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 151 | >>> import io, sys |
| 152 | >>> sys.stdout = io.StringIO() |
| 153 | >>> print('foo') |
| 154 | >>> print('hello world!') |
| 155 | >>> sys.stderr.write(sys.stdout.getvalue()) |
| 156 | foo |
| 157 | hello world! |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 159 | A custom object to do the same would look like this:: |
| 160 | |
| 161 | >>> import io, sys |
| 162 | >>> class StdoutCatcher(io.TextIOBase): |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 163 | ... def __init__(self): |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 164 | ... self.data = [] |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 165 | ... def write(self, stuff): |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 166 | ... self.data.append(stuff) |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 167 | ... |
| 168 | >>> import sys |
| 169 | >>> sys.stdout = StdoutCatcher() |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 170 | >>> print('foo') |
| 171 | >>> print('hello world!') |
Antoine Pitrou | d4ddec5 | 2011-12-03 22:35:31 +0100 | [diff] [blame] | 172 | >>> sys.stderr.write(''.join(sys.stdout.data)) |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 173 | foo |
| 174 | hello world! |
| 175 | |
| 176 | |
| 177 | How do I access a module written in Python from C? |
| 178 | -------------------------------------------------- |
| 179 | |
| 180 | You can get a pointer to the module object as follows:: |
| 181 | |
| 182 | module = PyImport_ImportModule("<modulename>"); |
| 183 | |
| 184 | If the module hasn't been imported yet (i.e. it is not yet present in |
| 185 | :data:`sys.modules`), this initializes the module; otherwise it simply returns |
| 186 | the value of ``sys.modules["<modulename>"]``. Note that it doesn't enter the |
| 187 | module into any namespace -- it only ensures it has been initialized and is |
| 188 | stored in :data:`sys.modules`. |
| 189 | |
| 190 | You can then access the module's attributes (i.e. any name defined in the |
| 191 | module) as follows:: |
| 192 | |
| 193 | attr = PyObject_GetAttrString(module, "<attrname>"); |
| 194 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 195 | Calling :c:func:`PyObject_SetAttrString` to assign to variables in the module |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 196 | also works. |
| 197 | |
| 198 | |
| 199 | How do I interface to C++ objects from Python? |
| 200 | ---------------------------------------------- |
| 201 | |
| 202 | Depending on your requirements, there are many approaches. To do this manually, |
| 203 | begin by reading :ref:`the "Extending and Embedding" document |
| 204 | <extending-index>`. Realize that for the Python run-time system, there isn't a |
| 205 | whole lot of difference between C and C++ -- so the strategy of building a new |
| 206 | Python type around a C structure (pointer) type will also work for C++ objects. |
| 207 | |
Georg Brandl | 4abda54 | 2010-07-12 09:00:29 +0000 | [diff] [blame] | 208 | For C++ libraries, see :ref:`c-wrapper-software`. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 209 | |
| 210 | |
| 211 | I added a module using the Setup file and the make fails; why? |
| 212 | -------------------------------------------------------------- |
| 213 | |
| 214 | Setup must end in a newline, if there is no newline there, the build process |
| 215 | fails. (Fixing this requires some ugly shell script hackery, and this bug is so |
| 216 | minor that it doesn't seem worth the effort.) |
| 217 | |
| 218 | |
| 219 | How do I debug an extension? |
| 220 | ---------------------------- |
| 221 | |
| 222 | When using GDB with dynamically loaded extensions, you can't set a breakpoint in |
| 223 | your extension until your extension is loaded. |
| 224 | |
| 225 | In your ``.gdbinit`` file (or interactively), add the command:: |
| 226 | |
| 227 | br _PyImport_LoadDynamicModule |
| 228 | |
| 229 | Then, 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 | |
| 238 | I want to compile a Python module on my Linux system, but some files are missing. Why? |
| 239 | -------------------------------------------------------------------------------------- |
| 240 | |
| 241 | Most packaged versions of Python don't include the |
| 242 | :file:`/usr/lib/python2.{x}/config/` directory, which contains various files |
| 243 | required for compiling Python extensions. |
| 244 | |
| 245 | For Red Hat, install the python-devel RPM to get the necessary files. |
| 246 | |
| 247 | For Debian, run ``apt-get install python-dev``. |
| 248 | |
| 249 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 250 | How do I tell "incomplete input" from "invalid input"? |
| 251 | ------------------------------------------------------ |
| 252 | |
| 253 | Sometimes you want to emulate the Python interactive interpreter's behavior, |
| 254 | where it gives you a continuation prompt when the input is incomplete (e.g. you |
| 255 | typed the start of an "if" statement or you didn't close your parentheses or |
| 256 | triple string quotes), but it gives you a syntax error message immediately when |
| 257 | the input is invalid. |
| 258 | |
| 259 | In Python you can use the :mod:`codeop` module, which approximates the parser's |
| 260 | behavior sufficiently. IDLE uses this, for example. |
| 261 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 262 | The easiest way to do it in C is to call :c:func:`PyRun_InteractiveLoop` (perhaps |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 263 | in a separate thread) and let the Python interpreter handle the input for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 264 | you. You can also set the :c:func:`PyOS_ReadlineFunctionPointer` to point at your |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 265 | custom input function. See ``Modules/readline.c`` and ``Parser/myreadline.c`` |
| 266 | for more hints. |
| 267 | |
| 268 | However sometimes you have to run the embedded Python interpreter in the same |
| 269 | thread as your rest application and you can't allow the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 270 | :c:func:`PyRun_InteractiveLoop` to stop while waiting for user input. The one |
| 271 | solution then is to call :c:func:`PyParser_ParseString` and test for ``e.error`` |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 272 | equal to ``E_EOF``, which means the input is incomplete). Here's a sample code |
| 273 | fragment, untested, inspired by code from Alex Farber:: |
| 274 | |
| 275 | #include <Python.h> |
| 276 | #include <node.h> |
| 277 | #include <errcode.h> |
| 278 | #include <grammar.h> |
| 279 | #include <parsetok.h> |
| 280 | #include <compile.h> |
| 281 | |
| 282 | int testcomplete(char *code) |
| 283 | /* code should end in \n */ |
| 284 | /* return -1 for error, 0 for incomplete, 1 for complete */ |
| 285 | { |
| 286 | node *n; |
| 287 | perrdetail e; |
| 288 | |
| 289 | n = PyParser_ParseString(code, &_PyParser_Grammar, |
| 290 | Py_file_input, &e); |
| 291 | if (n == NULL) { |
| 292 | if (e.error == E_EOF) |
| 293 | return 0; |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | PyNode_Free(n); |
| 298 | return 1; |
| 299 | } |
| 300 | |
| 301 | Another solution is trying to compile the received string with |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 302 | :c:func:`Py_CompileString`. If it compiles without errors, try to execute the |
| 303 | returned code object by calling :c:func:`PyEval_EvalCode`. Otherwise save the |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 304 | input for later. If the compilation fails, find out if it's an error or just |
| 305 | more input is required - by extracting the message string from the exception |
| 306 | tuple and comparing it to the string "unexpected EOF while parsing". Here is a |
| 307 | complete example using the GNU readline library (you may want to ignore |
| 308 | **SIGINT** while calling readline()):: |
| 309 | |
| 310 | #include <stdio.h> |
| 311 | #include <readline.h> |
| 312 | |
| 313 | #include <Python.h> |
| 314 | #include <object.h> |
| 315 | #include <compile.h> |
| 316 | #include <eval.h> |
| 317 | |
| 318 | int main (int argc, char* argv[]) |
| 319 | { |
| 320 | int i, j, done = 0; /* lengths of line, code */ |
| 321 | char ps1[] = ">>> "; |
| 322 | char ps2[] = "... "; |
| 323 | char *prompt = ps1; |
| 324 | char *msg, *line, *code = NULL; |
| 325 | PyObject *src, *glb, *loc; |
| 326 | PyObject *exc, *val, *trb, *obj, *dum; |
| 327 | |
| 328 | Py_Initialize (); |
| 329 | loc = PyDict_New (); |
| 330 | glb = PyDict_New (); |
| 331 | PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ()); |
| 332 | |
| 333 | while (!done) |
| 334 | { |
| 335 | line = readline (prompt); |
| 336 | |
Serhiy Storchaka | 0424eaf | 2015-09-12 17:45:25 +0300 | [diff] [blame] | 337 | if (NULL == line) /* Ctrl-D pressed */ |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 338 | { |
| 339 | done = 1; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | i = strlen (line); |
| 344 | |
| 345 | if (i > 0) |
| 346 | add_history (line); /* save non-empty lines */ |
| 347 | |
| 348 | if (NULL == code) /* nothing in code yet */ |
| 349 | j = 0; |
| 350 | else |
| 351 | j = strlen (code); |
| 352 | |
| 353 | code = realloc (code, i + j + 2); |
| 354 | if (NULL == code) /* out of memory */ |
| 355 | exit (1); |
| 356 | |
| 357 | if (0 == j) /* code was empty, so */ |
| 358 | code[0] = '\0'; /* keep strncat happy */ |
| 359 | |
| 360 | strncat (code, line, i); /* append line to code */ |
| 361 | code[i + j] = '\n'; /* append '\n' to code */ |
| 362 | code[i + j + 1] = '\0'; |
| 363 | |
| 364 | src = Py_CompileString (code, "<stdin>", Py_single_input); |
| 365 | |
| 366 | if (NULL != src) /* compiled just fine - */ |
| 367 | { |
| 368 | if (ps1 == prompt || /* ">>> " or */ |
| 369 | '\n' == code[i + j - 1]) /* "... " and double '\n' */ |
| 370 | { /* so execute it */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 371 | dum = PyEval_EvalCode (src, glb, loc); |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 372 | Py_XDECREF (dum); |
| 373 | Py_XDECREF (src); |
| 374 | free (code); |
| 375 | code = NULL; |
| 376 | if (PyErr_Occurred ()) |
| 377 | PyErr_Print (); |
| 378 | prompt = ps1; |
| 379 | } |
| 380 | } /* syntax error or E_EOF? */ |
| 381 | else if (PyErr_ExceptionMatches (PyExc_SyntaxError)) |
| 382 | { |
| 383 | PyErr_Fetch (&exc, &val, &trb); /* clears exception! */ |
| 384 | |
| 385 | if (PyArg_ParseTuple (val, "sO", &msg, &obj) && |
| 386 | !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ |
| 387 | { |
| 388 | Py_XDECREF (exc); |
| 389 | Py_XDECREF (val); |
| 390 | Py_XDECREF (trb); |
| 391 | prompt = ps2; |
| 392 | } |
| 393 | else /* some other syntax error */ |
| 394 | { |
| 395 | PyErr_Restore (exc, val, trb); |
| 396 | PyErr_Print (); |
| 397 | free (code); |
| 398 | code = NULL; |
| 399 | prompt = ps1; |
| 400 | } |
| 401 | } |
| 402 | else /* some non-syntax error */ |
| 403 | { |
| 404 | PyErr_Print (); |
| 405 | free (code); |
| 406 | code = NULL; |
| 407 | prompt = ps1; |
| 408 | } |
| 409 | |
| 410 | free (line); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | Py_XDECREF(glb); |
| 415 | Py_XDECREF(loc); |
| 416 | Py_Finalize(); |
| 417 | exit(0); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | How do I find undefined g++ symbols __builtin_new or __pure_virtual? |
| 422 | -------------------------------------------------------------------- |
| 423 | |
| 424 | To dynamically load g++ extension modules, you must recompile Python, relink it |
Ezio Melotti | 0639d5a | 2009-12-19 23:26:38 +0000 | [diff] [blame] | 425 | using g++ (change LINKCC in the Python Modules Makefile), and link your |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 426 | extension module using g++ (e.g., ``g++ -shared -o mymodule.so mymodule.o``). |
| 427 | |
| 428 | |
| 429 | Can I create an object class with some methods implemented in C and others in Python (e.g. through inheritance)? |
| 430 | ---------------------------------------------------------------------------------------------------------------- |
| 431 | |
Benjamin Peterson | 0071b3d | 2015-10-10 23:23:55 -0700 | [diff] [blame] | 432 | Yes, you can inherit from built-in classes such as :class:`int`, :class:`list`, |
| 433 | :class:`dict`, etc. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 434 | |
| 435 | The Boost Python Library (BPL, http://www.boost.org/libs/python/doc/index.html) |
| 436 | provides a way of doing this from C++ (i.e. you can inherit from an extension |
| 437 | class written in C++ using the BPL). |