-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Initial Python 3 compatibility fixes #23256
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
Changes from all commits
255dc8c
e722385
aa3b4eb
4c8be84
7e02439
3dad59e
d0c24fd
b5ec4a9
b2fde82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ class `BenchmarkDoctor` analyzes performance tests, implements `check` COMMAND. | |
""" | ||
|
||
import argparse | ||
import functools | ||
import glob | ||
import logging | ||
import math | ||
|
@@ -37,6 +38,9 @@ import time | |
|
||
from compare_perf_tests import LogParser | ||
|
||
if not hasattr(sys.modules['__builtin__'], 'reduce'): | ||
reduce = functools.reduce | ||
|
||
DRIVER_DIR = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
|
@@ -178,14 +182,10 @@ class BenchmarkDriver(object): | |
|
||
Returns the aggregated result of independent benchmark invocations. | ||
""" | ||
def merge_results(a, b): | ||
a.merge(b) | ||
return a | ||
|
||
return reduce(merge_results, | ||
[self.run(test, measure_memory=True, | ||
result_sets = [self.run(test, measure_memory=True, | ||
num_iters=1, quantile=20) | ||
for _ in range(self.args.independent_samples)]) | ||
for _ in range(self.args.independent_samples)] | ||
return reduce(lambda m, res: (m.merge(res), m)[1], result_sets) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original version of this seems less magical and easier for non-python folks to grok. |
||
|
||
def log_results(self, output, log_file=None): | ||
"""Log output to `log_file`. | ||
|
@@ -303,8 +303,12 @@ class MarkdownReportHandler(logging.StreamHandler): | |
msg = self.format(record) | ||
stream = self.stream | ||
try: | ||
if (isinstance(msg, unicode) and | ||
getattr(stream, 'encoding', None)): | ||
is_text = isinstance(msg, unicode) | ||
except NameError: | ||
is_text = isinstance(msg, bytes) | ||
|
||
try: | ||
if is_text and getattr(stream, 'encoding', None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I remember correctly it was specifically a check to find out whether the attribute was present or not - keep in mind I was maintaining compatibility with both 2 and 3, not just updating to 3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I mistyped. Corrected: |
||
stream.write(msg.encode(stream.encoding)) | ||
else: | ||
stream.write(msg) | ||
|
@@ -414,6 +418,7 @@ class BenchmarkDoctor(object): | |
name = measurements['name'] | ||
setup, ratio = BenchmarkDoctor._setup_overhead(measurements) | ||
setup = 0 if ratio < 0.05 else setup | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove whitespace-only changes. |
||
runtime = min( | ||
[(result.samples.min - correction) for i_series in | ||
[BenchmarkDoctor._select(measurements, num_iters=i) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,11 @@ | |
import time | ||
import unittest | ||
|
||
from StringIO import StringIO | ||
try: | ||
from StringIO import StringIO # py2 | ||
except ModuleNotFoundError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: (This also happens in more places below) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was getting no such module errors from Python 3 because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I wanted to say is that |
||
from io import StringIO # py3 | ||
|
||
from imp import load_source | ||
|
||
from compare_perf_tests import PerformanceTestResult | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
%{ | ||
from __future__ import division | ||
try: | ||
xrange | ||
except NameError: | ||
xrange = range | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my version I simply change the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tend to agree; there was some reason I went with this approach that I've forgotten now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, please just use |
||
|
||
from SwiftIntTypes import all_integer_types | ||
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8 | ||
storagescalarCounts = [2,4,8,16,32,64] | ||
|
@@ -46,9 +52,9 @@ public struct SIMD${n}<Scalar> : SIMD where Scalar: SIMDScalar { | |
|
||
/// Creates a new vector from the given elements. | ||
@_transparent | ||
public init(${', '.join(['_ v' + str(i) + ': Scalar' for i in range(n)])}) { | ||
public init(${', '.join(['_ v' + str(i) + ': Scalar' for i in list(range(n))])}) { | ||
self.init() | ||
% for i in range(n): | ||
% for i in list(range(n)): | ||
self[${i}] = v${i} | ||
% end | ||
} | ||
|
@@ -65,7 +71,7 @@ public struct SIMD${n}<Scalar> : SIMD where Scalar: SIMDScalar { | |
self.init(${', '.join('xyzw'[:n])}) | ||
} | ||
|
||
% for i in range(n): | ||
% for i in list(range(n)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the three cases above, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, these shouldn't need any changes. |
||
/// The ${ordinalPositions[i]} element of the vector. | ||
@_transparent | ||
public var ${'xyzw'[i]}: Scalar { | ||
|
@@ -78,17 +84,17 @@ public struct SIMD${n}<Scalar> : SIMD where Scalar: SIMDScalar { | |
% if n >= 4: | ||
/// Creates a new vector from two half-length vectors. | ||
@_transparent | ||
public init(lowHalf: SIMD${n/2}<Scalar>, highHalf: SIMD${n/2}<Scalar>) { | ||
public init(lowHalf: SIMD${n//2}<Scalar>, highHalf: SIMD${n//2}<Scalar>) { | ||
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}<Scalar> { | ||
public var ${half}Half: SIMD${n//2}<Scalar> { | ||
@inlinable get { | ||
var result = SIMD${n/2}<Scalar>() | ||
var result = SIMD${n//2}<Scalar>() | ||
for i in result.indices { result[i] = self[${indx}] } | ||
return result | ||
} | ||
|
@@ -184,7 +190,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. | ||
@_fixed_layout | ||
@_alignment(${bytes if bytes <= 16 else 16}) | ||
|
@@ -229,7 +235,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. | ||
@_fixed_layout | ||
@_alignment(${bytes if bytes <= 16 else 16}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this change, either. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Python 3 definitely went bananas without this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this change is needed. In Python 3, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because if you enter |
||
|
||
/// Returns a Boolean value indicating whether the corresponding components of | ||
/// two tuples are equal. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ constants.""") | |
|
||
p = subprocess.Popen( | ||
[args.file_check_path] + unknown_args, stdin=subprocess.PIPE) | ||
stdout, stderr = p.communicate(stdin) | ||
stdout, stderr = p.communicate(stdin.encode('utf-8')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My changes in I found out that when invoked from |
||
if stdout is not None: | ||
print(stdout) | ||
if stderr is not None: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,10 @@ | |
import os | ||
import subprocess | ||
|
||
import subprocess_utils | ||
try: | ||
from . import subprocess_utils | ||
except ValueError: | ||
import subprocess_utils | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is a relative import, Python 2 and 3 will work with this change:
In #23038 I use this trick to change all this relative imports that look absolute to Python 3 everywhere. |
||
|
||
DRY_RUN = False | ||
SQUELCH_STDERR = True | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,8 @@ def setUp(self): | |
self.module_cache = os.path.join(self.tmp_dir, 'module_cache') | ||
self.sdk = subprocess.check_output(['xcrun', '--sdk', 'macosx', | ||
'--toolchain', 'Default', | ||
'--show-sdk-path']).strip("\n") | ||
'--show-sdk-path'] | ||
).decode('utf-8').strip("\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the (This happens a couple more times below). |
||
self.tools = swift_tools.SwiftTools(self.build_dir) | ||
self.passes = ['--pass=-bug-reducer-tester'] | ||
|
||
|
@@ -111,7 +112,7 @@ def test_basic(self): | |
'--extra-silopt-arg=-bug-reducer-tester-failure-kind=opt-crasher' | ||
] | ||
args.extend(self.passes) | ||
output = self.run_check_output(args).split("\n") | ||
output = self.run_check_output(args).decode('utf-8').split("\n") | ||
self.assertTrue("*** Successfully Reduced file!" in output) | ||
self.assertTrue("*** Final Functions: " + | ||
"$s9testbasic6foo413yyF" in output) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is easier to use
functools.reduce
in both Python 2 and 3.(This happens in a couple more places in other files, I will use the same solution there)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I was learning as I went along, so didn't always pick up the best solutions, especially in some of my earlier changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you adopt that throughout? It seems cleaner.