Re: [Python-Dev] struct module and coercing floats to integers
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.)) '\x00\x00' Python 2.5 refuses to coerce float to int: $ python2.5 -c import struct; print repr(struct.pack('H', 0.)) 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 ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
[Python-Dev] struct module and coercing floats to integers
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.)) '\x00\x00' Python 2.5 refuses to coerce float to int: $ python2.5 -c import struct; print repr(struct.pack('H', 0.)) 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) Either 2 or 3 seems reasonable to me, with a preference for 3 because none of my code depends on old bugs in the struct module :) As far as precedent goes, the array module *used* to coerce floats silently, but it's had a DeprecationWarning since at least Python 2.3 (but perhaps even earlier). Maybe it's time to promote that warning to an exception for Python 2.5? [1] The pre-2.5 behavior should really be considered a bug, the documentation says Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly. I wouldn't consider arbitrary floating point numbers to match the value required by an integer format exactly. Floats are not in general interchangeable with integers in Python anyway (e.g. list indexes, etc.). -bob ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] struct module and coercing floats to integers
On 7/28/06, Bob Ippolito [EMAIL PROTECTED] wrote: http://python.org/sf/1530559 [1] The pre-2.5 behavior should really be considered a bug, the documentation says Return a string containing the values v1, v2, ... packed according to the given format. The arguments must match the values required by the format exactly. I wouldn't consider arbitrary floating point numbers to match the value required by an integer format exactly. Floats are not in general interchangeable with integers in Python anyway (e.g. list indexes, etc.). While it may be a bug, it's not as hard to run into, nor as illogical as the presentation here makes it sound. The original code[1] took a float value between 0 and 2, and wanted to use pack('H', round(value * 32768)) The workaround is a trivial change pack('H', int(round(value * 32768))) but the timeframe is less than ideal, as working code will suddenly stop and recieve only mildly helpful error message. The fact that round returns a float rather than an int, while intentional, does not feature prominently in one's mine when the first version yielded the expected results. I would appreciate option 2 which retains compatibility but warns that the construct is bad. I will accept any of the options, as it's clear that floats don't make sense. It's just unfortunate that the previous implementation let them through in a way the new implementation does not. [1] http://www.sacredchao.net/quodlibet/changeset/3706 Michael -- Michael Urman http://www.tortall.net/mu/blog ___ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com