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')