Add fallback for environments where ctypes.PythonAPI is not available.#938
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces fallback mechanisms for environments without CPython-specific ctypes APIs (such as PyPy) in both gcsfs/prefetcher.py and gcsfs/zb_hns_utils.py. When these APIs are unavailable, the code now gracefully falls back to standard Python slicing and bytearray operations. Corresponding unit tests have also been added to verify these fallback paths. The review feedback suggests explicitly defining the CPython-specific ctypes API variables as None in the fallback except blocks to prevent static analysis or linter warnings about undefined names. Additionally, it is recommended to refactor the defensive check in DirectMemmoveBuffer to avoid using a dummy value of -1 for _start_address when falling back to bytearray.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #938 +/- ##
==========================================
- Coverage 89.77% 89.68% -0.09%
==========================================
Files 16 16
Lines 3569 3579 +10
==========================================
+ Hits 3204 3210 +6
- Misses 365 369 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
55084a6 to
13d5a4d
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces safe fallbacks for non-CPython environments (such as PyPy) where CPython-specific ctypes APIs are unavailable. It wraps the loading of PyBytes_FromStringAndSize and PyBytes_AsString in try-except blocks across gcsfs/prefetcher.py and gcsfs/zb_hns_utils.py, falling back to standard Python slicing and bytearray operations when these APIs are missing. Additionally, unit tests are added to verify these fallback paths. The reviewer suggests importing the CPython API detection variables from gcsfs.prefetcher into gcsfs.zb_hns_utils.py to avoid code duplication.
13d5a4d to
68c1b98
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces fallback mechanisms for non-CPython environments (such as PyPy) where CPython-specific ctypes APIs are unavailable. It conditionally uses standard Python slicing and bytearray operations when HAS_CPYTHON_API is false, and adds corresponding unit tests to verify this fallback behavior. The reviewer suggests optimizing the non-CPython fallback path in DirectMemmoveBuffer by utilizing a memoryview over the bytearray to perform more efficient, in-place slice assignments.
68c1b98 to
ef99268
Compare
|
@zhixiangli, we can merge this. |
The newly introduced
prefetcherandbuffercomponents utilize thectypeslibrary, specifically relying on the CPython C-API (ctypes.pythonapi) for optimized, gil-free, zero-copy memory operations. Whilectypesis available in alternative Python implementations like PyPy, thectypes.pythonapimodule is not supported in those environments.To address this, this PR introduces a graceful fallback mechanism. If an exception occurs while attempting to load
ctypes.pythonapi, the code now safely falls back to native Python slicing and standard bytearray allocations. This ensures full cross-platform compatibility for the prefetcher and buffer across different Python implementations, trading a minor performance hit for broader support.More about this discussion can be found here: #888 (comment)