summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2017-02-20 21:59:30 (GMT)
committerGitHub <noreply@github.com>2017-02-20 21:59:30 (GMT)
commitd9b3cdd137239a5913de2252c3ce269e35ac63d2 (patch)
tree1b078e6ce4266f80100bbe550b8833e25d1dc880
parentce552e2d5c4ff90218fb41847e8ffb1fd3ba3b2d (diff)
downloadcpython-d9b3cdd137239a5913de2252c3ce269e35ac63d2.zip
cpython-d9b3cdd137239a5913de2252c3ce269e35ac63d2.tar.gz
cpython-d9b3cdd137239a5913de2252c3ce269e35ac63d2.tar.bz2
bpo-29602: fix signed zero handling in complex constructor. (#203) (#206)
* Fix incorrect handling of signed zeros for complex-related classes. * Add Misc/NEWS entry. (cherry picked from commit 112ec38c15b388fe025ccb85369a584d218b1160)
-rw-r--r--Lib/test/test_complex.py23
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/complexobject.c6
3 files changed, 30 insertions, 3 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index c249ca7..cee4934 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -387,6 +387,29 @@ class ComplexTest(unittest.TestCase):
self.assertAlmostEqual(complex(complex1(1j)), 2j)
self.assertRaises(TypeError, complex, complex2(1j))
+ @support.requires_IEEE_754
+ def test_constructor_special_numbers(self):
+ class complex2(complex):
+ pass
+ for x in 0.0, -0.0, INF, -INF, NAN:
+ for y in 0.0, -0.0, INF, -INF, NAN:
+ with self.subTest(x=x, y=y):
+ z = complex(x, y)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex2(x, y)
+ self.assertIs(type(z), complex2)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex(complex2(x, y))
+ self.assertIs(type(z), complex)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+ z = complex2(complex(x, y))
+ self.assertIs(type(z), complex2)
+ self.assertFloatsAreIdentical(z.real, x)
+ self.assertFloatsAreIdentical(z.imag, y)
+
def test_underscores(self):
# check underscores
for lit in VALID_UNDERSCORE_LITERALS:
diff --git a/Misc/NEWS b/Misc/NEWS
index 0fed99b..d5bbd83 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 3.6.1 release candidate 1?
Core and Builtins
-----------------
+- bpo-29602: Fix incorrect handling of signed zeros in complex constructor for
+ complex subclasses and for inputs having a __complex__ method. Patch
+ by Serhiy Storchaka.
+
- bpo-29347: Fixed possibly dereferencing undefined pointers
when creating weakref objects.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 31e1278..cfaba68 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -1025,11 +1025,11 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
cr.real = PyFloat_AsDouble(tmp);
- cr.imag = 0.0; /* Shut up compiler warning */
+ cr.imag = 0.0;
Py_DECREF(tmp);
}
if (i == NULL) {
- ci.real = 0.0;
+ ci.real = cr.imag;
}
else if (PyComplex_Check(i)) {
ci = ((PyComplexObject*)i)->cval;
@@ -1051,7 +1051,7 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (ci_is_complex) {
cr.real -= ci.imag;
}
- if (cr_is_complex) {
+ if (cr_is_complex && i != NULL) {
ci.real += cr.imag;
}
return complex_subtype_from_doubles(type, cr.real, ci.real);