From 2c7f278382f4fe8071508b0a880aae34f8edfd5e Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Mon, 18 Apr 2016 15:22:25 +0530 Subject: add compile_code function to compile before checking --- yaksh/python_stdout_evaluator.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'yaksh/python_stdout_evaluator.py') diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py index 9443e37..85efa51 100644 --- a/yaksh/python_stdout_evaluator.py +++ b/yaksh/python_stdout_evaluator.py @@ -25,22 +25,31 @@ def redirect_stdout(): class PythonStdoutEvaluator(CodeEvaluator): """Tests the Python code obtained from Code Server""" + def compile_code(self, user_answer, expected_output): + if hasattr(self, 'output_value'): + return None + else: + submitted = compile(user_answer, '', mode='exec') + with redirect_stdout() as output_buffer: + g = {} + exec submitted in g + self.output_value = output_buffer.getvalue() + return self.output_value + def check_code(self, user_answer, expected_output): success = False tb = None - # expected_output = test_case_data[0] - submitted = compile(user_answer, '', mode='exec') - with redirect_stdout() as output_buffer: - g = {} - exec submitted in g - raw_output_value = output_buffer.getvalue() - # output_value = raw_output_value.encode('string_escape').strip() - output_value = raw_output_value.strip() + # submitted = compile(user_answer, '', mode='exec') + # with redirect_stdout() as output_buffer: + # g = {} + # exec submitted in g + # raw_output_value = output_buffer.getvalue() + # output_value = raw_output_value.strip() if expected_output in user_answer: success = False err = "Incorrect Answer: Please avoid printing the expected output directly" - elif output_value == expected_output: + elif self.output_value == expected_output: success = True err = "Correct answer" -- cgit