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
@@ -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("//")