From 1a35ce21aac8622660b93c0df656483e5529b1ec Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:09:01 -0600 Subject: [PATCH 1/2] aio(fix[pty]): Reject bare command strings why: Indexing a command path as argv can select its first character. what: - Validate command strings before allocating PTY descriptors. - Document the one-element argument form and test early rejection. Fixes #108 --- docs/topics/sync-and-asyncio.md | 4 ++++ src/libvt/aio.py | 5 +++++ tests/test_aio.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) 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") From a88aa64138d27d2fa5b27392f8714044c2deb4d3 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:33:15 -0600 Subject: [PATCH 2/2] CHANGES(docs): Document the PTY argument list why: A bare executable name is a string, which can be mistaken for a sequence of arguments. what: - Show the single-element command list and the early TypeError. --- CHANGES | 7 +++++++ 1 file changed, 7 insertions(+) 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)