Parsing a Python dictionary inside a Python extension

2005-05-27 Thread quadric
Hi,

I would like to pass a dictionary from my Python code to my Python 
extension, extract
various values from the dictionary (from within the extension) , modify the 
values for the
relevant keys and return the modified dictionary to Python.

Can someone point me to an example of what the C code might look like to do 
this?


I tried something like this and it has not worked.


static PyObject *  ModifyDictionary( PyObject * self , PyObject * args )
{
int atab1 = 0 , atab2 = 0;
PyObject * vdict  = NULL;
PyObject * item  = NULL;
PyObject * ndict = NULL;

PyArg_ParseTuple( args , "O" , & vdict );

if ( (item = PyDict_GetItemString( vdict , "atab1")) != NULL 
)   PyArg_ParseTuple( item , "i" , &atab1   );
if ( (item = PyDict_GetItemString( vdict , "atab2")) != NULL 
)   PyArg_ParseTuple( item , "i" , &atab2   );

// modify values here and rebuild the dictionary
//  ndict = Py_BuildValue( create dictionary here ..)

return ndict ;
}


Can someone tell me where I am going wrong or point me to an example?

Thanks for your help.


-- 
http://mail.python.org/mailman/listinfo/python-list


Inserting a dictionary of lists into '__main__' of an embedded interpreter

2005-05-27 Thread quadric
Hi,

I have an application that has an embedded interpreter.  This application 
loads many
DLL's and passes a PyObject *  to each DLL that was gotten from the 
following call:

PyObject * pmod = PyImport_AddModule("__main__") ;


Later, in one of the many DLL's that interact with the embedded 
interpreter, I attempt to insert
a dictionary of lists into the module represented by the pointer 
'pmod'  passed to the DLL from the
main application.

The dictionary of lists is created with the following call:

PyObject * abm_dict = Py_BuildValue( "{s:O,s:O,s:O,s:O,s:O,s:O,s:O,s:O,s:O}" ,
 "i_list" ,   i_list ,   
"l_list"   ,   l_list  , 
"bpl_list"  , bpl_list,
   "rt_list" 
,  rt_list , "st_list"   ,st_list   ,  "u2_list"  , u2_list ,
"t_list" 
, t_list   ,"ot_list"  , ot_list  ,  "plt_list" , plt_list
   );

where each item following a quoted string is a PyObject *  created by 
another Py_BuildValue  statement.

After building 'abm_dict' , I attempt to insert it into the embedded 
interpreter with the following call:

PyModule_AddObject( pmod , "abm_dict" , abm_dict );

The application crashes at this point.

Being a bit new to embedded Python, I don't readily see what I'm doing 
wrong.  Could one of you more
experienced guys save me a whole lot of time and point out the obvious to me?

Thanks for your help.








-- 
http://mail.python.org/mailman/listinfo/python-list