This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/util/pyexec.py

32 lines
959 B
Python
Raw Permalink Normal View History

2013-11-12 07:06:06 +01:00
import http
import web
def eval_py(code, paste_multiline=True):
2012-09-05 00:11:12 +02:00
attempts = 0
while True:
2012-09-05 00:11:12 +02:00
try:
output = http.get("http://eval.appspot.com/eval", statement=code).rstrip('\n')
2012-09-05 12:35:46 +02:00
# sometimes the API returns a blank string on first attempt, lets try again
# and make sure it is actually supposed to be a blank string. ._.
if output == "":
output = http.get("http://eval.appspot.com/eval", statement=code).rstrip('\n')
2012-09-05 11:35:44 +02:00
break
2012-09-05 00:11:12 +02:00
except http.HTTPError:
if attempts > 2:
return "Failed to execute code."
else:
attempts += 1
continue
if "Traceback (most recent call last):" in output:
status = "Python error: "
else:
status = "Code executed sucessfully: "
2012-09-04 21:52:03 +02:00
if "\n" in output and paste_multiline:
2012-09-02 13:48:47 +02:00
return status + web.haste(output)
else:
2012-09-04 21:52:03 +02:00
return output