refactor(python): consolidate bytecode identity across readers

Share CPython magic framing, version and header facts while keeping standalone marshal coverage and PyInstaller reconstruction eligibility distinct. Preserve legacy headers, opaque unknowns and source bytes.

Validated 272 focused tests across source, CLI and MCP workflows, a 38-test reader recheck, and check:fast.
This commit is contained in:
morluto committed 2026-10-27 02:09:52 +08:00
1 parent 669e617eec
commit d06ca092fb
8 files changed
+322 -97

No files matched your search

+27 -41
View File
@@ -16,66 +16,52 @@ const nonNegativeInt32 = z.number().int().min(0).max(0x7fffffff);
export const PYTHON_PYC_CODE_LAYOUTS = ["py37", "py38", "py311"] as const;
export type PythonPycCodeLayout = (typeof PYTHON_PYC_CODE_LAYOUTS)[number];
type MagicEntry = {
readonly python_version: string;
interface PythonPycMarshalProfile {
readonly code_layout: PythonPycCodeLayout;
readonly header_bytes: 12 | 16;
/** CPython constructor policy for negative co_stacksize/co_flags fields. */
readonly negative_code_fields: "accepted" | "revision-dependent" | "rejected";
};
}
const magics = (
const profiles = (
values: readonly number[],
python_version: string,
code_layout: PythonPycCodeLayout,
header_bytes: 12 | 16 = PYTHON_PYC_HEADER_BYTES,
negative_code_fields: MagicEntry["negative_code_fields"] = "rejected",
): ReadonlyArray<readonly [number, MagicEntry]> =>
values.map((magic) => [
magic,
{ python_version, code_layout, header_bytes, negative_code_fields },
]);
negative_code_fields: PythonPycMarshalProfile["negative_code_fields"] = "rejected",
): ReadonlyArray<readonly [number, PythonPycMarshalProfile]> =>
values.map((magic) => [magic, { code_layout, negative_code_fields }]);
const consecutive = (
const consecutiveProfiles = (
start: number,
end: number,
python_version: string,
code_layout: PythonPycCodeLayout,
): ReadonlyArray<readonly [number, MagicEntry]> =>
magics(
): ReadonlyArray<readonly [number, PythonPycMarshalProfile]> =>
profiles(
Array.from({ length: end - start + 1 }, (_, offset) => start + offset),
python_version,
code_layout,
);
/**
* Pinned CPython pyc magics for 3.7–3.13. Identity is the 16-bit little-endian
* field; `\r\n` is required separately. Layout follows marshal TYPE_CODE, not
* bytecode opcode tables.
*/
export const PYTHON_PYC_MAGIC_TABLE: ReadonlyMap<number, MagicEntry> = new Map([
...magics([3390, 3391], "3.7", "py37", 12, "accepted"),
...magics([3392, 3393, 3394], "3.7", "py37", 16, "accepted"),
...magics([3400, 3401], "3.8", "py37", 16, "accepted"),
/** Standalone marshal coverage; source identity and header shape have a separate owner. */
export const PYTHON_PYC_MARSHAL_PROFILES: ReadonlyMap<
number,
PythonPycMarshalProfile
> = new Map([
...profiles([3390, 3391, 3392, 3393, 3394], "py37", "accepted"),
...profiles([3400, 3401], "py37", "accepted"),
// 3410 spans 3.8.0a4 and b1; a9f05d69 added constructor sign checks between.
...magics([3410], "3.8", "py38", 16, "revision-dependent"),
...magics([3411, 3412, 3413], "3.8", "py38", 16, "rejected"),
...magics([3420, 3421, 3422, 3423, 3424, 3425], "3.9", "py38"),
...magics(
[3430, 3431, 3432, 3433, 3434, 3435, 3436, 3437, 3438, 3439],
"3.10",
"py38",
),
...profiles([3410], "py38", "revision-dependent"),
...profiles([3411, 3412, 3413], "py38"),
...consecutiveProfiles(3420, 3425, "py38"),
...consecutiveProfiles(3430, 3439, "py38"),
// 3.11 alphas before 3494 marshal other code layouts: no co_qualname
// before 3460, and PEP 657 end-line and column tables until 3494.
...consecutive(3494, 3495, "3.11", "py311"),
...consecutive(3500, 3531, "3.12", "py311"),
...consecutive(3550, 3571, "3.13", "py311"),
...consecutiveProfiles(3494, 3495, "py311"),
...consecutiveProfiles(3500, 3531, "py311"),
...consecutiveProfiles(3550, 3571, "py311"),
]);
/** Map a pyc magic integer to its pinned CPython family, or null when unknown. */
export const pythonPycMagicEntry = (magic: number): MagicEntry | null =>
PYTHON_PYC_MAGIC_TABLE.get(magic) ?? null;
export const pythonPycMarshalProfile = (
magic: number,
): PythonPycMarshalProfile | null =>
PYTHON_PYC_MARSHAL_PROFILES.get(magic) ?? null;
export const PYTHON_PYC_LIMIT_MAXIMA = {
max_file_bytes: 64 * 1024 * 1024,