Using a dictionary to pass data to/from embedded python functions

wardm wardm66 at gmail.com
Fri Aug 11 17:02:09 EDT 2006


I have created a Dict object in a C++ App that calls (embedded) Python 
functions.
The Dict is to be used to pass variable data between the C++ App and the 
python functions.

However I cannot get the Python functions to 'see' the Dict created in the 
C++ App.

The C++ app creates the Dict using the following functions:

// Create the dict for the viable data
 m_pVarDictionary = PyDict_New();

// Create interface module
 m_pInterfaceModule = PyImport_AddModule("InterfaceModule");


 // Add the Dictionary to the interface Module
 int status = PyModule_AddObject(m_pInterfaceModule, "VarDictionary" , 
m_pVarDictionary);


 // Load the script module
 m_pScriptModule = PyImport_ImportModule("MyScriptModule");

The C++ app calls the functions using the following functions:

  PyObject* func = PyObject_GetAttrString(m_pScriptModule, "functionName");

  if (func && PyCallable_Check(func))
  {

   PyObject* ret = PyObject_CallObject(func, NULL);

  }


My test python function looks like this:


"""----------------------------------------------------------------------
Simple scripts for testing Dict Access
-------------------------------------------------------------------------"""
from InterfaceModule import VarDictionary

def hello():


    # dumps the contents of VarDictionary
    d2 = VarDictionary
    f1.write(len(d2))
    f1.write('\n')

    for k, v in d2.iteritems():
       f1.write(k)
       f1.write (':')
       f1.write(str(v))
       f1.write('\n')

    f1.close()


This python code throws an exception when it attempts to access the 
"VarDictionary".
Does anyone know why this fails ?

I have tried adding code that dumps the Dict returned from vars(),
and could not see "VarDictionary" as an entry in the Vars() dict.





More information about the Python-list mailing list