PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/doc/faq.rst

https://bitbucket.org/pypy/pypy/
ReStructuredText | 399 lines | 290 code | 109 blank | 0 comment | 0 complexity | 8f4cb00bf6d5a313df0e8cc45aa05090 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. Frequently Asked Questions
  2. ==========================
  3. .. contents::
  4. See also: `Frequently ask questions about RPython.`__
  5. .. __: http://rpython.readthedocs.org/en/latest/faq.html
  6. ---------------------------
  7. What is PyPy?
  8. -------------
  9. PyPy is a reimplementation of Python in Python, using the RPython translation
  10. toolchain.
  11. PyPy tries to find new answers about ease of creation, flexibility,
  12. maintainability and speed trade-offs for language implementations.
  13. For further details see our :doc:`goal and architecture document <architecture>`.
  14. Is PyPy a drop in replacement for CPython?
  15. ------------------------------------------
  16. Almost!
  17. The mostly likely stumbling block for any given project is support for
  18. :ref:`extension modules <extension-modules>`. PyPy supports a continually growing
  19. number of extension modules, but so far mostly only those found in the
  20. standard library.
  21. The language features (including builtin types and functions) are very
  22. complete and well tested, so if your project does not use many
  23. extension modules there is a good chance that it will work with PyPy.
  24. We list the differences we know about in :doc:`cpython differences <cpython_differences>`.
  25. Module xyz does not work with PyPy: ImportError
  26. -----------------------------------------------
  27. A module installed for CPython is not automatically available for PyPy
  28. --- just like a module installed for CPython 2.6 is not automatically
  29. available for CPython 2.7 if you installed both. In other words, you
  30. need to install the module xyz specifically for PyPy.
  31. On Linux, this means that you cannot use ``apt-get`` or some similar
  32. package manager: these tools are only meant *for the version of CPython
  33. provided by the same package manager.* So forget about them for now
  34. and read on.
  35. It is quite common nowadays that xyz is available on PyPI_ and
  36. installable with ``pip install xyz``. The simplest solution is to `use
  37. virtualenv (as documented here)`_. Then enter (activate) the virtualenv
  38. and type: ``pip install xyz``. If you don't know or don't want virtualenv,
  39. you can also install ``pip`` globally by saying ``pypy -m ensurepip``.
  40. If you get errors from the C compiler, the module is a CPython C
  41. Extension module using unsupported features. `See below.`_
  42. Alternatively, if either the module xyz is not available on PyPI or you
  43. don't want to use virtualenv, then download the source code of xyz,
  44. decompress the zip/tarball, and run the standard command: ``pypy
  45. setup.py install``. (Note: `pypy` here instead of `python`.) As usual
  46. you may need to run the command with `sudo` for a global installation.
  47. The other commands of ``setup.py`` are available too, like ``build``.
  48. .. _PyPI: https://pypi.python.org/pypi
  49. .. _`use virtualenv (as documented here)`: install.html#installing-using-virtualenv
  50. Module xyz does not work in the sandboxed PyPy?
  51. -----------------------------------------------
  52. You cannot import *any* extension module in a `sandboxed PyPy`_,
  53. sorry. Even the built-in modules available are very limited.
  54. Sandboxing in PyPy is a good proof of concept, really safe IMHO, but
  55. it is only a proof of concept. It seriously requires someone working
  56. on it. Before this occurs, it can only be used it for "pure Python"
  57. examples: programs that import mostly nothing (or only pure Python
  58. modules, recursively).
  59. .. _`sandboxed PyPy`: sandbox.html
  60. .. _`See below.`:
  61. Do CPython Extension modules work with PyPy?
  62. --------------------------------------------
  63. We have experimental support for CPython extension modules, so
  64. they run with minor changes. This has been a part of PyPy since
  65. the 1.4 release, but support is still in beta phase. CPython
  66. extension modules in PyPy are often much slower than in CPython due to
  67. the need to emulate refcounting. It is often faster to take out your
  68. CPython extension and replace it with a pure python version that the
  69. JIT can see. If trying to install module xyz, and the module has both
  70. a C and a Python version of the same code, try first to disable the C
  71. version; this is usually easily done by changing some line in ``setup.py``.
  72. We fully support ctypes-based extensions. But for best performance, we
  73. recommend that you use the cffi_ module to interface with C code.
  74. For information on which third party extensions work (or do not work)
  75. with PyPy see the `compatibility wiki`_.
  76. For more information about how we manage refcounting semamtics see
  77. rawrefcount_
  78. .. _compatibility wiki: https://bitbucket.org/pypy/compatibility/wiki/Home
  79. .. _cffi: http://cffi.readthedocs.org/
  80. .. _rawrefcount: discussion/rawrefcount.html
  81. On which platforms does PyPy run?
  82. ---------------------------------
  83. PyPy currently supports:
  84. * **x86** machines on most common operating systems
  85. (Linux 32/64 bits, Mac OS X 64 bits, Windows 32 bits, OpenBSD, FreeBSD),
  86. * newer **ARM** hardware (ARMv6 or ARMv7, with VFPv3) running Linux,
  87. * big- and little-endian variants of **PPC64** running Linux,
  88. * **s390x** running Linux
  89. PyPy is regularly and extensively tested on Linux machines. It
  90. works on Mac and Windows: it is tested there, but most of us are running
  91. Linux so fixes may depend on 3rd-party contributions.
  92. To bootstrap from sources, PyPy can use either CPython 2.7 or
  93. another (e.g. older) PyPy. Cross-translation is not really supported:
  94. e.g. to build a 32-bit PyPy, you need to have a 32-bit environment.
  95. Cross-translation is only explicitly supported between a 32-bit Intel
  96. Linux and ARM Linux (see :ref:`here <rpython:arm>`).
  97. Which Python version (2.x?) does PyPy implement?
  98. ------------------------------------------------
  99. PyPy currently aims to be fully compatible with Python 2.7. That means that
  100. it contains the standard library of Python 2.7 and that it supports 2.7
  101. features (such as set comprehensions).
  102. .. _threading:
  103. Does PyPy have a GIL? Why?
  104. -------------------------------------------------
  105. Yes, PyPy has a GIL. Removing the GIL is very hard. The problems are
  106. essentially the same as with CPython (including the fact that our
  107. garbage collectors are not thread-safe so far). Fixing it is possible,
  108. as shown by Jython and IronPython, but difficult. It would require
  109. adapting the whole source code of PyPy, including subtle decisions about
  110. whether some effects are ok or not for the user (i.e. the Python
  111. programmer).
  112. Instead, since 2012, there is work going on on a still very experimental
  113. :doc:`Software Transactional Memory <stm>` (STM) version of PyPy. This should give
  114. an alternative PyPy which works without a GIL, while at the same time
  115. continuing to give the Python programmer the complete illusion of having
  116. one.
  117. Is PyPy more clever than CPython about Tail Calls?
  118. --------------------------------------------------
  119. No. PyPy follows the Python language design, including the built-in
  120. debugger features. This prevents tail calls, as summarized by Guido
  121. van Rossum in two__ blog__ posts. Moreover, neither the JIT nor
  122. Stackless__ change anything to that.
  123. .. __: http://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html
  124. .. __: http://neopythonic.blogspot.com/2009/04/final-words-on-tail-calls.html
  125. .. __: stackless.html
  126. How do I write extension modules for PyPy?
  127. ------------------------------------------
  128. See :doc:`extending`.
  129. .. _how-fast-is-pypy:
  130. How fast is PyPy?
  131. -----------------
  132. This really depends on your code.
  133. For pure Python algorithmic code, it is very fast. For more typical
  134. Python programs we generally are 3 times the speed of CPython 2.7.
  135. You might be interested in our `benchmarking site`_ and our
  136. :ref:`jit documentation <rpython:jit>`.
  137. `Your tests are not a benchmark`_: tests tend to be slow under PyPy
  138. because they run exactly once; if they are good tests, they exercise
  139. various corner cases in your code. This is a bad case for JIT
  140. compilers. Note also that our JIT has a very high warm-up cost, meaning
  141. that any program is slow at the beginning. If you want to compare the
  142. timings with CPython, even relatively simple programs need to run *at
  143. least* one second, preferrably at least a few seconds. Large,
  144. complicated programs need even more time to warm-up the JIT.
  145. .. _benchmarking site: http://speed.pypy.org
  146. .. _your tests are not a benchmark: http://alexgaynor.net/2013/jul/15/your-tests-are-not-benchmark/
  147. Couldn't the JIT dump and reload already-compiled machine code?
  148. ---------------------------------------------------------------
  149. No, we found no way of doing that. The JIT generates machine code
  150. containing a large number of constant addresses --- constant at the time
  151. the machine code is generated. The vast majority is probably not at all
  152. constants that you find in the executable, with a nice link name. E.g.
  153. the addresses of Python classes are used all the time, but Python
  154. classes don't come statically from the executable; they are created anew
  155. every time you restart your program. This makes saving and reloading
  156. machine code completely impossible without some very advanced way of
  157. mapping addresses in the old (now-dead) process to addresses in the new
  158. process, including checking that all the previous assumptions about the
  159. (now-dead) object are still true about the new object.
  160. Would type annotations help PyPy's performance?
  161. -----------------------------------------------
  162. Two examples of type annotations that are being proposed for improved
  163. performance are `Cython types`__ and `PEP 484 - Type Hints`__.
  164. .. __: http://docs.cython.org/src/reference/language_basics.html#declaring-data-types
  165. .. __: https://www.python.org/dev/peps/pep-0484/
  166. **Cython types** are, by construction, similar to C declarations. For
  167. example, a local variable or an instance attribute can be declared
  168. ``"cdef int"`` to force a machine word to be used. This changes the
  169. usual Python semantics (e.g. no overflow checks, and errors when
  170. trying to write other types of objects there). It gives some extra
  171. performance, but the exact benefits are unclear: right now
  172. (January 2015) for example we are investigating a technique that would
  173. store machine-word integers directly on instances, giving part of the
  174. benefits without the user-supplied ``"cdef int"``.
  175. **PEP 484 - Type Hints,** on the other hand, is almost entirely
  176. useless if you're looking at performance. First, as the name implies,
  177. they are *hints:* they must still be checked at runtime, like PEP 484
  178. says. Or maybe you're fine with a mode in which you get very obscure
  179. crashes when the type annotations are wrong; but even in that case the
  180. speed benefits would be extremely minor.
  181. There are several reasons for why. One of them is that annotations
  182. are at the wrong level (e.g. a PEP 484 "int" corresponds to Python 3's
  183. int type, which does not necessarily fits inside one machine word;
  184. even worse, an "int" annotation allows arbitrary int subclasses).
  185. Another is that a lot more information is needed to produce good code
  186. (e.g. "this ``f()`` called here really means this function there, and
  187. will never be monkey-patched" -- same with ``len()`` or ``list()``,
  188. btw). The third reason is that some "guards" in PyPy's JIT traces
  189. don't really have an obvious corresponding type (e.g. "this dict is so
  190. far using keys which don't override ``__hash__`` so a more efficient
  191. implementation was used"). Many guards don't even have any correspondence
  192. with types at all ("this class attribute was not modified"; "the loop
  193. counter did not reach zero so we don't need to release the GIL"; and
  194. so on).
  195. As PyPy works right now, it is able to derive far more useful
  196. information than can ever be given by PEP 484, and it works
  197. automatically. As far as we know, this is true even if we would add
  198. other techniques to PyPy, like a fast first-pass JIT.
  199. .. _`prolog and javascript`:
  200. Can I use PyPy's translation toolchain for other languages besides Python?
  201. --------------------------------------------------------------------------
  202. Yes. The toolsuite that translates the PyPy interpreter is quite
  203. general and can be used to create optimized versions of interpreters
  204. for any language, not just Python. Of course, these interpreters
  205. can make use of the same features that PyPy brings to Python:
  206. translation to various languages, stackless features,
  207. garbage collection, implementation of various things like arbitrarily long
  208. integers, etc.
  209. Currently, we have `Topaz`_, a Ruby interpreter; `Hippy`_, a PHP
  210. interpreter; preliminary versions of a `JavaScript interpreter`_
  211. (Leonardo Santagada as his Summer of PyPy project); a `Prolog interpreter`_
  212. (Carl Friedrich Bolz as his Bachelor thesis); and a `SmallTalk interpreter`_
  213. (produced during a sprint). On the `PyPy bitbucket page`_ there is also a
  214. Scheme and an Io implementation; both of these are unfinished at the moment.
  215. .. _Topaz: http://topazruby.com/
  216. .. _Hippy: http://morepypy.blogspot.ch/2012/07/hello-everyone.html
  217. .. _JavaScript interpreter: https://bitbucket.org/pypy/lang-js/
  218. .. _Prolog interpreter: https://bitbucket.org/cfbolz/pyrolog/
  219. .. _SmallTalk interpreter: http://dx.doi.org/10.1007/978-3-540-89275-5_7
  220. .. _PyPy bitbucket page: https://bitbucket.org/pypy/
  221. How do I get into PyPy development? Can I come to sprints?
  222. -----------------------------------------------------------
  223. Certainly you can come to sprints! We always welcome newcomers and try
  224. to help them as much as possible to get started with the project. We
  225. provide tutorials and pair them with experienced PyPy
  226. developers. Newcomers should have some Python experience and read some
  227. of the PyPy documentation before coming to a sprint.
  228. Coming to a sprint is usually the best way to get into PyPy development.
  229. If you get stuck or need advice, :doc:`contact us <index>`. IRC is
  230. the most immediate way to get feedback (at least during some parts of the day;
  231. most PyPy developers are in Europe) and the `mailing list`_ is better for long
  232. discussions.
  233. .. _mailing list: http://mail.python.org/mailman/listinfo/pypy-dev
  234. OSError: ... cannot restore segment prot after reloc... Help?
  235. -------------------------------------------------------------
  236. On Linux, if SELinux is enabled, you may get errors along the lines of
  237. "OSError: externmod.so: cannot restore segment prot after reloc: Permission
  238. denied." This is caused by a slight abuse of the C compiler during
  239. configuration, and can be disabled by running the following command with root
  240. privileges:
  241. .. code-block:: console
  242. # setenforce 0
  243. This will disable SELinux's protection and allow PyPy to configure correctly.
  244. Be sure to enable it again if you need it!
  245. How should I report a bug?
  246. --------------------------
  247. Our bug tracker is here: https://bitbucket.org/pypy/pypy/issues/
  248. Missing features or incompatibilities with CPython are considered
  249. bugs, and they are welcome. (See also our list of `known
  250. incompatibilities`__.)
  251. .. __: http://pypy.org/compat.html
  252. For bugs of the kind "I'm getting a PyPy crash or a strange
  253. exception", please note that: **We can't do anything without
  254. reproducing the bug ourselves**. We cannot do anything with
  255. tracebacks from gdb, or core dumps. This is not only because the
  256. standard PyPy is compiled without debug symbols. The real reason is
  257. that a C-level traceback is usually of no help at all in PyPy.
  258. Debugging PyPy can be annoying.
  259. `This is a clear and useful bug report.`__ (Admittedly, sometimes
  260. the problem is really hard to reproduce, but please try to.)
  261. .. __: https://bitbucket.org/pypy/pypy/issues/2363/segfault-in-gc-pinned-object-in
  262. In more details:
  263. * First, please give the exact PyPy version, and the OS.
  264. * It might help focus our search if we know if the bug can be
  265. reproduced on a "``pypy --jit off``" or not. If "``pypy --jit
  266. off``" always works, then the problem might be in the JIT.
  267. Otherwise, we know we can ignore that part.
  268. * If you got the bug using only Open Source components, please give a
  269. step-by-step guide that we can follow to reproduce the problem
  270. ourselves. Don't assume we know anything about any program other
  271. than PyPy. We would like a guide that we can follow point by point
  272. (without guessing or having to figure things out)
  273. on a machine similar to yours, starting from a bare PyPy, until we
  274. see the same problem. (If you can, you can try to reduce the number
  275. of steps and the time it needs to run, but that is not mandatory.)
  276. * If the bug involves Closed Source components, or just too many Open
  277. Source components to install them all ourselves, then maybe you can
  278. give us some temporary ssh access to a machine where the bug can be
  279. reproduced. Or, maybe we can download a VirtualBox or VMWare
  280. virtual machine where the problem occurs.
  281. * If giving us access would require us to use tools other than ssh,
  282. make appointments, or sign a NDA, then we can consider a commerical
  283. support contract for a small sum of money.
  284. * If even that is not possible for you, then sorry, we can't help.
  285. Of course, you can try to debug the problem yourself, and we can help
  286. you get started if you ask on the #pypy IRC channel, but be prepared:
  287. debugging an annoying PyPy problem usually involves quite a lot of gdb
  288. in auto-generated C code, and at least some knowledge about the
  289. various components involved, from PyPy's own RPython source code to
  290. the GC and possibly the JIT.