Skip to content

Commit b3a124f

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 71824c6 commit b3a124f

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
@@ -967,10 +967,9 @@ def report(f, dest):
967967
msg = 'template %r never instantiated' % (name,)
968968
else:
969969
assert False
970-
sys.stderr.write('%s:%d: warning WUNUSED: %s during build.'
971-
% (dest, line, msg)
972-
+ ' Only partial porting possible\n')
973-
sys.stderr.write('%s: original location\n' % (loc,))
970+
print(f'{dest}:{line}: warning WUNUSED: {msg} during build. Only partial porting possible',
971+
file=sys.stderr)
972+
print(f'{loc}: original location', file=sys.stderr)
974973

975974
tags = {
976975
'PSHA1': PSHA1,
@@ -1088,19 +1087,18 @@ def main(argv):
10881087
t = tags[tag](loc, ast.literal_eval(params))
10891088
transformations.setdefault(t.phase, []).append((t, lineno, line))
10901089
except:
1091-
sys.stderr.write("Unexpected error on this porting tag:\n")
1092-
sys.stderr.write(line)
1093-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1090+
print("Unexpected error on this porting tag:", file=sys.stderr)
1091+
print(line, file=sys.stderr)
1092+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
10941093
raise
10951094

10961095
if not transformations:
1097-
sys.stderr.write('%s:0: error: no tags found matching file %r' % (
1098-
tagfilename, args.src))
1096+
print(f'{tagfilename}:0: error: no tags found matching file {args.src}', file=sys.stderr)
10991097
exit(1)
11001098

11011099
if args.compat and is_device_file(args.src):
1102-
sys.stderr.write('%s:0: warning: file contains device statement,'
1103-
% (args.src,) + ' ignoring --compat flag\n')
1100+
print(f'{args.src}:0: warning: file contains device statement, ignoring --compat flag',
1101+
file=sys.stderr)
11041102
args.compat = False
11051103
src = SourceFile(args.src, args.compat)
11061104
errors = 0
@@ -1110,14 +1108,14 @@ def main(argv):
11101108
t.apply(src)
11111109
except PortingFailure as e:
11121110
(loc, _) = line.split(' porting ', 2)
1113-
sys.stderr.write("%s error%s: %s\n" % (
1114-
loc, '' if e.tag is None else ' ' + e.tag, e))
1115-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1111+
print(f"""{loc} error{'' if e.tag is None else ' ' + e.tag}: {e}""",
1112+
file=sys.stderr)
1113+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
11161114
errors += 1
11171115
except:
1118-
sys.stderr.write("Unexpected error on this porting tag:\n")
1119-
sys.stderr.write(line)
1120-
sys.stderr.write("%s:%d: found here\n" % (tagfilename, lineno))
1116+
print("Unexpected error on this porting tag:", file=sys.stderr)
1117+
print(line, file=sys.stderr)
1118+
print(f"{tagfilename}:{lineno}: found here", file=sys.stderr)
11211119
traceback.print_exc()
11221120
errors += 1
11231121

@@ -1127,9 +1125,9 @@ def main(argv):
11271125
src.commit(f)
11281126
if errors:
11291127
total = sum(map(len, transformations.values()))
1130-
sys.stderr.write(f'''\
1128+
print(f'''\
11311129
*** Failed to apply {errors} out of {total} porting tags; partial result saved to {args.dest}. Consider applying the failed tags manually.
1132-
''')
1130+
''', file=sys.stderr)
11331131
exit(1)
11341132

11351133
if __name__ == '__main__':

0 commit comments

Comments
 (0)