Callback for _system.
(p: subprocess.Popen[bytes])
| 66 | |
| 67 | |
| 68 | def _system_body(p: subprocess.Popen[bytes]) -> int: |
| 69 | """Callback for _system.""" |
| 70 | enc = DEFAULT_ENCODING |
| 71 | |
| 72 | # Dec 2024: in both of these functions, I'm not sure why we .splitlines() |
| 73 | # the bytes and then decode each line individually instead of just decoding |
| 74 | # the whole thing at once. |
| 75 | def stdout_read() -> None: |
| 76 | try: |
| 77 | assert p.stdout is not None |
| 78 | for byte_line in (read_no_interrupt(p.stdout) or b"").splitlines(): |
| 79 | line = byte_line.decode(enc, "replace") |
| 80 | print(line, file=sys.stdout) |
| 81 | except Exception as e: |
| 82 | print(f"Error reading stdout: {e}", file=sys.stderr) |
| 83 | |
| 84 | def stderr_read() -> None: |
| 85 | try: |
| 86 | assert p.stderr is not None |
| 87 | for byte_line in (read_no_interrupt(p.stderr) or b"").splitlines(): |
| 88 | line = byte_line.decode(enc, "replace") |
| 89 | print(line, file=sys.stderr) |
| 90 | except Exception as e: |
| 91 | print(f"Error reading stderr: {e}", file=sys.stderr) |
| 92 | |
| 93 | stdout_thread = Thread(target=stdout_read) |
| 94 | stderr_thread = Thread(target=stderr_read) |
| 95 | |
| 96 | stdout_thread.start() |
| 97 | stderr_thread.start() |
| 98 | |
| 99 | # Wait to finish for returncode. Unfortunately, Python has a bug where |
| 100 | # wait() isn't interruptible (https://bugs.python.org/issue28168) so poll in |
| 101 | # a loop instead of just doing `return p.wait()` |
| 102 | while True: |
| 103 | result = p.poll() |
| 104 | if result is None: |
| 105 | time.sleep(0.01) |
| 106 | else: |
| 107 | break |
| 108 | |
| 109 | # Join the threads to ensure they complete before returning |
| 110 | stdout_thread.join() |
| 111 | stderr_thread.join() |
| 112 | |
| 113 | return result |
| 114 | |
| 115 | |
| 116 | def system(cmd: str) -> Optional[int]: |
nothing calls this directly
no test coverage detected
searching dependent graphs…