Author: Ronan Lamy <[email protected]>
Branch: raises(Exception)-must-die
Changeset: r80175:0b8a0b568238
Date: 2015-10-13 20:54 +0100
http://bitbucket.org/pypy/pypy/changeset/0b8a0b568238/
Log: make some tests and exceptions more precise
diff --git a/rpython/annotator/bookkeeper.py b/rpython/annotator/bookkeeper.py
--- a/rpython/annotator/bookkeeper.py
+++ b/rpython/annotator/bookkeeper.py
@@ -8,12 +8,12 @@
from contextlib import contextmanager
from rpython.flowspace.model import Constant
-from rpython.annotator.model import (SomeOrderedDict,
- SomeString, SomeChar, SomeFloat, unionof, SomeInstance, SomeDict,
- SomeBuiltin, SomePBC, SomeInteger, TLS, SomeUnicodeCodePoint,
+from rpython.annotator.model import (
+ SomeOrderedDict, SomeString, SomeChar, SomeFloat, unionof, SomeInstance,
+ SomeDict, SomeBuiltin, SomePBC, SomeInteger, TLS, SomeUnicodeCodePoint,
s_None, s_ImpossibleValue, SomeBool, SomeTuple,
SomeImpossibleValue, SomeUnicodeString, SomeList, HarmlesslyBlocked,
- SomeWeakRef, SomeByteArray, SomeConstantType, SomeProperty)
+ SomeWeakRef, SomeByteArray, SomeConstantType, SomeProperty, AnnotatorError)
from rpython.annotator.classdef import InstanceSource, ClassDef
from rpython.annotator.listdef import ListDef, ListItem
from rpython.annotator.dictdef import DictDef
@@ -225,7 +225,8 @@
x = int(x)
result = SomeInteger(nonneg = x>=0)
else:
- raise Exception("seeing a prebuilt long (value %s)" % hex(x))
+ # XXX: better error reporting?
+ raise ValueError("seeing a prebuilt long (value %s)" % hex(x))
elif issubclass(tp, str): # py.lib uses annotated str subclasses
no_nul = not '\x00' in x
if len(x) == 1:
diff --git a/rpython/annotator/classdef.py b/rpython/annotator/classdef.py
--- a/rpython/annotator/classdef.py
+++ b/rpython/annotator/classdef.py
@@ -112,14 +112,10 @@
for desc in s_newvalue.descriptions:
if desc.selfclassdef is None:
if homedef.classdesc.settled:
- raise Exception("demoting method %s "
- "to settled class %s not "
- "allowed" %
- (self.name, homedef)
- )
- #self.bookkeeper.warning("demoting method %s "
- # "to base class %s" %
- # (self.name, homedef))
+ raise AnnotatorError(
+ "demoting method %s to settled class "
+ "%s not allowed" % (self.name, homedef)
+ )
break
# check for attributes forbidden by slots or _attrs_
diff --git a/rpython/annotator/description.py b/rpython/annotator/description.py
--- a/rpython/annotator/description.py
+++ b/rpython/annotator/description.py
@@ -801,8 +801,9 @@
s_init = basedesc.s_read_attribute('__init__')
parent_has_init = isinstance(s_init, SomePBC)
if has_init and not parent_has_init:
- raise Exception("some subclasses among %r declare __init__(),"
- " but not the common parent class" % (descs,))
+ raise AnnotatorError(
+ "some subclasses among %r declare __init__(),"
+ " but not the common parent class" % (descs,))
# make a PBC of MethodDescs, one for the __init__ of each class
initdescs = []
for desc, classdef in zip(descs, classdefs):
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
@@ -4,10 +4,12 @@
from rpython.conftest import option
from rpython.annotator import model as annmodel
+from rpython.annotator.model import AnnotatorError, UnionError
from rpython.annotator.annrpython import RPythonAnnotator as _RPythonAnnotator
+from rpython.annotator.classdef import NoSuchAttrError
from rpython.translator.translator import graphof as tgraphof
from rpython.annotator.policy import AnnotatorPolicy
-from rpython.annotator.signature import Sig
+from rpython.annotator.signature import Sig, SignatureError
from rpython.annotator.listdef import ListDef, ListChangeUnallowed
from rpython.annotator.dictdef import DictDef
from rpython.flowspace.model import *
@@ -213,7 +215,7 @@
def f():
return X().meth()
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [])
+ py.test.raises(AnnotatorError, a.build_types, f, [])
def test_methodcall1(self):
a = self.RPythonAnnotator()
@@ -360,7 +362,7 @@
def f(l):
return g(*l)
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.AnnotatorError):
+ with py.test.raises(AnnotatorError):
a.build_types(f, [[int]])
def test_star_unpack_and_keywords(self):
@@ -960,7 +962,7 @@
def f():
return large_constant
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(ValueError):
a.build_types(f, [])
# if you want to get a r_uint, you have to be explicit about it
@@ -968,7 +970,7 @@
def f(a, b):
return a + b
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(UnionError):
a.build_types(f, [r_uint, int])
def test_merge_different_ints(self):
@@ -979,7 +981,7 @@
c = b
return c
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(UnionError):
a.build_types(f, [r_uint, int])
def test_merge_ruint_zero(self):
@@ -2616,14 +2618,14 @@
def f():
return A()
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [])
+ py.test.raises(AnnotatorError, a.build_types, f, [])
#
class B(object):
pass
x = B()
def g():
return isinstance(x, A)
- py.test.raises(annmodel.AnnotatorError, a.build_types, g, [])
+ py.test.raises(AnnotatorError, a.build_types, g, [])
def test_import_from_mixin(self):
class M(object):
@@ -2698,7 +2700,7 @@
return a.x # should explode here
a = self.RPythonAnnotator()
- with py.test.raises(Exception) as excinfo:
+ with py.test.raises(NoSuchAttrError) as excinfo:
a.build_types(f, [int])
# this should explode on reading the attribute 'a.x', but it can
# sometimes explode on 'self.x = x', which does not make much sense.
@@ -2933,7 +2935,7 @@
s = a.build_types(fun, [s_nonneg, s_nonneg])
assert isinstance(s, annmodel.SomeInteger)
assert not s.nonneg
- with py.test.raises(Exception):
+ with py.test.raises(SignatureError):
a.build_types(fun, [int, int])
def test_sig_simpler(self):
@@ -2946,7 +2948,7 @@
s = a.build_types(fun, [s_nonneg, s_nonneg])
assert isinstance(s, annmodel.SomeInteger)
assert not s.nonneg
- with py.test.raises(Exception):
+ with py.test.raises(SignatureError):
a.build_types(fun, [int, int])
def test_sig_lambda(self):
@@ -2961,7 +2963,7 @@
s = a.build_types(fun, [int, s_nonneg])
assert isinstance(s, annmodel.SomeInteger)
assert not s.nonneg
- with py.test.raises(Exception):
+ with py.test.raises(SignatureError):
a.build_types(fun, [s_nonneg, int])
def test_sig_bug(self):
@@ -3012,8 +3014,8 @@
if works:
a.build_types(fun, [int])
else:
- from rpython.annotator.classdef import NoSuchAttrError
- py.test.raises(NoSuchAttrError, a.build_types, fun, [int])
+ with py.test.raises(NoSuchAttrError):
+ a.build_types(fun, [int])
def test_slots_enforce_attrs(self):
class Superbase(object):
@@ -3146,7 +3148,7 @@
return a.n()
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(AnnotatorError):
a.build_types(fun, [bool])
def test_float_cmp(self):
@@ -3236,6 +3238,7 @@
assert isinstance(s.items[2], annmodel.SomeInstance)
assert s.items[2].flags == {}
+ @py.test.mark.xfail
def test_no_access_directly_on_heap(self):
from rpython.rlib.jit import hint
@@ -3252,7 +3255,7 @@
i.x = x
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(AnnotatorError):
a.build_types(f, [])
@@ -3277,7 +3280,7 @@
c.m.l.append(x)
a = self.RPythonAnnotator()
- py.test.raises(AssertionError, a.build_types, f, [])
+ py.test.raises(AnnotatorError, a.build_types, f, [])
def f():
x = A()
@@ -3285,7 +3288,7 @@
c.m.d[None] = x
a = self.RPythonAnnotator()
- py.test.raises(AssertionError, a.build_types, f, [])
+ py.test.raises(AnnotatorError, a.build_types, f, [])
def f():
x = A()
@@ -3293,7 +3296,7 @@
c.m.d[x] = None
a = self.RPythonAnnotator()
- py.test.raises(AssertionError, a.build_types, f, [])
+ py.test.raises(AnnotatorError, a.build_types, f, [])
def test_ctr_location(self):
class A:
@@ -3352,7 +3355,7 @@
if g(x, y):
g(x, r_uint(y))
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(UnionError):
a.build_types(f, [int, int])
def test_compare_with_zero(self):
@@ -3475,22 +3478,22 @@
return '%s' % unichr(x)
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int])
+ py.test.raises(AnnotatorError, a.build_types, f, [int])
def f(x):
return '%s' % (unichr(x) * 3)
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int])
+ py.test.raises(AnnotatorError, a.build_types, f, [int])
def f(x):
return '%s%s' % (1, unichr(x))
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int])
+ py.test.raises(AnnotatorError, a.build_types, f, [int])
def f(x):
return '%s%s' % (1, unichr(x) * 15)
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, a.build_types, f, [int])
+ py.test.raises(AnnotatorError, a.build_types, f, [int])
def test_strformatting_tuple(self):
@@ -3528,7 +3531,7 @@
return [1, 2, 3][s:e]
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, "a.build_types(f, [int, int])")
+ py.test.raises(AnnotatorError, "a.build_types(f, [int, int])")
a.build_types(f, [annmodel.SomeInteger(nonneg=True),
annmodel.SomeInteger(nonneg=True)])
def f(x):
@@ -3541,20 +3544,20 @@
return "xyz".find("x", s, e)
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError, "a.build_types(f, [int, int])")
+ py.test.raises(AnnotatorError, "a.build_types(f, [int, int])")
a.build_types(f, [annmodel.SomeInteger(nonneg=True),
annmodel.SomeInteger(nonneg=True)])
def f(s, e):
return "xyz".rfind("x", s, e)
- py.test.raises(annmodel.AnnotatorError, "a.build_types(f, [int, int])")
+ py.test.raises(AnnotatorError, "a.build_types(f, [int, int])")
a.build_types(f, [annmodel.SomeInteger(nonneg=True),
annmodel.SomeInteger(nonneg=True)])
def f(s, e):
return "xyz".count("x", s, e)
- py.test.raises(annmodel.AnnotatorError, "a.build_types(f, [int, int])")
+ py.test.raises(AnnotatorError, "a.build_types(f, [int, int])")
a.build_types(f, [annmodel.SomeInteger(nonneg=True),
annmodel.SomeInteger(nonneg=True)])
@@ -3728,7 +3731,8 @@
raise Exception(lle)
# ^^^ instead, must cast back from a base ptr to an instance
a = self.RPythonAnnotator()
- py.test.raises(AssertionError, a.build_types, f, [])
+ with py.test.raises(AssertionError):
+ a.build_types(f, [])
def test_enumerate(self):
def f():
@@ -4113,7 +4117,7 @@
e = cls()
e.foo = "bar"
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(NoSuchAttrError):
a.build_types(fn, [])
def test_lower_char(self):
@@ -4226,7 +4230,7 @@
return "bbb"
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f, [int])
the_exc = exc.value
@@ -4242,7 +4246,7 @@
return (1, 2)
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f, [int])
assert "RPython cannot unify tuples of different length: 2 versus 1"
in exc.value.msg
@@ -4255,7 +4259,7 @@
return -1
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f, [int])
assert ("RPython cannot prove that these integers are of the "
@@ -4272,7 +4276,7 @@
return B()
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f, [int])
assert ("RPython cannot unify instances with no common base class"
@@ -4288,7 +4292,7 @@
return d.itervalues()
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f, [int])
assert ("RPython cannot unify incompatible iterator variants" in
@@ -4300,7 +4304,7 @@
a = A()
return getattr(a, y)
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.AnnotatorError) as exc:
+ with py.test.raises(AnnotatorError) as exc:
a.build_types(f, [str])
assert ("variable argument to getattr" in exc.value.msg)
@@ -4308,7 +4312,7 @@
def f(x):
return x()
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.AnnotatorError) as exc:
+ with py.test.raises(AnnotatorError) as exc:
a.build_types(f, [str])
assert ("Cannot prove that the object is callable" in exc.value.msg)
@@ -4317,7 +4321,7 @@
def f(x):
l.append(x)
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as excinfo:
+ with py.test.raises(UnionError) as excinfo:
a.build_types(f, [int])
assert 'Happened at file' in excinfo.value.source
assert 'Known variable annotations:' in excinfo.value.source
@@ -4326,7 +4330,7 @@
def f(s, x):
return s.format(x)
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.AnnotatorError) as exc:
+ with py.test.raises(AnnotatorError) as exc:
a.build_types(f, [str, str])
assert ("format() is not RPython" in exc.value.msg)
@@ -4362,7 +4366,7 @@
def f(x):
a, b = x
a = self.RPythonAnnotator()
- py.test.raises(annmodel.AnnotatorError,
+ py.test.raises(AnnotatorError,
a.build_types, f, [annmodel.s_None])
def test_class___name__(self):
@@ -4476,10 +4480,10 @@
o = O2(n)
o.x = 20
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f1, [int])
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.UnionError) as exc:
+ with py.test.raises(UnionError) as exc:
a.build_types(f2, [int])
def test_property_union_2(self):
@@ -4508,7 +4512,7 @@
a = self.RPythonAnnotator()
# Ideally, this should translate to something sensible,
# but for now, AnnotatorError is better than silently mistranslating.
- with py.test.raises(annmodel.AnnotatorError):
+ with py.test.raises(AnnotatorError):
a.build_types(f, [int])
def test_property_union_3(self):
@@ -4528,7 +4532,7 @@
obj = B()
return obj.x
a = self.RPythonAnnotator()
- with py.test.raises(annmodel.AnnotatorError):
+ with py.test.raises(AnnotatorError):
a.build_types(f, [int])
def test_dict_can_be_none_ordering_issue(self):
diff --git a/rpython/annotator/test/test_annsimplifyrpython.py
b/rpython/annotator/test/test_annsimplifyrpython.py
--- a/rpython/annotator/test/test_annsimplifyrpython.py
+++ b/rpython/annotator/test/test_annsimplifyrpython.py
@@ -3,6 +3,7 @@
from rpython.annotator.test.test_annrpython import graphof
from rpython.annotator.test.test_annrpython import TestAnnotateTestCase as
parent
+from rpython.annotator.model import AnnotatorError
class TestAnnotateAndSimplifyTestCase(parent):
@@ -132,5 +133,5 @@
cls = C
return cls().foo
a = self.RPythonAnnotator()
- with py.test.raises(Exception):
+ with py.test.raises(AnnotatorError):
a.build_types(f, [int])
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit