C++ and Embedded Python

Stefan Behnel stefan_ml at behnel.de
Fri Jan 20 12:17:50 EST 2012

jasonkeddie at hotmail.com, 20.01.2012 14:52:
> Is there a method to allow a pointer to an object in local memory to
> be shared with embedded Python within a C++ executable?
> The reason I require this is I have a C++ instance of a class that has
> data memers that are themeselves pointers to other object types. It is
> a fairly complicated object.
> In an ideal world I would just pass this pointer into an instance of
> the Python interpreter. This would allow the python program access and
> change some properties of this object instance, then pass it back to
> the calling C++ program. I would use this same system to pass messages
> between the C++ (calling program) and the embedded Python interpreter.
> I have looked at using shared memory, which will work until you
> require embedded pointers within your object as you cannot dynamically
> allocate memory in a shared space.

Do I understand correctly that you only have one instance of a Python
runtime, embedded in a C++ program? If that's the case, I don't see the
problem. Just create an extension type to model your API towards the C++
objects and pass data back and forth. Cython will make that easy.

I specifically do not see why you would care about shared memory here. If
both parts of the application (i.e. Python code and C++ code) are in the
same process, they will usually be able to access the same memory without
any special setup.

If I misunderstood your intention, please provide more details, including
the steps you undertook so far and a specific description of what didn't
work. You may also want to provide some hints on what kind of data you want
to exchange and what both sides will do with it.


More information about the Python-list mailing list