added traceback for better debugging messages
This commit is contained in:
parent
be71c9b4b7
commit
2727bd2422
1 changed files with 3 additions and 6 deletions
|
@ -2,6 +2,7 @@ import sys
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import io
|
import io
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import traceback
|
||||||
|
|
||||||
def create_namespace():
|
def create_namespace():
|
||||||
module_name = "__main__"
|
module_name = "__main__"
|
||||||
|
@ -12,8 +13,6 @@ def create_namespace():
|
||||||
|
|
||||||
def run_code(code_str, namespace):
|
def run_code(code_str, namespace):
|
||||||
# redirect stdout to a buffer to capture output
|
# 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()
|
buffer = io.StringIO()
|
||||||
with contextlib.redirect_stdout(buffer):
|
with contextlib.redirect_stdout(buffer):
|
||||||
try:
|
try:
|
||||||
|
@ -25,16 +24,14 @@ def run_code(code_str, namespace):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# print any errors to the buffer
|
# print any errors to the buffer
|
||||||
print(f"Error: {e}", file=buffer)
|
print(f"Error: {e}", file=buffer)
|
||||||
|
traceback.print_exc(file=buffer)
|
||||||
|
|
||||||
# return the captured output or error message as a string
|
# return the captured output or error message as a string
|
||||||
result = buffer.getvalue().strip()
|
result = buffer.getvalue().strip()
|
||||||
if result:
|
if result:
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
return "No output, maybe an error? maybe not."
|
return "---\nProcess finished with no output\n---"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
code_str1 = '''import numpy as np\nimport matplotlib.pyplot as plt'''
|
code_str1 = '''import numpy as np\nimport matplotlib.pyplot as plt'''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue