8000 Fix: unsafe_local_code_executor global import by qieqieplus · Pull Request #869 · google/adk-python · GitHub
[go: up one dir, main page]

Skip to content

Fix: unsafe_local_code_executor global import #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

qieqieplus
Copy link
@qieqieplus qieqieplus commented May 22, 2025

How to reproduce the error:

from google.adk.code_executors import UnsafeLocalCodeExecutor
from google.adk.code_executors.code_execution_utils import CodeExecutionInput

result = UnsafeLocalCodeExecutor().execute_code(
    invocation_context=None,
    code_execution_input=CodeExecutionInput(
        code='''
import math

def pi():
    return math.pi

print(pi())
'''
    )
)

print(result)

output:

CodeExecutionResult(stdout='', stderr="name 'math' is not defined", output_files=[])

Copy link
google-cla bot commented May 22, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@@ -55,10 +55,9 @@ def execute_code(
error = ''
try:
globals_ = {}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

globals_ = {"__name__": "__main__"}

for better compatibility?

@hangfei
Copy link
Collaborator
hangfei commented May 30, 2025

Could you explain what's the issue and how your fix works?

Please also fix CLA.

@hangfei hangfei self-requested a review May 30, 2025 20:58
@qieqieplus
Copy link
Author

Could you explain what's the issue and how your fix works?

Please also fix CLA.

When exec() is called with only the globals dict, it is shared for both global and local scopes during execution. Modules imported at the top level (e.g., import math) are added to globals_ and are then accessible to functions defined within the executed code, as these functions will use globals_ as their global namespace.

@qieqieplus qieqieplus changed the title fix unsafe_local_code_executor global import Fix: unsafe_local_code_executor global import Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0