Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions apps/common/utils/tool_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from maxkb.const import BASE_DIR, CONFIG
from maxkb.const import PROJECT_DIR

import tempfile
python_directory = sys.executable


Expand Down Expand Up @@ -212,4 +212,15 @@ def validate_banned_keywords(self, code_str):

@staticmethod
def _exec(_code):
return subprocess.run([python_directory, '-c', _code], text=True, capture_output=True)
@staticmethod
def _exec(_code):
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as temp_file:
temp_file.write(_code)
temp_file_path = temp_file.name
try:
return subprocess.run([python_directory, temp_file_path], text=True, capture_output=True)
finally:
try:
os.unlink(temp_file_path)
except:
pass