| 1 |
Writing extension modules for pypy |
| 2 |
================================== |
| 3 |
|
| 4 |
This document tries to explain how to interface the PyPy python interpreter |
| 5 |
with any external library. |
| 6 |
|
| 7 |
Right now, there are the following possibilities of providing |
| 8 |
third-party modules for the PyPy python interpreter (in order, from most |
| 9 |
directly useful to most messy to use with PyPy): |
| 10 |
|
| 11 |
* Write them in pure Python and use CFFI_. |
| 12 |
|
| 13 |
* Write them in pure Python and use ctypes_. |
| 14 |
|
| 15 |
* Write them in C++ and bind them through cppyy_ using Cling. |
| 16 |
|
| 17 |
* Write them as `RPython mixed modules`_. |
| 18 |
|
| 19 |
|
| 20 |
CFFI |
| 21 |
---- |
| 22 |
|
| 23 |
CFFI__ is the recommended way. It is a way to write pure Python code |
| 24 |
that accesses C libraries. The idea is to support either ABI- or |
| 25 |
API-level access to C --- so that you can sanely access C libraries |
| 26 |
without depending on details like the exact field order in the C |
| 27 |
structures or the numerical value of all the constants. It works on |
| 28 |
both CPython (as a separate ``python -mpip install cffi``) and on PyPy, where it |
| 29 |
is included by default. |
| 30 |
|
| 31 |
PyPy's JIT does a quite reasonable job on the Python code that call C |
| 32 |
functions or manipulate C pointers with CFFI. (As of PyPy 2.2.1, it |
| 33 |
could still be improved, but is already good.) |
| 34 |
|
| 35 |
See the documentation here__. |
| 36 |
|
| 37 |
.. __: https://cffi.readthedocs.org/ |
| 38 |
.. __: https://cffi.readthedocs.org/ |
| 39 |
|
| 40 |
|
| 41 |
CTypes |
| 42 |
------ |
| 43 |
|
| 44 |
The goal of the ctypes module of PyPy is to be as compatible as possible |
| 45 |
with the `CPython ctypes`_ version. It works for large examples, such |
| 46 |
as pyglet. PyPy's implementation is not strictly 100% compatible with |
| 47 |
CPython, but close enough for most cases. |
| 48 |
More (but older) information is available :doc:`here <discussion/ctypes-implementation>`. |
| 49 |
Also, ctypes' performance is not as good as CFFI's. |
| 50 |
|
| 51 |
.. _CPython ctypes: https://docs.python.org/library/ctypes.html |
| 52 |
|
| 53 |
PyPy implements ctypes as pure Python code around two built-in modules |
| 54 |
called ``_rawffi`` and ``_rawffi.alt``, which give a very low-level binding to |
| 55 |
the C library libffi_. Nowadays it is not recommended to use directly |
| 56 |
these two modules. |
| 57 |
|
| 58 |
.. _libffi: https://sourceware.org/libffi/ |
| 59 |
|
| 60 |
|
| 61 |
cppyy |
| 62 |
----- |
| 63 |
|
| 64 |
For C++, _cppyy_ is an automated bindings generator available for both |
| 65 |
PyPy and CPython. |
| 66 |
_cppyy_ relies on declarations from C++ header files to dynamically |
| 67 |
construct Python equivalent classes, functions, variables, etc. |
| 68 |
It is designed for use by large scale programs and supports modern C++. |
| 69 |
With PyPy, it leverages the built-in ``_cppyy`` module, allowing the JIT to |
| 70 |
remove most of the cross-language overhead. |
| 71 |
|
| 72 |
To install, run ``<pypy> -mpip install cppyy``. |
| 73 |
Further details are available in the `full documentation`_. |
| 74 |
|
| 75 |
.. _`full documentation`: https://cppyy.readthedocs.org/ |
| 76 |
|
| 77 |
|
| 78 |
RPython Mixed Modules |
| 79 |
--------------------- |
| 80 |
|
| 81 |
This is the internal way to write built-in extension modules in PyPy. |
| 82 |
It cannot be used by any 3rd-party module: the extension modules are |
| 83 |
*built-in*, not independently loadable DLLs. |
| 84 |
|
| 85 |
This is reserved for special cases: it gives direct access to e.g. the |
| 86 |
details of the JIT, allowing us to tweak its interaction with user code. |
| 87 |
This is how the numpy module is being developed. |
| 88 |
|
| 89 |
|