tp_obj tp_iter(TP,tp_obj self, tp_obj k) { int type = obj_type(self); if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); } if (type == TP_DICT && obj_type(k) == TP_NUMBER) { return tp_dict_val(self)->items[_tp_dict_next(tp, tp_dict_val(self))].key; } tp_raise(None,"tp_iter(%s,%s)",STR(self),STR(k)); }
/* Function: tp_iter * Iterate through a list or dict. * * If self is a list/string/dictionary, this will iterate over the * elements/characters/keys respectively, if k is an increasing index * starting with 0 up to the length of the object-1. * * In the case of a list of string, the returned items will correspond to the * item at index k. For a dictionary, no guarantees are made about the order. * You also cannot call the function with a specific k to get a specific * item -- it is only meant for iterating through all items, calling this * function len(self) times. Use <tp_get> to retrieve a specific item, and * <tp_len> to get the length. * * Parameters: * self - The object over which to iterate. * k - You must pass 0 on the first call, then increase it by 1 after each call, * and don't call the function with k >= len(self). * * Returns: * The first (k = 0) or next (k = 1 .. len(self)-1) item in the iteration. */ tp_obj tp_iter(TP,tp_obj self, tp_obj k) { int type = self.type; if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); } if (type == TP_DICT && k.type == TP_NUMBER) { return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key; } tp_raise(tp_None,tp_string("(tp_iter) TypeError: iteration over non-sequence")); }
tp_obj tp_iter(TP,tp_obj self, tp_obj k) { int type = self.type; if (type == TP_LIST || type == TP_STRING) { return tp_get(tp,self,k); } if (type == TP_DICT && k.type == TP_NUMBER) { return self.dict.val->items[_tp_dict_next(tp,self.dict.val)].key; } tp_raise(tp_None,"tp_iter(%s,%s)",TP_CSTR(self),TP_CSTR(k)); }
void tp_follow(TP,tp_obj v) { int type = v.type; if (type == TP_LIST) { int n; for (n=0; n<v.list.val->len; n++) { tp_grey(tp,v.list.val->items[n]); } } if (type == TP_DICT) { int i; for (i=0; i<v.dict.val->len; i++) { int n = _tp_dict_next(tp,v.dict.val); tp_grey(tp,v.dict.val->items[n].key); tp_grey(tp,v.dict.val->items[n].val); } } if (type == TP_FNC) { tp_grey(tp,v.fnc.info->self); tp_grey(tp,v.fnc.info->globals); } }