Skip to content

Commit

Permalink
'Refactored by Sourcery'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourcery AI committed Dec 4, 2023
1 parent 176d8e2 commit 9f3d71d
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 262 deletions.
23 changes: 10 additions & 13 deletions benchmarks/thirdparty/benchmark/mingw.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def repository(urls = urls, log = EmptyLogger()):
socket.close()
for entry in repo.split('\n')[:-1]:
value = entry.split('|')
version = tuple([int(n) for n in value[0].strip().split('.')])
version = tuple(int(n) for n in value[0].strip().split('.'))
version = versions.setdefault(version, {})
arch = value[1].strip()
if arch == 'x32':
Expand Down Expand Up @@ -117,7 +117,7 @@ def unpack(archive, location, log = EmptyLogger()):
'''
sevenzip = find_7zip(log)
log.info('unpacking %s', os.path.basename(archive))
cmd = [sevenzip, 'x', archive, '-o' + location, '-y']
cmd = [sevenzip, 'x', archive, f'-o{location}', '-y']
log.debug(' - %r', cmd)
with open(os.devnull, 'w') as devnull:
subprocess.check_call(cmd, stdout = devnull)
Expand All @@ -137,8 +137,7 @@ def download(url, location, log = EmptyLogger()):
content = stream.getheader('Content-Disposition') or ''
except AttributeError:
content = stream.headers.getheader('Content-Disposition') or ''
matches = re_content.match(content)
if matches:
if matches := re_content.match(content):
filename = matches.group(2)
else:
parsed = parse.urlparse(stream.geturl())
Expand All @@ -147,26 +146,24 @@ def download(url, location, log = EmptyLogger()):
try:
os.makedirs(location)
except OSError as e:
if e.errno == errno.EEXIST and os.path.isdir(location):
pass
else:
if e.errno != errno.EEXIST or not os.path.isdir(location):
raise

archive = os.path.join(location, filename)
with open(archive, 'wb') as out:
while True:
buf = stream.read(1024)
if not buf:
if buf := stream.read(1024):
out.write(buf)
else:
break
out.write(buf)
unpack(archive, location, log = log)
os.remove(archive)

possible = os.path.join(location, 'mingw64')
if not os.path.exists(possible):
possible = os.path.join(location, 'mingw32')
if not os.path.exists(possible):
raise ValueError('Failed to find unpacked MinGW: ' + possible)
raise ValueError(f'Failed to find unpacked MinGW: {possible}')
return possible

def root(location = None, arch = None, version = None, threading = None,
Expand Down Expand Up @@ -204,7 +201,7 @@ def root(location = None, arch = None, version = None, threading = None,
exceptions = 'sjlj'
else:
exceptions = keys[0]
if revision == None:
if revision is None:
revision = max(versions[version][arch][threading][exceptions].keys())
if not location:
location = os.path.join(tempfile.gettempdir(), 'mingw-builds')
Expand Down Expand Up @@ -234,7 +231,7 @@ def root(location = None, arch = None, version = None, threading = None,
elif arch == 'i686':
root_dir = os.path.join(location, slug, 'mingw32')
else:
raise ValueError('Unknown MinGW arch: ' + arch)
raise ValueError(f'Unknown MinGW arch: {arch}')

# Download if needed
if not os.path.exists(root_dir):
Expand Down
16 changes: 7 additions & 9 deletions benchmarks/thirdparty/benchmark/tools/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def main():

# NOTE: if test_baseline == test_contender, you are analyzing the stdev

description = 'Comparing %s to %s' % (test_baseline, test_contender)
description = f'Comparing {test_baseline} to {test_contender}'
elif args.mode == 'filters':
test_baseline = args.test[0].name
test_contender = args.test[0].name
Expand All @@ -162,8 +162,7 @@ def main():
# NOTE: if filter_baseline == filter_contender, you are analyzing the
# stdev

description = 'Comparing %s to %s (from %s)' % (
filter_baseline, filter_contender, args.test[0].name)
description = f'Comparing {filter_baseline} to {filter_contender} (from {args.test[0].name})'
elif args.mode == 'benchmarksfiltered':
test_baseline = args.test_baseline[0].name
test_contender = args.test_contender[0].name
Expand All @@ -173,11 +172,10 @@ def main():
# NOTE: if test_baseline == test_contender and
# filter_baseline == filter_contender, you are analyzing the stdev

description = 'Comparing %s (from %s) to %s (from %s)' % (
filter_baseline, test_baseline, filter_contender, test_contender)
description = f'Comparing {filter_baseline} (from {test_baseline}) to {filter_contender} (from {test_contender})'
else:
# should never happen
print("Unrecognized mode of operation: '%s'" % args.mode)
print(f"Unrecognized mode of operation: '{args.mode}'")
parser.print_help()
exit(1)

Expand All @@ -187,8 +185,8 @@ def main():
options_contender = []

if filter_baseline and filter_contender:
options_baseline = ['--benchmark_filter=%s' % filter_baseline]
options_contender = ['--benchmark_filter=%s' % filter_contender]
options_baseline = [f'--benchmark_filter={filter_baseline}']
options_contender = [f'--benchmark_filter={filter_contender}']

# Run the benchmarks and report the results
json1 = json1_orig = gbench.util.run_or_load_benchmark(
Expand All @@ -198,7 +196,7 @@ def main():

# Now, filter the benchmarks so that the difference report can work
if filter_baseline and filter_contender:
replacement = '[%s vs. %s]' % (filter_baseline, filter_contender)
replacement = f'[{filter_baseline} vs. {filter_contender}]'
json1 = gbench.report.filter_benchmark(
json1_orig, filter_baseline, replacement)
json2 = gbench.report.filter_benchmark(
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/thirdparty/benchmark/tools/compare_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ def main():
test2 = args.test2[0]
if unknown_args:
# should never happen
print("Unrecognized positional argument arguments: '%s'"
% unknown_args)
print(f"Unrecognized positional argument arguments: '{unknown_args}'")
exit(1)
benchmark_options = args.benchmark_options
check_inputs(test1, test2, benchmark_options)
# Run the benchmarks and report the results
json1 = gbench.util.run_or_load_benchmark(test1, benchmark_options)
json2 = gbench.util.run_or_load_benchmark(test2, benchmark_options)
output_lines = gbench.report.generate_difference_report(json1, json2)
print('Comparing %s to %s' % (test1, test2))
print(f'Comparing {test1} to {test2}')
for ln in output_lines:
print(ln)

Expand Down
7 changes: 3 additions & 4 deletions benchmarks/thirdparty/benchmark/tools/gbench/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def calculate_change(old_val, new_val):
"""
Return a float representing the decimal change between old_val and new_val.
"""
if old_val == 0 and new_val == 0:
return 0.0
if old_val == 0:
if new_val == 0:
return 0.0
return float(new_val - old_val) / (float(old_val + new_val) / 2)
return float(new_val - old_val) / abs(old_val)

Expand All @@ -73,8 +73,7 @@ def filter_benchmark(json_orig, family, replacement=""):
Apply a filter to the json, and only leave the 'family' of benchmarks.
"""
regex = re.compile(family)
filtered = {}
filtered['benchmarks'] = []
filtered = {'benchmarks': []}
for be in json_orig['benchmarks']:
if not regex.search(be['name']):
continue
Expand Down
13 changes: 6 additions & 7 deletions benchmarks/thirdparty/benchmark/tools/gbench/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ def classify_input_file(filename):
ftype = IT_Invalid
err_msg = None
if not os.path.exists(filename):
err_msg = "'%s' does not exist" % filename
err_msg = f"'{filename}' does not exist"
elif not os.path.isfile(filename):
err_msg = "'%s' does not name a file" % filename
err_msg = f"'{filename}' does not name a file"
elif is_executable_file(filename):
ftype = IT_Executable
elif is_json_file(filename):
ftype = IT_JSON
else:
err_msg = "'%s' does not name a valid benchmark executable or JSON file" % filename
err_msg = f"'{filename}' does not name a valid benchmark executable or JSON file"
return ftype, err_msg


Expand All @@ -80,7 +80,7 @@ def check_input_file(filename):
"""
ftype, msg = classify_input_file(filename)
if ftype == IT_Invalid:
print("Invalid input file: %s" % msg)
print(f"Invalid input file: {msg}")
sys.exit(1)
return ftype

Expand Down Expand Up @@ -128,11 +128,10 @@ def run_benchmark(exe_name, benchmark_flags):
is_temp_output = True
thandle, output_name = tempfile.mkstemp()
os.close(thandle)
benchmark_flags = list(benchmark_flags) + \
['--benchmark_out=%s' % output_name]
benchmark_flags = (list(benchmark_flags) + [f'--benchmark_out={output_name}'])

cmd = [exe_name] + benchmark_flags
print("RUNNING: %s" % ' '.join(cmd))
print(f"RUNNING: {' '.join(cmd)}")
exitCode = subprocess.call(cmd)
if exitCode != 0:
print('TEST FAILED...')
Expand Down
26 changes: 9 additions & 17 deletions benchmarks/thirdparty/benchmark/tools/strip_asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ def find_used_labels(asm):
found = set()
label_re = re.compile("\s*j[a-z]+\s+\.L([a-zA-Z0-9][a-zA-Z0-9_]*)")
for l in asm.splitlines():
m = label_re.match(l)
if m:
found.add('.L%s' % m.group(1))
if m := label_re.match(l):
found.add(f'.L{m.group(1)}')
return found


def normalize_labels(asm):
decls = set()
label_decl = re.compile("^[.]{0,1}L([a-zA-Z0-9][a-zA-Z0-9_]*)(?=:)")
for l in asm.splitlines():
m = label_decl.match(l)
if m:
if m := label_decl.match(l):
decls.add(m.group(0))
if len(decls) == 0:
if not decls:
return asm
needs_dot = next(iter(decls))[0] != '.'
if not needs_dot:
Expand Down Expand Up @@ -103,16 +101,10 @@ def process_asm(asm):
for l in asm.splitlines():
# Remove Mach-O attribute
l = l.replace('@GOTPCREL', '')
add_line = True
for reg in discard_regexes:
if reg.match(l) is not None:
add_line = False
break
for reg in keep_regexes:
if reg.match(l) is not None:
add_line = True
break
if add_line:
if add_line := next(
(True for reg in keep_regexes if reg.match(l) is not None),
all(reg.match(l) is None for reg in discard_regexes),
):
if fn_label_def.match(l) and len(new_contents) != 0:
new_contents += '\n'
l = process_identifiers(l)
Expand All @@ -133,7 +125,7 @@ def main():
input = args.input[0]
output = args.out[0]
if not os.path.isfile(input):
print(("ERROR: input file '%s' does not exist") % input)
print(f"ERROR: input file '{input}' does not exist")
sys.exit(1)
contents = None
with open(input, 'r') as f:
Expand Down
6 changes: 2 additions & 4 deletions doc/scripts/send_to_wandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
def strip_comments(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
return " " # note: a space and not an empty string
else:
return s
return " " if s.startswith('/') else s

pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
Expand Down
11 changes: 4 additions & 7 deletions third_party/amalgamate/amalgamate.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,9 @@ def generate(self):


def _is_within(match, matches):
for m in matches:
if match.start() > m.start() and \
match.end() < m.end():
return True
return False
return any(
match.start() > m.start() and match.end() < m.end() for m in matches
)


class TranslationUnit(object):
Expand All @@ -134,8 +132,7 @@ class TranslationUnit(object):
# Search for pattern in self.content, add the match to
# contexts if found and update the index accordingly.
def _search_content(self, index, pattern, contexts):
match = pattern.search(self.content, index)
if match:
if match := pattern.search(self.content, index):
contexts.append(match)
return match.end()
return index + 2
Expand Down
Loading

0 comments on commit 9f3d71d

Please sign in to comment.