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 '\x00\x01' >>> struct.pack("H", 1.0) '\x01\x00' In addition the DeprecationWarning message gives a strange location for the problem. With the attached struct_warning.py it locates the problem at the function call foo() instead of the problematic call of struct.pack: $ python2.6 struct_warning.py struct_warning.py:6: DeprecationWarning: integer argument expected, got float foo()
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 "", line 1, in 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
It appears that the different behavior results from trying to preserve backward compatibility with earlier version of Python see: http://bugs.python.org/issue1229380
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".
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 '\xff\xff\xff\xff'