fix(python): validate marshal coverage and consolidate pyc tests (#14036)

This commit is contained in:
morluto authored and GitHub committed 2026-11-23 06:55:30 -05:00
1 parent ca26f2ac84
commit 4ff98b84fb
9 files changed
+234 -273

No files matched your search

@@ -1,21 +0,0 @@
import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { pythonPycBytecodeIdentity } from "./PythonPycBytecodeEquality.js";
const raw = (bytes: Buffer): string =>
createHash("sha256").update(bytes).digest("hex");
const identity = (bytes: Buffer, magic: number): string =>
pythonPycBytecodeIdentity(bytes, magic, raw(bytes));
it("skips inline-cache units only under the pinned 3.14 and 3.15 magics", () => {
// Opcode 80 carries nine cache code units in the 3627 table; only the cache
// bytes differ between the two bytecodes.
const first = Buffer.from([80, 0, ...Array(18).fill(1), 83, 0]);
const second = Buffer.from([80, 0, ...Array(18).fill(9), 83, 0]);
expect(identity(first, 3627)).toBe(identity(second, 3627));
// A magic with no pinned table keeps the raw bytecode digest, so code
// objects that differ only in cache units stay distinct.
expect(identity(first, 3619)).toBe(`${String(first.length)}:${raw(first)}`);
expect(identity(first, 3619)).not.toBe(identity(second, 3619));
});
+14 -5
View File
@@ -108,9 +108,16 @@ it.each([
{ magic: 3571, opcode: 82, cacheUnits: 9 },
{ magic: 3627, opcode: 80, cacheUnits: 9 },
{ magic: 3666, opcode: 128, cacheUnits: 1 },
{
magic: 3394,
layout: "py37" as const,
opcode: 80,
cacheUnits: 9,
distinct: 5,
},
])(
"uses the $magic cache width for code equality and retains raw bytecode",
({ magic, opcode, cacheUnits }) => {
"uses the $magic bytecode equality rules and retains raw bytecode",
({ magic, layout = "py311" as const, opcode, cacheUnits, distinct = 4 }) => {
const cache = Buffer.alloc(cacheUnits * 2);
const bytecode = (arg: number, cachePayload: Buffer, nextArg = 0) =>
Buffer.concat([
@@ -129,15 +136,17 @@ it.each([
bytecode(1, cache),
bytecode(0, cache, 1),
Buffer.concat([original, Buffer.from([0, 0])]),
].map((code) => marshalCode("py311", { bytecode: code })),
].map((code) => marshalCode(layout, { bytecode: code })),
]);
const report = parse(
Buffer.concat([
pythonPycHeader(magic),
marshalCode("py311", { consts: [frozenSet] }),
marshalCode(layout, { consts: [frozenSet] }),
]),
);
expect(root(report).consts).toEqual([{ kind: "frozenset", length: 4 }]);
expect(root(report).consts).toEqual([
{ kind: "frozenset", length: distinct },
]);
expect(report.code_objects[1]?.bytecode).toEqual({
bytes: original.length,
sha256: createHash("sha256").update(original).digest("hex"),