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

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


The following commit(s) were added to refs/heads/dev by this push:
     new cc93373  refactor: Prefer `bytearray_to_str|bytes` to raw Python API 
(#6)
cc93373 is described below

commit cc93373b344715422d158d14b5502d7c673a0153
Author: Junru Shao <[email protected]>
AuthorDate: Sun Sep 14 04:01:49 2025 -0700

    refactor: Prefer `bytearray_to_str|bytes` to raw Python API (#6)
---
 python/tvm_ffi/cython/base.pxi     |  4 ++++
 python/tvm_ffi/cython/dtype.pxi    |  4 ++--
 python/tvm_ffi/cython/function.pxi | 20 ++++++--------------
 python/tvm_ffi/cython/object.pxi   |  2 +-
 python/tvm_ffi/cython/string.pxi   |  4 ++--
 5 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/python/tvm_ffi/cython/base.pxi b/python/tvm_ffi/cython/base.pxi
index ef583c7..08f7df2 100644
--- a/python/tvm_ffi/cython/base.pxi
+++ b/python/tvm_ffi/cython/base.pxi
@@ -354,6 +354,10 @@ cdef inline str bytearray_to_str(const TVMFFIByteArray* x):
     return PyBytes_FromStringAndSize(x.data, x.size).decode("utf-8")
 
 
+cdef inline bytes bytearray_to_bytes(const TVMFFIByteArray* x):
+    return PyBytes_FromStringAndSize(x.data, x.size)
+
+
 cdef inline c_str(pystr):
     """Create ctypes char * from a python string
 
diff --git a/python/tvm_ffi/cython/dtype.pxi b/python/tvm_ffi/cython/dtype.pxi
index d9e20b7..4656b04 100644
--- a/python/tvm_ffi/cython/dtype.pxi
+++ b/python/tvm_ffi/cython/dtype.pxi
@@ -99,11 +99,11 @@ cdef class DataType:
         CHECK_CALL(TVMFFIDataTypeToString(&(self.cdtype), &temp_any))
         if temp_any.type_index == kTVMFFISmallStr:
             bytes = TVMFFISmallBytesGetContentByteArray(&temp_any)
-            res = py_str(PyBytes_FromStringAndSize(bytes.data, bytes.size))
+            res = bytearray_to_str(&bytes)
             return res
 
         bytes_ptr = TVMFFIBytesGetByteArrayPtr(temp_any.v_obj)
-        res = py_str(PyBytes_FromStringAndSize(bytes_ptr.data, bytes_ptr.size))
+        res = bytearray_to_str(bytes_ptr)
         CHECK_CALL(TVMFFIObjectDecRef(temp_any.v_obj))
         return res
 
diff --git a/python/tvm_ffi/cython/function.pxi 
b/python/tvm_ffi/cython/function.pxi
index ce6ea54..c80e238 100644
--- a/python/tvm_ffi/cython/function.pxi
+++ b/python/tvm_ffi/cython/function.pxi
@@ -37,14 +37,14 @@ cdef inline object make_ret_small_str(TVMFFIAny result):
     """convert small string to return value."""
     cdef TVMFFIByteArray bytes
     bytes = TVMFFISmallBytesGetContentByteArray(&result)
-    return py_str(PyBytes_FromStringAndSize(bytes.data, bytes.size))
+    return bytearray_to_str(&bytes)
 
 
 cdef inline object make_ret_small_bytes(TVMFFIAny result):
     """convert small bytes to return value."""
     cdef TVMFFIByteArray bytes
     bytes = TVMFFISmallBytesGetContentByteArray(&result)
-    return PyBytes_FromStringAndSize(bytes.data, bytes.size)
+    return bytearray_to_bytes(&bytes)
 
 
 cdef inline object make_ret(TVMFFIAny result, DLPackToPyObject 
c_dlpack_to_pyobject = NULL):
@@ -689,12 +689,8 @@ def _add_class_attrs_by_reflection(int type_index, object 
cls):
         (<FieldSetter>setter).offset = field.offset
         if (field.flags & kTVMFFIFieldFlagBitMaskWritable) == 0:
             setter = None
-        doc = (
-            py_str(PyBytes_FromStringAndSize(field.doc.data, field.doc.size))
-            if field.doc.size != 0
-            else None
-        )
-        name = py_str(PyBytes_FromStringAndSize(field.name.data, 
field.name.size))
+        doc = bytearray_to_str(&field.doc) if field.doc.size != 0 else None
+        name = bytearray_to_str(&field.name)
         if hasattr(cls, name):
             # skip already defined attributes
             continue
@@ -703,12 +699,8 @@ def _add_class_attrs_by_reflection(int type_index, object 
cls):
     for i in range(num_methods):
         # attach methods to the class
         method = &(info.methods[i])
-        name = py_str(PyBytes_FromStringAndSize(method.name.data, 
method.name.size))
-        doc = (
-            py_str(PyBytes_FromStringAndSize(method.doc.data, method.doc.size))
-            if method.doc.size != 0
-            else None
-        )
+        name = bytearray_to_str(&method.name)
+        doc = bytearray_to_str(&method.doc) if method.doc.size != 0 else None
         method_func = _get_method_from_method_info(method)
 
         if method.flags & kTVMFFIFieldFlagBitMaskIsStaticMethod:
diff --git a/python/tvm_ffi/cython/object.pxi b/python/tvm_ffi/cython/object.pxi
index 1d026b2..90821e3 100644
--- a/python/tvm_ffi/cython/object.pxi
+++ b/python/tvm_ffi/cython/object.pxi
@@ -257,7 +257,7 @@ cdef inline str _type_index_to_key(int32_t tindex):
     if info == NULL:
         return "<unknown>"
     type_key = &(info.type_key)
-    return py_str(PyBytes_FromStringAndSize(type_key.data, type_key.size))
+    return bytearray_to_str(type_key)
 
 
 cdef inline object make_ret_opaque_object(TVMFFIAny result):
diff --git a/python/tvm_ffi/cython/string.pxi b/python/tvm_ffi/cython/string.pxi
index 0737259..4119e7b 100644
--- a/python/tvm_ffi/cython/string.pxi
+++ b/python/tvm_ffi/cython/string.pxi
@@ -20,12 +20,12 @@
 
 cdef inline str _string_obj_get_py_str(obj):
     cdef TVMFFIByteArray* bytes = 
TVMFFIBytesGetByteArrayPtr((<Object>obj).chandle)
-    return py_str(PyBytes_FromStringAndSize(bytes.data, bytes.size))
+    return bytearray_to_str(bytes)
 
 
 cdef inline bytes _bytes_obj_get_py_bytes(obj):
     cdef TVMFFIByteArray* bytes = 
TVMFFIBytesGetByteArrayPtr((<Object>obj).chandle)
-    return PyBytes_FromStringAndSize(bytes.data, bytes.size)
+    return bytearray_to_bytes(bytes)
 
 
 

Reply via email to