Passing values from C++ to embedded python.

Bjorn Pettersen BPettersen at NAREX.com
Wed May 29 22:58:30 EDT 2002


> From: Justin Dubs [mailto:jtdubs at eos.ncsu.edu] 
> 
> 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?

If switching to SIP instead of SWIG is an option all you'd have to do
is:

  PyObject* val = sipMapCppToSelf(nDatePtr, sipClass_NDate);

where NDate is the wrapped class. I'm assuming SWIG has similar
functionality, but it's been several years since I used it last...

-- bjorn





More information about the Python-list mailing list