Skip to content

feat: make bufflength a maximum width for str/bytes writes#43

Merged
JeanExtreme002 merged 1 commit into
mainfrom
jeanextreme002/str-bytes-write-max-width
Jun 4, 2026
Merged

feat: make bufflength a maximum width for str/bytes writes#43
JeanExtreme002 merged 1 commit into
mainfrom
jeanextreme002/str-bytes-write-max-width

Conversation

@JeanExtreme002
Copy link
Copy Markdown
Owner

Summary

Changes the meaning of bufflength for str / bytes writes 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). Now bufflength is 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.
  • A value shorter than the cap is written as-is (no padding).
  • bufflength=None still writes the whole value.
Call Writes Bytes
write(a, str, 3, "ola") "ola" 3
write(a, str, 3, "olá") "olá" 4
write(a, str, 2, "ola") "ol" 2
write(a, str, 2, "olá") "ol" 2
write(a, str, 2, "óólá") "óó" 4
write(a, bytes, 2, b"abc") b"ab" 2

Changes

  • util/convert.py: prepare_write truncates instead of padding.
  • Refreshed docstrings (process/abstract.py, macos/process.py), guide pages (docs/guide/read-write.md), and API reference (docs/api/openprocess.md).
  • Updated tests/test_write_str_bytes_width.py to assert the truncation semantics.

Notes

The public method still returns the original value passed in (not the truncated form), matching the existing write_process_memory contract.

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.
@JeanExtreme002 JeanExtreme002 merged commit 2a0ac68 into main Jun 4, 2026
12 checks passed
@github-actions github-actions Bot deleted the jeanextreme002/str-bytes-write-max-width branch June 4, 2026 22:54
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.

1 participant