diff options
author | maheshgudi | 2016-08-03 15:16:08 +0530 |
---|---|---|
committer | maheshgudi | 2016-08-03 15:16:08 +0530 |
commit | 40fbb5d8f1d4174f7d7e2d4723e9fbfc40040dcb (patch) | |
tree | 7885b674d2deb064137f1f501ed1707bfd54daf8 /yaksh/python_stdout_evaluator.py | |
parent | 1b71abc9437d721a41f017db406f312755f5a4c4 (diff) | |
parent | 2b03aeb36fa333ea1644a248c742cf0c1df12a5f (diff) | |
download | online_test-40fbb5d8f1d4174f7d7e2d4723e9fbfc40040dcb.tar.gz online_test-40fbb5d8f1d4174f7d7e2d4723e9fbfc40040dcb.tar.bz2 online_test-40fbb5d8f1d4174f7d7e2d4723e9fbfc40040dcb.zip |
rebase changes with stdio evaluator
Diffstat (limited to 'yaksh/python_stdout_evaluator.py')
-rw-r--r-- | yaksh/python_stdout_evaluator.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/yaksh/python_stdout_evaluator.py b/yaksh/python_stdout_evaluator.py new file mode 100644 index 0000000..8f69b24 --- /dev/null +++ b/yaksh/python_stdout_evaluator.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +import sys +import traceback +import os +from os.path import join +import importlib +from contextlib import contextmanager + +# local imports +from code_evaluator import CodeEvaluator +from file_utils import copy_files, delete_files + + +@contextmanager +def redirect_stdout(): + from StringIO import StringIO + new_target = StringIO() + + old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout + try: + yield new_target # run some code with the replaced stdout + finally: + sys.stdout = old_target # restore to the previous value + + +class PythonStdoutEvaluator(CodeEvaluator): + """Tests the Python code obtained from Code Server""" + + def teardown(self): + super(PythonStdoutEvaluator, self).teardown() + # Delete the created file. + if self.files: + delete_files(self.files) + + def compile_code(self, user_answer, file_paths, expected_output): + self.files = [] + if file_paths: + self.files = copy_files(file_paths) + if hasattr(self, 'output_value'): + return None + else: + submitted = compile(user_answer, '<string>', mode='exec') + with redirect_stdout() as output_buffer: + exec_scope = {} + exec submitted in exec_scope + self.output_value = output_buffer.getvalue() + return self.output_value + + def check_code(self, user_answer, file_paths, expected_output): + success = False + tb = None + if expected_output in user_answer: + success = False + err = ("Incorrect Answer: Please avoid " + "printing the expected output directly" + ) + elif self.output_value == expected_output: + success = True + err = "Correct answer" + + else: + success = False + err = "Incorrect Answer" + del tb + return success, err |