0

Using Python 2.6 (don't judge me ;-) ) with the logging module, I'm finding that trying to log an entire dict with unicode bits is causing problems.

log.debug("mydict is %s", mydict)

This is causing UnicodeDecodeError exceptions when run in a test runner, but not when I'm in a simple python shell.

>>> d
{'foo': u'\u2615'}
>>> d['bar'] = d['foo'].encode('utf8')
>>> d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> print d['foo']
☕
>>> print d['bar']
☕
>>>
>>> d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> print "%s" % d
{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}
>>> import logging
>>> logging.basicConfig()
>>> log = logging.getLogger('')
>>> log.setLevel(logging.DEBUG)
>>> log.debug("%s", d)
DEBUG:root:{'foo': u'\u2615', 'bar': '\xe2\x98\x95'}

So in a terminal it's all good, but in a test runner I'm getting exceptions as python is apparently trying to decode the repr string of the dict using an ascii codec.

So, I'm wondering why the inconsistency? And what is a good, safe way to send a dict to the logger for debug purposes?

Cheers, Mike

Michael Soulier
  • 803
  • 1
  • 9
  • 20
  • Provide sample code that causes the error. – Mark Tolonen Apr 18 '18 at 01:29
  • It was a simple logging statement like in my description. I can't really provide my entire test suite, but it's for django if that helps. Basically running "python manage.py test ". – Michael Soulier Apr 18 '18 at 02:32
  • Break it down to the fewest lines that reproduce the error and the traceback of the error message itself. Posting code that works doesn't show us what is wrong. The repr() of your dict *is* ASCII, so decoding it with the `ascii` codec should succeed, even if we don't know why the decode is occurring. – Mark Tolonen Apr 18 '18 at 05:41
  • I now suspect that this is my problem, as my test framework is piping to tee and to a log file. https://stackoverflow.com/questions/4545661/unicodedecodeerror-when-redirecting-to-file – Michael Soulier May 03 '18 at 11:40

0 Answers0