|
5 | 5 | raise unittest.SkipTest("test only relevant on win32") |
6 | 6 |
|
7 | 7 |
|
| 8 | +import subprocess |
| 9 | +from tempfile import TemporaryDirectory |
| 10 | +import os |
| 11 | +import time |
8 | 12 | import itertools |
9 | 13 | from functools import partial |
10 | 14 | from test.support import force_not_colorized_test_class |
@@ -577,5 +581,40 @@ def test_up_vt(self): |
577 | 581 | self.assertEqual(self.mock.call_count, 3) |
578 | 582 |
|
579 | 583 |
|
| 584 | +class WindowsCommandLineTests(unittest.TestCase): |
| 585 | + def test_for_crash_traceback_with_redirected_stdout(self): |
| 586 | + """python.bat -i -c "print('hlwd')" > file.txt""" |
| 587 | + script_command = "print('script has run')" |
| 588 | + |
| 589 | + with TemporaryDirectory() as tmp_dir: |
| 590 | + stdout_path = os.path.join(tmp_dir, "WinCMDLineTests.txt") |
| 591 | + |
| 592 | + with open(stdout_path, "w", encoding="utf-8") as stdout_file, \ |
| 593 | + subprocess.Popen( |
| 594 | + [sys.executable, '-i', '-c', script_command], |
| 595 | + stdin=None, |
| 596 | + stdout=stdout_file, |
| 597 | + stderr=subprocess.PIPE, |
| 598 | + text=True, encoding='utf-8', errors='replace' |
| 599 | + ) as process: |
| 600 | + |
| 601 | + time.sleep(3) |
| 602 | + |
| 603 | + if process.poll() is None: |
| 604 | + process.kill() |
| 605 | + |
| 606 | + stderr_output = process.stderr.read() |
| 607 | + |
| 608 | + has_crash_traceback = ( |
| 609 | + "OSError" in stderr_output |
| 610 | + and len(stderr_output) > 1200 |
| 611 | + ) |
| 612 | + |
| 613 | + if has_crash_traceback: |
| 614 | + self.fail( |
| 615 | + "Detected the endless OSError traceback.\n" |
| 616 | + f"Stderr was:\n{stderr_output[:1200]}" |
| 617 | + ) |
| 618 | + |
580 | 619 | if __name__ == "__main__": |
581 | 620 | unittest.main() |
0 commit comments