[PY] Support pickling FFI String and Bytes#643
Conversation
Pickled FFI string-like values should only persist their Python payload, avoiding serialization of the hidden cached FFI backing object.
There was a problem hiding this comment.
Code Review
This pull request implements custom pickling support for the String and Bytes classes in python/tvm_ffi/cython/string.pxi by adding __reduce_ex__ methods, along with corresponding unit tests to verify that cached FFI objects are cleared during roundtrip serialization. The reviewer feedback points out that the current __reduce_ex__ implementations return a 2-tuple, which discards the instance's __dict__ and causes any custom or subclass attributes to be silently lost during pickling. It is recommended to return the state dictionary (excluding _tvm_ffi_cached_object) as the third element of the reduction tuple to preserve these attributes.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Keep ordinary instance state when pickling FFI String and Bytes while still dropping the cached FFI backing object.
|
Addressed the latest Gemini comments in cb9272a:
|
Round-tripping FFI String and Bytes through pickle should produce raw Python str and bytes values instead of preserving FFI wrapper types.
|
Updated the pickle semantics in a030c1f per requested behavior:
This intentionally drops FFI wrapper type/subclass state across pickle, matching the desired raw Python payload behavior. |
Summary
tvm_ffi.core.Stringandtvm_ffi.core.Bytesso only the Python payload is serialized._tvm_ffi_cached_objectbacking object.testing.echo.Notes
apache/mainand supersedes [PY] Support pickling ffi.String without cached FFI object #642, which became dirty after main advanced.Bytes.__slots__is intentionally unchanged because Python does not support non-empty__slots__onbytessubclasses.Test plan
uv pip install --reinstall --verbose . && .venv/bin/pytest -vvs tests/python/test_string.pyPYTHONPATH=/raid/user_data/yixind/miniforge3/lib/python3.12/site-packages pre-commit run --files python/tvm_ffi/cython/string.pxi tests/python/test_string.py