| 1 |
"""Test IPythonKernel directly""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import os |
| 5 |
|
| 6 |
import pytest |
| 7 |
import zmq |
| 8 |
from IPython.core.history import DummyDB |
| 9 |
|
| 10 |
from ipykernel.comm.comm import BaseComm |
| 11 |
from ipykernel.ipkernel import IPythonKernel, _create_comm |
| 12 |
|
| 13 |
from .conftest import MockIPyKernel |
| 14 |
|
| 15 |
if os.name == "nt": |
| 16 |
pytest.skip("skipping tests on windows", allow_module_level=True) |
| 17 |
|
| 18 |
|
| 19 |
class user_mod: |
| 20 |
__dict__ = {} |
| 21 |
|
| 22 |
|
| 23 |
async def test_properties(ipkernel: IPythonKernel) -> None: |
| 24 |
ipkernel.user_module = user_mod() |
| 25 |
ipkernel.user_ns = {} |
| 26 |
|
| 27 |
|
| 28 |
async def test_direct_kernel_info_request(ipkernel): |
| 29 |
reply = await ipkernel.test_shell_message("kernel_info_request", {}) |
| 30 |
assert reply["header"]["msg_type"] == "kernel_info_reply" |
| 31 |
|
| 32 |
|
| 33 |
async def test_direct_execute_request(ipkernel: MockIPyKernel) -> None: |
| 34 |
reply = await ipkernel.test_shell_message("execute_request", dict(code="hello", silent=False)) |
| 35 |
assert reply["header"]["msg_type"] == "execute_reply" |
| 36 |
reply = await ipkernel.test_shell_message( |
| 37 |
"execute_request", dict(code="trigger_error", silent=False) |
| 38 |
) |
| 39 |
assert reply["content"]["status"] == "aborted" |
| 40 |
|
| 41 |
reply = await ipkernel.test_shell_message("execute_request", dict(code="hello", silent=False)) |
| 42 |
assert reply["header"]["msg_type"] == "execute_reply" |
| 43 |
|
| 44 |
|
| 45 |
async def test_direct_execute_request_aborting(ipkernel): |
| 46 |
ipkernel._aborting = True |
| 47 |
reply = await ipkernel.test_shell_message("execute_request", dict(code="hello", silent=False)) |
| 48 |
assert reply["header"]["msg_type"] == "execute_reply" |
| 49 |
assert reply["content"]["status"] == "aborted" |
| 50 |
|
| 51 |
|
| 52 |
async def test_complete_request(ipkernel): |
| 53 |
reply = await ipkernel.test_shell_message("complete_request", dict(code="hello", cursor_pos=0)) |
| 54 |
assert reply["header"]["msg_type"] == "complete_reply" |
| 55 |
ipkernel.use_experimental_completions = False |
| 56 |
reply = await ipkernel.test_shell_message( |
| 57 |
"complete_request", dict(code="hello", cursor_pos=None) |
| 58 |
) |
| 59 |
assert reply["header"]["msg_type"] == "complete_reply" |
| 60 |
|
| 61 |
|
| 62 |
async def test_inspect_request(ipkernel): |
| 63 |
reply = await ipkernel.test_shell_message("inspect_request", dict(code="hello", cursor_pos=0)) |
| 64 |
assert reply["header"]["msg_type"] == "inspect_reply" |
| 65 |
|
| 66 |
|
| 67 |
async def test_history_request(ipkernel): |
| 68 |
ipkernel.shell.history_manager.db = DummyDB() |
| 69 |
reply = await ipkernel.test_shell_message( |
| 70 |
"history_request", dict(hist_access_type="", output="", raw="") |
| 71 |
) |
| 72 |
assert reply["header"]["msg_type"] == "history_reply" |
| 73 |
reply = await ipkernel.test_shell_message( |
| 74 |
"history_request", dict(hist_access_type="tail", output="", raw="") |
| 75 |
) |
| 76 |
assert reply["header"]["msg_type"] == "history_reply" |
| 77 |
reply = await ipkernel.test_shell_message( |
| 78 |
"history_request", dict(hist_access_type="range", output="", raw="") |
| 79 |
) |
| 80 |
assert reply["header"]["msg_type"] == "history_reply" |
| 81 |
reply = await ipkernel.test_shell_message( |
| 82 |
"history_request", dict(hist_access_type="search", output="", raw="") |
| 83 |
) |
| 84 |
assert reply["header"]["msg_type"] == "history_reply" |
| 85 |
|
| 86 |
|
| 87 |
async def test_comm_info_request(ipkernel): |
| 88 |
reply = await ipkernel.test_shell_message("comm_info_request") |
| 89 |
assert reply["header"]["msg_type"] == "comm_info_reply" |
| 90 |
|
| 91 |
|
| 92 |
async def test_direct_interrupt_request(ipkernel): |
| 93 |
reply = await ipkernel.test_control_message("interrupt_request", {}) |
| 94 |
assert reply["header"]["msg_type"] == "interrupt_reply" |
| 95 |
assert reply["content"] == {"status": "ok"} |
| 96 |
|
| 97 |
# test failure on interrupt request |
| 98 |
def raiseOSError(): |
| 99 |
msg = "evalue" |
| 100 |
raise OSError(msg) |
| 101 |
|
| 102 |
ipkernel._send_interrupt_children = raiseOSError |
| 103 |
reply = await ipkernel.test_control_message("interrupt_request", {}) |
| 104 |
assert reply["header"]["msg_type"] == "interrupt_reply" |
| 105 |
assert reply["content"]["status"] == "error" |
| 106 |
assert reply["content"]["ename"] == "OSError" |
| 107 |
assert reply["content"]["evalue"] == "evalue" |
| 108 |
assert len(reply["content"]["traceback"]) > 0 |
| 109 |
|
| 110 |
|
| 111 |
# TODO: this causes deadlock |
| 112 |
# async def test_direct_shutdown_request(ipkernel): |
| 113 |
# reply = await ipkernel.test_shell_message("shutdown_request", dict(restart=False)) |
| 114 |
# assert reply["header"]["msg_type"] == "shutdown_reply" |
| 115 |
# reply = await ipkernel.test_shell_message("shutdown_request", dict(restart=True)) |
| 116 |
# assert reply["header"]["msg_type"] == "shutdown_reply" |
| 117 |
|
| 118 |
# TODO: this causes deadlock |
| 119 |
# async def test_direct_usage_request(kernel): |
| 120 |
# reply = await kernel.test_control_message("usage_request", {}) |
| 121 |
# assert reply['header']['msg_type'] == 'usage_reply' |
| 122 |
|
| 123 |
|
| 124 |
async def test_is_complete_request(ipkernel: MockIPyKernel) -> None: |
| 125 |
reply = await ipkernel.test_shell_message("is_complete_request", dict(code="hello")) |
| 126 |
assert reply["header"]["msg_type"] == "is_complete_reply" |
| 127 |
setattr(ipkernel, "shell.input_transformer_manager", None) |
| 128 |
reply = await ipkernel.test_shell_message("is_complete_request", dict(code="hello")) |
| 129 |
assert reply["header"]["msg_type"] == "is_complete_reply" |
| 130 |
|
| 131 |
|
| 132 |
def test_do_apply(ipkernel: MockIPyKernel) -> None: |
| 133 |
from ipyparallel import pack_apply_message |
| 134 |
|
| 135 |
def hello(): |
| 136 |
pass |
| 137 |
|
| 138 |
msg = pack_apply_message(hello, (), {}) |
| 139 |
ipkernel.do_apply(None, msg, "1", {}) |
| 140 |
ipkernel.do_apply(None, [], "1", {}) |
| 141 |
|
| 142 |
|
| 143 |
async def test_direct_debug_request(ipkernel): |
| 144 |
reply = await ipkernel.test_control_message("debug_request", {}) |
| 145 |
assert reply["header"]["msg_type"] == "debug_reply" |
| 146 |
|
| 147 |
|
| 148 |
async def test_direct_clear(ipkernel): |
| 149 |
ipkernel.do_clear() |
| 150 |
|
| 151 |
|
| 152 |
async def test_cancel_on_sigint(ipkernel: IPythonKernel) -> None: |
| 153 |
future: asyncio.Future = asyncio.Future() |
| 154 |
with ipkernel._cancel_on_sigint(future): |
| 155 |
pass |
| 156 |
future.set_result(None) |
| 157 |
|
| 158 |
|
| 159 |
def test_dispatch_debugpy(ipkernel: IPythonKernel) -> None: |
| 160 |
msg = ipkernel.session.msg("debug_request", {}) |
| 161 |
msg_list = ipkernel.session.serialize(msg) |
| 162 |
ipkernel.dispatch_debugpy([zmq.Message(m) for m in msg_list]) |
| 163 |
|
| 164 |
|
| 165 |
async def test_start(ipkernel: IPythonKernel) -> None: |
| 166 |
shell_future: asyncio.Future = asyncio.Future() |
| 167 |
|
| 168 |
def fake_publish_status(status, channel): |
| 169 |
if status == "starting" and channel == "shell": |
| 170 |
shell_future.set_result(None) |
| 171 |
|
| 172 |
ipkernel._publish_status = fake_publish_status # type:ignore |
| 173 |
ipkernel.start() |
| 174 |
|
| 175 |
await shell_future |
| 176 |
|
| 177 |
shell_stream = ipkernel.shell_stream |
| 178 |
assert shell_stream is not None |
| 179 |
assert not shell_stream.closed() |
| 180 |
|
| 181 |
|
| 182 |
async def test_start_no_debugpy(ipkernel: IPythonKernel) -> None: |
| 183 |
shell_future: asyncio.Future = asyncio.Future() |
| 184 |
|
| 185 |
def fake_publish_status(status, channel): |
| 186 |
if status == "starting" and channel == "shell": |
| 187 |
shell_future.set_result(None) |
| 188 |
|
| 189 |
ipkernel._publish_status = fake_publish_status # type:ignore |
| 190 |
ipkernel.debugpy_stream = None |
| 191 |
ipkernel.start() |
| 192 |
|
| 193 |
await shell_future |
| 194 |
|
| 195 |
shell_stream = ipkernel.shell_stream |
| 196 |
assert shell_stream is not None |
| 197 |
assert not shell_stream.closed() |
| 198 |
|
| 199 |
|
| 200 |
def test_create_comm(): |
| 201 |
assert isinstance(_create_comm(), BaseComm) |
| 202 |
|
| 203 |
|
| 204 |
def test_finish_metadata(ipkernel: IPythonKernel) -> None: |
| 205 |
reply_content = dict(status="error", ename="UnmetDependency") |
| 206 |
metadata = ipkernel.finish_metadata({}, {}, reply_content) |
| 207 |
assert metadata["dependencies_met"] is False |
| 208 |
|
| 209 |
|
| 210 |
async def test_do_debug_request(ipkernel: IPythonKernel) -> None: |
| 211 |
msg = ipkernel.session.msg("debug_request", {}) |
| 212 |
ipkernel.session.serialize(msg) |
| 213 |
await ipkernel.do_debug_request(msg) |