From 2001f07e523f3408b7ab0cd0ddd19e2e29543006 Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sun, 27 Jan 2019 03:37:44 -0600 Subject: [PATCH 1/4] Python 2/3 compat: gyb, gyb_sourcekit_support, gyb_syntax_support, line-directive --- utils/gyb_syntax_support/Node.py | 2 +- utils/gyb_syntax_support/__init__.py | 28 +++++++++---------- utils/line-directive | 40 +++++++++++++++------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/utils/gyb_syntax_support/Node.py b/utils/gyb_syntax_support/Node.py index cae36316e14c3..eb3899a07e378 100644 --- a/utils/gyb_syntax_support/Node.py +++ b/utils/gyb_syntax_support/Node.py @@ -1,7 +1,7 @@ from __future__ import print_function import sys # noqa: I201 -from kinds import SYNTAX_BASE_KINDS, kind_to_type, lowercase_first_word +from .kinds import SYNTAX_BASE_KINDS, kind_to_type, lowercase_first_word def error(msg): diff --git a/utils/gyb_syntax_support/__init__.py b/utils/gyb_syntax_support/__init__.py index 63ddd8203bb29..5c42e5410e83b 100644 --- a/utils/gyb_syntax_support/__init__.py +++ b/utils/gyb_syntax_support/__init__.py @@ -1,22 +1,22 @@ import textwrap -from AttributeNodes import ATTRIBUTE_NODES # noqa: I201 -from AvailabilityNodes import AVAILABILITY_NODES # noqa: I201 -import Classification # noqa: I201 -from CommonNodes import COMMON_NODES # noqa: I201 -from DeclNodes import DECL_NODES # noqa: I201 -from ExprNodes import EXPR_NODES # noqa: I201 -from GenericNodes import GENERIC_NODES # noqa: I201 - -from NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \ +from .AttributeNodes import ATTRIBUTE_NODES # noqa: I201 +from .AvailabilityNodes import AVAILABILITY_NODES # noqa: I201 +from . import Classification # noqa: I201 +from .CommonNodes import COMMON_NODES # noqa: I201 +from .DeclNodes import DECL_NODES # noqa: I201 +from .ExprNodes import EXPR_NODES # noqa: I201 +from .GenericNodes import GENERIC_NODES # noqa: I201 + +from .NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \ get_serialization_code, \ verify_syntax_node_serialization_codes -from PatternNodes import PATTERN_NODES # noqa: I201 -from StmtNodes import STMT_NODES # noqa: I201 +from .PatternNodes import PATTERN_NODES # noqa: I201 +from .StmtNodes import STMT_NODES # noqa: I201 -import Token -from Trivia import TRIVIAS # noqa: I201 -from TypeNodes import TYPE_NODES # noqa: I201 +from . import Token +from .Trivia import TRIVIAS # noqa: I201 +from .TypeNodes import TYPE_NODES # noqa: I201 # Re-export global constants diff --git a/utils/line-directive b/utils/line-directive index b12260be5da39..45f1fc6651077 100755 --- a/utils/line-directive +++ b/utils/line-directive @@ -61,7 +61,10 @@ line_pattern = re.compile( def _make_line_map(target_filename, stream=None): """ - >>> from StringIO import StringIO + >>> try: + ... from StringIO import StringIO # py2 + ... except ModuleNotFoundError: + ... from io import StringIO # py3 >>> _make_line_map('box', ... StringIO('''// ###sourceLocation(file: "foo.bar", line: 3) ... line 2 @@ -101,7 +104,7 @@ def map_line_to_source_file(target_filename, target_line_num): >>> # manually handle closing and deleting this file to allow us to open >>> # the file by its name across all platforms. >>> t = NamedTemporaryFile(delete=False) - >>> t.write('''line 1 + >>> _ = t.write(b'''line 1 ... line 2 ... // ###sourceLocation(file: "foo.bar", line: 20) ... line 4 @@ -151,7 +154,7 @@ def map_line_from_source_file(source_filename, source_line_num, >>> # manually handle closing and deleting this file to allow us to open >>> # the file by its name across all platforms. >>> t = NamedTemporaryFile(delete=False) - >>> t.write('''line 1 + >>> _ = t.write(b'''line 1 ... line 2 ... // ###sourceLocation(file: "foo.bar", line: 20) ... line 4 @@ -237,7 +240,7 @@ def run(): >>> # manually handle closing and deleting this file to allow us to open >>> # the file by its name across all platforms. >>> target1 = NamedTemporaryFile(delete=False) - >>> target1.write('''line 1 + >>> _ = target1.write(b'''line 1 ... line 2 ... // ###sourceLocation(file: "foo.bar", line: 20) ... line 4 @@ -252,7 +255,7 @@ def run(): >>> # manually handle closing and deleting this file to allow us to open >>> # the file by its name across all platforms. >>> target2 = NamedTemporaryFile(delete=False) - >>> target2.write('''// ###sourceLocation(file: "foo.bar", line: 7) + >>> _ = target2.write(b'''// ###sourceLocation(file: "foo.bar", line: 7) ... line 2 ... line 3 ... // ###sourceLocation(file: "fox.box", line: 11) @@ -269,21 +272,21 @@ def run(): >>> # the file by its name across all platforms. >>> raw_output = NamedTemporaryFile(delete=False) >>> target1_name, target2_name = target1.name, target2.name - >>> raw_output.write('''A - ... %(target1_name)s:2:111: error one + >>> _ = raw_output.write('''A + ... {target1_name}:2:111: error one ... B - ... %(target1_name)s:4:222: error two + ... {target1_name}:4:222: error two ... C - ... %(target1_name)s:8:333: error three + ... {target1_name}:8:333: error three ... D - ... glitch in file %(target2_name)s:1 assert one + ... glitch in file {target2_name}:1 assert one ... E - ... glitch in file %(target2_name)s, line 2 assert two - ... glitch at %(target2_name)s, line 3 assert three - ... glitch at %(target2_name)s:4 assert four - ... glitch in [%(target2_name)s, line 5 assert five - ... glitch in [%(target2_name)s:22 assert six - ... ''' % locals()) + ... glitch in file {target2_name}, line 2 assert two + ... glitch at {target2_name}, line 3 assert three + ... glitch at {target2_name}:4 assert four + ... glitch in [{target2_name}, line 5 assert five + ... glitch in [{target2_name}:22 assert six + ... '''.format(**locals()).encode('utf-8')) >>> raw_output.flush() Run this tool on the two targets, using a portable version of Unix 'cat' to @@ -330,7 +333,7 @@ def run(): >>> # manually handle closing and deleting this file to allow us to open >>> # the file by its name across all platforms. >>> long_output = NamedTemporaryFile(delete=False) - >>> long_output.write(''' + >>> _ = long_output.write(b''' ... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 1) ... // ###sourceLocation(file: "/public/core/Map.swift.gyb", line: 1) ... //===--- Map.swift.gyb - Lazily map over a Sequence -----------*- swif @@ -649,7 +652,8 @@ def run(): >>> long_output.flush() >>> long_output_result = subprocess.check_output([sys.executable, ... __file__, long_output.name, '--', - ... 'echo', long_output.name + ':112:27: error:']).rstrip() + ... 'echo', long_output.name + ':112:27: error:' + ... ]).rstrip().decode('ascii') >>> print(long_output_result) /public/core/Map.swift.gyb:117:27: error: >>> target1.close() From e3c60e631d0266d7374061334c9d4019e46505c4 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sat, 30 May 2020 16:57:39 -0700 Subject: [PATCH 2/4] gyb: make SyntaxSupport python3 compatible Adjust the python imports to be python2 and python3 compatible to enable gyb to be switched over to python3. --- utils/gyb_sourcekit_support/__init__.py | 6 +++--- utils/gyb_syntax_support/AttributeNodes.py | 4 ++-- utils/gyb_syntax_support/AvailabilityNodes.py | 4 ++-- utils/gyb_syntax_support/Child.py | 6 +++--- utils/gyb_syntax_support/Classification.py | 4 ++-- utils/gyb_syntax_support/CommonNodes.py | 4 ++-- utils/gyb_syntax_support/DeclNodes.py | 4 ++-- utils/gyb_syntax_support/ExprNodes.py | 4 ++-- utils/gyb_syntax_support/GenericNodes.py | 4 ++-- utils/gyb_syntax_support/NodeSerializationCodes.py | 2 +- utils/gyb_syntax_support/PatternNodes.py | 4 ++-- utils/gyb_syntax_support/StmtNodes.py | 4 ++-- utils/gyb_syntax_support/Token.py | 6 +++--- utils/gyb_syntax_support/Trivia.py | 4 ++-- utils/gyb_syntax_support/TypeNodes.py | 4 ++-- utils/gyb_syntax_support/__init__.py | 3 ++- 16 files changed, 34 insertions(+), 33 deletions(-) diff --git a/utils/gyb_sourcekit_support/__init__.py b/utils/gyb_sourcekit_support/__init__.py index 90b1a806cb02c..76630346f3793 100644 --- a/utils/gyb_sourcekit_support/__init__.py +++ b/utils/gyb_sourcekit_support/__init__.py @@ -14,9 +14,9 @@ # utils/gyb_sourcekit_support/ directory as a module. # # ---------------------------------------------------------------------------- -from UIDs import UID_KEYS -from UIDs import UID_KINDS -from UIDs import UID_REQUESTS +from .UIDs import UID_KEYS +from .UIDs import UID_KINDS +from .UIDs import UID_REQUESTS def check_uid_duplication(): diff --git a/utils/gyb_syntax_support/AttributeNodes.py b/utils/gyb_syntax_support/AttributeNodes.py index 0788c518531cf..f0d7d9cd8c9d3 100644 --- a/utils/gyb_syntax_support/AttributeNodes.py +++ b/utils/gyb_syntax_support/AttributeNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 ATTRIBUTE_NODES = [ # token-list -> token? token-list? diff --git a/utils/gyb_syntax_support/AvailabilityNodes.py b/utils/gyb_syntax_support/AvailabilityNodes.py index 9eaefa96ec997..0d9579344e6be 100644 --- a/utils/gyb_syntax_support/AvailabilityNodes.py +++ b/utils/gyb_syntax_support/AvailabilityNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 AVAILABILITY_NODES = [ # availability-spec-list -> availability-entry availability-spec-list? diff --git a/utils/gyb_syntax_support/Child.py b/utils/gyb_syntax_support/Child.py index 3cbc432be90c6..33621cc394c79 100644 --- a/utils/gyb_syntax_support/Child.py +++ b/utils/gyb_syntax_support/Child.py @@ -1,7 +1,7 @@ # flake8: noqa I201 -from Classification import classification_by_name -from Token import SYNTAX_TOKEN_MAP -from kinds import SYNTAX_BASE_KINDS, kind_to_type, lowercase_first_word +from .Classification import classification_by_name +from .Token import SYNTAX_TOKEN_MAP +from .kinds import SYNTAX_BASE_KINDS, kind_to_type, lowercase_first_word class Child(object): diff --git a/utils/gyb_syntax_support/Classification.py b/utils/gyb_syntax_support/Classification.py index f5d4df70c5349..fa11b05a35f2c 100644 --- a/utils/gyb_syntax_support/Classification.py +++ b/utils/gyb_syntax_support/Classification.py @@ -1,5 +1,5 @@ -from Node import error -from kinds import lowercase_first_word # noqa: I201 +from .Node import error +from .kinds import lowercase_first_word # noqa: I201 class SyntaxClassification(object): diff --git a/utils/gyb_syntax_support/CommonNodes.py b/utils/gyb_syntax_support/CommonNodes.py index 0827680553b7e..896ef7261b29f 100644 --- a/utils/gyb_syntax_support/CommonNodes.py +++ b/utils/gyb_syntax_support/CommonNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 COMMON_NODES = [ Node('Decl', kind='Syntax'), diff --git a/utils/gyb_syntax_support/DeclNodes.py b/utils/gyb_syntax_support/DeclNodes.py index f9b47d50b313d..8c76efc24d751 100644 --- a/utils/gyb_syntax_support/DeclNodes.py +++ b/utils/gyb_syntax_support/DeclNodes.py @@ -1,6 +1,6 @@ # flake8: noqa I201 -from Child import Child -from Node import Node +from .Child import Child +from .Node import Node DECL_NODES = [ diff --git a/utils/gyb_syntax_support/ExprNodes.py b/utils/gyb_syntax_support/ExprNodes.py index c5ebc8a4b976b..2d2295fec724c 100644 --- a/utils/gyb_syntax_support/ExprNodes.py +++ b/utils/gyb_syntax_support/ExprNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 EXPR_NODES = [ # An inout expression. diff --git a/utils/gyb_syntax_support/GenericNodes.py b/utils/gyb_syntax_support/GenericNodes.py index 57ab1c5e67c4c..c8ffa13eae8c8 100644 --- a/utils/gyb_syntax_support/GenericNodes.py +++ b/utils/gyb_syntax_support/GenericNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 GENERIC_NODES = [ # generic-where-clause -> 'where' requirement-list diff --git a/utils/gyb_syntax_support/NodeSerializationCodes.py b/utils/gyb_syntax_support/NodeSerializationCodes.py index eeea90df395eb..7d1589e98cc89 100644 --- a/utils/gyb_syntax_support/NodeSerializationCodes.py +++ b/utils/gyb_syntax_support/NodeSerializationCodes.py @@ -1,4 +1,4 @@ -from Node import error +from .Node import error SYNTAX_NODE_SERIALIZATION_CODES = { diff --git a/utils/gyb_syntax_support/PatternNodes.py b/utils/gyb_syntax_support/PatternNodes.py index 20e5cc6662855..454c950ec6e8c 100644 --- a/utils/gyb_syntax_support/PatternNodes.py +++ b/utils/gyb_syntax_support/PatternNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 PATTERN_NODES = [ diff --git a/utils/gyb_syntax_support/StmtNodes.py b/utils/gyb_syntax_support/StmtNodes.py index 49edc89298a94..f662f3975a5bf 100644 --- a/utils/gyb_syntax_support/StmtNodes.py +++ b/utils/gyb_syntax_support/StmtNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 STMT_NODES = [ # continue-stmt -> 'continue' label? ';'? diff --git a/utils/gyb_syntax_support/Token.py b/utils/gyb_syntax_support/Token.py index aaacc4fcfeb33..0aa1e685f75aa 100644 --- a/utils/gyb_syntax_support/Token.py +++ b/utils/gyb_syntax_support/Token.py @@ -1,6 +1,6 @@ -from Classification import classification_by_name -from Node import error # noqa: I201 -from kinds import lowercase_first_word # noqa: I201 +from .Classification import classification_by_name +from .Node import error # noqa: I201 +from .kinds import lowercase_first_word # noqa: I201 class Token(object): diff --git a/utils/gyb_syntax_support/Trivia.py b/utils/gyb_syntax_support/Trivia.py index dc30c24d528f6..b9e606ad64d60 100644 --- a/utils/gyb_syntax_support/Trivia.py +++ b/utils/gyb_syntax_support/Trivia.py @@ -1,5 +1,5 @@ -from Node import error -from kinds import lowercase_first_word # noqa: I201 +from .Node import error +from .kinds import lowercase_first_word # noqa: I201 class Trivia(object): diff --git a/utils/gyb_syntax_support/TypeNodes.py b/utils/gyb_syntax_support/TypeNodes.py index a9d8dfeabbb14..11a3e2c7e6d47 100644 --- a/utils/gyb_syntax_support/TypeNodes.py +++ b/utils/gyb_syntax_support/TypeNodes.py @@ -1,5 +1,5 @@ -from Child import Child -from Node import Node # noqa: I201 +from .Child import Child +from .Node import Node # noqa: I201 TYPE_NODES = [ # simple-type-identifier -> identifier generic-argument-clause? diff --git a/utils/gyb_syntax_support/__init__.py b/utils/gyb_syntax_support/__init__.py index 5c42e5410e83b..e3960dc95c813 100644 --- a/utils/gyb_syntax_support/__init__.py +++ b/utils/gyb_syntax_support/__init__.py @@ -7,6 +7,8 @@ from .ExprNodes import EXPR_NODES # noqa: I201 from .GenericNodes import GENERIC_NODES # noqa: I201 +from . import Token + from .NodeSerializationCodes import SYNTAX_NODE_SERIALIZATION_CODES, \ get_serialization_code, \ verify_syntax_node_serialization_codes @@ -14,7 +16,6 @@ from .PatternNodes import PATTERN_NODES # noqa: I201 from .StmtNodes import STMT_NODES # noqa: I201 -from . import Token from .Trivia import TRIVIAS # noqa: I201 from .TypeNodes import TYPE_NODES # noqa: I201 From 2127cbf5e8391795b78a523a8a22e1fc61b2d50e Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sun, 27 Jan 2019 03:38:25 -0600 Subject: [PATCH 3/4] Python 2/3 compat: Swift stdlib --- stdlib/public/core/IntegerTypes.swift.gyb | 4 ++-- stdlib/public/core/SIMDVectorTypes.swift.gyb | 15 ++++++++------- stdlib/public/core/Tuple.swift.gyb | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/stdlib/public/core/IntegerTypes.swift.gyb b/stdlib/public/core/IntegerTypes.swift.gyb index 4cdb3348e9a3d..34c1ae509c660 100644 --- a/stdlib/public/core/IntegerTypes.swift.gyb +++ b/stdlib/public/core/IntegerTypes.swift.gyb @@ -14,10 +14,10 @@ # Utility code for later in this template # +from __future__ import division from SwiftIntTypes import all_integer_types, int_max_bits, should_define_truncating_bit_pattern_init from SwiftFloatingPointTypes import getFtoIBounds -from string import maketrans, capitalize from itertools import chain # Number of bits in the Builtin.Word type @@ -1658,7 +1658,7 @@ extension ${Self}: Hashable { return Hasher._hash( seed: seed, bytes: UInt64(truncatingIfNeeded: ${U}${Self}(_value)), - count: ${bits / 8}) + count: ${bits // 8}) % end } } diff --git a/stdlib/public/core/SIMDVectorTypes.swift.gyb b/stdlib/public/core/SIMDVectorTypes.swift.gyb index 925253a92854b..ba887ed6f81be 100644 --- a/stdlib/public/core/SIMDVectorTypes.swift.gyb +++ b/stdlib/public/core/SIMDVectorTypes.swift.gyb @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// %{ +from __future__ import division from SwiftIntTypes import all_integer_types word_bits = int(CMAKE_SIZEOF_VOID_P) * 8 storagescalarCounts = [2,4,8,16,32,64] @@ -90,17 +91,17 @@ public struct SIMD${n}: SIMD where Scalar: SIMDScalar { % if n >= 4: /// Creates a new vector from two half-length vectors. @_transparent - public init(lowHalf: SIMD${n/2}, highHalf: SIMD${n/2}) { + public init(lowHalf: SIMD${n//2}, highHalf: SIMD${n//2}) { self.init() self.lowHalf = lowHalf self.highHalf = highHalf } -% for (half,indx) in [('low','i'), ('high',str(n/2)+'+i'), ('even','2*i'), ('odd','2*i+1')]: +% for (half,indx) in [('low','i'), ('high',str(n//2)+'+i'), ('even','2*i'), ('odd','2*i+1')]: /// A half-length vector made up of the ${half} elements of the vector. - public var ${half}Half: SIMD${n/2} { + public var ${half}Half: SIMD${n//2} { @inlinable get { - var result = SIMD${n/2}() + var result = SIMD${n//2}() for i in result.indices { result[i] = self[${indx}] } return result } @@ -159,7 +160,7 @@ extension SIMD${n}: CustomDebugStringConvertible { public var debugDescription: String { return "SIMD${n}<\(Scalar.self)>(${', '.join(map(lambda c: '\\(self['+ str(c) + '])', - xrange(n)))})" + range(n)))})" } } @@ -212,7 +213,7 @@ extension ${Self}: SIMDScalar { public typealias SIMDMaskScalar = ${Mask} % for n in storagescalarCounts: -% bytes = n * self_type.bits / 8 +% bytes = n * self_type.bits // 8 /// Storage for a vector of ${spelledNumbers[n]} integers. @frozen @_alignment(${bytes if bytes <= 16 else 16}) @@ -260,7 +261,7 @@ extension ${Self} : SIMDScalar { public typealias SIMDMaskScalar = Int${bits} % for n in storagescalarCounts: -% bytes = n * bits / 8 +% bytes = n * bits // 8 /// Storage for a vector of ${spelledNumbers[n]} floating-point values. @frozen @_alignment(${bytes if bytes <= 16 else 16}) diff --git a/stdlib/public/core/Tuple.swift.gyb b/stdlib/public/core/Tuple.swift.gyb index 29548c92d0e17..63d7ee25f0c96 100644 --- a/stdlib/public/core/Tuple.swift.gyb +++ b/stdlib/public/core/Tuple.swift.gyb @@ -111,7 +111,7 @@ public func >=(lhs: (), rhs: ()) -> Bool { % equatableTypeParams = ", ".join(["{}: Equatable".format(c) for c in typeParams]) % originalTuple = "(\"a\", {})".format(", ".join(map(str, range(1, arity)))) -% greaterTuple = "(\"a\", {})".format(", ".join(map(str, range(1, arity - 1) + [arity]))) +% greaterTuple = "(\"a\", {})".format(", ".join(map(str, list(range(1, arity - 1)) + [arity]))) /// Returns a Boolean value indicating whether the corresponding components of /// two tuples are equal. From 23f2d34272612d43af6c2d22f302eeb062a9020e Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 31 May 2020 12:12:30 -0700 Subject: [PATCH 4/4] stdlib: make the FloatingPoint GYB Python3 friendly --- stdlib/public/core/FloatingPointTypes.swift.gyb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/FloatingPointTypes.swift.gyb b/stdlib/public/core/FloatingPointTypes.swift.gyb index 844286bfdeddc..393f7924aca79 100644 --- a/stdlib/public/core/FloatingPointTypes.swift.gyb +++ b/stdlib/public/core/FloatingPointTypes.swift.gyb @@ -13,6 +13,7 @@ import SwiftShims %{ +from __future__ import division from SwiftIntTypes import all_integer_types from SwiftFloatingPointTypes import all_floating_point_types @@ -981,7 +982,7 @@ extension ${Self}: Hashable { %elif bits == 64: return Hasher._hash(seed: seed, v.bitPattern) %elif bits < 64: - return Hasher._hash(seed: seed, bytes: UInt64(v.bitPattern), count: ${bits/8}) + return Hasher._hash(seed: seed, bytes: UInt64(v.bitPattern), count: ${bits//8}) %else: #error("Unimplemented") %end