Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/sync-command-handle-accessors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2b/python-sdk": minor
---

Add `stdout`, `stderr`, `error` and `exit_code` accessors to the sync `CommandHandle`, mirroring the async handle and the JS SDK for parity.
36 changes: 36 additions & 0 deletions packages/python-sdk/e2b/sandbox_sync/commands/command_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ def pid(self):
"""
return self._pid

@property
def stdout(self):
"""
Command stdout output.
"""
return "".join(self._stdout_chunks)

@property
def stderr(self):
"""
Command stderr output.
"""
return "".join(self._stderr_chunks)

@property
def error(self):
"""
Command execution error message.
"""
if self._result is None:
return None
return self._result.error

@property
def exit_code(self):
"""
Command execution exit code.

`0` if the command finished successfully.

It is `None` if the command is still running.
"""
if self._result is None:
return None
return self._result.exit_code

def __init__(
self,
pid: int,
Expand Down
23 changes: 23 additions & 0 deletions packages/python-sdk/tests/test_command_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,26 @@ async def events():
# be flushed to the stdout callback as a replacement character.
assert "".join(chunks) == "a�"
assert isinstance(handle._iteration_exception, RuntimeError)


def test_sync_command_handle_exposes_result_accessors():
def events():
yield _stdout_event(b"out")
yield _stderr_event(b"err")
yield _end_event(0)

handle = CommandHandle(pid=1, handle_kill=lambda: True, events=events())

# None while the command is still running, matching the async twin.
assert handle.exit_code is None
assert handle.error is None
assert handle.stdout == ""
assert handle.stderr == ""

handle.wait()

assert handle.stdout == "out"
assert handle.stderr == "err"
assert handle.exit_code == 0
assert handle.error is None