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
| ... | ... |
@@ -50,15 +50,13 @@ def main(): |
| 50 | 50 |
opts = get_options() |
| 51 | 51 |
outfile = None |
| 52 | 52 |
if opts.outfile: |
| 53 |
- outfile = open(opts.outfile, 'a', 0) |
|
| 53 |
+ # note, binary mode so we can do unbuffered output. |
|
| 54 |
+ outfile = open(opts.outfile, 'ab', 0) |
|
| 54 | 55 |
|
| 55 | 56 |
# Otherwise fileinput reprocess args as files |
| 56 | 57 |
sys.argv = [] |
| 57 |
- while True: |
|
| 58 |
- line = sys.stdin.readline() |
|
| 59 |
- if not line: |
|
| 60 |
- return 0 |
|
| 61 | 58 |
|
| 59 |
+ for line in iter(sys.stdin.readline, ''): |
|
| 62 | 60 |
# put skip lines here |
| 63 | 61 |
if skip_line(line): |
| 64 | 62 |
continue |
| ... | ... |
@@ -75,8 +73,16 @@ def main(): |
| 75 | 75 |
if opts.verbose: |
| 76 | 76 |
sys.stdout.write(line) |
| 77 | 77 |
sys.stdout.flush() |
| 78 |
+ |
|
| 78 | 79 |
if outfile: |
| 79 |
- outfile.write(line) |
|
| 80 |
+ # We've opened outfile as a binary file to get the |
|
| 81 |
+ # non-buffered behaviour. on python3, sys.stdin was |
|
| 82 |
+ # opened with the system encoding and made the line into |
|
| 83 |
+ # utf-8, so write the logfile out in utf-8 bytes. |
|
| 84 |
+ if sys.version_info < (3,): |
|
| 85 |
+ outfile.write(line) |
|
| 86 |
+ else: |
|
| 87 |
+ outfile.write(line.encode('utf-8'))
|
|
| 80 | 88 |
outfile.flush() |
| 81 | 89 |
|
| 82 | 90 |
|