[issue7355] Struct incorrectly compiles format strings

Mark Dickinson report at bugs.python.org
Sat Apr 10 22:26:43 CEST 2010

Mark Dickinson <dickinsm at gmail.com> added the comment:

> When and why things are padded usually depends on the compiler, OS,
> and computer architecture. [...]

Sure.  The struct module has a rather simplistic set of rules for adding padding, but I believe that it manages to match the platform's C rules (assuming no relevant extra compile flags) in most cases.

If we wanted something that's *guaranteed* to match C then I agree that's probably not easy within the struct module itself;  it probably belongs in ctypes somewhere instead.

For trailing padding, one fairly obvious and simple rule to use (to match the simplicity of the current rules for padding) would be to add enough padding so that the alignment of the struct matches the largest alignment of any member of the struct.  So e.g. on a platform where doubles are aligned to 8 bytes and ints to 4 bytes, 'db' would have 7 bytes of trailing padding, while 'iib' would have 3 bytes.  'b' would have no trailing padding at all.  I *think* (but would have to check) that this again matches what happens on common platforms.

> Another thing that came to mind when thinking about structure layout
> issues was whether it might be useful to provide "packed" structures

These already exist, provided that you're willing to accept 'standard' rather than 'native' sizes.  The struct modes for '<', '>', '=' and '!' never introduce padding (unless it's explicitly asked for with 'x').


On the subject of documentation, I think there are two points that need to be made more clearly in the current docs:

(1) *By default* (i.e. without a '<', '>', ...), padding *is* used;  but is automatically turned off with '<', '>', '=', '!'.  Ideally this should be mentioned very early on, since it seems to trip up a lot of people.

(2) Padding is only added between successive struct members; there's no trailing padding.  But padding can be forced using Eric's trick (though you end up having to choose exactly what alignment you want, e.g. by specifying '0i' rather than '0q' or '0d').


Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list