-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_runner.py
More file actions
34 lines (32 loc) · 875 Bytes
/
python_runner.py
File metadata and controls
34 lines (32 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import tempfile
import subprocess
import traceback
import sys
def run_python(code: str):
try:
compile(code, "<user_code>", "exec")
except SyntaxError as e:
return {
"success": False,
"language": "python",
"error_type": "SyntaxError",
"line": e.lineno,
"message": e.msg
}
try:
exec(code, {})
return {
"success": True,
"language": "python",
"output": "Program executed successfully"
}
except Exception as e:
tb = traceback.extract_tb(sys.exc_info()[2])
last = tb[-1]
return {
"success": False,
"language": "python",
"error_type": type(e).__name__,
"line": last.lineno,
"message": str(e)
}