[pypy-dev] cpyext performance

Armin Rigo arigo at tunes.org
Fri Aug 31 15:53:24 CEST 2012


Hi,

On Thu, Aug 30, 2012 at 10:28 AM, Armin Rigo <arigo at tunes.org> wrote:
> This is a blocker, and one that the plan towards CFFI should
> completely fix.

A quick but successful attempt can be found in cffi/demo/{api,pyobj}.py.

api.py is a bit of a hack but lets you declare functions with the
decorator @ffi.pyexport("C function signature").  This gives you
Python-implemented functions that you can call back from C.  They are
available from the C code passed into verify().  (A possible extension
of this idea would be to choose the precise name and C-level interface
that we want to give to the .so; with only a few extra hacks this
could let the user write pure Python CFFI code to implement a .so that
can be used in the system without knowledge that it is written in
Python in the first place, e.g. as a plug-in to some existing
program.)

pyobj.py is a quick attempt at exposing the C API I described earlier,
with "object descriptors".  So far the API is only sufficient for the
demo in this file, which is a C function that iterates over a list and
computes the sum of its items.  In the first version it assumes all
items are numbers that fit in an "int".  In the second version it
really calls the Python addition, and so supports various kinds of
objects.  Note how the whole API is basically defined and implemented
as regular Python code.

These examples should work well on both CPython and PyPy.


A bientôt,

Armin.


More information about the pypy-dev mailing list