struct.pack bug?

Discussion in 'Python' started by Jonathan Fine, Feb 8, 2007.

  1. Hello

    I find the following inconsistent:
    ===
    >>> sys.version

    '2.4.1a0 (#2, Feb 9 2005, 12:50:04) \n[GCC 3.3.5 (Debian 1:3.3.5-8)]'
    >>> pack('>B', 256)

    '\x00'
    >>> pack('<B', 256)

    '\x00'
    >>> pack('B', 256)

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

    ===

    I was hoping that '>B' and '<B' would raise an exception,
    ust as 'B' does.


    On Oct 27 2006, 11:17 am, Jansson Christer reported a
    different anomoly to this newsgroup, using the same
    subject.

    --
    Jonathan
     
    Jonathan Fine, Feb 8, 2007
    #1
    1. Advertising

  2. Jonathan Fine

    John Machin Guest

    On Feb 9, 8:58 am, Jonathan Fine <> wrote:
    > Hello
    >
    > I find the following inconsistent:
    > ===
    > >>> sys.version

    > '2.4.1a0 (#2, Feb 9 2005, 12:50:04) \n[GCC 3.3.5 (Debian 1:3.3.5-8)]'
    > >>> pack('>B', 256)

    > '\x00'
    > >>> pack('<B', 256)

    > '\x00'
    > >>> pack('B', 256)

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

    > ===
    >
    > I was hoping that '>B' and '<B' would raise an exception,
    > ust as 'B' does.
    >
    > On Oct 27 2006, 11:17 am, Jansson Christer reported a
    > different anomoly to this newsgroup, using the same
    > subject.


    Your Python is out-of-date in two dimensions: the 2.4 series is way
    past 2.4.1, and Python 2.5 has been out for a while.

    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    (Intel)] on win
    32
    | >>> from struct import pack
    | >>> pack('<B', 256)
    | __main__:1: DeprecationWarning: 'B' format requires 0 <= number <=
    255
    | '\x00'

    Until the deprecation warning becomes an exception in 2.6, I'd suggest
    doing your own checking:
    assert 0 <= pack_B_candidate <= 255

    HTH,
    John
     
    John Machin, Feb 9, 2007
    #2
    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,442
    Chris Fogelklou
    Apr 20, 2004
  2. Tim Jones
    Replies:
    0
    Views:
    405
    Tim Jones
    Jan 31, 2004
  3. Alex Stapleton

    Bug in struct.pack?

    Alex Stapleton, Jan 11, 2006, in forum: Python
    Replies:
    1
    Views:
    334
    Raymond Hettinger
    Jan 11, 2006
  4. Jansson Christer

    struct.pack bug?

    Jansson Christer, Oct 27, 2006, in forum: Python
    Replies:
    2
    Views:
    1,002
    Jansson Christer
    Oct 27, 2006
  5. Alexander Farber

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

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

Share This Page