|
18 | 18 | from test.support import (Error, captured_output, cpython_only, ALWAYS_EQ, |
19 | 19 | requires_debug_ranges, has_no_debug_ranges, |
20 | 20 | requires_subprocess) |
21 | | -from test.support.os_helper import TESTFN, unlink |
22 | | -from test.support.script_helper import assert_python_ok, assert_python_failure |
| 21 | +from test.support.os_helper import TESTFN, temp_dir, unlink |
| 22 | +from test.support.script_helper import assert_python_ok, assert_python_failure, make_script |
23 | 23 | from test.support.import_helper import forget |
24 | 24 | from test.support import force_not_colorized, force_not_colorized_test_class |
25 | 25 |
|
@@ -504,6 +504,33 @@ def __del__(self): |
504 | 504 | b'ZeroDivisionError: division by zero'] |
505 | 505 | self.assertEqual(stderr.splitlines(), expected) |
506 | 506 |
|
| 507 | + @cpython_only |
| 508 | + def test_lost_io_open(self): |
| 509 | + # GH-142737: Display the traceback even if io.open is lost |
| 510 | + crasher = textwrap.dedent("""\ |
| 511 | + import io |
| 512 | + import traceback |
| 513 | + # Trigger fallback mode |
| 514 | + traceback._print_exception_bltin = None |
| 515 | + del io.open |
| 516 | + raise RuntimeError("should not crash") |
| 517 | + """) |
| 518 | + |
| 519 | + # Create a temporary script to exercise _Py_FindSourceFile |
| 520 | + with temp_dir() as script_dir: |
| 521 | + script = make_script( |
| 522 | + script_dir=script_dir, |
| 523 | + script_basename='tb_test_no_io_open', |
| 524 | + source=crasher) |
| 525 | + rc, stdout, stderr = assert_python_failure(script) |
| 526 | + |
| 527 | + self.assertEqual(rc, 1) # Make sure it's not a crash |
| 528 | + |
| 529 | + expected = [b'Traceback (most recent call last):', |
| 530 | + f' File "{script}", line 6, in <module>'.encode(), |
| 531 | + b'RuntimeError: should not crash'] |
| 532 | + self.assertEqual(stderr.splitlines(), expected) |
| 533 | + |
507 | 534 | def test_print_exception(self): |
508 | 535 | output = StringIO() |
509 | 536 | traceback.print_exception( |
|
0 commit comments