Skip to content

gh-163051: Fix ShareableList layout for empty str/bytes - #163052

Open
hyongtao-code wants to merge 4 commits into
python:mainfrom
hyongtao-code:fix-empty
Open

hyongtao-code wants to merge 4 commits into
python:mainfrom
hyongtao-code:fix-empty

Conversation

@hyongtao-code

@hyongtao-code hyongtao-code commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

An empty str/bytes was given a zero-length slot: the item size is now
computed as a ceiling division,

self._alignment * ((len(encoded) - 1) // self._alignment + 1)

which yields 0 when len(encoded) == 0. The item then shares its offset
with the next item, or with the packing-format metadata when it is the
last one, so filling that slot either raises a misleading ValueError or
silently overwrites the neighbour (and, in the trailing case, destroys
the block). Clamp the result to one alignment unit.

The layout of non-empty items is unchanged, so blocks created by earlier
releases keep working.

Add a regression test covering empty str and bytes in the first, middle
and last position.

Signed-off-by: Yongtao Huang yongtaoh2022@gmail.com

@efanntyo

efanntyo commented Dec 3, 2026

Copy link
Copy Markdown

Could the regression also construct a list whose initial last element is empty, e.g. [42, ''], [42, b''], [''] and [b'']? At head 9b2aedf, the new fixture starts with ['', b'', 42]; assigning sl[2] = '' later reuses the int's already allocated eight-byte slot, so it does not exercise a zero-length trailing allocation at construction. That is the branch where the issue describes overlap with packing-format metadata.

As an earlier-runtime control, I ran all four constructor cases on CPython 3.12.16/Linux, attached a second ShareableList(name=sl.shm.name), and replaced the last element with 7. Initial formats were q8s / 8s; afterward both views read [42, 7] / [7], with formats qq / q. Both handles were closed and the owned block unlinked in finally.

Testing constructor-time trailing empties plus reading the attached-by-name view after mutation would cover both metadata survival and layout reconstruction. This is proposed coverage after source review, not a reproduction of the 3.16 failure or execution of this PR build. AI-assisted investigation; the 3.12 control was run locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants