Skip to content

Commit 979fbd9

Browse files
committed
fixup! feat(core.utils): bytes-like code is NVVM-only
1 parent 0de6189 commit 979fbd9

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

cuda_core/cuda/core/utils/_program_cache.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,14 @@ def make_program_cache_key(
593593
if isinstance(code, str):
594594
code_bytes = code.encode("utf-8")
595595
elif isinstance(code, (bytes, bytearray)):
596+
# Program() only accepts bytes-like ``code`` for the NVVM backend
597+
# (_program.pyx Program_init); c++/ptx require ``str``. Mirror that
598+
# so the cache helper doesn't mint keys for inputs the real compile
599+
# would reject.
600+
if backend != "nvvm":
601+
raise TypeError(
602+
f"code must be str for code_type={code_type!r}; bytes/bytearray are only accepted for code_type='nvvm'."
603+
)
596604
code_bytes = bytes(code)
597605
else:
598606
raise TypeError(f"code must be str or bytes, got {type(code).__name__}")

cuda_core/tests/test_program_cache.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,20 @@ def test_make_program_cache_key_name_expressions_str_bytes_distinct():
284284
assert _make_key(name_expressions=("foo",)) != _make_key(name_expressions=(b"foo",))
285285

286286

287+
@pytest.mark.parametrize(
288+
"code_type, target_type",
289+
[
290+
pytest.param("c++", "cubin", id="nvrtc"),
291+
pytest.param("ptx", "cubin", id="ptx"),
292+
],
293+
)
294+
def test_make_program_cache_key_rejects_bytes_code_outside_nvvm(code_type, target_type):
295+
"""``Program()`` only accepts bytes-like code for NVVM; c++ and PTX
296+
require str. The cache helper must mirror that rejection."""
297+
with pytest.raises(TypeError, match="code must be str for code_type"):
298+
_make_key(code=b"abc", code_type=code_type, target_type=target_type)
299+
300+
287301
@pytest.mark.parametrize(
288302
"code_type, code, target_type",
289303
[

0 commit comments

Comments
 (0)