Skip to content

fix: don't truncate the last byte of FileStructure.struntil - #2761

Open
tarann26 wants to merge 2 commits into
Gallopsled:devfrom
tarann26:fix-struntil-off-by-one
Open

tarann26 wants to merge 2 commits into
Gallopsled:devfrom
tarann26:fix-struntil-off-by-one

Conversation

@tarann26

Copy link
Copy Markdown
Contributor

FileStructure.struntil(v) is documented to return the structure up to and including field v, but it ended with return structure[:-1], which dropped the last byte of that final field. So struntil("flags") returned 7 bytes instead of the full 8, and every call came back one byte short. This removes the truncation so the payload includes the whole of field v, as the docstring already promises.

This also affects write() and read(), which build their payloads from struntil('fileno'). Their output is now 116 bytes where it used to be 115. The extra byte is the final byte of the fileno field that was previously being cut off, so this is the corrected behaviour rather than a change in intent, and the doctests for both methods have been updated to match.

The stray slice has been there since the module was first added, and there's no compensating logic elsewhere. struntil has no other callers in the tree, so the blast radius is limited to these three doctests.

Testing

Updated the three existing doctests whose expected output gains a byte (struntil("_IO_buf_end"), write(), read()), and added two length checks to the struntil docstring so the expected sizes are explicit:

>>> len(fileStr.struntil("flags"))
8
>>> len(fileStr.struntil("_IO_buf_end"))
72
python -m sphinx -b doctest docs/source docs/build/doctest docs/source/filepointer.rst

gives 43 passed, 0 failures (it was 41 before). Also checked on i386, where struntil("flags") correctly returns 4 bytes.

Target Branch

Targeting dev. The issue is labelled backport-required — happy to redo this against stable or beta if you'd rather it land there first.

Fixes #2403.

This branch has not been deployed

No deployments
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.

[Bug?] FileStructure.struntil one byte shorter for no apparent reason?

1 participant