Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

Add augmented assignment operator support to InternalPythonInterpreter (#3805)

Copilot7efbcbbd3eeb
diff --git a/camel/interpreters/internal_python_interpreter.py b/camel/interpreters/internal_python_interpreter.py
index 1d717c5b1..a5b39b144 100644
--- a/camel/interpreters/internal_python_interpreter.py
+++ b/camel/interpreters/internal_python_interpreter.py
@@ -14,6 +14,7 @@
import ast
import difflib
import importlib
+import operator
import os
import subprocess
import typing
@@ -294,6 +295,10 @@ class InternalPythonInterpreter(BaseInterpreter):
# update the state. We return the variable assigned as it may
# be used to determine the final result.
return self._execute_assign(expression)
+ elif isinstance(expression, ast.AugAssign):
+ # Augmented assignment (+=, -=, *=, etc.) -> update the state
+ # and return the new value
+ return self._execute_augassign(expression)
elif isinstance(expression, ast.Attribute):
value = self._execute_ast(expression.value)
return getattr(value, expression.attr)
@@ -396,6 +401,55 @@ class InternalPythonInterpreter(BaseInterpreter):
f"{target.__class__.__name__} instead."
)
+ def _execute_augassign(self, augassign: ast.AugAssign) -> Any:
+ # Get the current value of the target variable
+ target = augassign.target
+ if not isinstance(target, ast.Name):
+ raise InterpreterError(
+ f"Unsupported target for augmented assignment. "
+ f"Expected ast.Name, got {target.__class__.__name__} instead."
+ )
+
+ current_value = self._get_value_from_state(target.id)
+ operator_node = augassign.op
+ right_value = self._execute_ast(augassign.value)
+
+ # Apply the operation based on the operator type
+ if isinstance(operator_node, ast.Add):
+ result = operator.iadd(current_value, right_value)
+ elif isinstance(operator_node, ast.Sub):
+ result = operator.isub(current_value, right_value)
+ elif isinstance(operator_node, ast.Mult):
+ result = operator.imul(current_value, right_value)
+ elif isinstance(operator_node, ast.Div):
+ result = operator.itruediv(current_value, right_value)
+ elif isinstance(operator_node, ast.FloorDiv):
+ result = operator.ifloordiv(current_value, right_value)
+ elif isinstance(operator_node, ast.Mod):
+ result = operator.imod(current_value, right_value)
+ elif isinstance(operator_node, ast.Pow):
+ result = operator.ipow(current_value, right_value)
+ elif isinstance(operator_node, ast.LShift):
+ result = operator.ilshift(current_value, right_value)
+ elif isinstance(operator_node, ast.RShift):
+ result = operator.irshift(current_value, right_value)
+ elif isinstance(operator_node, ast.BitOr):
+ result = operator.ior(current_value, right_value)
+ elif isinstance(operator_node, ast.BitXor):
+ result = operator.ixor(current_value, right_value)
+ elif isinstance(operator_node, ast.BitAnd):
+ result = operator.iand(current_value, right_value)
+ elif isinstance(operator_node, ast.MatMult):
+ result = operator.imatmul(current_value, right_value)
+ else:
+ raise InterpreterError(
+ f"Unsupported augmented assignment operator: {operator_node}"
+ )
+
+ # Update the state with the new value
+ self.state[target.id] = result
+ return result
+
def _execute_call(self, call: ast.Call) -> Any:
callable_func = self._execute_ast(call.func)
diff --git a/test/interpreters/test_python_interpreter.py b/test/interpreters/test_python_interpreter.py
index f92cbfef3..cb14f9b20 100644
--- a/test/interpreters/test_python_interpreter.py
+++ b/test/interpreters/test_python_interpreter.py
@@ -286,16 +286,53 @@ res = ",".join(l)"""
assert execution_res == "2,3,5,7,11"
-def test_expression_not_support(interpreter: InternalPythonInterpreter):
- code = """x = 1
-x += 1"""
- with pytest.raises(InterpreterError) as e:
- interpreter.execute(code, keep_state=False)
- exec_msg = e.value.args[0]
- assert exec_msg == (
- "Evaluation of the code stopped at node 1. See:"
- "\nAugAssign is not supported."
+def test_augassign_operations(interpreter: InternalPythonInterpreter):
+ # Test various augmented assignment operations
+ code = """x = 10
+x += 5"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 15
+
+ code = """x = 10
+x -= 3"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 7
+
+ code = """x = 4
+x *= 3"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 12
+
+ code = """x = 20
+x /= 4"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 5.0
+
+ code = """x = 17
+x //= 5"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 3
+
+ code = """x = 17
+x %= 5"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 2
+
+ code = """x = 2
+x **= 3"""
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == 8
+
+ code = "\n".join(
+ [
+ "x = []",
+ "y = x",
+ "x += [1]",
+ "y",
+ ]
)
+ execution_res = interpreter.execute(code, keep_state=False)
+ assert execution_res == [1]
def test_allow_builtins(interpreter: InternalPythonInterpreter):