Skip to content

Commit d92e564

Browse files
committed
port_dml: Convert sys.stderr.write to print
It's easy to forget the trailing newline character when using sys.stderr.write and there are some such cases. Fix by converting all of them to print calls with file=sys.stderr instead. Replace printf-style format strings with modern f-string formatting to make it more beautiful.
1 parent eb513fe commit d92e564

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

py/port_dml.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,9 @@ def report(f, dest):
10281028
msg = 'template %r never instantiated' % (name,)
10291029
else:
10301030
assert False
1031-
sys.stderr.write('%s:%d: warning WUNUSED: %s during build.'
1032-
% (dest, line, msg)
1033-
+ ' Only partial porting possible\n')
1034-
sys.stderr.write('%s: original location\n' % (loc,))
1031+
print(f'{dest}:{line}: warning WUNUSED: {msg} during build. Only partial porting possible',
1032+
file=sys.stderr)
1033+
print(f'{loc}: original location', file=sys.stderr)
10351034

10361035
tags = {
10371036
'PSHA1': PSHA1,
@@ -1152,19 +1151,18 @@ def main(argv):
11521151
t = tags[tag](loc, ast.literal_eval(params))
11531152
transformations.setdefault(t.phase, []).append((t, lineno, line))
11541153
except:
1155-
sys.stderr.write("Unexpected error on this porting tag:\n")
1156-
sys.stderr.write(line)
1157-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1154+
print("Unexpected error on this porting tag:", file=sys.stderr)
1155+
print(line, file=sys.stderr)
1156+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
11581157
raise
11591158

11601159
if not transformations:
1161-
sys.stderr.write('%s:0: error: no tags found matching file %r' % (
1162-
tagfilename, args.src))
1160+
print(f'{tagfilename}:0: error: no tags found matching file {args.src}', file=sys.stderr)
11631161
exit(1)
11641162

11651163
if args.compat and is_device_file(args.src):
1166-
sys.stderr.write('%s:0: warning: file contains device statement,'
1167-
% (args.src,) + ' ignoring --compat flag\n')
1164+
print(f'{args.src}:0: warning: file contains device statement, ignoring --compat flag',
1165+
file=sys.stderr)
11681166
args.compat = False
11691167
src = SourceFile(args.src, args.compat)
11701168
errors = 0
@@ -1174,14 +1172,14 @@ def main(argv):
11741172
t.apply(src)
11751173
except PortingFailure as e:
11761174
(loc, _) = line.split(' porting ', 2)
1177-
sys.stderr.write("%s error%s: %s\n" % (
1178-
loc, '' if e.tag is None else ' ' + e.tag, e))
1179-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1175+
print(f"""{loc} error{'' if e.tag is None else ' ' + e.tag}: {e}""",
1176+
file=sys.stderr)
1177+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
11801178
errors += 1
11811179
except:
1182-
sys.stderr.write("Unexpected error on this porting tag:\n")
1183-
sys.stderr.write(line)
1184-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1180+
print("Unexpected error on this porting tag:", file=sys.stderr)
1181+
print(line, file=sys.stderr)
1182+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
11851183
traceback.print_exc()
11861184
errors += 1
11871185

@@ -1191,9 +1189,9 @@ def main(argv):
11911189
src.commit(f)
11921190
if errors:
11931191
total = sum(map(len, transformations.values()))
1194-
sys.stderr.write(f'''\
1192+
print(f'''\
11951193
*** Failed to apply {errors} out of {total} porting tags; partial result saved to {args.dest}. Consider applying the failed tags manually.
1196-
''')
1194+
''', file=sys.stderr)
11971195
exit(2)
11981196

11991197
if __name__ == '__main__':

0 commit comments

Comments
 (0)