mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +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
@@ -32,6 +32,10 @@ cliTest(
|
||||
result.json,
|
||||
).normalized_result;
|
||||
expect(report.artifact).toMatchObject({ path, sha256: golden.sha256 });
|
||||
expect(report.header).toMatchObject({
|
||||
python_version: "3.12",
|
||||
code_layout: "py311",
|
||||
});
|
||||
expect(report.code_objects[0]?.consts).toEqual(
|
||||
golden.cases.map((sample) => ({
|
||||
kind: "frozenset",
|
||||
@@ -110,92 +114,6 @@ const marshalInt64 = (value: bigint): Buffer => {
|
||||
return Buffer.concat([Buffer.from([0x49]), payload]);
|
||||
};
|
||||
|
||||
const marshalInt32 = (value: number): Buffer => {
|
||||
const payload = Buffer.alloc(4);
|
||||
payload.writeInt32LE(value);
|
||||
return Buffer.concat([Buffer.from([0x69]), payload]);
|
||||
};
|
||||
|
||||
const marshalSingleItemTuple = (item: Buffer): Buffer =>
|
||||
Buffer.concat([Buffer.from([0x29, 1]), item]);
|
||||
|
||||
cliTest(
|
||||
"reads backward-compatible signed 64-bit marshal integers",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-int64-");
|
||||
for (const [name, value, bits] of [
|
||||
["positive", 42n, 6],
|
||||
["negative", -1n, 1],
|
||||
["minimum", -(1n << 63n), 64],
|
||||
["maximum", (1n << 63n) - 1n, 63],
|
||||
] as const) {
|
||||
const path = join(root, `${name}.pyc`);
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: marshalSingleItemTuple(marshalInt64(value)),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.code_objects[0]?.consts).toContainEqual(
|
||||
{ kind: "int", bits, value: value.toString() },
|
||||
);
|
||||
}
|
||||
|
||||
const ordinaryIntPath = join(root, "ordinary-int.pyc");
|
||||
await writeFile(
|
||||
ordinaryIntPath,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
constsObject: marshalSingleItemTuple(marshalInt32(-42)),
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const ordinaryInt = await cli.run({
|
||||
arguments: ["inspect-python-pyc", ordinaryIntPath, "--json"],
|
||||
});
|
||||
expect(ordinaryInt.exitCode, ordinaryInt.stderr).toBe(0);
|
||||
const ordinaryEvidence = completePycEvidenceSchema.parse(ordinaryInt.json);
|
||||
expect(
|
||||
ordinaryEvidence.normalized_result.code_objects[0]?.consts,
|
||||
).toContainEqual({ kind: "int", bits: 6, value: "-42" });
|
||||
|
||||
const truncatedPath = join(root, "truncated.pyc");
|
||||
await writeFile(
|
||||
truncatedPath,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
Buffer.from([0x49]),
|
||||
Buffer.alloc(7),
|
||||
]),
|
||||
);
|
||||
const truncated = await cli.run({
|
||||
arguments: ["inspect-python-pyc", truncatedPath, "--json"],
|
||||
});
|
||||
expect(truncated.exitCode).toBe(1);
|
||||
expect(truncated.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "inspect_python_pyc",
|
||||
issues: [
|
||||
{
|
||||
reason: "invalid_format",
|
||||
message: expect.stringContaining("64-bit integer"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"retains distinct Python 3.13 code objects after STORE_SLICE",
|
||||
async ({ cli }) => {
|
||||
@@ -363,127 +281,6 @@ cliTest("reads a CPython 3.15 magic 3666 stdlib pyc", async ({ cli }) => {
|
||||
);
|
||||
});
|
||||
|
||||
cliTest(
|
||||
"uses magic-specific cache tables for synthetic pyc bytecode equality",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-inline-cache-");
|
||||
const cases = [
|
||||
{ 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 },
|
||||
];
|
||||
for (const { magic, opcode, cacheUnits } of cases) {
|
||||
const cache = Buffer.alloc(cacheUnits * 2);
|
||||
const bytecode = (arg: number, cachePayload: Buffer) =>
|
||||
Buffer.concat([
|
||||
Buffer.from([opcode, arg]),
|
||||
cachePayload,
|
||||
Buffer.from([83, 0]),
|
||||
]);
|
||||
const changedCache = Buffer.from(cache);
|
||||
changedCache[0] = 1;
|
||||
const frozenSet = Buffer.concat([
|
||||
Buffer.from([0x3e, 4, 0, 0, 0]),
|
||||
...[
|
||||
bytecode(0, cache),
|
||||
bytecode(0, changedCache),
|
||||
bytecode(1, cache),
|
||||
Buffer.concat([bytecode(0, cache), Buffer.from([0, 0])]),
|
||||
].map((code) => marshalCode("py311", { bytecode: code })),
|
||||
]);
|
||||
const pyc = Buffer.concat([
|
||||
pythonPycHeader(magic),
|
||||
marshalCode("py311", { consts: [frozenSet] }),
|
||||
]);
|
||||
const path = join(root, `inline-caches-${magic}.pyc`);
|
||||
await writeFile(path, pyc);
|
||||
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(
|
||||
result.exitCode,
|
||||
`${result.stderr}\n${JSON.stringify(result.json)}`,
|
||||
).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.code_objects[0]?.consts).toEqual([
|
||||
expect.objectContaining({ kind: "frozenset", length: 3 }),
|
||||
]);
|
||||
expect(evidence.normalized_result.code_objects[1]?.bytecode).toEqual({
|
||||
bytes: bytecode(0, cache).length,
|
||||
sha256: createHash("sha256").update(bytecode(0, cache)).digest("hex"),
|
||||
});
|
||||
expect(evidence.normalized_result.artifact.sha256).toBe(
|
||||
createHash("sha256").update(pyc).digest("hex"),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"keeps distinct Python code names distinct when JavaScript display strings collide",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-unicode-name-");
|
||||
const scalar = Buffer.from([0xf0, 0x9f, 0x98, 0x80]);
|
||||
const surrogatePair = Buffer.from([0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80]);
|
||||
const code = (name: Buffer) =>
|
||||
marshalCode("py311", {
|
||||
nameObject: marshalUtf8(name),
|
||||
qualnameObject: marshalUtf8(name),
|
||||
});
|
||||
const frozenSet = Buffer.concat([
|
||||
Buffer.from([0x3e, 2, 0, 0, 0]),
|
||||
code(scalar),
|
||||
code(surrogatePair),
|
||||
]);
|
||||
const path = join(root, "unicode-names.pyc");
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { consts: [frozenSet] }),
|
||||
]),
|
||||
);
|
||||
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(
|
||||
result.exitCode,
|
||||
`${result.stderr}\n${JSON.stringify(result.json)}`,
|
||||
).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
const codes = evidence.normalized_result.code_objects;
|
||||
expect(codes[1]?.name).toBe(codes[2]?.name);
|
||||
expect(codes[0]?.consts).toEqual([
|
||||
expect.objectContaining({ kind: "frozenset", length: 2 }),
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"reports nested CPython code objects through the public CLI",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-cli-");
|
||||
const path = join(root, "sample.pyc");
|
||||
await writeFile(path, pythonPycFixture("py311", { nested: true }));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.header.python_version).toBe("3.12");
|
||||
expect(evidence.normalized_result.code_objects[1]?.name).toBe("inner");
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"classifies unsupported 3.7 alpha headers without labeling their marshal data malformed",
|
||||
async ({ cli }) => {
|
||||
@@ -493,167 +290,21 @@ cliTest(
|
||||
"hex",
|
||||
);
|
||||
expect(marshal).toHaveLength(77);
|
||||
for (const magic of [3390, 3391]) {
|
||||
const path = join(root, `${magic}.pyc`);
|
||||
const header = Buffer.alloc(12);
|
||||
header.writeUInt16LE(magic);
|
||||
header[2] = 0x0d;
|
||||
header[3] = 0x0a;
|
||||
await writeFile(path, Buffer.concat([header, marshal]));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "unsupported_target",
|
||||
category: "unsupported_target",
|
||||
});
|
||||
expect(JSON.stringify(result.json)).toContain(
|
||||
"unsupported 12-byte CPython header",
|
||||
);
|
||||
expect(result.json).not.toHaveProperty("normalized_result");
|
||||
}
|
||||
|
||||
for (const magic of [3392, 3394]) {
|
||||
const path = join(root, `${magic}.pyc`);
|
||||
await writeFile(path, Buffer.concat([pythonPycHeader(magic), marshal]));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.header.magic).toBe(magic);
|
||||
expect(evidence.normalized_result.code_objects[0]?.bytecode.bytes).toBe(
|
||||
4,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves historical signed code fields and reports the ambiguous 3410 constructor revision",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-signed-fields-");
|
||||
for (const { name, magic, layout, fields, expected } of [
|
||||
{
|
||||
name: "zero",
|
||||
magic: 3531,
|
||||
layout: "py311",
|
||||
fields: { flags: 0 },
|
||||
expected: { stacksize: 1, flags: 0 },
|
||||
},
|
||||
{
|
||||
name: "unknown-positive-flags",
|
||||
magic: 3531,
|
||||
layout: "py311",
|
||||
fields: { flags: 0x40000000 },
|
||||
expected: { stacksize: 1, flags: 0x40000000 },
|
||||
},
|
||||
{
|
||||
name: "negative-stacksize-py37",
|
||||
magic: 3394,
|
||||
layout: "py37",
|
||||
fields: { stacksize: 0xffffffff },
|
||||
expected: { stacksize: -1, flags: 0 },
|
||||
},
|
||||
{
|
||||
name: "negative-flags-py38-alpha",
|
||||
magic: 3401,
|
||||
layout: "py37",
|
||||
fields: { flags: 0x80000000 },
|
||||
expected: { stacksize: 1, flags: -0x80000000 },
|
||||
},
|
||||
{
|
||||
name: "negative-stack-and-flags-py38-ambiguous",
|
||||
magic: 3410,
|
||||
layout: "py38",
|
||||
fields: { stacksize: 0xffffffff, flags: 0x80000000 },
|
||||
expected: { stacksize: -1, flags: -0x80000000 },
|
||||
},
|
||||
] as const) {
|
||||
const path = join(root, `${name}.pyc`);
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]),
|
||||
);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.code_objects[0]).toMatchObject(
|
||||
expected,
|
||||
);
|
||||
if (magic === 3410)
|
||||
expect(evidence.normalized_result.limitations).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringContaining("Magic 3410 is shared"),
|
||||
]),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"rejects negative code fields after the pinned constructor validation transition",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory(
|
||||
"rea-python-pyc-signed-rejection-",
|
||||
const path = join(root, "3390.pyc");
|
||||
const header = Buffer.from("3e0d0d0a0000000000000000", "hex");
|
||||
await writeFile(path, Buffer.concat([header, marshal]));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "unsupported_target",
|
||||
category: "unsupported_target",
|
||||
});
|
||||
expect(JSON.stringify(result.json)).toContain(
|
||||
"unsupported 12-byte CPython header",
|
||||
);
|
||||
for (const { name, magic, layout, fields, message } of [
|
||||
{
|
||||
name: "negative-stacksize-py38-b2",
|
||||
magic: 3411,
|
||||
layout: "py38",
|
||||
fields: { stacksize: 0xffffffff },
|
||||
message: "co_stacksize is negative",
|
||||
},
|
||||
{
|
||||
name: "negative-flags-py38-b2",
|
||||
magic: 3411,
|
||||
layout: "py38",
|
||||
fields: { flags: 0x80000000 },
|
||||
message: "co_flags is negative",
|
||||
},
|
||||
{
|
||||
name: "negative-stacksize-py311",
|
||||
magic: 3531,
|
||||
layout: "py311",
|
||||
fields: { stacksize: 0xffffffff },
|
||||
message: "co_stacksize is negative",
|
||||
},
|
||||
{
|
||||
name: "negative-flags-py311",
|
||||
magic: 3531,
|
||||
layout: "py311",
|
||||
fields: { flags: 0x80000000 },
|
||||
message: "co_flags is negative",
|
||||
},
|
||||
] as const) {
|
||||
const path = join(root, `${name}.pyc`);
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([pythonPycHeader(magic), marshalCode(layout, fields)]),
|
||||
);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
category: "invalid_input",
|
||||
details: {
|
||||
operation: "inspect_python_pyc",
|
||||
issues: [
|
||||
{
|
||||
reason: "invalid_format",
|
||||
message: expect.stringContaining(message),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(result.json).not.toHaveProperty("normalized_result");
|
||||
}
|
||||
expect(result.json).not.toHaveProperty("normalized_result");
|
||||
},
|
||||
);
|
||||
|
||||
@@ -737,57 +388,6 @@ cliTest(
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"counts code keys using the selected CPython equality rules",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-equality-");
|
||||
const cases = [
|
||||
{ magic: 3413, layout: "py38" as const, lengths: [1, 1] },
|
||||
{ magic: 3439, layout: "py38" as const, lengths: [1, 1] },
|
||||
{ magic: 3531, layout: "py311" as const, lengths: [2, 2] },
|
||||
];
|
||||
for (const [index, testCase] of cases.entries()) {
|
||||
const code = (linetable: Buffer) =>
|
||||
marshalCode(testCase.layout, {
|
||||
name: "f",
|
||||
filename: "fixture.py",
|
||||
firstlineno: 1,
|
||||
linetable,
|
||||
});
|
||||
const frozenSet = Buffer.concat([
|
||||
Buffer.from([0x3e]),
|
||||
Buffer.from([2, 0, 0, 0]),
|
||||
code(Buffer.from([2, 1])),
|
||||
code(Buffer.from([2, 2])),
|
||||
]);
|
||||
const dict = Buffer.concat([
|
||||
Buffer.from([0x7b]),
|
||||
code(Buffer.from([2, 1])),
|
||||
Buffer.from([0x4e]),
|
||||
code(Buffer.from([2, 2])),
|
||||
Buffer.from([0x54, 0x30]),
|
||||
]);
|
||||
const path = join(root, `case-${index}.pyc`);
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(testCase.magic),
|
||||
marshalCode(testCase.layout, { consts: [frozenSet, dict] }),
|
||||
]),
|
||||
);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.code_objects[0]?.consts).toEqual([
|
||||
{ kind: "frozenset", length: testCase.lengths[0] },
|
||||
{ kind: "dict", length: testCase.lengths[1] },
|
||||
]);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"uses constructor-normalized legacy flags for code keys and preserves raw flags",
|
||||
async ({ cli }) => {
|
||||
@@ -898,7 +498,18 @@ cliTest(
|
||||
"resolves a relative pyc path against the working directory",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-cli-rel-");
|
||||
await writeFile(join(root, "sample.pyc"), pythonPycFixture("py37"));
|
||||
const path = join(root, "sample.pyc");
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3394),
|
||||
marshalCode("py37", {
|
||||
flags: 0,
|
||||
consts: [
|
||||
...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64),
|
||||
Buffer.from([0x69, 0xd6, 0xff, 0xff, 0xff]),
|
||||
],
|
||||
}),
|
||||
]);
|
||||
await writeFile(path, bytes);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", "sample.pyc", "--json"],
|
||||
cwd: root,
|
||||
@@ -906,7 +517,21 @@ cliTest(
|
||||
expect(result.exitCode, result.stderr).toBe(0);
|
||||
const evidence = completePycEvidenceSchema.parse(result.json);
|
||||
expect(evidence.normalized_result.header.python_version).toBe("3.7");
|
||||
expect(evidence.subject?.local_path).toBe(join(root, "sample.pyc"));
|
||||
expect(evidence.subject?.local_path).toBe(path);
|
||||
expect(evidence.normalized_result.artifact).toMatchObject({
|
||||
path,
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
});
|
||||
expect(evidence.normalized_result.code_objects[0]).toMatchObject({
|
||||
flags: 0,
|
||||
consts: [
|
||||
{ kind: "int", bits: 6, value: "42" },
|
||||
{ kind: "int", bits: 1, value: "-1" },
|
||||
{ kind: "int", bits: 64, value: "-9223372036854775808" },
|
||||
{ kind: "int", bits: 63, value: "9223372036854775807" },
|
||||
{ kind: "int", bits: 6, value: "-42" },
|
||||
],
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
@@ -930,19 +555,32 @@ cliTest.skipIf(process.platform === "win32")(
|
||||
},
|
||||
);
|
||||
|
||||
cliTest("reports a truncated pyc as invalid input", async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-cli-bad-");
|
||||
const path = join(root, "broken.pyc");
|
||||
await writeFile(path, Buffer.from("not a pyc"));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: { operation: "inspect_python_pyc" },
|
||||
});
|
||||
});
|
||||
cliTest(
|
||||
"reports a known pyc header truncated after its magic",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-pyc-cli-bad-");
|
||||
const path = join(root, "broken.pyc");
|
||||
await writeFile(path, pythonPycHeader(3531).subarray(0, 8));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "inspect_python_pyc",
|
||||
issues: [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_format",
|
||||
message: expect.stringContaining("shorter than a 16-byte CPython"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(result.json).not.toHaveProperty("normalized_result");
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves whole Unicode scalars in bounded pyc previews through CLI and MCP",
|
||||
@@ -979,13 +617,18 @@ cliTest(
|
||||
truncated: true,
|
||||
},
|
||||
];
|
||||
const consts = cases.map(({ input }) => {
|
||||
const text = Buffer.from(input, "utf8");
|
||||
const header = Buffer.alloc(5);
|
||||
header[0] = 0x75;
|
||||
header.writeUInt32LE(text.length, 1);
|
||||
return Buffer.concat([header, text]);
|
||||
});
|
||||
const prefix = "a".repeat(79);
|
||||
const scalar = Buffer.from(prefix + "𐀀z");
|
||||
const separate = Buffer.concat([
|
||||
Buffer.from(prefix),
|
||||
Buffer.from([0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80]),
|
||||
Buffer.from("z"),
|
||||
]);
|
||||
const consts = [
|
||||
...cases.map(({ input }) => Buffer.from(input, "utf8")),
|
||||
scalar,
|
||||
separate,
|
||||
].map(marshalUtf8);
|
||||
const bytes = Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", { consts }),
|
||||
@@ -1001,58 +644,13 @@ cliTest(
|
||||
response.json,
|
||||
).normalized_result;
|
||||
expect(report.coverage.status).toBe("complete");
|
||||
expect(report.code_objects[0]?.consts).toEqual(
|
||||
cases.map((entry) => ({
|
||||
expect(report.code_objects[0]?.consts).toEqual([
|
||||
...cases.map((entry) => ({
|
||||
kind: "str",
|
||||
value: entry.preview,
|
||||
bytes: entry.bytes,
|
||||
truncated: entry.truncated,
|
||||
})),
|
||||
);
|
||||
const call = await cli.sharedMcpCall?.();
|
||||
if (call === undefined) throw new Error("Expected file MCP session");
|
||||
const mcp = await call("inspect_python_pyc", { path });
|
||||
expect(mcp.isError).not.toBe(true);
|
||||
expect(
|
||||
completePycEvidenceSchema.parse(mcp.structuredContent).normalized_result,
|
||||
).toEqual(report);
|
||||
const toon = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--format", "toon"],
|
||||
});
|
||||
expect(toon.exitCode, toon.stderr).toBe(0);
|
||||
expect(toon.stdout).not.toContain("�");
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"distinguishes source surrogate code points from astral scalars at the preview cut",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-python-source-boundary-");
|
||||
const path = join(root, "source-boundaries.pyc");
|
||||
const prefix = "a".repeat(79);
|
||||
const scalar = Buffer.from(prefix + "𐀀z");
|
||||
const separate = Buffer.concat([
|
||||
Buffer.from(prefix),
|
||||
Buffer.from([0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80]),
|
||||
Buffer.from("z"),
|
||||
]);
|
||||
await writeFile(
|
||||
path,
|
||||
Buffer.concat([
|
||||
pythonPycHeader(3531),
|
||||
marshalCode("py311", {
|
||||
consts: [marshalUtf8(scalar), marshalUtf8(separate)],
|
||||
}),
|
||||
]),
|
||||
);
|
||||
const response = await cli.run({
|
||||
arguments: ["inspect-python-pyc", path, "--json"],
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
const report = completePycEvidenceSchema.parse(
|
||||
response.json,
|
||||
).normalized_result;
|
||||
expect(report.code_objects[0]?.consts).toEqual([
|
||||
{ kind: "str", value: prefix, bytes: 84, truncated: true },
|
||||
{ kind: "str", value: prefix + "\ud800", bytes: 86, truncated: true },
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user