From cf1d2c0f932bc4c8afe23d1f6c0d6f609d036d39 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sat, 5 Dec 2026 15:11:56 +0800 Subject: [PATCH] test(python): reuse the marshal small tuple fixture (#16975) --- src/artifacts/pyinstaller/CArchive.fixture.ts | 4 +--- src/python/PythonPyc.fixture.ts | 2 +- src/python/PythonPycParser.test.ts | 15 ++++++--------- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/artifacts/pyinstaller/CArchive.fixture.ts b/src/artifacts/pyinstaller/CArchive.fixture.ts index ec887110c80..3ee2354ca62 100644 --- a/src/artifacts/pyinstaller/CArchive.fixture.ts +++ b/src/artifacts/pyinstaller/CArchive.fixture.ts @@ -1,6 +1,7 @@ import { deflateSync } from "node:zlib"; import { pe } from "../../domain/binaryTarget.fixture.js"; import { + marshalSmallTuple as smallTuple, pythonPycFixture, pythonPycHeader, } from "../../python/PythonPyc.fixture.js"; @@ -286,9 +287,6 @@ const marshalPair = ( smallTuple([int32(module.typeCode), int32(offset), int32(length)]), ]); -const smallTuple = (items: readonly Buffer[]): Buffer => - Buffer.concat([Buffer.from([0x29, items.length]), ...items]); - const marshalString = (text: string | Buffer, interned: boolean): Buffer => { const body = Buffer.isBuffer(text) ? text : Buffer.from(text, "utf8"); if (!interned && body.length <= 255 && body.every((byte) => byte < 0x80)) diff --git a/src/python/PythonPyc.fixture.ts b/src/python/PythonPyc.fixture.ts index 1898597b2b8..90bc67c18ae 100644 --- a/src/python/PythonPyc.fixture.ts +++ b/src/python/PythonPyc.fixture.ts @@ -53,7 +53,7 @@ const marshalShortAscii = (value: string, interned = false): Buffer => u8(Buffer.byteLength(value, "utf8")), Buffer.from(value, "utf8"), ); -const marshalSmallTuple = (items: Buffer[]): Buffer => +export const marshalSmallTuple = (items: readonly Buffer[]): Buffer => concat(u8(0x29), u8(items.length), ...items); export type FixtureCodeFields = { diff --git a/src/python/PythonPycParser.test.ts b/src/python/PythonPycParser.test.ts index 56c356a77c3..0d8cccc1848 100644 --- a/src/python/PythonPycParser.test.ts +++ b/src/python/PythonPycParser.test.ts @@ -11,6 +11,7 @@ import { } from "../domain/python/pythonPyc.js"; import { marshalCode, + marshalSmallTuple as tuple, pythonPycFixture, pythonPycHeader, pythonPycWithCode, @@ -1264,15 +1265,13 @@ it("accepts code objects as set members and dictionary keys", () => { it("rejects code keys and members whose consts are unhashable", () => { const list = Buffer.concat([Buffer.from([0x5b]), u32(0)]); const int = Buffer.concat([Buffer.from([0x69]), i32(1)]); - const tuple = (...items: Buffer[]): Buffer => - Buffer.concat([Buffer.from([0x29, items.length]), ...items]); const code = (...consts: Buffer[]): Buffer => marshalCode("py311", { name: "u", qualname: "u", consts }); const setOf = (kind: number, member: Buffer): Buffer => Buffer.concat([Buffer.from([kind]), u32(1), member]); const dictKey = (key: Buffer): Buffer => Buffer.concat([Buffer.from([0x7b]), key, int, Buffer.from([0x30])]); - for (const key of [code(list), code(tuple(int, list)), code(code(list))]) { + for (const key of [code(list), code(tuple([int, list])), code(code(list))]) { expect(() => parse(withConsts([setOf(0x3c, key)]))).toThrow( ParseFailureError, ); @@ -1287,7 +1286,7 @@ it("rejects code keys and members whose consts are unhashable", () => { name: "u", qualname: "u", flagged: true, - constsObject: tuple(marshalRef(0)), + constsObject: tuple([marshalRef(0)]), }), ]); expect(() => parse(withConsts([cyclic, setOf(0x3c, marshalRef(1))]))).toThrow( @@ -1301,7 +1300,7 @@ it("rejects code keys and members whose consts are unhashable", () => { marshalCode("py311", { name: "u", qualname: "u", - constsObject: tuple(marshalRef(0)), + constsObject: tuple([marshalRef(0)]), }), marshalCode("py311", { name: "u", @@ -1321,13 +1320,13 @@ it("rejects code keys and members whose consts are unhashable", () => { ), ).toThrow(ParseFailureError); - const unhashed = parse(withConsts([tuple(code(list))])); + const unhashed = parse(withConsts([tuple([code(list)])])); expect(unhashed.coverage.status).toBe("complete"); expect(unhashed.code_objects[1]?.consts[0]).toEqual({ kind: "list", length: 0, }); - const hashable = parse(withConsts([setOf(0x3c, code(tuple(int)))])); + const hashable = parse(withConsts([setOf(0x3c, code(tuple([int])))])); expect(root(hashable).consts[0]).toEqual({ kind: "set", length: 1 }); expect(hashable.coverage.status).toBe("complete"); }); @@ -1473,8 +1472,6 @@ it("keeps one descendant list when many wrappers share a container", () => { it("keeps code objects whose consts differ only by tuple shape", () => { const int = (value: number): Buffer => Buffer.concat([Buffer.from([0x69]), i32(value)]); - const tuple = (items: Buffer[]): Buffer => - Buffer.concat([Buffer.from([0x29, items.length]), ...items]); const code = (consts: Buffer): Buffer => marshalCode("py311", { name: "f",