mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Continue rolling back pep-3141 changes that changed behavior from 2.5. This
round included: * Revert round to its 2.6 behavior (half away from 0). * Because round, floor, and ceil always return float again, it's no longer necessary to have them delegate to __xxx___, so I've ripped that out of their implementations and the Real ABC. This also helps in implementing types that work in both 2.6 and 3.0: you return int from the __xxx__ methods, and let it get enabled by the version upgrade. * Make pow(-1, .5) raise a ValueError again.
This commit is contained in:
parent
f7476c4d46
commit
9871d8fe22
13 changed files with 75 additions and 252 deletions
|
@ -189,25 +189,6 @@ class Real(Complex):
|
|||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def __floor__(self):
|
||||
"""Finds the greatest Integral <= self."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def __ceil__(self):
|
||||
"""Finds the least Integral >= self."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def __round__(self, ndigits=None):
|
||||
"""Rounds self to ndigits decimal places, defaulting to 0.
|
||||
|
||||
If ndigits is omitted or None, returns an Integral, otherwise
|
||||
returns a Real. Rounds half toward even.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def __divmod__(self, other):
|
||||
"""divmod(self, other): The pair (self // other, self % other).
|
||||
|
||||
|
|
|
@ -1456,13 +1456,12 @@ class BuiltinTest(unittest.TestCase):
|
|||
else:
|
||||
self.assertAlmostEqual(pow(x, y, z), 24.0)
|
||||
|
||||
self.assertAlmostEqual(pow(-1, 0.5), 1j)
|
||||
self.assertAlmostEqual(pow(-1, 1./3), 0.5 + 0.8660254037844386j)
|
||||
|
||||
self.assertRaises(TypeError, pow, -1, -2, 3)
|
||||
self.assertRaises(ValueError, pow, 1, 2, 0)
|
||||
self.assertRaises(TypeError, pow, -1L, -2L, 3L)
|
||||
self.assertRaises(ValueError, pow, 1L, 2L, 0L)
|
||||
# Will return complex in 3.0:
|
||||
self.assertRaises(ValueError, pow, -342.43, 0.234)
|
||||
|
||||
self.assertRaises(TypeError, pow)
|
||||
|
||||
|
@ -1664,11 +1663,11 @@ class BuiltinTest(unittest.TestCase):
|
|||
self.assertEqual(type(round(-8.0, 0)), float)
|
||||
self.assertEqual(type(round(-8.0, 1)), float)
|
||||
|
||||
# Check even / odd rounding behaviour
|
||||
# Check half rounding behaviour.
|
||||
self.assertEqual(round(5.5), 6)
|
||||
self.assertEqual(round(6.5), 6)
|
||||
self.assertEqual(round(6.5), 7)
|
||||
self.assertEqual(round(-5.5), -6)
|
||||
self.assertEqual(round(-6.5), -6)
|
||||
self.assertEqual(round(-6.5), -7)
|
||||
|
||||
# Check behavior on ints
|
||||
self.assertEqual(round(0), 0)
|
||||
|
@ -1686,8 +1685,8 @@ class BuiltinTest(unittest.TestCase):
|
|||
|
||||
# test generic rounding delegation for reals
|
||||
class TestRound(object):
|
||||
def __round__(self):
|
||||
return 23
|
||||
def __float__(self):
|
||||
return 23.0
|
||||
|
||||
class TestNoRound(object):
|
||||
pass
|
||||
|
@ -1695,13 +1694,12 @@ class BuiltinTest(unittest.TestCase):
|
|||
self.assertEqual(round(TestRound()), 23)
|
||||
|
||||
self.assertRaises(TypeError, round, 1, 2, 3)
|
||||
# XXX: This is not ideal, but see the comment in builtin_round().
|
||||
self.assertRaises(AttributeError, round, TestNoRound())
|
||||
self.assertRaises(TypeError, round, TestNoRound())
|
||||
|
||||
t = TestNoRound()
|
||||
t.__round__ = lambda *args: args
|
||||
self.assertEquals((), round(t))
|
||||
self.assertEquals((0,), round(t, 0))
|
||||
t.__float__ = lambda *args: args
|
||||
self.assertRaises(TypeError, round, t)
|
||||
self.assertRaises(TypeError, round, t, 0)
|
||||
|
||||
def test_setattr(self):
|
||||
setattr(sys, 'spam', 1)
|
||||
|
|
|
@ -385,9 +385,7 @@ class LongTest(unittest.TestCase):
|
|||
"1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
|
||||
"math.sin(huge)", "math.sin(mhuge)",
|
||||
"math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
|
||||
# math.floor() of an int returns an int now
|
||||
##"math.floor(huge)", "math.floor(mhuge)",
|
||||
]:
|
||||
"math.floor(huge)", "math.floor(mhuge)"]:
|
||||
|
||||
self.assertRaises(OverflowError, eval, test, namespace)
|
||||
|
||||
|
|
|
@ -63,8 +63,8 @@ class MathTests(unittest.TestCase):
|
|||
self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
|
||||
|
||||
class TestCeil(object):
|
||||
def __ceil__(self):
|
||||
return 42
|
||||
def __float__(self):
|
||||
return 41.3
|
||||
class TestNoCeil(object):
|
||||
pass
|
||||
self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
|
||||
|
@ -123,8 +123,8 @@ class MathTests(unittest.TestCase):
|
|||
self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
|
||||
|
||||
class TestFloor(object):
|
||||
def __floor__(self):
|
||||
return 42
|
||||
def __float__(self):
|
||||
return 42.3
|
||||
class TestNoFloor(object):
|
||||
pass
|
||||
self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue