annotate Lib/test/test_code_module.py @ 81343:3a7cd8efc19f

#16852: merge with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Thu, 10 Jan 2013 03:29:45 +0200
parents a812de69b493
children 2933598a7802
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
78672
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
1 "Test InteractiveConsole and InteractiveInterpreter from code module"
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
2 import sys
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
3 import unittest
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
4 from contextlib import ExitStack
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
5 from unittest import mock
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
6 from test import support
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
7
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
8 code = support.import_module('code')
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
9
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
10
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
11 class TestInteractiveConsole(unittest.TestCase):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
12
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
13 def setUp(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
14 self.console = code.InteractiveConsole()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
15 self.mock_sys()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
16
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
17 def mock_sys(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
18 "Mock system environment for InteractiveConsole"
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
19 # use exit stack to match patch context managers to addCleanup
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20 stack = ExitStack()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
21 self.addCleanup(stack.close)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
22 self.infunc = stack.enter_context(mock.patch('code.input',
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
23 create=True))
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
24 self.stdout = stack.enter_context(mock.patch('code.sys.stdout'))
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
25 self.stderr = stack.enter_context(mock.patch('code.sys.stderr'))
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
26 prepatch = mock.patch('code.sys', wraps=code.sys, spec=code.sys)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
27 self.sysmod = stack.enter_context(prepatch)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
28 if sys.excepthook is sys.__excepthook__:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
29 self.sysmod.excepthook = self.sysmod.__excepthook__
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
30
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
31 def test_ps1(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
32 self.infunc.side_effect = EOFError('Finished')
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
33 self.console.interact()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
34 self.assertEqual(self.sysmod.ps1, '>>> ')
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
35
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
36 def test_ps2(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
37 self.infunc.side_effect = EOFError('Finished')
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
38 self.console.interact()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
39 self.assertEqual(self.sysmod.ps2, '... ')
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
40
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
41 def test_console_stderr(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
42 self.infunc.side_effect = ["'antioch'", "", EOFError('Finished')]
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
43 self.console.interact()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
44 for call in list(self.stdout.method_calls):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
45 if 'antioch' in ''.join(call[1]):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
46 break
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
47 else:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
48 raise AssertionError("no console stdout")
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
49
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
50 def test_syntax_error(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
51 self.infunc.side_effect = ["undefined", EOFError('Finished')]
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
52 self.console.interact()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
53 for call in self.stderr.method_calls:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
54 if 'NameError:' in ''.join(call[1]):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
55 break
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
56 else:
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
57 raise AssertionError("No syntax error from console")
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
58
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
59 def test_sysexcepthook(self):
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
60 self.infunc.side_effect = ["raise ValueError('')",
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
61 EOFError('Finished')]
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
62 hook = mock.Mock()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
63 self.sysmod.excepthook = hook
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
64 self.console.interact()
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
65 self.assertTrue(hook.called)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
66
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
67
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
68 def test_main():
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
69 support.run_unittest(TestInteractiveConsole)
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
70
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
71 if __name__ == "__main__":
a812de69b493 Issue #12643: Respect sys.excepthook in code.InteractiveConsole
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
72 unittest.main()