[pypy-svn] r36327 - in pypy/dist/pypy/annotation: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 9 11:34:17 CET 2007


Author: arigo
Date: Tue Jan  9 11:34:11 2007
New Revision: 36327

Modified:
   pypy/dist/pypy/annotation/builtin.py
   pypy/dist/pypy/annotation/test/test_annrpython.py
Log:
(mwh, arigo)

Avoid crashing with blocked blocks in an empty-range() situation.



Modified: pypy/dist/pypy/annotation/builtin.py
==============================================================================
--- pypy/dist/pypy/annotation/builtin.py	(original)
+++ pypy/dist/pypy/annotation/builtin.py	Tue Jan  9 11:34:11 2007
@@ -57,18 +57,27 @@
         s_step = args[2]
     else:
         raise Exception, "range() takes 1 to 3 arguments"
+    empty = False  # so far
     if not s_step.is_constant():
         step = 0 # this case signals a variable step
     else:
         step = s_step.const
         if step == 0:
             raise Exception, "range() with step zero"
-    nonneg = False # so far
-    if step > 0:
-        nonneg = s_start.nonneg
-    elif step < 0:
-        nonneg = s_stop.nonneg or (s_stop.is_constant() and s_stop.const >= -1)
-    return getbookkeeper().newlist(SomeInteger(nonneg=nonneg), range_step=step)
+        if s_start.is_constant() and s_stop.is_constant():
+            if len(range(s_start.const, s_stop.const, step)) == 0:
+                empty = True
+    if empty:
+        s_item = s_ImpossibleValue
+    else:
+        nonneg = False # so far
+        if step > 0:
+            nonneg = s_start.nonneg
+        elif step < 0:
+            nonneg = s_stop.nonneg or (s_stop.is_constant() and
+                                       s_stop.const >= -1)
+        s_item = SomeInteger(nonneg=nonneg)
+    return getbookkeeper().newlist(s_item, range_step=step)
 
 builtin_xrange = builtin_range # xxx for now allow it
 

Modified: pypy/dist/pypy/annotation/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/annotation/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/annotation/test/test_annrpython.py	Tue Jan  9 11:34:11 2007
@@ -2437,6 +2437,19 @@
         s = a.build_types(fun, [float, float])
         assert [s_item.knowntype for s_item in s.items] == [bool] * 6
 
+    def test_empty_range(self):
+        def g(lst):
+            total = 0
+            for i in range(len(lst)):
+                total += lst[i]
+            return total
+        def fun():
+            return g([])
+
+        a = self.RPythonAnnotator(policy=policy.AnnotatorPolicy())
+        s = a.build_types(fun, [])
+        assert s.const == 0
+
 
 def g(n):
     return [0,1,2,n]



More information about the Pypy-commit mailing list