From c7078542406f3b6739e440e31fcc4b19db31884a Mon Sep 17 00:00:00 2001 From: Omar Atie Date: Thu, 30 Jul 2026 21:15:14 -0700 Subject: [PATCH 1/3] fix(cuda.bindings): make cythonization warning-clean and enable -Werror Clear the Cython warnings that blocked matching cuda.core's warning_errors setting (#2450): drop ignored except clauses on Python-returning cudla cpdefs, declare LOAD_LIBRARY_SEARCH_SYSTEM32 as const in windll.pxd, and enable Cython Options.warning_errors in build_hooks. Add source-level regression tests so these patterns do not return. Signed-off-by: Omar Atie Co-authored-by: Cursor --- cuda_bindings/build_hooks.py | 2 + cuda_bindings/cuda/bindings/_lib/windll.pxd | 2 +- cuda_bindings/cuda/bindings/cudla.pxd | 6 +- cuda_bindings/cuda/bindings/cudla.pyx | 6 +- .../tests/test_cython_warning_cleanliness.py | 75 +++++++++++++++++++ 5 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 cuda_bindings/tests/test_cython_warning_cleanliness.py diff --git a/cuda_bindings/build_hooks.py b/cuda_bindings/build_hooks.py index a50133f9777..bb0d1b566b7 100644 --- a/cuda_bindings/build_hooks.py +++ b/cuda_bindings/build_hooks.py @@ -131,6 +131,7 @@ def _build_cuda_bindings(debug=False): that metadata queries do not require a CUDA toolkit installation. """ from Cython.Build import cythonize + from Cython.Compiler import Options as _CythonOptions global _extensions @@ -224,6 +225,7 @@ def get_static_libraries(f): ) # Cythonize + _CythonOptions.warning_errors = True cython_directives = {"language_level": 3, "embedsignature": True, "binding": True, "freethreading_compatible": True} if compile_for_coverage: cython_directives["linetrace"] = True diff --git a/cuda_bindings/cuda/bindings/_lib/windll.pxd b/cuda_bindings/cuda/bindings/_lib/windll.pxd index 294a1a9fd90..b5fd5c4db90 100644 --- a/cuda_bindings/cuda/bindings/_lib/windll.pxd +++ b/cuda_bindings/cuda/bindings/_lib/windll.pxd @@ -14,7 +14,7 @@ cdef extern from "windows.h" nogil: ctypedef const char *LPCSTR ctypedef int BOOL - cdef DWORD LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 + const DWORD LOAD_LIBRARY_SEARCH_SYSTEM32 HMODULE _LoadLibraryExW "LoadLibraryExW"( LPCWSTR lpLibFileName, diff --git a/cuda_bindings/cuda/bindings/cudla.pxd b/cuda_bindings/cuda/bindings/cudla.pxd index cb58d685a24..46b920f3c91 100644 --- a/cuda_bindings/cuda/bindings/cudla.pxd +++ b/cuda_bindings/cuda/bindings/cudla.pxd @@ -3,7 +3,7 @@ # This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly. -# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=e64a78a1b3e010d167373d7c9635ff4637dfd1a6a38ffafb671dcde4e12aaaad +# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=b3303a34762cec1db0d9ab7662fe4a4a0f5b80350810cc0b646a04c9c6bb19c3 from libc.stdint cimport intptr_t from .cycudla cimport * @@ -44,10 +44,10 @@ cpdef intptr_t mem_register(intptr_t dev_handle, intptr_t ptr, size_t size, uint cpdef intptr_t module_load_from_memory(intptr_t dev_handle, p_module, size_t module_size, uint32_t flags) except * cpdef module_unload(intptr_t h_module, uint32_t flags) cpdef submit_task(intptr_t dev_handle, intptr_t ptr_to_tasks, uint32_t num_tasks, intptr_t stream, uint32_t flags) -cpdef object device_get_attribute(intptr_t dev_handle, int attrib) except * +cpdef object device_get_attribute(intptr_t dev_handle, int attrib) cpdef mem_unregister(intptr_t dev_handle, intptr_t dev_ptr) cpdef int get_last_error(intptr_t dev_handle) except? 0 cpdef destroy_device(intptr_t dev_handle) cpdef set_task_timeout_in_ms(intptr_t dev_handle, uint32_t timeout) -cpdef module_get_attributes(intptr_t h_module, int attr_type) except * +cpdef module_get_attributes(intptr_t h_module, int attr_type) diff --git a/cuda_bindings/cuda/bindings/cudla.pyx b/cuda_bindings/cuda/bindings/cudla.pyx index 6ef2cba32e9..af240a100c5 100644 --- a/cuda_bindings/cuda/bindings/cudla.pyx +++ b/cuda_bindings/cuda/bindings/cudla.pyx @@ -2,7 +2,7 @@ # SPDX-License-Identifier: Apache-2.0 # This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly. -# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=b793aebd0586162e23d26c82e2bd54c21675584e3c23d6e443f3a83c61a8674c +# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=ee04e18498a1e4e597afcdead5590bfe22af25afb2d43cd85b19089bee10a721 # <<<< PREAMBLE CONTENT >>>> @@ -1776,7 +1776,7 @@ cpdef submit_task(intptr_t dev_handle, intptr_t ptr_to_tasks, uint32_t num_tasks check_status(__status__) -cpdef object device_get_attribute(intptr_t dev_handle, int attrib) except *: +cpdef object device_get_attribute(intptr_t dev_handle, int attrib): cdef DevAttribute p_attribute_py = DevAttribute() cdef cudlaDevAttribute *p_attribute = (p_attribute_py._get_ptr()) with nogil: @@ -1810,7 +1810,7 @@ cpdef set_task_timeout_in_ms(intptr_t dev_handle, uint32_t timeout): check_status(__status__) -cpdef module_get_attributes(intptr_t h_module, int attr_type) except *: +cpdef module_get_attributes(intptr_t h_module, int attr_type): """Query module attributes, interpreting the cudlaModuleAttribute union based on the requested attribute type. diff --git a/cuda_bindings/tests/test_cython_warning_cleanliness.py b/cuda_bindings/tests/test_cython_warning_cleanliness.py new file mode 100644 index 00000000000..352c159ca7e --- /dev/null +++ b/cuda_bindings/tests/test_cython_warning_cleanliness.py @@ -0,0 +1,75 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Regression guards for cuda.bindings Cython warning cleanliness (#2450).""" + +from __future__ import annotations + +import ast +import re +from pathlib import Path + +import pytest + +_BINDINGS_ROOT = Path(__file__).resolve().parents[1] +_CUDA_BINDINGS = _BINDINGS_ROOT / "cuda" / "bindings" + +# cpdef returning a Python object must not carry an exception clause; Cython +# warns (and with warning_errors, fails) that the clause is ignored. +_CPDEF_OBJECT_EXCEPT_RE = re.compile( + r"^\s*cpdef\s+object\s+\w+\s*\([^)]*\)\s+except\b", + re.MULTILINE, +) +_MODULE_GET_ATTRIBUTES_EXCEPT_RE = re.compile( + r"^\s*cpdef\s+module_get_attributes\s*\([^)]*\)\s+except\b", + re.MULTILINE, +) + + +@pytest.mark.agent_authored(model="grok-4.5") +def test_build_hooks_enable_cython_warning_errors(): + """Source builds must treat Cython warnings as errors.""" + source = (_BINDINGS_ROOT / "build_hooks.py").read_text(encoding="utf-8") + tree = ast.parse(source) + assigned = { + node.targets[0].attr + for node in ast.walk(tree) + if isinstance(node, ast.Assign) + and len(node.targets) == 1 + and isinstance(node.targets[0], ast.Attribute) + and isinstance(node.value, ast.Constant) + and node.value.value is True + } + assert "warning_errors" in assigned, ( + "cuda_bindings/build_hooks.py must set Cython Options.warning_errors = True" + ) + + +@pytest.mark.agent_authored(model="grok-4.5") +def test_windll_pxd_declares_load_library_flag_as_const(): + """Assignment in a ``.pxd`` is not executed; declare the Win32 flag as const.""" + text = (_CUDA_BINDINGS / "_lib" / "windll.pxd").read_text(encoding="utf-8") + assert "LOAD_LIBRARY_SEARCH_SYSTEM32" in text + assert re.search( + r"^\s*const\s+DWORD\s+LOAD_LIBRARY_SEARCH_SYSTEM32\s*$", + text, + re.MULTILINE, + ), "LOAD_LIBRARY_SEARCH_SYSTEM32 must be declared as const DWORD (no assignment)" + assert re.search( + r"LOAD_LIBRARY_SEARCH_SYSTEM32\s*=", + text, + ) is None + + +@pytest.mark.agent_authored(model="grok-4.5") +@pytest.mark.parametrize("filename", ["cudla.pxd", "cudla.pyx"]) +def test_cudla_cpdef_python_returns_have_no_except_clause(filename): + """Regression for cybind emitting ``except *`` on Python-returning cpdefs.""" + text = (_CUDA_BINDINGS / filename).read_text(encoding="utf-8") + assert _CPDEF_OBJECT_EXCEPT_RE.search(text) is None, ( + f"{filename} has cpdef object ... except ..., which Cython warns about" + ) + # Declared without an explicit object return type, but returns Python values. + assert _MODULE_GET_ATTRIBUTES_EXCEPT_RE.search(text) is None, ( + f"{filename}: cpdef module_get_attributes(...) except ... is invalid for Python returns" + ) From a9f3036758351592f059d07f2dc7ddf00fdcb2f6 Mon Sep 17 00:00:00 2001 From: Omar Atie Date: Tue, 4 Aug 2026 16:42:14 -0700 Subject: [PATCH 2/3] style: ruff-format cython warning cleanliness tests Signed-off-by: Omar Atie Co-authored-by: Cursor --- .../tests/test_cython_warning_cleanliness.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cuda_bindings/tests/test_cython_warning_cleanliness.py b/cuda_bindings/tests/test_cython_warning_cleanliness.py index 352c159ca7e..e312ef5c137 100644 --- a/cuda_bindings/tests/test_cython_warning_cleanliness.py +++ b/cuda_bindings/tests/test_cython_warning_cleanliness.py @@ -40,9 +40,7 @@ def test_build_hooks_enable_cython_warning_errors(): and isinstance(node.value, ast.Constant) and node.value.value is True } - assert "warning_errors" in assigned, ( - "cuda_bindings/build_hooks.py must set Cython Options.warning_errors = True" - ) + assert "warning_errors" in assigned, "cuda_bindings/build_hooks.py must set Cython Options.warning_errors = True" @pytest.mark.agent_authored(model="grok-4.5") @@ -55,10 +53,13 @@ def test_windll_pxd_declares_load_library_flag_as_const(): text, re.MULTILINE, ), "LOAD_LIBRARY_SEARCH_SYSTEM32 must be declared as const DWORD (no assignment)" - assert re.search( - r"LOAD_LIBRARY_SEARCH_SYSTEM32\s*=", - text, - ) is None + assert ( + re.search( + r"LOAD_LIBRARY_SEARCH_SYSTEM32\s*=", + text, + ) + is None + ) @pytest.mark.agent_authored(model="grok-4.5") From 1695c51d1744eaa0a809601db1c4a1a980cc377f Mon Sep 17 00:00:00 2001 From: Omar Atie Date: Tue, 11 Aug 2026 10:34:17 -0700 Subject: [PATCH 3/3] test(cuda.bindings): drop cython warning cleanliness tests Address review feedback: warning_errors in build_hooks already guards against Cython warning regressions, so the source-level tests add unnecessary maintenance cost. Signed-off-by: Omar Atie Co-authored-by: Cursor --- .../tests/test_cython_warning_cleanliness.py | 76 ------------------- 1 file changed, 76 deletions(-) delete mode 100644 cuda_bindings/tests/test_cython_warning_cleanliness.py diff --git a/cuda_bindings/tests/test_cython_warning_cleanliness.py b/cuda_bindings/tests/test_cython_warning_cleanliness.py deleted file mode 100644 index e312ef5c137..00000000000 --- a/cuda_bindings/tests/test_cython_warning_cleanliness.py +++ /dev/null @@ -1,76 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 - -"""Regression guards for cuda.bindings Cython warning cleanliness (#2450).""" - -from __future__ import annotations - -import ast -import re -from pathlib import Path - -import pytest - -_BINDINGS_ROOT = Path(__file__).resolve().parents[1] -_CUDA_BINDINGS = _BINDINGS_ROOT / "cuda" / "bindings" - -# cpdef returning a Python object must not carry an exception clause; Cython -# warns (and with warning_errors, fails) that the clause is ignored. -_CPDEF_OBJECT_EXCEPT_RE = re.compile( - r"^\s*cpdef\s+object\s+\w+\s*\([^)]*\)\s+except\b", - re.MULTILINE, -) -_MODULE_GET_ATTRIBUTES_EXCEPT_RE = re.compile( - r"^\s*cpdef\s+module_get_attributes\s*\([^)]*\)\s+except\b", - re.MULTILINE, -) - - -@pytest.mark.agent_authored(model="grok-4.5") -def test_build_hooks_enable_cython_warning_errors(): - """Source builds must treat Cython warnings as errors.""" - source = (_BINDINGS_ROOT / "build_hooks.py").read_text(encoding="utf-8") - tree = ast.parse(source) - assigned = { - node.targets[0].attr - for node in ast.walk(tree) - if isinstance(node, ast.Assign) - and len(node.targets) == 1 - and isinstance(node.targets[0], ast.Attribute) - and isinstance(node.value, ast.Constant) - and node.value.value is True - } - assert "warning_errors" in assigned, "cuda_bindings/build_hooks.py must set Cython Options.warning_errors = True" - - -@pytest.mark.agent_authored(model="grok-4.5") -def test_windll_pxd_declares_load_library_flag_as_const(): - """Assignment in a ``.pxd`` is not executed; declare the Win32 flag as const.""" - text = (_CUDA_BINDINGS / "_lib" / "windll.pxd").read_text(encoding="utf-8") - assert "LOAD_LIBRARY_SEARCH_SYSTEM32" in text - assert re.search( - r"^\s*const\s+DWORD\s+LOAD_LIBRARY_SEARCH_SYSTEM32\s*$", - text, - re.MULTILINE, - ), "LOAD_LIBRARY_SEARCH_SYSTEM32 must be declared as const DWORD (no assignment)" - assert ( - re.search( - r"LOAD_LIBRARY_SEARCH_SYSTEM32\s*=", - text, - ) - is None - ) - - -@pytest.mark.agent_authored(model="grok-4.5") -@pytest.mark.parametrize("filename", ["cudla.pxd", "cudla.pyx"]) -def test_cudla_cpdef_python_returns_have_no_except_clause(filename): - """Regression for cybind emitting ``except *`` on Python-returning cpdefs.""" - text = (_CUDA_BINDINGS / filename).read_text(encoding="utf-8") - assert _CPDEF_OBJECT_EXCEPT_RE.search(text) is None, ( - f"{filename} has cpdef object ... except ..., which Cython warns about" - ) - # Declared without an explicit object return type, but returns Python values. - assert _MODULE_GET_ATTRIBUTES_EXCEPT_RE.search(text) is None, ( - f"{filename}: cpdef module_get_attributes(...) except ... is invalid for Python returns" - )