# coding=utf-8 import ast from fcntl import fcntl, F_GETFL, F_SETFL import io import multiprocessing import os import queue import signal import subprocess import sys import threading import time import server.user_session from db.models import Problem from . import problems __all__ = ['PythonSession'] class PythonSession(object): """Abstracts a Python session. Only public methods are available to the outside world due to the use of multiprocessing managers. Therefore prefix any private methods with an underscore (_). No properties are accessible; use getters and setters instead. Values are passed by value instead of by reference (deep copy!). """ def __init__(self, output_cb=None): self._access_lock = threading.Lock() self._sent_hints = [] self._control = queue.Queue() self._interpreter = threading.Thread(target=_interpreter, kwargs={'control': self._control, 'callback': output_cb}) self._interpreter.start() def run(self, code=None, inputs=None, timeout=1.0): # Launch processes. futures = [] for expr, stdin in inputs: conn_parent, conn_child = multiprocessing.Pipe() p = multiprocessing.Process(target=_run_exec, args=(conn_child, code, expr, stdin)) p.start() futures.append((p, conn_parent)) # Wait for results. results = [] start = time.monotonic() for p, conn in futures: now = time.monotonic() real_timeout = max(0, timeout - (now - start)) if conn.poll(real_timeout): results.append(conn.recv()) else: results.append((None, None, None, 'timed out')) 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)) def __del__(self): self.destroy() def hint(self, sid, problem_id, program): language, problem_group, problem = Problem.get_identifier(problem_id) language_module = problems.load_language(language, 'common') problem_module = problems.load_problem(language, problem_group, problem, 'common') hints = [] if hasattr(language_module, 'hint'): hints = language_module.hint(self.run, program) if not hints and hasattr(problem_module, 'hint'): hints = problem_module.hint(self.run, program) if not hints: hints = [{'id': 'no_hint'}] self._instantiate_and_save_hints(language_module, problem_module, hints) return hints def test(self, sid, problem_id, program): language, problem_group, problem = Problem.get_identifier(problem_id) language_module = problems.load_language(language, 'common') problem_module = problems.load_problem(language, problem_group, problem, 'common') try: passed, hints = problem_module.test(self.run, program) except AttributeError as ex: hints = [{'id': 'system_error', 'args': {'message': 'test function does not exist'}}] self._instantiate_and_save_hints(language_module, problem_module, hints) return hints # Add hint parameters (such as message index) based on hint class. Append # the finalized hints to the list of sent hints. def _instantiate_and_save_hints(self, language_mod, problem_mod, hints): with self._access_lock: for hint in hints: for mod in [language_mod, problem_mod]: if hasattr(mod, 'hint_type') and hint['id'] in mod.hint_type: hint_type = mod.hint_type[hint['id']] hint_type.instantiate(hint, self._sent_hints) self._sent_hints.extend(hints) def _interpreter(control, callback): directory = os.path.dirname(os.path.realpath(__file__)) # TODO drop privileges using a wrapper script = os.path.join(directory, '..', 'python', 'interpreter.py') proc = None while True: # Ensure the interpreter process is running. if proc is None: proc = subprocess.Popen([script], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Set the non-blocking flag for stdout. flags = fcntl(proc.stdout.fileno(), F_GETFL) fcntl(proc.stdout.fileno(), F_SETFL, flags | os.O_NONBLOCK) # Get a control command. try: cmd, data = control.get_nowait() 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: pass # Communicate with child process. retcode = proc.poll() if retcode is None: data = proc.stdout.read() if data: # NOTE this might fail if read() stops in the middle of utf8 sequence text = data.decode('utf-8') if text: callback(text) else: if retcode == -31: callback('Child killed due to sandbox misbehavior.\n') else: callback('Child exited with status "{}".\n'.format(retcode)) proc = None # TODO we should select() on control and proc.stdout instead of polling time.sleep(0.1) # We are done, kill the child. if proc is not None: proc.kill() # Execute [code] and evaluate [expr]. Input is given by the string [stdin]. # Return result of evaluation, the contents of stdout and stderr, and the # exception traceback. # TODO sandbox this def _run_exec(conn, code, expr=None, stdin=''): result, out, err, exc = None, None, None, None sys.stdin = io.StringIO(stdin) sys.stdout = io.StringIO() sys.stderr = io.StringIO() try: env = {} if code: exec(code, env) if expr: result = eval(expr, env) except Exception as ex: # Exception is not JSON serializable, so return traceback as string # (without the first entry, which is this function). import traceback e_type, e_value, e_tb = sys.exc_info() stack = traceback.extract_tb(e_tb) exc = ''.join( ['Traceback (most recent call last):\n'] + [' line {}, in {}\n'.format(lineno, name) + (line+'\n' if line else '') for filename, lineno, name, line in stack[1:]] + traceback.format_exception_only(e_type, e_value) ).rstrip() finally: out = sys.stdout.getvalue() err = sys.stderr.getvalue() sys.stdin.close() sys.stdout.close() sys.stderr.close() conn.send((result, out, err, exc))