|
4 | 4 | from io import StringIO
|
5 | 5 | import linecache
|
6 | 6 | import sys
|
| 7 | +import types |
7 | 8 | import inspect
|
8 | 9 | import unittest
|
9 | 10 | import re
|
@@ -1129,7 +1130,7 @@ def test_print_exception_bad_type_python(self):
|
1129 | 1130 | class BaseExceptionReportingTests:
|
1130 | 1131 |
|
1131 | 1132 | def get_exception(self, exception_or_callable):
|
1132 |
| - if isinstance(exception_or_callable, Exception): |
| 1133 | + if isinstance(exception_or_callable, BaseException): |
1133 | 1134 | return exception_or_callable
|
1134 | 1135 | try:
|
1135 | 1136 | exception_or_callable()
|
@@ -1851,6 +1852,31 @@ def exc():
|
1851 | 1852 | report = self.get_report(exc)
|
1852 | 1853 | self.assertEqual(report, expected)
|
1853 | 1854 |
|
| 1855 | + def test_KeyboardInterrupt_at_first_line_of_frame(self): |
| 1856 | + # see GH-93249 |
| 1857 | + def f(): |
| 1858 | + return sys._getframe() |
| 1859 | + |
| 1860 | + tb_next = None |
| 1861 | + frame = f() |
| 1862 | + lasti = 0 |
| 1863 | + lineno = f.__code__.co_firstlineno |
| 1864 | + tb = types.TracebackType(tb_next, frame, lasti, lineno) |
| 1865 | + |
| 1866 | + exc = KeyboardInterrupt() |
| 1867 | + exc.__traceback__ = tb |
| 1868 | + |
| 1869 | + expected = (f'Traceback (most recent call last):\n' |
| 1870 | + f' File "{__file__}", line {lineno}, in f\n' |
| 1871 | + f' def f():\n' |
| 1872 | + f'\n' |
| 1873 | + f'KeyboardInterrupt\n') |
| 1874 | + |
| 1875 | + report = self.get_report(exc) |
| 1876 | + # remove trailing writespace: |
| 1877 | + report = '\n'.join([l.rstrip() for l in report.split('\n')]) |
| 1878 | + self.assertEqual(report, expected) |
| 1879 | + |
1854 | 1880 |
|
1855 | 1881 | class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
|
1856 | 1882 | #
|
|
0 commit comments