1 | n/a | import unittest |
---|
2 | n/a | import math |
---|
3 | n/a | import sys |
---|
4 | n/a | from test import support |
---|
5 | n/a | # Skip this test if the _testcapi module isn't available. |
---|
6 | n/a | support.import_module('_testcapi') |
---|
7 | n/a | from _testcapi import getargs_keywords, getargs_keyword_only |
---|
8 | n/a | |
---|
9 | n/a | # > How about the following counterproposal. This also changes some of |
---|
10 | n/a | # > the other format codes to be a little more regular. |
---|
11 | n/a | # > |
---|
12 | n/a | # > Code C type Range check |
---|
13 | n/a | # > |
---|
14 | n/a | # > b unsigned char 0..UCHAR_MAX |
---|
15 | n/a | # > h signed short SHRT_MIN..SHRT_MAX |
---|
16 | n/a | # > B unsigned char none ** |
---|
17 | n/a | # > H unsigned short none ** |
---|
18 | n/a | # > k * unsigned long none |
---|
19 | n/a | # > I * unsigned int 0..UINT_MAX |
---|
20 | n/a | # |
---|
21 | n/a | # |
---|
22 | n/a | # > i int INT_MIN..INT_MAX |
---|
23 | n/a | # > l long LONG_MIN..LONG_MAX |
---|
24 | n/a | # |
---|
25 | n/a | # > K * unsigned long long none |
---|
26 | n/a | # > L long long LLONG_MIN..LLONG_MAX |
---|
27 | n/a | # |
---|
28 | n/a | # > Notes: |
---|
29 | n/a | # > |
---|
30 | n/a | # > * New format codes. |
---|
31 | n/a | # > |
---|
32 | n/a | # > ** Changed from previous "range-and-a-half" to "none"; the |
---|
33 | n/a | # > range-and-a-half checking wasn't particularly useful. |
---|
34 | n/a | # |
---|
35 | n/a | # Plus a C API or two, e.g. PyLong_AsUnsignedLongMask() -> |
---|
36 | n/a | # unsigned long and PyLong_AsUnsignedLongLongMask() -> unsigned |
---|
37 | n/a | # long long (if that exists). |
---|
38 | n/a | |
---|
39 | n/a | LARGE = 0x7FFFFFFF |
---|
40 | n/a | VERY_LARGE = 0xFF0000121212121212121242 |
---|
41 | n/a | |
---|
42 | n/a | from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
---|
43 | n/a | INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
---|
44 | n/a | SHRT_MIN, SHRT_MAX, FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX |
---|
45 | n/a | |
---|
46 | n/a | DBL_MAX_EXP = sys.float_info.max_exp |
---|
47 | n/a | INF = float('inf') |
---|
48 | n/a | NAN = float('nan') |
---|
49 | n/a | |
---|
50 | n/a | # fake, they are not defined in Python's header files |
---|
51 | n/a | LLONG_MAX = 2**63-1 |
---|
52 | n/a | LLONG_MIN = -2**63 |
---|
53 | n/a | ULLONG_MAX = 2**64-1 |
---|
54 | n/a | |
---|
55 | n/a | class Int: |
---|
56 | n/a | def __int__(self): |
---|
57 | n/a | return 99 |
---|
58 | n/a | |
---|
59 | n/a | class IntSubclass(int): |
---|
60 | n/a | def __int__(self): |
---|
61 | n/a | return 99 |
---|
62 | n/a | |
---|
63 | n/a | class BadInt: |
---|
64 | n/a | def __int__(self): |
---|
65 | n/a | return 1.0 |
---|
66 | n/a | |
---|
67 | n/a | class BadInt2: |
---|
68 | n/a | def __int__(self): |
---|
69 | n/a | return True |
---|
70 | n/a | |
---|
71 | n/a | class BadInt3(int): |
---|
72 | n/a | def __int__(self): |
---|
73 | n/a | return True |
---|
74 | n/a | |
---|
75 | n/a | |
---|
76 | n/a | class Float: |
---|
77 | n/a | def __float__(self): |
---|
78 | n/a | return 4.25 |
---|
79 | n/a | |
---|
80 | n/a | class FloatSubclass(float): |
---|
81 | n/a | pass |
---|
82 | n/a | |
---|
83 | n/a | class FloatSubclass2(float): |
---|
84 | n/a | def __float__(self): |
---|
85 | n/a | return 4.25 |
---|
86 | n/a | |
---|
87 | n/a | class BadFloat: |
---|
88 | n/a | def __float__(self): |
---|
89 | n/a | return 687 |
---|
90 | n/a | |
---|
91 | n/a | class BadFloat2: |
---|
92 | n/a | def __float__(self): |
---|
93 | n/a | return FloatSubclass(4.25) |
---|
94 | n/a | |
---|
95 | n/a | class BadFloat3(float): |
---|
96 | n/a | def __float__(self): |
---|
97 | n/a | return FloatSubclass(4.25) |
---|
98 | n/a | |
---|
99 | n/a | |
---|
100 | n/a | class Complex: |
---|
101 | n/a | def __complex__(self): |
---|
102 | n/a | return 4.25+0.5j |
---|
103 | n/a | |
---|
104 | n/a | class ComplexSubclass(complex): |
---|
105 | n/a | pass |
---|
106 | n/a | |
---|
107 | n/a | class ComplexSubclass2(complex): |
---|
108 | n/a | def __complex__(self): |
---|
109 | n/a | return 4.25+0.5j |
---|
110 | n/a | |
---|
111 | n/a | class BadComplex: |
---|
112 | n/a | def __complex__(self): |
---|
113 | n/a | return 1.25 |
---|
114 | n/a | |
---|
115 | n/a | class BadComplex2: |
---|
116 | n/a | def __complex__(self): |
---|
117 | n/a | return ComplexSubclass(4.25+0.5j) |
---|
118 | n/a | |
---|
119 | n/a | class BadComplex3(complex): |
---|
120 | n/a | def __complex__(self): |
---|
121 | n/a | return ComplexSubclass(4.25+0.5j) |
---|
122 | n/a | |
---|
123 | n/a | |
---|
124 | n/a | class TupleSubclass(tuple): |
---|
125 | n/a | pass |
---|
126 | n/a | |
---|
127 | n/a | class DictSubclass(dict): |
---|
128 | n/a | pass |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class Unsigned_TestCase(unittest.TestCase): |
---|
132 | n/a | def test_b(self): |
---|
133 | n/a | from _testcapi import getargs_b |
---|
134 | n/a | # b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
---|
135 | n/a | self.assertRaises(TypeError, getargs_b, 3.14) |
---|
136 | n/a | self.assertEqual(99, getargs_b(Int())) |
---|
137 | n/a | self.assertEqual(0, getargs_b(IntSubclass())) |
---|
138 | n/a | self.assertRaises(TypeError, getargs_b, BadInt()) |
---|
139 | n/a | with self.assertWarns(DeprecationWarning): |
---|
140 | n/a | self.assertEqual(1, getargs_b(BadInt2())) |
---|
141 | n/a | self.assertEqual(0, getargs_b(BadInt3())) |
---|
142 | n/a | |
---|
143 | n/a | self.assertRaises(OverflowError, getargs_b, -1) |
---|
144 | n/a | self.assertEqual(0, getargs_b(0)) |
---|
145 | n/a | self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
---|
146 | n/a | self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
---|
147 | n/a | |
---|
148 | n/a | self.assertEqual(42, getargs_b(42)) |
---|
149 | n/a | self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
---|
150 | n/a | |
---|
151 | n/a | def test_B(self): |
---|
152 | n/a | from _testcapi import getargs_B |
---|
153 | n/a | # B returns 'unsigned char', no range checking |
---|
154 | n/a | self.assertRaises(TypeError, getargs_B, 3.14) |
---|
155 | n/a | self.assertEqual(99, getargs_B(Int())) |
---|
156 | n/a | self.assertEqual(0, getargs_B(IntSubclass())) |
---|
157 | n/a | self.assertRaises(TypeError, getargs_B, BadInt()) |
---|
158 | n/a | with self.assertWarns(DeprecationWarning): |
---|
159 | n/a | self.assertEqual(1, getargs_B(BadInt2())) |
---|
160 | n/a | self.assertEqual(0, getargs_B(BadInt3())) |
---|
161 | n/a | |
---|
162 | n/a | self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
---|
163 | n/a | self.assertEqual(0, getargs_B(0)) |
---|
164 | n/a | self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
---|
165 | n/a | self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
---|
166 | n/a | |
---|
167 | n/a | self.assertEqual(42, getargs_B(42)) |
---|
168 | n/a | self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
---|
169 | n/a | |
---|
170 | n/a | def test_H(self): |
---|
171 | n/a | from _testcapi import getargs_H |
---|
172 | n/a | # H returns 'unsigned short', no range checking |
---|
173 | n/a | self.assertRaises(TypeError, getargs_H, 3.14) |
---|
174 | n/a | self.assertEqual(99, getargs_H(Int())) |
---|
175 | n/a | self.assertEqual(0, getargs_H(IntSubclass())) |
---|
176 | n/a | self.assertRaises(TypeError, getargs_H, BadInt()) |
---|
177 | n/a | with self.assertWarns(DeprecationWarning): |
---|
178 | n/a | self.assertEqual(1, getargs_H(BadInt2())) |
---|
179 | n/a | self.assertEqual(0, getargs_H(BadInt3())) |
---|
180 | n/a | |
---|
181 | n/a | self.assertEqual(USHRT_MAX, getargs_H(-1)) |
---|
182 | n/a | self.assertEqual(0, getargs_H(0)) |
---|
183 | n/a | self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
---|
184 | n/a | self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
---|
185 | n/a | |
---|
186 | n/a | self.assertEqual(42, getargs_H(42)) |
---|
187 | n/a | |
---|
188 | n/a | self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
---|
189 | n/a | |
---|
190 | n/a | def test_I(self): |
---|
191 | n/a | from _testcapi import getargs_I |
---|
192 | n/a | # I returns 'unsigned int', no range checking |
---|
193 | n/a | self.assertRaises(TypeError, getargs_I, 3.14) |
---|
194 | n/a | self.assertEqual(99, getargs_I(Int())) |
---|
195 | n/a | self.assertEqual(0, getargs_I(IntSubclass())) |
---|
196 | n/a | self.assertRaises(TypeError, getargs_I, BadInt()) |
---|
197 | n/a | with self.assertWarns(DeprecationWarning): |
---|
198 | n/a | self.assertEqual(1, getargs_I(BadInt2())) |
---|
199 | n/a | self.assertEqual(0, getargs_I(BadInt3())) |
---|
200 | n/a | |
---|
201 | n/a | self.assertEqual(UINT_MAX, getargs_I(-1)) |
---|
202 | n/a | self.assertEqual(0, getargs_I(0)) |
---|
203 | n/a | self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
---|
204 | n/a | self.assertEqual(0, getargs_I(UINT_MAX+1)) |
---|
205 | n/a | |
---|
206 | n/a | self.assertEqual(42, getargs_I(42)) |
---|
207 | n/a | |
---|
208 | n/a | self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
---|
209 | n/a | |
---|
210 | n/a | def test_k(self): |
---|
211 | n/a | from _testcapi import getargs_k |
---|
212 | n/a | # k returns 'unsigned long', no range checking |
---|
213 | n/a | # it does not accept float, or instances with __int__ |
---|
214 | n/a | self.assertRaises(TypeError, getargs_k, 3.14) |
---|
215 | n/a | self.assertRaises(TypeError, getargs_k, Int()) |
---|
216 | n/a | self.assertEqual(0, getargs_k(IntSubclass())) |
---|
217 | n/a | self.assertRaises(TypeError, getargs_k, BadInt()) |
---|
218 | n/a | self.assertRaises(TypeError, getargs_k, BadInt2()) |
---|
219 | n/a | self.assertEqual(0, getargs_k(BadInt3())) |
---|
220 | n/a | |
---|
221 | n/a | self.assertEqual(ULONG_MAX, getargs_k(-1)) |
---|
222 | n/a | self.assertEqual(0, getargs_k(0)) |
---|
223 | n/a | self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
---|
224 | n/a | self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
---|
225 | n/a | |
---|
226 | n/a | self.assertEqual(42, getargs_k(42)) |
---|
227 | n/a | |
---|
228 | n/a | self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
---|
229 | n/a | |
---|
230 | n/a | class Signed_TestCase(unittest.TestCase): |
---|
231 | n/a | def test_h(self): |
---|
232 | n/a | from _testcapi import getargs_h |
---|
233 | n/a | # h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
---|
234 | n/a | self.assertRaises(TypeError, getargs_h, 3.14) |
---|
235 | n/a | self.assertEqual(99, getargs_h(Int())) |
---|
236 | n/a | self.assertEqual(0, getargs_h(IntSubclass())) |
---|
237 | n/a | self.assertRaises(TypeError, getargs_h, BadInt()) |
---|
238 | n/a | with self.assertWarns(DeprecationWarning): |
---|
239 | n/a | self.assertEqual(1, getargs_h(BadInt2())) |
---|
240 | n/a | self.assertEqual(0, getargs_h(BadInt3())) |
---|
241 | n/a | |
---|
242 | n/a | self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
---|
243 | n/a | self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
---|
244 | n/a | self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
---|
245 | n/a | self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
---|
246 | n/a | |
---|
247 | n/a | self.assertEqual(42, getargs_h(42)) |
---|
248 | n/a | self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
---|
249 | n/a | |
---|
250 | n/a | def test_i(self): |
---|
251 | n/a | from _testcapi import getargs_i |
---|
252 | n/a | # i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
---|
253 | n/a | self.assertRaises(TypeError, getargs_i, 3.14) |
---|
254 | n/a | self.assertEqual(99, getargs_i(Int())) |
---|
255 | n/a | self.assertEqual(0, getargs_i(IntSubclass())) |
---|
256 | n/a | self.assertRaises(TypeError, getargs_i, BadInt()) |
---|
257 | n/a | with self.assertWarns(DeprecationWarning): |
---|
258 | n/a | self.assertEqual(1, getargs_i(BadInt2())) |
---|
259 | n/a | self.assertEqual(0, getargs_i(BadInt3())) |
---|
260 | n/a | |
---|
261 | n/a | self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
---|
262 | n/a | self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
---|
263 | n/a | self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
---|
264 | n/a | self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
---|
265 | n/a | |
---|
266 | n/a | self.assertEqual(42, getargs_i(42)) |
---|
267 | n/a | self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
---|
268 | n/a | |
---|
269 | n/a | def test_l(self): |
---|
270 | n/a | from _testcapi import getargs_l |
---|
271 | n/a | # l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
---|
272 | n/a | self.assertRaises(TypeError, getargs_l, 3.14) |
---|
273 | n/a | self.assertEqual(99, getargs_l(Int())) |
---|
274 | n/a | self.assertEqual(0, getargs_l(IntSubclass())) |
---|
275 | n/a | self.assertRaises(TypeError, getargs_l, BadInt()) |
---|
276 | n/a | with self.assertWarns(DeprecationWarning): |
---|
277 | n/a | self.assertEqual(1, getargs_l(BadInt2())) |
---|
278 | n/a | self.assertEqual(0, getargs_l(BadInt3())) |
---|
279 | n/a | |
---|
280 | n/a | self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
---|
281 | n/a | self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
---|
282 | n/a | self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
---|
283 | n/a | self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
---|
284 | n/a | |
---|
285 | n/a | self.assertEqual(42, getargs_l(42)) |
---|
286 | n/a | self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
---|
287 | n/a | |
---|
288 | n/a | def test_n(self): |
---|
289 | n/a | from _testcapi import getargs_n |
---|
290 | n/a | # n returns 'Py_ssize_t', and does range checking |
---|
291 | n/a | # (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
---|
292 | n/a | self.assertRaises(TypeError, getargs_n, 3.14) |
---|
293 | n/a | self.assertRaises(TypeError, getargs_n, Int()) |
---|
294 | n/a | self.assertEqual(0, getargs_n(IntSubclass())) |
---|
295 | n/a | self.assertRaises(TypeError, getargs_n, BadInt()) |
---|
296 | n/a | self.assertRaises(TypeError, getargs_n, BadInt2()) |
---|
297 | n/a | self.assertEqual(0, getargs_n(BadInt3())) |
---|
298 | n/a | |
---|
299 | n/a | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
---|
300 | n/a | self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
---|
301 | n/a | self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
---|
302 | n/a | self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
---|
303 | n/a | |
---|
304 | n/a | self.assertEqual(42, getargs_n(42)) |
---|
305 | n/a | self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
---|
306 | n/a | |
---|
307 | n/a | |
---|
308 | n/a | class LongLong_TestCase(unittest.TestCase): |
---|
309 | n/a | def test_L(self): |
---|
310 | n/a | from _testcapi import getargs_L |
---|
311 | n/a | # L returns 'long long', and does range checking (LLONG_MIN |
---|
312 | n/a | # ... LLONG_MAX) |
---|
313 | n/a | self.assertRaises(TypeError, getargs_L, 3.14) |
---|
314 | n/a | self.assertRaises(TypeError, getargs_L, "Hello") |
---|
315 | n/a | self.assertEqual(99, getargs_L(Int())) |
---|
316 | n/a | self.assertEqual(0, getargs_L(IntSubclass())) |
---|
317 | n/a | self.assertRaises(TypeError, getargs_L, BadInt()) |
---|
318 | n/a | with self.assertWarns(DeprecationWarning): |
---|
319 | n/a | self.assertEqual(1, getargs_L(BadInt2())) |
---|
320 | n/a | self.assertEqual(0, getargs_L(BadInt3())) |
---|
321 | n/a | |
---|
322 | n/a | self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
---|
323 | n/a | self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
---|
324 | n/a | self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
---|
325 | n/a | self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
---|
326 | n/a | |
---|
327 | n/a | self.assertEqual(42, getargs_L(42)) |
---|
328 | n/a | self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
---|
329 | n/a | |
---|
330 | n/a | def test_K(self): |
---|
331 | n/a | from _testcapi import getargs_K |
---|
332 | n/a | # K return 'unsigned long long', no range checking |
---|
333 | n/a | self.assertRaises(TypeError, getargs_K, 3.14) |
---|
334 | n/a | self.assertRaises(TypeError, getargs_K, Int()) |
---|
335 | n/a | self.assertEqual(0, getargs_K(IntSubclass())) |
---|
336 | n/a | self.assertRaises(TypeError, getargs_K, BadInt()) |
---|
337 | n/a | self.assertRaises(TypeError, getargs_K, BadInt2()) |
---|
338 | n/a | self.assertEqual(0, getargs_K(BadInt3())) |
---|
339 | n/a | |
---|
340 | n/a | self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
---|
341 | n/a | self.assertEqual(0, getargs_K(0)) |
---|
342 | n/a | self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
---|
343 | n/a | |
---|
344 | n/a | self.assertEqual(42, getargs_K(42)) |
---|
345 | n/a | |
---|
346 | n/a | self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
---|
347 | n/a | |
---|
348 | n/a | |
---|
349 | n/a | class Float_TestCase(unittest.TestCase): |
---|
350 | n/a | def assertEqualWithSign(self, actual, expected): |
---|
351 | n/a | self.assertEqual(actual, expected) |
---|
352 | n/a | self.assertEqual(math.copysign(1, actual), math.copysign(1, expected)) |
---|
353 | n/a | |
---|
354 | n/a | def test_f(self): |
---|
355 | n/a | from _testcapi import getargs_f |
---|
356 | n/a | self.assertEqual(getargs_f(4.25), 4.25) |
---|
357 | n/a | self.assertEqual(getargs_f(4), 4.0) |
---|
358 | n/a | self.assertRaises(TypeError, getargs_f, 4.25+0j) |
---|
359 | n/a | self.assertEqual(getargs_f(Float()), 4.25) |
---|
360 | n/a | self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5) |
---|
361 | n/a | self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5) |
---|
362 | n/a | self.assertRaises(TypeError, getargs_f, BadFloat()) |
---|
363 | n/a | with self.assertWarns(DeprecationWarning): |
---|
364 | n/a | self.assertEqual(getargs_f(BadFloat2()), 4.25) |
---|
365 | n/a | self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5) |
---|
366 | n/a | |
---|
367 | n/a | for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF): |
---|
368 | n/a | self.assertEqual(getargs_f(x), x) |
---|
369 | n/a | if FLT_MAX < DBL_MAX: |
---|
370 | n/a | self.assertEqual(getargs_f(DBL_MAX), INF) |
---|
371 | n/a | self.assertEqual(getargs_f(-DBL_MAX), -INF) |
---|
372 | n/a | if FLT_MIN > DBL_MIN: |
---|
373 | n/a | self.assertEqualWithSign(getargs_f(DBL_MIN), 0.0) |
---|
374 | n/a | self.assertEqualWithSign(getargs_f(-DBL_MIN), -0.0) |
---|
375 | n/a | self.assertEqualWithSign(getargs_f(0.0), 0.0) |
---|
376 | n/a | self.assertEqualWithSign(getargs_f(-0.0), -0.0) |
---|
377 | n/a | r = getargs_f(NAN) |
---|
378 | n/a | self.assertNotEqual(r, r) |
---|
379 | n/a | |
---|
380 | n/a | def test_d(self): |
---|
381 | n/a | from _testcapi import getargs_d |
---|
382 | n/a | self.assertEqual(getargs_d(4.25), 4.25) |
---|
383 | n/a | self.assertEqual(getargs_d(4), 4.0) |
---|
384 | n/a | self.assertRaises(TypeError, getargs_d, 4.25+0j) |
---|
385 | n/a | self.assertEqual(getargs_d(Float()), 4.25) |
---|
386 | n/a | self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5) |
---|
387 | n/a | self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5) |
---|
388 | n/a | self.assertRaises(TypeError, getargs_d, BadFloat()) |
---|
389 | n/a | with self.assertWarns(DeprecationWarning): |
---|
390 | n/a | self.assertEqual(getargs_d(BadFloat2()), 4.25) |
---|
391 | n/a | self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5) |
---|
392 | n/a | |
---|
393 | n/a | for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
---|
394 | n/a | self.assertEqual(getargs_d(x), x) |
---|
395 | n/a | self.assertRaises(OverflowError, getargs_d, 1<<DBL_MAX_EXP) |
---|
396 | n/a | self.assertRaises(OverflowError, getargs_d, -1<<DBL_MAX_EXP) |
---|
397 | n/a | self.assertEqualWithSign(getargs_d(0.0), 0.0) |
---|
398 | n/a | self.assertEqualWithSign(getargs_d(-0.0), -0.0) |
---|
399 | n/a | r = getargs_d(NAN) |
---|
400 | n/a | self.assertNotEqual(r, r) |
---|
401 | n/a | |
---|
402 | n/a | def test_D(self): |
---|
403 | n/a | from _testcapi import getargs_D |
---|
404 | n/a | self.assertEqual(getargs_D(4.25+0.5j), 4.25+0.5j) |
---|
405 | n/a | self.assertEqual(getargs_D(4.25), 4.25+0j) |
---|
406 | n/a | self.assertEqual(getargs_D(4), 4.0+0j) |
---|
407 | n/a | self.assertEqual(getargs_D(Complex()), 4.25+0.5j) |
---|
408 | n/a | self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j) |
---|
409 | n/a | self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j) |
---|
410 | n/a | self.assertRaises(TypeError, getargs_D, BadComplex()) |
---|
411 | n/a | self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j) |
---|
412 | n/a | self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j) |
---|
413 | n/a | |
---|
414 | n/a | for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
---|
415 | n/a | c = complex(x, 1.0) |
---|
416 | n/a | self.assertEqual(getargs_D(c), c) |
---|
417 | n/a | c = complex(1.0, x) |
---|
418 | n/a | self.assertEqual(getargs_D(c), c) |
---|
419 | n/a | self.assertEqualWithSign(getargs_D(complex(0.0, 1.0)).real, 0.0) |
---|
420 | n/a | self.assertEqualWithSign(getargs_D(complex(-0.0, 1.0)).real, -0.0) |
---|
421 | n/a | self.assertEqualWithSign(getargs_D(complex(1.0, 0.0)).imag, 0.0) |
---|
422 | n/a | self.assertEqualWithSign(getargs_D(complex(1.0, -0.0)).imag, -0.0) |
---|
423 | n/a | |
---|
424 | n/a | |
---|
425 | n/a | class Paradox: |
---|
426 | n/a | "This statement is false." |
---|
427 | n/a | def __bool__(self): |
---|
428 | n/a | raise NotImplementedError |
---|
429 | n/a | |
---|
430 | n/a | class Boolean_TestCase(unittest.TestCase): |
---|
431 | n/a | def test_p(self): |
---|
432 | n/a | from _testcapi import getargs_p |
---|
433 | n/a | self.assertEqual(0, getargs_p(False)) |
---|
434 | n/a | self.assertEqual(0, getargs_p(None)) |
---|
435 | n/a | self.assertEqual(0, getargs_p(0)) |
---|
436 | n/a | self.assertEqual(0, getargs_p(0.0)) |
---|
437 | n/a | self.assertEqual(0, getargs_p(0j)) |
---|
438 | n/a | self.assertEqual(0, getargs_p('')) |
---|
439 | n/a | self.assertEqual(0, getargs_p(())) |
---|
440 | n/a | self.assertEqual(0, getargs_p([])) |
---|
441 | n/a | self.assertEqual(0, getargs_p({})) |
---|
442 | n/a | |
---|
443 | n/a | self.assertEqual(1, getargs_p(True)) |
---|
444 | n/a | self.assertEqual(1, getargs_p(1)) |
---|
445 | n/a | self.assertEqual(1, getargs_p(1.0)) |
---|
446 | n/a | self.assertEqual(1, getargs_p(1j)) |
---|
447 | n/a | self.assertEqual(1, getargs_p('x')) |
---|
448 | n/a | self.assertEqual(1, getargs_p((1,))) |
---|
449 | n/a | self.assertEqual(1, getargs_p([1])) |
---|
450 | n/a | self.assertEqual(1, getargs_p({1:2})) |
---|
451 | n/a | self.assertEqual(1, getargs_p(unittest.TestCase)) |
---|
452 | n/a | |
---|
453 | n/a | self.assertRaises(NotImplementedError, getargs_p, Paradox()) |
---|
454 | n/a | |
---|
455 | n/a | |
---|
456 | n/a | class Tuple_TestCase(unittest.TestCase): |
---|
457 | n/a | def test_args(self): |
---|
458 | n/a | from _testcapi import get_args |
---|
459 | n/a | |
---|
460 | n/a | ret = get_args(1, 2) |
---|
461 | n/a | self.assertEqual(ret, (1, 2)) |
---|
462 | n/a | self.assertIs(type(ret), tuple) |
---|
463 | n/a | |
---|
464 | n/a | ret = get_args(1, *(2, 3)) |
---|
465 | n/a | self.assertEqual(ret, (1, 2, 3)) |
---|
466 | n/a | self.assertIs(type(ret), tuple) |
---|
467 | n/a | |
---|
468 | n/a | ret = get_args(*[1, 2]) |
---|
469 | n/a | self.assertEqual(ret, (1, 2)) |
---|
470 | n/a | self.assertIs(type(ret), tuple) |
---|
471 | n/a | |
---|
472 | n/a | ret = get_args(*TupleSubclass([1, 2])) |
---|
473 | n/a | self.assertEqual(ret, (1, 2)) |
---|
474 | n/a | self.assertIs(type(ret), tuple) |
---|
475 | n/a | |
---|
476 | n/a | ret = get_args() |
---|
477 | n/a | self.assertIn(ret, ((), None)) |
---|
478 | n/a | self.assertIn(type(ret), (tuple, type(None))) |
---|
479 | n/a | |
---|
480 | n/a | ret = get_args(*()) |
---|
481 | n/a | self.assertIn(ret, ((), None)) |
---|
482 | n/a | self.assertIn(type(ret), (tuple, type(None))) |
---|
483 | n/a | |
---|
484 | n/a | def test_tuple(self): |
---|
485 | n/a | from _testcapi import getargs_tuple |
---|
486 | n/a | |
---|
487 | n/a | ret = getargs_tuple(1, (2, 3)) |
---|
488 | n/a | self.assertEqual(ret, (1,2,3)) |
---|
489 | n/a | |
---|
490 | n/a | # make sure invalid tuple arguments are handled correctly |
---|
491 | n/a | class seq: |
---|
492 | n/a | def __len__(self): |
---|
493 | n/a | return 2 |
---|
494 | n/a | def __getitem__(self, n): |
---|
495 | n/a | raise ValueError |
---|
496 | n/a | self.assertRaises(TypeError, getargs_tuple, 1, seq()) |
---|
497 | n/a | |
---|
498 | n/a | class Keywords_TestCase(unittest.TestCase): |
---|
499 | n/a | def test_kwargs(self): |
---|
500 | n/a | from _testcapi import get_kwargs |
---|
501 | n/a | |
---|
502 | n/a | ret = get_kwargs(a=1, b=2) |
---|
503 | n/a | self.assertEqual(ret, {'a': 1, 'b': 2}) |
---|
504 | n/a | self.assertIs(type(ret), dict) |
---|
505 | n/a | |
---|
506 | n/a | ret = get_kwargs(a=1, **{'b': 2, 'c': 3}) |
---|
507 | n/a | self.assertEqual(ret, {'a': 1, 'b': 2, 'c': 3}) |
---|
508 | n/a | self.assertIs(type(ret), dict) |
---|
509 | n/a | |
---|
510 | n/a | ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2})) |
---|
511 | n/a | self.assertEqual(ret, {'a': 1, 'b': 2}) |
---|
512 | n/a | self.assertIs(type(ret), dict) |
---|
513 | n/a | |
---|
514 | n/a | ret = get_kwargs() |
---|
515 | n/a | self.assertIn(ret, ({}, None)) |
---|
516 | n/a | self.assertIn(type(ret), (dict, type(None))) |
---|
517 | n/a | |
---|
518 | n/a | ret = get_kwargs(**{}) |
---|
519 | n/a | self.assertIn(ret, ({}, None)) |
---|
520 | n/a | self.assertIn(type(ret), (dict, type(None))) |
---|
521 | n/a | |
---|
522 | n/a | def test_positional_args(self): |
---|
523 | n/a | # using all positional args |
---|
524 | n/a | self.assertEqual( |
---|
525 | n/a | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
---|
526 | n/a | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
---|
527 | n/a | ) |
---|
528 | n/a | |
---|
529 | n/a | def test_mixed_args(self): |
---|
530 | n/a | # positional and keyword args |
---|
531 | n/a | self.assertEqual( |
---|
532 | n/a | getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
---|
533 | n/a | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
---|
534 | n/a | ) |
---|
535 | n/a | |
---|
536 | n/a | def test_keyword_args(self): |
---|
537 | n/a | # all keywords |
---|
538 | n/a | self.assertEqual( |
---|
539 | n/a | getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
---|
540 | n/a | (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
---|
541 | n/a | ) |
---|
542 | n/a | |
---|
543 | n/a | def test_optional_args(self): |
---|
544 | n/a | # missing optional keyword args, skipping tuples |
---|
545 | n/a | self.assertEqual( |
---|
546 | n/a | getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
---|
547 | n/a | (1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
---|
548 | n/a | ) |
---|
549 | n/a | |
---|
550 | n/a | def test_required_args(self): |
---|
551 | n/a | # required arg missing |
---|
552 | n/a | try: |
---|
553 | n/a | getargs_keywords(arg1=(1,2)) |
---|
554 | n/a | except TypeError as err: |
---|
555 | n/a | self.assertEqual(str(err), "Required argument 'arg2' (pos 2) not found") |
---|
556 | n/a | else: |
---|
557 | n/a | self.fail('TypeError should have been raised') |
---|
558 | n/a | |
---|
559 | n/a | def test_too_many_args(self): |
---|
560 | n/a | try: |
---|
561 | n/a | getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
---|
562 | n/a | except TypeError as err: |
---|
563 | n/a | self.assertEqual(str(err), "function takes at most 5 arguments (6 given)") |
---|
564 | n/a | else: |
---|
565 | n/a | self.fail('TypeError should have been raised') |
---|
566 | n/a | |
---|
567 | n/a | def test_invalid_keyword(self): |
---|
568 | n/a | # extraneous keyword arg |
---|
569 | n/a | try: |
---|
570 | n/a | getargs_keywords((1,2),3,arg5=10,arg666=666) |
---|
571 | n/a | except TypeError as err: |
---|
572 | n/a | self.assertEqual(str(err), "'arg666' is an invalid keyword argument for this function") |
---|
573 | n/a | else: |
---|
574 | n/a | self.fail('TypeError should have been raised') |
---|
575 | n/a | |
---|
576 | n/a | def test_surrogate_keyword(self): |
---|
577 | n/a | try: |
---|
578 | n/a | getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
---|
579 | n/a | except TypeError as err: |
---|
580 | n/a | self.assertEqual(str(err), "'\udc80' is an invalid keyword argument for this function") |
---|
581 | n/a | else: |
---|
582 | n/a | self.fail('TypeError should have been raised') |
---|
583 | n/a | |
---|
584 | n/a | class KeywordOnly_TestCase(unittest.TestCase): |
---|
585 | n/a | def test_positional_args(self): |
---|
586 | n/a | # using all possible positional args |
---|
587 | n/a | self.assertEqual( |
---|
588 | n/a | getargs_keyword_only(1, 2), |
---|
589 | n/a | (1, 2, -1) |
---|
590 | n/a | ) |
---|
591 | n/a | |
---|
592 | n/a | def test_mixed_args(self): |
---|
593 | n/a | # positional and keyword args |
---|
594 | n/a | self.assertEqual( |
---|
595 | n/a | getargs_keyword_only(1, 2, keyword_only=3), |
---|
596 | n/a | (1, 2, 3) |
---|
597 | n/a | ) |
---|
598 | n/a | |
---|
599 | n/a | def test_keyword_args(self): |
---|
600 | n/a | # all keywords |
---|
601 | n/a | self.assertEqual( |
---|
602 | n/a | getargs_keyword_only(required=1, optional=2, keyword_only=3), |
---|
603 | n/a | (1, 2, 3) |
---|
604 | n/a | ) |
---|
605 | n/a | |
---|
606 | n/a | def test_optional_args(self): |
---|
607 | n/a | # missing optional keyword args, skipping tuples |
---|
608 | n/a | self.assertEqual( |
---|
609 | n/a | getargs_keyword_only(required=1, optional=2), |
---|
610 | n/a | (1, 2, -1) |
---|
611 | n/a | ) |
---|
612 | n/a | self.assertEqual( |
---|
613 | n/a | getargs_keyword_only(required=1, keyword_only=3), |
---|
614 | n/a | (1, -1, 3) |
---|
615 | n/a | ) |
---|
616 | n/a | |
---|
617 | n/a | def test_required_args(self): |
---|
618 | n/a | self.assertEqual( |
---|
619 | n/a | getargs_keyword_only(1), |
---|
620 | n/a | (1, -1, -1) |
---|
621 | n/a | ) |
---|
622 | n/a | self.assertEqual( |
---|
623 | n/a | getargs_keyword_only(required=1), |
---|
624 | n/a | (1, -1, -1) |
---|
625 | n/a | ) |
---|
626 | n/a | # required arg missing |
---|
627 | n/a | with self.assertRaisesRegex(TypeError, |
---|
628 | n/a | r"Required argument 'required' \(pos 1\) not found"): |
---|
629 | n/a | getargs_keyword_only(optional=2) |
---|
630 | n/a | |
---|
631 | n/a | with self.assertRaisesRegex(TypeError, |
---|
632 | n/a | r"Required argument 'required' \(pos 1\) not found"): |
---|
633 | n/a | getargs_keyword_only(keyword_only=3) |
---|
634 | n/a | |
---|
635 | n/a | def test_too_many_args(self): |
---|
636 | n/a | with self.assertRaisesRegex(TypeError, |
---|
637 | n/a | r"Function takes at most 2 positional arguments \(3 given\)"): |
---|
638 | n/a | getargs_keyword_only(1, 2, 3) |
---|
639 | n/a | |
---|
640 | n/a | with self.assertRaisesRegex(TypeError, |
---|
641 | n/a | r"function takes at most 3 arguments \(4 given\)"): |
---|
642 | n/a | getargs_keyword_only(1, 2, 3, keyword_only=5) |
---|
643 | n/a | |
---|
644 | n/a | def test_invalid_keyword(self): |
---|
645 | n/a | # extraneous keyword arg |
---|
646 | n/a | with self.assertRaisesRegex(TypeError, |
---|
647 | n/a | "'monster' is an invalid keyword argument for this function"): |
---|
648 | n/a | getargs_keyword_only(1, 2, monster=666) |
---|
649 | n/a | |
---|
650 | n/a | def test_surrogate_keyword(self): |
---|
651 | n/a | with self.assertRaisesRegex(TypeError, |
---|
652 | n/a | "'\udc80' is an invalid keyword argument for this function"): |
---|
653 | n/a | getargs_keyword_only(1, 2, **{'\uDC80': 10}) |
---|
654 | n/a | |
---|
655 | n/a | |
---|
656 | n/a | class PositionalOnlyAndKeywords_TestCase(unittest.TestCase): |
---|
657 | n/a | from _testcapi import getargs_positional_only_and_keywords as getargs |
---|
658 | n/a | |
---|
659 | n/a | def test_positional_args(self): |
---|
660 | n/a | # using all possible positional args |
---|
661 | n/a | self.assertEqual(self.getargs(1, 2, 3), (1, 2, 3)) |
---|
662 | n/a | |
---|
663 | n/a | def test_mixed_args(self): |
---|
664 | n/a | # positional and keyword args |
---|
665 | n/a | self.assertEqual(self.getargs(1, 2, keyword=3), (1, 2, 3)) |
---|
666 | n/a | |
---|
667 | n/a | def test_optional_args(self): |
---|
668 | n/a | # missing optional args |
---|
669 | n/a | self.assertEqual(self.getargs(1, 2), (1, 2, -1)) |
---|
670 | n/a | self.assertEqual(self.getargs(1, keyword=3), (1, -1, 3)) |
---|
671 | n/a | |
---|
672 | n/a | def test_required_args(self): |
---|
673 | n/a | self.assertEqual(self.getargs(1), (1, -1, -1)) |
---|
674 | n/a | # required positional arg missing |
---|
675 | n/a | with self.assertRaisesRegex(TypeError, |
---|
676 | n/a | r"Function takes at least 1 positional arguments \(0 given\)"): |
---|
677 | n/a | self.getargs() |
---|
678 | n/a | |
---|
679 | n/a | with self.assertRaisesRegex(TypeError, |
---|
680 | n/a | r"Function takes at least 1 positional arguments \(0 given\)"): |
---|
681 | n/a | self.getargs(keyword=3) |
---|
682 | n/a | |
---|
683 | n/a | def test_empty_keyword(self): |
---|
684 | n/a | with self.assertRaisesRegex(TypeError, |
---|
685 | n/a | "'' is an invalid keyword argument for this function"): |
---|
686 | n/a | self.getargs(1, 2, **{'': 666}) |
---|
687 | n/a | |
---|
688 | n/a | |
---|
689 | n/a | class Bytes_TestCase(unittest.TestCase): |
---|
690 | n/a | def test_c(self): |
---|
691 | n/a | from _testcapi import getargs_c |
---|
692 | n/a | self.assertRaises(TypeError, getargs_c, b'abc') # len > 1 |
---|
693 | n/a | self.assertEqual(getargs_c(b'a'), 97) |
---|
694 | n/a | self.assertEqual(getargs_c(bytearray(b'a')), 97) |
---|
695 | n/a | self.assertRaises(TypeError, getargs_c, memoryview(b'a')) |
---|
696 | n/a | self.assertRaises(TypeError, getargs_c, 's') |
---|
697 | n/a | self.assertRaises(TypeError, getargs_c, 97) |
---|
698 | n/a | self.assertRaises(TypeError, getargs_c, None) |
---|
699 | n/a | |
---|
700 | n/a | def test_y(self): |
---|
701 | n/a | from _testcapi import getargs_y |
---|
702 | n/a | self.assertRaises(TypeError, getargs_y, 'abc\xe9') |
---|
703 | n/a | self.assertEqual(getargs_y(b'bytes'), b'bytes') |
---|
704 | n/a | self.assertRaises(ValueError, getargs_y, b'nul:\0') |
---|
705 | n/a | self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) |
---|
706 | n/a | self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) |
---|
707 | n/a | self.assertRaises(TypeError, getargs_y, None) |
---|
708 | n/a | |
---|
709 | n/a | def test_y_star(self): |
---|
710 | n/a | from _testcapi import getargs_y_star |
---|
711 | n/a | self.assertRaises(TypeError, getargs_y_star, 'abc\xe9') |
---|
712 | n/a | self.assertEqual(getargs_y_star(b'bytes'), b'bytes') |
---|
713 | n/a | self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0') |
---|
714 | n/a | self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray') |
---|
715 | n/a | self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview') |
---|
716 | n/a | self.assertRaises(TypeError, getargs_y_star, None) |
---|
717 | n/a | |
---|
718 | n/a | def test_y_hash(self): |
---|
719 | n/a | from _testcapi import getargs_y_hash |
---|
720 | n/a | self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9') |
---|
721 | n/a | self.assertEqual(getargs_y_hash(b'bytes'), b'bytes') |
---|
722 | n/a | self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0') |
---|
723 | n/a | self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray')) |
---|
724 | n/a | self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview')) |
---|
725 | n/a | self.assertRaises(TypeError, getargs_y_hash, None) |
---|
726 | n/a | |
---|
727 | n/a | def test_w_star(self): |
---|
728 | n/a | # getargs_w_star() modifies first and last byte |
---|
729 | n/a | from _testcapi import getargs_w_star |
---|
730 | n/a | self.assertRaises(TypeError, getargs_w_star, 'abc\xe9') |
---|
731 | n/a | self.assertRaises(TypeError, getargs_w_star, b'bytes') |
---|
732 | n/a | self.assertRaises(TypeError, getargs_w_star, b'nul:\0') |
---|
733 | n/a | self.assertRaises(TypeError, getargs_w_star, memoryview(b'bytes')) |
---|
734 | n/a | buf = bytearray(b'bytearray') |
---|
735 | n/a | self.assertEqual(getargs_w_star(buf), b'[ytearra]') |
---|
736 | n/a | self.assertEqual(buf, bytearray(b'[ytearra]')) |
---|
737 | n/a | buf = bytearray(b'memoryview') |
---|
738 | n/a | self.assertEqual(getargs_w_star(memoryview(buf)), b'[emoryvie]') |
---|
739 | n/a | self.assertEqual(buf, bytearray(b'[emoryvie]')) |
---|
740 | n/a | self.assertRaises(TypeError, getargs_w_star, None) |
---|
741 | n/a | |
---|
742 | n/a | |
---|
743 | n/a | class String_TestCase(unittest.TestCase): |
---|
744 | n/a | def test_C(self): |
---|
745 | n/a | from _testcapi import getargs_C |
---|
746 | n/a | self.assertRaises(TypeError, getargs_C, 'abc') # len > 1 |
---|
747 | n/a | self.assertEqual(getargs_C('a'), 97) |
---|
748 | n/a | self.assertEqual(getargs_C('\u20ac'), 0x20ac) |
---|
749 | n/a | self.assertEqual(getargs_C('\U0001f40d'), 0x1f40d) |
---|
750 | n/a | self.assertRaises(TypeError, getargs_C, b'a') |
---|
751 | n/a | self.assertRaises(TypeError, getargs_C, bytearray(b'a')) |
---|
752 | n/a | self.assertRaises(TypeError, getargs_C, memoryview(b'a')) |
---|
753 | n/a | self.assertRaises(TypeError, getargs_C, 97) |
---|
754 | n/a | self.assertRaises(TypeError, getargs_C, None) |
---|
755 | n/a | |
---|
756 | n/a | def test_s(self): |
---|
757 | n/a | from _testcapi import getargs_s |
---|
758 | n/a | self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') |
---|
759 | n/a | self.assertRaises(ValueError, getargs_s, 'nul:\0') |
---|
760 | n/a | self.assertRaises(TypeError, getargs_s, b'bytes') |
---|
761 | n/a | self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) |
---|
762 | n/a | self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) |
---|
763 | n/a | self.assertRaises(TypeError, getargs_s, None) |
---|
764 | n/a | |
---|
765 | n/a | def test_s_star(self): |
---|
766 | n/a | from _testcapi import getargs_s_star |
---|
767 | n/a | self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9') |
---|
768 | n/a | self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0') |
---|
769 | n/a | self.assertEqual(getargs_s_star(b'bytes'), b'bytes') |
---|
770 | n/a | self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray') |
---|
771 | n/a | self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview') |
---|
772 | n/a | self.assertRaises(TypeError, getargs_s_star, None) |
---|
773 | n/a | |
---|
774 | n/a | def test_s_hash(self): |
---|
775 | n/a | from _testcapi import getargs_s_hash |
---|
776 | n/a | self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9') |
---|
777 | n/a | self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0') |
---|
778 | n/a | self.assertEqual(getargs_s_hash(b'bytes'), b'bytes') |
---|
779 | n/a | self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray')) |
---|
780 | n/a | self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview')) |
---|
781 | n/a | self.assertRaises(TypeError, getargs_s_hash, None) |
---|
782 | n/a | |
---|
783 | n/a | def test_z(self): |
---|
784 | n/a | from _testcapi import getargs_z |
---|
785 | n/a | self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') |
---|
786 | n/a | self.assertRaises(ValueError, getargs_z, 'nul:\0') |
---|
787 | n/a | self.assertRaises(TypeError, getargs_z, b'bytes') |
---|
788 | n/a | self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) |
---|
789 | n/a | self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) |
---|
790 | n/a | self.assertIsNone(getargs_z(None)) |
---|
791 | n/a | |
---|
792 | n/a | def test_z_star(self): |
---|
793 | n/a | from _testcapi import getargs_z_star |
---|
794 | n/a | self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9') |
---|
795 | n/a | self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0') |
---|
796 | n/a | self.assertEqual(getargs_z_star(b'bytes'), b'bytes') |
---|
797 | n/a | self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray') |
---|
798 | n/a | self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview') |
---|
799 | n/a | self.assertIsNone(getargs_z_star(None)) |
---|
800 | n/a | |
---|
801 | n/a | def test_z_hash(self): |
---|
802 | n/a | from _testcapi import getargs_z_hash |
---|
803 | n/a | self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9') |
---|
804 | n/a | self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0') |
---|
805 | n/a | self.assertEqual(getargs_z_hash(b'bytes'), b'bytes') |
---|
806 | n/a | self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray')) |
---|
807 | n/a | self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview')) |
---|
808 | n/a | self.assertIsNone(getargs_z_hash(None)) |
---|
809 | n/a | |
---|
810 | n/a | def test_es(self): |
---|
811 | n/a | from _testcapi import getargs_es |
---|
812 | n/a | self.assertEqual(getargs_es('abc\xe9'), b'abc\xc3\xa9') |
---|
813 | n/a | self.assertEqual(getargs_es('abc\xe9', 'latin1'), b'abc\xe9') |
---|
814 | n/a | self.assertRaises(UnicodeEncodeError, getargs_es, 'abc\xe9', 'ascii') |
---|
815 | n/a | self.assertRaises(LookupError, getargs_es, 'abc\xe9', 'spam') |
---|
816 | n/a | self.assertRaises(TypeError, getargs_es, b'bytes', 'latin1') |
---|
817 | n/a | self.assertRaises(TypeError, getargs_es, bytearray(b'bytearray'), 'latin1') |
---|
818 | n/a | self.assertRaises(TypeError, getargs_es, memoryview(b'memoryview'), 'latin1') |
---|
819 | n/a | self.assertRaises(TypeError, getargs_es, None, 'latin1') |
---|
820 | n/a | self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1') |
---|
821 | n/a | |
---|
822 | n/a | def test_et(self): |
---|
823 | n/a | from _testcapi import getargs_et |
---|
824 | n/a | self.assertEqual(getargs_et('abc\xe9'), b'abc\xc3\xa9') |
---|
825 | n/a | self.assertEqual(getargs_et('abc\xe9', 'latin1'), b'abc\xe9') |
---|
826 | n/a | self.assertRaises(UnicodeEncodeError, getargs_et, 'abc\xe9', 'ascii') |
---|
827 | n/a | self.assertRaises(LookupError, getargs_et, 'abc\xe9', 'spam') |
---|
828 | n/a | self.assertEqual(getargs_et(b'bytes', 'latin1'), b'bytes') |
---|
829 | n/a | self.assertEqual(getargs_et(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
---|
830 | n/a | self.assertRaises(TypeError, getargs_et, memoryview(b'memoryview'), 'latin1') |
---|
831 | n/a | self.assertRaises(TypeError, getargs_et, None, 'latin1') |
---|
832 | n/a | self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1') |
---|
833 | n/a | self.assertRaises(TypeError, getargs_et, b'nul:\0', 'latin1') |
---|
834 | n/a | self.assertRaises(TypeError, getargs_et, bytearray(b'nul:\0'), 'latin1') |
---|
835 | n/a | |
---|
836 | n/a | def test_es_hash(self): |
---|
837 | n/a | from _testcapi import getargs_es_hash |
---|
838 | n/a | self.assertEqual(getargs_es_hash('abc\xe9'), b'abc\xc3\xa9') |
---|
839 | n/a | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1'), b'abc\xe9') |
---|
840 | n/a | self.assertRaises(UnicodeEncodeError, getargs_es_hash, 'abc\xe9', 'ascii') |
---|
841 | n/a | self.assertRaises(LookupError, getargs_es_hash, 'abc\xe9', 'spam') |
---|
842 | n/a | self.assertRaises(TypeError, getargs_es_hash, b'bytes', 'latin1') |
---|
843 | n/a | self.assertRaises(TypeError, getargs_es_hash, bytearray(b'bytearray'), 'latin1') |
---|
844 | n/a | self.assertRaises(TypeError, getargs_es_hash, memoryview(b'memoryview'), 'latin1') |
---|
845 | n/a | self.assertRaises(TypeError, getargs_es_hash, None, 'latin1') |
---|
846 | n/a | self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), b'nul:\0') |
---|
847 | n/a | |
---|
848 | n/a | buf = bytearray(b'x'*8) |
---|
849 | n/a | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
---|
850 | n/a | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
---|
851 | n/a | buf = bytearray(b'x'*5) |
---|
852 | n/a | self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
---|
853 | n/a | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
---|
854 | n/a | buf = bytearray(b'x'*4) |
---|
855 | n/a | self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
---|
856 | n/a | self.assertEqual(buf, bytearray(b'x'*4)) |
---|
857 | n/a | buf = bytearray() |
---|
858 | n/a | self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
---|
859 | n/a | |
---|
860 | n/a | def test_et_hash(self): |
---|
861 | n/a | from _testcapi import getargs_et_hash |
---|
862 | n/a | self.assertEqual(getargs_et_hash('abc\xe9'), b'abc\xc3\xa9') |
---|
863 | n/a | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1'), b'abc\xe9') |
---|
864 | n/a | self.assertRaises(UnicodeEncodeError, getargs_et_hash, 'abc\xe9', 'ascii') |
---|
865 | n/a | self.assertRaises(LookupError, getargs_et_hash, 'abc\xe9', 'spam') |
---|
866 | n/a | self.assertEqual(getargs_et_hash(b'bytes', 'latin1'), b'bytes') |
---|
867 | n/a | self.assertEqual(getargs_et_hash(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
---|
868 | n/a | self.assertRaises(TypeError, getargs_et_hash, memoryview(b'memoryview'), 'latin1') |
---|
869 | n/a | self.assertRaises(TypeError, getargs_et_hash, None, 'latin1') |
---|
870 | n/a | self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), b'nul:\0') |
---|
871 | n/a | self.assertEqual(getargs_et_hash(b'nul:\0', 'latin1'), b'nul:\0') |
---|
872 | n/a | self.assertEqual(getargs_et_hash(bytearray(b'nul:\0'), 'latin1'), b'nul:\0') |
---|
873 | n/a | |
---|
874 | n/a | buf = bytearray(b'x'*8) |
---|
875 | n/a | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
---|
876 | n/a | self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
---|
877 | n/a | buf = bytearray(b'x'*5) |
---|
878 | n/a | self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
---|
879 | n/a | self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
---|
880 | n/a | buf = bytearray(b'x'*4) |
---|
881 | n/a | self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
---|
882 | n/a | self.assertEqual(buf, bytearray(b'x'*4)) |
---|
883 | n/a | buf = bytearray() |
---|
884 | n/a | self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
---|
885 | n/a | |
---|
886 | n/a | def test_u(self): |
---|
887 | n/a | from _testcapi import getargs_u |
---|
888 | n/a | self.assertEqual(getargs_u('abc\xe9'), 'abc\xe9') |
---|
889 | n/a | self.assertRaises(ValueError, getargs_u, 'nul:\0') |
---|
890 | n/a | self.assertRaises(TypeError, getargs_u, b'bytes') |
---|
891 | n/a | self.assertRaises(TypeError, getargs_u, bytearray(b'bytearray')) |
---|
892 | n/a | self.assertRaises(TypeError, getargs_u, memoryview(b'memoryview')) |
---|
893 | n/a | self.assertRaises(TypeError, getargs_u, None) |
---|
894 | n/a | |
---|
895 | n/a | def test_u_hash(self): |
---|
896 | n/a | from _testcapi import getargs_u_hash |
---|
897 | n/a | self.assertEqual(getargs_u_hash('abc\xe9'), 'abc\xe9') |
---|
898 | n/a | self.assertEqual(getargs_u_hash('nul:\0'), 'nul:\0') |
---|
899 | n/a | self.assertRaises(TypeError, getargs_u_hash, b'bytes') |
---|
900 | n/a | self.assertRaises(TypeError, getargs_u_hash, bytearray(b'bytearray')) |
---|
901 | n/a | self.assertRaises(TypeError, getargs_u_hash, memoryview(b'memoryview')) |
---|
902 | n/a | self.assertRaises(TypeError, getargs_u_hash, None) |
---|
903 | n/a | |
---|
904 | n/a | def test_Z(self): |
---|
905 | n/a | from _testcapi import getargs_Z |
---|
906 | n/a | self.assertEqual(getargs_Z('abc\xe9'), 'abc\xe9') |
---|
907 | n/a | self.assertRaises(ValueError, getargs_Z, 'nul:\0') |
---|
908 | n/a | self.assertRaises(TypeError, getargs_Z, b'bytes') |
---|
909 | n/a | self.assertRaises(TypeError, getargs_Z, bytearray(b'bytearray')) |
---|
910 | n/a | self.assertRaises(TypeError, getargs_Z, memoryview(b'memoryview')) |
---|
911 | n/a | self.assertIsNone(getargs_Z(None)) |
---|
912 | n/a | |
---|
913 | n/a | def test_Z_hash(self): |
---|
914 | n/a | from _testcapi import getargs_Z_hash |
---|
915 | n/a | self.assertEqual(getargs_Z_hash('abc\xe9'), 'abc\xe9') |
---|
916 | n/a | self.assertEqual(getargs_Z_hash('nul:\0'), 'nul:\0') |
---|
917 | n/a | self.assertRaises(TypeError, getargs_Z_hash, b'bytes') |
---|
918 | n/a | self.assertRaises(TypeError, getargs_Z_hash, bytearray(b'bytearray')) |
---|
919 | n/a | self.assertRaises(TypeError, getargs_Z_hash, memoryview(b'memoryview')) |
---|
920 | n/a | self.assertIsNone(getargs_Z_hash(None)) |
---|
921 | n/a | |
---|
922 | n/a | |
---|
923 | n/a | class Object_TestCase(unittest.TestCase): |
---|
924 | n/a | def test_S(self): |
---|
925 | n/a | from _testcapi import getargs_S |
---|
926 | n/a | obj = b'bytes' |
---|
927 | n/a | self.assertIs(getargs_S(obj), obj) |
---|
928 | n/a | self.assertRaises(TypeError, getargs_S, bytearray(b'bytearray')) |
---|
929 | n/a | self.assertRaises(TypeError, getargs_S, 'str') |
---|
930 | n/a | self.assertRaises(TypeError, getargs_S, None) |
---|
931 | n/a | self.assertRaises(TypeError, getargs_S, memoryview(obj)) |
---|
932 | n/a | |
---|
933 | n/a | def test_Y(self): |
---|
934 | n/a | from _testcapi import getargs_Y |
---|
935 | n/a | obj = bytearray(b'bytearray') |
---|
936 | n/a | self.assertIs(getargs_Y(obj), obj) |
---|
937 | n/a | self.assertRaises(TypeError, getargs_Y, b'bytes') |
---|
938 | n/a | self.assertRaises(TypeError, getargs_Y, 'str') |
---|
939 | n/a | self.assertRaises(TypeError, getargs_Y, None) |
---|
940 | n/a | self.assertRaises(TypeError, getargs_Y, memoryview(obj)) |
---|
941 | n/a | |
---|
942 | n/a | def test_U(self): |
---|
943 | n/a | from _testcapi import getargs_U |
---|
944 | n/a | obj = 'str' |
---|
945 | n/a | self.assertIs(getargs_U(obj), obj) |
---|
946 | n/a | self.assertRaises(TypeError, getargs_U, b'bytes') |
---|
947 | n/a | self.assertRaises(TypeError, getargs_U, bytearray(b'bytearray')) |
---|
948 | n/a | self.assertRaises(TypeError, getargs_U, None) |
---|
949 | n/a | |
---|
950 | n/a | |
---|
951 | n/a | if __name__ == "__main__": |
---|
952 | n/a | unittest.main() |
---|