py3 changes for outfilter.py

I started running this with dib where we have pure python3
environments and it failed.

You can't have unbuffered text i/o in python3 for ... reasons? [1]
Changing the file to binary mode works around this.  Python3 opens
sys.stdin in text mode, so we need to manually convert the unicode
strings to bytes before we write them to the binary file.

[1] http://bugs.python.org/issue17404

Change-Id: Iebb26f0d3c2347d262cbc10dfd0912840cd05878
This commit is contained in:
Ian Wienand
2017-06-16 12:56:53 +10:00
parent a9e946471e
commit e033e1b80f

View File

@@ -50,15 +50,13 @@ def main():
opts = get_options()
outfile = None
if opts.outfile:
outfile = open(opts.outfile, 'a', 0)
# note, binary mode so we can do unbuffered output.
outfile = open(opts.outfile, 'ab', 0)
# Otherwise fileinput reprocess args as files
sys.argv = []
while True:
line = sys.stdin.readline()
if not line:
return 0
for line in iter(sys.stdin.readline, ''):
# put skip lines here
if skip_line(line):
continue
@@ -75,8 +73,16 @@ def main():
if opts.verbose:
sys.stdout.write(line)
sys.stdout.flush()
if outfile:
outfile.write(line)
# We've opened outfile as a binary file to get the
# non-buffered behaviour. on python3, sys.stdin was
# opened with the system encoding and made the line into
# utf-8, so write the logfile out in utf-8 bytes.
if sys.version_info < (3,):
outfile.write(line)
else:
outfile.write(line.encode('utf-8'))
outfile.flush()