[pypy-svn] r78381 - in pypy/branch/fast-forward/pypy/translator/goal: . test2

afa at codespeak.net afa at codespeak.net
Thu Oct 28 00:10:10 CEST 2010


Author: afa
Date: Thu Oct 28 00:10:04 2010
New Revision: 78381

Modified:
   pypy/branch/fast-forward/pypy/translator/goal/app_main.py
   pypy/branch/fast-forward/pypy/translator/goal/test2/test_app_main.py
Log:
The command::
   python -i -c "import sys; sys.exit(1)"
- must display the SystemExit traceback
- must enter interactive mode


Modified: pypy/branch/fast-forward/pypy/translator/goal/app_main.py
==============================================================================
--- pypy/branch/fast-forward/pypy/translator/goal/app_main.py	(original)
+++ pypy/branch/fast-forward/pypy/translator/goal/app_main.py	Thu Oct 28 00:10:04 2010
@@ -21,6 +21,25 @@
 
 originalexcepthook = sys.__excepthook__
 
+def handle_sys_exit(e):
+    # exit if we catch a w_SystemExit
+    exitcode = e.code
+    if exitcode is None:
+        exitcode = 0
+    else:
+        try:
+            exitcode = int(exitcode)
+        except:
+            # not an integer: print it to stderr
+            try:
+                stderr = sys.stderr
+            except AttributeError:
+                pass   # too bad
+            else:
+                print >> stderr, exitcode
+            exitcode = 1
+    raise SystemExit(exitcode)
+
 def run_toplevel(f, *fargs, **fkwds):
     """Calls f() and handles all OperationErrors.
     Intended use is to run the main program or one interactive statement.
@@ -44,67 +63,53 @@
                 stdout.write('\n')
 
     except SystemExit, e:
-        # exit if we catch a w_SystemExit
-        exitcode = e.code
-        if exitcode is None:
-            exitcode = 0
-        else:
-            try:
-                exitcode = int(exitcode)
-            except:
-                # not an integer: print it to stderr
-                try:
-                    stderr = sys.stderr
-                except AttributeError:
-                    pass   # too bad
-                else:
-                    print >> stderr, exitcode
-                exitcode = 1
-        raise SystemExit(exitcode)
+        handle_sys_exit(e)
+    except:
+        display_exception()
+        return False
+    return True   # success
+
+def display_exception():
+    etype, evalue, etraceback = sys.exc_info()
+    try:
+        # extra debugging info in case the code below goes very wrong
+        if DEBUG and hasattr(sys, 'stderr'):
+            s = getattr(etype, '__name__', repr(etype))
+            print >> sys.stderr, "debug: exception-type: ", s
+            print >> sys.stderr, "debug: exception-value:", str(evalue)
+            tbentry = etraceback
+            if tbentry:
+                while tbentry.tb_next:
+                    tbentry = tbentry.tb_next
+                lineno = tbentry.tb_lineno
+                filename = tbentry.tb_frame.f_code.co_filename
+                print >> sys.stderr, "debug: exception-tb:    %s:%d" % (
+                    filename, lineno)
+
+        # set the sys.last_xxx attributes
+        sys.last_type = etype
+        sys.last_value = evalue
+        sys.last_traceback = etraceback
+
+        # call sys.excepthook
+        hook = getattr(sys, 'excepthook', originalexcepthook)
+        hook(etype, evalue, etraceback)
+        return # done
 
     except:
-        etype, evalue, etraceback = sys.exc_info()
         try:
-            # extra debugging info in case the code below goes very wrong
-            if DEBUG and hasattr(sys, 'stderr'):
-                s = getattr(etype, '__name__', repr(etype))
-                print >> sys.stderr, "debug: exception-type: ", s
-                print >> sys.stderr, "debug: exception-value:", str(evalue)
-                tbentry = etraceback
-                if tbentry:
-                    while tbentry.tb_next:
-                        tbentry = tbentry.tb_next
-                    lineno = tbentry.tb_lineno
-                    filename = tbentry.tb_frame.f_code.co_filename
-                    print >> sys.stderr, "debug: exception-tb:    %s:%d" % (
-                        filename, lineno)
-
-            # set the sys.last_xxx attributes
-            sys.last_type = etype
-            sys.last_value = evalue
-            sys.last_traceback = etraceback
-
-            # call sys.excepthook
-            hook = getattr(sys, 'excepthook', originalexcepthook)
-            hook(etype, evalue, etraceback)
-            return False   # done
-
-        except:
-            try:
-                stderr = sys.stderr
-            except AttributeError:
-                pass   # too bad
-            else:
-                print >> stderr, 'Error calling sys.excepthook:'
-                originalexcepthook(*sys.exc_info())
-                print >> stderr
-                print >> stderr, 'Original exception was:'
+            stderr = sys.stderr
+        except AttributeError:
+            pass   # too bad
+        else:
+            print >> stderr, 'Error calling sys.excepthook:'
+            originalexcepthook(*sys.exc_info())
+            print >> stderr
+            print >> stderr, 'Original exception was:'
 
-        # we only get here if sys.excepthook didn't do its job
-        originalexcepthook(etype, evalue, etraceback)
-        return False
+    # we only get here if sys.excepthook didn't do its job
+    originalexcepthook(etype, evalue, etraceback)
 
-    return True   # success
 
 # ____________________________________________________________
 # Option parsing
@@ -481,14 +486,25 @@
             sys.path.insert(0, scriptdir)
             success = run_toplevel(execfile, sys.argv[0], mainmodule.__dict__)
 
-        # start a prompt if requested
+    except SystemExit, e:
+        status = e.code
         if inspect_requested():
+            display_exception()
+    else:
+        status = not success
+
+    # start a prompt if requested
+    if inspect_requested():
+        inteactive = False
+        try:
             from _pypy_interact import interactive_console
             success = run_toplevel(interactive_console, mainmodule)
-    except SystemExit, e:
-        return e.code
-    else:
-        return not success
+        except SystemExit, e:
+            status = e.code
+        else:
+            status = not success
+
+    return status
 
 def resolvedirof(filename):
     try:

Modified: pypy/branch/fast-forward/pypy/translator/goal/test2/test_app_main.py
==============================================================================
--- pypy/branch/fast-forward/pypy/translator/goal/test2/test_app_main.py	(original)
+++ pypy/branch/fast-forward/pypy/translator/goal/test2/test_app_main.py	Thu Oct 28 00:10:04 2010
@@ -241,6 +241,11 @@
         child.sendline('"pypy.translator.goal.test2.mymodule" in sys.modules')
         child.expect('False')
 
+    def test_option_i_noexit(self):
+        child = self.spawn(['-i', '-c', 'import sys; sys.exit(1)'])
+        child.expect('Traceback')
+        child.expect('SystemExit: 1')
+
     def test_options_u_i(self):
         if sys.platform == "win32":
             skip("close_fds is not supported on Windows platforms")



More information about the Pypy-commit mailing list