Skip to content

Commit 41430be

Browse files
committed
tools: port Python 3 compat patches from node-gyp to gyp
Refs: nodejs/node-gyp#1820 Refs: nodejs/node-gyp#1843 PR-URL: #29897 Reviewed-By: Christian Clauss <[email protected]> Reviewed-By: Sam Roberts <[email protected]>
1 parent aac2476 commit 41430be

File tree

10 files changed

+36
-29
lines changed

10 files changed

+36
-29
lines changed

tools/gyp/pylib/gyp/MSVSNew.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def MakeGuid(name, seed='msvs_new'):
4545
not change when the project for a target is rebuilt.
4646
"""
4747
# Calculate a MD5 signature for the seed and name.
48-
d = hashlib.md5(str(seed) + str(name)).hexdigest().upper()
48+
d = hashlib.md5((str(seed) + str(name)).encode('utf-8')).hexdigest().upper()
4949
# Convert most of the signature to GUID form (discard the rest)
5050
guid = ('{' + d[:8] + '-' + d[8:12] + '-' + d[12:16] + '-' + d[16:20]
5151
+ '-' + d[20:32] + '}')

tools/gyp/pylib/gyp/common.py

+3
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ def close(self):
394394
os.unlink(self.tmp_path)
395395
raise
396396

397+
def write(self, s):
398+
self.tmp_file.write(s.encode('utf-8'))
399+
397400
return Writer()
398401

399402

tools/gyp/pylib/gyp/easy_xml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False,
120120

121121
default_encoding = locale.getdefaultlocale()[1]
122122
if default_encoding and default_encoding.upper() != encoding.upper():
123-
xml_string = xml_string.decode(default_encoding).encode(encoding)
123+
xml_string = xml_string.encode(encoding)
124124

125125
# Get the old content
126126
try:
@@ -132,7 +132,7 @@ def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False,
132132

133133
# It has changed, write it
134134
if existing != xml_string:
135-
f = open(path, 'w')
135+
f = open(path, 'wb')
136136
f.write(xml_string)
137137
f.close()
138138

tools/gyp/pylib/gyp/generator/analyzer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def find_matching_compile_target_names(self):
671671
assert self.is_build_impacted()
672672
# Compile targets are found by searching up from changed targets.
673673
# Reset the visited status for _GetBuildTargets.
674-
for target in self._name_to_target.itervalues():
674+
for target in self._name_to_target.values():
675675
target.visited = False
676676

677677
supplied_targets = _LookupTargets(self._supplied_target_names_no_all(),

tools/gyp/pylib/gyp/generator/eclipse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def WriteMacros(out, eclipse_langs, defines):
272272
out.write(' <language name="holder for library settings"></language>\n')
273273
for lang in eclipse_langs:
274274
out.write(' <language name="%s">\n' % lang)
275-
for key in sorted(defines.iterkeys()):
275+
for key in sorted(defines):
276276
out.write(' <macro><name>%s</name><value>%s</value></macro>\n' %
277277
(escape(key), escape(defines[key])))
278278
out.write(' </language>\n')

tools/gyp/pylib/gyp/generator/make.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def Write(self, qualified_target, base_path, output_filename, spec, configs,
800800
gyp.xcode_emulation.MacPrefixHeader(
801801
self.xcode_settings, lambda p: Sourceify(self.Absolutify(p)),
802802
self.Pchify))
803-
sources = filter(Compilable, all_sources)
803+
sources = list(filter(Compilable, all_sources))
804804
if sources:
805805
self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT1)
806806
extensions = set([os.path.splitext(s)[1] for s in sources])
@@ -953,7 +953,7 @@ def WriteActions(self, actions, extra_sources, extra_outputs,
953953
outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs]
954954
inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs]
955955

956-
self.WriteDoCmd(outputs, map(Sourceify, map(self.Absolutify, inputs)),
956+
self.WriteDoCmd(outputs, [Sourceify(self.Absolutify(i)) for i in inputs],
957957
part_of_all=part_of_all, command=name)
958958

959959
# Stuff the outputs in a variable so we can refer to them later.
@@ -1002,8 +1002,8 @@ def WriteRules(self, rules, extra_sources, extra_outputs,
10021002
extra_sources += outputs
10031003
if int(rule.get('process_outputs_as_mac_bundle_resources', False)):
10041004
extra_mac_bundle_resources += outputs
1005-
inputs = map(Sourceify, map(self.Absolutify, [rule_source] +
1006-
rule.get('inputs', [])))
1005+
inputs = [Sourceify(self.Absolutify(i)) for i
1006+
in [rule_source] + rule.get('inputs', [])]
10071007
actions = ['$(call do_cmd,%s_%d)' % (name, count)]
10081008

10091009
if name == 'resources_grit':
@@ -1126,7 +1126,7 @@ def WriteCopies(self, copies, extra_outputs, part_of_all):
11261126
path = gyp.xcode_emulation.ExpandEnvVars(path, env)
11271127
self.WriteDoCmd([output], [path], 'copy', part_of_all)
11281128
outputs.append(output)
1129-
self.WriteLn('%s = %s' % (variable, ' '.join(map(QuoteSpaces, outputs))))
1129+
self.WriteLn('%s = %s' % (variable, ' '.join(QuoteSpaces(o) for o in outputs)))
11301130
extra_outputs.append('$(%s)' % variable)
11311131
self.WriteLn()
11321132

@@ -1137,7 +1137,7 @@ def WriteMacBundleResources(self, resources, bundle_deps):
11371137

11381138
for output, res in gyp.xcode_emulation.GetMacBundleResources(
11391139
generator_default_variables['PRODUCT_DIR'], self.xcode_settings,
1140-
map(Sourceify, map(self.Absolutify, resources))):
1140+
[Sourceify(self.Absolutify(r)) for r in resources]):
11411141
_, ext = os.path.splitext(output)
11421142
if ext != '.xcassets':
11431143
# Make does not supports '.xcassets' emulation.
@@ -1217,11 +1217,11 @@ def WriteSources(self, configs, deps, sources,
12171217
self.WriteList(cflags_objcc, 'CFLAGS_OBJCC_%s' % configname)
12181218
includes = config.get('include_dirs')
12191219
if includes:
1220-
includes = map(Sourceify, map(self.Absolutify, includes))
1220+
includes = [Sourceify(self.Absolutify(i)) for i in includes]
12211221
self.WriteList(includes, 'INCS_%s' % configname, prefix='-I')
12221222

1223-
compilable = filter(Compilable, sources)
1224-
objs = map(self.Objectify, map(self.Absolutify, map(Target, compilable)))
1223+
compilable = list(filter(Compilable, sources))
1224+
objs = [self.Objectify(self.Absolutify(Target(c))) for c in compilable]
12251225
self.WriteList(objs, 'OBJS')
12261226

12271227
for obj in objs:
@@ -1293,7 +1293,7 @@ def WriteSources(self, configs, deps, sources,
12931293

12941294
# If there are any object files in our input file list, link them into our
12951295
# output.
1296-
extra_link_deps += filter(Linkable, sources)
1296+
extra_link_deps += list(filter(Linkable, sources))
12971297

12981298
self.WriteLn()
12991299

@@ -1543,7 +1543,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps,
15431543

15441544
# Bundle dependencies. Note that the code below adds actions to this
15451545
# target, so if you move these two lines, move the lines below as well.
1546-
self.WriteList(map(QuoteSpaces, bundle_deps), 'BUNDLE_DEPS')
1546+
self.WriteList([QuoteSpaces(dep) for dep in bundle_deps], 'BUNDLE_DEPS')
15471547
self.WriteLn('%s: $(BUNDLE_DEPS)' % QuoteSpaces(self.output))
15481548

15491549
# After the framework is built, package it. Needs to happen before
@@ -1577,7 +1577,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps,
15771577
if self.type == 'executable':
15781578
self.WriteLn('%s: LD_INPUTS := %s' % (
15791579
QuoteSpaces(self.output_binary),
1580-
' '.join(map(QuoteSpaces, link_deps))))
1580+
' '.join(QuoteSpaces(dep) for dep in link_deps)))
15811581
if self.toolset == 'host' and self.flavor == 'android':
15821582
self.WriteDoCmd([self.output_binary], link_deps, 'link_host',
15831583
part_of_all, postbuilds=postbuilds)
@@ -1599,7 +1599,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps,
15991599
elif self.type == 'shared_library':
16001600
self.WriteLn('%s: LD_INPUTS := %s' % (
16011601
QuoteSpaces(self.output_binary),
1602-
' '.join(map(QuoteSpaces, link_deps))))
1602+
' '.join(QuoteSpaces(dep) for dep in link_deps)))
16031603
self.WriteDoCmd([self.output_binary], link_deps, 'solink', part_of_all,
16041604
postbuilds=postbuilds)
16051605
elif self.type == 'loadable_module':
@@ -1815,7 +1815,7 @@ def WriteAndroidNdkModuleRule(self, module_name, all_sources, link_deps):
18151815
default_cpp_ext = ext
18161816
self.WriteLn('LOCAL_CPP_EXTENSION := ' + default_cpp_ext)
18171817

1818-
self.WriteList(map(self.Absolutify, filter(Compilable, all_sources)),
1818+
self.WriteList(list(map(self.Absolutify, filter(Compilable, all_sources))),
18191819
'LOCAL_SRC_FILES')
18201820

18211821
# Filter out those which do not match prefix and suffix and produce
@@ -1956,7 +1956,7 @@ def WriteAutoRegenerationRule(params, root_makefile, makefile_name,
19561956
"%(makefile_name)s: %(deps)s\n"
19571957
"\t$(call do_cmd,regen_makefile)\n\n" % {
19581958
'makefile_name': makefile_name,
1959-
'deps': ' '.join(map(Sourceify, build_files)),
1959+
'deps': ' '.join(Sourceify(bf) for bf in build_files),
19601960
'cmd': gyp.common.EncodePOSIXShellList(
19611961
[gyp_binary, '-fmake'] +
19621962
gyp.RegenerateFlags(options) +

tools/gyp/pylib/gyp/generator/msvs.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1778,8 +1778,8 @@ def _CollapseSingles(parent, node):
17781778
# such projects up one level.
17791779
if (type(node) == dict and
17801780
len(node) == 1 and
1781-
node.keys()[0] == parent + '.vcproj'):
1782-
return node[node.keys()[0]]
1781+
list(node)[0] == parent + '.vcproj'):
1782+
return node[list(node)[0]]
17831783
if type(node) != dict:
17841784
return node
17851785
for child in node:
@@ -1798,8 +1798,8 @@ def _GatherSolutionFolders(sln_projects, project_objects, flat):
17981798
# Walk down from the top until we hit a folder that has more than one entry.
17991799
# In practice, this strips the top-level "src/" dir from the hierarchy in
18001800
# the solution.
1801-
while len(root) == 1 and type(root[root.keys()[0]]) == dict:
1802-
root = root[root.keys()[0]]
1801+
while len(root) == 1 and type(root[list(root)[0]]) == dict:
1802+
root = root[list(root)[0]]
18031803
# Collapse singles.
18041804
root = _CollapseSingles('', root)
18051805
# Merge buckets until everything is a root entry.
@@ -2728,7 +2728,7 @@ def _GetMSBuildGlobalProperties(spec, version, guid, gyp_file_name):
27282728

27292729
platform_name = None
27302730
msvs_windows_sdk_version = None
2731-
for configuration in spec['configurations'].itervalues():
2731+
for configuration in spec['configurations'].values():
27322732
platform_name = platform_name or _ConfigPlatform(configuration)
27332733
msvs_windows_sdk_version = (msvs_windows_sdk_version or
27342734
_ConfigWindowsTargetPlatformVersion(configuration, version))
@@ -3299,7 +3299,7 @@ def _GetMSBuildProjectReferences(project):
32993299
['Project', guid],
33003300
['ReferenceOutputAssembly', 'false']
33013301
]
3302-
for config in dependency.spec.get('configurations', {}).itervalues():
3302+
for config in dependency.spec.get('configurations', {}).values():
33033303
if config.get('msvs_use_library_dependency_inputs', 0):
33043304
project_ref.append(['UseLibraryDependencyInputs', 'true'])
33053305
break
@@ -3368,7 +3368,7 @@ def _GenerateMSBuildProject(project, options, version, generator_flags):
33683368
extension_to_rule_name, _GetUniquePlatforms(spec))
33693369
missing_sources = _VerifySourcesExist(sources, project_dir)
33703370

3371-
for configuration in configurations.itervalues():
3371+
for configuration in configurations.values():
33723372
_FinalizeMSBuildSettings(spec, configuration)
33733373

33743374
# Add attributes to root element

tools/gyp/pylib/gyp/input.py

+4
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,12 @@ def to_utf8(s):
942942
else:
943943
replacement = variables[contents]
944944

945+
if isinstance(replacement, bytes) and not isinstance(replacement, str):
946+
replacement = replacement.decode("utf-8") # done on Python 3 only
945947
if type(replacement) is list:
946948
for item in replacement:
949+
if isinstance(item, bytes) and not isinstance(item, str):
950+
item = item.decode("utf-8") # done on Python 3 only
947951
if not contents[-1] == '/' and type(item) not in (str, int):
948952
raise GypError('Variable ' + contents +
949953
' must expand to a string or list of strings; ' +

tools/gyp/pylib/gyp/xcode_emulation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ def _HasIOSTarget(targets):
17921792
def _AddIOSDeviceConfigurations(targets):
17931793
"""Clone all targets and append -iphoneos to the name. Configure these targets
17941794
to build for iOS devices and use correct architectures for those builds."""
1795-
for target_dict in targets.itervalues():
1795+
for target_dict in targets.values():
17961796
toolset = target_dict['toolset']
17971797
configs = target_dict['configurations']
17981798
for config_name, simulator_config_dict in dict(configs).items():

tools/gyp/pylib/gyp/xcode_ninja.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _TargetFromSpec(old_spec, params):
8585
"%s/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)" % ninja_toplevel
8686

8787
if 'configurations' in old_spec:
88-
for config in old_spec['configurations'].iterkeys():
88+
for config in old_spec['configurations']:
8989
old_xcode_settings = \
9090
old_spec['configurations'][config].get('xcode_settings', {})
9191
if 'IPHONEOS_DEPLOYMENT_TARGET' in old_xcode_settings:

0 commit comments

Comments
 (0)