From a0c836d6193101d94c49f506be6ff0d38ae01894 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 5 Dec 2026 12:58:47 +0800 Subject: [PATCH] test(python): share signed 64-bit marshal constants (#16945) --- src/python/PythonPyc.fixture.ts | 7 +++++++ tests/acceptance/pythonPycCli.test.ts | 9 ++------- tests/boundary/mcp/pythonPycMcp.test.ts | 12 ++---------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/python/PythonPyc.fixture.ts b/src/python/PythonPyc.fixture.ts index 392b9412f21..1898597b2b8 100644 --- a/src/python/PythonPyc.fixture.ts +++ b/src/python/PythonPyc.fixture.ts @@ -37,6 +37,13 @@ export const pythonPycHeader = ( return header; }; +export const signedInt64Constants = (): Buffer[] => + [42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map((value) => { + const payload = Buffer.alloc(8); + payload.writeBigInt64LE(value); + return Buffer.concat([Buffer.from([0x49]), payload]); + }); + const marshalNone = (): Buffer => u8(0x4e); const marshalBytes = (value: Buffer, flagged = false): Buffer => concat(u8(0x73 | (flagged ? FLAG_REF : 0)), u32(value.length), value); diff --git a/tests/acceptance/pythonPycCli.test.ts b/tests/acceptance/pythonPycCli.test.ts index 913f841eeed..4a08e5a159d 100644 --- a/tests/acceptance/pythonPycCli.test.ts +++ b/tests/acceptance/pythonPycCli.test.ts @@ -9,6 +9,7 @@ import { pythonPycFixture, pythonPycHeader, pythonPycWithCode, + signedInt64Constants, } from "../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; import { cliTest } from "../support/cli/cliFixture.js"; @@ -109,12 +110,6 @@ const marshalUtf8 = (bytes: Buffer): Buffer => { return Buffer.concat([Buffer.from([0x75]), length, bytes]); }; -const marshalInt64 = (value: bigint): Buffer => { - const payload = Buffer.alloc(8); - payload.writeBigInt64LE(value); - return Buffer.concat([Buffer.from([0x49]), payload]); -}; - cliTest( "retains distinct Python 3.13 code objects after STORE_SLICE", async ({ cli }) => { @@ -493,7 +488,7 @@ cliTest( const bytes = pythonPycWithCode(3394, "py37", { flags: 0, consts: [ - ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), + ...signedInt64Constants(), Buffer.from([0x69, 0xd6, 0xff, 0xff, 0xff]), ], }); diff --git a/tests/boundary/mcp/pythonPycMcp.test.ts b/tests/boundary/mcp/pythonPycMcp.test.ts index dd73a32f6e0..b3ac5523b62 100644 --- a/tests/boundary/mcp/pythonPycMcp.test.ts +++ b/tests/boundary/mcp/pythonPycMcp.test.ts @@ -16,6 +16,7 @@ import { pythonPycFixture, pythonPycHeader, pythonPycWithCode, + signedInt64Constants, } from "../../../src/python/PythonPyc.fixture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js"; @@ -24,12 +25,6 @@ const completePycEvidenceSchema = toolContract( "inspect_python_pyc", ).outputSchema.safeExtend({ normalized_result: pythonPycSchema }); -const marshalInt64 = (value: bigint): Buffer => { - const payload = Buffer.alloc(8); - payload.writeBigInt64LE(value); - return Buffer.concat([Buffer.from([0x49]), payload]); -}; - const marshalInt32 = (value: number): Buffer => { const payload = Buffer.alloc(4); payload.writeInt32LE(value); @@ -61,10 +56,7 @@ it("reads signed TYPE_INT64 constants and rejects a truncated payload", async () await writeFile( path, pythonPycWithCode(3531, "py311", { - consts: [ - ...[42n, -1n, -(1n << 63n), (1n << 63n) - 1n].map(marshalInt64), - marshalInt32(-42), - ], + consts: [...signedInt64Constants(), marshalInt32(-42)], }), ); const response = await client.callTool({