From 15798a1d6a724e6077ecdc24bfd99298ead8f0c2 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Tue, 31 Jan 2012 00:34:12 -0800 Subject: [PATCH] Cherry-pick b151a1ee967 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. --- tornado/options.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tornado/options.py b/tornado/options.py index f9f472ff..5fb91e1f 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -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"),