File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed
src/google/adk/code_executors Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from __future__ import annotations
16
+
15
17
from contextlib import redirect_stdout
16
18
import io
19
+ import re
20
+ from typing import Any
17
21
18
22
from pydantic import Field
19
23
from typing_extensions import override
24
28
from .code_execution_utils import CodeExecutionResult
25
29
26
30
31
+ def _prepare_globals (code : str , globals_ : dict [str , Any ]) -> None :
32
+ """Prepare globals for code execution, injecting __name__ if needed."""
33
+ if re .search (r"if\s+__name__\s*==\s*['\"]__main__['\"]" , code ):
34
+ globals_ ['__name__' ] = '__main__'
35
+
36
+
27
37
class UnsafeLocalCodeExecutor (BaseCodeExecutor ):
28
38
"""A code executor that unsafely execute code in the current local context."""
29
39
@@ -55,6 +65,7 @@ def execute_code(
55
65
error = ''
56
66
try :
57
67
globals_ = {}
68
+ _prepare_globals (code_execution_input .code , globals_ )
58
69
locals_ = {}
59
70
stdout = io .StringIO ()
60
71
with redirect_stdout (stdout ):
You can’t perform that action at this time.
0 commit comments