# Python reference

> API reference for every public class and method exported by the dvm-run Python package.

This is the Markdown of https://docs.dvm.run/reference/python/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

Written by hand from `python/src/dvm` (version 0.3.0) because the package’s docstrings are not generated into this static site; covers every symbol in `dvm.__all__`.

`Sandbox`, `SandboxPoint`, and `Command` are blocking; `AsyncSandbox`, `AsyncSandboxPoint`, and `AsyncCommand` provide the same methods as coroutines.

## `Sandbox`

| Call                                                                                                                                                                        | Returns         |                                                                                                                                             |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `Sandbox.create(image, *, command=None, entrypoint=None, cpus=None, memory_mib=None, seed=None, env=None, name=None, network=None, allow=None, api_key=None, api_url=None)` | `Sandbox`       | Makes a sandbox; returns once it runs, or has exited. `network` is `"public"`, `"none"` or `"allowlist"`; `allow` alone makes an allowlist. |
| `Sandbox.connect(sandbox_id, *, api_key=None, api_url=None)`                                                                                                                | `Sandbox`       | A handle to a sandbox made elsewhere.                                                                                                       |
| `.id`, `.name`                                                                                                                                                              | `int`, `str`    |                                                                                                                                             |
| `.spec`                                                                                                                                                                     | `dict`          | The sandbox’s spec as the API gives it.                                                                                                     |
| `.state()`                                                                                                                                                                  | `RunState`      |                                                                                                                                             |
| `.pause()`, `.resume()`                                                                                                                                                     | `None`          |                                                                                                                                             |
| `.set_network(network, allow=None)`                                                                                                                                         | `None`          | From now on; open connections stay.                                                                                                         |
| `.stop()`                                                                                                                                                                   | `None`          | One that already ended stays as it is.                                                                                                      |
| `.wait()`                                                                                                                                                                   | `int`           | The exit code; a stopped or failed run raises its error.                                                                                    |
| `.run(command, *, env=None, cwd=None, stdin=None, user=None)`                                                                                                               | `CommandResult` | A session’s command, to its end. `command` is a `list[str]` (argv) or a `str` (a script).                                                   |
| `.start(command, *, env=None, cwd=None, stdin=None, user=None)`                                                                                                             | `Command`       | Returns once started. Without `stdin`, stdin stays open.                                                                                    |
| `.write_file(path, data, *, mode=None)`                                                                                                                                     | `None`          | `data` is `bytes`.                                                                                                                          |
| `.read_file(path)`                                                                                                                                                          | `bytes`         |                                                                                                                                             |
| `.end()`                                                                                                                                                                    | `int`           | Ends a session’s input; waits for the exit code.                                                                                            |
| `.output()`                                                                                                                                                                 | `Output`        | What the first process wrote so far.                                                                                                        |
| `.seek(*, at_ns=None, thread=None, event=None, watch=None)`                                                                                                                 | `SandboxPoint`  | No target: the end. `at_ns`, or `thread` and `event` together.                                                                              |
| `.close()`                                                                                                                                                                  | `None`          | Closes the handle’s connection; the sandbox goes on.                                                                                        |
| `with Sandbox.create(…) as sandbox:`                                                                                                                                        |                 | Ends a session’s input and waits, or stops a paused sandbox or a program; then closes.                                                      |

## `SandboxPoint`

| Member                                 |                          |                                            |
| -------------------------------------- | ------------------------ | ------------------------------------------ |
| `.thread`, `.process`, `.event`        | `int`                    | Where it stopped.                          |
| `.ns`                                  | `int` or `None`          | About when, in the recording’s time.       |
| `.at`                                  | `StopEvent`              |                                            |
| `.operations`, `.stats`                | `int`, `dict[str, int]`  |                                            |
| `.step(*, watch=None)`                 | `SandboxPoint` or `None` | The next watched event; `None` at the end. |
| `.read_file(path)`                     | `bytes`                  |                                            |
| `.list_dir(path)`                      | `list[DirEntry]`         |                                            |
| `.stat(path, *, follow_symlinks=True)` | `FileStat`               |                                            |

`watch` accepts a list of watch points, `"syscall"`, `"syscall:<name>"`, `"file-syscalls"`, `"socket-syscalls"`, `"processes"`, or `"page-fault"` (hyphens `-` and underscores `_` are interchangeable), or raw API objects as `dict`s.

## `Command`

| Call                 | Returns         |                                             |
| -------------------- | --------------- | ------------------------------------------- |
| `.wait()`            | `CommandResult` |                                             |
| `.signal(number)`    | `None`          | 1 to 64, to its process group.              |
| `.write_stdin(data)` | `None`          | Queues `bytes`.                             |
| `.close_stdin()`     | `None`          | After the queued bytes; safe to call again. |

## Values

| Class             | Fields                                                                                                                                                                      |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `CommandResult`   | `exit_code: int`, `stdout: bytes`, `stderr: bytes`                                                                                                                          |
| `Output`          | `stdout: bytes`, `stderr: bytes`                                                                                                                                            |
| `RunState`        | `kind: str`, `exit_code: int \| None`, `reason: str \| None`, `is_active: bool`                                                                                             |
| `StopEvent`       | `kind` (`"syscall"`, `"page_fault"`, `"arrival"`); for a syscall `syscall`, `name`, `args`, `decoded`; for a page fault `address`, `write`, `present`; for an arrival `rip` |
| `DecodedArgument` | `arg: int`, `kind` (`"path"`, `"strings"`, `"address"`, `"bytes"`), `value`, `truncated: bool`                                                                              |
| `DirEntry`        | `name: str`, `raw_name: bytes`, `type: str`                                                                                                                                 |
| `FileStat`        | `type`, `mode`, `uid`, `gid`, `size`, `links`, `mtime` and `ctime` as `(seconds, nanoseconds)`, `target: bytes \| None`, `mount: str \| None`                               |

## Errors

All exceptions inherit from `DvmError`:

* `NetworkError`
* `ApiError` (`status`, `message`), with subclasses `InvalidRequestError`, `AuthenticationError`, `NotFoundError`, and `ConflictError`
* `SandboxEndedError` (`sandbox_id`, `state`), with subclass `SandboxFailedError` (`reason`)

See [Errors](/reference/errors/) for details.

## Command line

Run `dvm-run login [--api-url URL] [--no-browser]` or `python -m dvm login` ([Getting in](/start/access/)).
