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.
Reviewed: https:/ /review. openstack. org/462735 /git.openstack. org/cgit/ openstack/ oslo.log/ commit/ ?id=72e5c3c1e25 8589957011506c3 6dd8af0a481513
Committed: https:/
Submitter: Jenkins
Branch: master
commit 72e5c3c1e258589 957011506c36dd8 af0a481513
Author: melanie witt <email address hidden>
Date: Thu May 4 20:22:47 2017 +0000
Use dict arg values for unicode checks in ContextFormatter
In ContextFormatte r.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:
UnicodeDe codeError: '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: I8c479e507efcf8 acd3e3faa4a702f a6e1f18772f