Use dict arg values for unicode checks in ContextFormatter

In ContextFormatter.format(), for python2 it checks each arg to
determine whether unicode should be used for the format message.
The problem is the code assumes the args are a list, when they can
also be a dict, for example:

  LOG.info('%(thing)s', {'thing': '...'})

and in that case, the dict keys were implicitly being used for the
checks. The checks will always pass on string dict keys, so the
format message gets converted to unicode even though the corresponding
args will ultimately fail decoding to unicode. Then, the logging fails
with:

  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc6 in
  position 0: ordinal not in range(128)

when the unicode format message causes an implicit conversion attempt
of the args to unicode [1].

This adds a check for the args type and uses the dict values for the
unicode checks so that dict args with values that fail decoding will
have: should_use_unicode = False.

Closes-Bug: #1580728

[1] https://github.com/python/cpython/blob/2e576f5/Lib/logging/__init__.py#L338

Change-Id: I8c479e507efcf8acd3e3faa4a702fa6e1f18772f
This commit is contained in:
melanie witt 2017-05-04 20:22:47 +00:00
parent ce484ec3ff
commit 72e5c3c1e2
2 changed files with 9 additions and 1 deletions

View File

@ -270,7 +270,9 @@ class ContextFormatter(logging.Formatter):
if six.PY2:
should_use_unicode = True
for arg in record.args or []:
args = (record.args.values() if isinstance(record.args, dict)
else record.args)
for arg in args or []:
try:
six.text_type(arg)
except UnicodeDecodeError:

View File

@ -737,6 +737,12 @@ class ContextFormatterTestCase(LogTestBase):
self.log.info(b'%s', u'\u2622'.encode('utf8'))
self.assertIn(expected, self.stream.getvalue())
def test_dict_args_with_unicode(self):
msg = '%(thing)s'
arg = {'thing': '\xc6\x91\xc6\xa1\xc6\xa1'}
self.log.info(msg, arg)
self.assertIn(arg['thing'], self.stream.getvalue())
class ExceptionLoggingTestCase(LogTestBase):
"""Test that Exceptions are logged."""