Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize Python 2 code to get ready for Python 3 #1571

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gyp/pylib/gyp/MSVSNew.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import random

import gyp.common
from gyp.common import compat_cmp

# hashlib is supplied as of Python 2.5 as the replacement interface for md5
# and other secure hashes. In 2.6, md5 is deprecated. Import hashlib if
Expand Down Expand Up @@ -62,7 +63,7 @@ def MakeGuid(name, seed='msvs_new'):
class MSVSSolutionEntry(object):
def __cmp__(self, other):
# Sort by name then guid (so things are in order on vs2008).
return cmp((self.name, self.get_guid()), (other.name, other.get_guid()))
return compat_cmp((self.name, self.get_guid()), (other.name, other.get_guid()))


class MSVSFolder(MSVSSolutionEntry):
Expand Down
2 changes: 1 addition & 1 deletion gyp/pylib/gyp/MSVSSettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from __future__ import print_function

from gyp import string_types
from gyp.compat import string_types

import sys
import re
Expand Down
4 changes: 2 additions & 2 deletions gyp/pylib/gyp/MSVSSettings_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

"""Unit tests for the MSVSSettings.py file."""

import StringIO
from gyp.compat import StringIO
import unittest
import gyp.MSVSSettings as MSVSSettings


class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.stderr = StringIO.StringIO()
self.stderr = StringIO()

def _ExpectedWarnings(self, expected):
"""Compares recorded lines to expected warnings."""
Expand Down
8 changes: 3 additions & 5 deletions gyp/pylib/gyp/MSVSVersion.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,14 @@ def _RegistryGetValueUsingWinReg(key, value):
ImportError if _winreg is unavailable.
"""
try:
# Python 2
from _winreg import OpenKey, QueryValueEx
from winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE
except ImportError:
# Python 3
from winreg import OpenKey, QueryValueEx
from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE

try:
root, subkey = key.split('\\', 1)
assert root == 'HKLM' # Only need HKLM for now.
with OpenKey(_winreg.HKEY_LOCAL_MACHINE, subkey) as hkey:
with OpenKey(HKEY_LOCAL_MACHINE, subkey) as hkey:
return QueryValueEx(hkey, value)[0]
except WindowsError:
return None
Expand Down
7 changes: 1 addition & 6 deletions gyp/pylib/gyp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
import sys
import traceback
from gyp.common import GypError
from gyp.compat import string_types

try:
# Python 2
string_types = basestring
except NameError:
# Python 3
string_types = str

# Default debug modes for GYP
debug = {}
Expand Down
24 changes: 24 additions & 0 deletions gyp/pylib/gyp/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys

if sys.version_info[0] == 2:
from cStringIO import StringIO
string_types = (basestring,) # noqa: F821
simple_types = types = (type(None), int, long, float, bool, # noqa: F821
str, unicode, type, basestring) # noqa: F821
unicode_type = unicode # noqa: F821
compat_cmp = cmp # noqa: F821
else:
from io import StringIO
string_types = (str, )
simple_types = types = (type(None), int, float, bool, str, type)
unicode_type = str

def compat_cmp(x, y):

if x == y:
return 0

if x < y:
return -1

return 1
4 changes: 2 additions & 2 deletions gyp/pylib/gyp/easy_xml_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import gyp.easy_xml as easy_xml
import unittest
import StringIO
from gyp.compat import StringIO


class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.stderr = StringIO.StringIO()
self.stderr = StringIO()

def test_EasyXml_simple(self):
self.assertEqual(
Expand Down
2 changes: 1 addition & 1 deletion gyp/pylib/gyp/generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def find_matching_compile_target_names(self):
assert self.is_build_impacted();
# Compile targets are found by searching up from changed targets.
# Reset the visited status for _GetBuildTargets.
for target in self._name_to_target.itervalues():
for target in self._name_to_target.values():
target.visited = False

supplied_targets = _LookupTargets(self._supplied_target_names_no_all(),
Expand Down
11 changes: 6 additions & 5 deletions gyp/pylib/gyp/generator/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def WriteRules(self, rules, extra_sources, extra_outputs):

# We set up a rule to build the first output, and then set up
# a rule for each additional output to depend on the first.
outputs = map(self.LocalPathify, outputs)
outputs = list(map(self.LocalPathify, outputs))
main_output = outputs[0]
self.WriteLn('%s: gyp_local_path := $(LOCAL_PATH)' % main_output)
self.WriteLn('%s: gyp_var_prefix := $(GYP_VAR_PREFIX)' % main_output)
Expand Down Expand Up @@ -475,7 +475,7 @@ def WriteSourceFlags(self, spec, configs):
self.WriteLn('\n# Include paths placed before CFLAGS/CPPFLAGS')
includes = list(config.get('include_dirs', []))
includes.extend(extracted_includes)
includes = map(Sourceify, map(self.LocalPathify, includes))
includes = list(map(Sourceify, map(self.LocalPathify, includes)))
includes = self.NormalizeIncludePaths(includes)
self.WriteList(includes, 'LOCAL_C_INCLUDES_%s' % configname)

Expand Down Expand Up @@ -510,9 +510,9 @@ def WriteSources(self, spec, configs, extra_sources):
spec, configs: input from gyp.
extra_sources: Sources generated from Actions or Rules.
"""
sources = filter(make.Compilable, spec.get('sources', []))
sources = list(filter(make.Compilable, spec.get('sources', [])))
generated_not_sources = [x for x in extra_sources if not make.Compilable(x)]
extra_sources = filter(make.Compilable, extra_sources)
extra_sources = list(filter(make.Compilable, extra_sources))

