Skip to content

Python Interpreter

Classes:

  • InterpreterError

    An error raised when the interpretor cannot evaluate a Python expression, due to syntax error or unsupported

Functions:

  • evaluate_ast

    Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given

  • evaluate_python_code

    Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set

InterpreterError

Bases: ValueError

An error raised when the interpretor cannot evaluate a Python expression, due to syntax error or unsupported operations.

Source code in tapeagents/tools/python_interpreter.py
29
30
31
32
33
34
35
class InterpreterError(ValueError):
    """
    An error raised when the interpretor cannot evaluate a Python expression, due to syntax error or unsupported
    operations.
    """

    pass

evaluate_ast(expression, state, tools, authorized_imports=LIST_SAFE_MODULES)

Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given set of functions.

This function will recurse trough the nodes of the tree provided.

Parameters:

  • expression (`ast.AST`) –

    The code to evaluate, as an abstract syntax tree.

  • state (`Dict[str, Any]`) –

    A dictionary mapping variable names to values. The state is updated if need be when the evaluation encounters assignements.

  • tools (`Dict[str, Callable]`) –

    The functions that may be called during the evaluation. Any call to another function will fail with an InterpreterError.

  • authorized_imports (`List[str]`, default: LIST_SAFE_MODULES ) –

    The list of modules that can be imported by the code. By default, only a few safe modules are allowed. Add more at your own risk!

Source code in tapeagents/tools/python_interpreter.py
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
def evaluate_ast(
    expression: ast.AST,
    state: Dict[str, Any],
    tools: Dict[str, Callable],
    authorized_imports: List[str] = LIST_SAFE_MODULES,
):
    """
    Evaluate an abstract syntax tree using the content of the variables stored in a state and only evaluating a given
    set of functions.

    This function will recurse trough the nodes of the tree provided.

    Args:
        expression (`ast.AST`):
            The code to evaluate, as an abstract syntax tree.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` is updated if need be when the evaluation
            encounters assignements.
        tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation. Any call to another function will fail with an
            `InterpreterError`.
        authorized_imports (`List[str]`):
            The list of modules that can be imported by the code. By default, only a few safe modules are allowed.
            Add more at your own risk!
    """
    if isinstance(expression, ast.Assign):
        # Assignement -> we evaluate the assignment which should update the state
        # We return the variable assigned as it may be used to determine the final result.
        return evaluate_assign(expression, state, tools)
    elif isinstance(expression, ast.AugAssign):
        return evaluate_augassign(expression, state, tools)
    elif isinstance(expression, ast.Call):
        # Function call -> we return the value of the function call
        return evaluate_call(expression, state, tools)
    elif isinstance(expression, ast.Constant):
        # Constant -> just return the value
        return expression.value
    elif isinstance(expression, ast.Tuple):
        return tuple(evaluate_ast(elt, state, tools) for elt in expression.elts)
    elif isinstance(expression, ast.ListComp):
        return evaluate_listcomp(expression, state, tools)
    elif isinstance(expression, ast.UnaryOp):
        return evaluate_unaryop(expression, state, tools)
    elif isinstance(expression, ast.BoolOp):
        # Boolean operation -> evaluate the operation
        return evaluate_boolop(expression, state, tools)
    elif isinstance(expression, ast.Break):
        raise BreakException()
    elif isinstance(expression, ast.Continue):
        raise ContinueException()
    elif isinstance(expression, ast.BinOp):
        # Binary operation -> execute operation
        return evaluate_binop(expression, state, tools)
    elif isinstance(expression, ast.Compare):
        # Comparison -> evaluate the comparison
        return evaluate_condition(expression, state, tools)
    elif isinstance(expression, ast.Lambda):
        return evaluate_lambda(expression, state, tools)
    elif isinstance(expression, ast.FunctionDef):
        return evaluate_function_def(expression, state, tools)
    elif isinstance(expression, ast.Dict):
        # Dict -> evaluate all keys and values
        keys = [evaluate_ast(k, state, tools) for k in expression.keys]
        values = [evaluate_ast(v, state, tools) for v in expression.values]
        return dict(zip(keys, values))
    elif isinstance(expression, ast.Expr):
        # Expression -> evaluate the content
        return evaluate_ast(expression.value, state, tools)
    elif isinstance(expression, ast.For):
        # For loop -> execute the loop
        return evaluate_for(expression, state, tools)
    elif isinstance(expression, ast.FormattedValue):
        # Formatted value (part of f-string) -> evaluate the content and return
        return evaluate_ast(expression.value, state, tools)
    elif isinstance(expression, ast.If):
        # If -> execute the right branch
        return evaluate_if(expression, state, tools)
    elif hasattr(ast, "Index") and isinstance(expression, ast.Index):
        return evaluate_ast(expression.value, state, tools)
    elif isinstance(expression, ast.JoinedStr):
        return "".join([str(evaluate_ast(v, state, tools)) for v in expression.values])
    elif isinstance(expression, ast.List):
        # List -> evaluate all elements
        return [evaluate_ast(elt, state, tools) for elt in expression.elts]
    elif isinstance(expression, ast.Name):
        # Name -> pick up the value in the state
        return evaluate_name(expression, state, tools)
    elif isinstance(expression, ast.Subscript):
        # Subscript -> return the value of the indexing
        return evaluate_subscript(expression, state, tools)
    elif isinstance(expression, ast.IfExp):
        test_val = evaluate_ast(expression.test, state, tools)
        if test_val:
            return evaluate_ast(expression.body, state, tools)
        else:
            return evaluate_ast(expression.orelse, state, tools)
    elif isinstance(expression, ast.Attribute):
        obj = evaluate_ast(expression.value, state, tools)
        return getattr(obj, expression.attr)
    elif isinstance(expression, ast.Slice):
        return slice(
            evaluate_ast(expression.lower, state, tools) if expression.lower is not None else None,
            evaluate_ast(expression.upper, state, tools) if expression.upper is not None else None,
            evaluate_ast(expression.step, state, tools) if expression.step is not None else None,
        )
    elif isinstance(expression, ast.ListComp) or isinstance(expression, ast.GeneratorExp):
        result = []
        vars = {}
        for generator in expression.generators:
            var_name = generator.target.id
            iter_value = evaluate_ast(generator.iter, state, tools)
            for value in iter_value:
                vars[var_name] = value
                if all(evaluate_ast(if_clause, {**state, **vars}, tools) for if_clause in generator.ifs):
                    elem = evaluate_ast(expression.elt, {**state, **vars}, tools)
                    result.append(elem)
        return result
    elif isinstance(expression, ast.DictComp):
        result = {}
        for gen in expression.generators:
            for container in get_iterable(evaluate_ast(gen.iter, state, tools)):
                state[gen.target.id] = container
                key = evaluate_ast(expression.key, state, tools)
                value = evaluate_ast(expression.value, state, tools)
                result[key] = value
        return result
    elif isinstance(expression, ast.Import):
        for alias in expression.names:
            if alias.name in authorized_imports:
                module = __import__(alias.name)
                state[alias.asname or alias.name] = module
            else:
                raise InterpreterError(f"Import of {alias.name} is not allowed.")
        return None
    elif isinstance(expression, ast.While):
        return evaluate_while(expression, state, tools)
    elif isinstance(expression, ast.ImportFrom):
        if expression.module in authorized_imports:
            module = __import__(expression.module)
            for alias in expression.names:
                state[alias.asname or alias.name] = getattr(module, alias.name)
        else:
            raise InterpreterError(f"Import from {expression.module} is not allowed.")
        return None
    elif isinstance(expression, ast.ClassDef):
        return evaluate_class_def(expression, state, tools)
    elif isinstance(expression, ast.Try):
        return evaluate_try(expression, state, tools)
    elif isinstance(expression, ast.Raise):
        return evaluate_raise(expression, state, tools)
    elif isinstance(expression, ast.Assert):
        return evaluate_assert(expression, state, tools)
    elif isinstance(expression, ast.With):
        return evaluate_with(expression, state, tools)
    elif isinstance(expression, ast.Set):
        return {evaluate_ast(elt, state, tools) for elt in expression.elts}
    elif isinstance(expression, ast.Return):
        raise ReturnException(evaluate_ast(expression.value, state, tools) if expression.value else None)
    else:
        # For now we refuse anything else. Let's add things as we need them.
        raise InterpreterError(f"{expression.__class__.__name__} is not supported.")

