Commits

Amaury Forgeot d'Arc  committed 25f5ab9

annotation of range(): if start and step are non-negative,
then the item is non-negative as well.

  • Participants
  • Parent commits 1a29f25
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/annotation/builtin.py

         s_item = s_ImpossibleValue
     else:
         nonneg = False # so far
-        if step > 0:
+        if step > 0 or s_step.nonneg:
             nonneg = s_start.nonneg
         elif step < 0:
             nonneg = s_stop.nonneg or (s_stop.is_constant() and

File pypy/annotation/test/test_annrpython.py

         assert isinstance(s, annmodel.SomeInteger)
         assert s.nonneg
 
+    def test_range_nonneg_variablestep(self):
+        def get_step(n):
+            if n == 1:
+                return 2
+            else:
+                return 3
+        def fun(n, k):
+            step = get_step(n)
+            for i in range(0, n, step):
+                if k == 17:
+                    return i
+            return 0
+        a = self.RPythonAnnotator()
+        s = a.build_types(fun, [int, int])
+        assert isinstance(s, annmodel.SomeInteger)
+        assert s.nonneg
+
     def test_reverse_range_nonneg(self):
         def fun(n, k):
             for i in range(n-1, -1, -1):