Debian Codegraph @

rpython/doc/faq.rst

raw
1 Frequently Asked Questions
2 ==========================
3
4 .. contents::
5
6 See also: `Frequently ask questions about PyPy.`__
7
8 .. __: http://pypy.readthedocs.org/en/latest/faq.html
9
10 --------------------------
11
12 What is RPython?
13 ----------------
14
15 RPython is a framework for implementing interpreters and virtual machines for
16 programming languages, especially dynamic languages.
17
18
19 Can RPython compile normal Python programs to C?
20 ------------------------------------------------
21
22 No, RPython is not a Python compiler.
23
24 In Python, it is mostly impossible to *prove* anything about the types
25 that a program will manipulate by doing a static analysis. It should be
26 clear if you are familiar with Python, but if in doubt see [BRETT]_.
27
28 If you want a fast Python program, please use the PyPy :doc:`JIT <jit/index>` instead.
29
30 .. [BRETT] Brett Cannon,
31 Localized Type Inference of Atomic Types in Python,
32 http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.90.3231
33
34
35 .. _PyPy's RPython:
36
37 What is this RPython language?
38 ------------------------------
39
40 RPython is a restricted subset of the Python language. It is used for
41 implementing dynamic language interpreters within the PyPy toolchain. The
42 restrictions ensure that type inference (and so, ultimately, translation
43 to other languages) of RPython programs is possible.
44
45 The property of "being RPython" always applies to a full program, not to single
46 functions or modules (the translation toolchain does a full program analysis).
47 The translation toolchain follows all calls
48 recursively and discovers what belongs to the program and what does not.
49
50 RPython program restrictions mostly limit the ability
51 to mix types in arbitrary ways. RPython does not allow the binding of two
52 different types in the same variable. In this respect (and in some others) it
53 feels a bit like Java. Other features not allowed in RPython are the use of
54 special methods (``__xxx__``) except ``__init__`` and ``__del__``, and the
55 use of reflection capabilities (e.g. ``__dict__``).
56
57 You cannot use most existing standard library modules from RPython. The
58 exceptions are
59 some functions in ``os``, ``math`` and ``time`` that have native support.
60
61 To read more about the RPython limitations read the :doc:`RPython description <rpython>`.
62
63
64 Does RPython have anything to do with Zope's Restricted Python?
65 ---------------------------------------------------------------
66
67 No. `Zope's RestrictedPython`_ aims to provide a sandboxed
68 execution environment for CPython. `PyPy's RPython`_ is the implementation
69 language for dynamic language interpreters. However, PyPy also provides
70 a robust :ref:`sandboxed Python Interpreter <pypy:sandbox>`.
71
72 .. _Zope's RestrictedPython: http://pypi.python.org/pypi/RestrictedPython
73
74
75 What's the ``"NOT_RPYTHON"`` I see in some docstrings?
76 ------------------------------------------------------
77
78 If you put "NOT_RPYTHON" into the docstring of a function and that function is
79 found while trying to translate an RPython program, the translation process
80 stops and reports this as an error. You can therefore mark functions as
81 "NOT_RPYTHON" to make sure that they are never analyzed.
82
83 This method of marking a function as not RPython is outdated. For new code,
84 please use the ``@objectmodel.not_rpython`` decorator instead.
85
86
87 Couldn't we simply take a Python syntax tree and turn it into Lisp?
88 -------------------------------------------------------------------
89
90 It's not necessarily nonsense, but it's not really The PyPy Way. It's
91 pretty hard, without some kind of type inference, to translate this
92 Python::
93
94 a + b
95
96 into anything significantly more efficient than this Common Lisp::
97
98 (py:add a b)
99
100 And making type inference possible is what RPython is all about.
101
102 You could make ``#'py:add`` a generic function and see if a given CLOS
103 implementation is fast enough to give a useful speed (but I think the
104 coercion rules would probably drive you insane first). -- mwh
105
106
107 Do I have to rewrite my programs in RPython?
108 --------------------------------------------
109
110 No, and you shouldn't try. First and foremost, RPython is a language
111 designed for writing interpreters. It is a restricted subset of
112 Python. If your program is not an interpreter but tries to do "real
113 things", like use *any* part of the standard Python library or *any*
114 3rd-party library, then it is not RPython to start with. You should
115 only look at RPython if you try to `write your own interpreter`__.
116
117 .. __: `How do I compile my own interpreters?`_
118
119 If your goal is to speed up Python code, then look at the regular PyPy,
120 which is a full and compliant Python 2.7 interpreter (which happens to
121 be written in RPython). Not only is it not necessary for you to rewrite
122 your code in RPython, it might not give you any speed improvements even
123 if you manage to.
124
125 Yes, it is possible with enough effort to compile small self-contained
126 pieces of RPython code doing a few performance-sensitive things. But
127 this case is not interesting for us. If you needed to rewrite the code
128 in RPython, you could as well have rewritten it in C or C++ or Java for
129 example. These are much more supported, much more documented languages
130 `:-)`
131
132 *The above paragraphs are not the whole truth. It* is *true that there
133 are cases where writing a program as RPython gives you substantially
134 better speed than running it on top of PyPy. However, the attitude of
135 the core group of people behind PyPy is to answer: "then report it as a
136 performance bug against PyPy!".*
137
138 *Here is a more diluted way to put it. The "No, don't!" above is a
139 general warning we give to new people. They are likely to need a lot
140 of help from* some *source, because RPython is not so simple nor
141 extensively documented; but at the same time, we, the pypy core group
142 of people, are not willing to invest time in supporting 3rd-party
143 projects that do very different things than interpreters for dynamic
144 languages --- just because we have other interests and there are only
145 so many hours a day. So as a summary I believe it is only fair to
146 attempt to point newcomers at existing alternatives, which are more
147 mainstream and where they will get help from many people.*
148
149 *If anybody seriously wants to promote RPython anyway, they are welcome
150 to: we won't actively resist such a plan. There are a lot of things
151 that could be done to make RPython a better Java-ish language for
152 example, starting with supporting non-GIL-based multithreading, but we
153 don't implement them because they have little relevance to us. This
154 is open source, which means that anybody is free to promote and
155 develop anything; but it also means that you must let us choose* not
156 *to go into that direction ourselves.*
157
158
159 Which backends are there for the RPython toolchain?
160 ---------------------------------------------------
161
162 Currently, the only backend is :ref:`C <genc>`.
163 It can translate the entire PyPy interpreter.
164 To learn more about backends take a look at the :doc:`translation document <translation>`.
165
166
167 Could we use LLVM?
168 ------------------
169
170 In theory yes. But we tried to use it 5 or 6 times already, as a
171 translation backend or as a JIT backend --- and failed each time.
172
173 In more details: using LLVM as a (static) translation backend is
174 pointless nowadays because you can generate C code and compile it with
175 clang. (Note that compiling PyPy with clang gives a result that is not
176 faster than compiling it with gcc.) We might in theory get extra
177 benefits from LLVM's GC integration, but this requires more work on the
178 LLVM side before it would be remotely useful. Anyway, it could be
179 interfaced via a custom primitive in the C code. (The latest such
180 experimental backend is in the branch ``llvm-translation-backend``,
181 which can translate PyPy with or without the JIT on Linux.)
182
183 On the other hand, using LLVM as our JIT backend looks interesting as
184 well --- but again we made an attempt, and it failed: LLVM has no way to
185 patch the generated machine code, and is not suited at all to tracing
186 JITs. Even one big method JIT trying to use LLVM `has given up`__ for
187 similar reasons; read that blog post for more details.
188
189 .. __: https://webkit.org/blog/5852/introducing-the-b3-jit-compiler/
190
191 So the position of the core PyPy developers is that if anyone wants to
192 make an N+1'th attempt with LLVM, they are welcome, and they will receive a
193 bit of help on the IRC channel, but they are left with the burden of proof
194 that it works.
195
196
197 Compiling PyPy swaps or runs out of memory
198 ------------------------------------------
199
200 This is documented (here__ and here__). It needs 4 GB of RAM to run
201 "rpython targetpypystandalone" on top of PyPy, a bit more when running
202 on top of CPython. If you have less than 4 GB free, it will just swap
203 forever (or fail if you don't have enough swap). And we mean *free:*
204 if the machine has 4 GB *in total,* then it will swap.
205
206 On 32-bit, divide the numbers by two. (We didn't try recently, but in
207 the past it was possible to compile a 32-bit version on a 2 GB Linux
208 machine with nothing else running: no Gnome/KDE, for example.)
209
210 .. __: http://pypy.org/download.html#building-from-source
211 .. __: https://pypy.readthedocs.org/en/latest/getting-started-python.html#translating-the-pypy-python-interpreter
212
213
214 .. _compile-own-interpreters:
215
216 How do I compile my own interpreters?
217 -------------------------------------
218
219 Begin by reading `Andrew Brown's tutorial`_ .
220
221 .. _Andrew Brown's tutorial: https://www.pypy.org/posts/2011/04/tutorial-writing-interpreter-with-pypy-3785910476193156295.html
222
223
224 Can RPython modules for PyPy be translated independently?
225 ---------------------------------------------------------
226
227 No, you have to rebuild the entire interpreter. This means two things:
228
229 * It is imperative to use test-driven development. You have to exhaustively
230 test your module in pure Python, before even attempting to
231 translate it. Once you translate it, you should have only a few typing
232 issues left to fix, but otherwise the result should work out of the box.
233
234 * Second, and perhaps most important: do you have a really good reason
235 for writing the module in RPython in the first place? Nowadays you
236 should really look at alternatives, like writing it in pure Python,
237 using cffi_ if it needs to call C code.
238
239 In this context it is not that important to be able to translate
240 RPython modules independently of translating the complete interpreter.
241 (It could be done given enough efforts, but it's a really serious
242 undertaking. Consider it as quite unlikely for now.)
243
244 .. _cffi: http://cffi.readthedocs.org/
245
246
247 Why does the translator draw a Mandelbrot fractal while translating?
248 --------------------------------------------------------------------
249
250 Because it's fun.