diff options
author | Timotej Lazar <timotej.lazar@araneo.org> | 2015-09-22 11:33:43 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@araneo.org> | 2015-09-22 11:33:43 +0200 |
commit | 15f35dc1c2eb50a8140f1a0abf45d5aa25fdf66b (patch) | |
tree | f0d793936f66467df6f711944b9d7adae893dde1 /server/python_session.py | |
parent | dce8ec719b1d85acf3c753effae3f28629dd847c (diff) |
Add support for execing the user's Python program
Diffstat (limited to 'server/python_session.py')
-rw-r--r-- | server/python_session.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/server/python_session.py b/server/python_session.py index 62fcbf8..6f2c999 100644 --- a/server/python_session.py +++ b/server/python_session.py @@ -6,6 +6,7 @@ import io import multiprocessing import os import queue +import signal import subprocess import sys import threading @@ -55,9 +56,15 @@ class PythonSession(object): p.terminate() return results + def exec(self, program): + self._control.put_nowait(('exec', program)) + def push(self, stdin): self._control.put_nowait(('push', stdin)) + def stop(self): + self._control.put_nowait(('stop', None)) + def destroy(self): self._control.put_nowait(('done', None)) @@ -125,9 +132,15 @@ def _interpreter(control, callback): # Get a control command. try: cmd, data = control.get_nowait() - if cmd == 'push': + if cmd == 'exec': + exec_str = 'exec("""\\\n{}\n""")\n'.format(data.replace('"', '\\"')) + proc.stdin.write(exec_str.encode('utf-8')) + proc.stdin.flush() + elif cmd == 'push': proc.stdin.write(data.encode('utf-8')) proc.stdin.flush() + elif cmd == 'stop': + proc.send_signal(signal.SIGINT) elif cmd == 'done': break except: |