Skip to content

Add fallback for environments where ctypes.PythonAPI is not available.#938

Merged
zhixiangli merged 1 commit into
fsspec:mainfrom
ankitaluthra1:pypy-support
Jun 26, 2026
Merged

Add fallback for environments where ctypes.PythonAPI is not available.#938
zhixiangli merged 1 commit into
fsspec:mainfrom
ankitaluthra1:pypy-support

Conversation

@googlyrahman

Copy link
Copy Markdown
Collaborator

The newly introduced prefetcher and buffer components utilize the ctypes library, specifically relying on the CPython C-API (ctypes.pythonapi) for optimized, gil-free, zero-copy memory operations. While ctypes is available in alternative Python implementations like PyPy, the ctypes.pythonapi module 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)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread gcsfs/prefetcher.py Outdated
Comment thread gcsfs/zb_hns_utils.py
Comment thread gcsfs/zb_hns_utils.py
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.23529% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.68%. Comparing base (3bd3383) to head (ef99268).

Files with missing lines Patch % Lines
gcsfs/zb_hns_utils.py 84.61% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@googlyrahman

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread gcsfs/zb_hns_utils.py
@googlyrahman

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread gcsfs/zb_hns_utils.py
Comment thread gcsfs/zb_hns_utils.py Outdated
@googlyrahman

Copy link
Copy Markdown
Collaborator Author

@zhixiangli, we can merge this.

@zhixiangli zhixiangli merged commit a38076b into fsspec:main Jun 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants