|
4 | 4 |
|
5 | 5 | import test.support |
6 | 6 | from test.support import verbose, strip_python_stderr, import_module, cpython_only |
7 | | -from test.script_helper import assert_python_ok |
| 7 | +from test.script_helper import assert_python_ok, assert_python_failure |
8 | 8 |
|
9 | 9 | import random |
10 | 10 | import re |
|
15 | 15 | import unittest |
16 | 16 | import weakref |
17 | 17 | import os |
18 | | -from test.script_helper import assert_python_ok, assert_python_failure |
19 | 18 | import subprocess |
20 | 19 |
|
21 | 20 | from test import lock_tests |
@@ -962,6 +961,88 @@ def outer(): |
962 | 961 | self.assertEqual(p.returncode, 0, "Unexpected error: " + stderr.decode()) |
963 | 962 | self.assertEqual(data, expected_output) |
964 | 963 |
|
| 964 | + def test_print_exception(self): |
| 965 | + script = r"""if True: |
| 966 | + import threading |
| 967 | + import time |
| 968 | +
|
| 969 | + running = False |
| 970 | + def run(): |
| 971 | + global running |
| 972 | + running = True |
| 973 | + while running: |
| 974 | + time.sleep(0.01) |
| 975 | + 1/0 |
| 976 | + t = threading.Thread(target=run) |
| 977 | + t.start() |
| 978 | + while not running: |
| 979 | + time.sleep(0.01) |
| 980 | + running = False |
| 981 | + t.join() |
| 982 | + """ |
| 983 | + rc, out, err = assert_python_ok("-c", script) |
| 984 | + self.assertEqual(out, b'') |
| 985 | + err = err.decode() |
| 986 | + self.assertIn("Exception in thread", err) |
| 987 | + self.assertIn("Traceback (most recent call last):", err) |
| 988 | + self.assertIn("ZeroDivisionError", err) |
| 989 | + self.assertNotIn("Unhandled exception", err) |
| 990 | + |
| 991 | + def test_print_exception_stderr_is_none_1(self): |
| 992 | + script = r"""if True: |
| 993 | + import sys |
| 994 | + import threading |
| 995 | + import time |
| 996 | +
|
| 997 | + running = False |
| 998 | + def run(): |
| 999 | + global running |
| 1000 | + running = True |
| 1001 | + while running: |
| 1002 | + time.sleep(0.01) |
| 1003 | + 1/0 |
| 1004 | + t = threading.Thread(target=run) |
| 1005 | + t.start() |
| 1006 | + while not running: |
| 1007 | + time.sleep(0.01) |
| 1008 | + sys.stderr = None |
| 1009 | + running = False |
| 1010 | + t.join() |
| 1011 | + """ |
| 1012 | + rc, out, err = assert_python_ok("-c", script) |
| 1013 | + self.assertEqual(out, b'') |
| 1014 | + err = err.decode() |
| 1015 | + self.assertIn("Exception in thread", err) |
| 1016 | + self.assertIn("Traceback (most recent call last):", err) |
| 1017 | + self.assertIn("ZeroDivisionError", err) |
| 1018 | + self.assertNotIn("Unhandled exception", err) |
| 1019 | + |
| 1020 | + def test_print_exception_stderr_is_none_2(self): |
| 1021 | + script = r"""if True: |
| 1022 | + import sys |
| 1023 | + import threading |
| 1024 | + import time |
| 1025 | +
|
| 1026 | + running = False |
| 1027 | + def run(): |
| 1028 | + global running |
| 1029 | + running = True |
| 1030 | + while running: |
| 1031 | + time.sleep(0.01) |
| 1032 | + 1/0 |
| 1033 | + sys.stderr = None |
| 1034 | + t = threading.Thread(target=run) |
| 1035 | + t.start() |
| 1036 | + while not running: |
| 1037 | + time.sleep(0.01) |
| 1038 | + running = False |
| 1039 | + t.join() |
| 1040 | + """ |
| 1041 | + rc, out, err = assert_python_ok("-c", script) |
| 1042 | + self.assertEqual(out, b'') |
| 1043 | + self.assertNotIn("Unhandled exception", err.decode()) |
| 1044 | + |
| 1045 | + |
965 | 1046 | class TimerTests(BaseTestCase): |
966 | 1047 |
|
967 | 1048 | def setUp(self): |
|
0 commit comments