diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_cmath.py | 7 | ||||
-rw-r--r-- | Lib/test/test_complex.py | 18 | ||||
-rw-r--r-- | Lib/test/test_float.py | 15 | ||||
-rw-r--r-- | Lib/test/test_getargs2.py | 6 | ||||
-rw-r--r-- | Lib/test/test_index.py | 2 | ||||
-rw-r--r-- | Lib/test/test_int.py | 66 |
6 files changed, 103 insertions, 11 deletions
diff --git a/Lib/test/test_cmath.py b/Lib/test/test_cmath.py index 43a074b..a00185f 100644 --- a/Lib/test/test_cmath.py +++ b/Lib/test/test_cmath.py @@ -220,12 +220,11 @@ class CMathTests(unittest.TestCase): pass class NeitherComplexNorFloatOS: pass - class MyInt(object): + class Index: def __int__(self): return 2 def __index__(self): return 2 - class MyIntOS: + class MyInt: def __int__(self): return 2 - def __index__(self): return 2 # other possible combinations of __float__ and __complex__ # that should work @@ -255,6 +254,7 @@ class CMathTests(unittest.TestCase): self.assertEqual(f(FloatAndComplexOS()), f(cx_arg)) self.assertEqual(f(JustFloat()), f(flt_arg)) self.assertEqual(f(JustFloatOS()), f(flt_arg)) + self.assertEqual(f(Index()), f(int(Index()))) # TypeError should be raised for classes not providing # either __complex__ or __float__, even if they provide # __int__ or __index__. An old-style class @@ -263,7 +263,6 @@ class CMathTests(unittest.TestCase): self.assertRaises(TypeError, f, NeitherComplexNorFloat()) self.assertRaises(TypeError, f, MyInt()) self.assertRaises(Exception, f, NeitherComplexNorFloatOS()) - self.assertRaises(Exception, f, MyIntOS()) # non-complex return value from __complex__ -> TypeError for bad_complex in non_complexes: self.assertRaises(TypeError, f, MyComplex(bad_complex)) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 21c6eae..fe1e566 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -368,6 +368,24 @@ class ComplexTest(unittest.TestCase): self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j) self.assertRaises(TypeError, complex, float2(None)) + class MyIndex: + def __init__(self, value): + self.value = value + def __index__(self): + return self.value + + self.assertAlmostEqual(complex(MyIndex(42)), 42.0+0.0j) + self.assertAlmostEqual(complex(123, MyIndex(42)), 123.0+42.0j) + self.assertRaises(OverflowError, complex, MyIndex(2**2000)) + self.assertRaises(OverflowError, complex, 123, MyIndex(2**2000)) + + class MyInt: + def __int__(self): + return 42 + + self.assertRaises(TypeError, complex, MyInt()) + self.assertRaises(TypeError, complex, 123, MyInt()) + class complex0(complex): """Test usage of __complex__() when inheriting from 'complex'""" def __complex__(self): diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 5278d71..b656582 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -223,6 +223,21 @@ class GeneralFloatCases(unittest.TestCase): with self.assertWarns(DeprecationWarning): self.assertIs(type(FloatSubclass(F())), FloatSubclass) + class MyIndex: + def __init__(self, value): + self.value = value + def __index__(self): + return self.value + + self.assertEqual(float(MyIndex(42)), 42.0) + self.assertRaises(OverflowError, float, MyIndex(2**2000)) + + class MyInt: + def __int__(self): + return 42 + + self.assertRaises(TypeError, float, MyInt()) + def test_keyword_args(self): with self.assertRaisesRegex(TypeError, 'keyword argument'): float(x='3.14') diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py index 07e2d15..1a73fa4 100644 --- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -457,6 +457,8 @@ class Float_TestCase(unittest.TestCase): with self.assertWarns(DeprecationWarning): self.assertEqual(getargs_f(BadFloat2()), 4.25) self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5) + self.assertEqual(getargs_f(Index()), 99.0) + self.assertRaises(TypeError, getargs_f, Int()) for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF): self.assertEqual(getargs_f(x), x) @@ -489,6 +491,8 @@ class Float_TestCase(unittest.TestCase): with self.assertWarns(DeprecationWarning): self.assertEqual(getargs_d(BadFloat2()), 4.25) self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5) + self.assertEqual(getargs_d(Index()), 99.0) + self.assertRaises(TypeError, getargs_d, Int()) for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): self.assertEqual(getargs_d(x), x) @@ -511,6 +515,8 @@ class Float_TestCase(unittest.TestCase): with self.assertWarns(DeprecationWarning): self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j) self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j) + self.assertEqual(getargs_D(Index()), 99.0+0j) + self.assertRaises(TypeError, getargs_D, Int()) for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): c = complex(x, 1.0) diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index a2ac321..cbdc56c 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -60,7 +60,7 @@ class BaseTestCase(unittest.TestCase): # subclasses. See issue #17576. class MyInt(int): def __index__(self): - return int(self) + 1 + return int(str(self)) + 1 my_int = MyInt(7) direct_index = my_int.__index__() diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 307ca36..6fdf52e 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -378,15 +378,23 @@ class IntTestCases(unittest.TestCase): int(ExceptionalTrunc()) for trunc_result_base in (object, Classic): - class Integral(trunc_result_base): - def __int__(self): + class Index(trunc_result_base): + def __index__(self): return 42 class TruncReturnsNonInt(base): def __trunc__(self): - return Integral() - with self.assertWarns(DeprecationWarning): - self.assertEqual(int(TruncReturnsNonInt()), 42) + return Index() + self.assertEqual(int(TruncReturnsNonInt()), 42) + + class Intable(trunc_result_base): + def __int__(self): + return 42 + + class TruncReturnsNonIndex(base): + def __trunc__(self): + return Intable() + self.assertEqual(int(TruncReturnsNonInt()), 42) class NonIntegral(trunc_result_base): def __trunc__(self): @@ -418,6 +426,21 @@ class IntTestCases(unittest.TestCase): with self.assertRaises(TypeError): int(TruncReturnsBadInt()) + def test_int_subclass_with_index(self): + class MyIndex(int): + def __index__(self): + return 42 + + class BadIndex(int): + def __index__(self): + return 42.0 + + my_int = MyIndex(7) + self.assertEqual(my_int, 7) + self.assertEqual(int(my_int), 7) + + self.assertEqual(int(BadIndex()), 0) + def test_int_subclass_with_int(self): class MyInt(int): def __int__(self): @@ -431,9 +454,19 @@ class IntTestCases(unittest.TestCase): self.assertEqual(my_int, 7) self.assertEqual(int(my_int), 42) - self.assertRaises(TypeError, int, BadInt()) + my_int = BadInt(7) + self.assertEqual(my_int, 7) + self.assertRaises(TypeError, int, my_int) def test_int_returns_int_subclass(self): + class BadIndex: + def __index__(self): + return True + + class BadIndex2(int): + def __index__(self): + return True + class BadInt: def __int__(self): return True @@ -442,6 +475,10 @@ class IntTestCases(unittest.TestCase): def __int__(self): return True + class TruncReturnsBadIndex: + def __trunc__(self): + return BadIndex() + class TruncReturnsBadInt: def __trunc__(self): return BadInt() @@ -450,6 +487,17 @@ class IntTestCases(unittest.TestCase): def __trunc__(self): return True + bad_int = BadIndex() + with self.assertWarns(DeprecationWarning): + n = int(bad_int) + self.assertEqual(n, 1) + self.assertIs(type(n), int) + + bad_int = BadIndex2() + n = int(bad_int) + self.assertEqual(n, 0) + self.assertIs(type(n), int) + bad_int = BadInt() with self.assertWarns(DeprecationWarning): n = int(bad_int) @@ -462,6 +510,12 @@ class IntTestCases(unittest.TestCase): self.assertEqual(n, 1) self.assertIs(type(n), int) + bad_int = TruncReturnsBadIndex() + with self.assertWarns(DeprecationWarning): + n = int(bad_int) + self.assertEqual(n, 1) + self.assertIs(type(n), int) + bad_int = TruncReturnsBadInt() with self.assertWarns(DeprecationWarning): n = int(bad_int) |