[pypy-dev] translating own language to shared lib

Armin Rigo arigo at tunes.org
Fri Jan 4 19:40:34 CET 2013


Hi,

Regarding your other question: "how to embed PyPy in C/C++
application", missing _Py_Initialize.  It's not supported for now but
could be, given enough work.

Here is an alternative, assuming that you don't need to have an
interface compatible with CPython (the PyXxx functions).  We could
come up with some cffi-based solution (http://cffi.readthedocs.org/).
For example, with minimal amount of work, we could give you the
possibility to do something like this:

* The C/C++ code loads "libpypy-c.dll" and calls some function
'initialize("initial_script.py")'.  (Or, running on CPython, it would
load "libpython27.dll" and call _PyInitialize();
PyRun_File("initial_script.py");.)

* initial_script.py uses CFFI in a slightly different way than
documented above (the other way around, basically): it declares a C
interface, but with C functions that end up calling back into Python.
(These C functions themselves are not meant to be called *from*
Python.)

* Then initial_script.py finishes and returns the name of the library
built by "ffi.verify()".

* The C/C++ side then loads and uses that library --- a
regular-looking C library with whatever custom C API that you declared
above.

The trick is that this lets you write a regular library that can be
called from C, with the C API you choose, but *implemented as Python
code*.  I imagine that this could be generally interesting.


A bientôt,

Armin.


More information about the pypy-dev mailing list