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