Skip to content

Commit 83c8a92

Browse files
author
Langfun Authors
committed
Added output_files to SandboxOutput. Changed Sandbox to be a Protocol, allowing for more flexibility in implementation.
PiperOrigin-RevId: 764379017
1 parent 529656f commit 83c8a92

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

langfun/core/coding/python/sandboxing.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import abc
1717
import os
1818
import tempfile
19-
from typing import Annotated
19+
from typing import Annotated, Protocol, Self
2020

2121
from langfun.core.coding.python import parsing
2222
import pyglove as pg
@@ -35,10 +35,42 @@ class SandboxOutput(pg.Object):
3535
'The stderr of the sandbox execution.'
3636
] = ''
3737

38+
output_files: Annotated[
39+
dict[str, bytes],
40+
'The output files of the sandbox execution.'
41+
] = {}
3842

39-
class Sandbox(pg.Object):
43+
44+
class Sandbox(Protocol):
4045
"""Interface for Python sandbox."""
4146

47+
def run(
48+
self,
49+
code: str,
50+
**kwargs,
51+
) -> SandboxOutput:
52+
"""Runs code in the sandbox."""
53+
54+
def upload(
55+
self,
56+
path: str,
57+
**kwargs,
58+
) -> str:
59+
"""Uploads a file to the sandbox."""
60+
61+
def clone(self) -> 'Sandbox':
62+
"""Clones the sandbox."""
63+
64+
def __enter__(self) -> Self:
65+
"""Sandboxes should be used as a context manager."""
66+
67+
def __exit__(self, exc_type, exc_value, traceback) -> None:
68+
"""Sandboxes should be used as a context manager."""
69+
70+
71+
class BaseSandbox(pg.Object):
72+
"""Interface and partial implementation for Python sandbox."""
73+
4274
def _on_bound(self):
4375
super()._on_bound()
4476
self._uploaded_files: dict[str, str] = {}
@@ -105,7 +137,7 @@ def __exit__(self, exc_type, exc_value, traceback):
105137
self.cleanup()
106138

107139

108-
class MultiProcessingSandbox(Sandbox):
140+
class MultiProcessingSandbox(BaseSandbox):
109141
"""Sandbox using multiprocessing."""
110142

111143
def _on_bound(self):

0 commit comments

Comments
 (0)