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.

classification
Title: struct.calcsize() incorrect
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Struct incorrectly compiles format strings
View: 7355
Assigned To: Nosy List: boa124, mark.dickinson
Priority: normal Keywords:

Created on 2013-04-02 12:40 by boa124, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg185834 - (view) Author: bonnet (boa124) Date: 2013-04-02 12:40
struct.calcsize('iiiii?') : 21

appears to be incorrect ; in C sizeof : 24

C code:
typedef struct {
  int	param1;
  int	param2;
  int	param3;
  int	param4;
  int	param5;
  unsigned char param6;
} struct_titi6;

main:
struct_titi6 toto3;
printf("taille toto3 : %d\n",sizeof(toto3));
printf("taille toto3.param1 : %d\n",sizeof(toto3.param1));
printf("taille toto3.param6 : %d\n",sizeof(toto3.param6));

results:
taille toto3 : 24
taille toto3.param1 : 4
taille toto3.param6 : 1
msg185837 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-04-02 13:34
This is working as designed: the result of struct.calcsize matches the length of the string that you'll get back from struct.pack using that format.  Padding isn't included after the last entry in the struct.

That's not to say that it was a good design decision, but that's the design that was chosen, and it's not realistic to change this for the bugfix releases.  It may be possible to do something for Python 3.4.

You can add a '0L' to the end of your struct to force padding bytes at the end.  See:

http://docs.python.org/2/library/struct.html#examples

for an example of this.

See also #7189, #5145.
msg185839 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-04-02 13:37
And also #7355, which led to that example being added to the documentation.  Closing as duplicate.
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61817
2013-04-02 13:37:44mark.dickinsonsetstatus: open -> closed
superseder: Struct incorrectly compiles format strings
resolution: duplicate
messages: + msg185839
2013-04-02 13:34:06mark.dickinsonsetnosy: + mark.dickinson

messages: + msg185837
versions: + Python 3.4, - Python 2.6
2013-04-02 12:40:51boa124create