473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 10524
"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 code: msg = struct.pack('BBBB',word0, word1, word2,...
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 part is right, as I'm multiplying by 3, 'cause each...
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 writing a big chunk of data (10,000 cells) to...
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 them out to the file system? All add(), addfile()...
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 signifies for example struct.pack( "<cI", 'n',...
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 created using ''.join(header, .....) Could anyone...
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 Jun 26, 9:00 am, "Steven Clark"...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.