Author: Armin Rigo <[email protected]>
Branch:
Changeset: r52903:70a3702714bf
Date: 2012-02-25 22:21 +0100
http://bitbucket.org/pypy/pypy/changeset/70a3702714bf/
Log: Fix for 183a045d52d1: iterate over non-mutated lists using a non-
mutating getitem.
diff --git a/pypy/rpython/lltypesystem/rlist.py
b/pypy/rpython/lltypesystem/rlist.py
--- a/pypy/rpython/lltypesystem/rlist.py
+++ b/pypy/rpython/lltypesystem/rlist.py
@@ -392,7 +392,11 @@
('list', r_list.lowleveltype),
('index', Signed)))
self.ll_listiter = ll_listiter
- self.ll_listnext = ll_listnext
+ if (isinstance(r_list, FixedSizeListRepr)
+ and not r_list.listitem.mutated):
+ self.ll_listnext = ll_listnext_foldable
+ else:
+ self.ll_listnext = ll_listnext
self.ll_getnextindex = ll_getnextindex
def ll_listiter(ITERPTR, lst):
@@ -409,5 +413,14 @@
iter.index = index + 1 # cannot overflow because index < l.length
return l.ll_getitem_fast(index)
+def ll_listnext_foldable(iter):
+ from pypy.rpython.rlist import ll_getitem_foldable_nonneg
+ l = iter.list
+ index = iter.index
+ if index >= l.ll_length():
+ raise StopIteration
+ iter.index = index + 1 # cannot overflow because index < l.length
+ return ll_getitem_foldable_nonneg(l, index)
+
def ll_getnextindex(iter):
return iter.index
diff --git a/pypy/rpython/test/test_rlist.py b/pypy/rpython/test/test_rlist.py
--- a/pypy/rpython/test/test_rlist.py
+++ b/pypy/rpython/test/test_rlist.py
@@ -8,6 +8,7 @@
from pypy.rpython.rlist import *
from pypy.rpython.lltypesystem.rlist import ListRepr, FixedSizeListRepr,
ll_newlist, ll_fixed_newlist
from pypy.rpython.lltypesystem import rlist as ll_rlist
+from pypy.rpython.llinterp import LLException
from pypy.rpython.ootypesystem import rlist as oo_rlist
from pypy.rpython.rint import signed_repr
from pypy.objspace.flow.model import Constant, Variable
@@ -1477,6 +1478,80 @@
assert func1.oopspec == 'list.getitem_foldable(l, index)'
assert not hasattr(func2, 'oopspec')
+ def test_iterate_over_immutable_list(self):
+ from pypy.rpython import rlist
+ class MyException(Exception):
+ pass
+ lst = list('abcdef')
+ def dummyfn():
+ total = 0
+ for c in lst:
+ total += ord(c)
+ return total
+ #
+ prev = rlist.ll_getitem_foldable_nonneg
+ try:
+ def seen_ok(l, index):
+ if index == 5:
+ raise KeyError # expected case
+ return prev(l, index)
+ rlist.ll_getitem_foldable_nonneg = seen_ok
+ e = raises(LLException, self.interpret, dummyfn, [])
+ assert 'KeyError' in str(e.value)
+ finally:
+ rlist.ll_getitem_foldable_nonneg = prev
+
+ def test_iterate_over_immutable_list_quasiimmut_attr(self):
+ from pypy.rpython import rlist
+ class MyException(Exception):
+ pass
+ class Foo:
+ _immutable_fields_ = ['lst?[*]']
+ lst = list('abcdef')
+ foo = Foo()
+ def dummyfn():
+ total = 0
+ for c in foo.lst:
+ total += ord(c)
+ return total
+ #
+ prev = rlist.ll_getitem_foldable_nonneg
+ try:
+ def seen_ok(l, index):
+ if index == 5:
+ raise KeyError # expected case
+ return prev(l, index)
+ rlist.ll_getitem_foldable_nonneg = seen_ok
+ e = raises(LLException, self.interpret, dummyfn, [])
+ assert 'KeyError' in str(e.value)
+ finally:
+ rlist.ll_getitem_foldable_nonneg = prev
+
+ def test_iterate_over_mutable_list(self):
+ from pypy.rpython import rlist
+ class MyException(Exception):
+ pass
+ lst = list('abcdef')
+ def dummyfn():
+ total = 0
+ for c in lst:
+ total += ord(c)
+ lst[0] = 'x'
+ return total
+ #
+ prev = rlist.ll_getitem_foldable_nonneg
+ try:
+ def seen_ok(l, index):
+ if index == 5:
+ raise KeyError # expected case
+ return prev(l, index)
+ rlist.ll_getitem_foldable_nonneg = seen_ok
+ res = self.interpret(dummyfn, [])
+ assert res == sum(map(ord, 'abcdef'))
+ finally:
+ rlist.ll_getitem_foldable_nonneg = prev
+
+
class TestOOtype(BaseTestRlist, OORtypeMixin):
rlist = oo_rlist
type_system = 'ootype'
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit