mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
test(python): consolidate pyc parser and CLI coverage (#13566)
This commit is contained in:
1 parent
2be4cc468e
commit
917ab7f5fd
2 files changed
+207
-496
No files matched your search
@@ -55,7 +55,15 @@ const measure = (bytes: Buffer) => {
|
||||
};
|
||||
|
||||
it("reads 3.7, 3.10 and 3.12 layouts including nested code", () => {
|
||||
const earliestTimestamp = parse(pythonPycFixture("py37", { magic: 3392 }));
|
||||
expect(earliestTimestamp.header).toMatchObject({
|
||||
magic: 3392,
|
||||
python_version: "3.7",
|
||||
code_layout: "py37",
|
||||
});
|
||||
expect(root(earliestTimestamp).bytecode.bytes).toBe(4);
|
||||
const py37 = parse(pythonPycFixture("py37"));
|
||||
expect(root(py37).bytecode.bytes).toBe(4);
|
||||
expect(py37.header).toMatchObject({
|
||||
magic: 3394,
|
||||
python_version: "3.7",
|
||||
@@ -89,6 +97,106 @@ it("reads 3.7, 3.10 and 3.12 layouts including nested code", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{ magic: 3495, opcode: 166, cacheUnits: 1 },
|
||||
{ magic: 3500, opcode: 25, cacheUnits: 4 },
|
||||
{ magic: 3524, opcode: 25, cacheUnits: 1 },
|
||||
{ magic: 3531, opcode: 106, cacheUnits: 9 },
|
||||
{ magic: 3550, opcode: 25, cacheUnits: 1 },
|
||||
{ magic: 3553, opcode: 25, cacheUnits: 1 },
|
||||
{ magic: 3564, opcode: 58, cacheUnits: 1 },
|
||||
{ magic: 3571, opcode: 82, cacheUnits: 9 },
|
||||
{ magic: 3627, opcode: 80, cacheUnits: 9 },
|
||||
{ magic: 3666, opcode: 128, cacheUnits: 1 },
|
||||
])(
|
||||
"uses the $magic cache width for code equality and retains raw bytecode",
|
||||
({ magic, opcode, cacheUnits }) => {
|
||||
const cache = Buffer.alloc(cacheUnits * 2);
|
||||
const bytecode = (arg: number, cachePayload: Buffer, nextArg = 0) =>
|
||||
Buffer.concat([
|
||||
Buffer.from([opcode, arg]),
|
||||
cachePayload,
|
||||
Buffer.from([83, nextArg]),
|
||||
]);
|
||||
const original = bytecode(0, cache);
|
||||
const changedCache = Buffer.from(cache);
|
||||
changedCache[changedCache.length - 1] = 1;
|
||||
const frozenSet = Buffer.concat([
|
||||
Buffer.from([0x3e, 5, 0, 0, 0]),
|
||||
...[
|
||||
original,
|
||||
bytecode(0, changedCache),
|
||||
bytecode(1, cache),
|
||||
bytecode(0, cache, 1),
|
||||
Buffer.concat([original, Buffer.from([0, 0])]),
|
||||
].map((code) => marshalCode("py311", { bytecode: code })),
|
||||
]);
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(magic),
|
||||
marshalCode("py311", { consts: [frozenSet] }),
|
||||
]),
|
||||
);
|
||||
expect(root(report).consts).toEqual([{ kind: "frozenset", length: 4 }]);
|
||||
expect(report.code_objects[1]?.bytecode).toEqual({
|
||||
bytes: original.length,
|
||||
sha256: createHash("sha256").update(original).digest("hex"),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ magic: 3413, layout: "py38" as const, expected: 1 },
|
||||
{ magic: 3439, layout: "py38" as const, expected: 1 },
|
||||
{ magic: 3531, layout: "py311" as const, expected: 2 },
|
||||
])(
|
||||
"uses the $magic equality rules for line tables in sets and dictionaries",
|
||||
({ magic, layout, expected }) => {
|
||||
const code = (linetable: Buffer) =>
|
||||
marshalCode(layout, { name: "f", firstlineno: 1, linetable });
|
||||
const first = code(Buffer.from([2, 1]));
|
||||
const second = code(Buffer.from([2, 2]));
|
||||
const set = Buffer.concat([Buffer.from([0x3e, 2, 0, 0, 0]), first, second]);
|
||||
const dictionary = Buffer.concat([
|
||||
Buffer.from([0x7b]),
|
||||
first,
|
||||
Buffer.from([0x4e]),
|
||||
second,
|
||||
Buffer.from([0x54, 0x30]),
|
||||
]);
|
||||
const report = parse(
|
||||
Buffer.concat([
|
||||
pythonPycHeader(magic),
|
||||
marshalCode(layout, { consts: [set, dictionary] }),
|
||||
]),
|
||||
);
|
||||
expect(root(report).consts).toEqual([
|
||||
{ kind: "frozenset", length: expected },
|
||||
{ kind: "dict", length: expected },
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
it("distinguishes code names with identical JavaScript display text", () => {
|
||||
const scalar = marshalUnicode(Buffer.from([0xf0, 0x9f, 0x98, 0x80]));
|
||||
const surrogatePair = marshalUnicode(
|
||||
Buffer.from([0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80]),
|
||||
);
|
||||
const code = (name: Buffer) =>
|
||||
marshalCode("py311", { nameObject: name, qualnameObject: name });
|
||||
const set = Buffer.concat([
|
||||
Buffer.from([0x3e, 2, 0, 0, 0]),
|
||||
code(scalar),
|
||||
code(surrogatePair),
|
||||
]);
|
||||
const report = parse(withConsts([set]));
|
||||
expect(report.code_objects.slice(1).map(({ name }) => name)).toEqual([
|
||||
"😀",
|
||||
"😀",
|
||||
]);
|
||||
expect(root(report).consts).toEqual([{ kind: "frozenset", length: 2 }]);
|
||||
});
|
||||
|
||||
it("preserves signed co_firstlineno in code objects and code constants", () => {
|
||||
const nested = marshalCode("py311", {
|
||||
name: "inner",
|
||||
@@ -1792,15 +1900,20 @@ it("rejects code CPython treats as corrupt instead of complete coverage", () =>
|
||||
expect(() => parse(file("py37", 3394, { argcount: 1 }))).toThrow(
|
||||
/co_varnames is too small/,
|
||||
);
|
||||
expect(() => parse(file("py311", 3531, { flags: 0x80000000 }))).toThrow(
|
||||
/co_flags is negative/,
|
||||
);
|
||||
expect(() => parse(file("py38", 3411, { flags: 0x80000000 }))).toThrow(
|
||||
/co_flags is negative/,
|
||||
);
|
||||
expect(() => parse(file("py38", 3411, { stacksize: 0xffffffff }))).toThrow(
|
||||
/co_stacksize is negative/,
|
||||
);
|
||||
for (const [layout, magic] of [
|
||||
["py38", 3411],
|
||||
["py311", 3531],
|
||||
] as const)
|
||||
for (const [fields, message] of [
|
||||
[{ stacksize: 0xffffffff }, "co_stacksize is negative"],
|
||||
[{ flags: 0x80000000 }, "co_flags is negative"],
|
||||
] as const)
|
||||
expect(() => parse(file(layout, magic, fields))).toThrow(
|
||||
expect.objectContaining({
|
||||
kind: "malformed",
|
||||
message: expect.stringContaining(message),
|
||||
}),
|
||||
);
|
||||
|
||||
const valid = parse(
|
||||
file("py311", 3531, {
|
||||
|
||||
Reference in new issue
Block a user