From 92c57ee16d2234f2e00e6f4737ce1f3569c4c7e1 Mon Sep 17 00:00:00 2001 From: milde Date: Tue, 1 Jan 2013 20:00:21 +0000 Subject: [PATCH] Unify/simplify type testing in "validate_*_list". Circumvent the problem that the value may be "unicode" or "str" by testing for "not a list". git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7584 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/frontend.py | 17 ++++++----------- test/test_settings.py | 13 +++++++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docutils/frontend.py b/docutils/frontend.py index ebecff19b..ef6a1f90b 100644 --- a/docutils/frontend.py +++ b/docutils/frontend.py @@ -158,7 +158,7 @@ def validate_threshold(setting, value, option_parser, def validate_colon_separated_string_list( setting, value, option_parser, config_parser=None, config_section=None): - if isinstance(value, unicode): + if not isinstance(value, list): value = value.split(':') else: last = value.pop() @@ -169,20 +169,15 @@ def validate_comma_separated_list(setting, value, option_parser, config_parser=None, config_section=None): """Check/normalize list arguments (split at "," and strip whitespace). """ - # `value` is already a list when given as command line option - # and "action" is "append" - # in python2 buildhtml.py calls this once with str after several - # times with unicode. MAYBE fix somewhere else. - #if isinstance(value, unicode): #py3 - #if isinstance(value, basestr): # py3 and py2.7 - if not hasattr(value, 'pop'): + # `value` is already a ``list`` when given as command line option + # and "action" is "append" and ``unicode`` or ``str`` else. + if not isinstance(value, list): value = [value] # this function is called for every option added to `value` # -> split the last item and append the result: last = value.pop() - classes = [cls.strip(u' \t\n') for cls in last.split(',') - if cls.strip(u' \t\n')] - value.extend(classes) + items = [i.strip(u' \t\n') for i in last.split(u',') if i.strip(u' \t\n')] + value.extend(items) return value def validate_url_trailing_slash( diff --git a/test/test_settings.py b/test/test_settings.py index d7c111c0f..bde80328f 100755 --- a/test/test_settings.py +++ b/test/test_settings.py @@ -198,9 +198,11 @@ class HelperFunctionsTests(unittest.TestCase): def test_validate_colon_separated_string_list(self): tests = ( (u'a', ['a',] ), - (u'a:12', ['a', '12'] ), + ('a', ['a',] ), + (u'a:b', ['a', 'b'] ), + ('a:b', ['a', 'b'] ), ([u'a',], ['a',] ), - # TODO ("u'a',", ['a',] ), AttributeError: 'str' object has no attribute 'pop' + ([u'a', u'b:c'], ['a', 'b', 'c'] ), ) for t in tests: self.assertEqual( @@ -211,9 +213,12 @@ class HelperFunctionsTests(unittest.TestCase): def test_validate_comma_separated_list(self): tests = ( (u'a', ['a',] ), - (u'a,12', ['a', '12'] ), + ('a', ['a',] ), + (u'a,b', ['a', 'b'] ), + ('a,b', ['a', 'b'] ), ([u'a',], ['a',] ), - ('a,', ['a',] ), # in python3 this is unicode too + ([u'a', u'b,c'], ['a', 'b', 'c'] ), + (['a', 'b,c'], ['a', 'b', 'c'] ), ) for t in tests: self.assertEqual( -- 2.11.4.GIT