def timed(timeout, fn, args=(), kargs={}): """Evaluates expr in the given frame. PARAMETERS: fn -- function; Python function to be evaluated args -- tuple; positional arguments for fn kargs -- dict; keyword arguments for fn timeout -- int; number of seconds before timer interrupt (defaults to TIMEOUT RETURN: Result of calling fn(*args, **kargs). RAISES: Timeout -- if thread takes longer than timemout to execute Error -- if calling fn raises an error, raise it """ submission = __ReturningThread(fn, args, kargs) submission.start() submission.join(timeout) if submission.is_alive(): raise exceptions.Timeout(timeout) if submission.error is not None: raise submission.error return submission.result
def timed(timeout, fn, args=(), kargs={}): """For a nonzero timeout, evaluates a call expression in a separate thread. If the timeout is 0, the expression is evaluated in the main thread. PARAMETERS: fn -- function; Python function to be evaluated args -- tuple; positional arguments for fn kargs -- dict; keyword arguments for fn timeout -- int; number of seconds before timer interrupt RETURN: Result of calling fn(*args, **kargs). RAISES: Timeout -- if thread takes longer than timeout to execute Error -- if calling fn raises an error, raise it """ if timeout == 0: return fn(*args, **kargs) submission = __ReturningThread(fn, args, kargs) submission.start() submission.join(timeout) if submission.is_alive(): raise exceptions.Timeout(timeout) if submission.error is not None: raise submission.error return submission.result
def _use_sqlite_cli(self, env): """Pipes the test case into the "sqlite3" executable. The method _has_sqlite_cli MUST be called before this method is called. PARAMETERS: env -- mapping; represents shell environment variables. Primarily, this allows modifications to PATH to check the current directory first. RETURNS: (test, expected, result), where test -- str; test input that is piped into sqlite3 expected -- str; the expected output, for display purposes result -- str; the actual output from piping input into sqlite3 """ test = [] expected = [] for line in self._setup + self._code + self._teardown: if isinstance(line, interpreter.CodeAnswer): expected.extend(line.output) elif line.startswith(self.PS1): test.append(line[len(self.PS1):]) elif line.startswith(self.PS2): test.append(line[len(self.PS2):]) test = '\n'.join(test) result, error = (None, None) process = None args = ['sqlite3'] sqlite_shell = get_sqlite_shell() if sqlite_shell: if self.timeout is None: (stdin, stdout, stderr) = (io.StringIO(test), io.StringIO(), io.StringIO()) sqlite_shell.main(*args, stdin=stdin, stdout=stdout, stderr=stderr) result, error = (stdout.getvalue(), stderr.getvalue()) else: args[:] = [sys.executable] + subprocess._args_from_interpreter_flags() + ["--", sqlite_shell.__file__] + args[1:] if result is None: process = subprocess.Popen(args, universal_newlines=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) if process: try: result, error = process.communicate(test, timeout=self.timeout) except subprocess.TimeoutExpired as e: process.kill() print('# Error: evaluation exceeded {} seconds.'.format(self.timeout)) raise interpreter.ConsoleException(exceptions.Timeout(self.timeout)) return test, '\n'.join(expected), (error + '\n' + result).strip()