cpython: a812de69b493 (original) (raw)
Mercurial > cpython
changeset 78672:a812de69b493
Issue #12643: Respect sys.excepthook in code.InteractiveConsole [#12643]
Nick Coghlan ncoghlan@gmail.com | |
---|---|
date | Mon, 20 Aug 2012 23:02:28 +1000 |
parents | 5b1b9cfb7fe8 |
children | 663de4dbb88a |
files | Lib/code.py Lib/test/test_code_module.py Lib/test/test_sundry.py Misc/ACKS Misc/NEWS |
diffstat | 5 files changed, 91 insertions(+), 5 deletions(-)[+] [-] Lib/code.py 19 Lib/test/test_code_module.py 72 Lib/test/test_sundry.py 1 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/code.py +++ b/Lib/code.py @@ -105,9 +105,10 @@ class InteractiveInterpreter: The output is written by self.write(), below. """
type, value, sys.last_traceback = sys.exc_info()[](#l1.7)
type, value, tb = sys.exc_info()[](#l1.8) sys.last_type = type[](#l1.9) sys.last_value = value[](#l1.10)
sys.last_traceback = tb[](#l1.11) if filename and type is SyntaxError:[](#l1.12) # Work hard to stuff the correct filename in the exception[](#l1.13) try:[](#l1.14)
@@ -119,8 +120,13 @@ class InteractiveInterpreter: # Stuff in the right filename value = SyntaxError(msg, (filename, lineno, offset, line)) sys.last_value = value
lines = traceback.format_exception_only(type, value)[](#l1.19)
self.write(''.join(lines))[](#l1.20)
if sys.excepthook is sys.__excepthook__:[](#l1.21)
lines = traceback.format_exception_only(type, value)[](#l1.22)
self.write(''.join(lines))[](#l1.23)
else:[](#l1.24)
# If someone has set sys.excepthook, we let that take precedence[](#l1.25)
# over self.write[](#l1.26)
sys.excepthook(type, value, tb)[](#l1.27)
def showtraceback(self): """Display the exception that just occurred. @@ -143,7 +149,12 @@ class InteractiveInterpreter: lines.extend(traceback.format_exception_only(type, value)) finally: tblist = tb = None
self.write(''.join(lines))[](#l1.35)
if sys.excepthook is sys.__excepthook__:[](#l1.36)
self.write(''.join(lines))[](#l1.37)
else:[](#l1.38)
# If someone has set sys.excepthook, we let that take precedence[](#l1.39)
# over self.write[](#l1.40)
sys.excepthook(type, value, tb)[](#l1.41)
def write(self, data): """Write a string.
new file mode 100644 --- /dev/null +++ b/Lib/test/test_code_module.py @@ -0,0 +1,72 @@ +"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 mock_sys(self):
"Mock system environment for InteractiveConsole"[](#l2.22)
# use exit stack to match patch context managers to addCleanup[](#l2.23)
stack = ExitStack()[](#l2.24)
self.addCleanup(stack.close)[](#l2.25)
self.infunc = stack.enter_context(mock.patch('code.input',[](#l2.26)
create=True))[](#l2.27)
self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))[](#l2.28)
self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))[](#l2.29)
prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)[](#l2.30)
self.sysmod = stack.enter_context(prepatch)[](#l2.31)
if sys.excepthook is sys.__excepthook__:[](#l2.32)
self.sysmod.excepthook = self.sysmod.__excepthook__[](#l2.33)
- def test_ps1(self):
self.infunc.side_effect = EOFError('Finished')[](#l2.36)
self.console.interact()[](#l2.37)
self.assertEqual(self.sysmod.ps1, '>>> ')[](#l2.38)
- def test_ps2(self):
self.infunc.side_effect = EOFError('Finished')[](#l2.41)
self.console.interact()[](#l2.42)
self.assertEqual(self.sysmod.ps2, '... ')[](#l2.43)
- def test_console_stderr(self):
self.infunc.side_effect = ["'antioch'", "", EOFError('Finished')][](#l2.46)
self.console.interact()[](#l2.47)
for call in list(self.stdout.method_calls):[](#l2.48)
if 'antioch' in ''.join(call[1]):[](#l2.49)
break[](#l2.50)
else:[](#l2.51)
raise AssertionError("no console stdout")[](#l2.52)
- def test_syntax_error(self):
self.infunc.side_effect = ["undefined", EOFError('Finished')][](#l2.55)
self.console.interact()[](#l2.56)
for call in self.stderr.method_calls:[](#l2.57)
if 'NameError:' in ''.join(call[1]):[](#l2.58)
break[](#l2.59)
else:[](#l2.60)
raise AssertionError("No syntax error from console")[](#l2.61)
- def test_sysexcepthook(self):
self.infunc.side_effect = ["raise ValueError('')",[](#l2.64)
EOFError('Finished')][](#l2.65)
hook = mock.Mock()[](#l2.66)
self.sysmod.excepthook = hook[](#l2.67)
self.console.interact()[](#l2.68)
self.assertTrue(hook.called)[](#l2.69)
--- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -9,7 +9,6 @@ class TestUntestedModules(unittest.TestC with support.check_warnings(quiet=True): import bdb import cgitb
import code[](#l3.7)
import distutils.bcppcompiler import distutils.ccompiler
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -487,6 +487,7 @@ Fredrik Håård Catalin Iacob Mihai Ibanescu Ali Ikinci +Aaron Iles Lars Immisch Bobby Impollonia Meador Inge
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,9 @@ Core and Builtins Library ------- +- Issue #12643: code.InteractiveConsole now respects sys.excepthook when