| 1 |
# Platform-specific subprocess bits'n'pieces. |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import os |
| 5 |
import sys |
| 6 |
from typing import TYPE_CHECKING |
| 7 |
|
| 8 |
import trio |
| 9 |
|
| 10 |
from .. import _core, _subprocess |
| 11 |
from .._abc import ReceiveStream, SendStream # noqa: TC001 |
| 12 |
|
| 13 |
_wait_child_exiting_error: ImportError | None = None |
| 14 |
_create_child_pipe_error: ImportError | None = None |
| 15 |
|
| 16 |
|
| 17 |
if TYPE_CHECKING: |
| 18 |
# internal types for the pipe representations used in type checking only |
| 19 |
class ClosableSendStream(SendStream): |
| 20 |
def close(self) -> None: ... |
| 21 |
|
| 22 |
class ClosableReceiveStream(ReceiveStream): |
| 23 |
def close(self) -> None: ... |
| 24 |
|
| 25 |
|
| 26 |
# Fallback versions of the functions provided -- implementations |
| 27 |
# per OS are imported atop these at the bottom of the module. |
| 28 |
async def wait_child_exiting(process: _subprocess.Process) -> None: |
| 29 |
"""Block until the child process managed by ``process`` is exiting. |
| 30 |
|
| 31 |
It is invalid to call this function if the process has already |
| 32 |
been waited on; that is, ``process.returncode`` must be None. |
| 33 |
|
| 34 |
When this function returns, it indicates that a call to |
| 35 |
:meth:`subprocess.Popen.wait` will immediately be able to |
| 36 |
return the process's exit status. The actual exit status is not |
| 37 |
consumed by this call, since :class:`~subprocess.Popen` wants |
| 38 |
to be able to do that itself. |
| 39 |
""" |
| 40 |
raise NotImplementedError from _wait_child_exiting_error # pragma: no cover |
| 41 |
|
| 42 |
|
| 43 |
def create_pipe_to_child_stdin() -> tuple[ClosableSendStream, int]: |
| 44 |
"""Create a new pipe suitable for sending data from this |
| 45 |
process to the standard input of a child we're about to spawn. |
| 46 |
|
| 47 |
Returns: |
| 48 |
A pair ``(trio_end, subprocess_end)`` where ``trio_end`` is a |
| 49 |
:class:`~trio.abc.SendStream` and ``subprocess_end`` is |
| 50 |
something suitable for passing as the ``stdin`` argument of |
| 51 |
:class:`subprocess.Popen`. |
| 52 |
""" |
| 53 |
raise NotImplementedError from _create_child_pipe_error # pragma: no cover |
| 54 |
|
| 55 |
|
| 56 |
def create_pipe_from_child_output() -> tuple[ClosableReceiveStream, int]: |
| 57 |
"""Create a new pipe suitable for receiving data into this |
| 58 |
process from the standard output or error stream of a child |
| 59 |
we're about to spawn. |
| 60 |
|
| 61 |
Returns: |
| 62 |
A pair ``(trio_end, subprocess_end)`` where ``trio_end`` is a |
| 63 |
:class:`~trio.abc.ReceiveStream` and ``subprocess_end`` is |
| 64 |
something suitable for passing as the ``stdin`` argument of |
| 65 |
:class:`subprocess.Popen`. |
| 66 |
""" |
| 67 |
raise NotImplementedError from _create_child_pipe_error # pragma: no cover |
| 68 |
|
| 69 |
|
| 70 |
try: |
| 71 |
if sys.platform == "win32": |
| 72 |
from .windows import wait_child_exiting |
| 73 |
elif sys.platform != "linux" and (TYPE_CHECKING or hasattr(_core, "wait_kevent")): |
| 74 |
from .kqueue import wait_child_exiting |
| 75 |
else: |
| 76 |
# as it's an exported symbol, noqa'd |
| 77 |
from .waitid import wait_child_exiting # noqa: F401 |
| 78 |
except ImportError as ex: # pragma: no cover |
| 79 |
_wait_child_exiting_error = ex |
| 80 |
|
| 81 |
try: |
| 82 |
if TYPE_CHECKING: |
| 83 |
# Not worth type checking these definitions |
| 84 |
pass |
| 85 |
|
| 86 |
elif os.name == "posix": |
| 87 |
|
| 88 |
def create_pipe_to_child_stdin() -> tuple[trio.lowlevel.FdStream, int]: |
| 89 |
rfd, wfd = os.pipe() |
| 90 |
return trio.lowlevel.FdStream(wfd), rfd |
| 91 |
|
| 92 |
def create_pipe_from_child_output() -> tuple[trio.lowlevel.FdStream, int]: |
| 93 |
rfd, wfd = os.pipe() |
| 94 |
return trio.lowlevel.FdStream(rfd), wfd |
| 95 |
|
| 96 |
elif os.name == "nt": |
| 97 |
import msvcrt |
| 98 |
|
| 99 |
# This isn't exported or documented, but it's also not |
| 100 |
# underscore-prefixed, and seems kosher to use. The asyncio docs |
| 101 |
# for 3.5 included an example that imported socketpair from |
| 102 |
# windows_utils (before socket.socketpair existed on Windows), and |
| 103 |
# when asyncio.windows_utils.socketpair was removed in 3.7, the |
| 104 |
# removal was mentioned in the release notes. |
| 105 |
from asyncio.windows_utils import pipe as windows_pipe |
| 106 |
|
| 107 |
from .._windows_pipes import PipeReceiveStream, PipeSendStream |
| 108 |
|
| 109 |
def create_pipe_to_child_stdin() -> tuple[PipeSendStream, int]: |
| 110 |
# for stdin, we want the write end (our end) to use overlapped I/O |
| 111 |
rh, wh = windows_pipe(overlapped=(False, True)) |
| 112 |
return PipeSendStream(wh), msvcrt.open_osfhandle(rh, os.O_RDONLY) |
| 113 |
|
| 114 |
def create_pipe_from_child_output() -> tuple[PipeReceiveStream, int]: |
| 115 |
# for stdout/err, it's the read end that's overlapped |
| 116 |
rh, wh = windows_pipe(overlapped=(True, False)) |
| 117 |
return PipeReceiveStream(rh), msvcrt.open_osfhandle(wh, 0) |
| 118 |
|
| 119 |
else: # pragma: no cover |
| 120 |
raise ImportError("pipes not implemented on this platform") |
| 121 |
|
| 122 |
except ImportError as ex: # pragma: no cover |
| 123 |
_create_child_pipe_error = ex |