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 mark.dickinson
Recipients eric.smith, mark.dickinson, meador.inge, sgk284
Date 2010-04-10.20:26:40
SpamBayes Score 3.6209924e-13
Marked as misclassified No
Message-id <1270931203.74.0.614232146661.issue7355@psf.upfronthosting.co.za>
In-reply-to
Content
> 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').
History
Date User Action Args
2010-04-10 20:26:43mark.dickinsonsetrecipients: + mark.dickinson, eric.smith, meador.inge, sgk284
2010-04-10 20:26:43mark.dickinsonsetmessageid: <1270931203.74.0.614232146661.issue7355@psf.upfronthosting.co.za>
2010-04-10 20:26:42mark.dickinsonlinkissue7355 messages
2010-04-10 20:26:40mark.dickinsoncreate