Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #19663 -- Allowed None in colorize() text parameter

Thanks Jonathan Liuti for the report and the initial patch, and
Simon Charette for the review.
  • Loading branch information...
commit 04141c525d2b5021b8e9d2452582181097a0850d 1 parent 0412b7d
@claudep claudep authored
View
4 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):
"""
View
11 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 @@ def test_opts_case(self):
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')
Please sign in to comment.
Something went wrong with that request. Please try again.