Skip to content

Commit 0df5a85

Browse files
committed
don't require the presence of __getformat__ or __setformat__; use requires_IEEE_754 globally
1 parent f5e81d6 commit 0df5a85

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

Lib/test/test_float.py

+49-42
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
INF = float("inf")
1313
NAN = float("nan")
1414

15+
have_getformat = hasattr(float, "__getformat__")
16+
requires_getformat = unittest.skipUnless(have_getformat,
17+
"requires __getformat__")
18+
requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
19+
"requires __setformat__")
1520
# decorator for skipping tests on non-IEEE 754 platforms
16-
requires_IEEE_754 = unittest.skipUnless(
21+
requires_IEEE_754 = unittest.skipUnless(have_getformat and
1722
float.__getformat__("double").startswith("IEEE"),
1823
"test requires IEEE 754 doubles")
1924

@@ -357,6 +362,7 @@ def test_float_pow(self):
357362
#self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)
358363

359364

365+
@requires_setformat
360366
class FormatFunctionsTestCase(unittest.TestCase):
361367

362368
def setUp(self):
@@ -407,6 +413,7 @@ def test_setformat(self):
407413
# on non-IEEE platforms, attempting to unpack a bit pattern
408414
# representing an infinity or a NaN should raise an exception.
409415

416+
@requires_setformat
410417
class UnknownFormatTestCase(unittest.TestCase):
411418
def setUp(self):
412419
self.save_formats = {'double':float.__getformat__('double'),
@@ -439,41 +446,42 @@ def test_float_specials_dont_unpack(self):
439446
# let's also try to guarantee that -0.0 and 0.0 don't get confused.
440447

441448
class IEEEFormatTestCase(unittest.TestCase):
442-
if float.__getformat__("double").startswith("IEEE"):
443-
def test_double_specials_do_unpack(self):
444-
for fmt, data in [('>d', BE_DOUBLE_INF),
445-
('>d', BE_DOUBLE_NAN),
446-
('<d', LE_DOUBLE_INF),
447-
('<d', LE_DOUBLE_NAN)]:
448-
struct.unpack(fmt, data)
449-
450-
if float.__getformat__("float").startswith("IEEE"):
451-
def test_float_specials_do_unpack(self):
452-
for fmt, data in [('>f', BE_FLOAT_INF),
453-
('>f', BE_FLOAT_NAN),
454-
('<f', LE_FLOAT_INF),
455-
('<f', LE_FLOAT_NAN)]:
456-
struct.unpack(fmt, data)
457-
458-
if float.__getformat__("double").startswith("IEEE"):
459-
def test_negative_zero(self):
460-
def pos_pos():
461-
return 0.0, math.atan2(0.0, -1)
462-
def pos_neg():
463-
return 0.0, math.atan2(-0.0, -1)
464-
def neg_pos():
465-
return -0.0, math.atan2(0.0, -1)
466-
def neg_neg():
467-
return -0.0, math.atan2(-0.0, -1)
468-
self.assertEquals(pos_pos(), neg_pos())
469-
self.assertEquals(pos_neg(), neg_neg())
470-
471-
if float.__getformat__("double").startswith("IEEE"):
472-
def test_underflow_sign(self):
473-
# check that -1e-1000 gives -0.0, not 0.0
474-
self.assertEquals(math.atan2(-1e-1000, -1), math.atan2(-0.0, -1))
475-
self.assertEquals(math.atan2(float('-1e-1000'), -1),
476-
math.atan2(-0.0, -1))
449+
450+
@requires_IEEE_754
451+
def test_double_specials_do_unpack(self):
452+
for fmt, data in [('>d', BE_DOUBLE_INF),
453+
('>d', BE_DOUBLE_NAN),
454+
('<d', LE_DOUBLE_INF),
455+
('<d', LE_DOUBLE_NAN)]:
456+
struct.unpack(fmt, data)
457+
458+
@requires_IEEE_754
459+
def test_float_specials_do_unpack(self):
460+
for fmt, data in [('>f', BE_FLOAT_INF),
461+
('>f', BE_FLOAT_NAN),
462+
('<f', LE_FLOAT_INF),
463+
('<f', LE_FLOAT_NAN)]:
464+
struct.unpack(fmt, data)
465+
466+
@requires_IEEE_754
467+
def test_negative_zero(self):
468+
def pos_pos():
469+
return 0.0, math.atan2(0.0, -1)
470+
def pos_neg():
471+
return 0.0, math.atan2(-0.0, -1)
472+
def neg_pos():
473+
return -0.0, math.atan2(0.0, -1)
474+
def neg_neg():
475+
return -0.0, math.atan2(-0.0, -1)
476+
self.assertEquals(pos_pos(), neg_pos())
477+
self.assertEquals(pos_neg(), neg_neg())
478+
479+
@requires_IEEE_754
480+
def test_underflow_sign(self):
481+
# check that -1e-1000 gives -0.0, not 0.0
482+
self.assertEquals(math.atan2(-1e-1000, -1), math.atan2(-0.0, -1))
483+
self.assertEquals(math.atan2(float('-1e-1000'), -1),
484+
math.atan2(-0.0, -1))
477485

478486
def test_format(self):
479487
# these should be rewritten to use both format(x, spec) and
@@ -530,8 +538,7 @@ def test_format(self):
530538
self.assertEqual('{0:f}'.format(NAN), 'nan')
531539
self.assertEqual('{0:F}'.format(NAN), 'NAN')
532540

533-
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
534-
"test requires IEEE 754 doubles")
541+
@requires_IEEE_754
535542
def test_format_testfile(self):
536543
for line in open(format_testfile):
537544
if line.startswith('--'):
@@ -613,8 +620,8 @@ def test_short_repr(self):
613620
self.assertEqual(s, repr(float(s)))
614621
self.assertEqual(negs, repr(float(negs)))
615622

616-
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
617-
"test requires IEEE 754 doubles")
623+
624+
@requires_IEEE_754
618625
class RoundTestCase(unittest.TestCase):
619626
def test_second_argument_type(self):
620627
# any type with an __index__ method should be permitted as
@@ -752,8 +759,8 @@ def test_halfway_cases(self):
752759
self.assertAlmostEqual(round(0.5e22, -22), 1e22)
753760
self.assertAlmostEqual(round(1.5e22, -22), 2e22)
754761

755-
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
756-
"test requires IEEE 754 doubles")
762+
763+
@requires_IEEE_754
757764
def test_format_specials(self):
758765
# Test formatting of nans and infs.
759766

0 commit comments

Comments
 (0)