This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm-ffi.git


The following commit(s) were added to refs/heads/main by this push:
     new e6a85e9  [CYTHON] Fix ctypes.c_void_p for nullptr (#235)
e6a85e9 is described below

commit e6a85e9cbc4e2d0b8f414a2dfda051f4c37bb1a1
Author: Tianqi Chen <[email protected]>
AuthorDate: Fri Nov 7 13:09:13 2025 -0500

    [CYTHON] Fix ctypes.c_void_p for nullptr (#235)
    
    ctypes.c_void_p.value returns None instead of 0 when we construct a
    nullptr with value=0. This PR fixes the corner case.
---
 python/tvm_ffi/cython/base.pxi | 3 ++-
 tests/python/test_function.py  | 4 ++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/python/tvm_ffi/cython/base.pxi b/python/tvm_ffi/cython/base.pxi
index 0e3aa72..597956b 100644
--- a/python/tvm_ffi/cython/base.pxi
+++ b/python/tvm_ffi/cython/base.pxi
@@ -439,7 +439,8 @@ cdef inline object ctypes_handle(void* chandle):
 cdef inline void* c_handle(object handle):
     """Cast C types handle to c handle."""
     cdef unsigned long long v_ptr
-    v_ptr = handle.value
+    cdef object value = handle.value
+    v_ptr = 0 if value is None else value
     return <void*>(v_ptr)
 
 
diff --git a/tests/python/test_function.py b/tests/python/test_function.py
index 8d6dd34..deef0f5 100644
--- a/tests/python/test_function.py
+++ b/tests/python/test_function.py
@@ -72,6 +72,10 @@ def test_echo() -> None:
     assert isinstance(c_void_p_result, ctypes.c_void_p)
     assert c_void_p_result.value == 0x12345678
 
+    # test c_void_p for nullptr
+    c_void_p_nullptr_result = fecho(ctypes.c_void_p(0))
+    c_void_p_nullptr_result is None
+
     # test function: aka object
     fadd = tvm_ffi.convert(lambda a, b: a + b)
     fadd1 = fecho(fadd)

Reply via email to