Repository navigation
Expand file tree
/
Copy pathcode_execution.py
More file actions
345 lines (305 loc) · 11.4 KB
/
Copy pathcode_execution.py
File metadata and controls
345 lines (305 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Code utils for extracting and executing python code in string format."""
from __future__ import annotations
from collections.abc import Mapping, Sequence
import io
import linecache
import multiprocessing
import os
import queue
import re
import signal
import sys
import time
from typing import Any
class ExtractionError(Exception):
"""Custom exception for errors during code extraction."""
class ExecutionError(Exception):
"""Custom exception for errors during code execution."""
class ExecutionTimeoutError(Exception):
"""Custom exception for code execution timing out."""
def extract_python_code(
code_string: str | None,
target_fn_name: str | None = None,
) -> tuple[str, str]:
"""Extracts the most relevant Python code block and the surrounding plan.
Finds all Markdown-style Python code blocks (```python ... ```).
- Prioritizes blocks containing the definition of `target_fn_name` if given.
- If multiple blocks match, the one with the most lines is chosen.
- The Markdown `python_plan` consists of all text from the original string,
excluding the content of the selected `python_code` block.
Args:
code_string: The string potentially containing Python code blocks.
target_fn_name: The name of the function to look for in code blocks.
Returns:
A tuple of:
- python_code: The Python code from the selected block, stripped.
- python_plan: Text before and after the selected code block, stripped and
joined by a space. Includes any other code blocks.
Raises:
ExtractionError: If the input is not a valid string, or if no valid
complete Python code blocks (```python ... ```) are found.
"""
if not isinstance(code_string, str) or not code_string.strip():
raise ExtractionError(
"Input code_string cannot be None, empty or type: "
f"{type(code_string).__name__}.."
)
pattern = r"```python\s*(.*?)\s*```"
matches = list(re.finditer(pattern, code_string, flags=re.DOTALL))
if not matches: # No complete ```python ... ``` blocks found.
if "```python" not in code_string:
raise ExtractionError(
"No Python code block start marker (```python) found."
)
else:
# "```python" was found, but no regex match, indicating an issue.
raise ExtractionError(
"Python code block start marker '```python' found, but no valid"
" complete block could be extracted."
)
best_match = None
if target_fn_name:
# Look for blocks containing the target Python function definition.
target_def_pattern = rf"(^|\n)def\s+{target_fn_name}\s*\("
fn_matches = [
m for m in matches if re.search(target_def_pattern, m.group(1).strip())
]
if fn_matches:
best_match = max(fn_matches, key=lambda m: len(m.group(1).strip()))
if not best_match:
# Fallback to the longest block overall
best_match = max(matches, key=lambda m: len(m.group(1).strip()))
start_text = code_string[: best_match.start()].strip()
end_text = code_string[best_match.end() :].strip()
python_code = best_match.group(1).strip()
# The plan is everything before the longest match and everything after code.
python_plan = " ".join([text for text in [start_text, end_text] if text])
return python_code, python_plan
def _run_compiled_code(
extracted_code: str,
target_fn_name: str,
fn_args: Sequence[Any],
fn_kwargs: Mapping[str, Any],
initial_namespace: Mapping[str, Any] | None,
local_filename: str,
) -> Any:
"""Compiles and executes the extracted code, and calls the target function."""
local_namespace = dict(initial_namespace) if initial_namespace else {}
try:
compiled_code = compile(
extracted_code, filename=local_filename, mode="exec"
)
exec(compiled_code, local_namespace) # pylint: disable=exec-used
target_fn = local_namespace.get(target_fn_name)
if target_fn is None:
raise ExecutionError(
f"Target function '{target_fn_name}' not found in executed code."
)
if not callable(target_fn):
raise ExecutionError(
f"Target '{target_fn_name}' was found but is not callable."
)
return target_fn(*fn_args, **fn_kwargs)
except Exception as e:
raise ExecutionError(
f"Error during code execution: {type(e).__name__} - {e}"
) from e
def _execute_target(
q: multiprocessing.Queue,
extracted_code: str,
target_fn_name: str,
fn_args: Sequence[Any],
fn_kwargs: Mapping[str, Any],
initial_namespace: Mapping[str, Any],
local_filename: str,
):
"""Target function to run in a separate process."""
# Prevent child from receiving SIGINT and dumping tracebacks onto Colab.
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Redirect stdout/stderr to prevent concurrent writes to Colab ZMQ sockets,
# which instantly crashes the Colab kernel.
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
try:
linecache.cache[local_filename] = (
len(extracted_code),
None,
extracted_code.splitlines(True),
local_filename,
)
code_output = _run_compiled_code(
extracted_code,
target_fn_name,
fn_args,
fn_kwargs,
initial_namespace,
local_filename,
)
q.put(("success", code_output))
except Exception as e: # pylint: disable=broad-exception-caught
# Catching all exceptions is intentional here to report any error in the
# provided code back to the mainprocess.
q.put(("error", e))
finally:
linecache.cache.pop(local_filename, None)
def execute_code(
extracted_code: str,
target_fn_name: str,
fn_args: Sequence[Any] | None = None,
fn_kwargs: Mapping[str, Any] | None = None,
initial_namespace: Mapping[str, Any] | None = None,
local_filename: str = "<dynamic_code>",
timeout_seconds: float | None = None,
) -> tuple[Any, float]:
"""Compiles, executes code, and calls a target function, with timeout.
Args:
extracted_code: The Python code string to execute.
target_fn_name: The name of the function to call within the executed code.
fn_args: Positional arguments for the target function.
fn_kwargs: Keyword arguments for the target function.
initial_namespace: Mapping to pre-populate the execution namespace.
local_filename: Filename for tracebacks.
timeout_seconds: Optional maximum time to allow for execution.
Returns:
tuple[Any, float]: A tuple containing:
- code_output: Return value of the target_fn.
- exec_time: Execution time in seconds.
Raises:
ExecutionError: Wraps any exception raised during code compilation,
execution, target function lookup, or the target function call.
ExecutionTimeoutError: If the execution time exceeds timeout_seconds.
"""
fn_args = fn_args or []
fn_kwargs = fn_kwargs or {}
start_time = time.time()
if timeout_seconds is None:
linecache.cache[local_filename] = (
len(extracted_code),
None,
extracted_code.splitlines(True),
local_filename,
)
try:
code_output = _run_compiled_code(
extracted_code,
target_fn_name,
fn_args,
fn_kwargs,
initial_namespace,
local_filename,
)
finally:
linecache.cache.pop(local_filename, None)
exec_time = time.time() - start_time
return code_output, exec_time
else:
# Sandbox execution timeout handling:
# Using 'multiprocessing' with 'fork' context for isolated execution.
# 'fork' is preferred here because it avoids pickling issues with modules
# (e.g., pandas, numpy) and has lower overhead than 'spawn' or 'forkserver'.
#
# Risk Mitigation:
# 1. Child runs isolated code.
# 2. Child ignores SIGINT.
# 3. Child stdout/stderr redirected to prevent socket corruption.
# 4. Cleanup: Parent uses SIGKILL in finally block to prevent zombies.
#
# Note: Forkserver could be considered later if process stability degrades.
ctx = multiprocessing.get_context("fork")
q = ctx.Queue()
args = (
q,
extracted_code,
target_fn_name,
fn_args,
fn_kwargs,
initial_namespace,
local_filename,
)
p = ctx.Process(target=_execute_target, args=args)
p.start()
try:
# Wait on queue with timeout to prevent deadlock from full pipe buffers
try:
if (
isinstance(p.pid, int)
and timeout_seconds is not None
and timeout_seconds > 1.0
):
deadline = time.monotonic() + timeout_seconds
while True:
remaining = deadline - time.monotonic()
if remaining <= 0:
raise queue.Empty()
try:
status, result = q.get(timeout=min(0.2, remaining))
break
except queue.Empty:
if not p.is_alive():
raise
else:
status, result = q.get(timeout=timeout_seconds)
exec_time = time.time() - start_time
except queue.Empty as e:
if not p.is_alive():
exit_info = ""
if isinstance(p.exitcode, int) and p.exitcode < 0:
exit_info = (
f" (exitcode={p.exitcode}; if using JAX/XLA, the C++ runtime"
" aborted in this environment—fall back to NumPy/scikit-learn"
" such as MLPClassifier)"
)
raise ExecutionError(
"Failed to get result from process queue: process died"
f" unexpectedly.{exit_info}"
) from e
# If the process is still alive after queue timeout, it means the
# execution itself timed out.
try:
os.kill(p.pid, signal.SIGKILL) # pyrefly: ignore[bad-argument-type]
except ProcessLookupError:
pass
p.join(timeout=1.0)
raise ExecutionTimeoutError(
f"Code execution timed out after {timeout_seconds} seconds."
) from e
except Exception as e:
raise ExecutionError(f"Error retrieving from queue: {e}") from e
# Join with a short timeout in case background worker threads (e.g.
# joblib/loky/OpenMP) in the forked child block interpreter shutdown.
p.join(timeout=2.0)
if p.is_alive():
try:
os.kill(p.pid, signal.SIGKILL) # pyrefly: ignore[bad-argument-type]
except ProcessLookupError:
pass
p.join(timeout=1.0)
if status == "success":
return result, exec_time
else:
if isinstance(result, Exception):
# Re-raise the exception from the subprocess
raise result
else:
raise ExecutionError(f"Unknown error during code execution: {result}")
finally:
# Clean up child process regardless of exceptions like KeyboardInterrupt.
if p.is_alive():
try:
os.kill(p.pid, signal.SIGKILL) # pyrefly: ignore[bad-argument-type]
except ProcessLookupError:
pass
p.join()