"""Request loop run by the grader as uid 65534: python3.11 -I -S -B driver.py PKGROOT. PKGROOT holds a root-owned copy of the *.py files of /app/canary. Each request line is "IDPLAN-JSON"; canary.answer(plan) is called with the plan as a dict and the result is written to the original stdout as one line "IDOKCANON" or "IDERRmessage". CANON is "none" for None and otherwise "q/", the exact value of the returned number in lowest terms (an int or a fractions.Fraction; floats and bools are refused). Anything the agent's code prints goes to stderr. -S keeps site-packages off sys.path; it is added back explicitly (jinja2). """ import os import sys proto = os.dup(1) os.dup2(2, 1) sys.path.insert(0, sys.argv[1]) for _p in ("/usr/local/lib/python3.11/site-packages",): sys.path.append(_p) sys.set_int_max_str_digits(0) import json # noqa: E402 import numbers # noqa: E402 from fractions import Fraction # noqa: E402 def answer(text): data = (text.replace("\n", " ").replace("\r", " ") + "\n").encode("utf-8", "replace") while data: n = os.write(proto, data) data = data[n:] def canon(v): if v is None: return "none" if isinstance(v, bool) or not isinstance(v, numbers.Rational): raise TypeError("answer must be an int, a Fraction or None, got %s" % type(v).__name__) f = Fraction(v.numerator, v.denominator) return "q%d/%d" % (f.numerator, f.denominator) from canary import answer as solve # noqa: E402 answer("READY") for line in sys.stdin: line = line.rstrip("\n") if not line: continue rid, payload = line.split("\t", 1) try: plan = json.loads(payload) answer(rid + "\tOK\t" + canon(solve(plan))) except BaseException as exc: # noqa: BLE001 answer(rid + "\tERR\t" + ("%s: %s" % (type(exc).__name__, exc))[:300])