MCPcopy
hub / github.com/huggingface/smolagents / _execute_code

Function _execute_code

src/smolagents/local_python_executor.py:1639–1661  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1637
1638 # Define the actual execution logic
1639 def _execute_code():
1640 result = None
1641 try:
1642 for node in expression.body:
1643 result = evaluate_ast(node, state, static_tools, custom_tools, authorized_imports)
1644 state["_print_outputs"].value = truncate_content(
1645 str(state["_print_outputs"]), max_length=max_print_outputs_length
1646 )
1647 is_final_answer = False
1648 return result, is_final_answer
1649 except FinalAnswerException as e:
1650 state["_print_outputs"].value = truncate_content(
1651 str(state["_print_outputs"]), max_length=max_print_outputs_length
1652 )
1653 is_final_answer = True
1654 return e.value, is_final_answer
1655 except Exception as e:
1656 state["_print_outputs"].value = truncate_content(
1657 str(state["_print_outputs"]), max_length=max_print_outputs_length
1658 )
1659 raise InterpreterError(
1660 f"Code execution failed at line '{ast.get_source_segment(code, node)}' due to: {type(e).__name__}: {e}"
1661 )
1662
1663 # Apply timeout if specified
1664 if timeout_seconds is not None:

Callers 1

evaluate_python_codeFunction · 0.85

Calls 3

evaluate_astFunction · 0.85
truncate_contentFunction · 0.85
InterpreterErrorClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…