Update shared_vis_python_exe.py
Browse files- shared_vis_python_exe.py +67 -67
shared_vis_python_exe.py
CHANGED
@@ -282,79 +282,79 @@ class PythonExecutor:
|
|
282 |
# return result, report
|
283 |
|
284 |
def execute(
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
|
|
|
|
|
|
|
|
|
|
309 |
# 移除 timeout 调用
|
310 |
runtime.exec_code("\n".join(code))
|
311 |
-
|
312 |
-
|
313 |
-
elif answer_symbol:
|
314 |
-
# 移除 timeout 调用
|
315 |
-
runtime.exec_code("\n".join(code))
|
316 |
-
result = runtime._global_vars.get(answer_symbol, "")
|
317 |
-
elif answer_expr:
|
318 |
-
# 移除 timeout 调用
|
319 |
-
runtime.exec_code("\n".join(code))
|
320 |
-
# 移除 timeout 调用
|
321 |
-
result = runtime.eval_code(answer_expr)
|
322 |
-
else:
|
323 |
-
if len(code) > 1:
|
324 |
-
# 移除 timeout 调用
|
325 |
-
runtime.exec_code("\n".join(code[:-1]))
|
326 |
-
# 移除 timeout 调用
|
327 |
-
result = runtime.eval_code(code[-1])
|
328 |
-
else:
|
329 |
# 移除 timeout 调用
|
330 |
runtime.exec_code("\n".join(code))
|
331 |
-
|
332 |
-
|
333 |
-
# 检查是否有捕获的图像
|
334 |
-
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
335 |
-
if captured_figures:
|
336 |
-
# 如果有文本输出和图像,将它们组合
|
337 |
-
if result:
|
338 |
-
result = {
|
339 |
-
'text': result,
|
340 |
-
'images': captured_figures
|
341 |
-
}
|
342 |
else:
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
pickle.dumps(result)
|
353 |
-
except Exception as e:
|
354 |
-
result = f"Result serialization error: {str(e)}"
|
355 |
-
report = f"Serialization Error: {str(e)}"
|
356 |
|
357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
|
359 |
|
360 |
def apply(self, code, messages):
|
|
|
282 |
# return result, report
|
283 |
|
284 |
def execute(
|
285 |
+
self,
|
286 |
+
code,
|
287 |
+
messages,
|
288 |
+
get_answer_from_stdout=True,
|
289 |
+
runtime_class=None,
|
290 |
+
# run_time_instance=None,
|
291 |
+
answer_symbol=None,
|
292 |
+
answer_expr=None,
|
293 |
+
# 移除 timeout_length 参数
|
294 |
+
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
295 |
+
# print("dome")
|
296 |
+
# try:
|
297 |
+
# 在每个进程中创建新的运行时实例
|
298 |
+
print(f"################################################## I am excuting ! #############################################################")
|
299 |
+
print(str(messages)[0:500])
|
300 |
+
print(str(messages)[-500:])
|
301 |
+
print(runtime_class)
|
302 |
+
# runtime = runtime_class(messages)
|
303 |
+
runtime = self.persistent_runtime
|
304 |
+
print(f"################################################## I am excuting ! #############################################################")
|
305 |
+
|
306 |
+
if get_answer_from_stdout:
|
307 |
+
program_io = io.StringIO()
|
308 |
+
with redirect_stdout(program_io):
|
309 |
+
# 移除 timeout 调用
|
310 |
+
runtime.exec_code("\n".join(code))
|
311 |
+
program_io.seek(0)
|
312 |
+
result = program_io.read()
|
313 |
+
elif answer_symbol:
|
314 |
# 移除 timeout 调用
|
315 |
runtime.exec_code("\n".join(code))
|
316 |
+
result = runtime._global_vars.get(answer_symbol, "")
|
317 |
+
elif answer_expr:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
318 |
# 移除 timeout 调用
|
319 |
runtime.exec_code("\n".join(code))
|
320 |
+
# 移除 timeout 调用
|
321 |
+
result = runtime.eval_code(answer_expr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
else:
|
323 |
+
if len(code) > 1:
|
324 |
+
# 移除 timeout 调用
|
325 |
+
runtime.exec_code("\n".join(code[:-1]))
|
326 |
+
# 移除 timeout 调用
|
327 |
+
result = runtime.eval_code(code[-1])
|
328 |
+
else:
|
329 |
+
# 移除 timeout 调用
|
330 |
+
runtime.exec_code("\n".join(code))
|
331 |
+
result = ""
|
|
|
|
|
|
|
|
|
332 |
|
333 |
+
# 检查是否有捕获的图像
|
334 |
+
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
335 |
+
if captured_figures:
|
336 |
+
# 如果有文本输出和图像,将它们组合
|
337 |
+
if result:
|
338 |
+
result = {
|
339 |
+
'text': result,
|
340 |
+
'images': captured_figures
|
341 |
+
}
|
342 |
+
else:
|
343 |
+
result = {'images': captured_figures}
|
344 |
+
|
345 |
+
report = "Done"
|
346 |
+
# except Exception as e:
|
347 |
+
# result = ""
|
348 |
+
# report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
349 |
+
|
350 |
+
# 确保结果可序列化
|
351 |
+
try:
|
352 |
+
pickle.dumps(result)
|
353 |
+
except Exception as e:
|
354 |
+
result = f"Result serialization error: {str(e)}"
|
355 |
+
report = f"Serialization Error: {str(e)}"
|
356 |
+
|
357 |
+
return result, report
|
358 |
|
359 |
|
360 |
def apply(self, code, messages):
|