From 04141c525d2b5021b8e9d2452582181097a0850d Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 1 Feb 2013 22:51:08 +0100 Subject: [PATCH] Fixed #19663 -- Allowed None in colorize() text parameter Thanks Jonathan Liuti for the report and the initial patch, and Simon Charette for the review. --- django/utils/termcolors.py | 4 ++-- tests/regressiontests/utils/termcolors.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/django/utils/termcolors.py b/django/utils/termcolors.py index 4f74b564a5a..bb14837716b 100644 --- a/django/utils/termcolors.py +++ b/django/utils/termcolors.py @@ -52,8 +52,8 @@ def colorize(text='', opts=(), **kwargs): if o in opt_dict: code_list.append(opt_dict[o]) if 'noreset' not in opts: - text = text + '\x1b[%sm' % RESET - return ('\x1b[%sm' % ';'.join(code_list)) + text + text = '%s\x1b[%sm' % (text or '', RESET) + return '%s%s' % (('\x1b[%sm' % ';'.join(code_list)), text or '') def make_style(opts=(), **kwargs): """ diff --git a/tests/regressiontests/utils/termcolors.py b/tests/regressiontests/utils/termcolors.py index 23fcf557f82..8cc28009cdd 100644 --- a/tests/regressiontests/utils/termcolors.py +++ b/tests/regressiontests/utils/termcolors.py @@ -1,5 +1,7 @@ from django.utils import unittest -from django.utils.termcolors import parse_color_setting, PALETTES, DEFAULT_PALETTE, LIGHT_PALETTE, DARK_PALETTE, NOCOLOR_PALETTE +from django.utils.termcolors import (parse_color_setting, PALETTES, + DEFAULT_PALETTE, LIGHT_PALETTE, DARK_PALETTE, NOCOLOR_PALETTE, colorize) + class TermColorTests(unittest.TestCase): @@ -146,3 +148,10 @@ class TermColorTests(unittest.TestCase): self.assertEqual(parse_color_setting('error=green,bLiNk'), dict(PALETTES[NOCOLOR_PALETTE], ERROR={'fg':'green', 'opts': ('blink',)})) + + def test_colorize_empty_text(self): + self.assertEqual(colorize(text=None), '\x1b[m\x1b[0m') + self.assertEqual(colorize(text=''), '\x1b[m\x1b[0m') + + self.assertEqual(colorize(text=None, opts=('noreset')), '\x1b[m') + self.assertEqual(colorize(text='', opts=('noreset')), '\x1b[m')