# brintOS Python self-test. Reaching ALL-PYTHON-OK proves the dynamic-split # CPython paradigm works on the glibc Emcraft machine: the interpreter comes # up (loaded from shared /usr/lib/libpython3.13.so by the python3-dyn stub), # core language works (arithmetic, recursion, strings, list/dict), the staged # stdlib imports (json, os), UTF-8 text round-trips, and file I/O runs against # the real rootfs (the records are written by the test itself, then read # back). Modeled on the Tcl/Expect/Awk self-tests: labelled checks, # env-var-free, one line per check, a pass counter, an ALL-*-OK sentinel # gated on every check passing. import json, os, sys total = 9 passed = 0 def check(tag, got, want): global passed if got == want: passed += 1 print(f"P-{tag} OK") else: print(f"P-{tag} FAIL (got {got!r}, want {want!r})") # P1 - interpreter up: the shipped CPython reports its 3.13 version. print("P1-ver", sys.version.split()[0]) check("P1ver", sys.version_info[:2], (3, 13)) # P2 - core language: arithmetic, recursion, strings, list + dict. check("P2expr", 6 * 7, 42) def fib(n): return n if n < 2 else fib(n - 1) + fib(n - 2) check("P2fib", fib(15), 610) check("P2str", "abc".upper() + "/" + str(len("hello")), "ABC/5") check("P2seq", (sorted([2, 1, 3]), {k: k * k for k in range(4)}[3]), ([1, 2, 3], 9)) # P3 - staged stdlib + text: json round-trip, UTF-8 round-trip. check("P3json", json.loads(json.dumps({"a": [1, 2, 3]}))["a"][2], 3) check("P3utf8", "é".encode("utf-8").decode("utf-8"), "é") # P4 - file I/O on the real rootfs: write records, read them back, remove. f = "/tmp/selftest_python.dat" with open(f, "w") as fh: fh.write("alpha 1\nbeta 2\n") with open(f) as fh: lines = fh.read().splitlines() os.remove(f) check("P4file", (len(lines), lines[1].split()[0]), (2, "beta")) # P5 - dynamic-split witness: the shared libpython the stub dlopens is a real # file in the shipped rootfs - the interpreter lives there, not in the stub # (removing it makes python3 fail loud with PYDL-FAIL, the RED witness). check("P5so", os.path.isfile("/usr/lib/libpython3.13.so"), True) print(f"P-SUMMARY pass={passed}/{total}") if passed == total: print("ALL-PYTHON-OK")