| 1 |
import contextlib |
| 2 |
import os |
| 3 |
import pickle |
| 4 |
from textwrap import dedent |
| 5 |
import threading |
| 6 |
import unittest |
| 7 |
|
| 8 |
from test import support |
| 9 |
from test.support import import_helper |
| 10 |
from test.support import os_helper |
| 11 |
from test.support import script_helper |
| 12 |
|
| 13 |
|
| 14 |
_interpreters = import_helper.import_module('_interpreters') |
| 15 |
from _interpreters import InterpreterNotFoundError |
| 16 |
|
| 17 |
|
| 18 |
################################## |
| 19 |
# helpers |
| 20 |
|
| 21 |
def _captured_script(script): |
| 22 |
r, w = os.pipe() |
| 23 |
indented = script.replace('\n', '\n ') |
| 24 |
wrapped = dedent(f""" |
| 25 |
import contextlib |
| 26 |
with open({w}, 'w', encoding="utf-8") as spipe: |
| 27 |
with contextlib.redirect_stdout(spipe): |
| 28 |
{indented} |
| 29 |
""") |
| 30 |
return wrapped, open(r, encoding="utf-8") |
| 31 |
|
| 32 |
|
| 33 |
def _run_output(interp, request): |
| 34 |
script, rpipe = _captured_script(request) |
| 35 |
with rpipe: |
| 36 |
_interpreters.run_string(interp, script) |
| 37 |
return rpipe.read() |
| 38 |
|
| 39 |
|
| 40 |
def _wait_for_interp_to_run(interp, timeout=None): |
| 41 |
# bpo-37224: Running this test file in multiprocesses will fail randomly. |
| 42 |
# The failure reason is that the thread can't acquire the cpu to |
| 43 |
# run subinterpreter earlier than the main thread in multiprocess. |
| 44 |
if timeout is None: |
| 45 |
timeout = support.SHORT_TIMEOUT |
| 46 |
for _ in support.sleeping_retry(timeout, error=False): |
| 47 |
if _interpreters.is_running(interp): |
| 48 |
break |
| 49 |
else: |
| 50 |
raise RuntimeError('interp is not running') |
| 51 |
|
| 52 |
|
| 53 |
@contextlib.contextmanager |
| 54 |
def _running(interp): |
| 55 |
r, w = os.pipe() |
| 56 |
def run(): |
| 57 |
_interpreters.run_string(interp, dedent(f""" |
| 58 |
# wait for "signal" |
| 59 |
with open({r}, encoding="utf-8") as rpipe: |
| 60 |
rpipe.read() |
| 61 |
""")) |
| 62 |
|
| 63 |
t = threading.Thread(target=run) |
| 64 |
t.start() |
| 65 |
_wait_for_interp_to_run(interp) |
| 66 |
|
| 67 |
yield |
| 68 |
|
| 69 |
with open(w, 'w', encoding="utf-8") as spipe: |
| 70 |
spipe.write('done') |
| 71 |
t.join() |
| 72 |
|
| 73 |
|
| 74 |
def clean_up_interpreters(): |
| 75 |
for id, *_ in _interpreters.list_all(): |
| 76 |
if id == 0: # main |
| 77 |
continue |
| 78 |
try: |
| 79 |
_interpreters.destroy(id) |
| 80 |
except _interpreters.InterpreterError: |
| 81 |
pass # already destroyed |
| 82 |
|
| 83 |
|
| 84 |
class TestBase(unittest.TestCase): |
| 85 |
|
| 86 |
def tearDown(self): |
| 87 |
clean_up_interpreters() |
| 88 |
|
| 89 |
|
| 90 |
################################## |
| 91 |
# misc. tests |
| 92 |
|
| 93 |
class IsShareableTests(unittest.TestCase): |
| 94 |
|
| 95 |
def test_default_shareables(self): |
| 96 |
shareables = [ |
| 97 |
# singletons |
| 98 |
None, |
| 99 |
# builtin objects |
| 100 |
b'spam', |
| 101 |
'spam', |
| 102 |
10, |
| 103 |
-10, |
| 104 |
True, |
| 105 |
False, |
| 106 |
100.0, |
| 107 |
(1, ('spam', 'eggs')), |
| 108 |
] |
| 109 |
for obj in shareables: |
| 110 |
with self.subTest(obj): |
| 111 |
self.assertTrue( |
| 112 |
_interpreters.is_shareable(obj)) |
| 113 |
|
| 114 |
def test_not_shareable(self): |
| 115 |
class Cheese: |
| 116 |
def __init__(self, name): |
| 117 |
self.name = name |
| 118 |
def __str__(self): |
| 119 |
return self.name |
| 120 |
|
| 121 |
class SubBytes(bytes): |
| 122 |
"""A subclass of a shareable type.""" |
| 123 |
|
| 124 |
not_shareables = [ |
| 125 |
# singletons |
| 126 |
NotImplemented, |
| 127 |
..., |
| 128 |
# builtin types and objects |
| 129 |
type, |
| 130 |
object, |
| 131 |
object(), |
| 132 |
Exception(), |
| 133 |
# user-defined types and objects |
| 134 |
Cheese, |
| 135 |
Cheese('Wensleydale'), |
| 136 |
SubBytes(b'spam'), |
| 137 |
] |
| 138 |
for obj in not_shareables: |
| 139 |
with self.subTest(repr(obj)): |
| 140 |
self.assertFalse( |
| 141 |
_interpreters.is_shareable(obj)) |
| 142 |
|
| 143 |
|
| 144 |
class ModuleTests(TestBase): |
| 145 |
|
| 146 |
def test_import_in_interpreter(self): |
| 147 |
_run_output( |
| 148 |
_interpreters.create(), |
| 149 |
'import _interpreters', |
| 150 |
) |
| 151 |
|
| 152 |
|
| 153 |
################################## |
| 154 |
# interpreter tests |
| 155 |
|
| 156 |
class ListAllTests(TestBase): |
| 157 |
|
| 158 |
def test_initial(self): |
| 159 |
main, *_ = _interpreters.get_main() |
| 160 |
ids = [id for id, *_ in _interpreters.list_all()] |
| 161 |
self.assertEqual(ids, [main]) |
| 162 |
|
| 163 |
def test_after_creating(self): |
| 164 |
main, *_ = _interpreters.get_main() |
| 165 |
first = _interpreters.create() |
| 166 |
second = _interpreters.create() |
| 167 |
ids = [id for id, *_ in _interpreters.list_all()] |
| 168 |
self.assertEqual(ids, [main, first, second]) |
| 169 |
|
| 170 |
def test_after_destroying(self): |
| 171 |
main, *_ = _interpreters.get_main() |
| 172 |
first = _interpreters.create() |
| 173 |
second = _interpreters.create() |
| 174 |
_interpreters.destroy(first) |
| 175 |
ids = [id for id, *_ in _interpreters.list_all()] |
| 176 |
self.assertEqual(ids, [main, second]) |
| 177 |
|
| 178 |
|
| 179 |
class GetCurrentTests(TestBase): |
| 180 |
|
| 181 |
def test_main(self): |
| 182 |
main, *_ = _interpreters.get_main() |
| 183 |
cur, *_ = _interpreters.get_current() |
| 184 |
self.assertEqual(cur, main) |
| 185 |
self.assertIsInstance(cur, int) |
| 186 |
|
| 187 |
def test_subinterpreter(self): |
| 188 |
main, *_ = _interpreters.get_main() |
| 189 |
interp = _interpreters.create() |
| 190 |
out = _run_output(interp, dedent(""" |
| 191 |
import _interpreters |
| 192 |
cur, *_ = _interpreters.get_current() |
| 193 |
print(cur) |
| 194 |
assert isinstance(cur, int) |
| 195 |
""")) |
| 196 |
cur = int(out.strip()) |
| 197 |
_, expected = [id for id, *_ in _interpreters.list_all()] |
| 198 |
self.assertEqual(cur, expected) |
| 199 |
self.assertNotEqual(cur, main) |
| 200 |
|
| 201 |
|
| 202 |
class GetMainTests(TestBase): |
| 203 |
|
| 204 |
def test_from_main(self): |
| 205 |
[expected] = [id for id, *_ in _interpreters.list_all()] |
| 206 |
main, *_ = _interpreters.get_main() |
| 207 |
self.assertEqual(main, expected) |
| 208 |
self.assertIsInstance(main, int) |
| 209 |
|
| 210 |
def test_from_subinterpreter(self): |
| 211 |
[expected] = [id for id, *_ in _interpreters.list_all()] |
| 212 |
interp = _interpreters.create() |
| 213 |
out = _run_output(interp, dedent(""" |
| 214 |
import _interpreters |
| 215 |
main, *_ = _interpreters.get_main() |
| 216 |
print(main) |
| 217 |
assert isinstance(main, int) |
| 218 |
""")) |
| 219 |
main = int(out.strip()) |
| 220 |
self.assertEqual(main, expected) |
| 221 |
|
| 222 |
|
| 223 |
class IsRunningTests(TestBase): |
| 224 |
|
| 225 |
def test_main(self): |
| 226 |
main, *_ = _interpreters.get_main() |
| 227 |
self.assertTrue(_interpreters.is_running(main)) |
| 228 |
|
| 229 |
@unittest.skip('Fails on FreeBSD') |
| 230 |
def test_subinterpreter(self): |
| 231 |
interp = _interpreters.create() |
| 232 |
self.assertFalse(_interpreters.is_running(interp)) |
| 233 |
|
| 234 |
with _running(interp): |
| 235 |
self.assertTrue(_interpreters.is_running(interp)) |
| 236 |
self.assertFalse(_interpreters.is_running(interp)) |
| 237 |
|
| 238 |
def test_from_subinterpreter(self): |
| 239 |
interp = _interpreters.create() |
| 240 |
out = _run_output(interp, dedent(f""" |
| 241 |
import _interpreters |
| 242 |
if _interpreters.is_running({interp}): |
| 243 |
print(True) |
| 244 |
else: |
| 245 |
print(False) |
| 246 |
""")) |
| 247 |
self.assertEqual(out.strip(), 'True') |
| 248 |
|
| 249 |
def test_already_destroyed(self): |
| 250 |
interp = _interpreters.create() |
| 251 |
_interpreters.destroy(interp) |
| 252 |
with self.assertRaises(InterpreterNotFoundError): |
| 253 |
_interpreters.is_running(interp) |
| 254 |
|
| 255 |
def test_does_not_exist(self): |
| 256 |
with self.assertRaises(InterpreterNotFoundError): |
| 257 |
_interpreters.is_running(1_000_000) |
| 258 |
|
| 259 |
def test_bad_id(self): |
| 260 |
with self.assertRaises(ValueError): |
| 261 |
_interpreters.is_running(-1) |
| 262 |
|
| 263 |
|
| 264 |
class CreateTests(TestBase): |
| 265 |
|
| 266 |
def test_in_main(self): |
| 267 |
id = _interpreters.create() |
| 268 |
self.assertIsInstance(id, int) |
| 269 |
|
| 270 |
after = [id for id, *_ in _interpreters.list_all()] |
| 271 |
self.assertIn(id, after) |
| 272 |
|
| 273 |
@unittest.skip('enable this test when working on pystate.c') |
| 274 |
def test_unique_id(self): |
| 275 |
seen = set() |
| 276 |
for _ in range(100): |
| 277 |
id = _interpreters.create() |
| 278 |
_interpreters.destroy(id) |
| 279 |
seen.add(id) |
| 280 |
|
| 281 |
self.assertEqual(len(seen), 100) |
| 282 |
|
| 283 |
@support.skip_if_sanitizer('gh-129824: race on tp_flags', thread=True) |
| 284 |
def test_in_thread(self): |
| 285 |
lock = threading.Lock() |
| 286 |
id = None |
| 287 |
def f(): |
| 288 |
nonlocal id |
| 289 |
id = _interpreters.create() |
| 290 |
lock.acquire() |
| 291 |
lock.release() |
| 292 |
|
| 293 |
t = threading.Thread(target=f) |
| 294 |
with lock: |
| 295 |
t.start() |
| 296 |
t.join() |
| 297 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 298 |
self.assertIn(id, after) |
| 299 |
|
| 300 |
def test_in_subinterpreter(self): |
| 301 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 302 |
id1 = _interpreters.create() |
| 303 |
out = _run_output(id1, dedent(""" |
| 304 |
import _interpreters |
| 305 |
id = _interpreters.create() |
| 306 |
print(id) |
| 307 |
assert isinstance(id, int) |
| 308 |
""")) |
| 309 |
id2 = int(out.strip()) |
| 310 |
|
| 311 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 312 |
self.assertEqual(after, {main, id1, id2}) |
| 313 |
|
| 314 |
def test_in_threaded_subinterpreter(self): |
| 315 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 316 |
id1 = _interpreters.create() |
| 317 |
id2 = None |
| 318 |
def f(): |
| 319 |
nonlocal id2 |
| 320 |
out = _run_output(id1, dedent(""" |
| 321 |
import _interpreters |
| 322 |
id = _interpreters.create() |
| 323 |
print(id) |
| 324 |
""")) |
| 325 |
id2 = int(out.strip()) |
| 326 |
|
| 327 |
t = threading.Thread(target=f) |
| 328 |
t.start() |
| 329 |
t.join() |
| 330 |
|
| 331 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 332 |
self.assertEqual(after, {main, id1, id2}) |
| 333 |
|
| 334 |
def test_after_destroy_all(self): |
| 335 |
before = set(id for id, *_ in _interpreters.list_all()) |
| 336 |
# Create 3 subinterpreters. |
| 337 |
ids = [] |
| 338 |
for _ in range(3): |
| 339 |
id = _interpreters.create() |
| 340 |
ids.append(id) |
| 341 |
# Now destroy them. |
| 342 |
for id in ids: |
| 343 |
_interpreters.destroy(id) |
| 344 |
# Finally, create another. |
| 345 |
id = _interpreters.create() |
| 346 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 347 |
self.assertEqual(after, before | {id}) |
| 348 |
|
| 349 |
def test_after_destroy_some(self): |
| 350 |
before = set(id for id, *_ in _interpreters.list_all()) |
| 351 |
# Create 3 subinterpreters. |
| 352 |
id1 = _interpreters.create() |
| 353 |
id2 = _interpreters.create() |
| 354 |
id3 = _interpreters.create() |
| 355 |
# Now destroy 2 of them. |
| 356 |
_interpreters.destroy(id1) |
| 357 |
_interpreters.destroy(id3) |
| 358 |
# Finally, create another. |
| 359 |
id = _interpreters.create() |
| 360 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 361 |
self.assertEqual(after, before | {id, id2}) |
| 362 |
|
| 363 |
|
| 364 |
class DestroyTests(TestBase): |
| 365 |
|
| 366 |
def test_one(self): |
| 367 |
id1 = _interpreters.create() |
| 368 |
id2 = _interpreters.create() |
| 369 |
id3 = _interpreters.create() |
| 370 |
before = set(id for id, *_ in _interpreters.list_all()) |
| 371 |
self.assertIn(id2, before) |
| 372 |
|
| 373 |
_interpreters.destroy(id2) |
| 374 |
|
| 375 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 376 |
self.assertNotIn(id2, after) |
| 377 |
self.assertIn(id1, after) |
| 378 |
self.assertIn(id3, after) |
| 379 |
|
| 380 |
def test_all(self): |
| 381 |
initial = set(id for id, *_ in _interpreters.list_all()) |
| 382 |
ids = set() |
| 383 |
for _ in range(3): |
| 384 |
id = _interpreters.create() |
| 385 |
ids.add(id) |
| 386 |
before = set(id for id, *_ in _interpreters.list_all()) |
| 387 |
self.assertEqual(before, initial | ids) |
| 388 |
for id in ids: |
| 389 |
_interpreters.destroy(id) |
| 390 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 391 |
self.assertEqual(after, initial) |
| 392 |
|
| 393 |
def test_main(self): |
| 394 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 395 |
with self.assertRaises(_interpreters.InterpreterError): |
| 396 |
_interpreters.destroy(main) |
| 397 |
|
| 398 |
def f(): |
| 399 |
with self.assertRaises(_interpreters.InterpreterError): |
| 400 |
_interpreters.destroy(main) |
| 401 |
|
| 402 |
t = threading.Thread(target=f) |
| 403 |
t.start() |
| 404 |
t.join() |
| 405 |
|
| 406 |
def test_already_destroyed(self): |
| 407 |
id = _interpreters.create() |
| 408 |
_interpreters.destroy(id) |
| 409 |
with self.assertRaises(InterpreterNotFoundError): |
| 410 |
_interpreters.destroy(id) |
| 411 |
|
| 412 |
def test_does_not_exist(self): |
| 413 |
with self.assertRaises(InterpreterNotFoundError): |
| 414 |
_interpreters.destroy(1_000_000) |
| 415 |
|
| 416 |
def test_bad_id(self): |
| 417 |
with self.assertRaises(ValueError): |
| 418 |
_interpreters.destroy(-1) |
| 419 |
|
| 420 |
def test_from_current(self): |
| 421 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 422 |
id = _interpreters.create() |
| 423 |
script = dedent(f""" |
| 424 |
import _interpreters |
| 425 |
try: |
| 426 |
_interpreters.destroy({id}) |
| 427 |
except _interpreters.InterpreterError: |
| 428 |
pass |
| 429 |
""") |
| 430 |
|
| 431 |
_interpreters.run_string(id, script) |
| 432 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 433 |
self.assertEqual(after, {main, id}) |
| 434 |
|
| 435 |
def test_from_sibling(self): |
| 436 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 437 |
id1 = _interpreters.create() |
| 438 |
id2 = _interpreters.create() |
| 439 |
script = dedent(f""" |
| 440 |
import _interpreters |
| 441 |
_interpreters.destroy({id2}) |
| 442 |
""") |
| 443 |
_interpreters.run_string(id1, script) |
| 444 |
|
| 445 |
after = set(id for id, *_ in _interpreters.list_all()) |
| 446 |
self.assertEqual(after, {main, id1}) |
| 447 |
|
| 448 |
def test_from_other_thread(self): |
| 449 |
id = _interpreters.create() |
| 450 |
def f(): |
| 451 |
_interpreters.destroy(id) |
| 452 |
|
| 453 |
t = threading.Thread(target=f) |
| 454 |
t.start() |
| 455 |
t.join() |
| 456 |
|
| 457 |
def test_still_running(self): |
| 458 |
main, = [id for id, *_ in _interpreters.list_all()] |
| 459 |
interp = _interpreters.create() |
| 460 |
with _running(interp): |
| 461 |
self.assertTrue(_interpreters.is_running(interp), |
| 462 |
msg=f"Interp {interp} should be running before destruction.") |
| 463 |
|
| 464 |
with self.assertRaises(_interpreters.InterpreterError, |
| 465 |
msg=f"Should not be able to destroy interp {interp} while it's still running."): |
| 466 |
_interpreters.destroy(interp) |
| 467 |
self.assertTrue(_interpreters.is_running(interp)) |
| 468 |
|
| 469 |
|
| 470 |
class CommonTests(TestBase): |
| 471 |
def setUp(self): |
| 472 |
super().setUp() |
| 473 |
self.id = _interpreters.create() |
| 474 |
|
| 475 |
def test_signatures(self): |
| 476 |
# See https://github.com/python/cpython/issues/126654 |
| 477 |
msg = r'_interpreters.exec\(\) argument 3 must be dict, not int' |
| 478 |
with self.assertRaisesRegex(TypeError, msg): |
| 479 |
_interpreters.exec(self.id, 'a', 1) |
| 480 |
with self.assertRaisesRegex(TypeError, msg): |
| 481 |
_interpreters.exec(self.id, 'a', shared=1) |
| 482 |
msg = r'_interpreters.run_string\(\) argument 3 must be dict, not int' |
| 483 |
with self.assertRaisesRegex(TypeError, msg): |
| 484 |
_interpreters.run_string(self.id, 'a', shared=1) |
| 485 |
msg = r'_interpreters.run_func\(\) argument 3 must be dict, not int' |
| 486 |
with self.assertRaisesRegex(TypeError, msg): |
| 487 |
_interpreters.run_func(self.id, lambda: None, shared=1) |
| 488 |
# See https://github.com/python/cpython/issues/135855 |
| 489 |
msg = r'_interpreters.set___main___attrs\(\) argument 2 must be dict, not int' |
| 490 |
with self.assertRaisesRegex(TypeError, msg): |
| 491 |
_interpreters.set___main___attrs(self.id, 1) |
| 492 |
|
| 493 |
def test_invalid_shared_none(self): |
| 494 |
msg = r'must be dict, not None' |
| 495 |
with self.assertRaisesRegex(TypeError, msg): |
| 496 |
_interpreters.exec(self.id, 'a', shared=None) |
| 497 |
with self.assertRaisesRegex(TypeError, msg): |
| 498 |
_interpreters.run_string(self.id, 'a', shared=None) |
| 499 |
with self.assertRaisesRegex(TypeError, msg): |
| 500 |
_interpreters.run_func(self.id, lambda: None, shared=None) |
| 501 |
with self.assertRaisesRegex(TypeError, msg): |
| 502 |
_interpreters.set___main___attrs(self.id, None) |
| 503 |
|
| 504 |
def test_invalid_shared_encoding(self): |
| 505 |
# See https://github.com/python/cpython/issues/127196 |
| 506 |
bad_shared = {"\uD82A": 0} |
| 507 |
msg = 'surrogates not allowed' |
| 508 |
with self.assertRaisesRegex(UnicodeEncodeError, msg): |
| 509 |
_interpreters.exec(self.id, 'a', shared=bad_shared) |
| 510 |
with self.assertRaisesRegex(UnicodeEncodeError, msg): |
| 511 |
_interpreters.run_string(self.id, 'a', shared=bad_shared) |
| 512 |
with self.assertRaisesRegex(UnicodeEncodeError, msg): |
| 513 |
_interpreters.run_func(self.id, lambda: None, shared=bad_shared) |
| 514 |
|
| 515 |
|
| 516 |
class RunStringTests(TestBase): |
| 517 |
|
| 518 |
def setUp(self): |
| 519 |
super().setUp() |
| 520 |
self.id = _interpreters.create() |
| 521 |
|
| 522 |
def test_success(self): |
| 523 |
script, file = _captured_script('print("it worked!", end="")') |
| 524 |
with file: |
| 525 |
_interpreters.run_string(self.id, script) |
| 526 |
out = file.read() |
| 527 |
|
| 528 |
self.assertEqual(out, 'it worked!') |
| 529 |
|
| 530 |
def test_in_thread(self): |
| 531 |
script, file = _captured_script('print("it worked!", end="")') |
| 532 |
with file: |
| 533 |
def f(): |
| 534 |
_interpreters.run_string(self.id, script) |
| 535 |
|
| 536 |
t = threading.Thread(target=f) |
| 537 |
t.start() |
| 538 |
t.join() |
| 539 |
out = file.read() |
| 540 |
|
| 541 |
self.assertEqual(out, 'it worked!') |
| 542 |
|
| 543 |
def test_create_thread(self): |
| 544 |
subinterp = _interpreters.create() |
| 545 |
script, file = _captured_script(""" |
| 546 |
import threading |
| 547 |
def f(): |
| 548 |
print('it worked!', end='') |
| 549 |
|
| 550 |
t = threading.Thread(target=f) |
| 551 |
t.start() |
| 552 |
t.join() |
| 553 |
""") |
| 554 |
with file: |
| 555 |
_interpreters.run_string(subinterp, script) |
| 556 |
out = file.read() |
| 557 |
|
| 558 |
self.assertEqual(out, 'it worked!') |
| 559 |
|
| 560 |
def test_create_daemon_thread(self): |
| 561 |
with self.subTest('isolated'): |
| 562 |
expected = 'spam spam spam spam spam' |
| 563 |
subinterp = _interpreters.create('isolated') |
| 564 |
script, file = _captured_script(f""" |
| 565 |
import threading |
| 566 |
def f(): |
| 567 |
print('it worked!', end='') |
| 568 |
|
| 569 |
try: |
| 570 |
t = threading.Thread(target=f, daemon=True) |
| 571 |
t.start() |
| 572 |
t.join() |
| 573 |
except RuntimeError: |
| 574 |
print('{expected}', end='') |
| 575 |
""") |
| 576 |
with file: |
| 577 |
_interpreters.run_string(subinterp, script) |
| 578 |
out = file.read() |
| 579 |
|
| 580 |
self.assertEqual(out, expected) |
| 581 |
|
| 582 |
with self.subTest('not isolated'): |
| 583 |
subinterp = _interpreters.create('legacy') |
| 584 |
script, file = _captured_script(""" |
| 585 |
import threading |
| 586 |
def f(): |
| 587 |
print('it worked!', end='') |
| 588 |
|
| 589 |
t = threading.Thread(target=f, daemon=True) |
| 590 |
t.start() |
| 591 |
t.join() |
| 592 |
""") |
| 593 |
with file: |
| 594 |
_interpreters.run_string(subinterp, script) |
| 595 |
out = file.read() |
| 596 |
|
| 597 |
self.assertEqual(out, 'it worked!') |
| 598 |
|
| 599 |
def test_shareable_types(self): |
| 600 |
interp = _interpreters.create() |
| 601 |
objects = [ |
| 602 |
None, |
| 603 |
'spam', |
| 604 |
b'spam', |
| 605 |
42, |
| 606 |
] |
| 607 |
for obj in objects: |
| 608 |
with self.subTest(obj): |
| 609 |
_interpreters.set___main___attrs(interp, dict(obj=obj)) |
| 610 |
_interpreters.run_string( |
| 611 |
interp, |
| 612 |
f'assert(obj == {obj!r})', |
| 613 |
) |
| 614 |
|
| 615 |
def test_os_exec(self): |
| 616 |
expected = 'spam spam spam spam spam' |
| 617 |
subinterp = _interpreters.create() |
| 618 |
script, file = _captured_script(f""" |
| 619 |
import os, sys |
| 620 |
try: |
| 621 |
os.execl(sys.executable) |
| 622 |
except RuntimeError: |
| 623 |
print('{expected}', end='') |
| 624 |
""") |
| 625 |
with file: |
| 626 |
_interpreters.run_string(subinterp, script) |
| 627 |
out = file.read() |
| 628 |
|
| 629 |
self.assertEqual(out, expected) |
| 630 |
|
| 631 |
@support.requires_fork() |
| 632 |
def test_fork(self): |
| 633 |
import tempfile |
| 634 |
with tempfile.NamedTemporaryFile('w+', encoding="utf-8") as file: |
| 635 |
file.write('') |
| 636 |
file.flush() |
| 637 |
|
| 638 |
expected = 'spam spam spam spam spam' |
| 639 |
script = dedent(f""" |
| 640 |
import os |
| 641 |
try: |
| 642 |
os.fork() |
| 643 |
except RuntimeError: |
| 644 |
with open('{file.name}', 'w', encoding='utf-8') as out: |
| 645 |
out.write('{expected}') |
| 646 |
""") |
| 647 |
_interpreters.run_string(self.id, script) |
| 648 |
|
| 649 |
file.seek(0) |
| 650 |
content = file.read() |
| 651 |
self.assertEqual(content, expected) |
| 652 |
|
| 653 |
def test_already_running(self): |
| 654 |
with _running(self.id): |
| 655 |
with self.assertRaises(_interpreters.InterpreterError): |
| 656 |
_interpreters.run_string(self.id, 'print("spam")') |
| 657 |
|
| 658 |
def test_does_not_exist(self): |
| 659 |
id = 0 |
| 660 |
while id in set(id for id, *_ in _interpreters.list_all()): |
| 661 |
id += 1 |
| 662 |
with self.assertRaises(InterpreterNotFoundError): |
| 663 |
_interpreters.run_string(id, 'print("spam")') |
| 664 |
|
| 665 |
def test_error_id(self): |
| 666 |
with self.assertRaises(ValueError): |
| 667 |
_interpreters.run_string(-1, 'print("spam")') |
| 668 |
|
| 669 |
def test_bad_id(self): |
| 670 |
with self.assertRaises(TypeError): |
| 671 |
_interpreters.run_string('spam', 'print("spam")') |
| 672 |
|
| 673 |
def test_bad_script(self): |
| 674 |
with self.assertRaises(TypeError): |
| 675 |
_interpreters.run_string(self.id, 10) |
| 676 |
|
| 677 |
def test_bytes_for_script(self): |
| 678 |
with self.assertRaises(TypeError): |
| 679 |
_interpreters.run_string(self.id, b'print("spam")') |
| 680 |
|
| 681 |
def test_str_subclass_string(self): |
| 682 |
class StrSubclass(str): pass |
| 683 |
|
| 684 |
output = _run_output(self.id, StrSubclass('print(1 + 2)')) |
| 685 |
self.assertEqual(output, '3\n') |
| 686 |
|
| 687 |
def test_with_shared(self): |
| 688 |
r, w = os.pipe() |
| 689 |
|
| 690 |
shared = { |
| 691 |
'spam': b'ham', |
| 692 |
'eggs': b'-1', |
| 693 |
'cheddar': None, |
| 694 |
} |
| 695 |
script = dedent(f""" |
| 696 |
eggs = int(eggs) |
| 697 |
spam = 42 |
| 698 |
result = spam + eggs |
| 699 |
|
| 700 |
ns = dict(vars()) |
| 701 |
del ns['__builtins__'] |
| 702 |
import pickle |
| 703 |
with open({w}, 'wb') as chan: |
| 704 |
pickle.dump(ns, chan) |
| 705 |
""") |
| 706 |
_interpreters.set___main___attrs(self.id, shared) |
| 707 |
_interpreters.run_string(self.id, script) |
| 708 |
with open(r, 'rb') as chan: |
| 709 |
ns = pickle.load(chan) |
| 710 |
|
| 711 |
self.assertEqual(ns['spam'], 42) |
| 712 |
self.assertEqual(ns['eggs'], -1) |
| 713 |
self.assertEqual(ns['result'], 41) |
| 714 |
self.assertIsNone(ns['cheddar']) |
| 715 |
|
| 716 |
def test_shared_overwrites(self): |
| 717 |
_interpreters.run_string(self.id, dedent(""" |
| 718 |
spam = 'eggs' |
| 719 |
ns1 = dict(vars()) |
| 720 |
del ns1['__builtins__'] |
| 721 |
""")) |
| 722 |
|
| 723 |
shared = {'spam': b'ham'} |
| 724 |
script = dedent(""" |
| 725 |
ns2 = dict(vars()) |
| 726 |
del ns2['__builtins__'] |
| 727 |
""") |
| 728 |
_interpreters.set___main___attrs(self.id, shared) |
| 729 |
_interpreters.run_string(self.id, script) |
| 730 |
|
| 731 |
r, w = os.pipe() |
| 732 |
script = dedent(f""" |
| 733 |
ns = dict(vars()) |
| 734 |
del ns['__builtins__'] |
| 735 |
import pickle |
| 736 |
with open({w}, 'wb') as chan: |
| 737 |
pickle.dump(ns, chan) |
| 738 |
""") |
| 739 |
_interpreters.run_string(self.id, script) |
| 740 |
with open(r, 'rb') as chan: |
| 741 |
ns = pickle.load(chan) |
| 742 |
|
| 743 |
self.assertEqual(ns['ns1']['spam'], 'eggs') |
| 744 |
self.assertEqual(ns['ns2']['spam'], b'ham') |
| 745 |
self.assertEqual(ns['spam'], b'ham') |
| 746 |
|
| 747 |
def test_shared_overwrites_default_vars(self): |
| 748 |
r, w = os.pipe() |
| 749 |
|
| 750 |
shared = {'__name__': b'not __main__'} |
| 751 |
script = dedent(f""" |
| 752 |
spam = 42 |
| 753 |
|
| 754 |
ns = dict(vars()) |
| 755 |
del ns['__builtins__'] |
| 756 |
import pickle |
| 757 |
with open({w}, 'wb') as chan: |
| 758 |
pickle.dump(ns, chan) |
| 759 |
""") |
| 760 |
_interpreters.set___main___attrs(self.id, shared) |
| 761 |
_interpreters.run_string(self.id, script) |
| 762 |
with open(r, 'rb') as chan: |
| 763 |
ns = pickle.load(chan) |
| 764 |
|
| 765 |
self.assertEqual(ns['__name__'], b'not __main__') |
| 766 |
|
| 767 |
def test_main_reused(self): |
| 768 |
r, w = os.pipe() |
| 769 |
_interpreters.run_string(self.id, dedent(f""" |
| 770 |
spam = True |
| 771 |
|
| 772 |
ns = dict(vars()) |
| 773 |
del ns['__builtins__'] |
| 774 |
import pickle |
| 775 |
with open({w}, 'wb') as chan: |
| 776 |
pickle.dump(ns, chan) |
| 777 |
del ns, pickle, chan |
| 778 |
""")) |
| 779 |
with open(r, 'rb') as chan: |
| 780 |
ns1 = pickle.load(chan) |
| 781 |
|
| 782 |
r, w = os.pipe() |
| 783 |
_interpreters.run_string(self.id, dedent(f""" |
| 784 |
eggs = False |
| 785 |
|
| 786 |
ns = dict(vars()) |
| 787 |
del ns['__builtins__'] |
| 788 |
import pickle |
| 789 |
with open({w}, 'wb') as chan: |
| 790 |
pickle.dump(ns, chan) |
| 791 |
""")) |
| 792 |
with open(r, 'rb') as chan: |
| 793 |
ns2 = pickle.load(chan) |
| 794 |
|
| 795 |
self.assertIn('spam', ns1) |
| 796 |
self.assertNotIn('eggs', ns1) |
| 797 |
self.assertIn('eggs', ns2) |
| 798 |
self.assertIn('spam', ns2) |
| 799 |
|
| 800 |
def test_execution_namespace_is_main(self): |
| 801 |
r, w = os.pipe() |
| 802 |
|
| 803 |
script = dedent(f""" |
| 804 |
spam = 42 |
| 805 |
|
| 806 |
ns = dict(vars()) |
| 807 |
ns['__builtins__'] = str(ns['__builtins__']) |
| 808 |
import pickle |
| 809 |
with open({w}, 'wb') as chan: |
| 810 |
pickle.dump(ns, chan) |
| 811 |
""") |
| 812 |
_interpreters.run_string(self.id, script) |
| 813 |
with open(r, 'rb') as chan: |
| 814 |
ns = pickle.load(chan) |
| 815 |
|
| 816 |
ns.pop('__builtins__') |
| 817 |
ns.pop('__loader__') |
| 818 |
self.assertEqual(ns, { |
| 819 |
'__name__': '__main__', |
| 820 |
'__doc__': None, |
| 821 |
'__package__': None, |
| 822 |
'__spec__': None, |
| 823 |
'spam': 42, |
| 824 |
}) |
| 825 |
|
| 826 |
# XXX Fix this test! |
| 827 |
@unittest.skip('blocking forever') |
| 828 |
def test_still_running_at_exit(self): |
| 829 |
script = dedent(""" |
| 830 |
from textwrap import dedent |
| 831 |
import threading |
| 832 |
import _interpreters |
| 833 |
id = _interpreters.create() |
| 834 |
def f(): |
| 835 |
_interpreters.run_string(id, dedent(''' |
| 836 |
import time |
| 837 |
# Give plenty of time for the main interpreter to finish. |
| 838 |
time.sleep(1_000_000) |
| 839 |
''')) |
| 840 |
|
| 841 |
t = threading.Thread(target=f) |
| 842 |
t.start() |
| 843 |
""") |
| 844 |
with support.temp_dir() as dirname: |
| 845 |
filename = script_helper.make_script(dirname, 'interp', script) |
| 846 |
with script_helper.spawn_python(filename) as proc: |
| 847 |
retcode = proc.wait() |
| 848 |
|
| 849 |
self.assertEqual(retcode, 0) |
| 850 |
|
| 851 |
|
| 852 |
class RunFailedTests(TestBase): |
| 853 |
|
| 854 |
def setUp(self): |
| 855 |
super().setUp() |
| 856 |
self.id = _interpreters.create() |
| 857 |
|
| 858 |
def add_module(self, modname, text): |
| 859 |
import tempfile |
| 860 |
tempdir = tempfile.mkdtemp() |
| 861 |
self.addCleanup(lambda: os_helper.rmtree(tempdir)) |
| 862 |
_interpreters.run_string(self.id, dedent(f""" |
| 863 |
import sys |
| 864 |
sys.path.insert(0, {tempdir!r}) |
| 865 |
""")) |
| 866 |
return script_helper.make_script(tempdir, modname, text) |
| 867 |
|
| 868 |
def run_script(self, text, *, fails=False): |
| 869 |
r, w = os.pipe() |
| 870 |
try: |
| 871 |
script = dedent(f""" |
| 872 |
import os, sys |
| 873 |
os.write({w}, b'0') |
| 874 |
|
| 875 |
# This raises an exception: |
| 876 |
{{}} |
| 877 |
|
| 878 |
# Nothing from here down should ever run. |
| 879 |
os.write({w}, b'1') |
| 880 |
class NeverError(Exception): pass |
| 881 |
raise NeverError # never raised |
| 882 |
""").format(dedent(text)) |
| 883 |
if fails: |
| 884 |
err = _interpreters.run_string(self.id, script) |
| 885 |
self.assertIsNot(err, None) |
| 886 |
return err |
| 887 |
else: |
| 888 |
err = _interpreters.run_string(self.id, script) |
| 889 |
self.assertIs(err, None) |
| 890 |
return None |
| 891 |
except: |
| 892 |
raise # re-raise |
| 893 |
else: |
| 894 |
msg = os.read(r, 100) |
| 895 |
self.assertEqual(msg, b'0') |
| 896 |
finally: |
| 897 |
os.close(r) |
| 898 |
os.close(w) |
| 899 |
|
| 900 |
def _assert_run_failed(self, exctype, msg, script): |
| 901 |
if isinstance(exctype, str): |
| 902 |
exctype_name = exctype |
| 903 |
exctype = None |
| 904 |
else: |
| 905 |
exctype_name = exctype.__name__ |
| 906 |
|
| 907 |
# Run the script. |
| 908 |
excinfo = self.run_script(script, fails=True) |
| 909 |
|
| 910 |
# Check the wrapper exception. |
| 911 |
self.assertEqual(excinfo.type.__name__, exctype_name) |
| 912 |
if msg is None: |
| 913 |
self.assertEqual(excinfo.formatted.split(':')[0], |
| 914 |
exctype_name) |
| 915 |
else: |
| 916 |
self.assertEqual(excinfo.formatted, |
| 917 |
'{}: {}'.format(exctype_name, msg)) |
| 918 |
|
| 919 |
return excinfo |
| 920 |
|
| 921 |
def assert_run_failed(self, exctype, script): |
| 922 |
self._assert_run_failed(exctype, None, script) |
| 923 |
|
| 924 |
def assert_run_failed_msg(self, exctype, msg, script): |
| 925 |
self._assert_run_failed(exctype, msg, script) |
| 926 |
|
| 927 |
def test_exit(self): |
| 928 |
with self.subTest('sys.exit(0)'): |
| 929 |
# XXX Should an unhandled SystemExit(0) be handled as not-an-error? |
| 930 |
self.assert_run_failed(SystemExit, """ |
| 931 |
sys.exit(0) |
| 932 |
""") |
| 933 |
|
| 934 |
with self.subTest('sys.exit()'): |
| 935 |
self.assert_run_failed(SystemExit, """ |
| 936 |
import sys |
| 937 |
sys.exit() |
| 938 |
""") |
| 939 |
|
| 940 |
with self.subTest('sys.exit(42)'): |
| 941 |
self.assert_run_failed_msg(SystemExit, '42', """ |
| 942 |
import sys |
| 943 |
sys.exit(42) |
| 944 |
""") |
| 945 |
|
| 946 |
with self.subTest('SystemExit'): |
| 947 |
self.assert_run_failed_msg(SystemExit, '42', """ |
| 948 |
raise SystemExit(42) |
| 949 |
""") |
| 950 |
|
| 951 |
# XXX Also check os._exit() (via a subprocess)? |
| 952 |
|
| 953 |
def test_plain_exception(self): |
| 954 |
self.assert_run_failed_msg(Exception, 'spam', """ |
| 955 |
raise Exception("spam") |
| 956 |
""") |
| 957 |
|
| 958 |
def test_invalid_syntax(self): |
| 959 |
script = dedent(""" |
| 960 |
x = 1 + 2 |
| 961 |
y = 2 + 4 |
| 962 |
z = 4 + 8 |
| 963 |
|
| 964 |
# missing close paren |
| 965 |
print("spam" |
| 966 |
|
| 967 |
if x + y + z < 20: |
| 968 |
... |
| 969 |
""") |
| 970 |
|
| 971 |
with self.subTest('script'): |
| 972 |
with self.assertRaises(SyntaxError): |
| 973 |
_interpreters.run_string(self.id, script) |
| 974 |
|
| 975 |
with self.subTest('module'): |
| 976 |
modname = 'spam_spam_spam' |
| 977 |
filename = self.add_module(modname, script) |
| 978 |
self.assert_run_failed(SyntaxError, f""" |
| 979 |
import {modname} |
| 980 |
""") |
| 981 |
|
| 982 |
def test_NameError(self): |
| 983 |
self.assert_run_failed(NameError, """ |
| 984 |
res = spam + eggs |
| 985 |
""") |
| 986 |
# XXX check preserved suggestions |
| 987 |
|
| 988 |
def test_AttributeError(self): |
| 989 |
self.assert_run_failed(AttributeError, """ |
| 990 |
object().spam |
| 991 |
""") |
| 992 |
# XXX check preserved suggestions |
| 993 |
|
| 994 |
def test_ExceptionGroup(self): |
| 995 |
self.assert_run_failed(ExceptionGroup, """ |
| 996 |
raise ExceptionGroup('exceptions', [ |
| 997 |
Exception('spam'), |
| 998 |
ImportError('eggs'), |
| 999 |
]) |
| 1000 |
""") |
| 1001 |
|
| 1002 |
def test_user_defined_exception(self): |
| 1003 |
self.assert_run_failed_msg('MyError', 'spam', """ |
| 1004 |
class MyError(Exception): |
| 1005 |
pass |
| 1006 |
raise MyError('spam') |
| 1007 |
""") |
| 1008 |
|
| 1009 |
|
| 1010 |
class RunFuncTests(TestBase): |
| 1011 |
|
| 1012 |
def setUp(self): |
| 1013 |
super().setUp() |
| 1014 |
self.id = _interpreters.create() |
| 1015 |
|
| 1016 |
def test_success(self): |
| 1017 |
r, w = os.pipe() |
| 1018 |
def script(): |
| 1019 |
global w |
| 1020 |
import contextlib |
| 1021 |
with open(w, 'w', encoding="utf-8") as spipe: |
| 1022 |
with contextlib.redirect_stdout(spipe): |
| 1023 |
print('it worked!', end='') |
| 1024 |
_interpreters.set___main___attrs(self.id, dict(w=w)) |
| 1025 |
_interpreters.run_func(self.id, script) |
| 1026 |
|
| 1027 |
with open(r, encoding="utf-8") as outfile: |
| 1028 |
out = outfile.read() |
| 1029 |
|
| 1030 |
self.assertEqual(out, 'it worked!') |
| 1031 |
|
| 1032 |
def test_in_thread(self): |
| 1033 |
r, w = os.pipe() |
| 1034 |
def script(): |
| 1035 |
global w |
| 1036 |
import contextlib |
| 1037 |
with open(w, 'w', encoding="utf-8") as spipe: |
| 1038 |
with contextlib.redirect_stdout(spipe): |
| 1039 |
print('it worked!', end='') |
| 1040 |
failed = None |
| 1041 |
def f(): |
| 1042 |
nonlocal failed |
| 1043 |
try: |
| 1044 |
_interpreters.set___main___attrs(self.id, dict(w=w)) |
| 1045 |
_interpreters.run_func(self.id, script) |
| 1046 |
except Exception as exc: |
| 1047 |
failed = exc |
| 1048 |
t = threading.Thread(target=f) |
| 1049 |
t.start() |
| 1050 |
t.join() |
| 1051 |
if failed: |
| 1052 |
raise Exception from failed |
| 1053 |
|
| 1054 |
with open(r, encoding="utf-8") as outfile: |
| 1055 |
out = outfile.read() |
| 1056 |
|
| 1057 |
self.assertEqual(out, 'it worked!') |
| 1058 |
|
| 1059 |
def test_code_object(self): |
| 1060 |
r, w = os.pipe() |
| 1061 |
|
| 1062 |
def script(): |
| 1063 |
global w |
| 1064 |
import contextlib |
| 1065 |
with open(w, 'w', encoding="utf-8") as spipe: |
| 1066 |
with contextlib.redirect_stdout(spipe): |
| 1067 |
print('it worked!', end='') |
| 1068 |
code = script.__code__ |
| 1069 |
_interpreters.set___main___attrs(self.id, dict(w=w)) |
| 1070 |
_interpreters.run_func(self.id, code) |
| 1071 |
|
| 1072 |
with open(r, encoding="utf-8") as outfile: |
| 1073 |
out = outfile.read() |
| 1074 |
|
| 1075 |
self.assertEqual(out, 'it worked!') |
| 1076 |
|
| 1077 |
def test_closure(self): |
| 1078 |
spam = True |
| 1079 |
def script(): |
| 1080 |
assert spam |
| 1081 |
with self.assertRaises(ValueError): |
| 1082 |
_interpreters.run_func(self.id, script) |
| 1083 |
|
| 1084 |
def test_return_value(self): |
| 1085 |
def script(): |
| 1086 |
return 'spam' |
| 1087 |
with self.assertRaises(ValueError): |
| 1088 |
_interpreters.run_func(self.id, script) |
| 1089 |
|
| 1090 |
# @unittest.skip("we're not quite there yet") |
| 1091 |
def test_args(self): |
| 1092 |
with self.subTest('args'): |
| 1093 |
def script(a, b=0): |
| 1094 |
assert a == b |
| 1095 |
with self.assertRaises(ValueError): |
| 1096 |
_interpreters.run_func(self.id, script) |
| 1097 |
|
| 1098 |
with self.subTest('*args'): |
| 1099 |
def script(*args): |
| 1100 |
assert not args |
| 1101 |
with self.assertRaises(ValueError): |
| 1102 |
_interpreters.run_func(self.id, script) |
| 1103 |
|
| 1104 |
with self.subTest('**kwargs'): |
| 1105 |
def script(**kwargs): |
| 1106 |
assert not kwargs |
| 1107 |
with self.assertRaises(ValueError): |
| 1108 |
_interpreters.run_func(self.id, script) |
| 1109 |
|
| 1110 |
with self.subTest('kwonly'): |
| 1111 |
def script(*, spam=True): |
| 1112 |
assert spam |
| 1113 |
with self.assertRaises(ValueError): |
| 1114 |
_interpreters.run_func(self.id, script) |
| 1115 |
|
| 1116 |
with self.subTest('posonly'): |
| 1117 |
def script(spam, /): |
| 1118 |
assert spam |
| 1119 |
with self.assertRaises(ValueError): |
| 1120 |
_interpreters.run_func(self.id, script) |
| 1121 |
|
| 1122 |
|
| 1123 |
if __name__ == '__main__': |
| 1124 |
unittest.main() |