1818# List of allowed modules for execution
1919ALLOWED_MODULES = {
2020 'math' : math ,
21+ 'numpy' : 'numpy' , # String indicates module should be imported in execution context
2122}
2223
2324# Initial code generation prompt
6061
6162# Simulation prompt
6263SIMULATION_PROMPT = '''
63- The following Python code could not be executed after several attempts.
64- Please simulate its execution and determine the final value that would be in the 'answer' variable.
64+ The following Python code could not be executed directly. Analyze the code and determine what the answer would be.
65+ Pay special attention to:
66+ 1. The core computational logic, ignoring any visualization or display code
67+ 2. The key mathematical operations that determine the final answer
68+ 3. Any logic that affects the 'answer' variable
6569
66- Code to simulate :
70+ Code to analyze :
6771```python
6872{code}
6973```
7074
71- Last error encountered:
75+ Runtime error encountered:
7276{error}
7377
74- Important:
75- 1. Follow the logic of the code exactly
76- 2. Perform all calculations carefully
77- 3. Return ONLY the final numeric or string value, no explanations
78- 4. If the code contains semantic functions (like text analysis), use your judgment to simulate them
78+ Return ONLY the final value that would be in the 'answer' variable. Return just the value, no explanations.
7979'''
8080
8181def extract_code_blocks (text : str ) -> List [str ]:
@@ -93,12 +93,25 @@ def sanitize_code(code: str) -> str:
9393 # Add standard imports
9494 imports = "\n " .join (f"import { mod } " for mod in ALLOWED_MODULES )
9595
96+ # Remove or modify problematic visualization code
97+ lines = code .split ('\n ' )
98+ safe_lines = []
99+ for line in lines :
100+ # Skip matplotlib-related imports and plotting commands
101+ if any (x in line .lower () for x in ['matplotlib' , 'plt.' , '.plot(' , '.show(' , 'figure' , 'subplot' ]):
102+ continue
103+ # Keep the line if it's not visualization-related
104+ safe_lines .append (line )
105+
106+ safe_code = '\n ' .join (safe_lines )
107+
96108 # Add safety wrapper
97109 wrapper = f"""
98110{ imports }
99111
100112def safe_execute():
101- { code .replace ('\n ' , '\n ' )}
113+ import numpy as np # Always allow numpy
114+ { safe_code .replace ('\n ' , '\n ' )}
102115 return answer if 'answer' in locals() else None
103116
104117result = safe_execute()
@@ -111,22 +124,25 @@ def execute_code(code: str) -> Tuple[Any, str]:
111124 logger .info ("Attempting to execute code" )
112125 logger .info (f"Code:\n { code } " )
113126
114- execution_env = {}
115127 try :
116- sanitized_code = sanitize_code (code )
117- exec (sanitized_code , execution_env )
118- answer = execution_env .get ('answer' )
128+ # Create a clean environment
129+ execution_env = {}
130+
131+ # Execute the code as-is
132+ exec (code , execution_env )
119133
120- if answer is not None :
134+ # Look for answer variable
135+ if 'answer' in execution_env :
136+ answer = execution_env ['answer' ]
121137 logger .info (f"Execution successful. Answer: { answer } " )
122138 return answer , None
123139 else :
124- error = "Code executed but did not produce an answer"
140+ error = "Code executed but did not produce an answer variable "
125141 logger .warning (error )
126142 return None , error
127143
128144 except Exception as e :
129- error = f" { type ( e ). __name__ } : { str (e )} \n { traceback . format_exc () } "
145+ error = str (e )
130146 logger .error (f"Execution failed: { error } " )
131147 return None , error
132148
0 commit comments