blob: baf70d16321c538904b90bc05dab4a1aa03e1b42 [file] [log] [blame]
Thomas Hellera4ea6032003-04-17 18:55:45 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Christian Heimes380f7f22008-02-28 11:19:05 +00003from _testcapi import getargs_keywords
Thomas Hellera4ea6032003-04-17 18:55:45 +00004
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 Rossumbbb931b2003-04-18 00:13:53 +000012> h signed short SHRT_MIN..SHRT_MAX
Thomas Hellera4ea6032003-04-17 18:55:45 +000013> 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
32Plus a C API or two, e.g. PyInt_AsLongMask() ->
33unsigned long and PyInt_AsLongLongMask() -> unsigned
34long long (if that exists).
35"""
36
37LARGE = 0x7FFFFFFF
Guido van Rossume2a383d2007-01-15 16:59:06 +000038VERY_LARGE = 0xFF0000121212121212121242
Thomas Hellera4ea6032003-04-17 18:55:45 +000039
40from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
Mark Dickinson1554b182009-12-20 16:03:30 +000041 INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \
42 SHRT_MIN, SHRT_MAX
Thomas Hellera4ea6032003-04-17 18:55:45 +000043
Thomas Hellera4ea6032003-04-17 18:55:45 +000044# fake, they are not defined in Python's header files
45LLONG_MAX = 2**63-1
46LLONG_MIN = -2**63
47ULLONG_MAX = 2**64-1
48
Thomas Hellera4ea6032003-04-17 18:55:45 +000049class Int:
50 def __int__(self):
51 return 99
52
53class Unsigned_TestCase(unittest.TestCase):
54 def test_b(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000055 from _testcapi import getargs_b
Thomas Hellera4ea6032003-04-17 18:55:45 +000056 # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +000057 self.assertRaises(TypeError, getargs_b, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000058 self.assertEqual(99, getargs_b(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000059
Thomas Heller54aa5782003-04-24 16:15:29 +000060 self.assertRaises(OverflowError, getargs_b, -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000061 self.assertEqual(0, getargs_b(0))
62 self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +000063 self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1)
Thomas Hellera4ea6032003-04-17 18:55:45 +000064
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000065 self.assertEqual(42, getargs_b(42))
Thomas Heller54aa5782003-04-24 16:15:29 +000066 self.assertRaises(OverflowError, getargs_b, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +000067
Thomas Hellera4ea6032003-04-17 18:55:45 +000068 def test_B(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000069 from _testcapi import getargs_B
Thomas Hellera4ea6032003-04-17 18:55:45 +000070 # B returns 'unsigned char', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +000071 self.assertRaises(TypeError, getargs_B, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000072 self.assertEqual(99, getargs_B(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000073
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000074 self.assertEqual(UCHAR_MAX, getargs_B(-1))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 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 Hellera4ea6032003-04-17 18:55:45 +000078
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertEqual(42, getargs_B(42))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000080 self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +000081
82 def test_H(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000083 from _testcapi import getargs_H
Thomas Hellera4ea6032003-04-17 18:55:45 +000084 # H returns 'unsigned short', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +000085 self.assertRaises(TypeError, getargs_H, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000086 self.assertEqual(99, getargs_H(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000087
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000088 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 Hellera4ea6032003-04-17 18:55:45 +000092
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000093 self.assertEqual(42, getargs_H(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +000094
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000095 self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +000096
97 def test_I(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000098 from _testcapi import getargs_I
Thomas Hellera4ea6032003-04-17 18:55:45 +000099 # I returns 'unsigned int', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +0000100 self.assertRaises(TypeError, getargs_I, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000101 self.assertEqual(99, getargs_I(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000102
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000103 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 Hellera4ea6032003-04-17 18:55:45 +0000107
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000108 self.assertEqual(42, getargs_I(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000109
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000110 self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000111
112 def test_k(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000113 from _testcapi import getargs_k
Thomas Hellera4ea6032003-04-17 18:55:45 +0000114 # k returns 'unsigned long', no range checking
115 # it does not accept float, or instances with __int__
Thomas Heller54aa5782003-04-24 16:15:29 +0000116 self.assertRaises(TypeError, getargs_k, 3.14)
Thomas Heller54aa5782003-04-24 16:15:29 +0000117 self.assertRaises(TypeError, getargs_k, Int())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000118
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000119 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 Hellera4ea6032003-04-17 18:55:45 +0000123
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000124 self.assertEqual(42, getargs_k(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000125
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000126 self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000127
128class Signed_TestCase(unittest.TestCase):
Mark Dickinson1554b182009-12-20 16:03:30 +0000129 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 Hellera4ea6032003-04-17 18:55:45 +0000143 def test_i(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000144 from _testcapi import getargs_i
Thomas Hellera4ea6032003-04-17 18:55:45 +0000145 # i returns 'int', and does range checking (INT_MIN ... INT_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +0000146 self.assertRaises(TypeError, getargs_i, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000147 self.assertEqual(99, getargs_i(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000148
Thomas Heller54aa5782003-04-24 16:15:29 +0000149 self.assertRaises(OverflowError, getargs_i, INT_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000150 self.assertEqual(INT_MIN, getargs_i(INT_MIN))
151 self.assertEqual(INT_MAX, getargs_i(INT_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000152 self.assertRaises(OverflowError, getargs_i, INT_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000153
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000154 self.assertEqual(42, getargs_i(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000155 self.assertRaises(OverflowError, getargs_i, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000156
157 def test_l(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000158 from _testcapi import getargs_l
Thomas Hellera4ea6032003-04-17 18:55:45 +0000159 # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +0000160 self.assertRaises(TypeError, getargs_l, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000161 self.assertEqual(99, getargs_l(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000162
Thomas Heller54aa5782003-04-24 16:15:29 +0000163 self.assertRaises(OverflowError, getargs_l, LONG_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000164 self.assertEqual(LONG_MIN, getargs_l(LONG_MIN))
165 self.assertEqual(LONG_MAX, getargs_l(LONG_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000166 self.assertRaises(OverflowError, getargs_l, LONG_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000167
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000168 self.assertEqual(42, getargs_l(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000169 self.assertRaises(OverflowError, getargs_l, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000170
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 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 Woutersd8073282006-04-21 11:36:13 +0000175 self.assertRaises(TypeError, getargs_n, 3.14)
Trent Nelson35133582008-04-22 19:02:40 +0000176 self.assertRaises(TypeError, getargs_n, Int())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177
178 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000179 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 Wouters49fd7fa2006-04-21 10:40:58 +0000181 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)
182
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000183 self.assertEqual(42, getargs_n(42))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184 self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
185
Thomas Hellera4ea6032003-04-17 18:55:45 +0000186
187class LongLong_TestCase(unittest.TestCase):
188 def test_L(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000189 from _testcapi import getargs_L
Mark Dickinsonde604012010-01-01 19:27:32 +0000190 # L returns 'long long', and does range checking (LLONG_MIN
191 # ... LLONG_MAX)
Mark Dickinsonc7301312010-06-10 16:05:10 +0000192 self.assertRaises(TypeError, getargs_L, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000193 self.assertRaises(TypeError, getargs_L, "Hello")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000194 self.assertEqual(99, getargs_L(Int()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000195
Thomas Heller54aa5782003-04-24 16:15:29 +0000196 self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000197 self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN))
198 self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000199 self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000200
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000201 self.assertEqual(42, getargs_L(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000202 self.assertRaises(OverflowError, getargs_L, VERY_LARGE)
Tim Peters0eadaac2003-04-24 16:02:54 +0000203
Thomas Hellera4ea6032003-04-17 18:55:45 +0000204 def test_K(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000205 from _testcapi import getargs_K
Thomas Hellera4ea6032003-04-17 18:55:45 +0000206 # K return 'unsigned long long', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +0000207 self.assertRaises(TypeError, getargs_K, 3.14)
Thomas Heller54aa5782003-04-24 16:15:29 +0000208 self.assertRaises(TypeError, getargs_K, Int())
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000209 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 Hellera4ea6032003-04-17 18:55:45 +0000212
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000213 self.assertEqual(42, getargs_K(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000214
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000215 self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000216
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000217
218class 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 Heimes380f7f22008-02-28 11:19:05 +0000233class 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 Stinner93b55132010-05-19 00:54:06 +0000240
Christian Heimes380f7f22008-02-28 11:19:05 +0000241 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 Stinner93b55132010-05-19 00:54:06 +0000247
Christian Heimes380f7f22008-02-28 11:19:05 +0000248 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 Stinner93b55132010-05-19 00:54:06 +0000254
Christian Heimes380f7f22008-02-28 11:19:05 +0000255 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 Stinner93b55132010-05-19 00:54:06 +0000261
Christian Heimes380f7f22008-02-28 11:19:05 +0000262 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 Stinner93b55132010-05-19 00:54:06 +0000270
Christian Heimes380f7f22008-02-28 11:19:05 +0000271 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 Stinner93b55132010-05-19 00:54:06 +0000278
Christian Heimes380f7f22008-02-28 11:19:05 +0000279 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 Wouters0e3f5912006-08-11 14:57:12 +0000287
Victor Stinner93b55132010-05-19 00:54:06 +0000288 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 Hellera4ea6032003-04-17 18:55:45 +0000296def test_main():
Christian Heimes380f7f22008-02-28 11:19:05 +0000297 tests = [Signed_TestCase, Unsigned_TestCase, Tuple_TestCase, Keywords_TestCase]
Thomas Hellera4ea6032003-04-17 18:55:45 +0000298 try:
Thomas Heller54aa5782003-04-24 16:15:29 +0000299 from _testcapi import getargs_L, getargs_K
300 except ImportError:
Thomas Hellera4ea6032003-04-17 18:55:45 +0000301 pass # PY_LONG_LONG not available
302 else:
Walter Dörwald21d3a322003-05-01 17:45:56 +0000303 tests.append(LongLong_TestCase)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000304 support.run_unittest(*tests)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000305
306if __name__ == "__main__":
307 test_main()