From 6f4834e9c38c91d5369b8c1e585e8fc8e58decb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Sun, 15 Mar 2020 09:11:51 +0100 Subject: [PATCH] fix: port ab4aca868d from upstream (#11) Port some remaining Python 3 fixes. Refs: https://github.com/nodejs/node/issues/32170 Refs: https://chromium.googlesource.com/external/gyp/+/ab4aca868d68b9de9b20a9991bbbb5e78ab48a30%5E! --- pylib/gyp/__init__.py | 2 +- pylib/gyp/common.py | 2 +- pylib/gyp/common_test.py | 1 + pylib/gyp/generator/cmake.py | 13 +++++---- pylib/gyp/generator/dump_dependency_json.py | 3 +- pylib/gyp/generator/eclipse.py | 2 +- pylib/gyp/generator/make.py | 10 +++---- pylib/gyp/generator/msvs.py | 8 +++--- pylib/gyp/generator/ninja.py | 8 +++--- pylib/gyp/generator/xcode.py | 18 ++++++------ pylib/gyp/input.py | 31 ++++++++++----------- pylib/gyp/mac_tool.py | 15 +++++----- pylib/gyp/msvs_emulation.py | 14 ++++------ pylib/gyp/win_tool.py | 14 ++++++---- pylib/gyp/xcodeproj_file.py | 18 ++++++------ pylib/gyp/xml_fix.py | 3 +- 16 files changed, 80 insertions(+), 82 deletions(-) diff --git a/pylib/gyp/__init__.py b/pylib/gyp/__init__.py index dee83401..09e2b8bb 100755 --- a/pylib/gyp/__init__.py +++ b/pylib/gyp/__init__.py @@ -532,7 +532,7 @@ def gyp_main(args): generator.GenerateOutput(flat_list, targets, data, params) if options.configs: - valid_configs = targets[flat_list[0]]['configurations'].keys() + valid_configs = targets[flat_list[0]]['configurations'] for conf in options.configs: if conf not in valid_configs: raise GypError('Invalid config specified via --build: %s' % conf) diff --git a/pylib/gyp/common.py b/pylib/gyp/common.py index aa410e1d..1e9f5e88 100644 --- a/pylib/gyp/common.py +++ b/pylib/gyp/common.py @@ -355,7 +355,7 @@ def __init__(self): prefix=os.path.split(filename)[1] + '.gyp.', dir=base_temp_dir) try: - self.tmp_file = os.fdopen(tmp_fd, 'wb') + self.tmp_file = os.fdopen(tmp_fd, 'w') except Exception: # Don't leave turds behind. os.unlink(self.tmp_path) diff --git a/pylib/gyp/common_test.py b/pylib/gyp/common_test.py index b75bbb84..fb14bb15 100755 --- a/pylib/gyp/common_test.py +++ b/pylib/gyp/common_test.py @@ -63,6 +63,7 @@ def test_platform_default(self): self.assertFlavor('solaris', 'sunos' , {}) self.assertFlavor('linux' , 'linux2' , {}) self.assertFlavor('linux' , 'linux3' , {}) + self.assertFlavor('linux' , 'linux' , {}) def test_param(self): self.assertFlavor('foobar', 'linux2' , {'flavor': 'foobar'}) diff --git a/pylib/gyp/generator/cmake.py b/pylib/gyp/generator/cmake.py index e966a8f2..87677913 100644 --- a/pylib/gyp/generator/cmake.py +++ b/pylib/gyp/generator/cmake.py @@ -38,6 +38,12 @@ import gyp.common import gyp.xcode_emulation +try: + # maketrans moved to str in python3. + _maketrans = string.maketrans +except NameError: + _maketrans = str.maketrans + generator_default_variables = { 'EXECUTABLE_PREFIX': '', 'EXECUTABLE_SUFFIX': '', @@ -240,10 +246,7 @@ def StringToCMakeTargetName(a): Invalid for make: ':' Invalid for unknown reasons but cause failures: '.' """ - try: - return a.translate(str.maketrans(' /():."', '_______')) - except AttributeError: - return a.translate(string.maketrans(' /():."', '_______')) + return a.translate(_maketrans(' /():."', '_______')) def WriteActions(target_name, actions, extra_sources, extra_deps, @@ -1235,7 +1238,7 @@ def GenerateOutput(target_list, target_dicts, data, params): GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: - config_names = target_dicts[target_list[0]]['configurations'].keys() + config_names = target_dicts[target_list[0]]['configurations'] if params['parallel']: try: pool = multiprocessing.Pool(len(config_names)) diff --git a/pylib/gyp/generator/dump_dependency_json.py b/pylib/gyp/generator/dump_dependency_json.py index 8e4f3168..2bf3f397 100644 --- a/pylib/gyp/generator/dump_dependency_json.py +++ b/pylib/gyp/generator/dump_dependency_json.py @@ -1,8 +1,9 @@ -from __future__ import print_function # Copyright (c) 2012 Google Inc. All rights reserved. # 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 collections import os import gyp diff --git a/pylib/gyp/generator/eclipse.py b/pylib/gyp/generator/eclipse.py index 80e5fb63..fd1c5e49 100644 --- a/pylib/gyp/generator/eclipse.py +++ b/pylib/gyp/generator/eclipse.py @@ -424,7 +424,7 @@ def GenerateOutput(target_list, target_dicts, data, params): GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: - config_names = target_dicts[target_list[0]]['configurations'].keys() + config_names = target_dicts[target_list[0]]['configurations'] for config_name in config_names: GenerateOutputForConfig(target_list, target_dicts, data, params, config_name) diff --git a/pylib/gyp/generator/make.py b/pylib/gyp/generator/make.py index 26cf88cc..de0db4b2 100644 --- a/pylib/gyp/generator/make.py +++ b/pylib/gyp/generator/make.py @@ -821,7 +821,7 @@ def Write(self, qualified_target, base_path, output_filename, spec, configs, gyp.xcode_emulation.MacPrefixHeader( self.xcode_settings, lambda p: Sourceify(self.Absolutify(p)), self.Pchify)) - sources = list(filter(Compilable, all_sources)) + sources = [x for x in all_sources if Compilable(x)] if sources: self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT1) extensions = set([os.path.splitext(s)[1] for s in sources]) @@ -1314,7 +1314,7 @@ def WriteSources(self, configs, deps, sources, # If there are any object files in our input file list, link them into our # output. - extra_link_deps += list(filter(Linkable, sources)) + extra_link_deps += [source for source in sources if Linkable(source)] self.WriteLn() @@ -1547,9 +1547,9 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps, # Postbuilds expect to be run in the gyp file's directory, so insert an # implicit postbuild to cd to there. postbuilds.insert(0, gyp.common.EncodePOSIXShellList(['cd', self.path])) - for i in range(len(postbuilds)): - if not postbuilds[i].startswith('$'): - postbuilds[i] = EscapeShellArgument(postbuilds[i]) + for i, postbuild in enumerate(postbuilds): + if not postbuild.startswith('$'): + postbuilds[i] = EscapeShellArgument(postbuild) self.WriteLn('%s: builddir := $(abs_builddir)' % QuoteSpaces(self.output)) self.WriteLn('%s: POSTBUILDS := %s' % ( QuoteSpaces(self.output), ' '.join(postbuilds))) diff --git a/pylib/gyp/generator/msvs.py b/pylib/gyp/generator/msvs.py index 933042c7..a7588969 100644 --- a/pylib/gyp/generator/msvs.py +++ b/pylib/gyp/generator/msvs.py @@ -1787,8 +1787,8 @@ def _CollapseSingles(parent, node): # such projects up one level. if (type(node) == dict and len(node) == 1 and - list(node)[0] == parent + '.vcproj'): - return node[list(node)[0]] + next(iter(node)) == parent + '.vcproj'): + return node[next(iter(node))] if type(node) != dict: return node for child in node: @@ -1807,8 +1807,8 @@ def _GatherSolutionFolders(sln_projects, project_objects, flat): # Walk down from the top until we hit a folder that has more than one entry. # In practice, this strips the top-level "src/" dir from the hierarchy in # the solution. - while len(root) == 1 and type(root[list(root)[0]]) == dict: - root = root[list(root)[0]] + while len(root) == 1 and type(root[next(iter(root))]) == dict: + root = root[next(iter(root))] # Collapse singles. root = _CollapseSingles('', root) # Merge buckets until everything is a root entry. diff --git a/pylib/gyp/generator/ninja.py b/pylib/gyp/generator/ninja.py index d5006bf8..205829f4 100644 --- a/pylib/gyp/generator/ninja.py +++ b/pylib/gyp/generator/ninja.py @@ -1774,7 +1774,7 @@ class MEMORYSTATUSEX(ctypes.Structure): # VS 2015 uses 20% more working set than VS 2013 and can consume all RAM # on a 64 GB machine. - mem_limit = max(1, stat.ullTotalPhys / (5 * (2 ** 30))) # total / 5GB + mem_limit = max(1, stat.ullTotalPhys // (5 * (2 ** 30))) # total / 5GB hard_cap = max(1, int(os.environ.get('GYP_LINK_CONCURRENCY_MAX', 2**32))) return min(mem_limit, hard_cap) elif sys.platform.startswith('linux'): @@ -1786,14 +1786,14 @@ class MEMORYSTATUSEX(ctypes.Structure): if not match: continue # Allow 8Gb per link on Linux because Gold is quite memory hungry - return max(1, int(match.group(1)) / (8 * (2 ** 20))) + return max(1, int(match.group(1)) // (8 * (2 ** 20))) return 1 elif sys.platform == 'darwin': try: avail_bytes = int(subprocess.check_output(['sysctl', '-n', 'hw.memsize'])) # A static library debug build of Chromium's unit_tests takes ~2.7GB, so # 4GB per ld process allows for some more bloat. - return max(1, avail_bytes / (4 * (2 ** 30))) # total / 4GB + return max(1, avail_bytes // (4 * (2 ** 30))) # total / 4GB except: return 1 else: @@ -2483,7 +2483,7 @@ def GenerateOutput(target_list, target_dicts, data, params): GenerateOutputForConfig(target_list, target_dicts, data, params, user_config) else: - config_names = target_dicts[target_list[0]]['configurations'].keys() + config_names = target_dicts[target_list[0]]['configurations'] if params['parallel']: try: pool = multiprocessing.Pool(len(config_names)) diff --git a/pylib/gyp/generator/xcode.py b/pylib/gyp/generator/xcode.py index 4917ba77..1d75b24f 100644 --- a/pylib/gyp/generator/xcode.py +++ b/pylib/gyp/generator/xcode.py @@ -446,7 +446,7 @@ def Write(self): dir=self.path) try: - output_file = os.fdopen(output_fd, 'wb') + output_file = os.fdopen(output_fd, 'w') self.project_file.Print(output_file) output_file.close() @@ -1018,22 +1018,21 @@ def GenerateOutput(target_list, target_dicts, data, params): makefile_name) # TODO(mark): try/close? Write to a temporary file and swap it only # if it's got changes? - makefile = open(makefile_path, 'wb') + makefile = open(makefile_path, 'w') # make will build the first target in the makefile by default. By # convention, it's called "all". List all (or at least one) # concrete output for each rule source as a prerequisite of the "all" # target. makefile.write('all: \\\n') - for concrete_output_index in \ - range(0, len(concrete_outputs_by_rule_source)): + for concrete_output_index, concrete_output_by_rule_source in \ + enumerate(concrete_outputs_by_rule_source): # Only list the first (index [0]) concrete output of each input # in the "all" target. Otherwise, a parallel make (-j > 1) would # attempt to process each input multiple times simultaneously. # Otherwise, "all" could just contain the entire list of # concrete_outputs_all. - concrete_output = \ - concrete_outputs_by_rule_source[concrete_output_index][0] + concrete_output = concrete_output_by_rule_source[0] if concrete_output_index == len(concrete_outputs_by_rule_source) - 1: eol = '' else: @@ -1049,8 +1048,8 @@ def GenerateOutput(target_list, target_dicts, data, params): # rule source. Collect the names of the directories that are # required. concrete_output_dirs = [] - for concrete_output_index in range(0, len(concrete_outputs)): - concrete_output = concrete_outputs[concrete_output_index] + for concrete_output_index, concrete_output in \ + enumerate(concrete_outputs): if concrete_output_index == 0: bol = '' else: @@ -1068,8 +1067,7 @@ def GenerateOutput(target_list, target_dicts, data, params): # the set of additional rule inputs, if any. prerequisites = [rule_source] prerequisites.extend(rule.get('inputs', [])) - for prerequisite_index in range(0, len(prerequisites)): - prerequisite = prerequisites[prerequisite_index] + for prerequisite_index, prerequisite in enumerate(prerequisites): if prerequisite_index == len(prerequisites) - 1: eol = '' else: diff --git a/pylib/gyp/input.py b/pylib/gyp/input.py index 1f40abb0..ddf8db26 100644 --- a/pylib/gyp/input.py +++ b/pylib/gyp/input.py @@ -1028,9 +1028,9 @@ def ExpandVariables(input, phase, variables, build_file): # Convert all strings that are canonically-represented integers into integers. if type(output) is list: - for index in range(0, len(output)): - if IsStrCanonicalInt(output[index]): - output[index] = int(output[index]) + for index, outstr in enumerate(output): + if IsStrCanonicalInt(outstr): + output[index] = int(outstr) elif IsStrCanonicalInt(output): output = int(output) @@ -1386,9 +1386,9 @@ def QualifyDependencies(targets): toolset = target_dict['toolset'] for dependency_key in all_dependency_sections: dependencies = target_dict.get(dependency_key, []) - for index in range(0, len(dependencies)): + for index, dep in enumerate(dependencies): dep_file, dep_target, dep_toolset = gyp.common.ResolveTarget( - target_build_file, dependencies[index], toolset) + target_build_file, dep, toolset) if not multiple_toolsets: # Ignore toolset specification in the dependency if it is specified. dep_toolset = toolset @@ -1835,7 +1835,7 @@ def BuildDependencyList(targets): if not root_node.dependents: # If all targets have dependencies, add the first target as a dependent # of root_node so that the cycle can be discovered from root_node. - target = targets.keys()[0] + target = next(iter(targets)) target_node = dependency_nodes[target] target_node.dependencies.append(root_node) root_node.dependents.append(target_node) @@ -1898,7 +1898,7 @@ def VerifyNoGYPFileCircularDependencies(targets): if not root_node.dependents: # If all files have dependencies, add the first file as a dependent # of root_node so that the cycle can be discovered from root_node. - file_node = dependency_nodes.values()[0] + file_node = next(iter(dependency_nodes.values())) file_node.dependencies.append(root_node) root_node.dependents.append(file_node) cycles = [] @@ -2295,10 +2295,9 @@ def SetUpConfigurations(target, target_dict): merged_configurations[configuration]) # Now drop all the abstract ones. - for configuration in list(target_dict['configurations']): - old_configuration_dict = target_dict['configurations'][configuration] - if old_configuration_dict.get('abstract'): - del target_dict['configurations'][configuration] + configs = target_dict['configurations'] + target_dict['configurations'] = \ + {k: v for k, v in configs.items() if not v.get('abstract')} # Now that all of the target's configurations have been built, go through # the target dict's keys and remove everything that's been moved into a @@ -2406,8 +2405,8 @@ def ProcessListFiltersInDict(name, the_dict): exclude_key = list_key + '!' if exclude_key in the_dict: for exclude_item in the_dict[exclude_key]: - for index in range(0, len(the_list)): - if exclude_item == the_list[index]: + for index, list_item in enumerate(the_list): + if exclude_item == list_item: # This item matches the exclude_item, so set its action to 0 # (exclude). list_actions[index] = 0 @@ -2432,8 +2431,7 @@ def ProcessListFiltersInDict(name, the_dict): raise ValueError('Unrecognized action ' + action + ' in ' + name + \ ' key ' + regex_key) - for index in range(0, len(the_list)): - list_item = the_list[index] + for index, list_item in enumerate(the_list): if list_actions[index] == action_value: # Even if the regex matches, nothing will change so continue (regex # searches are expensive). @@ -2672,8 +2670,7 @@ def TurnIntIntoStrInDict(the_dict): def TurnIntIntoStrInList(the_list): """Given list the_list, recursively converts all integers into strings. """ - for index in range(0, len(the_list)): - item = the_list[index] + for index, item in enumerate(the_list): if type(item) is int: the_list[index] = str(item) elif type(item) is dict: diff --git a/pylib/gyp/mac_tool.py b/pylib/gyp/mac_tool.py index 781a8633..aa9f3d43 100755 --- a/pylib/gyp/mac_tool.py +++ b/pylib/gyp/mac_tool.py @@ -18,7 +18,6 @@ import plistlib import re import shutil -import string import struct import subprocess import sys @@ -157,9 +156,11 @@ def _DetectInputEncoding(self, file_name): header = fp.read(3) except Exception: return None - if header.startswith(("\xFE\xFF", "\xFF\xFE")): + if header.startswith(b"\xFE\xFF"): return "UTF-16" - elif header.startswith("\xEF\xBB\xBF"): + elif header.startswith(b"\xFF\xFE"): + return "UTF-16" + elif header.startswith(b"\xEF\xBB\xBF"): return "UTF-8" else: return None @@ -173,7 +174,7 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys): # Insert synthesized key/value pairs (e.g. BuildMachineOSBuild). plist = plistlib.readPlistFromString(lines) if keys: - plist = dict(plist.items() + json.loads(keys[0]).items()) + plist.update(json.loads(keys[0])) lines = plistlib.writePlistToString(plist) # Go through all the environment variables and replace them as variables in @@ -184,7 +185,7 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys): continue evar = '${%s}' % key evalue = os.environ[key] - lines = string.replace(lines, evar, evalue) + lines = lines.replace(lines, evar, evalue) # Xcode supports various suffices on environment variables, which are # all undocumented. :rfc1034identifier is used in the standard project @@ -194,11 +195,11 @@ def ExecCopyInfoPlist(self, source, dest, convert_to_binary, *keys): # in a URL either -- oops, hence :rfc1034identifier was born. evar = '${%s:identifier}' % key evalue = IDENT_RE.sub('_', os.environ[key]) - lines = string.replace(lines, evar, evalue) + lines = lines.replace(lines, evar, evalue) evar = '${%s:rfc1034identifier}' % key evalue = IDENT_RE.sub('-', os.environ[key]) - lines = string.replace(lines, evar, evalue) + lines = lines.replace(lines, evar, evalue) # Remove any keys with values that haven't been replaced. lines = lines.splitlines() diff --git a/pylib/gyp/msvs_emulation.py b/pylib/gyp/msvs_emulation.py index d42e2e47..57b258e8 100644 --- a/pylib/gyp/msvs_emulation.py +++ b/pylib/gyp/msvs_emulation.py @@ -490,7 +490,7 @@ def GetCflags(self, config): # https://msdn.microsoft.com/en-us/library/dn502518.aspx cflags.append('/FS') # ninja handles parallelism by itself, don't have the compiler do it too. - cflags = filter(lambda x: not x.startswith('/MP'), cflags) + cflags = [x for x in cflags if not x.startswith('/MP')] return cflags def _GetPchFlags(self, config, extension): @@ -660,19 +660,17 @@ def GetLdflags(self, config, gyp_to_build_path, expand_special, # If the base address is not specifically controlled, DYNAMICBASE should # be on by default. - base_flags = filter(lambda x: 'DYNAMICBASE' in x or x == '/FIXED', - ldflags) - if not base_flags: + if not any('DYNAMICBASE' in flag or flag == '/FIXED' for flag in ldflags): ldflags.append('/DYNAMICBASE') # If the NXCOMPAT flag has not been specified, default to on. Despite the # documentation that says this only defaults to on when the subsystem is # Vista or greater (which applies to the linker), the IDE defaults it on # unless it's explicitly off. - if not filter(lambda x: 'NXCOMPAT' in x, ldflags): + if not any('NXCOMPAT' in flag for flag in ldflags): ldflags.append('/NXCOMPAT') - have_def_file = filter(lambda x: x.startswith('/DEF:'), ldflags) + have_def_file = any(flag.startswith('/DEF:') for flag in ldflags) manifest_flags, intermediate_manifest, manifest_files = \ self._GetLdManifestFlags(config, manifest_base_name, gyp_to_build_path, is_executable and not have_def_file, build_dir) @@ -1071,7 +1069,7 @@ def GenerateEnvironmentFiles(toplevel_build_dir, generator_flags, env['INCLUDE'] = ';'.join(system_includes) env_block = _FormatAsEnvironmentBlock(env) - f = open_out(os.path.join(toplevel_build_dir, 'environment.' + arch), 'wb') + f = open_out(os.path.join(toplevel_build_dir, 'environment.' + arch), 'w') f.write(env_block) f.close() @@ -1095,7 +1093,7 @@ def VerifyMissingSources(sources, build_dir, generator_flags, gyp_to_ninja): if int(generator_flags.get('msvs_error_on_missing_sources', 0)): no_specials = filter(lambda x: '$' not in x, sources) relative = [os.path.join(build_dir, gyp_to_ninja(s)) for s in no_specials] - missing = filter(lambda x: not os.path.exists(x), relative) + missing = [x for x in relative if not os.path.exists(x)] if missing: # They'll look like out\Release\..\..\stuff\things.cc, so normalize the # path for a slightly less crazy looking output. diff --git a/pylib/gyp/win_tool.py b/pylib/gyp/win_tool.py index cfdacb0d..23b4826a 100755 --- a/pylib/gyp/win_tool.py +++ b/pylib/gyp/win_tool.py @@ -198,16 +198,18 @@ def ExecLinkWithManifests(self, arch, embed_manifest, out, ldcmd, resname, our_manifest = '%(out)s.manifest' % variables # Load and normalize the manifests. mt.exe sometimes removes whitespace, # and sometimes doesn't unfortunately. - with open(our_manifest, 'rb') as our_f: - with open(assert_manifest, 'rb') as assert_f: + with open(our_manifest, 'r') as our_f: + with open(assert_manifest, 'r') as assert_f: our_data = our_f.read().translate(None, string.whitespace) assert_data = assert_f.read().translate(None, string.whitespace) if our_data != assert_data: os.unlink(out) def dump(filename): - sys.stderr.write('%s\n-----\n' % filename) - with open(filename, 'rb') as f: - sys.stderr.write(f.read() + '\n-----\n') + print(filename, file=sys.stderr) + print('-----', file=sys.stderr) + with open(filename, 'r') as f: + print(f.read(), file=sys.stderr) + print('-----', file=sys.stderr) dump(intermediate_manifest) dump(our_manifest) dump(assert_manifest) @@ -238,7 +240,7 @@ def ExecManifestToRc(self, arch, *args): |args| is tuple containing path to resource file, path to manifest file and resource name which can be "1" (for executables) or "2" (for DLLs).""" manifest_path, resource_path, resource_name = args - with open(resource_path, 'wb') as output: + with open(resource_path, 'w') as output: output.write('#include \n%s RT_MANIFEST "%s"' % ( resource_name, os.path.abspath(manifest_path).replace('\\', '/'))) diff --git a/pylib/gyp/xcodeproj_file.py b/pylib/gyp/xcodeproj_file.py index 1e950dce..1f9c1913 100644 --- a/pylib/gyp/xcodeproj_file.py +++ b/pylib/gyp/xcodeproj_file.py @@ -445,7 +445,7 @@ def _HashUpdate(hash, data): # is 160 bits. Instead of throwing out 64 bits of the digest, xor them # into the portion that gets used. assert hash.digest_size % 4 == 0 - digest_int_count = hash.digest_size / 4 + digest_int_count = hash.digest_size // 4 digest_ints = struct.unpack('>' + 'I' * digest_int_count, hash.digest()) id_ints = [0, 0, 0] for index in range(0, digest_int_count): @@ -599,7 +599,7 @@ def _XCPrintableValue(self, tabs, value, flatten_list=False): comment = value.Comment() elif isinstance(value, str): printable += self._EncodeString(value) - elif isinstance(value, unicode): + elif isinstance(value, basestring): printable += self._EncodeString(value.encode('utf-8')) elif isinstance(value, int): printable += str(value) @@ -762,7 +762,7 @@ def UpdateProperties(self, properties, do_copy=False): ' must be list, not ' + value.__class__.__name__) for item in value: if not isinstance(item, property_type) and \ - not (item.__class__ == unicode and property_type == str): + not (isinstance(item, basestring) and property_type == str): # Accept unicode where str is specified. str is treated as # UTF-8-encoded. raise TypeError( @@ -770,7 +770,7 @@ def UpdateProperties(self, properties, do_copy=False): ' must be ' + property_type.__name__ + ', not ' + \ item.__class__.__name__) elif not isinstance(value, property_type) and \ - not (value.__class__ == unicode and property_type == str): + not (isinstance(value, basestring) and property_type == str): # Accept unicode where str is specified. str is treated as # UTF-8-encoded. raise TypeError( @@ -1421,8 +1421,8 @@ def PathHashables(self): xche = self while xche != None and isinstance(xche, XCHierarchicalElement): xche_hashables = xche.Hashables() - for index in range(0, len(xche_hashables)): - hashables.insert(index, xche_hashables[index]) + for index, xche_hashable in enumerate(xche_hashables): + hashables.insert(index, xche_hashable) xche = xche.parent return hashables @@ -2463,8 +2463,7 @@ def HeadersPhase(self): # The headers phase should come before the resources, sources, and # frameworks phases, if any. insert_at = len(self._properties['buildPhases']) - for index in range(0, len(self._properties['buildPhases'])): - phase = self._properties['buildPhases'][index] + for index, phase in enumerate(self._properties['buildPhases']): if isinstance(phase, PBXResourcesBuildPhase) or \ isinstance(phase, PBXSourcesBuildPhase) or \ isinstance(phase, PBXFrameworksBuildPhase): @@ -2484,8 +2483,7 @@ def ResourcesPhase(self): # The resources phase should come before the sources and frameworks # phases, if any. insert_at = len(self._properties['buildPhases']) - for index in range(0, len(self._properties['buildPhases'])): - phase = self._properties['buildPhases'][index] + for index, phase in enumerate(self._properties['buildPhases']): if isinstance(phase, PBXSourcesBuildPhase) or \ isinstance(phase, PBXFrameworksBuildPhase): insert_at = index diff --git a/pylib/gyp/xml_fix.py b/pylib/gyp/xml_fix.py index 5de84815..4308d99b 100644 --- a/pylib/gyp/xml_fix.py +++ b/pylib/gyp/xml_fix.py @@ -32,8 +32,7 @@ def _Replacement_writexml(self, writer, indent="", addindent="", newl=""): writer.write(indent+"<" + self.tagName) attrs = self._get_attributes() - a_names = attrs.keys() - a_names.sort() + a_names = sorted(attrs.keys()) for a_name in a_names: writer.write(" %s=\"" % a_name)