| 1 |
import unittest |
| 2 |
from test.support import (cpython_only, is_wasi, requires_limited_api, Py_DEBUG, |
| 3 |
set_recursion_limit, skip_on_s390x, |
| 4 |
skip_emscripten_stack_overflow, |
| 5 |
skip_wasi_stack_overflow, skip_if_sanitizer, |
| 6 |
import_helper) |
| 7 |
try: |
| 8 |
import _testcapi |
| 9 |
except ImportError: |
| 10 |
_testcapi = None |
| 11 |
try: |
| 12 |
import _testlimitedcapi |
| 13 |
except ImportError: |
| 14 |
_testlimitedcapi = None |
| 15 |
try: |
| 16 |
import _testinternalcapi |
| 17 |
except ImportError: |
| 18 |
_testinternalcapi = None |
| 19 |
import struct |
| 20 |
import collections |
| 21 |
import itertools |
| 22 |
import gc |
| 23 |
import contextlib |
| 24 |
import types |
| 25 |
|
| 26 |
|
| 27 |
class BadStr(str): |
| 28 |
def __eq__(self, other): |
| 29 |
return True |
| 30 |
def __hash__(self): |
| 31 |
# Guaranteed different hash |
| 32 |
return str.__hash__(self) ^ 3 |
| 33 |
|
| 34 |
|
| 35 |
class FunctionCalls(unittest.TestCase): |
| 36 |
|
| 37 |
def test_kwargs_order(self): |
| 38 |
# bpo-34320: **kwargs should preserve order of passed OrderedDict |
| 39 |
od = collections.OrderedDict([('a', 1), ('b', 2)]) |
| 40 |
od.move_to_end('a') |
| 41 |
expected = list(od.items()) |
| 42 |
|
| 43 |
def fn(**kw): |
| 44 |
return kw |
| 45 |
|
| 46 |
res = fn(**od) |
| 47 |
self.assertIsInstance(res, dict) |
| 48 |
self.assertEqual(list(res.items()), expected) |
| 49 |
|
| 50 |
def test_frames_are_popped_after_failed_calls(self): |
| 51 |
# GH-93252: stuff blows up if we don't pop the new frame after |
| 52 |
# recovering from failed calls: |
| 53 |
def f(): |
| 54 |
pass |
| 55 |
class C: |
| 56 |
def m(self): |
| 57 |
pass |
| 58 |
callables = [f, C.m, [].__len__] |
| 59 |
for c in callables: |
| 60 |
for _ in range(1000): |
| 61 |
try: |
| 62 |
c(None) |
| 63 |
except TypeError: |
| 64 |
pass |
| 65 |
# BOOM! |
| 66 |
|
| 67 |
|
| 68 |
@cpython_only |
| 69 |
class CFunctionCallsErrorMessages(unittest.TestCase): |
| 70 |
|
| 71 |
def test_varargs0(self): |
| 72 |
msg = r"__contains__\(\) takes exactly one argument \(0 given\)" |
| 73 |
self.assertRaisesRegex(TypeError, msg, {}.__contains__) |
| 74 |
|
| 75 |
def test_varargs2(self): |
| 76 |
msg = r"__contains__\(\) takes exactly one argument \(2 given\)" |
| 77 |
self.assertRaisesRegex(TypeError, msg, {}.__contains__, 0, 1) |
| 78 |
|
| 79 |
def test_varargs3(self): |
| 80 |
msg = r"^from_bytes\(\) takes at most 2 positional arguments \(3 given\)" |
| 81 |
self.assertRaisesRegex(TypeError, msg, int.from_bytes, b'a', 'little', False) |
| 82 |
|
| 83 |
def test_varargs1min(self): |
| 84 |
msg = (r"get\(\) takes at least 1 argument \(0 given\)|" |
| 85 |
r"get expected at least 1 argument, got 0") |
| 86 |
self.assertRaisesRegex(TypeError, msg, {}.get) |
| 87 |
|
| 88 |
msg = r"expected 1 argument, got 0" |
| 89 |
self.assertRaisesRegex(TypeError, msg, {}.__delattr__) |
| 90 |
|
| 91 |
def test_varargs2min(self): |
| 92 |
msg = r"getattr expected at least 2 arguments, got 0" |
| 93 |
self.assertRaisesRegex(TypeError, msg, getattr) |
| 94 |
|
| 95 |
def test_varargs1max(self): |
| 96 |
msg = (r"input\(\) takes at most 1 argument \(2 given\)|" |
| 97 |
r"input expected at most 1 argument, got 2") |
| 98 |
self.assertRaisesRegex(TypeError, msg, input, 1, 2) |
| 99 |
|
| 100 |
def test_varargs2max(self): |
| 101 |
msg = (r"get\(\) takes at most 2 arguments \(3 given\)|" |
| 102 |
r"get expected at most 2 arguments, got 3") |
| 103 |
self.assertRaisesRegex(TypeError, msg, {}.get, 1, 2, 3) |
| 104 |
|
| 105 |
def test_varargs1_kw(self): |
| 106 |
msg = r"__contains__\(\) takes no keyword arguments" |
| 107 |
self.assertRaisesRegex(TypeError, msg, {}.__contains__, x=2) |
| 108 |
|
| 109 |
def test_varargs2_kw(self): |
| 110 |
msg = r"__contains__\(\) takes no keyword arguments" |
| 111 |
self.assertRaisesRegex(TypeError, msg, {}.__contains__, x=2, y=2) |
| 112 |
|
| 113 |
def test_varargs3_kw(self): |
| 114 |
msg = r"bool\(\) takes no keyword arguments" |
| 115 |
self.assertRaisesRegex(TypeError, msg, bool, x=2) |
| 116 |
|
| 117 |
def test_varargs4_kw(self): |
| 118 |
msg = r"^(list[.])?index\(\) takes no keyword arguments$" |
| 119 |
self.assertRaisesRegex(TypeError, msg, [].index, x=2) |
| 120 |
|
| 121 |
def test_varargs5_kw(self): |
| 122 |
msg = r"^hasattr\(\) takes no keyword arguments$" |
| 123 |
self.assertRaisesRegex(TypeError, msg, hasattr, x=2) |
| 124 |
|
| 125 |
def test_varargs6_kw(self): |
| 126 |
msg = r"^getattr\(\) takes no keyword arguments$" |
| 127 |
self.assertRaisesRegex(TypeError, msg, getattr, x=2) |
| 128 |
|
| 129 |
def test_varargs7_kw(self): |
| 130 |
msg = r"^next\(\) takes no keyword arguments$" |
| 131 |
self.assertRaisesRegex(TypeError, msg, next, x=2) |
| 132 |
|
| 133 |
def test_varargs8_kw(self): |
| 134 |
msg = r"^_struct[.]pack\(\) takes no keyword arguments$" |
| 135 |
self.assertRaisesRegex(TypeError, msg, struct.pack, x=2) |
| 136 |
|
| 137 |
def test_varargs9_kw(self): |
| 138 |
msg = r"^_struct[.]pack_into\(\) takes no keyword arguments$" |
| 139 |
self.assertRaisesRegex(TypeError, msg, struct.pack_into, x=2) |
| 140 |
|
| 141 |
def test_varargs10_kw(self): |
| 142 |
msg = r"^deque[.]index\(\) takes no keyword arguments$" |
| 143 |
self.assertRaisesRegex(TypeError, msg, collections.deque().index, x=2) |
| 144 |
|
| 145 |
def test_varargs11_kw(self): |
| 146 |
msg = r"^Struct[.]pack\(\) takes no keyword arguments$" |
| 147 |
self.assertRaisesRegex(TypeError, msg, struct.Struct.pack, struct.Struct(""), x=2) |
| 148 |
|
| 149 |
def test_varargs12_kw(self): |
| 150 |
msg = r"^staticmethod\(\) takes no keyword arguments$" |
| 151 |
self.assertRaisesRegex(TypeError, msg, staticmethod, func=id) |
| 152 |
|
| 153 |
def test_varargs13_kw(self): |
| 154 |
msg = r"^classmethod\(\) takes no keyword arguments$" |
| 155 |
self.assertRaisesRegex(TypeError, msg, classmethod, func=id) |
| 156 |
|
| 157 |
def test_varargs14_kw(self): |
| 158 |
msg = r"^product\(\) takes at most 1 keyword argument \(2 given\)$" |
| 159 |
self.assertRaisesRegex(TypeError, msg, |
| 160 |
itertools.product, 0, repeat=1, foo=2) |
| 161 |
|
| 162 |
def test_varargs15_kw(self): |
| 163 |
msg = r"^ImportError\(\) takes at most 3 keyword arguments \(4 given\)$" |
| 164 |
self.assertRaisesRegex(TypeError, msg, |
| 165 |
ImportError, 0, name=1, path=2, name_from=3, foo=3) |
| 166 |
|
| 167 |
def test_varargs16_kw(self): |
| 168 |
msg = r"^min\(\) takes at most 2 keyword arguments \(3 given\)$" |
| 169 |
self.assertRaisesRegex(TypeError, msg, |
| 170 |
min, 0, default=1, key=2, foo=3) |
| 171 |
|
| 172 |
def test_varargs17_kw(self): |
| 173 |
msg = r"print\(\) got an unexpected keyword argument 'foo'$" |
| 174 |
self.assertRaisesRegex(TypeError, msg, |
| 175 |
print, 0, sep=1, end=2, file=3, flush=4, foo=5) |
| 176 |
|
| 177 |
def test_varargs18_kw(self): |
| 178 |
# _PyArg_UnpackKeywords() with varpos |
| 179 |
msg = r"invalid keyword argument for print\(\)$" |
| 180 |
with self.assertRaisesRegex(TypeError, msg): |
| 181 |
print(0, 1, **{BadStr('foo'): ','}) |
| 182 |
|
| 183 |
def test_varargs19_kw(self): |
| 184 |
# _PyArg_UnpackKeywords() |
| 185 |
msg = r"invalid keyword argument for round\(\)$" |
| 186 |
with self.assertRaisesRegex(TypeError, msg): |
| 187 |
round(1.75, **{BadStr('foo'): 1}) |
| 188 |
|
| 189 |
def test_oldargs0_1(self): |
| 190 |
msg = r"keys\(\) takes no arguments \(1 given\)" |
| 191 |
self.assertRaisesRegex(TypeError, msg, {}.keys, 0) |
| 192 |
|
| 193 |
def test_oldargs0_2(self): |
| 194 |
msg = r"keys\(\) takes no arguments \(2 given\)" |
| 195 |
self.assertRaisesRegex(TypeError, msg, {}.keys, 0, 1) |
| 196 |
|
| 197 |
def test_oldargs0_1_kw(self): |
| 198 |
msg = r"keys\(\) takes no keyword arguments" |
| 199 |
self.assertRaisesRegex(TypeError, msg, {}.keys, x=2) |
| 200 |
|
| 201 |
def test_oldargs0_2_kw(self): |
| 202 |
msg = r"keys\(\) takes no keyword arguments" |
| 203 |
self.assertRaisesRegex(TypeError, msg, {}.keys, x=2, y=2) |
| 204 |
|
| 205 |
def test_oldargs1_0(self): |
| 206 |
msg = r"count\(\) takes exactly one argument \(0 given\)" |
| 207 |
self.assertRaisesRegex(TypeError, msg, [].count) |
| 208 |
|
| 209 |
def test_oldargs1_2(self): |
| 210 |
msg = r"count\(\) takes exactly one argument \(2 given\)" |
| 211 |
self.assertRaisesRegex(TypeError, msg, [].count, 1, 2) |
| 212 |
|
| 213 |
def test_oldargs1_0_kw(self): |
| 214 |
msg = r"count\(\) takes no keyword arguments" |
| 215 |
self.assertRaisesRegex(TypeError, msg, [].count, x=2) |
| 216 |
|
| 217 |
def test_oldargs1_1_kw(self): |
| 218 |
msg = r"count\(\) takes no keyword arguments" |
| 219 |
self.assertRaisesRegex(TypeError, msg, [].count, {}, x=2) |
| 220 |
|
| 221 |
def test_oldargs1_2_kw(self): |
| 222 |
msg = r"count\(\) takes no keyword arguments" |
| 223 |
self.assertRaisesRegex(TypeError, msg, [].count, x=2, y=2) |
| 224 |
|
| 225 |
def test_object_not_callable(self): |
| 226 |
msg = r"^'object' object is not callable$" |
| 227 |
self.assertRaisesRegex(TypeError, msg, object()) |
| 228 |
|
| 229 |
def test_module_not_callable_no_suggestion_0(self): |
| 230 |
msg = r"^'module' object is not callable$" |
| 231 |
self.assertRaisesRegex(TypeError, msg, types.ModuleType("mod")) |
| 232 |
|
| 233 |
def test_module_not_callable_no_suggestion_1(self): |
| 234 |
msg = r"^'module' object is not callable$" |
| 235 |
mod = types.ModuleType("mod") |
| 236 |
mod.mod = 42 |
| 237 |
self.assertRaisesRegex(TypeError, msg, mod) |
| 238 |
|
| 239 |
def test_module_not_callable_no_suggestion_2(self): |
| 240 |
msg = r"^'module' object is not callable$" |
| 241 |
mod = types.ModuleType("mod") |
| 242 |
del mod.__name__ |
| 243 |
self.assertRaisesRegex(TypeError, msg, mod) |
| 244 |
|
| 245 |
def test_module_not_callable_no_suggestion_3(self): |
| 246 |
msg = r"^'module' object is not callable$" |
| 247 |
mod = types.ModuleType("mod") |
| 248 |
mod.__name__ = 42 |
| 249 |
self.assertRaisesRegex(TypeError, msg, mod) |
| 250 |
|
| 251 |
def test_module_not_callable_suggestion(self): |
| 252 |
msg = r"^'module' object is not callable\. Did you mean: 'mod\.mod\(\.\.\.\)'\?$" |
| 253 |
mod = types.ModuleType("mod") |
| 254 |
mod.mod = lambda: ... |
| 255 |
self.assertRaisesRegex(TypeError, msg, mod) |
| 256 |
|
| 257 |
|
| 258 |
@unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 259 |
class TestCallingConventions(unittest.TestCase): |
| 260 |
"""Test calling using various C calling conventions (METH_*) from Python |
| 261 |
|
| 262 |
Subclasses test several kinds of functions (module-level, methods, |
| 263 |
class methods static methods) using these attributes: |
| 264 |
obj: the object that contains tested functions (as attributes) |
| 265 |
expected_self: expected "self" argument to the C function |
| 266 |
|
| 267 |
The base class tests module-level functions. |
| 268 |
""" |
| 269 |
|
| 270 |
def setUp(self): |
| 271 |
self.obj = self.expected_self = _testcapi |
| 272 |
|
| 273 |
def test_varargs(self): |
| 274 |
self.assertEqual( |
| 275 |
self.obj.meth_varargs(1, 2, 3), |
| 276 |
(self.expected_self, (1, 2, 3)), |
| 277 |
) |
| 278 |
|
| 279 |
def test_varargs_ext(self): |
| 280 |
self.assertEqual( |
| 281 |
self.obj.meth_varargs(*(1, 2, 3)), |
| 282 |
(self.expected_self, (1, 2, 3)), |
| 283 |
) |
| 284 |
|
| 285 |
def test_varargs_error_kw(self): |
| 286 |
msg = r"meth_varargs\(\) takes no keyword arguments" |
| 287 |
self.assertRaisesRegex( |
| 288 |
TypeError, msg, lambda: self.obj.meth_varargs(k=1), |
| 289 |
) |
| 290 |
|
| 291 |
def test_varargs_keywords(self): |
| 292 |
self.assertEqual( |
| 293 |
self.obj.meth_varargs_keywords(1, 2, a=3, b=4), |
| 294 |
(self.expected_self, (1, 2), {'a': 3, 'b': 4}) |
| 295 |
) |
| 296 |
|
| 297 |
def test_varargs_keywords_ext(self): |
| 298 |
self.assertEqual( |
| 299 |
self.obj.meth_varargs_keywords(*[1, 2], **{'a': 3, 'b': 4}), |
| 300 |
(self.expected_self, (1, 2), {'a': 3, 'b': 4}) |
| 301 |
) |
| 302 |
|
| 303 |
def test_o(self): |
| 304 |
self.assertEqual(self.obj.meth_o(1), (self.expected_self, 1)) |
| 305 |
|
| 306 |
def test_o_ext(self): |
| 307 |
self.assertEqual(self.obj.meth_o(*[1]), (self.expected_self, 1)) |
| 308 |
|
| 309 |
def test_o_error_no_arg(self): |
| 310 |
msg = r"meth_o\(\) takes exactly one argument \(0 given\)" |
| 311 |
self.assertRaisesRegex(TypeError, msg, self.obj.meth_o) |
| 312 |
|
| 313 |
def test_o_error_two_args(self): |
| 314 |
msg = r"meth_o\(\) takes exactly one argument \(2 given\)" |
| 315 |
self.assertRaisesRegex( |
| 316 |
TypeError, msg, lambda: self.obj.meth_o(1, 2), |
| 317 |
) |
| 318 |
|
| 319 |
def test_o_error_ext(self): |
| 320 |
msg = r"meth_o\(\) takes exactly one argument \(3 given\)" |
| 321 |
self.assertRaisesRegex( |
| 322 |
TypeError, msg, lambda: self.obj.meth_o(*(1, 2, 3)), |
| 323 |
) |
| 324 |
|
| 325 |
def test_o_error_kw(self): |
| 326 |
msg = r"meth_o\(\) takes no keyword arguments" |
| 327 |
self.assertRaisesRegex( |
| 328 |
TypeError, msg, lambda: self.obj.meth_o(k=1), |
| 329 |
) |
| 330 |
|
| 331 |
def test_o_error_arg_kw(self): |
| 332 |
msg = r"meth_o\(\) takes no keyword arguments" |
| 333 |
self.assertRaisesRegex( |
| 334 |
TypeError, msg, lambda: self.obj.meth_o(k=1), |
| 335 |
) |
| 336 |
|
| 337 |
def test_noargs(self): |
| 338 |
self.assertEqual(self.obj.meth_noargs(), self.expected_self) |
| 339 |
|
| 340 |
def test_noargs_ext(self): |
| 341 |
self.assertEqual(self.obj.meth_noargs(*[]), self.expected_self) |
| 342 |
|
| 343 |
def test_noargs_error_arg(self): |
| 344 |
msg = r"meth_noargs\(\) takes no arguments \(1 given\)" |
| 345 |
self.assertRaisesRegex( |
| 346 |
TypeError, msg, lambda: self.obj.meth_noargs(1), |
| 347 |
) |
| 348 |
|
| 349 |
def test_noargs_error_arg2(self): |
| 350 |
msg = r"meth_noargs\(\) takes no arguments \(2 given\)" |
| 351 |
self.assertRaisesRegex( |
| 352 |
TypeError, msg, lambda: self.obj.meth_noargs(1, 2), |
| 353 |
) |
| 354 |
|
| 355 |
def test_noargs_error_ext(self): |
| 356 |
msg = r"meth_noargs\(\) takes no arguments \(3 given\)" |
| 357 |
self.assertRaisesRegex( |
| 358 |
TypeError, msg, lambda: self.obj.meth_noargs(*(1, 2, 3)), |
| 359 |
) |
| 360 |
|
| 361 |
def test_noargs_error_kw(self): |
| 362 |
msg = r"meth_noargs\(\) takes no keyword arguments" |
| 363 |
self.assertRaisesRegex( |
| 364 |
TypeError, msg, lambda: self.obj.meth_noargs(k=1), |
| 365 |
) |
| 366 |
|
| 367 |
def test_fastcall(self): |
| 368 |
self.assertEqual( |
| 369 |
self.obj.meth_fastcall(1, 2, 3), |
| 370 |
(self.expected_self, (1, 2, 3)), |
| 371 |
) |
| 372 |
|
| 373 |
def test_fastcall_ext(self): |
| 374 |
self.assertEqual( |
| 375 |
self.obj.meth_fastcall(*(1, 2, 3)), |
| 376 |
(self.expected_self, (1, 2, 3)), |
| 377 |
) |
| 378 |
|
| 379 |
def test_fastcall_error_kw(self): |
| 380 |
msg = r"meth_fastcall\(\) takes no keyword arguments" |
| 381 |
self.assertRaisesRegex( |
| 382 |
TypeError, msg, lambda: self.obj.meth_fastcall(k=1), |
| 383 |
) |
| 384 |
|
| 385 |
def test_fastcall_keywords(self): |
| 386 |
self.assertEqual( |
| 387 |
self.obj.meth_fastcall_keywords(1, 2, a=3, b=4), |
| 388 |
(self.expected_self, (1, 2), {'a': 3, 'b': 4}) |
| 389 |
) |
| 390 |
|
| 391 |
def test_fastcall_keywords_ext(self): |
| 392 |
self.assertEqual( |
| 393 |
self.obj.meth_fastcall_keywords(*(1, 2), **{'a': 3, 'b': 4}), |
| 394 |
(self.expected_self, (1, 2), {'a': 3, 'b': 4}) |
| 395 |
) |
| 396 |
|
| 397 |
|
| 398 |
class TestCallingConventionsInstance(TestCallingConventions): |
| 399 |
"""Test calling instance methods using various calling conventions""" |
| 400 |
|
| 401 |
def setUp(self): |
| 402 |
self.obj = self.expected_self = _testcapi.MethInstance() |
| 403 |
|
| 404 |
|
| 405 |
class TestCallingConventionsClass(TestCallingConventions): |
| 406 |
"""Test calling class methods using various calling conventions""" |
| 407 |
|
| 408 |
def setUp(self): |
| 409 |
self.obj = self.expected_self = _testcapi.MethClass |
| 410 |
|
| 411 |
|
| 412 |
class TestCallingConventionsClassInstance(TestCallingConventions): |
| 413 |
"""Test calling class methods on instance""" |
| 414 |
|
| 415 |
def setUp(self): |
| 416 |
self.obj = _testcapi.MethClass() |
| 417 |
self.expected_self = _testcapi.MethClass |
| 418 |
|
| 419 |
|
| 420 |
class TestCallingConventionsStatic(TestCallingConventions): |
| 421 |
"""Test calling static methods using various calling conventions""" |
| 422 |
|
| 423 |
def setUp(self): |
| 424 |
self.obj = _testcapi.MethStatic() |
| 425 |
self.expected_self = None |
| 426 |
|
| 427 |
|
| 428 |
def pyfunc(arg1, arg2): |
| 429 |
return [arg1, arg2] |
| 430 |
|
| 431 |
|
| 432 |
def pyfunc_noarg(): |
| 433 |
return "noarg" |
| 434 |
|
| 435 |
|
| 436 |
class PythonClass: |
| 437 |
def method(self, arg1, arg2): |
| 438 |
return [arg1, arg2] |
| 439 |
|
| 440 |
def method_noarg(self): |
| 441 |
return "noarg" |
| 442 |
|
| 443 |
@classmethod |
| 444 |
def class_method(cls): |
| 445 |
return "classmethod" |
| 446 |
|
| 447 |
@staticmethod |
| 448 |
def static_method(): |
| 449 |
return "staticmethod" |
| 450 |
|
| 451 |
|
| 452 |
PYTHON_INSTANCE = PythonClass() |
| 453 |
|
| 454 |
NULL_OR_EMPTY = object() |
| 455 |
|
| 456 |
|
| 457 |
class FastCallTests(unittest.TestCase): |
| 458 |
"""Test calling using various callables from C |
| 459 |
""" |
| 460 |
|
| 461 |
# Test calls with positional arguments |
| 462 |
CALLS_POSARGS = [ |
| 463 |
# (func, args: tuple, result) |
| 464 |
|
| 465 |
# Python function with 2 arguments |
| 466 |
(pyfunc, (1, 2), [1, 2]), |
| 467 |
|
| 468 |
# Python function without argument |
| 469 |
(pyfunc_noarg, (), "noarg"), |
| 470 |
|
| 471 |
# Python class methods |
| 472 |
(PythonClass.class_method, (), "classmethod"), |
| 473 |
(PythonClass.static_method, (), "staticmethod"), |
| 474 |
|
| 475 |
# Python instance methods |
| 476 |
(PYTHON_INSTANCE.method, (1, 2), [1, 2]), |
| 477 |
(PYTHON_INSTANCE.method_noarg, (), "noarg"), |
| 478 |
(PYTHON_INSTANCE.class_method, (), "classmethod"), |
| 479 |
(PYTHON_INSTANCE.static_method, (), "staticmethod"), |
| 480 |
|
| 481 |
# C callables are added later |
| 482 |
] |
| 483 |
|
| 484 |
# Test calls with positional and keyword arguments |
| 485 |
CALLS_KWARGS = [ |
| 486 |
# (func, args: tuple, kwargs: dict, result) |
| 487 |
|
| 488 |
# Python function with 2 arguments |
| 489 |
(pyfunc, (1,), {'arg2': 2}, [1, 2]), |
| 490 |
(pyfunc, (), {'arg1': 1, 'arg2': 2}, [1, 2]), |
| 491 |
|
| 492 |
# Python instance methods |
| 493 |
(PYTHON_INSTANCE.method, (1,), {'arg2': 2}, [1, 2]), |
| 494 |
(PYTHON_INSTANCE.method, (), {'arg1': 1, 'arg2': 2}, [1, 2]), |
| 495 |
|
| 496 |
# C callables are added later |
| 497 |
] |
| 498 |
|
| 499 |
# Add all the calling conventions and variants of C callables |
| 500 |
if _testcapi: |
| 501 |
_instance = _testcapi.MethInstance() |
| 502 |
for obj, expected_self in ( |
| 503 |
(_testcapi, _testcapi), # module-level function |
| 504 |
(_instance, _instance), # bound method |
| 505 |
(_testcapi.MethClass, _testcapi.MethClass), # class method on class |
| 506 |
(_testcapi.MethClass(), _testcapi.MethClass), # class method on inst. |
| 507 |
(_testcapi.MethStatic, None), # static method |
| 508 |
): |
| 509 |
CALLS_POSARGS.extend([ |
| 510 |
(obj.meth_varargs, (1, 2), (expected_self, (1, 2))), |
| 511 |
(obj.meth_varargs_keywords, |
| 512 |
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)), |
| 513 |
(obj.meth_fastcall, (1, 2), (expected_self, (1, 2))), |
| 514 |
(obj.meth_fastcall, (), (expected_self, ())), |
| 515 |
(obj.meth_fastcall_keywords, |
| 516 |
(1, 2), (expected_self, (1, 2), NULL_OR_EMPTY)), |
| 517 |
(obj.meth_fastcall_keywords, |
| 518 |
(), (expected_self, (), NULL_OR_EMPTY)), |
| 519 |
(obj.meth_noargs, (), expected_self), |
| 520 |
(obj.meth_o, (123, ), (expected_self, 123)), |
| 521 |
]) |
| 522 |
|
| 523 |
CALLS_KWARGS.extend([ |
| 524 |
(obj.meth_varargs_keywords, |
| 525 |
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})), |
| 526 |
(obj.meth_varargs_keywords, |
| 527 |
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})), |
| 528 |
(obj.meth_varargs_keywords, |
| 529 |
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)), |
| 530 |
(obj.meth_fastcall_keywords, |
| 531 |
(1, 2), {'x': 'y'}, (expected_self, (1, 2), {'x': 'y'})), |
| 532 |
(obj.meth_fastcall_keywords, |
| 533 |
(), {'x': 'y'}, (expected_self, (), {'x': 'y'})), |
| 534 |
(obj.meth_fastcall_keywords, |
| 535 |
(1, 2), {}, (expected_self, (1, 2), NULL_OR_EMPTY)), |
| 536 |
]) |
| 537 |
|
| 538 |
def check_result(self, result, expected): |
| 539 |
if isinstance(expected, tuple) and expected[-1] is NULL_OR_EMPTY: |
| 540 |
if result[-1] in ({}, None): |
| 541 |
expected = (*expected[:-1], result[-1]) |
| 542 |
self.assertEqual(result, expected) |
| 543 |
|
| 544 |
@unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 545 |
def test_vectorcall_dict(self): |
| 546 |
# Test PyObject_VectorcallDict() |
| 547 |
|
| 548 |
for func, args, expected in self.CALLS_POSARGS: |
| 549 |
with self.subTest(func=func, args=args): |
| 550 |
# kwargs=NULL |
| 551 |
result = _testcapi.pyobject_fastcalldict(func, args, None) |
| 552 |
self.check_result(result, expected) |
| 553 |
|
| 554 |
if not args: |
| 555 |
# args=NULL, nargs=0, kwargs=NULL |
| 556 |
result = _testcapi.pyobject_fastcalldict(func, None, None) |
| 557 |
self.check_result(result, expected) |
| 558 |
|
| 559 |
for func, args, kwargs, expected in self.CALLS_KWARGS: |
| 560 |
with self.subTest(func=func, args=args, kwargs=kwargs): |
| 561 |
result = _testcapi.pyobject_fastcalldict(func, args, kwargs) |
| 562 |
self.check_result(result, expected) |
| 563 |
|
| 564 |
@unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 565 |
def test_vectorcall(self): |
| 566 |
# Test PyObject_Vectorcall() |
| 567 |
|
| 568 |
for func, args, expected in self.CALLS_POSARGS: |
| 569 |
with self.subTest(func=func, args=args): |
| 570 |
# kwnames=NULL |
| 571 |
result = _testcapi.pyobject_vectorcall(func, args, None) |
| 572 |
self.check_result(result, expected) |
| 573 |
|
| 574 |
# kwnames=() |
| 575 |
result = _testcapi.pyobject_vectorcall(func, args, ()) |
| 576 |
self.check_result(result, expected) |
| 577 |
|
| 578 |
if not args: |
| 579 |
# kwnames=NULL |
| 580 |
result = _testcapi.pyobject_vectorcall(func, None, None) |
| 581 |
self.check_result(result, expected) |
| 582 |
|
| 583 |
# kwnames=() |
| 584 |
result = _testcapi.pyobject_vectorcall(func, None, ()) |
| 585 |
self.check_result(result, expected) |
| 586 |
|
| 587 |
for func, args, kwargs, expected in self.CALLS_KWARGS: |
| 588 |
with self.subTest(func=func, args=args, kwargs=kwargs): |
| 589 |
kwnames = tuple(kwargs.keys()) |
| 590 |
args = args + tuple(kwargs.values()) |
| 591 |
result = _testcapi.pyobject_vectorcall(func, args, kwnames) |
| 592 |
self.check_result(result, expected) |
| 593 |
|
| 594 |
def test_fastcall_clearing_dict(self): |
| 595 |
# Test bpo-36907: the point of the test is just checking that this |
| 596 |
# does not crash. |
| 597 |
class IntWithDict: |
| 598 |
__slots__ = ["kwargs"] |
| 599 |
def __init__(self, **kwargs): |
| 600 |
self.kwargs = kwargs |
| 601 |
def __index__(self): |
| 602 |
self.kwargs.clear() |
| 603 |
gc.collect() |
| 604 |
return 0 |
| 605 |
x = IntWithDict(optimize=IntWithDict()) |
| 606 |
# We test the argument handling of "compile" here, the compilation |
| 607 |
# itself is not relevant. When we pass flags=x below, x.__index__() is |
| 608 |
# called, which changes the keywords dict. |
| 609 |
compile("pass", "", "exec", x, **x.kwargs) |
| 610 |
|
| 611 |
|
| 612 |
Py_TPFLAGS_HAVE_VECTORCALL = 1 << 11 |
| 613 |
Py_TPFLAGS_METHOD_DESCRIPTOR = 1 << 17 |
| 614 |
|
| 615 |
|
| 616 |
def testfunction(self): |
| 617 |
"""some doc""" |
| 618 |
return self |
| 619 |
|
| 620 |
|
| 621 |
def testfunction_kw(self, *, kw): |
| 622 |
"""some doc""" |
| 623 |
return self |
| 624 |
|
| 625 |
|
| 626 |
@unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 627 |
class TestPEP590(unittest.TestCase): |
| 628 |
|
| 629 |
def test_method_descriptor_flag(self): |
| 630 |
import functools |
| 631 |
cached = functools.lru_cache(1)(testfunction) |
| 632 |
|
| 633 |
self.assertFalse(type(repr).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 634 |
self.assertTrue(type(list.append).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 635 |
self.assertTrue(type(list.__add__).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 636 |
self.assertTrue(type(testfunction).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 637 |
self.assertTrue(type(cached).__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 638 |
|
| 639 |
self.assertTrue(_testcapi.MethodDescriptorBase.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 640 |
self.assertTrue(_testcapi.MethodDescriptorDerived.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 641 |
self.assertFalse(_testcapi.MethodDescriptorNopGet.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 642 |
|
| 643 |
# Mutable heap types should not inherit Py_TPFLAGS_METHOD_DESCRIPTOR |
| 644 |
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase): |
| 645 |
pass |
| 646 |
self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_METHOD_DESCRIPTOR) |
| 647 |
|
| 648 |
def test_vectorcall_flag(self): |
| 649 |
self.assertTrue(_testcapi.MethodDescriptorBase.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 650 |
self.assertTrue(_testcapi.MethodDescriptorDerived.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 651 |
self.assertFalse(_testcapi.MethodDescriptorNopGet.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 652 |
self.assertTrue(_testcapi.MethodDescriptor2.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 653 |
|
| 654 |
# Mutable heap types should inherit Py_TPFLAGS_HAVE_VECTORCALL, |
| 655 |
# but should lose it when __call__ is overridden |
| 656 |
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase): |
| 657 |
pass |
| 658 |
self.assertTrue(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 659 |
MethodDescriptorHeap.__call__ = print |
| 660 |
self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 661 |
|
| 662 |
# Mutable heap types should not inherit Py_TPFLAGS_HAVE_VECTORCALL if |
| 663 |
# they define __call__ directly |
| 664 |
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase): |
| 665 |
def __call__(self): |
| 666 |
pass |
| 667 |
self.assertFalse(MethodDescriptorHeap.__flags__ & Py_TPFLAGS_HAVE_VECTORCALL) |
| 668 |
|
| 669 |
def test_vectorcall_override(self): |
| 670 |
# Check that tp_call can correctly override vectorcall. |
| 671 |
# MethodDescriptorNopGet implements tp_call but it inherits from |
| 672 |
# MethodDescriptorBase, which implements vectorcall. Since |
| 673 |
# MethodDescriptorNopGet returns the args tuple when called, we check |
| 674 |
# additionally that no new tuple is created for this call. |
| 675 |
args = tuple(range(5)) |
| 676 |
f = _testcapi.MethodDescriptorNopGet() |
| 677 |
self.assertIs(f(*args), args) |
| 678 |
|
| 679 |
def test_vectorcall_override_on_mutable_class(self): |
| 680 |
"""Setting __call__ should disable vectorcall""" |
| 681 |
TestType = _testcapi.make_vectorcall_class() |
| 682 |
instance = TestType() |
| 683 |
self.assertEqual(instance(), "tp_call") |
| 684 |
instance.set_vectorcall(TestType) |
| 685 |
self.assertEqual(instance(), "vectorcall") # assume vectorcall is used |
| 686 |
TestType.__call__ = lambda self: "custom" |
| 687 |
self.assertEqual(instance(), "custom") |
| 688 |
|
| 689 |
def test_vectorcall_override_with_subclass(self): |
| 690 |
"""Setting __call__ on a superclass should disable vectorcall""" |
| 691 |
SuperType = _testcapi.make_vectorcall_class() |
| 692 |
class DerivedType(SuperType): |
| 693 |
pass |
| 694 |
|
| 695 |
instance = DerivedType() |
| 696 |
|
| 697 |
# Derived types with its own vectorcall should be unaffected |
| 698 |
UnaffectedType1 = _testcapi.make_vectorcall_class(DerivedType) |
| 699 |
UnaffectedType2 = _testcapi.make_vectorcall_class(SuperType) |
| 700 |
|
| 701 |
# Aside: Quickly check that the C helper actually made derived types |
| 702 |
self.assertIsSubclass(UnaffectedType1, DerivedType) |
| 703 |
self.assertIsSubclass(UnaffectedType2, SuperType) |
| 704 |
|
| 705 |
# Initial state: tp_call |
| 706 |
self.assertEqual(instance(), "tp_call") |
| 707 |
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), True) |
| 708 |
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), True) |
| 709 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True) |
| 710 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True) |
| 711 |
|
| 712 |
# Setting the vectorcall function |
| 713 |
instance.set_vectorcall(SuperType) |
| 714 |
|
| 715 |
self.assertEqual(instance(), "vectorcall") |
| 716 |
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), True) |
| 717 |
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), True) |
| 718 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True) |
| 719 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True) |
| 720 |
|
| 721 |
# Setting __call__ should remove vectorcall from all subclasses |
| 722 |
SuperType.__call__ = lambda self: "custom" |
| 723 |
|
| 724 |
self.assertEqual(instance(), "custom") |
| 725 |
self.assertEqual(_testcapi.has_vectorcall_flag(SuperType), False) |
| 726 |
self.assertEqual(_testcapi.has_vectorcall_flag(DerivedType), False) |
| 727 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType1), True) |
| 728 |
self.assertEqual(_testcapi.has_vectorcall_flag(UnaffectedType2), True) |
| 729 |
|
| 730 |
|
| 731 |
def test_vectorcall(self): |
| 732 |
# Test a bunch of different ways to call objects: |
| 733 |
# 1. vectorcall using PyVectorcall_Call() |
| 734 |
# (only for objects that support vectorcall directly) |
| 735 |
# 2. normal call |
| 736 |
# 3. vectorcall using PyObject_Vectorcall() |
| 737 |
# 4. call as bound method |
| 738 |
# 5. call using functools.partial |
| 739 |
|
| 740 |
# A list of (function, args, kwargs, result) calls to test |
| 741 |
calls = [(len, (range(42),), {}, 42), |
| 742 |
(list.append, ([], 0), {}, None), |
| 743 |
([].append, (0,), {}, None), |
| 744 |
(sum, ([36],), {"start":6}, 42), |
| 745 |
(testfunction, (42,), {}, 42), |
| 746 |
(testfunction_kw, (42,), {"kw":None}, 42), |
| 747 |
(_testcapi.MethodDescriptorBase(), (0,), {}, True), |
| 748 |
(_testcapi.MethodDescriptorDerived(), (0,), {}, True), |
| 749 |
(_testcapi.MethodDescriptor2(), (0,), {}, False)] |
| 750 |
|
| 751 |
from _testcapi import pyobject_vectorcall, pyvectorcall_call |
| 752 |
from types import MethodType |
| 753 |
from functools import partial |
| 754 |
|
| 755 |
def vectorcall(func, args, kwargs): |
| 756 |
args = *args, *kwargs.values() |
| 757 |
kwnames = tuple(kwargs) |
| 758 |
return pyobject_vectorcall(func, args, kwnames) |
| 759 |
|
| 760 |
for (func, args, kwargs, expected) in calls: |
| 761 |
with self.subTest(str(func)): |
| 762 |
if not kwargs: |
| 763 |
self.assertEqual(expected, pyvectorcall_call(func, args)) |
| 764 |
self.assertEqual(expected, pyvectorcall_call(func, args, kwargs)) |
| 765 |
|
| 766 |
# Add derived classes (which do not support vectorcall directly, |
| 767 |
# but do support all other ways of calling). |
| 768 |
|
| 769 |
class MethodDescriptorHeap(_testcapi.MethodDescriptorBase): |
| 770 |
pass |
| 771 |
|
| 772 |
class MethodDescriptorOverridden(_testcapi.MethodDescriptorBase): |
| 773 |
def __call__(self, n): |
| 774 |
return 'new' |
| 775 |
|
| 776 |
class SuperBase: |
| 777 |
def __call__(self, *args): |
| 778 |
return super().__call__(*args) |
| 779 |
|
| 780 |
class MethodDescriptorSuper(SuperBase, _testcapi.MethodDescriptorBase): |
| 781 |
def __call__(self, *args): |
| 782 |
return super().__call__(*args) |
| 783 |
|
| 784 |
calls += [ |
| 785 |
(dict.update, ({},), {"key":True}, None), |
| 786 |
({}.update, ({},), {"key":True}, None), |
| 787 |
(MethodDescriptorHeap(), (0,), {}, True), |
| 788 |
(MethodDescriptorOverridden(), (0,), {}, 'new'), |
| 789 |
(MethodDescriptorSuper(), (0,), {}, True), |
| 790 |
] |
| 791 |
|
| 792 |
for (func, args, kwargs, expected) in calls: |
| 793 |
with self.subTest(str(func)): |
| 794 |
args1 = args[1:] |
| 795 |
meth = MethodType(func, args[0]) |
| 796 |
wrapped = partial(func) |
| 797 |
if not kwargs: |
| 798 |
self.assertEqual(expected, func(*args)) |
| 799 |
self.assertEqual(expected, pyobject_vectorcall(func, args, None)) |
| 800 |
self.assertEqual(expected, meth(*args1)) |
| 801 |
self.assertEqual(expected, wrapped(*args)) |
| 802 |
self.assertEqual(expected, func(*args, **kwargs)) |
| 803 |
self.assertEqual(expected, vectorcall(func, args, kwargs)) |
| 804 |
self.assertEqual(expected, meth(*args1, **kwargs)) |
| 805 |
self.assertEqual(expected, wrapped(*args, **kwargs)) |
| 806 |
|
| 807 |
def test_setvectorcall(self): |
| 808 |
from _testcapi import function_setvectorcall |
| 809 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 810 |
def f(num): return num + 1 |
| 811 |
assert_equal = self.assertEqual |
| 812 |
num = 10 |
| 813 |
assert_equal(11, f(num)) |
| 814 |
function_setvectorcall(f) |
| 815 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 816 |
assert_equal("overridden", f(num)) |
| 817 |
|
| 818 |
def test_setvectorcall_load_attr_specialization_skip(self): |
| 819 |
from _testcapi import function_setvectorcall |
| 820 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 821 |
|
| 822 |
class X: |
| 823 |
def __getattribute__(self, attr): |
| 824 |
return attr |
| 825 |
|
| 826 |
assert_equal = self.assertEqual |
| 827 |
x = X() |
| 828 |
assert_equal("a", x.a) |
| 829 |
function_setvectorcall(X.__getattribute__) |
| 830 |
# make sure specialization doesn't trigger |
| 831 |
# when vectorcall is overridden |
| 832 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 833 |
assert_equal("overridden", x.a) |
| 834 |
|
| 835 |
def test_setvectorcall_load_attr_specialization_deopt(self): |
| 836 |
from _testcapi import function_setvectorcall |
| 837 |
_testinternalcapi = import_helper.import_module("_testinternalcapi") |
| 838 |
|
| 839 |
class X: |
| 840 |
def __getattribute__(self, attr): |
| 841 |
return attr |
| 842 |
|
| 843 |
def get_a(x): |
| 844 |
return x.a |
| 845 |
|
| 846 |
assert_equal = self.assertEqual |
| 847 |
x = X() |
| 848 |
# trigger LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN specialization |
| 849 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 850 |
assert_equal("a", get_a(x)) |
| 851 |
function_setvectorcall(X.__getattribute__) |
| 852 |
# make sure specialized LOAD_ATTR_GETATTRIBUTE_OVERRIDDEN |
| 853 |
# gets deopted due to overridden vectorcall |
| 854 |
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): |
| 855 |
assert_equal("overridden", get_a(x)) |
| 856 |
|
| 857 |
@requires_limited_api |
| 858 |
def test_vectorcall_limited_incoming(self): |
| 859 |
from _testcapi import pyobject_vectorcall |
| 860 |
for cls in (_testlimitedcapi.LimitedVectorCallClass, |
| 861 |
_testlimitedcapi.LimitedRelativeVectorCallClass): |
| 862 |
with self.subTest(cls=cls): |
| 863 |
obj = cls() |
| 864 |
self.assertEqual( |
| 865 |
pyobject_vectorcall(obj, (), ()), |
| 866 |
"vectorcall called") |
| 867 |
|
| 868 |
@requires_limited_api |
| 869 |
def test_vectorcall_limited_outgoing(self): |
| 870 |
from _testlimitedcapi import call_vectorcall |
| 871 |
|
| 872 |
args_captured = [] |
| 873 |
kwargs_captured = [] |
| 874 |
|
| 875 |
def f(*args, **kwargs): |
| 876 |
args_captured.append(args) |
| 877 |
kwargs_captured.append(kwargs) |
| 878 |
return "success" |
| 879 |
|
| 880 |
self.assertEqual(call_vectorcall(f), "success") |
| 881 |
self.assertEqual(args_captured, [("foo",)]) |
| 882 |
self.assertEqual(kwargs_captured, [{"baz": "bar"}]) |
| 883 |
|
| 884 |
@requires_limited_api |
| 885 |
def test_vectorcall_limited_outgoing_method(self): |
| 886 |
from _testlimitedcapi import call_vectorcall_method |
| 887 |
|
| 888 |
args_captured = [] |
| 889 |
kwargs_captured = [] |
| 890 |
|
| 891 |
class TestInstance: |
| 892 |
def f(self, *args, **kwargs): |
| 893 |
args_captured.append(args) |
| 894 |
kwargs_captured.append(kwargs) |
| 895 |
return "success" |
| 896 |
|
| 897 |
self.assertEqual(call_vectorcall_method(TestInstance()), "success") |
| 898 |
self.assertEqual(args_captured, [("foo",)]) |
| 899 |
self.assertEqual(kwargs_captured, [{"baz": "bar"}]) |
| 900 |
|
| 901 |
class A: |
| 902 |
def method_two_args(self, x, y): |
| 903 |
pass |
| 904 |
|
| 905 |
@staticmethod |
| 906 |
def static_no_args(): |
| 907 |
pass |
| 908 |
|
| 909 |
@staticmethod |
| 910 |
def positional_only(arg, /): |
| 911 |
pass |
| 912 |
|
| 913 |
@cpython_only |
| 914 |
class TestErrorMessagesUseQualifiedName(unittest.TestCase): |
| 915 |
|
| 916 |
@contextlib.contextmanager |
| 917 |
def check_raises_type_error(self, message): |
| 918 |
with self.assertRaises(TypeError) as cm: |
| 919 |
yield |
| 920 |
self.assertEqual(str(cm.exception), message) |
| 921 |
|
| 922 |
def test_missing_arguments(self): |
| 923 |
msg = "A.method_two_args() missing 1 required positional argument: 'y'" |
| 924 |
with self.check_raises_type_error(msg): |
| 925 |
A().method_two_args("x") |
| 926 |
|
| 927 |
def test_too_many_positional(self): |
| 928 |
msg = "A.static_no_args() takes 0 positional arguments but 1 was given" |
| 929 |
with self.check_raises_type_error(msg): |
| 930 |
A.static_no_args("oops it's an arg") |
| 931 |
|
| 932 |
def test_positional_only_passed_as_keyword(self): |
| 933 |
msg = "A.positional_only() got some positional-only arguments passed as keyword arguments: 'arg'" |
| 934 |
with self.check_raises_type_error(msg): |
| 935 |
A.positional_only(arg="x") |
| 936 |
|
| 937 |
def test_unexpected_keyword(self): |
| 938 |
msg = "A.method_two_args() got an unexpected keyword argument 'bad'" |
| 939 |
with self.check_raises_type_error(msg): |
| 940 |
A().method_two_args(bad="x") |
| 941 |
|
| 942 |
def test_multiple_values(self): |
| 943 |
msg = "A.method_two_args() got multiple values for argument 'x'" |
| 944 |
with self.check_raises_type_error(msg): |
| 945 |
A().method_two_args("x", "y", x="oops") |
| 946 |
|
| 947 |
@cpython_only |
| 948 |
class TestErrorMessagesSuggestions(unittest.TestCase): |
| 949 |
@contextlib.contextmanager |
| 950 |
def check_suggestion_includes(self, message): |
| 951 |
with self.assertRaises(TypeError) as cm: |
| 952 |
yield |
| 953 |
self.assertIn(f"Did you mean '{message}'?", str(cm.exception)) |
| 954 |
|
| 955 |
@contextlib.contextmanager |
| 956 |
def check_suggestion_not_present(self): |
| 957 |
with self.assertRaises(TypeError) as cm: |
| 958 |
yield |
| 959 |
self.assertNotIn("Did you mean", str(cm.exception)) |
| 960 |
|
| 961 |
def test_unexpected_keyword_suggestion_valid_positions(self): |
| 962 |
def foo(blech=None, /, aaa=None, *args, late1=None): |
| 963 |
pass |
| 964 |
|
| 965 |
cases = [ |
| 966 |
("blach", None), |
| 967 |
("aa", "aaa"), |
| 968 |
("orgs", None), |
| 969 |
("late11", "late1"), |
| 970 |
] |
| 971 |
|
| 972 |
for keyword, suggestion in cases: |
| 973 |
with self.subTest(keyword): |
| 974 |
ctx = self.check_suggestion_includes(suggestion) if suggestion else self.check_suggestion_not_present() |
| 975 |
with ctx: |
| 976 |
foo(**{keyword:None}) |
| 977 |
|
| 978 |
def test_unexpected_keyword_suggestion_kinds(self): |
| 979 |
|
| 980 |
def substitution(noise=None, more_noise=None, a = None, blech = None): |
| 981 |
pass |
| 982 |
|
| 983 |
def elimination(noise = None, more_noise = None, a = None, blch = None): |
| 984 |
pass |
| 985 |
|
| 986 |
def addition(noise = None, more_noise = None, a = None, bluchin = None): |
| 987 |
pass |
| 988 |
|
| 989 |
def substitution_over_elimination(blach = None, bluc = None): |
| 990 |
pass |
| 991 |
|
| 992 |
def substitution_over_addition(blach = None, bluchi = None): |
| 993 |
pass |
| 994 |
|
| 995 |
def elimination_over_addition(bluc = None, blucha = None): |
| 996 |
pass |
| 997 |
|
| 998 |
def case_change_over_substitution(BLuch=None, Luch = None, fluch = None): |
| 999 |
pass |
| 1000 |
|
| 1001 |
for func, suggestion in [ |
| 1002 |
(addition, "bluchin"), |
| 1003 |
(substitution, "blech"), |
| 1004 |
(elimination, "blch"), |
| 1005 |
(addition, "bluchin"), |
| 1006 |
(substitution_over_elimination, "blach"), |
| 1007 |
(substitution_over_addition, "blach"), |
| 1008 |
(elimination_over_addition, "bluc"), |
| 1009 |
(case_change_over_substitution, "BLuch"), |
| 1010 |
]: |
| 1011 |
with self.subTest(suggestion): |
| 1012 |
with self.check_suggestion_includes(suggestion): |
| 1013 |
func(bluch=None) |
| 1014 |
|
| 1015 |
def test_unexpected_keyword_suggestion_via_getargs(self): |
| 1016 |
with self.check_suggestion_includes("maxsplit"): |
| 1017 |
"foo".split(maxsplt=1) |
| 1018 |
|
| 1019 |
self.assertRaisesRegex( |
| 1020 |
TypeError, r"split\(\) got an unexpected keyword argument 'blech'$", |
| 1021 |
"foo".split, blech=1 |
| 1022 |
) |
| 1023 |
with self.check_suggestion_not_present(): |
| 1024 |
"foo".split(blech=1) |
| 1025 |
with self.check_suggestion_not_present(): |
| 1026 |
"foo".split(more_noise=1, maxsplt=1) |
| 1027 |
|
| 1028 |
# Also test the vgetargskeywords path |
| 1029 |
with self.check_suggestion_includes("name"): |
| 1030 |
ImportError(namez="oops") |
| 1031 |
|
| 1032 |
self.assertRaisesRegex( |
| 1033 |
TypeError, r"ImportError\(\) got an unexpected keyword argument 'blech'$", |
| 1034 |
ImportError, blech=1 |
| 1035 |
) |
| 1036 |
with self.check_suggestion_not_present(): |
| 1037 |
ImportError(blech=1) |
| 1038 |
with self.check_suggestion_not_present(): |
| 1039 |
ImportError(blech=1, namez="oops") |
| 1040 |
|
| 1041 |
@cpython_only |
| 1042 |
class TestRecursion(unittest.TestCase): |
| 1043 |
|
| 1044 |
def test_margin_is_sufficient(self): |
| 1045 |
|
| 1046 |
def get_sp(): |
| 1047 |
return _testinternalcapi.get_stack_pointer() |
| 1048 |
|
| 1049 |
this_sp = _testinternalcapi.get_stack_pointer() |
| 1050 |
lower_sp = _testcapi.pyobject_vectorcall(get_sp, (), ()) |
| 1051 |
if _testcapi._Py_STACK_GROWS_DOWN: |
| 1052 |
self.assertLess(lower_sp, this_sp) |
| 1053 |
safe_margin = this_sp - lower_sp |
| 1054 |
else: |
| 1055 |
self.assertLess(this_sp, lower_sp) |
| 1056 |
safe_margin = lower_sp - this_sp |
| 1057 |
# Add an (arbitrary) extra 20% for safety |
| 1058 |
safe_margin *= 6 / 5 |
| 1059 |
self.assertLess(safe_margin, _testinternalcapi.get_stack_margin()) |
| 1060 |
|
| 1061 |
@skip_on_s390x |
| 1062 |
@unittest.skipIf(is_wasi and Py_DEBUG, "requires deep stack") |
| 1063 |
@skip_if_sanitizer("requires deep stack", thread=True) |
| 1064 |
@unittest.skipIf(_testcapi is None, "requires _testcapi") |
| 1065 |
@skip_emscripten_stack_overflow() |
| 1066 |
@skip_wasi_stack_overflow() |
| 1067 |
def test_super_deep(self): |
| 1068 |
|
| 1069 |
def recurse(n): |
| 1070 |
if n: |
| 1071 |
recurse(n-1) |
| 1072 |
|
| 1073 |
def py_recurse(n, m): |
| 1074 |
if n: |
| 1075 |
py_recurse(n-1, m) |
| 1076 |
else: |
| 1077 |
c_py_recurse(m-1) |
| 1078 |
|
| 1079 |
def c_recurse(n): |
| 1080 |
if n: |
| 1081 |
_testcapi.pyobject_vectorcall(c_recurse, (n-1,), ()) |
| 1082 |
|
| 1083 |
def c_py_recurse(m): |
| 1084 |
if m: |
| 1085 |
_testcapi.pyobject_vectorcall(py_recurse, (1000, m), ()) |
| 1086 |
|
| 1087 |
with set_recursion_limit(100_000): |
| 1088 |
recurse(90_000) |
| 1089 |
with self.assertRaises(RecursionError): |
| 1090 |
recurse(101_000) |
| 1091 |
c_recurse(50) |
| 1092 |
with self.assertRaises(RecursionError): |
| 1093 |
c_recurse(90_000) |
| 1094 |
c_py_recurse(50) |
| 1095 |
with self.assertRaises(RecursionError): |
| 1096 |
c_py_recurse(100_000) |
| 1097 |
|
| 1098 |
def test_recursion_with_kwargs(self): |
| 1099 |
# GH-137883: The interpreter forgot to check the recursion limit when |
| 1100 |
# calling with keywords. |
| 1101 |
def recurse_kw(a=0): |
| 1102 |
recurse_kw(a=0) |
| 1103 |
with self.assertRaises(RecursionError): |
| 1104 |
recurse_kw() |
| 1105 |
|
| 1106 |
|
| 1107 |
class TestFunctionWithManyArgs(unittest.TestCase): |
| 1108 |
def test_function_with_many_args(self): |
| 1109 |
for N in (10, 500, 1000): |
| 1110 |
with self.subTest(N=N): |
| 1111 |
args = ",".join([f"a{i}" for i in range(N)]) |
| 1112 |
src = f"def f({args}) : return a{N//2}" |
| 1113 |
l = {} |
| 1114 |
exec(src, {}, l) |
| 1115 |
self.assertEqual(l['f'](*range(N)), N//2) |
| 1116 |
|
| 1117 |
|
| 1118 |
@unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 1119 |
class TestCAPI(unittest.TestCase): |
| 1120 |
def test_cfunction_call(self): |
| 1121 |
def func(*args, **kwargs): |
| 1122 |
return (args, kwargs) |
| 1123 |
|
| 1124 |
# PyCFunction_Call() was removed in Python 3.13 API, but was kept in |
| 1125 |
# the stable ABI. |
| 1126 |
def PyCFunction_Call(func, *args, **kwargs): |
| 1127 |
if kwargs: |
| 1128 |
return _testcapi.pycfunction_call(func, args, kwargs) |
| 1129 |
else: |
| 1130 |
return _testcapi.pycfunction_call(func, args) |
| 1131 |
|
| 1132 |
self.assertEqual(PyCFunction_Call(func), ((), {})) |
| 1133 |
self.assertEqual(PyCFunction_Call(func, 1, 2, 3), ((1, 2, 3), {})) |
| 1134 |
self.assertEqual(PyCFunction_Call(func, "arg", num=5), (("arg",), {'num': 5})) |
| 1135 |
|
| 1136 |
|
| 1137 |
if __name__ == "__main__": |
| 1138 |
unittest.main() |