feat: make bufflength a maximum width for str/bytes writes#43
Merged
Conversation
Previously bufflength acted as a minimum field width for str/bytes writes: the whole value was always written and shorter values were NUL-padded up to bufflength. This changes it to a maximum that truncates and never pads. For str the cap counts characters (applied before UTF-8 encoding, so multibyte characters are never split); for bytes it counts bytes. A value shorter than the cap is written as-is, and bufflength=None still writes the whole value. Update prepare_write accordingly and refresh the affected docstrings, guide pages, API reference and tests.
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.
Summary
Changes the meaning of
bufflengthforstr/byteswrites from a minimum field width to a maximum width that truncates.Previously the whole value was always written and shorter values were NUL-padded up to
bufflength(to clear a fixed-size field). Nowbufflengthis a hard cap that only ever shortens the value — it never pads.Behavior
str— the cap counts characters, applied before UTF-8 encoding, so multibyte characters are never split mid-sequence.bytes— the cap counts bytes.bufflength=Nonestill writes the whole value.write(a, str, 3, "ola")"ola"write(a, str, 3, "olá")"olá"write(a, str, 2, "ola")"ol"write(a, str, 2, "olá")"ol"write(a, str, 2, "óólá")"óó"write(a, bytes, 2, b"abc")b"ab"Changes
util/convert.py:prepare_writetruncates instead of padding.process/abstract.py,macos/process.py), guide pages (docs/guide/read-write.md), and API reference (docs/api/openprocess.md).tests/test_write_str_bytes_width.pyto assert the truncation semantics.Notes
The public method still returns the original
valuepassed in (not the truncated form), matching the existingwrite_process_memorycontract.