[pypy-dev] PyPy C API

Armin Rigo arigo at tunes.org
Sat Jul 14 10:35:48 CEST 2012

Hi Amaury,

On Tue, Jul 10, 2012 at 7:57 PM, Amaury Forgeot d'Arc
<amauryfa at gmail.com> wrote:
> Do you have an idea what this API would look like?
> Then I can help with the implementation :)

If we want to go down this path without caring for compatibility with
CPython's C API, but instead focusing on what gives the best
performance, then I would think about something like this:

pypyobj pypy_wraplong(long value);
pypyobj pypy_add(pypyobj x, pypyobj y);
void pypy_close(pypyobj x);
pypyobj pypy_dup(pypyobj x);

using handles of type "pypyobj", which are basically opaque stuff (or
even just integers, with "-1" meaning "exception").  Instead of the
refcounting approach of CPython, it would be similar to file
descriptors: a file descriptor refers to a file, but most files don't
have any open file descriptor, and some files may have more than one.
Any "object descriptor" must be closed with pypy_close().  pypy_dup()
just duplicates the object descriptor, so that both descriptors refer
to the same object but will be pypy_close()d independently.

This can be implemented efficiently: the C->PyPy direction is just
doing one array lookup (this minimal indirection is hard to avoid
anyway with a moving GC); and the PyPy->C direction (like the return
value from pypy_add) just creates a new object descriptor anyway,
without needing to look if one already exists.

(1) Do I make some sense, and (2) is there any real use case for such
an API?  E.g. would the expected performance gains of Cython justify
the rewrite needed to handle such an API, which is quite different
from CPython's?

A bientôt,


More information about the pypy-dev mailing list