Commit 04141c52 authored by Claude Paroz's avatar Claude Paroz
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.
parent 0412b7d2
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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):
    """
+10 −1
Original line number Diff line number Diff line
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')