Fix #4070, #4111: crashes when the warning message contains format strings (again)

This commit is contained in:
Takeshi KOMIYA 2017-10-05 00:00:48 +09:00
parent 4dc46350a7
commit 50640b700b
3 changed files with 13 additions and 4 deletions

View File

@ -22,6 +22,7 @@ Bugs fixed
* #4100: Remove debug print from autodoc extension * #4100: Remove debug print from autodoc extension
* #3987: Changing theme from alabaster causes HTML build to fail * #3987: Changing theme from alabaster causes HTML build to fail
* #4096: C++, don't crash when using the wrong role type. Thanks to mitya57. * #4096: C++, don't crash when using the wrong role type. Thanks to mitya57.
* #4070, #4111: crashes when the warning message contains format strings (again)
Testing Testing
-------- --------

View File

@ -356,11 +356,15 @@ class WarningIsErrorFilter(logging.Filter):
return True return True
elif self.app.warningiserror: elif self.app.warningiserror:
location = getattr(record, 'location', '') location = getattr(record, 'location', '')
message = record.msg.replace('%', '%%') try:
message = record.msg % record.args
except TypeError:
message = record.msg # use record.msg itself
if location: if location:
raise SphinxWarning(location + ":" + message % record.args) raise SphinxWarning(location + ":" + message)
else: else:
raise SphinxWarning(message % record.args) raise SphinxWarning(message)
else: else:
return True return True

View File

@ -165,7 +165,11 @@ def test_warningiserror(app, status, warning):
# if True, warning raises SphinxWarning exception # if True, warning raises SphinxWarning exception
app.warningiserror = True app.warningiserror = True
with pytest.raises(SphinxWarning): with pytest.raises(SphinxWarning):
logger.warning('message') logger.warning('message: %s', 'arg')
# message contains format string (refs: #4070)
with pytest.raises(SphinxWarning):
logger.warning('%s')
def test_warning_location(app, status, warning): def test_warning_location(app, status, warning):