Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > struct.pack bug?

Reply
Thread Tools

struct.pack bug?

 
 
Jonathan Fine
Guest
Posts: n/a
 
      02-08-2007
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-]'
>>> 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

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      02-09-2007
On Feb 9, 8:58 am, Jonathan Fine <j...@pytex.org> 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-]'
> >>> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57