summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2008-08-04 21:30:09 +0000
committerMark Dickinson <dickinsm@gmail.com>2008-08-04 21:30:09 +0000
commitb646757e01d51c242eef2f9802f1ca6836a5804a (patch)
treef0976c5dcec20fa8bf42db406006df4058e394a1
parentff6868cf10409441666d462af7df6a04faa45d1c (diff)
downloadcpython-git-b646757e01d51c242eef2f9802f1ca6836a5804a.tar.gz
Issue #1481296: (again!) Make conversion of a float NaN to an int or
long raise ValueError instead of returning 0. Also, change the error message for conversion of an infinity to an integer, replacing 'long' by 'integer', so that it's appropriate for both long(float('inf')) and int(float('inf')).
-rw-r--r--Lib/test/test_long.py3
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/longobject.c6
3 files changed, 9 insertions, 3 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index b67c764ad5..e53fd058cf 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -745,7 +745,8 @@ class LongTest(unittest.TestCase):
def test_nan_inf(self):
self.assertRaises(OverflowError, long, float('inf'))
- self.assertEqual(long(float('nan')), 0L)
+ self.assertRaises(OverflowError, long, float('-inf'))
+ self.assertRaises(ValueError, long, float('nan'))
def test_main():
test_support.run_unittest(LongTest)
diff --git a/Misc/NEWS b/Misc/NEWS
index 950d9cb7df..e02aca4c11 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 beta 3?
Core and Builtins
-----------------
+- Issue #1481296: Make long(float('nan')) and int(float('nan')) raise
+ ValueError consistently across platforms.
+
- Issue #3479: On platforms where sizeof(int) is smaller than sizeof(long)
(64bit Unix, for example), unichr() would truncate its argument and return
u'\x00' for unichr(2**32). Now it properly raises an OverflowError.
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 228376a9df..65130696d4 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -176,11 +176,13 @@ PyLong_FromDouble(double dval)
neg = 0;
if (Py_IS_INFINITY(dval)) {
PyErr_SetString(PyExc_OverflowError,
- "cannot convert float infinity to long");
+ "cannot convert float infinity to integer");
return NULL;
}
if (Py_IS_NAN(dval)) {
- return PyLong_FromLong(0L);
+ PyErr_SetString(PyExc_ValueError,
+ "cannot convert float NaN to integer");
+ return NULL;
}
if (dval < 0.0) {
neg = 1;