added traceback for better debugging messages

This commit is contained in:
Crizomb 2023-04-07 13:19:54 +02:00 committed by GitHub
parent be71c9b4b7
commit 2727bd2422
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ import sys
import importlib.util
import io
import contextlib
import traceback
def create_namespace():
module_name = "__main__"
@ -12,8 +13,6 @@ def create_namespace():
def run_code(code_str, namespace):
# redirect stdout to a buffer to capture output
if "import os" in code_str:
return "Error: import os is not allowed. use direct shell command instead."
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
try:
@ -25,16 +24,14 @@ def run_code(code_str, namespace):
except Exception as e:
# print any errors to the buffer
print(f"Error: {e}", file=buffer)
traceback.print_exc(file=buffer)
# return the captured output or error message as a string
result = buffer.getvalue().strip()
if result:
return result
else:
return "No output, maybe an error? maybe not."
return "---\nProcess finished with no output\n---"
def test():
code_str1 = '''import numpy as np\nimport matplotlib.pyplot as plt'''