mirror of
https://github.com/python/cpython.git
synced 2025-10-03 05:35:59 +00:00
removed types usage and added test coverage (work for #3986)
This commit is contained in:
parent
c5ed5ba585
commit
98da8e151a
2 changed files with 51 additions and 9 deletions
|
@ -7,8 +7,7 @@ in the distutils.command package.
|
||||||
__revision__ = "$Id$"
|
__revision__ = "$Id$"
|
||||||
|
|
||||||
import sys, os, string, re
|
import sys, os, string, re
|
||||||
from types import *
|
from distutils.errors import DistutilsOptionError
|
||||||
from distutils.errors import *
|
|
||||||
from distutils import util, dir_util, file_util, archive_util, dep_util
|
from distutils import util, dir_util, file_util, archive_util, dep_util
|
||||||
from distutils import log
|
from distutils import log
|
||||||
|
|
||||||
|
@ -220,7 +219,7 @@ class Command:
|
||||||
if val is None:
|
if val is None:
|
||||||
setattr(self, option, default)
|
setattr(self, option, default)
|
||||||
return default
|
return default
|
||||||
elif type(val) is not StringType:
|
elif not isinstance(val, str):
|
||||||
raise DistutilsOptionError, \
|
raise DistutilsOptionError, \
|
||||||
"'%s' must be a %s (got `%s`)" % (option, what, val)
|
"'%s' must be a %s (got `%s`)" % (option, what, val)
|
||||||
return val
|
return val
|
||||||
|
@ -240,19 +239,24 @@ class Command:
|
||||||
val = getattr(self, option)
|
val = getattr(self, option)
|
||||||
if val is None:
|
if val is None:
|
||||||
return
|
return
|
||||||
elif type(val) is StringType:
|
elif isinstance(val, str):
|
||||||
setattr(self, option, re.split(r',\s*|\s+', val))
|
setattr(self, option, re.split(r',\s*|\s+', val))
|
||||||
else:
|
else:
|
||||||
if type(val) is ListType:
|
if isinstance(val, list):
|
||||||
types = map(type, val)
|
# checks if all elements are str
|
||||||
ok = (types == [StringType] * len(val))
|
ok = 1
|
||||||
|
for element in val:
|
||||||
|
if not isinstance(element, str):
|
||||||
|
ok = 0
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
ok = 0
|
ok = 0
|
||||||
|
|
||||||
if not ok:
|
if not ok:
|
||||||
raise DistutilsOptionError, \
|
raise DistutilsOptionError, \
|
||||||
"'%s' must be a list of strings (got %r)" % \
|
"'%s' must be a list of strings (got %r)" % \
|
||||||
(option, val)
|
(option, val)
|
||||||
|
|
||||||
|
|
||||||
def _ensure_tested_string (self, option, tester,
|
def _ensure_tested_string (self, option, tester,
|
||||||
what, error_fmt, default=None):
|
what, error_fmt, default=None):
|
||||||
|
|
38
Lib/distutils/tests/test_cmd.py
Normal file
38
Lib/distutils/tests/test_cmd.py
Normal file
|
@ -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())
|
Loading…
Add table
Add a link
Reference in a new issue