Skip to content

cuda.bindings: add decode_c_str helper for actionable UnicodeDecodeError (#2122)#2128

Closed
aryanputta wants to merge 4 commits into
NVIDIA:mainfrom
aryanputta:decode-c-string-actionable
Closed

cuda.bindings: add decode_c_str helper for actionable UnicodeDecodeError (#2122)#2128
aryanputta wants to merge 4 commits into
NVIDIA:mainfrom
aryanputta:decode-c-string-actionable

Conversation

@aryanputta
Copy link
Copy Markdown
Contributor

@aryanputta aryanputta commented May 22, 2026

Summary

Adds cuda.bindings._internal.strdecode.decode_c_str(data, api_name). Success path = bytes.decode() as today. On UnicodeDecodeError, re-raises the same exception type with reason extended to include the originating CUDA API and a bounded hex preview of the buffer.

Implements the helper proposed inline on #2118 ("a tiny helper... source API name plus a bounded repr/hex dump"). #2118 fixed the WSL symptom in cuda_core; this closes the diagnostics gap in cuda_bindings.

Design

  • Pure-Python _internal/strdecode.py next to _fast_enum.py — no Cython rebuild
  • Same exception type; original chained via from e
  • Hex preview, stops at first NUL with stopped at NUL@<offset> marker,
    caps at 64 bytes with +N more

Example

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 0:
invalid start byte (returned by nvmlSystemGetProcessName;
bytes=<5 bytes; hex='f8 9a 80 80 af'>)

Scope of this PR

Two files, both hand-written:

  • cuda_bindings/cuda/bindings/_internal/strdecode.py — the helper
  • cuda_bindings/tests/test_strdecode.py — 11 unit tests

The existing _output_.decode() sites in nvml.error_string,
nvvm.get_error_string, nvfatbin.get_error_string, and
cufile.cufileop_status_error are good candidates to adopt this helper.
Those modules are maintained on the NVIDIA side, so wiring is left to the
maintainers rather than included here.

Refs #2118. Closes #2122.

- new pure-Python helper cuda.bindings._internal.strdecode.decode_c_str(data, api_name); success path unchanged from bytes.decode()

- on UnicodeDecodeError, re-raises same exception type with reason extended to include CUDA API name + bounded hex preview (stop at first NUL with offset marker, cap 64 bytes with +N more)

- exception type and __cause__ chain preserved so existing except UnicodeDecodeError keeps working

- placed in _internal/ next to _fast_enum.py: no Cython rebuild, unit-testable without compiling extensions

- generated .pyx call sites (nvml.error_string, nvvm.get_error_string, nvfatbin.get_error_string, cufile.cufileop_status_error) left untouched per cuda_bindings/CLAUDE.md; adoption belongs in next generator sync

Implements the helper rwgk proposed inline on NVIDIA#2118.

Refs NVIDIA#2118. Closes NVIDIA#2122.

Signed-off-by: Aryan Putta <aryansputta@gmail.com>
@copy-pr-bot
Copy link
Copy Markdown
Contributor

copy-pr-bot Bot commented May 22, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions
Copy link
Copy Markdown

Needs-Restricted-Paths-Review was assigned by CI: Restricted Paths Guard.

For details, open this workflow run and click Summary.

@github-actions github-actions Bot added cuda.bindings Everything related to the cuda.bindings module Needs-Restricted-Paths-Review PR touches cuda_bindings or cuda_python; only NVIDIA employees may modify these paths; see LICENSEs labels May 22, 2026
Signed-off-by: Aryan Putta <aryansputta@gmail.com>
Drop tests that verified stdlib behavior or trivial format strings; keep the 11 that pin a non-obvious invariant (issue spec, codec/position preservation, NUL/cap markers).

Signed-off-by: Aryan Putta <aryansputta@gmail.com>
No annotation in the file uses PEP 604 union syntax or forward references, so the import is unnecessary. Matches the style of the neighboring _fast_enum.py which does not carry it.
@aryanputta aryanputta force-pushed the decode-c-string-actionable branch from f631b8e to eb45dac Compare May 22, 2026 06:21
@aryanputta
Copy link
Copy Markdown
Contributor Author

aryanputta commented May 22, 2026

Requesting review when you have a chance. I kept this PR scoped to the helper itself. If this direction looks good, I can send a follow-up wiring it into the current decode sites in nvml, nvvm, nvfatbin, and cufile.

@rwgk
Copy link
Copy Markdown
Contributor

rwgk commented May 22, 2026

Hi @aryanputta, unfortunately we cannot accept external contributions to cuda-bindings :-(

It's not compatible with the cuda-bindings LICENSE terms.

I should probably add that explanation to the automatic bot comment above.

@rwgk rwgk closed this May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module Needs-Restricted-Paths-Review PR touches cuda_bindings or cuda_python; only NVIDIA employees may modify these paths; see LICENSEs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH]: Make cuda_bindings UnicodeDecodeError more actionable

2 participants