Skip to content

Commit f9aa1bf

Browse files
committed
Preserve CUDA 12 registered handle getter errors
Carry forward #2551 and its regression test. A KeyError inside a registered getter must propagate instead of being reported as an unknown handle type. Signed-off-by: Ralf W. Grosse-Kunstleve <rgrossekunst@nvidia.com>
1 parent 737ab4a commit f9aa1bf

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎cuda_bindings_12/cuda/bindings/utils/__init__.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def get_cuda_native_handle(obj: Any) -> int:
2626
"""
2727
obj_type = type(obj)
2828
try:
29-
return _handle_getters[obj_type](obj)
29+
getter = _handle_getters[obj_type]
3030
except KeyError:
3131
raise TypeError("Unknown type: " + str(obj_type)) from None
32+
# Deliberately outside the try: a KeyError raised by the getter itself is a
33+
# bug in that getter, not an unregistered type.
34+
return getter(obj)

‎cuda_bindings_12/tests/test_utils.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ def test_get_handle_error(target):
9292
handle = get_cuda_native_handle(target)
9393

9494

95+
@pytest.mark.agent_authored(model="claude-opus-5")
96+
def test_get_handle_does_not_report_a_registered_type_as_unknown(monkeypatch):
97+
"""A KeyError from inside a handle getter is a bug in that getter.
98+
99+
Reporting it as "Unknown type" is wrong twice over: the type *is*
100+
registered, and `from None` hides the traceback that would say otherwise.
101+
"""
102+
from cuda.bindings.utils import _handle_getters
103+
104+
class Registered:
105+
pass
106+
107+
def getter(_obj):
108+
raise KeyError("lookup inside the getter failed")
109+
110+
monkeypatch.setitem(_handle_getters, Registered, getter)
111+
112+
with pytest.raises(KeyError, match="lookup inside the getter failed"):
113+
get_cuda_native_handle(Registered())
114+
115+
95116
@pytest.mark.parametrize(
96117
"module",
97118
[

0 commit comments

Comments
 (0)