Skip to content

Commit 2143763

Browse files
refactor: change print to sys.stdout.
1 parent cdc5fae commit 2143763

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

apps/common/utils/tool_code.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ def init_sandbox_dir():
7676

7777
def exec_code(self, code_str, keywords, function_name=None):
7878
_id = str(uuid.uuid7())
79-
success = '{"code":200,"msg":"成功","data":exec_result}'
80-
err = '{"code":500,"msg":str(e),"data":None}'
8179
action_function = f'({function_name !a}, locals_v.get({function_name !a}))' if function_name else 'locals_v.popitem()'
8280
python_paths = CONFIG.get_sandbox_python_package_paths().split(',')
8381
set_run_user = f'os.setgid({pwd.getpwnam(_run_user).pw_gid});os.setuid({pwd.getpwnam(_run_user).pw_uid});' if _enable_sandbox else ''
@@ -87,9 +85,9 @@ def exec_code(self, code_str, keywords, function_name=None):
8785
path_to_exclude = ['/opt/py3/lib/python3.11/site-packages', '/opt/maxkb-app/apps']
8886
sys.path = [p for p in sys.path if p not in path_to_exclude]
8987
sys.path += {python_paths}
90-
locals_v={'{}'}
88+
locals_v={{}}
9189
keywords={keywords}
92-
globals_v={'{}'}
90+
globals_v={{}}
9391
{set_run_user}
9492
os.environ.clear()
9593
exec({dedent(code_str)!a}, globals_v, locals_v)
@@ -98,11 +96,11 @@ def exec_code(self, code_str, keywords, function_name=None):
9896
globals_v[local] = locals_v[local]
9997
exec_result=f(**keywords)
10098
sys.stdout.write("\\n{_id}:")
101-
json.dump({success}, sys.stdout, default=str)
99+
json.dump({{'code':200,'msg':'success','data':exec_result}}, sys.stdout, default=str)
102100
except Exception as e:
103101
if isinstance(e, MemoryError): e = Exception("Cannot allocate more memory: exceeded the limit of {_process_limit_mem_mb} MB.")
104102
sys.stdout.write("\\n{_id}:")
105-
json.dump({err}, sys.stdout, default=str)
103+
json.dump({{'code':500,'msg':str(e),'data':None}}, sys.stdout, default=str)
106104
sys.stdout.flush()
107105
"""
108106
maxkb_logger.debug(f"Sandbox execute code: {_exec_code}")

0 commit comments

Comments
 (0)