# Determine and output the C++ extension used by these sources.
# We simply find the first C++ file and use that extension.
Expand Down Expand Up @@ -574,7 +574,8 @@ def WriteSources(self, spec, configs, extra_sources):
self.WriteList(final_generated_sources, 'LOCAL_GENERATED_SOURCES')

origin_src_dirs = gyp.common.uniquer(origin_src_dirs)
origin_src_dirs = map(Sourceify, map(self.LocalPathify, origin_src_dirs))
origin_src_dirs = list(map(Sourceify, map(self.LocalPathify,
origin_src_dirs)))
self.WriteList(origin_src_dirs, 'GYP_COPIED_SOURCE_ORIGIN_DIRS')

self.WriteList(local_files, 'LOCAL_SRC_FILES')
Expand Down
3 changes: 1 addition & 2 deletions gyp/pylib/gyp/generator/eclipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def WriteMacros(out, eclipse_langs, defines):
out.write(' <language name="holder for library settings"></language>\n')
for lang in eclipse_langs:
out.write(' <language name="%s">\n' % lang)
for key in sorted(defines.iterkeys()):
for key in sorted(defines.keys()):
out.write(' <macro><name>%s</name><value>%s</value></macro>\n' %
(escape(key), escape(defines[key])))
out.write(' </language>\n')
Expand Down Expand Up @@ -422,4 +422,3 @@ def GenerateOutput(target_list, target_dicts, data, params):
for config_name in config_names:
GenerateOutputForConfig(target_list, target_dicts, data, params,
config_name)

37 changes: 20 additions & 17 deletions gyp/pylib/gyp/generator/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = filter(Compilable, all_sources)
sources = list(filter(Compilable, all_sources))
if sources:
self.WriteLn(SHARED_HEADER_SUFFIX_RULES_COMMENT1)
extensions = set([os.path.splitext(s)[1] for s in sources])
Expand Down Expand Up @@ -950,7 +950,7 @@ def WriteActions(self, actions, extra_sources, extra_outputs,
'%s%s'
% (name, cd_action, command))
self.WriteLn()
outputs = map(self.Absolutify, outputs)
outputs = list(map(self.Absolutify, outputs))
# The makefile rules are all relative to the top dir, but the gyp actions
# are defined relative to their containing dir. This replaces the obj
# variable for the action rule with an absolute version so that the output
Expand All @@ -974,8 +974,9 @@ def WriteActions(self, actions, extra_sources, extra_outputs,
outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs]
inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs]

self.WriteDoCmd(outputs, map(Sourceify, map(self.Absolutify, inputs)),
part_of_all=part_of_all, command=name)
self.WriteDoCmd(
outputs, list(map(Sourceify, map(self.Absolutify, inputs))),
part_of_all=part_of_all, command=name)

