[New-bugs-announce] [issue27446] struct: allow per-item byte order

Zack Weinberg report at bugs.python.org
Sun Jul 3 12:20:09 EDT 2016


New submission from Zack Weinberg:

Occasionally one encounters binary formats which do not stick to one byte order throughout.  For example, I have a C program that reads and writes  arrays of this struct:

```
struct conn_data
{
  uint32_t ipv4_addr; /* read - network byte order - target IPv4 address */
  uint16_t tcp_port;  /* read - network byte order - target TCP port */
  uint16_t errnm;     /* write - native byte order - errno code */
  uint64_t elapsed;   /* write - native byte order - elapsed time in ns */
};
```

On little-endian systems, the first and second fields of this struct will be in the opposite byte order from the third and fourth.

If the struct module allowed specification of byte order on a per-item basis, it would make working with structures of this type simpler.  Hypothetical notation:

```
addr, port, errnm, elapsed = struct.unpack("=4s!H=H=Q")
addr = socket.inet_ntoa(addr)
```

instead of what one must do now,

```
addr, port, errnm, elapsed = struct.unpack("=4sHHQ")
addr = socket.inet_ntoa(addr)
port = socket.ntohs(port)
```

To avoid ambiguities and confusion, I would suggest that, if more than one item has its own byte-order specifier, _all_ items must have byte-order specifiers (with the possible exception of the 1-byte item types?), and that this is not allowed in an '@'-mode format.

----------
components: Library (Lib)
messages: 269770
nosy: zwol
priority: normal
severity: normal
status: open
title: struct: allow per-item byte order
type: enhancement

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


More information about the New-bugs-announce mailing list