472,353 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

struct.pack bug?

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
Oct 27 '06 #1
2 10136
"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>

Oct 27 '06 #2
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
Oct 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joshua Forgione | last post by:
Hello. I am reverse engineering some Python Code. I am using the book "The Quick Python Book" as an aide. The following line exists in the...
5
by: Panos Laganakos | last post by:
Hello, I have a list that includes lists of integers, in the form of: li = , , ...] packed = struct.pack(str(len(li)*3)+'i', li) The frmt...
1
by: tkpmep | last post by:
I write data to Excel files using PyExcelerator 0.6.3.a and have done so successfully for small files (10-15 cells). I'm experiencing an error when...
5
by: andmarti | last post by:
If I build a strict with: import struct print struck.pack ('i', 1) it returns a '\n'. What's wrong with it??? :( --
7
by: aurora00 | last post by:
I have a program that generates a number of files that will be packaged into a tarball. Can I stream the content into TarFile without first writing...
1
by: praveen0437 | last post by:
hi i know about format argument in pack and unpack like b h i etc but in some cases i am getting < or = symbols along with them what does they...
9
by: zhangbo2050 | last post by:
Basically i'm trying to form a mac packet. 1. a simple mac header is generated using struct.pack('!HH', desti_mac, src_mac) 2. the packet is...
5
by: Steven Clark | last post by:
Can anyone explain to me why struct.pack('HB',1,2) gives 3 bytes, whereas struct.pack('BH',1,2) gives 4 bytes? -Steven
0
by: Cameron Simpson | last post by:
On 25Jun2008 22:38, Steven Clark <steven.p.clark@gmail.comwrote: | On Wed, Jun 25, 2008 at 7:03 PM, John Machin <sjmachin@lexicon.netwrote: | On...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.