| 1 |
import sys |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from .utils import TIMEOUT, get_replies, get_reply, new_kernel |
| 6 |
|
| 7 |
seq = 0 |
| 8 |
|
| 9 |
# Tests support debugpy not being installed, in which case the tests don't do anything useful |
| 10 |
# functionally as the debug message replies are usually empty dictionaries, but they confirm that |
| 11 |
# ipykernel doesn't block, or segfault, or raise an exception. |
| 12 |
try: |
| 13 |
import debugpy |
| 14 |
except ImportError: |
| 15 |
debugpy = None |
| 16 |
|
| 17 |
|
| 18 |
def prepare_debug_request(kernel, command, arguments=None): |
| 19 |
"""Prepare a debug request but do not send it.""" |
| 20 |
global seq |
| 21 |
seq += 1 |
| 22 |
|
| 23 |
msg = kernel.session.msg( |
| 24 |
"debug_request", |
| 25 |
{ |
| 26 |
"type": "request", |
| 27 |
"seq": seq, |
| 28 |
"command": command, |
| 29 |
"arguments": arguments or {}, |
| 30 |
}, |
| 31 |
) |
| 32 |
return msg |
| 33 |
|
| 34 |
|
| 35 |
def wait_for_debug_request(kernel, command, arguments=None, full_reply=False): |
| 36 |
"""Carry out a debug request and return the reply content. |
| 37 |
|
| 38 |
It does not check if the request was successful. |
| 39 |
""" |
| 40 |
msg = prepare_debug_request(kernel, command, arguments) |
| 41 |
kernel.control_channel.send(msg) |
| 42 |
reply = get_reply(kernel, msg["header"]["msg_id"], channel="control") |
| 43 |
return reply if full_reply else reply["content"] |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture() |
| 47 |
def kernel(): |
| 48 |
with new_kernel() as kc: |
| 49 |
yield kc |
| 50 |
|
| 51 |
|
| 52 |
@pytest.fixture() |
| 53 |
def kernel_with_debug(kernel): |
| 54 |
# Initialize |
| 55 |
wait_for_debug_request( |
| 56 |
kernel, |
| 57 |
"initialize", |
| 58 |
{ |
| 59 |
"clientID": "test-client", |
| 60 |
"clientName": "testClient", |
| 61 |
"adapterID": "", |
| 62 |
"pathFormat": "path", |
| 63 |
"linesStartAt1": True, |
| 64 |
"columnsStartAt1": True, |
| 65 |
"supportsVariableType": True, |
| 66 |
"supportsVariablePaging": True, |
| 67 |
"supportsRunInTerminalRequest": True, |
| 68 |
"locale": "en", |
| 69 |
}, |
| 70 |
) |
| 71 |
|
| 72 |
# Attach |
| 73 |
wait_for_debug_request(kernel, "attach") |
| 74 |
|
| 75 |
try: |
| 76 |
yield kernel |
| 77 |
finally: |
| 78 |
# Detach |
| 79 |
wait_for_debug_request(kernel, "disconnect", {"restart": False, "terminateDebuggee": True}) |
| 80 |
|
| 81 |
|
| 82 |
def test_debug_initialize(kernel): |
| 83 |
reply = wait_for_debug_request( |
| 84 |
kernel, |
| 85 |
"initialize", |
| 86 |
{ |
| 87 |
"clientID": "test-client", |
| 88 |
"clientName": "testClient", |
| 89 |
"adapterID": "", |
| 90 |
"pathFormat": "path", |
| 91 |
"linesStartAt1": True, |
| 92 |
"columnsStartAt1": True, |
| 93 |
"supportsVariableType": True, |
| 94 |
"supportsVariablePaging": True, |
| 95 |
"supportsRunInTerminalRequest": True, |
| 96 |
"locale": "en", |
| 97 |
}, |
| 98 |
) |
| 99 |
if debugpy: |
| 100 |
assert reply["success"] |
| 101 |
else: |
| 102 |
assert reply == {} |
| 103 |
|
| 104 |
|
| 105 |
def test_supported_features(kernel_with_debug): |
| 106 |
kernel_with_debug.kernel_info() |
| 107 |
reply = kernel_with_debug.get_shell_msg(timeout=TIMEOUT) |
| 108 |
supported_features = reply["content"]["supported_features"] |
| 109 |
|
| 110 |
if debugpy: |
| 111 |
assert "debugger" in supported_features |
| 112 |
else: |
| 113 |
assert "debugger" not in supported_features |
| 114 |
|
| 115 |
|
| 116 |
def test_attach_debug(kernel_with_debug): |
| 117 |
reply = wait_for_debug_request( |
| 118 |
kernel_with_debug, "evaluate", {"expression": "'a' + 'b'", "context": "repl"} |
| 119 |
) |
| 120 |
if debugpy: |
| 121 |
assert reply["success"] |
| 122 |
assert reply["body"]["result"] == "" |
| 123 |
else: |
| 124 |
assert reply == {} |
| 125 |
|
| 126 |
|
| 127 |
def test_set_breakpoints(kernel_with_debug): |
| 128 |
code = """def f(a, b): |
| 129 |
c = a + b |
| 130 |
return c |
| 131 |
|
| 132 |
f(2, 3)""" |
| 133 |
|
| 134 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 135 |
if debugpy: |
| 136 |
source = r["body"]["sourcePath"] |
| 137 |
else: |
| 138 |
assert r == {} |
| 139 |
source = "non-existent path" |
| 140 |
|
| 141 |
reply = wait_for_debug_request( |
| 142 |
kernel_with_debug, |
| 143 |
"setBreakpoints", |
| 144 |
{ |
| 145 |
"breakpoints": [{"line": 2}], |
| 146 |
"source": {"path": source}, |
| 147 |
"sourceModified": False, |
| 148 |
}, |
| 149 |
) |
| 150 |
if debugpy: |
| 151 |
assert reply["success"] |
| 152 |
assert len(reply["body"]["breakpoints"]) == 1 |
| 153 |
assert reply["body"]["breakpoints"][0]["verified"] |
| 154 |
assert reply["body"]["breakpoints"][0]["source"]["path"] == source |
| 155 |
else: |
| 156 |
assert reply == {} |
| 157 |
|
| 158 |
r = wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 159 |
|
| 160 |
def func(b): |
| 161 |
return b["source"] |
| 162 |
|
| 163 |
if debugpy: |
| 164 |
assert source in map(func, r["body"]["breakpoints"]) |
| 165 |
else: |
| 166 |
assert r == {} |
| 167 |
|
| 168 |
r = wait_for_debug_request(kernel_with_debug, "configurationDone") |
| 169 |
if debugpy: |
| 170 |
assert r["success"] |
| 171 |
else: |
| 172 |
assert r == {} |
| 173 |
|
| 174 |
|
| 175 |
def test_stop_on_breakpoint(kernel_with_debug): |
| 176 |
code = """def f(a, b): |
| 177 |
c = a + b |
| 178 |
return c |
| 179 |
|
| 180 |
f(2, 3)""" |
| 181 |
|
| 182 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 183 |
if debugpy: |
| 184 |
source = r["body"]["sourcePath"] |
| 185 |
else: |
| 186 |
assert r == {} |
| 187 |
source = "some path" |
| 188 |
|
| 189 |
wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 190 |
|
| 191 |
wait_for_debug_request( |
| 192 |
kernel_with_debug, |
| 193 |
"setBreakpoints", |
| 194 |
{ |
| 195 |
"breakpoints": [{"line": 2}], |
| 196 |
"source": {"path": source}, |
| 197 |
"sourceModified": False, |
| 198 |
}, |
| 199 |
) |
| 200 |
|
| 201 |
wait_for_debug_request(kernel_with_debug, "configurationDone", full_reply=True) |
| 202 |
|
| 203 |
kernel_with_debug.execute(code) |
| 204 |
|
| 205 |
if not debugpy: |
| 206 |
# Cannot stop on breakpoint if debugpy not installed |
| 207 |
return |
| 208 |
|
| 209 |
# Wait for stop on breakpoint |
| 210 |
msg: dict = {"msg_type": "", "content": {}} |
| 211 |
while msg.get("msg_type") != "debug_event" or msg["content"].get("event") != "stopped": |
| 212 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 213 |
|
| 214 |
assert msg["content"]["body"]["reason"] == "breakpoint" |
| 215 |
|
| 216 |
|
| 217 |
def test_breakpoint_in_cell_with_leading_empty_lines(kernel_with_debug): |
| 218 |
code = """ |
| 219 |
def f(a, b): |
| 220 |
c = a + b |
| 221 |
return c |
| 222 |
|
| 223 |
f(2, 3)""" |
| 224 |
|
| 225 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 226 |
if debugpy: |
| 227 |
source = r["body"]["sourcePath"] |
| 228 |
else: |
| 229 |
assert r == {} |
| 230 |
source = "some path" |
| 231 |
|
| 232 |
wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 233 |
|
| 234 |
wait_for_debug_request( |
| 235 |
kernel_with_debug, |
| 236 |
"setBreakpoints", |
| 237 |
{ |
| 238 |
"breakpoints": [{"line": 6}], |
| 239 |
"source": {"path": source}, |
| 240 |
"sourceModified": False, |
| 241 |
}, |
| 242 |
) |
| 243 |
|
| 244 |
wait_for_debug_request(kernel_with_debug, "configurationDone", full_reply=True) |
| 245 |
|
| 246 |
kernel_with_debug.execute(code) |
| 247 |
|
| 248 |
if not debugpy: |
| 249 |
# Cannot stop on breakpoint if debugpy not installed |
| 250 |
return |
| 251 |
|
| 252 |
# Wait for stop on breakpoint |
| 253 |
msg: dict = {"msg_type": "", "content": {}} |
| 254 |
while msg.get("msg_type") != "debug_event" or msg["content"].get("event") != "stopped": |
| 255 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 256 |
|
| 257 |
assert msg["content"]["body"]["reason"] == "breakpoint" |
| 258 |
|
| 259 |
|
| 260 |
def test_rich_inspect_not_at_breakpoint(kernel_with_debug): |
| 261 |
var_name = "text" |
| 262 |
value = "Hello the world" |
| 263 |
code = f"""{var_name}='{value}' |
| 264 |
print({var_name}) |
| 265 |
""" |
| 266 |
|
| 267 |
msg_id = kernel_with_debug.execute(code) |
| 268 |
get_reply(kernel_with_debug, msg_id) |
| 269 |
|
| 270 |
r = wait_for_debug_request(kernel_with_debug, "inspectVariables") |
| 271 |
|
| 272 |
def func(v): |
| 273 |
return v["name"] |
| 274 |
|
| 275 |
if debugpy: |
| 276 |
assert var_name in list(map(func, r["body"]["variables"])) |
| 277 |
else: |
| 278 |
assert r == {} |
| 279 |
|
| 280 |
reply = wait_for_debug_request( |
| 281 |
kernel_with_debug, |
| 282 |
"richInspectVariables", |
| 283 |
{"variableName": var_name}, |
| 284 |
) |
| 285 |
|
| 286 |
if debugpy: |
| 287 |
assert reply["body"]["data"] == {"text/plain": f"'{value}'"} |
| 288 |
else: |
| 289 |
assert reply == {} |
| 290 |
|
| 291 |
|
| 292 |
def test_rich_inspect_at_breakpoint(kernel_with_debug): |
| 293 |
code = """def f(a, b): |
| 294 |
c = a + b |
| 295 |
return c |
| 296 |
|
| 297 |
f(2, 3)""" |
| 298 |
|
| 299 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 300 |
if debugpy: |
| 301 |
source = r["body"]["sourcePath"] |
| 302 |
else: |
| 303 |
assert r == {} |
| 304 |
source = "some path" |
| 305 |
|
| 306 |
wait_for_debug_request( |
| 307 |
kernel_with_debug, |
| 308 |
"setBreakpoints", |
| 309 |
{ |
| 310 |
"breakpoints": [{"line": 2}], |
| 311 |
"source": {"path": source}, |
| 312 |
"sourceModified": False, |
| 313 |
}, |
| 314 |
) |
| 315 |
|
| 316 |
r = wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 317 |
|
| 318 |
r = wait_for_debug_request(kernel_with_debug, "configurationDone") |
| 319 |
|
| 320 |
kernel_with_debug.execute(code) |
| 321 |
|
| 322 |
if not debugpy: |
| 323 |
# Cannot stop on breakpoint if debugpy not installed |
| 324 |
return |
| 325 |
|
| 326 |
# Wait for stop on breakpoint |
| 327 |
msg: dict = {"msg_type": "", "content": {}} |
| 328 |
while msg.get("msg_type") != "debug_event" or msg["content"].get("event") != "stopped": |
| 329 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 330 |
|
| 331 |
stacks = wait_for_debug_request(kernel_with_debug, "stackTrace", {"threadId": 1})["body"][ |
| 332 |
"stackFrames" |
| 333 |
] |
| 334 |
|
| 335 |
scopes = wait_for_debug_request(kernel_with_debug, "scopes", {"frameId": stacks[0]["id"]})[ |
| 336 |
"body" |
| 337 |
]["scopes"] |
| 338 |
|
| 339 |
locals_ = wait_for_debug_request( |
| 340 |
kernel_with_debug, |
| 341 |
"variables", |
| 342 |
{ |
| 343 |
"variablesReference": next(filter(lambda s: s["name"] == "Locals", scopes))[ |
| 344 |
"variablesReference" |
| 345 |
] |
| 346 |
}, |
| 347 |
)["body"]["variables"] |
| 348 |
|
| 349 |
reply = wait_for_debug_request( |
| 350 |
kernel_with_debug, |
| 351 |
"richInspectVariables", |
| 352 |
{"variableName": locals_[0]["name"], "frameId": stacks[0]["id"]}, |
| 353 |
) |
| 354 |
|
| 355 |
assert reply["body"]["data"] == {"text/plain": locals_[0]["value"]} |
| 356 |
|
| 357 |
|
| 358 |
def test_convert_to_long_pathname(): |
| 359 |
if sys.platform == "win32": |
| 360 |
from ipykernel.compiler import _convert_to_long_pathname |
| 361 |
|
| 362 |
_convert_to_long_pathname(__file__) |
| 363 |
|
| 364 |
|
| 365 |
def test_copy_to_globals(kernel_with_debug): |
| 366 |
local_var_name = "var" |
| 367 |
global_var_name = "var_copy" |
| 368 |
code = f"""from IPython.core.display import HTML |
| 369 |
def my_test(): |
| 370 |
{local_var_name} = HTML('<p>test content</p>') |
| 371 |
pass |
| 372 |
a = 2 |
| 373 |
my_test()""" |
| 374 |
|
| 375 |
# Init debugger and set breakpoint |
| 376 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 377 |
if debugpy: |
| 378 |
source = r["body"]["sourcePath"] |
| 379 |
else: |
| 380 |
assert r == {} |
| 381 |
source = "some path" |
| 382 |
|
| 383 |
wait_for_debug_request( |
| 384 |
kernel_with_debug, |
| 385 |
"setBreakpoints", |
| 386 |
{ |
| 387 |
"breakpoints": [{"line": 4}], |
| 388 |
"source": {"path": source}, |
| 389 |
"sourceModified": False, |
| 390 |
}, |
| 391 |
) |
| 392 |
|
| 393 |
wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 394 |
|
| 395 |
wait_for_debug_request(kernel_with_debug, "configurationDone") |
| 396 |
|
| 397 |
# Execute code |
| 398 |
kernel_with_debug.execute(code) |
| 399 |
|
| 400 |
if not debugpy: |
| 401 |
# Cannot stop on breakpoint if debugpy not installed |
| 402 |
return |
| 403 |
|
| 404 |
# Wait for stop on breakpoint |
| 405 |
msg: dict = {"msg_type": "", "content": {}} |
| 406 |
while msg.get("msg_type") != "debug_event" or msg["content"].get("event") != "stopped": |
| 407 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 408 |
|
| 409 |
stacks = wait_for_debug_request(kernel_with_debug, "stackTrace", {"threadId": 1})["body"][ |
| 410 |
"stackFrames" |
| 411 |
] |
| 412 |
|
| 413 |
# Get local frame id |
| 414 |
frame_id = stacks[0]["id"] |
| 415 |
|
| 416 |
# Copy the variable |
| 417 |
wait_for_debug_request( |
| 418 |
kernel_with_debug, |
| 419 |
"copyToGlobals", |
| 420 |
{ |
| 421 |
"srcVariableName": local_var_name, |
| 422 |
"dstVariableName": global_var_name, |
| 423 |
"srcFrameId": frame_id, |
| 424 |
}, |
| 425 |
) |
| 426 |
|
| 427 |
# Get the scopes |
| 428 |
scopes = wait_for_debug_request(kernel_with_debug, "scopes", {"frameId": frame_id})["body"][ |
| 429 |
"scopes" |
| 430 |
] |
| 431 |
|
| 432 |
# Get the local variable |
| 433 |
locals_ = wait_for_debug_request( |
| 434 |
kernel_with_debug, |
| 435 |
"variables", |
| 436 |
{ |
| 437 |
"variablesReference": next(filter(lambda s: s["name"] == "Locals", scopes))[ |
| 438 |
"variablesReference" |
| 439 |
] |
| 440 |
}, |
| 441 |
)["body"]["variables"] |
| 442 |
|
| 443 |
local_var = None |
| 444 |
for variable in locals_: |
| 445 |
if local_var_name in variable["evaluateName"]: |
| 446 |
local_var = variable |
| 447 |
assert local_var is not None |
| 448 |
|
| 449 |
# Get the global variable (copy of the local variable) |
| 450 |
globals_ = wait_for_debug_request( |
| 451 |
kernel_with_debug, |
| 452 |
"variables", |
| 453 |
{ |
| 454 |
"variablesReference": next(filter(lambda s: s["name"] == "Globals", scopes))[ |
| 455 |
"variablesReference" |
| 456 |
] |
| 457 |
}, |
| 458 |
)["body"]["variables"] |
| 459 |
|
| 460 |
global_var = None |
| 461 |
for variable in globals_: |
| 462 |
if global_var_name in variable["evaluateName"]: |
| 463 |
global_var = variable |
| 464 |
assert global_var is not None |
| 465 |
|
| 466 |
# Compare local and global variable |
| 467 |
assert global_var["value"] == local_var["value"] and global_var["type"] == local_var["type"] # noqa: PT018 |
| 468 |
|
| 469 |
|
| 470 |
def test_debug_requests_sequential(kernel_with_debug): |
| 471 |
# Issue https://github.com/ipython/ipykernel/issues/1412 |
| 472 |
# Control channel requests should be executed sequentially not concurrently. |
| 473 |
code = """def f(a, b): |
| 474 |
c = a + b |
| 475 |
return c |
| 476 |
|
| 477 |
f(2, 3)""" |
| 478 |
|
| 479 |
r = wait_for_debug_request(kernel_with_debug, "dumpCell", {"code": code}) |
| 480 |
if debugpy: |
| 481 |
source = r["body"]["sourcePath"] |
| 482 |
else: |
| 483 |
assert r == {} |
| 484 |
source = "some path" |
| 485 |
|
| 486 |
wait_for_debug_request( |
| 487 |
kernel_with_debug, |
| 488 |
"setBreakpoints", |
| 489 |
{ |
| 490 |
"breakpoints": [{"line": 2}], |
| 491 |
"source": {"path": source}, |
| 492 |
"sourceModified": False, |
| 493 |
}, |
| 494 |
) |
| 495 |
|
| 496 |
wait_for_debug_request(kernel_with_debug, "debugInfo") |
| 497 |
wait_for_debug_request(kernel_with_debug, "configurationDone") |
| 498 |
kernel_with_debug.execute(code) |
| 499 |
|
| 500 |
if not debugpy: |
| 501 |
# Cannot stop on breakpoint if debugpy not installed |
| 502 |
return |
| 503 |
|
| 504 |
# Wait for stop on breakpoint |
| 505 |
msg: dict = {"msg_type": "", "content": {}} |
| 506 |
while msg.get("msg_type") != "debug_event" or msg["content"].get("event") != "stopped": |
| 507 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 508 |
|
| 509 |
stacks = wait_for_debug_request(kernel_with_debug, "stackTrace", {"threadId": 1})["body"][ |
| 510 |
"stackFrames" |
| 511 |
] |
| 512 |
|
| 513 |
scopes = wait_for_debug_request(kernel_with_debug, "scopes", {"frameId": stacks[0]["id"]})[ |
| 514 |
"body" |
| 515 |
]["scopes"] |
| 516 |
|
| 517 |
# Get variablesReference for both Locals and Globals. |
| 518 |
locals_ref = next(filter(lambda s: s["name"] == "Locals", scopes))["variablesReference"] |
| 519 |
globals_ref = next(filter(lambda s: s["name"] == "Globals", scopes))["variablesReference"] |
| 520 |
|
| 521 |
msgs = [] |
| 522 |
for ref in [locals_ref, globals_ref]: |
| 523 |
msgs.append( |
| 524 |
prepare_debug_request(kernel_with_debug, "variables", {"variablesReference": ref}) |
| 525 |
) |
| 526 |
|
| 527 |
# Send messages in quick succession. |
| 528 |
for msg in msgs: |
| 529 |
kernel_with_debug.control_channel.send(msg) |
| 530 |
|
| 531 |
replies = get_replies(kernel_with_debug, [msg["msg_id"] for msg in msgs], channel="control") |
| 532 |
|
| 533 |
# Check debug variable returns are correct. |
| 534 |
locals = replies[0]["content"] |
| 535 |
assert locals["success"] |
| 536 |
variables = locals["body"]["variables"] |
| 537 |
var = next(filter(lambda v: v["name"] == "a", variables)) |
| 538 |
assert var["type"] == "int" |
| 539 |
assert var["value"] == "2" |
| 540 |
var = next(filter(lambda v: v["name"] == "b", variables)) |
| 541 |
assert var["type"] == "int" |
| 542 |
assert var["value"] == "3" |
| 543 |
|
| 544 |
globals = replies[1]["content"] |
| 545 |
assert globals["success"] |
| 546 |
variables = globals["body"]["variables"] |
| 547 |
|
| 548 |
names = [v["name"] for v in variables] |
| 549 |
assert "function variables" in names |
| 550 |
assert "special variables" in names |
| 551 |
|
| 552 |
# Check status iopub messages alternate between busy and idle. |
| 553 |
execution_states = [] |
| 554 |
while len(execution_states) < 8: |
| 555 |
msg = kernel_with_debug.get_iopub_msg(timeout=TIMEOUT) |
| 556 |
if msg["msg_type"] == "status": |
| 557 |
execution_states.append(msg["content"]["execution_state"]) |
| 558 |
assert execution_states.count("busy") == 4 |
| 559 |
assert execution_states.count("idle") == 4 |
| 560 |
assert execution_states == ["busy", "idle"] * 4 |