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

Sourcery refactored develop branch #1

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
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('.'))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function repository refactored with the following changes:

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']
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function unpack refactored with the following changes:

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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function download refactored with the following changes:

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:
Comment on lines -207 to +204
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function root refactored with the following changes:

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}'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

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}'")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

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
Comment on lines -64 to +66
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function calculate_change refactored with the following changes:

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': []}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function filter_benchmark refactored with the following changes:

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"
Comment on lines -63 to +71
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function classify_input_file refactored with the following changes:

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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function check_input_file refactored with the following changes:

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)}")
Comment on lines -131 to +134
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function run_benchmark refactored with the following changes:

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):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (llm): While the walrus operator is a nice addition to Python 3.8, it may reduce readability for those unfamiliar with the syntax. Consider if compatibility with earlier Python versions is a concern.

Suggested change
if m := label_re.match(l):
m = label_re.match(l)
+ if m:

found.add(f'.L{m.group(1)}')
Comment on lines -16 to +17
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function find_used_labels refactored with the following changes:

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:
Comment on lines -26 to +27
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function normalize_labels refactored with the following changes:

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(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (llm): The use of assignment expression (walrus operator) with generator expressions and the 'all' function can be confusing. Consider refactoring for clarity.

(True for reg in keep_regexes if reg.match(l) is not None),
all(reg.match(l) is None for reg in discard_regexes),
):
Comment on lines -106 to +107
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function process_asm refactored with the following changes:

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")
Comment on lines -136 to +128
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:

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

Comment on lines -31 to +32
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function strip_comments refactored with the following changes:

This removes the following comments ( why? ):

# note: a space and not an empty string

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
)
Comment on lines -108 to +110
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _is_within refactored with the following changes:

  • Use any() instead of for loop (use-any)



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):
Comment on lines -137 to +135
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TranslationUnit._search_content refactored with the following changes:

contexts.append(match)
return match.end()
return index + 2
Expand Down
Loading
Loading