Passing values from C++ to embedded python.
Justin Dubs
jtdubs at eos.ncsu.edu
Wed May 29 20:58:43 EDT 2002
Hey everyone,
I have a C++ app in which I have embedded the python interpreter. I have a
thread which does the simple Py_Initialize(), PyRun_InteractiveLoop(),
Py_Finalize() business using stdin and stdout. Meanwhile the main thread
does all the regular functionality of the app in another window. I have
several classes, written in C++, which I have wrapped with SWIG and compiled
into a shared library. C++ can instantiate the original classes, obviously,
as they are written in C++. Python can instantiate the wrapped classes from
the shared library and that also works perfectly.
So, my problem is thus:
If I have a pointer to an instance of one of these classes in C++, how can I
create a python variable that points to the same instance?
In other words, in pseudocode:
In C++:
MyClass *foo = new MyClass();
foo->SetAnswer(42);
// insert unknown code to communicate foo to python
In Python, from my interactive interpreter running on stdin and stdout
within the C++ app:
# insert unknown code to receive foo from c++
>>> foo.getAnswer();
42
I hope you can understand what I am trying to accomplish. I need to make
python aware of an instance of a variable that exists in C++. I tried doing
this some simple ways from C++ using PyCObject_* and PyInstance_* and
PyNew_* and other things. But, alas, I am but a beginner with the Python/C
API and I could really use some help.
Thanks a lot everyone,
Justin Dubs
More information about the Python-list
mailing list