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