e2b-code-interpreter 2.1.1__tar.gz → 2.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: e2b-code-interpreter
3
- Version: 2.1.1
3
+ Version: 2.2.1
4
4
  Summary: E2B Code Interpreter - Stateful code execution
5
5
  Home-page: https://e2b.dev/
6
6
  License: MIT
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
16
  Requires-Dist: attrs (>=21.3.0)
17
- Requires-Dist: e2b (>=2.2.1,<3.0.0)
17
+ Requires-Dist: e2b (>=2.3.0,<3.0.0)
18
18
  Requires-Dist: httpx (>=0.20.0,<1.0.0)
19
19
  Project-URL: Bug Tracker, https://github.com/e2b-dev/code-interpreter/issues
20
20
  Project-URL: Repository, https://github.com/e2b-dev/code-interpreter/tree/main/python
@@ -20,8 +20,8 @@ from e2b_code_interpreter.models import (
20
20
  Context,
21
21
  Result,
22
22
  aextract_exception,
23
- parse_output,
24
- OutputHandler,
23
+ OutputHandlerWithAsync,
24
+ async_parse_output,
25
25
  OutputMessage,
26
26
  )
27
27
  from e2b_code_interpreter.exceptions import (
@@ -69,10 +69,10 @@ class AsyncSandbox(BaseAsyncSandbox):
69
69
  self,
70
70
  code: str,
71
71
  language: Union[Literal["python"], None] = None,
72
- on_stdout: Optional[OutputHandler[OutputMessage]] = None,
73
- on_stderr: Optional[OutputHandler[OutputMessage]] = None,
74
- on_result: Optional[OutputHandler[Result]] = None,
75
- on_error: Optional[OutputHandler[ExecutionError]] = None,
72
+ on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
73
+ on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
74
+ on_result: Optional[OutputHandlerWithAsync[Result]] = None,
75
+ on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
76
76
  envs: Optional[Dict[str, str]] = None,
77
77
  timeout: Optional[float] = None,
78
78
  request_timeout: Optional[float] = None,
@@ -103,10 +103,10 @@ class AsyncSandbox(BaseAsyncSandbox):
103
103
  self,
104
104
  code: str,
105
105
  language: Optional[str] = None,
106
- on_stdout: Optional[OutputHandler[OutputMessage]] = None,
107
- on_stderr: Optional[OutputHandler[OutputMessage]] = None,
108
- on_result: Optional[OutputHandler[Result]] = None,
109
- on_error: Optional[OutputHandler[ExecutionError]] = None,
106
+ on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
107
+ on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
108
+ on_result: Optional[OutputHandlerWithAsync[Result]] = None,
109
+ on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
110
110
  envs: Optional[Dict[str, str]] = None,
111
111
  timeout: Optional[float] = None,
112
112
  request_timeout: Optional[float] = None,
@@ -138,10 +138,10 @@ class AsyncSandbox(BaseAsyncSandbox):
138
138
  self,
139
139
  code: str,
140
140
  context: Optional[Context] = None,
141
- on_stdout: Optional[OutputHandler[OutputMessage]] = None,
142
- on_stderr: Optional[OutputHandler[OutputMessage]] = None,
143
- on_result: Optional[OutputHandler[Result]] = None,
144
- on_error: Optional[OutputHandler[ExecutionError]] = None,
141
+ on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
142
+ on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
143
+ on_result: Optional[OutputHandlerWithAsync[Result]] = None,
144
+ on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
145
145
  envs: Optional[Dict[str, str]] = None,
146
146
  timeout: Optional[float] = None,
147
147
  request_timeout: Optional[float] = None,
@@ -172,10 +172,10 @@ class AsyncSandbox(BaseAsyncSandbox):
172
172
  code: str,
173
173
  language: Optional[str] = None,
174
174
  context: Optional[Context] = None,
175
- on_stdout: Optional[OutputHandler[OutputMessage]] = None,
176
- on_stderr: Optional[OutputHandler[OutputMessage]] = None,
177
- on_result: Optional[OutputHandler[Result]] = None,
178
- on_error: Optional[OutputHandler[ExecutionError]] = None,
175
+ on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
176
+ on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
177
+ on_result: Optional[OutputHandlerWithAsync[Result]] = None,
178
+ on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
179
179
  envs: Optional[Dict[str, str]] = None,
180
180
  timeout: Optional[float] = None,
181
181
  request_timeout: Optional[float] = None,
@@ -215,7 +215,7 @@ class AsyncSandbox(BaseAsyncSandbox):
215
215
  execution = Execution()
216
216
 
217
217
  async for line in response.aiter_lines():
218
- parse_output(
218
+ await async_parse_output(
219
219
  execution,
220
220
  line,
221
221
  on_stdout=on_stdout,
@@ -1,3 +1,4 @@
1
+ import inspect
1
2
  import json
2
3
  import logging
3
4
 
@@ -20,8 +21,10 @@ from httpx import Response
20
21
  from .charts import Chart, _deserialize_chart
21
22
 
22
23
  T = TypeVar("T")
23
- OutputHandler = Union[
24
- Callable[[T], Any],
24
+ OutputHandler = Union[Callable[[T], Any],]
25
+
26
+ OutputHandlerWithAsync = Union[
27
+ OutputHandler[T],
25
28
  Callable[[T], Awaitable[Any]],
26
29
  ]
27
30
 
@@ -422,6 +425,32 @@ def parse_output(
422
425
  on_result: Optional[OutputHandler[Result]] = None,
423
426
  on_error: Optional[OutputHandler[ExecutionError]] = None,
424
427
  ):
428
+ _parse_output(execution, output, on_stdout, on_stderr, on_result, on_error)
429
+
430
+
431
+ async def async_parse_output(
432
+ execution: Execution,
433
+ output: str,
434
+ on_stdout: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
435
+ on_stderr: Optional[OutputHandlerWithAsync[OutputMessage]] = None,
436
+ on_result: Optional[OutputHandlerWithAsync[Result]] = None,
437
+ on_error: Optional[OutputHandlerWithAsync[ExecutionError]] = None,
438
+ ):
439
+ none_or_awaitable = _parse_output(
440
+ execution, output, on_stdout, on_stderr, on_result, on_error
441
+ )
442
+ if inspect.isawaitable(none_or_awaitable):
443
+ await none_or_awaitable
444
+
445
+
446
+ def _parse_output(
447
+ execution: Execution,
448
+ output: str,
449
+ on_stdout: Optional[OutputHandler[OutputMessage]] = None,
450
+ on_stderr: Optional[OutputHandler[OutputMessage]] = None,
451
+ on_result: Optional[OutputHandler[Result]] = None,
452
+ on_error: Optional[OutputHandler[ExecutionError]] = None,
453
+ ) -> Union[None, Awaitable[Any]]:
425
454
  data = json.loads(output)
426
455
  data_type = data.pop("type")
427
456
 
@@ -429,22 +458,24 @@ def parse_output(
429
458
  result = Result(**data)
430
459
  execution.results.append(result)
431
460
  if on_result:
432
- on_result(result)
461
+ return on_result(result)
433
462
  elif data_type == "stdout":
434
463
  execution.logs.stdout.append(data["text"])
435
464
  if on_stdout:
436
- on_stdout(OutputMessage(data["text"], data["timestamp"], False))
465
+ return on_stdout(OutputMessage(data["text"], data["timestamp"], False))
437
466
  elif data_type == "stderr":
438
467
  execution.logs.stderr.append(data["text"])
439
468
  if on_stderr:
440
- on_stderr(OutputMessage(data["text"], data["timestamp"], True))
469
+ return on_stderr(OutputMessage(data["text"], data["timestamp"], True))
441
470
  elif data_type == "error":
442
471
  execution.error = ExecutionError(data["name"], data["value"], data["traceback"])
443
472
  if on_error:
444
- on_error(execution.error)
473
+ return on_error(execution.error)
445
474
  elif data_type == "number_of_executions":
446
475
  execution.execution_count = data["execution_count"]
447
476
 
477
+ return None
478
+
448
479
 
449
480
  @dataclass
450
481
  class Context:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "e2b-code-interpreter"
3
- version = "2.1.1"
3
+ version = "2.2.1"
4
4
  description = "E2B Code Interpreter - Stateful code execution"
5
5
  authors = ["e2b <hello@e2b.dev>"]
6
6
  license = "MIT"
@@ -14,7 +14,7 @@ python = "^3.9"
14
14
 
15
15
  httpx = ">=0.20.0, <1.0.0"
16
16
  attrs = ">=21.3.0"
17
- e2b = "^2.2.1"
17
+ e2b = "^2.3.0"
18
18
 
19
19
  [tool.poetry.group.dev.dependencies]
20
20
  pytest = "^7.4.0"