| 1 |
import string |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from test import support |
| 5 |
from test.support import import_helper |
| 6 |
from test.support import script_helper |
| 7 |
from test.support import warnings_helper |
| 8 |
from test.support.testcase import FloatsAreIdenticalMixin |
| 9 |
# Skip this test if the _testcapi module isn't available. |
| 10 |
_testcapi = import_helper.import_module('_testcapi') |
| 11 |
from _testcapi import getargs_keywords, getargs_keyword_only |
| 12 |
|
| 13 |
try: |
| 14 |
import _testinternalcapi |
| 15 |
except ImportError: |
| 16 |
_testinternalcapi = None |
| 17 |
|
| 18 |
# > How about the following counterproposal. This also changes some of |
| 19 |
# > the other format codes to be a little more regular. |
| 20 |
# > |
| 21 |
# > Code C type Range check |
| 22 |
# > |
| 23 |
# > b unsigned char 0..UCHAR_MAX |
| 24 |
# > h signed short SHRT_MIN..SHRT_MAX |
| 25 |
# > B unsigned char none ** |
| 26 |
# > H unsigned short none ** |
| 27 |
# > k * unsigned long none |
| 28 |
# > I * unsigned int 0..UINT_MAX |
| 29 |
# |
| 30 |
# |
| 31 |
# > i int INT_MIN..INT_MAX |
| 32 |
# > l long LONG_MIN..LONG_MAX |
| 33 |
# |
| 34 |
# > K * unsigned long long none |
| 35 |
# > L long long LLONG_MIN..LLONG_MAX |
| 36 |
# |
| 37 |
# > Notes: |
| 38 |
# > |
| 39 |
# > * New format codes. |
| 40 |
# > |
| 41 |
# > ** Changed from previous "range-and-a-half" to "none"; the |
| 42 |
# > range-and-a-half checking wasn't particularly useful. |
| 43 |
# |
| 44 |
# Plus a C API or two, e.g. PyLong_AsUnsignedLongMask() -> |
| 45 |
# unsigned long and PyLong_AsUnsignedLongLongMask() -> unsigned |
| 46 |
# long long (if that exists). |
| 47 |
|
| 48 |
LARGE = 0x7FFFFFFF |
| 49 |
VERY_LARGE = 0xFF0000121212121212121242 |
| 50 |
|
| 51 |
from _testcapi import UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, INT_MAX, \ |
| 52 |
INT_MIN, LONG_MIN, LONG_MAX, PY_SSIZE_T_MIN, PY_SSIZE_T_MAX, \ |
| 53 |
SHRT_MIN, SHRT_MAX, FLT_MIN, FLT_MAX, DBL_MIN, DBL_MAX |
| 54 |
|
| 55 |
DBL_MAX_EXP = sys.float_info.max_exp |
| 56 |
INF = float('inf') |
| 57 |
NAN = float('nan') |
| 58 |
|
| 59 |
# fake, they are not defined in Python's header files |
| 60 |
LLONG_MAX = 2**63-1 |
| 61 |
LLONG_MIN = -2**63 |
| 62 |
ULLONG_MAX = 2**64-1 |
| 63 |
|
| 64 |
NULL = None |
| 65 |
|
| 66 |
class CustomError(Exception): |
| 67 |
pass |
| 68 |
|
| 69 |
class Index: |
| 70 |
def __index__(self): |
| 71 |
return 99 |
| 72 |
|
| 73 |
class IndexIntSubclass(int): |
| 74 |
def __index__(self): |
| 75 |
return 99 |
| 76 |
|
| 77 |
class BadIndex: |
| 78 |
def __index__(self): |
| 79 |
return 1.0 |
| 80 |
|
| 81 |
class BadIndex2: |
| 82 |
def __index__(self): |
| 83 |
return True |
| 84 |
|
| 85 |
class BadIndex3(int): |
| 86 |
def __index__(self): |
| 87 |
return True |
| 88 |
|
| 89 |
|
| 90 |
class Int: |
| 91 |
def __int__(self): |
| 92 |
return 99 |
| 93 |
|
| 94 |
class IntSubclass(int): |
| 95 |
def __int__(self): |
| 96 |
return 99 |
| 97 |
|
| 98 |
class BadInt: |
| 99 |
def __int__(self): |
| 100 |
return 1.0 |
| 101 |
|
| 102 |
class BadInt2: |
| 103 |
def __int__(self): |
| 104 |
return True |
| 105 |
|
| 106 |
class BadInt3(int): |
| 107 |
def __int__(self): |
| 108 |
return True |
| 109 |
|
| 110 |
|
| 111 |
class Float: |
| 112 |
def __float__(self): |
| 113 |
return 4.25 |
| 114 |
|
| 115 |
class FloatSubclass(float): |
| 116 |
pass |
| 117 |
|
| 118 |
class FloatSubclass2(float): |
| 119 |
def __float__(self): |
| 120 |
return 4.25 |
| 121 |
|
| 122 |
class BadFloat: |
| 123 |
def __float__(self): |
| 124 |
return 687 |
| 125 |
|
| 126 |
class BadFloat2: |
| 127 |
def __float__(self): |
| 128 |
return FloatSubclass(4.25) |
| 129 |
|
| 130 |
class BadFloat3(float): |
| 131 |
def __float__(self): |
| 132 |
return FloatSubclass(4.25) |
| 133 |
|
| 134 |
|
| 135 |
class Complex: |
| 136 |
def __complex__(self): |
| 137 |
return 4.25+0.5j |
| 138 |
|
| 139 |
class ComplexSubclass(complex): |
| 140 |
pass |
| 141 |
|
| 142 |
class ComplexSubclass2(complex): |
| 143 |
def __complex__(self): |
| 144 |
return 4.25+0.5j |
| 145 |
|
| 146 |
class BadComplex: |
| 147 |
def __complex__(self): |
| 148 |
return 1.25 |
| 149 |
|
| 150 |
class BadComplex2: |
| 151 |
def __complex__(self): |
| 152 |
return ComplexSubclass(4.25+0.5j) |
| 153 |
|
| 154 |
class BadComplex3(complex): |
| 155 |
def __complex__(self): |
| 156 |
return ComplexSubclass(4.25+0.5j) |
| 157 |
|
| 158 |
|
| 159 |
class TupleSubclass(tuple): |
| 160 |
pass |
| 161 |
|
| 162 |
class DictSubclass(dict): |
| 163 |
pass |
| 164 |
|
| 165 |
NONCONTIG_WRITABLE = memoryview(bytearray(b'noncontig'))[::-2] |
| 166 |
NONCONTIG_READONLY = memoryview(b'noncontig')[::-2] |
| 167 |
|
| 168 |
class Unsigned_TestCase(unittest.TestCase): |
| 169 |
def test_b(self): |
| 170 |
from _testcapi import getargs_b |
| 171 |
# b returns 'unsigned char', and does range checking (0 ... UCHAR_MAX) |
| 172 |
self.assertRaises(TypeError, getargs_b, 3.14) |
| 173 |
self.assertEqual(99, getargs_b(Index())) |
| 174 |
self.assertEqual(0, getargs_b(IndexIntSubclass())) |
| 175 |
self.assertRaises(TypeError, getargs_b, BadIndex()) |
| 176 |
with self.assertWarns(DeprecationWarning): |
| 177 |
self.assertEqual(1, getargs_b(BadIndex2())) |
| 178 |
self.assertEqual(0, getargs_b(BadIndex3())) |
| 179 |
self.assertRaises(TypeError, getargs_b, Int()) |
| 180 |
self.assertEqual(0, getargs_b(IntSubclass())) |
| 181 |
self.assertRaises(TypeError, getargs_b, BadInt()) |
| 182 |
self.assertRaises(TypeError, getargs_b, BadInt2()) |
| 183 |
self.assertEqual(0, getargs_b(BadInt3())) |
| 184 |
|
| 185 |
self.assertRaises(OverflowError, getargs_b, -1) |
| 186 |
self.assertEqual(0, getargs_b(0)) |
| 187 |
self.assertEqual(UCHAR_MAX, getargs_b(UCHAR_MAX)) |
| 188 |
self.assertRaises(OverflowError, getargs_b, UCHAR_MAX + 1) |
| 189 |
|
| 190 |
self.assertEqual(42, getargs_b(42)) |
| 191 |
self.assertRaises(OverflowError, getargs_b, VERY_LARGE) |
| 192 |
|
| 193 |
def test_B(self): |
| 194 |
from _testcapi import getargs_B |
| 195 |
# B returns 'unsigned char', no range checking |
| 196 |
self.assertRaises(TypeError, getargs_B, 3.14) |
| 197 |
self.assertEqual(99, getargs_B(Index())) |
| 198 |
self.assertEqual(0, getargs_B(IndexIntSubclass())) |
| 199 |
self.assertRaises(TypeError, getargs_B, BadIndex()) |
| 200 |
with self.assertWarns(DeprecationWarning): |
| 201 |
self.assertEqual(1, getargs_B(BadIndex2())) |
| 202 |
self.assertEqual(0, getargs_B(BadIndex3())) |
| 203 |
self.assertRaises(TypeError, getargs_B, Int()) |
| 204 |
self.assertEqual(0, getargs_B(IntSubclass())) |
| 205 |
self.assertRaises(TypeError, getargs_B, BadInt()) |
| 206 |
self.assertRaises(TypeError, getargs_B, BadInt2()) |
| 207 |
self.assertEqual(0, getargs_B(BadInt3())) |
| 208 |
|
| 209 |
self.assertEqual(UCHAR_MAX, getargs_B(-1)) |
| 210 |
self.assertEqual(0, getargs_B(0)) |
| 211 |
self.assertEqual(UCHAR_MAX, getargs_B(UCHAR_MAX)) |
| 212 |
self.assertEqual(0, getargs_B(UCHAR_MAX+1)) |
| 213 |
|
| 214 |
self.assertEqual(42, getargs_B(42)) |
| 215 |
self.assertEqual(UCHAR_MAX & VERY_LARGE, getargs_B(VERY_LARGE)) |
| 216 |
|
| 217 |
def test_H(self): |
| 218 |
from _testcapi import getargs_H |
| 219 |
# H returns 'unsigned short', no range checking |
| 220 |
self.assertRaises(TypeError, getargs_H, 3.14) |
| 221 |
self.assertEqual(99, getargs_H(Index())) |
| 222 |
self.assertEqual(0, getargs_H(IndexIntSubclass())) |
| 223 |
self.assertRaises(TypeError, getargs_H, BadIndex()) |
| 224 |
with self.assertWarns(DeprecationWarning): |
| 225 |
self.assertEqual(1, getargs_H(BadIndex2())) |
| 226 |
self.assertEqual(0, getargs_H(BadIndex3())) |
| 227 |
self.assertRaises(TypeError, getargs_H, Int()) |
| 228 |
self.assertEqual(0, getargs_H(IntSubclass())) |
| 229 |
self.assertRaises(TypeError, getargs_H, BadInt()) |
| 230 |
self.assertRaises(TypeError, getargs_H, BadInt2()) |
| 231 |
self.assertEqual(0, getargs_H(BadInt3())) |
| 232 |
|
| 233 |
self.assertEqual(USHRT_MAX, getargs_H(-1)) |
| 234 |
self.assertEqual(0, getargs_H(0)) |
| 235 |
self.assertEqual(USHRT_MAX, getargs_H(USHRT_MAX)) |
| 236 |
self.assertEqual(0, getargs_H(USHRT_MAX+1)) |
| 237 |
|
| 238 |
self.assertEqual(42, getargs_H(42)) |
| 239 |
|
| 240 |
self.assertEqual(VERY_LARGE & USHRT_MAX, getargs_H(VERY_LARGE)) |
| 241 |
|
| 242 |
def test_I(self): |
| 243 |
from _testcapi import getargs_I |
| 244 |
# I returns 'unsigned int', no range checking |
| 245 |
self.assertRaises(TypeError, getargs_I, 3.14) |
| 246 |
self.assertEqual(99, getargs_I(Index())) |
| 247 |
self.assertEqual(0, getargs_I(IndexIntSubclass())) |
| 248 |
self.assertRaises(TypeError, getargs_I, BadIndex()) |
| 249 |
with self.assertWarns(DeprecationWarning): |
| 250 |
self.assertEqual(1, getargs_I(BadIndex2())) |
| 251 |
self.assertEqual(0, getargs_I(BadIndex3())) |
| 252 |
self.assertRaises(TypeError, getargs_I, Int()) |
| 253 |
self.assertEqual(0, getargs_I(IntSubclass())) |
| 254 |
self.assertRaises(TypeError, getargs_I, BadInt()) |
| 255 |
self.assertRaises(TypeError, getargs_I, BadInt2()) |
| 256 |
self.assertEqual(0, getargs_I(BadInt3())) |
| 257 |
|
| 258 |
self.assertEqual(UINT_MAX, getargs_I(-1)) |
| 259 |
self.assertEqual(0, getargs_I(0)) |
| 260 |
self.assertEqual(UINT_MAX, getargs_I(UINT_MAX)) |
| 261 |
self.assertEqual(0, getargs_I(UINT_MAX+1)) |
| 262 |
|
| 263 |
self.assertEqual(42, getargs_I(42)) |
| 264 |
|
| 265 |
self.assertEqual(VERY_LARGE & UINT_MAX, getargs_I(VERY_LARGE)) |
| 266 |
|
| 267 |
def test_k(self): |
| 268 |
from _testcapi import getargs_k |
| 269 |
# k returns 'unsigned long', no range checking |
| 270 |
self.assertRaises(TypeError, getargs_k, 3.14) |
| 271 |
self.assertEqual(99, getargs_k(Index())) |
| 272 |
self.assertEqual(0, getargs_k(IndexIntSubclass())) |
| 273 |
self.assertRaises(TypeError, getargs_k, BadIndex()) |
| 274 |
with self.assertWarns(DeprecationWarning): |
| 275 |
self.assertEqual(1, getargs_k(BadIndex2())) |
| 276 |
self.assertEqual(0, getargs_k(BadIndex3())) |
| 277 |
self.assertRaises(TypeError, getargs_k, Int()) |
| 278 |
self.assertEqual(0, getargs_k(IntSubclass())) |
| 279 |
self.assertRaises(TypeError, getargs_k, BadInt()) |
| 280 |
self.assertRaises(TypeError, getargs_k, BadInt2()) |
| 281 |
self.assertEqual(0, getargs_k(BadInt3())) |
| 282 |
|
| 283 |
self.assertEqual(ULONG_MAX, getargs_k(-1)) |
| 284 |
self.assertEqual(0, getargs_k(0)) |
| 285 |
self.assertEqual(ULONG_MAX, getargs_k(ULONG_MAX)) |
| 286 |
self.assertEqual(0, getargs_k(ULONG_MAX+1)) |
| 287 |
|
| 288 |
self.assertEqual(42, getargs_k(42)) |
| 289 |
|
| 290 |
self.assertEqual(VERY_LARGE & ULONG_MAX, getargs_k(VERY_LARGE)) |
| 291 |
|
| 292 |
class Signed_TestCase(unittest.TestCase): |
| 293 |
def test_h(self): |
| 294 |
from _testcapi import getargs_h |
| 295 |
# h returns 'short', and does range checking (SHRT_MIN ... SHRT_MAX) |
| 296 |
self.assertRaises(TypeError, getargs_h, 3.14) |
| 297 |
self.assertEqual(99, getargs_h(Index())) |
| 298 |
self.assertEqual(0, getargs_h(IndexIntSubclass())) |
| 299 |
self.assertRaises(TypeError, getargs_h, BadIndex()) |
| 300 |
with self.assertWarns(DeprecationWarning): |
| 301 |
self.assertEqual(1, getargs_h(BadIndex2())) |
| 302 |
self.assertEqual(0, getargs_h(BadIndex3())) |
| 303 |
self.assertRaises(TypeError, getargs_h, Int()) |
| 304 |
self.assertEqual(0, getargs_h(IntSubclass())) |
| 305 |
self.assertRaises(TypeError, getargs_h, BadInt()) |
| 306 |
self.assertRaises(TypeError, getargs_h, BadInt2()) |
| 307 |
self.assertEqual(0, getargs_h(BadInt3())) |
| 308 |
|
| 309 |
self.assertRaises(OverflowError, getargs_h, SHRT_MIN-1) |
| 310 |
self.assertEqual(SHRT_MIN, getargs_h(SHRT_MIN)) |
| 311 |
self.assertEqual(SHRT_MAX, getargs_h(SHRT_MAX)) |
| 312 |
self.assertRaises(OverflowError, getargs_h, SHRT_MAX+1) |
| 313 |
|
| 314 |
self.assertEqual(42, getargs_h(42)) |
| 315 |
self.assertRaises(OverflowError, getargs_h, VERY_LARGE) |
| 316 |
|
| 317 |
def test_i(self): |
| 318 |
from _testcapi import getargs_i |
| 319 |
# i returns 'int', and does range checking (INT_MIN ... INT_MAX) |
| 320 |
self.assertRaises(TypeError, getargs_i, 3.14) |
| 321 |
self.assertEqual(99, getargs_i(Index())) |
| 322 |
self.assertEqual(0, getargs_i(IndexIntSubclass())) |
| 323 |
self.assertRaises(TypeError, getargs_i, BadIndex()) |
| 324 |
with self.assertWarns(DeprecationWarning): |
| 325 |
self.assertEqual(1, getargs_i(BadIndex2())) |
| 326 |
self.assertEqual(0, getargs_i(BadIndex3())) |
| 327 |
self.assertRaises(TypeError, getargs_i, Int()) |
| 328 |
self.assertEqual(0, getargs_i(IntSubclass())) |
| 329 |
self.assertRaises(TypeError, getargs_i, BadInt()) |
| 330 |
self.assertRaises(TypeError, getargs_i, BadInt2()) |
| 331 |
self.assertEqual(0, getargs_i(BadInt3())) |
| 332 |
|
| 333 |
self.assertRaises(OverflowError, getargs_i, INT_MIN-1) |
| 334 |
self.assertEqual(INT_MIN, getargs_i(INT_MIN)) |
| 335 |
self.assertEqual(INT_MAX, getargs_i(INT_MAX)) |
| 336 |
self.assertRaises(OverflowError, getargs_i, INT_MAX+1) |
| 337 |
|
| 338 |
self.assertEqual(42, getargs_i(42)) |
| 339 |
self.assertRaises(OverflowError, getargs_i, VERY_LARGE) |
| 340 |
|
| 341 |
def test_l(self): |
| 342 |
from _testcapi import getargs_l |
| 343 |
# l returns 'long', and does range checking (LONG_MIN ... LONG_MAX) |
| 344 |
self.assertRaises(TypeError, getargs_l, 3.14) |
| 345 |
self.assertEqual(99, getargs_l(Index())) |
| 346 |
self.assertEqual(0, getargs_l(IndexIntSubclass())) |
| 347 |
self.assertRaises(TypeError, getargs_l, BadIndex()) |
| 348 |
with self.assertWarns(DeprecationWarning): |
| 349 |
self.assertEqual(1, getargs_l(BadIndex2())) |
| 350 |
self.assertEqual(0, getargs_l(BadIndex3())) |
| 351 |
self.assertRaises(TypeError, getargs_l, Int()) |
| 352 |
self.assertEqual(0, getargs_l(IntSubclass())) |
| 353 |
self.assertRaises(TypeError, getargs_l, BadInt()) |
| 354 |
self.assertRaises(TypeError, getargs_l, BadInt2()) |
| 355 |
self.assertEqual(0, getargs_l(BadInt3())) |
| 356 |
|
| 357 |
self.assertRaises(OverflowError, getargs_l, LONG_MIN-1) |
| 358 |
self.assertEqual(LONG_MIN, getargs_l(LONG_MIN)) |
| 359 |
self.assertEqual(LONG_MAX, getargs_l(LONG_MAX)) |
| 360 |
self.assertRaises(OverflowError, getargs_l, LONG_MAX+1) |
| 361 |
|
| 362 |
self.assertEqual(42, getargs_l(42)) |
| 363 |
self.assertRaises(OverflowError, getargs_l, VERY_LARGE) |
| 364 |
|
| 365 |
def test_n(self): |
| 366 |
from _testcapi import getargs_n |
| 367 |
# n returns 'Py_ssize_t', and does range checking |
| 368 |
# (PY_SSIZE_T_MIN ... PY_SSIZE_T_MAX) |
| 369 |
self.assertRaises(TypeError, getargs_n, 3.14) |
| 370 |
self.assertEqual(99, getargs_n(Index())) |
| 371 |
self.assertEqual(0, getargs_n(IndexIntSubclass())) |
| 372 |
self.assertRaises(TypeError, getargs_n, BadIndex()) |
| 373 |
with self.assertWarns(DeprecationWarning): |
| 374 |
self.assertEqual(1, getargs_n(BadIndex2())) |
| 375 |
self.assertEqual(0, getargs_n(BadIndex3())) |
| 376 |
self.assertRaises(TypeError, getargs_n, Int()) |
| 377 |
self.assertEqual(0, getargs_n(IntSubclass())) |
| 378 |
self.assertRaises(TypeError, getargs_n, BadInt()) |
| 379 |
self.assertRaises(TypeError, getargs_n, BadInt2()) |
| 380 |
self.assertEqual(0, getargs_n(BadInt3())) |
| 381 |
|
| 382 |
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MIN-1) |
| 383 |
self.assertEqual(PY_SSIZE_T_MIN, getargs_n(PY_SSIZE_T_MIN)) |
| 384 |
self.assertEqual(PY_SSIZE_T_MAX, getargs_n(PY_SSIZE_T_MAX)) |
| 385 |
self.assertRaises(OverflowError, getargs_n, PY_SSIZE_T_MAX+1) |
| 386 |
|
| 387 |
self.assertEqual(42, getargs_n(42)) |
| 388 |
self.assertRaises(OverflowError, getargs_n, VERY_LARGE) |
| 389 |
|
| 390 |
|
| 391 |
class LongLong_TestCase(unittest.TestCase): |
| 392 |
def test_L(self): |
| 393 |
from _testcapi import getargs_L |
| 394 |
# L returns 'long long', and does range checking (LLONG_MIN |
| 395 |
# ... LLONG_MAX) |
| 396 |
self.assertRaises(TypeError, getargs_L, 3.14) |
| 397 |
self.assertRaises(TypeError, getargs_L, "Hello") |
| 398 |
self.assertEqual(99, getargs_L(Index())) |
| 399 |
self.assertEqual(0, getargs_L(IndexIntSubclass())) |
| 400 |
self.assertRaises(TypeError, getargs_L, BadIndex()) |
| 401 |
with self.assertWarns(DeprecationWarning): |
| 402 |
self.assertEqual(1, getargs_L(BadIndex2())) |
| 403 |
self.assertEqual(0, getargs_L(BadIndex3())) |
| 404 |
self.assertRaises(TypeError, getargs_L, Int()) |
| 405 |
self.assertEqual(0, getargs_L(IntSubclass())) |
| 406 |
self.assertRaises(TypeError, getargs_L, BadInt()) |
| 407 |
self.assertRaises(TypeError, getargs_L, BadInt2()) |
| 408 |
self.assertEqual(0, getargs_L(BadInt3())) |
| 409 |
|
| 410 |
self.assertRaises(OverflowError, getargs_L, LLONG_MIN-1) |
| 411 |
self.assertEqual(LLONG_MIN, getargs_L(LLONG_MIN)) |
| 412 |
self.assertEqual(LLONG_MAX, getargs_L(LLONG_MAX)) |
| 413 |
self.assertRaises(OverflowError, getargs_L, LLONG_MAX+1) |
| 414 |
|
| 415 |
self.assertEqual(42, getargs_L(42)) |
| 416 |
self.assertRaises(OverflowError, getargs_L, VERY_LARGE) |
| 417 |
|
| 418 |
def test_K(self): |
| 419 |
from _testcapi import getargs_K |
| 420 |
# K return 'unsigned long long', no range checking |
| 421 |
self.assertRaises(TypeError, getargs_K, 3.14) |
| 422 |
self.assertEqual(99, getargs_K(Index())) |
| 423 |
self.assertEqual(0, getargs_K(IndexIntSubclass())) |
| 424 |
self.assertRaises(TypeError, getargs_K, BadIndex()) |
| 425 |
with self.assertWarns(DeprecationWarning): |
| 426 |
self.assertEqual(1, getargs_K(BadIndex2())) |
| 427 |
self.assertEqual(0, getargs_K(BadIndex3())) |
| 428 |
self.assertRaises(TypeError, getargs_K, Int()) |
| 429 |
self.assertEqual(0, getargs_K(IntSubclass())) |
| 430 |
self.assertRaises(TypeError, getargs_K, BadInt()) |
| 431 |
self.assertRaises(TypeError, getargs_K, BadInt2()) |
| 432 |
self.assertEqual(0, getargs_K(BadInt3())) |
| 433 |
|
| 434 |
self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 435 |
self.assertEqual(0, getargs_K(0)) |
| 436 |
self.assertEqual(ULLONG_MAX, getargs_K(ULLONG_MAX)) |
| 437 |
self.assertEqual(0, getargs_K(ULLONG_MAX+1)) |
| 438 |
|
| 439 |
self.assertEqual(42, getargs_K(42)) |
| 440 |
|
| 441 |
self.assertEqual(VERY_LARGE & ULLONG_MAX, getargs_K(VERY_LARGE)) |
| 442 |
|
| 443 |
|
| 444 |
class Float_TestCase(unittest.TestCase, FloatsAreIdenticalMixin): |
| 445 |
def test_f(self): |
| 446 |
from _testcapi import getargs_f |
| 447 |
self.assertEqual(getargs_f(4.25), 4.25) |
| 448 |
self.assertEqual(getargs_f(4), 4.0) |
| 449 |
self.assertRaises(TypeError, getargs_f, 4.25+0j) |
| 450 |
self.assertEqual(getargs_f(Float()), 4.25) |
| 451 |
self.assertEqual(getargs_f(FloatSubclass(7.5)), 7.5) |
| 452 |
self.assertEqual(getargs_f(FloatSubclass2(7.5)), 7.5) |
| 453 |
self.assertRaises(TypeError, getargs_f, BadFloat()) |
| 454 |
with self.assertWarns(DeprecationWarning): |
| 455 |
self.assertEqual(getargs_f(BadFloat2()), 4.25) |
| 456 |
self.assertEqual(getargs_f(BadFloat3(7.5)), 7.5) |
| 457 |
self.assertEqual(getargs_f(Index()), 99.0) |
| 458 |
self.assertRaises(TypeError, getargs_f, Int()) |
| 459 |
|
| 460 |
for x in (FLT_MIN, -FLT_MIN, FLT_MAX, -FLT_MAX, INF, -INF): |
| 461 |
self.assertEqual(getargs_f(x), x) |
| 462 |
if FLT_MAX < DBL_MAX: |
| 463 |
self.assertEqual(getargs_f(DBL_MAX), INF) |
| 464 |
self.assertEqual(getargs_f(-DBL_MAX), -INF) |
| 465 |
if FLT_MIN > DBL_MIN: |
| 466 |
self.assertFloatsAreIdentical(getargs_f(DBL_MIN), 0.0) |
| 467 |
self.assertFloatsAreIdentical(getargs_f(-DBL_MIN), -0.0) |
| 468 |
self.assertFloatsAreIdentical(getargs_f(0.0), 0.0) |
| 469 |
self.assertFloatsAreIdentical(getargs_f(-0.0), -0.0) |
| 470 |
r = getargs_f(NAN) |
| 471 |
self.assertNotEqual(r, r) |
| 472 |
|
| 473 |
@support.requires_IEEE_754 |
| 474 |
def test_f_rounding(self): |
| 475 |
from _testcapi import getargs_f |
| 476 |
self.assertEqual(getargs_f(3.40282356e38), FLT_MAX) |
| 477 |
self.assertEqual(getargs_f(-3.40282356e38), -FLT_MAX) |
| 478 |
|
| 479 |
def test_d(self): |
| 480 |
from _testcapi import getargs_d |
| 481 |
self.assertEqual(getargs_d(4.25), 4.25) |
| 482 |
self.assertEqual(getargs_d(4), 4.0) |
| 483 |
self.assertRaises(TypeError, getargs_d, 4.25+0j) |
| 484 |
self.assertEqual(getargs_d(Float()), 4.25) |
| 485 |
self.assertEqual(getargs_d(FloatSubclass(7.5)), 7.5) |
| 486 |
self.assertEqual(getargs_d(FloatSubclass2(7.5)), 7.5) |
| 487 |
self.assertRaises(TypeError, getargs_d, BadFloat()) |
| 488 |
with self.assertWarns(DeprecationWarning): |
| 489 |
self.assertEqual(getargs_d(BadFloat2()), 4.25) |
| 490 |
self.assertEqual(getargs_d(BadFloat3(7.5)), 7.5) |
| 491 |
self.assertEqual(getargs_d(Index()), 99.0) |
| 492 |
self.assertRaises(TypeError, getargs_d, Int()) |
| 493 |
|
| 494 |
for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
| 495 |
self.assertEqual(getargs_d(x), x) |
| 496 |
self.assertRaises(OverflowError, getargs_d, 1<<DBL_MAX_EXP) |
| 497 |
self.assertRaises(OverflowError, getargs_d, -1<<DBL_MAX_EXP) |
| 498 |
self.assertFloatsAreIdentical(getargs_d(0.0), 0.0) |
| 499 |
self.assertFloatsAreIdentical(getargs_d(-0.0), -0.0) |
| 500 |
r = getargs_d(NAN) |
| 501 |
self.assertNotEqual(r, r) |
| 502 |
|
| 503 |
def test_D(self): |
| 504 |
from _testcapi import getargs_D |
| 505 |
self.assertEqual(getargs_D(4.25+0.5j), 4.25+0.5j) |
| 506 |
self.assertEqual(getargs_D(4.25), 4.25+0j) |
| 507 |
self.assertEqual(getargs_D(4), 4.0+0j) |
| 508 |
self.assertEqual(getargs_D(Complex()), 4.25+0.5j) |
| 509 |
self.assertEqual(getargs_D(ComplexSubclass(7.5+0.25j)), 7.5+0.25j) |
| 510 |
self.assertEqual(getargs_D(ComplexSubclass2(7.5+0.25j)), 7.5+0.25j) |
| 511 |
self.assertRaises(TypeError, getargs_D, BadComplex()) |
| 512 |
with self.assertWarns(DeprecationWarning): |
| 513 |
self.assertEqual(getargs_D(BadComplex2()), 4.25+0.5j) |
| 514 |
self.assertEqual(getargs_D(BadComplex3(7.5+0.25j)), 7.5+0.25j) |
| 515 |
self.assertEqual(getargs_D(Index()), 99.0+0j) |
| 516 |
self.assertRaises(TypeError, getargs_D, Int()) |
| 517 |
|
| 518 |
for x in (DBL_MIN, -DBL_MIN, DBL_MAX, -DBL_MAX, INF, -INF): |
| 519 |
c = complex(x, 1.0) |
| 520 |
self.assertEqual(getargs_D(c), c) |
| 521 |
c = complex(1.0, x) |
| 522 |
self.assertEqual(getargs_D(c), c) |
| 523 |
self.assertFloatsAreIdentical(getargs_D(complex(0.0, 1.0)).real, 0.0) |
| 524 |
self.assertFloatsAreIdentical(getargs_D(complex(-0.0, 1.0)).real, -0.0) |
| 525 |
self.assertFloatsAreIdentical(getargs_D(complex(1.0, 0.0)).imag, 0.0) |
| 526 |
self.assertFloatsAreIdentical(getargs_D(complex(1.0, -0.0)).imag, -0.0) |
| 527 |
|
| 528 |
|
| 529 |
class Paradox: |
| 530 |
"This statement is false." |
| 531 |
def __bool__(self): |
| 532 |
raise NotImplementedError |
| 533 |
|
| 534 |
class Boolean_TestCase(unittest.TestCase): |
| 535 |
def test_p(self): |
| 536 |
from _testcapi import getargs_p |
| 537 |
self.assertEqual(0, getargs_p(False)) |
| 538 |
self.assertEqual(0, getargs_p(None)) |
| 539 |
self.assertEqual(0, getargs_p(0)) |
| 540 |
self.assertEqual(0, getargs_p(0.0)) |
| 541 |
self.assertEqual(0, getargs_p(0j)) |
| 542 |
self.assertEqual(0, getargs_p('')) |
| 543 |
self.assertEqual(0, getargs_p(())) |
| 544 |
self.assertEqual(0, getargs_p([])) |
| 545 |
self.assertEqual(0, getargs_p({})) |
| 546 |
|
| 547 |
self.assertEqual(1, getargs_p(True)) |
| 548 |
self.assertEqual(1, getargs_p(1)) |
| 549 |
self.assertEqual(1, getargs_p(1.0)) |
| 550 |
self.assertEqual(1, getargs_p(1j)) |
| 551 |
self.assertEqual(1, getargs_p('x')) |
| 552 |
self.assertEqual(1, getargs_p((1,))) |
| 553 |
self.assertEqual(1, getargs_p([1])) |
| 554 |
self.assertEqual(1, getargs_p({1:2})) |
| 555 |
self.assertEqual(1, getargs_p(unittest.TestCase)) |
| 556 |
|
| 557 |
self.assertRaises(NotImplementedError, getargs_p, Paradox()) |
| 558 |
|
| 559 |
|
| 560 |
class Tuple_TestCase(unittest.TestCase): |
| 561 |
def test_args(self): |
| 562 |
from _testcapi import get_args |
| 563 |
|
| 564 |
ret = get_args(1, 2) |
| 565 |
self.assertEqual(ret, (1, 2)) |
| 566 |
self.assertIs(type(ret), tuple) |
| 567 |
|
| 568 |
ret = get_args(1, *(2, 3)) |
| 569 |
self.assertEqual(ret, (1, 2, 3)) |
| 570 |
self.assertIs(type(ret), tuple) |
| 571 |
|
| 572 |
ret = get_args(*[1, 2]) |
| 573 |
self.assertEqual(ret, (1, 2)) |
| 574 |
self.assertIs(type(ret), tuple) |
| 575 |
|
| 576 |
ret = get_args(*TupleSubclass([1, 2])) |
| 577 |
self.assertEqual(ret, (1, 2)) |
| 578 |
self.assertIs(type(ret), tuple) |
| 579 |
|
| 580 |
ret = get_args() |
| 581 |
self.assertIn(ret, ((), None)) |
| 582 |
self.assertIn(type(ret), (tuple, type(None))) |
| 583 |
|
| 584 |
ret = get_args(*()) |
| 585 |
self.assertIn(ret, ((), None)) |
| 586 |
self.assertIn(type(ret), (tuple, type(None))) |
| 587 |
|
| 588 |
def test_tuple(self): |
| 589 |
from _testcapi import getargs_tuple |
| 590 |
|
| 591 |
ret = getargs_tuple(1, (2, 3)) |
| 592 |
self.assertEqual(ret, (1,2,3)) |
| 593 |
|
| 594 |
# make sure invalid sequence arguments are handled correctly |
| 595 |
class TestSeq: |
| 596 |
def __len__(self): |
| 597 |
return 2 |
| 598 |
def __getitem__(self, n): |
| 599 |
raise CustomError |
| 600 |
self.assertRaises(CustomError, getargs_tuple, 1, TestSeq()) |
| 601 |
|
| 602 |
class Keywords_TestCase(unittest.TestCase): |
| 603 |
def test_kwargs(self): |
| 604 |
from _testcapi import get_kwargs |
| 605 |
|
| 606 |
ret = get_kwargs(a=1, b=2) |
| 607 |
self.assertEqual(ret, {'a': 1, 'b': 2}) |
| 608 |
self.assertIs(type(ret), dict) |
| 609 |
|
| 610 |
ret = get_kwargs(a=1, **{'b': 2, 'c': 3}) |
| 611 |
self.assertEqual(ret, {'a': 1, 'b': 2, 'c': 3}) |
| 612 |
self.assertIs(type(ret), dict) |
| 613 |
|
| 614 |
ret = get_kwargs(**DictSubclass({'a': 1, 'b': 2})) |
| 615 |
self.assertEqual(ret, {'a': 1, 'b': 2}) |
| 616 |
self.assertIs(type(ret), dict) |
| 617 |
|
| 618 |
ret = get_kwargs() |
| 619 |
self.assertIn(ret, ({}, None)) |
| 620 |
self.assertIn(type(ret), (dict, type(None))) |
| 621 |
|
| 622 |
ret = get_kwargs(**{}) |
| 623 |
self.assertIn(ret, ({}, None)) |
| 624 |
self.assertIn(type(ret), (dict, type(None))) |
| 625 |
|
| 626 |
def test_positional_args(self): |
| 627 |
# using all positional args |
| 628 |
self.assertEqual( |
| 629 |
getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), 10), |
| 630 |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 631 |
) |
| 632 |
|
| 633 |
def test_mixed_args(self): |
| 634 |
# positional and keyword args |
| 635 |
self.assertEqual( |
| 636 |
getargs_keywords((1,2), 3, (4,(5,6)), arg4=(7,8,9), arg5=10), |
| 637 |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 638 |
) |
| 639 |
|
| 640 |
def test_keyword_args(self): |
| 641 |
# all keywords |
| 642 |
self.assertEqual( |
| 643 |
getargs_keywords(arg1=(1,2), arg2=3, arg3=(4,(5,6)), arg4=(7,8,9), arg5=10), |
| 644 |
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) |
| 645 |
) |
| 646 |
|
| 647 |
def test_optional_args(self): |
| 648 |
# missing optional keyword args, skipping tuples |
| 649 |
self.assertEqual( |
| 650 |
getargs_keywords(arg1=(1,2), arg2=3, arg5=10), |
| 651 |
(1, 2, 3, -1, -1, -1, -1, -1, -1, 10) |
| 652 |
) |
| 653 |
|
| 654 |
def test_required_args(self): |
| 655 |
# required arg missing |
| 656 |
try: |
| 657 |
getargs_keywords(arg1=(1,2)) |
| 658 |
except TypeError as err: |
| 659 |
self.assertEqual( |
| 660 |
str(err), "function missing required argument 'arg2' (pos 2)") |
| 661 |
else: |
| 662 |
self.fail('TypeError should have been raised') |
| 663 |
|
| 664 |
def test_too_many_args(self): |
| 665 |
try: |
| 666 |
getargs_keywords((1,2),3,(4,(5,6)),(7,8,9),10,111) |
| 667 |
except TypeError as err: |
| 668 |
self.assertEqual(str(err), "function takes at most 5 arguments (6 given)") |
| 669 |
else: |
| 670 |
self.fail('TypeError should have been raised') |
| 671 |
|
| 672 |
def test_invalid_keyword(self): |
| 673 |
# extraneous keyword arg |
| 674 |
try: |
| 675 |
getargs_keywords((1,2),3,arg5=10,arg666=666) |
| 676 |
except TypeError as err: |
| 677 |
self.assertEqual(str(err), "this function got an unexpected keyword argument 'arg666'") |
| 678 |
else: |
| 679 |
self.fail('TypeError should have been raised') |
| 680 |
|
| 681 |
def test_surrogate_keyword(self): |
| 682 |
try: |
| 683 |
getargs_keywords((1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
| 684 |
except TypeError as err: |
| 685 |
self.assertEqual(str(err), "this function got an unexpected keyword argument '\udc80'") |
| 686 |
else: |
| 687 |
self.fail('TypeError should have been raised') |
| 688 |
|
| 689 |
class KeywordOnly_TestCase(unittest.TestCase): |
| 690 |
def test_positional_args(self): |
| 691 |
# using all possible positional args |
| 692 |
self.assertEqual( |
| 693 |
getargs_keyword_only(1, 2), |
| 694 |
(1, 2, -1) |
| 695 |
) |
| 696 |
|
| 697 |
def test_mixed_args(self): |
| 698 |
# positional and keyword args |
| 699 |
self.assertEqual( |
| 700 |
getargs_keyword_only(1, 2, keyword_only=3), |
| 701 |
(1, 2, 3) |
| 702 |
) |
| 703 |
|
| 704 |
def test_keyword_args(self): |
| 705 |
# all keywords |
| 706 |
self.assertEqual( |
| 707 |
getargs_keyword_only(required=1, optional=2, keyword_only=3), |
| 708 |
(1, 2, 3) |
| 709 |
) |
| 710 |
|
| 711 |
def test_optional_args(self): |
| 712 |
# missing optional keyword args, skipping tuples |
| 713 |
self.assertEqual( |
| 714 |
getargs_keyword_only(required=1, optional=2), |
| 715 |
(1, 2, -1) |
| 716 |
) |
| 717 |
self.assertEqual( |
| 718 |
getargs_keyword_only(required=1, keyword_only=3), |
| 719 |
(1, -1, 3) |
| 720 |
) |
| 721 |
|
| 722 |
def test_required_args(self): |
| 723 |
self.assertEqual( |
| 724 |
getargs_keyword_only(1), |
| 725 |
(1, -1, -1) |
| 726 |
) |
| 727 |
self.assertEqual( |
| 728 |
getargs_keyword_only(required=1), |
| 729 |
(1, -1, -1) |
| 730 |
) |
| 731 |
# required arg missing |
| 732 |
with self.assertRaisesRegex(TypeError, |
| 733 |
r"function missing required argument 'required' \(pos 1\)"): |
| 734 |
getargs_keyword_only(optional=2) |
| 735 |
|
| 736 |
with self.assertRaisesRegex(TypeError, |
| 737 |
r"function missing required argument 'required' \(pos 1\)"): |
| 738 |
getargs_keyword_only(keyword_only=3) |
| 739 |
|
| 740 |
def test_too_many_args(self): |
| 741 |
with self.assertRaisesRegex(TypeError, |
| 742 |
r"function takes at most 2 positional arguments \(3 given\)"): |
| 743 |
getargs_keyword_only(1, 2, 3) |
| 744 |
|
| 745 |
with self.assertRaisesRegex(TypeError, |
| 746 |
r"function takes at most 3 arguments \(4 given\)"): |
| 747 |
getargs_keyword_only(1, 2, 3, keyword_only=5) |
| 748 |
|
| 749 |
def test_invalid_keyword(self): |
| 750 |
# extraneous keyword arg |
| 751 |
with self.assertRaisesRegex(TypeError, |
| 752 |
"this function got an unexpected keyword argument 'monster'"): |
| 753 |
getargs_keyword_only(1, 2, monster=666) |
| 754 |
|
| 755 |
def test_surrogate_keyword(self): |
| 756 |
with self.assertRaisesRegex(TypeError, |
| 757 |
"this function got an unexpected keyword argument '\udc80'"): |
| 758 |
getargs_keyword_only(1, 2, **{'\uDC80': 10}) |
| 759 |
|
| 760 |
def test_weird_str_subclass(self): |
| 761 |
class BadStr(str): |
| 762 |
def __eq__(self, other): |
| 763 |
return True |
| 764 |
def __hash__(self): |
| 765 |
# Guaranteed different hash |
| 766 |
return str.__hash__(self) ^ 3 |
| 767 |
with self.assertRaisesRegex(TypeError, |
| 768 |
"invalid keyword argument for this function"): |
| 769 |
getargs_keyword_only(1, 2, **{BadStr("keyword_only"): 3}) |
| 770 |
with self.assertRaisesRegex(TypeError, |
| 771 |
"this function got an unexpected keyword argument"): |
| 772 |
getargs_keyword_only(1, 2, **{BadStr("monster"): 666}) |
| 773 |
|
| 774 |
def test_weird_str_subclass2(self): |
| 775 |
class BadStr(str): |
| 776 |
def __eq__(self, other): |
| 777 |
return False |
| 778 |
def __hash__(self): |
| 779 |
return str.__hash__(self) |
| 780 |
with self.assertRaisesRegex(TypeError, |
| 781 |
"invalid keyword argument for this function"): |
| 782 |
getargs_keyword_only(1, 2, **{BadStr("keyword_only"): 3}) |
| 783 |
with self.assertRaisesRegex(TypeError, |
| 784 |
"this function got an unexpected keyword argument"): |
| 785 |
getargs_keyword_only(1, 2, **{BadStr("monster"): 666}) |
| 786 |
|
| 787 |
|
| 788 |
class PositionalOnlyAndKeywords_TestCase(unittest.TestCase): |
| 789 |
from _testcapi import getargs_positional_only_and_keywords as getargs |
| 790 |
|
| 791 |
def test_positional_args(self): |
| 792 |
# using all possible positional args |
| 793 |
self.assertEqual(self.getargs(1, 2, 3), (1, 2, 3)) |
| 794 |
|
| 795 |
def test_mixed_args(self): |
| 796 |
# positional and keyword args |
| 797 |
self.assertEqual(self.getargs(1, 2, keyword=3), (1, 2, 3)) |
| 798 |
|
| 799 |
def test_optional_args(self): |
| 800 |
# missing optional args |
| 801 |
self.assertEqual(self.getargs(1, 2), (1, 2, -1)) |
| 802 |
self.assertEqual(self.getargs(1, keyword=3), (1, -1, 3)) |
| 803 |
|
| 804 |
def test_required_args(self): |
| 805 |
self.assertEqual(self.getargs(1), (1, -1, -1)) |
| 806 |
# required positional arg missing |
| 807 |
with self.assertRaisesRegex(TypeError, |
| 808 |
r"function takes at least 1 positional argument \(0 given\)"): |
| 809 |
self.getargs() |
| 810 |
|
| 811 |
with self.assertRaisesRegex(TypeError, |
| 812 |
r"function takes at least 1 positional argument \(0 given\)"): |
| 813 |
self.getargs(keyword=3) |
| 814 |
|
| 815 |
def test_empty_keyword(self): |
| 816 |
with self.assertRaisesRegex(TypeError, |
| 817 |
"this function got an unexpected keyword argument ''"): |
| 818 |
self.getargs(1, 2, **{'': 666}) |
| 819 |
|
| 820 |
|
| 821 |
class Bytes_TestCase(unittest.TestCase): |
| 822 |
def test_c(self): |
| 823 |
from _testcapi import getargs_c |
| 824 |
self.assertRaises(TypeError, getargs_c, b'abc') # len > 1 |
| 825 |
self.assertEqual(getargs_c(b'a'), 97) |
| 826 |
self.assertEqual(getargs_c(bytearray(b'a')), 97) |
| 827 |
self.assertRaises(TypeError, getargs_c, memoryview(b'a')) |
| 828 |
self.assertRaises(TypeError, getargs_c, 's') |
| 829 |
self.assertRaises(TypeError, getargs_c, 97) |
| 830 |
self.assertRaises(TypeError, getargs_c, None) |
| 831 |
|
| 832 |
def test_y(self): |
| 833 |
from _testcapi import getargs_y |
| 834 |
self.assertRaises(TypeError, getargs_y, 'abc\xe9') |
| 835 |
self.assertEqual(getargs_y(b'bytes'), b'bytes') |
| 836 |
self.assertRaises(ValueError, getargs_y, b'nul:\0') |
| 837 |
self.assertRaises(TypeError, getargs_y, bytearray(b'bytearray')) |
| 838 |
self.assertRaises(TypeError, getargs_y, memoryview(b'memoryview')) |
| 839 |
self.assertRaises(TypeError, getargs_y, None) |
| 840 |
|
| 841 |
def test_y_star(self): |
| 842 |
from _testcapi import getargs_y_star |
| 843 |
self.assertRaises(TypeError, getargs_y_star, 'abc\xe9') |
| 844 |
self.assertEqual(getargs_y_star(b'bytes'), b'bytes') |
| 845 |
self.assertEqual(getargs_y_star(b'nul:\0'), b'nul:\0') |
| 846 |
self.assertEqual(getargs_y_star(bytearray(b'bytearray')), b'bytearray') |
| 847 |
self.assertEqual(getargs_y_star(memoryview(b'memoryview')), b'memoryview') |
| 848 |
self.assertRaises(TypeError, getargs_y_star, None) |
| 849 |
self.assertRaises(BufferError, getargs_y_star, NONCONTIG_WRITABLE) |
| 850 |
self.assertRaises(BufferError, getargs_y_star, NONCONTIG_READONLY) |
| 851 |
|
| 852 |
def test_y_hash(self): |
| 853 |
from _testcapi import getargs_y_hash |
| 854 |
self.assertRaises(TypeError, getargs_y_hash, 'abc\xe9') |
| 855 |
self.assertEqual(getargs_y_hash(b'bytes'), b'bytes') |
| 856 |
self.assertEqual(getargs_y_hash(b'nul:\0'), b'nul:\0') |
| 857 |
self.assertRaises(TypeError, getargs_y_hash, bytearray(b'bytearray')) |
| 858 |
self.assertRaises(TypeError, getargs_y_hash, memoryview(b'memoryview')) |
| 859 |
self.assertRaises(TypeError, getargs_y_hash, None) |
| 860 |
# TypeError: must be read-only bytes-like object, not memoryview |
| 861 |
self.assertRaises(TypeError, getargs_y_hash, NONCONTIG_WRITABLE) |
| 862 |
self.assertRaises(TypeError, getargs_y_hash, NONCONTIG_READONLY) |
| 863 |
|
| 864 |
def test_w_star(self): |
| 865 |
# getargs_w_star() modifies first and last byte |
| 866 |
# getargs_w_star_opt() takes additional optional args: with one |
| 867 |
# argument it should behave the same as getargs_w_star |
| 868 |
from _testcapi import getargs_w_star, getargs_w_star_opt |
| 869 |
for func in (getargs_w_star, getargs_w_star_opt): |
| 870 |
with self.subTest(func=func): |
| 871 |
self.assertRaises(TypeError, func, 'abc\xe9') |
| 872 |
self.assertRaises(TypeError, func, b'bytes') |
| 873 |
self.assertRaises(TypeError, func, b'nul:\0') |
| 874 |
self.assertRaises(TypeError, func, memoryview(b'bytes')) |
| 875 |
buf = bytearray(b'bytearray') |
| 876 |
self.assertEqual(func(buf), b'[ytearra]') |
| 877 |
self.assertEqual(buf, bytearray(b'[ytearra]')) |
| 878 |
buf = bytearray(b'memoryview') |
| 879 |
self.assertEqual(func(memoryview(buf)), b'[emoryvie]') |
| 880 |
self.assertEqual(buf, bytearray(b'[emoryvie]')) |
| 881 |
self.assertRaises(TypeError, func, None) |
| 882 |
self.assertRaises(TypeError, func, NONCONTIG_WRITABLE) |
| 883 |
self.assertRaises(TypeError, func, NONCONTIG_READONLY) |
| 884 |
|
| 885 |
def test_getargs_empty(self): |
| 886 |
from _testcapi import getargs_empty |
| 887 |
self.assertTrue(getargs_empty()) |
| 888 |
self.assertRaises(TypeError, getargs_empty, 1) |
| 889 |
self.assertRaises(TypeError, getargs_empty, 1, 2, 3) |
| 890 |
self.assertRaises(TypeError, getargs_empty, a=1) |
| 891 |
self.assertRaises(TypeError, getargs_empty, a=1, b=2) |
| 892 |
self.assertRaises(TypeError, getargs_empty, 'x', 'y', 'z', a=1, b=2) |
| 893 |
|
| 894 |
|
| 895 |
class String_TestCase(unittest.TestCase): |
| 896 |
def test_C(self): |
| 897 |
from _testcapi import getargs_C |
| 898 |
self.assertRaises(TypeError, getargs_C, 'abc') # len > 1 |
| 899 |
self.assertEqual(getargs_C('a'), 97) |
| 900 |
self.assertEqual(getargs_C('\u20ac'), 0x20ac) |
| 901 |
self.assertEqual(getargs_C('\U0001f40d'), 0x1f40d) |
| 902 |
self.assertRaises(TypeError, getargs_C, b'a') |
| 903 |
self.assertRaises(TypeError, getargs_C, bytearray(b'a')) |
| 904 |
self.assertRaises(TypeError, getargs_C, memoryview(b'a')) |
| 905 |
self.assertRaises(TypeError, getargs_C, 97) |
| 906 |
self.assertRaises(TypeError, getargs_C, None) |
| 907 |
|
| 908 |
def test_s(self): |
| 909 |
from _testcapi import getargs_s |
| 910 |
self.assertEqual(getargs_s('abc\xe9'), b'abc\xc3\xa9') |
| 911 |
self.assertRaises(ValueError, getargs_s, 'nul:\0') |
| 912 |
self.assertRaises(TypeError, getargs_s, b'bytes') |
| 913 |
self.assertRaises(TypeError, getargs_s, bytearray(b'bytearray')) |
| 914 |
self.assertRaises(TypeError, getargs_s, memoryview(b'memoryview')) |
| 915 |
self.assertRaises(TypeError, getargs_s, None) |
| 916 |
|
| 917 |
def test_s_star(self): |
| 918 |
from _testcapi import getargs_s_star |
| 919 |
self.assertEqual(getargs_s_star('abc\xe9'), b'abc\xc3\xa9') |
| 920 |
self.assertEqual(getargs_s_star('nul:\0'), b'nul:\0') |
| 921 |
self.assertEqual(getargs_s_star(b'bytes'), b'bytes') |
| 922 |
self.assertEqual(getargs_s_star(bytearray(b'bytearray')), b'bytearray') |
| 923 |
self.assertEqual(getargs_s_star(memoryview(b'memoryview')), b'memoryview') |
| 924 |
self.assertRaises(TypeError, getargs_s_star, None) |
| 925 |
self.assertRaises(BufferError, getargs_s_star, NONCONTIG_WRITABLE) |
| 926 |
self.assertRaises(BufferError, getargs_s_star, NONCONTIG_READONLY) |
| 927 |
|
| 928 |
def test_s_hash(self): |
| 929 |
from _testcapi import getargs_s_hash |
| 930 |
self.assertEqual(getargs_s_hash('abc\xe9'), b'abc\xc3\xa9') |
| 931 |
self.assertEqual(getargs_s_hash('nul:\0'), b'nul:\0') |
| 932 |
self.assertEqual(getargs_s_hash(b'bytes'), b'bytes') |
| 933 |
self.assertRaises(TypeError, getargs_s_hash, bytearray(b'bytearray')) |
| 934 |
self.assertRaises(TypeError, getargs_s_hash, memoryview(b'memoryview')) |
| 935 |
self.assertRaises(TypeError, getargs_s_hash, None) |
| 936 |
# TypeError: must be read-only bytes-like object, not memoryview |
| 937 |
self.assertRaises(TypeError, getargs_s_hash, NONCONTIG_WRITABLE) |
| 938 |
self.assertRaises(TypeError, getargs_s_hash, NONCONTIG_READONLY) |
| 939 |
|
| 940 |
def test_z(self): |
| 941 |
from _testcapi import getargs_z |
| 942 |
self.assertEqual(getargs_z('abc\xe9'), b'abc\xc3\xa9') |
| 943 |
self.assertRaises(ValueError, getargs_z, 'nul:\0') |
| 944 |
self.assertRaises(TypeError, getargs_z, b'bytes') |
| 945 |
self.assertRaises(TypeError, getargs_z, bytearray(b'bytearray')) |
| 946 |
self.assertRaises(TypeError, getargs_z, memoryview(b'memoryview')) |
| 947 |
self.assertIsNone(getargs_z(None)) |
| 948 |
|
| 949 |
def test_z_star(self): |
| 950 |
from _testcapi import getargs_z_star |
| 951 |
self.assertEqual(getargs_z_star('abc\xe9'), b'abc\xc3\xa9') |
| 952 |
self.assertEqual(getargs_z_star('nul:\0'), b'nul:\0') |
| 953 |
self.assertEqual(getargs_z_star(b'bytes'), b'bytes') |
| 954 |
self.assertEqual(getargs_z_star(bytearray(b'bytearray')), b'bytearray') |
| 955 |
self.assertEqual(getargs_z_star(memoryview(b'memoryview')), b'memoryview') |
| 956 |
self.assertIsNone(getargs_z_star(None)) |
| 957 |
self.assertRaises(BufferError, getargs_z_star, NONCONTIG_WRITABLE) |
| 958 |
self.assertRaises(BufferError, getargs_z_star, NONCONTIG_READONLY) |
| 959 |
|
| 960 |
def test_z_hash(self): |
| 961 |
from _testcapi import getargs_z_hash |
| 962 |
self.assertEqual(getargs_z_hash('abc\xe9'), b'abc\xc3\xa9') |
| 963 |
self.assertEqual(getargs_z_hash('nul:\0'), b'nul:\0') |
| 964 |
self.assertEqual(getargs_z_hash(b'bytes'), b'bytes') |
| 965 |
self.assertRaises(TypeError, getargs_z_hash, bytearray(b'bytearray')) |
| 966 |
self.assertRaises(TypeError, getargs_z_hash, memoryview(b'memoryview')) |
| 967 |
self.assertIsNone(getargs_z_hash(None)) |
| 968 |
# TypeError: must be read-only bytes-like object, not memoryview |
| 969 |
self.assertRaises(TypeError, getargs_z_hash, NONCONTIG_WRITABLE) |
| 970 |
self.assertRaises(TypeError, getargs_z_hash, NONCONTIG_READONLY) |
| 971 |
|
| 972 |
def test_es(self): |
| 973 |
from _testcapi import getargs_es |
| 974 |
self.assertEqual(getargs_es('abc\xe9'), b'abc\xc3\xa9') |
| 975 |
self.assertEqual(getargs_es('abc\xe9', 'latin1'), b'abc\xe9') |
| 976 |
self.assertRaises(UnicodeEncodeError, getargs_es, 'abc\xe9', 'ascii') |
| 977 |
self.assertRaises(LookupError, getargs_es, 'abc\xe9', 'spam') |
| 978 |
self.assertRaises(TypeError, getargs_es, b'bytes', 'latin1') |
| 979 |
self.assertRaises(TypeError, getargs_es, bytearray(b'bytearray'), 'latin1') |
| 980 |
self.assertRaises(TypeError, getargs_es, memoryview(b'memoryview'), 'latin1') |
| 981 |
self.assertRaises(TypeError, getargs_es, None, 'latin1') |
| 982 |
self.assertRaises(TypeError, getargs_es, 'nul:\0', 'latin1') |
| 983 |
|
| 984 |
def test_et(self): |
| 985 |
from _testcapi import getargs_et |
| 986 |
self.assertEqual(getargs_et('abc\xe9'), b'abc\xc3\xa9') |
| 987 |
self.assertEqual(getargs_et('abc\xe9', 'latin1'), b'abc\xe9') |
| 988 |
self.assertRaises(UnicodeEncodeError, getargs_et, 'abc\xe9', 'ascii') |
| 989 |
self.assertRaises(LookupError, getargs_et, 'abc\xe9', 'spam') |
| 990 |
self.assertEqual(getargs_et(b'bytes', 'latin1'), b'bytes') |
| 991 |
self.assertEqual(getargs_et(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 992 |
self.assertRaises(TypeError, getargs_et, memoryview(b'memoryview'), 'latin1') |
| 993 |
self.assertRaises(TypeError, getargs_et, None, 'latin1') |
| 994 |
self.assertRaises(TypeError, getargs_et, 'nul:\0', 'latin1') |
| 995 |
self.assertRaises(TypeError, getargs_et, b'nul:\0', 'latin1') |
| 996 |
self.assertRaises(TypeError, getargs_et, bytearray(b'nul:\0'), 'latin1') |
| 997 |
|
| 998 |
def test_es_hash(self): |
| 999 |
from _testcapi import getargs_es_hash |
| 1000 |
self.assertEqual(getargs_es_hash('abc\xe9'), b'abc\xc3\xa9') |
| 1001 |
self.assertEqual(getargs_es_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 1002 |
self.assertRaises(UnicodeEncodeError, getargs_es_hash, 'abc\xe9', 'ascii') |
| 1003 |
self.assertRaises(LookupError, getargs_es_hash, 'abc\xe9', 'spam') |
| 1004 |
self.assertRaises(TypeError, getargs_es_hash, b'bytes', 'latin1') |
| 1005 |
self.assertRaises(TypeError, getargs_es_hash, bytearray(b'bytearray'), 'latin1') |
| 1006 |
self.assertRaises(TypeError, getargs_es_hash, memoryview(b'memoryview'), 'latin1') |
| 1007 |
self.assertRaises(TypeError, getargs_es_hash, None, 'latin1') |
| 1008 |
self.assertEqual(getargs_es_hash('nul:\0', 'latin1'), b'nul:\0') |
| 1009 |
|
| 1010 |
buf = bytearray(b'x'*8) |
| 1011 |
self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 1012 |
self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 1013 |
buf = bytearray(b'x'*5) |
| 1014 |
self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 1015 |
self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 1016 |
buf = bytearray(b'x'*4) |
| 1017 |
self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
| 1018 |
self.assertEqual(buf, bytearray(b'x'*4)) |
| 1019 |
buf = bytearray() |
| 1020 |
self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf) |
| 1021 |
|
| 1022 |
def test_et_hash(self): |
| 1023 |
from _testcapi import getargs_et_hash |
| 1024 |
self.assertEqual(getargs_et_hash('abc\xe9'), b'abc\xc3\xa9') |
| 1025 |
self.assertEqual(getargs_et_hash('abc\xe9', 'latin1'), b'abc\xe9') |
| 1026 |
self.assertRaises(UnicodeEncodeError, getargs_et_hash, 'abc\xe9', 'ascii') |
| 1027 |
self.assertRaises(LookupError, getargs_et_hash, 'abc\xe9', 'spam') |
| 1028 |
self.assertEqual(getargs_et_hash(b'bytes', 'latin1'), b'bytes') |
| 1029 |
self.assertEqual(getargs_et_hash(bytearray(b'bytearray'), 'latin1'), b'bytearray') |
| 1030 |
self.assertRaises(TypeError, getargs_et_hash, memoryview(b'memoryview'), 'latin1') |
| 1031 |
self.assertRaises(TypeError, getargs_et_hash, None, 'latin1') |
| 1032 |
self.assertEqual(getargs_et_hash('nul:\0', 'latin1'), b'nul:\0') |
| 1033 |
self.assertEqual(getargs_et_hash(b'nul:\0', 'latin1'), b'nul:\0') |
| 1034 |
self.assertEqual(getargs_et_hash(bytearray(b'nul:\0'), 'latin1'), b'nul:\0') |
| 1035 |
|
| 1036 |
buf = bytearray(b'x'*8) |
| 1037 |
self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 1038 |
self.assertEqual(buf, bytearray(b'abc\xe9\x00xxx')) |
| 1039 |
buf = bytearray(b'x'*5) |
| 1040 |
self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9') |
| 1041 |
self.assertEqual(buf, bytearray(b'abc\xe9\x00')) |
| 1042 |
buf = bytearray(b'x'*4) |
| 1043 |
self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 1044 |
self.assertEqual(buf, bytearray(b'x'*4)) |
| 1045 |
buf = bytearray() |
| 1046 |
self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf) |
| 1047 |
|
| 1048 |
def test_gh_99240_clear_args(self): |
| 1049 |
from _testcapi import gh_99240_clear_args |
| 1050 |
self.assertRaises(TypeError, gh_99240_clear_args, 'a', '\0b') |
| 1051 |
|
| 1052 |
|
| 1053 |
class Object_TestCase(unittest.TestCase): |
| 1054 |
def test_S(self): |
| 1055 |
from _testcapi import getargs_S |
| 1056 |
obj = b'bytes' |
| 1057 |
self.assertIs(getargs_S(obj), obj) |
| 1058 |
self.assertRaises(TypeError, getargs_S, bytearray(b'bytearray')) |
| 1059 |
self.assertRaises(TypeError, getargs_S, 'str') |
| 1060 |
self.assertRaises(TypeError, getargs_S, None) |
| 1061 |
self.assertRaises(TypeError, getargs_S, memoryview(obj)) |
| 1062 |
|
| 1063 |
def test_Y(self): |
| 1064 |
from _testcapi import getargs_Y |
| 1065 |
obj = bytearray(b'bytearray') |
| 1066 |
self.assertIs(getargs_Y(obj), obj) |
| 1067 |
self.assertRaises(TypeError, getargs_Y, b'bytes') |
| 1068 |
self.assertRaises(TypeError, getargs_Y, 'str') |
| 1069 |
self.assertRaises(TypeError, getargs_Y, None) |
| 1070 |
self.assertRaises(TypeError, getargs_Y, memoryview(obj)) |
| 1071 |
|
| 1072 |
def test_U(self): |
| 1073 |
from _testcapi import getargs_U |
| 1074 |
obj = 'str' |
| 1075 |
self.assertIs(getargs_U(obj), obj) |
| 1076 |
self.assertRaises(TypeError, getargs_U, b'bytes') |
| 1077 |
self.assertRaises(TypeError, getargs_U, bytearray(b'bytearray')) |
| 1078 |
self.assertRaises(TypeError, getargs_U, None) |
| 1079 |
|
| 1080 |
|
| 1081 |
# Bug #6012 |
| 1082 |
class Test6012(unittest.TestCase): |
| 1083 |
def test(self): |
| 1084 |
self.assertEqual(_testcapi.argparsing("Hello", "World"), 1) |
| 1085 |
|
| 1086 |
|
| 1087 |
class SkipitemTest(unittest.TestCase): |
| 1088 |
|
| 1089 |
# u, and Z raises DeprecationWarning |
| 1090 |
@warnings_helper.ignore_warnings(category=DeprecationWarning) |
| 1091 |
def test_skipitem(self): |
| 1092 |
""" |
| 1093 |
If this test failed, you probably added a new "format unit" |
| 1094 |
in Python/getargs.c, but neglected to update our poor friend |
| 1095 |
skipitem() in the same file. (If so, shame on you!) |
| 1096 |
|
| 1097 |
With a few exceptions**, this function brute-force tests all |
| 1098 |
printable ASCII*** characters (32 to 126 inclusive) as format units, |
| 1099 |
checking to see that PyArg_ParseTupleAndKeywords() return consistent |
| 1100 |
errors both when the unit is attempted to be used and when it is |
| 1101 |
skipped. If the format unit doesn't exist, we'll get one of two |
| 1102 |
specific error messages (one for used, one for skipped); if it does |
| 1103 |
exist we *won't* get that error--we'll get either no error or some |
| 1104 |
other error. If we get the specific "does not exist" error for one |
| 1105 |
test and not for the other, there's a mismatch, and the test fails. |
| 1106 |
|
| 1107 |
** Some format units have special funny semantics and it would |
| 1108 |
be difficult to accommodate them here. Since these are all |
| 1109 |
well-established and properly skipped in skipitem() we can |
| 1110 |
get away with not testing them--this test is really intended |
| 1111 |
to catch *new* format units. |
| 1112 |
|
| 1113 |
*** Python C source files must be ASCII. Therefore it's impossible |
| 1114 |
to have non-ASCII format units. |
| 1115 |
|
| 1116 |
""" |
| 1117 |
empty_tuple = () |
| 1118 |
tuple_1 = (0,) |
| 1119 |
dict_b = {'b':1} |
| 1120 |
keywords = ["a", "b"] |
| 1121 |
|
| 1122 |
for i in range(32, 127): |
| 1123 |
c = chr(i) |
| 1124 |
|
| 1125 |
# skip parentheses, the error reporting is inconsistent about them |
| 1126 |
# skip 'e' and 'w', they're always two-character codes |
| 1127 |
# skip '|' and '$', they don't represent arguments anyway |
| 1128 |
if c in '()ew|$': |
| 1129 |
continue |
| 1130 |
|
| 1131 |
# test the format unit when not skipped |
| 1132 |
format = c + "i" |
| 1133 |
try: |
| 1134 |
_testcapi.parse_tuple_and_keywords(tuple_1, dict_b, |
| 1135 |
format, keywords) |
| 1136 |
when_not_skipped = False |
| 1137 |
except SystemError as e: |
| 1138 |
s = "argument 1 (impossible<bad format char>)" |
| 1139 |
when_not_skipped = (str(e) == s) |
| 1140 |
except TypeError: |
| 1141 |
when_not_skipped = False |
| 1142 |
|
| 1143 |
# test the format unit when skipped |
| 1144 |
optional_format = "|" + format |
| 1145 |
try: |
| 1146 |
_testcapi.parse_tuple_and_keywords(empty_tuple, dict_b, |
| 1147 |
optional_format, keywords) |
| 1148 |
when_skipped = False |
| 1149 |
except SystemError as e: |
| 1150 |
s = "impossible<bad format char>: '{}'".format(format) |
| 1151 |
when_skipped = (str(e) == s) |
| 1152 |
|
| 1153 |
message = ("test_skipitem_parity: " |
| 1154 |
"detected mismatch between convertsimple and skipitem " |
| 1155 |
"for format unit '{}' ({}), not skipped {}, skipped {}".format( |
| 1156 |
c, i, when_skipped, when_not_skipped)) |
| 1157 |
self.assertIs(when_skipped, when_not_skipped, message) |
| 1158 |
|
| 1159 |
def test_skipitem_with_suffix(self): |
| 1160 |
parse = _testcapi.parse_tuple_and_keywords |
| 1161 |
empty_tuple = () |
| 1162 |
tuple_1 = (0,) |
| 1163 |
dict_b = {'b':1} |
| 1164 |
keywords = ["a", "b"] |
| 1165 |
|
| 1166 |
supported = ('s#', 's*', 'z#', 'z*', 'y#', 'y*', 'w*') |
| 1167 |
for c in string.ascii_letters: |
| 1168 |
for c2 in '#*': |
| 1169 |
f = c + c2 |
| 1170 |
with self.subTest(format=f): |
| 1171 |
optional_format = "|" + f + "i" |
| 1172 |
if f in supported: |
| 1173 |
parse(empty_tuple, dict_b, optional_format, keywords) |
| 1174 |
else: |
| 1175 |
with self.assertRaisesRegex(SystemError, |
| 1176 |
'impossible<bad format char>'): |
| 1177 |
parse(empty_tuple, dict_b, optional_format, keywords) |
| 1178 |
|
| 1179 |
for c in map(chr, range(32, 128)): |
| 1180 |
f = 'e' + c |
| 1181 |
optional_format = "|" + f + "i" |
| 1182 |
with self.subTest(format=f): |
| 1183 |
if c in 'st': |
| 1184 |
parse(empty_tuple, dict_b, optional_format, keywords) |
| 1185 |
else: |
| 1186 |
with self.assertRaisesRegex(SystemError, |
| 1187 |
'impossible<bad format char>'): |
| 1188 |
parse(empty_tuple, dict_b, optional_format, keywords) |
| 1189 |
|
| 1190 |
|
| 1191 |
class ParseTupleAndKeywords_Test(unittest.TestCase): |
| 1192 |
|
| 1193 |
def test_parse_tuple_and_keywords(self): |
| 1194 |
# Test handling errors in the parse_tuple_and_keywords helper itself |
| 1195 |
self.assertRaises(TypeError, _testcapi.parse_tuple_and_keywords, |
| 1196 |
(), {}, 42, []) |
| 1197 |
self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 1198 |
(), {}, '', 42) |
| 1199 |
self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 1200 |
(), {}, '', [''] * 42) |
| 1201 |
self.assertRaises(ValueError, _testcapi.parse_tuple_and_keywords, |
| 1202 |
(), {}, '', [42]) |
| 1203 |
|
| 1204 |
def test_basic(self): |
| 1205 |
parse = _testcapi.parse_tuple_and_keywords |
| 1206 |
|
| 1207 |
self.assertEqual(parse((), {'a': 1}, 'O', ['a']), (1,)) |
| 1208 |
self.assertEqual(parse((), {}, '|O', ['a']), (NULL,)) |
| 1209 |
self.assertEqual(parse((1, 2), {}, 'OO', ['a', 'b']), (1, 2)) |
| 1210 |
self.assertEqual(parse((1,), {'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1211 |
self.assertEqual(parse((), {'a': 1, 'b': 2}, 'OO', ['a', 'b']), (1, 2)) |
| 1212 |
self.assertEqual(parse((), {'b': 2}, '|OO', ['a', 'b']), (NULL, 2)) |
| 1213 |
|
| 1214 |
with self.assertRaisesRegex(TypeError, |
| 1215 |
"function missing required argument 'a'"): |
| 1216 |
parse((), {}, 'O', ['a']) |
| 1217 |
with self.assertRaisesRegex(TypeError, |
| 1218 |
"this function got an unexpected keyword argument 'b'"): |
| 1219 |
parse((), {'b': 1}, '|O', ['a']) |
| 1220 |
with self.assertRaisesRegex(TypeError, |
| 1221 |
fr"argument for function given by name \('a'\) " |
| 1222 |
fr"and position \(1\)"): |
| 1223 |
parse((1,), {'a': 2}, 'O|O', ['a', 'b']) |
| 1224 |
|
| 1225 |
def test_bad_use(self): |
| 1226 |
# Test handling invalid format and keywords in |
| 1227 |
# PyArg_ParseTupleAndKeywords() |
| 1228 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1229 |
(1,), {}, '||O', ['a']) |
| 1230 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1231 |
(1, 2), {}, '|O|O', ['a', 'b']) |
| 1232 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1233 |
(), {'a': 1}, '$$O', ['a']) |
| 1234 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1235 |
(), {'a': 1, 'b': 2}, '$O$O', ['a', 'b']) |
| 1236 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1237 |
(), {'a': 1}, '$|O', ['a']) |
| 1238 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1239 |
(), {'a': 1, 'b': 2}, '$O|O', ['a', 'b']) |
| 1240 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1241 |
(1,), {}, '|O', ['a', 'b']) |
| 1242 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1243 |
(1,), {}, '|OO', ['a']) |
| 1244 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1245 |
(), {}, '|$O', ['']) |
| 1246 |
self.assertRaises(SystemError, _testcapi.parse_tuple_and_keywords, |
| 1247 |
(), {}, '|OO', ['a', '']) |
| 1248 |
|
| 1249 |
def test_positional_only(self): |
| 1250 |
parse = _testcapi.parse_tuple_and_keywords |
| 1251 |
|
| 1252 |
self.assertEqual(parse((1, 2, 3), {}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
| 1253 |
self.assertEqual(parse((1, 2), {'a': 3}, 'OOO', ['', '', 'a']), (1, 2, 3)) |
| 1254 |
with self.assertRaisesRegex(TypeError, |
| 1255 |
r'function takes at least 2 positional arguments \(1 given\)'): |
| 1256 |
parse((1,), {'a': 3}, 'OOO', ['', '', 'a']) |
| 1257 |
self.assertEqual(parse((1,), {}, 'O|OO', ['', '', 'a']), |
| 1258 |
(1, NULL, NULL)) |
| 1259 |
with self.assertRaisesRegex(TypeError, |
| 1260 |
r'function takes at least 1 positional argument \(0 given\)'): |
| 1261 |
parse((), {}, 'O|OO', ['', '', 'a']) |
| 1262 |
self.assertEqual(parse((1, 2), {'a': 3}, 'OO$O', ['', '', 'a']), |
| 1263 |
(1, 2, 3)) |
| 1264 |
with self.assertRaisesRegex(TypeError, |
| 1265 |
r'function takes exactly 2 positional arguments \(1 given\)'): |
| 1266 |
parse((1,), {'a': 3}, 'OO$O', ['', '', 'a']) |
| 1267 |
self.assertEqual(parse((1,), {}, 'O|O$O', ['', '', 'a']), |
| 1268 |
(1, NULL, NULL)) |
| 1269 |
with self.assertRaisesRegex(TypeError, |
| 1270 |
r'function takes at least 1 positional argument \(0 given\)'): |
| 1271 |
parse((), {}, 'O|O$O', ['', '', 'a']) |
| 1272 |
with self.assertRaisesRegex(SystemError, r'Empty parameter name after \$'): |
| 1273 |
parse((1,), {}, 'O|$OO', ['', '', 'a']) |
| 1274 |
with self.assertRaisesRegex(SystemError, 'Empty keyword'): |
| 1275 |
parse((1,), {}, 'O|OO', ['', 'a', '']) |
| 1276 |
|
| 1277 |
def test_nonascii_keywords(self): |
| 1278 |
parse = _testcapi.parse_tuple_and_keywords |
| 1279 |
|
| 1280 |
for name in ('a', 'ä', 'ŷ', '㷷', '𐀀'): |
| 1281 |
with self.subTest(name=name): |
| 1282 |
self.assertEqual(parse((), {name: 1}, 'O', [name]), (1,)) |
| 1283 |
self.assertEqual(parse((), {}, '|O', [name]), (NULL,)) |
| 1284 |
with self.assertRaisesRegex(TypeError, |
| 1285 |
f"function missing required argument '{name}'"): |
| 1286 |
parse((), {}, 'O', [name]) |
| 1287 |
with self.assertRaisesRegex(TypeError, |
| 1288 |
fr"argument for function given by name \('{name}'\) " |
| 1289 |
fr"and position \(1\)"): |
| 1290 |
parse((1,), {name: 2}, 'O|O', [name, 'b']) |
| 1291 |
with self.assertRaisesRegex(TypeError, |
| 1292 |
f"this function got an unexpected keyword argument '{name}'"): |
| 1293 |
parse((), {name: 1}, '|O', ['b']) |
| 1294 |
with self.assertRaisesRegex(TypeError, |
| 1295 |
"this function got an unexpected keyword argument 'b'"): |
| 1296 |
parse((), {'b': 1}, '|O', [name]) |
| 1297 |
|
| 1298 |
invalid = name.encode() + (name.encode()[:-1] or b'\x80') |
| 1299 |
self.assertEqual(parse((), {}, '|O', [invalid]), (NULL,)) |
| 1300 |
self.assertEqual(parse((1,), {'b': 2}, 'O|O', [invalid, 'b']), |
| 1301 |
(1, 2)) |
| 1302 |
with self.assertRaisesRegex(TypeError, |
| 1303 |
f"function missing required argument '{name}\ufffd'"): |
| 1304 |
parse((), {}, 'O', [invalid]) |
| 1305 |
with self.assertRaisesRegex(UnicodeDecodeError, |
| 1306 |
f"'utf-8' codec can't decode bytes? "): |
| 1307 |
parse((), {'b': 1}, '|OO', [invalid, 'b']) |
| 1308 |
with self.assertRaisesRegex(UnicodeDecodeError, |
| 1309 |
f"'utf-8' codec can't decode bytes? "): |
| 1310 |
parse((), {'b': 1}, '|O', [invalid]) |
| 1311 |
|
| 1312 |
for name2 in ('b', 'ë', 'ĉ', 'Ɐ', '𐀁'): |
| 1313 |
with self.subTest(name2=name2): |
| 1314 |
with self.assertRaisesRegex(TypeError, |
| 1315 |
f"this function got an unexpected keyword argument '{name2}'"): |
| 1316 |
parse((), {name2: 1}, '|O', [name]) |
| 1317 |
|
| 1318 |
name2 = name.encode().decode('latin1') |
| 1319 |
if name2 != name: |
| 1320 |
with self.assertRaisesRegex(TypeError, |
| 1321 |
f"this function got an unexpected keyword argument '{name2}'"): |
| 1322 |
parse((), {name2: 1}, '|O', [name]) |
| 1323 |
name3 = name + '3' |
| 1324 |
with self.assertRaisesRegex(TypeError, |
| 1325 |
f"this function got an unexpected keyword argument '{name2}'"): |
| 1326 |
parse((), {name2: 1, name3: 2}, '|OO', [name, name3]) |
| 1327 |
|
| 1328 |
def test_nested_sequence(self): |
| 1329 |
parse = _testcapi.parse_tuple_and_keywords |
| 1330 |
|
| 1331 |
self.assertEqual(parse(((1, 2, 3),), {}, '(OOO)', ['a']), (1, 2, 3)) |
| 1332 |
self.assertEqual(parse((1, (2, 3), 4), {}, 'O(OO)O', ['a', 'b', 'c']), |
| 1333 |
(1, 2, 3, 4)) |
| 1334 |
parse(((1, 2, 3),), {}, '(iii)', ['a']) |
| 1335 |
parse(([1, 2, 3],), {}, '(iii)', ['a']) |
| 1336 |
|
| 1337 |
with self.assertRaisesRegex(TypeError, |
| 1338 |
"argument 1 must be tuple of length 2, not 3"): |
| 1339 |
parse(((1, 2, 3),), {}, '(ii)', ['a']) |
| 1340 |
with self.assertRaisesRegex(TypeError, |
| 1341 |
"argument 1 must be tuple of length 2, not 1"): |
| 1342 |
parse(((1,),), {}, '(ii)', ['a']) |
| 1343 |
with self.assertRaisesRegex(TypeError, |
| 1344 |
"argument 1 must be sequence of length 2, not 3"): |
| 1345 |
parse(([1, 2, 3],), {}, '(ii)', ['a']) |
| 1346 |
with self.assertRaisesRegex(TypeError, |
| 1347 |
"argument 1 must be sequence of length 2, not 1"): |
| 1348 |
parse(([1,],), {}, '(ii)', ['a']) |
| 1349 |
with self.assertRaisesRegex(TypeError, |
| 1350 |
"argument 1 must be 2-item tuple, not int"): |
| 1351 |
parse((1,), {}, '(ii)', ['a']) |
| 1352 |
with self.assertRaisesRegex(TypeError, |
| 1353 |
"argument 1 must be 2-item tuple, not None$"): |
| 1354 |
parse((None,), {}, '(ii)', ['a']) |
| 1355 |
with self.assertRaisesRegex(TypeError, |
| 1356 |
"argument 1 must be 2-item tuple, not str"): |
| 1357 |
parse(('ab',), {}, '(CC)', ['a']) |
| 1358 |
with self.assertRaisesRegex(TypeError, |
| 1359 |
"argument 1 must be 2-item tuple, not bytes"): |
| 1360 |
parse((b'ab',), {}, '(ii)', ['a']) |
| 1361 |
with self.assertRaisesRegex(TypeError, |
| 1362 |
"argument 1 must be 2-item tuple, not bytearray"): |
| 1363 |
parse((bytearray(b'ab'),), {}, '(ii)', ['a']) |
| 1364 |
with self.assertRaisesRegex(TypeError, |
| 1365 |
"argument 1 must be 2-item tuple, not dict"): |
| 1366 |
parse(({},), {}, '(ii)', ['a']) |
| 1367 |
|
| 1368 |
with self.assertWarnsRegex(DeprecationWarning, |
| 1369 |
"argument must be 3-item tuple, not list"): |
| 1370 |
self.assertEqual(parse(([1, 2, 3],), {}, '(OOO)', ['a']), (1, 2, 3)) |
| 1371 |
with self.assertWarnsRegex(DeprecationWarning, |
| 1372 |
"argument must be 2-item tuple, not list"): |
| 1373 |
with self.assertRaisesRegex(TypeError, |
| 1374 |
"argument 1 must be tuple of length 2, not 3"): |
| 1375 |
parse(([1, 2, 3],), {}, '(OO)', ['a']) |
| 1376 |
with self.assertWarnsRegex(DeprecationWarning, |
| 1377 |
"argument must be 2-item tuple, not list"): |
| 1378 |
with self.assertRaisesRegex(TypeError, |
| 1379 |
"argument 1 must be tuple of length 2, not 1"): |
| 1380 |
parse(([1,],), {}, '(OO)', ['a']) |
| 1381 |
|
| 1382 |
for f in 'es', 'et', 'es#', 'et#': |
| 1383 |
with self.assertRaises(LookupError): # empty encoding "" |
| 1384 |
parse((('a',),), {}, '(' + f + ')', ['a']) |
| 1385 |
with self.assertRaisesRegex(TypeError, |
| 1386 |
"argument 1 must be tuple of length 1, not 0"): |
| 1387 |
parse(((),), {}, '(' + f + ')', ['a']) |
| 1388 |
with self.assertRaisesRegex(TypeError, |
| 1389 |
"argument 1 must be sequence of length 1, not 0"): |
| 1390 |
parse(([],), {}, '(' + f + ')', ['a']) |
| 1391 |
|
| 1392 |
@unittest.skipIf(_testinternalcapi is None, 'needs _testinternalcapi') |
| 1393 |
def test_gh_119213(self): |
| 1394 |
rc, out, err = script_helper.assert_python_ok("-c", """if True: |
| 1395 |
from test import support |
| 1396 |
script = '''if True: |
| 1397 |
import _testinternalcapi |
| 1398 |
_testinternalcapi.gh_119213_getargs(spam='eggs') |
| 1399 |
''' |
| 1400 |
config = dict( |
| 1401 |
allow_fork=False, |
| 1402 |
allow_exec=False, |
| 1403 |
allow_threads=True, |
| 1404 |
allow_daemon_threads=False, |
| 1405 |
use_main_obmalloc=False, |
| 1406 |
gil=2, |
| 1407 |
check_multi_interp_extensions=True, |
| 1408 |
) |
| 1409 |
rc = support.run_in_subinterp_with_config(script, **config) |
| 1410 |
assert rc == 0 |
| 1411 |
|
| 1412 |
# The crash is different if the interpreter was not destroyed first. |
| 1413 |
#interpid = _testinternalcapi.create_interpreter() |
| 1414 |
#rc = _testinternalcapi.exec_interpreter(interpid, script) |
| 1415 |
#assert rc == 0 |
| 1416 |
""") |
| 1417 |
self.assertEqual(rc, 0) |
| 1418 |
|
| 1419 |
|
| 1420 |
if __name__ == "__main__": |
| 1421 |
unittest.main() |