blob: 1853a2dbed731b5ae425b76a35dc9700b27d221a [file] [log] [blame]
Thomas Hellera4ea6032003-04-17 18:55:45 +00001import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00002from test import support
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +02003# Skip this test if the _testcapi module isn't available.
4support.import_module('_testcapi')
Larry Hastings83a9f482012-03-20 20:06:16 +00005from _testcapi import getargs_keywords, getargs_keyword_only
Brett Cannon3e9a9ae2013-06-12 21:25:59 -04006try:
7 from _testcapi import getargs_L, getargs_K
8except ImportError:
9 getargs_L = None # PY_LONG_LONG not available
Thomas Hellera4ea6032003-04-17 18:55:45 +000010
Victor Stinner765531d2013-03-26 01:11:54 +010011# > How about the following counterproposal. This also changes some of
12# > the other format codes to be a little more regular.
13# >
14# > Code C type Range check
15# >
16# > b unsigned char 0..UCHAR_MAX
17# > h signed short SHRT_MIN..SHRT_MAX
18# > B unsigned char none **
19# > H unsigned short none **
20# > k * unsigned long none
21# > I * unsigned int 0..UINT_MAX
22#
23#
24# > i int INT_MIN..INT_MAX
25# > l long LONG_MIN..LONG_MAX
26#
27# > K * unsigned long long none
28# > L long long LLONG_MIN..LLONG_MAX
29#
30# > Notes:
31# >
32# > * New format codes.
33# >
34# > ** Changed from previous "range-and-a-half" to "none"; the
35# > range-and-a-half checking wasn't particularly useful.
36#
37# Plus a C API or two, e.g. PyInt_AsLongMask() ->
38# unsigned long and PyInt_AsLongLongMask() -> unsigned
39# long long (if that exists).
Thomas Hellera4ea6032003-04-17 18:55:45 +000040
41LARGE = 0x7FFFFFFF
Guido van Rossume2a383d2007-01-15 16:59:06 +000042VERY_LARGE = 0xFF0000121212121212121242
Thomas Hellera4ea6032003-04-17 18:55:45 +000043
44from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \
Mark Dickinson1554b182009-12-20 16:03:30 +000045 INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \
46 SHRT_MIN, SHRT_MAX
Thomas Hellera4ea6032003-04-17 18:55:45 +000047
Thomas Hellera4ea6032003-04-17 18:55:45 +000048# fake, they are not defined in Python's header files
49LLONG_MAX = 2**63-1
50LLONG_MIN = -2**63
51ULLONG_MAX = 2**64-1
52
Thomas Hellera4ea6032003-04-17 18:55:45 +000053class Int:
54 def __int__(self):
55 return 99
56
Serhiy Storchaka31a65542013-12-11 21:07:54 +020057class IntSubclass(int):
58 def __int__(self):
59 return 99
60
61class BadInt:
62 def __int__(self):
63 return 1.0
64
65class BadInt2:
66 def __int__(self):
67 return True
68
69class BadInt3(int):
70 def __int__(self):
71 return True
72
73
Thomas Hellera4ea6032003-04-17 18:55:45 +000074class Unsigned_TestCase(unittest.TestCase):
75 def test_b(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000076 from _testcapi import getargs_b
Thomas Hellera4ea6032003-04-17 18:55:45 +000077 # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +000078 self.assertRaises(TypeError, getargs_b, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000079 self.assertEqual(99, getargs_b(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +020080 self.assertEqual(0, getargs_b(IntSubclass()))
81 self.assertRaises(TypeError, getargs_b, BadInt())
82 with self.assertWarns(DeprecationWarning):
83 self.assertEqual(1, getargs_b(BadInt2()))
84 self.assertEqual(0, getargs_b(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +000085
Thomas Heller54aa5782003-04-24 16:15:29 +000086 self.assertRaises(OverflowError, getargs_b, -1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000087 self.assertEqual(0, getargs_b(0))
88 self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +000089 self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1)
Thomas Hellera4ea6032003-04-17 18:55:45 +000090
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000091 self.assertEqual(42, getargs_b(42))
Thomas Heller54aa5782003-04-24 16:15:29 +000092 self.assertRaises(OverflowError, getargs_b, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +000093
Thomas Hellera4ea6032003-04-17 18:55:45 +000094 def test_B(self):
Thomas Heller54aa5782003-04-24 16:15:29 +000095 from _testcapi import getargs_B
Thomas Hellera4ea6032003-04-17 18:55:45 +000096 # B returns 'unsigned char', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +000097 self.assertRaises(TypeError, getargs_B, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000098 self.assertEqual(99, getargs_B(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +020099 self.assertEqual(0, getargs_B(IntSubclass()))
100 self.assertRaises(TypeError, getargs_B, BadInt())
101 with self.assertWarns(DeprecationWarning):
102 self.assertEqual(1, getargs_B(BadInt2()))
103 self.assertEqual(0, getargs_B(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000104
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000105 self.assertEqual(UCHAR_MAX, getargs_B(-1))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000106 self.assertEqual(0, getargs_B(0))
107 self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX))
108 self.assertEqual(0, getargs_B(UCHAR_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000109
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000110 self.assertEqual(42, getargs_B(42))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000111 self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000112
113 def test_H(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000114 from _testcapi import getargs_H
Thomas Hellera4ea6032003-04-17 18:55:45 +0000115 # H returns 'unsigned short', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +0000116 self.assertRaises(TypeError, getargs_H, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000117 self.assertEqual(99, getargs_H(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200118 self.assertEqual(0, getargs_H(IntSubclass()))
119 self.assertRaises(TypeError, getargs_H, BadInt())
120 with self.assertWarns(DeprecationWarning):
121 self.assertEqual(1, getargs_H(BadInt2()))
122 self.assertEqual(0, getargs_H(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000123
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000124 self.assertEqual(USHRT_MAX, getargs_H(-1))
125 self.assertEqual(0, getargs_H(0))
126 self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX))
127 self.assertEqual(0, getargs_H(USHRT_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000128
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000129 self.assertEqual(42, getargs_H(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000130
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000131 self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000132
133 def test_I(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000134 from _testcapi import getargs_I
Thomas Hellera4ea6032003-04-17 18:55:45 +0000135 # I returns 'unsigned int', no range checking
Thomas Woutersc9471232006-04-15 09:15:11 +0000136 self.assertRaises(TypeError, getargs_I, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000137 self.assertEqual(99, getargs_I(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200138 self.assertEqual(0, getargs_I(IntSubclass()))
139 self.assertRaises(TypeError, getargs_I, BadInt())
140 with self.assertWarns(DeprecationWarning):
141 self.assertEqual(1, getargs_I(BadInt2()))
142 self.assertEqual(0, getargs_I(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000143
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000144 self.assertEqual(UINT_MAX, getargs_I(-1))
145 self.assertEqual(0, getargs_I(0))
146 self.assertEqual(UINT_MAX, getargs_I(UINT_MAX))
147 self.assertEqual(0, getargs_I(UINT_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000148
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000149 self.assertEqual(42, getargs_I(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000150
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000151 self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000152
153 def test_k(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000154 from _testcapi import getargs_k
Thomas Hellera4ea6032003-04-17 18:55:45 +0000155 # k returns 'unsigned long', no range checking
156 # it does not accept float, or instances with __int__
Thomas Heller54aa5782003-04-24 16:15:29 +0000157 self.assertRaises(TypeError, getargs_k, 3.14)
Thomas Heller54aa5782003-04-24 16:15:29 +0000158 self.assertRaises(TypeError, getargs_k, Int())
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200159 self.assertEqual(0, getargs_k(IntSubclass()))
160 self.assertRaises(TypeError, getargs_k, BadInt())
161 self.assertRaises(TypeError, getargs_k, BadInt2())
162 self.assertEqual(0, getargs_k(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000163
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000164 self.assertEqual(ULONG_MAX, getargs_k(-1))
165 self.assertEqual(0, getargs_k(0))
166 self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX))
167 self.assertEqual(0, getargs_k(ULONG_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000168
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertEqual(42, getargs_k(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000170
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000171 self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000172
173class Signed_TestCase(unittest.TestCase):
Mark Dickinson1554b182009-12-20 16:03:30 +0000174 def test_h(self):
175 from _testcapi import getargs_h
176 # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX)
177 self.assertRaises(TypeError, getargs_h, 3.14)
178 self.assertEqual(99, getargs_h(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200179 self.assertEqual(0, getargs_h(IntSubclass()))
180 self.assertRaises(TypeError, getargs_h, BadInt())
181 with self.assertWarns(DeprecationWarning):
182 self.assertEqual(1, getargs_h(BadInt2()))
183 self.assertEqual(0, getargs_h(BadInt3()))
Mark Dickinson1554b182009-12-20 16:03:30 +0000184
185 self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1)
186 self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN))
187 self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX))
188 self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1)
189
190 self.assertEqual(42, getargs_h(42))
191 self.assertRaises(OverflowError, getargs_h, VERY_LARGE)
192
Thomas Hellera4ea6032003-04-17 18:55:45 +0000193 def test_i(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000194 from _testcapi import getargs_i
Thomas Hellera4ea6032003-04-17 18:55:45 +0000195 # i returns 'int', and does range checking (INT_MIN ... INT_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +0000196 self.assertRaises(TypeError, getargs_i, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000197 self.assertEqual(99, getargs_i(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200198 self.assertEqual(0, getargs_i(IntSubclass()))
199 self.assertRaises(TypeError, getargs_i, BadInt())
200 with self.assertWarns(DeprecationWarning):
201 self.assertEqual(1, getargs_i(BadInt2()))
202 self.assertEqual(0, getargs_i(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000203
Thomas Heller54aa5782003-04-24 16:15:29 +0000204 self.assertRaises(OverflowError, getargs_i, INT_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000205 self.assertEqual(INT_MIN, getargs_i(INT_MIN))
206 self.assertEqual(INT_MAX, getargs_i(INT_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000207 self.assertRaises(OverflowError, getargs_i, INT_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000208
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000209 self.assertEqual(42, getargs_i(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000210 self.assertRaises(OverflowError, getargs_i, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000211
212 def test_l(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000213 from _testcapi import getargs_l
Thomas Hellera4ea6032003-04-17 18:55:45 +0000214 # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX)
Thomas Woutersc9471232006-04-15 09:15:11 +0000215 self.assertRaises(TypeError, getargs_l, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000216 self.assertEqual(99, getargs_l(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200217 self.assertEqual(0, getargs_l(IntSubclass()))
218 self.assertRaises(TypeError, getargs_l, BadInt())
219 with self.assertWarns(DeprecationWarning):
220 self.assertEqual(1, getargs_l(BadInt2()))
221 self.assertEqual(0, getargs_l(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000222
Thomas Heller54aa5782003-04-24 16:15:29 +0000223 self.assertRaises(OverflowError, getargs_l, LONG_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000224 self.assertEqual(LONG_MIN, getargs_l(LONG_MIN))
225 self.assertEqual(LONG_MAX, getargs_l(LONG_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000226 self.assertRaises(OverflowError, getargs_l, LONG_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000227
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000228 self.assertEqual(42, getargs_l(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000229 self.assertRaises(OverflowError, getargs_l, VERY_LARGE)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000230
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000231 def test_n(self):
232 from _testcapi import getargs_n
233 # n returns 'Py_ssize_t', and does range checking
234 # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX)
Thomas Woutersd8073282006-04-21 11:36:13 +0000235 self.assertRaises(TypeError, getargs_n, 3.14)
Trent Nelson35133582008-04-22 19:02:40 +0000236 self.assertRaises(TypeError, getargs_n, Int())
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200237 self.assertEqual(0, getargs_n(IntSubclass()))
238 self.assertRaises(TypeError, getargs_n, BadInt())
239 self.assertRaises(TypeError, getargs_n, BadInt2())
240 self.assertEqual(0, getargs_n(BadInt3()))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241
242 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000243 self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN))
244 self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1)
246
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000247 self.assertEqual(42, getargs_n(42))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 self.assertRaises(OverflowError, getargs_n, VERY_LARGE)
249
Thomas Hellera4ea6032003-04-17 18:55:45 +0000250
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400251@unittest.skipIf(getargs_L is None, 'PY_LONG_LONG is not available')
Thomas Hellera4ea6032003-04-17 18:55:45 +0000252class LongLong_TestCase(unittest.TestCase):
253 def test_L(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000254 from _testcapi import getargs_L
Mark Dickinsonde604012010-01-01 19:27:32 +0000255 # L returns 'long long', and does range checking (LLONG_MIN
256 # ... LLONG_MAX)
Mark Dickinsonc7301312010-06-10 16:05:10 +0000257 self.assertRaises(TypeError, getargs_L, 3.14)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000258 self.assertRaises(TypeError, getargs_L, "Hello")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000259 self.assertEqual(99, getargs_L(Int()))
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200260 self.assertEqual(0, getargs_L(IntSubclass()))
261 self.assertRaises(TypeError, getargs_L, BadInt())
262 with self.assertWarns(DeprecationWarning):
263 self.assertEqual(1, getargs_L(BadInt2()))
264 self.assertEqual(0, getargs_L(BadInt3()))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000265
Thomas Heller54aa5782003-04-24 16:15:29 +0000266 self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000267 self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN))
268 self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX))
Thomas Heller54aa5782003-04-24 16:15:29 +0000269 self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1)
Thomas Hellera4ea6032003-04-17 18:55:45 +0000270
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000271 self.assertEqual(42, getargs_L(42))
Thomas Heller54aa5782003-04-24 16:15:29 +0000272 self.assertRaises(OverflowError, getargs_L, VERY_LARGE)
Tim Peters0eadaac2003-04-24 16:02:54 +0000273
Thomas Hellera4ea6032003-04-17 18:55:45 +0000274 def test_K(self):
Thomas Heller54aa5782003-04-24 16:15:29 +0000275 from _testcapi import getargs_K
Thomas Hellera4ea6032003-04-17 18:55:45 +0000276 # K return 'unsigned long long', no range checking
Thomas Heller54aa5782003-04-24 16:15:29 +0000277 self.assertRaises(TypeError, getargs_K, 3.14)
Thomas Heller54aa5782003-04-24 16:15:29 +0000278 self.assertRaises(TypeError, getargs_K, Int())
Serhiy Storchaka31a65542013-12-11 21:07:54 +0200279 self.assertEqual(0, getargs_K(IntSubclass()))
280 self.assertRaises(TypeError, getargs_K, BadInt())
281 self.assertRaises(TypeError, getargs_K, BadInt2())
282 self.assertEqual(0, getargs_K(BadInt3()))
283
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000284 self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX))
285 self.assertEqual(0, getargs_K(0))
286 self.assertEqual(0, getargs_K(ULLONG_MAX+1))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000287
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000288 self.assertEqual(42, getargs_K(42))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000289
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000290 self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000291
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700292class Paradox:
293 "This statement is false."
294 def __bool__(self):
295 raise NotImplementedError
296
297class Boolean_TestCase(unittest.TestCase):
298 def test_p(self):
299 from _testcapi import getargs_p
300 self.assertEqual(0, getargs_p(False))
301 self.assertEqual(0, getargs_p(None))
302 self.assertEqual(0, getargs_p(0))
303 self.assertEqual(0, getargs_p(0.0))
304 self.assertEqual(0, getargs_p(0j))
305 self.assertEqual(0, getargs_p(''))
306 self.assertEqual(0, getargs_p(()))
307 self.assertEqual(0, getargs_p([]))
308 self.assertEqual(0, getargs_p({}))
309
310 self.assertEqual(1, getargs_p(True))
311 self.assertEqual(1, getargs_p(1))
312 self.assertEqual(1, getargs_p(1.0))
313 self.assertEqual(1, getargs_p(1j))
314 self.assertEqual(1, getargs_p('x'))
315 self.assertEqual(1, getargs_p((1,)))
316 self.assertEqual(1, getargs_p([1]))
317 self.assertEqual(1, getargs_p({1:2}))
318 self.assertEqual(1, getargs_p(unittest.TestCase))
319
320 self.assertRaises(NotImplementedError, getargs_p, Paradox())
321
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000322
323class Tuple_TestCase(unittest.TestCase):
324 def test_tuple(self):
325 from _testcapi import getargs_tuple
326
327 ret = getargs_tuple(1, (2, 3))
Ezio Melottib3aedd42010-11-20 19:04:17 +0000328 self.assertEqual(ret, (1,2,3))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000329
330 # make sure invalid tuple arguments are handled correctly
331 class seq:
332 def __len__(self):
333 return 2
334 def __getitem__(self, n):
335 raise ValueError
336 self.assertRaises(TypeError, getargs_tuple, 1, seq())
337
Christian Heimes380f7f22008-02-28 11:19:05 +0000338class Keywords_TestCase(unittest.TestCase):
339 def test_positional_args(self):
340 # using all positional args
Ezio Melottib3aedd42010-11-20 19:04:17 +0000341 self.assertEqual(
Christian Heimes380f7f22008-02-28 11:19:05 +0000342 getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10),
343 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
344 )
Victor Stinner93b55132010-05-19 00:54:06 +0000345
Christian Heimes380f7f22008-02-28 11:19:05 +0000346 def test_mixed_args(self):
347 # positional and keyword args
Ezio Melottib3aedd42010-11-20 19:04:17 +0000348 self.assertEqual(
Christian Heimes380f7f22008-02-28 11:19:05 +0000349 getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10),
350 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
351 )
Victor Stinner93b55132010-05-19 00:54:06 +0000352
Christian Heimes380f7f22008-02-28 11:19:05 +0000353 def test_keyword_args(self):
354 # all keywords
Ezio Melottib3aedd42010-11-20 19:04:17 +0000355 self.assertEqual(
Christian Heimes380f7f22008-02-28 11:19:05 +0000356 getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10),
357 (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
358 )
Victor Stinner93b55132010-05-19 00:54:06 +0000359
Christian Heimes380f7f22008-02-28 11:19:05 +0000360 def test_optional_args(self):
361 # missing optional keyword args, skipping tuples
Ezio Melottib3aedd42010-11-20 19:04:17 +0000362 self.assertEqual(
Christian Heimes380f7f22008-02-28 11:19:05 +0000363 getargs_keywords(arg1=(1,2), arg2=3, arg5=10),
364 (1, 2, 3, -1, -1, -1, -1, -1, -1, 10)
365 )
Victor Stinner93b55132010-05-19 00:54:06 +0000366
Christian Heimes380f7f22008-02-28 11:19:05 +0000367 def test_required_args(self):
368 # required arg missing
369 try:
370 getargs_keywords(arg1=(1,2))
371 except TypeError as err:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000372 self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found")
Christian Heimes380f7f22008-02-28 11:19:05 +0000373 else:
374 self.fail('TypeError should have been raised')
Victor Stinner93b55132010-05-19 00:54:06 +0000375
Christian Heimes380f7f22008-02-28 11:19:05 +0000376 def test_too_many_args(self):
377 try:
378 getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111)
379 except TypeError as err:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000380 self.assertEqual(str(err), "function takes at most 5 arguments (6 given)")
Christian Heimes380f7f22008-02-28 11:19:05 +0000381 else:
382 self.fail('TypeError should have been raised')
Victor Stinner93b55132010-05-19 00:54:06 +0000383
Christian Heimes380f7f22008-02-28 11:19:05 +0000384 def test_invalid_keyword(self):
385 # extraneous keyword arg
386 try:
387 getargs_keywords((1,2),3,arg5=10,arg666=666)
388 except TypeError as err:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000389 self.assertEqual(str(err), "'arg666' is an invalid keyword argument for this function")
Christian Heimes380f7f22008-02-28 11:19:05 +0000390 else:
391 self.fail('TypeError should have been raised')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000392
Victor Stinner93b55132010-05-19 00:54:06 +0000393 def test_surrogate_keyword(self):
394 try:
395 getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10})
396 except TypeError as err:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000397 self.assertEqual(str(err), "'\udc80' is an invalid keyword argument for this function")
Victor Stinner93b55132010-05-19 00:54:06 +0000398 else:
399 self.fail('TypeError should have been raised')
400
Larry Hastings83a9f482012-03-20 20:06:16 +0000401class KeywordOnly_TestCase(unittest.TestCase):
402 def test_positional_args(self):
403 # using all possible positional args
404 self.assertEqual(
405 getargs_keyword_only(1, 2),
406 (1, 2, -1)
407 )
408
409 def test_mixed_args(self):
410 # positional and keyword args
411 self.assertEqual(
412 getargs_keyword_only(1, 2, keyword_only=3),
413 (1, 2, 3)
414 )
415
416 def test_keyword_args(self):
417 # all keywords
418 self.assertEqual(
419 getargs_keyword_only(required=1, optional=2, keyword_only=3),
420 (1, 2, 3)
421 )
422
423 def test_optional_args(self):
424 # missing optional keyword args, skipping tuples
425 self.assertEqual(
426 getargs_keyword_only(required=1, optional=2),
427 (1, 2, -1)
428 )
429 self.assertEqual(
430 getargs_keyword_only(required=1, keyword_only=3),
431 (1, -1, 3)
432 )
433
434 def test_required_args(self):
435 self.assertEqual(
436 getargs_keyword_only(1),
437 (1, -1, -1)
438 )
439 self.assertEqual(
440 getargs_keyword_only(required=1),
441 (1, -1, -1)
442 )
443 # required arg missing
444 with self.assertRaisesRegex(TypeError,
445 "Required argument 'required' \(pos 1\) not found"):
446 getargs_keyword_only(optional=2)
447
448 with self.assertRaisesRegex(TypeError,
449 "Required argument 'required' \(pos 1\) not found"):
450 getargs_keyword_only(keyword_only=3)
451
452 def test_too_many_args(self):
453 with self.assertRaisesRegex(TypeError,
454 "Function takes at most 2 positional arguments \(3 given\)"):
455 getargs_keyword_only(1, 2, 3)
456
457 with self.assertRaisesRegex(TypeError,
458 "function takes at most 3 arguments \(4 given\)"):
459 getargs_keyword_only(1, 2, 3, keyword_only=5)
460
461 def test_invalid_keyword(self):
462 # extraneous keyword arg
463 with self.assertRaisesRegex(TypeError,
464 "'monster' is an invalid keyword argument for this function"):
465 getargs_keyword_only(1, 2, monster=666)
466
467 def test_surrogate_keyword(self):
468 with self.assertRaisesRegex(TypeError,
469 "'\udc80' is an invalid keyword argument for this function"):
470 getargs_keyword_only(1, 2, **{'\uDC80': 10})
471
Victor Stinner06e49dd2010-06-13 18:21:50 +0000472class Bytes_TestCase(unittest.TestCase):
Eli Bendersky906b88f2011-07-29 07:05:08 +0300473 def test_c(self):
474 from _testcapi import getargs_c
475 self.assertRaises(TypeError, getargs_c, b'abc') # len > 1
476 self.assertEqual(getargs_c(b'a'), b'a')
477 self.assertEqual(getargs_c(bytearray(b'a')), b'a')
478 self.assertRaises(TypeError, getargs_c, memoryview(b'a'))
479 self.assertRaises(TypeError, getargs_c, 's')
480 self.assertRaises(TypeError, getargs_c, None)
481
Victor Stinner06e49dd2010-06-13 18:21:50 +0000482 def test_s(self):
483 from _testcapi import getargs_s
484 self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9')
485 self.assertRaises(TypeError, getargs_s, 'nul:\0')
486 self.assertRaises(TypeError, getargs_s, b'bytes')
487 self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray'))
488 self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview'))
489 self.assertRaises(TypeError, getargs_s, None)
490
491 def test_s_star(self):
492 from _testcapi import getargs_s_star
493 self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9')
494 self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0')
495 self.assertEqual(getargs_s_star(b'bytes'), b'bytes')
496 self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray')
497 self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview')
498 self.assertRaises(TypeError, getargs_s_star, None)
499
500 def test_s_hash(self):
501 from _testcapi import getargs_s_hash
502 self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9')
503 self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0')
504 self.assertEqual(getargs_s_hash(b'bytes'), b'bytes')
505 self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray'))
506 self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview'))
507 self.assertRaises(TypeError, getargs_s_hash, None)
508
509 def test_z(self):
510 from _testcapi import getargs_z
511 self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9')
512 self.assertRaises(TypeError, getargs_z, 'nul:\0')
Victor Stinner4aae1eb2010-06-24 22:08:25 +0000513 self.assertRaises(TypeError, getargs_z, b'bytes')
Victor Stinner06e49dd2010-06-13 18:21:50 +0000514 self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray'))
515 self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview'))
516 self.assertIsNone(getargs_z(None))
517
518 def test_z_star(self):
519 from _testcapi import getargs_z_star
520 self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9')
521 self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0')
522 self.assertEqual(getargs_z_star(b'bytes'), b'bytes')
523 self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray')
524 self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview')
525 self.assertIsNone(getargs_z_star(None))
526
527 def test_z_hash(self):
528 from _testcapi import getargs_z_hash
529 self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9')
530 self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0')
531 self.assertEqual(getargs_z_hash(b'bytes'), b'bytes')
532 self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray'))
533 self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview'))
534 self.assertIsNone(getargs_z_hash(None))
535
536 def test_y(self):
537 from _testcapi import getargs_y
538 self.assertRaises(TypeError, getargs_y, 'abc\xe9')
539 self.assertEqual(getargs_y(b'bytes'), b'bytes')
540 self.assertRaises(TypeError, getargs_y, b'nul:\0')
541 self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray'))
542 self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview'))
543 self.assertRaises(TypeError, getargs_y, None)
544
545 def test_y_star(self):
546 from _testcapi import getargs_y_star
547 self.assertRaises(TypeError, getargs_y_star, 'abc\xe9')
548 self.assertEqual(getargs_y_star(b'bytes'), b'bytes')
549 self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0')
550 self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray')
551 self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview')
552 self.assertRaises(TypeError, getargs_y_star, None)
553
554 def test_y_hash(self):
555 from _testcapi import getargs_y_hash
556 self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9')
557 self.assertEqual(getargs_y_hash(b'bytes'), b'bytes')
558 self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0')
559 self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray'))
560 self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview'))
561 self.assertRaises(TypeError, getargs_y_hash, None)
562
Victor Stinner25e8ec42010-06-25 00:02:38 +0000563 def test_w_star(self):
564 # getargs_w_star() modifies first and last byte
565 from _testcapi import getargs_w_star
566 self.assertRaises(TypeError, getargs_w_star, 'abc\xe9')
567 self.assertRaises(TypeError, getargs_w_star, b'bytes')
568 self.assertRaises(TypeError, getargs_w_star, b'nul:\0')
Antoine Pitrouad62b032011-01-18 18:57:52 +0000569 self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes'))
Victor Stinner25e8ec42010-06-25 00:02:38 +0000570 self.assertEqual(getargs_w_star(bytearray(b'bytearray')), b'[ytearra]')
Antoine Pitrouad62b032011-01-18 18:57:52 +0000571 self.assertEqual(getargs_w_star(memoryview(bytearray(b'memoryview'))),
572 b'[emoryvie]')
Victor Stinner25e8ec42010-06-25 00:02:38 +0000573 self.assertRaises(TypeError, getargs_w_star, None)
574
Victor Stinner06e49dd2010-06-13 18:21:50 +0000575
576class Unicode_TestCase(unittest.TestCase):
577 def test_u(self):
578 from _testcapi import getargs_u
579 self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9')
580 self.assertRaises(TypeError, getargs_u, 'nul:\0')
581 self.assertRaises(TypeError, getargs_u, b'bytes')
582 self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray'))
583 self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview'))
584 self.assertRaises(TypeError, getargs_u, None)
585
586 def test_u_hash(self):
587 from _testcapi import getargs_u_hash
588 self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9')
589 self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0')
590 self.assertRaises(TypeError, getargs_u_hash, b'bytes')
591 self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray'))
592 self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview'))
593 self.assertRaises(TypeError, getargs_u_hash, None)
594
595 def test_Z(self):
596 from _testcapi import getargs_Z
597 self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9')
598 self.assertRaises(TypeError, getargs_Z, 'nul:\0')
599 self.assertRaises(TypeError, getargs_Z, b'bytes')
600 self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray'))
601 self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview'))
602 self.assertIsNone(getargs_Z(None))
603
604 def test_Z_hash(self):
605 from _testcapi import getargs_Z_hash
606 self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9')
607 self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0')
608 self.assertRaises(TypeError, getargs_Z_hash, b'bytes')
609 self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray'))
610 self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview'))
611 self.assertIsNone(getargs_Z_hash(None))
612
613
Thomas Hellera4ea6032003-04-17 18:55:45 +0000614if __name__ == "__main__":
Brett Cannon3e9a9ae2013-06-12 21:25:59 -0400615 unittest.main()