| 1 |
PyPy's ctypes implementation |
| 2 |
============================ |
| 3 |
|
| 4 |
Summary |
| 5 |
-------- |
| 6 |
|
| 7 |
Terminology: |
| 8 |
|
| 9 |
* application level code - code written in full Python |
| 10 |
|
| 11 |
* interpreter level code - code written in RPython, compiled |
| 12 |
to something else, say C, part of the interpreter. |
| 13 |
|
| 14 |
PyPy's ctypes implementation in its current state proves the |
| 15 |
feasibility of implementing a module with the same interface and |
| 16 |
behavior for PyPy as ctypes for CPython. |
| 17 |
|
| 18 |
PyPy's implementation internally uses `libffi`_ like CPython's ctypes. |
| 19 |
In our implementation as much as possible of the code is written in |
| 20 |
full Python, not RPython. In CPython's situation, the equivalent would |
| 21 |
be to write as little as possible code in C. We essentially favored |
| 22 |
rapid experimentation over worrying about speed for this first trial |
| 23 |
implementation. This allowed to provide a working implementation with |
| 24 |
a large part of ctypes features in 2 months real time. |
| 25 |
|
| 26 |
We reused the ``ctypes`` package version 1.0.2 as-is from CPython. We |
| 27 |
implemented ``_ctypes`` which is a C module in CPython mostly in pure |
| 28 |
Python based on a lower-level layer extension module ``_rawffi``. |
| 29 |
|
| 30 |
.. _libffi: http://sources.redhat.com/libffi/ |
| 31 |
|
| 32 |
|
| 33 |
Low-level part: ``_rawffi`` |
| 34 |
--------------------------- |
| 35 |
|
| 36 |
This PyPy extension module (:source:`pypy/module/_rawffi`) exposes a simple interface |
| 37 |
to create C objects (arrays and structures) and calling functions |
| 38 |
in dynamic libraries through libffi. Freeing objects in most cases and making |
| 39 |
sure that objects referring to each other are kept alive is responsibility of the higher levels. |
| 40 |
|
| 41 |
This module uses bindings to libffi which are defined in :source:`rpython/rlib/libffi.py`. |
| 42 |
|
| 43 |
We tried to keep this module as small as possible. It is conceivable |
| 44 |
that other implementations (e.g. Jython) could use our ctypes |
| 45 |
implementation by writing their version of ``_rawffi``. |
| 46 |
|
| 47 |
|
| 48 |
High-level parts |
| 49 |
----------------- |
| 50 |
|
| 51 |
The reused ``ctypes`` package lives in ``lib_pypy/ctypes``. ``_ctypes`` |
| 52 |
implementing the same interface as ``_ctypes`` in CPython is in |
| 53 |
``lib_pypy/_ctypes``. |
| 54 |
|
| 55 |
|
| 56 |
Discussion and limitations |
| 57 |
----------------------------- |
| 58 |
|
| 59 |
Reimplementing ctypes features was in general possible. PyPy supports |
| 60 |
pluggable garbage collectors, some of them are moving collectors, this |
| 61 |
means that the strategy of passing direct references inside Python |
| 62 |
objects to an external library is not feasible (unless the GCs |
| 63 |
support pinning, which is not the case right now). The consequence of |
| 64 |
this is that sometimes copying instead of sharing is required, this |
| 65 |
may result in some semantics differences. C objects created with |
| 66 |
_rawffi itself are allocated outside of the GC heap, such that they can be |
| 67 |
passed to external functions without worries. |
| 68 |
|
| 69 |
Porting the implementation to interpreter-level should likely improve |
| 70 |
its speed. Furthermore the current layering and the current _rawffi |
| 71 |
interface require more object allocations and copying than strictly |
| 72 |
necessary; this too could be improved. |
| 73 |
|
| 74 |
Here is a list of the limitations and missing features of the |
| 75 |
current implementation: |
| 76 |
|
| 77 |
* ``ctypes.pythonapi`` is missing. In previous versions, it was present |
| 78 |
and redirected to the `cpyext` C API emulation layer, but our |
| 79 |
implementation did not do anything sensible about the GIL and the |
| 80 |
functions were named with an extra "Py", for example |
| 81 |
``PyPyInt_FromLong()``. It was removed for being unhelpful. |
| 82 |
|
| 83 |
* We copy Python strings instead of having pointers to raw buffers |
| 84 |
|
| 85 |
* Features we did not get to implement: |
| 86 |
|
| 87 |
- custom alignment and bit-fields |
| 88 |
|
| 89 |
- resizing (``resize()`` function) |
| 90 |
|
| 91 |
- non-native byte-order objects |
| 92 |
|
| 93 |
- callbacks accepting by-value structures |
| 94 |
|
| 95 |
- slight semantic differences that ctypes makes |
| 96 |
between its primitive types and user subclasses |
| 97 |
of its primitive types |
| 98 |
|
| 99 |
|
| 100 |
Running application examples |
| 101 |
------------------------------ |
| 102 |
|
| 103 |
`pyglet`_ is known to run. We also had some success with pygame-ctypes (which is no longer maintained) and with a snapshot of the experimental pysqlite-ctypes. We will only describe how to run the pyglet examples. |
| 104 |
|
| 105 |
|
| 106 |
pyglet |
| 107 |
~~~~~~ |
| 108 |
|
| 109 |
We tried pyglet checking it out from its repository at revision 1984. |
| 110 |
|
| 111 |
From pyglet, the following examples are known to work: |
| 112 |
|
| 113 |
- opengl.py |
| 114 |
- multiple_windows.py |
| 115 |
- events.py |
| 116 |
- html_label.py |
| 117 |
- timer.py |
| 118 |
- window_platform_event.py |
| 119 |
- fixed_resolution.py |
| 120 |
|
| 121 |
The pypy-c translated to run the ctypes tests can be used to run the pyglet examples as well. They can be run like e.g.:: |
| 122 |
|
| 123 |
$ cd pyglet/ |
| 124 |
$ PYTHONPATH=. ../ctypes-stable/pypy/goal/pypy-c examples/opengl.py |
| 125 |
|
| 126 |
|
| 127 |
they usually should be terminated with ctrl-c. Refer to the their doc strings for details about how they should behave. |
| 128 |
|
| 129 |
The following examples don't work for reasons independent from ctypes: |
| 130 |
|
| 131 |
- image_convert.py needs PIL |
| 132 |
- image_display.py needs PIL |
| 133 |
- astraea/astraea.py needs PIL |
| 134 |
|
| 135 |
We did not try the following examples: |
| 136 |
|
| 137 |
- media_player.py needs avbin or at least a proper sound card setup for |
| 138 |
.wav files |
| 139 |
- video.py needs avbin |
| 140 |
- soundscape needs avbin |
| 141 |
|
| 142 |
.. _pyglet: http://pyglet.org/ |
| 143 |
|