Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(676)

Unified Diff: test/lib/TestCommon.py

Issue 1454433002: Python 3 compatibility Base URL: https://chromium.googlesource.com/external/gyp.git@master
Patch Set: Rebase with master (4ec6c4e3a94bd04a6da2858163d40b2429b8aad1) Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/lib/TestCommon.py
diff --git a/test/lib/TestCommon.py b/test/lib/TestCommon.py
index 2f526a6e3d729e82b9ca6915137b9da56244f9f2..a888a4813c05c2c979bfb2b0357e456d3e01687a 100644
--- a/test/lib/TestCommon.py
+++ b/test/lib/TestCommon.py
@@ -88,6 +88,8 @@ The TestCommon module also provides the following variables
# AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
+from __future__ import print_function
+
__author__ = "Steven Knight <knight at baldmt dot com>"
__revision__ = "TestCommon.py 0.37.D001 2010/01/11 16:55:50 knight"
__version__ = "0.37"
@@ -96,10 +98,11 @@ import copy
import os
import os.path
import stat
-import string
import sys
-import types
-import UserList
+try:
+ from UserList import UserList
+except ImportError:
+ from collections import UserList
from TestCmd import *
from TestCmd import __all__
@@ -138,7 +141,7 @@ elif sys.platform == 'cygwin':
dll_suffix = '.dll'
module_prefix = ''
module_suffix = '.dll'
-elif string.find(sys.platform, 'irix') != -1:
+elif sys.platform.find('irix') != -1:
exe_suffix = ''
obj_suffix = '.o'
shobj_suffix = '.o'
@@ -149,7 +152,7 @@ elif string.find(sys.platform, 'irix') != -1:
dll_suffix = '.so'
module_prefix = 'lib'
module_prefix = '.so'
-elif string.find(sys.platform, 'darwin') != -1:
+elif sys.platform.find('darwin') != -1:
exe_suffix = ''
obj_suffix = '.o'
shobj_suffix = '.os'
@@ -160,7 +163,7 @@ elif string.find(sys.platform, 'darwin') != -1:
dll_suffix = '.dylib'
module_prefix = ''
module_suffix = '.so'
-elif string.find(sys.platform, 'sunos') != -1:
+elif sys.platform.find('sunos') != -1:
exe_suffix = ''
obj_suffix = '.o'
shobj_suffix = '.os'
@@ -184,8 +187,8 @@ else:
module_suffix = '.so'
def is_List(e):
- return type(e) is types.ListType \
- or isinstance(e, UserList.UserList)
+ return type(e) is list \
+ or isinstance(e, UserList)
def is_writable(f):
mode = os.stat(f)[stat.ST_MODE]
@@ -227,7 +230,7 @@ class TestCommon(TestCmd):
calling the base class initialization, and then changing directory
to the workdir.
"""
- apply(TestCmd.__init__, [self], kw)
+ TestCmd.__init__(self, **kw)
os.chdir(self.workdir)
def must_be_writable(self, *files):
@@ -237,26 +240,26 @@ class TestCommon(TestCmd):
them. Exits FAILED if any of the files does not exist or is
not writable.
"""
- files = map(lambda x: is_List(x) and apply(os.path.join, x) or x, files)
+ files = map((lambda x: os.path.join(*x) if is_List(x) else x), files)
existing, missing = separate_files(files)
- unwritable = filter(lambda x, iw=is_writable: not iw(x), existing)
+ unwritable = [x for x in existing if not is_writable(x)]
if missing:
- print "Missing files: `%s'" % string.join(missing, "', `")
+ print("Missing files: `%s'" % "', `".join(missing))
if unwritable:
- print "Unwritable files: `%s'" % string.join(unwritable, "', `")
+ print("Unwritable files: `%s'" % "', `".join(unwritable))
self.fail_test(missing + unwritable)
- def must_contain(self, file, required, mode = 'rb'):
+ def must_contain(self, file, required, mode = 'r'):
"""Ensures that the specified file contains the required text.
"""
file_contents = self.read(file, mode)
- contains = (string.find(file_contents, required) != -1)
+ contains = (file_contents.find(required) != -1)
if not contains:
- print "File `%s' does not contain required string." % file
- print self.banner('Required string ')
- print required
- print self.banner('%s contents ' % file)
- print file_contents
+ print("File `%s' does not contain required string." % file)
+ print(self.banner('Required string '))
+ print(required)
+ print(self.banner('%s contents ' % file))
+ print(file_contents)
self.fail_test(not contains)
def must_contain_all_lines(self, output, lines, title=None, find=None):
@@ -271,7 +274,7 @@ class TestCommon(TestCmd):
for lines in the output.
"""
if find is None:
- find = lambda o, l: string.find(o, l) != -1
+ find = lambda o, l: o.find(l) != -1
missing = []
for line in lines:
if not find(output, line):
@@ -299,7 +302,7 @@ class TestCommon(TestCmd):
for lines in the output.
"""
if find is None:
- find = lambda o, l: string.find(o, l) != -1
+ find = lambda o, l: o.find(l) != -1
for line in lines:
if find(output, line):
return
@@ -323,13 +326,13 @@ class TestCommon(TestCmd):
pathname will be constructed by concatenating them. Exits FAILED
if any of the files does not exist.
"""
- files = map(lambda x: is_List(x) and apply(os.path.join, x) or x, files)
- missing = filter(lambda x: not os.path.exists(x), files)
+ files = map((lambda x: os.path.join(*x) if is_List(x) else x), files)
+ missing = [f for f in files if not os.path.exists(f)]
if missing:
- print "Missing files: `%s'" % string.join(missing, "', `")
+ print("Missing files: `%s'" % "', `".join(missing))
self.fail_test(missing)
- def must_match(self, file, expect, mode = 'rb'):
+ def must_match(self, file, expect, mode = 'r'):
"""Matches the contents of the specified file (first argument)
against the expected contents (second argument). The expected
contents are a list of lines or a string which will be split
@@ -341,21 +344,21 @@ class TestCommon(TestCmd):
except KeyboardInterrupt:
raise
except:
- print "Unexpected contents of `%s'" % file
+ print("Unexpected contents of `%s'" % file)
self.diff(expect, file_contents, 'contents ')
raise
- def must_not_contain(self, file, banned, mode = 'rb'):
+ def must_not_contain(self, file, banned, mode = 'r'):
"""Ensures that the specified file doesn't contain the banned text.
"""
file_contents = self.read(file, mode)
- contains = (string.find(file_contents, banned) != -1)
+ contains = (file_contents.find(banned) != -1)
if contains:
- print "File `%s' contains banned string." % file
- print self.banner('Banned string ')
- print banned
- print self.banner('%s contents ' % file)
- print file_contents
+ print("File `%s' contains banned string." % file)
+ print(self.banner('Banned string '))
+ print(banned)
+ print(self.banner('%s contents ' % file))
+ print(file_contents)
self.fail_test(contains)
def must_not_contain_any_line(self, output, lines, title=None, find=None):
@@ -370,7 +373,7 @@ class TestCommon(TestCmd):
for lines in the output.
"""
if find is None:
- find = lambda o, l: string.find(o, l) != -1
+ find = lambda o, l: o.find(l) != -1
unexpected = []
for line in lines:
if find(output, line):
@@ -395,10 +398,10 @@ class TestCommon(TestCmd):
which case the pathname will be constructed by concatenating them.
Exits FAILED if any of the files exists.
"""
- files = map(lambda x: is_List(x) and apply(os.path.join, x) or x, files)
- existing = filter(os.path.exists, files)
+ files = map((lambda x: os.path.join(*x) if is_List(x) else x), files)
+ existing = [f for f in files if os.path.exists(f)]
if existing:
- print "Unexpected files exist: `%s'" % string.join(existing, "', `")
+ print("Unexpected files exist: `%s'" % "', `".join(existing))
self.fail_test(existing)
def must_not_be_writable(self, *files):
@@ -408,13 +411,13 @@ class TestCommon(TestCmd):
them. Exits FAILED if any of the files does not exist or is
writable.
"""
- files = map(lambda x: is_List(x) and apply(os.path.join, x) or x, files)
+ files = map((lambda x: os.path.join(*x) if is_List(x) else x), files)
existing, missing = separate_files(files)
- writable = filter(is_writable, existing)
+ writable = [x for x in existing if is_writable(x)]
if missing:
- print "Missing files: `%s'" % string.join(missing, "', `")
+ print("Missing files: `%s'" % "', `".join(missing))
if writable:
- print "Writable files: `%s'" % string.join(writable, "', `")
+ print("Writable files: `%s'" % "', `".join(writable))
self.fail_test(missing + writable)
def _complete(self, actual_stdout, expected_stdout,
@@ -427,21 +430,21 @@ class TestCommon(TestCmd):
expect = ''
if status != 0:
expect = " (expected %s)" % str(status)
- print "%s returned %s%s" % (self.program, str(_status(self)), expect)
- print self.banner('STDOUT ')
- print actual_stdout
- print self.banner('STDERR ')
- print actual_stderr
+ print("%s returned %s%s" % (self.program, str(_status(self)), expect))
+ print(self.banner('STDOUT '))
+ print(actual_stdout)
+ print(self.banner('STDERR '))
+ print(actual_stderr)
self.fail_test()
if not expected_stdout is None and not match(actual_stdout, expected_stdout):
self.diff(expected_stdout, actual_stdout, 'STDOUT ')
if actual_stderr:
- print self.banner('STDERR ')
- print actual_stderr
+ print(self.banner('STDERR '))
+ print(actual_stderr)
self.fail_test()
if not expected_stderr is None and not match(actual_stderr, expected_stderr):
- print self.banner('STDOUT ')
- print actual_stdout
+ print(self.banner('STDOUT '))
+ print(actual_stdout)
self.diff(expected_stderr, actual_stderr, 'STDERR ')
self.fail_test()
@@ -463,20 +466,19 @@ class TestCommon(TestCmd):
arguments = options + " " + arguments
try:
- return apply(TestCmd.start,
- (self, program, interpreter, arguments, universal_newlines),
- kw)
+ return TestCmd.start(self, program, interpreter, arguments,
+ universal_newlines, **kw)
except KeyboardInterrupt:
raise
- except Exception, e:
- print self.banner('STDOUT ')
+ except Exception as e:
+ print(self.banner('STDOUT '))
try:
- print self.stdout()
+ print(self.stdout())
except IndexError:
pass
- print self.banner('STDERR ')
+ print(self.banner('STDERR '))
try:
- print self.stderr()
+ print(self.stderr())
except IndexError:
pass
cmd_args = self.command_args(program, interpreter, arguments)
@@ -501,7 +503,7 @@ class TestCommon(TestCmd):
command. A value of None means don't
test exit status.
"""
- apply(TestCmd.finish, (self, popen,), kw)
+ TestCmd.finish(self, popen, **kw)
match = kw.get('match', self.match)
self._complete(self.stdout(), stdout,
self.stderr(), stderr, status, match)
@@ -539,7 +541,7 @@ class TestCommon(TestCmd):
arguments = options + " " + arguments
kw['arguments'] = arguments
match = kw.pop('match', self.match)
- apply(TestCmd.run, [self], kw)
+ TestCmd.run(self, **kw)
self._complete(self.stdout(), stdout,
self.stderr(), stderr, status, match)

Powered by Google App Engine
This is Rietveld 408576698