From 179a9d3c8b3d25a4c6eaa3c83e148c4089a192e8 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 20 Dec 2015 09:17:34 +0000 Subject: [PATCH 1/3] Fix exception/error handling for Python 3 --- tools/SourceKit/bindings/python/sourcekitd/capi.py | 8 ++++---- utils/SwiftBuildSupport.py | 2 +- utils/gyb.py | 4 ++-- utils/submit-benchmark-results | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/tools/SourceKit/bindings/python/sourcekitd/capi.py b/tools/SourceKit/bindings/python/sourcekitd/capi.py index 6989c47b5361b..84e9c96094e58 100644 --- a/tools/SourceKit/bindings/python/sourcekitd/capi.py +++ b/tools/SourceKit/bindings/python/sourcekitd/capi.py @@ -156,7 +156,7 @@ def __init__(self, value): if value >= len(ErrorKind._kinds): ErrorKind._kinds += [None] * (value - len(ErrorKind._kinds) + 1) if ErrorKind._kinds[value] is not None: - raise ValueError,'ErrorKind already loaded' + raise ValueError('ErrorKind already loaded') self.value = value ErrorKind._kinds[value] = self ErrorKind._name_map = None @@ -177,7 +177,7 @@ def name(self): @staticmethod def from_id(id): if id >= len(ErrorKind._kinds) or ErrorKind._kinds[id] is None: - raise ValueError,'Unknown type kind %d' % id + raise ValueError('Unknown type kind %d' % id) return ErrorKind._kinds[id] def __repr__(self): @@ -239,7 +239,7 @@ def __init__(self, value): if value >= len(VariantType._kinds): VariantType._kinds += [None] * (value - len(VariantType._kinds) + 1) if VariantType._kinds[value] is not None: - raise ValueError,'VariantType already loaded' + raise ValueError('VariantType already loaded') self.value = value VariantType._kinds[value] = self VariantType._name_map = None @@ -260,7 +260,7 @@ def name(self): @staticmethod def from_id(id): if id >= len(VariantType._kinds) or VariantType._kinds[id] is None: - raise ValueError,'Unknown type kind %d' % id + raise ValueError('Unknown type kind %d' % id) return VariantType._kinds[id] def __repr__(self): diff --git a/utils/SwiftBuildSupport.py b/utils/SwiftBuildSupport.py index 22abaab0d4118..823a24f5354a1 100644 --- a/utils/SwiftBuildSupport.py +++ b/utils/SwiftBuildSupport.py @@ -139,7 +139,7 @@ def _get_preset_options_impl(config, substitutions, preset_name): for o in config.options(section_name): try: a = config.get(section_name, o) - except ConfigParser.InterpolationMissingOptionError, e: + except configparser.InterpolationMissingOptionError as e: # e.reference contains the correctly formatted option missing_opts.append(e.reference) continue diff --git a/utils/gyb.py b/utils/gyb.py index 3c16c9395a13a..77a527699ae7c 100755 --- a/utils/gyb.py +++ b/utils/gyb.py @@ -135,7 +135,7 @@ def tokenizePythonToUnmatchedCloseCurly(sourceText, start, lineStarts): if nesting < 0: return tokenPosToIndex(tokenStart, start, lineStarts) - except tokenize.TokenError, (message, errorPos): + except tokenize.TokenError as (message, errorPos): return tokenPosToIndex(errorPos, start, lineStarts) return len(sourceText) @@ -324,7 +324,7 @@ def splitGybLines(sourceLines): lastTokenText,lastTokenKind = tokenText,tokenKind - except tokenize.TokenError, (message, errorPos): + except tokenize.TokenError as (message, errorPos): return [] # Let the later compile() call report the error if lastTokenText == ':': diff --git a/utils/submit-benchmark-results b/utils/submit-benchmark-results index bdd08a2bacf5f..d2aa759bce79f 100755 --- a/utils/submit-benchmark-results +++ b/utils/submit-benchmark-results @@ -32,7 +32,7 @@ def capture_with_result(args, include_stderr=False): stderr = subprocess.STDOUT try: p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=stderr) - except OSError,e: + except OSError as e: if e.errno == errno.ENOENT: sys.exit('no such file or directory: %r when running %s.' % ( args[0], ' '.join(args))) From 844d81aa566828a6ce4a9fb5156afa342c5a2627 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 20 Dec 2015 09:25:06 +0000 Subject: [PATCH 2/3] Fixes for dict and list handling in Python 3 Python 3 made a number of changes to dicts and map/range/filter to be more efficient -- rather than returning a list, by default they use a memory-efficient iterator. This meant some methods were renamed, and sometimes we really do need a list. This commit (i) gets all the method names right and (ii) adds explicit casting to lists in cases where it really is needed. --- tools/SourceKit/bindings/python/sourcekitd/capi.py | 2 +- utils/GYBUnicodeDataUtils.py | 4 ++-- utils/apply-fixit-edits.py | 4 ++-- utils/cmpcodesize/cmpcodesize/main.py | 2 +- utils/pass-pipeline/scripts/pipelines_build_script.py | 2 +- utils/recursive-lipo | 8 ++++---- utils/update-checkout | 2 +- utils/viewcfg | 8 ++++---- 8 files changed, 16 insertions(+), 16 deletions(-) diff --git a/tools/SourceKit/bindings/python/sourcekitd/capi.py b/tools/SourceKit/bindings/python/sourcekitd/capi.py index 84e9c96094e58..ed0afa5464672 100644 --- a/tools/SourceKit/bindings/python/sourcekitd/capi.py +++ b/tools/SourceKit/bindings/python/sourcekitd/capi.py @@ -529,7 +529,7 @@ def register_functions(lib, ignore_errors): def register(item): return register_function(lib, item, ignore_errors) - map(register, functionList) + list(map(register, functionList)) class Config: library_path = None diff --git a/utils/GYBUnicodeDataUtils.py b/utils/GYBUnicodeDataUtils.py index 37004d78671b8..2ee642083b723 100644 --- a/utils/GYBUnicodeDataUtils.py +++ b/utils/GYBUnicodeDataUtils.py @@ -64,7 +64,7 @@ def __init__(self, grapheme_break_property_file_name): # values to symbolic values. self.symbolic_values = \ [ None ] * (max(self.numeric_value_table.values()) + 1) - for k,v in self.numeric_value_table.iteritems(): + for k,v in self.numeric_value_table.items(): self.symbolic_values[v] = k # Load the data file. @@ -329,7 +329,7 @@ def map_index(idx): else: return idx - return map(map_index, indexes) + return list(map(map_index, indexes)) # If self.BMP_data contains identical data blocks, keep the first one, # remove duplicates and change the indexes in self.BMP_lookup to point to diff --git a/utils/apply-fixit-edits.py b/utils/apply-fixit-edits.py index 05b3b3cd22b9c..1d9cc6bc8611d 100755 --- a/utils/apply-fixit-edits.py +++ b/utils/apply-fixit-edits.py @@ -49,11 +49,11 @@ def apply_edits(path): edits_per_file = {} for ed in edits_set: fname = ed[0] - if not edits_per_file.has_key(fname): + if fname not in edits_per_file: edits_per_file[fname] = [] edits_per_file[fname].append((ed[1], ed[2], ed[3])) - for fname, edits in edits_per_file.iteritems(): + for fname, edits in edits_per_file.items(): print('Updating', fname) edits.sort(reverse=True) file_data = open(fname).read() diff --git a/utils/cmpcodesize/cmpcodesize/main.py b/utils/cmpcodesize/cmpcodesize/main.py index 0ba3423cf0dee..5c67dbc7b8675 100644 --- a/utils/cmpcodesize/cmpcodesize/main.py +++ b/utils/cmpcodesize/cmpcodesize/main.py @@ -168,7 +168,7 @@ def main(): sizes = collections.defaultdict(int) for file in oldFiles: readSizes(sizes, file, True, False) - print(listFunctionSizes(sizes.items())) + print(listFunctionSizes(list(sizes.items()))) else: compareFunctionSizes(oldFiles, newFiles) else: diff --git a/utils/pass-pipeline/scripts/pipelines_build_script.py b/utils/pass-pipeline/scripts/pipelines_build_script.py index 1adee8b7e59bb..742007a42e338 100755 --- a/utils/pass-pipeline/scripts/pipelines_build_script.py +++ b/utils/pass-pipeline/scripts/pipelines_build_script.py @@ -41,7 +41,7 @@ def run_build_script_with_data_file(build_script, data_file, verbose=False): sys.stdout.write(" Failure:\n") def build_disable_slice_pipelines(**kwargs): - pipeline_range = range(len(PIPELINES)) + pipeline_range = list(range(len(PIPELINES))) def get_pipeline_args(script, iter): result = [script] diff --git a/utils/recursive-lipo b/utils/recursive-lipo index ea96bf9d5fc30..2651632199e13 100755 --- a/utils/recursive-lipo +++ b/utils/recursive-lipo @@ -22,10 +22,10 @@ def merge_file_lists(src_root_dirs, skip_files, skip_subpaths): rel_dir = os.path.relpath(src_dir, src_root_dir) rel_files = [os.path.join(rel_dir, file) for file in files+dirs if file not in skip_files] - file_list.extend(filter(lambda file: - file not in file_list, rel_files)) - dirs[:] = filter(lambda dir: - os.path.join(rel_dir, dir) not in skip_subpaths, dirs) + file_list.extend([file for file in rel_files + if file not in file_list]) + dirs[:] = [dir for dir in dirs + if os.path.join(rel_dir, dir) not in skip_subpaths] return file_list diff --git a/utils/update-checkout b/utils/update-checkout index 1008df468fe45..85bb8eb967aef 100755 --- a/utils/update-checkout +++ b/utils/update-checkout @@ -64,7 +64,7 @@ def obtain_additional_swift_sources(opts = {'with_ssh': False}): 'swift-corelibs-foundation': 'apple/swift-corelibs-foundation', 'swift-integration-tests': 'apple/swift-integration-tests', } - for dir_name, repo in additional_repos.iteritems(): + for dir_name, repo in additional_repos.items(): with WorkingDirectory(SWIFT_SOURCE_ROOT): if not os.path.isdir(os.path.join(dir_name, ".git")): print("--- Cloning '" + dir_name + "' ---") diff --git a/utils/viewcfg b/utils/viewcfg index 4256b9a39889f..2581f49e57293 100755 --- a/utils/viewcfg +++ b/utils/viewcfg @@ -119,17 +119,17 @@ def main(): # Add empty blocks which we didn't see, but which are referenced. newBlocks = { } - for name, block in blocks.iteritems(): + for name, block in blocks.items(): for adjName in (block.preds + block.getSuccs()): if not adjName in blocks: newBlocks[adjName] = Block(adjName, None) - blocks = dict(blocks.items() + newBlocks.items()) + blocks = dict(list(blocks.items()) + list(newBlocks.items())) # Add missing edges if we didn't see a successor in the terminator # but the block is mentioned in the pred list of the successor. - for name, block in blocks.iteritems(): + for name, block in blocks.items(): for predName in block.preds: predBlock = blocks[predName] if not name in predBlock.getSuccs(): @@ -141,7 +141,7 @@ def main(): outFile = open(fileName, "w") outFile.write('digraph "CFG" {\n') - for name, block in blocks.iteritems(): + for name, block in blocks.items(): if block.content is not None: outFile.write("\tNode" + str(block.index) + \ " [shape=record,label=\"{" + block.content + "}\"];\n") From eda8cefce569c442ea8e5d2e54499612721b7a51 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 20 Dec 2015 09:29:49 +0000 Subject: [PATCH 3/3] Handle changes to the Python 3 standard library --- .../Inputs/update-dependencies-bad.py | 2 +- .../bindings/python/sourcekitd/capi.py | 8 ++++++++ utils/SwiftBuildSupport.py | 6 +++++- utils/demo-tool | 7 +++++++ utils/gyb.py | 6 +++++- utils/submit-benchmark-results | 19 ++++++++++++------- utils/swift-bench.py | 2 +- 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/test/Driver/Dependencies/Inputs/update-dependencies-bad.py b/test/Driver/Dependencies/Inputs/update-dependencies-bad.py index b427b50ab8099..bdd35392138a3 100755 --- a/test/Driver/Dependencies/Inputs/update-dependencies-bad.py +++ b/test/Driver/Dependencies/Inputs/update-dependencies-bad.py @@ -17,4 +17,4 @@ exit(1) dir = os.path.dirname(os.path.abspath(__file__)) -execfile(os.path.join(dir, "update-dependencies.py")) +exec(compile(open(os.path.join(dir, "update-dependencies.py")).read(), os.path.join(dir, "update-dependencies.py"), 'exec')) diff --git a/tools/SourceKit/bindings/python/sourcekitd/capi.py b/tools/SourceKit/bindings/python/sourcekitd/capi.py index ed0afa5464672..a96de347b74bf 100644 --- a/tools/SourceKit/bindings/python/sourcekitd/capi.py +++ b/tools/SourceKit/bindings/python/sourcekitd/capi.py @@ -12,6 +12,14 @@ from ctypes import * +# Python 3 has just one integer type, which mostly behaves like the `long` +# type from Python 2. There's an `isinstance(obj, (int,long,bool))` in this +# file; this fudge is to make that work on Python 3. See PEP 237. +try: + long +except NameError: + long = int + # ctypes doesn't implicitly convert c_void_p to the appropriate wrapper # object. This is a problem, because it means that from_parameter will see an # integer and pass the wrong value on platforms where int != void*. Work around diff --git a/utils/SwiftBuildSupport.py b/utils/SwiftBuildSupport.py index 823a24f5354a1..e4eb1c8ad950c 100644 --- a/utils/SwiftBuildSupport.py +++ b/utils/SwiftBuildSupport.py @@ -12,7 +12,11 @@ from __future__ import print_function -import ConfigParser +try: + import ConfigParser +except ImportError: # Python 3 + import configparser as ConfigParser + import os import pipes import subprocess diff --git a/utils/demo-tool b/utils/demo-tool index ec7c4bf86fd9f..5d1bd1e7e520e 100755 --- a/utils/demo-tool +++ b/utils/demo-tool @@ -9,6 +9,13 @@ import time import random import sys +# In Python 3, raw_input() was renamed to input() and the original +# input() was removed. +try: + raw_input +except NameError: + raw_input = input + def send_to_screen(screen_name, arg0=None, command='stuff'): args = ['screen', '-S', screen_name, '-p', '0', '-X', command] diff --git a/utils/gyb.py b/utils/gyb.py index 77a527699ae7c..76f450d7b6eea 100755 --- a/utils/gyb.py +++ b/utils/gyb.py @@ -5,12 +5,16 @@ from __future__ import print_function import re -from cStringIO import StringIO import tokenize import textwrap from bisect import bisect import os +try: + from cStringIO import StringIO +except ImportError + from io import StringIO + def getLineStarts(s): """Return a list containing the start index of each line in s. diff --git a/utils/submit-benchmark-results b/utils/submit-benchmark-results index d2aa759bce79f..d9460ced6123f 100755 --- a/utils/submit-benchmark-results +++ b/utils/submit-benchmark-results @@ -12,8 +12,13 @@ import json import optparse import subprocess import sys -import urllib -import urllib2 + +try: + from urllib import urlencode + from urllib2 import urlopen, Request +except ImportError: # Python 3 + from urllib.parse import urlencode + from urllib.request import urlopen, Request # Test status codes. PASS = 0 @@ -52,9 +57,9 @@ def timestamp(): def submit_results_to_server(results_data, submit_url): # Submit the URL encoded data. - data = urllib.urlencode({ 'input_data' : results_data, - 'commit' : '1' }) - response = urllib2.urlopen(urllib2.Request(submit_url, data)) + data = urlencode({'input_data': results_data, + 'commit': '1'}) + response = urlopen(Request(submit_url, data)) result_data = response.read() # The result is expected to be a JSON object. @@ -113,7 +118,7 @@ def main(): utcnow = datetime.datetime.utcnow() start_time = utcnow - datetime.timedelta(seconds=data['elapsed']) end_time = utcnow - + # Create the LNT report format. lnt_results = {} lnt_results['Machine'] = { @@ -186,6 +191,6 @@ def main(): # Submit the results to an LNT server, if requested. if opts.submit_url: submit_results_to_server(lnt_result_data, opts.submit_url) - + if __name__ == '__main__': main() diff --git a/utils/swift-bench.py b/utils/swift-bench.py index 0f7612ae0bcf0..2108c5f38fbfe 100644 --- a/utils/swift-bench.py +++ b/utils/swift-bench.py @@ -270,7 +270,7 @@ def computeItersNumber(self, name): spent = int(execTime) / 1000000 # Convert ns to ms if spent <= self.minIterTime: scale *= 2 - if scale > sys.maxint: + if scale > sys.maxsize: return (0, 0) except subprocess.CalledProcessError as e: r = e.output