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.
This commit is contained in:
Christopher Haster
2025-06-27 12:42:18 -05:00
parent 8b6e51d54e
commit 8cc81aef7d
11 changed files with 62 additions and 60 deletions
+11 -10
View File
@@ -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("//")
+5 -5
View File
@@ -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':
+4 -4
View File
@@ -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('<svg '
+5 -5
View File
@@ -4421,11 +4421,11 @@ def main_(ring, disk, mroots=None, *,
title_littlefs=False,
title_usage=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':
+4 -4
View File
@@ -4512,10 +4512,10 @@ def main(disk, output, mroots=None, *,
# 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('<svg '
+4 -4
View File
@@ -1284,10 +1284,10 @@ def main(path='-', *,
if wear else ''))
# give ring a writeln function
def writeln(s=''):
ring.write(s)
ring.write('\n')
ring.writeln = writeln
def writeln(self, s=''):
self.write(s)
self.write('\n')
ring.writeln = writeln.__get__(ring)
# figure out width/height
if width is None:
+5 -5
View File
@@ -1243,11 +1243,11 @@ def main_(ring, csv_paths, *,
subplot={},
subplots=[],
**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':
+4 -4
View File
@@ -583,10 +583,10 @@ def main(input=None, output=None, *,
p = Parser(in_f.read(), '')
with openio(output or '-', '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)
# write extra verbose asserts
mkheader(f, limit=limit)
+11 -10
View File
@@ -374,17 +374,18 @@ def compile(test_paths, **args):
# write generated test 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("//")
+5 -5
View File
@@ -964,11 +964,11 @@ def main_(ring, csv_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':
+4 -4
View File
@@ -877,10 +877,10 @@ def main(csv_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('<svg '