struct.pack bug?

Discussion in 'Python' started by Jansson Christer, Oct 27, 2006.

  1. Hi all,

    I have discovered that in my Python 2.4.1 installation (on Solaris 8),
    struct.pack handles things in a way that seems inconsistent to me.

    I haven't found any comprehensible documentation over known issues with
    Python 2.4.1 so I try this...

    Here's the thing:

    >>> from struct import pack
    >>> pack('B', -1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    struct.error: ubyte format requires 0<=number<=255
    >>> pack('H', -1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    struct.error: short format requires 0<=number<=USHRT_MAX
    >>> pack('L', -1)

    '\xff\xff\xff\xff'
    >>>


    Shouldn't pack('L', -1) raise an exception like the others, rather than
    behaving like pack('l', -1)?
    Is this fixed in later versions?

    (I don't have access to later versions and have failed to install any.
    Python 2.5 compiles nicely but "make install" fails without leaving any
    clues about how it failed and no installation troubleshooting guides to
    be found at python.org. Python 2.4.4 doesn't even compile since it
    apparently requires a newer libstdc++ than the one on our system... see
    why I'm asking the newsgroup?)

    --
    --

    Christer Jansson
    WiseOne AB
    +46 708 21 42 84
     
    Jansson Christer, Oct 27, 2006
    #1
    1. Advertising

  2. "Jansson Christer" wrote:

    > I have discovered that in my Python 2.4.1 installation (on Solaris 8),
    > struct.pack handles things in a way that seems inconsistent to me.
    >
    > I haven't found any comprehensible documentation over known issues with
    > Python 2.4.1 so I try this...
    >
    > Here's the thing:
    >
    > >>> from struct import pack
    > >>> pack('B', -1)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > struct.error: ubyte format requires 0<=number<=255
    > >>> pack('H', -1)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > struct.error: short format requires 0<=number<=USHRT_MAX
    > >>> pack('L', -1)

    > '\xff\xff\xff\xff'
    > >>>

    >
    > Shouldn't pack('L', -1) raise an exception like the others, rather than
    > behaving like pack('l', -1)?


    probably; the reason for the old behaviour is probably to simplify for code that
    uses Python int's to represent 32-bit values. (mixing longs and ints used to be a
    lot more difficult than it is today).

    > Is this fixed in later versions?


    the "struct" module was rewritten in Python 2.5; the new module issues a
    warning, and then appears to *clamp* the value to the allowed range, in-
    stead of grabbing the low bits:

    >>> import struct


    >>> struct.pack("B", -1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python25\Lib\struct.py", line 63, in pack
    return o.pack(*args)
    struct.error: ubyte format requires 0 <= number <= 255

    >>> struct.pack("H", -1)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Python25\Lib\struct.py", line 63, in pack
    return o.pack(*args)
    struct.error: short format requires 0 <= number <= USHRT_MAX

    >>> struct.pack("I", -1)

    __main__:1: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295
    '\x00\x00\x00\x00'

    >>> struct.pack("L", -1)

    __main__:1: DeprecationWarning: 'L' format requires 0 <= number <= 4294967295
    '\x00\x00\x00\x00'

    </F>
     
    Fredrik Lundh, Oct 27, 2006
    #2
    1. Advertising

  3. Fredrik Lundh wrote:
    > "Jansson Christer" wrote:
    >
    >
    >>I have discovered that in my Python 2.4.1 installation (on Solaris 8),
    >>struct.pack handles things in a way that seems inconsistent to me.
    >>
    >>I haven't found any comprehensible documentation over known issues with
    >>Python 2.4.1 so I try this...
    >>
    >>Here's the thing:
    >>
    >>
    >>>>>from struct import pack
    >>>>>pack('B', -1)

    >>
    >>Traceback (most recent call last):
    >> File "<stdin>", line 1, in ?
    >>struct.error: ubyte format requires 0<=number<=255
    >>
    >>>>>pack('H', -1)

    >>
    >>Traceback (most recent call last):
    >> File "<stdin>", line 1, in ?
    >>struct.error: short format requires 0<=number<=USHRT_MAX
    >>
    >>>>>pack('L', -1)

    >>
    >>'\xff\xff\xff\xff'
    >>
    >>Shouldn't pack('L', -1) raise an exception like the others, rather than
    >>behaving like pack('l', -1)?

    >
    >
    > probably; the reason for the old behaviour is probably to simplify for code that
    > uses Python int's to represent 32-bit values. (mixing longs and ints used to be a
    > lot more difficult than it is today).
    >
    >
    >>Is this fixed in later versions?

    >
    >
    > the "struct" module was rewritten in Python 2.5; the new module issues a
    > warning, and then appears to *clamp* the value to the allowed range, in-
    > stead of grabbing the low bits:
    >
    >
    >>>>import struct

    >
    >
    >>>>struct.pack("B", -1)

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\Python25\Lib\struct.py", line 63, in pack
    > return o.pack(*args)
    > struct.error: ubyte format requires 0 <= number <= 255
    >
    >
    >>>>struct.pack("H", -1)

    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "C:\Python25\Lib\struct.py", line 63, in pack
    > return o.pack(*args)
    > struct.error: short format requires 0 <= number <= USHRT_MAX
    >
    >
    >>>>struct.pack("I", -1)

    >
    > __main__:1: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295
    > '\x00\x00\x00\x00'
    >
    >
    >>>>struct.pack("L", -1)

    >
    > __main__:1: DeprecationWarning: 'L' format requires 0 <= number <= 4294967295
    > '\x00\x00\x00\x00'
    >
    > </F>
    >
    >
    >


    Ok this implies that somebody is actually thinking about how to handle
    this, so I guess I won't file a bug report and simply solve my problems
    without relying on those exceptions...

    Thank you!
    --
    --

    Christer Jansson
    WiseOne AB
    +46 708 21 42 84
     
    Jansson Christer, Oct 27, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,441
    Chris Fogelklou
    Apr 20, 2004
  2. Tim Jones
    Replies:
    0
    Views:
    404
    Tim Jones
    Jan 31, 2004
  3. Alex Stapleton

    Bug in struct.pack?

    Alex Stapleton, Jan 11, 2006, in forum: Python
    Replies:
    1
    Views:
    333
    Raymond Hettinger
    Jan 11, 2006
  4. Jonathan Fine

    struct.pack bug?

    Jonathan Fine, Feb 8, 2007, in forum: Python
    Replies:
    1
    Views:
    375
    John Machin
    Feb 9, 2007
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    167
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page