This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Elizacat
Recipients Elizacat
Date 2017-01-19.18:24:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1484850272.25.0.472309940141.issue29328@psf.upfronthosting.co.za>
In-reply-to
Content
There was some discussion on python-ideas about this, and I figured it would be more productive to bring it here since to me this appears to be a glaring omission.

The struct module has no capability to support variable-length strings; this includes null-terminated and Pascal-ish strings with a different integer datatype (usually in binary) specifying length.

This unfortunate omission makes the struct module extremely unwieldy to use in situations where you need to unpack a lot of variable-length strings, especially iteratively; see https://mail.python.org/pipermail/python-ideas/2017-January/044328.html for why. For zero-terminated strings, it is essentially impossible.

It's worth noting many modern protocols use variable-length strings, including DHCP.

I therefore propose the following extensions to the struct module (details can be bikeshedded over :P):

- Z (uppercase) format specifier (I did not invent this idea, see https://github.com/stendec/netstruct - although that uses $), which states the preceding whole-number datatype is the length of a string that follows.
- z (lowercase) format specifier, which specifies a null-terminated (also known as C style) string. An optional length parameter can be added to specify the maximum search length.

These two additions will make the struct module much more usable in a wider variety of contexts.
History
Date User Action Args
2017-01-19 18:24:32Elizacatsetrecipients: + Elizacat
2017-01-19 18:24:32Elizacatsetmessageid: <1484850272.25.0.472309940141.issue29328@psf.upfronthosting.co.za>
2017-01-19 18:24:32Elizacatlinkissue29328 messages
2017-01-19 18:24:32Elizacatcreate