def execute(request): # AJAX request # method: POST # params: user_script, input_data post = request.POST if 'user_script' in post and 'input_data' in post: user_script = post['user_script'] input_data = post['input_data'] explain = False if post.get('explain', True) == 'false' else True res = execute_python(user_script, stdin=input_data, explain=explain).__dict__ if explain: del res['stdout'] # exact the same information is present on the last frame - why should it be duplicated? del res['stderr'] # see ^ if 'trace' in res: event = res['trace'][-1] if event['event'] == 'exception' or event['event'] == 'uncaught_exception': explain_error(event, user_script) if 'exception' in res and res['exception'] is not None: explain_error(res['exception'], user_script) return HttpResponse(dumps(res), content_type='application/json') else: return HttpResponseBadRequest()
def test_code(self, code): res = execute_python(code, stdin=self.test_input) user_answer = res.stdout.strip() is_correct = self._compare_sequences_of_tokens(user_answer, self.correct_answer) return TestResult(self, res.result, res.stderr, user_answer, is_correct)
def execute(request): # AJAX request # method: POST # params: user_script, input_data post = request.POST if 'user_script' in post and 'input_data' in post: user_script = post['user_script'] input_data = post['input_data'] explain = False if post.get('explain', True) == 'false' else True res = execute_python(user_script, stdin=input_data, explain=explain).__dict__ if explain: del res[ 'stdout'] # exact the same information is present on the last frame - why should it be duplicated? del res['stderr'] # see ^ if 'trace' in res: event = res['trace'][-1] if event['event'] == 'exception' or event[ 'event'] == 'uncaught_exception': explain_error(event, user_script) if 'exception' in res and res['exception'] is not None: explain_error(res['exception'], user_script) return HttpResponse(dumps(res), content_type='application/json') else: return HttpResponseBadRequest()
def test_code_snippets_cause_errors_that_match(self): for entry in error_translations.ERROR_TRANSLATIONS: if entry.code: execution = execute_python(entry.code) self.assertTrue(isinstance(execution.exception, dict), str(type(execution.exception)) + entry.source_regexp) exception_str = execution.exception['exception_str'] self.assertTrue(re.match(entry.regexp, exception_str), 'Regexp "{0}" doesn\'t match message {1}'.format( entry.source_regexp, exception_str))
def test_code_snippets_cause_errors_that_match(self): for entry in error_translations.ERROR_TRANSLATIONS: if entry.code: execution = execute_python(entry.code) self.assertTrue( isinstance(execution.exception, dict), str(type(execution.exception)) + entry.source_regexp) exception_str = execution.exception['exception_str'] self.assertTrue( re.match(entry.regexp, exception_str), 'Regexp "{0}" doesn\'t match message {1}'.format( entry.source_regexp, exception_str))
def test_list_index_out_of_range(self): execution = execute_python('[][0]', stdin='', explain=False) self.assertEqual(execution.stderr, 'IndexError: list index out of range')