1. python_mirrors
  2. sandbox/loewis

Commits

Nick Coghlan  committed a812de6 Draft

Issue #12643: Respect sys.excepthook in code.InteractiveConsole

  • Participants
  • Parent commits 5b1b9cf
  • Branches default

Comments (0)

Files changed (5)

File Lib/code.py

View file
         The output is written by self.write(), below.
 
         """
-        type, value, sys.last_traceback = sys.exc_info()
+        type, value, tb = sys.exc_info()
         sys.last_type = type
         sys.last_value = value
+        sys.last_traceback = tb
         if filename and type is SyntaxError:
             # Work hard to stuff the correct filename in the exception
             try:
                 # Stuff in the right filename
                 value = SyntaxError(msg, (filename, lineno, offset, line))
                 sys.last_value = value
-        lines = traceback.format_exception_only(type, value)
-        self.write(''.join(lines))
+        if sys.excepthook is sys.__excepthook__:
+            lines = traceback.format_exception_only(type, value)
+            self.write(''.join(lines))
+        else:
+            # If someone has set sys.excepthook, we let that take precedence
+            # over self.write
+            sys.excepthook(type, value, tb)
 
     def showtraceback(self):
         """Display the exception that just occurred.
             lines.extend(traceback.format_exception_only(type, value))
         finally:
             tblist = tb = None
-        self.write(''.join(lines))
+        if sys.excepthook is sys.__excepthook__:
+            self.write(''.join(lines))
+        else:
+            # If someone has set sys.excepthook, we let that take precedence
+            # over self.write
+            sys.excepthook(type, value, tb)
 
     def write(self, data):
         """Write a string.

File Lib/test/test_code_module.py

View file
+"Test InteractiveConsole and InteractiveInterpreter from code module"
+import sys
+import unittest
+from contextlib import ExitStack
+from unittest import mock
+from test import support
+
+code = support.import_module('code')
+
+
+class TestInteractiveConsole(unittest.TestCase):
+
+    def setUp(self):
+        self.console = code.InteractiveConsole()
+        self.mock_sys()
+
+    def mock_sys(self):
+        "Mock system environment for InteractiveConsole"
+        # use exit stack to match patch context managers to addCleanup
+        stack = ExitStack()
+        self.addCleanup(stack.close)
+        self.infunc = stack.enter_context(mock.patch('code.input',
+                                          create=True))
+        self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))
+        self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))
+        prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)
+        self.sysmod = stack.enter_context(prepatch)
+        if sys.excepthook is sys.__excepthook__:
+            self.sysmod.excepthook = self.sysmod.__excepthook__
+
+    def test_ps1(self):
+        self.infunc.side_effect = EOFError('Finished')
+        self.console.interact()
+        self.assertEqual(self.sysmod.ps1, '>>> ')
+
+    def test_ps2(self):
+        self.infunc.side_effect = EOFError('Finished')
+        self.console.interact()
+        self.assertEqual(self.sysmod.ps2, '... ')
+
+    def test_console_stderr(self):
+        self.infunc.side_effect = ["'antioch'", "", EOFError('Finished')]
+        self.console.interact()
+        for call in list(self.stdout.method_calls):
+            if 'antioch' in ''.join(call[1]):
+                break
+        else:
+            raise AssertionError("no console stdout")
+
+    def test_syntax_error(self):
+        self.infunc.side_effect = ["undefined", EOFError('Finished')]
+        self.console.interact()
+        for call in self.stderr.method_calls:
+            if 'NameError:' in ''.join(call[1]):
+                break
+        else:
+            raise AssertionError("No syntax error from console")
+
+    def test_sysexcepthook(self):
+        self.infunc.side_effect = ["raise ValueError('')",
+                                    EOFError('Finished')]
+        hook = mock.Mock()
+        self.sysmod.excepthook = hook
+        self.console.interact()
+        self.assertTrue(hook.called)
+
+
+def test_main():
+    support.run_unittest(TestInteractiveConsole)
+
+if __name__ == "__main__":
+    unittest.main()

File Lib/test/test_sundry.py

View file
         with support.check_warnings(quiet=True):
             import bdb
             import cgitb
-            import code
 
             import distutils.bcppcompiler
             import distutils.ccompiler

File Misc/ACKS

View file
 Catalin Iacob
 Mihai Ibanescu
 Ali Ikinci
+Aaron Iles
 Lars Immisch
 Bobby Impollonia
 Meador Inge

File Misc/NEWS

View file
 Library
 -------
 
+- Issue #12643: code.InteractiveConsole now respects sys.excepthook when
+  displaying exceptions (Patch by Aaron Iles)
+
 - Issue #13579: string.Formatter now understands the 'a' conversion specifier.
 
 - Issue #15595: Fix subprocess.Popen(universal_newlines=True)