| 1 |
Coding Guide |
| 2 |
============ |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
|
| 6 |
This document describes coding requirements and conventions for |
| 7 |
working with the PyPy code base. Please read it carefully and |
| 8 |
ask back any questions you might have. The document does not talk |
| 9 |
very much about coding style issues. We mostly follow :pep:`8` though. |
| 10 |
If in doubt, follow the style that is already present in the code base. |
| 11 |
|
| 12 |
|
| 13 |
Overview and motivation |
| 14 |
------------------------ |
| 15 |
|
| 16 |
We are writing a Python interpreter in Python, using Python's well known |
| 17 |
ability to step behind the algorithmic problems as a language. At first glance, |
| 18 |
one might think this achieves nothing but a better understanding how the |
| 19 |
interpreter works. This alone would make it worth doing, but we have much |
| 20 |
larger goals. |
| 21 |
|
| 22 |
|
| 23 |
CPython vs. PyPy |
| 24 |
~~~~~~~~~~~~~~~~ |
| 25 |
|
| 26 |
Compared to the CPython implementation, Python takes the role of the C |
| 27 |
Code. We rewrite the CPython interpreter in Python itself. We could |
| 28 |
also aim at writing a more flexible interpreter at C level but we |
| 29 |
want to use Python to give an alternative description of the interpreter. |
| 30 |
|
| 31 |
The clear advantage is that such a description is shorter and simpler to |
| 32 |
read, and many implementation details vanish. The drawback of this approach is |
| 33 |
that this interpreter will be unbearably slow as long as it is run on top |
| 34 |
of CPython. |
| 35 |
|
| 36 |
To get to a useful interpreter again, we need to translate our |
| 37 |
high-level description of Python to a lower level one. One rather |
| 38 |
straight-forward way is to do a whole program analysis of the PyPy |
| 39 |
interpreter and create a C source, again. There are many other ways, |
| 40 |
but let's stick with this somewhat canonical approach. |
| 41 |
|
| 42 |
|
| 43 |
.. _application-level: |
| 44 |
.. _interpreter-level: |
| 45 |
|
| 46 |
Application-level and interpreter-level execution and objects |
| 47 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 |
|
| 49 |
Since Python is used for implementing all of our code base, there is a |
| 50 |
crucial distinction to be aware of: that between *interpreter-level* objects and |
| 51 |
*application-level* objects. The latter are the ones that you deal with |
| 52 |
when you write normal python programs. Interpreter-level code, however, |
| 53 |
cannot invoke operations nor access attributes from application-level |
| 54 |
objects. You will immediately recognize any interpreter level code in |
| 55 |
PyPy, because half the variable and object names start with a ``w_``, which |
| 56 |
indicates that they are `wrapped`_ application-level values. |
| 57 |
|
| 58 |
Let's show the difference with a simple example. To sum the contents of |
| 59 |
two variables ``a`` and ``b``, one would write the simple application-level |
| 60 |
``a+b`` -- in contrast, the equivalent interpreter-level code is |
| 61 |
``space.add(w_a, w_b)``, where ``space`` is an instance of an object space, |
| 62 |
and ``w_a`` and ``w_b`` are typical names for the wrapped versions of the |
| 63 |
two variables. |
| 64 |
|
| 65 |
It helps to remember how CPython deals with the same issue: interpreter |
| 66 |
level code, in CPython, is written in C and thus typical code for the |
| 67 |
addition is ``PyNumber_Add(p_a, p_b)`` where ``p_a`` and ``p_b`` are C |
| 68 |
variables of type ``PyObject*``. This is conceptually similar to how we write |
| 69 |
our interpreter-level code in Python. |
| 70 |
|
| 71 |
Moreover, in PyPy we have to make a sharp distinction between |
| 72 |
interpreter- and application-level *exceptions*: application exceptions |
| 73 |
are always contained inside an instance of ``OperationError``. This |
| 74 |
makes it easy to distinguish failures (or bugs) in our interpreter-level code |
| 75 |
from failures appearing in a python application level program that we are |
| 76 |
interpreting. |
| 77 |
|
| 78 |
|
| 79 |
.. _app-preferable: |
| 80 |
|
| 81 |
Application level is often preferable |
| 82 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 83 |
|
| 84 |
Application-level code is substantially higher-level, and therefore |
| 85 |
correspondingly easier to write and debug. For example, suppose we want |
| 86 |
to implement the ``update`` method of dict objects. Programming at |
| 87 |
application level, we can write an obvious, simple implementation, one |
| 88 |
that looks like an **executable definition** of ``update``, for |
| 89 |
example:: |
| 90 |
|
| 91 |
def update(self, other): |
| 92 |
for k in other.keys(): |
| 93 |
self[k] = other[k] |
| 94 |
|
| 95 |
If we had to code only at interpreter level, we would have to code |
| 96 |
something much lower-level and involved, say something like:: |
| 97 |
|
| 98 |
def update(space, w_self, w_other): |
| 99 |
w_keys = space.call_method(w_other, 'keys') |
| 100 |
w_iter = space.iter(w_keys) |
| 101 |
while True: |
| 102 |
try: |
| 103 |
w_key = space.next(w_iter) |
| 104 |
except OperationError as e: |
| 105 |
if not e.match(space, space.w_StopIteration): |
| 106 |
raise # re-raise other app-level exceptions |
| 107 |
break |
| 108 |
w_value = space.getitem(w_other, w_key) |
| 109 |
space.setitem(w_self, w_key, w_value) |
| 110 |
|
| 111 |
This interpreter-level implementation looks much more similar to the C |
| 112 |
source code. It is still more readable than its C counterpart because |
| 113 |
it doesn't contain memory management details and can use Python's native |
| 114 |
exception mechanism. |
| 115 |
|
| 116 |
In any case, it should be obvious that the application-level implementation |
| 117 |
is definitely more readable, more elegant and more maintainable than the |
| 118 |
interpreter-level one (and indeed, dict.update is really implemented at |
| 119 |
applevel in PyPy). |
| 120 |
|
| 121 |
In fact, in almost all parts of PyPy, you find application level code in |
| 122 |
the middle of interpreter-level code. Apart from some bootstrapping |
| 123 |
problems (application level functions need a certain initialization |
| 124 |
level of the object space before they can be executed), application |
| 125 |
level code is usually preferable. We have an abstraction (called the |
| 126 |
'Gateway') which allows the caller of a function to remain ignorant of |
| 127 |
whether a particular function is implemented at application or |
| 128 |
interpreter level. |
| 129 |
|
| 130 |
|
| 131 |
Our runtime interpreter is "RPython" |
| 132 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 133 |
|
| 134 |
In order to make a C code generator feasible all code on interpreter level has |
| 135 |
to restrict itself to a subset of the Python language, and we adhere to some |
| 136 |
rules which make translation to lower level languages feasible. Code on |
| 137 |
application level can still use the full expressivity of Python. |
| 138 |
|
| 139 |
Unlike source-to-source translations (like e.g. Starkiller_ or more recently |
| 140 |
ShedSkin_) we start |
| 141 |
translation from live python code objects which constitute our Python |
| 142 |
interpreter. When doing its work of interpreting bytecode our Python |
| 143 |
implementation must behave in a static way often referenced as |
| 144 |
"RPythonic". |
| 145 |
|
| 146 |
.. _Starkiller: https://people.csail.mit.edu/jrb/Projects/starkiller.pdf |
| 147 |
.. _ShedSkin: https://shed-skin.blogspot.com/ |
| 148 |
|
| 149 |
However, when the PyPy interpreter is started as a Python program, it |
| 150 |
can use all of the Python language until it reaches a certain point in |
| 151 |
time, from which on everything that is being executed must be static. |
| 152 |
That is, during initialization our program is free to use the |
| 153 |
full dynamism of Python, including dynamic code generation. |
| 154 |
|
| 155 |
An example can be found in the current implementation which is quite |
| 156 |
elegant: For the definition of all the opcodes of the Python |
| 157 |
interpreter, the module ``dis`` is imported and used to initialize our |
| 158 |
bytecode interpreter. (See ``__initclass__`` in |
| 159 |
:source:`pypy/interpreter/pyopcode.py`). This |
| 160 |
saves us from adding extra modules to PyPy. The import code is run at |
| 161 |
startup time, and we are allowed to use the CPython builtin import |
| 162 |
function. |
| 163 |
|
| 164 |
After the startup code is finished, all resulting objects, functions, |
| 165 |
code blocks etc. must adhere to certain runtime restrictions which we |
| 166 |
describe further below. Here is some background for why this is so: |
| 167 |
during translation, a whole program analysis ("type inference") is |
| 168 |
performed, which makes use of the restrictions defined in RPython. This |
| 169 |
enables the code generator to emit efficient machine level replacements |
| 170 |
for pure integer objects, for instance. |
| 171 |
|
| 172 |
.. _wrapped: |
| 173 |
|
| 174 |
Wrapping rules |
| 175 |
-------------- |
| 176 |
|
| 177 |
Wrapping |
| 178 |
~~~~~~~~ |
| 179 |
|
| 180 |
PyPy is made of Python source code at two levels: there is on the one hand |
| 181 |
*application-level code* that looks like normal Python code, and that |
| 182 |
implements some functionalities as one would expect from Python code (e.g. one |
| 183 |
can give a pure Python implementation of some built-in functions like |
| 184 |
``zip()``). There is also *interpreter-level code* for the functionalities |
| 185 |
that must more directly manipulate interpreter data and objects (e.g. the main |
| 186 |
loop of the interpreter, and the various object spaces). |
| 187 |
|
| 188 |
Application-level code doesn't see object spaces explicitly: it runs using an |
| 189 |
object space to support the objects it manipulates, but this is implicit. |
| 190 |
There is no need for particular conventions for application-level code. The |
| 191 |
sequel is only about interpreter-level code. (Ideally, no application-level |
| 192 |
variable should be called ``space`` or ``w_xxx`` to avoid confusion.) |
| 193 |
|
| 194 |
The ``w_`` prefixes so lavishly used in the example above indicate, |
| 195 |
by PyPy coding convention, that we are dealing with *wrapped* (or *boxed*) objects, |
| 196 |
that is, interpreter-level objects which the object space constructs |
| 197 |
to implement corresponding application-level objects. Each object |
| 198 |
space supplies ``wrap``, ``unwrap``, ``int_w``, ``interpclass_w``, |
| 199 |
etc. operations that move between the two levels for objects of simple |
| 200 |
built-in types; each object space also implements other Python types |
| 201 |
with suitable interpreter-level classes with some amount of internal |
| 202 |
structure. |
| 203 |
|
| 204 |
For example, an application-level Python ``list`` |
| 205 |
is implemented by the :ref:`standard object space <standard-object-space>` as an |
| 206 |
instance of ``W_ListObject``, which has an instance attribute |
| 207 |
``wrappeditems`` (an interpreter-level list which contains the |
| 208 |
application-level list's items as wrapped objects). |
| 209 |
|
| 210 |
The rules are described in more details below. |
| 211 |
|
| 212 |
|
| 213 |
Naming conventions |
| 214 |
~~~~~~~~~~~~~~~~~~ |
| 215 |
|
| 216 |
* ``space``: the object space is only visible at |
| 217 |
interpreter-level code, where it is by convention passed around by the name |
| 218 |
``space``. |
| 219 |
|
| 220 |
* ``w_xxx``: any object seen by application-level code is an |
| 221 |
object explicitly managed by the object space. From the |
| 222 |
interpreter-level point of view, this is called a *wrapped* |
| 223 |
object. The ``w_`` prefix is used for any type of |
| 224 |
application-level object. |
| 225 |
|
| 226 |
* ``xxx_w``: an interpreter-level container for wrapped |
| 227 |
objects, for example a list or a dict containing wrapped |
| 228 |
objects. Not to be confused with a wrapped object that |
| 229 |
would be a list or a dict: these are normal wrapped objects, |
| 230 |
so they use the ``w_`` prefix. |
| 231 |
|
| 232 |
|
| 233 |
Operations on ``w_xxx`` |
| 234 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 235 |
|
| 236 |
The core bytecode interpreter considers wrapped objects as black boxes. |
| 237 |
It is not allowed to inspect them directly. The allowed |
| 238 |
operations are all implemented on the object space: they are |
| 239 |
called ``space.xxx()``, where ``xxx`` is a standard operation |
| 240 |
name (``add``, ``getattr``, ``call``, ``eq``...). They are documented in the |
| 241 |
:ref:`object space document <objspace-interface>`. |
| 242 |
|
| 243 |
A short warning: **don't do** ``w_x == w_y`` or ``w_x is w_y``! |
| 244 |
rationale for this rule is that there is no reason that two |
| 245 |
wrappers are related in any way even if they contain what |
| 246 |
looks like the same object at application-level. To check |
| 247 |
for equality, use ``space.is_true(space.eq(w_x, w_y))`` or |
| 248 |
even better the short-cut ``space.eq_w(w_x, w_y)`` returning |
| 249 |
directly a interpreter-level bool. To check for identity, |
| 250 |
use ``space.is_true(space.is_(w_x, w_y))`` or better |
| 251 |
``space.is_w(w_x, w_y)``. |
| 252 |
|
| 253 |
|
| 254 |
.. _applevel-exceptions: |
| 255 |
|
| 256 |
Application-level exceptions |
| 257 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 258 |
|
| 259 |
Interpreter-level code can use exceptions freely. However, |
| 260 |
all application-level exceptions are represented as an |
| 261 |
``OperationError`` at interpreter-level. In other words, all |
| 262 |
exceptions that are potentially visible at application-level |
| 263 |
are internally an ``OperationError``. This is the case of all |
| 264 |
errors reported by the object space operations |
| 265 |
(``space.add()`` etc.). |
| 266 |
|
| 267 |
To raise an application-level exception:: |
| 268 |
|
| 269 |
from pypy.interpreter.error import oefmt |
| 270 |
|
| 271 |
raise oefmt(space.w_XxxError, "message") |
| 272 |
|
| 273 |
raise oefmt(space.w_XxxError, "file '%s' not found in '%s'", filename, dir) |
| 274 |
|
| 275 |
raise oefmt(space.w_XxxError, "file descriptor '%d' not open", fd) |
| 276 |
|
| 277 |
To catch a specific application-level exception:: |
| 278 |
|
| 279 |
try: |
| 280 |
... |
| 281 |
except OperationError as e: |
| 282 |
if not e.match(space, space.w_XxxError): |
| 283 |
raise |
| 284 |
... |
| 285 |
|
| 286 |
This construct catches all application-level exceptions, so we |
| 287 |
have to match it against the particular ``w_XxxError`` we are |
| 288 |
interested in and re-raise other exceptions. The exception |
| 289 |
instance ``e`` holds two attributes that you can inspect: |
| 290 |
``e.w_type`` and ``e.w_value``. Do not use ``e.w_type`` to |
| 291 |
match an exception, as this will miss exceptions that are |
| 292 |
instances of subclasses. |
| 293 |
|
| 294 |
|
| 295 |
.. _modules: |
| 296 |
|
| 297 |
Modules in PyPy |
| 298 |
--------------- |
| 299 |
|
| 300 |
Modules visible from application programs are imported from |
| 301 |
interpreter or application level files. PyPy reuses almost all python |
| 302 |
modules of CPython's standard library, currently from version 2.7.8. We |
| 303 |
sometimes need to `modify modules`_ and - more often - regression tests |
| 304 |
because they rely on implementation details of CPython. |
| 305 |
|
| 306 |
If we don't just modify an original CPython module but need to rewrite |
| 307 |
it from scratch we put it into :source:`lib_pypy/` as a pure application level |
| 308 |
module. |
| 309 |
|
| 310 |
When we need access to interpreter-level objects we put the module into |
| 311 |
:source:`pypy/module`. Such modules use a `mixed module mechanism`_ |
| 312 |
which makes it convenient to use both interpreter- and application-level parts |
| 313 |
for the implementation. Note that there is no extra facility for |
| 314 |
pure-interpreter level modules, you just write a mixed module and leave the |
| 315 |
application-level part empty. |
| 316 |
|
| 317 |
|
| 318 |
Determining the location of a module implementation |
| 319 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 320 |
|
| 321 |
You can interactively find out where a module comes from, when running py.py. |
| 322 |
here are examples for the possible locations:: |
| 323 |
|
| 324 |
>>>> import sys |
| 325 |
>>>> sys.__file__ |
| 326 |
'/home/hpk/pypy-dist/pypy/module/sys' |
| 327 |
|
| 328 |
>>>> import cPickle |
| 329 |
>>>> cPickle.__file__ |
| 330 |
'/home/hpk/pypy-dist/lib_pypy/cPickle..py' |
| 331 |
|
| 332 |
>>>> import os |
| 333 |
>>>> os.__file__ |
| 334 |
'/home/hpk/pypy-dist/lib-python/2.7/os.py' |
| 335 |
>>>> |
| 336 |
|
| 337 |
|
| 338 |
Module directories / Import order |
| 339 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 340 |
|
| 341 |
Here is the order in which PyPy looks up Python modules: |
| 342 |
|
| 343 |
*pypy/module* |
| 344 |
|
| 345 |
mixed interpreter/app-level builtin modules, such as |
| 346 |
the ``sys`` and ``__builtin__`` module. |
| 347 |
|
| 348 |
*contents of PYTHONPATH* |
| 349 |
|
| 350 |
lookup application level modules in each of the ``:`` separated |
| 351 |
list of directories, specified in the ``PYTHONPATH`` environment |
| 352 |
variable. |
| 353 |
|
| 354 |
*lib_pypy/* |
| 355 |
|
| 356 |
contains pure Python reimplementation of modules. |
| 357 |
|
| 358 |
*lib-python/2.7/* |
| 359 |
|
| 360 |
The modified CPython library. |
| 361 |
|
| 362 |
|
| 363 |
.. _modify modules: |
| 364 |
|
| 365 |
Modifying a CPython library module or regression test |
| 366 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 367 |
|
| 368 |
Although PyPy is very compatible with CPython we sometimes need |
| 369 |
to change modules contained in our copy of the standard library, |
| 370 |
often due to the fact that PyPy works with all new-style classes |
| 371 |
by default and CPython has a number of places where it relies |
| 372 |
on some classes being old-style. |
| 373 |
|
| 374 |
We just maintain those changes in place, |
| 375 |
to see what is changed we have a branch called `vendor/stdlib` |
| 376 |
which contains the unmodified cpython stdlib |
| 377 |
|
| 378 |
|
| 379 |
.. _mixed module mechanism: |
| 380 |
.. _mixed-modules: |
| 381 |
|
| 382 |
Implementing a mixed interpreter/application level Module |
| 383 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 384 |
|
| 385 |
If a module needs to access PyPy's interpreter level |
| 386 |
then it is implemented as a mixed module. |
| 387 |
|
| 388 |
Mixed modules are directories in :source:`pypy/module` with an `__init__.py` |
| 389 |
file containing specifications where each name in a module comes from. |
| 390 |
Only specified names will be exported to a Mixed Module's applevel |
| 391 |
namespace. |
| 392 |
|
| 393 |
Sometimes it is necessary to really write some functions in C (or whatever |
| 394 |
target language). See rffi_ details. |
| 395 |
|
| 396 |
.. _rffi: https://rpython.readthedocs.org/en/latest/rffi.html |
| 397 |
|
| 398 |
application level definitions |
| 399 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 400 |
|
| 401 |
Application level specifications are found in the `appleveldefs` |
| 402 |
dictionary found in ``__init__.py`` files of directories in ``pypy/module``. |
| 403 |
For example, in :source:`pypy/module/__builtin__/__init__.py` you find the following |
| 404 |
entry specifying where ``__builtin__.locals`` comes from:: |
| 405 |
|
| 406 |
... |
| 407 |
'locals' : 'app_inspect.locals', |
| 408 |
... |
| 409 |
|
| 410 |
The ``app_`` prefix indicates that the submodule ``app_inspect`` is |
| 411 |
interpreted at application level and the wrapped function value for ``locals`` |
| 412 |
will be extracted accordingly. |
| 413 |
|
| 414 |
|
| 415 |
interpreter level definitions |
| 416 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 417 |
|
| 418 |
Interpreter level specifications are found in the ``interpleveldefs`` |
| 419 |
dictionary found in ``__init__.py`` files of directories in ``pypy/module``. |
| 420 |
For example, in :source:`pypy/module/__builtin__/__init__.py` the following |
| 421 |
entry specifies where ``__builtin__.len`` comes from:: |
| 422 |
|
| 423 |
... |
| 424 |
'len' : 'operation.len', |
| 425 |
... |
| 426 |
|
| 427 |
The ``operation`` submodule lives at interpreter level and ``len`` |
| 428 |
is expected to be exposable to application level. Here is |
| 429 |
the definition for ``operation.len()``:: |
| 430 |
|
| 431 |
def len(space, w_obj): |
| 432 |
"len(object) -> integer\n\nReturn the number of items of a sequence or mapping." |
| 433 |
return space.len(w_obj) |
| 434 |
|
| 435 |
Exposed interpreter level functions usually take a ``space`` argument |
| 436 |
and some wrapped values (see `Wrapping rules`_) . |
| 437 |
|
| 438 |
You can also use a convenient shortcut in ``interpleveldefs`` dictionaries: |
| 439 |
namely an expression in parentheses to specify an interpreter level |
| 440 |
expression directly (instead of pulling it indirectly from a file):: |
| 441 |
|
| 442 |
... |
| 443 |
'None' : '(space.w_None)', |
| 444 |
'False' : '(space.w_False)', |
| 445 |
... |
| 446 |
|
| 447 |
The interpreter level expression has a ``space`` binding when |
| 448 |
it is executed. |
| 449 |
|
| 450 |
Adding an entry under pypy/module (e.g. mymodule) entails automatic |
| 451 |
creation of a new config option (such as --withmod-mymodule and |
| 452 |
--withoutmod-mymodule (the latter being the default)) for py.py and |
| 453 |
translate.py. |
| 454 |
|
| 455 |
|
| 456 |
Testing modules in ``lib_pypy/`` |
| 457 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 458 |
|
| 459 |
You can go to the :source:`pypy/module/test_lib_pypy/` directory and invoke the |
| 460 |
testing tool ("py.test" or "python ../../pypy/test_all.py") to run tests |
| 461 |
against the lib_pypy hierarchy. This allows us to quickly test our |
| 462 |
python-coded reimplementations against CPython. |
| 463 |
|
| 464 |
|
| 465 |
Testing modules in ``pypy/module`` |
| 466 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 467 |
|
| 468 |
Simply change to ``pypy/module`` or to a subdirectory and `run the |
| 469 |
tests as usual`_. |
| 470 |
|
| 471 |
|
| 472 |
Testing modules in ``lib-python`` |
| 473 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 474 |
|
| 475 |
In order to let CPython's regression tests run against PyPy |
| 476 |
you can switch to the :source:`lib-python/` directory and run |
| 477 |
the testing tool in order to start compliance tests. |
| 478 |
(XXX check windows compatibility for producing test reports). |
| 479 |
|
| 480 |
|
| 481 |
Naming conventions and directory layout |
| 482 |
--------------------------------------- |
| 483 |
|
| 484 |
Directory and File Naming |
| 485 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 486 |
|
| 487 |
- directories/modules/namespaces are always **lowercase** |
| 488 |
|
| 489 |
- never use plural names in directory and file names |
| 490 |
|
| 491 |
- ``__init__.py`` is usually empty except for |
| 492 |
``pypy/objspace/*`` and ``pypy/module/*/__init__.py``. |
| 493 |
|
| 494 |
- don't use more than 4 directory nesting levels |
| 495 |
|
| 496 |
- keep filenames concise and completion-friendly. |
| 497 |
|
| 498 |
|
| 499 |
Naming of python objects |
| 500 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 501 |
|
| 502 |
- class names are **CamelCase** |
| 503 |
|
| 504 |
- functions/methods are lowercase and ``_`` separated |
| 505 |
|
| 506 |
- objectspace classes are spelled ``XyzObjSpace``. e.g. |
| 507 |
|
| 508 |
- StdObjSpace |
| 509 |
- FlowObjSpace |
| 510 |
|
| 511 |
- at interpreter level and in ObjSpace all boxed values |
| 512 |
have a leading ``w_`` to indicate "wrapped values". This |
| 513 |
includes w_self. Don't use ``w_`` in application level |
| 514 |
python only code. |
| 515 |
|
| 516 |
|
| 517 |
Committing & Branching to the repository |
| 518 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 519 |
|
| 520 |
- write good log messages because several people |
| 521 |
are reading the diffs. |
| 522 |
|
| 523 |
- What was previously called ``trunk`` is called the ``default`` branch in |
| 524 |
mercurial. Branches in mercurial are always pushed together with the rest |
| 525 |
of the repository. To create a ``try1`` branch (assuming that a branch named |
| 526 |
``try1`` doesn't already exists) you should do:: |
| 527 |
|
| 528 |
hg branch try1 |
| 529 |
|
| 530 |
The branch will be recorded in the repository only after a commit. To switch |
| 531 |
back to the default branch:: |
| 532 |
|
| 533 |
hg update default |
| 534 |
|
| 535 |
For further details use the help or refer to the `official wiki`_:: |
| 536 |
|
| 537 |
hg help branch |
| 538 |
|
| 539 |
.. _official wiki: https://www.mercurial-scm.org/wiki/ |
| 540 |
|
| 541 |
|
| 542 |
.. _using-development-tracker: |
| 543 |
|
| 544 |
Using the development bug/feature tracker |
| 545 |
----------------------------------------- |
| 546 |
|
| 547 |
We use https://github.com/pypy/pypy for :source:`issues` tracking and |
| 548 |
:source:`pull-requests`. |
| 549 |
|
| 550 |
.. _testing: |
| 551 |
|
| 552 |
Testing in PyPy |
| 553 |
--------------- |
| 554 |
|
| 555 |
Our tests are based on the `py.test`_ tool which lets you write |
| 556 |
unittests without boilerplate. All tests of modules |
| 557 |
in a directory usually reside in a subdirectory **test**. There are |
| 558 |
basically two types of unit tests: |
| 559 |
|
| 560 |
- **Interpreter Level tests**. They run at the same level as PyPy's |
| 561 |
interpreter. |
| 562 |
|
| 563 |
- **Application Level tests**. They run at application level which means |
| 564 |
that they look like straight python code but they are interpreted by PyPy. |
| 565 |
|
| 566 |
.. _py.test: https://pytest.org/ |
| 567 |
|
| 568 |
|
| 569 |
Interpreter level tests |
| 570 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 571 |
|
| 572 |
You can write test functions and methods like this:: |
| 573 |
|
| 574 |
def test_something(space): |
| 575 |
# use space ... |
| 576 |
|
| 577 |
class TestSomething(object): |
| 578 |
def test_some(self): |
| 579 |
# use 'self.space' here |
| 580 |
|
| 581 |
Note that the prefix `test` for test functions and `Test` for test |
| 582 |
classes is mandatory. In both cases you can import Python modules at |
| 583 |
module global level and use plain 'assert' statements thanks to the |
| 584 |
usage of the `py.test`_ tool. |
| 585 |
|
| 586 |
Application level tests |
| 587 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 588 |
|
| 589 |
For testing the conformance and well-behavedness of PyPy it |
| 590 |
is often sufficient to write "normal" application-level |
| 591 |
Python code that doesn't need to be aware of any particular |
| 592 |
coding style or restrictions. If we have a choice we often |
| 593 |
use application level tests which are in files whose name starts with the |
| 594 |
`apptest_` prefix and look like this:: |
| 595 |
|
| 596 |
# spaceconfig = {"usemodules":["array"]} |
| 597 |
def test_this(): |
| 598 |
# application level test code |
| 599 |
|
| 600 |
These application level test functions will run on top |
| 601 |
of PyPy, i.e. they have no access to interpreter details. |
| 602 |
|
| 603 |
By default, they run on top of an untranslated PyPy which runs on top of the |
| 604 |
host interpreter. When passing the `-D` option, they run directly on top of the |
| 605 |
host interpreter, which is usually a translated pypy executable in this case:: |
| 606 |
|
| 607 |
pypy3 -m pytest -D pypy/ |
| 608 |
|
| 609 |
Note that in interpreted mode, only a small subset of pytest's functionality is |
| 610 |
available. To configure the object space, the host interpreter will parse the |
| 611 |
optional spaceconfig declaration. This declaration must be in the form of a |
| 612 |
valid json dict. |
| 613 |
|
| 614 |
Mixed-level tests (deprecated) |
| 615 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 616 |
|
| 617 |
Mixed-level tests are similar to application-level tests, the difference being |
| 618 |
that they're just snippets of app-level code embedded in an interp-level test |
| 619 |
file, like this:: |
| 620 |
|
| 621 |
class AppTestSomething(object): |
| 622 |
def test_this(self): |
| 623 |
# application level test code |
| 624 |
|
| 625 |
You cannot use imported modules from global level because |
| 626 |
they are imported at interpreter-level while you test code |
| 627 |
runs at application level. If you need to use modules |
| 628 |
you have to import them within the test function. |
| 629 |
|
| 630 |
Data can be passed into the AppTest using |
| 631 |
the ``setup_class`` method of the AppTest. All wrapped objects that are |
| 632 |
attached to the class there and start with ``w_`` can be accessed |
| 633 |
via self (but without the ``w_``) in the actual test method. An example:: |
| 634 |
|
| 635 |
class AppTestErrno(object): |
| 636 |
def setup_class(cls): |
| 637 |
cls.w_d = cls.space.wrap({"a": 1, "b", 2}) |
| 638 |
|
| 639 |
def test_dict(self): |
| 640 |
assert self.d["a"] == 1 |
| 641 |
assert self.d["b"] == 2 |
| 642 |
|
| 643 |
|
| 644 |
.. _run the tests as usual: |
| 645 |
|
| 646 |
Another possibility is to use cls.space.appexec, for example:: |
| 647 |
|
| 648 |
class AppTestSomething(object): |
| 649 |
def setup_class(cls): |
| 650 |
arg = 2 |
| 651 |
cls.w_result = cls.space.appexec([cls.space.wrap(arg)], """(arg): |
| 652 |
return arg ** 6 |
| 653 |
""") |
| 654 |
|
| 655 |
def test_power(self): |
| 656 |
assert self.result == 2 ** 6 |
| 657 |
|
| 658 |
which executes the code string function with the given arguments at app level. |
| 659 |
Note the use of ``w_result`` in ``setup_class`` but self.result in the test. |
| 660 |
Here is how to define an app level class in ``setup_class`` that can be used |
| 661 |
in subsequent tests:: |
| 662 |
|
| 663 |
class AppTestSet(object): |
| 664 |
def setup_class(cls): |
| 665 |
w_fakeint = cls.space.appexec([], """(): |
| 666 |
class FakeInt(object): |
| 667 |
def __init__(self, value): |
| 668 |
self.value = value |
| 669 |
def __hash__(self): |
| 670 |
return hash(self.value) |
| 671 |
|
| 672 |
def __eq__(self, other): |
| 673 |
if other == self.value: |
| 674 |
return True |
| 675 |
return False |
| 676 |
return FakeInt |
| 677 |
""") |
| 678 |
cls.w_FakeInt = w_fakeint |
| 679 |
|
| 680 |
def test_fakeint(self): |
| 681 |
f1 = self.FakeInt(4) |
| 682 |
assert f1 == 4 |
| 683 |
assert hash(f1) == hash(4) |
| 684 |
|
| 685 |
|
| 686 |
Command line tool test_all |
| 687 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 688 |
|
| 689 |
You can run almost all of PyPy's tests by invoking:: |
| 690 |
|
| 691 |
python test_all.py file_or_directory |
| 692 |
|
| 693 |
which is a synonym for the general `py.test`_ utility |
| 694 |
located in the ``py/bin/`` directory. For switches to |
| 695 |
modify test execution pass the ``-h`` option. |
| 696 |
|
| 697 |
|
| 698 |
Coverage reports |
| 699 |
~~~~~~~~~~~~~~~~ |
| 700 |
|
| 701 |
In order to get coverage reports the `pytest-cov`_ plugin is included. |
| 702 |
it adds some extra requirements ( coverage_ and `cov-core`_ ) |
| 703 |
and can once they are installed coverage testing can be invoked via:: |
| 704 |
|
| 705 |
python test_all.py --cov file_or_direcory_to_cover file_or_directory |
| 706 |
|
| 707 |
.. _pytest-cov: https://pypi.python.org/pypi/pytest-cov |
| 708 |
.. _coverage: https://pypi.python.org/pypi/coverage |
| 709 |
.. _cov-core: https://pypi.python.org/pypi/cov-core |
| 710 |
|
| 711 |
|
| 712 |
Test conventions |
| 713 |
~~~~~~~~~~~~~~~~ |
| 714 |
|
| 715 |
- adding features requires adding appropriate tests. (It often even |
| 716 |
makes sense to first write the tests so that you are sure that they |
| 717 |
actually can fail.) |
| 718 |
|
| 719 |
- All over the pypy source code there are test/ directories |
| 720 |
which contain unit tests. Such scripts can usually be executed |
| 721 |
directly or are collectively run by pypy/test_all.py |
| 722 |
|
| 723 |
|
| 724 |
.. _change documentation and website: |
| 725 |
|
| 726 |
Changing documentation and website |
| 727 |
---------------------------------- |
| 728 |
|
| 729 |
documentation/website files in your local checkout |
| 730 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 731 |
|
| 732 |
Most of the PyPy's documentation is kept in `pypy/doc`. |
| 733 |
You can simply edit or add '.rst' files which contain ReST-markuped |
| 734 |
files. Here is a `ReST quickstart`_ but you can also just look |
| 735 |
at the existing documentation and see how things work. |
| 736 |
|
| 737 |
Note that the web site of https://pypy.org/ is maintained separately. |
| 738 |
It is in the repository https://github.com/pypy/pypy.org |
| 739 |
|
| 740 |
.. _ReST quickstart: https://docutils.sourceforge.net/docs/user/rst/quickref.html |
| 741 |
|
| 742 |
|
| 743 |
Automatically test documentation/website changes |
| 744 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 745 |
|
| 746 |
We automatically check referential integrity and ReST-conformance. In order to |
| 747 |
run the tests you need sphinx_ installed. Then go to the local checkout |
| 748 |
of the documentation directory and run the Makefile:: |
| 749 |
|
| 750 |
cd pypy/doc |
| 751 |
make html |
| 752 |
|
| 753 |
If you see no failures chances are high that your modifications at least |
| 754 |
don't produce ReST-errors or wrong local references. Now you will have `.html` |
| 755 |
files in the documentation directory which you can point your browser to! |
| 756 |
|
| 757 |
Additionally, if you also want to check for remote references inside |
| 758 |
the documentation issue:: |
| 759 |
|
| 760 |
make linkcheck |
| 761 |
|
| 762 |
which will check that remote URLs are reachable. |
| 763 |
|
| 764 |
.. _sphinx: https://sphinx.pocoo.org/ |