Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

struct.calcsize() incorrect #61817

Closed
boa124 mannequin opened this issue Apr 2, 2013 · 3 comments
Closed

struct.calcsize() incorrect #61817

boa124 mannequin opened this issue Apr 2, 2013 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@boa124
Copy link
Mannequin

boa124 mannequin commented Apr 2, 2013

BPO 17617
Nosy @mdickinson
Superseder
  • bpo-7355: Struct incorrectly compiles format strings
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-04-02.13:37:44.516>
    created_at = <Date 2013-04-02.12:40:51.549>
    labels = ['type-bug', 'library']
    title = 'struct.calcsize() incorrect'
    updated_at = <Date 2013-04-02.13:37:44.515>
    user = 'https://bugs.python.org/boa124'

    bugs.python.org fields:

    activity = <Date 2013-04-02.13:37:44.515>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-04-02.13:37:44.516>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2013-04-02.12:40:51.549>
    creator = 'boa124'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 17617
    keywords = []
    message_count = 3.0
    messages = ['185834', '185837', '185839']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'boa124']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '7355'
    type = 'behavior'
    url = 'https://bugs.python.org/issue17617'
    versions = ['Python 3.4']

    @boa124
    Copy link
    Mannequin Author

    boa124 mannequin commented Apr 2, 2013

    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

    @boa124 boa124 mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 2, 2013
    @mdickinson
    Copy link
    Member

    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 bpo-7189, bpo-5145.

    @mdickinson
    Copy link
    Member

    And also bpo-7355, which led to that example being added to the documentation. Closing as duplicate.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant