16
16
import abc
17
17
import os
18
18
import tempfile
19
- from typing import Annotated
19
+ from typing import Annotated , Protocol , Self
20
20
21
21
from langfun .core .coding .python import parsing
22
22
import pyglove as pg
@@ -35,10 +35,42 @@ class SandboxOutput(pg.Object):
35
35
'The stderr of the sandbox execution.'
36
36
] = ''
37
37
38
+ output_files : Annotated [
39
+ dict [str , bytes ],
40
+ 'The output files of the sandbox execution.'
41
+ ] = {}
38
42
39
- class Sandbox (pg .Object ):
43
+
44
+ class Sandbox (Protocol ):
40
45
"""Interface for Python sandbox."""
41
46
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
+
42
74
def _on_bound (self ):
43
75
super ()._on_bound ()
44
76
self ._uploaded_files : dict [str , str ] = {}
@@ -105,7 +137,7 @@ def __exit__(self, exc_type, exc_value, traceback):
105
137
self .cleanup ()
106
138
107
139
108
- class MultiProcessingSandbox (Sandbox ):
140
+ class MultiProcessingSandbox (BaseSandbox ):
109
141
"""Sandbox using multiprocessing."""
110
142
111
143
def _on_bound (self ):
0 commit comments