This is a function that evaluate all expressions and statements and return the result as a string. It also return Exceptions as strings. It is used in trypython.jcubic.pl
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 | from StringIO import StringIO
def execute(code, _globals={}, _locals={}):
import sys
fake_stdout = StringIO()
__stdout = sys.stdout
sys.stdout = fake_stdout
try:
#try if this is expressions
ret = eval(code, _globals, _locals)
result = fake_stdout.getvalue()
sys.stdout = __stdout
if ret:
result += str(ret)
return result
except:
try:
exec(code, _globals, _locals)
except:
sys.stdout = __stdout
import traceback
buf = StringIO()
traceback.print_exc(file=buf)
return buf.getvalue()
else:
sys.stdout = __stdout
return fake_stdout.getvalue()
|
It change stdout to StringIO and try to return result of eval along with output, if fail it try to use exec and return output from that.
Download
Copy to clipboard
Nice impl. however I should point out to those that read this;
This is considered very evil and dangerous and UNLESS you trust the source of the data don't do things like this.
Otherwise, nice post :)
--JamesMills (prologic)
This is the same evil and dangerous as eval/exec itself.
PS: I use this on JSON-RPC Interpreter but I block imports from 'os' and 'subprocess' modules and block access to files. I use this function:
I use 'posix' for 'os' because it's hosted on GNU/Linux.
Go to this directory and read "text.txt" /home/jcubic/domains/jcubic.pl/public_html/trypython/cgi-bin
Exec is EVIL!
(Don't worry, I touched nothing else)
I forget about modules inside modules this is list of all sub modules: