Title: Strange DeprecationWarning behaviour in module struct
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.6
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, hagen, isandler, mark.dickinson
Priority: normal Keywords:

Created on 2009-02-10 07:14 by hagen, last changed 2010-04-06 15:23 by mark.dickinson. This issue is now closed.

File name Uploaded Description Edit hagen, 2009-02-10 07:14
Messages (5)
msg81532 - (view) Author: Hagen F├╝rstenau (hagen) Date: 2009-02-10 07:14
struct.pack seems to raise a DeprecationWarning for some structure
formats, but not for others:

Python 2.6.1 (r261:67515, Dec  5 2008, 07:40:41) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.pack(">H", 1.0)
sys:1: DeprecationWarning: integer argument expected, got float
>>> struct.pack("H", 1.0)

In addition the DeprecationWarning message gives a strange location for
the problem. With the attached it locates the problem
at the function call foo() instead of the problematic call of struct.pack:

$ python2.6 DeprecationWarning: integer argument expected, got
msg83159 - (view) Author: Ilya Sandler (isandler) Date: 2009-03-04 23:01
Here is another case, which I think is even worse.

Range checks are done inconsistently as well:

.../trunk> ./python -c 'import struct; struct.pack("B", 257)

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

.../trunk> ./python -c 'import struct; struct.pack(">B", 257)' 
sys:1: DeprecationWarning: 'B' format requires 0 <= number <= 255
msg83206 - (view) Author: Ilya Sandler (isandler) Date: 2009-03-05 17:29
It appears that the different behavior results from trying to preserve
backward compatibility with earlier version of Python

msg102367 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-04-05 10:35
Both examples now give consistent behavior independent of byteorder in trunk: packing floats with "H" works, packing bytes out of range with "B" raises.

Closing as "out of date".
msg102469 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2010-04-06 15:23
The wrong location for the DeprecationWarnings was also fixed in trunk in r78690.  I've backported the fix to release26-maint in r79834.

I don't dare mess further with the warnings in a bugfix release;  as Georg points out, the issues are fixed in trunk.  BTW, one of the 2.6 issues (again fixed in trunk) that wasn't mentioned above is that some packs produce *two* overflow-related warnings:

>>> import struct
>>> struct.pack('<L', -1)
__main__:1: DeprecationWarning: struct integer overflow masking is deprecated
__main__:1: DeprecationWarning: 'L' format requires 0 <= number <= 4294967295
Date User Action Args
2010-04-06 15:23:14mark.dickinsonsetnosy: + mark.dickinson
messages: + msg102469
2010-04-05 10:35:58georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg102367

resolution: out of date
2009-03-05 17:29:11isandlersetmessages: + msg83206
2009-03-04 23:01:55isandlersetnosy: + isandler
messages: + msg83159
2009-02-10 07:14:28hagencreate