| 139 | # stream API. |
| 140 | |
| 141 | async def process_stream( |
| 142 | proc_stream: asyncio.StreamReader, setting: _HANDLE, default_stream: IO[str] |
| 143 | ) -> bytes: |
| 144 | output = [] |
| 145 | while True: |
| 146 | try: |
| 147 | line = await proc_stream.readuntil() |
| 148 | except asyncio.LimitOverrunError as e: |
| 149 | line = await proc_stream.readexactly(e.consumed) |
| 150 | except asyncio.IncompleteReadError as e: |
| 151 | line = e.partial |
| 152 | if not line: |
| 153 | break |
| 154 | output.append(line) |
| 155 | if setting == subprocess.PIPE: |
| 156 | pass |
| 157 | elif setting == subprocess.STDOUT: |
| 158 | sys.stdout.buffer.write(line) |
| 159 | elif isinstance(setting, int): |
| 160 | os.write(setting, line) |
| 161 | elif setting is None: |
| 162 | # Sigh. See https://stackoverflow.com/questions/55681488/python-3-write-binary-to-stdout-respecting-buffering |
| 163 | default_stream.write(line.decode("utf-8")) |
| 164 | else: |
| 165 | # NB: don't use setting.write directly, that will |
| 166 | # not properly handle binary. This gives us |
| 167 | # "parity" with the normal subprocess implementation |
| 168 | os.write(setting.fileno(), line) |
| 169 | return b"".join(output) |
| 170 | |
| 171 | async def feed_input(stdin_writer: Optional[asyncio.StreamWriter]) -> None: |
| 172 | if stdin_writer is None: |