Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Add pty to Python

Jakub Novak53ee6bbbebd7
diff --git a/packages/python-sdk/e2b/sandbox/process/process_handle.py b/packages/python-sdk/e2b/sandbox/process/process_handle.py
index 7c52ed083..dac528597 100644
--- a/packages/python-sdk/e2b/sandbox/process/process_handle.py
+++ b/packages/python-sdk/e2b/sandbox/process/process_handle.py
@@ -5,6 +5,13 @@ from e2b.exceptions import SandboxException
Stdout = str
Stderr = str
+Pty = str
+
+
+@dataclass
+class PtySize:
+ rows: int
+ cols: int
@dataclass
diff --git a/packages/python-sdk/e2b/sandbox_async/main.py b/packages/python-sdk/e2b/sandbox_async/main.py
index 553567902..2c7a3d12a 100644
--- a/packages/python-sdk/e2b/sandbox_async/main.py
+++ b/packages/python-sdk/e2b/sandbox_async/main.py
@@ -13,6 +13,7 @@ from e2b.envd.api import (
)
from e2b.sandbox_async.filesystem.filesystem import Filesystem
from e2b.sandbox_async.process.process import Process
+from e2b.sandbox_async.pty.main import Pty
from e2b.sandbox_async.sandbox_api import SandboxApi
from e2b.sandbox.main import SandboxSetup
@@ -46,6 +47,10 @@ class AsyncSandbox(SandboxSetup, SandboxApi):
def commands(self) -> Process:
return self._process
+ @property
+ def pty(self) -> Pty:
+ return self._pty
+
@property
def sandbox_id(self) -> str:
return self._sandbox_id
@@ -83,6 +88,11 @@ class AsyncSandbox(SandboxSetup, SandboxApi):
self.connection_config,
self._transport._pool,
)
+ self._pty = Pty(
+ self.envd_api_url,
+ self.connection_config,
+ self._transport._pool,
+ )
async def is_running(self, request_timeout: Optional[float] = None) -> bool:
try:
diff --git a/packages/python-sdk/e2b/sandbox_async/process/process.py b/packages/python-sdk/e2b/sandbox_async/process/process.py
index c3f5f4236..727ad8cfa 100644
--- a/packages/python-sdk/e2b/sandbox_async/process/process.py
+++ b/packages/python-sdk/e2b/sandbox_async/process/process.py
@@ -112,7 +112,7 @@ class Process:
self,
cmd: str,
background: Union[Literal[False], None] = None,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: Optional[OutputHandler[Stdout]] = None,
@@ -126,7 +126,7 @@ class Process:
self,
cmd: str,
background: Literal[True],
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: Optional[OutputHandler[Stdout]] = None,
@@ -139,7 +139,7 @@ class Process:
self,
cmd: str,
background: Union[bool, None] = None,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: Optional[OutputHandler[Stdout]] = None,
@@ -163,7 +163,7 @@ class Process:
async def _start(
self,
cmd: str,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
timeout: Optional[float] = 60,
diff --git a/packages/python-sdk/e2b/sandbox_async/process/process_handle.py b/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
index dea12db3a..e57b1e9c4 100644
--- a/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
+++ b/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
@@ -17,6 +17,7 @@ from e2b.sandbox.process.process_handle import (
ProcessResult,
Stderr,
Stdout,
+ Pty,
)
from e2b.sandbox_async.utilts import OutputHandler
@@ -55,6 +56,7 @@ class AsyncProcessHandle:
],
on_stdout: Optional[OutputHandler[Stdout]] = None,
on_stderr: Optional[OutputHandler[Stderr]] = None,
+ on_pty: Optional[OutputHandler[Pty]] = None,
):
self._pid = pid
self._handle_kill = handle_kill
@@ -65,6 +67,7 @@ class AsyncProcessHandle:
self._on_stdout = on_stdout
self._on_stderr = on_stderr
+ self._on_pty = on_pty
self._result: Optional[ProcessResult] = None
self._iteration_exception: Optional[Exception] = None
@@ -74,7 +77,9 @@ class AsyncProcessHandle:
async def _iterate_events(
self,
) -> AsyncGenerator[
- Union[Tuple[Stdout, None], Tuple[None, Stderr]],
+ Union[
+ Tuple[Stdout, None, None], Tuple[None, Stderr, None], Tuple[None, None, Pty]
+ ],
None,
]:
async for event in self._events:
@@ -82,11 +87,13 @@ class AsyncProcessHandle:
if event.event.data.stdout:
out = event.event.data.stdout.decode()
self._stdout += out
- yield out, None
+ yield out, None, None
if event.event.data.stderr:
out = event.event.data.stderr.decode()
self._stderr += out
- yield None, out
+ yield None, out, None
+ if event.event.data.pty:
+ yield None, None, event.event.data.pty
if event.event.HasField("end"):
self._result = ProcessResult(
stdout=self._stdout,
@@ -102,7 +109,7 @@ class AsyncProcessHandle:
async def _handle_events(self):
try:
- async for stdout, stderr in self._iterate_events():
+ async for stdout, stderr, pty in self._iterate_events():
if stdout is not None and self._on_stdout:
cb = self._on_stdout(stdout)
if inspect.isawaitable(cb):
@@ -111,6 +118,10 @@ class AsyncProcessHandle:
cb = self._on_stderr(stderr)
if inspect.isawaitable(cb):
await cb
+ elif pty is not None and self._on_pty:
+ cb = self._on_pty(pty)
+ if inspect.isawaitable(cb):
+ await cb
except StopAsyncIteration:
pass
except Exception as e:
diff --git a/packages/python-sdk/e2b/sandbox_async/pty/main.py b/packages/python-sdk/e2b/sandbox_async/pty/main.py
new file mode 100644
index 000000000..d52c2d4d2
--- /dev/null
+++ b/packages/python-sdk/e2b/sandbox_async/pty/main.py
@@ -0,0 +1,136 @@
+import e2b_connect
+import httpcore
+
+from typing import Dict, Optional, Callable
+
+from e2b.envd.process import process_connect, process_pb2
+from e2b.connection_config import (
+ Username,
+ ConnectionConfig,
+)
+from e2b.exceptions import SandboxException
+from e2b.envd.rpc import authentication_header, handle_rpc_exception
+from e2b.sandbox.process.process_handle import PtySize
+from e2b.sandbox_async.process.process_handle import AsyncProcessHandle
+
+
+class Pty:
+ def __init__(
+ self,
+ envd_api_url: str,
+ connection_config: ConnectionConfig,
+ pool: httpcore.AsyncConnectionPool,
+ ) -> None:
+ self._connection_config = connection_config
+ self._rpc = process_connect.ProcessClient(
+ envd_api_url,
+ # TODO: Fix and enable compression again — the headers compression is not solved for streaming.
+ # compressor=e2b_connect.GzipCompressor,
+ async_pool=pool,
+ )
+
+ async def kill(
+ self,
+ pid: int,
+ request_timeout: Optional[float] = None,
+ ) -> bool:
+ try:
+ await self._rpc.asend_signal(
+ process_pb2.SendSignalRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ signal=process_pb2.Signal.SIGNAL_SIGKILL,
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+ return True
+ except Exception as e:
+ if isinstance(e, e2b_connect.ConnectException):
+ if e.status == e2b_connect.Code.not_found:
+ return False
+ raise handle_rpc_exception(e)
+
+ async def send_stdin(
+ self,
+ pid: int,
+ data: bytes,
+ request_timeout: Optional[float] = None,
+ ):
+ try:
+ await self._rpc.asend_input(
+ process_pb2.SendInputRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ input=process_pb2.ProcessInput(
+ pty=data,
+ ),
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+ except Exception as e:
+ raise handle_rpc_exception(e)
+
+ async def create(
+ self,
+ size: PtySize,
+ user: Username = "user",
+ cwd: Optional[str] = None,
+ on_data: Optional[Callable[[bytes], None]] = None,
+ envs: Optional[Dict[str, str]] = None,
+ timeout: Optional[float] = 60,
+ request_timeout: Optional[float] = None,
+ ) -> AsyncProcessHandle:
+ envs = envs or {}
+ envs["TERM"] = "xterm-256color"
+ events = self._rpc.astart(
+ process_pb2.StartRequest(
+ process=process_pb2.ProcessConfig(
+ cmd="/bin/bash",
+ envs=envs,
+ args=["-i", "-l"],
+ cwd=cwd,
+ ),
+ pty=process_pb2.PTY(
+ size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols)
+ ),
+ ),
+ headers=authentication_header(user),
+ timeout=timeout,
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+
+ try:
+ start_event = await events.__anext__()
+
+ if not start_event.HasField("event"):
+ raise SandboxException(
+ f"Failed to start process: expected start event, got {start_event}"
+ )
+
+ return AsyncProcessHandle(
+ pid=start_event.event.start.pid,
+ handle_kill=lambda: self.kill(start_event.event.start.pid),
+ events=events,
+ on_pty=on_data,
+ )
+ except Exception as e:
+ raise handle_rpc_exception(e)
+
+ async def resize(
+ self, pid: int, size: PtySize, request_timeout: Optional[float] = None
+ ):
+ await self._rpc.aupdate(
+ process_pb2.UpdateRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ pty=process_pb2.PTY(
+ size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols),
+ ),
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
diff --git a/packages/python-sdk/e2b/sandbox_sync/main.py b/packages/python-sdk/e2b/sandbox_sync/main.py
index 6c31de5a2..be3e82a6d 100644
--- a/packages/python-sdk/e2b/sandbox_sync/main.py
+++ b/packages/python-sdk/e2b/sandbox_sync/main.py
@@ -13,6 +13,7 @@ from e2b.exceptions import SandboxException, format_request_timeout_error
from e2b.sandbox.main import SandboxSetup
from e2b.sandbox_sync.filesystem.filesystem import Filesystem
from e2b.sandbox_sync.process.main import Process
+from e2b.sandbox_sync.pty.main import Pty
from e2b.sandbox_sync.sandbox_api import SandboxApi
@@ -40,6 +41,10 @@ class Sandbox(SandboxSetup, SandboxApi):
def commands(self) -> Process:
return self._process
+ @property
+ def pty(self) -> Pty:
+ return self._pty
+
@property
def sandbox_id(self) -> str:
return self._sandbox_id
@@ -116,6 +121,11 @@ class Sandbox(SandboxSetup, SandboxApi):
self.connection_config,
self._transport._pool,
)
+ self._pty = Pty(
+ self.envd_api_url,
+ self.connection_config,
+ self._transport._pool,
+ )
def is_running(self, request_timeout: Optional[float] = None) -> bool:
try:
diff --git a/packages/python-sdk/e2b/sandbox_sync/process/main.py b/packages/python-sdk/e2b/sandbox_sync/process/main.py
index 28002ddec..e3b450209 100644
--- a/packages/python-sdk/e2b/sandbox_sync/process/main.py
+++ b/packages/python-sdk/e2b/sandbox_sync/process/main.py
@@ -103,7 +103,7 @@ class Process:
self,
cmd: str,
background: Union[Literal[False], None] = None,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: Optional[Callable[[str], None]] = None,
@@ -117,7 +117,7 @@ class Process:
self,
cmd: str,
background: Literal[True],
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: None = None,
@@ -130,7 +130,7 @@ class Process:
self,
cmd: str,
background: Union[bool, None] = None,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
on_stdout: Optional[Callable[[str], None]] = None,
@@ -159,7 +159,7 @@ class Process:
def _start(
self,
cmd: str,
- envs: Optional[Dict[str, str]] = {},
+ envs: Optional[Dict[str, str]] = None,
user: Username = "user",
cwd: Optional[str] = None,
timeout: Optional[float] = 60,
diff --git a/packages/python-sdk/e2b/sandbox_sync/process/process_handle.py b/packages/python-sdk/e2b/sandbox_sync/process/process_handle.py
index f803bd6ed..3c45504c2 100644
--- a/packages/python-sdk/e2b/sandbox_sync/process/process_handle.py
+++ b/packages/python-sdk/e2b/sandbox_sync/process/process_handle.py
@@ -7,6 +7,7 @@ from e2b.sandbox.process.process_handle import (
ProcessResult,
Stderr,
Stdout,
+ Pty,
)
@@ -39,7 +40,9 @@ class ProcessHandle:
def _handle_events(
self,
) -> Generator[
- Union[Tuple[Stdout, None], Tuple[None, Stderr]],
+ Union[
+ Tuple[Stdout, None, None], Tuple[None, Stderr, None], Tuple[None, None, Pty]
+ ],
None,
None,
]:
@@ -48,11 +51,13 @@ class ProcessHandle:
if event.event.data.stdout:
out = event.event.data.stdout.decode()
self._stdout += out
- yield out, None
+ yield out, None, None
if event.event.data.stderr:
out = event.event.data.stderr.decode()
self._stderr += out
- yield None, out
+ yield None, out, None
+ if event.event.data.pty:
+ yield None, None, event.event.data.pty
if event.event.HasField("end"):
self._result = ProcessResult(
stdout=self._stdout,
@@ -68,13 +73,16 @@ class ProcessHandle:
self,
on_stdout: Optional[Callable[[str], None]] = None,
on_stderr: Optional[Callable[[str], None]] = None,
+ on_pty: Optional[Callable[[str], None]] = None,
) -> ProcessResult:
try:
- for stdout, stderr in self:
+ for stdout, stderr, pty in self:
if stdout is not None and on_stdout:
on_stdout(stdout)
elif stderr is not None and on_stderr:
on_stderr(stderr)
+ elif pty is not None and on_pty:
+ on_pty(pty)
except StopIteration:
pass
except Exception as e:
diff --git a/packages/python-sdk/e2b/sandbox_sync/pty/main.py b/packages/python-sdk/e2b/sandbox_sync/pty/main.py
new file mode 100644
index 000000000..3d4cbc761
--- /dev/null
+++ b/packages/python-sdk/e2b/sandbox_sync/pty/main.py
@@ -0,0 +1,134 @@
+import e2b_connect
+import httpcore
+
+from typing import Dict, Optional
+
+from e2b.envd.process import process_connect, process_pb2
+from e2b.connection_config import (
+ Username,
+ ConnectionConfig,
+)
+from e2b.exceptions import SandboxException
+from e2b.envd.rpc import authentication_header, handle_rpc_exception
+from e2b.sandbox.process.process_handle import PtySize
+from e2b.sandbox_sync.process.process_handle import ProcessHandle
+
+
+class Pty:
+ def __init__(
+ self,
+ envd_api_url: str,
+ connection_config: ConnectionConfig,
+ pool: httpcore.ConnectionPool,
+ ) -> None:
+ self._connection_config = connection_config
+ self._rpc = process_connect.ProcessClient(
+ envd_api_url,
+ # TODO: Fix and enable compression again — the headers compression is not solved for streaming.
+ # compressor=e2b_connect.GzipCompressor,
+ pool=pool,
+ )
+
+ def kill(
+ self,
+ pid: int,
+ request_timeout: Optional[float] = None,
+ ) -> bool:
+ try:
+ self._rpc.send_signal(
+ process_pb2.SendSignalRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ signal=process_pb2.Signal.SIGNAL_SIGKILL,
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+ return True
+ except Exception as e:
+ if isinstance(e, e2b_connect.ConnectException):
+ if e.status == e2b_connect.Code.not_found:
+ return False
+ raise handle_rpc_exception(e)
+
+ def send_stdin(
+ self,
+ pid: int,
+ data: bytes,
+ request_timeout: Optional[float] = None,
+ ) -> None:
+ try:
+ self._rpc.send_input(
+ process_pb2.SendInputRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ input=process_pb2.ProcessInput(
+ pty=data,
+ ),
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+ except Exception as e:
+ raise handle_rpc_exception(e)
+
+ def create(
+ self,
+ size: PtySize,
+ user: Username = "user",
+ cwd: Optional[str] = None,
+ envs: Optional[Dict[str, str]] = None,
+ timeout: Optional[float] = 60,
+ request_timeout: Optional[float] = None,
+ ) -> ProcessHandle:
+ envs = envs or {}
+ envs["TERM"] = "xterm-256color"
+ events = self._rpc.start(
+ process_pb2.StartRequest(
+ process=process_pb2.ProcessConfig(
+ cmd="/bin/bash",
+ envs=envs,
+ args=["-i", "-l"],
+ cwd=cwd,
+ ),
+ pty=process_pb2.PTY(
+ size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols)
+ ),
+ ),
+ headers=authentication_header(user),
+ timeout=timeout,
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
+
+ try:
+ start_event = events.__next__()
+
+ if not start_event.HasField("event"):
+ raise SandboxException(
+ f"Failed to start process: expected start event, got {start_event}"
+ )
+
+ return ProcessHandle(
+ pid=start_event.event.start.pid,
+ handle_kill=lambda: self.kill(start_event.event.start.pid),
+ events=events,
+ )
+ except Exception as e:
+ raise handle_rpc_exception(e)
+
+ def resize(
+ self, pid: int, size: PtySize, request_timeout: Optional[float] = None
+ ) -> None:
+ self._rpc.update(
+ process_pb2.UpdateRequest(
+ process=process_pb2.ProcessSelector(pid=pid),
+ pty=process_pb2.PTY(
+ size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols),
+ ),
+ ),
+ request_timeout=self._connection_config.get_request_timeout(
+ request_timeout
+ ),
+ )
diff --git a/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py b/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
index efcf89707..b0d671428 100644
--- a/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
+++ b/packages/python-sdk/tests/async/sandbox_async/commands/test_env_vars.py
@@ -4,7 +4,7 @@ from e2b import AsyncSandbox
async def test_command_envs(async_sandbox: AsyncSandbox):
- cmd = await async_sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
+ cmd = await async_sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
assert cmd.stdout.strip() == "bar"
diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_files_list.py b/packages/python-sdk/tests/async/sandbox_async/files/test_files_list.py
index 81332415e..62113160f 100644
--- a/packages/python-sdk/tests/async/sandbox_async/files/test_files_list.py
+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_files_list.py
@@ -1,8 +1,10 @@
+import uuid
+
from e2b import AsyncSandbox, FileType
async def test_list_directory(async_sandbox: AsyncSandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
await async_sandbox.files.make_dir(dir_name)
files = await async_sandbox.files.list(dir_name)
diff --git a/packages/python-sdk/tests/async/sandbox_async/files/test_make_dir.py b/packages/python-sdk/tests/async/sandbox_async/files/test_make_dir.py
index 3f5d57244..d5c723099 100644
--- a/packages/python-sdk/tests/async/sandbox_async/files/test_make_dir.py
+++ b/packages/python-sdk/tests/async/sandbox_async/files/test_make_dir.py
@@ -1,8 +1,10 @@
+import uuid
+
from e2b import AsyncSandbox
async def test_make_directory(async_sandbox: AsyncSandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
await async_sandbox.files.make_dir(dir_name)
exists = await async_sandbox.files.exists(dir_name)
@@ -10,7 +12,7 @@ async def test_make_directory(async_sandbox: AsyncSandbox):
async def test_make_directory_already_exists(async_sandbox: AsyncSandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
created = await async_sandbox.files.make_dir(dir_name)
assert created
@@ -20,7 +22,7 @@ async def test_make_directory_already_exists(async_sandbox: AsyncSandbox):
async def test_make_nested_directory(async_sandbox: AsyncSandbox):
- nested_dir_name = "test_directory/nested_directory"
+ nested_dir_name = f"test_directory_{uuid.uuid4()}/nested_directory"
await async_sandbox.files.make_dir(nested_dir_name)
exists = await async_sandbox.files.exists(nested_dir_name)
diff --git a/packages/python-sdk/tests/async/sandbox_async/pty/test_pty_create.py b/packages/python-sdk/tests/async/sandbox_async/pty/test_pty_create.py
index e69de29bb..a2009da55 100644
--- a/packages/python-sdk/tests/async/sandbox_async/pty/test_pty_create.py
+++ b/packages/python-sdk/tests/async/sandbox_async/pty/test_pty_create.py
@@ -0,0 +1,21 @@
+from e2b import AsyncSandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+async def test_pty_create(async_sandbox: AsyncSandbox):
+ output = []
+
+ def append_data(data:list, x: bytes):
+ data.append(x.decode("utf-8"))
+
+ terminal = await async_sandbox.pty.create(
+ PtySize(80, 24), on_data=lambda x: append_data(output, x), envs={"ABC": "123"}
+ )
+
+ await async_sandbox.pty.send_stdin(terminal.pid, b"echo $ABC\n")
+ await async_sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+
+ await terminal.wait()
+ assert terminal.exit_code == 0
+
+ assert "123" in ''.join(output)
diff --git a/packages/python-sdk/tests/async/sandbox_async/pty/test_resize.py b/packages/python-sdk/tests/async/sandbox_async/pty/test_resize.py
index e69de29bb..d49db52dd 100644
--- a/packages/python-sdk/tests/async/sandbox_async/pty/test_resize.py
+++ b/packages/python-sdk/tests/async/sandbox_async/pty/test_resize.py
@@ -0,0 +1,31 @@
+from e2b import AsyncSandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+async def test_resize(async_sandbox: AsyncSandbox):
+ output = []
+
+ def append_data(data: list, x: bytes):
+ data.append(x.decode("utf-8"))
+
+ terminal = await async_sandbox.pty.create(PtySize(cols=80, rows=24), on_data=lambda x: append_data(output, x))
+
+ await async_sandbox.pty.send_stdin(terminal.pid, b"tput cols\n")
+ await async_sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+ await terminal.wait()
+ assert terminal.exit_code == 0
+
+ assert "80" in ''.join(output)
+
+ output = []
+
+ terminal = await async_sandbox.pty.create(PtySize(cols=80, rows=24), on_data=lambda x: append_data(output, x))
+
+ await async_sandbox.pty.resize(terminal.pid, PtySize(cols=100, rows=24))
+ await async_sandbox.pty.send_stdin(terminal.pid, b"tput cols\n")
+ await async_sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+
+ await terminal.wait()
+ assert terminal.exit_code == 0
+ assert "100" in ''.join(output)
+
diff --git a/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py b/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py
new file mode 100644
index 000000000..a6d354cb8
--- /dev/null
+++ b/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py
@@ -0,0 +1,9 @@
+from e2b import AsyncSandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+async def test_send_input(async_sandbox: AsyncSandbox):
+ terminal = await async_sandbox.pty.create(PtySize(cols=80, rows=24))
+ await async_sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+ await terminal.wait()
+ assert terminal.exit_code == 0
diff --git a/packages/python-sdk/tests/async/sandbox_async/pty/test_stream_input.py b/packages/python-sdk/tests/async/sandbox_async/pty/test_stream_input.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
index 79954adf7..e0d364c55 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_env_vars.py
@@ -4,7 +4,7 @@ from e2b import Sandbox
def test_command_envs(sandbox: Sandbox):
- cmd = sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
+ cmd = sandbox.commands.run("echo $FOO", envs={"FOO": "bar"})
assert cmd.stdout.strip() == "bar"
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_send_stdin.py b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_send_stdin.py
index 640cdf340..a9eeed24f 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/commands/test_send_stdin.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/commands/test_send_stdin.py
@@ -5,7 +5,7 @@ def test_send_stdin_to_process(sandbox: Sandbox):
cmd = sandbox.commands.run("cat", background=True)
sandbox.commands.send_stdin(cmd.pid, "Hello, World!")
- for stdout, _ in cmd:
+ for stdout, _, _ in cmd:
assert stdout == "Hello, World!"
break
@@ -14,7 +14,7 @@ def test_send_special_characters_to_process(sandbox: Sandbox):
cmd = sandbox.commands.run("cat", background=True)
sandbox.commands.send_stdin(cmd.pid, "!@#$%^&*()_+")
- for stdout, _ in cmd:
+ for stdout, _, _ in cmd:
assert stdout == "!@#$%^&*()_+"
break
@@ -23,6 +23,6 @@ def test_send_multiline_string_to_process(sandbox: Sandbox):
cmd = sandbox.commands.run("cat", background=True)
sandbox.commands.send_stdin(cmd.pid, "Hello,\nWorld!")
- for stdout, _ in cmd:
+ for stdout, _, _ in cmd:
assert stdout == "Hello,\nWorld!"
break
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_list_dir.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_list_dir.py
index 420aee529..b243ce96a 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_list_dir.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_list_dir.py
@@ -1,8 +1,10 @@
+import uuid
+
from e2b import Sandbox, FileType
def test_list_directory(sandbox: Sandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
sandbox.files.make_dir(dir_name)
files = sandbox.files.list(dir_name)
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/files/test_make_dir.py b/packages/python-sdk/tests/sync/sandbox_sync/files/test_make_dir.py
index 698c961e4..9251e3d19 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/files/test_make_dir.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/files/test_make_dir.py
@@ -1,8 +1,10 @@
+import uuid
+
from e2b import Sandbox
def test_make_directory(sandbox: Sandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
sandbox.files.make_dir(dir_name)
exists = sandbox.files.exists(dir_name)
@@ -10,7 +12,7 @@ def test_make_directory(sandbox: Sandbox):
async def test_make_directory_already_exists(sandbox: Sandbox):
- dir_name = "test_directory"
+ dir_name = f"test_directory_{uuid.uuid4()}"
created = sandbox.files.make_dir(dir_name)
assert created
@@ -20,7 +22,7 @@ async def test_make_directory_already_exists(sandbox: Sandbox):
def test_make_nested_directory(sandbox: Sandbox):
- nested_dir_name = "test_directory/nested_directory"
+ nested_dir_name = f"test_directory_{uuid.uuid4()}/nested_directory"
sandbox.files.make_dir(nested_dir_name)
exists = sandbox.files.exists(nested_dir_name)
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty.py b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty.py
new file mode 100644
index 000000000..b36d0af5a
--- /dev/null
+++ b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty.py
@@ -0,0 +1,21 @@
+from e2b import Sandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+def test_pty(sandbox: Sandbox):
+ def append_data(data:list, x: bytes):
+ data.append(x.decode("utf-8"))
+
+ terminal = sandbox.pty.create(
+ PtySize(80, 24), envs={"ABC": "123"}, cwd="/"
+ )
+
+ sandbox.pty.send_stdin(terminal.pid, b"echo $ABC\n")
+ sandbox.pty.send_stdin(terminal.pid, b"pwd\n")
+ sandbox.pty.send_stdin(terminal.pid, b"logout\n")
+
+ output = []
+ result = terminal.wait(on_pty=lambda x: append_data(output, x))
+ assert result.exit_code == 0
+
+ assert "123" in '\n'.join(output)
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty_create.py b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_pty_create.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_resize.py b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_resize.py
index e69de29bb..741105ca8 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_resize.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_resize.py
@@ -0,0 +1,31 @@
+from e2b import Sandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+def test_resize(sandbox: Sandbox):
+ def append_data(data:list, x: bytes):
+ data.append(x.decode("utf-8"))
+
+ terminal = sandbox.pty.create(PtySize(cols=80, rows=24))
+
+ sandbox.pty.send_stdin(terminal.pid, b"tput cols\n")
+ sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+
+ output = []
+ result = terminal.wait(on_pty=lambda x: append_data(output, x))
+ assert result.exit_code == 0
+
+ assert "80" in ''.join(output)
+
+ terminal = sandbox.pty.create(PtySize(cols=80, rows=24))
+
+ sandbox.pty.resize(terminal.pid, PtySize(cols=100, rows=24))
+ sandbox.pty.send_stdin(terminal.pid, b"tput cols\n")
+ sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+
+ output = []
+ result = terminal.wait(on_pty=lambda x: append_data(output, x))
+ assert result.exit_code == 0
+
+ assert "100" in ''.join(output)
+
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_send_input.py b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_send_input.py
new file mode 100644
index 000000000..dbb62923f
--- /dev/null
+++ b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_send_input.py
@@ -0,0 +1,9 @@
+from e2b import Sandbox
+from e2b.sandbox.process.process_handle import PtySize
+
+
+def test_send_input(sandbox: Sandbox):
+ terminal = sandbox.pty.create(PtySize(cols=80, rows=24))
+ sandbox.pty.send_stdin(terminal.pid, b"exit\n")
+ result = terminal.wait()
+ assert result.exit_code == 0
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/pty/test_stream_input.py b/packages/python-sdk/tests/sync/sandbox_sync/pty/test_stream_input.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/packages/python-sdk/tests/sync/sandbox_sync/test_host.py b/packages/python-sdk/tests/sync/sandbox_sync/test_host.py
index 92a149a80..18de6d5c1 100644
--- a/packages/python-sdk/tests/sync/sandbox_sync/test_host.py
+++ b/packages/python-sdk/tests/sync/sandbox_sync/test_host.py
@@ -4,11 +4,11 @@ from time import sleep
def test_ping_server(sandbox, debug):
- cmd = sandbox.commands.run("python -m http.server 8000", background=True)
+ cmd = sandbox.commands.run("python -m http.server 8001", background=True)
try:
sleep(1)
- host = sandbox.get_host(8000)
+ host = sandbox.get_host(8001)
res = httpx.get(f"{'http' if debug else 'https'}://{host}")
assert res.status_code == 200