| 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | import subprocess |
| 4 | |
| 5 | # this program should be showing the first paragraph prefixed with |
| 6 | # `first:` and the remaining lines prefixed with `remaining:`. |
| 7 | # |
| 8 | # the point of this program is to show that this would *not* work with |
| 9 | # a normal `open` call, which has `buffering=1` and would therefore |
| 10 | # slurp more than what we are displaying in the loop. |
| 11 | stream = open('/etc/hosts', mode='rb', buffering=0) |
| 12 | |
| 13 | for line in stream: |
| 14 | print("first: %s" % line.decode('utf8'), end='') |
| 15 | if line == b"\n": |
| 16 | break |
| 17 | |
| 18 | proc = subprocess.Popen(['sed', 's/^/remaining:/'], stdin=stream, close_fds=False) |
| 19 | proc.wait() |