"""A malformed GGUF header is refused with a ValueError that names the file, never a KeyError, OverflowError or struct.error (#1919). Tensor-size support and setup compatibility policy are unchanged. Over minimal GGUF v3 files written here (no download, no model, no GPU). python -m unittest tools.test_gguf_reader_malformed """ from __future__ import annotations import contextlib import io import struct import sys import tempfile import unittest from unittest import mock from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tools")) sys.path.insert(0, str(ROOT)) import setup as S # noqa: E402 from gguf_reader import GGUFFile # noqa: E402 class Stop(Exception): """setup.fail(), caught instead of exiting.""" def gstr(s: bytes) -> bytes: return struct.pack(" bytes: return struct.pack(" bytes: return gstr(name) + struct.pack(" bytes: return b + bytes(-len(b) % 32 + data) class MalformedHeader(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.path = Path(self.tmp.name) / "m-00001-of-00002.gguf" def tearDown(self): self.tmp.cleanup() def refused(self, raw: bytes) -> str: self.path.write_bytes(raw) with self.assertRaises(ValueError) as cm: GGUFFile(self.path) self.assertIn(self.path.name, str(cm.exception)) return str(cm.exception) def test_unknown_metadata_value_type(self): self.refused(pad(header(0, gstr(b"k") + struct.pack("