[issue29328] struct module should support variable-length strings

Ethan Furman report at bugs.python.org
Thu Jan 19 16:15:08 EST 2017


Ethan Furman added the comment:

>From Yury Selivanov:
-------------------
This is a neat idea, but this will only work for parsing framed
binary protocols.  For example, if you protocol prefixes all packets
with a length field, you can write an efficient read buffer and
use your proposal to decode all of message's fields in one shot.
Which is good.

Not all protocols use framing though.  For instance, your proposal
won't help to write Thrift or Postgres protocols parsers.

Overall, I'm not sure that this is worth the hassle.  With proposal:

   data, = struct.unpack('!H$', buf)
   buf = buf[2+len(data):]

with the current struct module:

   len, = struct.unpack('!H', buf)
   data = buf[2:2+len]
   buf = buf[2+len:]

Another thing: struct.calcsize won't work with structs that use
variable length fields.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29328>
_______________________________________


More information about the Python-bugs-list mailing list