Skip to content
Draft
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
9 changes: 8 additions & 1 deletion cuda_bindings_12/cuda/bindings/_internal/utils.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj,
nested_ptr.reset(nested_vec, True)
for i, obj_i in enumerate(obj):
if ResT is char:
obj_i_bytes = (<str?>(obj_i)).encode()
obj_i_type = type(obj_i)
if obj_i_type is str:
obj_i_bytes = obj_i.encode("utf-8")
elif obj_i_type is bytes:
obj_i_bytes = obj_i
else:
raise TypeError(
f"Expected str or bytes, got {obj_i_type.__name__}")
str_len = <size_t>(len(obj_i_bytes)) + 1 # including null termination
deref(nested_res_vec)[i].resize(str_len)
obj_i_ptr = <char*>(obj_i_bytes)
Expand Down
1 change: 1 addition & 0 deletions cuda_bindings_12/cuda/bindings/_lib/utils.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ cdef class _HelperCUcoredumpSettings:
cdef cydriver.CUcoredumpSettings_enum _attrib
cdef bint _is_getter
cdef size_t _size
cdef object _references # keeps caller bytes alive so _charstar stays valid

# Return values
cdef cpp_bool _bool
Expand Down
6 changes: 5 additions & 1 deletion cuda_bindings_12/cuda/bindings/_lib/utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ cdef class _HelperCUcoredumpSettings:
self._cptr = <void*><void_ptr>self._charstar
self._size = 1024
else:
# Keep a reference so the borrowed _charstar buffer stays alive.
self._references = init_value
self._charstar = init_value
self._cptr = <void*><void_ptr>self._charstar
self._size = len(init_value)
Expand All @@ -756,7 +758,9 @@ cdef class _HelperCUcoredumpSettings:
raise TypeError('Unsupported attribute: {}'.format(attr.name))

def __dealloc__(self):
pass
# Only the getter owns heap storage; the setter borrows caller bytes.
if self._is_getter:
free(self._charstar)

@property
def cptr(self):
Expand Down
5 changes: 4 additions & 1 deletion cuda_bindings_12/cuda/bindings/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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)
7 changes: 7 additions & 0 deletions cuda_bindings_12/tests/test_nvjitlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def test_create_and_destroy(option):
nvjitlink.destroy(handle)


@pytest.mark.human_authored
def test_create_and_destroy_bytes_options():
handle = nvjitlink.create(1, [b"-arch=sm_80"])
assert handle != 0
nvjitlink.destroy(handle)


@pytest.mark.parametrize("option", ARCHITECTURES)
def test_complete_empty(option):
handle = nvjitlink.create(1, [f"-arch={option}"])
Expand Down
2 changes: 1 addition & 1 deletion cuda_bindings_12/tests/test_nvvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def test_get_buffer_empty(get_size, get_buffer):
assert buffer == b"\x00"


@pytest.mark.parametrize("options", [[], ["-opt=0"], ["-opt=3", "-g"]])
@pytest.mark.parametrize("options", [[], ["-opt=0"], ["-opt=3", "-g"], [b"-opt=0"]])
def test_compile_program_with_minimal_nvvm_ir(minimal_nvvmir, options):
with nvvm_program() as prog:
nvvm.add_module_to_program(prog, minimal_nvvmir, len(minimal_nvvmir), "FileNameHere.ll")
Expand Down
21 changes: 21 additions & 0 deletions cuda_bindings_12/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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",
[
Expand Down
Loading