Getting Started with RPython


Please read this FAQ entry first!

RPython is a subset of Python that can be statically compiled. The PyPy interpreter is written mostly in RPython (with pieces in Python), while the RPython compiler is written in Python. The hard to understand part is that Python is a meta-programming language for RPython, that is, “being valid RPython” is a question that only makes sense on the live objects after the imports are done. This might require more explanation. You start writing RPython from entry_point, a good starting point is rpython/translator/goal/ This does not do all that much, but is a start. Now if code analyzed (in this case entry_point) calls some functions, those calls will be followed. Those followed calls have to be RPython themselves (and everything they call etc.), however not entire module files. To show how you can use metaprogramming, we can do a silly example (note that closures are not RPython):

def generator(operation):
    if operation == 'add':
       def f(a, b):
           return a + b
       def f(a, b):
           return a - b
    return f

add = generator('add')
sub = generator('sub')

def entry_point(argv):
    print add(sub(int(argv[1]), 3) 4)
    return 0

In this example entry_point is RPython, add and sub are RPython, however, generator is not.

The following introductory level articles are available:

Trying out the translator

The translator is a tool based on the PyPy interpreter which can translate sufficiently static RPython programs into low-level code (in particular it can be used to translate the full Python interpreter). To be able to experiment with it you need to download and install the usual (CPython) version of:

To start the interactive translator shell do:

cd rpython
python bin/

Test snippets of translatable code are provided in the file rpython/translator/test/, which is imported under the name snippet. For example:

>>> t = Translation(snippet.is_perfect_number, [int])
>>> t.view()

After that, the graph viewer pops up, that lets you interactively inspect the flow graph. To move around, click on something that you want to inspect. To get help about how to use it, press ‘H’. To close it again, press ‘Q’.

Trying out the type annotator

We have a type annotator that can completely infer types for functions like is_perfect_number (as well as for much larger examples):

>>> t.annotate()
>>> t.view()

Move the mouse over variable names (in red) to see their inferred types.

Translating the flow graph to C code

The graph can be turned into C code:

>>> t.rtype()
>>> lib = t.compile_c()

The first command replaces the operations with other low level versions that only use low level types that are available in C (e.g. int). The compiled version is now in a .so library. You can run it say using ctypes:

>>> f = get_c_function(lib, snippet.is_perfect_number)
>>> f(5)
>>> f(6)

A slightly larger example

There is a small-to-medium demo showing the translator and the annotator:

python bin/rpython --view --annotate translator/goal/

This causes to display itself as a call graph and class hierarchy. Clicking on functions shows the flow graph of the particular function. Clicking on a class shows the attributes of its instances. All this information (call graph, local variables’ types, attributes of instances) is computed by the annotator.

To turn this example to C code (compiled to the executable bpnn-c), type simply:

python bin/rpython translator/goal/

Translating Full Programs

To translate full RPython programs, there is the script in rpython/translator/goal. Examples for this are a slightly changed version of Pystone:

python bin/rpython translator/goal/targetrpystonedalone

This will produce the executable “targetrpystonedalone-c”.

The largest example of this process is to translate the full Python interpreter. There is also an FAQ about how to set up this process for your own interpreters.

There are several environment variables you can find useful while playing with the RPython:

RPython uses temporary session directories to store files that are generated during the translation process(e.g., translated C files). PYPY_USESSION_DIR serves as a base directory for these session dirs. The default value for this variable is the system’s temporary dir.
By default RPython keeps only the last PYPY_USESSION_KEEP (defaults to 3) session dirs inside PYPY_USESSION_DIR. Increase this value if you want to preserve C files longer (useful when producing lots of lldebug builds).

Where to start reading the sources

PyPy is made from parts that are relatively independent of each other. You should start looking at the part that attracts you most (all paths are relative to the PyPy top level directory). You may look at our directory reference or start off at one of the following points:

Running PyPy’s unit tests

PyPy development always was and is still thoroughly test-driven. We use the flexible py.test testing tool which you can install independently and use for other projects.

The PyPy source tree comes with an inlined version of py.test which you can invoke by typing:

python -h

This is usually equivalent to using an installed version:

py.test -h

If you encounter problems with the installed version make sure you have the correct version installed which you can find out with the --version switch.

Now on to running some tests. PyPy has many different test directories and you can use shell completion to point at directories or files:

py.test pypy/interpreter/test/

# or for running tests of a whole subdirectory
py.test pypy/interpreter/

See py.test usage and invocations for some more generic info on how you can run tests.

Beware trying to run “all” pypy tests by pointing to the root directory or even the top level subdirectory pypy. It takes hours and uses huge amounts of RAM and is not recommended.

To run CPython regression tests you can point to the lib-python directory:

py.test lib-python/2.7/test/

This will usually take a long time because this will run the PyPy Python interpreter on top of CPython. On the plus side, it’s usually still faster than doing a full translation and running the regression test with the translated PyPy Python interpreter.

Special Introspection Features of the Untranslated Python Interpreter

If you are interested in the inner workings of the PyPy Python interpreter, there are some features of the untranslated Python interpreter that allow you to introspect its internals.

Interpreter-level console

If you start an untranslated Python interpreter via:

python pypy/bin/

If you press <Ctrl-C> on the console you enter the interpreter-level console, a usual CPython console. You can then access internal objects of PyPy (e.g. the object space) and any variables you have created on the PyPy prompt with the prefix w_:

>>>> a = 123
>>>> <Ctrl-C>
*** Entering interpreter-level console ***
>>> w_a

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:

>>> w_l = space.newlist([space.wrap(1), space.wrap("abc")])
>>> <Ctrl-D>
*** Leaving interpreter-level console ***

>>>> l
[1, 'abc']

Note that the prompt of the interpreter-level console is only ‘>>>’ since it runs on CPython level. If you want to return to PyPy, press <Ctrl-D> (under Linux) or <Ctrl-Z>, <Enter> (under Windows).

You may be interested in reading more about the distinction between interpreter-level and app-level.

Tracing bytecodes

You can use a simple tracing mode to monitor the interpretation of bytecodes. To enable it, set __pytrace__ = 1 on the interactive PyPy console:

>>>> __pytrace__ = 1
Tracing enabled
>>>> x = 5
        <module>:           LOAD_CONST    0 (5)
        <module>:           STORE_NAME    0 (x)
        <module>:           LOAD_CONST    1 (None)
        <module>:           RETURN_VALUE    0
>>>> x
        <module>:           LOAD_NAME    0 (x)
        <module>:           PRINT_EXPR    0
        <module>:           LOAD_CONST    0 (None)
        <module>:           RETURN_VALUE    0


The example-interpreter repository contains an example interpreter written using the RPython translation toolchain.

Additional Tools for running (and hacking) PyPy

We use some optional tools for developing PyPy. They are not required to run the basic tests or to get an interactive PyPy prompt but they help to understand and debug PyPy especially for the translation process.

py.test and the py lib

The py.test testing tool drives all our testing needs.

We use the py library for filesystem path manipulations, terminal writing, logging and some other support functionality.

You don’t necessarily need to install these two libraries because we also ship them inlined in the PyPy source tree.

Getting involved

PyPy employs an open development process. You are invited to join our pypy-dev mailing list or look at the other contact possibilities. Usually we give out commit rights fairly liberally, so if you want to do something with PyPy, you can become a committer. We are also doing coding Sprints which are separately announced and often happen around Python conferences such as EuroPython or Pycon. Upcoming events are usually announced on the blog.