Skip to content

Fix setattr with empty or non-contiguous data (and enable an abi3 build) - #184

Merged
BrianPugh merged 3 commits into
jrast:masterfrom
amgross:abi3
Nov 30, 2026
Merged

BrianPugh merged 3 commits into
jrast:masterfrom
amgross:abi3

Conversation

@amgross

@amgross amgross commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Bugs in setattr

setattr fails on valid input, because of the typed memoryview (const unsigned char[::1]) it takes the data through:

  • empty value: fs.setattr("/file.txt", "a", b"") raises IndexError: Out of bounds on buffer access
  • non-contiguous bytes-like value: fs.setattr("/file.txt", "a", memoryview(b"aXbXcX")[::2]) raises BufferError

(Try the new tests in test/test_attr.py on current master to see both failures.)

Solution

getattr / setattr no longer use typed memoryviews:

  • getattr takes a plain pointer into the bytearray it already allocates.
  • setattr turns any non-bytes value into bytes through memoryview() and takes its pointer.

Behaviour is otherwise as before: values with items wider than one byte (e.g. array('H')) still raise ValueError, and non-bytes-like values (int, str, list) still raise TypeError.

New tests: bytes / bytearray / memoryview / array('B') / strided memoryview stored and read back as bytes; 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 --wheel builds a cp39-abi3 wheel. 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 runs pytest with 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.yml still builds one wheel per Python version. Switching releases to abi3 (one cp39-abi3 wheel per platform, plus cp38 while 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.

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.
@amgross

amgross commented Nov 26, 2026

Copy link
Copy Markdown
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.

@BrianPugh

Copy link
Copy Markdown
Collaborator

Thanks amgross! I'll try and review this tonight; if copilot gives bad feedback you can certainly ignore it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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-bytes inputs 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

@BrianPugh
BrianPugh merged commit f04a1ce into jrast:master Nov 30, 2026
26 checks passed
@amgross

amgross commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you very much @BrianPugh

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.

3 participants