Author: Maciej Fijalkowski <[email protected]>
Branch: rdict-experiments-3
Changeset: r67253:876473158bd8
Date: 2013-10-09 18:43 +0200
http://bitbucket.org/pypy/pypy/changeset/876473158bd8/
Log: (fijal, arigo) simple fixes to make iteration work
diff --git a/rpython/rtyper/lltypesystem/rdict.py
b/rpython/rtyper/lltypesystem/rdict.py
--- a/rpython/rtyper/lltypesystem/rdict.py
+++ b/rpython/rtyper/lltypesystem/rdict.py
@@ -878,14 +878,17 @@
#
# Iteration.
+def get_ll_dictiter(DICTPTR):
+ return lltype.Ptr(lltype.GcStruct('dictiter',
+ ('dict', DICTPTR),
+ ('index', lltype.Signed)))
+
class DictIteratorRepr(AbstractDictIteratorRepr):
def __init__(self, r_dict, variant="keys"):
self.r_dict = r_dict
self.variant = variant
- self.lowleveltype = lltype.Ptr(lltype.GcStruct('dictiter',
- ('dict', r_dict.lowleveltype),
- ('index', lltype.Signed)))
+ self.lowleveltype = get_ll_dictiter(r_dict.lowleveltype)
self.ll_dictiter = ll_dictiter
self.ll_dictnext = ll_dictnext_group[variant]
@@ -905,31 +908,35 @@
def ll_dictnext(RETURNTYPE, iter):
# note that RETURNTYPE is None for keys and values
dict = iter.dict
- if dict:
- entries = dict.entries
- index = iter.index
- assert index >= 0
- entries_len = len(entries)
- while index < entries_len:
- entry = entries[index]
- is_valid = entries.valid(index)
- index = index + 1
- if is_valid:
- iter.index = index
- if RETURNTYPE is lltype.Void:
- return None
- elif kind == 'items':
- r = lltype.malloc(RETURNTYPE.TO)
- r.item0 = recast(RETURNTYPE.TO.item0, entry.key)
- r.item1 = recast(RETURNTYPE.TO.item1, entry.value)
- return r
- elif kind == 'keys':
- return entry.key
- elif kind == 'values':
- return entry.value
- # clear the reference to the dict and prevent restarts
- iter.dict = lltype.nullptr(lltype.typeOf(iter).TO.dict.TO)
+ if not dict:
+ raise StopIteration
+
+ entries = dict.entries
+ index = iter.index
+ assert index >= 0
+ entries_len = dict.num_used_items
+ while index < entries_len:
+ entry = entries[index]
+ is_valid = entries.valid(index)
+ index = index + 1
+ if is_valid:
+ iter.index = index
+ if RETURNTYPE is lltype.Void:
+ return None
+ elif kind == 'items':
+ r = lltype.malloc(RETURNTYPE.TO)
+ r.item0 = recast(RETURNTYPE.TO.item0, entry.key)
+ r.item1 = recast(RETURNTYPE.TO.item1, entry.value)
+ return r
+ elif kind == 'keys':
+ return entry.key
+ elif kind == 'values':
+ return entry.value
+
+ # clear the reference to the dict and prevent restarts
+ iter.dict = lltype.nullptr(lltype.typeOf(iter).TO.dict.TO)
raise StopIteration
+
return ll_dictnext
ll_dictnext_group = {'keys' : _make_ll_dictnext('keys'),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit