Skip to content

Commit

Permalink
bpo-29602: fix signed zero handling in complex constructor (#204)
Browse files Browse the repository at this point in the history
* bpo-29602: fix signed zero handling in complex constructor

* Add missing have_getformat definition; remove use of unittest subtests.
  • Loading branch information
mdickinson committed Feb 20, 2017
1 parent 7131a73 commit c0b336e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
NAN = float("nan")
# These tests ensure that complex math does the right thing

# decorator for skipping tests on non-IEEE 754 platforms
have_getformat = hasattr(float, "__getformat__")
requires_IEEE_754 = unittest.skipUnless(have_getformat and
float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")


class ComplexTest(unittest.TestCase):

def assertAlmostEqual(self, a, b):
Expand Down Expand Up @@ -441,6 +448,28 @@ def __rmod__(self,other):
b = 'y %s x' % op
self.assertTrue(type(eval(a)) is type(eval(b)) is xcomplex)

@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:
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_hash(self):
for x in xrange(-30, 30):
self.assertEqual(hash(x), hash(complex(x, 0)))
Expand Down
4 changes: 4 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ What's New in Python 2.7.14?
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.

Expand Down
6 changes: 3 additions & 3 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1232,11 +1232,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;
Expand All @@ -1258,7 +1258,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);
Expand Down

0 comments on commit c0b336e

Please sign in to comment.