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