-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-user.py
More file actions
38 lines (30 loc) · 831 Bytes
/
run-user.py
File metadata and controls
38 lines (30 loc) · 831 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
35
36
37
38
import sys
import logging
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def printException():
exc_type, exc_obj, tb = sys.exc_info()
print(exc_obj)
try:
# redirect stdout to a string (fake_stdout)
# compile into byte code and then execute the byte code
# then process the printed output from the code we ran
orig_stdout= sys.stdout
fake_stdout= StringIO()
sys.stdout= fake_stdout
code= compile(open(sys.argv[1]).read(), '<string>', 'exec')
exec(code)
sys.stderr.flush()
sys.stdout.flush()
sys.stdout= orig_stdout
output= fake_stdout.getvalue()
print(output)
sys.stderr.flush()
sys.stdout.flush()
exit(0)
except (IOError, SyntaxError, NameError) as e:
printException()
exit(1)