MCPcopy Create free account
hub / github.com/UCSB-AI/EvoPresent / run_code

Function run_code

Evopresent/utils/wei_utils.py:219–242  ·  view source on GitHub ↗

Execute Python code and capture stdout as well as the full stack trace on error. Forces __name__ = "__main__" so that if __name__ == "__main__": blocks will run. Returns: (output, error) - output: string containing everything that was printed to stdout - err

(code)

Source from the content-addressed store, hash-verified

217 return run_code(utils_functions + '\n' + code)
218
219def run_code(code):
220 """
221 Execute Python code and capture stdout as well as the full stack trace on error.
222 Forces __name__ = "__main__" so that if __name__ == "__main__": blocks will run.
223
224 Returns:
225 (output, error)
226 - output: string containing everything that was printed to stdout
227 - error: string containing the full traceback if an exception occurred; None otherwise
228 """
229 stdout_capture = io.StringIO()
230 # Provide a globals dict specifying that __name__ is "__main__"
231 exec_globals = {"__name__": "__main__"}
232
233 with contextlib.redirect_stdout(stdout_capture):
234 try:
235 exec(code, exec_globals)
236 error = None
237 except Exception:
238 # Capture the entire stack trace
239 error = traceback.format_exc()
240
241 output = stdout_capture.getvalue()
242 return output, error
243
244
245def run_code_from_agent(agent, msg, num_retries=1):

Callers 6

run_code_with_utilsFunction · 0.85
run_code_from_agentFunction · 0.85
run_modularFunction · 0.85
fill_contentFunction · 0.85
gen_layoutFunction · 0.85
gen_layout_parallelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected