Debian Codegraph @

pypy/doc/contributing.rst

raw
1 Contributing Guidelines
2 =======================
3
4 .. contents::
5
6 PyPy is a very large project that has a reputation of being hard to dive into.
7 Some of this fame is warranted, some of it is purely accidental. There are three
8 important lessons that everyone willing to contribute should learn:
9
10 * PyPy has layers. There are many pieces of architecture that are very well
11 separated from each other. More about this below, but often the manifestation
12 of this is that things are at a different layer than you would expect them
13 to be. For example if you are looking for the JIT implementation, you will
14 not find it in the implementation of the Python programming language.
15
16 * Because of the above, we are very serious about Test Driven Development.
17 It's not only what we believe in, but also that PyPy's architecture is
18 working very well with TDD in mind and not so well without it. Often
19 development means progressing in an unrelated corner, one unittest
20 at a time; and then flipping a giant switch, bringing it all together.
21 (It generally works out of the box. If it doesn't, then we didn't
22 write enough unit tests.) It's worth repeating - PyPy's
23 approach is great if you do TDD, and not so great otherwise.
24
25 * PyPy uses an entirely different set of tools - most of them included
26 in the PyPy repository. There is no Makefile, nor autoconf. More below.
27
28 The first thing to remember is that the PyPy project is very different than most
29 projects out there. It's also different from a classic compiler project,
30 so academic courses about compilers often don't apply or lead in the wrong
31 direction. However, if you want to understand how designing & building a runtime
32 works in the real world then this is a great project!
33
34
35 Getting involved
36 ^^^^^^^^^^^^^^^^
37
38 PyPy employs a relatively standard open-source development process. You are
39 encouraged as a first step to join our `pypy-dev mailing list`_ and IRC channel,
40 details of which can be found in our :ref:`contact <contact>` section. The folks
41 there are very friendly, and can point you in the right direction.
42
43 We give out commit rights usually fairly liberally, so if you want to do something
44 with PyPy, you can become a "developer" by logging into https://foss.heptapod.net
45 and clicking the "Request Access" link on the `PyPy group page`_. We also run
46 coding sprints which are separately announced and are usually announced on `the
47 blog`_.
48
49 Like any Open Source project, issues should be filed on the `issue tracker`_,
50 and `pull requests`_ to fix issues are welcome.
51
52 Further Reading: :ref:`Contact <contact>`
53
54 .. _the blog: https://pypy.org/blog
55 .. _pypy-dev mailing list: https://mail.python.org/mailman/listinfo/pypy-dev
56 .. _`PyPy group page`: https://github.com/pypy
57 .. _`pull requests`: https://github.com/pypy/pypy/pulls/
58
59
60 Your first contribution
61 ^^^^^^^^^^^^^^^^^^^^^^^
62
63 The first and most important rule how **not** to contribute to PyPy is
64 "just hacking a feature". This won't work, and you'll find your PR will typically
65 require a lot of re-work. There are a few reasons why not:
66
67 * build times are large
68 * PyPy has very thick layer separation
69 * context of the CPython runtime is often required
70
71 Instead, reach out on the dev mailing list or the IRC channel, and we're more
72 than happy to help! :)
73
74 Some ideas for first contributions are:
75
76 * Documentation - this will give you an understanding of the pypy architecture
77 * Test failures - find a failing test in the `nightly builds`_, and fix it
78 * Missing language features - these are listed in our `issue tracker`_
79
80 .. _nightly builds: https://buildbot.pypy.org/nightly/
81 .. _issue tracker: https://github.com/pypy/pypy/issues/
82
83
84 Source Control
85 --------------
86
87 PyPy's main git repositories are hosted here: https://github.com/pypy,
88 and legacy repositories are hosted here: https://foss.heptapod.net/pypy.
89
90 PyPy's legacy repositories are hosted on `Heptapod <https://heptapod.net/>`_.
91 Heptapod is a friendly fork of GitLab Community
92 Edition supporting Mercurial. https://foss.heptapod.net is a public instance
93 for Free and Open-Source Software (more information `here
94 <https://foss.heptapod.net/heptapod/foss.heptapod.net>`_).
95
96 Thanks to `Octobus <https://octobus.net/>`_ and `Clever Cloud
97 <https://www.clever-cloud.com>`_ for providing this service!
98
99 .. raw:: html
100
101 <h1 align="center">
102 <a href="https://foss.heptapod.net/heptapod/foss.heptapod.net">
103 <img width="500" alt="Octobus + Clever Cloud"
104 src="https://foss.heptapod.net/heptapod/slides/2020-FOSDEM/raw/branch/default/octobus+clever.png"
105 >
106 </a>
107 </h1>
108
109
110 Clone
111 -----
112
113 * Clone the PyPy repo to your local machine with the command
114 ``git clone https://github.com/pypy/pypy.git``. It takes a minute or two
115 but only ever needs to be done once. See also
116 https://pypy.org/download.html#building-from-source .
117
118 * Now you have a complete copy of the PyPy repo.
119
120
121 Edit
122 ----
123
124 * Edit things. Use ``git diff`` to see what you changed. Use ``git add``
125 to make git aware of new files you added, e.g. new test files.
126 Use ``git status`` to see if there are such files. Write and run tests!
127 (See the rest of this page.)
128
129 * Commit regularly with ``git commit``. A one-line commit message is
130 fine. We love to have tons of commits; make one as soon as you have
131 some progress, even if it is only some new test that doesn't pass yet,
132 or fixing things even if not all tests pass. Step by step, you are
133 building the history of your changes, which is the point of a version
134 control system. (There are commands like ``git log``
135 that you should read about later, to learn how to navigate this
136 history.)
137
138 * The commits stay on your machine until you do ``git push`` to "push"
139 them back to your fork. The commands ``git push`` and ``git pull`` copy
140 commits around, with the goal that all repos in question end up with
141 the exact same set of commits.
142
143 * You should push often; there is no real reason not to. Remember that
144 even if they are pushed, with the setup above, the commits are only in the
145 branch you
146 named. Yes, they are publicly visible, but don't worry about someone
147 walking around the many branches of PyPy saying "hah, look
148 at the bad coding style of that person". Try to get into the mindset
149 that your work is not secret and it's fine that way. We might not
150 accept it as is for PyPy, asking you instead to improve some things,
151 but we are not going to judge you unless you don't write tests.
152
153
154 Pull Request
155 -------------
156
157 * The final step is to open a pull request, so that we know that you'd
158 like to merge that branch back to the original ``pypy/pypy`` repo.
159 This can also be done several times if you have interesting
160 intermediate states, but if you get there, then we're likely to
161 proceed to the next stage, which is...
162
163 * If you get closer to the regular day-to-day development, you'll notice
164 that we generally push small changes as one or a few commits directly
165 to the branch ``default`` or ``py3.9``. Also, we often collaborate even if
166 we are on other branches, which do not really "belong" to anyone. At this
167 point you'll need ``git merge`` and learn how to resolve conflicts that
168 sometimes occur when two people try to push different commits in
169 parallel on the same branch. But it is likely an issue for later ``:-)``
170
171
172 Architecture
173 ^^^^^^^^^^^^
174
175 PyPy has layers. Just like ogres or onions. Those layers help us keep the
176 respective parts separated enough to be worked on independently and make the
177 complexity manageable. This is, again, just a sanity requirement for such
178 a complex project. For example writing a new optimization for the JIT usually
179 does **not** involve touching a Python interpreter at all or the JIT assembler
180 backend or the garbage collector. Instead it requires writing small tests in
181 ``rpython/jit/metainterp/optimizeopt/test/test_*`` and fixing files there.
182 After that, you can just compile PyPy and things should just work.
183
184 Further Reading: :doc:`architecture <architecture>`
185
186
187 Where to start?
188 ---------------
189
190 PyPy is made from parts that are relatively independent of each other.
191 You should start looking at the part that attracts you most (all paths are
192 relative to the PyPy top level directory). You may look at our
193 :doc:`directory reference <dir-reference>` or start off at one of the following
194 points:
195
196 * :source:`pypy/interpreter` contains the bytecode interpreter: bytecode dispatcher
197 in :source:`pypy/interpreter/pyopcode.py`, frame and code objects in
198 :source:`pypy/interpreter/eval.py` and :source:`pypy/interpreter/pyframe.py`,
199 function objects and argument passing in :source:`pypy/interpreter/function.py`
200 and :source:`pypy/interpreter/argument.py`, the object space interface
201 definition in :source:`pypy/interpreter/baseobjspace.py`, modules in
202 :source:`pypy/interpreter/module.py` and :source:`pypy/interpreter/mixedmodule.py`.
203 Core types supporting the bytecode interpreter are defined in
204 :source:`pypy/interpreter/typedef.py`.
205
206 * :source:`pypy/interpreter/pyparser` contains a recursive descent parser,
207 and grammar files that allow it to parse the syntax of various Python
208 versions. Once the grammar has been processed, the parser can be
209 translated by the above machinery into efficient code.
210
211 * :source:`pypy/interpreter/astcompiler` contains the compiler. This
212 contains a modified version of the compiler package from CPython
213 that fixes some bugs and is translatable.
214
215 * :source:`pypy/objspace/std` contains the
216 :ref:`Standard object space <standard-object-space>`. The main file
217 is :source:`pypy/objspace/std/objspace.py`. For each type, the file
218 ``xxxobject.py`` contains the implementation for objects of type ``xxx``,
219 as a first approximation. (Some types have multiple implementations.)
220
221
222 Building
223 ^^^^^^^^
224
225 For building PyPy, we recommend installing a pre-built PyPy first (see
226 :doc:`install`). It is possible to build PyPy with CPython, but it will take a
227 lot longer to run -- depending on your architecture, between two and three
228 times as long.
229
230 Further Reading: :doc:`Build <build>`
231
232
233 Coding Guide
234 ------------
235
236 As well as the usual :pep:`8` and formatting standards, there are a number of
237 naming conventions and coding styles that are important to understand before
238 browsing the source.
239
240 Further Reading: :doc:`Coding Guide <coding-guide>`
241
242
243 Testing
244 ^^^^^^^
245
246 Test driven development
247 -----------------------
248
249 Instead, we practice a lot of test driven development. This is partly because
250 of very high quality requirements for compilers and partly because there is
251 simply no other way to get around such a complex project, that will keep you sane.
252 There are probably people out there who are smart enough not to need it, we're
253 not one of those. You may consider familiarizing yourself with `pytest`_,
254 since this is a tool we use for tests. We ship our own tweaked version of
255 pytest in the top of the tree, so ``python -m pytest`` will pick up our version,
256 which means our tests need to run with that version of pytest.
257
258 We also have post-translation tests in the ``extra_tests`` directory that are
259 run in a virtual environment from a separate directory, so they use a more
260 up-to-date version of pytest. As much as possible, these are meant to
261 pass with CPython as well.
262
263 .. _pytest: https://pytest.org/
264
265
266 Running PyPy's unit tests
267 -------------------------
268
269 PyPy development always was and is still thoroughly test-driven.
270 There are two modes of tests: those that run on top of RPython before
271 translation (untranslated tests) and those that run on top of a translated
272 ``pypy`` (app tests). Since RPython is a dialect of Python2, the untranslated
273 tests run with a python2 host.
274
275 The PyPy source tree comes with an inlined version of ``py.test``
276 which you can invoke by typing::
277
278 python2 pytest.py -h
279
280 You will need the `build requirements`_ to run tests successfully, since many of
281 them compile little pieces of PyPy and then run the tests inside that minimal
282 interpreter. The ``cpyext`` tests also require ``pycparser``, and many tests build
283 cases with ``hypothesis``.
284
285 Now on to running some tests. PyPy has many different test directories
286 and you can use shell completion to point at directories or files::
287
288 python2 pytest.py pypy/interpreter/test/test_pyframe.py
289
290 # or for running tests of a whole subdirectory
291 python2 pytest.py pypy/interpreter/
292
293 Beware trying to run "all" pypy tests by pointing to the root
294 directory or even the top level subdirectory ``pypy``. It takes
295 hours and uses huge amounts of RAM and is not recommended.
296
297 To run CPython regression tests, you should start with a translated PyPy and
298 run the tests as you would with CPython (see below). You can, however, also
299 attempt to run the tests before translation, but be aware that it is done with
300 a hack that doesn't work in all cases and it is usually extremely slow:
301 ``py.test lib-python/2.7/test/test_datetime.py``. Usually, a better idea is to
302 extract a minimal failing test of at most a few lines, and put it into one of
303 our own tests in ``pypy/*/test/``.
304
305 .. _`build requirements`: build.html#install-build-time-dependencies
306
307
308 App level testing
309 -----------------
310
311 While the usual invocation of ``python2 pytest.py`` runs app-level tests on an
312 untranslated PyPy that runs on top of python2, we have a test extension to run tests
313 directly on the host python. This is very convenient for modules such as
314 ``cpyext``, to compare and contrast test results between CPython and PyPy.
315
316 App-level tests (ones whose file name start with ``apptest_`` not ``test_``)
317 run directly on the host interpreter when passing ``-D`` or
318 ``--direct-apptest`` to ``pytest``::
319
320 pypy3 -m pytest -D pypy/interpreter/test/apptest_pyframe.py
321
322 Mixed-level tests (the usual ones that start with ``test_``) are invoked by using the ``-A`` or ``--runappdirect`` option to
323 ``pytest``::
324
325 python2 pytest.py -A pypy/module/cpyext/test
326
327 where ``python2`` can be either ``python2`` or ``pypy2``. On any of the ``py3*`` branches, the
328 collection phase must be run with ``python2`` so untranslated tests are run
329 with::
330
331 python2 pytest.py -A pypy/module/cpyext/test --python=path/to/pypy3
332
333
334 Testing After Translation
335 -------------------------
336
337 If you run translation, you will end up with a binary named ``pypy-c`` (or
338 ``pypy3-c`` for the Python3 branches) in the directory where you ran the
339 translation.
340
341 To run a test from the standard CPython regression test suite, use the regular
342 Python way, i.e. (use the exact binary name)::
343
344 ./pypy3-c -m test.test_datetime
345 # or
346 ./pypy3-c lib-python/3/test/test_audit.py
347
348
349 Buildbot
350 --------
351
352 PyPy runs a buildbot-based CI system at https://buildbot.pypy.org. This is
353 driven by the code at https://foss.heptapod.net/pypy/buildbot. The Linux
354 runners on x86_64, i686, and aarch64 use a docker container, which manages
355 dependencies. See the Dockerfile_ for more info. The Windows runner uses
356 dependencies from the ``win64_14x`` branch of the externals_ repo. The macOS
357 runners (x86_64, arm64), use a venv on a M1 machine.
358
359 .. _Dockerfile: https://foss.heptapod.net/pypy/buildbot/-/tree/branch/default/docker
360 .. _externals: https://foss.heptapod.net/pypy/externals
361
362
363 Tooling & Utilities
364 ^^^^^^^^^^^^^^^^^^^
365
366 If you are interested in the inner workings of the PyPy Python interpreter,
367 there are some features of the untranslated Python interpreter that allow you
368 to introspect its internals.
369
370
371 Interpreter-level console
372 -------------------------
373
374 To start interpreting Python with PyPy, install a C compiler that is
375 supported by distutils and use Python 2.7 or greater to run PyPy::
376
377 cd pypy
378 python bin/pyinteractive.py
379
380 After a few seconds (remember: this is running on top of CPython), you should
381 be at the PyPy prompt, which is the same as the Python prompt, but with an
382 extra ">".
383
384 If you press
385 <Ctrl-C> on the console you enter the interpreter-level console, a
386 usual CPython console. You can then access internal objects of PyPy
387 (e.g. the :ref:`object space <objspace>`) and any variables you have created on the PyPy
388 prompt with the prefix ``w_``::
389
390 >>>> a = 123
391 >>>> <Ctrl-C>
392 *** Entering interpreter-level console ***
393 >>> w_a
394 W_IntObject(123)
395
396 The mechanism works in both directions. If you define a variable with the ``w_`` prefix on the interpreter-level, you will see it on the app-level::
397
398 >>> w_l = space.newlist([space.wrap(1), space.wrap("abc")])
399 >>> <Ctrl-D>
400 *** Leaving interpreter-level console ***
401
402 KeyboardInterrupt
403 >>>> l
404 [1, 'abc']
405
406 Note that the prompt of the interpreter-level console is only '>>>' since
407 it runs on CPython level. If you want to return to PyPy, press <Ctrl-D> (under
408 Linux) or <Ctrl-Z>, <Enter> (under Windows).
409
410 Also note that not all modules are available by default in this mode (for
411 example: ``_continuation`` needed by ``greenlet``), you may need to use one of
412 ``--withmod-...`` command line options.
413
414 You may be interested in reading more about the distinction between
415 :ref:`interpreter-level and app-level <interpreter-level>`.
416
417
418 pyinteractive.py options
419 ------------------------
420
421 To list the PyPy interpreter command line options, type::
422
423 cd pypy
424 python bin/pyinteractive.py --help
425
426 pyinteractive.py supports most of the options that CPython supports too (in addition to a
427 large amount of options that can be used to customize pyinteractive.py).
428 As an example of using PyPy from the command line, you could type::
429
430 python pyinteractive.py --withmod-time -c "from test import pystone; pystone.main(10)"
431
432 Alternatively, as with regular Python, you can simply give a
433 script name on the command line::
434
435 python pyinteractive.py --withmod-time ../../lib-python/2.7/test/pystone.py 10
436
437 The ``--withmod-xxx`` option enables the built-in module ``xxx``. By
438 default almost none of them are, because initializing them takes time.
439 If you want anyway to enable all built-in modules, you can use
440 ``--allworkingmodules``.
441
442 See our :doc:`configuration sections <config/index>` for details about what all the commandline
443 options do.
444
445
446 .. _trace example:
447
448 Tracing bytecode and operations on objects
449 ------------------------------------------
450
451 You can use a simple tracing mode to monitor the interpretation of
452 bytecodes. To enable it, set ``__pytrace__ = 1`` on the interactive
453 PyPy console::
454
455 >>>> __pytrace__ = 1
456 Tracing enabled
457 >>>> x = 5
458 <module>: LOAD_CONST 0 (5)
459 <module>: STORE_NAME 0 (x)
460 <module>: LOAD_CONST 1 (None)
461 <module>: RETURN_VALUE 0
462 >>>> x
463 <module>: LOAD_NAME 0 (x)
464 <module>: PRINT_EXPR 0
465 5
466 <module>: LOAD_CONST 0 (None)
467 <module>: RETURN_VALUE 0
468 >>>>
469
470
471 Demos
472 ^^^^^
473
474 The `example-interpreter`_ repository contains an example interpreter
475 written using the RPython translation toolchain.
476
477 .. _example-interpreter: https://foss.heptapod.net/pypy/example-interpreter
478
479
480 graphviz & pygame for flow graph viewing (highly recommended)
481 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
482
483 graphviz and pygame are both necessary if you want to look at generated flow
484 graphs:
485
486 graphviz: https://www.graphviz.org/Download.php
487
488 pygame: https://www.pygame.org/download.shtml
489