Skip to content

Commit c59866a

Browse files
author
mark.dickinson
committed
Issue 705836: Fix struct.pack(">f", 1e40) to behave consistently
across platforms: it should now raise OverflowError on all platforms. (Previously it raised OverflowError only on non IEEE 754 platforms.) Also fix the (already existing) test for this behaviour so that it actually raises TestFailed instead of just referencing it. git-svn-id: http://svn.python.org/projects/python/trunk@61383 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6eedb91 commit c59866a

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

Lib/test/test_struct.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ def test_705836():
482482
except OverflowError:
483483
pass
484484
else:
485-
TestFailed("expected OverflowError")
485+
raise TestFailed("expected OverflowError")
486486

487487
test_705836()
488488

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Core and builtins
2121
Library
2222
-------
2323

24+
- Issue #705836: struct.pack(">f", x) now raises OverflowError on all
25+
platforms when x is too large to fit into an IEEE 754 float; previously
26+
it only raised OverflowError on non IEEE 754 platforms.
27+
2428
- Issue #1106316: pdb.post_mortem()'s parameter, "traceback", is now
2529
optional: it defaults to the traceback of the exception that is currently
2630
being handled (is mandatory to be in the middle of an exception, otherwise

Objects/floatobject.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,9 +1751,6 @@ PyFloat_Fini(void)
17511751

17521752
/*----------------------------------------------------------------------------
17531753
* _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
1754-
*
1755-
* TODO: On platforms that use the standard IEEE-754 single and double
1756-
* formats natively, these routines could simply copy the bytes.
17571754
*/
17581755
int
17591756
_PyFloat_Pack4(double x, unsigned char *p, int le)
@@ -1833,28 +1830,31 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
18331830
/* Done */
18341831
return 0;
18351832

1836-
Overflow:
1837-
PyErr_SetString(PyExc_OverflowError,
1838-
"float too large to pack with f format");
1839-
return -1;
18401833
}
18411834
else {
18421835
float y = (float)x;
18431836
const char *s = (char*)&y;
18441837
int i, incr = 1;
18451838

1839+
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
1840+
goto Overflow;
1841+
18461842
if ((float_format == ieee_little_endian_format && !le)
18471843
|| (float_format == ieee_big_endian_format && le)) {
18481844
p += 3;
18491845
incr = -1;
18501846
}
1851-
1847+
18521848
for (i = 0; i < 4; i++) {
18531849
*p = *s++;
18541850
p += incr;
18551851
}
18561852
return 0;
18571853
}
1854+
Overflow:
1855+
PyErr_SetString(PyExc_OverflowError,
1856+
"float too large to pack with f format");
1857+
return -1;
18581858
}
18591859

18601860
int

0 commit comments

Comments
 (0)