Closed torokati44 closed 1 year ago
My current solution involves invoking a "converter" Python function which recognizes a number of wrapped C++ types, and extracts the data from them recursively into regular Python primitives, strings, lists, dictionaries, etc. - which I can then access through their PyObject *
from C++ via the appropriate CPython API for each type, and convert them back to C++ objects.
This works okay for my purposes, but is not nice, and involves an unnecessary back-and-forth conversion.
See CPyCppyy/API.h
, using Instance_Check
to check it's a cppyy bound instance, then Instance_AsVoidPtr
to extract the pointer. (It does still subsequently require a cast on the C++ side to the actual type, but the pointer will have the correct offset to the type Python thinks it has.)
Closing as presumed clarified. If that's not the case, feel free to reopen or start a new issue.
I have an embedded CPython interpreter in a C++ program. Within that interpreter, I'm using Cppyy to allow interaction between the
exec
-ed Python scripts and the native parts of the application. Sometimes I want an evaluated Python expression to return a C++ object, but I can't get to it from thePyObject *
of the wrapper returned by the interpreter.See this little example (I had to split
getWords()
into a separate library otherwise the symbol was not found by Cppyy... :/ ):words.h
:words.cc
:main.cc
:Compile and run on Linux like this: