From bd11dd1c51ef17592384df927c47023071639f96 Mon Sep 17 00:00:00 2001 From: Dirk Pranke Date: Fri, 7 Dec 2018 17:16:02 -0800 Subject: [PATCH] More miscellaneous fixes for Python3 compatibility. This patch fixes a bunch of stuff flake8 revealed (thanks to cclauss@bluewin.ch for the tip!) and a few dangling print statements (thanks to Ola.Eklundh@qlik.com for the tip!). Bug: gyp:36 Change-Id: Ie24cdda2613c7d76bf06875f9de435e001a3e8b6 Reviewed-on: https://chromium-review.googlesource.com/c/1368752 Reviewed-by: Mark Mentovai Reviewed-by: Dirk Pranke --- gyptest.py | 2 +- pylib/gyp/MSVSNew.py | 7 +++++++ pylib/gyp/common.py | 2 +- pylib/gyp/generator/make.py | 2 +- pylib/gyp/generator/msvs.py | 8 +++----- pylib/gyp/mac_tool.py | 2 +- pylib/gyp/xcodeproj_file.py | 7 +++++++ samples/samples | 8 +++++--- test/actions/src/action_missing_name.gyp | 2 +- test/intermediate_dir/src/script.py | 2 -- test/lib/TestCmd.py | 4 ++-- tools/pretty_vcproj.py | 10 ++++++++-- 12 files changed, 37 insertions(+), 19 deletions(-) diff --git a/gyptest.py b/gyptest.py index 9930e78c..1a9ffca7 100755 --- a/gyptest.py +++ b/gyptest.py @@ -58,7 +58,7 @@ def main(argv=None): os.chdir(args.chdir) if args.path: - extra_path = [os.path.abspath(p) for p in opts.path] + extra_path = [os.path.abspath(p) for p in args.path] extra_path = os.pathsep.join(extra_path) os.environ['PATH'] = extra_path + os.pathsep + os.environ['PATH'] diff --git a/pylib/gyp/MSVSNew.py b/pylib/gyp/MSVSNew.py index 593f0e5b..04459318 100644 --- a/pylib/gyp/MSVSNew.py +++ b/pylib/gyp/MSVSNew.py @@ -21,6 +21,13 @@ _new_md5 = md5.new +try: + # cmp was removed in python3. + cmp +except NameError: + def cmp(a, b): + return (a > b) - (a < b) + # Initialize random number generator random.seed() diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py index 1823de89..b268d229 100644 --- a/pylib/gyp/common.py +++ b/pylib/gyp/common.py @@ -584,7 +584,7 @@ def TopologicallySorted(graph, get_edges): graph = {'a': '$(b) $(c)', 'b': 'hi', 'c': '$(b)'} def GetEdges(node): return re.findall(r'\$\(([^))]\)', graph[node]) - print TopologicallySorted(graph.keys(), GetEdges) + print(TopologicallySorted(graph.keys(), GetEdges)) ==> ['a', 'c', b'] """ diff --git a/pylib/gyp/generator/make.py b/pylib/gyp/generator/make.py index 2057e3a9..8c2827e9 100644 --- a/pylib/gyp/generator/make.py +++ b/pylib/gyp/generator/make.py @@ -1636,7 +1636,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, self.WriteDoCmd([self.output_binary], deps, 'touch', part_of_all, postbuilds=postbuilds) else: - print("WARNING: no output for", self.type, target) + print("WARNING: no output for", self.type, self.target) # Add an alias for each target (if there are any outputs). # Installable target aliases are created below. diff --git a/pylib/gyp/generator/msvs.py b/pylib/gyp/generator/msvs.py index e8a2b366..9eac0285 100644 --- a/pylib/gyp/generator/msvs.py +++ b/pylib/gyp/generator/msvs.py @@ -308,10 +308,8 @@ def _ConfigWindowsTargetPlatformVersion(config_data, version): if names: return names[0] else: - print >> sys.stdout, ( - 'Warning: No include files found for ' - 'detected Windows SDK version %s' % (version) - ) + print('Warning: No include files found for ' + 'detected Windows SDK version %s' % (version)) def _BuildCommandLineForRuleRaw(spec, cmd, cygwin_shell, has_input_path, @@ -2065,7 +2063,7 @@ def GenerateOutput(target_list, target_dicts, data, params): if generator_flags.get('msvs_error_on_missing_sources', False): raise GypError(error_message) else: - print("Warning: " + error_message, file=sys.stdout) + print("Warning: " + error_message) def _GenerateMSBuildFiltersFile(filters_path, source_files, diff --git a/pylib/gyp/mac_tool.py b/pylib/gyp/mac_tool.py index 7d3a8c27..84f88639 100755 --- a/pylib/gyp/mac_tool.py +++ b/pylib/gyp/mac_tool.py @@ -670,7 +670,7 @@ def WriteHmap(output_name, filelist): count = len(filelist) capacity = NextGreaterPowerOf2(count) strings_offset = 24 + (12 * capacity) - max_value_length = len(max(filelist.items(), key=lambda (k,v):len(v))[1]) + max_value_length = len(max(filelist.items(), key=lambda t: len(t[1]))[1]) out = open(output_name, "wb") out.write(struct.pack(' b) - (a < b) + # See XCObject._EncodeString. This pattern is used to determine when a string # can be printed unquoted. Strings that match this pattern may be printed # unquoted. Strings that do not match must be quoted and may be further diff --git a/samples/samples b/samples/samples index 804b6189..ff26de38 100755 --- a/samples/samples +++ b/samples/samples @@ -4,6 +4,8 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +from __future__ import print_function + import os.path import shutil import sys @@ -57,7 +59,7 @@ gyps = [ def Main(argv): if len(argv) != 3 or argv[1] not in ['push', 'pull']: - print 'Usage: %s push/pull PATH_TO_CHROME' % argv[0] + print('Usage: %s push/pull PATH_TO_CHROME' % argv[0]) return 1 path_to_chrome = argv[2] @@ -66,10 +68,10 @@ def Main(argv): chrome_file = os.path.join(path_to_chrome, g) local_file = os.path.join(os.path.dirname(argv[0]), os.path.split(g)[1]) if argv[1] == 'push': - print 'Copying %s to %s' % (local_file, chrome_file) + print('Copying %s to %s' % (local_file, chrome_file)) shutil.copyfile(local_file, chrome_file) elif argv[1] == 'pull': - print 'Copying %s to %s' % (chrome_file, local_file) + print('Copying %s to %s' % (chrome_file, local_file)) shutil.copyfile(chrome_file, local_file) else: assert False diff --git a/test/actions/src/action_missing_name.gyp b/test/actions/src/action_missing_name.gyp index 00424c35..6647aac3 100644 --- a/test/actions/src/action_missing_name.gyp +++ b/test/actions/src/action_missing_name.gyp @@ -15,7 +15,7 @@ 'action': [ 'python', '-c', - 'print \'missing name\'', + 'from __future__ import print_function; print(\'missing name\')', ], }, ], diff --git a/test/intermediate_dir/src/script.py b/test/intermediate_dir/src/script.py index 7abc7ee1..2eb73ac2 100755 --- a/test/intermediate_dir/src/script.py +++ b/test/intermediate_dir/src/script.py @@ -15,8 +15,6 @@ sys.argv[2], fourth = shlex.split(sys.argv[2].replace('\\', '\\\\')) sys.argv.append(fourth) -#print >>sys.stderr, sys.argv - with open(sys.argv[2], 'w') as f: f.write(sys.argv[1]) diff --git a/test/lib/TestCmd.py b/test/lib/TestCmd.py index 5f519c69..1ec50933 100644 --- a/test/lib/TestCmd.py +++ b/test/lib/TestCmd.py @@ -829,13 +829,13 @@ def recv_some(p, t=.1, e=1, tr=5, stderr=0): time.sleep(max((x-time.time())/tr, 0)) return ''.join(y) -# TODO(3.0: rewrite to use memoryview() def send_all(p, data): + data = memoryview(data) while len(data): sent = p.send(data) if sent is None: raise Exception(disconnect_message) - data = buffer(data, sent) + data = data[sent:] diff --git a/tools/pretty_vcproj.py b/tools/pretty_vcproj.py index f02e59ea..4454d9b2 100755 --- a/tools/pretty_vcproj.py +++ b/tools/pretty_vcproj.py @@ -22,6 +22,13 @@ from xml.dom.minidom import parse from xml.dom.minidom import Node +try: + # cmp was removed in python3. + cmp +except NameError: + def cmp(a, b): + return (a > b) - (a < b) + REPLACEMENTS = dict() ARGUMENTS = None @@ -63,7 +70,7 @@ def get_string(node): def PrettyPrintNode(node, indent=0): if node.nodeType == Node.TEXT_NODE: if node.data.strip(): - print '%s%s' % (' '*indent, node.data.strip()) + print('%s%s' % (' '*indent, node.data.strip())) return if node.childNodes: @@ -322,7 +329,6 @@ def main(argv): # Finally, we use the prett xml function to print the vcproj back to the # user. - #print dom.toprettyxml(newl="\n") PrettyPrintNode(dom.documentElement) return 0