summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_getargs2.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_getargs2.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_getargs2.py')
-rw-r--r--Lib/test/test_getargs2.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py
index beea76a..1ee94d7 100644
--- a/Lib/test/test_getargs2.py
+++ b/Lib/test/test_getargs2.py
@@ -52,12 +52,34 @@ class Int:
def __int__(self):
return 99
+class IntSubclass(int):
+ def __int__(self):
+ return 99
+
+class BadInt:
+ def __int__(self):
+ return 1.0
+
+class BadInt2:
+ def __int__(self):
+ return True
+
+class BadInt3(int):
+ def __int__(self):
+ return True
+
+
class Unsigned_TestCase(unittest.TestCase):
def test_b(self):
from _testcapi import getargs_b
# b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
self.assertRaises(TypeError, getargs_b, 3.14)
self.assertEqual(99, getargs_b(Int()))
+ self.assertEqual(0, getargs_b(IntSubclass()))
+ self.assertRaises(TypeError, getargs_b, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_b(BadInt2()))
+ self.assertEqual(0, getargs_b(BadInt3()))
self.assertRaises(OverflowError, getargs_b, -1)
self.assertEqual(0, getargs_b(0))
@@ -72,6 +94,11 @@ class Unsigned_TestCase(unittest.TestCase):
# B returns 'unsigned char', no range checking
self.assertRaises(TypeError, getargs_B, 3.14)
self.assertEqual(99, getargs_B(Int()))
+ self.assertEqual(0, getargs_B(IntSubclass()))
+ self.assertRaises(TypeError, getargs_B, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_B(BadInt2()))
+ self.assertEqual(0, getargs_B(BadInt3()))
self.assertEqual(UCHAR_MAX, getargs_B(-1))
self.assertEqual(0, getargs_B(0))
@@ -86,6 +113,11 @@ class Unsigned_TestCase(unittest.TestCase):
# H returns 'unsigned short', no range checking
self.assertRaises(TypeError, getargs_H, 3.14)
self.assertEqual(99, getargs_H(Int()))
+ self.assertEqual(0, getargs_H(IntSubclass()))
+ self.assertRaises(TypeError, getargs_H, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_H(BadInt2()))
+ self.assertEqual(0, getargs_H(BadInt3()))
self.assertEqual(USHRT_MAX, getargs_H(-1))
self.assertEqual(0, getargs_H(0))
@@ -101,6 +133,11 @@ class Unsigned_TestCase(unittest.TestCase):
# I returns 'unsigned int', no range checking
self.assertRaises(TypeError, getargs_I, 3.14)
self.assertEqual(99, getargs_I(Int()))
+ self.assertEqual(0, getargs_I(IntSubclass()))
+ self.assertRaises(TypeError, getargs_I, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_I(BadInt2()))
+ self.assertEqual(0, getargs_I(BadInt3()))
self.assertEqual(UINT_MAX, getargs_I(-1))
self.assertEqual(0, getargs_I(0))
@@ -117,6 +154,10 @@ class Unsigned_TestCase(unittest.TestCase):
# it does not accept float, or instances with __int__
self.assertRaises(TypeError, getargs_k, 3.14)
self.assertRaises(TypeError, getargs_k, Int())
+ self.assertEqual(0, getargs_k(IntSubclass()))
+ self.assertRaises(TypeError, getargs_k, BadInt())
+ self.assertRaises(TypeError, getargs_k, BadInt2())
+ self.assertEqual(0, getargs_k(BadInt3()))
self.assertEqual(ULONG_MAX, getargs_k(-1))
self.assertEqual(0, getargs_k(0))
@@ -133,6 +174,11 @@ class Signed_TestCase(unittest.TestCase):
# h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX)
self.assertRaises(TypeError, getargs_h, 3.14)
self.assertEqual(99, getargs_h(Int()))
+ self.assertEqual(0, getargs_h(IntSubclass()))
+ self.assertRaises(TypeError, getargs_h, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_h(BadInt2()))
+ self.assertEqual(0, getargs_h(BadInt3()))
self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1)
self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN))
@@ -147,6 +193,11 @@ class Signed_TestCase(unittest.TestCase):
# i returns 'int', and does range checking (INT_MIN ... INT_MAX)
self.assertRaises(TypeError, getargs_i, 3.14)
self.assertEqual(99, getargs_i(Int()))
+ self.assertEqual(0, getargs_i(IntSubclass()))
+ self.assertRaises(TypeError, getargs_i, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_i(BadInt2()))
+ self.assertEqual(0, getargs_i(BadInt3()))
self.assertRaises(OverflowError, getargs_i, INT_MIN-1)
self.assertEqual(INT_MIN, getargs_i(INT_MIN))
@@ -161,6 +212,11 @@ class Signed_TestCase(unittest.TestCase):
# l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
self.assertRaises(TypeError, getargs_l, 3.14)
self.assertEqual(99, getargs_l(Int()))
+ self.assertEqual(0, getargs_l(IntSubclass()))
+ self.assertRaises(TypeError, getargs_l, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_l(BadInt2()))
+ self.assertEqual(0, getargs_l(BadInt3()))
self.assertRaises(OverflowError, getargs_l, LONG_MIN-1)
self.assertEqual(LONG_MIN, getargs_l(LONG_MIN))
@@ -176,6 +232,10 @@ class Signed_TestCase(unittest.TestCase):
# (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)
self.assertRaises(TypeError, getargs_n, 3.14)
self.assertRaises(TypeError, getargs_n, Int())
+ self.assertEqual(0, getargs_n(IntSubclass()))
+ self.assertRaises(TypeError, getargs_n, BadInt())
+ self.assertRaises(TypeError, getargs_n, BadInt2())
+ self.assertEqual(0, getargs_n(BadInt3()))
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))
@@ -195,6 +255,11 @@ class LongLong_TestCase(unittest.TestCase):
self.assertRaises(TypeError, getargs_L, 3.14)
self.assertRaises(TypeError, getargs_L, "Hello")
self.assertEqual(99, getargs_L(Int()))
+ self.assertEqual(0, getargs_L(IntSubclass()))
+ self.assertRaises(TypeError, getargs_L, BadInt())
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(1, getargs_L(BadInt2()))
+ self.assertEqual(0, getargs_L(BadInt3()))
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN))
@@ -209,6 +274,11 @@ class LongLong_TestCase(unittest.TestCase):
# K return 'unsigned long long', no range checking
self.assertRaises(TypeError, getargs_K, 3.14)
self.assertRaises(TypeError, getargs_K, Int())
+ self.assertEqual(0, getargs_K(IntSubclass()))
+ self.assertRaises(TypeError, getargs_K, BadInt())
+ self.assertRaises(TypeError, getargs_K, BadInt2())
+ self.assertEqual(0, getargs_K(BadInt3()))
+
self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
self.assertEqual(0, getargs_K(0))
self.assertEqual(0, getargs_K(ULLONG_MAX+1))