Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r76458:31c12715fa13 Date: 2015-03-18 18:19 +0100 http://bitbucket.org/pypy/pypy/changeset/31c12715fa13/
Log: Complain more cleanly about reversed(x) or enumerate(x) for objects of unsupported type diff --git a/rpython/rtyper/lltypesystem/rlist.py b/rpython/rtyper/lltypesystem/rlist.py --- a/rpython/rtyper/lltypesystem/rlist.py +++ b/rpython/rtyper/lltypesystem/rlist.py @@ -57,7 +57,7 @@ elif variant == ("reversed",): return ReversedListIteratorRepr(self) else: - raise NotImplementedError(variant) + raise TyperError("unsupported %r iterator over a list" % (variant,)) def get_itemarray_lowleveltype(self): ITEM = self.item_repr.lowleveltype diff --git a/rpython/rtyper/lltypesystem/rrange.py b/rpython/rtyper/lltypesystem/rrange.py --- a/rpython/rtyper/lltypesystem/rrange.py +++ b/rpython/rtyper/lltypesystem/rrange.py @@ -59,7 +59,10 @@ self.ll_newrange = ll_newrange self.ll_newrangest = ll_newrangest - def make_iterator_repr(self): + def make_iterator_repr(self, variant=None): + if variant is not None: + raise TyperError("unsupported %r iterator over a range list" % + (variant,)) return RangeIteratorRepr(self) diff --git a/rpython/rtyper/lltypesystem/rstr.py b/rpython/rtyper/lltypesystem/rstr.py --- a/rpython/rtyper/lltypesystem/rstr.py +++ b/rpython/rtyper/lltypesystem/rstr.py @@ -156,7 +156,10 @@ self.CACHE[value] = p return p - def make_iterator_repr(self): + def make_iterator_repr(self, variant=None): + if variant is not None: + raise TyperError("unsupported %r iterator over a str/unicode" % + (variant,)) return self.repr.iterator_repr def can_ll_be_null(self, s_value): diff --git a/rpython/rtyper/rtuple.py b/rpython/rtyper/rtuple.py --- a/rpython/rtyper/rtuple.py +++ b/rpython/rtyper/rtuple.py @@ -210,7 +210,10 @@ ll_str = property(gen_str_function) - def make_iterator_repr(self): + def make_iterator_repr(self, variant=None): + if variant is not None: + raise TyperError("unsupported %r iterator over a tuple" % + (variant,)) if len(self.items_r) == 1: # subclasses are supposed to set the IteratorRepr attribute return self.IteratorRepr(self) diff --git a/rpython/rtyper/test/test_rstr.py b/rpython/rtyper/test/test_rstr.py --- a/rpython/rtyper/test/test_rstr.py +++ b/rpython/rtyper/test/test_rstr.py @@ -116,6 +116,16 @@ res = self.interpret(fn, [1]) assert res == 1 + ord('a') + 10000 + def test_str_iterator_reversed_unsupported(self): + const = self.const + def fn(): + total = 0 + t = const('foo') + for x in reversed(t): + total += ord(x) + return total + py.test.raises(TyperError, self.interpret, fn, []) + def test_char_constant(self): const = self.const def fn(s): diff --git a/rpython/rtyper/test/test_rtuple.py b/rpython/rtyper/test/test_rtuple.py --- a/rpython/rtyper/test/test_rtuple.py +++ b/rpython/rtyper/test/test_rtuple.py @@ -1,8 +1,10 @@ +import py from rpython.rtyper.rtuple import TUPLE_TYPE, TupleRepr from rpython.rtyper.lltypesystem.lltype import Signed, Bool from rpython.rtyper.rbool import bool_repr from rpython.rtyper.rint import signed_repr from rpython.rtyper.test.tool import BaseRtypingTest +from rpython.rtyper.error import TyperError from rpython.rlib.objectmodel import compute_hash from rpython.translator.translator import TranslationContext @@ -228,6 +230,15 @@ res = self.interpret(f, [93813]) assert res == 93813 + def test_tuple_iterator_reversed_unsupported(self): + def f(i): + total = 0 + t = (i,) + for x in reversed(t): + total += x + return total + py.test.raises(TyperError, self.interpret, f, [93813]) + def test_inst_tuple_iter(self): class A: pass _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit