Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ def _checkLevel(level):
rv = level
elif str(level) == level:
if level not in _nameToLevel:
raise ValueError("Unknown level: %r" % level)
raise ValueError(f"Unknown level: {repr(level)}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get to this code you would need to have passed the test str(level) == level, how would a tuple do this? I wouldn't think this line needs changing.

rv = _nameToLevel[level]
else:
raise TypeError("Level not an integer or a valid string: %r" % level)
raise TypeError("Level not an integer or a valid string: "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be "Level not an integer or a valid string: %s" % repr(level) which should always work. I'm not against f-strings, but there is no need to introduce them here for this purpose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with "...: %r" % (level,)" since it's closer to the old code, but happy to push a commit to go with your variant if you want

f"{repr(level)}")
return rv

#---------------------------------------------------------------------------
Expand Down
33 changes: 20 additions & 13 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3608,7 +3608,15 @@ def tzname(self, dt):

utc = UTC()

class FormatterTest(unittest.TestCase):
class AssertErrorMessage:

def assert_error_message(self, exception, message, *args, **kwargs):
try:
self.assertRaises((), *args, **kwargs)
except exception as e:
self.assertEqual(message, str(e))

class FormatterTest(unittest.TestCase, AssertErrorMessage):
def setUp(self):
self.common = {
'name': 'formatter.test',
Expand All @@ -3629,12 +3637,6 @@ def get_record(self, name=None):
result.update(self.variants[name])
return logging.makeLogRecord(result)

def assert_error_message(self, exception, message, *args, **kwargs):
try:
self.assertRaises(exception, *args, **kwargs)
except exception as e:
self.assertEqual(message, e.message)

def test_percent(self):
# Test %-formatting
r = self.get_record()
Expand Down Expand Up @@ -3753,7 +3755,7 @@ def test_format_validate(self):
# Testing failure for '-' in field name
self.assert_error_message(
ValueError,
"invalid field name/expression: 'name-thing'",
"invalid format: invalid field name/expression: 'name-thing'",
logging.Formatter, "{name-thing}", style="{"
)
# Testing failure for style mismatch
Expand All @@ -3776,7 +3778,7 @@ def test_format_validate(self):
# Testing failure for invalid spec
self.assert_error_message(
ValueError,
"bad specifier: '.2ff'",
"invalid format: bad specifier: '.2ff'",
logging.Formatter, '{process:.2ff}', style='{'
)
self.assertRaises(ValueError, logging.Formatter, '{process:.2Z}', style='{')
Expand All @@ -3786,12 +3788,12 @@ def test_format_validate(self):
# Testing failure for mismatch braces
self.assert_error_message(
ValueError,
"invalid format: unmatched '{' in format spec",
"invalid format: expected '}' before end of string",
logging.Formatter, '{process', style='{'
)
self.assert_error_message(
ValueError,
"invalid format: unmatched '{' in format spec",
"invalid format: Single '}' encountered in format string",
logging.Formatter, 'process}', style='{'
)
self.assertRaises(ValueError, logging.Formatter, '{{foo!r:4.2}', style='{')
Expand Down Expand Up @@ -4531,7 +4533,7 @@ def process(self, msg, kwargs):
self.assertIs(self.logger.manager, orig_manager)


class LoggerTest(BaseTest):
class LoggerTest(BaseTest, AssertErrorMessage):

def setUp(self):
super(LoggerTest, self).setUp()
Expand All @@ -4543,7 +4545,12 @@ def setUp(self):
self.addCleanup(logging.shutdown)

def test_set_invalid_level(self):
self.assertRaises(TypeError, self.logger.setLevel, object())
self.assert_error_message(
TypeError, 'Level not an integer or a valid string: None',
self.logger.setLevel, None)
self.assert_error_message(
TypeError, 'Level not an integer or a valid string: (0, 0)',
self.logger.setLevel, (0, 0))

def test_exception(self):
msg = 'testing exception: %r'
Expand Down