Skip to content

Commit fca2d39

Browse files
committed
Test also logging.captureWarnings(True)
1 parent 26e3e94 commit fca2d39

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from contextlib import contextmanager
22
import linecache
3+
import logging
34
import os
45
import importlib
56
import inspect
@@ -534,6 +535,21 @@ def custom_showwarning(message, category, filename, lineno,
534535
self.assertEqual(len(warns), 1)
535536
self.assertEqual(str(warns[0]), "custom")
536537

538+
def test_catchwarnings_logging(self):
539+
# gh-146358: catch_warnings(record=True) must replace the
540+
# showwarning() function set by logging.captureWarnings(True).
541+
542+
with self.module.catch_warnings():
543+
self.module.resetwarnings()
544+
logging.captureWarnings(True)
545+
546+
with self.module.catch_warnings(record=True) as recorded:
547+
self.module.warn("recorded")
548+
self.assertEqual(len(recorded), 1)
549+
self.assertEqual(str(recorded[0].message), 'recorded')
550+
551+
logging.captureWarnings(False)
552+
537553

538554
class CFilterTests(FilterTests, unittest.TestCase):
539555
module = c_warnings

0 commit comments

Comments
 (0)