[pypy-commit] pypy better-enforceargs: add a better error message

antocuni noreply at buildbot.pypy.org
Tue Jul 17 18:11:00 CEST 2012


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: better-enforceargs
Changeset: r56110:6b02160a6eb6
Date: 2012-07-17 18:08 +0200
http://bitbucket.org/pypy/pypy/changeset/6b02160a6eb6/

Log:	add a better error message

diff --git a/pypy/rlib/objectmodel.py b/pypy/rlib/objectmodel.py
--- a/pypy/rlib/objectmodel.py
+++ b/pypy/rlib/objectmodel.py
@@ -131,13 +131,15 @@
                 return t
             return annotationoftype(t)
         def typecheck(*args):
-            for expected_type, arg in zip(types, args):
+            for i, (expected_type, arg) in enumerate(zip(types, args)):
                 if expected_type is None:
                     continue
                 s_expected = get_annotation(expected_type)
                 s_argtype = get_annotation(type(arg))
                 if not s_expected.contains(s_argtype):
-                    raise TypeError
+                    msg = "%s argument number %d must be of type %s" % (
+                        f.func_name, i+1, expected_type)
+                    raise TypeError, msg
         #
         # we cannot simply wrap the function using *args, **kwds, because it's
         # not RPython. Instead, we generate a function with exactly the same
diff --git a/pypy/rlib/test/test_objectmodel.py b/pypy/rlib/test/test_objectmodel.py
--- a/pypy/rlib/test/test_objectmodel.py
+++ b/pypy/rlib/test/test_objectmodel.py
@@ -424,7 +424,8 @@
     assert f._annenforceargs_ == (int, str, None)
     assert f.func_name == 'f'
     assert f(1, 'hello', 42) == (1, 'hello', 42)
-    py.test.raises(TypeError, "f(1, 2, 3)")
+    exc = py.test.raises(TypeError, "f(1, 2, 3)")
+    assert exc.value.message == "f argument number 2 must be of type <type 'str'>"
     py.test.raises(TypeError, "f('hello', 'world', 3)")
 
 def test_enforceargs_defaults():


More information about the pypy-commit mailing list