Skip to content

Commit c707854

Browse files
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 <atiaomar1978-hub@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 88e3df2 commit c707854

5 files changed

Lines changed: 84 additions & 7 deletions

File tree

cuda_bindings/build_hooks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def _build_cuda_bindings(debug=False):
131131
that metadata queries do not require a CUDA toolkit installation.
132132
"""
133133
from Cython.Build import cythonize
134+
from Cython.Compiler import Options as _CythonOptions
134135

135136
global _extensions
136137

@@ -224,6 +225,7 @@ def get_static_libraries(f):
224225
)
225226

226227
# Cythonize
228+
_CythonOptions.warning_errors = True
227229
cython_directives = {"language_level": 3, "embedsignature": True, "binding": True, "freethreading_compatible": True}
228230
if compile_for_coverage:
229231
cython_directives["linetrace"] = True

cuda_bindings/cuda/bindings/_lib/windll.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cdef extern from "windows.h" nogil:
1414
ctypedef const char *LPCSTR
1515
ctypedef int BOOL
1616

17-
cdef DWORD LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800
17+
const DWORD LOAD_LIBRARY_SEARCH_SYSTEM32
1818

1919
HMODULE _LoadLibraryExW "LoadLibraryExW"(
2020
LPCWSTR lpLibFileName,

cuda_bindings/cuda/bindings/cudla.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly.
55

6-
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=e64a78a1b3e010d167373d7c9635ff4637dfd1a6a38ffafb671dcde4e12aaaad
6+
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=b3303a34762cec1db0d9ab7662fe4a4a0f5b80350810cc0b646a04c9c6bb19c3
77
from libc.stdint cimport intptr_t
88

99
from .cycudla cimport *
@@ -44,10 +44,10 @@ cpdef intptr_t mem_register(intptr_t dev_handle, intptr_t ptr, size_t size, uint
4444
cpdef intptr_t module_load_from_memory(intptr_t dev_handle, p_module, size_t module_size, uint32_t flags) except *
4545
cpdef module_unload(intptr_t h_module, uint32_t flags)
4646
cpdef submit_task(intptr_t dev_handle, intptr_t ptr_to_tasks, uint32_t num_tasks, intptr_t stream, uint32_t flags)
47-
cpdef object device_get_attribute(intptr_t dev_handle, int attrib) except *
47+
cpdef object device_get_attribute(intptr_t dev_handle, int attrib)
4848
cpdef mem_unregister(intptr_t dev_handle, intptr_t dev_ptr)
4949
cpdef int get_last_error(intptr_t dev_handle) except? 0
5050
cpdef destroy_device(intptr_t dev_handle)
5151
cpdef set_task_timeout_in_ms(intptr_t dev_handle, uint32_t timeout)
5252

53-
cpdef module_get_attributes(intptr_t h_module, int attr_type) except *
53+
cpdef module_get_attributes(intptr_t h_module, int attr_type)

cuda_bindings/cuda/bindings/cudla.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
# This code was automatically generated across versions from 1.5.0 to 13.3.0. Do not modify it directly.
5-
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=b793aebd0586162e23d26c82e2bd54c21675584e3c23d6e443f3a83c61a8674c
5+
# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=ee04e18498a1e4e597afcdead5590bfe22af25afb2d43cd85b19089bee10a721
66

77

88
# <<<< PREAMBLE CONTENT >>>>
@@ -1776,7 +1776,7 @@ cpdef submit_task(intptr_t dev_handle, intptr_t ptr_to_tasks, uint32_t num_tasks
17761776
check_status(__status__)
17771777

17781778

1779-
cpdef object device_get_attribute(intptr_t dev_handle, int attrib) except *:
1779+
cpdef object device_get_attribute(intptr_t dev_handle, int attrib):
17801780
cdef DevAttribute p_attribute_py = DevAttribute()
17811781
cdef cudlaDevAttribute *p_attribute = <cudlaDevAttribute *><intptr_t>(p_attribute_py._get_ptr())
17821782
with nogil:
@@ -1810,7 +1810,7 @@ cpdef set_task_timeout_in_ms(intptr_t dev_handle, uint32_t timeout):
18101810
check_status(__status__)
18111811

18121812

1813-
cpdef module_get_attributes(intptr_t h_module, int attr_type) except *:
1813+
cpdef module_get_attributes(intptr_t h_module, int attr_type):
18141814
"""Query module attributes, interpreting the cudlaModuleAttribute union
18151815
based on the requested attribute type.
18161816
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)