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.pack("I", "foo"); struct.pack("L", "foo") should fail #45118

Closed
theller opened this issue Jun 21, 2007 · 8 comments
Closed

struct.pack("I", "foo"); struct.pack("L", "foo") should fail #45118

theller opened this issue Jun 21, 2007 · 8 comments
Assignees
Labels
extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@theller
Copy link

theller commented Jun 21, 2007

BPO 1741130
Nosy @theller, @mdickinson, @devdanzin
Files
  • fail_pack_non_int.diff: Check the type of the arg to pack(), with trivial tests.
  • issue1741130.patch
  • 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 = 'https://github.com/mdickinson'
    closed_at = <Date 2009-07-05.10:08:20.727>
    created_at = <Date 2007-06-21.18:36:53.000>
    labels = ['extension-modules', 'type-bug']
    title = 'struct.pack("I", "foo"); struct.pack("L", "foo") should fail'
    updated_at = <Date 2009-07-05.10:08:20.726>
    user = 'https://github.com/theller'

    bugs.python.org fields:

    activity = <Date 2009-07-05.10:08:20.726>
    actor = 'mark.dickinson'
    assignee = 'mark.dickinson'
    closed = True
    closed_date = <Date 2009-07-05.10:08:20.727>
    closer = 'mark.dickinson'
    components = ['Extension Modules']
    creation = <Date 2007-06-21.18:36:53.000>
    creator = 'theller'
    dependencies = []
    files = ['13968', '14451']
    hgrepos = []
    issue_num = 1741130
    keywords = ['patch']
    message_count = 8.0
    messages = ['32377', '84737', '85043', '86181', '87607', '90109', '90129', '90149']
    nosy_count = 4.0
    nosy_names = ['theller', 'mark.dickinson', 'ajaksu2', 'vdupras']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1741130'
    versions = ['Python 2.7']

    @theller
    Copy link
    Author

    theller commented Jun 21, 2007

    The following code prints out "I L" when run with python 2.5 and current SVN trunk (if you silence the DeprecationWarnings):

    """
    import struct

    for c in "bBhHiIlLqQdf":
        try:
            struct.pack(c, "foo bar")
        except struct.error:
            pass
        else:
            print c
    """

    It works correctly (prints nothing) in Python 2.4.

    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented Mar 31, 2009

    Cannot confirm for trunk.

    @devdanzin devdanzin mannequin added extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error labels Mar 31, 2009
    @vdupras
    Copy link
    Mannequin

    vdupras mannequin commented Apr 1, 2009

    While the behavior cannot be reproduced in the trunk, in can be
    reproduced in the 2.6 release:

    $ python -W ignore
    Python 2.6.1 (r261:67515, Dec  6 2008, 16:42:21) 
    [GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import struct
    >>> struct.pack('L', 'foobar')
    '\x00\x00\x00\x00'
    >>> struct.pack('I', 'foobar')
    '\x00\x00\x00\x00'
    >>> struct.pack('i', 'foobar')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    struct.error: required argument is not an integer
    >>>

    @mdickinson
    Copy link
    Member

    It looks as though this was sort-of fixed sometime between 2.6.1 and
    2.6.2. In 2.6.2, I get the following (and results from trunk and 3.0.1
    are similar):

    Python 2.6.2+ (release26-maint:71755, Apr 19 2009, 22:06:02) 
    [GCC 4.0.1 (Apple Inc. build 5490)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import struct
    >>> struct.pack('L', 'not an integer')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for &: 'str' and 'long'

    That error message suggests that there's something nasty happening
    somewhere, though. It looks as though we're getting the right type of
    exception, but for the wrong reasons.

    @devdanzin
    Copy link
    Mannequin

    devdanzin mannequin commented May 12, 2009

    Mark, Virgil: Thanks for correcting my wrong assessment!

    The lucky TypeError comes from rev 68120.

    It looks like that error message in trunk is due to a "PyNumber_And(v,
    pylong_ulong_mask)" when v isn't a PyNumber. I've added a "get_pylong(v)
    == NULL" check in the attached patch, but my C is weak :)

    @mdickinson
    Copy link
    Member

    Thanks for the patch, Daniel! It certainly fixes the problem. I was
    planning something a little more drastic, though---I think the struct
    module could do with a bit of a cleanup in this area.

    At the moment it's not clear exactly what types should be accepted by
    struct.pack with an integer format. Just ints and longs (and their
    subclases)? Anything implementing an __index__ method? Anything
    implementing an __int__ method (e.g., Decimal instances)?

    I propose doing a little bit of rewriting so that

    (1) all attempted conversions of a PyObject to a C integer
    go through PyNumber_Index; thus anything with an __index__
    method can be packed.

    (2) If PY_STRUCT_FLOAT_COERCE is defined, instances of float or
    subclasses of float (i.e., everything that passes PyFloat_Check)
    are also accepted, for backwards compatibility.

    Does this seem reasonable?

    @mdickinson
    Copy link
    Member

    Here's a patch that does some general cleanup of the object->integer
    helper functions in the struct module; in the process, it fixes this
    bug. With this patch, all conversions from a PyObject to a C integer go
    through get_pylong, so they're all treated the same way. Currently
    (i.e., without the patch) there's a lack of consistency in the way the
    various integer codes are handled---some codes emit a warning for float
    conversions and some ('q', 'Q') don't; some codes will happily convert
    a Decimal instance, and others won't. Some codes produce this strange
    'unsupported operand types' message and some don't, etc.

    @mdickinson
    Copy link
    Member

    Strange TypeError message fixed in r73858: those pack operations now raise
    struct.error, like they do for all other integer codes.

    @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
    extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants