quantalogic 0.59.3__py3-none-any.whl → 0.61.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- quantalogic/agent.py +268 -24
- quantalogic/agent_config.py +5 -5
- quantalogic/agent_factory.py +2 -2
- quantalogic/codeact/__init__.py +0 -0
- quantalogic/codeact/agent.py +499 -0
- quantalogic/codeact/cli.py +232 -0
- quantalogic/codeact/constants.py +9 -0
- quantalogic/codeact/events.py +78 -0
- quantalogic/codeact/llm_util.py +76 -0
- quantalogic/codeact/prompts/error_format.j2 +11 -0
- quantalogic/codeact/prompts/generate_action.j2 +26 -0
- quantalogic/codeact/prompts/generate_program.j2 +39 -0
- quantalogic/codeact/prompts/response_format.j2 +11 -0
- quantalogic/codeact/tools_manager.py +135 -0
- quantalogic/codeact/utils.py +135 -0
- quantalogic/coding_agent.py +2 -2
- quantalogic/create_custom_agent.py +26 -78
- quantalogic/prompts/chat_system_prompt.j2 +10 -7
- quantalogic/prompts/code_2_system_prompt.j2 +190 -0
- quantalogic/prompts/code_system_prompt.j2 +142 -0
- quantalogic/prompts/doc_system_prompt.j2 +178 -0
- quantalogic/prompts/legal_2_system_prompt.j2 +218 -0
- quantalogic/prompts/legal_system_prompt.j2 +140 -0
- quantalogic/prompts/system_prompt.j2 +6 -2
- quantalogic/prompts/tools_prompt.j2 +2 -4
- quantalogic/prompts.py +23 -4
- quantalogic/python_interpreter/__init__.py +23 -0
- quantalogic/python_interpreter/assignment_visitors.py +63 -0
- quantalogic/python_interpreter/base_visitors.py +20 -0
- quantalogic/python_interpreter/class_visitors.py +22 -0
- quantalogic/python_interpreter/comprehension_visitors.py +172 -0
- quantalogic/python_interpreter/context_visitors.py +59 -0
- quantalogic/python_interpreter/control_flow_visitors.py +88 -0
- quantalogic/python_interpreter/exception_visitors.py +109 -0
- quantalogic/python_interpreter/exceptions.py +39 -0
- quantalogic/python_interpreter/execution.py +202 -0
- quantalogic/python_interpreter/function_utils.py +386 -0
- quantalogic/python_interpreter/function_visitors.py +209 -0
- quantalogic/python_interpreter/import_visitors.py +28 -0
- quantalogic/python_interpreter/interpreter_core.py +358 -0
- quantalogic/python_interpreter/literal_visitors.py +74 -0
- quantalogic/python_interpreter/misc_visitors.py +148 -0
- quantalogic/python_interpreter/operator_visitors.py +108 -0
- quantalogic/python_interpreter/scope.py +10 -0
- quantalogic/python_interpreter/visit_handlers.py +110 -0
- quantalogic/server/agent_server.py +1 -1
- quantalogic/tools/__init__.py +6 -3
- quantalogic/tools/action_gen.py +366 -0
- quantalogic/tools/duckduckgo_search_tool.py +1 -0
- quantalogic/tools/execute_bash_command_tool.py +114 -57
- quantalogic/tools/file_tracker_tool.py +49 -0
- quantalogic/tools/google_packages/google_news_tool.py +3 -0
- quantalogic/tools/image_generation/dalle_e.py +89 -137
- quantalogic/tools/python_tool.py +13 -0
- quantalogic/tools/rag_tool/__init__.py +2 -9
- quantalogic/tools/rag_tool/document_rag_sources_.py +728 -0
- quantalogic/tools/rag_tool/ocr_pdf_markdown.py +144 -0
- quantalogic/tools/replace_in_file_tool.py +1 -1
- quantalogic/tools/{search_definition_names.py → search_definition_names_tool.py} +2 -2
- quantalogic/tools/terminal_capture_tool.py +293 -0
- quantalogic/tools/tool.py +120 -22
- quantalogic/tools/utilities/__init__.py +2 -0
- quantalogic/tools/utilities/download_file_tool.py +3 -5
- quantalogic/tools/utilities/llm_tool.py +283 -0
- quantalogic/tools/utilities/selenium_tool.py +296 -0
- quantalogic/tools/utilities/vscode_tool.py +1 -1
- quantalogic/tools/web_navigation/__init__.py +5 -0
- quantalogic/tools/web_navigation/web_tool.py +145 -0
- quantalogic/tools/write_file_tool.py +72 -36
- quantalogic/utils/__init__.py +0 -1
- quantalogic/utils/test_python_interpreter.py +119 -0
- {quantalogic-0.59.3.dist-info → quantalogic-0.61.0.dist-info}/METADATA +7 -2
- {quantalogic-0.59.3.dist-info → quantalogic-0.61.0.dist-info}/RECORD +76 -35
- quantalogic/tools/rag_tool/document_metadata.py +0 -15
- quantalogic/tools/rag_tool/query_response.py +0 -20
- quantalogic/tools/rag_tool/rag_tool.py +0 -566
- quantalogic/tools/rag_tool/rag_tool_beta.py +0 -264
- quantalogic/utils/python_interpreter.py +0 -905
- {quantalogic-0.59.3.dist-info → quantalogic-0.61.0.dist-info}/LICENSE +0 -0
- {quantalogic-0.59.3.dist-info → quantalogic-0.61.0.dist-info}/WHEEL +0 -0
- {quantalogic-0.59.3.dist-info → quantalogic-0.61.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,209 @@
|
|
1
|
+
import ast
|
2
|
+
import asyncio
|
3
|
+
import inspect
|
4
|
+
from typing import Any, Dict, List
|
5
|
+
|
6
|
+
from .exceptions import WrappedException
|
7
|
+
from .function_utils import AsyncFunction, Function, LambdaFunction, AsyncGeneratorFunction
|
8
|
+
from .interpreter_core import ASTInterpreter
|
9
|
+
|
10
|
+
async def visit_FunctionDef(self: ASTInterpreter, node: ast.FunctionDef, wrap_exceptions: bool = True) -> None:
|
11
|
+
closure: List[Dict[str, Any]] = self.env_stack[:]
|
12
|
+
pos_kw_params = [arg.arg for arg in node.args.args]
|
13
|
+
vararg_name = node.args.vararg.arg if node.args.vararg else None
|
14
|
+
kwonly_params = [arg.arg for arg in node.args.kwonlyargs]
|
15
|
+
kwarg_name = node.args.kwarg.arg if node.args.kwarg else None
|
16
|
+
pos_defaults_values = [await self.visit(default, wrap_exceptions=True) for default in node.args.defaults]
|
17
|
+
num_pos_defaults = len(pos_defaults_values)
|
18
|
+
pos_defaults = dict(zip(pos_kw_params[-num_pos_defaults:], pos_defaults_values)) if num_pos_defaults else {}
|
19
|
+
kw_defaults_values = [await self.visit(default, wrap_exceptions=True) if default else None for default in node.args.kw_defaults]
|
20
|
+
kw_defaults = dict(zip(kwonly_params, kw_defaults_values))
|
21
|
+
kw_defaults = {k: v for k, v in kw_defaults.items() if v is not None}
|
22
|
+
|
23
|
+
func = Function(node, closure, self, pos_kw_params, vararg_name, kwonly_params, kwarg_name, pos_defaults, kw_defaults)
|
24
|
+
decorated_func = func
|
25
|
+
for decorator in reversed(node.decorator_list):
|
26
|
+
dec = await self.visit(decorator, wrap_exceptions=True)
|
27
|
+
if asyncio.iscoroutine(dec):
|
28
|
+
dec = await dec
|
29
|
+
if dec in (staticmethod, classmethod, property):
|
30
|
+
decorated_func = dec(func)
|
31
|
+
else:
|
32
|
+
decorated_func = await self._execute_function(dec, [decorated_func], {})
|
33
|
+
self.set_variable(node.name, decorated_func)
|
34
|
+
|
35
|
+
async def visit_AsyncFunctionDef(self: ASTInterpreter, node: ast.AsyncFunctionDef, wrap_exceptions: bool = True) -> None:
|
36
|
+
closure: List[Dict[str, Any]] = self.env_stack[:]
|
37
|
+
pos_kw_params = [arg.arg for arg in node.args.args]
|
38
|
+
vararg_name = node.args.vararg.arg if node.args.vararg else None
|
39
|
+
kwonly_params = [arg.arg for arg in node.args.kwonlyargs]
|
40
|
+
kwarg_name = node.args.kwarg.arg if node.args.kwarg else None
|
41
|
+
pos_defaults_values = [await self.visit(default, wrap_exceptions=True) for default in node.args.defaults]
|
42
|
+
num_pos_defaults = len(pos_defaults_values)
|
43
|
+
pos_defaults = dict(zip(pos_kw_params[-num_pos_defaults:], pos_defaults_values)) if num_pos_defaults else {}
|
44
|
+
kw_defaults_values = [await self.visit(default, wrap_exceptions=True) if default else None for default in node.args.kw_defaults]
|
45
|
+
kw_defaults = dict(zip(kwonly_params, kw_defaults_values))
|
46
|
+
kw_defaults = {k: v for k, v in kw_defaults.items() if v is not None}
|
47
|
+
|
48
|
+
func = AsyncFunction(node, closure, self, pos_kw_params, vararg_name, kwonly_params, kwarg_name, pos_defaults, kw_defaults)
|
49
|
+
for decorator in reversed(node.decorator_list):
|
50
|
+
dec = await self.visit(decorator, wrap_exceptions=True)
|
51
|
+
if asyncio.iscoroutine(dec):
|
52
|
+
dec = await dec
|
53
|
+
func = await self._execute_function(dec, [func], {})
|
54
|
+
self.set_variable(node.name, func)
|
55
|
+
|
56
|
+
async def visit_AsyncGeneratorDef(self: ASTInterpreter, node: ast.AsyncFunctionDef, wrap_exceptions: bool = True) -> None:
|
57
|
+
closure: List[Dict[str, Any]] = self.env_stack[:]
|
58
|
+
pos_kw_params = [arg.arg for arg in node.args.args]
|
59
|
+
vararg_name = node.args.vararg.arg if node.args.vararg else None
|
60
|
+
kwonly_params = [arg.arg for arg in node.args.kwonlyargs]
|
61
|
+
kwarg_name = node.args.kwarg.arg if node.args.kwarg else None
|
62
|
+
pos_defaults_values = [await self.visit(default, wrap_exceptions=True) for default in node.args.defaults]
|
63
|
+
num_pos_defaults = len(pos_defaults_values)
|
64
|
+
pos_defaults = dict(zip(pos_kw_params[-num_pos_defaults:], pos_defaults_values)) if num_pos_defaults else {}
|
65
|
+
kw_defaults_values = [await self.visit(default, wrap_exceptions=True) if default else None for default in node.args.kw_defaults]
|
66
|
+
kw_defaults = dict(zip(kwonly_params, kw_defaults_values))
|
67
|
+
kw_defaults = {k: v for k, v in kw_defaults.items() if v is not None}
|
68
|
+
|
69
|
+
func = AsyncGeneratorFunction(node, closure, self, pos_kw_params, vararg_name, kwonly_params, kwarg_name, pos_defaults, kw_defaults)
|
70
|
+
for decorator in reversed(node.decorator_list):
|
71
|
+
dec = await self.visit(decorator, wrap_exceptions=True)
|
72
|
+
if asyncio.iscoroutine(dec):
|
73
|
+
dec = await dec
|
74
|
+
func = await self._execute_function(dec, [func], {})
|
75
|
+
self.set_variable(node.name, func)
|
76
|
+
|
77
|
+
async def visit_Call(self: ASTInterpreter, node: ast.Call, is_await_context: bool = False, wrap_exceptions: bool = True) -> Any:
|
78
|
+
func = await self.visit(node.func, wrap_exceptions=wrap_exceptions)
|
79
|
+
|
80
|
+
evaluated_args: List[Any] = []
|
81
|
+
for arg in node.args:
|
82
|
+
arg_value = await self.visit(arg, wrap_exceptions=wrap_exceptions)
|
83
|
+
if isinstance(arg, ast.Starred):
|
84
|
+
evaluated_args.extend(arg_value)
|
85
|
+
else:
|
86
|
+
evaluated_args.append(arg_value)
|
87
|
+
|
88
|
+
kwargs: Dict[str, Any] = {}
|
89
|
+
for kw in node.keywords:
|
90
|
+
if kw.arg is None:
|
91
|
+
unpacked_kwargs = await self.visit(kw.value, wrap_exceptions=wrap_exceptions)
|
92
|
+
if not isinstance(unpacked_kwargs, dict):
|
93
|
+
raise TypeError(f"** argument must be a mapping, not {type(unpacked_kwargs).__name__}")
|
94
|
+
kwargs.update(unpacked_kwargs)
|
95
|
+
else:
|
96
|
+
kwargs[kw.arg] = await self.visit(kw.value, wrap_exceptions=wrap_exceptions)
|
97
|
+
|
98
|
+
# Handle str() explicitly to avoid __new__ restriction
|
99
|
+
if func is str:
|
100
|
+
if len(evaluated_args) != 1:
|
101
|
+
raise TypeError(f"str() takes exactly one argument ({len(evaluated_args)} given)")
|
102
|
+
arg = evaluated_args[0]
|
103
|
+
return f"{arg}" # Use f-string to safely convert to string
|
104
|
+
|
105
|
+
# Handle exceptions passed to str() (original behavior preserved)
|
106
|
+
if func is str and len(evaluated_args) == 1 and isinstance(evaluated_args[0], BaseException):
|
107
|
+
exc = evaluated_args[0]
|
108
|
+
if isinstance(exc, WrappedException) and hasattr(exc, 'original_exception'):
|
109
|
+
inner_exc = exc.original_exception
|
110
|
+
return inner_exc.args[0] if inner_exc.args else str(inner_exc)
|
111
|
+
return exc.args[0] if exc.args else str(exc)
|
112
|
+
|
113
|
+
if func is super:
|
114
|
+
if len(evaluated_args) == 0:
|
115
|
+
if not (self.current_class and self.current_instance):
|
116
|
+
# Infer from call stack if possible
|
117
|
+
for frame in reversed(self.env_stack):
|
118
|
+
if 'self' in frame and '__current_method__' in frame:
|
119
|
+
self.current_instance = frame['self']
|
120
|
+
self.current_class = frame['self'].__class__
|
121
|
+
break
|
122
|
+
if not (self.current_class and self.current_instance):
|
123
|
+
raise TypeError("super() without arguments requires a class instantiation context")
|
124
|
+
result = super(self.current_class, self.current_instance)
|
125
|
+
elif len(evaluated_args) >= 2:
|
126
|
+
cls, obj = evaluated_args[0], evaluated_args[1]
|
127
|
+
result = super(cls, obj)
|
128
|
+
else:
|
129
|
+
raise TypeError("super() requires class and instance arguments")
|
130
|
+
return result
|
131
|
+
|
132
|
+
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Call) and isinstance(node.func.value.func, ast.Name) and node.func.value.func.id == 'super':
|
133
|
+
super_call = await self.visit(node.func.value, wrap_exceptions=wrap_exceptions)
|
134
|
+
method = getattr(super_call, node.func.attr)
|
135
|
+
return await self._execute_function(method, evaluated_args, kwargs)
|
136
|
+
|
137
|
+
if func is list and len(evaluated_args) == 1 and hasattr(evaluated_args[0], '__aiter__'):
|
138
|
+
return [val async for val in evaluated_args[0]]
|
139
|
+
|
140
|
+
if func in (range, list, dict, set, tuple, frozenset):
|
141
|
+
return func(*evaluated_args, **kwargs)
|
142
|
+
|
143
|
+
if inspect.isclass(func):
|
144
|
+
instance = await self._create_class_instance(func, *evaluated_args, **kwargs)
|
145
|
+
return instance
|
146
|
+
|
147
|
+
if isinstance(func, (staticmethod, classmethod, property)):
|
148
|
+
if isinstance(func, property):
|
149
|
+
result = func.fget(*evaluated_args, **kwargs)
|
150
|
+
else:
|
151
|
+
result = func(*evaluated_args, **kwargs)
|
152
|
+
elif asyncio.iscoroutinefunction(func) or isinstance(func, AsyncFunction):
|
153
|
+
result = func(*evaluated_args, **kwargs)
|
154
|
+
if not is_await_context:
|
155
|
+
result = await result
|
156
|
+
elif isinstance(func, Function):
|
157
|
+
if func.node.name == "__init__":
|
158
|
+
await func(*evaluated_args, **kwargs)
|
159
|
+
return None
|
160
|
+
result = await func(*evaluated_args, **kwargs)
|
161
|
+
elif isinstance(func, AsyncGeneratorFunction):
|
162
|
+
return func(*evaluated_args, **kwargs)
|
163
|
+
else:
|
164
|
+
result = func(*evaluated_args, **kwargs)
|
165
|
+
if asyncio.iscoroutine(result) and not is_await_context:
|
166
|
+
result = await result
|
167
|
+
return result
|
168
|
+
|
169
|
+
async def visit_Await(self: ASTInterpreter, node: ast.Await, wrap_exceptions: bool = True) -> Any:
|
170
|
+
coro = await self.visit(node.value, is_await_context=True, wrap_exceptions=wrap_exceptions)
|
171
|
+
if not asyncio.iscoroutine(coro):
|
172
|
+
raise TypeError(f"Cannot await non-coroutine object: {type(coro)}")
|
173
|
+
|
174
|
+
try:
|
175
|
+
return await asyncio.wait_for(coro, timeout=60)
|
176
|
+
except asyncio.TimeoutError as e:
|
177
|
+
line_info = f"line {node.lineno}" if hasattr(node, "lineno") else "unknown line"
|
178
|
+
context_line = self.source_lines[node.lineno - 1] if self.source_lines and hasattr(node, "lineno") else "<unknown>"
|
179
|
+
error_msg = f"Operation timed out after 60 seconds at {line_info}: {context_line.strip()}"
|
180
|
+
logger_msg = f"Coroutine execution timed out: {error_msg}"
|
181
|
+
self.env_stack[0]["logger"].error(logger_msg)
|
182
|
+
|
183
|
+
if wrap_exceptions:
|
184
|
+
col = getattr(node, "col_offset", 0)
|
185
|
+
raise WrappedException(error_msg, e, node.lineno if hasattr(node, "lineno") else 0, col, context_line)
|
186
|
+
else:
|
187
|
+
raise asyncio.TimeoutError(error_msg) from e
|
188
|
+
|
189
|
+
async def visit_Lambda(self: ASTInterpreter, node: ast.Lambda, wrap_exceptions: bool = True) -> Any:
|
190
|
+
closure: List[Dict[str, Any]] = self.env_stack[:]
|
191
|
+
pos_kw_params = [arg.arg for arg in node.args.args]
|
192
|
+
vararg_name = node.args.vararg.arg if node.args.vararg else None
|
193
|
+
kwonly_params = [arg.arg for arg in node.args.kwonlyargs]
|
194
|
+
kwarg_name = node.args.kwarg.arg if node.args.kwarg else None
|
195
|
+
pos_defaults_values = [await self.visit(default, wrap_exceptions=True) for default in node.args.defaults]
|
196
|
+
num_pos_defaults = len(pos_defaults_values)
|
197
|
+
pos_defaults = dict(zip(pos_kw_params[-num_pos_defaults:], pos_defaults_values)) if num_pos_defaults else {}
|
198
|
+
kw_defaults_values = [await self.visit(default, wrap_exceptions=True) if default else None for default in node.args.kw_defaults]
|
199
|
+
kw_defaults = dict(zip(kwonly_params, kw_defaults_values))
|
200
|
+
kw_defaults = {k: v for k, v in kw_defaults.items() if v is not None}
|
201
|
+
|
202
|
+
lambda_func = LambdaFunction(
|
203
|
+
node, closure, self, pos_kw_params, vararg_name, kwonly_params, kwarg_name, pos_defaults, kw_defaults
|
204
|
+
)
|
205
|
+
|
206
|
+
async def lambda_wrapper(*args, **kwargs):
|
207
|
+
return await lambda_func(*args, **kwargs)
|
208
|
+
|
209
|
+
return lambda_wrapper
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import ast
|
2
|
+
from typing import Any
|
3
|
+
|
4
|
+
from .interpreter_core import ASTInterpreter
|
5
|
+
|
6
|
+
async def visit_Import(self: ASTInterpreter, node: ast.Import, wrap_exceptions: bool = True) -> None:
|
7
|
+
for alias in node.names:
|
8
|
+
module_name: str = alias.name
|
9
|
+
asname: str = alias.asname if alias.asname is not None else module_name
|
10
|
+
if module_name not in self.allowed_modules:
|
11
|
+
raise Exception(
|
12
|
+
f"Import Error: Module '{module_name}' is not allowed. Only {self.allowed_modules} are permitted."
|
13
|
+
)
|
14
|
+
self.set_variable(asname, self.modules[module_name])
|
15
|
+
|
16
|
+
async def visit_ImportFrom(self: ASTInterpreter, node: ast.ImportFrom, wrap_exceptions: bool = True) -> None:
|
17
|
+
if not node.module:
|
18
|
+
raise Exception("Import Error: Missing module name in 'from ... import ...' statement")
|
19
|
+
if node.module not in self.allowed_modules:
|
20
|
+
raise Exception(
|
21
|
+
f"Import Error: Module '{node.module}' is not allowed. Only {self.allowed_modules} are permitted."
|
22
|
+
)
|
23
|
+
for alias in node.names:
|
24
|
+
if alias.name == "*":
|
25
|
+
raise Exception("Import Error: 'from ... import *' is not supported.")
|
26
|
+
asname = alias.asname if alias.asname else alias.name
|
27
|
+
attr = getattr(self.modules[node.module], alias.name)
|
28
|
+
self.set_variable(asname, attr)
|
@@ -0,0 +1,358 @@
|
|
1
|
+
import ast
|
2
|
+
import asyncio
|
3
|
+
import builtins
|
4
|
+
import logging
|
5
|
+
import threading
|
6
|
+
from typing import Any, Callable, Dict, List, Optional, Tuple
|
7
|
+
|
8
|
+
import psutil # New dependency for resource monitoring
|
9
|
+
|
10
|
+
from .exceptions import BreakException, ContinueException, ReturnException, WrappedException
|
11
|
+
from .scope import Scope
|
12
|
+
|
13
|
+
|
14
|
+
class ASTInterpreter:
|
15
|
+
def __init__(
|
16
|
+
self,
|
17
|
+
allowed_modules: List[str],
|
18
|
+
env_stack: Optional[List[Dict[str, Any]]] = None,
|
19
|
+
source: Optional[str] = None,
|
20
|
+
restrict_os: bool = True,
|
21
|
+
namespace: Optional[Dict[str, Any]] = None,
|
22
|
+
max_recursion_depth: int = 1000, # Now configurable
|
23
|
+
max_operations: int = 10000000, # Added operation limit
|
24
|
+
max_memory_mb: int = 1024, # Added memory limit (1GB default)
|
25
|
+
sync_mode: bool = False, # Added for sync execution optimization
|
26
|
+
safe_builtins: Optional[Dict[str, Any]] = None # New customizable safe_builtins
|
27
|
+
) -> None:
|
28
|
+
self.allowed_modules: List[str] = allowed_modules
|
29
|
+
self.modules: Dict[str, Any] = {mod: __import__(mod) for mod in allowed_modules}
|
30
|
+
self.restrict_os: bool = restrict_os
|
31
|
+
self.sync_mode: bool = sync_mode
|
32
|
+
self.max_operations: int = max_operations
|
33
|
+
self.operations_count: int = 0
|
34
|
+
self.max_memory_mb: int = max_memory_mb
|
35
|
+
self.process = psutil.Process() # For memory monitoring
|
36
|
+
self.type_hints: Dict[str, Any] = {} # Added for type aliases
|
37
|
+
self.special_methods: Dict[str, Callable] = {} # Added for special method dispatch
|
38
|
+
|
39
|
+
# Default safe_builtins if none provided
|
40
|
+
default_safe_builtins: Dict[str, Any] = {
|
41
|
+
'None': None,
|
42
|
+
'False': False,
|
43
|
+
'True': True,
|
44
|
+
'int': int,
|
45
|
+
'float': float,
|
46
|
+
'str': str,
|
47
|
+
'list': list,
|
48
|
+
'dict': dict,
|
49
|
+
'set': set,
|
50
|
+
'tuple': tuple,
|
51
|
+
'bool': bool,
|
52
|
+
'object': object,
|
53
|
+
'range': range,
|
54
|
+
'iter': iter, # Added for test_iterators
|
55
|
+
'next': next, # Added to fix test_iterators
|
56
|
+
'sorted': sorted, # Added for test_dictionary_methods
|
57
|
+
'frozenset': frozenset, # Added for test_frozenset
|
58
|
+
'super': super, # Added for test_class_inheritance
|
59
|
+
'len': len, # Added for test_empty_structures
|
60
|
+
'property': property, # Added for test_property_decorator
|
61
|
+
'staticmethod': staticmethod, # Added for test_static_method
|
62
|
+
'classmethod': classmethod, # Added for test_class_method
|
63
|
+
'__name__': '__main__', # Simulate script execution context
|
64
|
+
}
|
65
|
+
|
66
|
+
# Use provided safe_builtins or default
|
67
|
+
self.safe_builtins = safe_builtins if safe_builtins is not None else default_safe_builtins
|
68
|
+
|
69
|
+
if env_stack is None:
|
70
|
+
self.env_stack: List[Dict[str, Any]] = [{}]
|
71
|
+
self.env_stack[0].update(self.modules)
|
72
|
+
|
73
|
+
# Define explicitly allowed built-in functions
|
74
|
+
allowed_builtins = {
|
75
|
+
"enumerate": enumerate,
|
76
|
+
"zip": zip,
|
77
|
+
"sum": sum,
|
78
|
+
"min": min,
|
79
|
+
"max": max,
|
80
|
+
"abs": abs,
|
81
|
+
"round": round,
|
82
|
+
"str": str,
|
83
|
+
"repr": repr,
|
84
|
+
"id": id,
|
85
|
+
"Exception": Exception,
|
86
|
+
"ZeroDivisionError": ZeroDivisionError,
|
87
|
+
"ValueError": ValueError,
|
88
|
+
"TypeError": TypeError,
|
89
|
+
"print": print,
|
90
|
+
"getattr": self.safe_getattr,
|
91
|
+
"vars": lambda obj=None: vars(obj) if obj else dict(self.env_stack[-1]),
|
92
|
+
}
|
93
|
+
|
94
|
+
# Update safe_builtins with allowed functions and restricted import
|
95
|
+
self.safe_builtins.update(allowed_builtins)
|
96
|
+
self.safe_builtins["__import__"] = self.safe_import
|
97
|
+
|
98
|
+
# Set the restricted builtins in the environment
|
99
|
+
self.env_stack[0]["__builtins__"] = self.safe_builtins
|
100
|
+
self.env_stack[0].update(self.safe_builtins)
|
101
|
+
self.env_stack[0]["logger"] = logging.getLogger(__name__)
|
102
|
+
|
103
|
+
# Add the provided namespace (e.g., tools) to the environment
|
104
|
+
if namespace is not None:
|
105
|
+
self.env_stack[0].update(namespace)
|
106
|
+
else:
|
107
|
+
self.env_stack = env_stack
|
108
|
+
if "__builtins__" not in self.env_stack[0]:
|
109
|
+
# Define explicitly allowed built-in functions
|
110
|
+
allowed_builtins = {
|
111
|
+
"enumerate": enumerate,
|
112
|
+
"zip": zip,
|
113
|
+
"sum": sum,
|
114
|
+
"min": min,
|
115
|
+
"max": max,
|
116
|
+
"abs": abs,
|
117
|
+
"round": round,
|
118
|
+
"str": str,
|
119
|
+
"repr": repr,
|
120
|
+
"id": id,
|
121
|
+
"Exception": Exception,
|
122
|
+
"ZeroDivisionError": ZeroDivisionError,
|
123
|
+
"ValueError": ValueError,
|
124
|
+
"TypeError": TypeError,
|
125
|
+
"print": print,
|
126
|
+
"getattr": self.safe_getattr,
|
127
|
+
"vars": lambda obj=None: vars(obj) if obj else dict(self.env_stack[-1]),
|
128
|
+
}
|
129
|
+
|
130
|
+
# Update safe_builtins with allowed functions and restricted import
|
131
|
+
self.safe_builtins.update(allowed_builtins)
|
132
|
+
self.safe_builtins["__import__"] = self.safe_import
|
133
|
+
|
134
|
+
# Set the restricted builtins in the environment
|
135
|
+
self.env_stack[0]["__builtins__"] = self.safe_builtins
|
136
|
+
self.env_stack[0].update(self.safe_builtins)
|
137
|
+
self.env_stack[0]["logger"] = logging.getLogger(__name__)
|
138
|
+
|
139
|
+
if namespace is not None:
|
140
|
+
self.env_stack[0].update(namespace)
|
141
|
+
|
142
|
+
if self.restrict_os:
|
143
|
+
os_related_modules = {"os", "sys", "subprocess", "shutil", "platform"}
|
144
|
+
for mod in os_related_modules:
|
145
|
+
if mod in self.modules:
|
146
|
+
del self.modules[mod]
|
147
|
+
for mod in list(self.allowed_modules):
|
148
|
+
if mod in os_related_modules:
|
149
|
+
self.allowed_modules.remove(mod)
|
150
|
+
|
151
|
+
self.source_lines: Optional[List[str]] = source.splitlines() if source else None
|
152
|
+
self.var_cache: Dict[str, Any] = {}
|
153
|
+
self.recursion_depth: int = 0
|
154
|
+
self.max_recursion_depth: int = max_recursion_depth
|
155
|
+
self.loop = None
|
156
|
+
self.current_class = None
|
157
|
+
self.current_instance = None
|
158
|
+
self.current_exception = None
|
159
|
+
self.last_exception = None
|
160
|
+
self.lock = threading.Lock() # Added for thread safety
|
161
|
+
|
162
|
+
if "decimal" in self.modules:
|
163
|
+
dec = self.modules["decimal"]
|
164
|
+
self.env_stack[0]["Decimal"] = dec.Decimal
|
165
|
+
self.env_stack[0]["getcontext"] = dec.getcontext
|
166
|
+
self.env_stack[0]["setcontext"] = dec.setcontext
|
167
|
+
self.env_stack[0]["localcontext"] = dec.localcontext
|
168
|
+
self.env_stack[0]["Context"] = dec.Context
|
169
|
+
|
170
|
+
from . import visit_handlers
|
171
|
+
for handler_name in visit_handlers.__all__:
|
172
|
+
handler = getattr(visit_handlers, handler_name)
|
173
|
+
setattr(self, handler_name, handler.__get__(self, ASTInterpreter))
|
174
|
+
|
175
|
+
def safe_import(self, name: str, globals=None, locals=None, fromlist=(), level=0) -> Any:
|
176
|
+
os_related_modules = {"os", "sys", "subprocess", "shutil", "platform"}
|
177
|
+
if self.restrict_os and name in os_related_modules:
|
178
|
+
raise ImportError(f"Import Error: Module '{name}' is blocked due to OS restriction.")
|
179
|
+
if name not in self.allowed_modules:
|
180
|
+
raise ImportError(f"Import Error: Module '{name}' is not allowed. Only {self.allowed_modules} are permitted.")
|
181
|
+
return self.modules[name]
|
182
|
+
|
183
|
+
def safe_getattr(self, obj: Any, name: str, default: Any = None) -> Any:
|
184
|
+
if name.startswith('__') and name.endswith('__') and name not in ['__init__', '__call__']:
|
185
|
+
raise AttributeError(f"Access to dunder attribute '{name}' is restricted.")
|
186
|
+
return getattr(obj, name, default)
|
187
|
+
|
188
|
+
def spawn_from_env(self, env_stack: List[Dict[str, Any]]) -> "ASTInterpreter":
|
189
|
+
new_interp = ASTInterpreter(
|
190
|
+
self.allowed_modules,
|
191
|
+
env_stack,
|
192
|
+
source="\n".join(self.source_lines) if self.source_lines else None,
|
193
|
+
restrict_os=self.restrict_os,
|
194
|
+
max_recursion_depth=self.max_recursion_depth,
|
195
|
+
max_operations=self.max_operations,
|
196
|
+
max_memory_mb=self.max_memory_mb,
|
197
|
+
sync_mode=self.sync_mode,
|
198
|
+
safe_builtins=self.safe_builtins # Pass along the customized safe_builtins
|
199
|
+
)
|
200
|
+
new_interp.loop = self.loop
|
201
|
+
new_interp.var_cache = self.var_cache.copy()
|
202
|
+
return new_interp
|
203
|
+
|
204
|
+
def get_variable(self, name: str) -> Any:
|
205
|
+
with self.lock:
|
206
|
+
if name in self.var_cache:
|
207
|
+
return self.var_cache[name]
|
208
|
+
for frame in reversed(self.env_stack):
|
209
|
+
if name in frame:
|
210
|
+
self.var_cache[name] = frame[name]
|
211
|
+
return frame[name]
|
212
|
+
raise NameError(f"Name '{name}' is not defined.")
|
213
|
+
|
214
|
+
def set_variable(self, name: str, value: Any) -> None:
|
215
|
+
with self.lock:
|
216
|
+
if "__global_names__" in self.env_stack[-1] and name in self.env_stack[-1]["__global_names__"]:
|
217
|
+
self.env_stack[0][name] = value
|
218
|
+
if name in self.var_cache:
|
219
|
+
del self.var_cache[name]
|
220
|
+
elif "__nonlocal_names__" in self.env_stack[-1] and name in self.env_stack[-1]["__nonlocal_names__"]:
|
221
|
+
for frame in reversed(self.env_stack[:-1]):
|
222
|
+
if name in frame:
|
223
|
+
frame[name] = value
|
224
|
+
if name in self.var_cache:
|
225
|
+
del self.var_cache[name]
|
226
|
+
return
|
227
|
+
raise NameError(f"Nonlocal name '{name}' not found in outer scope")
|
228
|
+
else:
|
229
|
+
self.env_stack[-1][name] = value
|
230
|
+
if name in self.var_cache:
|
231
|
+
del self.var_cache[name]
|
232
|
+
|
233
|
+
async def assign(self, target: ast.AST, value: Any) -> None:
|
234
|
+
if isinstance(target, ast.Name):
|
235
|
+
self.set_variable(target.id, value)
|
236
|
+
elif isinstance(target, (ast.Tuple, ast.List)):
|
237
|
+
star_index = None
|
238
|
+
for i, elt in enumerate(target.elts):
|
239
|
+
if isinstance(elt, ast.Starred):
|
240
|
+
if star_index is not None:
|
241
|
+
raise Exception("Multiple starred expressions not supported")
|
242
|
+
star_index = i
|
243
|
+
if star_index is None:
|
244
|
+
if len(target.elts) != len(value):
|
245
|
+
raise ValueError("Unpacking mismatch")
|
246
|
+
for t, v in zip(target.elts, value):
|
247
|
+
await self.assign(t, v)
|
248
|
+
else:
|
249
|
+
total = len(value)
|
250
|
+
before = target.elts[:star_index]
|
251
|
+
after = target.elts[star_index + 1:]
|
252
|
+
if len(before) + len(after) > total:
|
253
|
+
raise ValueError("Unpacking mismatch")
|
254
|
+
for i, elt2 in enumerate(before):
|
255
|
+
await self.assign(elt2, value[i])
|
256
|
+
starred_count = total - len(before) - len(after)
|
257
|
+
await self.assign(target.elts[star_index].value, value[len(before):len(before) + starred_count])
|
258
|
+
for j, elt2 in enumerate(after):
|
259
|
+
await self.assign(elt2, value[len(before) + starred_count + j])
|
260
|
+
elif isinstance(target, ast.Attribute):
|
261
|
+
obj = await self.visit(target.value, wrap_exceptions=True)
|
262
|
+
setattr(obj, target.attr, value)
|
263
|
+
elif isinstance(target, ast.Subscript):
|
264
|
+
obj = await self.visit(target.value, wrap_exceptions=True)
|
265
|
+
key = await self.visit(target.slice, wrap_exceptions=True)
|
266
|
+
obj[key] = value
|
267
|
+
else:
|
268
|
+
raise Exception("Unsupported assignment target type: " + str(type(target)))
|
269
|
+
|
270
|
+
async def visit(self, node: ast.AST, is_await_context: bool = False, wrap_exceptions: bool = True) -> Any:
|
271
|
+
self.operations_count += 1
|
272
|
+
if self.operations_count > self.max_operations:
|
273
|
+
raise RuntimeError(f"Exceeded maximum operations ({self.max_operations})")
|
274
|
+
memory_usage = self.process.memory_info().rss / 1024 / 1024 # MB
|
275
|
+
if memory_usage > self.max_memory_mb:
|
276
|
+
raise MemoryError(f"Memory usage exceeded limit ({self.max_memory_mb} MB)")
|
277
|
+
|
278
|
+
self.recursion_depth += 1
|
279
|
+
if self.recursion_depth > self.max_recursion_depth:
|
280
|
+
raise RecursionError(f"Maximum recursion depth exceeded ({self.max_recursion_depth})")
|
281
|
+
|
282
|
+
method_name: str = "visit_" + node.__class__.__name__
|
283
|
+
method = getattr(self, method_name, self.generic_visit)
|
284
|
+
self.env_stack[0]["logger"].debug(f"Visiting {method_name} at line {getattr(node, 'lineno', 'unknown')}")
|
285
|
+
|
286
|
+
try:
|
287
|
+
if self.sync_mode and not hasattr(node, 'await'): # Optimize for sync code
|
288
|
+
result = method(node, wrap_exceptions=wrap_exceptions)
|
289
|
+
elif method_name == "visit_Call":
|
290
|
+
result = await method(node, is_await_context, wrap_exceptions)
|
291
|
+
else:
|
292
|
+
result = await method(node, wrap_exceptions=wrap_exceptions)
|
293
|
+
self.recursion_depth -= 1
|
294
|
+
return result
|
295
|
+
except (ReturnException, BreakException, ContinueException):
|
296
|
+
self.recursion_depth -= 1
|
297
|
+
raise
|
298
|
+
except Exception as e:
|
299
|
+
self.recursion_depth -= 1
|
300
|
+
if not wrap_exceptions:
|
301
|
+
raise
|
302
|
+
lineno = getattr(node, "lineno", None) or 1
|
303
|
+
col = getattr(node, "col_offset", None) or 0
|
304
|
+
context_line = self.source_lines[lineno - 1] if self.source_lines and 1 <= lineno <= len(self.source_lines) else ""
|
305
|
+
raise WrappedException(
|
306
|
+
f"Error line {lineno}, col {col}:\n{context_line}\nDescription: {str(e)}", e, lineno, col, context_line
|
307
|
+
) from e
|
308
|
+
|
309
|
+
async def generic_visit(self, node: ast.AST, wrap_exceptions: bool = True) -> Any:
|
310
|
+
lineno = getattr(node, "lineno", None) or 1
|
311
|
+
context_line = self.source_lines[lineno - 1] if self.source_lines and 1 <= lineno <= len(self.source_lines) else ""
|
312
|
+
raise Exception(
|
313
|
+
f"Unsupported AST node type: {node.__class__.__name__} at line {lineno}.\nContext: {context_line}"
|
314
|
+
)
|
315
|
+
|
316
|
+
async def execute_async(self, node: ast.Module) -> Any:
|
317
|
+
return await self.visit(node)
|
318
|
+
|
319
|
+
def new_scope(self):
|
320
|
+
return Scope(self.env_stack)
|
321
|
+
|
322
|
+
async def _resolve_exception_type(self, node: Optional[ast.AST]) -> Any:
|
323
|
+
if node is None:
|
324
|
+
return Exception
|
325
|
+
if isinstance(node, ast.Name):
|
326
|
+
exc_type = self.get_variable(node.id)
|
327
|
+
if exc_type in (Exception, ZeroDivisionError, ValueError, TypeError):
|
328
|
+
return exc_type
|
329
|
+
return exc_type
|
330
|
+
if isinstance(node, ast.Call):
|
331
|
+
return await self.visit(node, wrap_exceptions=True)
|
332
|
+
return None
|
333
|
+
|
334
|
+
async def _create_class_instance(self, cls: type, *args, **kwargs):
|
335
|
+
if cls in (super, Exception, BaseException) or issubclass(cls, BaseException):
|
336
|
+
instance = cls.__new__(cls, *args, **kwargs)
|
337
|
+
if hasattr(instance, '__init__'):
|
338
|
+
init_method = instance.__init__.__func__ if hasattr(instance.__init__, '__func__') else instance.__init__
|
339
|
+
await self._execute_function(init_method, [instance] + list(args), kwargs)
|
340
|
+
return instance
|
341
|
+
instance = object.__new__(cls)
|
342
|
+
self.current_instance = instance
|
343
|
+
self.current_class = cls
|
344
|
+
init_method = cls.__init__.__func__ if hasattr(cls.__init__, '__func__') else cls.__init__
|
345
|
+
await self._execute_function(init_method, [instance] + list(args), kwargs)
|
346
|
+
self.current_instance = None
|
347
|
+
self.current_class = None
|
348
|
+
return instance
|
349
|
+
|
350
|
+
async def _execute_function(self, func: Any, args: List[Any], kwargs: Dict[str, Any]):
|
351
|
+
if asyncio.iscoroutinefunction(func):
|
352
|
+
return await func(*args, **kwargs)
|
353
|
+
elif callable(func):
|
354
|
+
result = func(*args, **kwargs)
|
355
|
+
if asyncio.iscoroutine(result):
|
356
|
+
return await result
|
357
|
+
return result
|
358
|
+
raise TypeError(f"Object {func} is not callable")
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import ast
|
2
|
+
from typing import Any, Dict, List, Tuple
|
3
|
+
|
4
|
+
from .interpreter_core import ASTInterpreter
|
5
|
+
from .function_utils import Function
|
6
|
+
|
7
|
+
async def visit_Constant(self: ASTInterpreter, node: ast.Constant, wrap_exceptions: bool = True) -> Any:
|
8
|
+
return node.value
|
9
|
+
|
10
|
+
async def visit_Name(self: ASTInterpreter, node: ast.Name, wrap_exceptions: bool = True) -> Any:
|
11
|
+
if isinstance(node.ctx, ast.Load):
|
12
|
+
return self.get_variable(node.id)
|
13
|
+
elif isinstance(node.ctx, ast.Store):
|
14
|
+
return node.id
|
15
|
+
else:
|
16
|
+
raise Exception("Unsupported context for Name")
|
17
|
+
|
18
|
+
async def visit_List(self: ASTInterpreter, node: ast.List, wrap_exceptions: bool = True) -> List[Any]:
|
19
|
+
return [await self.visit(elt, wrap_exceptions=wrap_exceptions) for elt in node.elts]
|
20
|
+
|
21
|
+
async def visit_Tuple(self: ASTInterpreter, node: ast.Tuple, wrap_exceptions: bool = True) -> Tuple[Any, ...]:
|
22
|
+
elements = [await self.visit(elt, wrap_exceptions=wrap_exceptions) for elt in node.elts]
|
23
|
+
return tuple(elements)
|
24
|
+
|
25
|
+
async def visit_Dict(self: ASTInterpreter, node: ast.Dict, wrap_exceptions: bool = True) -> Dict[Any, Any]:
|
26
|
+
return {
|
27
|
+
await self.visit(k, wrap_exceptions=wrap_exceptions): await self.visit(v, wrap_exceptions=wrap_exceptions)
|
28
|
+
for k, v in zip(node.keys, node.values)
|
29
|
+
}
|
30
|
+
|
31
|
+
async def visit_Set(self: ASTInterpreter, node: ast.Set, wrap_exceptions: bool = True) -> set:
|
32
|
+
elements = [await self.visit(elt, wrap_exceptions=wrap_exceptions) for elt in node.elts]
|
33
|
+
return set(elements)
|
34
|
+
|
35
|
+
async def visit_Attribute(self: ASTInterpreter, node: ast.Attribute, wrap_exceptions: bool = True) -> Any:
|
36
|
+
value = await self.visit(node.value, wrap_exceptions=wrap_exceptions)
|
37
|
+
attr = node.attr
|
38
|
+
prop = getattr(type(value), attr, None)
|
39
|
+
if isinstance(prop, property) and isinstance(prop.fget, Function):
|
40
|
+
return await prop.fget(value)
|
41
|
+
return getattr(value, attr)
|
42
|
+
|
43
|
+
async def visit_Subscript(self: ASTInterpreter, node: ast.Subscript, wrap_exceptions: bool = True) -> Any:
|
44
|
+
value: Any = await self.visit(node.value, wrap_exceptions=wrap_exceptions)
|
45
|
+
slice_val: Any = await self.visit(node.slice, wrap_exceptions=wrap_exceptions)
|
46
|
+
return value[slice_val]
|
47
|
+
|
48
|
+
async def visit_Slice(self: ASTInterpreter, node: ast.Slice, wrap_exceptions: bool = True) -> slice:
|
49
|
+
lower: Any = await self.visit(node.lower, wrap_exceptions=wrap_exceptions) if node.lower else None
|
50
|
+
upper: Any = await self.visit(node.upper, wrap_exceptions=wrap_exceptions) if node.upper else None
|
51
|
+
step: Any = await self.visit(node.step, wrap_exceptions=wrap_exceptions) if node.step else None
|
52
|
+
return slice(lower, upper, step)
|
53
|
+
|
54
|
+
async def visit_Index(self: ASTInterpreter, node: ast.Index, wrap_exceptions: bool = True) -> Any:
|
55
|
+
return await self.visit(node.value, wrap_exceptions=wrap_exceptions)
|
56
|
+
|
57
|
+
async def visit_Starred(self: ASTInterpreter, node: ast.Starred, wrap_exceptions: bool = True) -> Any:
|
58
|
+
value = await self.visit(node.value, wrap_exceptions=wrap_exceptions)
|
59
|
+
if not isinstance(value, (list, tuple, set)):
|
60
|
+
raise TypeError(f"Cannot unpack non-iterable object of type {type(value).__name__}")
|
61
|
+
return value
|
62
|
+
|
63
|
+
async def visit_JoinedStr(self: ASTInterpreter, node: ast.JoinedStr, wrap_exceptions: bool = True) -> str:
|
64
|
+
parts = []
|
65
|
+
for value in node.values:
|
66
|
+
val = await self.visit(value, wrap_exceptions=wrap_exceptions)
|
67
|
+
if isinstance(value, ast.FormattedValue):
|
68
|
+
parts.append(str(val))
|
69
|
+
else:
|
70
|
+
parts.append(val)
|
71
|
+
return "".join(parts)
|
72
|
+
|
73
|
+
async def visit_FormattedValue(self: ASTInterpreter, node: ast.FormattedValue, wrap_exceptions: bool = True) -> Any:
|
74
|
+
return await self.visit(node.value, wrap_exceptions=wrap_exceptions)
|