diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 275f7ce47d09b5..95f0f2494aca7e 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4570,6 +4570,9 @@ def setUp(self): self.variants = { 'custom': { 'custom': 1234 + }, + 'exception': { + 'exc_info': ZeroDivisionError() } } @@ -4909,6 +4912,34 @@ def test_relativeCreated_has_higher_precision(self): # After PR gh-102412, precision (places) increases from 3 to 7 self.assertAlmostEqual(relativeCreated, offset_ns / 1e6, places=7) + def test_multiple_formatters_exc_text(self): + # Regression test for current behavior where exc_text is only set if it is None + r = self.get_record() + r.exc_info = (ZeroDivisionError, ZeroDivisionError(), None) + + f = logging.Formatter('${%(message)s}') + f.format(r) + self.assertIsNotNone(r.exc_text) + exc_text = r.exc_text + + f = logging.Formatter('%(asctime)s') + f.format(r) + self.assertEqual(exc_text, r.exc_text) + + def test_multiple_formatters_set_exc_text(self): + # Tests that exc_text is changed when set_exc_text option is set on the Formatter + r = self.get_record() + r.exc_info = (ZeroDivisionError, ZeroDivisionError(), None) + + f = logging.Formatter('${%(message)s}') + f.format(r) + self.assertIsNotNone(r.exc_text) + exc_text = r.exc_text + + f = logging.Formatter('%(asctime)s', set_exc_text=True) + f.format(r) + self.assertNotEqual(exc_text, r.exc_text) + class TestBufferingFormatter(logging.BufferingFormatter): def formatHeader(self, records): @@ -5010,7 +5041,6 @@ def inner(): class RecordingHandler(logging.NullHandler): - def __init__(self, *args, **kwargs): super(RecordingHandler, self).__init__(*args, **kwargs) self.records = []