Author: Edd Barrett <vex...@gmail.com> Branch: improve-errors-again Changeset: r66414:c7946783093d Date: 2013-08-28 15:00 +0100 http://bitbucket.org/pypy/pypy/changeset/c7946783093d/
Log: (Edd, Ronan) Change any RPython error that the user should see to a subclass of AnnotatorError. Builtin exceptions should only be used to report errors with RPython itself and not the user program. In doing so we remove ErrorWrapper (yay) diff --git a/rpython/annotator/annrpython.py b/rpython/annotator/annrpython.py --- a/rpython/annotator/annrpython.py +++ b/rpython/annotator/annrpython.py @@ -5,7 +5,7 @@ from rpython.tool.ansi_print import ansi_log from rpython.tool.pairtype import pair from rpython.tool.error import (format_blocked_annotation_error, - gather_error, ErrorWrapper, source_lines) + gather_error, source_lines) from rpython.flowspace.model import (Variable, Constant, FunctionGraph, c_last_exception, checkgraph) from rpython.translator import simplify, transform @@ -136,9 +136,7 @@ checkgraph(flowgraph) nbarg = len(flowgraph.getargs()) - if len(inputcells) != nbarg: - raise TypeError("%s expects %d args, got %d" %( - flowgraph, nbarg, len(inputcells))) + assert len(inputcells) == nbarg # wrong number of args # register the entry point self.addpendinggraph(flowgraph, inputcells) @@ -159,7 +157,7 @@ else: return object else: - raise TypeError, ("Variable or Constant instance expected, " + raise TypeError("Variable or Constant instance expected, " "got %r" % (variable,)) def getuserclassdefinitions(self): @@ -243,7 +241,7 @@ # return annmodel.s_ImpossibleValue return self.bookkeeper.immutableconstant(arg) else: - raise TypeError, 'Variable or Constant expected, got %r' % (arg,) + raise TypeError('Variable or Constant expected, got %r' % (arg,)) def typeannotation(self, t): return signature.annotation(t, self.bookkeeper) @@ -604,12 +602,7 @@ resultcell = consider_meth(*argcells) except annmodel.AnnotatorError as e: # note that UnionError is a subclass graph = self.bookkeeper.position_key[0] - e.source = '\n'.join(source_lines(graph, block, opindex, long=True)) - raise - except Exception, e: - graph = self.bookkeeper.position_key[0] - e.args = e.args + ( - ErrorWrapper(gather_error(self, graph, block, opindex)),) + e.source = gather_error(self, graph, block, opindex) raise if resultcell is None: resultcell = self.noreturnvalue(op) diff --git a/rpython/annotator/argument.py b/rpython/annotator/argument.py --- a/rpython/annotator/argument.py +++ b/rpython/annotator/argument.py @@ -115,9 +115,7 @@ elif num_args > co_argcount: raise ArgErrCount(num_args, num_kwds, signature, defaults_w, 0) - # if a **kwargs argument is needed, explode - if signature.has_kwarg(): - raise TypeError("Keyword arguments as **kwargs is not supported by RPython") + assert not signature.has_kwarg() # XXX should not happen? # handle keyword arguments num_remainingkwds = 0 diff --git a/rpython/annotator/binaryop.py b/rpython/annotator/binaryop.py --- a/rpython/annotator/binaryop.py +++ b/rpython/annotator/binaryop.py @@ -457,7 +457,7 @@ class __extend__(pairtype(SomeString, SomeUnicodeString), pairtype(SomeUnicodeString, SomeString)): def mod((str, unistring)): - raise NotImplementedError( + raise AnnotatorError( "string formatting mixing strings and unicode not supported") @@ -471,7 +471,7 @@ if (is_unicode and isinstance(s_item, (SomeChar, SomeString)) or is_string and isinstance(s_item, (SomeUnicodeCodePoint, SomeUnicodeString))): - raise NotImplementedError( + raise AnnotatorError( "string formatting mixing strings and unicode not supported") getbookkeeper().count('strformat', s_string, s_tuple) no_nul = s_string.no_nul diff --git a/rpython/annotator/builtin.py b/rpython/annotator/builtin.py --- a/rpython/annotator/builtin.py +++ b/rpython/annotator/builtin.py @@ -337,7 +337,7 @@ return SomeAddress() def unicodedata_decimal(s_uchr): - raise TypeError, "unicodedate.decimal() calls should not happen at interp-level" + raise TypeError("unicodedate.decimal() calls should not happen at interp-level") def test(*args): return s_Bool diff --git a/rpython/annotator/classdef.py b/rpython/annotator/classdef.py --- a/rpython/annotator/classdef.py +++ b/rpython/annotator/classdef.py @@ -2,7 +2,7 @@ Type inference for user-defined classes. """ from rpython.annotator.model import SomePBC, s_ImpossibleValue, unionof -from rpython.annotator.model import SomeInteger, SomeTuple, SomeString +from rpython.annotator.model import SomeInteger, SomeTuple, SomeString, AnnotatorError from rpython.annotator import description @@ -429,7 +429,8 @@ result.extend(slots) return result -class NoSuchAttrError(Exception): +#class NoSuchAttrError(Exception): +class NoSuchAttrError(AnnotatorError): """Raised when an attribute is found on a class where __slots__ or _attrs_ forbits it.""" diff --git a/rpython/annotator/description.py b/rpython/annotator/description.py --- a/rpython/annotator/description.py +++ b/rpython/annotator/description.py @@ -6,6 +6,7 @@ from rpython.annotator.argument import rawshape, ArgErr from rpython.tool.sourcetools import valid_identifier, func_with_new_name from rpython.tool.pairtype import extendabletype +from rpython.annotator.model import AnnotatorError class CallFamily(object): """A family of Desc objects that could be called from common call sites. @@ -261,7 +262,7 @@ try: inputcells = args.match_signature(signature, defs_s) except ArgErr, e: - raise TypeError("signature mismatch: %s() %s" % + raise AnnotatorError("signature mismatch: %s() %s" % (self.name, e.getmsg())) return inputcells @@ -678,7 +679,7 @@ value = value.__get__(42) classdef = None # don't bind elif isinstance(value, classmethod): - raise AssertionError("classmethods are not supported") + raise AnnotatorError("classmethods are not supported") s_value = self.bookkeeper.immutablevalue(value) if classdef is not None: s_value = s_value.bind_callables_under(classdef, name) diff --git a/rpython/annotator/test/test_annrpython.py b/rpython/annotator/test/test_annrpython.py --- a/rpython/annotator/test/test_annrpython.py +++ b/rpython/annotator/test/test_annrpython.py @@ -228,7 +228,7 @@ def f(): return X().meth() a = self.RPythonAnnotator() - py.test.raises(AssertionError, a.build_types, f, []) + py.test.raises(annmodel.AnnotatorError, a.build_types, f, []) def test_methodcall1(self): a = self.RPythonAnnotator() @@ -3381,22 +3381,22 @@ return '%s' % unichr(x) a = self.RPythonAnnotator() - py.test.raises(NotImplementedError, a.build_types, f, [int]) + py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int]) def f(x): return '%s' % (unichr(x) * 3) a = self.RPythonAnnotator() - py.test.raises(NotImplementedError, a.build_types, f, [int]) + py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int]) def f(x): return '%s%s' % (1, unichr(x)) a = self.RPythonAnnotator() - py.test.raises(NotImplementedError, a.build_types, f, [int]) + py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int]) def f(x): return '%s%s' % (1, unichr(x) * 15) a = self.RPythonAnnotator() - py.test.raises(NotImplementedError, a.build_types, f, [int]) + py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int]) def test_strformatting_tuple(self): @@ -3434,7 +3434,7 @@ return [1, 2, 3][s:e] a = self.RPythonAnnotator() - py.test.raises(TypeError, "a.build_types(f, [int, int])") + py.test.raises(annmodel.AnnotatorError, "a.build_types(f, [int, int])") a.build_types(f, [annmodel.SomeInteger(nonneg=True), annmodel.SomeInteger(nonneg=True)]) def f(x): @@ -4038,7 +4038,6 @@ s_objs = set([type(the_exc.s_obj1), type(the_exc.s_obj2)]) assert s_objs == set([annmodel.SomeInteger, annmodel.SomeString]) - assert the_exc.msg == None # Check that this is a generic UnionError def test_unionerror_tuple_size(self): def f(x): @@ -4051,7 +4050,7 @@ with py.test.raises(annmodel.UnionError) as exc: a.build_types(f, [int]) - assert exc.value.msg == "RPython cannot unify tuples of different length: 2 versus 1" + assert "RPython cannot unify tuples of different length: 2 versus 1" in exc.value.msg def test_unionerror_signedness(self): def f(x): @@ -4064,8 +4063,8 @@ with py.test.raises(annmodel.UnionError) as exc: a.build_types(f, [int]) - assert exc.value.msg == ("RPython cannot prove that these integers are of " - "the same signedness") + assert ("RPython cannot prove that these integers are of the " + "same signedness" in exc.value.msg) def test_unionerror_instance(self): class A(object): pass @@ -4081,7 +4080,8 @@ with py.test.raises(annmodel.UnionError) as exc: a.build_types(f, [int]) - assert exc.value.msg == ("RPython cannot unify instances with no common base class") + assert ("RPython cannot unify instances with no common base class" + in exc.value.msg) def test_unionerror_iters(self): @@ -4096,7 +4096,8 @@ with py.test.raises(annmodel.UnionError) as exc: a.build_types(f, [int]) - assert exc.value.msg == ("RPython cannot unify incompatible iterator variants") + assert ("RPython cannot unify incompatible iterator variants" in + exc.value.msg) def g(n): return [0, 1, 2, n] diff --git a/rpython/annotator/test/test_argument.py b/rpython/annotator/test/test_argument.py --- a/rpython/annotator/test/test_argument.py +++ b/rpython/annotator/test/test_argument.py @@ -77,10 +77,6 @@ new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() - args = make_arguments_for_translation(space, [1], {'c': 5, 'd': 7}) - sig = Signature(['a', 'b', 'c'], None, 'kw') - py.test.raises(TypeError, args.match_signature, sig, [2, 3]) - def test_rawshape(self): space = DummySpace() args = make_arguments_for_translation(space, [1,2,3]) diff --git a/rpython/annotator/unaryop.py b/rpython/annotator/unaryop.py --- a/rpython/annotator/unaryop.py +++ b/rpython/annotator/unaryop.py @@ -84,8 +84,7 @@ return obj.is_true() def hash(obj): - raise TypeError, ("cannot use hash() in RPython; " - "see objectmodel.compute_xxx()") + raise AnnotatorError("cannot use hash() in RPython") def str(obj): getbookkeeper().count('str', obj) @@ -341,10 +340,10 @@ def check_negative_slice(s_start, s_stop): if isinstance(s_start, SomeInteger) and not s_start.nonneg: - raise TypeError("slicing: not proven to have non-negative start") + raise AnnotatorError("slicing: not proven to have non-negative start") if isinstance(s_stop, SomeInteger) and not s_stop.nonneg and \ getattr(s_stop, 'const', 0) != -1: - raise TypeError("slicing: not proven to have non-negative stop") + raise AnnotatorError("slicing: not proven to have non-negative stop") class __extend__(SomeDict): @@ -529,10 +528,10 @@ class __extend__(SomeUnicodeString): def method_encode(uni, s_enc): if not s_enc.is_constant(): - raise TypeError("Non-constant encoding not supported") + raise AnnotatorError("Non-constant encoding not supported") enc = s_enc.const if enc not in ('ascii', 'latin-1', 'utf-8'): - raise TypeError("Encoding %s not supported for unicode" % (enc,)) + raise AnnotatorError("Encoding %s not supported for unicode" % (enc,)) return SomeString() method_encode.can_only_throw = [UnicodeEncodeError] @@ -562,10 +561,10 @@ def method_decode(str, s_enc): if not s_enc.is_constant(): - raise TypeError("Non-constant encoding not supported") + raise AnnotatorError("Non-constant encoding not supported") enc = s_enc.const if enc not in ('ascii', 'latin-1', 'utf-8'): - raise TypeError("Encoding %s not supported for strings" % (enc,)) + raise AnnotatorError("Encoding %s not supported for strings" % (enc,)) return SomeUnicodeString() method_decode.can_only_throw = [UnicodeDecodeError] diff --git a/rpython/tool/error.py b/rpython/tool/error.py --- a/rpython/tool/error.py +++ b/rpython/tool/error.py @@ -68,16 +68,6 @@ lines = source_lines1(graph, *args, **kwds) return ['In %r:' % (graph,)] + lines -class NoSuchAttrError(Exception): - pass - -class ErrorWrapper(object): - def __init__(self, msg): - self.msg = msg - - def __repr__(self): - return '%s' % (self.msg,) - def gather_error(annotator, graph, block, operindex): msg = [""] _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit