From 8cc81aef7dac6c5587797a227fc556e290c83217 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 27 Jun 2025 12:42:18 -0500 Subject: [PATCH] scripts: Adopt __get__ binding for write/writeln methods This actually binds our custom write/writeln functions as methods to the file object: def writeln(self, s=''): self.write(s) self.write('\n') f.writeln = writeln.__get__(f) This doesn't really gain us anything, but is a bit more correct and may be safer if other code messes with the file's internals. --- scripts/bench.py | 21 +++++++++++---------- scripts/codemap.py | 10 +++++----- scripts/codemapsvg.py | 8 ++++---- scripts/dbgbmap.py | 10 +++++----- scripts/dbgbmapsvg.py | 8 ++++---- scripts/dbgtrace.py | 8 ++++---- scripts/plot.py | 10 +++++----- scripts/prettyasserts.py | 8 ++++---- scripts/test.py | 21 +++++++++++---------- scripts/treemap.py | 10 +++++----- scripts/treemapsvg.py | 8 ++++---- 11 files changed, 62 insertions(+), 60 deletions(-) diff --git a/scripts/bench.py b/scripts/bench.py index 99bf2376..113f986d 100755 --- a/scripts/bench.py +++ b/scripts/bench.py @@ -362,17 +362,18 @@ def compile(bench_paths, **args): # write generated bench source if 'output' in args: with openio(args['output'], 'w') as f: - _write = f.write - def write(s): - f.lineno += s.count('\n') - _write(s) - def writeln(s=''): - f.lineno += s.count('\n') + 1 - _write(s) - _write('\n') + # some helpful file functions f.lineno = 1 - f.write = write - f.writeln = writeln + f.write_ = f.write + def write(self, s): + self.lineno += s.count('\n') + self.write_(s) + f.write = write.__get__(f) + def writeln(self, s=''): + self.lineno += s.count('\n') + 1 + self.write_(s) + self.write_('\n') + f.writeln = writeln.__get__(f) f.writeln("// Generated by %s:" % sys.argv[0]) f.writeln("//") diff --git a/scripts/codemap.py b/scripts/codemap.py index d026a4c9..66ae1ed3 100755 --- a/scripts/codemap.py +++ b/scripts/codemap.py @@ -951,11 +951,11 @@ def main_(ring, paths, *, label=False, no_label=False, **args): - # give ring an writeln function - def writeln(s=''): - ring.write(s) - ring.write('\n') - ring.writeln = writeln + # give ring a writeln function + def writeln(self, s=''): + self.write(s) + self.write('\n') + ring.writeln = writeln.__get__(ring) # figure out what color should be if color == 'auto': diff --git a/scripts/codemapsvg.py b/scripts/codemapsvg.py index 110aad81..9be108e3 100755 --- a/scripts/codemapsvg.py +++ b/scripts/codemapsvg.py @@ -1174,10 +1174,10 @@ def main(paths, output, *, # create svg file with openio(output, 'w') as f: - def writeln(s=''): - f.write(s) - f.write('\n') - f.writeln = writeln + def writeln(self, s=''): + self.write(s) + self.write('\n') + f.writeln = writeln.__get__(f) # yes this is svg f.write('