classification
Title: doc: clarify that struct.pack_into writes 0x00 for pad bytes
Type: behavior Stage:
Components: Documentation Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: bsder, docs@python, r.david.murray
Priority: normal Keywords: easy

Created on 2014-02-28 09:17 by bsder, last changed 2021-06-18 09:51 by iritkatriel.

Messages (2)
msg212416 - (view) Author: Andrew P. Lentvorski, Jr. (bsder) Date: 2014-02-28 09:17
This code did something unexpected to me:
>>> a = bytearray('1234')
>>> a
bytearray(b'1234')
>>> struct.pack_into('xBxB', a, 0, 0x59, 0x5A)
>>> a
bytearray(b'\x00Y\x00Z')


The unexpected part was that the 'x' pad byte formatter actually *overwrote* the bytes to 0 rather than leaving them alone.

Not necessarily a bug, but the fact that the pad byte writes 0x00 rather than being untouched/ignored should be documented.
msg212433 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-28 14:07
Contrawise, I would have found it very surprising if it had not padded with null bytes, but yes, the docs could be explicit about it by saying that 'x' padding means inserting null bytes.
History
Date User Action Args
2021-06-18 09:51:08iritkatrielsettitle: struct.pack_into writes 0x00 for pad bytes -> doc: clarify that struct.pack_into writes 0x00 for pad bytes
nosy: + docs@python

assignee: docs@python
versions: + Python 3.11, - Python 2.7
components: + Documentation, - Interpreter Core
keywords: + easy
2014-02-28 14:07:44r.david.murraysetnosy: + r.david.murray
messages: + msg212433
2014-02-28 09:17:29bsdercreate