Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 3 | from _testcapi import getargs_keywords |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 4 | |
| 5 | """ |
| 6 | > How about the following counterproposal. This also changes some of |
| 7 | > the other format codes to be a little more regular. |
| 8 | > |
| 9 | > Code C type Range check |
| 10 | > |
| 11 | > b unsigned char 0..UCHAR_MAX |
Guido van Rossum | bbb931b | 2003-04-18 00:13:53 +0000 | [diff] [blame] | 12 | > h signed short SHRT_MIN..SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 13 | > B unsigned char none ** |
| 14 | > H unsigned short none ** |
| 15 | > k * unsigned long none |
| 16 | > I * unsigned int 0..UINT_MAX |
| 17 | |
| 18 | |
| 19 | > i int INT_MIN..INT_MAX |
| 20 | > l long LONG_MIN..LONG_MAX |
| 21 | |
| 22 | > K * unsigned long long none |
| 23 | > L long long LLONG_MIN..LLONG_MAX |
| 24 | |
| 25 | > Notes: |
| 26 | > |
| 27 | > * New format codes. |
| 28 | > |
| 29 | > ** Changed from previous "range-and-a-half" to "none"; the |
| 30 | > range-and-a-half checking wasn't particularly useful. |
| 31 | |
| 32 | Plus a C API or two, e.g. PyInt_AsLongMask() -> |
| 33 | unsigned long and PyInt_AsLongLongMask() -> unsigned |
| 34 | long long (if that exists). |
| 35 | """ |
| 36 | |
| 37 | LARGE = 0x7FFFFFFF |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 38 | VERY_LARGE = 0xFF0000121212121212121242 |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 39 | |
| 40 | from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 41 | INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
| 42 | SHRT_MIN, SHRT_MAX |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 43 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 44 | # fake, they are not defined in Python's header files |
| 45 | LLONG_MAX = 2**63-1 |
| 46 | LLONG_MIN = -2**63 |
| 47 | ULLONG_MAX = 2**64-1 |
| 48 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 49 | class Int: |
| 50 | def __int__(self): |
| 51 | return 99 |
| 52 | |
| 53 | class Unsigned_TestCase(unittest.TestCase): |
| 54 | def test_b(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 55 | from _testcapi import getargs_b |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 56 | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 57 | self.assertRaises(TypeError, getargs_b, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 58 | self.assertEqual(99, getargs_b(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 59 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 60 | self.assertRaises(OverflowError, getargs_b, -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 61 | self.assertEqual(0, getargs_b(0)) |
| 62 | self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 63 | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 64 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 65 | self.assertEqual(42, getargs_b(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 66 | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 67 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 68 | def test_B(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 69 | from _testcapi import getargs_B |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 70 | # B returns 'unsigned char', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 71 | self.assertRaises(TypeError, getargs_B, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertEqual(99, getargs_B(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 74 | self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 75 | self.assertEqual(0, getargs_B(0)) |
| 76 | self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 77 | self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 78 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 79 | self.assertEqual(42, getargs_B(42)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 80 | self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 81 | |
| 82 | def test_H(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 83 | from _testcapi import getargs_H |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 84 | # H returns 'unsigned short', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 85 | self.assertRaises(TypeError, getargs_H, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 86 | self.assertEqual(99, getargs_H(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 88 | self.assertEqual(USHRT_MAX, getargs_H(-1)) |
| 89 | self.assertEqual(0, getargs_H(0)) |
| 90 | self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 91 | self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 92 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 93 | self.assertEqual(42, getargs_H(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 94 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 95 | self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 96 | |
| 97 | def test_I(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 98 | from _testcapi import getargs_I |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 99 | # I returns 'unsigned int', no range checking |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 100 | self.assertRaises(TypeError, getargs_I, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 101 | self.assertEqual(99, getargs_I(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 103 | self.assertEqual(UINT_MAX, getargs_I(-1)) |
| 104 | self.assertEqual(0, getargs_I(0)) |
| 105 | self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 106 | self.assertEqual(0, getargs_I(UINT_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 107 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 108 | self.assertEqual(42, getargs_I(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 109 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 110 | self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 111 | |
| 112 | def test_k(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 113 | from _testcapi import getargs_k |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 114 | # k returns 'unsigned long', no range checking |
| 115 | # it does not accept float, or instances with __int__ |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 116 | self.assertRaises(TypeError, getargs_k, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 117 | self.assertRaises(TypeError, getargs_k, Int()) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 119 | self.assertEqual(ULONG_MAX, getargs_k(-1)) |
| 120 | self.assertEqual(0, getargs_k(0)) |
| 121 | self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 122 | self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 123 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 124 | self.assertEqual(42, getargs_k(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 126 | self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 127 | |
| 128 | class Signed_TestCase(unittest.TestCase): |
Mark Dickinson | 1554b18 | 2009-12-20 16:03:30 +0000 | [diff] [blame] | 129 | def test_h(self): |
| 130 | from _testcapi import getargs_h |
| 131 | # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
| 132 | self.assertRaises(TypeError, getargs_h, 3.14) |
| 133 | self.assertEqual(99, getargs_h(Int())) |
| 134 | |
| 135 | self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
| 136 | self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
| 137 | self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
| 138 | self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
| 139 | |
| 140 | self.assertEqual(42, getargs_h(42)) |
| 141 | self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
| 142 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 143 | def test_i(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 144 | from _testcapi import getargs_i |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 145 | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 146 | self.assertRaises(TypeError, getargs_i, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 147 | self.assertEqual(99, getargs_i(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 148 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 149 | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 150 | self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
| 151 | self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 152 | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 153 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 154 | self.assertEqual(42, getargs_i(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 155 | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 156 | |
| 157 | def test_l(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 158 | from _testcapi import getargs_l |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 159 | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
Thomas Wouters | c947123 | 2006-04-15 09:15:11 +0000 | [diff] [blame] | 160 | self.assertRaises(TypeError, getargs_l, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 161 | self.assertEqual(99, getargs_l(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 162 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 163 | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 164 | self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 165 | self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 166 | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 167 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 168 | self.assertEqual(42, getargs_l(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 169 | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 170 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 171 | def test_n(self): |
| 172 | from _testcapi import getargs_n |
| 173 | # n returns 'Py_ssize_t', and does range checking |
| 174 | # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
Thomas Wouters | d807328 | 2006-04-21 11:36:13 +0000 | [diff] [blame] | 175 | self.assertRaises(TypeError, getargs_n, 3.14) |
Trent Nelson | 3513358 | 2008-04-22 19:02:40 +0000 | [diff] [blame] | 176 | self.assertRaises(TypeError, getargs_n, Int()) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 177 | |
| 178 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 179 | self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
| 180 | self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 181 | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
| 182 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 183 | self.assertEqual(42, getargs_n(42)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 184 | self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
| 185 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 186 | |
| 187 | class LongLong_TestCase(unittest.TestCase): |
| 188 | def test_L(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 189 | from _testcapi import getargs_L |
Mark Dickinson | de60401 | 2010-01-01 19:27:32 +0000 | [diff] [blame] | 190 | # L returns 'long long', and does range checking (LLONG_MIN |
| 191 | # ... LLONG_MAX) |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame^] | 192 | self.assertRaises(TypeError, getargs_L, 3.14) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 193 | self.assertRaises(TypeError, getargs_L, "Hello") |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 194 | self.assertEqual(99, getargs_L(Int())) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 195 | |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 196 | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 197 | self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 198 | self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 199 | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 201 | self.assertEqual(42, getargs_L(42)) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 202 | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
Tim Peters | 0eadaac | 2003-04-24 16:02:54 +0000 | [diff] [blame] | 203 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 204 | def test_K(self): |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 205 | from _testcapi import getargs_K |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 206 | # K return 'unsigned long long', no range checking |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 207 | self.assertRaises(TypeError, getargs_K, 3.14) |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 208 | self.assertRaises(TypeError, getargs_K, Int()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 209 | self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 210 | self.assertEqual(0, getargs_K(0)) |
| 211 | self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 213 | self.assertEqual(42, getargs_K(42)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 215 | self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 216 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | |
| 218 | class Tuple_TestCase(unittest.TestCase): |
| 219 | def test_tuple(self): |
| 220 | from _testcapi import getargs_tuple |
| 221 | |
| 222 | ret = getargs_tuple(1, (2, 3)) |
| 223 | self.assertEquals(ret, (1,2,3)) |
| 224 | |
| 225 | # make sure invalid tuple arguments are handled correctly |
| 226 | class seq: |
| 227 | def __len__(self): |
| 228 | return 2 |
| 229 | def __getitem__(self, n): |
| 230 | raise ValueError |
| 231 | self.assertRaises(TypeError, getargs_tuple, 1, seq()) |
| 232 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 233 | class Keywords_TestCase(unittest.TestCase): |
| 234 | def test_positional_args(self): |
| 235 | # using all positional args |
| 236 | self.assertEquals( |
| 237 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
| 238 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 239 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 240 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 241 | def test_mixed_args(self): |
| 242 | # positional and keyword args |
| 243 | self.assertEquals( |
| 244 | getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
| 245 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 246 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 247 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 248 | def test_keyword_args(self): |
| 249 | # all keywords |
| 250 | self.assertEquals( |
| 251 | getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
| 252 | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 253 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 254 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 255 | def test_optional_args(self): |
| 256 | # missing optional keyword args, skipping tuples |
| 257 | self.assertEquals( |
| 258 | getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
| 259 | (1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
| 260 | ) |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 261 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 262 | def test_required_args(self): |
| 263 | # required arg missing |
| 264 | try: |
| 265 | getargs_keywords(arg1=(1,2)) |
| 266 | except TypeError as err: |
| 267 | self.assertEquals(str(err), "Required argument 'arg2' (pos 2) not found") |
| 268 | else: |
| 269 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 270 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 271 | def test_too_many_args(self): |
| 272 | try: |
| 273 | getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
| 274 | except TypeError as err: |
| 275 | self.assertEquals(str(err), "function takes at most 5 arguments (6 given)") |
| 276 | else: |
| 277 | self.fail('TypeError should have been raised') |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 278 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 279 | def test_invalid_keyword(self): |
| 280 | # extraneous keyword arg |
| 281 | try: |
| 282 | getargs_keywords((1,2),3,arg5=10,arg666=666) |
| 283 | except TypeError as err: |
| 284 | self.assertEquals(str(err), "'arg666' is an invalid keyword argument for this function") |
| 285 | else: |
| 286 | self.fail('TypeError should have been raised') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 287 | |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 288 | def test_surrogate_keyword(self): |
| 289 | try: |
| 290 | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
| 291 | except TypeError as err: |
| 292 | self.assertEquals(str(err), "'\udc80' is an invalid keyword argument for this function") |
| 293 | else: |
| 294 | self.fail('TypeError should have been raised') |
| 295 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 296 | def test_main(): |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 297 | tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase, Keywords_TestCase] |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 298 | try: |
Thomas Heller | 54aa578 | 2003-04-24 16:15:29 +0000 | [diff] [blame] | 299 | from _testcapi import getargs_L, getargs_K |
| 300 | except ImportError: |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 301 | pass # PY_LONG_LONG not available |
| 302 | else: |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 303 | tests.append(LongLong_TestCase) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 304 | support.run_unittest(*tests) |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 305 | |
| 306 | if __name__ == "__main__": |
| 307 | test_main() |