VC++/Python integration: Function calls with objects as parameters???

Carl Johan Rehn carljohan.rehn at chello.se
Mon Sep 18 12:50:09 EDT 2000


Dear friends!

I am a newcomer to the Python language and have a VC++/Python integration
problem that I have struggled with for some time now and not been able to
solve. The application consists of a number crunching algorithm in VC++
which makes use of a large amount of information stored in a Python module.

Thus, I need to pass a Python module to VC++ and from within VC++ make
function calls (with object parameters) of object attributes of the passed
Python module.

Now, I have managed to get attributes and call functions with ordinary
parameters such as strings, integers, and floats. However, object parameters
of calls to functions will not work. It may be the case that I have used the
wrong format string for objects (since I am not sure what it should be; I
have tried to find some documentation on this, but have not found any).

Has anyone tried this before? I would be very grateful to any suggestions or
hints!

Yours / Carl

This is an extract of the code that I am working with:

Object tree(const Tuple &t)
{
   if(t.length()!=1)
        throw Exception("Incorrect number of arguments.");

   // A python module is passed as the first element of a tuple...
   Object pyOptPar=(Object) t[0];

   // Get object attribute of Python module...
   Object objinstr;
   objinstr = pyOptPar.getAttr("Instrument");

   // Function call (read) of object attribute with a string parameter;
return Python object...
   Object objlme;
   objlme = PyObject_CallMethod(objinstr.ptr(), "read", "s", "id='lme'");

   // Get attribute (addr) of Python object...
   Object objiu;
   objiu = objlme.getAttr("addr");

   // Function call (curve) of object, no parameters; return Python
object...
   Object objy_disc;
   objy_disc = PyObject_CallMethod(objiu.ptr(), "curve", NULL);

   // Function call (date_today)...
   Object objdate_today;
   objdate_today = PyObject_CallMethod(pyOptPar.ptr(), "date_today", NULL);

   // Get attribute (exp_day)...
   Object objexp_day;
   objexp_day = objlme.getAttr("exp_day");

   // Get attribute (curr)...
   Object objcurr;
   objcurr = objlme.getAttr("pdayoffset");

   // Get attribute (curr)...
   Object objpdayoffset;
   objpdayoffset = objlme.getAttr("pdayoffset");
   int ipdayoffset = (Int) objpdayoffset;

   // This is where I am having trouble: I cannot pass a Python object
(objcurr) to the function add_day.
   // I have managed to handle function calls with ordinary parameters such
as strings, integers, and floats; but
   // have not yet grasped the technique of handling parameter objects. What
is the correct format string of Python objects?
   // Is it O!, O?, or O&? Are there any documents available that describes
function calls of Python objects and format strings?
   Object objd_disc;
   objd_disc = PyObject_CallMethod(objexp_day.ptr(), "add_day", "O i",
objcurr, ipdayoffset);
   return objd_disc;
}





More information about the Python-list mailing list