[pypy-svn] r7373 - pypy/trunk/src/pypy/objspace/flow

arigo at codespeak.net arigo at codespeak.net
Thu Nov 18 13:18:47 CET 2004


Author: arigo
Date: Thu Nov 18 13:18:47 2004
New Revision: 7373

Modified:
   pypy/trunk/src/pypy/objspace/flow/objspace.py
Log:
More hacking of unpackiterable() to use len() and getitem() instead of
iter() and next().  Helps with unpacking tuples of a length known (later) by
the annotator.


Modified: pypy/trunk/src/pypy/objspace/flow/objspace.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/flow/objspace.py	(original)
+++ pypy/trunk/src/pypy/objspace/flow/objspace.py	Thu Nov 18 13:18:47 2004
@@ -22,7 +22,8 @@
         self.w_None     = Constant(None)
         self.w_False    = Constant(False)
         self.w_True     = Constant(True)
-        for exc in [KeyError, ValueError, IndexError, StopIteration]:
+        for exc in [KeyError, ValueError, IndexError, StopIteration,
+                    AssertionError]:
             clsname = exc.__name__
             setattr(self, 'w_'+clsname, Constant(exc))
         self.specialcases = {}
@@ -143,17 +144,22 @@
         if isinstance(w_iterable, Variable) and expected_length is None:
             # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK
             print ("*** cannot unpack a Variable iterable "
-                   "without knowing its length, assuming up to 7 items")
-            w_iterator = self.iter(w_iterable)
+                   "without knowing its length,")
+            print "    assuming a list or tuple with up to 7 items"
             items = []
-            for i in range(7):
-                try:
-                    w_item = self.next(w_iterator)
-                except OperationError, e:
-                    if not e.match(self, self.w_StopIteration):
-                        raise
+            w_len = self.len(w_iterable)
+            i = 0
+            while True:
+                w_i = self.wrap(i)
+                w_cond = self.eq(w_len, w_i)
+                if self.is_true(w_cond):
                     break  # done
+                if i == 7:
+                    # too many values
+                    raise OperationError(self.w_AssertionError, self.w_None)
+                w_item = self.do_operation('getitem', w_iterable, w_i)
                 items.append(w_item)
+                i += 1
             return items
             # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK
         return ObjSpace.unpackiterable(self, w_iterable, expected_length)



More information about the Pypy-commit mailing list