Skip to content

Commit 6b43bba

Browse files
author
tarek.ziade
committed
removed types usage and added test coverage (work for #3986)
git-svn-id: http://svn.python.org/projects/python/trunk@69360 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 14e03e3 commit 6b43bba

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed

Lib/distutils/cmd.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
__revision__ = "$Id$"
88

99
import sys, os, string, re
10-
from types import *
11-
from distutils.errors import *
10+
from distutils.errors import DistutilsOptionError
1211
from distutils import util, dir_util, file_util, archive_util, dep_util
1312
from distutils import log
1413

@@ -220,7 +219,7 @@ def _ensure_stringlike (self, option, what, default=None):
220219
if val is None:
221220
setattr(self, option, default)
222221
return default
223-
elif type(val) is not StringType:
222+
elif not isinstance(val, str):
224223
raise DistutilsOptionError, \
225224
"'%s' must be a %s (got `%s`)" % (option, what, val)
226225
return val
@@ -240,19 +239,24 @@ def ensure_string_list (self, option):
240239
val = getattr(self, option)
241240
if val is None:
242241
return
243-
elif type(val) is StringType:
242+
elif isinstance(val, str):
244243
setattr(self, option, re.split(r',\s*|\s+', val))
245244
else:
246-
if type(val) is ListType:
247-
types = map(type, val)
248-
ok = (types == [StringType] * len(val))
245+
if isinstance(val, list):
246+
# checks if all elements are str
247+
ok = 1
248+
for element in val:
249+
if not isinstance(element, str):
250+
ok = 0
251+
break
249252
else:
250253
ok = 0
251254

252255
if not ok:
253256
raise DistutilsOptionError, \
254-
"'%s' must be a list of strings (got %r)" % \
255-
(option, val)
257+
"'%s' must be a list of strings (got %r)" % \
258+
(option, val)
259+
256260

257261
def _ensure_tested_string (self, option, tester,
258262
what, error_fmt, default=None):

Lib/distutils/tests/test_cmd.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""Tests for distutils.cmd."""
2+
import unittest
3+
4+
from distutils.cmd import Command
5+
from distutils.dist import Distribution
6+
from distutils.errors import DistutilsOptionError
7+
8+
class CommandTestCase(unittest.TestCase):
9+
10+
def test_ensure_string_list(self):
11+
12+
class MyCmd(Command):
13+
14+
def initialize_options(self):
15+
pass
16+
17+
dist = Distribution()
18+
cmd = MyCmd(dist)
19+
20+
cmd.not_string_list = ['one', 2, 'three']
21+
cmd.yes_string_list = ['one', 'two', 'three']
22+
cmd.not_string_list2 = object()
23+
cmd.yes_string_list2 = 'ok'
24+
25+
cmd.ensure_string_list('yes_string_list')
26+
cmd.ensure_string_list('yes_string_list2')
27+
28+
self.assertRaises(DistutilsOptionError,
29+
cmd.ensure_string_list, 'not_string_list')
30+
31+
self.assertRaises(DistutilsOptionError,
32+
cmd.ensure_string_list, 'not_string_list2')
33+
34+
def test_suite():
35+
return unittest.makeSuite(CommandTestCase)
36+
37+
if __name__ == '__main__':
38+
test_support.run_unittest(test_suite())

0 commit comments

Comments
 (0)