MCPcopy Create free account
hub / github.com/GAIR-NLP/factool / python_executor

Class python_executor

factool/math/tool.py:6–35  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4import pdb
5
6class python_executor:
7 def __init__(self):
8 pass
9
10 def run_single(self, program):
11 buffer = io.StringIO() # Create an in-memory buffer for the output
12 stdout = sys.stdout # Save the original standard output
13 sys.stdout = buffer # Redirect the standard output to the buffer
14 try:
15 exec(program)
16 except Exception as e:
17 # Handle the error here
18 error_message = str(e)
19 sys.stdout = stdout # Restore the original standard output
20 return error_message
21
22 exec(program)
23 sys.stdout = stdout # Restore the original standard output
24 output = buffer.getvalue() # Get the output from the buffer
25 return output
26
27 def run(self, snippet):
28 if snippet == None:
29 return None
30 exec_result = self.run_single(snippet)
31 if exec_result and 'False' in exec_result:
32 exec_result = 'False'
33 else:
34 exec_result = 'True'
35 return exec_result

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected