How to Do Something with RPython Part 1

Ok, so here's how to set up a new independent codebase which you want to write in RPython using the C backend. I use this kind of set up when hacking on Safelisp, which is a language totally unrelated to Python which I am implementing in RPython.

This will take you through writing, translating, and running a program which prints "hello world" to standard out.

I'll assume you have an SVN checkout of pypy at ~/Projects/pypy/.

$ mkdir MyProjectInRPython
$ cd MyProjectInRPython
$ echo "This is my RPython-based project." > README
$ mkdir myproj
$ touch myproj/__init__.py
$ emacs myproj/myproj_target.py
And put the following code in the file:
# __________  Entry point  __________

def entry_point(argv):
print "hello world"
return 0

# _____ Define and setup target ___

def target(*args):
return entry_point, None

Now compile and run it.
$ ~/Projects/pypy/pypy/translator/goal/translate.py  --batch myproj/myproj_target.py
[Lots and lots of output]

$ ./myproj_target-c
hello world

That translate.py command is really long, so I generally put a symlink to it in my ~/bin named "pypy-translate". The --batch flag is meant to disable the interactive debugging facilities during translation -- if you leave it off, a very nice pygame viewer will pop up showing the annotated graph of your application, which is a great way to learn more. It'll also give you a Pdb debugger on the console.

I'll post further about actually writing some real RPython code. It ain't easy.

2 comments:

Fuzzyman said...

Wow, great.

Is there a reference or specification for RPython somewhere ?

A guide to writing extensions using RPython would be *fantastic*.

Christopher Armstrong said...

No, there isn't a reference or spec for RPython. That's one of the reasons I'm writing this stuff. There is some very superficial content on the PyPy coding guide about RPython, though.

At some point I'm going to want to embed my RPython code in CPython, so I will have to figure out how to write an extension module, but that'll probably be a while from now.