# Stuff the outputs in a variable so we can refer to them later.
outputs_variable = 'action_%s_outputs' % name
Expand Down Expand Up @@ -1023,8 +1024,8 @@ def WriteRules(self, rules, extra_sources, extra_outputs,
extra_sources += outputs
if int(rule.get('process_outputs_as_mac_bundle_resources', False)):
extra_mac_bundle_resources += outputs
inputs = map(Sourceify, map(self.Absolutify, [rule_source] +
rule.get('inputs', [])))
inputs = list(map(Sourceify, map(self.Absolutify, [rule_source] +
rule.get('inputs', []))))
actions = ['$(call do_cmd,%s_%d)' % (name, count)]

if name == 'resources_grit':
Expand All @@ -1040,7 +1041,7 @@ def WriteRules(self, rules, extra_sources, extra_outputs,
outputs = [gyp.xcode_emulation.ExpandEnvVars(o, env) for o in outputs]
inputs = [gyp.xcode_emulation.ExpandEnvVars(i, env) for i in inputs]

outputs = map(self.Absolutify, outputs)
outputs = list(map(self.Absolutify, outputs))
all_outputs += outputs
# Only write the 'obj' and 'builddir' rules for the "primary" output
# (:1); it's superfluous for the "extra outputs", and this avoids
Expand Down Expand Up @@ -1158,7 +1159,7 @@ def WriteMacBundleResources(self, resources, bundle_deps):

for output, res in gyp.xcode_emulation.GetMacBundleResources(
generator_default_variables['PRODUCT_DIR'], self.xcode_settings,
map(Sourceify, map(self.Absolutify, resources))):
list(map(Sourceify, map(self.Absolutify, resources)))):
_, ext = os.path.splitext(output)
if ext != '.xcassets':
# Make does not supports '.xcassets' emulation.
Expand Down Expand Up @@ -1238,11 +1239,12 @@ def WriteSources(self, configs, deps, sources,
self.WriteList(cflags_objcc, 'CFLAGS_OBJCC_%s' % configname)
includes = config.get('include_dirs')
if includes:
includes = map(Sourceify, map(self.Absolutify, includes))
includes = list(map(Sourceify, map(self.Absolutify, includes)))
self.WriteList(includes, 'INCS_%s' % configname, prefix='-I')

compilable = filter(Compilable, sources)
objs = map(self.Objectify, map(self.Absolutify, map(Target, compilable)))
compilable = list(filter(Compilable, sources))
objs = list(map(
self.Objectify, map(self.Absolutify, map(Target, compilable))))
self.WriteList(objs, 'OBJS')

for obj in objs:
Expand Down Expand Up @@ -1314,7 +1316,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 += filter(Linkable, sources)
extra_link_deps += list(filter(Linkable, sources))

self.WriteLn()

Expand Down Expand Up @@ -1564,7 +1566,7 @@ def WriteTarget(self, spec, configs, deps, link_deps, bundle_deps,

# Bundle dependencies. Note that the code below adds actions to this
# target, so if you move these two lines, move the lines below as well.
self.WriteList(map(QuoteSpaces, bundle_deps), 'BUNDLE_DEPS')
self.WriteList(list(map(QuoteSpaces, bundle_deps)), 'BUNDLE_DEPS')
self.WriteLn('%s: $(BUNDLE_DEPS)' % QuoteSpaces(self.output))

# After the framework is built, package it. Needs to happen before
Expand Down Expand Up @@ -1746,8 +1748,8 @@ def WriteMakeRule(self, outputs, inputs, actions=None, comment=None,
output is just a name to run the rule
command: (optional) command name to generate unambiguous labels
"""
outputs = map(QuoteSpaces, outputs)
inputs = map(QuoteSpaces, inputs)
outputs = list(map(QuoteSpaces, outputs))
inputs = list(map(QuoteSpaces, inputs))

if comment:
self.WriteLn('# ' + comment)
Expand Down Expand Up @@ -1836,8 +1838,9 @@ def WriteAndroidNdkModuleRule(self, module_name, all_sources, link_deps):
default_cpp_ext = ext
self.WriteLn('LOCAL_CPP_EXTENSION := ' + default_cpp_ext)

self.WriteList(map(self.Absolutify, filter(Compilable, all_sources)),
'LOCAL_SRC_FILES')
self.WriteList(list(map(self.Absolutify, list(filter(Compilable,
all_sources))),
'LOCAL_SRC_FILES'))

# Filter out those which do not match prefix and suffix and produce
# the resulting list without prefix and suffix.
Expand Down
6 changes: 3 additions & 3 deletions gyp/pylib/gyp/generator/msvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2682,7 +2682,7 @@ def _GetMSBuildGlobalProperties(spec, guid, gyp_file_name):

platform_name = None
msvs_windows_target_platform_version = None
for configuration in spec['configurations'].itervalues():
for configuration in spec['configurations'].values():
platform_name = platform_name or _ConfigPlatform(configuration)
msvs_windows_target_platform_version = \
msvs_windows_target_platform_version or \
Expand Down Expand Up @@ -3242,7 +3242,7 @@ def _GetMSBuildProjectReferences(project):
['Project', guid],
['ReferenceOutputAssembly', 'false']
]
for config in dependency.spec.get('configurations', {}).itervalues():
for config in dependency.spec.get('configurations', {}).values():
if config.get('msvs_use_library_dependency_inputs', 0):
project_ref.append(['UseLibraryDependencyInputs', 'true'])
break
Expand Down Expand Up @@ -3311,7 +3311,7 @@ def _GenerateMSBuildProject(project, options, version, generator_flags):
extension_to_rule_name)
missing_sources = _VerifySourcesExist(sources, project_dir)

for configuration in configurations.itervalues():
for configuration in configurations.values():
_FinalizeMSBuildSettings(spec, configuration)

# Add attributes to root element
Expand Down
4 changes: 2 additions & 2 deletions gyp/pylib/gyp/generator/msvs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import gyp.generator.msvs as msvs
import unittest
import StringIO
from gyp.compat import StringIO


class TestSequenceFunctions(unittest.TestCase):

def setUp(self):
self.stderr = StringIO.StringIO()
self.stderr = StringIO()

def test_GetLibraries(self):
self.assertEqual(
Expand Down
Loading