[pypy-dev] PyPy C API

Ronny Pfannschmidt Ronny.Pfannschmidt at gmx.de
Sat Jul 14 12:09:21 CEST 2012

On 07/14/2012 10:35 AM, Armin Rigo wrote:
> 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?
(1) yes
     cython could target that with some added primitives like
     new_ref and drop_rev which in cpython would incref/decref
     and on pypy would dup/close
      this can be the basis of creating/generating higher level apis to
      interface with pypy

      imagine a gobject+gobject introspection based wrapper
      that would allow stuff like the following
      in vala/javascript/anything else for example::

        import PyPy/ PyPy = require('pypy')
        var long1 = PyPy.Int.from_long(123)
        var long2 = PyPy.Int.from_long(123)
        var result = long1.add(long2)
        var text = result.to_string()
      (slightly crazy)
      it might be possible to create a pure c lib
      that can be used as compat layer
      for pypy and cpython extensions
      which would allow gradual adoption even
      in cases where cpyext would have been too slow
      and ctypes/cffi aren't a option

      (but i have a feeling that using such a lib would require
       a linter to help avoiding all those easy misstakes)

-- Ronny
> A bientôt,
> Armin.
> _______________________________________________
> pypy-dev mailing list
> pypy-dev at python.org
> http://mail.python.org/mailman/listinfo/pypy-dev

More information about the pypy-dev mailing list