How to add element to the list in C extension module?

Wojciech Zabolotny wzab at ise.pw.edu.pl
Thu Dec 9 09:56:50 EST 1999


Hi All,

I need to dynamically build the complex python data structure in my
C extension.
I'd like to build a list of structures (lists or dictionaries), where the
amount of items is not known a priori.
I tried to do something like this:

PyObject * ob;
int count=0;

//There is the loop where I add items
while(!loop_end_condition)
{
 // here key1, val1 ,key2, val2 are calculated
 // ...
 //
 if(!count)
  {
    ob=Py_BuildValue("{s:i,s:i}",key1,val1,key2,val2);
  }
 else
  {
    ob=Py_BuildValue("O{s:i,s:i}",key1,val1,key2,val2);
  }
}

But what I get is eg.:
(({'bc': 5, 'as': 3}, {'bc': 5, 'as': 3}), {'bc': 5, 'as': 3})
instead of:
({'bc': 5, 'as': 3}, {'bc': 5, 'as': 3}, {'bc': 5, 'as': 3})

How to do it correctly?

By the way. Does Py_BuildValue increases the reference count of the
created object? What format ("O" or "N") should I use to nest such object
in another object created by Py_BuildValue?

-- 
                              Wojciech M. Zabolotny
        http://www.ise.pw.edu.pl/~wzab  <--> wzab at ise.pw.edu.pl

http://www.freedos.org  Free DOS for free people!




More information about the Python-list mailing list