diff options
author | Tarek Ziadé <ziade.tarek@gmail.com> | 2009-02-06 08:55:23 (GMT) |
---|---|---|
committer | Tarek Ziadé <ziade.tarek@gmail.com> | 2009-02-06 08:55:23 (GMT) |
commit | 98da8e151ac8117aa84819903d7b3fb844b4e630 (patch) | |
tree | 3348bf3544b0cb63668cc527a3e912bdc5d0fa99 /Lib/distutils | |
parent | c5ed5ba585a30c08c69444680f97f2740956ec7f (diff) | |
download | cpython-98da8e151ac8117aa84819903d7b3fb844b4e630.zip cpython-98da8e151ac8117aa84819903d7b3fb844b4e630.tar.gz cpython-98da8e151ac8117aa84819903d7b3fb844b4e630.tar.bz2 |
removed types usage and added test coverage (work for #3986)
Diffstat (limited to 'Lib/distutils')
-rw-r--r-- | Lib/distutils/cmd.py | 22 | ||||
-rw-r--r-- | Lib/distutils/tests/test_cmd.py | 38 |
2 files changed, 51 insertions, 9 deletions
diff --git a/Lib/distutils/cmd.py b/Lib/distutils/cmd.py index 267cf18..2d6cfb1 100644 --- a/Lib/distutils/cmd.py +++ b/Lib/distutils/cmd.py @@ -7,8 +7,7 @@ in the distutils.command package. __revision__ = "$Id$" import sys, os, string, re -from types import * -from distutils.errors import * +from distutils.errors import DistutilsOptionError from distutils import util, dir_util, file_util, archive_util, dep_util from distutils import log @@ -220,7 +219,7 @@ class Command: if val is None: setattr(self, option, default) return default - elif type(val) is not StringType: + elif not isinstance(val, str): raise DistutilsOptionError, \ "'%s' must be a %s (got `%s`)" % (option, what, val) return val @@ -240,19 +239,24 @@ class Command: val = getattr(self, option) if val is None: return - elif type(val) is StringType: + elif isinstance(val, str): setattr(self, option, re.split(r',\s*|\s+', val)) else: - if type(val) is ListType: - types = map(type, val) - ok = (types == [StringType] * len(val)) + if isinstance(val, list): + # checks if all elements are str + ok = 1 + for element in val: + if not isinstance(element, str): + ok = 0 + break else: ok = 0 if not ok: raise DistutilsOptionError, \ - "'%s' must be a list of strings (got %r)" % \ - (option, val) + "'%s' must be a list of strings (got %r)" % \ + (option, val) + def _ensure_tested_string (self, option, tester, what, error_fmt, default=None): diff --git a/Lib/distutils/tests/test_cmd.py b/Lib/distutils/tests/test_cmd.py new file mode 100644 index 0000000..19079c0 --- /dev/null +++ b/Lib/distutils/tests/test_cmd.py @@ -0,0 +1,38 @@ +"""Tests for distutils.cmd.""" +import unittest + +from distutils.cmd import Command +from distutils.dist import Distribution +from distutils.errors import DistutilsOptionError + +class CommandTestCase(unittest.TestCase): + + def test_ensure_string_list(self): + + class MyCmd(Command): + + def initialize_options(self): + pass + + dist = Distribution() + cmd = MyCmd(dist) + + cmd.not_string_list = ['one', 2, 'three'] + cmd.yes_string_list = ['one', 'two', 'three'] + cmd.not_string_list2 = object() + cmd.yes_string_list2 = 'ok' + + cmd.ensure_string_list('yes_string_list') + cmd.ensure_string_list('yes_string_list2') + + self.assertRaises(DistutilsOptionError, + cmd.ensure_string_list, 'not_string_list') + + self.assertRaises(DistutilsOptionError, + cmd.ensure_string_list, 'not_string_list2') + +def test_suite(): + return unittest.makeSuite(CommandTestCase) + +if __name__ == '__main__': + test_support.run_unittest(test_suite()) |