From 22999a69e1e810757823abef87b744ad3686f908 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 25 Apr 2009 13:16:50 +0000 Subject: [PATCH] Issue #5829: complex('1e-500') shouldn't raise an exception. Also fix some confusing indentation. --- Lib/test/test_complex.py | 3 +++ Misc/NEWS | 2 ++ Objects/complexobject.c | 18 +++++++++--------- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 9a3310198cb..000dd9d1410 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -220,6 +220,9 @@ class ComplexTest(unittest.TestCase): self.assertAlmostEqual(complex("+1"), +1) self.assertAlmostEqual(complex("(1+2j)"), 1+2j) self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j) + self.assertAlmostEqual(complex("1E-500"), 1e-500+0j) + self.assertAlmostEqual(complex("1e-500J"), 1e-500j) + self.assertAlmostEqual(complex("+1e-315-1e-400j"), 1e-315-1e-400j) class complex2(complex): pass self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j) diff --git a/Misc/NEWS b/Misc/NEWS index b9afc3fc2d6..f3be9370e13 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,8 @@ What's New in Python 2.6.3 Core and Builtins ----------------- +- Issue #5829: complex('1e-500') no longer raises an exception + - Issue #5787: object.__getattribute__(some_type, "__bases__") segfaulted on some builtin types. diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 9943d0d5004..8ce61b747ea 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -995,16 +995,16 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v) } errno = 0; PyFPE_START_PROTECT("strtod", return 0) - z = PyOS_ascii_strtod(s, &end) ; + z = PyOS_ascii_strtod(s, &end) ; PyFPE_END_PROTECT(z) - if (errno != 0) { - PyOS_snprintf(buffer, sizeof(buffer), - "float() out of range: %.150s", s); - PyErr_SetString( - PyExc_ValueError, - buffer); - return NULL; - } + if (errno == ERANGE && fabs(z) >= 1.0) { + PyOS_snprintf(buffer, sizeof(buffer), + "float() out of range: %.150s", s); + PyErr_SetString( + PyExc_ValueError, + buffer); + return NULL; + } s=end; if (*s=='J' || *s=='j') {