|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import logging |
| 5 | +import subprocess |
5 | 6 | import types |
6 | 7 | import typing |
| 8 | +from pathlib import Path |
7 | 9 | from typing import Any, Callable, List, Optional, Set, Union |
8 | 10 |
|
9 | 11 | from deprecated import deprecated |
@@ -328,19 +330,41 @@ def cdp_get_module(domain: Union[str, types.ModuleType]): |
328 | 330 | return domain_mod |
329 | 331 |
|
330 | 332 |
|
331 | | -async def _read_process_stderr( |
332 | | - process: asyncio.subprocess.Process, n: int = 2**16 |
333 | | -) -> str: |
| 333 | +def _start_process( |
| 334 | + exe: str | Path, params: List[str], is_posix: bool |
| 335 | +) -> subprocess.Popen: |
| 336 | + """ |
| 337 | + Start a subprocess with the given executable and parameters. |
| 338 | +
|
| 339 | + :param exe: The executable to run. |
| 340 | + :param params: List of parameters to pass to the executable. |
| 341 | + :param is_posix: Boolean indicating if the system is POSIX compliant. |
| 342 | +
|
| 343 | + :return: An instance of `subprocess.Popen`. |
| 344 | + """ |
| 345 | + return subprocess.Popen( |
| 346 | + [str(exe)] + params, |
| 347 | + stdin=subprocess.PIPE, |
| 348 | + stdout=subprocess.PIPE, |
| 349 | + stderr=subprocess.PIPE, |
| 350 | + close_fds=is_posix, |
| 351 | + ) |
| 352 | + |
| 353 | + |
| 354 | +async def _read_process_stderr(process: subprocess.Popen, n: int = 2**16) -> str: |
334 | 355 | """ |
335 | 356 | Read the given number of bytes from the stderr of the given process. |
336 | 357 |
|
337 | 358 | Read bytes are automatically decoded to utf-8. |
338 | 359 | """ |
339 | | - if process.stderr is None: |
340 | | - raise ValueError("Process has no stderr") |
| 360 | + |
| 361 | + async def read_stderr() -> bytes: |
| 362 | + if process.stderr is None: |
| 363 | + raise ValueError("Process has no stderr") |
| 364 | + return await asyncio.to_thread(process.stderr.read, n) |
341 | 365 |
|
342 | 366 | try: |
343 | | - return (await asyncio.wait_for(process.stderr.read(n), 0.25)).decode("utf-8") |
| 367 | + return (await asyncio.wait_for(read_stderr(), 0.25)).decode("utf-8") |
344 | 368 | except asyncio.TimeoutError: |
345 | 369 | logger.debug("Timeout reading process stderr") |
346 | 370 | return "" |
0 commit comments