(
code,
get_answer_from_stdout=None,
runtime=None,
answer_symbol=None,
answer_expr=None,
timeout_length=20,
)
| 147 | |
| 148 | @staticmethod |
| 149 | def execute( |
| 150 | code, |
| 151 | get_answer_from_stdout=None, |
| 152 | runtime=None, |
| 153 | answer_symbol=None, |
| 154 | answer_expr=None, |
| 155 | timeout_length=20, |
| 156 | ): |
| 157 | from timeout_decorator import timeout |
| 158 | try: |
| 159 | if get_answer_from_stdout: |
| 160 | program_io = io.StringIO() |
| 161 | with redirect_stdout(program_io): |
| 162 | timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) |
| 163 | program_io.seek(0) |
| 164 | result = program_io.read() |
| 165 | elif answer_symbol: |
| 166 | timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) |
| 167 | result = runtime._global_vars[answer_symbol] |
| 168 | elif answer_expr: |
| 169 | timeout(timeout_length)(runtime.exec_code)('\n'.join(code)) |
| 170 | result = timeout(timeout_length)(runtime.eval_code)(answer_expr) |
| 171 | else: |
| 172 | timeout(timeout_length)(runtime.exec_code)('\n'.join(code[:-1])) |
| 173 | result = timeout(timeout_length)(runtime.eval_code)(code[-1]) |
| 174 | report = 'Done' |
| 175 | str(result) |
| 176 | pickle.dumps(result) # serialization check |
| 177 | except Exception: |
| 178 | result = '' |
| 179 | report = traceback.format_exc().split('\n')[-2] |
| 180 | return result, report |
| 181 | |
| 182 | @staticmethod |
| 183 | def truncate(s, max_length=256): |
no test coverage detected