[Python-Dev] struct module and coercing floats to integers

Bob Ippolito bob at redivi.com
Thu Aug 3 05:20:40 CEST 2006


On Jul 28, 2006, at 1:35 PM, Bob Ippolito wrote:

> It seems that the pre-2.5 struct module has some additional
> undocumented behavior[1] that didn't percolate into the new version:
> http://python.org/sf/1530559
>
> Python 2.4 and previous will coerce floats to integers when necessary
> as such without any kind of complaint:
>
> $ python2.4 -c "import struct; print repr(struct.pack('>H',
> 0.9999999999999999))"
> '\x00\x00'
>
> Python 2.5 refuses to coerce float to int:
>
> $ python2.5 -c "import struct; print repr(struct.pack('>H',
> 0.9999999999999999))"
> Traceback (most recent call last):
>    File "<string>", line 1, in <module>
>    File "/Users/bob/src/python/Lib/struct.py", line 63, in pack
>      return o.pack(*args)
> TypeError: unsupported operand type(s) for &: 'float' and 'long'
>
> The available options are to:
>
> 1. Reinstate the pre-2.5 weirdness
> 2. Reinstate the pre-2.5 weirdness with a DeprecationWarning
> 3. Break existing code that relies on undocumented behavior (seems
> more like a bug than lack of specification)

There's a patch in the tracker for 2. It should get applied when the  
trunk freeze is over.

-bob



More information about the Python-Dev mailing list