34 lines
784 B
Python
34 lines
784 B
Python
# Runtime compatibility module
|
|
# Usage: from deck.runtime import runcode
|
|
# Created by Luxferre in 2025, released into public domain
|
|
|
|
CMDCOMPILER = False
|
|
try:
|
|
from codeop import compile_command
|
|
CMDCOMPILER = True
|
|
except:
|
|
pass
|
|
|
|
def runcode(code):
|
|
"""
|
|
Compile and evaluate Python code dynamically.
|
|
Returns (statuscode, result) tuple.
|
|
Return status codes:
|
|
0 on error, 1 if the command is incomplete, 2 on success
|
|
"""
|
|
status = 0 # error status
|
|
res = None
|
|
if CMDCOMPILER:
|
|
compiled = compile_command(code)
|
|
else:
|
|
compiled = compile(code, '<string>', 'exec')
|
|
if compiled:
|
|
try:
|
|
res = exec(compiled)
|
|
status = 2 # success status
|
|
except Exception as e:
|
|
res = str(e)
|
|
else:
|
|
status = 1 # continuation status
|
|
return (status, res)
|