1
1
# coding=utf-8
2
2
import ast
3
3
import os
4
- import pickle
4
+ import json
5
5
import subprocess
6
6
import sys
7
7
from textwrap import dedent
@@ -40,15 +40,15 @@ def _createdir(self):
40
40
def exec_code (self , code_str , keywords ):
41
41
self .validate_banned_keywords (code_str )
42
42
_id = str (uuid .uuid7 ())
43
- success = '{"code":200,"msg":"成功","data":exec_result}'
43
+ success = '{"code":200,"msg":"成功","data":json.dumps( exec_result, default=str) }'
44
44
err = '{"code":500,"msg":str(e),"data":None}'
45
45
result_path = f'{ self .sandbox_path } /result/{ _id } .result'
46
46
python_paths = CONFIG .get_sandbox_python_package_paths ().split (',' )
47
47
_exec_code = f"""
48
48
try:
49
49
import os
50
50
import sys
51
- import pickle
51
+ import json
52
52
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
53
53
sys.path = [p for p in sys.path if p not in path_to_exclude]
54
54
sys.path += { python_paths }
@@ -64,20 +64,20 @@ def exec_code(self, code_str, keywords):
64
64
for local in locals_v:
65
65
globals_v[local] = locals_v[local]
66
66
exec_result=f(**keywords)
67
- with open({ result_path !a} , 'wb ') as file:
68
- file.write(pickle .dumps({ success } ))
67
+ with open({ result_path !a} , 'w ') as file:
68
+ file.write(json .dumps({ success } ))
69
69
except Exception as e:
70
- with open({ result_path !a} , 'wb ') as file:
71
- file.write(pickle .dumps({ err } ))
70
+ with open({ result_path !a} , 'w ') as file:
71
+ file.write(json .dumps({ err } ))
72
72
"""
73
73
if self .sandbox :
74
74
subprocess_result = self ._exec_sandbox (_exec_code , _id )
75
75
else :
76
76
subprocess_result = self ._exec (_exec_code )
77
77
if subprocess_result .returncode == 1 :
78
78
raise Exception (subprocess_result .stderr )
79
- with open (result_path , 'rb ' ) as file :
80
- result = pickle .loads (file .read ())
79
+ with open (result_path , 'r ' ) as file :
80
+ result = json .loads (file .read ())
81
81
os .remove (result_path )
82
82
if result .get ('code' ) == 200 :
83
83
return result .get ('data' )
0 commit comments