Skip to content

Commit 386cd1e

Browse files
committed
long(float('nan')) raises an OverflowError as discussed on the mailing list a week ago
1 parent 62fe8a8 commit 386cd1e

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

Lib/test/test_long.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ def test_format(self):
539539

540540
def test_nan_inf(self):
541541
self.assertRaises(OverflowError, int, float('inf'))
542-
self.assertEqual(int(float('nan')), 0)
542+
self.assertRaises(OverflowError, int, float('nan'))
543543

544544
def test_main():
545545
test_support.run_unittest(LongTest)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.0a3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Object/longobject.c: long(float('nan')) raises an OverflowError instead
16+
of returning 0.
17+
1518
- Issue #1762972: __file__ points to the source file instead of the pyc/pyo
1619
file if the py file exists.
1720

Objects/longobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ PyLong_FromDouble(double dval)
255255
return NULL;
256256
}
257257
if (Py_IS_NAN(dval)) {
258-
return PyLong_FromLong(0L);
258+
PyErr_SetString(PyExc_OverflowError,
259+
"cannot convert float NaN to int");
260+
return NULL;
259261
}
260262
if (dval < 0.0) {
261263
neg = 1;

0 commit comments

Comments
 (0)