Skip to content

Commit 9f1bcd4

Browse files
thefourtheyeaddaleax
authored andcommitted
tools: make test.py Python 3 compatible
PR-URL: #25767 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: cclauss <cclauss@me.com> Reviewed-By: gengjiawen <technicalcute@gmail.com>
1 parent 8a8c178 commit 9f1bcd4

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

tools/test.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def cmp(x, y): # Python 3
6868
except NameError:
6969
xrange = range # Python 3
7070

71+
try:
72+
from urllib.parse import unquote # Python 3
73+
except ImportError:
74+
from urllib import unquote # Python 2
75+
76+
7177
logger = logging.getLogger('testrunner')
7278
skip_regex = re.compile(r'# SKIP\S*\s+(.*)', re.IGNORECASE)
7379

@@ -119,7 +125,7 @@ def Run(self, tasks):
119125
# Spawn N-1 threads and then use this thread as the last one.
120126
# That way -j1 avoids threading altogether which is a nice fallback
121127
# in case of threading problems.
122-
for i in xrange(tasks - 1):
128+
for i in range(tasks - 1):
123129
thread = threading.Thread(target=self.RunSingle, args=[True, i + 1])
124130
threads.append(thread)
125131
thread.start()
@@ -755,7 +761,7 @@ def Execute(args, context, timeout=None, env={}, faketty=False, disable_core_fil
755761
del env_copy["NODE_PATH"]
756762

757763
# Extend environment
758-
for key, value in env.iteritems():
764+
for key, value in env.items():
759765
env_copy[key] = value
760766

761767
preexec_fn = None
@@ -808,7 +814,7 @@ def __init__(self, context, root, section):
808814
def Contains(self, path, file):
809815
if len(path) > len(file):
810816
return False
811-
for i in xrange(len(path)):
817+
for i in range(len(path)):
812818
if not path[i].match(NormalizePath(file[i])):
813819
return False
814820
return True
@@ -1263,7 +1269,7 @@ def GetOutcomes(self, env, defs):
12631269
def Contains(self, path):
12641270
if len(self.path) > len(path):
12651271
return False
1266-
for i in xrange(len(self.path)):
1272+
for i in range(len(self.path)):
12671273
if not self.path[i].match(path[i]):
12681274
return False
12691275
return True
@@ -1330,7 +1336,7 @@ def BuildOptions():
13301336
help='write test output to file. NOTE: this only applies the tap progress indicator')
13311337
result.add_option("-p", "--progress",
13321338
help="The style of progress indicator (verbose, dots, color, mono, tap)",
1333-
choices=PROGRESS_INDICATORS.keys(), default="mono")
1339+
choices=list(PROGRESS_INDICATORS.keys()), default="mono")
13341340
result.add_option("--report", help="Print a summary of the tests to be run",
13351341
default=False, action="store_true")
13361342
result.add_option("-s", "--suite", help="A test suite",
@@ -1403,15 +1409,15 @@ def ProcessOptions(options):
14031409
options.mode = options.mode.split(',')
14041410
options.run = options.run.split(',')
14051411
# Split at commas and filter out all the empty strings.
1406-
options.skip_tests = filter(bool, options.skip_tests.split(','))
1412+
options.skip_tests = [test for test in options.skip_tests.split(',') if test]
14071413
if options.run == [""]:
14081414
options.run = None
14091415
elif len(options.run) != 2:
14101416
print("The run argument must be two comma-separated integers.")
14111417
return False
14121418
else:
14131419
try:
1414-
options.run = map(int, options.run)
1420+
options.run = [int(level) for level in options.run]
14151421
except ValueError:
14161422
print("Could not parse the integers from the run argument.")
14171423
return False
@@ -1479,10 +1485,9 @@ def ExpandCommand(args):
14791485
return args
14801486
return ExpandCommand
14811487
else:
1482-
pos = value.find('@')
1483-
import urllib
1484-
prefix = urllib.unquote(value[:pos]).split()
1485-
suffix = urllib.unquote(value[pos+1:]).split()
1488+
prefix, _, suffix = value.partition('@')
1489+
prefix = unquote(prefix).split()
1490+
suffix = unquote(suffix).split()
14861491
def ExpandCommand(args):
14871492
return prefix + args + suffix
14881493
return ExpandCommand
@@ -1531,8 +1536,8 @@ def PrintCrashed(code):
15311536

15321537
def ArgsToTestPaths(test_root, args, suites):
15331538
if len(args) == 0 or 'default' in args:
1534-
def_suites = filter(lambda s: s not in IGNORED_SUITES, suites)
1535-
args = filter(lambda a: a != 'default', args) + def_suites
1539+
def_suites = [s for s in suites if s not in IGNORED_SUITES]
1540+
args = [a for a in args if a != 'default'] + def_suites
15361541
subsystem_regex = re.compile(r'^[a-zA-Z-]*$')
15371542
check = lambda arg: subsystem_regex.match(arg) and (arg not in suites)
15381543
mapped_args = ["*/test*-%s-*" % arg if check(arg) else arg for arg in args]
@@ -1699,7 +1704,9 @@ def should_keep(case):
16991704
else:
17001705
return True
17011706

1702-
cases_to_run = filter(should_keep, all_cases)
1707+
cases_to_run = [
1708+
test_case for test_case in all_cases if should_keep(test_case)
1709+
]
17031710

17041711
if options.report:
17051712
print(REPORT_TEMPLATE % {
@@ -1716,7 +1723,7 @@ def should_keep(case):
17161723
# can be different in different machines
17171724
cases_to_run.sort(key=lambda c: (c.arch, c.mode, c.file))
17181725
cases_to_run = [ cases_to_run[i] for i
1719-
in xrange(options.run[0],
1726+
in range(options.run[0],
17201727
len(cases_to_run),
17211728
options.run[1]) ]
17221729
if len(cases_to_run) == 0:

0 commit comments

Comments
 (0)