[pypy-commit] pypy default: One more try at tweaking this code. Need to turn the RPython exception

arigo pypy.commits at gmail.com
Tue Nov 8 04:59:02 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r88202:474587286cd1
Date: 2016-11-08 10:58 +0100
http://bitbucket.org/pypy/pypy/changeset/474587286cd1/

Log:	One more try at tweaking this code. Need to turn the RPython
	exception into SystemError in gateway.py too, so that it can be
	caught and processed in the topmost frame (code like try: ... except
	SystemError: ... would miss it).

diff --git a/pypy/interpreter/error.py b/pypy/interpreter/error.py
--- a/pypy/interpreter/error.py
+++ b/pypy/interpreter/error.py
@@ -509,3 +509,28 @@
     if module:
         space.setattr(w_exc, space.wrap("__module__"), space.wrap(module))
     return w_exc
+
+def _convert_unexpected_exception_extra(space, e):
+    "NOT_RPYTHON"
+    if e.__class__.__name__ in (
+        'Skipped',     # list of exception class names that are ok
+        ):             # to get during ==untranslated tests== only
+        raise
+    # include the RPython-level traceback
+    exc = sys.exc_info()
+    import traceback, cStringIO
+    f = cStringIO.StringIO()
+    print >> f, "\nTraceback (interpreter-level):"
+    traceback.print_tb(exc[2], file=f)
+    return f.getvalue()
+
+def get_converted_unexpected_exception(space, e):
+    if we_are_translated():
+        from rpython.rlib.debug import debug_print_traceback
+        debug_print_traceback()
+        extra = '; internal traceback was dumped to stderr'
+    else:
+        extra = _convert_unexpected_exception_extra(space, e)
+    return OperationError(space.w_SystemError, space.wrap(
+        "unexpected internal exception (please report a bug): %r%s" %
+        (e, extra)))
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -725,13 +725,8 @@
         except RuntimeError:   # not on top of py.py
             raise OperationError(space.w_RuntimeError, space.w_None)
         except Exception as e:      # general fall-back
-            if we_are_translated():
-                from rpython.rlib.debug import debug_print_traceback
-                debug_print_traceback()
-            # propagate the exception anyway, which will be turned
-            # into a proper OperationError(SystemError) when we
-            # reach PyFrame.execute_frame()
-            raise
+            from pypy.interpreter import error
+            raise error.get_converted_unexpected_exception(space, e)
 
 # (verbose) performance hack below
 
diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py
--- a/pypy/interpreter/pyframe.py
+++ b/pypy/interpreter/pyframe.py
@@ -889,30 +889,10 @@
             frame = frame.f_backref()
         return None
 
-    def _convert_unexpected_exception_extra(self, e):
-        "NOT_RPYTHON"
-        if e.__class__.__name__ in (
-            'Skipped',     # list of exception class names that are ok
-            ):             # to get during ==untranslated tests== only
-            raise
-        # include the RPython-level traceback
-        exc = sys.exc_info()
-        import traceback, cStringIO
-        f = cStringIO.StringIO()
-        print >> f, "\nTraceback (interpreter-level):"
-        traceback.print_tb(exc[2], file=f)
-        return f.getvalue()
+    def _convert_unexpected_exception(self, e):
+        from pypy.interpreter import error
 
-    def _convert_unexpected_exception(self, e):
-        if we_are_translated():
-            from rpython.rlib.debug import debug_print_traceback
-            debug_print_traceback()
-            extra = '; internal traceback(s) were dumped to stderr'
-        else:
-            extra = self._convert_unexpected_exception_extra(e)
-        operr = OperationError(self.space.w_SystemError, self.space.wrap(
-            "unexpected internal exception (please report a bug): %r%s" %
-            (e, extra)))
+        operr = error.get_converted_unexpected_exception(self.space, e)
         pytraceback.record_application_traceback(
             self.space, operr, self, self.last_instr)
         raise operr
diff --git a/pypy/interpreter/test/test_gateway.py b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -787,6 +787,23 @@
         assert ('unexpected internal exception (please '
                 'report a bug): UnexpectedException') in err
 
+    def test_system_error_2(self):
+        class UnexpectedException(Exception):
+            pass
+        space = self.space
+        def g(space):
+            raise UnexpectedException
+        w_g = space.wrap(gateway.interp2app_temp(g))
+        w_msg = space.appexec([w_g], """(my_g):
+            try:
+                my_g()
+            except SystemError as e:
+                return str(e)
+        """)
+        err = space.str_w(w_msg)
+        assert ('unexpected internal exception (please '
+                'report a bug): UnexpectedException') in err
+
 
 class AppTestPyTestMark:
     @py.test.mark.unlikely_to_exist


More information about the pypy-commit mailing list