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.
This commit is contained in:
Mark Dickinson 2008-03-14 14:23:37 +00:00
parent 6119540d70
commit c23b8a7af9
3 changed files with 13 additions and 9 deletions

View File

@ -482,7 +482,7 @@ def test_705836():
except OverflowError: except OverflowError:
pass pass
else: else:
TestFailed("expected OverflowError") raise TestFailed("expected OverflowError")
test_705836() test_705836()

View File

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

View File

@ -1751,9 +1751,6 @@ PyFloat_Fini(void)
/*---------------------------------------------------------------------------- /*----------------------------------------------------------------------------
* _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h. * _PyFloat_{Pack,Unpack}{4,8}. See floatobject.h.
*
* TODO: On platforms that use the standard IEEE-754 single and double
* formats natively, these routines could simply copy the bytes.
*/ */
int int
_PyFloat_Pack4(double x, unsigned char *p, int le) _PyFloat_Pack4(double x, unsigned char *p, int le)
@ -1833,16 +1830,15 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
/* Done */ /* Done */
return 0; return 0;
Overflow:
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with f format");
return -1;
} }
else { else {
float y = (float)x; float y = (float)x;
const char *s = (char*)&y; const char *s = (char*)&y;
int i, incr = 1; int i, incr = 1;
if (Py_IS_INFINITY(y) && !Py_IS_INFINITY(x))
goto Overflow;
if ((float_format == ieee_little_endian_format && !le) if ((float_format == ieee_little_endian_format && !le)
|| (float_format == ieee_big_endian_format && le)) { || (float_format == ieee_big_endian_format && le)) {
p += 3; p += 3;
@ -1855,6 +1851,10 @@ _PyFloat_Pack4(double x, unsigned char *p, int le)
} }
return 0; return 0;
} }
Overflow:
PyErr_SetString(PyExc_OverflowError,
"float too large to pack with f format");
return -1;
} }
int int