Skip to content

Commit 56f845b

Browse files
committed
Scripts: Rename l variables and other clean-up
Rename `l` variables, rewrite two `lambda`-assigned helpers as `def`, and clean up two `not X in Y` checks to `X not in Y` to satisfy ruff E713, E731, E741 rules. Signed-off-by: Matthias J. Kannwischer <matthias@zerorisc.com>
1 parent 7f21cc3 commit 56f845b

5 files changed

Lines changed: 112 additions & 106 deletions

File tree

ruff.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,4 @@ extend-include = [
1515
select = ["E", "F", "W"]
1616
ignore = [
1717
"E501", # line too long
18-
"E713", # `not in` rather than `not X in Y`
19-
"E731", # lambda assignment
20-
"E741", # ambiguous variable name
2118
]

scripts/autogen

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -556,23 +556,23 @@ def adjust_preprocessor_comments_for_filename(
556556
# - `force_print` indicates if a comment should be omitted
557557
if_stack = []
558558

559-
def merge_escaped_lines(l, i):
560-
while l.endswith("\\"):
561-
l = l.removesuffix("\\").rstrip() + content[i + 1].lstrip()
559+
def merge_escaped_lines(line, i):
560+
while line.endswith("\\"):
561+
line = line.removesuffix("\\").rstrip() + content[i + 1].lstrip()
562562
i = i + 1
563-
return (l, i)
563+
return (line, i)
564564

565-
def merge_commented_lines(l, i):
565+
def merge_commented_lines(line, i):
566566
# Not very robust, but good enough
567-
if not "/*" in l or "*/" in l:
568-
return (l, i)
567+
if "/*" not in line or "*/" in line:
568+
return (line, i)
569569
i += 1
570570
while "*/" not in content[i]:
571-
l += content[i]
571+
line += content[i]
572572
i += 1
573573

574-
l += content[i]
575-
return (l, i)
574+
line += content[i]
575+
return (line, i)
576576

577577
def should_print(cur_line_no, conds, line_no, force_print):
578578
line_threshold = 5
@@ -631,26 +631,26 @@ def adjust_preprocessor_comments_for_filename(
631631

632632
i = 0
633633
while i < len(content):
634-
l = content[i].strip()
634+
line = content[i].strip()
635635
# Replace #ifdef by #if defined(...)
636-
if l.startswith("#ifdef "):
637-
l = "#if defined(" + l.removeprefix("#ifdef").strip() + ")"
638-
if l.startswith("#ifndef "):
639-
l = "#if !defined(" + l.removeprefix("#ifndef").strip() + ")"
640-
if l.startswith("#if"):
641-
l, _ = merge_escaped_lines(l, i)
642-
cond = l.removeprefix("#if")
636+
if line.startswith("#ifdef "):
637+
line = "#if defined(" + line.removeprefix("#ifdef").strip() + ")"
638+
if line.startswith("#ifndef "):
639+
line = "#if !defined(" + line.removeprefix("#ifndef").strip() + ")"
640+
if line.startswith("#if"):
641+
line, _ = merge_escaped_lines(line, i)
642+
cond = line.removeprefix("#if")
643643
if_stack.append(([cond], i, True, False))
644644
new_content.append(content[i])
645-
elif l.startswith("#elif"):
645+
elif line.startswith("#elif"):
646646
conds, _, _, force_print = if_stack.pop()
647-
l, _ = merge_escaped_lines(l, i)
648-
conds.append(l.removeprefix("#elif"))
647+
line, _ = merge_escaped_lines(line, i)
648+
conds.append(line.removeprefix("#elif"))
649649
if_stack.append((conds, i, True, force_print))
650650
new_content.append(content[i])
651-
elif l.startswith("#else"):
652-
l, i = merge_escaped_lines(l, i)
653-
_, i = merge_commented_lines(l, i)
651+
elif line.startswith("#else"):
652+
line, i = merge_escaped_lines(line, i)
653+
_, i = merge_commented_lines(line, i)
654654
conds, j, branch, force_print = if_stack.pop()
655655
assert branch is True
656656
print_else = should_print(i, cond, j, force_print)
@@ -660,9 +660,9 @@ def adjust_preprocessor_comments_for_filename(
660660
new_content.append(adhoc_format("#else", cond))
661661
else:
662662
new_content.append("#else")
663-
elif l.startswith("#endif"):
664-
l, i = merge_escaped_lines(l, i)
665-
_, i = merge_commented_lines(l, i)
663+
elif line.startswith("#endif"):
664+
line, i = merge_escaped_lines(line, i)
665+
_, i = merge_commented_lines(line, i)
666666
conds, j, branch, force_print = if_stack.pop()
667667
print_endif = should_print(i, conds, j, force_print)
668668
if print_endif is False:
@@ -675,7 +675,7 @@ def adjust_preprocessor_comments_for_filename(
675675
# handle `#if ...` inside documentation as this would
676676
# lead to nested `/* ... */`.
677677
i_old = i
678-
_, i = merge_commented_lines(l, i_old)
678+
_, i = merge_commented_lines(line, i_old)
679679
new_content += content[i_old : i + 1]
680680
i += 1
681681

@@ -944,12 +944,12 @@ def print_hol_light_array(g, as_int=True, entries_per_line=8, pad=0):
944944
c = "&" if as_int is True else ""
945945
return f"{prefix}{c}{n:>{pad}};"
946946

947-
l = list(map(format_hol_light_int, g))
947+
items = list(map(format_hol_light_int, g))
948948
# Remove `;` from end of last entry
949-
l[-1] = l[-1][:-1]
949+
items[-1] = items[-1][:-1]
950950

951-
for i in range(0, len(l), entries_per_line):
952-
yield " " + " ".join(l[i : i + entries_per_line])
951+
for i in range(0, len(items), entries_per_line):
952+
yield " " + " ".join(items[i : i + entries_per_line])
953953

954954

955955
def gen_aarch64_hol_light_zeta_file():
@@ -1200,8 +1200,8 @@ def gen_avx2_fwd_ntt_zetas():
12001200
root_pairs = list(
12011201
map(lambda x: gen_twiddles(layer, block_base + x, repeat), block_offsets)
12021202
)
1203-
yield from (r for l in root_pairs for r in l[1])
1204-
yield from (r for l in root_pairs for r in l[0])
1203+
yield from (r for pair in root_pairs for r in pair[1])
1204+
yield from (r for pair in root_pairs for r in pair[0])
12051205

12061206
# Layers 1 twiddle
12071207
yield from gen_twiddles_many(0, 0, range(1), 4)
@@ -1710,11 +1710,11 @@ def print_hol_light_word64_list(g, entries_per_line=4):
17101710
Analogous to print_hol_light_array but for int64 word lists.
17111711
Yields lines with leading indent, semicolon-separated.
17121712
"""
1713-
l = [f"word 0x{v:016X}" for v in g]
1713+
items = [f"word 0x{v:016X}" for v in g]
17141714

1715-
for i in range(0, len(l), entries_per_line):
1716-
row = l[i : i + entries_per_line]
1717-
is_last = i + entries_per_line >= len(l)
1715+
for i in range(0, len(items), entries_per_line):
1716+
row = items[i : i + entries_per_line]
1717+
is_last = i + entries_per_line >= len(items)
17181718
line = "; ".join(row)
17191719
if not is_last:
17201720
line += ";"
@@ -2105,8 +2105,8 @@ def get_all_files():
21052105

21062106

21072107
def get_defines_from_file(c):
2108-
for l in read_file(c).split("\n"):
2109-
m = _RE_DEFINE.match(l)
2108+
for line in read_file(c).split("\n"):
2109+
m = _RE_DEFINE.match(line)
21102110
if m:
21112111
yield (c, m.group(1))
21122112

@@ -2143,7 +2143,9 @@ def get_checked_defines():
21432143

21442144
def gen_monolithic_undef_all_core(filt=None, desc=""):
21452145
if filt is None:
2146-
filt = lambda c: True
2146+
2147+
def filt(c):
2148+
return True
21472149

21482150
if desc != "":
21492151
yield "/*"
@@ -2655,22 +2657,22 @@ def check_macro_typos():
26552657
def check_asm_register_aliases_for_file(filename):
26562658
"""Checks that `filename` has no mismatching or dangling register aliases"""
26572659

2658-
def get_alias_def(l):
2659-
s = list(filter(lambda s: s != "", l.strip().split(" ")))
2660+
def get_alias_def(line):
2661+
s = list(filter(lambda s: s != "", line.strip().split(" ")))
26602662
if len(s) < 3 or s[1] != ".req":
26612663
return None
26622664
return s[0]
26632665

2664-
def get_alias_undef(l):
2665-
if l.strip().startswith(".unreq") is False:
2666+
def get_alias_undef(line):
2667+
if line.strip().startswith(".unreq") is False:
26662668
return None
2667-
return list(filter(lambda s: s != "", l.strip().split(" ")))[1]
2669+
return list(filter(lambda s: s != "", line.strip().split(" ")))[1]
26682670

26692671
content = read_file(filename)
26702672
aliases = {}
2671-
for i, l in enumerate(content.split("\n")):
2672-
alias_def = get_alias_def(l)
2673-
alias_undef = get_alias_undef(l)
2673+
for i, line in enumerate(content.split("\n")):
2674+
alias_def = get_alias_def(line)
2675+
alias_undef = get_alias_undef(line)
26742676
if alias_def is not None:
26752677
if alias_def in aliases.keys():
26762678
raise Exception(
@@ -3225,7 +3227,9 @@ def synchronize_file(f, in_dir, out_dir, delete=False, no_simplify=False, **kwar
32253227
# don't do it here, the dry-run would fail because of a
32263228
# mismatching intermediate file
32273229
if f.endswith(".h"):
3228-
transform = lambda c: adjust_header_guard_for_filename(c, outfile_full)
3230+
3231+
def transform(c):
3232+
return adjust_header_guard_for_filename(c, outfile_full)
32293233
else:
32303234
transform = None
32313235
update_via_copy(f, outfile_full, transform=transform)
@@ -3410,8 +3414,8 @@ def adjust_header_guard_for_filename(content, header_file):
34103414

34113415
# Skip over initial commentary
34123416
insert_at = None
3413-
for i, l in enumerate(content):
3414-
if l.strip() == "" or l.startswith(("/*", " *")):
3417+
for i, line in enumerate(content):
3418+
if line.strip() == "" or line.startswith(("/*", " *")):
34153419
continue
34163420
insert_at = i
34173421
break
@@ -3623,8 +3627,8 @@ def gen_markdown_citations_for(filename, bibliography):
36233627

36243628
# Lookup all citations in style `[^ID]`
36253629
citations = {}
3626-
for i, l in enumerate(content):
3627-
for m in _RE_MARKDOWN_CITE.finditer(l):
3630+
for i, line in enumerate(content):
3631+
for m in _RE_MARKDOWN_CITE.finditer(line):
36283632
cite_id = m.group("id")
36293633
uses = citations.get(cite_id, [])
36303634
uses.append((filename, i))
@@ -3641,8 +3645,8 @@ def gen_markdown_citations_for(filename, bibliography):
36413645
# Find explicit footnote definitions outside the bibliography block
36423646
# These are lines matching `[^ID]: ...` and should be excluded from autogeneration
36433647
explicit_footnotes = set()
3644-
for l in content:
3645-
m = re.match(r"^\[\^(\w+)\]:", l)
3648+
for line in content:
3649+
m = re.match(r"^\[\^(\w+)\]:", line)
36463650
if m:
36473651
explicit_footnotes.add(m.group(1))
36483652

@@ -3691,8 +3695,8 @@ def gen_c_citations_for(filename, bibliography):
36913695

36923696
# Lookup all citations in style `@[ID]`
36933697
citations = {}
3694-
for i, l in enumerate(content):
3695-
for m in _RE_C_CITE.finditer(l):
3698+
for i, line in enumerate(content):
3699+
for m in _RE_C_CITE.finditer(line):
36963700
cite_id = m.group("id")
36973701
uses = citations.get(cite_id, [])
36983702
# Remember usage. +1 because line counting starts at 1
@@ -3742,8 +3746,8 @@ def gen_c_citations_for(filename, bibliography):
37423746
# Add references to file after initial header section
37433747
# Skip over copyright
37443748
insert_at = None
3745-
for i, l in enumerate(content):
3746-
if l.startswith(("/*", " *")):
3749+
for i, line in enumerate(content):
3750+
if line.startswith(("/*", " *")):
37473751
continue
37483752
insert_at = i
37493753
break

scripts/simpasm

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,46 @@ def patchup_disasm(asm, cfify=False):
2929
return lbl + ":"
3030

3131
# Find first label
32-
for i, l in enumerate(asm):
33-
if decode_label(l) is not None:
32+
for i, line in enumerate(asm):
33+
if decode_label(line) is not None:
3434
break
3535

3636
asm = asm[i + 1 :]
3737

3838
def gen(asm):
39-
for l in asm:
40-
if l.strip() == "":
39+
for line in asm:
40+
if line.strip() == "":
4141
yield ""
4242
continue
43-
lbl = decode_label(l)
43+
lbl = decode_label(line)
4444
# Re-format labels as assembly labels
4545
if lbl is not None:
4646
yield make_label(lbl)
4747
continue
4848
# Drop comments
49-
l = l.split(";")[0]
49+
line = line.split(";")[0]
5050
# Re-format references to labels
5151
# Those are assumed to have the form `0xDEADBEEF <label>`
5252
if cfify:
53-
l = re.sub(
54-
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>", r"L\g<label>", l
53+
line = re.sub(
54+
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>",
55+
r"L\g<label>",
56+
line,
5557
)
5658
else:
57-
l = re.sub(
58-
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>", r"\g<label>", l
59+
line = re.sub(
60+
r"(0x)?[0-9a-fA-F]+\s+<(?P<label>[a-zA-Z0-9_]+)>",
61+
r"\g<label>",
62+
line,
5963
)
6064
# Drop address and byte code from line
6165
d = re.search(
62-
r"^\s*[0-9a-fA-F]+:\s+([0-9a-fA-F][0-9a-fA-F][ ]*)+\s+(?P<inst>.*)$", l
66+
r"^\s*[0-9a-fA-F]+:\s+([0-9a-fA-F][0-9a-fA-F][ ]*)+\s+(?P<inst>.*)$",
67+
line,
6368
)
6469
if d is None:
6570
raise Exception(
66-
f'The following does not seem to be an assembly line of the expected format `ADDRESS: BYTECODE INSTRUCTION`:\n"{l}"'
71+
f'The following does not seem to be an assembly line of the expected format `ADDRESS: BYTECODE INSTRUCTION`:\n"{line}"'
6772
)
6873
yield " " * indentation + d.group("inst").expandtabs(1)
6974

@@ -76,8 +81,8 @@ def find_header_footer(asm, filename):
7681

7782
# Extract header
7883
header_end = None
79-
for i, l in enumerate(asm):
80-
if header_end_marker in l:
84+
for i, line in enumerate(asm):
85+
if header_end_marker in line:
8186
header_end = i
8287
break
8388
if header_end is None:
@@ -88,8 +93,8 @@ def find_header_footer(asm, filename):
8893

8994
# Extract footer
9095
footer_start = None
91-
for i, l in enumerate(asm):
92-
if footer_start_marker in l:
96+
for i, line in enumerate(asm):
97+
if footer_start_marker in line:
9398
footer_start = i
9499
break
95100
if footer_start is None:
@@ -105,8 +110,8 @@ def find_header_footer(asm, filename):
105110

106111
def find_globals(asm):
107112
global_symbols = []
108-
for l in asm:
109-
r = re.search(r"^\s*\.global\s+(.*)$", l)
113+
for line in asm:
114+
r = re.search(r"^\s*\.global\s+(.*)$", line)
110115
if r is None:
111116
continue
112117
global_symbols.append(r.group(1))
@@ -121,9 +126,9 @@ def drop_if_from_header(header):
121126
header_new = []
122127
i = 0
123128
while i < len(header):
124-
l = header[i]
125-
if not l.strip().startswith("#if"):
126-
header_new.append(l)
129+
line = header[i]
130+
if not line.strip().startswith("#if"):
131+
header_new.append(line)
127132
i += 1
128133
continue
129134
header_new.append("#if 1")
@@ -138,9 +143,9 @@ def drop_preprocessor_directives(header):
138143
header_new = []
139144
i = 0
140145
while i < len(header):
141-
l = header[i]
142-
if not l.strip().startswith("#"):
143-
header_new.append(l)
146+
line = header[i]
147+
if not line.strip().startswith("#"):
148+
header_new.append(line)
144149
i += 1
145150
continue
146151
while i < len(header) and header[i].endswith("\\"):

0 commit comments

Comments
 (0)