[pypy-svn] pypy fast-forward: Change OperationError.errorstr() to format errors like

amauryfa commits-noreply at bitbucket.org
Tue Jan 4 14:54:03 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40383:1de01c8ed895
Date: 2011-01-04 14:52 +0100
http://bitbucket.org/pypy/pypy/changeset/1de01c8ed895/

Log:	Change OperationError.errorstr() to format errors like
	exc_type.__name__: repr(exc_value) this function is mostly used by
	write_unraisable(), which now looks more like CPython.

diff --git a/pypy/interpreter/test/test_interpreter.py b/pypy/interpreter/test/test_interpreter.py
--- a/pypy/interpreter/test/test_interpreter.py
+++ b/pypy/interpreter/test/test_interpreter.py
@@ -1,13 +1,12 @@
 import py 
 import sys
+from pypy.interpreter import gateway, module, error
 
 class TestInterpreter: 
 
     def codetest(self, source, functionname, args):
         """Compile and run the given code string, and then call its function
         named by 'functionname' with arguments 'args'."""
-        from pypy.interpreter import baseobjspace
-        from pypy.interpreter import pyframe, gateway, module
         space = self.space
 
         source = str(py.code.Source(source).strip()) + '\n'
@@ -27,7 +26,7 @@
         wrappedfunc = space.getitem(w_glob, w(functionname))
         try:
             w_output = space.call_function(wrappedfunc, *wrappedargs)
-        except baseobjspace.OperationError, e:
+        except error.OperationError, e:
             #e.print_detailed_traceback(space)
             return '<<<%s>>>' % e.errorstr(space)
         else:
@@ -67,12 +66,14 @@
         assert self.codetest(code, 'f', [0]) == -12
         assert self.codetest(code, 'f', [1]) == 1
 
-##     def test_raise(self):
-##         x = self.codetest('''
-## def f():
-##     raise 1
-## ''', 'f', [])
-##         self.assertEquals(x, '<<<TypeError: exceptions must be instances or subclasses of Exception or strings (deprecated), not int>>>')
+    def test_raise(self):
+        x = self.codetest('''
+            def f():
+                raise 1
+            ''', 'f', [])
+        assert "TypeError:" in x
+        assert ("exceptions must be old-style classes "
+                "or derived from BaseException") in x
 
     def test_except2(self):
         x = self.codetest('''
@@ -101,11 +102,9 @@
                 '''
         assert self.codetest(code, 'f', [2]) == 0
         assert self.codetest(code, 'f', [0]) == "infinite result"
-        ess = "TypeError: unsupported operand type"
         res = self.codetest(code, 'f', ['x'])
-        assert res.find(ess) >= 0
-        # the following (original) test was a bit too strict...:
-        # self.assertEquals(self.codetest(code, 'f', ['x']), "<<<TypeError: unsupported operand type(s) for //: 'int' and 'str'>>>")
+        assert "TypeError:" in res
+        assert "unsupported operand type" in res
 
     def test_break(self):
         code = '''

diff --git a/pypy/interpreter/test/test_error.py b/pypy/interpreter/test/test_error.py
--- a/pypy/interpreter/test/test_error.py
+++ b/pypy/interpreter/test/test_error.py
@@ -34,3 +34,7 @@
 
 def test_operationerrfmt_empty():
     py.test.raises(AssertionError, operationerrfmt, "w_type", "foobar")
+
+def test_errorstr(space):
+    operr = OperationError(space.w_ValueError, space.wrap("message"))
+    assert operr.errorstr(space) == "ValueError: 'message'"

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -74,7 +74,7 @@
                 exc_value = ""
             else:
                 try:
-                    exc_value = space.str_w(space.str(w_value))
+                    exc_value = space.str_w(space.repr(w_value))
                 except OperationError:
                     # oups, cannot __str__ the exception object
                     exc_value = "<oups, exception object itself cannot be str'd>"
@@ -230,8 +230,8 @@
                 objrepr = space.str_w(space.repr(w_object))
             except OperationError:
                 objrepr = '?'
-        msg = 'Exception "%s" in %s%s ignored\n' % (self.errorstr(space),
-                                                    where, objrepr)
+        msg = 'Exception %s in %s%s ignored\n' % (self.errorstr(space),
+                                                  where, objrepr)
         try:
             space.call_method(space.sys.get('stderr'), 'write', space.wrap(msg))
         except OperationError:


More information about the Pypy-commit mailing list