PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/doc/extending.rst

http://github.com/pypy/pypy
ReStructuredText | 170 lines | 129 code | 41 blank | 0 comment | 0 complexity | c52b25fbed0509090378f209a3285910 MD5 | raw file
  1. ===================================
  2. Writing extension modules for pypy
  3. ===================================
  4. This document tries to explain how to interface the PyPy python interpreter
  5. with any external library.
  6. Note: We try to describe state-of-the art, but it
  7. might fade out of date as this is the front on which things are changing
  8. in pypy rapidly.
  9. Possibilities
  10. =============
  11. Right now, there are three possibilities of providing third-party modules
  12. for the PyPy python interpreter (in order of usefulness):
  13. * Write them in pure python and use ctypes, see ctypes_
  14. section
  15. * Write them in pure python and use direct libffi low-level bindings, See
  16. \_ffi_ module description.
  17. * Write them in RPython as mixedmodule_, using *rffi* as bindings.
  18. * Write them in C++ and bind them through Reflex_ (EXPERIMENTAL)
  19. .. _ctypes: #CTypes
  20. .. _\_ffi: #LibFFI
  21. .. _mixedmodule: #Mixed Modules
  22. CTypes
  23. ======
  24. The ctypes module in PyPy is ready to use.
  25. It's goal is to be as-compatible-as-possible with the
  26. `CPython ctypes`_ version. Right now it's able to support large examples,
  27. such as pyglet. PyPy is planning to have a 100% compatible ctypes
  28. implementation, without the CPython C-level API bindings (so it is very
  29. unlikely that direct object-manipulation trickery through this API will work).
  30. We also provide a `ctypes-configure`_ for overcoming the platform dependencies,
  31. not relying on the ctypes codegen. This tool works by querying gcc about
  32. platform-dependent details (compiling small snippets of C code and running
  33. them), so it'll benefit not pypy-related ctypes-based modules as well.
  34. ctypes call are optimized by the JIT and the resulting machine code contains a
  35. direct call to the target C function. However, due to the very dynamic nature
  36. of ctypes, some overhead over a bare C call is still present, in particular to
  37. check/convert the types of the parameters. Moreover, even if most calls are
  38. optimized, some cannot and thus need to follow the slow path, not optimized by
  39. the JIT.
  40. .. _`ctypes-configure`: ctypes-implementation.html#ctypes-configure
  41. .. _`CPython ctypes`: http://docs.python.org/library/ctypes.html
  42. Pros
  43. ----
  44. Stable, CPython-compatible API. Most calls are fast, optimized by JIT.
  45. Cons
  46. ----
  47. Problems with platform-dependency (although we partially solve
  48. those). Although the JIT optimizes ctypes calls, some overhead is still
  49. present. The slow-path is very slow.
  50. LibFFI
  51. ======
  52. Mostly in order to be able to write a ctypes module, we developed a very
  53. low-level libffi bindings called ``_ffi``. (libffi is a C-level library for dynamic calling,
  54. which is used by CPython ctypes). This library provides stable and usable API,
  55. although it's API is a very low-level one. It does not contain any
  56. magic. It is also optimized by the JIT, but has much less overhead than ctypes.
  57. Pros
  58. ----
  59. It Works. Probably more suitable for a delicate code where ctypes magic goes
  60. in a way. All calls are optimized by the JIT, there is no slow path as in
  61. ctypes.
  62. Cons
  63. ----
  64. It combines disadvantages of using ctypes with disadvantages of using mixed
  65. modules. CPython-incompatible API, very rough and low-level.
  66. Mixed Modules
  67. =============
  68. This is the most advanced and powerful way of writing extension modules.
  69. It has some serious disadvantages:
  70. * a mixed module needs to be written in RPython, which is far more
  71. complicated than Python (XXX link)
  72. * due to lack of separate compilation (as of July 2011), each
  73. compilation-check requires to recompile whole PyPy python interpreter,
  74. which takes 0.5-1h. We plan to solve this at some point in near future.
  75. * although rpython is a garbage-collected language, the border between
  76. C and RPython needs to be managed by hand (each object that goes into the
  77. C level must be explicitly freed).
  78. Some documentation is available `here`_
  79. .. _`here`: rffi.html
  80. XXX we should provide detailed docs about lltype and rffi, especially if we
  81. want people to follow that way.
  82. Reflex
  83. ======
  84. This method is still experimental and is being exercised on a branch,
  85. `reflex-support`_, which adds the `cppyy`_ module.
  86. The method works by using the `Reflex package`_ to provide reflection
  87. information of the C++ code, which is then used to automatically generate
  88. bindings at runtime.
  89. From a python standpoint, there is no difference between generating bindings
  90. at runtime, or having them "statically" generated and available in scripts
  91. or compiled into extension modules: python classes and functions are always
  92. runtime structures, created when a script or module loads.
  93. However, if the backend itself is capable of dynamic behavior, it is a much
  94. better functional match to python, allowing tighter integration and more
  95. natural language mappings.
  96. Full details are `available here`_.
  97. .. _`cppyy`: cppyy.html
  98. .. _`reflex-support`: cppyy.html
  99. .. _`Reflex package`: http://root.cern.ch/drupal/content/reflex
  100. .. _`available here`: cppyy.html
  101. Pros
  102. ----
  103. The cppyy module is written in RPython, which makes it possible to keep the
  104. code execution visible to the JIT all the way to the actual point of call into
  105. C++, thus allowing for a very fast interface.
  106. Reflex is currently in use in large software environments in High Energy
  107. Physics (HEP), across many different projects and packages, and its use can be
  108. virtually completely automated in a production environment.
  109. One of its uses in HEP is in providing language bindings for CPython.
  110. Thus, it is possible to use Reflex to have bound code work on both CPython and
  111. on PyPy.
  112. In the medium-term, Reflex will be replaced by `cling`_, which is based on
  113. `llvm`_.
  114. This will affect the backend only; the python-side interface is expected to
  115. remain the same, except that cling adds a lot of dynamic behavior to C++,
  116. enabling further language integration.
  117. .. _`cling`: http://root.cern.ch/drupal/content/cling
  118. .. _`llvm`: http://llvm.org/
  119. Cons
  120. ----
  121. C++ is a large language, and cppyy is not yet feature-complete.
  122. Still, the experience gained in developing the equivalent bindings for CPython
  123. means that adding missing features is a simple matter of engineering, not a
  124. question of research.
  125. The module is written so that currently missing features should do no harm if
  126. you don't use them, if you do need a particular feature, it may be necessary
  127. to work around it in python or with a C++ helper function.
  128. Although Reflex works on various platforms, the bindings with PyPy have only
  129. been tested on Linux.