Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r58865:25f5ab99c5e2
Date: 2012-11-12 23:13 +0100
http://bitbucket.org/pypy/pypy/changeset/25f5ab99c5e2/
Log: annotation of range(): if start and step are non-negative, then the
item is non-negative as well.
diff --git a/pypy/annotation/builtin.py b/pypy/annotation/builtin.py
--- a/pypy/annotation/builtin.py
+++ b/pypy/annotation/builtin.py
@@ -79,7 +79,7 @@
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
diff --git a/pypy/annotation/test/test_annrpython.py
b/pypy/annotation/test/test_annrpython.py
--- a/pypy/annotation/test/test_annrpython.py
+++ b/pypy/annotation/test/test_annrpython.py
@@ -2694,6 +2694,23 @@
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):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit