Cherry-pick b151a1ee96 into branch2.2.

Original commit message:

Add a version check for the curses unicode hack so it won't break when
python 3.2.3 or 3.3 are released.

Closes #450.
This commit is contained in:
Ben Darnell 2012-01-31 00:34:12 -08:00
parent c1f72d504f
commit 15798a1d6a
1 changed files with 11 additions and 6 deletions

View File

@ -354,12 +354,17 @@ class _LogFormatter(logging.Formatter):
logging.Formatter.__init__(self, *args, **kwargs)
self._color = color
if color:
# The curses module has some str/bytes confusion in python3.
# Most methods return bytes, but only accept strings.
# The explict calls to unicode() below are harmless in python2,
# but will do the right conversion in python3.
fg_color = unicode(curses.tigetstr("setaf") or
curses.tigetstr("setf") or "", "ascii")
# The curses module has some str/bytes confusion in
# python3. Until version 3.2.3, most methods return
# bytes, but only accept strings. In addition, we want to
# output these strings with the logging module, which
# works with unicode strings. The explicit calls to
# unicode() below are harmless in python2 but will do the
# right conversion in python 3.
fg_color = (curses.tigetstr("setaf") or
curses.tigetstr("setf") or "")
if (3, 0) < sys.version_info < (3, 2, 3):
fg_color = unicode(fg_color, "ascii")
self._colors = {
logging.DEBUG: unicode(curses.tparm(fg_color, 4), # Blue
"ascii"),