| 1 |
import os |
| 2 |
from textwrap import dedent |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from .test_embed_kernel import setup_kernel |
| 7 |
|
| 8 |
TIMEOUT = 15 |
| 9 |
|
| 10 |
if os.name == "nt": |
| 11 |
pytest.skip("skipping tests on windows", allow_module_level=True) |
| 12 |
|
| 13 |
|
| 14 |
@pytest.mark.flaky(max_runs=3) |
| 15 |
def test_ipython_start_kernel_userns(): |
| 16 |
import IPython |
| 17 |
|
| 18 |
if IPython.version_info > (9, 0): # noqa:SIM108 |
| 19 |
EXPECTED = "IPythonMainModule" |
| 20 |
else: |
| 21 |
# not this since https://github.com/ipython/ipython/pull/14754 |
| 22 |
EXPECTED = "DummyMod" |
| 23 |
|
| 24 |
cmd = dedent( |
| 25 |
""" |
| 26 |
from ipykernel.kernelapp import launch_new_instance |
| 27 |
ns = {"custom": 123} |
| 28 |
launch_new_instance(user_ns=ns) |
| 29 |
""" |
| 30 |
) |
| 31 |
|
| 32 |
with setup_kernel(cmd) as client: |
| 33 |
client.inspect("custom") |
| 34 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 35 |
content = msg["content"] |
| 36 |
assert content["found"] |
| 37 |
text = content["data"]["text/plain"] |
| 38 |
assert "123" in text |
| 39 |
|
| 40 |
# user_module should be an instance of DummyMod |
| 41 |
client.execute("usermod = get_ipython().user_module") |
| 42 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 43 |
content = msg["content"] |
| 44 |
assert content["status"] == "ok" |
| 45 |
client.inspect("usermod") |
| 46 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 47 |
content = msg["content"] |
| 48 |
assert content["found"] |
| 49 |
text = content["data"]["text/plain"] |
| 50 |
assert EXPECTED in text |
| 51 |
|
| 52 |
|
| 53 |
def test_start_kernel_background_thread(): |
| 54 |
cmd = dedent( |
| 55 |
""" |
| 56 |
import threading |
| 57 |
import asyncio |
| 58 |
from ipykernel.kernelapp import launch_new_instance |
| 59 |
|
| 60 |
def launch(): |
| 61 |
# Threads don't always have a default event loop so we need to |
| 62 |
# create and set a default |
| 63 |
loop = asyncio.new_event_loop() |
| 64 |
asyncio.set_event_loop(loop) |
| 65 |
launch_new_instance() |
| 66 |
|
| 67 |
thread = threading.Thread(target=launch) |
| 68 |
thread.start() |
| 69 |
thread.join() |
| 70 |
""" |
| 71 |
) |
| 72 |
|
| 73 |
with setup_kernel(cmd) as client: |
| 74 |
client.execute("a = 1") |
| 75 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 76 |
content = msg["content"] |
| 77 |
assert content["status"] == "ok" |
| 78 |
|
| 79 |
client.inspect("a") |
| 80 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 81 |
content = msg["content"] |
| 82 |
assert content["found"] |
| 83 |
text = content["data"]["text/plain"] |
| 84 |
assert "1" in text |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.flaky(max_runs=3) |
| 88 |
def test_ipython_start_kernel_no_userns(): |
| 89 |
# Issue #4188 - user_ns should be passed to shell as None, not {} |
| 90 |
cmd = dedent( |
| 91 |
""" |
| 92 |
from ipykernel.kernelapp import launch_new_instance |
| 93 |
launch_new_instance() |
| 94 |
""" |
| 95 |
) |
| 96 |
|
| 97 |
with setup_kernel(cmd) as client: |
| 98 |
# user_module should not be an instance of DummyMod |
| 99 |
client.execute("usermod = get_ipython().user_module") |
| 100 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 101 |
content = msg["content"] |
| 102 |
assert content["status"] == "ok" |
| 103 |
client.inspect("usermod") |
| 104 |
msg = client.get_shell_msg(timeout=TIMEOUT) |
| 105 |
content = msg["content"] |
| 106 |
assert content["found"] |
| 107 |
text = content["data"]["text/plain"] |
| 108 |
assert "DummyMod" not in text |