File size: 8,857 Bytes
67ebcef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""Runs one LiveCodeBench problem the way the official harness does (lcb_runner/evaluation/testing_util.py of
LiveCodeBench, read 2026-11-06): the same import prelude, stdin programs wrapped into wrapped_function() after removing a
final `if __name__ == '__main__':`, the same mocked stdin (MockStdinWithBuffer), stdout capture and comparison (exact
line match, else Decimal equality), call-based results compared with == (tuple -> list), the same reliability_guard.
Windows has no SIGALRM, so each test's 6 s limit is enforced by a watchdog thread that reports a timeout and ends the
process. All tests of one problem run in one process, in order, stopping at the first failure.
usage (from lcb_official.py): python -I lcb_official_runner.py payload.json   ->  one JSON line per test on stdout
"""
import ast
import faulthandler
import json
import os
import platform  # noqa: F401  (reliability_guard of the original refers to it)
import sys
import threading
from decimal import Decimal
from io import StringIO
from types import ModuleType
from unittest.mock import mock_open, patch

import numpy as np  # noqa: F401  (testing_util.py imports numpy before reliability_guard, so solutions can import it)

import_string = "from string import *\nfrom re import *\nfrom datetime import *\nfrom collections import *\nfrom heapq import *\nfrom bisect import *\nfrom copy import *\nfrom math import *\nfrom random import *\nfrom statistics import *\nfrom itertools import *\nfrom functools import *\nfrom operator import *\nfrom io import *\nfrom sys import *\nfrom json import *\nfrom builtins import *\nfrom typing import *\nimport string\nimport re\nimport datetime\nimport collections\nimport heapq\nimport bisect\nimport copy\nimport math\nimport random\nimport statistics\nimport itertools\nimport functools\nimport operator\nimport io\nimport sys\nimport json\nsys.setrecursionlimit(50000)\n"


class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        self._stringio.close = lambda x: 1
        return self

    def __exit__(self, *args):
        self.append(self._stringio.getvalue())
        del self._stringio
        sys.stdout = self._stdout


class MockBuffer:
    def __init__(self, inputs: str):
        self.inputs = inputs.encode("utf-8")

    def read(self, *args):
        return self.inputs

    def readline(self, *args):
        return self.inputs.split(b"\n")[0] + b"\n"


class MockStdinWithBuffer:
    def __init__(self, inputs: str):
        self.inputs = inputs
        self._stringio = StringIO(inputs)
        self.buffer = MockBuffer(inputs)

    def read(self, *args):
        return self.inputs

    def readline(self, *args):
        return self._stringio.readline(*args)

    def readlines(self, *args):
        return self.inputs.split("\n")

    def __getattr__(self, name):
        return getattr(self._stringio, name)


def call_method(method, inputs):
    if isinstance(inputs, list):
        inputs = "\n".join(inputs)
    inputs_line_iterator = iter(inputs.split("\n"))
    mock_stdin = MockStdinWithBuffer(inputs)

    @patch("builtins.open", mock_open(read_data=inputs))
    @patch("sys.stdin", mock_stdin)
    @patch("sys.stdin.readline", lambda *args: next(inputs_line_iterator))
    @patch("sys.stdin.readlines", lambda *args: inputs.split("\n"))
    @patch("sys.stdin.read", lambda *args: inputs)
    def _inner_call_method(_method):
        try:
            return _method()
        except SystemExit:
            pass

    return _inner_call_method(method)


def clean_if_name(code: str) -> str:
    try:
        astree = ast.parse(code)
        last_block = astree.body[-1]
        if isinstance(last_block, ast.If):
            condition = last_block.test
            if ast.unparse(condition).strip() == "__name__ == '__main__'":
                code = ast.unparse(astree.body[:-1]) + "\n" + ast.unparse(last_block.body)
    except Exception:
        pass
    return code


def make_function(code: str) -> str:
    try:
        import_stmts = []
        all_other_stmts = []
        astree = ast.parse(code)
        for stmt in astree.body:
            if isinstance(stmt, (ast.Import, ast.ImportFrom)):
                import_stmts.append(stmt)
            else:
                all_other_stmts.append(stmt)
        function_ast = ast.FunctionDef(
            name="wrapped_function",
            args=ast.arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]),
            body=all_other_stmts, decorator_list=[], lineno=-1)
        return import_string + "\n" + ast.unparse(import_stmts) + "\n" + ast.unparse(function_ast)
    except Exception:
        return code


def get_stripped_lines(val: str):
    val = val.strip()
    return [val_line.strip() for val_line in val.split("\n")]


def convert_line_to_decimals(line: str):
    try:
        return True, [Decimal(elem) for elem in line.split()]
    except Exception:
        return False, []


def same_stdio(prediction, gt_out):
    p, g = get_stripped_lines(prediction), get_stripped_lines(gt_out)
    if len(p) != len(g):
        return False
    for a, b in zip(p, g):
        if a == b:
            continue
        ok1, da = convert_line_to_decimals(a)
        ok2, db = convert_line_to_decimals(b)
        if not (ok1 and ok2 and da == db):
            return False
    return True


def reliability_guard():
    faulthandler.disable()
    import builtins
    builtins.quit = None
    os.environ["OMP_NUM_THREADS"] = "1"
    for name in ("kill", "system", "putenv", "remove", "removedirs", "rmdir", "fchdir", "setuid", "fork", "forkpty",
                 "killpg", "rename", "renames", "truncate", "replace", "unlink", "fchmod", "fchown", "chmod", "chown",
                 "chroot", "lchflags", "lchmod", "lchown", "getcwd", "chdir"):
        setattr(os, name, None)
    import shutil
    shutil.rmtree = shutil.move = shutil.chown = None
    import subprocess
    subprocess.Popen = None
    builtins.help = None
    for m in ("ipdb", "joblib", "resource", "psutil", "tkinter"):
        sys.modules[m] = None


def main():
    p = json.load(open(sys.argv[1], encoding="utf-8"))
    res_fd = os.dup(1)  # results go here; the program's own stray writes to the real stdout go to devnull
    null = os.open(os.devnull, os.O_WRONLY)
    os.dup2(null, 1)
    _exit, _write = os._exit, os.write

    def report(d):
        _write(res_fd, (json.dumps(d) + "\n").encode())

    def watchdog(k):
        report({"k": k, "res": "tle"})
        _exit(0)

    reliability_guard()
    timeout, mode = p["timeout"], p["mode"]
    code = import_string + "\n\n" + p["code"] if mode == "functional" else make_function(clean_if_name(p["code"]))
    t = threading.Timer(timeout, watchdog, args=(0,))
    t.start()
    try:
        tmp_sol = ModuleType("tmp_sol", "")
        exec(code, tmp_sol.__dict__)
        compiled = tmp_sol.Solution() if "class Solution" in code else tmp_sol
    except BaseException as e:
        t.cancel()
        report({"k": 0, "res": "error", "err": "compile: " + repr(e)[:300]})
        return
    t.cancel()
    method = getattr(compiled, p["fn"] if mode == "functional" else "wrapped_function", None)
    if method is None:
        report({"k": 0, "res": "error", "err": "function not found"})
        return
    tests = p["tests"]
    if mode == "functional":
        try:
            args_all = [[json.loads(line) for line in inp.split("\n")] for inp, _ in tests]
            outs_all = [json.loads(out) for _, out in tests]
        except Exception as e:
            report({"k": 0, "res": "error", "err": "decode: " + repr(e)[:200]})
            return
    for k, (inp, exp) in enumerate(tests):
        t = threading.Timer(timeout, watchdog, args=(k,))
        t.start()
        if mode == "functional":
            try:
                pred = method(*args_all[k])
            except BaseException as e:
                t.cancel()
                report({"k": k, "res": "error", "err": repr(e)[:300]})
                return
            t.cancel()
            if isinstance(pred, tuple):
                pred = list(pred)
            if not pred == outs_all[k]:
                report({"k": k, "res": "wrong", "got": repr(pred)[:300]})
                return
        else:
            err = None
            with Capturing() as captured:
                try:
                    call_method(method, inp)
                except BaseException as e:
                    err = repr(e)[:300]
            t.cancel()
            if err is not None:
                report({"k": k, "res": "error", "err": err})
                return
            if not same_stdio(captured[0], exp):
                report({"k": k, "res": "wrong", "got": captured[0][:300]})
                return
        report({"k": k, "res": "ok"})


main()