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