Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions python/tvm_ffi/cython/string.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class String(str, PyNativeObject):
val._tvm_ffi_cached_object = None
return val

def __reduce_ex__(self, protocol):
return (type(self), (str(self),))
Comment thread
Ubospica marked this conversation as resolved.
Outdated

# pylint: disable=no-self-argument
def __from_tvm_ffi_object__(cls, obj: Any) -> "String":
"""Construct a ``String`` from an FFI object (internal)."""
Expand All @@ -80,6 +83,9 @@ class Bytes(bytes, PyNativeObject):
val._tvm_ffi_cached_object = None
return val

def __reduce_ex__(self, protocol):
return (type(self), (bytes(self),))
Comment thread
Ubospica marked this conversation as resolved.
Outdated

# pylint: disable=no-self-argument
def __from_tvm_ffi_object__(cls, obj: Any) -> "Bytes":
"""Construct ``Bytes`` from an FFI object (internal)."""
Expand Down
19 changes: 19 additions & 0 deletions tests/python/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pickle

import tvm_ffi
import tvm_ffi.testing


def test_string() -> None:
Expand All @@ -34,6 +35,15 @@ def test_string() -> None:
s4 = pickle.loads(pickle.dumps(s))
assert s4 == "hello"

cached = fecho("x" * 200)
assert isinstance(cached, tvm_ffi.core.String)
assert cached._tvm_ffi_cached_object is not None

cached_roundtrip = pickle.loads(pickle.dumps(cached))
assert isinstance(cached_roundtrip, tvm_ffi.core.String)
assert cached_roundtrip == cached
assert cached_roundtrip._tvm_ffi_cached_object is None


def test_bytes() -> None:
fecho = tvm_ffi.get_global_func("testing.echo")
Expand All @@ -54,6 +64,15 @@ def test_bytes() -> None:
assert b5 == b"hello"
assert isinstance(b5, tvm_ffi.core.Bytes)

cached = fecho(b"x" * 200)
assert isinstance(cached, tvm_ffi.core.Bytes)
assert cached._tvm_ffi_cached_object is not None

cached_roundtrip = pickle.loads(pickle.dumps(cached))
assert isinstance(cached_roundtrip, tvm_ffi.core.Bytes)
assert cached_roundtrip == cached
assert cached_roundtrip._tvm_ffi_cached_object is None


def test_string_find_substr() -> None:
s = tvm_ffi.core.String("hello world")
Expand Down
Loading