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

Darrell news at dorb.com
Thu Dec 9 10:24:57 EST 1999


If you can use C++, then make your like easy.
http://starship.python.net/crew/gmcm/scxx.html

--
--Darrell
"Wojciech Zabolotny" <wzab at ise.pw.edu.pl> wrote in message
news:Pine.LNX.3.96.991209154347.4297A-100000 at ipebio15.ise.pw.edu.pl...
> 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;
>
> file://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!
>
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list