| 1 |
import contextlib |
| 2 |
import os |
| 3 |
import pickle |
| 4 |
import sys |
| 5 |
from textwrap import dedent |
| 6 |
import threading |
| 7 |
import types |
| 8 |
import unittest |
| 9 |
|
| 10 |
from test import support |
| 11 |
from test.support import os_helper |
| 12 |
from test.support import script_helper |
| 13 |
from test.support import import_helper |
| 14 |
from test.support.script_helper import assert_python_ok |
| 15 |
# Raise SkipTest if subinterpreters not supported. |
| 16 |
_interpreters = import_helper.import_module('_interpreters') |
| 17 |
from concurrent import interpreters |
| 18 |
from test.support import Py_GIL_DISABLED |
| 19 |
from test.support import force_not_colorized |
| 20 |
import test._crossinterp_definitions as defs |
| 21 |
from concurrent.interpreters import ( |
| 22 |
InterpreterError, InterpreterNotFoundError, ExecutionFailed, |
| 23 |
) |
| 24 |
from .utils import ( |
| 25 |
_captured_script, _run_output, _running, TestBase, |
| 26 |
requires_test_modules, _testinternalcapi, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
WHENCE_STR_UNKNOWN = 'unknown' |
| 31 |
WHENCE_STR_RUNTIME = 'runtime init' |
| 32 |
WHENCE_STR_LEGACY_CAPI = 'legacy C-API' |
| 33 |
WHENCE_STR_CAPI = 'C-API' |
| 34 |
WHENCE_STR_XI = 'cross-interpreter C-API' |
| 35 |
WHENCE_STR_STDLIB = '_interpreters module' |
| 36 |
|
| 37 |
|
| 38 |
def is_pickleable(obj): |
| 39 |
try: |
| 40 |
pickle.dumps(obj) |
| 41 |
except Exception: |
| 42 |
return False |
| 43 |
return True |
| 44 |
|
| 45 |
|
| 46 |
@contextlib.contextmanager |
| 47 |
def defined_in___main__(name, script, *, remove=False): |
| 48 |
import __main__ as mainmod |
| 49 |
mainns = vars(mainmod) |
| 50 |
assert name not in mainns |
| 51 |
exec(script, mainns, mainns) |
| 52 |
if remove: |
| 53 |
yield mainns.pop(name) |
| 54 |
else: |
| 55 |
try: |
| 56 |
yield mainns[name] |
| 57 |
finally: |
| 58 |
mainns.pop(name, None) |
| 59 |
|
| 60 |
|
| 61 |
def build_excinfo(exctype, msg=None, formatted=None, errdisplay=None): |
| 62 |
if isinstance(exctype, type): |
| 63 |
assert issubclass(exctype, BaseException), exctype |
| 64 |
exctype = types.SimpleNamespace( |
| 65 |
__name__=exctype.__name__, |
| 66 |
__qualname__=exctype.__qualname__, |
| 67 |
__module__=exctype.__module__, |
| 68 |
) |
| 69 |
elif isinstance(exctype, str): |
| 70 |
module, _, name = exctype.rpartition(exctype) |
| 71 |
if not module and name in __builtins__: |
| 72 |
module = 'builtins' |
| 73 |
exctype = types.SimpleNamespace( |
| 74 |
__name__=name, |
| 75 |
__qualname__=exctype, |
| 76 |
__module__=module or None, |
| 77 |
) |
| 78 |
else: |
| 79 |
assert isinstance(exctype, types.SimpleNamespace) |
| 80 |
assert msg is None or isinstance(msg, str), msg |
| 81 |
assert formatted is None or isinstance(formatted, str), formatted |
| 82 |
assert errdisplay is None or isinstance(errdisplay, str), errdisplay |
| 83 |
return types.SimpleNamespace( |
| 84 |
type=exctype, |
| 85 |
msg=msg, |
| 86 |
formatted=formatted, |
| 87 |
errdisplay=errdisplay, |
| 88 |
) |
| 89 |
|
| 90 |
|
| 91 |
class ModuleTests(TestBase): |
| 92 |
|
| 93 |
def test_queue_aliases(self): |
| 94 |
first = [ |
| 95 |
interpreters.create_queue, |
| 96 |
interpreters.Queue, |
| 97 |
interpreters.QueueEmpty, |
| 98 |
interpreters.QueueFull, |
| 99 |
] |
| 100 |
second = [ |
| 101 |
interpreters.create_queue, |
| 102 |
interpreters.Queue, |
| 103 |
interpreters.QueueEmpty, |
| 104 |
interpreters.QueueFull, |
| 105 |
] |
| 106 |
self.assertEqual(second, first) |
| 107 |
|
| 108 |
|
| 109 |
class CreateTests(TestBase): |
| 110 |
|
| 111 |
def test_in_main(self): |
| 112 |
interp = interpreters.create() |
| 113 |
self.assertIsInstance(interp, interpreters.Interpreter) |
| 114 |
self.assertIn(interp, interpreters.list_all()) |
| 115 |
|
| 116 |
# GH-126221: Passing an invalid Unicode character used to cause a SystemError |
| 117 |
self.assertRaises(UnicodeEncodeError, _interpreters.create, '\udc80') |
| 118 |
|
| 119 |
def test_in_thread(self): |
| 120 |
lock = threading.Lock() |
| 121 |
interp = None |
| 122 |
def f(): |
| 123 |
nonlocal interp |
| 124 |
interp = interpreters.create() |
| 125 |
lock.acquire() |
| 126 |
lock.release() |
| 127 |
t = threading.Thread(target=f) |
| 128 |
with lock: |
| 129 |
t.start() |
| 130 |
t.join() |
| 131 |
self.assertIn(interp, interpreters.list_all()) |
| 132 |
|
| 133 |
def test_in_subinterpreter(self): |
| 134 |
main, = interpreters.list_all() |
| 135 |
interp = interpreters.create() |
| 136 |
out = _run_output(interp, dedent(""" |
| 137 |
from concurrent import interpreters |
| 138 |
interp = interpreters.create() |
| 139 |
print(interp.id) |
| 140 |
""")) |
| 141 |
interp2 = interpreters.Interpreter(int(out)) |
| 142 |
self.assertEqual(interpreters.list_all(), [main, interp, interp2]) |
| 143 |
|
| 144 |
def test_after_destroy_all(self): |
| 145 |
before = set(interpreters.list_all()) |
| 146 |
# Create 3 subinterpreters. |
| 147 |
interp_lst = [] |
| 148 |
for _ in range(3): |
| 149 |
interps = interpreters.create() |
| 150 |
interp_lst.append(interps) |
| 151 |
# Now destroy them. |
| 152 |
for interp in interp_lst: |
| 153 |
interp.close() |
| 154 |
# Finally, create another. |
| 155 |
interp = interpreters.create() |
| 156 |
self.assertEqual(set(interpreters.list_all()), before | {interp}) |
| 157 |
|
| 158 |
def test_after_destroy_some(self): |
| 159 |
before = set(interpreters.list_all()) |
| 160 |
# Create 3 subinterpreters. |
| 161 |
interp1 = interpreters.create() |
| 162 |
interp2 = interpreters.create() |
| 163 |
interp3 = interpreters.create() |
| 164 |
# Now destroy 2 of them. |
| 165 |
interp1.close() |
| 166 |
interp2.close() |
| 167 |
# Finally, create another. |
| 168 |
interp = interpreters.create() |
| 169 |
self.assertEqual(set(interpreters.list_all()), before | {interp3, interp}) |
| 170 |
|
| 171 |
|
| 172 |
class GetMainTests(TestBase): |
| 173 |
|
| 174 |
def test_id(self): |
| 175 |
main = interpreters.get_main() |
| 176 |
self.assertEqual(main.id, 0) |
| 177 |
|
| 178 |
def test_current(self): |
| 179 |
main = interpreters.get_main() |
| 180 |
current = interpreters.get_current() |
| 181 |
self.assertIs(main, current) |
| 182 |
|
| 183 |
def test_idempotent(self): |
| 184 |
main1 = interpreters.get_main() |
| 185 |
main2 = interpreters.get_main() |
| 186 |
self.assertIs(main1, main2) |
| 187 |
|
| 188 |
|
| 189 |
class GetCurrentTests(TestBase): |
| 190 |
|
| 191 |
def test_main(self): |
| 192 |
main = interpreters.get_main() |
| 193 |
current = interpreters.get_current() |
| 194 |
self.assertEqual(current, main) |
| 195 |
|
| 196 |
def test_subinterpreter(self): |
| 197 |
main = interpreters.get_main() |
| 198 |
interp = interpreters.create() |
| 199 |
out = _run_output(interp, dedent(""" |
| 200 |
from concurrent import interpreters |
| 201 |
cur = interpreters.get_current() |
| 202 |
print(cur.id) |
| 203 |
""")) |
| 204 |
current = interpreters.Interpreter(int(out)) |
| 205 |
self.assertEqual(current, interp) |
| 206 |
self.assertNotEqual(current, main) |
| 207 |
|
| 208 |
def test_idempotent(self): |
| 209 |
with self.subTest('main'): |
| 210 |
cur1 = interpreters.get_current() |
| 211 |
cur2 = interpreters.get_current() |
| 212 |
self.assertIs(cur1, cur2) |
| 213 |
|
| 214 |
with self.subTest('subinterpreter'): |
| 215 |
interp = interpreters.create() |
| 216 |
out = _run_output(interp, dedent(""" |
| 217 |
from concurrent import interpreters |
| 218 |
cur = interpreters.get_current() |
| 219 |
print(id(cur)) |
| 220 |
cur = interpreters.get_current() |
| 221 |
print(id(cur)) |
| 222 |
""")) |
| 223 |
objid1, objid2 = (int(v) for v in out.splitlines()) |
| 224 |
self.assertEqual(objid1, objid2) |
| 225 |
|
| 226 |
with self.subTest('per-interpreter'): |
| 227 |
interp = interpreters.create() |
| 228 |
out = _run_output(interp, dedent(""" |
| 229 |
from concurrent import interpreters |
| 230 |
cur = interpreters.get_current() |
| 231 |
print(id(cur)) |
| 232 |
""")) |
| 233 |
id1 = int(out) |
| 234 |
id2 = id(interp) |
| 235 |
self.assertNotEqual(id1, id2) |
| 236 |
|
| 237 |
@requires_test_modules |
| 238 |
def test_created_with_capi(self): |
| 239 |
expected = _testinternalcapi.next_interpreter_id() |
| 240 |
text = self.run_temp_from_capi(f""" |
| 241 |
import {interpreters.__name__} as interpreters |
| 242 |
interp = interpreters.get_current() |
| 243 |
print((interp.id, interp.whence)) |
| 244 |
""") |
| 245 |
interpid, whence = eval(text) |
| 246 |
self.assertEqual(interpid, expected) |
| 247 |
self.assertEqual(whence, WHENCE_STR_CAPI) |
| 248 |
|
| 249 |
|
| 250 |
class ListAllTests(TestBase): |
| 251 |
|
| 252 |
def test_initial(self): |
| 253 |
interps = interpreters.list_all() |
| 254 |
self.assertEqual(1, len(interps)) |
| 255 |
|
| 256 |
def test_after_creating(self): |
| 257 |
main = interpreters.get_current() |
| 258 |
first = interpreters.create() |
| 259 |
second = interpreters.create() |
| 260 |
|
| 261 |
ids = [] |
| 262 |
for interp in interpreters.list_all(): |
| 263 |
ids.append(interp.id) |
| 264 |
|
| 265 |
self.assertEqual(ids, [main.id, first.id, second.id]) |
| 266 |
|
| 267 |
def test_after_destroying(self): |
| 268 |
main = interpreters.get_current() |
| 269 |
first = interpreters.create() |
| 270 |
second = interpreters.create() |
| 271 |
first.close() |
| 272 |
|
| 273 |
ids = [] |
| 274 |
for interp in interpreters.list_all(): |
| 275 |
ids.append(interp.id) |
| 276 |
|
| 277 |
self.assertEqual(ids, [main.id, second.id]) |
| 278 |
|
| 279 |
def test_idempotent(self): |
| 280 |
main = interpreters.get_current() |
| 281 |
first = interpreters.create() |
| 282 |
second = interpreters.create() |
| 283 |
expected = [main, first, second] |
| 284 |
|
| 285 |
actual = interpreters.list_all() |
| 286 |
|
| 287 |
self.assertEqual(actual, expected) |
| 288 |
for interp1, interp2 in zip(actual, expected): |
| 289 |
self.assertIs(interp1, interp2) |
| 290 |
|
| 291 |
def test_created_with_capi(self): |
| 292 |
mainid, *_ = _interpreters.get_main() |
| 293 |
interpid1 = _interpreters.create() |
| 294 |
interpid2 = _interpreters.create() |
| 295 |
interpid3 = _interpreters.create() |
| 296 |
interpid4 = interpid3 + 1 |
| 297 |
interpid5 = interpid4 + 1 |
| 298 |
expected = [ |
| 299 |
(mainid, WHENCE_STR_RUNTIME), |
| 300 |
(interpid1, WHENCE_STR_STDLIB), |
| 301 |
(interpid2, WHENCE_STR_STDLIB), |
| 302 |
(interpid3, WHENCE_STR_STDLIB), |
| 303 |
(interpid4, WHENCE_STR_CAPI), |
| 304 |
(interpid5, WHENCE_STR_STDLIB), |
| 305 |
] |
| 306 |
expected2 = expected[:-2] |
| 307 |
text = self.run_temp_from_capi(f""" |
| 308 |
import {interpreters.__name__} as interpreters |
| 309 |
interp = interpreters.create() |
| 310 |
print( |
| 311 |
[(i.id, i.whence) for i in interpreters.list_all()]) |
| 312 |
""") |
| 313 |
res = eval(text) |
| 314 |
res2 = [(i.id, i.whence) for i in interpreters.list_all()] |
| 315 |
self.assertEqual(res, expected) |
| 316 |
self.assertEqual(res2, expected2) |
| 317 |
|
| 318 |
|
| 319 |
class InterpreterObjectTests(TestBase): |
| 320 |
|
| 321 |
def test_init_int(self): |
| 322 |
interpid = interpreters.get_current().id |
| 323 |
interp = interpreters.Interpreter(interpid) |
| 324 |
self.assertEqual(interp.id, interpid) |
| 325 |
|
| 326 |
def test_init_interpreter_id(self): |
| 327 |
interpid = interpreters.get_current()._id |
| 328 |
interp = interpreters.Interpreter(interpid) |
| 329 |
self.assertEqual(interp._id, interpid) |
| 330 |
|
| 331 |
def test_init_unsupported(self): |
| 332 |
actualid = interpreters.get_current().id |
| 333 |
for interpid in [ |
| 334 |
str(actualid), |
| 335 |
float(actualid), |
| 336 |
object(), |
| 337 |
None, |
| 338 |
'', |
| 339 |
]: |
| 340 |
with self.subTest(repr(interpid)): |
| 341 |
with self.assertRaises(TypeError): |
| 342 |
interpreters.Interpreter(interpid) |
| 343 |
|
| 344 |
def test_idempotent(self): |
| 345 |
main = interpreters.get_main() |
| 346 |
interp = interpreters.Interpreter(main.id) |
| 347 |
self.assertIs(interp, main) |
| 348 |
|
| 349 |
def test_init_does_not_exist(self): |
| 350 |
with self.assertRaises(InterpreterNotFoundError): |
| 351 |
interpreters.Interpreter(1_000_000) |
| 352 |
|
| 353 |
def test_init_bad_id(self): |
| 354 |
with self.assertRaises(ValueError): |
| 355 |
interpreters.Interpreter(-1) |
| 356 |
|
| 357 |
def test_id_type(self): |
| 358 |
main = interpreters.get_main() |
| 359 |
current = interpreters.get_current() |
| 360 |
interp = interpreters.create() |
| 361 |
self.assertIsInstance(main.id, int) |
| 362 |
self.assertIsInstance(current.id, int) |
| 363 |
self.assertIsInstance(interp.id, int) |
| 364 |
|
| 365 |
def test_id_readonly(self): |
| 366 |
interp = interpreters.create() |
| 367 |
with self.assertRaises(AttributeError): |
| 368 |
interp.id = 1_000_000 |
| 369 |
|
| 370 |
def test_whence(self): |
| 371 |
main = interpreters.get_main() |
| 372 |
interp = interpreters.create() |
| 373 |
|
| 374 |
with self.subTest('main'): |
| 375 |
self.assertEqual(main.whence, WHENCE_STR_RUNTIME) |
| 376 |
|
| 377 |
with self.subTest('from _interpreters'): |
| 378 |
self.assertEqual(interp.whence, WHENCE_STR_STDLIB) |
| 379 |
|
| 380 |
with self.subTest('from C-API'): |
| 381 |
text = self.run_temp_from_capi(f""" |
| 382 |
import {interpreters.__name__} as interpreters |
| 383 |
interp = interpreters.get_current() |
| 384 |
print(repr(interp.whence)) |
| 385 |
""") |
| 386 |
whence = eval(text) |
| 387 |
self.assertEqual(whence, WHENCE_STR_CAPI) |
| 388 |
|
| 389 |
with self.subTest('readonly'): |
| 390 |
for value in [ |
| 391 |
None, |
| 392 |
WHENCE_STR_UNKNOWN, |
| 393 |
WHENCE_STR_RUNTIME, |
| 394 |
WHENCE_STR_STDLIB, |
| 395 |
WHENCE_STR_CAPI, |
| 396 |
]: |
| 397 |
with self.assertRaises(AttributeError): |
| 398 |
interp.whence = value |
| 399 |
with self.assertRaises(AttributeError): |
| 400 |
main.whence = value |
| 401 |
|
| 402 |
def test_hashable(self): |
| 403 |
interp = interpreters.create() |
| 404 |
expected = hash(interp.id) |
| 405 |
actual = hash(interp) |
| 406 |
self.assertEqual(actual, expected) |
| 407 |
|
| 408 |
def test_equality(self): |
| 409 |
interp1 = interpreters.create() |
| 410 |
interp2 = interpreters.create() |
| 411 |
self.assertEqual(interp1, interp1) |
| 412 |
self.assertNotEqual(interp1, interp2) |
| 413 |
|
| 414 |
def test_pickle(self): |
| 415 |
interp = interpreters.create() |
| 416 |
for protocol in range(pickle.HIGHEST_PROTOCOL + 1): |
| 417 |
with self.subTest(protocol=protocol): |
| 418 |
data = pickle.dumps(interp, protocol) |
| 419 |
unpickled = pickle.loads(data) |
| 420 |
self.assertEqual(unpickled, interp) |
| 421 |
|
| 422 |
@support.requires_subprocess() |
| 423 |
@force_not_colorized |
| 424 |
def test_cleanup_in_repl(self): |
| 425 |
# GH-135729: Using a subinterpreter in the REPL would lead to an unraisable |
| 426 |
# exception during finalization |
| 427 |
repl = script_helper.spawn_python("-i") |
| 428 |
script = b"""if True: |
| 429 |
from concurrent import interpreters |
| 430 |
interpreters.create() |
| 431 |
exit()""" |
| 432 |
stdout, stderr = repl.communicate(script) |
| 433 |
self.assertIsNone(stderr) |
| 434 |
self.assertIn(b"Interpreter.close()", stdout) |
| 435 |
self.assertNotIn(b"Traceback", stdout) |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
class TestInterpreterIsRunning(TestBase): |
| 440 |
|
| 441 |
def test_main(self): |
| 442 |
main = interpreters.get_main() |
| 443 |
self.assertTrue(main.is_running()) |
| 444 |
|
| 445 |
# XXX Is this still true? |
| 446 |
@unittest.skip('Fails on FreeBSD') |
| 447 |
def test_subinterpreter(self): |
| 448 |
interp = interpreters.create() |
| 449 |
self.assertFalse(interp.is_running()) |
| 450 |
|
| 451 |
with _running(interp): |
| 452 |
self.assertTrue(interp.is_running()) |
| 453 |
self.assertFalse(interp.is_running()) |
| 454 |
|
| 455 |
def test_finished(self): |
| 456 |
r, w = self.pipe() |
| 457 |
interp = interpreters.create() |
| 458 |
interp.exec(f"""if True: |
| 459 |
import os |
| 460 |
os.write({w}, b'x') |
| 461 |
""") |
| 462 |
self.assertFalse(interp.is_running()) |
| 463 |
self.assertEqual(os.read(r, 1), b'x') |
| 464 |
|
| 465 |
def test_from_subinterpreter(self): |
| 466 |
interp = interpreters.create() |
| 467 |
out = _run_output(interp, dedent(f""" |
| 468 |
import _interpreters |
| 469 |
if _interpreters.is_running({interp.id}): |
| 470 |
print(True) |
| 471 |
else: |
| 472 |
print(False) |
| 473 |
""")) |
| 474 |
self.assertEqual(out.strip(), 'True') |
| 475 |
|
| 476 |
def test_already_destroyed(self): |
| 477 |
interp = interpreters.create() |
| 478 |
interp.close() |
| 479 |
with self.assertRaises(InterpreterNotFoundError): |
| 480 |
interp.is_running() |
| 481 |
|
| 482 |
def test_with_only_background_threads(self): |
| 483 |
r_interp, w_interp = self.pipe() |
| 484 |
r_thread, w_thread = self.pipe() |
| 485 |
|
| 486 |
DONE = b'D' |
| 487 |
FINISHED = b'F' |
| 488 |
|
| 489 |
interp = interpreters.create() |
| 490 |
interp.exec(f"""if True: |
| 491 |
import os |
| 492 |
import threading |
| 493 |
|
| 494 |
def task(): |
| 495 |
v = os.read({r_thread}, 1) |
| 496 |
assert v == {DONE!r} |
| 497 |
os.write({w_interp}, {FINISHED!r}) |
| 498 |
t = threading.Thread(target=task) |
| 499 |
t.start() |
| 500 |
""") |
| 501 |
self.assertFalse(interp.is_running()) |
| 502 |
|
| 503 |
os.write(w_thread, DONE) |
| 504 |
interp.exec('t.join()') |
| 505 |
self.assertEqual(os.read(r_interp, 1), FINISHED) |
| 506 |
|
| 507 |
def test_created_with_capi(self): |
| 508 |
script = dedent(f""" |
| 509 |
import {interpreters.__name__} as interpreters |
| 510 |
interp = interpreters.get_current() |
| 511 |
print(interp.is_running()) |
| 512 |
""") |
| 513 |
def parse_results(text): |
| 514 |
self.assertNotEqual(text, "") |
| 515 |
try: |
| 516 |
return eval(text) |
| 517 |
except Exception: |
| 518 |
raise Exception(repr(text)) |
| 519 |
|
| 520 |
with self.subTest('running __main__ (from self)'): |
| 521 |
with self.interpreter_from_capi() as interpid: |
| 522 |
text = self.run_from_capi(interpid, script, main=True) |
| 523 |
running = parse_results(text) |
| 524 |
self.assertTrue(running) |
| 525 |
|
| 526 |
with self.subTest('running, but not __main__ (from self)'): |
| 527 |
text = self.run_temp_from_capi(script) |
| 528 |
running = parse_results(text) |
| 529 |
self.assertFalse(running) |
| 530 |
|
| 531 |
with self.subTest('running __main__ (from other)'): |
| 532 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 533 |
before = interp.is_running() |
| 534 |
with self.running_from_capi(interpid, main=True): |
| 535 |
during = interp.is_running() |
| 536 |
after = interp.is_running() |
| 537 |
self.assertFalse(before) |
| 538 |
self.assertTrue(during) |
| 539 |
self.assertFalse(after) |
| 540 |
|
| 541 |
with self.subTest('running, but not __main__ (from other)'): |
| 542 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 543 |
before = interp.is_running() |
| 544 |
with self.running_from_capi(interpid, main=False): |
| 545 |
during = interp.is_running() |
| 546 |
after = interp.is_running() |
| 547 |
self.assertFalse(before) |
| 548 |
self.assertFalse(during) |
| 549 |
self.assertFalse(after) |
| 550 |
|
| 551 |
with self.subTest('not running (from other)'): |
| 552 |
with self.interpreter_obj_from_capi() as (interp, _): |
| 553 |
running = interp.is_running() |
| 554 |
self.assertFalse(running) |
| 555 |
|
| 556 |
|
| 557 |
class TestInterpreterClose(TestBase): |
| 558 |
|
| 559 |
def test_basic(self): |
| 560 |
main = interpreters.get_main() |
| 561 |
interp1 = interpreters.create() |
| 562 |
interp2 = interpreters.create() |
| 563 |
interp3 = interpreters.create() |
| 564 |
self.assertEqual(set(interpreters.list_all()), |
| 565 |
{main, interp1, interp2, interp3}) |
| 566 |
interp2.close() |
| 567 |
self.assertEqual(set(interpreters.list_all()), |
| 568 |
{main, interp1, interp3}) |
| 569 |
|
| 570 |
def test_all(self): |
| 571 |
before = set(interpreters.list_all()) |
| 572 |
interps = set() |
| 573 |
for _ in range(3): |
| 574 |
interp = interpreters.create() |
| 575 |
interps.add(interp) |
| 576 |
self.assertEqual(set(interpreters.list_all()), before | interps) |
| 577 |
for interp in interps: |
| 578 |
interp.close() |
| 579 |
self.assertEqual(set(interpreters.list_all()), before) |
| 580 |
|
| 581 |
def test_main(self): |
| 582 |
main, = interpreters.list_all() |
| 583 |
with self.assertRaises(InterpreterError): |
| 584 |
main.close() |
| 585 |
|
| 586 |
def f(): |
| 587 |
with self.assertRaises(InterpreterError): |
| 588 |
main.close() |
| 589 |
|
| 590 |
t = threading.Thread(target=f) |
| 591 |
t.start() |
| 592 |
t.join() |
| 593 |
|
| 594 |
def test_already_destroyed(self): |
| 595 |
interp = interpreters.create() |
| 596 |
interp.close() |
| 597 |
with self.assertRaises(InterpreterNotFoundError): |
| 598 |
interp.close() |
| 599 |
|
| 600 |
def test_from_current(self): |
| 601 |
main, = interpreters.list_all() |
| 602 |
interp = interpreters.create() |
| 603 |
out = _run_output(interp, dedent(f""" |
| 604 |
from concurrent import interpreters |
| 605 |
interp = interpreters.Interpreter({interp.id}) |
| 606 |
try: |
| 607 |
interp.close() |
| 608 |
except interpreters.InterpreterError: |
| 609 |
print('failed') |
| 610 |
""")) |
| 611 |
self.assertEqual(out.strip(), 'failed') |
| 612 |
self.assertEqual(set(interpreters.list_all()), {main, interp}) |
| 613 |
|
| 614 |
def test_from_sibling(self): |
| 615 |
main, = interpreters.list_all() |
| 616 |
interp1 = interpreters.create() |
| 617 |
interp2 = interpreters.create() |
| 618 |
self.assertEqual(set(interpreters.list_all()), |
| 619 |
{main, interp1, interp2}) |
| 620 |
interp1.exec(dedent(f""" |
| 621 |
from concurrent import interpreters |
| 622 |
interp2 = interpreters.Interpreter({interp2.id}) |
| 623 |
interp2.close() |
| 624 |
interp3 = interpreters.create() |
| 625 |
interp3.close() |
| 626 |
""")) |
| 627 |
self.assertEqual(set(interpreters.list_all()), {main, interp1}) |
| 628 |
|
| 629 |
def test_from_other_thread(self): |
| 630 |
interp = interpreters.create() |
| 631 |
def f(): |
| 632 |
interp.close() |
| 633 |
|
| 634 |
t = threading.Thread(target=f) |
| 635 |
t.start() |
| 636 |
t.join() |
| 637 |
|
| 638 |
# XXX Is this still true? |
| 639 |
@unittest.skip('Fails on FreeBSD') |
| 640 |
def test_still_running(self): |
| 641 |
main, = interpreters.list_all() |
| 642 |
interp = interpreters.create() |
| 643 |
with _running(interp): |
| 644 |
with self.assertRaises(InterpreterError): |
| 645 |
interp.close() |
| 646 |
self.assertTrue(interp.is_running()) |
| 647 |
|
| 648 |
def test_subthreads_still_running(self): |
| 649 |
r_interp, w_interp = self.pipe() |
| 650 |
r_thread, w_thread = self.pipe() |
| 651 |
|
| 652 |
FINISHED = b'F' |
| 653 |
|
| 654 |
interp = interpreters.create() |
| 655 |
interp.exec(f"""if True: |
| 656 |
import os |
| 657 |
import threading |
| 658 |
import time |
| 659 |
|
| 660 |
done = False |
| 661 |
|
| 662 |
def notify_fini(): |
| 663 |
global done |
| 664 |
done = True |
| 665 |
t.join() |
| 666 |
threading._register_atexit(notify_fini) |
| 667 |
|
| 668 |
def task(): |
| 669 |
while not done: |
| 670 |
time.sleep(0.1) |
| 671 |
os.write({w_interp}, {FINISHED!r}) |
| 672 |
t = threading.Thread(target=task) |
| 673 |
t.start() |
| 674 |
""") |
| 675 |
interp.close() |
| 676 |
|
| 677 |
self.assertEqual(os.read(r_interp, 1), FINISHED) |
| 678 |
|
| 679 |
def test_created_with_capi(self): |
| 680 |
script = dedent(f""" |
| 681 |
import {interpreters.__name__} as interpreters |
| 682 |
interp = interpreters.get_current() |
| 683 |
interp.close() |
| 684 |
""") |
| 685 |
|
| 686 |
with self.subTest('running __main__ (from self)'): |
| 687 |
with self.interpreter_from_capi() as interpid: |
| 688 |
with self.assertRaisesRegex(ExecutionFailed, |
| 689 |
'InterpreterError.*unrecognized'): |
| 690 |
self.run_from_capi(interpid, script, main=True) |
| 691 |
|
| 692 |
with self.subTest('running, but not __main__ (from self)'): |
| 693 |
with self.assertRaisesRegex(ExecutionFailed, |
| 694 |
'InterpreterError.*unrecognized'): |
| 695 |
self.run_temp_from_capi(script) |
| 696 |
|
| 697 |
with self.subTest('running __main__ (from other)'): |
| 698 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 699 |
with self.running_from_capi(interpid, main=True): |
| 700 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 701 |
interp.close() |
| 702 |
# Make sure it wssn't closed. |
| 703 |
self.assertTrue( |
| 704 |
self.interp_exists(interpid)) |
| 705 |
|
| 706 |
# The rest would be skipped until we deal with running threads when |
| 707 |
# interp.close() is called. However, the "whence" restrictions |
| 708 |
# trigger first. |
| 709 |
|
| 710 |
with self.subTest('running, but not __main__ (from other)'): |
| 711 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 712 |
with self.running_from_capi(interpid, main=False): |
| 713 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 714 |
interp.close() |
| 715 |
# Make sure it wssn't closed. |
| 716 |
self.assertTrue( |
| 717 |
self.interp_exists(interpid)) |
| 718 |
|
| 719 |
with self.subTest('not running (from other)'): |
| 720 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 721 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 722 |
interp.close() |
| 723 |
self.assertTrue( |
| 724 |
self.interp_exists(interpid)) |
| 725 |
|
| 726 |
|
| 727 |
def test_remaining_threads(self): |
| 728 |
r_interp, w_interp = self.pipe() |
| 729 |
|
| 730 |
FINISHED = b'F' |
| 731 |
|
| 732 |
# It's unlikely, but technically speaking, it's possible |
| 733 |
# that the thread could've finished before interp.close() is |
| 734 |
# reached, so this test might not properly exercise the case. |
| 735 |
# However, it's quite unlikely and probably not worth bothering about. |
| 736 |
interp = interpreters.create() |
| 737 |
interp.exec(f"""if True: |
| 738 |
import os |
| 739 |
import threading |
| 740 |
import time |
| 741 |
|
| 742 |
def task(): |
| 743 |
time.sleep(1) |
| 744 |
os.write({w_interp}, {FINISHED!r}) |
| 745 |
|
| 746 |
threads = (threading.Thread(target=task) for _ in range(3)) |
| 747 |
for t in threads: |
| 748 |
t.start() |
| 749 |
""") |
| 750 |
interp.close() |
| 751 |
|
| 752 |
self.assertEqual(os.read(r_interp, 1), FINISHED) |
| 753 |
|
| 754 |
def test_remaining_daemon_threads(self): |
| 755 |
# Daemon threads leak reference by nature, because they hang threads |
| 756 |
# without allowing them to do cleanup (i.e., release refs). |
| 757 |
# To prevent that from messing up the refleak hunter and whatnot, we |
| 758 |
# run this in a subprocess. |
| 759 |
code = '''if True: |
| 760 |
import _interpreters |
| 761 |
import types |
| 762 |
interp = _interpreters.create( |
| 763 |
types.SimpleNamespace( |
| 764 |
use_main_obmalloc=False, |
| 765 |
allow_fork=False, |
| 766 |
allow_exec=False, |
| 767 |
allow_threads=True, |
| 768 |
allow_daemon_threads=True, |
| 769 |
check_multi_interp_extensions=True, |
| 770 |
gil='own', |
| 771 |
) |
| 772 |
) |
| 773 |
_interpreters.exec(interp, f"""if True: |
| 774 |
import threading |
| 775 |
import time |
| 776 |
|
| 777 |
def task(): |
| 778 |
time.sleep(3) |
| 779 |
|
| 780 |
threads = (threading.Thread(target=task, daemon=True) for _ in range(3)) |
| 781 |
for t in threads: |
| 782 |
t.start() |
| 783 |
""") |
| 784 |
_interpreters.destroy(interp) |
| 785 |
''' |
| 786 |
assert_python_ok('-c', code) |
| 787 |
|
| 788 |
|
| 789 |
class TestInterpreterPrepareMain(TestBase): |
| 790 |
|
| 791 |
def test_empty(self): |
| 792 |
interp = interpreters.create() |
| 793 |
with self.assertRaises(ValueError): |
| 794 |
interp.prepare_main() |
| 795 |
|
| 796 |
def test_dict(self): |
| 797 |
values = {'spam': 42, 'eggs': 'ham'} |
| 798 |
interp = interpreters.create() |
| 799 |
interp.prepare_main(values) |
| 800 |
out = _run_output(interp, dedent(""" |
| 801 |
print(spam, eggs) |
| 802 |
""")) |
| 803 |
self.assertEqual(out.strip(), '42 ham') |
| 804 |
|
| 805 |
def test_tuple(self): |
| 806 |
values = {'spam': 42, 'eggs': 'ham'} |
| 807 |
values = tuple(values.items()) |
| 808 |
interp = interpreters.create() |
| 809 |
interp.prepare_main(values) |
| 810 |
out = _run_output(interp, dedent(""" |
| 811 |
print(spam, eggs) |
| 812 |
""")) |
| 813 |
self.assertEqual(out.strip(), '42 ham') |
| 814 |
|
| 815 |
def test_kwargs(self): |
| 816 |
values = {'spam': 42, 'eggs': 'ham'} |
| 817 |
interp = interpreters.create() |
| 818 |
interp.prepare_main(**values) |
| 819 |
out = _run_output(interp, dedent(""" |
| 820 |
print(spam, eggs) |
| 821 |
""")) |
| 822 |
self.assertEqual(out.strip(), '42 ham') |
| 823 |
|
| 824 |
def test_dict_and_kwargs(self): |
| 825 |
values = {'spam': 42, 'eggs': 'ham'} |
| 826 |
interp = interpreters.create() |
| 827 |
interp.prepare_main(values, foo='bar') |
| 828 |
out = _run_output(interp, dedent(""" |
| 829 |
print(spam, eggs, foo) |
| 830 |
""")) |
| 831 |
self.assertEqual(out.strip(), '42 ham bar') |
| 832 |
|
| 833 |
def test_not_shareable(self): |
| 834 |
interp = interpreters.create() |
| 835 |
with self.assertRaises(interpreters.NotShareableError): |
| 836 |
interp.prepare_main(spam={'spam': 'eggs', 'foo': 'bar'}) |
| 837 |
|
| 838 |
# Make sure neither was actually bound. |
| 839 |
with self.assertRaises(ExecutionFailed): |
| 840 |
interp.exec('print(foo)') |
| 841 |
with self.assertRaises(ExecutionFailed): |
| 842 |
interp.exec('print(spam)') |
| 843 |
|
| 844 |
def test_running(self): |
| 845 |
interp = interpreters.create() |
| 846 |
interp.prepare_main({'spam': True}) |
| 847 |
with self.running(interp): |
| 848 |
with self.assertRaisesRegex(InterpreterError, 'running'): |
| 849 |
interp.prepare_main({'spam': False}) |
| 850 |
interp.exec('assert spam is True') |
| 851 |
|
| 852 |
@requires_test_modules |
| 853 |
def test_created_with_capi(self): |
| 854 |
with self.interpreter_obj_from_capi() as (interp, interpid): |
| 855 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 856 |
interp.prepare_main({'spam': True}) |
| 857 |
with self.assertRaisesRegex(ExecutionFailed, 'NameError'): |
| 858 |
self.run_from_capi(interpid, 'assert spam is True') |
| 859 |
|
| 860 |
|
| 861 |
class TestInterpreterExec(TestBase): |
| 862 |
|
| 863 |
def test_success(self): |
| 864 |
interp = interpreters.create() |
| 865 |
script, results = _captured_script('print("it worked!", end="")') |
| 866 |
with results: |
| 867 |
interp.exec(script) |
| 868 |
results = results.final() |
| 869 |
results.raise_if_failed() |
| 870 |
out = results.stdout |
| 871 |
|
| 872 |
self.assertEqual(out, 'it worked!') |
| 873 |
|
| 874 |
def test_failure(self): |
| 875 |
interp = interpreters.create() |
| 876 |
with self.assertRaises(ExecutionFailed): |
| 877 |
interp.exec('raise Exception') |
| 878 |
|
| 879 |
@force_not_colorized |
| 880 |
def test_display_preserved_exception(self): |
| 881 |
tempdir = self.temp_dir() |
| 882 |
modfile = self.make_module('spam', tempdir, text=""" |
| 883 |
def ham(): |
| 884 |
raise RuntimeError('uh-oh!') |
| 885 |
|
| 886 |
def eggs(): |
| 887 |
ham() |
| 888 |
""") |
| 889 |
scriptfile = self.make_script('script.py', tempdir, text=""" |
| 890 |
from concurrent import interpreters |
| 891 |
|
| 892 |
def script(): |
| 893 |
import spam |
| 894 |
spam.eggs() |
| 895 |
|
| 896 |
interp = interpreters.create() |
| 897 |
try: |
| 898 |
interp.exec(script) |
| 899 |
finally: |
| 900 |
interp.close() |
| 901 |
""") |
| 902 |
|
| 903 |
stdout, stderr = self.assert_python_failure(scriptfile) |
| 904 |
self.maxDiff = None |
| 905 |
interpmod_line, = (l for l in stderr.splitlines() if ' exec' in l) |
| 906 |
# File "{interpreters.__file__}", line 179, in exec |
| 907 |
self.assertEqual(stderr, dedent(f"""\ |
| 908 |
Traceback (most recent call last): |
| 909 |
File "{scriptfile}", line 10, in <module> |
| 910 |
interp.exec(script) |
| 911 |
~~~~~~~~~~~^^^^^^^^ |
| 912 |
{interpmod_line.strip()} |
| 913 |
raise ExecutionFailed(excinfo) |
| 914 |
concurrent.interpreters.ExecutionFailed: RuntimeError: uh-oh! |
| 915 |
|
| 916 |
Uncaught in the interpreter: |
| 917 |
|
| 918 |
Traceback (most recent call last): |
| 919 |
File "{scriptfile}", line 6, in script |
| 920 |
spam.eggs() |
| 921 |
~~~~~~~~~^^ |
| 922 |
File "{modfile}", line 6, in eggs |
| 923 |
ham() |
| 924 |
~~~^^ |
| 925 |
File "{modfile}", line 3, in ham |
| 926 |
raise RuntimeError('uh-oh!') |
| 927 |
RuntimeError: uh-oh! |
| 928 |
""")) |
| 929 |
self.assertEqual(stdout, '') |
| 930 |
|
| 931 |
def test_in_thread(self): |
| 932 |
interp = interpreters.create() |
| 933 |
script, results = _captured_script('print("it worked!", end="")') |
| 934 |
with results: |
| 935 |
def f(): |
| 936 |
interp.exec(script) |
| 937 |
|
| 938 |
t = threading.Thread(target=f) |
| 939 |
t.start() |
| 940 |
t.join() |
| 941 |
results = results.final() |
| 942 |
results.raise_if_failed() |
| 943 |
out = results.stdout |
| 944 |
|
| 945 |
self.assertEqual(out, 'it worked!') |
| 946 |
|
| 947 |
@support.requires_fork() |
| 948 |
def test_fork(self): |
| 949 |
interp = interpreters.create() |
| 950 |
import tempfile |
| 951 |
with tempfile.NamedTemporaryFile('w+', encoding='utf-8') as file: |
| 952 |
file.write('') |
| 953 |
file.flush() |
| 954 |
|
| 955 |
expected = 'spam spam spam spam spam' |
| 956 |
script = dedent(f""" |
| 957 |
import os |
| 958 |
try: |
| 959 |
os.fork() |
| 960 |
except RuntimeError: |
| 961 |
with open('{file.name}', 'w', encoding='utf-8') as out: |
| 962 |
out.write('{expected}') |
| 963 |
""") |
| 964 |
interp.exec(script) |
| 965 |
|
| 966 |
file.seek(0) |
| 967 |
content = file.read() |
| 968 |
self.assertEqual(content, expected) |
| 969 |
|
| 970 |
# XXX Is this still true? |
| 971 |
@unittest.skip('Fails on FreeBSD') |
| 972 |
def test_already_running(self): |
| 973 |
interp = interpreters.create() |
| 974 |
with _running(interp): |
| 975 |
with self.assertRaises(RuntimeError): |
| 976 |
interp.exec('print("spam")') |
| 977 |
|
| 978 |
def test_bad_script(self): |
| 979 |
interp = interpreters.create() |
| 980 |
with self.assertRaises(TypeError): |
| 981 |
interp.exec(10) |
| 982 |
|
| 983 |
def test_bytes_for_script(self): |
| 984 |
r, w = self.pipe() |
| 985 |
RAN = b'R' |
| 986 |
DONE = b'D' |
| 987 |
interp = interpreters.create() |
| 988 |
interp.exec(f"""if True: |
| 989 |
import os |
| 990 |
os.write({w}, {RAN!r}) |
| 991 |
""") |
| 992 |
os.write(w, DONE) |
| 993 |
self.assertEqual(os.read(r, 1), RAN) |
| 994 |
|
| 995 |
def test_with_background_threads_still_running(self): |
| 996 |
r_interp, w_interp = self.pipe() |
| 997 |
r_thread, w_thread = self.pipe() |
| 998 |
|
| 999 |
RAN = b'R' |
| 1000 |
DONE = b'D' |
| 1001 |
FINISHED = b'F' |
| 1002 |
|
| 1003 |
interp = interpreters.create() |
| 1004 |
interp.exec(f"""if True: |
| 1005 |
import os |
| 1006 |
import threading |
| 1007 |
|
| 1008 |
def task(): |
| 1009 |
v = os.read({r_thread}, 1) |
| 1010 |
assert v == {DONE!r} |
| 1011 |
os.write({w_interp}, {FINISHED!r}) |
| 1012 |
t = threading.Thread(target=task) |
| 1013 |
t.start() |
| 1014 |
os.write({w_interp}, {RAN!r}) |
| 1015 |
""") |
| 1016 |
interp.exec(f"""if True: |
| 1017 |
os.write({w_interp}, {RAN!r}) |
| 1018 |
""") |
| 1019 |
|
| 1020 |
os.write(w_thread, DONE) |
| 1021 |
interp.exec('t.join()') |
| 1022 |
self.assertEqual(os.read(r_interp, 1), RAN) |
| 1023 |
self.assertEqual(os.read(r_interp, 1), RAN) |
| 1024 |
self.assertEqual(os.read(r_interp, 1), FINISHED) |
| 1025 |
|
| 1026 |
def test_created_with_capi(self): |
| 1027 |
with self.interpreter_obj_from_capi() as (interp, _): |
| 1028 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 1029 |
interp.exec('raise Exception("it worked!")') |
| 1030 |
|
| 1031 |
def test_list_comprehension(self): |
| 1032 |
# gh-135450: List comprehensions caused an assertion failure |
| 1033 |
# in _PyCode_CheckNoExternalState() |
| 1034 |
import string |
| 1035 |
r_interp, w_interp = self.pipe() |
| 1036 |
|
| 1037 |
interp = interpreters.create() |
| 1038 |
interp.exec(f"""if True: |
| 1039 |
import os |
| 1040 |
comp = [str(i) for i in range(10)] |
| 1041 |
os.write({w_interp}, ''.join(comp).encode()) |
| 1042 |
""") |
| 1043 |
self.assertEqual(os.read(r_interp, 10).decode(), string.digits) |
| 1044 |
interp.close() |
| 1045 |
|
| 1046 |
|
| 1047 |
# test__interpreters covers the remaining |
| 1048 |
# Interpreter.exec() behavior. |
| 1049 |
|
| 1050 |
|
| 1051 |
call_func_noop = defs.spam_minimal |
| 1052 |
call_func_ident = defs.spam_returns_arg |
| 1053 |
call_func_failure = defs.spam_raises |
| 1054 |
|
| 1055 |
|
| 1056 |
def call_func_return_shareable(): |
| 1057 |
return (1, None) |
| 1058 |
|
| 1059 |
|
| 1060 |
def call_func_return_stateless_func(): |
| 1061 |
return (lambda x: x) |
| 1062 |
|
| 1063 |
|
| 1064 |
def call_func_return_pickleable(): |
| 1065 |
return [1, 2, 3] |
| 1066 |
|
| 1067 |
|
| 1068 |
def call_func_return_unpickleable(): |
| 1069 |
x = 42 |
| 1070 |
return (lambda: x) |
| 1071 |
|
| 1072 |
|
| 1073 |
def get_call_func_closure(value): |
| 1074 |
def call_func_closure(): |
| 1075 |
return value |
| 1076 |
return call_func_closure |
| 1077 |
|
| 1078 |
|
| 1079 |
def call_func_exec_wrapper(script, ns): |
| 1080 |
res = exec(script, ns, ns) |
| 1081 |
return res, ns, id(ns) |
| 1082 |
|
| 1083 |
|
| 1084 |
class Spam: |
| 1085 |
|
| 1086 |
@staticmethod |
| 1087 |
def noop(): |
| 1088 |
pass |
| 1089 |
|
| 1090 |
@classmethod |
| 1091 |
def from_values(cls, *values): |
| 1092 |
return cls(values) |
| 1093 |
|
| 1094 |
def __init__(self, value): |
| 1095 |
self.value = value |
| 1096 |
|
| 1097 |
def __call__(self, *args, **kwargs): |
| 1098 |
return (self.value, args, kwargs) |
| 1099 |
|
| 1100 |
def __eq__(self, other): |
| 1101 |
if not isinstance(other, Spam): |
| 1102 |
return NotImplemented |
| 1103 |
return self.value == other.value |
| 1104 |
|
| 1105 |
def run(self, *args, **kwargs): |
| 1106 |
return (self.value, args, kwargs) |
| 1107 |
|
| 1108 |
|
| 1109 |
def call_func_complex(op, /, value=None, *args, exc=None, **kwargs): |
| 1110 |
if exc is not None: |
| 1111 |
raise exc |
| 1112 |
if op == '': |
| 1113 |
raise ValueError('missing op') |
| 1114 |
elif op == 'ident': |
| 1115 |
if args or kwargs: |
| 1116 |
raise Exception((args, kwargs)) |
| 1117 |
return value |
| 1118 |
elif op == 'full-ident': |
| 1119 |
return (value, args, kwargs) |
| 1120 |
elif op == 'globals': |
| 1121 |
if value is not None or args or kwargs: |
| 1122 |
raise Exception((value, args, kwargs)) |
| 1123 |
return __name__ |
| 1124 |
elif op == 'interpid': |
| 1125 |
if value is not None or args or kwargs: |
| 1126 |
raise Exception((value, args, kwargs)) |
| 1127 |
return interpreters.get_current().id |
| 1128 |
elif op == 'closure': |
| 1129 |
if args or kwargs: |
| 1130 |
raise Exception((args, kwargs)) |
| 1131 |
return get_call_func_closure(value) |
| 1132 |
elif op == 'custom': |
| 1133 |
if args or kwargs: |
| 1134 |
raise Exception((args, kwargs)) |
| 1135 |
return Spam(value) |
| 1136 |
elif op == 'custom-inner': |
| 1137 |
if args or kwargs: |
| 1138 |
raise Exception((args, kwargs)) |
| 1139 |
class Eggs(Spam): |
| 1140 |
pass |
| 1141 |
return Eggs(value) |
| 1142 |
elif not isinstance(op, str): |
| 1143 |
raise TypeError(op) |
| 1144 |
else: |
| 1145 |
raise NotImplementedError(op) |
| 1146 |
|
| 1147 |
|
| 1148 |
class TestInterpreterCall(TestBase): |
| 1149 |
|
| 1150 |
# signature |
| 1151 |
# - blank |
| 1152 |
# - args |
| 1153 |
# - kwargs |
| 1154 |
# - args, kwargs |
| 1155 |
# return |
| 1156 |
# - nothing (None) |
| 1157 |
# - simple |
| 1158 |
# - closure |
| 1159 |
# - custom |
| 1160 |
# ops: |
| 1161 |
# - do nothing |
| 1162 |
# - fail |
| 1163 |
# - echo |
| 1164 |
# - do complex, relative to interpreter |
| 1165 |
# scope |
| 1166 |
# - global func |
| 1167 |
# - local closure |
| 1168 |
# - returned closure |
| 1169 |
# - callable type instance |
| 1170 |
# - type |
| 1171 |
# - classmethod |
| 1172 |
# - staticmethod |
| 1173 |
# - instance method |
| 1174 |
# exception |
| 1175 |
# - builtin |
| 1176 |
# - custom |
| 1177 |
# - preserves info (e.g. SyntaxError) |
| 1178 |
# - matching error display |
| 1179 |
|
| 1180 |
@contextlib.contextmanager |
| 1181 |
def assert_fails(self, expected): |
| 1182 |
with self.assertRaises(ExecutionFailed) as cm: |
| 1183 |
yield cm |
| 1184 |
uncaught = cm.exception.excinfo |
| 1185 |
self.assertEqual(uncaught.type.__name__, expected.__name__) |
| 1186 |
|
| 1187 |
def assert_fails_not_shareable(self): |
| 1188 |
return self.assert_fails(interpreters.NotShareableError) |
| 1189 |
|
| 1190 |
def assert_code_equal(self, code1, code2): |
| 1191 |
if code1 == code2: |
| 1192 |
return |
| 1193 |
self.assertEqual(code1.co_name, code2.co_name) |
| 1194 |
self.assertEqual(code1.co_flags, code2.co_flags) |
| 1195 |
self.assertEqual(code1.co_consts, code2.co_consts) |
| 1196 |
self.assertEqual(code1.co_varnames, code2.co_varnames) |
| 1197 |
self.assertEqual(code1.co_cellvars, code2.co_cellvars) |
| 1198 |
self.assertEqual(code1.co_freevars, code2.co_freevars) |
| 1199 |
self.assertEqual(code1.co_names, code2.co_names) |
| 1200 |
self.assertEqual( |
| 1201 |
_testinternalcapi.get_code_var_counts(code1), |
| 1202 |
_testinternalcapi.get_code_var_counts(code2), |
| 1203 |
) |
| 1204 |
self.assertEqual(code1.co_code, code2.co_code) |
| 1205 |
|
| 1206 |
def assert_funcs_equal(self, func1, func2): |
| 1207 |
if func1 == func2: |
| 1208 |
return |
| 1209 |
self.assertIs(type(func1), type(func2)) |
| 1210 |
self.assertEqual(func1.__name__, func2.__name__) |
| 1211 |
self.assertEqual(func1.__defaults__, func2.__defaults__) |
| 1212 |
self.assertEqual(func1.__kwdefaults__, func2.__kwdefaults__) |
| 1213 |
self.assertEqual(func1.__closure__, func2.__closure__) |
| 1214 |
self.assert_code_equal(func1.__code__, func2.__code__) |
| 1215 |
self.assertEqual( |
| 1216 |
_testinternalcapi.get_code_var_counts(func1), |
| 1217 |
_testinternalcapi.get_code_var_counts(func2), |
| 1218 |
) |
| 1219 |
|
| 1220 |
def assert_exceptions_equal(self, exc1, exc2): |
| 1221 |
assert isinstance(exc1, Exception) |
| 1222 |
assert isinstance(exc2, Exception) |
| 1223 |
if exc1 == exc2: |
| 1224 |
return |
| 1225 |
self.assertIs(type(exc1), type(exc2)) |
| 1226 |
self.assertEqual(exc1.args, exc2.args) |
| 1227 |
|
| 1228 |
def test_stateless_funcs(self): |
| 1229 |
interp = interpreters.create() |
| 1230 |
|
| 1231 |
func = call_func_noop |
| 1232 |
with self.subTest('no args, no return'): |
| 1233 |
res = interp.call(func) |
| 1234 |
self.assertIsNone(res) |
| 1235 |
|
| 1236 |
func = call_func_return_shareable |
| 1237 |
with self.subTest('no args, returns shareable'): |
| 1238 |
res = interp.call(func) |
| 1239 |
self.assertEqual(res, (1, None)) |
| 1240 |
|
| 1241 |
func = call_func_return_stateless_func |
| 1242 |
expected = (lambda x: x) |
| 1243 |
with self.subTest('no args, returns stateless func'): |
| 1244 |
res = interp.call(func) |
| 1245 |
self.assert_funcs_equal(res, expected) |
| 1246 |
|
| 1247 |
func = call_func_return_pickleable |
| 1248 |
with self.subTest('no args, returns pickleable'): |
| 1249 |
res = interp.call(func) |
| 1250 |
self.assertEqual(res, [1, 2, 3]) |
| 1251 |
|
| 1252 |
func = call_func_return_unpickleable |
| 1253 |
with self.subTest('no args, returns unpickleable'): |
| 1254 |
with self.assertRaises(interpreters.NotShareableError): |
| 1255 |
interp.call(func) |
| 1256 |
|
| 1257 |
def test_stateless_func_returns_arg(self): |
| 1258 |
interp = interpreters.create() |
| 1259 |
|
| 1260 |
for arg in [ |
| 1261 |
None, |
| 1262 |
10, |
| 1263 |
'spam!', |
| 1264 |
b'spam!', |
| 1265 |
(1, 2, 'spam!'), |
| 1266 |
memoryview(b'spam!'), |
| 1267 |
]: |
| 1268 |
with self.subTest(f'shareable {arg!r}'): |
| 1269 |
assert _interpreters.is_shareable(arg) |
| 1270 |
res = interp.call(defs.spam_returns_arg, arg) |
| 1271 |
self.assertEqual(res, arg) |
| 1272 |
|
| 1273 |
for arg in defs.STATELESS_FUNCTIONS: |
| 1274 |
with self.subTest(f'stateless func {arg!r}'): |
| 1275 |
res = interp.call(defs.spam_returns_arg, arg) |
| 1276 |
self.assert_funcs_equal(res, arg) |
| 1277 |
|
| 1278 |
for arg in defs.TOP_FUNCTIONS: |
| 1279 |
if arg in defs.STATELESS_FUNCTIONS: |
| 1280 |
continue |
| 1281 |
with self.subTest(f'stateful func {arg!r}'): |
| 1282 |
res = interp.call(defs.spam_returns_arg, arg) |
| 1283 |
self.assert_funcs_equal(res, arg) |
| 1284 |
assert is_pickleable(arg) |
| 1285 |
|
| 1286 |
for arg in [ |
| 1287 |
Ellipsis, |
| 1288 |
NotImplemented, |
| 1289 |
object(), |
| 1290 |
2**1000, |
| 1291 |
[1, 2, 3], |
| 1292 |
{'a': 1, 'b': 2}, |
| 1293 |
types.SimpleNamespace(x=42), |
| 1294 |
# builtin types |
| 1295 |
object, |
| 1296 |
type, |
| 1297 |
Exception, |
| 1298 |
ModuleNotFoundError, |
| 1299 |
# builtin exceptions |
| 1300 |
Exception('uh-oh!'), |
| 1301 |
ModuleNotFoundError('mymodule'), |
| 1302 |
# builtin fnctions |
| 1303 |
len, |
| 1304 |
sys.exit, |
| 1305 |
# user classes |
| 1306 |
*defs.TOP_CLASSES, |
| 1307 |
*(c(*a) for c, a in defs.TOP_CLASSES.items() |
| 1308 |
if c not in defs.CLASSES_WITHOUT_EQUALITY), |
| 1309 |
]: |
| 1310 |
with self.subTest(f'pickleable {arg!r}'): |
| 1311 |
res = interp.call(defs.spam_returns_arg, arg) |
| 1312 |
if type(arg) is object: |
| 1313 |
self.assertIs(type(res), object) |
| 1314 |
elif isinstance(arg, BaseException): |
| 1315 |
self.assert_exceptions_equal(res, arg) |
| 1316 |
else: |
| 1317 |
self.assertEqual(res, arg) |
| 1318 |
assert is_pickleable(arg) |
| 1319 |
|
| 1320 |
for arg in [ |
| 1321 |
types.MappingProxyType({}), |
| 1322 |
*(f for f in defs.NESTED_FUNCTIONS |
| 1323 |
if f not in defs.STATELESS_FUNCTIONS), |
| 1324 |
]: |
| 1325 |
with self.subTest(f'unpickleable {arg!r}'): |
| 1326 |
assert not _interpreters.is_shareable(arg) |
| 1327 |
assert not is_pickleable(arg) |
| 1328 |
with self.assertRaises(interpreters.NotShareableError): |
| 1329 |
interp.call(defs.spam_returns_arg, arg) |
| 1330 |
|
| 1331 |
def test_full_args(self): |
| 1332 |
interp = interpreters.create() |
| 1333 |
expected = (1, 2, 3, 4, 5, 6, ('?',), {'g': 7, 'h': 8}) |
| 1334 |
func = defs.spam_full_args |
| 1335 |
res = interp.call(func, 1, 2, 3, 4, '?', e=5, f=6, g=7, h=8) |
| 1336 |
self.assertEqual(res, expected) |
| 1337 |
|
| 1338 |
def test_full_defaults(self): |
| 1339 |
# pickleable, but not stateless |
| 1340 |
interp = interpreters.create() |
| 1341 |
expected = (-1, -2, -3, -4, -5, -6, (), {'g': 8, 'h': 9}) |
| 1342 |
res = interp.call(defs.spam_full_args_with_defaults, g=8, h=9) |
| 1343 |
self.assertEqual(res, expected) |
| 1344 |
|
| 1345 |
def test_modified_arg(self): |
| 1346 |
interp = interpreters.create() |
| 1347 |
script = dedent(""" |
| 1348 |
a = 7 |
| 1349 |
b = 2 |
| 1350 |
c = a ** b |
| 1351 |
""") |
| 1352 |
ns = {} |
| 1353 |
expected = {'a': 7, 'b': 2, 'c': 49} |
| 1354 |
res = interp.call(call_func_exec_wrapper, script, ns) |
| 1355 |
obj, resns, resid = res |
| 1356 |
del resns['__builtins__'] |
| 1357 |
self.assertIsNone(obj) |
| 1358 |
self.assertEqual(ns, {}) |
| 1359 |
self.assertEqual(resns, expected) |
| 1360 |
self.assertNotEqual(resid, id(ns)) |
| 1361 |
self.assertNotEqual(resid, id(resns)) |
| 1362 |
|
| 1363 |
def test_func_in___main___valid(self): |
| 1364 |
# pickleable, already there' |
| 1365 |
|
| 1366 |
with os_helper.temp_dir() as tempdir: |
| 1367 |
def new_mod(name, text): |
| 1368 |
script_helper.make_script(tempdir, name, dedent(text)) |
| 1369 |
|
| 1370 |
def run(text): |
| 1371 |
name = 'myscript' |
| 1372 |
text = dedent(f""" |
| 1373 |
import sys |
| 1374 |
sys.path.insert(0, {tempdir!r}) |
| 1375 |
|
| 1376 |
""") + dedent(text) |
| 1377 |
filename = script_helper.make_script(tempdir, name, text) |
| 1378 |
res = script_helper.assert_python_ok(filename) |
| 1379 |
return res.out.decode('utf-8').strip() |
| 1380 |
|
| 1381 |
# no module indirection |
| 1382 |
with self.subTest('no indirection'): |
| 1383 |
text = run(f""" |
| 1384 |
from concurrent import interpreters |
| 1385 |
|
| 1386 |
def spam(): |
| 1387 |
# This a global var... |
| 1388 |
return __name__ |
| 1389 |
|
| 1390 |
if __name__ == '__main__': |
| 1391 |
interp = interpreters.create() |
| 1392 |
res = interp.call(spam) |
| 1393 |
print(res) |
| 1394 |
""") |
| 1395 |
self.assertEqual(text, '<fake __main__>') |
| 1396 |
|
| 1397 |
# indirect as func, direct interp |
| 1398 |
new_mod('mymod', f""" |
| 1399 |
def run(interp, func): |
| 1400 |
return interp.call(func) |
| 1401 |
""") |
| 1402 |
with self.subTest('indirect as func, direct interp'): |
| 1403 |
text = run(f""" |
| 1404 |
from concurrent import interpreters |
| 1405 |
import mymod |
| 1406 |
|
| 1407 |
def spam(): |
| 1408 |
# This a global var... |
| 1409 |
return __name__ |
| 1410 |
|
| 1411 |
if __name__ == '__main__': |
| 1412 |
interp = interpreters.create() |
| 1413 |
res = mymod.run(interp, spam) |
| 1414 |
print(res) |
| 1415 |
""") |
| 1416 |
self.assertEqual(text, '<fake __main__>') |
| 1417 |
|
| 1418 |
# indirect as func, indirect interp |
| 1419 |
new_mod('mymod', f""" |
| 1420 |
from concurrent import interpreters |
| 1421 |
def run(func): |
| 1422 |
interp = interpreters.create() |
| 1423 |
return interp.call(func) |
| 1424 |
""") |
| 1425 |
with self.subTest('indirect as func, indirect interp'): |
| 1426 |
text = run(f""" |
| 1427 |
import mymod |
| 1428 |
|
| 1429 |
def spam(): |
| 1430 |
# This a global var... |
| 1431 |
return __name__ |
| 1432 |
|
| 1433 |
if __name__ == '__main__': |
| 1434 |
res = mymod.run(spam) |
| 1435 |
print(res) |
| 1436 |
""") |
| 1437 |
self.assertEqual(text, '<fake __main__>') |
| 1438 |
|
| 1439 |
def test_func_in___main___invalid(self): |
| 1440 |
interp = interpreters.create() |
| 1441 |
|
| 1442 |
funcname = f'{__name__.replace(".", "_")}_spam_okay' |
| 1443 |
script = dedent(f""" |
| 1444 |
def {funcname}(): |
| 1445 |
# This a global var... |
| 1446 |
return __name__ |
| 1447 |
""") |
| 1448 |
|
| 1449 |
with self.subTest('pickleable, added dynamically'): |
| 1450 |
with defined_in___main__(funcname, script) as arg: |
| 1451 |
with self.assertRaises(interpreters.NotShareableError): |
| 1452 |
interp.call(defs.spam_returns_arg, arg) |
| 1453 |
|
| 1454 |
with self.subTest('lying about __main__'): |
| 1455 |
with defined_in___main__(funcname, script, remove=True) as arg: |
| 1456 |
with self.assertRaises(interpreters.NotShareableError): |
| 1457 |
interp.call(defs.spam_returns_arg, arg) |
| 1458 |
|
| 1459 |
def test_func_in___main___hidden(self): |
| 1460 |
# When a top-level function that uses global variables is called |
| 1461 |
# through Interpreter.call(), it will be pickled, sent over, |
| 1462 |
# and unpickled. That requires that it be found in the other |
| 1463 |
# interpreter's __main__ module. However, the original script |
| 1464 |
# that defined the function is only run in the main interpreter, |
| 1465 |
# so pickle.loads() would normally fail. |
| 1466 |
# |
| 1467 |
# We work around this by running the script in the other |
| 1468 |
# interpreter. However, this is a one-off solution for the sake |
| 1469 |
# of unpickling, so we avoid modifying that interpreter's |
| 1470 |
# __main__ module by running the script in a hidden module. |
| 1471 |
# |
| 1472 |
# In this test we verify that the function runs with the hidden |
| 1473 |
# module as its __globals__ when called in the other interpreter, |
| 1474 |
# and that the interpreter's __main__ module is unaffected. |
| 1475 |
text = dedent(""" |
| 1476 |
eggs = True |
| 1477 |
|
| 1478 |
def spam(*, explicit=False): |
| 1479 |
if explicit: |
| 1480 |
import __main__ |
| 1481 |
ns = __main__.__dict__ |
| 1482 |
else: |
| 1483 |
# For now we have to have a LOAD_GLOBAL in the |
| 1484 |
# function in order for globals() to actually return |
| 1485 |
# spam.__globals__. Maybe it doesn't go through pickle? |
| 1486 |
# XXX We will fix this later. |
| 1487 |
spam |
| 1488 |
ns = globals() |
| 1489 |
|
| 1490 |
func = ns.get('spam') |
| 1491 |
return [ |
| 1492 |
id(ns), |
| 1493 |
ns.get('__name__'), |
| 1494 |
ns.get('__file__'), |
| 1495 |
id(func), |
| 1496 |
None if func is None else repr(func), |
| 1497 |
ns.get('eggs'), |
| 1498 |
ns.get('ham'), |
| 1499 |
] |
| 1500 |
|
| 1501 |
if __name__ == "__main__": |
| 1502 |
from concurrent import interpreters |
| 1503 |
interp = interpreters.create() |
| 1504 |
|
| 1505 |
ham = True |
| 1506 |
print([ |
| 1507 |
[ |
| 1508 |
spam(explicit=True), |
| 1509 |
spam(), |
| 1510 |
], |
| 1511 |
[ |
| 1512 |
interp.call(spam, explicit=True), |
| 1513 |
interp.call(spam), |
| 1514 |
], |
| 1515 |
]) |
| 1516 |
""") |
| 1517 |
with os_helper.temp_dir() as tempdir: |
| 1518 |
filename = script_helper.make_script(tempdir, 'my-script', text) |
| 1519 |
res = script_helper.assert_python_ok(filename) |
| 1520 |
stdout = res.out.decode('utf-8').strip() |
| 1521 |
local, remote = eval(stdout) |
| 1522 |
|
| 1523 |
# In the main interpreter. |
| 1524 |
main, unpickled = local |
| 1525 |
nsid, _, _, funcid, func, _, _ = main |
| 1526 |
self.assertEqual(main, [ |
| 1527 |
nsid, |
| 1528 |
'__main__', |
| 1529 |
filename, |
| 1530 |
funcid, |
| 1531 |
func, |
| 1532 |
True, |
| 1533 |
True, |
| 1534 |
]) |
| 1535 |
self.assertIsNot(func, None) |
| 1536 |
self.assertRegex(func, '^<function spam at 0x.*>$') |
| 1537 |
self.assertEqual(unpickled, main) |
| 1538 |
|
| 1539 |
# In the subinterpreter. |
| 1540 |
main, unpickled = remote |
| 1541 |
nsid1, _, _, funcid1, _, _, _ = main |
| 1542 |
self.assertEqual(main, [ |
| 1543 |
nsid1, |
| 1544 |
'__main__', |
| 1545 |
None, |
| 1546 |
funcid1, |
| 1547 |
None, |
| 1548 |
None, |
| 1549 |
None, |
| 1550 |
]) |
| 1551 |
nsid2, _, _, funcid2, func, _, _ = unpickled |
| 1552 |
self.assertEqual(unpickled, [ |
| 1553 |
nsid2, |
| 1554 |
'<fake __main__>', |
| 1555 |
filename, |
| 1556 |
funcid2, |
| 1557 |
func, |
| 1558 |
True, |
| 1559 |
None, |
| 1560 |
]) |
| 1561 |
self.assertIsNot(func, None) |
| 1562 |
self.assertRegex(func, '^<function spam at 0x.*>$') |
| 1563 |
self.assertNotEqual(nsid2, nsid1) |
| 1564 |
self.assertNotEqual(funcid2, funcid1) |
| 1565 |
|
| 1566 |
def test_func_in___main___uses_globals(self): |
| 1567 |
# See the note in test_func_in___main___hidden about pickle |
| 1568 |
# and the __main__ module. |
| 1569 |
# |
| 1570 |
# Additionally, the solution to that problem must provide |
| 1571 |
# for global variables on which a pickled function might rely. |
| 1572 |
# |
| 1573 |
# To check that, we run a script that has two global functions |
| 1574 |
# and a global variable in the __main__ module. One of the |
| 1575 |
# functions sets the global variable and the other returns |
| 1576 |
# the value. |
| 1577 |
# |
| 1578 |
# The script calls those functions multiple times in another |
| 1579 |
# interpreter, to verify the following: |
| 1580 |
# |
| 1581 |
# * the global variable is properly initialized |
| 1582 |
# * the global variable retains state between calls |
| 1583 |
# * the setter modifies that persistent variable |
| 1584 |
# * the getter uses the variable |
| 1585 |
# * the calls in the other interpreter do not modify |
| 1586 |
# the main interpreter |
| 1587 |
# * those calls don't modify the interpreter's __main__ module |
| 1588 |
# * the functions and variable do not actually show up in the |
| 1589 |
# other interpreter's __main__ module |
| 1590 |
text = dedent(""" |
| 1591 |
count = 0 |
| 1592 |
|
| 1593 |
def inc(x=1): |
| 1594 |
global count |
| 1595 |
count += x |
| 1596 |
|
| 1597 |
def get_count(): |
| 1598 |
return count |
| 1599 |
|
| 1600 |
if __name__ == "__main__": |
| 1601 |
counts = [] |
| 1602 |
results = [count, counts] |
| 1603 |
|
| 1604 |
from concurrent import interpreters |
| 1605 |
interp = interpreters.create() |
| 1606 |
|
| 1607 |
val = interp.call(get_count) |
| 1608 |
counts.append(val) |
| 1609 |
|
| 1610 |
interp.call(inc) |
| 1611 |
val = interp.call(get_count) |
| 1612 |
counts.append(val) |
| 1613 |
|
| 1614 |
interp.call(inc, 3) |
| 1615 |
val = interp.call(get_count) |
| 1616 |
counts.append(val) |
| 1617 |
|
| 1618 |
results.append(count) |
| 1619 |
|
| 1620 |
modified = {name: interp.call(eval, f'{name!r} in vars()') |
| 1621 |
for name in ('count', 'inc', 'get_count')} |
| 1622 |
results.append(modified) |
| 1623 |
|
| 1624 |
print(results) |
| 1625 |
""") |
| 1626 |
with os_helper.temp_dir() as tempdir: |
| 1627 |
filename = script_helper.make_script(tempdir, 'my-script', text) |
| 1628 |
res = script_helper.assert_python_ok(filename) |
| 1629 |
stdout = res.out.decode('utf-8').strip() |
| 1630 |
before, counts, after, modified = eval(stdout) |
| 1631 |
self.assertEqual(modified, { |
| 1632 |
'count': False, |
| 1633 |
'inc': False, |
| 1634 |
'get_count': False, |
| 1635 |
}) |
| 1636 |
self.assertEqual(before, 0) |
| 1637 |
self.assertEqual(after, 0) |
| 1638 |
self.assertEqual(counts, [0, 1, 4]) |
| 1639 |
|
| 1640 |
def test_raises(self): |
| 1641 |
interp = interpreters.create() |
| 1642 |
with self.assertRaises(ExecutionFailed): |
| 1643 |
interp.call(call_func_failure) |
| 1644 |
|
| 1645 |
with self.assert_fails(ValueError): |
| 1646 |
interp.call(call_func_complex, '???', exc=ValueError('spam')) |
| 1647 |
|
| 1648 |
def test_call_valid(self): |
| 1649 |
interp = interpreters.create() |
| 1650 |
|
| 1651 |
for i, (callable, args, kwargs, expected) in enumerate([ |
| 1652 |
(call_func_noop, (), {}, None), |
| 1653 |
(call_func_ident, ('spamspamspam',), {}, 'spamspamspam'), |
| 1654 |
(call_func_return_shareable, (), {}, (1, None)), |
| 1655 |
(call_func_return_pickleable, (), {}, [1, 2, 3]), |
| 1656 |
(Spam.noop, (), {}, None), |
| 1657 |
(Spam.from_values, (), {}, Spam(())), |
| 1658 |
(Spam.from_values, (1, 2, 3), {}, Spam((1, 2, 3))), |
| 1659 |
(Spam, ('???',), {}, Spam('???')), |
| 1660 |
(Spam(101), (), {}, (101, (), {})), |
| 1661 |
(Spam(10101).run, (), {}, (10101, (), {})), |
| 1662 |
(call_func_complex, ('ident', 'spam'), {}, 'spam'), |
| 1663 |
(call_func_complex, ('full-ident', 'spam'), {}, ('spam', (), {})), |
| 1664 |
(call_func_complex, ('full-ident', 'spam', 'ham'), {'eggs': '!!!'}, |
| 1665 |
('spam', ('ham',), {'eggs': '!!!'})), |
| 1666 |
(call_func_complex, ('globals',), {}, __name__), |
| 1667 |
(call_func_complex, ('interpid',), {}, interp.id), |
| 1668 |
(call_func_complex, ('custom', 'spam!'), {}, Spam('spam!')), |
| 1669 |
]): |
| 1670 |
with self.subTest(f'success case #{i+1}'): |
| 1671 |
res = interp.call(callable, *args, **kwargs) |
| 1672 |
self.assertEqual(res, expected) |
| 1673 |
|
| 1674 |
def test_call_invalid(self): |
| 1675 |
interp = interpreters.create() |
| 1676 |
|
| 1677 |
func = get_call_func_closure |
| 1678 |
with self.subTest(func): |
| 1679 |
with self.assertRaises(interpreters.NotShareableError): |
| 1680 |
interp.call(func, 42) |
| 1681 |
|
| 1682 |
func = get_call_func_closure(42) |
| 1683 |
with self.subTest(func): |
| 1684 |
with self.assertRaises(interpreters.NotShareableError): |
| 1685 |
interp.call(func) |
| 1686 |
|
| 1687 |
func = call_func_complex |
| 1688 |
op = 'closure' |
| 1689 |
with self.subTest(f'{func} ({op})'): |
| 1690 |
with self.assertRaises(interpreters.NotShareableError): |
| 1691 |
interp.call(func, op, value='~~~') |
| 1692 |
|
| 1693 |
op = 'custom-inner' |
| 1694 |
with self.subTest(f'{func} ({op})'): |
| 1695 |
with self.assertRaises(interpreters.NotShareableError): |
| 1696 |
interp.call(func, op, 'eggs!') |
| 1697 |
|
| 1698 |
def test_callable_requires_frame(self): |
| 1699 |
# There are various functions that require a current frame. |
| 1700 |
interp = interpreters.create() |
| 1701 |
for call, expected in [ |
| 1702 |
((eval, '[1, 2, 3]'), |
| 1703 |
[1, 2, 3]), |
| 1704 |
((eval, 'sum([1, 2, 3])'), |
| 1705 |
6), |
| 1706 |
((exec, '...'), |
| 1707 |
None), |
| 1708 |
]: |
| 1709 |
with self.subTest(str(call)): |
| 1710 |
res = interp.call(*call) |
| 1711 |
self.assertEqual(res, expected) |
| 1712 |
|
| 1713 |
result_not_pickleable = [ |
| 1714 |
globals, |
| 1715 |
locals, |
| 1716 |
vars, |
| 1717 |
] |
| 1718 |
for func, expectedtype in { |
| 1719 |
globals: dict, |
| 1720 |
locals: dict, |
| 1721 |
vars: dict, |
| 1722 |
dir: list, |
| 1723 |
}.items(): |
| 1724 |
with self.subTest(str(func)): |
| 1725 |
if func in result_not_pickleable: |
| 1726 |
with self.assertRaises(interpreters.NotShareableError): |
| 1727 |
interp.call(func) |
| 1728 |
else: |
| 1729 |
res = interp.call(func) |
| 1730 |
self.assertIsInstance(res, expectedtype) |
| 1731 |
self.assertIn('__builtins__', res) |
| 1732 |
|
| 1733 |
def test_globals_from_builtins(self): |
| 1734 |
# The builtins exec(), eval(), globals(), locals(), vars(), |
| 1735 |
# and dir() each runs relative to the target interpreter's |
| 1736 |
# __main__ module, when called directly. However, |
| 1737 |
# globals(), locals(), and vars() don't work when called |
| 1738 |
# directly so we don't check them. |
| 1739 |
from _frozen_importlib import BuiltinImporter |
| 1740 |
interp = interpreters.create() |
| 1741 |
|
| 1742 |
names = interp.call(dir) |
| 1743 |
self.assertEqual(names, [ |
| 1744 |
'__builtins__', |
| 1745 |
'__doc__', |
| 1746 |
'__loader__', |
| 1747 |
'__name__', |
| 1748 |
'__package__', |
| 1749 |
'__spec__', |
| 1750 |
]) |
| 1751 |
|
| 1752 |
values = {name: interp.call(eval, name) |
| 1753 |
for name in names if name != '__builtins__'} |
| 1754 |
self.assertEqual(values, { |
| 1755 |
'__name__': '__main__', |
| 1756 |
'__doc__': None, |
| 1757 |
'__spec__': None, # It wasn't imported, so no module spec? |
| 1758 |
'__package__': None, |
| 1759 |
'__loader__': BuiltinImporter, |
| 1760 |
}) |
| 1761 |
with self.assertRaises(ExecutionFailed): |
| 1762 |
interp.call(eval, 'spam'), |
| 1763 |
|
| 1764 |
interp.call(exec, f'assert dir() == {names}') |
| 1765 |
|
| 1766 |
# Update the interpreter's __main__. |
| 1767 |
interp.prepare_main(spam=42) |
| 1768 |
expected = names + ['spam'] |
| 1769 |
|
| 1770 |
names = interp.call(dir) |
| 1771 |
self.assertEqual(names, expected) |
| 1772 |
|
| 1773 |
value = interp.call(eval, 'spam') |
| 1774 |
self.assertEqual(value, 42) |
| 1775 |
|
| 1776 |
interp.call(exec, f'assert dir() == {expected}, dir()') |
| 1777 |
|
| 1778 |
def test_globals_from_stateless_func(self): |
| 1779 |
# A stateless func, which doesn't depend on any globals, |
| 1780 |
# doesn't go through pickle, so it runs in __main__. |
| 1781 |
def set_global(name, value): |
| 1782 |
globals()[name] = value |
| 1783 |
|
| 1784 |
def get_global(name): |
| 1785 |
return globals().get(name) |
| 1786 |
|
| 1787 |
interp = interpreters.create() |
| 1788 |
|
| 1789 |
modname = interp.call(get_global, '__name__') |
| 1790 |
self.assertEqual(modname, '__main__') |
| 1791 |
|
| 1792 |
res = interp.call(get_global, 'spam') |
| 1793 |
self.assertIsNone(res) |
| 1794 |
|
| 1795 |
interp.exec('spam = True') |
| 1796 |
res = interp.call(get_global, 'spam') |
| 1797 |
self.assertTrue(res) |
| 1798 |
|
| 1799 |
interp.call(set_global, 'spam', 42) |
| 1800 |
res = interp.call(get_global, 'spam') |
| 1801 |
self.assertEqual(res, 42) |
| 1802 |
|
| 1803 |
interp.exec('assert spam == 42, repr(spam)') |
| 1804 |
|
| 1805 |
def test_call_in_thread(self): |
| 1806 |
interp = interpreters.create() |
| 1807 |
|
| 1808 |
for i, (callable, args, kwargs) in enumerate([ |
| 1809 |
(call_func_noop, (), {}), |
| 1810 |
(call_func_return_shareable, (), {}), |
| 1811 |
(call_func_return_pickleable, (), {}), |
| 1812 |
(Spam.from_values, (), {}), |
| 1813 |
(Spam.from_values, (1, 2, 3), {}), |
| 1814 |
(Spam(101), (), {}), |
| 1815 |
(Spam(10101).run, (), {}), |
| 1816 |
(Spam.noop, (), {}), |
| 1817 |
(call_func_complex, ('ident', 'spam'), {}), |
| 1818 |
(call_func_complex, ('full-ident', 'spam'), {}), |
| 1819 |
(call_func_complex, ('full-ident', 'spam', 'ham'), {'eggs': '!!!'}), |
| 1820 |
(call_func_complex, ('globals',), {}), |
| 1821 |
(call_func_complex, ('interpid',), {}), |
| 1822 |
(call_func_complex, ('custom', 'spam!'), {}), |
| 1823 |
]): |
| 1824 |
with self.subTest(f'success case #{i+1}'): |
| 1825 |
with self.captured_thread_exception() as ctx: |
| 1826 |
t = interp.call_in_thread(callable, *args, **kwargs) |
| 1827 |
t.join() |
| 1828 |
self.assertIsNone(ctx.caught) |
| 1829 |
|
| 1830 |
for i, (callable, args, kwargs) in enumerate([ |
| 1831 |
(get_call_func_closure, (42,), {}), |
| 1832 |
(get_call_func_closure(42), (), {}), |
| 1833 |
]): |
| 1834 |
with self.subTest(f'invalid case #{i+1}'): |
| 1835 |
with self.captured_thread_exception() as ctx: |
| 1836 |
t = interp.call_in_thread(callable, *args, **kwargs) |
| 1837 |
t.join() |
| 1838 |
self.assertIsNotNone(ctx.caught) |
| 1839 |
|
| 1840 |
with self.captured_thread_exception() as ctx: |
| 1841 |
t = interp.call_in_thread(call_func_failure) |
| 1842 |
t.join() |
| 1843 |
self.assertIsNotNone(ctx.caught) |
| 1844 |
|
| 1845 |
|
| 1846 |
class TestIsShareable(TestBase): |
| 1847 |
|
| 1848 |
def test_default_shareables(self): |
| 1849 |
shareables = [ |
| 1850 |
# singletons |
| 1851 |
None, |
| 1852 |
# builtin objects |
| 1853 |
b'spam', |
| 1854 |
'spam', |
| 1855 |
10, |
| 1856 |
-10, |
| 1857 |
True, |
| 1858 |
False, |
| 1859 |
100.0, |
| 1860 |
(), |
| 1861 |
(1, ('spam', 'eggs'), True), |
| 1862 |
] |
| 1863 |
for obj in shareables: |
| 1864 |
with self.subTest(obj): |
| 1865 |
shareable = interpreters.is_shareable(obj) |
| 1866 |
self.assertTrue(shareable) |
| 1867 |
|
| 1868 |
def test_not_shareable(self): |
| 1869 |
class Cheese: |
| 1870 |
def __init__(self, name): |
| 1871 |
self.name = name |
| 1872 |
def __str__(self): |
| 1873 |
return self.name |
| 1874 |
|
| 1875 |
class SubBytes(bytes): |
| 1876 |
"""A subclass of a shareable type.""" |
| 1877 |
|
| 1878 |
not_shareables = [ |
| 1879 |
# singletons |
| 1880 |
NotImplemented, |
| 1881 |
..., |
| 1882 |
# builtin types and objects |
| 1883 |
type, |
| 1884 |
object, |
| 1885 |
object(), |
| 1886 |
Exception(), |
| 1887 |
# user-defined types and objects |
| 1888 |
Cheese, |
| 1889 |
Cheese('Wensleydale'), |
| 1890 |
SubBytes(b'spam'), |
| 1891 |
] |
| 1892 |
for obj in not_shareables: |
| 1893 |
with self.subTest(repr(obj)): |
| 1894 |
self.assertFalse( |
| 1895 |
interpreters.is_shareable(obj)) |
| 1896 |
|
| 1897 |
|
| 1898 |
class LowLevelTests(TestBase): |
| 1899 |
|
| 1900 |
# The behaviors in the low-level module are important in as much |
| 1901 |
# as they are exercised by the high-level module. Therefore the |
| 1902 |
# most important testing happens in the high-level tests. |
| 1903 |
# These low-level tests cover corner cases that are not |
| 1904 |
# encountered by the high-level module, thus they |
| 1905 |
# mostly shouldn't matter as much. |
| 1906 |
|
| 1907 |
def test_new_config(self): |
| 1908 |
# This test overlaps with |
| 1909 |
# test.test_capi.test_misc.InterpreterConfigTests. |
| 1910 |
|
| 1911 |
default = _interpreters.new_config('isolated') |
| 1912 |
with self.subTest('no arg'): |
| 1913 |
config = _interpreters.new_config() |
| 1914 |
self.assert_ns_equal(config, default) |
| 1915 |
self.assertIsNot(config, default) |
| 1916 |
|
| 1917 |
with self.subTest('default'): |
| 1918 |
config1 = _interpreters.new_config('default') |
| 1919 |
self.assert_ns_equal(config1, default) |
| 1920 |
self.assertIsNot(config1, default) |
| 1921 |
|
| 1922 |
config2 = _interpreters.new_config('default') |
| 1923 |
self.assert_ns_equal(config2, config1) |
| 1924 |
self.assertIsNot(config2, config1) |
| 1925 |
|
| 1926 |
for arg in ['', 'default']: |
| 1927 |
with self.subTest(f'default ({arg!r})'): |
| 1928 |
config = _interpreters.new_config(arg) |
| 1929 |
self.assert_ns_equal(config, default) |
| 1930 |
self.assertIsNot(config, default) |
| 1931 |
|
| 1932 |
supported = { |
| 1933 |
'isolated': types.SimpleNamespace( |
| 1934 |
use_main_obmalloc=False, |
| 1935 |
allow_fork=False, |
| 1936 |
allow_exec=False, |
| 1937 |
allow_threads=True, |
| 1938 |
allow_daemon_threads=False, |
| 1939 |
check_multi_interp_extensions=True, |
| 1940 |
gil='own', |
| 1941 |
), |
| 1942 |
'legacy': types.SimpleNamespace( |
| 1943 |
use_main_obmalloc=True, |
| 1944 |
allow_fork=True, |
| 1945 |
allow_exec=True, |
| 1946 |
allow_threads=True, |
| 1947 |
allow_daemon_threads=True, |
| 1948 |
check_multi_interp_extensions=bool(Py_GIL_DISABLED), |
| 1949 |
gil='shared', |
| 1950 |
), |
| 1951 |
'empty': types.SimpleNamespace( |
| 1952 |
use_main_obmalloc=False, |
| 1953 |
allow_fork=False, |
| 1954 |
allow_exec=False, |
| 1955 |
allow_threads=False, |
| 1956 |
allow_daemon_threads=False, |
| 1957 |
check_multi_interp_extensions=False, |
| 1958 |
gil='default', |
| 1959 |
), |
| 1960 |
} |
| 1961 |
gil_supported = ['default', 'shared', 'own'] |
| 1962 |
|
| 1963 |
for name, vanilla in supported.items(): |
| 1964 |
with self.subTest(f'supported ({name})'): |
| 1965 |
expected = vanilla |
| 1966 |
config1 = _interpreters.new_config(name) |
| 1967 |
self.assert_ns_equal(config1, expected) |
| 1968 |
self.assertIsNot(config1, expected) |
| 1969 |
|
| 1970 |
config2 = _interpreters.new_config(name) |
| 1971 |
self.assert_ns_equal(config2, config1) |
| 1972 |
self.assertIsNot(config2, config1) |
| 1973 |
|
| 1974 |
with self.subTest(f'noop override ({name})'): |
| 1975 |
expected = vanilla |
| 1976 |
overrides = vars(vanilla) |
| 1977 |
config = _interpreters.new_config(name, **overrides) |
| 1978 |
self.assert_ns_equal(config, expected) |
| 1979 |
|
| 1980 |
with self.subTest(f'override all ({name})'): |
| 1981 |
overrides = {k: not v for k, v in vars(vanilla).items()} |
| 1982 |
for gil in gil_supported: |
| 1983 |
if vanilla.gil == gil: |
| 1984 |
continue |
| 1985 |
overrides['gil'] = gil |
| 1986 |
expected = types.SimpleNamespace(**overrides) |
| 1987 |
config = _interpreters.new_config(name, **overrides) |
| 1988 |
self.assert_ns_equal(config, expected) |
| 1989 |
|
| 1990 |
# Override individual fields. |
| 1991 |
for field, old in vars(vanilla).items(): |
| 1992 |
if field == 'gil': |
| 1993 |
values = [v for v in gil_supported if v != old] |
| 1994 |
else: |
| 1995 |
values = [not old] |
| 1996 |
for val in values: |
| 1997 |
with self.subTest(f'{name}.{field} ({old!r} -> {val!r})'): |
| 1998 |
overrides = {field: val} |
| 1999 |
expected = types.SimpleNamespace( |
| 2000 |
**dict(vars(vanilla), **overrides), |
| 2001 |
) |
| 2002 |
config = _interpreters.new_config(name, **overrides) |
| 2003 |
self.assert_ns_equal(config, expected) |
| 2004 |
|
| 2005 |
with self.subTest('extra override'): |
| 2006 |
with self.assertRaises(ValueError): |
| 2007 |
_interpreters.new_config(spam=True) |
| 2008 |
|
| 2009 |
# Bad values for bool fields. |
| 2010 |
for field, value in vars(supported['empty']).items(): |
| 2011 |
if field == 'gil': |
| 2012 |
continue |
| 2013 |
assert isinstance(value, bool) |
| 2014 |
for value in [1, '', 'spam', 1.0, None, object()]: |
| 2015 |
with self.subTest(f'bad override ({field}={value!r})'): |
| 2016 |
with self.assertRaises(TypeError): |
| 2017 |
_interpreters.new_config(**{field: value}) |
| 2018 |
|
| 2019 |
# Bad values for .gil. |
| 2020 |
for value in [True, 1, 1.0, None, object()]: |
| 2021 |
with self.subTest(f'bad override (gil={value!r})'): |
| 2022 |
with self.assertRaises(TypeError): |
| 2023 |
_interpreters.new_config(gil=value) |
| 2024 |
for value in ['', 'spam']: |
| 2025 |
with self.subTest(f'bad override (gil={value!r})'): |
| 2026 |
with self.assertRaises(ValueError): |
| 2027 |
_interpreters.new_config(gil=value) |
| 2028 |
|
| 2029 |
def test_get_main(self): |
| 2030 |
interpid, whence = _interpreters.get_main() |
| 2031 |
self.assertEqual(interpid, 0) |
| 2032 |
self.assertEqual(whence, _interpreters.WHENCE_RUNTIME) |
| 2033 |
self.assertEqual( |
| 2034 |
_interpreters.whence(interpid), |
| 2035 |
_interpreters.WHENCE_RUNTIME) |
| 2036 |
|
| 2037 |
def test_get_current(self): |
| 2038 |
with self.subTest('main'): |
| 2039 |
main, *_ = _interpreters.get_main() |
| 2040 |
interpid, whence = _interpreters.get_current() |
| 2041 |
self.assertEqual(interpid, main) |
| 2042 |
self.assertEqual(whence, _interpreters.WHENCE_RUNTIME) |
| 2043 |
|
| 2044 |
script = f""" |
| 2045 |
import _interpreters |
| 2046 |
interpid, whence = _interpreters.get_current() |
| 2047 |
print((interpid, whence)) |
| 2048 |
""" |
| 2049 |
def parse_stdout(text): |
| 2050 |
interpid, whence = eval(text) |
| 2051 |
return interpid, whence |
| 2052 |
|
| 2053 |
with self.subTest('from _interpreters'): |
| 2054 |
orig = _interpreters.create() |
| 2055 |
text = self.run_and_capture(orig, script) |
| 2056 |
interpid, whence = parse_stdout(text) |
| 2057 |
self.assertEqual(interpid, orig) |
| 2058 |
self.assertEqual(whence, _interpreters.WHENCE_STDLIB) |
| 2059 |
|
| 2060 |
with self.subTest('from C-API'): |
| 2061 |
last = 0 |
| 2062 |
for id, *_ in _interpreters.list_all(): |
| 2063 |
last = max(last, id) |
| 2064 |
expected = last + 1 |
| 2065 |
text = self.run_temp_from_capi(script) |
| 2066 |
interpid, whence = parse_stdout(text) |
| 2067 |
self.assertEqual(interpid, expected) |
| 2068 |
self.assertEqual(whence, _interpreters.WHENCE_CAPI) |
| 2069 |
|
| 2070 |
def test_list_all(self): |
| 2071 |
mainid, *_ = _interpreters.get_main() |
| 2072 |
interpid1 = _interpreters.create() |
| 2073 |
interpid2 = _interpreters.create() |
| 2074 |
interpid3 = _interpreters.create() |
| 2075 |
expected = [ |
| 2076 |
(mainid, _interpreters.WHENCE_RUNTIME), |
| 2077 |
(interpid1, _interpreters.WHENCE_STDLIB), |
| 2078 |
(interpid2, _interpreters.WHENCE_STDLIB), |
| 2079 |
(interpid3, _interpreters.WHENCE_STDLIB), |
| 2080 |
] |
| 2081 |
|
| 2082 |
with self.subTest('main'): |
| 2083 |
res = _interpreters.list_all() |
| 2084 |
self.assertEqual(res, expected) |
| 2085 |
|
| 2086 |
with self.subTest('via interp from _interpreters'): |
| 2087 |
text = self.run_and_capture(interpid2, f""" |
| 2088 |
import _interpreters |
| 2089 |
print( |
| 2090 |
_interpreters.list_all()) |
| 2091 |
""") |
| 2092 |
|
| 2093 |
res = eval(text) |
| 2094 |
self.assertEqual(res, expected) |
| 2095 |
|
| 2096 |
with self.subTest('via interp from C-API'): |
| 2097 |
interpid4 = interpid3 + 1 |
| 2098 |
interpid5 = interpid4 + 1 |
| 2099 |
expected2 = expected + [ |
| 2100 |
(interpid4, _interpreters.WHENCE_CAPI), |
| 2101 |
(interpid5, _interpreters.WHENCE_STDLIB), |
| 2102 |
] |
| 2103 |
expected3 = expected + [ |
| 2104 |
(interpid5, _interpreters.WHENCE_STDLIB), |
| 2105 |
] |
| 2106 |
text = self.run_temp_from_capi(f""" |
| 2107 |
import _interpreters |
| 2108 |
_interpreters.create() |
| 2109 |
print( |
| 2110 |
_interpreters.list_all()) |
| 2111 |
""") |
| 2112 |
res2 = eval(text) |
| 2113 |
res3 = _interpreters.list_all() |
| 2114 |
self.assertEqual(res2, expected2) |
| 2115 |
self.assertEqual(res3, expected3) |
| 2116 |
|
| 2117 |
def test_create(self): |
| 2118 |
isolated = _interpreters.new_config('isolated') |
| 2119 |
legacy = _interpreters.new_config('legacy') |
| 2120 |
default = isolated |
| 2121 |
|
| 2122 |
with self.subTest('no args'): |
| 2123 |
interpid = _interpreters.create() |
| 2124 |
config = _interpreters.get_config(interpid) |
| 2125 |
self.assert_ns_equal(config, default) |
| 2126 |
|
| 2127 |
with self.subTest('config: None'): |
| 2128 |
interpid = _interpreters.create(None) |
| 2129 |
config = _interpreters.get_config(interpid) |
| 2130 |
self.assert_ns_equal(config, default) |
| 2131 |
|
| 2132 |
with self.subTest('config: \'empty\''): |
| 2133 |
with self.assertRaises(InterpreterError): |
| 2134 |
# The "empty" config isn't viable on its own. |
| 2135 |
_interpreters.create('empty') |
| 2136 |
|
| 2137 |
for arg, expected in { |
| 2138 |
'': default, |
| 2139 |
'default': default, |
| 2140 |
'isolated': isolated, |
| 2141 |
'legacy': legacy, |
| 2142 |
}.items(): |
| 2143 |
with self.subTest(f'str arg: {arg!r}'): |
| 2144 |
interpid = _interpreters.create(arg) |
| 2145 |
config = _interpreters.get_config(interpid) |
| 2146 |
self.assert_ns_equal(config, expected) |
| 2147 |
|
| 2148 |
with self.subTest('custom'): |
| 2149 |
orig = _interpreters.new_config('empty') |
| 2150 |
orig.use_main_obmalloc = True |
| 2151 |
orig.check_multi_interp_extensions = bool(Py_GIL_DISABLED) |
| 2152 |
orig.gil = 'shared' |
| 2153 |
interpid = _interpreters.create(orig) |
| 2154 |
config = _interpreters.get_config(interpid) |
| 2155 |
self.assert_ns_equal(config, orig) |
| 2156 |
|
| 2157 |
with self.subTest('missing fields'): |
| 2158 |
orig = _interpreters.new_config() |
| 2159 |
del orig.gil |
| 2160 |
with self.assertRaises(ValueError): |
| 2161 |
_interpreters.create(orig) |
| 2162 |
|
| 2163 |
with self.subTest('extra fields'): |
| 2164 |
orig = _interpreters.new_config() |
| 2165 |
orig.spam = True |
| 2166 |
with self.assertRaises(ValueError): |
| 2167 |
_interpreters.create(orig) |
| 2168 |
|
| 2169 |
with self.subTest('whence'): |
| 2170 |
interpid = _interpreters.create() |
| 2171 |
self.assertEqual( |
| 2172 |
_interpreters.whence(interpid), |
| 2173 |
_interpreters.WHENCE_STDLIB) |
| 2174 |
|
| 2175 |
@requires_test_modules |
| 2176 |
def test_destroy(self): |
| 2177 |
with self.subTest('from _interpreters'): |
| 2178 |
interpid = _interpreters.create() |
| 2179 |
before = [id for id, *_ in _interpreters.list_all()] |
| 2180 |
_interpreters.destroy(interpid) |
| 2181 |
after = [id for id, *_ in _interpreters.list_all()] |
| 2182 |
|
| 2183 |
self.assertIn(interpid, before) |
| 2184 |
self.assertNotIn(interpid, after) |
| 2185 |
self.assertFalse( |
| 2186 |
self.interp_exists(interpid)) |
| 2187 |
|
| 2188 |
with self.subTest('main'): |
| 2189 |
interpid, *_ = _interpreters.get_main() |
| 2190 |
with self.assertRaises(InterpreterError): |
| 2191 |
# It is the current interpreter. |
| 2192 |
_interpreters.destroy(interpid) |
| 2193 |
|
| 2194 |
with self.subTest('from C-API'): |
| 2195 |
interpid = _testinternalcapi.create_interpreter() |
| 2196 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 2197 |
_interpreters.destroy(interpid, restrict=True) |
| 2198 |
self.assertTrue( |
| 2199 |
self.interp_exists(interpid)) |
| 2200 |
_interpreters.destroy(interpid) |
| 2201 |
self.assertFalse( |
| 2202 |
self.interp_exists(interpid)) |
| 2203 |
|
| 2204 |
with self.subTest('basic C-API'): |
| 2205 |
interpid = _testinternalcapi.create_interpreter() |
| 2206 |
self.assertTrue( |
| 2207 |
self.interp_exists(interpid)) |
| 2208 |
_testinternalcapi.destroy_interpreter(interpid, basic=True) |
| 2209 |
self.assertFalse( |
| 2210 |
self.interp_exists(interpid)) |
| 2211 |
|
| 2212 |
def test_get_config(self): |
| 2213 |
# This test overlaps with |
| 2214 |
# test.test_capi.test_misc.InterpreterConfigTests. |
| 2215 |
|
| 2216 |
with self.subTest('main'): |
| 2217 |
expected = _interpreters.new_config('legacy') |
| 2218 |
expected.gil = 'own' |
| 2219 |
if Py_GIL_DISABLED: |
| 2220 |
expected.check_multi_interp_extensions = False |
| 2221 |
interpid, *_ = _interpreters.get_main() |
| 2222 |
config = _interpreters.get_config(interpid) |
| 2223 |
self.assert_ns_equal(config, expected) |
| 2224 |
|
| 2225 |
with self.subTest('isolated'): |
| 2226 |
expected = _interpreters.new_config('isolated') |
| 2227 |
interpid = _interpreters.create('isolated') |
| 2228 |
config = _interpreters.get_config(interpid) |
| 2229 |
self.assert_ns_equal(config, expected) |
| 2230 |
|
| 2231 |
with self.subTest('legacy'): |
| 2232 |
expected = _interpreters.new_config('legacy') |
| 2233 |
interpid = _interpreters.create('legacy') |
| 2234 |
config = _interpreters.get_config(interpid) |
| 2235 |
self.assert_ns_equal(config, expected) |
| 2236 |
|
| 2237 |
with self.subTest('from C-API'): |
| 2238 |
orig = _interpreters.new_config('isolated') |
| 2239 |
with self.interpreter_from_capi(orig) as interpid: |
| 2240 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 2241 |
_interpreters.get_config(interpid, restrict=True) |
| 2242 |
config = _interpreters.get_config(interpid) |
| 2243 |
self.assert_ns_equal(config, orig) |
| 2244 |
|
| 2245 |
@requires_test_modules |
| 2246 |
def test_whence(self): |
| 2247 |
with self.subTest('main'): |
| 2248 |
interpid, *_ = _interpreters.get_main() |
| 2249 |
whence = _interpreters.whence(interpid) |
| 2250 |
self.assertEqual(whence, _interpreters.WHENCE_RUNTIME) |
| 2251 |
|
| 2252 |
with self.subTest('stdlib'): |
| 2253 |
interpid = _interpreters.create() |
| 2254 |
whence = _interpreters.whence(interpid) |
| 2255 |
self.assertEqual(whence, _interpreters.WHENCE_STDLIB) |
| 2256 |
|
| 2257 |
for orig, name in { |
| 2258 |
_interpreters.WHENCE_UNKNOWN: 'not ready', |
| 2259 |
_interpreters.WHENCE_LEGACY_CAPI: 'legacy C-API', |
| 2260 |
_interpreters.WHENCE_CAPI: 'C-API', |
| 2261 |
_interpreters.WHENCE_XI: 'cross-interpreter C-API', |
| 2262 |
}.items(): |
| 2263 |
with self.subTest(f'from C-API ({orig}: {name})'): |
| 2264 |
with self.interpreter_from_capi(whence=orig) as interpid: |
| 2265 |
whence = _interpreters.whence(interpid) |
| 2266 |
self.assertEqual(whence, orig) |
| 2267 |
|
| 2268 |
with self.subTest('from C-API, running'): |
| 2269 |
text = self.run_temp_from_capi(dedent(f""" |
| 2270 |
import _interpreters |
| 2271 |
interpid, *_ = _interpreters.get_current() |
| 2272 |
print(_interpreters.whence(interpid)) |
| 2273 |
"""), |
| 2274 |
config=True) |
| 2275 |
whence = eval(text) |
| 2276 |
self.assertEqual(whence, _interpreters.WHENCE_CAPI) |
| 2277 |
|
| 2278 |
with self.subTest('from legacy C-API, running'): |
| 2279 |
... |
| 2280 |
text = self.run_temp_from_capi(dedent(f""" |
| 2281 |
import _interpreters |
| 2282 |
interpid, *_ = _interpreters.get_current() |
| 2283 |
print(_interpreters.whence(interpid)) |
| 2284 |
"""), |
| 2285 |
config=False) |
| 2286 |
whence = eval(text) |
| 2287 |
self.assertEqual(whence, _interpreters.WHENCE_LEGACY_CAPI) |
| 2288 |
|
| 2289 |
def test_contextvars_missing(self): |
| 2290 |
script = f""" |
| 2291 |
import contextvars |
| 2292 |
print(getattr(contextvars.Token, "MISSING", "'doesn't exist'")) |
| 2293 |
""" |
| 2294 |
|
| 2295 |
orig = _interpreters.create() |
| 2296 |
text = self.run_and_capture(orig, script) |
| 2297 |
self.assertEqual(text.strip(), "<Token.MISSING>") |
| 2298 |
|
| 2299 |
def test_is_running(self): |
| 2300 |
def check(interpid, expected): |
| 2301 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 2302 |
_interpreters.is_running(interpid, restrict=True) |
| 2303 |
running = _interpreters.is_running(interpid) |
| 2304 |
self.assertIs(running, expected) |
| 2305 |
|
| 2306 |
with self.subTest('from _interpreters (running)'): |
| 2307 |
interpid = _interpreters.create() |
| 2308 |
with self.running(interpid): |
| 2309 |
running = _interpreters.is_running(interpid) |
| 2310 |
self.assertTrue(running) |
| 2311 |
|
| 2312 |
with self.subTest('from _interpreters (not running)'): |
| 2313 |
interpid = _interpreters.create() |
| 2314 |
running = _interpreters.is_running(interpid) |
| 2315 |
self.assertFalse(running) |
| 2316 |
|
| 2317 |
with self.subTest('main'): |
| 2318 |
interpid, *_ = _interpreters.get_main() |
| 2319 |
check(interpid, True) |
| 2320 |
|
| 2321 |
with self.subTest('from C-API (running __main__)'): |
| 2322 |
with self.interpreter_from_capi() as interpid: |
| 2323 |
with self.running_from_capi(interpid, main=True): |
| 2324 |
check(interpid, True) |
| 2325 |
|
| 2326 |
with self.subTest('from C-API (running, but not __main__)'): |
| 2327 |
with self.interpreter_from_capi() as interpid: |
| 2328 |
with self.running_from_capi(interpid, main=False): |
| 2329 |
check(interpid, False) |
| 2330 |
|
| 2331 |
with self.subTest('from C-API (not running)'): |
| 2332 |
with self.interpreter_from_capi() as interpid: |
| 2333 |
check(interpid, False) |
| 2334 |
|
| 2335 |
def test_exec(self): |
| 2336 |
with self.subTest('run script'): |
| 2337 |
interpid = _interpreters.create() |
| 2338 |
script, results = _captured_script('print("it worked!", end="")') |
| 2339 |
with results: |
| 2340 |
exc = _interpreters.exec(interpid, script) |
| 2341 |
results = results.final() |
| 2342 |
results.raise_if_failed() |
| 2343 |
out = results.stdout |
| 2344 |
self.assertEqual(out, 'it worked!') |
| 2345 |
|
| 2346 |
with self.subTest('uncaught exception'): |
| 2347 |
interpid = _interpreters.create() |
| 2348 |
script, results = _captured_script(""" |
| 2349 |
raise Exception('uh-oh!') |
| 2350 |
print("it worked!", end="") |
| 2351 |
""") |
| 2352 |
with results: |
| 2353 |
exc = _interpreters.exec(interpid, script) |
| 2354 |
out = results.stdout() |
| 2355 |
expected = build_excinfo( |
| 2356 |
Exception, 'uh-oh!', |
| 2357 |
# We check these in other tests. |
| 2358 |
formatted=exc.formatted, |
| 2359 |
errdisplay=exc.errdisplay, |
| 2360 |
) |
| 2361 |
self.assertEqual(out, '') |
| 2362 |
self.assert_ns_equal(exc, expected) |
| 2363 |
|
| 2364 |
with self.subTest('from C-API'): |
| 2365 |
with self.interpreter_from_capi() as interpid: |
| 2366 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 2367 |
_interpreters.exec(interpid, 'raise Exception("it worked!")', |
| 2368 |
restrict=True) |
| 2369 |
exc = _interpreters.exec(interpid, 'raise Exception("it worked!")') |
| 2370 |
self.assertIsNot(exc, None) |
| 2371 |
self.assertEqual(exc.msg, 'it worked!') |
| 2372 |
|
| 2373 |
def test_call(self): |
| 2374 |
interpid = _interpreters.create() |
| 2375 |
|
| 2376 |
# Here we focus on basic args and return values. |
| 2377 |
# See TestInterpreterCall for full operational coverage, |
| 2378 |
# including supported callables. |
| 2379 |
|
| 2380 |
with self.subTest('no args, return None'): |
| 2381 |
func = defs.spam_minimal |
| 2382 |
res, exc = _interpreters.call(interpid, func) |
| 2383 |
self.assertIsNone(exc) |
| 2384 |
self.assertIsNone(res) |
| 2385 |
|
| 2386 |
with self.subTest('empty args, return None'): |
| 2387 |
func = defs.spam_minimal |
| 2388 |
res, exc = _interpreters.call(interpid, func, (), {}) |
| 2389 |
self.assertIsNone(exc) |
| 2390 |
self.assertIsNone(res) |
| 2391 |
|
| 2392 |
with self.subTest('no args, return non-None'): |
| 2393 |
func = defs.script_with_return |
| 2394 |
res, exc = _interpreters.call(interpid, func) |
| 2395 |
self.assertIsNone(exc) |
| 2396 |
self.assertIs(res, True) |
| 2397 |
|
| 2398 |
with self.subTest('full args, return non-None'): |
| 2399 |
expected = (1, 2, 3, 4, 5, 6, (7, 8), {'g': 9, 'h': 0}) |
| 2400 |
func = defs.spam_full_args |
| 2401 |
args = (1, 2, 3, 4, 7, 8) |
| 2402 |
kwargs = dict(e=5, f=6, g=9, h=0) |
| 2403 |
res, exc = _interpreters.call(interpid, func, args, kwargs) |
| 2404 |
self.assertIsNone(exc) |
| 2405 |
self.assertEqual(res, expected) |
| 2406 |
|
| 2407 |
with self.subTest('uncaught exception'): |
| 2408 |
func = defs.spam_raises |
| 2409 |
res, exc = _interpreters.call(interpid, func) |
| 2410 |
expected = build_excinfo( |
| 2411 |
Exception, 'spam!', |
| 2412 |
# We check these in other tests. |
| 2413 |
formatted=exc.formatted, |
| 2414 |
errdisplay=exc.errdisplay, |
| 2415 |
) |
| 2416 |
self.assertIsNone(res) |
| 2417 |
self.assertEqual(exc, expected) |
| 2418 |
|
| 2419 |
@requires_test_modules |
| 2420 |
def test_set___main___attrs(self): |
| 2421 |
with self.subTest('from _interpreters'): |
| 2422 |
interpid = _interpreters.create() |
| 2423 |
before1 = _interpreters.exec(interpid, 'assert spam == \'eggs\'') |
| 2424 |
before2 = _interpreters.exec(interpid, 'assert ham == 42') |
| 2425 |
self.assertEqual(before1.type.__name__, 'NameError') |
| 2426 |
self.assertEqual(before2.type.__name__, 'NameError') |
| 2427 |
|
| 2428 |
_interpreters.set___main___attrs(interpid, dict( |
| 2429 |
spam='eggs', |
| 2430 |
ham=42, |
| 2431 |
)) |
| 2432 |
after1 = _interpreters.exec(interpid, 'assert spam == \'eggs\'') |
| 2433 |
after2 = _interpreters.exec(interpid, 'assert ham == 42') |
| 2434 |
after3 = _interpreters.exec(interpid, 'assert spam == 42') |
| 2435 |
self.assertIs(after1, None) |
| 2436 |
self.assertIs(after2, None) |
| 2437 |
self.assertEqual(after3.type.__name__, 'AssertionError') |
| 2438 |
|
| 2439 |
with self.assertRaises(ValueError): |
| 2440 |
# GH-127165: Embedded NULL characters broke the lookup |
| 2441 |
_interpreters.set___main___attrs(interpid, {"\x00": 1}) |
| 2442 |
|
| 2443 |
with self.subTest('from C-API'): |
| 2444 |
with self.interpreter_from_capi() as interpid: |
| 2445 |
with self.assertRaisesRegex(InterpreterError, 'unrecognized'): |
| 2446 |
_interpreters.set___main___attrs(interpid, {'spam': True}, |
| 2447 |
restrict=True) |
| 2448 |
_interpreters.set___main___attrs(interpid, {'spam': True}) |
| 2449 |
rc = _testinternalcapi.exec_interpreter( |
| 2450 |
interpid, |
| 2451 |
'assert spam is True', |
| 2452 |
) |
| 2453 |
self.assertEqual(rc, 0) |
| 2454 |
|
| 2455 |
|
| 2456 |
if __name__ == '__main__': |
| 2457 |
# Test needs to be a package, so we can do relative imports. |
| 2458 |
unittest.main() |