diff --git a/src/model/PickleInspection.ts b/src/model/PickleInspection.ts index 6018e5a1746..e52dc941b9d 100644 --- a/src/model/PickleInspection.ts +++ b/src/model/PickleInspection.ts @@ -12,6 +12,7 @@ import { } from "./ModelFormat.js"; import { calleeName, + exactInt, pickleOpcodeName, type PickleCall, type PickleNode, @@ -176,9 +177,6 @@ export const pickleFacts = ( }; }; -const exactInt = (node: PickleNode | undefined): bigint | undefined => - node?.kind === "int" && node.value !== null ? node.value : undefined; - const text = (node: PickleNode | undefined): string | undefined => node?.kind === "str" ? node.value : undefined; diff --git a/src/model/PickleWalker.ts b/src/model/PickleWalker.ts index 356170d3015..30fc85a5fb6 100644 --- a/src/model/PickleWalker.ts +++ b/src/model/PickleWalker.ts @@ -37,6 +37,9 @@ export type PickleNode = | { readonly kind: "persistent"; readonly pid: PickleNode } | PickleCall; +export const exactInt = (node: PickleNode | undefined): bigint | undefined => + node?.kind === "int" && node.value !== null ? node.value : undefined; + /** A REDUCE/NEWOBJ/OBJ/INST result; APPEND, SETITEM and BUILD record what loading would do to it. */ export interface PickleCall { readonly kind: "call"; diff --git a/src/model/PytorchArtifactReader.ts b/src/model/PytorchArtifactReader.ts index 82c12b60a89..4b9b784e2ac 100644 --- a/src/model/PytorchArtifactReader.ts +++ b/src/model/PytorchArtifactReader.ts @@ -49,6 +49,7 @@ import { } from "./PickleInspection.js"; import { PickleWalker, + exactInt, PickleWork, type PickleBudgets, type PickleNode, @@ -191,9 +192,6 @@ const hashRemainder = async ( } }; -const intValue = (node: PickleNode | undefined): bigint | undefined => - node?.kind === "int" && node.value !== null ? node.value : undefined; - /** Storage references in first-appearance order; later conflicting declarations keep the first. */ const storageReferences = ( walker: PickleWalker, @@ -279,7 +277,7 @@ export const readPickleFile = async ( return walker; }; const first = await walk(0, "data"); - const legacy = intValue(first.result) === LEGACY_MAGIC; + const legacy = exactInt(first.result) === LEGACY_MAGIC; const limitations: string[] = []; let headerEnd = first.position; let version: number | null = null; @@ -290,7 +288,7 @@ export const readPickleFile = async ( if (legacy) { streams[0] = { ...streams[0]!, role: "magic" }; const protocol = await walk(headerEnd, "protocol"); - const declared = intValue(protocol.result); + const declared = exactInt(protocol.result); if (declared !== LEGACY_PROTOCOL) throw malformedInput( MODEL_FORMAT,