MCPcopy Create free account
hub / github.com/AMAP-ML/Thinking-with-Map / execute

Method execute

demo/qwen_agent/tools/python_executor.py:149–180  ·  view source on GitHub ↗
(
        code,
        get_answer_from_stdout=None,
        runtime=None,
        answer_symbol=None,
        answer_expr=None,
        timeout_length=20,
    )

Source from the content-addressed store, hash-verified

147
148 @staticmethod
149 def execute(
150 code,
151 get_answer_from_stdout=None,
152 runtime=None,
153 answer_symbol=None,
154 answer_expr=None,
155 timeout_length=20,
156 ):
157 from timeout_decorator import timeout
158 try:
159 if get_answer_from_stdout:
160 program_io = io.StringIO()
161 with redirect_stdout(program_io):
162 timeout(timeout_length)(runtime.exec_code)('\n'.join(code))
163 program_io.seek(0)
164 result = program_io.read()
165 elif answer_symbol:
166 timeout(timeout_length)(runtime.exec_code)('\n'.join(code))
167 result = runtime._global_vars[answer_symbol]
168 elif answer_expr:
169 timeout(timeout_length)(runtime.exec_code)('\n'.join(code))
170 result = timeout(timeout_length)(runtime.eval_code)(answer_expr)
171 else:
172 timeout(timeout_length)(runtime.exec_code)('\n'.join(code[:-1]))
173 result = timeout(timeout_length)(runtime.eval_code)(code[-1])
174 report = 'Done'
175 str(result)
176 pickle.dumps(result) # serialization check
177 except Exception:
178 result = ''
179 report = traceback.format_exc().split('\n')[-2]
180 return result, report
181
182 @staticmethod
183 def truncate(s, max_length=256):

Callers 1

_execute_codeMethod · 0.45

Calls 2

timeoutClass · 0.85
splitMethod · 0.80

Tested by

no test coverage detected