|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Regression guards for cuda.bindings Cython warning cleanliness (#2450).""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import ast |
| 9 | +import re |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +_BINDINGS_ROOT = Path(__file__).resolve().parents[1] |
| 15 | +_CUDA_BINDINGS = _BINDINGS_ROOT / "cuda" / "bindings" |
| 16 | + |
| 17 | +# cpdef returning a Python object must not carry an exception clause; Cython |
| 18 | +# warns (and with warning_errors, fails) that the clause is ignored. |
| 19 | +_CPDEF_OBJECT_EXCEPT_RE = re.compile( |
| 20 | + r"^\s*cpdef\s+object\s+\w+\s*\([^)]*\)\s+except\b", |
| 21 | + re.MULTILINE, |
| 22 | +) |
| 23 | +_MODULE_GET_ATTRIBUTES_EXCEPT_RE = re.compile( |
| 24 | + r"^\s*cpdef\s+module_get_attributes\s*\([^)]*\)\s+except\b", |
| 25 | + re.MULTILINE, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.agent_authored(model="grok-4.5") |
| 30 | +def test_build_hooks_enable_cython_warning_errors(): |
| 31 | + """Source builds must treat Cython warnings as errors.""" |
| 32 | + source = (_BINDINGS_ROOT / "build_hooks.py").read_text(encoding="utf-8") |
| 33 | + tree = ast.parse(source) |
| 34 | + assigned = { |
| 35 | + node.targets[0].attr |
| 36 | + for node in ast.walk(tree) |
| 37 | + if isinstance(node, ast.Assign) |
| 38 | + and len(node.targets) == 1 |
| 39 | + and isinstance(node.targets[0], ast.Attribute) |
| 40 | + and isinstance(node.value, ast.Constant) |
| 41 | + and node.value.value is True |
| 42 | + } |
| 43 | + assert "warning_errors" in assigned, ( |
| 44 | + "cuda_bindings/build_hooks.py must set Cython Options.warning_errors = True" |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.agent_authored(model="grok-4.5") |
| 49 | +def test_windll_pxd_declares_load_library_flag_as_const(): |
| 50 | + """Assignment in a ``.pxd`` is not executed; declare the Win32 flag as const.""" |
| 51 | + text = (_CUDA_BINDINGS / "_lib" / "windll.pxd").read_text(encoding="utf-8") |
| 52 | + assert "LOAD_LIBRARY_SEARCH_SYSTEM32" in text |
| 53 | + assert re.search( |
| 54 | + r"^\s*const\s+DWORD\s+LOAD_LIBRARY_SEARCH_SYSTEM32\s*$", |
| 55 | + text, |
| 56 | + re.MULTILINE, |
| 57 | + ), "LOAD_LIBRARY_SEARCH_SYSTEM32 must be declared as const DWORD (no assignment)" |
| 58 | + assert re.search( |
| 59 | + r"LOAD_LIBRARY_SEARCH_SYSTEM32\s*=", |
| 60 | + text, |
| 61 | + ) is None |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.agent_authored(model="grok-4.5") |
| 65 | +@pytest.mark.parametrize("filename", ["cudla.pxd", "cudla.pyx"]) |
| 66 | +def test_cudla_cpdef_python_returns_have_no_except_clause(filename): |
| 67 | + """Regression for cybind emitting ``except *`` on Python-returning cpdefs.""" |
| 68 | + text = (_CUDA_BINDINGS / filename).read_text(encoding="utf-8") |
| 69 | + assert _CPDEF_OBJECT_EXCEPT_RE.search(text) is None, ( |
| 70 | + f"{filename} has cpdef object ... except ..., which Cython warns about" |
| 71 | + ) |
| 72 | + # Declared without an explicit object return type, but returns Python values. |
| 73 | + assert _MODULE_GET_ATTRIBUTES_EXCEPT_RE.search(text) is None, ( |
| 74 | + f"{filename}: cpdef module_get_attributes(...) except ... is invalid for Python returns" |
| 75 | + ) |
0 commit comments