swizzle_search, tex_mode_probe: say numpy is missing in one line, as tile_probe does

This commit is contained in:
Reece Barker
2026-11-24 15:47:59 +01:00
parent a28927849a
commit e61aadda9f
4 changed files with 79 additions and 4 deletions

View File

@ -692,5 +692,35 @@ def test_preview_with_no_fitting_layout_reports_the_problem(tmp_path=None):
td.cleanup()
# `import numpy` raises ImportError in the child, as on a Python without numpy (CI's standard-library job)
WITHOUT_NUMPY = ("import runpy, sys; sys.modules['numpy'] = None; sys.argv = sys.argv[1:]; "
"runpy.run_path(sys.argv[0], run_name='__main__')")
class SwizzleSearchWithoutNumpyTest(unittest.TestCase):
"""The argument checks need no numpy, and the missing numpy itself is one line, not a traceback."""
def run_tool(self, *args):
return subprocess.run([sys.executable, "-I", "-c", WITHOUT_NUMPY, str(TOOL), *args],
capture_output=True, text=True, timeout=120)
def test_missing_file_is_a_usage_error_without_numpy(self):
with tempfile.TemporaryDirectory() as tmp:
p = self.run_tool(os.path.join(tmp, "missing.bin"))
self.assertEqual(p.returncode, 2, p.stderr)
self.assertIn("missing.bin", p.stderr)
self.assertNotIn("Traceback", p.stderr)
def test_missing_numpy_is_one_error_line(self):
with tempfile.TemporaryDirectory() as tmp:
blob = os.path.join(tmp, "blob.bin")
with open(blob, "wb") as f:
f.write(bytes(range(256)) * 64)
p = self.run_tool(blob)
self.assertEqual(p.returncode, 2, p.stderr)
self.assertEqual(p.stderr.strip().splitlines(),
["ERROR: swizzle_search.py needs numpy (python -m pip install numpy)"])
if __name__ == "__main__":
unittest.main()

View File

@ -10,6 +10,7 @@ import io
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
@ -973,5 +974,36 @@ if pytest is not None:
)
# `import numpy` raises ImportError in the child, as on a Python without numpy (CI's standard-library job)
WITHOUT_NUMPY = ("import runpy, sys; sys.modules['numpy'] = None; sys.argv = sys.argv[1:]; "
"runpy.run_path(sys.argv[0], run_name='__main__')")
class TexModeProbeWithoutNumpyTest(unittest.TestCase):
"""The argument checks need no numpy, and the missing numpy itself is one line, not a traceback."""
def run_tool(self, *args):
return subprocess.run([sys.executable, "-I", "-c", WITHOUT_NUMPY,
os.path.join(TOOLS_DIR, "tex_mode_probe.py"), *args],
capture_output=True, text=True, timeout=120)
def test_unreadable_index_is_a_usage_error_without_numpy(self):
with tempfile.TemporaryDirectory() as tmp:
p = self.run_tool(os.path.join(tmp, "missing.json"), tmp)
self.assertEqual(p.returncode, 2, p.stderr)
self.assertIn("missing.json", p.stderr)
self.assertNotIn("Traceback", p.stderr)
def test_missing_numpy_is_one_error_line(self):
with tempfile.TemporaryDirectory() as tmp:
index = os.path.join(tmp, "textures_index.json")
with open(index, "w", encoding="utf-8", newline="\n") as f:
f.write("[]")
p = self.run_tool(index, tmp)
self.assertEqual(p.returncode, 2, p.stderr)
self.assertEqual(p.stderr.strip().splitlines(),
["ERROR: tex_mode_probe.py needs numpy (python -m pip install numpy)"])
if __name__ == "__main__":
unittest.main()

View File

@ -12,7 +12,10 @@ import itertools
import os
import sys
import numpy as np
try:
import numpy as np
except ImportError: # main() checks the arguments first, then says numpy is missing in one line
np = None
sys.path.insert(0, os.path.dirname(__file__))
from tile_probe import block_feature, score, same_report_path # noqa: E402
@ -151,6 +154,9 @@ def main(argv=None):
except OSError as e: # a missing or unreadable input: one ERROR line, as the other tools do
print(f"ERROR: cannot read {a.blob}: {e.strerror or e}", file=sys.stderr)
return 2
if np is None:
print("ERROR: swizzle_search.py needs numpy (python -m pip install numpy)", file=sys.stderr)
return 2
res = search(data, a.bpe, a.tile, widths, a.tiles)
if not res:
print("ERROR: no layout fits the input, tile size and requested widths", file=sys.stderr)

View File

@ -13,11 +13,15 @@ import os
import sys
from collections import Counter
import numpy as np
try:
import numpy as np
except ImportError: # main() checks the arguments first, then says numpy is missing in one line
np = None
sys.path.insert(0, os.path.dirname(__file__))
from amd_swizzle import _parity, equation, load # noqa: E402
from bc_feature import block_average, smoothness # noqa: E402
if np is not None: # both helpers import numpy themselves
from amd_swizzle import _parity, equation, load # noqa: E402
from bc_feature import block_average, smoothness # noqa: E402
BPE = {"BC1": 8, "BC2": 16, "BC3": 16}
@ -90,6 +94,9 @@ def main():
ap.error(f"cannot read texture index {a.index}: {error}")
if not isinstance(index, list):
ap.error(f"{a.index}: texture index must contain a list")
if np is None:
print("ERROR: tex_mode_probe.py needs numpy (python -m pip install numpy)", file=sys.stderr)
return 2
nib, tab = load()
texs = [t for t in index if t["fmt_name"].startswith(a.fmt)
and bc_bpe(t["fmt_name"]) is not None and t["w"] >= 256 and t["h"] >= 256]