Repository navigation
Fix setattr with empty or non-contiguous data (and enable an abi3 build) - #184
Merged
Merged
Conversation
setattr with an empty value raised IndexError (Out of bounds on buffer access) and with a non-contiguous bytes-like value raised BufferError, both from the typed memoryview. getattr now takes a plain pointer into its bytearray; setattr turns any non-bytes value into bytes through memoryview() and takes its pointer. Values with items wider than one byte are still refused with ValueError, and non-bytes-like values with TypeError. Without typed memoryviews the extension no longer needs Py_buffer, which the limited API only has from Python 3.11, so it can be built against the stable ABI for older Pythons.
LITTLEFS_PY_LIMITED_API=<major.minor> (3.9 or later) builds the extension against the CPython stable ABI and tags the wheel cpXY-abi3, so one wheel runs on that Python and every later one. It needs Cython 3.1 or later, which setup.py checks only for this build. Unset, the build is unchanged. The abi3 workflow builds that wheel on Python 3.9 on Linux, macOS and Windows and runs the tests with it on Python 3.9 and 3.14, so a change that uses anything outside the 3.9 limited API fails in CI. Release wheels are not changed.
Compiling against the 3.9 limited API and importing on 3.9 is what catches a change outside it; later Pythons and other platforms are covered by the stable ABI itself. One job also cannot be blocked by another platform's runner.
Contributor
Author
|
Reduced the abi3 workflow to a single Ubuntu job (build the wheel on Python 3.9, run the tests with it). The previous macOS failures were runner capacity ("job was not acquired by Runner"), not the code. |
Collaborator
|
Thanks amgross! I'll try and review this tonight; if copilot gives bad feedback you can certainly ignore it. |
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues blocking approval.
No open issues 🎉
What changed in this PR
This PR fixes attribute writes for empty and non-contiguous bytes-like values and adds an optional stable-ABI build for the LittleFS Python extension. Release wheel generation remains unchanged.
Changes:
- Replace typed memoryviews in attribute access with pointers, normalizing non-
bytesinputs while retaining input validation. - Add regression tests for supported buffers, rejected types, wider items, and empty values.
- Add opt-in abi3 wheel configuration, documentation, and a Linux CI job that builds and tests on Python 3.9.
| File | Description |
|---|---|
test/test_attr.py |
Adds attribute input and round-trip regression tests. |
src/littlefs/lfs.pyx |
Removes typed memoryviews and handles empty and strided attribute data. |
setup.py |
Adds environment-controlled limited-API builds and a Cython version check. |
README.rst |
Documents the optional abi3 build command and requirements. |
.github/workflows/abi3.yml |
Builds, installs, and tests the abi3 wheel on Python 3.9. |
Review effort: Balanced
Contributor
Author
|
Thank you very much @BrianPugh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bugs in setattr
setattrfails on valid input, because of the typed memoryview (const unsigned char[::1]) it takes the data through:fs.setattr("/file.txt", "a", b"")raisesIndexError: Out of bounds on buffer accessfs.setattr("/file.txt", "a", memoryview(b"aXbXcX")[::2])raisesBufferError(Try the new tests in
test/test_attr.pyon current master to see both failures.)Solution
getattr/setattrno longer use typed memoryviews:getattrtakes a plain pointer into thebytearrayit already allocates.setattrturns any non-bytesvalue intobytesthroughmemoryview()and takes its pointer.Behaviour is otherwise as before: values with items wider than one byte (e.g.
array('H')) still raiseValueError, and non-bytes-like values (int,str,list) still raiseTypeError.New tests: bytes / bytearray / memoryview /
array('B')/ strided memoryview stored and read back asbytes; rejected types; wider items; empty value.Great side effect: abi3 build
Typed memoryviews need
Py_buffer, which the CPython limited API only has from 3.11. Without them the extension can be built once against the stable ABI (abi3) for CPython 3.9 and every later version. So this PR also adds:setup.py: opt-in,LITTLEFS_PY_LIMITED_API=3.9 python -m build --wheelbuilds acp39-abi3wheel. It needs Cython 3.1 or later (3.0 cannot build it), which is checked only for this build. Unset, nothing changes..github/workflows/abi3.yml: one Linux job that builds the abi3 wheel on Python 3.9 and runspytestwith it, so anything outside the 3.9 limited API is caught in CI.README.rst: a short note on the abi3 build.Not changed
Release wheels:
deploy.ymlstill builds one wheel per Python version. Switching releases to abi3 (onecp39-abi3wheel per platform, pluscp38while 3.8 is supported) would cut the cibuildwheel matrix, but it is your call: Cython's limited API code is somewhat slower, and free-threaded builds (3.14t) cannot use abi3.