Saturday, March 14, 2015

CPython inside Rusthon


Normally when mixing C++ with CPython you compile your C++ code and load it as an extension module. Sometimes, but less often CPython is embedded inside a C++ program. To interact with the Python interpreter from C++ requires writing verbose C-API calls all over the place. Rusthon simplifies using the C-API with special syntax ->, dedicated to PyObjects, that will generate the required PyObject_XXX calls for you.

simple tutorial: cpython_embed.md

The simple tutorial above shows you how to call methods on PyObject's and convert the return value to a C++ native type. Below shows how to use Go style channels to send PyObject's around to different threads created with spawn (c++11 std::thread).

multi-threaded GIL example: cpython_multithreaded.md

with gil:
    value = pyob->pymethod( cppob.cppmethod() as pyint ) as int
above example shows different calling styles for the PyObject and C++ object. note: as pyint becomes PyInt_FromLong from the CPython C-API, this is used to convert the return value of `cppmethod` to a PyObject.

No comments:

Post a Comment