Skip to content

Commit 509207c

Browse files
cclaussdpgeorge
authored andcommitted
tools/cc1: Apply ruff formatting to cc1 script.
Running `ruff format tools/cc1` picks up `tools/cc1` which is a Python file that does not have a .py file extension. Signed-off-by: Christian Clauss <cclauss@me.com> Signed-off-by: Damien George <damien@micropython.org>
1 parent 1e5da26 commit 509207c

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

tools/cc1

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import re
2323
# TODO somehow make them externally configurable
2424

2525
# this is the path to the true C compiler
26-
cc1_path = '/usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/cc1'
27-
#cc1_path = '/usr/lib/gcc/arm-none-eabi/5.3.0/cc1'
26+
cc1_path = "/usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/cc1"
27+
# cc1_path = '/usr/lib/gcc/arm-none-eabi/5.3.0/cc1'
2828

2929
# this must be the same as MICROPY_QSTR_BYTES_IN_HASH
3030
bytes_in_qstr_hash = 2
@@ -41,11 +41,16 @@ print_debug = False
4141
################################################################################
4242

4343
# precompile regexs
44-
re_preproc_line = re.compile(r'# [0-9]+ ')
45-
re_map_entry = re.compile(r'\{.+?\(MP_QSTR_([A-Za-z0-9_]+)\).+\},')
46-
re_mp_obj_dict_t = re.compile(r'(?P<head>(static )?const mp_obj_dict_t (?P<id>[a-z0-9_]+) = \{ \.base = \{&mp_type_dict\}, \.map = \{ \.all_keys_are_qstrs = 1, \.is_fixed = 1, \.is_ordered = )1(?P<tail>, \.used = .+ };)$')
47-
re_mp_map_t = re.compile(r'(?P<head>(static )?const mp_map_t (?P<id>[a-z0-9_]+) = \{ \.all_keys_are_qstrs = 1, \.is_fixed = 1, \.is_ordered = )1(?P<tail>, \.used = .+ };)$')
48-
re_mp_rom_map_elem_t = re.compile(r'static const mp_rom_map_elem_t [a-z_0-9]+\[\] = {$')
44+
re_preproc_line = re.compile(r"# [0-9]+ ")
45+
re_map_entry = re.compile(r"\{.+?\(MP_QSTR_([A-Za-z0-9_]+)\).+\},")
46+
re_mp_obj_dict_t = re.compile(
47+
r"(?P<head>(static )?const mp_obj_dict_t (?P<id>[a-z0-9_]+) = \{ \.base = \{&mp_type_dict\}, \.map = \{ \.all_keys_are_qstrs = 1, \.is_fixed = 1, \.is_ordered = )1(?P<tail>, \.used = .+ };)$"
48+
)
49+
re_mp_map_t = re.compile(
50+
r"(?P<head>(static )?const mp_map_t (?P<id>[a-z0-9_]+) = \{ \.all_keys_are_qstrs = 1, \.is_fixed = 1, \.is_ordered = )1(?P<tail>, \.used = .+ };)$"
51+
)
52+
re_mp_rom_map_elem_t = re.compile(r"static const mp_rom_map_elem_t [a-z_0-9]+\[\] = {$")
53+
4954

5055
# this must match the equivalent function in qstr.c
5156
def compute_hash(qstr):
@@ -55,18 +60,19 @@ def compute_hash(qstr):
5560
# Make sure that valid hash is never zero, zero means "hash not computed"
5661
return (hash & ((1 << (8 * bytes_in_qstr_hash)) - 1)) or 1
5762

63+
5864
# this algo must match the equivalent in map.c
5965
def hash_insert(map, key, value):
6066
hash = compute_hash(key)
6167
pos = hash % len(map)
6268
start_pos = pos
6369
if print_debug:
64-
print(' insert %s: start at %u/%u -- ' % (key, pos, len(map)), end='')
70+
print(" insert %s: start at %u/%u -- " % (key, pos, len(map)), end="")
6571
while True:
6672
if map[pos] is None:
6773
# found empty slot, so key is not in table
6874
if print_debug:
69-
print('put at %u' % pos)
75+
print("put at %u" % pos)
7076
map[pos] = (key, value)
7177
return
7278
else:
@@ -76,6 +82,7 @@ def hash_insert(map, key, value):
7682
pos = (pos + 1) % len(map)
7783
assert pos != start_pos
7884

85+
7986
def hash_find(map, key):
8087
hash = compute_hash(key)
8188
pos = hash % len(map)
@@ -92,6 +99,7 @@ def hash_find(map, key):
9299
if pos == start_pos:
93100
return attempts, None
94101

102+
95103
def process_map_table(file, line, output):
96104
output.append(line)
97105

@@ -101,7 +109,7 @@ def process_map_table(file, line, output):
101109
while True:
102110
line = file.readline()
103111
if len(line) == 0:
104-
print('unexpected end of input')
112+
print("unexpected end of input")
105113
sys.exit(1)
106114
line = line.strip()
107115
if len(line) == 0:
@@ -110,38 +118,38 @@ def process_map_table(file, line, output):
110118
if re_preproc_line.match(line):
111119
# preprocessor line number comment
112120
continue
113-
if line == '};':
121+
if line == "};":
114122
# end of table (we assume it appears on a single line)
115123
break
116124
table_contents.append(line)
117125

118126
# make combined string of entries
119-
entries_str = ''.join(table_contents)
127+
entries_str = "".join(table_contents)
120128

