Skip to content

Commit af4c70b

Browse files
committed
Do not report a registered type as "Unknown type" in get_cuda_native_handle
get_cuda_native_handle() wraps both the registry lookup and the getter call in one try: try: return _handle_getters[obj_type](obj) except KeyError: raise TypeError("Unknown type: " + str(obj_type)) from None The except clause is meant for "this type has no registered getter", but it also fires for a KeyError raised *inside* the getter. When that happens the diagnosis is wrong twice over: the reported type is registered, and `from None` suppresses the context so the traceback that would show the real failure is gone. >>> _add_cuda_native_handle_getter(Registered, getter_that_raises_keyerror) >>> get_cuda_native_handle(Registered()) TypeError: Unknown type: <class 'Registered'> Move the getter call out of the try. The unregistered-type path is unchanged, which the existing test_get_handle_error still covers.
1 parent 3bd069a commit af4c70b

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

cuda_bindings/cuda/bindings/utils/__init__.py

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

cuda_bindings/tests/test_utils.py

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

117117

118+
@pytest.mark.agent_authored(model="claude-opus-5")
119+
def test_get_handle_does_not_report_a_registered_type_as_unknown(monkeypatch):
120+
"""A KeyError from inside a handle getter is a bug in that getter.
121+
122+
Reporting it as "Unknown type" is wrong twice over: the type *is*
123+
registered, and `from None` hides the traceback that would say otherwise.
124+
"""
125+
from cuda.bindings.utils import _handle_getters
126+
127+
class Registered:
128+
pass
129+
130+
def getter(_obj):
131+
raise KeyError("lookup inside the getter failed")
132+
133+
monkeypatch.setitem(_handle_getters, Registered, getter)
134+
135+
with pytest.raises(KeyError, match="lookup inside the getter failed"):
136+
get_cuda_native_handle(Registered())
137+
138+
118139
@pytest.mark.parametrize(
119140
"module",
120141
# Top-level modules for external Python use

0 commit comments

Comments
 (0)