BUG: LocalPythonExecutor Code execution failed at line 'class Status(Enum) (original) (raw)

Problem
I'm working on running untrusted ai generated code in sandbox environment. However, when i am writing a testcase as following and run with LocalPythonExecutor

from enum import Enum

class Status(Enum): SUCCESS = "Success" FAILURE = "Failure" PENDING = "Pending" ERROR = "Error"

print(Status.SUCCESS)

Steps to reproduce
Please provide a minimal, self-contained, and reproducible example of the bug.

  1. Create a file for above testcase (i named as script.py)
  2. Create a main script for LocalPythonExecutor
  3. Run the main script

from smolagents.local_python_executor import LocalPythonExecutor import builtins

with open('script.py','r') as f: code = f.read()

custom_executor = LocalPythonExecutor( ["enum"] )

custom_executor.send_tools( { "print":builtins.print } )

result = custom_executor(code) print(result)

Actual behavior and error logs
It will raise the problem AttributeError: 'dict' object has no attribute '_member_names'. However if directly run the script without using LocalPythonExecutor, it run normally. The following is the error logs.

Traceback (most recent call last): File "/Users/victor/Desktop/Project/xxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 1584, in evaluate_python_code result = evaluate_ast(node, state, static_tools, custom_tools, authorized_imports) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/victor/Desktop/Project/xxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 201, in _check_return result = func(expression, state, static_tools, custom_tools, authorized_imports=authorized_imports) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/victor/Desktop/Project/xxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 1501, in evaluate_ast return evaluate_class_def(expression, *common_params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/victor/Desktop/Project/xxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 555, in evaluate_class_def new_class = type(class_name, tuple(bases), class_dict) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.12/Frameworks/Python.framework/Versions/3.11/lib/python3.11/enum.py", line 515, in new member_names = classdict._member_names ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'dict' object has no attribute '_member_names'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/Users/victor/Desktop/Project/xxx/run_script.py", line 18, in result = custom_executor(code) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/victor/Desktop/Project/xxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 1679, in call output, is_final_answer = evaluate_python_code( ^^^^^^^^^^^^^^^^^^^^^ File "/Users/victor/Desktop/Project/xxxx/.test/lib/python3.11/site-packages/smolagents/local_python_executor.py", line 1600, in evaluate_python_code raise InterpreterError( smolagents.local_python_executor.InterpreterError: Code execution failed at line 'class Status(Enum): SUCCESS = "Success" FAILURE = "Failure" PENDING = "Pending" ERROR = "Error"' due to: AttributeError: 'dict' object has no attribute '_member_names'

Expected behavior
Should print out the status.success

Environment:
Please complete the following information:

Name: smolagents
Version: 1.22.0
Summary: 🤗 smolagents: a barebones library for agents. Agents write python code to call tools or orchestrate other agents.
Home-page: 
Author: 
Author-email: Aymeric Roucher <aymeric@hf.co>
License: 
Location: /Users/victor/Desktop/Project/xxxx/.test/lib/python3.11/site-packages
Requires: huggingface-hub, jinja2, pillow, python-dotenv, requests, rich
Required-by: