Commit f8962ae1 authored by Vadim Gorbachev (bmsdave)'s avatar Vadim Gorbachev (bmsdave) Committed by Commit Bot

Preparing v8 to use with python3 /test

There are now less that 400 days until the end of life
of Python 2(aka _legacy_ Python) https://pythonclock.org/ .
The code compatibility check for python2 and python3
used the following tools: futurize, flake8
You can see the reports here: https://travis-ci.com/bmsdave/v8/builds

This CL was uploaded by git cl split.

Bug: v8:8594
Change-Id: Idbf467daf629a4e808345a6a88036c2a3f259138
Reviewed-on: https://chromium-review.googlesource.com/c/1470121
Commit-Queue: Michael Achenbach <machenbach@chromium.org>
Reviewed-by: 's avatarMichael Achenbach <machenbach@chromium.org>
Reviewed-by: 's avatarSergiy Belozorov <sergiyb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59679}
parent 6cad46b4
...@@ -8,6 +8,9 @@ Local benchmark runner. ...@@ -8,6 +8,9 @@ Local benchmark runner.
The -c option is mandatory. The -c option is mandatory.
''' '''
# for py2/py3 compatibility
from __future__ import print_function
import math import math
from optparse import OptionParser from optparse import OptionParser
import os import os
...@@ -96,7 +99,7 @@ class BenchmarkSuite(object): ...@@ -96,7 +99,7 @@ class BenchmarkSuite(object):
elif self.name in self.kGeometricScoreSuites: elif self.name in self.kGeometricScoreSuites:
self.ComputeScoreV8Octane(self.name) self.ComputeScoreV8Octane(self.name)
else: else:
print "Don't know how to compute score for suite: '%s'" % self.name print("Don't know how to compute score for suite: '%s'" % self.name)
def IsBetterThan(self, other): def IsBetterThan(self, other):
if self.name in self.kClassicScoreSuites: if self.name in self.kClassicScoreSuites:
...@@ -104,7 +107,7 @@ class BenchmarkSuite(object): ...@@ -104,7 +107,7 @@ class BenchmarkSuite(object):
elif self.name in self.kGeometricScoreSuites: elif self.name in self.kGeometricScoreSuites:
return self.score > other.score return self.score > other.score
else: else:
print "Don't know how to compare score for suite: '%s'" % self.name print("Don't know how to compare score for suite: '%s'" % self.name)
class BenchmarkRunner(object): class BenchmarkRunner(object):
...@@ -140,7 +143,7 @@ class BenchmarkRunner(object): ...@@ -140,7 +143,7 @@ class BenchmarkRunner(object):
outfile = "%s/out.%d.txt" % (self.outdir, i) outfile = "%s/out.%d.txt" % (self.outdir, i)
if os.path.exists(outfile) and not self.opts.force: if os.path.exists(outfile) and not self.opts.force:
continue continue
print "run #%d" % i print("run #%d" % i)
cmdline = "%s > %s" % (self.opts.command, outfile) cmdline = "%s > %s" % (self.opts.command, outfile)
subprocess.call(cmdline, shell=True) subprocess.call(cmdline, shell=True)
time.sleep(self.opts.sleep) time.sleep(self.opts.sleep)
...@@ -179,11 +182,11 @@ class BenchmarkRunner(object): ...@@ -179,11 +182,11 @@ class BenchmarkRunner(object):
suite.ProcessResults(self.opts) suite.ProcessResults(self.opts)
suite.ComputeScore() suite.ComputeScore()
print ("%s,%.1f,%.2f,%d " % print(("%s,%.1f,%.2f,%d " %
(suite.name, suite.score, suite.sigma, suite.num)), (suite.name, suite.score, suite.sigma, suite.num)), end=' ')
if self.opts.verbose: if self.opts.verbose:
print "" print("")
print "" print("")
if __name__ == '__main__': if __name__ == '__main__':
...@@ -206,14 +209,14 @@ if __name__ == '__main__': ...@@ -206,14 +209,14 @@ if __name__ == '__main__':
opts.sleep = int(opts.sleep) opts.sleep = int(opts.sleep)
if not opts.command: if not opts.command:
print "You must specify the command to run (-c). Aborting." print("You must specify the command to run (-c). Aborting.")
sys.exit(1) sys.exit(1)
cachedir = os.path.abspath(os.getcwd()) cachedir = os.path.abspath(os.getcwd())
if not opts.cachedir: if not opts.cachedir:
opts.cachedir = cachedir opts.cachedir = cachedir
if not os.path.exists(opts.cachedir): if not os.path.exists(opts.cachedir):
print "Directory " + opts.cachedir + " is not valid. Aborting." print("Directory " + opts.cachedir + " is not valid. Aborting.")
sys.exit(1) sys.exit(1)
br = BenchmarkRunner(args, os.getcwd(), opts) br = BenchmarkRunner(args, os.getcwd(), opts)
......
...@@ -10,6 +10,9 @@ runner directly into this script or specify the results file with ...@@ -10,6 +10,9 @@ runner directly into this script or specify the results file with
the -f option. the -f option.
''' '''
# for py2/py3 compatibility
from __future__ import print_function
import csv import csv
import math import math
from optparse import OptionParser from optparse import OptionParser
......
...@@ -31,6 +31,9 @@ You can run from any place: ...@@ -31,6 +31,9 @@ You can run from any place:
../../somewhere-strange/csuite.py sunspider compare ./d8-better ../../somewhere-strange/csuite.py sunspider compare ./d8-better
''' '''
# for py2/py3 compatibility
from __future__ import print_function
import os import os
from optparse import OptionParser from optparse import OptionParser
import subprocess import subprocess
...@@ -47,48 +50,48 @@ if __name__ == '__main__': ...@@ -47,48 +50,48 @@ if __name__ == '__main__':
(opts, args) = parser.parse_args() (opts, args) = parser.parse_args()
if len(args) < 3: if len(args) < 3:
print 'not enough arguments' print('not enough arguments')
sys.exit(1) sys.exit(1)
suite = args[0] suite = args[0]
mode = args[1] mode = args[1]
if suite not in ['octane', 'sunspider', 'kraken']: if suite not in ['octane', 'sunspider', 'kraken']:
print 'Suite must be octane, sunspider or kraken. Aborting.' print('Suite must be octane, sunspider or kraken. Aborting.')
sys.exit(1) sys.exit(1)
if mode != 'baseline' and mode != 'compare': if mode != 'baseline' and mode != 'compare':
print 'mode must be baseline or compare. Aborting.' print('mode must be baseline or compare. Aborting.')
sys.exit(1) sys.exit(1)
# Set up paths. # Set up paths.
d8_path = os.path.abspath(args[2]) d8_path = os.path.abspath(args[2])
if not os.path.exists(d8_path): if not os.path.exists(d8_path):
print d8_path + " is not valid." print(d8_path + " is not valid.")
sys.exit(1) sys.exit(1)
csuite_path = os.path.dirname(os.path.abspath(__file__)) csuite_path = os.path.dirname(os.path.abspath(__file__))
if not os.path.exists(csuite_path): if not os.path.exists(csuite_path):
print "The csuite directory is invalid." print("The csuite directory is invalid.")
sys.exit(1) sys.exit(1)
benchmark_py_path = os.path.join(csuite_path, "benchmark.py") benchmark_py_path = os.path.join(csuite_path, "benchmark.py")
if not os.path.exists(benchmark_py_path): if not os.path.exists(benchmark_py_path):
print "Unable to find benchmark.py in " + output_path_base \ print("Unable to find benchmark.py in " + csuite_path \
+ ". Aborting." + ". Aborting.")
sys.exit(1) sys.exit(1)
compare_baseline_py_path = os.path.join(csuite_path, compare_baseline_py_path = os.path.join(csuite_path,
"compare-baseline.py") "compare-baseline.py")
if not os.path.exists(compare_baseline_py_path): if not os.path.exists(compare_baseline_py_path):
print "Unable to find compare-baseline.py in " + output_path_base \ print("Unable to find compare-baseline.py in " + csuite_path \
+ ". Aborting." + ". Aborting.")
sys.exit(1) sys.exit(1)
benchmark_path = os.path.abspath(os.path.join(csuite_path, "../data")) benchmark_path = os.path.abspath(os.path.join(csuite_path, "../data"))
if not os.path.exists(benchmark_path): if not os.path.exists(benchmark_path):
print "I can't find the benchmark data directory. Aborting." print("I can't find the benchmark data directory. Aborting.")
sys.exit(1) sys.exit(1)
# Gather the remaining arguments into a string of extra args for d8. # Gather the remaining arguments into a string of extra args for d8.
...@@ -111,12 +114,12 @@ if __name__ == '__main__': ...@@ -111,12 +114,12 @@ if __name__ == '__main__':
if opts.runs: if opts.runs:
if (float(opts.runs) / runs) < 0.6: if (float(opts.runs) / runs) < 0.6:
print "Normally, %s requires %d runs to get stable results." \ print("Normally, %s requires %d runs to get stable results." \
% (suite, runs) % (suite, runs))
runs = int(opts.runs) runs = int(opts.runs)
if opts.verbose: if opts.verbose:
print "Running and averaging %s %d times." % (suite, runs) print("Running and averaging %s %d times." % (suite, runs))
# Ensure output directory is setup # Ensure output directory is setup
output_path_base = os.path.abspath(os.getcwd()) output_path_base = os.path.abspath(os.getcwd())
...@@ -124,16 +127,16 @@ if __name__ == '__main__': ...@@ -124,16 +127,16 @@ if __name__ == '__main__':
output_file = os.path.join(output_path, "master") output_file = os.path.join(output_path, "master")
if not os.path.exists(output_path): if not os.path.exists(output_path):
if opts.verbose: if opts.verbose:
print "Creating directory %s." % output_path print("Creating directory %s." % output_path)
os.mkdir(output_path) os.mkdir(output_path)
if opts.verbose: if opts.verbose:
print "Working directory for runs is %s." % suite_path print("Working directory for runs is %s." % suite_path)
inner_command = " -c \"%s --expose-gc %s %s \"" \ inner_command = " -c \"%s --expose-gc %s %s \"" \
% (d8_path, extra_args, cmd) % (d8_path, extra_args, cmd)
if opts.verbose: if opts.verbose:
print "calling d8 like so: %s." % inner_command print("calling d8 like so: %s." % inner_command)
cmdline_base = "python %s %s -fv -r %d -d %s" \ cmdline_base = "python %s %s -fv -r %d -d %s" \
% (benchmark_py_path, inner_command, runs, output_path_base) % (benchmark_py_path, inner_command, runs, output_path_base)
...@@ -145,10 +148,10 @@ if __name__ == '__main__': ...@@ -145,10 +148,10 @@ if __name__ == '__main__':
% (cmdline_base, compare_baseline_py_path, output_file) % (cmdline_base, compare_baseline_py_path, output_file)
if opts.verbose: if opts.verbose:
print "Spawning subprocess: %s." % cmdline print("Spawning subprocess: %s." % cmdline)
return_code = subprocess.call(cmdline, shell=True, cwd=suite_path) return_code = subprocess.call(cmdline, shell=True, cwd=suite_path)
if return_code < 0: if return_code < 0:
print "Error return code: %d." % return_code print("Error return code: %d." % return_code)
if mode == "baseline": if mode == "baseline":
print "Wrote %s." % output_file print("Wrote %s." % output_file)
print "Run %s again with compare mode to see results." % suite print("Run %s again with compare mode to see results." % suite)
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# for py2/py3 compatibility
from __future__ import print_function
import os import os
import shutil import shutil
...@@ -48,9 +51,9 @@ class TestLoader(testsuite.TestLoader): ...@@ -48,9 +51,9 @@ class TestLoader(testsuite.TestLoader):
output = cmd.execute() output = cmd.execute()
# TODO make errors visible (see duplicated code in 'unittests') # TODO make errors visible (see duplicated code in 'unittests')
if output.exit_code != 0: if output.exit_code != 0:
print cmd print(cmd)
print output.stdout print(output.stdout)
print output.stderr print(output.stderr)
return [] return []
return sorted(output.stdout.strip().split()) return sorted(output.stdout.strip().split())
......
...@@ -35,6 +35,11 @@ from testrunner.local import testsuite ...@@ -35,6 +35,11 @@ from testrunner.local import testsuite
from testrunner.objects import testcase from testrunner.objects import testcase
from testrunner.outproc import base as outproc from testrunner.outproc import base as outproc
try:
basestring # Python 2
except NameError: # Python 3
basestring = str
FILES_PATTERN = re.compile(r"//\s+Files:(.*)") FILES_PATTERN = re.compile(r"//\s+Files:(.*)")
ENV_PATTERN = re.compile(r"//\s+Environment Variables:(.*)") ENV_PATTERN = re.compile(r"//\s+Environment Variables:(.*)")
SELF_SCRIPT_PATTERN = re.compile(r"//\s+Env: TEST_FILE_NAME") SELF_SCRIPT_PATTERN = re.compile(r"//\s+Env: TEST_FILE_NAME")
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# for py2/py3 compatibility
from functools import reduce
import os import os
...@@ -91,7 +93,7 @@ class TestCase(testcase.D8TestCase): ...@@ -91,7 +93,7 @@ class TestCase(testcase.D8TestCase):
files = [os.path.join(self.suite.root, "mozilla-shell-emulation.js")] files = [os.path.join(self.suite.root, "mozilla-shell-emulation.js")]
testfilename = self.path + ".js" testfilename = self.path + ".js"
testfilepath = testfilename.split(os.path.sep) testfilepath = testfilename.split(os.path.sep)
for i in xrange(len(testfilepath)): for i in range(len(testfilepath)):
script = os.path.join(self.suite.test_root, script = os.path.join(self.suite.test_root,
reduce(os.path.join, testfilepath[:i], ""), reduce(os.path.join, testfilepath[:i], ""),
"shell.js") "shell.js")
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import os import os
from testrunner.local import testsuite from testrunner.local import testsuite
...@@ -66,7 +65,8 @@ class TestSuite(testsuite.TestSuite): ...@@ -66,7 +65,8 @@ class TestSuite(testsuite.TestSuite):
testsource = testsource.replace("$" + key, replacement[key]); testsource = testsource.replace("$" + key, replacement[key]);
Test(testname, testsource, expectation) Test(testname, testsource, expectation)
return MkTest return MkTest
execfile(pathname, {"Test": Test, "Template": Template}) with open(pathname) as in_file:
exec(in_file.read(), {"Test": Test, "Template": Template})
def ListTests(self): def ListTests(self):
result = [] result = []
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# for py2/py3 compatibility
from __future__ import print_function
import imp import imp
import itertools import itertools
......
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
# for py2/py3 compatibility
from __future__ import print_function
import os import os
from testrunner.local import command from testrunner.local import command
...@@ -23,7 +26,7 @@ class TestLoader(testsuite.TestLoader): ...@@ -23,7 +26,7 @@ class TestLoader(testsuite.TestLoader):
shell += ".exe" shell += ".exe"
output = None output = None
for i in xrange(3): # Try 3 times in case of errors. for i in range(3): # Try 3 times in case of errors.
cmd = command.Command( cmd = command.Command(
cmd_prefix=self.test_config.command_prefix, cmd_prefix=self.test_config.command_prefix,
shell=shell, shell=shell,
...@@ -32,13 +35,13 @@ class TestLoader(testsuite.TestLoader): ...@@ -32,13 +35,13 @@ class TestLoader(testsuite.TestLoader):
if output.exit_code == 0: if output.exit_code == 0:
break break
print "Test executable failed to list the tests (try %d).\n\nCmd:" % i print("Test executable failed to list the tests (try %d).\n\nCmd:" % i)
print cmd print(cmd)
print "\nStdout:" print("\nStdout:")
print output.stdout print(output.stdout)
print "\nStderr:" print("\nStderr:")
print output.stderr print(output.stderr)
print "\nExit code: %d" % output.exit_code print("\nExit code: %d" % output.exit_code)
else: else:
raise Exception("Test executable failed to list the tests.") raise Exception("Test executable failed to list the tests.")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment