From 8807f0ad487f42d0699f806eb36f562d71f8e02e Mon Sep 17 00:00:00 2001 From: Daniel Dromboski Date: Mon, 3 May 2021 11:29:44 -0400 Subject: [PATCH] [tools] More Python 3 compatibility fixes These should all be forward/backward compatible with Python 2/Python 3. [tools] Tweak statusfile.py for Python 3 .iteritems() does not exist in Python 3, only .items(). (While .iteritems() was meant to be an optimization over .items() in Python 2, .items() should work fine, and it is forward/backward compatible.) [tools] Fix another Python 3 issue in mb.py sys.platform used to return e.g. 'linux2', which is 'linux' plus whatever the first digit of `uname -r` was when Python was built. As of Python 3.3, it always returns just 'linux' for Linux OSes. Use `sys.platform.startswith('linux')` for forward/backward compatibility. [tools] Make base_runner.py Python 3 compatible dict.keys() returns a dict_keys in Python 3, whereas it used to return a simple array. list() is forward/backward compatible with identical results on Python 2/3 (returns array). (Tested on Linux x64, trying to recreate NodeJS's CI workflow.) [tools] Make tools/dev/v8gen.py work with Python 3 dict.keys() returns a dict_keys in Python 3, whereas it used to return a simple array. list() is forward/backward compatible with identical results on Python 2/3 (returns array). Comparing a None-type value numerically used to result in the None-type value always being considered "less than" the thing it is compared to. As of Python 3, numerically comparing against None or None-typed values results in an error. Check if a value is truthy before numerically comparing it, for forward/backward compatibility. print() used to transparently decode byte strings in Python 2. In Python 3, they must be explicitly decoded first. (Tested on Linux 64-bit, trying to recreate NodeJS's CI workflow.) Bug: v8:9871 Change-Id: I059bf98577a67649bbe7ec49848989d468da96b0 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2867270 Reviewed-by: Michael Achenbach Commit-Queue: Michael Achenbach Cr-Commit-Position: refs/heads/master@{#74369} --- tools/dev/v8gen.py | 10 ++++++---- tools/mb/mb.py | 2 +- tools/testrunner/base_runner.py | 2 +- tools/testrunner/local/statusfile.py | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tools/dev/v8gen.py b/tools/dev/v8gen.py index 18abf8aa25..c6ba1d2174 100755 --- a/tools/dev/v8gen.py +++ b/tools/dev/v8gen.py @@ -121,7 +121,7 @@ class GenerateGnArgs(object): add_common_options(list_cmd) # Default to "gen" unless global help is requested. - if not args or args[0] not in subps.choices.keys() + ['-h', '--help']: + if not args or args[0] not in list(subps.choices) + ['-h', '--help']: args = ['gen'] + args return self.parser.parse_args(args) @@ -193,14 +193,16 @@ class GenerateGnArgs(object): return 0 def verbose_print_1(self, text): - if self._options.verbosity >= 1: + if self._options.verbosity and self._options.verbosity >= 1: print('#' * 80) print(text) def verbose_print_2(self, text): - if self._options.verbosity >= 2: + if self._options.verbosity and self._options.verbosity >= 2: indent = ' ' * 2 for l in text.splitlines(): + if type(l) == bytes: + l = l.decode() print(indent + l) def _call_cmd(self, args): @@ -306,7 +308,7 @@ if __name__ == "__main__": try: sys.exit(gen.main()) except Exception: - if gen._options.verbosity < 2: + if not gen._options.verbosity or gen._options.verbosity < 2: print ('\nHint: You can raise verbosity (-vv) to see the output of ' 'failed commands.\n') raise diff --git a/tools/mb/mb.py b/tools/mb/mb.py index 9d81453a4c..7031ba50db 100755 --- a/tools/mb/mb.py +++ b/tools/mb/mb.py @@ -878,7 +878,7 @@ class MetaBuildWrapper(object): return err, labels def GNCmd(self, subcommand, path, *args): - if self.platform == 'linux2': + if self.platform.startswith('linux'): subdir, exe = 'linux64', 'gn' elif self.platform == 'darwin': subdir, exe = 'mac', 'gn' diff --git a/tools/testrunner/base_runner.py b/tools/testrunner/base_runner.py index 28a177e385..a03080cee3 100644 --- a/tools/testrunner/base_runner.py +++ b/tools/testrunner/base_runner.py @@ -358,7 +358,7 @@ class BaseTestRunner(object): # Progress parser.add_option("-p", "--progress", - choices=PROGRESS_INDICATORS.keys(), default="mono", + choices=list(PROGRESS_INDICATORS), default="mono", help="The style of progress indicator (verbose, dots, " "color, mono)") parser.add_option("--json-test-results", diff --git a/tools/testrunner/local/statusfile.py b/tools/testrunner/local/statusfile.py index 6c2cc01fb8..b6f97cd564 100644 --- a/tools/testrunner/local/statusfile.py +++ b/tools/testrunner/local/statusfile.py @@ -282,7 +282,7 @@ def ReadStatusFile(content, variables): def _ReadSection(section, variables, rules, prefix_rules): assert type(section) == dict - for rule, outcome_list in section.iteritems(): + for rule, outcome_list in section.items(): assert type(rule) == str if rule[-1] == '*':