|
1 | 1 | "Test InteractiveConsole and InteractiveInterpreter from code module" |
2 | 2 | import sys |
3 | 3 | import unittest |
| 4 | +from textwrap import dedent |
4 | 5 | from contextlib import ExitStack |
5 | 6 | from unittest import mock |
6 | 7 | from test import support |
@@ -78,6 +79,40 @@ def test_banner(self): |
78 | 79 | self.console.interact(banner='') |
79 | 80 | self.assertEqual(len(self.stderr.method_calls), 1) |
80 | 81 |
|
| 82 | + def test_cause_tb(self): |
| 83 | + self.infunc.side_effect = ["raise ValueError('') from AttributeError", |
| 84 | + EOFError('Finished')] |
| 85 | + self.console.interact() |
| 86 | + output = ''.join(''.join(call[1]) for call in self.stderr.method_calls) |
| 87 | + expected = dedent(""" |
| 88 | + AttributeError |
| 89 | +
|
| 90 | + The above exception was the direct cause of the following exception: |
| 91 | +
|
| 92 | + Traceback (most recent call last): |
| 93 | + File "<console>", line 1, in <module> |
| 94 | + ValueError |
| 95 | + """) |
| 96 | + self.assertIn(expected, output) |
| 97 | + |
| 98 | + def test_context_tb(self): |
| 99 | + self.infunc.side_effect = ["try: ham\nexcept: eggs\n", |
| 100 | + EOFError('Finished')] |
| 101 | + self.console.interact() |
| 102 | + output = ''.join(''.join(call[1]) for call in self.stderr.method_calls) |
| 103 | + expected = dedent(""" |
| 104 | + Traceback (most recent call last): |
| 105 | + File "<console>", line 1, in <module> |
| 106 | + NameError: name 'ham' is not defined |
| 107 | +
|
| 108 | + During handling of the above exception, another exception occurred: |
| 109 | +
|
| 110 | + Traceback (most recent call last): |
| 111 | + File "<console>", line 2, in <module> |
| 112 | + NameError: name 'eggs' is not defined |
| 113 | + """) |
| 114 | + self.assertIn(expected, output) |
| 115 | + |
81 | 116 |
|
82 | 117 | def test_main(): |
83 | 118 | support.run_unittest(TestInteractiveConsole) |
|
0 commit comments