[pypy-svn] r15636 - pypy/dist/pypy/objspace/std

ale at codespeak.net ale at codespeak.net
Thu Aug 4 18:54:01 CEST 2005


Author: ale
Date: Thu Aug  4 18:54:00 2005
New Revision: 15636

Modified:
   pypy/dist/pypy/objspace/std/dictobject.py
   pypy/dist/pypy/objspace/std/iterobject.py
Log:
Addded some support for len(iter).

This seems to work for list,tuples,str, dict, set

Please review carefully (The W_DictIterObject changes are a bit scary)

Modified: pypy/dist/pypy/objspace/std/dictobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/dictobject.py	(original)
+++ pypy/dist/pypy/objspace/std/dictobject.py	Thu Aug  4 18:54:00 2005
@@ -318,6 +318,7 @@
         w_self.w_dictobject = w_dictobject
         w_self.len = w_dictobject.used
         w_self.pos = 0
+        w_self.datapos = 0
 
     def return_entry(w_self, entry):
         raise NotImplementedError
@@ -348,18 +349,24 @@
             raise OperationError(space.w_RuntimeError,
                      space.wrap("dictionary changed size during iteration"))
         # look for the next entry
-        i = w_dictiter.pos
+        i = w_dictiter.datapos
         data = w_dict.data
         while i < len(data):
             entry = data[i]
             i += 1
             if entry.w_value is not None:
-                w_dictiter.pos = i
+                w_dictiter.pos += 1
+                w_dictiter.datapos = i
                 return w_dictiter.return_entry(entry)
         # no more entries
         w_dictiter.w_dictobject = None
     raise OperationError(space.w_StopIteration, space.w_None)
 
+def len__DictIterObject(space, w_dictiter):
+    w_dict = w_dictiter.w_dictobject
+    if w_dict is None:
+        return space.wrap(0)
+    return space.wrap(w_dictiter.len - w_dictiter.pos)
 # ____________________________________________________________
 
 from pypy.objspace.std import dicttype

Modified: pypy/dist/pypy/objspace/std/iterobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/iterobject.py	(original)
+++ pypy/dist/pypy/objspace/std/iterobject.py	Thu Aug  4 18:54:00 2005
@@ -34,4 +34,10 @@
     w_seqiter.index += 1
     return w_item
 
+def len__SeqIter(space,  w_seqiter):
+    if w_seqiter.w_seq is None:
+        return space.wrap(0)
+    w_len = space.sub(space.len(w_seqiter.w_seq), space.wrap(w_seqiter.index))
+    return w_len
+
 register_all(vars())



More information about the Pypy-commit mailing list