121129
# split into individual entries
122130
entries = []
123131
while entries_str:
124132
# look for single entry, by matching nested braces
125133
match = None
126-
if entries_str[0] == '{':
134+
if entries_str[0] == "{":
127135
nested_braces = 0
128136
for i in range(len(entries_str)):
129-
if entries_str[i] == '{':
137+
if entries_str[i] == "{":
130138
nested_braces += 1
131-
elif entries_str[i] == '}':
139+
elif entries_str[i] == "}":
132140
nested_braces -= 1
133141
if nested_braces == 0:
134-
match = re_map_entry.match(entries_str[:i + 2])
142+
match = re_map_entry.match(entries_str[: i + 2])
135143
break
136144

137145
if not match:
138-
print('unknown line in table:', entries_str)
146+
print("unknown line in table:", entries_str)
139147
sys.exit(1)
140148

141149
# extract single entry
142150
line = match.group(0)
143151
qstr = match.group(1)
144-
entries_str = entries_str[len(line):].lstrip()
152+
entries_str = entries_str[len(line) :].lstrip()
145153

146154
# add the qstr and the whole line to list of all entries
147155
entries.append((qstr, line))
@@ -164,28 +172,28 @@ def process_map_table(file, line, output):
164172
attempts, line = hash_find(map, qstr)
165173
assert line is not None
166174
if print_debug:
167-
print(' %s lookup took %u attempts' % (qstr, attempts))
175+
print(" %s lookup took %u attempts" % (qstr, attempts))
168176
total_attempts += attempts
169177
if len(entries):
170178
stats = len(map), len(entries) / len(map), total_attempts / len(entries)
171179
else:
172180
stats = 0, 0, 0
173181
if print_debug:
174-
print(' table stats: size=%d, load=%.2f, avg_lookups=%.1f' % stats)
182+
print(" table stats: size=%d, load=%.2f, avg_lookups=%.1f" % stats)
175183

176184
# output hash table
177185
for row in map:
178186
if row is None:
179-
output.append('{ 0, 0 },\n')
187+
output.append("{ 0, 0 },\n")
180188
else:
181-
output.append(row[1] + '\n')
182-
output.append('};\n')
189+
output.append(row[1] + "\n")
190+
output.append("};\n")
183191

184192
# skip to next non-blank line
185193
while True:
186194
line = file.readline()
187195
if len(line) == 0:
188-
print('unexpected end of input')
196+
print("unexpected end of input")
189197
sys.exit(1)
190198
line = line.strip()
191199
if len(line) == 0:
@@ -197,19 +205,20 @@ def process_map_table(file, line, output):
197205
if match is None:
198206
match = re_mp_map_t.match(line)
199207
if match is None:
200-
print('expecting mp_obj_dict_t or mp_map_t definition')
208+
print("expecting mp_obj_dict_t or mp_map_t definition")
201209
print(output[0])
202210
print(line)
203211
sys.exit(1)
204-
line = match.group('head') + '0' + match.group('tail') + '\n'
212+
line = match.group("head") + "0" + match.group("tail") + "\n"
205213
output.append(line)
206214

207-
return (match.group('id'),) + stats
215+
return (match.group("id"),) + stats
216+
208217

209218
def process_file(filename):
210219
output = []
211220
file_changed = False
212-
with open(filename, 'rt') as f:
221+
with open(filename, "rt") as f:
213222
while True:
214223
line = f.readline()
215224
if not line:
@@ -218,45 +227,48 @@ def process_file(filename):
218227
file_changed = True
219228
stats = process_map_table(f, line, output)
220229
if print_stats:
221-
print(' [%s: size=%d, load=%.2f, avg_lookups=%.1f]' % stats)
230+
print(" [%s: size=%d, load=%.2f, avg_lookups=%.1f]" % stats)
222231
else:
223232
output.append(line)
224233

225234
if file_changed:
226235
if print_debug:
227-
print(' modifying static maps in', output[0].strip())
228-
with open(filename, 'wt') as f:
236+
print(" modifying static maps in", output[0].strip())
237+
with open(filename, "wt") as f:
229238
for line in output:
230239
f.write(line)
231240

241+
232242
def main():
233243
# run actual C compiler
234244
# need to quote args that have special characters in them
235245
def quote(s):
236-
if s.find('<') != -1 or s.find('>') != -1:
246+
if s.find("<") != -1 or s.find(">") != -1:
237247
return "'" + s + "'"
238248
else:
239249
return s
240-
ret = os.system(cc1_path + ' ' + ' '.join(quote(s) for s in sys.argv[1:]))
250+
251+
ret = os.system(cc1_path + " " + " ".join(quote(s) for s in sys.argv[1:]))
241252
if ret != 0:
242-
ret = (ret & 0x7f) or 127 # make it in range 0-127, but non-zero
253+
ret = (ret & 0x7F) or 127 # make it in range 0-127, but non-zero
243254
sys.exit(ret)
244255

245-
if sys.argv[1] == '-E':
256+
if sys.argv[1] == "-E":
246257
# CPP has been run, now do our processing stage
247258
for i, arg in enumerate(sys.argv):
248-
if arg == '-o':
259+
if arg == "-o":
249260
return process_file(sys.argv[i + 1])
250261

251262
print('%s: could not find "-o" option' % (sys.argv[0],))
252263
sys.exit(1)
253-
elif sys.argv[1] == '-fpreprocessed':
264+
elif sys.argv[1] == "-fpreprocessed":
254265
# compiler has been run, nothing more to do
255266
return
256267
else:
257268
# unknown processing stage
258269
print('%s: unknown first option "%s"' % (sys.argv[0], sys.argv[1]))
259270
sys.exit(1)
260271

261-
if __name__ == '__main__':
272+
273+
if __name__ == "__main__":
262274
main()

0 commit comments

Comments
 (0)