diff --git a/CHANGES b/CHANGES index 50d3bcd1..b5d3c4cf 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,13 @@ _Notes on the upcoming release will go here._ +### Fixes + +#### Diagnose a bare PTY command (#165) + +Pass `open_pty([executable])` for a command with no arguments. A bare +command string now raises `TypeError` before PTY allocation. + ### Development #### Design review and issue skills (#155) diff --git a/docs/topics/sync-and-asyncio.md b/docs/topics/sync-and-asyncio.md index d4709224..77bbe89b 100644 --- a/docs/topics/sync-and-asyncio.md +++ b/docs/topics/sync-and-asyncio.md @@ -57,6 +57,10 @@ into a terminal and drives outbound input over an optional writer. {class}`~libvt.aio.AsyncSession` protocol (`terminal`, `eof`, `send`, `send_event`, `send_key`, `paste`, `drain`, `wait_for`, `aclose`). +Pass the executable and its arguments as a sequence, for example +`open_pty([sys.executable, "-i"])`. For an executable with no arguments, +pass `open_pty([executable])`. + ### Waiting for screen state {meth}`AsyncTerminalStream.wait_for() ` diff --git a/src/libvt/aio.py b/src/libvt/aio.py index e7f3537c..b071be36 100644 --- a/src/libvt/aio.py +++ b/src/libvt/aio.py @@ -1814,6 +1814,8 @@ async def open_pty( ------ NotImplementedError On platforms lacking POSIX ``pty.openpty()`` (Windows). + TypeError + If *argv* is a bare string. Use ``[executable]`` for one command. ValueError If *argv*, dimensions, *tty_mode*, *sync_timeout*, or buffer sizes are invalid. @@ -1823,6 +1825,9 @@ async def open_pty( if sys.platform == "win32": msg = "open_pty requires POSIX pty.openpty()" raise NotImplementedError(msg) + if isinstance(argv, str): + msg = "argv must be a sequence of strings such as [executable]" + raise TypeError(msg) if not argv: msg = "argv must not be empty" raise ValueError(msg) diff --git a/tests/test_aio.py b/tests/test_aio.py index 631a87e7..a27a9100 100644 --- a/tests/test_aio.py +++ b/tests/test_aio.py @@ -485,3 +485,22 @@ def write_eof(self) -> None: _TlsNoEofWriter(), ) as tls_stream: tls_stream.write_eof() + + +@pytest.mark.skipif(sys.platform == "win32", reason="POSIX pty required") +async def test_pty_rejects_bare_argv_before_allocating( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """The command boundary rejects a bare string before opening descriptors.""" + import pty + + def unexpected_open() -> tuple[int, int]: + pytest.fail("invalid argv reached PTY allocation") + + monkeypatch.setattr(pty, "openpty", unexpected_open) + with pytest.raises(TypeError, match=r"argv.*sequence.*strings"): + async with open_pty(sys.executable): + pytest.fail("invalid argv started a process") + with pytest.raises(ValueError, match="empty"): + async with open_pty([]): + pytest.fail("empty argv started a process")