mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
test(python): share pyc code fixture construction (#15652)
This commit is contained in:
1 parent
2f304e5c29
commit
caf4550b42
5 files changed
+184
-269
No files matched your search
@@ -146,6 +146,13 @@ export const marshalCode = (
|
||||
return concat(...parts);
|
||||
};
|
||||
|
||||
/** A timestamp-header pyc with independently selected magic and code layout. */
|
||||
export const pythonPycWithCode = (
|
||||
magic: number,
|
||||
layout: PythonPycCodeLayout,
|
||||
fields: FixtureCodeFields = {},
|
||||
): Buffer => concat(pythonPycHeader(magic), marshalCode(layout, fields));
|
||||
|
||||
/** Complete pyc with a module code object and optional nested function. */
|
||||
export const pythonPycFixture = (
|
||||
layout: PythonPycCodeLayout = "py311",
|
||||
|
||||
+120
-186
@@ -13,6 +13,7 @@ import {
|
||||
marshalCode,
|
||||
pythonPycFixture,
|
||||
pythonPycHeader,
|
||||
pythonPycWithCode,
|
||||
} from "./PythonPyc.fixture.js";
|
||||
import { parsePythonPyc } from "./PythonPycParser.js";
|
||||
import {
|
||||
@@ -143,10 +144,7 @@ it.each([
|
||||
].map((code) => marshalCode(layout, { bytecode: code })),
|
||||
]);
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(magic),
|
||||
marshalCode(layout, { consts: [frozenSet] }),
|
||||
]),
|
||||
pythonPycWithCode(magic, layout, { consts: [frozenSet] }),
|
||||
);
|
||||
expect(root(report).consts).toEqual([
|
||||
{ kind: "frozenset", length: distinct },
|
||||
@@ -178,10 +176,7 @@ it.each([
|
||||
Buffer.from([0x54, 0x30]),
|
||||
]);
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(magic),
|
||||
marshalCode(layout, { consts: [set, dictionary] }),
|
||||
]),
|
||||
pythonPycWithCode(magic, layout, { consts: [set, dictionary] }),
|
||||
);
|
||||
expect(root(report).consts).toEqual([
|
||||
{ kind: "frozenset", length: expected },
|
||||
@@ -215,13 +210,10 @@ it("preserves signed co_firstlineno in code objects and code constants", () => {
|
||||
name: "inner",
|
||||
firstlineno: -7,
|
||||
});
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
firstlineno: -1,
|
||||
consts: [nested],
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
firstlineno: -1,
|
||||
consts: [nested],
|
||||
});
|
||||
const report = parse(bytes);
|
||||
expect(root(report).firstlineno).toBe(-1);
|
||||
expect(root(report).consts).toContainEqual({
|
||||
@@ -305,9 +297,7 @@ it.each([
|
||||
] as const)(
|
||||
"decodes supported standalone magic $magic with its own marshal layout",
|
||||
({ magic, layout, version }) => {
|
||||
const result = parse(
|
||||
Buffer.concat([pythonPycHeader(magic), marshalCode(layout)]),
|
||||
);
|
||||
const result = parse(pythonPycWithCode(magic, layout));
|
||||
expect(result.header).toMatchObject({
|
||||
magic,
|
||||
python_version: version,
|
||||
@@ -341,7 +331,7 @@ it("does not qualify a magic outside the pinned identity table", () => {
|
||||
});
|
||||
|
||||
const withSliceConsts = (magic: number, consts: Buffer[]): Buffer =>
|
||||
Buffer.concat([pythonPycHeader(magic), marshalCode("py311", { consts })]);
|
||||
pythonPycWithCode(magic, "py311", { consts });
|
||||
const marshalSlice = (
|
||||
start: Buffer,
|
||||
stop: Buffer,
|
||||
@@ -508,7 +498,7 @@ const marshalLong = (digits: readonly number[], negative = false): Buffer =>
|
||||
const marshalUnicode = (bytes: Buffer): Buffer =>
|
||||
Buffer.concat([Buffer.from([0x75]), u32(bytes.length), bytes]);
|
||||
const withConsts = (consts: Buffer[]): Buffer =>
|
||||
Buffer.concat([pythonPycHeader(3531), marshalCode("py311", { consts })]);
|
||||
pythonPycWithCode(3531, "py311", { consts });
|
||||
const CO_VARARGS = 0x04;
|
||||
|
||||
/** Each level keeps one flagged child and two TYPE_REF copies of it. */
|
||||
@@ -557,10 +547,7 @@ it("bounds names repeated through TYPE_REF by what the report carries", () => {
|
||||
name,
|
||||
...Array.from({ length: 100 }, () => marshalRef(0)),
|
||||
]);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { namesObject }),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", { namesObject });
|
||||
expect(() =>
|
||||
parsePythonPyc(
|
||||
bytes,
|
||||
@@ -591,12 +578,9 @@ it("charges shared empty name tables against the object budget", () => {
|
||||
flagged: true,
|
||||
namesObject: index === 0 ? namesObject : marshalRef(1),
|
||||
});
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
consts: Array.from({ length: owners }, (_, index) => owner(index)),
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
consts: Array.from({ length: owners }, (_, index) => owner(index)),
|
||||
});
|
||||
const report = parseWith(bytes, {
|
||||
max_objects: 65_536,
|
||||
max_file_bytes: bytes.length,
|
||||
@@ -727,12 +711,7 @@ it("keeps signed zero and non-finite float and complex constants JSON-safe", ()
|
||||
});
|
||||
|
||||
it("accepts an empty co_filename", () => {
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { filename: "" }),
|
||||
]),
|
||||
);
|
||||
const report = parse(pythonPycWithCode(3531, "py311", { filename: "" }));
|
||||
expect(root(report).filename).toBe("");
|
||||
expect(pythonPycSchema.safeParse(report).success).toBe(true);
|
||||
});
|
||||
@@ -740,10 +719,9 @@ it("accepts an empty co_filename", () => {
|
||||
it("rejects a code object whose location tables are not bytes", () => {
|
||||
expect(() =>
|
||||
parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { exceptiontableObject: Buffer.from([0x53]) }),
|
||||
]),
|
||||
pythonPycWithCode(3531, "py311", {
|
||||
exceptiontableObject: Buffer.from([0x53]),
|
||||
}),
|
||||
),
|
||||
).toThrow(ParseFailureError);
|
||||
});
|
||||
@@ -751,19 +729,16 @@ it("rejects a code object whose location tables are not bytes", () => {
|
||||
it("hashes one shared byte string no matter how many times it is referenced", () => {
|
||||
const payload = Buffer.alloc(1024 * 1024, 0x61);
|
||||
const refs = 1200;
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(refs + 1),
|
||||
Buffer.from([0xf3]),
|
||||
u32(payload.length),
|
||||
payload,
|
||||
...Array.from({ length: refs }, () => marshalRef(0)),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(refs + 1),
|
||||
Buffer.from([0xf3]),
|
||||
u32(payload.length),
|
||||
payload,
|
||||
...Array.from({ length: refs }, () => marshalRef(0)),
|
||||
]),
|
||||
});
|
||||
const { report, work } = measure(bytes);
|
||||
expect(work.constSummaries).toBe(1);
|
||||
expect(work.digestedBytes).toBeLessThan(2 * payload.length);
|
||||
@@ -782,17 +757,14 @@ it("rejects constant tables whose copies exceed max_objects", () => {
|
||||
]);
|
||||
const child = (constsObject: Buffer, name: string) =>
|
||||
marshalCode("py311", { constsObject, name });
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
consts: [
|
||||
child(shared, "a"),
|
||||
...Array.from({ length: 5 }, (_, index) =>
|
||||
child(marshalRef(0), `b${index}`),
|
||||
),
|
||||
],
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
consts: [
|
||||
child(shared, "a"),
|
||||
...Array.from({ length: 5 }, (_, index) =>
|
||||
child(marshalRef(0), `b${index}`),
|
||||
),
|
||||
],
|
||||
});
|
||||
const artifact = {
|
||||
path: defaults.path,
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
@@ -824,23 +796,17 @@ it("rejects NULL tuple items and mismatched local kinds", () => {
|
||||
});
|
||||
expect(() =>
|
||||
parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
localsplusnames: ["v"],
|
||||
localspluskinds: Buffer.alloc(0),
|
||||
}),
|
||||
]),
|
||||
pythonPycWithCode(3531, "py311", {
|
||||
localsplusnames: ["v"],
|
||||
localspluskinds: Buffer.alloc(0),
|
||||
}),
|
||||
),
|
||||
).toThrow(ParseFailureError);
|
||||
const matched = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
localsplusnames: ["v"],
|
||||
localspluskinds: Buffer.from([0x20]),
|
||||
}),
|
||||
]),
|
||||
pythonPycWithCode(3531, "py311", {
|
||||
localsplusnames: ["v"],
|
||||
localspluskinds: Buffer.from([0x20]),
|
||||
}),
|
||||
);
|
||||
expect(root(matched).localsplusnames).toEqual(["v"]);
|
||||
expect(root(matched).localspluskinds_hex).toBe("20");
|
||||
@@ -893,17 +859,14 @@ it("charges every reported code-constant name against the file budget", () => {
|
||||
flagged: true,
|
||||
});
|
||||
const file = (copies: number): Buffer =>
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(copies),
|
||||
child,
|
||||
...Array.from({ length: copies - 1 }, () => marshalRef(0)),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(copies),
|
||||
child,
|
||||
...Array.from({ length: copies - 1 }, () => marshalRef(0)),
|
||||
]),
|
||||
});
|
||||
const once = parseLimited(file(1), 20_000);
|
||||
expect(root(once).consts[0]).toMatchObject({
|
||||
kind: "code",
|
||||
@@ -923,17 +886,14 @@ it("reuses a shared integer's bit length instead of converting every reference",
|
||||
long.subarray(1),
|
||||
]);
|
||||
const refs = 400;
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(refs + 1),
|
||||
flagged,
|
||||
...Array.from({ length: refs }, () => marshalRef(0)),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(refs + 1),
|
||||
flagged,
|
||||
...Array.from({ length: refs }, () => marshalRef(0)),
|
||||
]),
|
||||
});
|
||||
const { report, work } = measure(bytes);
|
||||
// One summary carries the bit length for the integer and all its references.
|
||||
expect(work.constSummaries).toBe(1);
|
||||
@@ -1045,14 +1005,11 @@ it("reads marshal version 0 text floats inside a code object", () => {
|
||||
it("accepts empty code names", () => {
|
||||
const child = marshalCode("py311", { name: "", qualname: "" });
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
name: "",
|
||||
qualname: "",
|
||||
consts: [child],
|
||||
}),
|
||||
]),
|
||||
pythonPycWithCode(3531, "py311", {
|
||||
name: "",
|
||||
qualname: "",
|
||||
consts: [child],
|
||||
}),
|
||||
);
|
||||
expect(root(report).name).toBe("");
|
||||
expect(root(report).qualname).toBe("");
|
||||
@@ -1197,12 +1154,9 @@ it("lists a shared container's code objects once per owner and charges each copy
|
||||
child,
|
||||
...Array.from({ length: copies - 1 }, () => marshalRef(1)),
|
||||
]);
|
||||
const repeated = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
consts: [container, ...Array.from({ length: 7 }, () => marshalRef(0))],
|
||||
}),
|
||||
]);
|
||||
const repeated = pythonPycWithCode(3531, "py311", {
|
||||
consts: [container, ...Array.from({ length: 7 }, () => marshalRef(0))],
|
||||
});
|
||||
const { report: shared, work } = measure(repeated);
|
||||
// Eight owners share one walk of the container and its children.
|
||||
expect(work.listedChildren).toBe(copies);
|
||||
@@ -1226,20 +1180,17 @@ it("lists a shared container's code objects once per owner and charges each copy
|
||||
flagged: true,
|
||||
constsObject: ownedContainer,
|
||||
});
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
consts: [
|
||||
first,
|
||||
...Array.from({ length: owners - 1 }, (_, index) =>
|
||||
marshalCode("py311", {
|
||||
name: `owner${index + 1}`,
|
||||
constsObject: marshalRef(1),
|
||||
}),
|
||||
),
|
||||
],
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
consts: [
|
||||
first,
|
||||
...Array.from({ length: owners - 1 }, (_, index) =>
|
||||
marshalCode("py311", {
|
||||
name: `owner${index + 1}`,
|
||||
constsObject: marshalRef(1),
|
||||
}),
|
||||
),
|
||||
],
|
||||
});
|
||||
const report = parse(bytes);
|
||||
expect(report.code_objects.map((code) => code.name)).toEqual([
|
||||
"<module>",
|
||||
@@ -1504,10 +1455,9 @@ it("keeps one descendant list when many wrappers share a container", () => {
|
||||
const shared = marshalRef(0);
|
||||
for (let index = 0; index < wrappers; index += 1)
|
||||
pieces.push(Buffer.from([0x29 | 0x80, 1]), shared);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { constsObject: Buffer.concat(pieces) }),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat(pieces),
|
||||
});
|
||||
const before = process.memoryUsage().heapUsed;
|
||||
const report = parseWith(bytes, { max_objects: 65_536 });
|
||||
const retained = process.memoryUsage().heapUsed - before;
|
||||
@@ -1645,10 +1595,9 @@ it("follows a long TYPE_REF chain without a recursive walk", () => {
|
||||
pieces.push(index === 0 ? hidden : marshalRef(index - 1));
|
||||
}
|
||||
pieces.push(later);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { constsObject: Buffer.concat(pieces) }),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat(pieces),
|
||||
});
|
||||
const { report, work } = measure(bytes);
|
||||
expect(work.listedChildren).toBe(links);
|
||||
expect(work.nestedSteps).toBe(2 * links);
|
||||
@@ -1673,11 +1622,7 @@ it("rejects negative CPython code counts and keeps them out of the schema", () =
|
||||
nlocals?: number;
|
||||
stacksize?: number;
|
||||
},
|
||||
) =>
|
||||
Buffer.concat([
|
||||
pythonPycHeader(layout === "py37" ? 3394 : 3531),
|
||||
marshalCode(layout, fields),
|
||||
]);
|
||||
) => pythonPycWithCode(layout === "py37" ? 3394 : 3531, layout, fields);
|
||||
for (const field of [
|
||||
"argcount",
|
||||
"posonlyargcount",
|
||||
@@ -1751,18 +1696,15 @@ it("keeps code objects whose name tables differ only by boundaries", () => {
|
||||
expect(pythonPycSchema.safeParse(split).success).toBe(true);
|
||||
|
||||
const tables = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3439),
|
||||
marshalCode("py38", {
|
||||
consts: [
|
||||
frozen(
|
||||
code("py38", { varnames: ["a"], cellvars: ["b"] }),
|
||||
code("py38", { varnames: ["a", "b"] }),
|
||||
code("py38", { varnames: ["a"], freevars: ["b"] }),
|
||||
),
|
||||
],
|
||||
}),
|
||||
]),
|
||||
pythonPycWithCode(3439, "py38", {
|
||||
consts: [
|
||||
frozen(
|
||||
code("py38", { varnames: ["a"], cellvars: ["b"] }),
|
||||
code("py38", { varnames: ["a", "b"] }),
|
||||
code("py38", { varnames: ["a"], freevars: ["b"] }),
|
||||
),
|
||||
],
|
||||
}),
|
||||
);
|
||||
expect(root(tables).consts[0]).toEqual({ kind: "frozenset", length: 3 });
|
||||
expect(tables.code_objects).toHaveLength(4);
|
||||
@@ -1787,10 +1729,9 @@ it("hashes a long TYPE_REF tuple chain without recursing", () => {
|
||||
marshalRef(links),
|
||||
marshalRef(links + 1),
|
||||
);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { constsObject: Buffer.concat(pieces) }),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat(pieces),
|
||||
});
|
||||
expect(bytes.length).toBeLessThan(64_000);
|
||||
const { report, work } = measure(bytes);
|
||||
// Two key steps per link: each tuple and its reference to the previous one.
|
||||
@@ -1810,19 +1751,16 @@ it("hashes shallow TYPE_REF code chains without using the JavaScript stack", ()
|
||||
consts: index === 0 ? [Buffer.from([0x4e])] : [marshalRef(index - 1)],
|
||||
}),
|
||||
);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(links + 1),
|
||||
...codes,
|
||||
Buffer.from([0x3c]),
|
||||
u32(1),
|
||||
marshalRef(links - 1),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3531, "py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(links + 1),
|
||||
...codes,
|
||||
Buffer.from([0x3c]),
|
||||
u32(1),
|
||||
marshalRef(links - 1),
|
||||
]),
|
||||
});
|
||||
const report = parseWith(bytes, { max_objects: 65_536 });
|
||||
expect(root(report).consts.at(-1)).toEqual({ kind: "set", length: 1 });
|
||||
expect(report.coverage).toMatchObject({
|
||||
@@ -1851,19 +1789,16 @@ it("hashes slice-wrapped TYPE_REF code chains without using the JavaScript stack
|
||||
],
|
||||
}),
|
||||
);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3627),
|
||||
marshalCode("py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(links + 1),
|
||||
...codes,
|
||||
Buffer.from([0x3c]),
|
||||
u32(1),
|
||||
marshalRef(links - 1),
|
||||
]),
|
||||
}),
|
||||
]);
|
||||
const bytes = pythonPycWithCode(3627, "py311", {
|
||||
constsObject: Buffer.concat([
|
||||
Buffer.from([0x28]),
|
||||
u32(links + 1),
|
||||
...codes,
|
||||
Buffer.from([0x3c]),
|
||||
u32(1),
|
||||
marshalRef(links - 1),
|
||||
]),
|
||||
});
|
||||
const report = parseWith(bytes, { max_objects: 65_536 });
|
||||
expect(root(report).consts.at(-1)).toEqual({ kind: "set", length: 1 });
|
||||
expect(report.coverage).toMatchObject({
|
||||
@@ -1877,8 +1812,7 @@ it("rejects code CPython treats as corrupt instead of complete coverage", () =>
|
||||
layout: "py37" | "py38" | "py311",
|
||||
magic: number,
|
||||
fields: Parameters<typeof marshalCode>[1],
|
||||
): Buffer =>
|
||||
Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]);
|
||||
): Buffer => pythonPycWithCode(magic, layout, fields);
|
||||
expect(() =>
|
||||
parse(file("py311", 3531, { bytecode: Buffer.from([0x53]) })),
|
||||
).toThrow(/co_code is malformed/);
|
||||
@@ -1975,7 +1909,7 @@ it("rejects code CPython treats as corrupt instead of complete coverage", () =>
|
||||
|
||||
it("leaves 3.11 alpha magics with other code layouts unsupported", () => {
|
||||
for (const magic of [3452, 3459, 3460, 3493]) {
|
||||
const bytes = Buffer.concat([pythonPycHeader(magic), marshalCode("py311")]);
|
||||
const bytes = pythonPycWithCode(magic, "py311");
|
||||
expect(() => parse(bytes)).toThrow(
|
||||
expect.objectContaining({ kind: "unsupported" }),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user