Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cuda_bindings/cuda/bindings/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def get_cuda_native_handle(obj: Any) -> int:
"""
obj_type = type(obj)
try:
return _handle_getters[obj_type](obj)
getter = _handle_getters[obj_type]
except KeyError:
raise TypeError("Unknown type: " + str(obj_type)) from None
# Deliberately outside the try: a KeyError raised by the getter itself is a
# bug in that getter, not an unregistered type.
return getter(obj)
21 changes: 21 additions & 0 deletions cuda_bindings/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ def test_get_handle_error(target):
handle = get_cuda_native_handle(target)


@pytest.mark.agent_authored(model="claude-opus-5")
def test_get_handle_does_not_report_a_registered_type_as_unknown(monkeypatch):
"""A KeyError from inside a handle getter is a bug in that getter.

Reporting it as "Unknown type" is wrong twice over: the type *is*
registered, and `from None` hides the traceback that would say otherwise.
"""
from cuda.bindings.utils import _handle_getters

class Registered:
pass

def getter(_obj):
raise KeyError("lookup inside the getter failed")

monkeypatch.setitem(_handle_getters, Registered, getter)

with pytest.raises(KeyError, match="lookup inside the getter failed"):
get_cuda_native_handle(Registered())


@pytest.mark.parametrize(
"module",
# Top-level modules for external Python use
Expand Down
Loading