Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
_Notes on the upcoming release will go here._
<!-- END PLACEHOLDER - ADD NEW CHANGELOG ENTRIES BELOW THIS LINE -->

### 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)
Expand Down
4 changes: 4 additions & 0 deletions docs/topics/sync-and-asyncio.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() <libvt.aio.AsyncTerminalStream.wait_for>`
Expand Down
5 changes: 5 additions & 0 deletions src/libvt/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Loading