Debian Codegraph @

pypy/doc/faq.rst

raw
1 Frequently Asked Questions
2 ==========================
3
4 .. contents::
5
6 See also: `Frequently asked questions about RPython.`__
7
8 .. __: https://rpython.readthedocs.org/en/latest/faq.html
9
10 ---------------------------
11
12
13 What is PyPy?
14 -------------
15
16 PyPy is a reimplementation of Python in Python, using the RPython translation
17 toolchain.
18
19 PyPy tries to find new answers about ease of creation, flexibility,
20 maintainability and speed trade-offs for language implementations.
21 For further details see our :doc:`goal and architecture document <architecture>`.
22
23
24 Is PyPy a drop in replacement for CPython?
25 ------------------------------------------
26
27 Almost!
28
29 The most likely stumbling block for any given project is support for
30 :ref:`extension modules <extension-modules>`. PyPy supports a continually growing
31 number of extension modules, but so far mostly only those found in the
32 standard library.
33
34 The language features (including builtin types and functions) are very
35 refined and well tested, so if your project doesn't use many
36 extension modules there is a good chance that it will work with PyPy.
37
38 We list the known differences in :doc:`cpython differences <cpython_differences>`.
39
40
41 Module xyz does not work with PyPy: ImportError
42 -----------------------------------------------
43
44 A module installed for CPython is not automatically available for PyPy
45 --- just like a module installed for CPython 3.6 is not automatically
46 available for CPython 3.7 if you installed both. In other words, you
47 need to install the module xyz specifically for PyPy.
48
49 On Linux, this means that you cannot use ``apt-get`` or some similar
50 package manager: these tools are only meant *for the version of CPython
51 provided by the same package manager.* So forget about them for now
52 and read on.
53
54 It is quite common nowadays that xyz is available on PyPI_ and
55 installable with ``<pypy> -mpip install xyz``. The simplest solution is to
56 `use virtualenv (as documented here)`_. Then enter (activate) the virtualenv
57 and type: ``pypy -mpip install xyz``. If you don't know or don't want
58 virtualenv, you can also use ``pip`` locally after ``pypy -m ensurepip``.
59 The `ensurepip module`_ is built-in to the PyPy downloads we provide.
60 Best practices with ``pip`` is to always call it as ``<python> -mpip ...``,
61 but if you wish to be able to call ``pip`` directly from the command line, you
62 must call ``pypy -mensurepip --default-pip``.
63
64 If you get errors from the C compiler, the module is a CPython C
65 Extension module using unsupported features. `See below.`_
66
67 Alternatively, if either the module xyz is not available on PyPI or you
68 don't want to use virtualenv, then download the source code of xyz,
69 decompress the zip/tarball, and run the standard command: ``pypy -mpip
70 install .`` from inside the source directory. (Note: ``pypy`` here instead
71 of ``python``.) As usual you may need to run the command with ``sudo`` for
72 a global installation.
73
74 .. _PyPI: https://pypi.org
75 .. _`use virtualenv (as documented here)`: install.html#installing-using-virtualenv
76 .. _`ensurepip module`: https://docs.python.org/3.6/library/ensurepip.html
77
78
79 Module xyz does not work in the sandboxed PyPy?
80 -----------------------------------------------
81
82 You cannot import *any* extension module in a `sandboxed PyPy`_,
83 sorry. Even the built-in modules available are very limited.
84 Sandboxing in PyPy is a good proof of concept, and is without a doubt
85 safe IMHO, however it is only a proof of concept. It currently requires
86 some work from a motivated developer. However, until then it can only be used for "pure Python"
87 example: programs that import mostly nothing (or only pure Python
88 modules, recursively).
89
90 .. _`sandboxed PyPy`: sandbox.html
91
92
93 .. _`See below.`:
94
95 Do C-extension modules work with PyPy?
96 --------------------------------------
97
98 **First note that some Linux distributions (e.g. Ubuntu, Debian) split
99 PyPy into several packages. If you installed a package called "pypy",
100 then you may also need to install "pypy-dev" for the following to work.**
101
102 We have support for c-extension modules (modules written using the C-API), so
103 they run without modifications. This has been a part of PyPy since
104 the 1.4 release, and support is almost complete. CPython
105 extension modules in PyPy are often much slower than in CPython due to
106 the need to emulate refcounting. It is often faster to take out your
107 c-extension and replace it with a pure python or CFFI version that the
108 JIT can optimize. If trying to install module xyz, and the module has both
109 a C and a Python version of the same code, try first to disable the C
110 version; this is usually easily done by changing some line in ``setup.py``.
111
112 We fully support ctypes-based extensions. But for best performance, we
113 recommend that you use the cffi_ module to interface with C code.
114
115 For more information about how we manage refcounting semantics see
116 rawrefcount_.
117
118 .. _cffi: https://cffi.readthedocs.org/
119 .. _rawrefcount: discussion/rawrefcount.html
120
121
122 On which platforms does PyPy run?
123 ---------------------------------
124
125 PyPy currently supports:
126
127 * **x86** machines on most common operating systems
128 (Linux 32/64 bits, Mac OS X 64 bits, Windows 32/64 bits, OpenBSD, FreeBSD),
129
130 * 64-bit **AArch**, also known as ARM64,
131
132 * **ARM** hardware (ARMv6 or ARMv7, with VFPv3) running Linux
133 (we no longer provide prebuilt binaries for these),
134
135 * big- and little-endian variants of **PPC64** running Linux,
136
137 * **s390x** running Linux
138
139 PyPy is regularly and extensively tested on Linux machines. It
140 works on Mac and Windows: it is tested there, but most of us are running
141 Linux so fixes may depend on 3rd-party contributions.
142
143 To bootstrap from sources, PyPy can use either CPython 2.7 or
144 PyPy 2.7. Cross-translation is not really supported:
145 e.g. to build a 32-bit PyPy, you need to have a 32-bit environment.
146
147
148 Which Python versions does PyPy implement?
149 ------------------------------------------
150
151 PyPy will always support 2.7 since RPython is written for it. In addition, PyPy
152 supports various Python3 versions, see the `release notes`_ for the latest
153 releases. Typically, we will support one or two versions of Python3.
154
155 .. _`release notes`: index-of-release-notes.html
156
157
158 .. _threading:
159
160 Does PyPy have a GIL? Why?
161 ---------------------------
162
163 Yes, PyPy has a GIL. Removing the GIL is very hard. On top of CPython,
164 you have two problems: (1) GC, in this case reference counting; (2) the
165 whole Python language.
166
167 For PyPy, the hard issue is (2): by that I mean issues like what occurs
168 if a mutable object is changed from one thread and read from another
169 concurrently. This is a problem for *any* mutable type: it needs
170 careful review and fixes (fine-grained locks, mostly) through the
171 *whole* Python interpreter. It is a major effort, although not
172 completely impossible, as Jython/IronPython showed. This includes
173 subtle decisions about whether some effects are ok or not for the user
174 (i.e. the Python programmer).
175
176 CPython has additionally the problem (1) of reference counting. With
177 PyPy, this sub-problem is simpler: we need to make our GC
178 multithread-aware. This is easier to do efficiently in PyPy than in
179 CPython. It doesn't solve the issue (2), though.
180
181 Note that there was work to support a
182 :doc:`Software Transactional Memory <stm>` (STM) version of PyPy. This
183 should give an alternative PyPy which works without a GIL, while at the
184 same time continuing to give the Python programmer the complete illusion
185 of having one. This work is currently a bit stalled because of its own
186 technical difficulties.
187
188 What about numpy, numpypy, micronumpy?
189 --------------------------------------
190
191 Way back in 2011, the PyPy team `started to reimplement`_ numpy in PyPy. It
192 has two pieces:
193
194 * the builtin module :source:`pypy/module/micronumpy`: this is written in
195 RPython and roughly covers the content of the ``numpy.core.multiarray``
196 module. Confusingly enough, this is available in PyPy under the name
197 ``_numpypy``. It is included by default in all the official releases of
198 PyPy (but it might be dropped in the future).
199
200 * a fork_ of the official numpy repository maintained by us and informally
201 called ``numpypy``: The main difference with the upstream numpy, is that
202 it is based on the micronumpy module written in RPython, instead of
203 ``numpy.core.multiarray`` which is written in C.
204
205
206 Should I install numpy or numpypy?
207 ----------------------------------
208
209 TL;DR version: you should use numpy. You can install it by doing ``pypy -m pip
210 install numpy``.
211
212 The upstream ``numpy`` is written in C, and runs under the cpyext
213 compatibility layer. Nowadays, cpyext is mature enough that you can simply
214 use the upstream ``numpy``, since it passes the test suite. The main drawback
215 of ``numpy`` is that cpyext
216 is infamously slow, and thus it has worse performance compared to
217 ``numpypy``. However, we are actively working on improving it, as we expect to
218 reach the same speed when HPy_ can be used.
219
220 On the other hand, ``numpypy`` is more JIT-friendly and very fast to call,
221 since it is written in RPython: but it is a reimplementation, and it's hard to
222 be completely compatible: over the years the project slowly matured and
223 eventually it was able to call out to the LAPACK and BLAS libraries to speed
224 matrix calculations, and reached around an 80% parity with the upstream
225 numpy. However, 80% is far from 100%. Since cpyext/numpy compatibility is
226 complete, we have discontinued support for ``numpypy``.
227
228 .. _`started to reimplement`: https://www.pypy.org/posts/2011/05/numpy-in-pypy-status-and-roadmap-8332894230779779992.html
229 .. _fork: https://github.com/pypy/numpypy
230 .. _HPy: https://hpyproject.org/
231
232
233 Is PyPy more clever than CPython about Tail Calls?
234 --------------------------------------------------
235
236 No. PyPy follows the Python language design, including the built-in
237 debugger features. This prevents tail calls, as summarized by Guido
238 van Rossum in two__ blog__ posts. Moreover, neither the JIT nor
239 Stackless__ change anything to that.
240
241 .. __: https://neopythonic.blogspot.com/2009/04/tail-recursion-elimination.html
242 .. __: https://neopythonic.blogspot.com/2009/04/final-words-on-tail-calls.html
243 .. __: stackless.html
244
245
246 How do I write extension modules for PyPy?
247 ------------------------------------------
248
249 See :doc:`extending`.
250
251
252 .. _how-fast-is-pypy:
253
254 How fast is PyPy?
255 -----------------
256 This really depends on your code.
257 For pure Python algorithmic code, it is very fast. For more typical
258 Python programs we generally are 3 times the speed of CPython 2.7.
259 You might be interested in our `benchmarking site`_ and our
260 :ref:`jit documentation <rpython:jit>`.
261
262 `Your tests are not a benchmark`_: tests tend to be slow under PyPy
263 because they run exactly once; if they are good tests, they exercise
264 various corner cases in your code. This is a bad case for JIT
265 compilers. Note also that our JIT has a very high warm-up cost, meaning
266 that any program is slow at the beginning. If you want to compare the
267 timings with CPython, even relatively simple programs need to run *at
268 least* one second, preferably at least a few seconds. Large,
269 complicated programs need even more time to warm-up the JIT.
270
271 .. _benchmarking site: https://speed.pypy.org
272
273 .. _your tests are not a benchmark: https://alexgaynor.net/2013/jul/15/your-tests-are-not-benchmark/
274
275
276 I wrote a 3-lines benchmark and it's not faster than CPython. Why?
277 -------------------------------------------------------------------
278
279 Three-lines benchmarks are benchmarks that either do absolutely nothing (in
280 which case PyPy is probably a lot faster than CPython), or more likely, they
281 are benchmarks that spend most of their time doing things in C.
282
283 For example, a loop that repeatedly issues one complex SQL operation will only
284 measure how performant the SQL database is. Similarly, computing many elements
285 from the Fibonacci series builds very large integers, so it only measures how
286 performant the long integer library is. This library is written in C for
287 CPython, and in RPython for PyPy, but that boils down to the same thing.
288
289 PyPy speeds up the code written *in Python*.
290
291
292 Couldn't the JIT dump and reload already-compiled machine code?
293 ---------------------------------------------------------------
294
295 No, we found no way of doing that. The JIT generates machine code
296 containing a large number of constant addresses --- constant at the time
297 the machine code is generated. The vast majority is probably not at all
298 constants that you find in the executable, with a nice link name. E.g.
299 the addresses of Python classes are used all the time, but Python
300 classes don't come statically from the executable; they are created anew
301 every time you restart your program. This makes saving and reloading
302 machine code completely impossible without some very advanced way of
303 mapping addresses in the old (now-dead) process to addresses in the new
304 process, including checking that all the previous assumptions about the
305 (now-dead) object are still true about the new object.
306
307
308 Would type annotations help PyPy's performance?
309 -----------------------------------------------
310
311 Two examples of type annotations that are being proposed for improved
312 performance are `Cython types`__ and :pep:`PEP 484 - Type Hints <484>`.
313
314 .. __: https://docs.cython.org/src/reference/language_basics.html#declaring-data-types
315
316 **Cython types** are, by construction, similar to C declarations. For
317 example, a local variable or an instance attribute can be declared
318 ``"cdef int"`` to force a machine word to be used. This changes the
319 usual Python semantics (e.g. no overflow checks, and errors when
320 trying to write other types of objects there). It gives some extra
321 performance, but the exact benefits are unclear: right now
322 (January 2015) for example we are investigating a technique that would
323 store machine-word integers directly on instances, giving part of the
324 benefits without the user-supplied ``"cdef int"``.
325
326 **PEP 484 - Type Hints,** on the other hand, is almost entirely
327 useless if you're looking at performance. First, as the name implies,
328 they are *hints:* they must still be checked at runtime, like PEP 484
329 says. Or maybe you're fine with a mode in which you get very obscure
330 crashes when the type annotations are wrong; but even in that case the
331 speed benefits would be extremely minor.
332
333 There are several reasons for why. One of them is that annotations
334 are at the wrong level (e.g. a PEP 484 "int" corresponds to Python 3's
335 int type, which does not necessarily fits inside one machine word;
336 even worse, an "int" annotation allows arbitrary int subclasses).
337 Another is that a lot more information is needed to produce good code
338 (e.g. "this ``f()`` called here really means this function there, and
339 will never be monkey-patched" -- same with ``len()`` or ``list()``,
340 btw). The third reason is that some "guards" in PyPy's JIT traces
341 don't really have an obvious corresponding type (e.g. "this dict is so
342 far using keys which don't override ``__hash__`` so a more efficient
343 implementation was used"). Many guards don't even have any correspondence
344 with types at all ("this class attribute was not modified"; "the loop
345 counter did not reach zero so we don't need to release the GIL"; and
346 so on).
347
348 As PyPy works right now, it is able to derive far more useful
349 information than can ever be given by PEP 484, and it works
350 automatically. As far as we know, this is true even if we would add
351 other techniques to PyPy, like a fast first-pass JIT.
352
353
354 .. _`prolog and javascript`:
355
356 Can I use PyPy's translation toolchain for other languages besides Python?
357 --------------------------------------------------------------------------
358
359 Yes. The toolsuite that translates the PyPy interpreter is quite
360 general and can be used to create optimized versions of interpreters
361 for any language, not just Python. Of course, these interpreters
362 can make use of the same features that PyPy brings to Python:
363 translation to various languages, stackless features,
364 garbage collection, implementation of various things like arbitrarily long
365 integers, etc.
366
367 Currently, we have `Topaz`_, a Ruby interpreter; `Hippy`_, a PHP
368 interpreter; preliminary versions of a `JavaScript interpreter`_
369 (Leonardo Santagada as his Summer of PyPy project); a `Prolog interpreter`_
370 (Carl Friedrich Bolz as his Bachelor thesis); and a `SmallTalk interpreter`_
371 (produced during a sprint). There is also an unfinished `Scheme`_ implementation.
372
373 .. _Topaz: https://github.com/topazproject/topaz
374 .. _Hippy: https://morepypy.blogspot.ch/2012/07/hello-everyone.html
375 .. _JavaScript interpreter: https://github.com/progval/rpython-langjs
376 .. _Prolog interpreter: https://bitbucket.org/cfbolz/pyrolog/
377 .. _SmallTalk interpreter: https://dx.doi.org/10.1007/978-3-540-89275-5_7
378 .. _Scheme: https://github.com/tomoh1r/rpython-lang-scheme
379
380
381 How do I get into PyPy development? Can I come to sprints?
382 -----------------------------------------------------------
383
384 Certainly you can come to sprints! We always welcome newcomers and try
385 to help them as much as possible to get started with the project. We
386 provide tutorials and pair them with experienced PyPy
387 developers. Newcomers should have some Python experience and read some
388 of the PyPy documentation before coming to a sprint.
389
390 Coming to a sprint is usually the best way to get into PyPy development.
391 If you get stuck or need advice, :doc:`contact us <index>`. IRC is
392 the most immediate way to get feedback (at least during some parts of the day;
393 most PyPy developers are in Europe) and the `mailing list`_ is better for long
394 discussions.
395
396 We also encourage engagement via the GitHub repo at
397 https://github.com/pypy/pypy. Issues can be filed and discussed in the
398 `issue tracker`_ and we welcome `pull requests`.
399
400 .. _`issue tracker`: https://github.com/pypy/pypy/issues/
401 .. _`pull requests`: https://github.com/pypy/pypy/pulls/
402
403 .. _mailing list: https://mail.python.org/mailman/listinfo/pypy-dev
404
405
406 OSError: ... cannot restore segment prot after reloc... Help?
407 -------------------------------------------------------------
408
409 On Linux, if SELinux is enabled, you may get errors along the lines of
410 "OSError: externmod.so: cannot restore segment prot after reloc: Permission
411 denied." This is caused by a slight abuse of the C compiler during
412 configuration, and can be disabled by running the following command with root
413 privileges:
414
415 .. code-block:: console
416
417 # setenforce 0
418
419 This will disable SELinux's protection and allow PyPy to configure correctly.
420 Be sure to enable it again if you need it!
421
422
423 How should I report a bug?
424 --------------------------
425
426 Our bug tracker is here: https://github.com/pypy/pypy/issues/
427
428 Missing features or incompatibilities with CPython are considered
429 bugs, and they are welcome. (See also our list of `known
430 incompatibilities`__.)
431
432 .. __: https://pypy.org/compat.html
433
434 For bugs of the kind "I'm getting a PyPy crash or a strange
435 exception", please note that: **We can't do anything without
436 reproducing the bug ourselves**. We cannot do anything with
437 tracebacks from gdb, or core dumps. This is not only because the
438 standard PyPy is compiled without debug symbols. The real reason is
439 that a C-level traceback is usually of no help at all in PyPy.
440 Debugging PyPy can be annoying.
441
442 `This is a clear and useful bug report.`__ (Admittedly, sometimes
443 the problem is really hard to reproduce, but please try to.)
444
445 .. __: https://github.com/pypy/pypy/issues/2363
446
447 In more details:
448
449 * First, please give the exact PyPy version, and the OS.
450
451 * It might help focus our search if we know if the bug can be
452 reproduced on a "``pypy --jit off``" or not. If "``pypy --jit
453 off``" always works, then the problem might be in the JIT.
454 Otherwise, we know we can ignore that part.
455
456 * If you got the bug using only Open Source components, please give a
457 step-by-step guide that we can follow to reproduce the problem
458 ourselves. Don't assume we know anything about any program other
459 than PyPy. We would like a guide that we can follow point by point
460 (without guessing or having to figure things out)
461 on a machine similar to yours, starting from a bare PyPy, until we
462 see the same problem. (If you can, you can try to reduce the number
463 of steps and the time it needs to run, but that is not mandatory.)
464
465 * If the bug involves Closed Source components, or just too many Open
466 Source components to install them all ourselves, then maybe you can
467 give us some temporary ssh access to a machine where the bug can be
468 reproduced. Or, maybe we can download a VirtualBox or VMWare
469 virtual machine where the problem occurs.
470
471 * If giving us access would require us to use tools other than ssh,
472 make appointments, or sign a NDA, then we can consider a commercial
473 support contract for a small sum of money.
474
475 * If even that is not possible for you, then sorry, we can't help.
476
477 Of course, you can try to debug the problem yourself, and we can help
478 you get started if you ask on the #pypy IRC channel, but be prepared:
479 debugging an annoying PyPy problem usually involves quite a lot of gdb
480 in auto-generated C code, and at least some knowledge about the
481 various components involved, from PyPy's own RPython source code to
482 the GC and possibly the JIT.
483
484
485 .. _git:
486 .. _github:
487
488 Why did PyPy switch to Git and move to GitHub?
489 ----------------------------------------------
490
491 PyPy moved from SVN to `Mercurial and bitbucket`_ in 2010. In 2020, when
492 bitbucket summarily dropped support for Mercurial, we debated whether to move
493 to Git/GitHub. At the time we concluded that (1) the Git workflow is not as
494 well suited as the Mercurial workflow for our style,
495 and (2) moving to github "just because everybody else does" is a argument on
496 thin grounds.
497
498 For (1), there are a few issues, but maybe the most important one is that the
499 PyPy repository has got thousands of *named* branches. Git has no equivalent
500 concept. Git has *branches,* of course, which in Mercurial are called
501 bookmarks. We're not talking about bookmarks.
502
503 The difference between git branches and named branches is not that important in
504 a repo with 10 branches (no matter how big). But in the case of PyPy, we had
505 at that moment 1840 branches. Most are closed by now, of course. But we would
506 really like to retain (both now and in the future) the ability to look at a
507 commit from the past, and know in which branch it was made. There is a
508 difference between the Git and the Mercurial branches and that this is not
509 always possible with Git--- we looked hard, and there is no built-in way to get
510 this workflow.
511
512 Still not convinced? Consider this git repo with three commits: commit #2 with
513 parent #1 and head of git branch "A"; commit #3 with also parent #1 but head of
514 git branch "B". When commit #1 was made, was it in the branch "A" or "B"?
515 (It could also be yet another branch whose head was also moved forward, or even
516 completely deleted.)
517
518 Soon after this discussion, we discovered `git notes`_, which allow annotating
519 each git commit with a note indicating its origin branch. While this is not a
520 perfect solution, it does somewhat soften the blow.
521
522 Our development efforts pivoted to integrate PyPy into the Open Source project
523 space: PyPy began to be tested on many popular python projects, including
524 availability on conda-forge_. This meant it became important to lower friction
525 in the interactions with other development groups. It turns out most of them
526 use GitHub, which only uses Git. So at the end of 2023 we `moved to
527 Git/GitHub`_ for our main development. The repos that do not have as much
528 public interaction still remain at https://foss.heptapod.net/pypy/.
529
530
531 What is needed for better Windows 64 support of PyPy?
532 -----------------------------------------------------
533
534 As of PyPy 7.3.5, PyPy supports Windows 64-bits. Since only on that platform
535 ``sizeof(long) != sizeof(void *)``, and the underlying data type for RPython is
536 ``long``, this proved to be challenging. It seems we have crossed that bridge,
537 and welcome help in bringing the Windows version into parity with CPython. In
538 particular, we still do not support Windows-specific features like
539 ``winconsoleio``, windows audit events, and the Windows ``faulthandler``.
540 Performance may lag behind Linux64, and the ``wininstaller`` branch is still
541 unfinished.
542
543 Help is welcome!
544
545
546 How long will PyPy support Python2?
547 -----------------------------------
548
549 Since RPython is built on top of Python2 and that is extremely unlikely to
550 change, the Python2 version of PyPy will be around "forever", i.e. as long as
551 PyPy itself is around.
552
553
554 .. _`mercurial and bitbucket`: https://www.pypy.org/posts/2010/12/pypy-migrates-to-mercurial-3308736161543832134.html
555 .. _`git notes`: https://git-scm.com/docs/git-notes
556 .. _`conda-forge`: https://www.pypy.org/posts/2022/11/pypy-and-conda-forge.html
557 .. _`moved to Git/GitHub`: https://www.pypy.org/posts/2023/12/pypy-moved-to-git-github.html