# SPDX-License-Identifier: MIT """Import boundaries (open decision 2) and the PyPy attribute guard.""" from __future__ import annotations import ast from pathlib import Path from helpers import fake_board SRC = Path(__file__).resolve().parents[1] / "src" / "a5200_python" def _imports(path: Path) -> set[str]: names = set() for node in ast.walk(ast.parse(path.read_text("utf-8"))): if isinstance(node, ast.Import): names.update(alias.name for alias in node.names) elif isinstance(node, ast.ImportFrom): names.add(("." * node.level) + (node.module or "")) return names def test_antic_and_gtia_import_nothing_from_the_board(): for chip in ("antic.py", "gtia.py"): imports = _imports(SRC / chip) assert imports <= {"__future__"}, (chip, imports) def test_cartridge_package_does_not_import_the_board(): for path in (SRC / "cartridge").glob("*.py"): for name in _imports(path): assert not name.startswith(("..", "a5200_python")), (path.name, name) def test_hot_objects_stay_under_pypys_attribute_limit(): # PyPy turns an instance with 80+ attributes into a dict (about 3x slower). board = fake_board() board.run_frame() for obj in (board, board.antic, board.gtia, board.pokey, board.cpu): assert len(vars(obj)) < 80, (type(obj).__name__, len(vars(obj)))