summaryrefslogtreecommitdiff
path: root/yaksh/stdio_evaluator.py
blob: 5e4ce18ad153263b49593f49a37ec9ac32423590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import unicode_literals
import os
import signal

# Local imports
from .base_evaluator import BaseEvaluator
from .grader import TimeoutException
from .compare_stdio import compare_outputs


class StdIOEvaluator(BaseEvaluator):
    def evaluate_stdio(self, user_answer, proc, expected_input, expected_output):
        success = False
        try:
            if expected_input:
                ip = expected_input.replace(",", " ")
                encoded_input = '{0}\n'.format(ip).encode('utf-8')
                user_output_bytes, output_err_bytes = proc.communicate(
                                                           encoded_input
                                                           )
            else:
                user_output_bytes, output_err_bytes = proc.communicate()
            user_output = user_output_bytes.decode('utf-8')
            output_err = output_err_bytes.decode('utf-8')
        except TimeoutException:
            os.killpg(os.getpgid(proc.pid), signal.SIGTERM)
            raise
        expected_output = expected_output.replace("\r", "")
        success, err = compare_outputs(expected_output,
                                       user_output,
                                       expected_input
                                       )
        return success, err