-
Notifications
You must be signed in to change notification settings - Fork 54
/
emitter.py
36 lines (29 loc) · 1.17 KB
/
emitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
# Helper binary used by test_shell.py to print interleaved sequences
# of strings to stderr/stdout.
import itertools
import sys
from typing import Iterator, Sequence, Tuple, TypeVar
T = TypeVar("T")
def grouper(n: int, iterable: Sequence[T]) -> Iterator[Tuple[T, ...]]:
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.zip_longest(*args)
if __name__ == "__main__":
for mode, payload in grouper(2, sys.argv[1:]):
# NB: this is \n and not linesep because we are going to send
# payload to a text mode stream (stderr/stdout) which will
# automatically translate to native layout as needed
payload = payload.replace(r"\n", "\n")
if mode == "e":
print(payload, end="", file=sys.stderr)
sys.stderr.flush()
elif mode == "o":
print(payload, end="", file=sys.stdout)
sys.stdout.flush()
elif mode == "r":
# Big enough payload to exceed default chunk limit
print("." * (4096 * 128), file=sys.stdout)
sys.stdout.flush()
else:
raise RuntimeError("Unrecognized mode")