evaluate_python_code(code, tools={}, state=None, authorized_imports=AUTHORIZED_IMPORTS)

Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set of functions.

This function will recurse through the nodes of the tree provided.

Parameters:

  • code (`str`) –

    The code to evaluate.

  • tools (`Dict[str, Callable]`, default: {} ) –

    The functions that may be called during the evaluation. Any call to another function will fail with an InterpreterError.

  • state (`Dict[str, Any]`, default: None ) –

    A dictionary mapping variable names to values. The state should contain the initial inputs but will be updated by this function to contain all variables as they are evaluated. The print outputs will be stored in the state under the key 'print_outputs'.

Source code in tapeagents/tools/python_interpreter.py
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
def evaluate_python_code(
    code: str,
    tools: Optional[Dict[str, Callable]] = {},
    state=None,
    authorized_imports: List[str] = AUTHORIZED_IMPORTS,
):
    """
    Evaluate a python expression using the content of the variables stored in a state and only evaluating a given set
    of functions.

    This function will recurse through the nodes of the tree provided.

    Args:
        code (`str`):
            The code to evaluate.
        tools (`Dict[str, Callable]`):
            The functions that may be called during the evaluation. Any call to another function will fail with an
            `InterpreterError`.
        state (`Dict[str, Any]`):
            A dictionary mapping variable names to values. The `state` should contain the initial inputs but will be
            updated by this function to contain all variables as they are evaluated.
            The print outputs will be stored in the state under the key 'print_outputs'.
    """
    try:
        expression = ast.parse(code)
    except SyntaxError as e:
        raise SyntaxError(f"The code generated by the agent is not valid.\n{e}")
    if state is None:
        state = {}
    result = None
    global PRINT_OUTPUTS
    PRINT_OUTPUTS = ""
    for node in expression.body:
        try:
            result = evaluate_ast(node, state, tools, authorized_imports)
        except InterpreterError as e:
            msg = f"Evaluation stopped at line '{ast.get_source_segment(code, node)}' because of the following error:\n{e}"
            if len(PRINT_OUTPUTS) > 0:
                msg += f"Executing code yielded these outputs:\n{PRINT_OUTPUTS}\n====\n"
            raise InterpreterError(msg)
        finally:
            state["print_outputs"] = PRINT_OUTPUTS

    return result