summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_int.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-12-11 19:07:54 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-12-11 19:07:54 (GMT)
commit31a655411a79b00517cdcd0a2752824d183db792 (patch)
tree78bcf4531d65f5b7eadf550642e8a842741e8108 /Lib/test/test_int.py
parentacd17304d290bd0b2ad41e5922f81679fea3ab6e (diff)
downloadcpython-31a655411a79b00517cdcd0a2752824d183db792.zip
cpython-31a655411a79b00517cdcd0a2752824d183db792.tar.gz
cpython-31a655411a79b00517cdcd0a2752824d183db792.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 c198bcc..a4487bc 100644
--- a/Lib/test/test_int.py
+++ b/Lib/test/test_int.py
@@ -263,32 +263,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
@@ -351,6 +326,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,