summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-08-20 13:02:28 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-08-20 13:02:28 (GMT)
commit8bd24fe9ff1c4d5dd3929465fc208b1375c32981 (patch)
treed9a69112450f0b01c7062a92ea87618e4af386a5 /Lib
parentc0c03be53e15cd2e724808d83031d939da8fa57b (diff)
downloadcpython-8bd24fe9ff1c4d5dd3929465fc208b1375c32981.zip
cpython-8bd24fe9ff1c4d5dd3929465fc208b1375c32981.tar.gz
cpython-8bd24fe9ff1c4d5dd3929465fc208b1375c32981.tar.bz2
Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Diffstat (limited to 'Lib')
-rw-r--r--Lib/code.py19
-rw-r--r--Lib/test/test_code_module.py72
-rw-r--r--Lib/test/test_sundry.py1
3 files changed, 87 insertions, 5 deletions
diff --git a/Lib/code.py b/Lib/code.py
index 605aede..9020aab 100644
--- 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()
+ 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:
@@ -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)
- 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.
@@ -143,7 +149,12 @@ class InteractiveInterpreter:
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.
diff --git a/Lib/test/test_code_module.py b/Lib/test/test_code_module.py
new file mode 100644
index 0000000..adef170
--- /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 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()
diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py
index 07802d6..fcccdf7 100644
--- a/Lib/test/test_sundry.py
+++ b/Lib/test/test_sundry.py
@@ -9,7 +9,6 @@ class TestUntestedModules(unittest.TestCase):
with support.check_warnings(quiet=True):
import bdb
import cgitb
- import code
import distutils.bcppcompiler
import distutils.ccompiler