diff -r 4feb10457c13 Lib/code.py --- a/Lib/code.py Sun Aug 19 17:45:40 2012 -0400 +++ b/Lib/code.py Mon Aug 20 18:07:31 2012 +1000 @@ -119,8 +119,11 @@ # 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: + sys.excepthook(sys.last_type, sys.last_value, sys.last_traceback) def showtraceback(self): """Display the exception that just occurred. @@ -143,7 +146,10 @@ 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: + sys.excepthook(sys.last_type, sys.last_value, sys.last_traceback) def write(self, data): """Write a string. diff -r 4feb10457c13 Lib/test/test_code_module.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lib/test/test_code_module.py Mon Aug 20 18:07:31 2012 +1000 @@ -0,0 +1,79 @@ +"Test InteractiveConsole and InteractiveInterpreter from code module" +import builtins +import sys +import unittest +from contextlib import ExitStack +from unittest import mock +from test import support + +code = support.import_module('code') + + +def mock_sys_module(func): + "Mock system environment for InteractiveConsole" + def wrapper(*args, **kwargs): + with ExitStack() as stack: + args = list(args) + args.append(stack.enter_context(mock.patch('builtins.input'))) + args.append(stack.enter_context(mock.patch('code.sys.stdout'))) + args.append(stack.enter_context(mock.patch('code.sys.stderr'))) + sysmock = mock.patch('code.sys', wraps=code.sys, spec=code.sys) + sysmock = stack.enter_context(sysmock) + args.append(sysmock) + if sys.excepthook is sys.__excepthook__: + sysmock.excepthook = sysmock.__excepthook__ + return func(*args, **kwargs) + return wrapper + + +class TestInteractiveConsole(unittest.TestCase): + + def setUp(self): + self.console = code.InteractiveConsole() + + @mock_sys_module + def test_ps1(self, infunc, stdout, stderr, sysmod): + infunc.side_effect = EOFError('Finished') + self.console.interact() + self.assertEqual(sysmod.ps1, '>>> ') + + @mock_sys_module + def test_ps2(self, infunc, stdout, stderr, sysmod): + infunc.side_effect = EOFError('Finished') + self.console.interact() + self.assertEqual(sysmod.ps2, '... ') + + @mock_sys_module + def test_console_stderr(self, infunc, stdout, stderr, sysmod): + infunc.side_effect = ["'antioch'", "", EOFError('Finished')] + self.console.interact() + for call in list(stdout.method_calls): + if 'antioch' in ''.join(call[1]): + break + else: + raise AssertionError("no console stdout") + + @mock_sys_module + def test_syntax_error(self, infunc, stdout, stderr, sysmod): + infunc.side_effect = ["undefined", EOFError('Finished')] + self.console.interact() + for call in stderr.method_calls: + if 'NameError:' in ''.join(call[1]): + break + else: + raise AssertionError("No syntax error from console") + + @mock_sys_module + def test_sysexcepthook(self, infunc, stdout, stderr, sysmod): + infunc.side_effect = ["raise ValueError('')", EOFError('Finished')] + hook = mock.Mock() + sysmod.excepthook = hook + self.console.interact() + self.assertTrue(hook.called) + + +def test_main(): + support.run_unittest(InteractiveConsolePrompt) + +if __name__ == "__main__": + unittest.main() diff -r 4feb10457c13 Lib/test/test_sundry.py --- a/Lib/test/test_sundry.py Sun Aug 19 17:45:40 2012 -0400 +++ b/Lib/test/test_sundry.py Mon Aug 20 18:07:31 2012 +1000 @@ -9,7 +9,6 @@ with support.check_warnings(quiet=True): import bdb import cgitb - import code import distutils.bcppcompiler import distutils.ccompiler