Skip to content

Commit 591c982

Browse files
authored
gh-128384: Fix for unreliable warnings unit tests. (gh-132611)
When the `showwarning()` function is replaced, make sure to restore it after the test finishes. Add a timeout for `Barrier()` so we don't hang for a long time if something goes wrong.
1 parent c6973ee commit 591c982

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -919,36 +919,44 @@ def test_showwarnmsg_missing(self):
919919
self.assertIn(text, result)
920920

921921
def test_showwarning_not_callable(self):
922-
with self.module.catch_warnings():
923-
self.module.filterwarnings("always", category=UserWarning)
924-
self.module.showwarning = print
925-
with support.captured_output('stdout'):
926-
self.module.warn('Warning!')
927-
self.module.showwarning = 23
928-
self.assertRaises(TypeError, self.module.warn, "Warning!")
922+
orig = self.module.showwarning
923+
try:
924+
with self.module.catch_warnings():
925+
self.module.filterwarnings("always", category=UserWarning)
926+
self.module.showwarning = print
927+
with support.captured_output('stdout'):
928+
self.module.warn('Warning!')
929+
self.module.showwarning = 23
930+
self.assertRaises(TypeError, self.module.warn, "Warning!")
931+
finally:
932+
self.module.showwarning = orig
929933

930934
def test_show_warning_output(self):
931935
# With showwarning() missing, make sure that output is okay.
932-
text = 'test show_warning'
933-
with self.module.catch_warnings():
934-
self.module.filterwarnings("always", category=UserWarning)
935-
del self.module.showwarning
936-
with support.captured_output('stderr') as stream:
937-
warning_tests.inner(text)
938-
result = stream.getvalue()
939-
self.assertEqual(result.count('\n'), 2,
940-
"Too many newlines in %r" % result)
941-
first_line, second_line = result.split('\n', 1)
942-
expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
943-
first_line_parts = first_line.rsplit(':', 3)
944-
path, line, warning_class, message = first_line_parts
945-
line = int(line)
946-
self.assertEqual(expected_file, path)
947-
self.assertEqual(warning_class, ' ' + UserWarning.__name__)
948-
self.assertEqual(message, ' ' + text)
949-
expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
950-
assert expected_line
951-
self.assertEqual(second_line, expected_line)
936+
orig = self.module.showwarning
937+
try:
938+
text = 'test show_warning'
939+
with self.module.catch_warnings():
940+
self.module.filterwarnings("always", category=UserWarning)
941+
del self.module.showwarning
942+
with support.captured_output('stderr') as stream:
943+
warning_tests.inner(text)
944+
result = stream.getvalue()
945+
self.assertEqual(result.count('\n'), 2,
946+
"Too many newlines in %r" % result)
947+
first_line, second_line = result.split('\n', 1)
948+
expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py'
949+
first_line_parts = first_line.rsplit(':', 3)
950+
path, line, warning_class, message = first_line_parts
951+
line = int(line)
952+
self.assertEqual(expected_file, path)
953+
self.assertEqual(warning_class, ' ' + UserWarning.__name__)
954+
self.assertEqual(message, ' ' + text)
955+
expected_line = ' ' + linecache.getline(path, line).strip() + '\n'
956+
assert expected_line
957+
self.assertEqual(second_line, expected_line)
958+
finally:
959+
self.module.showwarning = orig
952960

953961
def test_filename_none(self):
954962
# issue #12467: race condition if a warning is emitted at shutdown
@@ -1640,7 +1648,7 @@ def setUp(self):
16401648
def test_threaded_context(self):
16411649
import threading
16421650

1643-
barrier = threading.Barrier(2)
1651+
barrier = threading.Barrier(2, timeout=2)
16441652

16451653
def run_a():
16461654
with self.module.catch_warnings(record=True) as w:

0 commit comments

Comments
 (0)