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
* #3987: Changing theme from alabaster causes HTML build to fail
* #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
--------

View File

@ -356,11 +356,15 @@ class WarningIsErrorFilter(logging.Filter):
return True
elif self.app.warningiserror:
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:
raise SphinxWarning(location + ":" + message % record.args)
raise SphinxWarning(location + ":" + message)
else:
raise SphinxWarning(message % record.args)
raise SphinxWarning(message)
else:
return True

View File

@ -165,7 +165,11 @@ def test_warningiserror(app, status, warning):
# if True, warning raises SphinxWarning exception
app.warningiserror = True
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):