summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-12-11 19:26:36 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-12-11 19:26:36 (GMT)
commitc4f3212abc7e927f9218a7c82fae9e5639e272bc (patch)
tree18d65bdc892a0593c32c4ea037a992f82abfe6f0 /Lib/test/test_int.py
parentb282b3d804165d704130fabf0f609d5dd46c1ed2 (diff)
parent31a655411a79b00517cdcd0a2752824d183db792 (diff)
downloadcpython-c4f3212abc7e927f9218a7c82fae9e5639e272bc.zip
cpython-c4f3212abc7e927f9218a7c82fae9e5639e272bc.tar.gz
cpython-c4f3212abc7e927f9218a7c82fae9e5639e272bc.tar.bz2
Issue #17576: Deprecation warning emitted now when __int__() or __index__()
return not int instance. Introduced _PyLong_FromNbInt() and refactored PyLong_As*() functions.
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r--Lib/test/test_int.py76
1 files changed, 51 insertions, 25 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py
index 4e00622..e94602e 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -304,32 +304,7 @@ class IntTestCases(unittest.TestCase):
def __int__(self):
return 42
- class Foo1(object):
- def __int__(self):
- return 42
-
- class Foo2(int):
- def __int__(self):
- return 42
-
- class Foo3(int):
- def __int__(self):
- return self
-
- class Foo4(int):
- def __int__(self):
- return 42
-
- class Foo5(int):
- def __int__(self):
- return 42.
-
self.assertEqual(int(Foo0()), 42)
- self.assertEqual(int(Foo1()), 42)
- self.assertEqual(int(Foo2()), 42)
- self.assertEqual(int(Foo3()), 0)
- self.assertEqual(int(Foo4()), 42)
- self.assertRaises(TypeError, int, Foo5())
class Classic:
pass
@@ -392,6 +367,57 @@ class IntTestCases(unittest.TestCase):
with self.assertRaises(TypeError):
int(TruncReturnsBadInt())
+ def test_int_subclass_with_int(self):
+ class MyInt(int):
+ def __int__(self):
+ return 42
+
+ class BadInt(int):
+ def __int__(self):
+ return 42.0
+
+ my_int = MyInt(7)
+ self.assertEqual(my_int, 7)
+ self.assertEqual(int(my_int), 42)
+
+ self.assertRaises(TypeError, int, BadInt())
+
+ def test_int_returns_int_subclass(self):
+ class BadInt:
+ def __int__(self):
+ return True
+
+ class BadInt2(int):
+ def __int__(self):
+ return True
+
+ class TruncReturnsBadInt:
+ def __trunc__(self):
+ return BadInt()
+
+ class TruncReturnsIntSubclass:
+ def __trunc__(self):
+ return True
+
+ bad_int = BadInt()
+ with self.assertWarns(DeprecationWarning):
+ n = int(bad_int)
+ self.assertEqual(n, 1)
+
+ bad_int = BadInt2()
+ with self.assertWarns(DeprecationWarning):
+ n = int(bad_int)
+ self.assertEqual(n, 1)
+
+ bad_int = TruncReturnsBadInt()
+ with self.assertWarns(DeprecationWarning):
+ n = int(bad_int)
+ self.assertEqual(n, 1)
+
+ good_int = TruncReturnsIntSubclass()
+ n = int(good_int)
+ self.assertEqual(n, 1)
+
def test_error_message(self):
def check(s, base=None):
with self.assertRaises(ValueError,