You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "tqchen (via GitHub)" <gi...@apache.org> on 2026/04/26 16:43:41 UTC
[PR] [FIX] Handle kTVMFFIRawStr / kTVMFFIByteArrayPtr in callback args path [tvm-ffi]
tqchen opened a new pull request, #573:
URL: https://github.com/apache/tvm-ffi/pull/573
## Summary
- **Bug**: When C++ invokes a Python callback with non-owning `RawStr` or `ByteArrayPtr` arg shapes, `TVMFFICyCallbackArgSetterFactory` (in `pycallback.pxi`) raised `ValueError` for both type indices, crashing the callback immediately.
- **Fix**: Add two new Cython setter functions — `TVMFFIPyCallbackArgSetterRawStr_` and `TVMFFIPyCallbackArgSetterByteArrayPtr_` — and register them in `TVMFFICyCallbackArgSetterFactory`. Each setter converts the non-owning C-side view to a Python `str` (UTF-8 decode) or `bytes` (memcopy) before returning. Both non-owning views remain valid for the callback's duration, so there is no dangling-pointer concern.
- **Why not change `make_ret`**: The return-value path (C-side `ffi::Function` invocation) never carries `kTVMFFIRawStr` / `kTVMFFIByteArrayPtr` because C++ normalises to owned variants (`ffi::String` / `ffi::Bytes`) before returning. The existing `raise ValueError` arms in `make_ret` remain correct guards against an impossible return-side type, exactly like the `kTVMFFIObjectRValueRef` arm.
- **Test**: One regression test in `tests/python/test_function.py` (`test_callback_rawstr_and_bytearrayptr_args`) uses `tvm_ffi.cpp.load_inline` to build a C++ shim with two trampolines:
- `invoke_with_raw_str(callback)` — passes a C-string literal as `const char*`, which `TypeTraits<const char*>::CopyToAnyView` packs as `kTVMFFIRawStr` (with `v_c_str`).
- `invoke_with_byte_array_ptr(callback)` — passes a stack `TVMFFIByteArray*`, which `TypeTraits<TVMFFIByteArray*>::CopyToAnyView` packs as `kTVMFFIByteArrayPtr` (with `v_ptr`).
Both paths are exercised in a single test body; the callback asserts it receives a `str` / `bytes` with the expected content.
## Files changed
- `python/tvm_ffi/cython/pycallback.pxi` — two new setter functions + factory registration
- `tests/python/test_function.py` — one regression test + `import tvm_ffi.cpp`
## Test plan
- [ ] `uv run pytest -vvs tests/python/test_function.py` — 20 passed (was 19)
- [ ] `uv run pytest tests/python` — 2303 passed, 5 skipped, 2 xfailed (3 pre-existing ROCm failures unrelated to this change)
- [ ] `pre-commit run --files python/tvm_ffi/cython/pycallback.pxi tests/python/test_function.py` — all hooks passed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org
Re: [PR] [FIX] Handle kTVMFFIRawStr / kTVMFFIByteArrayPtr in callback args path [tvm-ffi]
Posted by "cyx-6 (via GitHub)" <gi...@apache.org>.
cyx-6 merged PR #573:
URL: https://github.com/apache/tvm-ffi/pull/573
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org