Iterating over tp_dict

77 views
Skip to first unread message

Manuel Torrinha

unread,
Apr 8, 2015, 6:36:10 PM4/8/15
to tin...@googlegroups.com
Hi,

I am trying to implement a function that iterates over the tp_dict structure inside tinypy.

I have implemented similar functions of other object types, including lists without trouble.

However with dicts I often get SEG FAULT while printing a value (not key) element of the dictionary.

I am using the alloc member of the structure as the number of elements present in the items array.

What could I be doing wrong?

Heres my code:

void print_tp_obj(tp_obj v){
 
int type = v.type;

 
switch(type){
   
case TP_DICT:
      print_tp_dict
(v);
     
break;
 
}
}

void print_tp_item(tp_item i){
 
/*key*/
 
if(strcmp(i.key.string.val, "") == 0)
   
return;
  fprintf
(stderr,"\"");
  print_tp_string
(i.key);
  fflush
(stderr);
  fprintf
(stderr,"\" : ");
 
/*value*/
  fprintf
(stderr,"\"");
  print_tp_obj
(i.val);
  fflush
(stderr);
  fprintf
(stderr,"\"");
}

void print_tp_dict(tp_obj v){
  tp_item
*items = v.dict.val->items;
 
int items_n = v.dict.val->alloc;
 
register int i;

  fprintf
(stderr,"{\n");
 
for(i=0; i < items_n; i++){

   
if(items[i].used != 1) {
     
continue;
   
}

    print_tp_item
(items[i]);

   
if(i != items_n-1)
      fprintf
(stderr,",\n");

 
}
  fprintf
(stderr,"}\n");
}

Thanks in advance!
Manuel
Reply all
Reply to author
Forward
0 new messages