mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
test(python): pin CPython code-object equality controls
This commit is contained in:
1 parent
653c49419e
commit
2fbffb067c
5 files changed
+192
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { pythonPycEqualityGolden } from "../../tests/fixtures/python-pyc-equality/golden.js";
|
||||
import { expect, it } from "vitest";
|
||||
import {
|
||||
inspectPythonPycInputSchema,
|
||||
@@ -20,6 +21,23 @@ const defaults = inspectPythonPycInputSchema.parse({
|
||||
path: "/artifacts/sample.pyc",
|
||||
});
|
||||
|
||||
it("matches CPython-produced code equality controls without merging distinct members", () => {
|
||||
const golden = pythonPycEqualityGolden;
|
||||
const bytes = Buffer.from(golden.pyc_base64, "base64");
|
||||
expect(createHash("sha256").update(bytes).digest("hex")).toBe(golden.sha256);
|
||||
const report = parse(bytes);
|
||||
expect(root(report).consts).toHaveLength(golden.cases.length);
|
||||
for (const [index, sample] of golden.cases.entries()) {
|
||||
expect(root(report).consts[index], sample.label).toEqual({
|
||||
kind: "frozenset",
|
||||
length: sample.members,
|
||||
});
|
||||
}
|
||||
expect(report.code_objects).toHaveLength(golden.code_objects);
|
||||
expect(report.coverage.status).toBe("complete");
|
||||
expect(pythonPycSchema.safeParse(report).success).toBe(true);
|
||||
});
|
||||
|
||||
const root = (report: PythonPyc) => {
|
||||
const code = report.code_objects[0];
|
||||
if (code === undefined) throw new Error("Report lists no code object");
|
||||
|
||||
@@ -11,11 +11,38 @@ import {
|
||||
} from "../../src/python/PythonPyc.fixture.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { cliTest, runFileCli } from "../support/cli/cliFixture.js";
|
||||
import { pythonPycEqualityGolden } from "../fixtures/python-pyc-equality/golden.js";
|
||||
|
||||
const completePycEvidenceSchema = toolContract(
|
||||
"inspect_python_pyc",
|
||||
).outputSchema.safeExtend({ normalized_result: pythonPycSchema });
|
||||
|
||||
cliTest(
|
||||
"preserves CPython code-member identity in public Evidence",
|
||||
async ({ cli }) => {
|
||||
const golden = pythonPycEqualityGolden;
|
||||
const directory = await createTestTempDirectory("rea-pyc-code-equality-");
|
||||
const path = join(directory, "equality.pyc");
|
||||
await writeFile(path, Buffer.from(golden.pyc_base64, "base64"));
|
||||
const result = await runFileCli(cli, {
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const report = completePycEvidenceSchema.parse(
|
||||
result.json,
|
||||
).normalized_result;
|
||||
expect(report.artifact).toMatchObject({ path, sha256: golden.sha256 });
|
||||
expect(report.code_objects[0]?.consts).toEqual(
|
||||
golden.cases.map((sample) => ({
|
||||
kind: "frozenset",
|
||||
length: sample.members,
|
||||
})),
|
||||
);
|
||||
expect(report.code_objects).toHaveLength(golden.code_objects);
|
||||
expect(report.coverage.status).toBe("complete");
|
||||
},
|
||||
);
|
||||
|
||||
const marshalUtf8 = (bytes: Buffer): Buffer => {
|
||||
const length = Buffer.alloc(4);
|
||||
length.writeUInt32LE(bytes.length);
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"producer": "CPython 3.12.14, marshal version 0",
|
||||
"sha256": "79ba971c36cafc602506514a020bdd8bd56d09baaf1fba37c79ea8b620553427",
|
||||
"pyc_base64": "yw0NCgAAAAAAAAAAAAAAAGMAAAAAAAAAAAAAAAAAAAAAAAAAAHMEAAAAlwB5ACgIAAAAPgIAAABjAgAAAAAAAAAAAAAAAgAAAAMAAABzDAAAAJcAfAB8AXoAAABTACgBAAAATigAAAAAKAIAAAB1AQAAAGF1AQAAAGJzAgAAACAgdQsAAABlcXVhbGl0eS5weXUBAAAAZnUBAAAAZgEAAABzCwAAAIAA2AgJiEGJBYAccwAAAABjAgAAAAAAAAAAAAAAAgAAAAMAAABzDAAAAJcAfAB8AXoAAABTACgBAAAATigAAAAAKAIAAAB1AQAAAGF1AQAAAGJzAgAAACAgdQsAAABlcXVh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AAcwwAAACXAHwAfAF6AAAAUwAoAQAAAGYBMSgAAAAAKAIAAAB1AQAAAGF1AQAAAGJzAgAAACAgdQsAAABlcXVhbGl0eS5weXUBAAAAZnUBAAAAZgEAAABzCwAAAIAA2AgJiEGJBYAccwAAAAA+AgAAAGMCAAAAAAAAAAAAAAACAAAAAwAAAHMMAAAAlwB8AHwBegAAAFMAKAEAAABmATAoAAAAACgCAAAAdQEAAABhdQEAAABicwIAAAAgIHULAAAAZXF1YWxpdHkucHl1AQAAAGZ1AQAAAGYBAAAAcwsAAACAANgICYhBiQWAHHMAAAAAYwIAAAAAAAAAAAAAAAIAAAADAAAAcwwAAACXAHwAfAF6AAAAUwAoAQAAAGYCLTAoAAAAACgCAAAAdQEAAABhdQEAAABicwIAAAAgIHULAAAAZXF1YWxpdHkucHl1AQAAAGZ1AQAAAGYBAAAAcwsAAACAANgICYhBiQWAHHMAAAAAKAAAAAAoAAAAAHMAAAAAdRAAAABlcXVhbGl0eS1yb290LnB5dQgAAAA8bW9kdWxlPnUIAAAAPG1vZHVsZT4BAAAAcwgAAADwAwEBAdgABHMAAAAA",
|
||||
"cases": [
|
||||
{
|
||||
"label": "qualified name is ignored",
|
||||
"members": 1
|
||||
},
|
||||
{
|
||||
"label": "stack size is ignored",
|
||||
"members": 1
|
||||
},
|
||||
{
|
||||
"label": "line table distinguishes members",
|
||||
"members": 2
|
||||
},
|
||||
{
|
||||
"label": "exception table distinguishes members",
|
||||
"members": 2
|
||||
},
|
||||
{
|
||||
"label": "first line distinguishes members",
|
||||
"members": 2
|
||||
},
|
||||
{
|
||||
"label": "bool and int constants stay distinct",
|
||||
"members": 2
|
||||
},
|
||||
{
|
||||
"label": "int and float constants stay distinct",
|
||||
"members": 2
|
||||
},
|
||||
{
|
||||
"label": "signed zero constants stay distinct",
|
||||
"members": 2
|
||||
}
|
||||
],
|
||||
"code_objects": 15
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { z } from "zod";
|
||||
|
||||
/** Recorded independently by produce.py; CI needs no matching Python install. */
|
||||
export const pythonPycEqualityGolden = z
|
||||
.strictObject({
|
||||
producer: z.literal("CPython 3.12.14, marshal version 0"),
|
||||
sha256: z.string().regex(/^[0-9a-f]{64}$/u),
|
||||
pyc_base64: z.string(),
|
||||
cases: z.array(
|
||||
z.strictObject({
|
||||
label: z.string(),
|
||||
members: z.union([z.literal(1), z.literal(2)]),
|
||||
}),
|
||||
),
|
||||
code_objects: z.int().positive(),
|
||||
})
|
||||
.parse(
|
||||
JSON.parse(readFileSync(new URL("./golden.json", import.meta.url), "utf8")),
|
||||
);
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
"""Regenerate with CPython 3.12.14: python3 -I -S produce.py [--check].
|
||||
|
||||
Only compile, replace, serialize, and load inert code objects; never execute them.
|
||||
Marshal v0 avoids reference sharing, so every retained member is a distinct object.
|
||||
Two-member frozensets are framed before loading: constructing a Python frozenset
|
||||
first would deduplicate the inputs and conceal the behavior being tested.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import marshal
|
||||
import struct
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
if sys.implementation.name != "cpython" or sys.version_info[:3] != (3, 12, 14):
|
||||
raise SystemExit("This recorded oracle requires CPython 3.12.14.")
|
||||
|
||||
base = compile("def f(a, b):\n return a + b\n", "equality.py", "exec").co_consts[0]
|
||||
handler = compile(
|
||||
"def f(a, b):\n try: return a / b\n except ZeroDivisionError: return None\n",
|
||||
"equality.py",
|
||||
"exec",
|
||||
).co_consts[0]
|
||||
cases = [
|
||||
("qualified name is ignored", base, base.replace(co_qualname="other.f")),
|
||||
("stack size is ignored", base, base.replace(co_stacksize=base.co_stacksize + 1)),
|
||||
("line table distinguishes members", base, base.replace(co_linetable=b"")),
|
||||
("exception table distinguishes members", handler, handler.replace(co_exceptiontable=b"")),
|
||||
(
|
||||
"first line distinguishes members",
|
||||
base,
|
||||
base.replace(co_firstlineno=base.co_firstlineno + 1),
|
||||
),
|
||||
(
|
||||
"bool and int constants stay distinct",
|
||||
base.replace(co_consts=(True,)),
|
||||
base.replace(co_consts=(1,)),
|
||||
),
|
||||
(
|
||||
"int and float constants stay distinct",
|
||||
base.replace(co_consts=(1,)),
|
||||
base.replace(co_consts=(1.0,)),
|
||||
),
|
||||
(
|
||||
"signed zero constants stay distinct",
|
||||
base.replace(co_consts=(0.0,)),
|
||||
base.replace(co_consts=(-0.0,)),
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def frozen_pair(left, right):
|
||||
return b">" + struct.pack("<i", 2) + marshal.dumps(left, 0) + marshal.dumps(right, 0)
|
||||
|
||||
|
||||
marker = (b"REA code-equality producer placeholder",)
|
||||
module = compile("pass", "equality-root.py", "exec").replace(co_consts=marker)
|
||||
serialized = marshal.dumps(module, 0)
|
||||
assert serialized.count(marshal.dumps(marker, 0)) == 1
|
||||
payload = b"(" + struct.pack("<i", len(cases))
|
||||
payload += b"".join(frozen_pair(left, right) for _, left, right in cases)
|
||||
serialized = serialized.replace(marshal.dumps(marker, 0), payload)
|
||||
loaded = marshal.loads(serialized)
|
||||
expected = []
|
||||
for (label, left, right), members in zip(cases, loaded.co_consts, strict=True):
|
||||
assert isinstance(members, frozenset)
|
||||
# CPython's code equality and the independently loaded container must agree.
|
||||
assert len(members) == (1 if left == right else 2)
|
||||
expected.append({"label": label, "members": len(members)})
|
||||
blob = importlib.util.MAGIC_NUMBER + bytes(12) + serialized
|
||||
result = {
|
||||
"producer": "CPython 3.12.14, marshal version 0",
|
||||
"sha256": hashlib.sha256(blob).hexdigest(),
|
||||
"pyc_base64": base64.b64encode(blob).decode("ascii"),
|
||||
"cases": expected,
|
||||
"code_objects": 1 + sum(case["members"] for case in expected),
|
||||
}
|
||||
path = Path(__file__).with_name("golden.json")
|
||||
if sys.argv[1:] == ["--check"]:
|
||||
assert json.loads(path.read_text()) == result, "Golden differs from CPython producer"
|
||||
print(f"CPython oracle matches {len(cases)} cases; bytecode was not executed.")
|
||||
elif not sys.argv[1:]:
|
||||
path.write_text(json.dumps(result, indent=2) + "\n")
|
||||
else:
|
||||
raise SystemExit("Usage: produce.py [--check]")
|
||||
Reference in new issue
Block a user