mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:48:35 +00:00
A .pyc does not help ast.parse. test_runtime_paths.py, test_isa_coverage.py, test_shader_stage_resume.py, test_cmake_translator_deps.py and test_resource_hygiene.py read the syntax tree of tools/rdna2spirv.py, seven whole-file parses of 7 s each on CPython 3.12, 3.13.0 to 3.13.15 and 3.14.0 to 3.14.7. They now call tests/semantic/piecewise_parse.parse, which gives the same tree (tests/test_piecewise_parse.py) in about 1 s. tools/isa_coverage.py parses inside scan_translator and accepted_names; its test patches ast.parse around those two calls on the real translator instead of changing the tool. Python 3.14.7, valid .pyc of the translator, two runs each: test_runtime_paths 16.0 -> 3.7 s, test_isa_coverage 15.0 -> 2.5 s, test_shader_stage_resume 14.2 -> 8.2 s, test_cmake_translator_deps 7.6 -> 1.4 s, test_resource_hygiene 7.6 -> 1.3 s. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""Regression tests ensuring tools close file handles cleanly under -W error::ResourceWarning."""
|
|
|
|
import ast
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
|
sys.path.append(str(ROOT / "tests" / "semantic"))
|
|
import piecewise_parse # noqa: E402 (ast.parse takes 7 s for tools/rdna2spirv.py on CPython 3.12)
|
|
|
|
|
|
def _bare_open_calls(path: pathlib.Path) -> list[tuple[int, str]]:
|
|
"""Return (lineno, source_line) for any open(...) call not managed by a with statement."""
|
|
src = path.read_text(encoding="utf-8")
|
|
tree = piecewise_parse.parse(src, str(path))
|
|
managed: set[int] = set()
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, (ast.With, ast.AsyncWith)):
|
|
for item in node.items:
|
|
if isinstance(item.context_expr, ast.Call):
|
|
managed.add(id(item.context_expr))
|
|
lines = src.splitlines()
|
|
bare: list[tuple[int, str]] = []
|
|
for node in ast.walk(tree):
|
|
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "open":
|
|
if id(node) not in managed:
|
|
bare.append((node.lineno, lines[node.lineno - 1].strip()))
|
|
return bare
|
|
|
|
|
|
class ResourceHygieneTest(unittest.TestCase):
|
|
def test_no_unmanaged_open_calls(self):
|
|
for rel in (
|
|
"tools/rdna2spirv.py",
|
|
"tools/amd_swizzle.py",
|
|
"tools/awc_opus.py",
|
|
"tools/wave64_graphics_scan.py",
|
|
"tools/tex_export.py",
|
|
"tools/tex_mode_probe.py",
|
|
"tools/shader_disasm.py",
|
|
"tools/hashdict.py",
|
|
"tools/d3d12_xs_surface.py",
|
|
):
|
|
with self.subTest(file=rel):
|
|
bare = _bare_open_calls(ROOT / rel)
|
|
self.assertEqual(bare, [], f"unmanaged open() calls in {rel}: {bare}")
|
|
|
|
def test_import_rdna2spirv_no_resource_warning(self):
|
|
r = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-W",
|
|
"error::ResourceWarning",
|
|
"-c",
|
|
f"import sys; sys.path.insert(0, {str(ROOT / 'tools')!r}); import rdna2spirv",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
self.assertEqual(r.returncode, 0, r.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|