diff --git a/tests/test_swizzle_search.py b/tests/test_swizzle_search.py index bd5e40183..ad41b6e14 100644 --- a/tests/test_swizzle_search.py +++ b/tests/test_swizzle_search.py @@ -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() diff --git a/tests/test_tex_mode_probe.py b/tests/test_tex_mode_probe.py index a550a55af..c74bda616 100644 --- a/tests/test_tex_mode_probe.py +++ b/tests/test_tex_mode_probe.py @@ -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() diff --git a/tools/swizzle_search.py b/tools/swizzle_search.py index 72c068944..3d8fa5afd 100644 --- a/tools/swizzle_search.py +++ b/tools/swizzle_search.py @@ -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) diff --git a/tools/tex_mode_probe.py b/tools/tex_mode_probe.py index 5c9e37542..a0ccf3fc8 100644 --- a/tools/tex_mode_probe.py +++ b/tools/tex_mode_probe.py @@ -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]