| 1 |
import io |
| 2 |
import itertools |
| 3 |
import json |
| 4 |
import os |
| 5 |
import re |
| 6 |
import signal |
| 7 |
import socket |
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import textwrap |
| 11 |
import unittest |
| 12 |
import unittest.mock |
| 13 |
from contextlib import closing, contextmanager, redirect_stdout, redirect_stderr, ExitStack |
| 14 |
from test.support import is_wasi, cpython_only, force_color, requires_subprocess, SHORT_TIMEOUT, subTests |
| 15 |
from test.support.os_helper import TESTFN, unlink |
| 16 |
from typing import List |
| 17 |
|
| 18 |
import pdb |
| 19 |
from pdb import _PdbServer, _PdbClient |
| 20 |
|
| 21 |
|
| 22 |
if not sys.is_remote_debug_enabled(): |
| 23 |
raise unittest.SkipTest('remote debugging is disabled') |
| 24 |
|
| 25 |
|
| 26 |
@contextmanager |
| 27 |
def kill_on_error(proc): |
| 28 |
"""Context manager killing the subprocess if a Python exception is raised.""" |
| 29 |
with proc: |
| 30 |
try: |
| 31 |
yield proc |
| 32 |
except: |
| 33 |
proc.kill() |
| 34 |
raise |
| 35 |
|
| 36 |
|
| 37 |
class MockSocketFile: |
| 38 |
"""Mock socket file for testing _PdbServer without actual socket connections.""" |
| 39 |
|
| 40 |
def __init__(self): |
| 41 |
self.input_queue = [] |
| 42 |
self.output_buffer = [] |
| 43 |
|
| 44 |
def write(self, data: bytes) -> None: |
| 45 |
"""Simulate write to socket.""" |
| 46 |
self.output_buffer.append(data) |
| 47 |
|
| 48 |
def flush(self) -> None: |
| 49 |
"""No-op flush implementation.""" |
| 50 |
pass |
| 51 |
|
| 52 |
def readline(self) -> bytes: |
| 53 |
"""Read a line from the prepared input queue.""" |
| 54 |
if not self.input_queue: |
| 55 |
return b"" |
| 56 |
return self.input_queue.pop(0) |
| 57 |
|
| 58 |
def close(self) -> None: |
| 59 |
"""Close the mock socket file.""" |
| 60 |
pass |
| 61 |
|
| 62 |
def add_input(self, data: dict) -> None: |
| 63 |
"""Add input that will be returned by readline.""" |
| 64 |
self.input_queue.append(json.dumps(data).encode() + b"\n") |
| 65 |
|
| 66 |
def get_output(self) -> List[dict]: |
| 67 |
"""Get the output that was written by the object being tested.""" |
| 68 |
results = [] |
| 69 |
for data in self.output_buffer: |
| 70 |
if isinstance(data, bytes) and data.endswith(b"\n"): |
| 71 |
try: |
| 72 |
results.append(json.loads(data.decode().strip())) |
| 73 |
except json.JSONDecodeError: |
| 74 |
pass # Ignore non-JSON output |
| 75 |
self.output_buffer = [] |
| 76 |
return results |
| 77 |
|
| 78 |
|
| 79 |
class PdbClientTestCase(unittest.TestCase): |
| 80 |
"""Tests for the _PdbClient class.""" |
| 81 |
|
| 82 |
def do_test( |
| 83 |
self, |
| 84 |
*, |
| 85 |
incoming, |
| 86 |
simulate_send_failure=False, |
| 87 |
simulate_sigint_during_stdout_write=False, |
| 88 |
use_interrupt_socket=False, |
| 89 |
expected_outgoing=None, |
| 90 |
expected_outgoing_signals=None, |
| 91 |
expected_completions=None, |
| 92 |
expected_exception=None, |
| 93 |
expected_stdout="", |
| 94 |
expected_stdout_substring="", |
| 95 |
expected_state=None, |
| 96 |
): |
| 97 |
if expected_outgoing is None: |
| 98 |
expected_outgoing = [] |
| 99 |
if expected_outgoing_signals is None: |
| 100 |
expected_outgoing_signals = [] |
| 101 |
if expected_completions is None: |
| 102 |
expected_completions = [] |
| 103 |
if expected_state is None: |
| 104 |
expected_state = {} |
| 105 |
|
| 106 |
expected_state.setdefault("write_failed", False) |
| 107 |
messages = [m for source, m in incoming if source == "server"] |
| 108 |
prompts = [m["prompt"] for source, m in incoming if source == "user"] |
| 109 |
|
| 110 |
input_iter = (m for source, m in incoming if source == "user") |
| 111 |
completions = [] |
| 112 |
|
| 113 |
def mock_input(prompt): |
| 114 |
message = next(input_iter, None) |
| 115 |
if message is None: |
| 116 |
raise EOFError |
| 117 |
|
| 118 |
if req := message.get("completion_request"): |
| 119 |
readline_mock = unittest.mock.Mock() |
| 120 |
readline_mock.get_line_buffer.return_value = req["line"] |
| 121 |
readline_mock.get_begidx.return_value = req["begidx"] |
| 122 |
readline_mock.get_endidx.return_value = req["endidx"] |
| 123 |
unittest.mock.seal(readline_mock) |
| 124 |
with unittest.mock.patch.dict(sys.modules, {"readline": readline_mock}): |
| 125 |
for param in itertools.count(): |
| 126 |
prefix = req["line"][req["begidx"] : req["endidx"]] |
| 127 |
completion = client.complete(prefix, param) |
| 128 |
if completion is None: |
| 129 |
break |
| 130 |
completions.append(completion) |
| 131 |
|
| 132 |
reply = message["input"] |
| 133 |
if isinstance(reply, BaseException): |
| 134 |
raise reply |
| 135 |
if isinstance(reply, str): |
| 136 |
return reply |
| 137 |
return reply() |
| 138 |
|
| 139 |
with ExitStack() as stack: |
| 140 |
client_sock, server_sock = socket.socketpair() |
| 141 |
stack.enter_context(closing(client_sock)) |
| 142 |
stack.enter_context(closing(server_sock)) |
| 143 |
|
| 144 |
server_sock = unittest.mock.Mock(wraps=server_sock) |
| 145 |
|
| 146 |
client_sock.sendall( |
| 147 |
b"".join( |
| 148 |
(m if isinstance(m, bytes) else json.dumps(m).encode()) + b"\n" |
| 149 |
for m in messages |
| 150 |
) |
| 151 |
) |
| 152 |
client_sock.shutdown(socket.SHUT_WR) |
| 153 |
|
| 154 |
if simulate_send_failure: |
| 155 |
server_sock.sendall = unittest.mock.Mock( |
| 156 |
side_effect=OSError("sendall failed") |
| 157 |
) |
| 158 |
client_sock.shutdown(socket.SHUT_RD) |
| 159 |
|
| 160 |
stdout = io.StringIO() |
| 161 |
|
| 162 |
if simulate_sigint_during_stdout_write: |
| 163 |
orig_stdout_write = stdout.write |
| 164 |
|
| 165 |
def sigint_stdout_write(s): |
| 166 |
signal.raise_signal(signal.SIGINT) |
| 167 |
return orig_stdout_write(s) |
| 168 |
|
| 169 |
stdout.write = sigint_stdout_write |
| 170 |
|
| 171 |
input_mock = stack.enter_context( |
| 172 |
unittest.mock.patch("pdb.input", side_effect=mock_input) |
| 173 |
) |
| 174 |
stack.enter_context(redirect_stdout(stdout)) |
| 175 |
|
| 176 |
if use_interrupt_socket: |
| 177 |
interrupt_sock = unittest.mock.Mock(spec=socket.socket) |
| 178 |
mock_kill = None |
| 179 |
else: |
| 180 |
interrupt_sock = None |
| 181 |
mock_kill = stack.enter_context( |
| 182 |
unittest.mock.patch("os.kill", spec=os.kill) |
| 183 |
) |
| 184 |
|
| 185 |
client = _PdbClient( |
| 186 |
pid=12345, |
| 187 |
server_socket=server_sock, |
| 188 |
interrupt_sock=interrupt_sock, |
| 189 |
) |
| 190 |
|
| 191 |
if expected_exception is not None: |
| 192 |
exception = expected_exception["exception"] |
| 193 |
msg = expected_exception["msg"] |
| 194 |
stack.enter_context(self.assertRaises(exception, msg=msg)) |
| 195 |
|
| 196 |
client.cmdloop() |
| 197 |
|
| 198 |
sent_msgs = [msg.args[0] for msg in server_sock.sendall.mock_calls] |
| 199 |
for msg in sent_msgs: |
| 200 |
assert msg.endswith(b"\n") |
| 201 |
actual_outgoing = [json.loads(msg) for msg in sent_msgs] |
| 202 |
|
| 203 |
self.assertEqual(actual_outgoing, expected_outgoing) |
| 204 |
self.assertEqual(completions, expected_completions) |
| 205 |
if expected_stdout_substring and not expected_stdout: |
| 206 |
self.assertIn(expected_stdout_substring, stdout.getvalue()) |
| 207 |
else: |
| 208 |
self.assertEqual(stdout.getvalue(), expected_stdout) |
| 209 |
input_mock.assert_has_calls([unittest.mock.call(p) for p in prompts]) |
| 210 |
actual_state = {k: getattr(client, k) for k in expected_state} |
| 211 |
self.assertEqual(actual_state, expected_state) |
| 212 |
|
| 213 |
if use_interrupt_socket: |
| 214 |
outgoing_signals = [ |
| 215 |
signal.Signals(int.from_bytes(call.args[0])) |
| 216 |
for call in interrupt_sock.sendall.call_args_list |
| 217 |
] |
| 218 |
else: |
| 219 |
assert mock_kill is not None |
| 220 |
outgoing_signals = [] |
| 221 |
for call in mock_kill.call_args_list: |
| 222 |
pid, signum = call.args |
| 223 |
self.assertEqual(pid, 12345) |
| 224 |
outgoing_signals.append(signal.Signals(signum)) |
| 225 |
self.assertEqual(outgoing_signals, expected_outgoing_signals) |
| 226 |
|
| 227 |
def test_remote_immediately_closing_the_connection(self): |
| 228 |
"""Test the behavior when the remote closes the connection immediately.""" |
| 229 |
incoming = [] |
| 230 |
expected_outgoing = [] |
| 231 |
self.do_test( |
| 232 |
incoming=incoming, |
| 233 |
expected_outgoing=expected_outgoing, |
| 234 |
) |
| 235 |
|
| 236 |
def test_handling_command_list(self): |
| 237 |
"""Test handling the command_list message.""" |
| 238 |
incoming = [ |
| 239 |
("server", {"command_list": ["help", "list", "continue"]}), |
| 240 |
] |
| 241 |
self.do_test( |
| 242 |
incoming=incoming, |
| 243 |
expected_outgoing=[], |
| 244 |
expected_state={ |
| 245 |
"pdb_commands": {"help", "list", "continue"}, |
| 246 |
}, |
| 247 |
) |
| 248 |
|
| 249 |
def test_handling_info_message(self): |
| 250 |
"""Test handling a message payload with type='info'.""" |
| 251 |
incoming = [ |
| 252 |
("server", {"message": "Some message or other\n", "type": "info"}), |
| 253 |
] |
| 254 |
self.do_test( |
| 255 |
incoming=incoming, |
| 256 |
expected_outgoing=[], |
| 257 |
expected_stdout="Some message or other\n", |
| 258 |
) |
| 259 |
|
| 260 |
def test_handling_error_message(self): |
| 261 |
"""Test handling a message payload with type='error'.""" |
| 262 |
incoming = [ |
| 263 |
("server", {"message": "Some message or other.", "type": "error"}), |
| 264 |
] |
| 265 |
self.do_test( |
| 266 |
incoming=incoming, |
| 267 |
expected_outgoing=[], |
| 268 |
expected_stdout="*** Some message or other.\n", |
| 269 |
) |
| 270 |
|
| 271 |
def test_handling_other_message(self): |
| 272 |
"""Test handling a message payload with an unrecognized type.""" |
| 273 |
incoming = [ |
| 274 |
("server", {"message": "Some message.\n", "type": "unknown"}), |
| 275 |
] |
| 276 |
self.do_test( |
| 277 |
incoming=incoming, |
| 278 |
expected_outgoing=[], |
| 279 |
expected_stdout="Some message.\n", |
| 280 |
) |
| 281 |
|
| 282 |
@unittest.skipIf(sys.flags.optimize >= 2, "Help not available for -OO") |
| 283 |
@subTests( |
| 284 |
"help_request,expected_substring", |
| 285 |
[ |
| 286 |
# a request to display help for a command |
| 287 |
({"help": "ll"}, "Usage: ll | longlist"), |
| 288 |
# a request to display a help overview |
| 289 |
({"help": ""}, "type help <topic>"), |
| 290 |
# a request to display the full PDB manual |
| 291 |
({"help": "pdb"}, ">>> import pdb"), |
| 292 |
], |
| 293 |
) |
| 294 |
def test_handling_help_when_available(self, help_request, expected_substring): |
| 295 |
"""Test handling help requests when help is available.""" |
| 296 |
incoming = [ |
| 297 |
("server", help_request), |
| 298 |
] |
| 299 |
self.do_test( |
| 300 |
incoming=incoming, |
| 301 |
expected_outgoing=[], |
| 302 |
expected_stdout_substring=expected_substring, |
| 303 |
) |
| 304 |
|
| 305 |
@unittest.skipIf(sys.flags.optimize < 2, "Needs -OO") |
| 306 |
@subTests( |
| 307 |
"help_request,expected_substring", |
| 308 |
[ |
| 309 |
# a request to display help for a command |
| 310 |
({"help": "ll"}, "No help for 'll'"), |
| 311 |
# a request to display a help overview |
| 312 |
({"help": ""}, "Undocumented commands"), |
| 313 |
# a request to display the full PDB manual |
| 314 |
({"help": "pdb"}, "No help for 'pdb'"), |
| 315 |
], |
| 316 |
) |
| 317 |
def test_handling_help_when_not_available(self, help_request, expected_substring): |
| 318 |
"""Test handling help requests when help is not available.""" |
| 319 |
incoming = [ |
| 320 |
("server", help_request), |
| 321 |
] |
| 322 |
self.do_test( |
| 323 |
incoming=incoming, |
| 324 |
expected_outgoing=[], |
| 325 |
expected_stdout_substring=expected_substring, |
| 326 |
) |
| 327 |
|
| 328 |
def test_handling_pdb_prompts(self): |
| 329 |
"""Test responding to pdb's normal prompts.""" |
| 330 |
incoming = [ |
| 331 |
("server", {"command_list": ["b"]}), |
| 332 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 333 |
("user", {"prompt": "(Pdb) ", "input": "lst ["}), |
| 334 |
("user", {"prompt": "... ", "input": "0 ]"}), |
| 335 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 336 |
("user", {"prompt": "(Pdb) ", "input": ""}), |
| 337 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 338 |
("user", {"prompt": "(Pdb) ", "input": "b ["}), |
| 339 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 340 |
("user", {"prompt": "(Pdb) ", "input": "! b ["}), |
| 341 |
("user", {"prompt": "... ", "input": "b ]"}), |
| 342 |
] |
| 343 |
self.do_test( |
| 344 |
incoming=incoming, |
| 345 |
expected_outgoing=[ |
| 346 |
{"reply": "lst [\n0 ]"}, |
| 347 |
{"reply": ""}, |
| 348 |
{"reply": "b ["}, |
| 349 |
{"reply": "!b [\nb ]"}, |
| 350 |
], |
| 351 |
expected_state={"state": "pdb"}, |
| 352 |
) |
| 353 |
|
| 354 |
def test_handling_interact_prompts(self): |
| 355 |
"""Test responding to pdb's interact mode prompts.""" |
| 356 |
incoming = [ |
| 357 |
("server", {"command_list": ["b"]}), |
| 358 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 359 |
("user", {"prompt": ">>> ", "input": "lst ["}), |
| 360 |
("user", {"prompt": "... ", "input": "0 ]"}), |
| 361 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 362 |
("user", {"prompt": ">>> ", "input": ""}), |
| 363 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 364 |
("user", {"prompt": ">>> ", "input": "b ["}), |
| 365 |
("user", {"prompt": "... ", "input": "b ]"}), |
| 366 |
] |
| 367 |
self.do_test( |
| 368 |
incoming=incoming, |
| 369 |
expected_outgoing=[ |
| 370 |
{"reply": "lst [\n0 ]"}, |
| 371 |
{"reply": ""}, |
| 372 |
{"reply": "b [\nb ]"}, |
| 373 |
], |
| 374 |
expected_state={"state": "interact"}, |
| 375 |
) |
| 376 |
|
| 377 |
def test_retry_pdb_prompt_on_syntax_error(self): |
| 378 |
"""Test re-prompting after a SyntaxError in a Python expression.""" |
| 379 |
incoming = [ |
| 380 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 381 |
("user", {"prompt": "(Pdb) ", "input": " lst ["}), |
| 382 |
("user", {"prompt": "(Pdb) ", "input": "lst ["}), |
| 383 |
("user", {"prompt": "... ", "input": " 0 ]"}), |
| 384 |
] |
| 385 |
self.do_test( |
| 386 |
incoming=incoming, |
| 387 |
expected_outgoing=[ |
| 388 |
{"reply": "lst [\n 0 ]"}, |
| 389 |
], |
| 390 |
expected_stdout_substring="*** IndentationError", |
| 391 |
expected_state={"state": "pdb"}, |
| 392 |
) |
| 393 |
|
| 394 |
def test_retry_interact_prompt_on_syntax_error(self): |
| 395 |
"""Test re-prompting after a SyntaxError in a Python expression.""" |
| 396 |
incoming = [ |
| 397 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 398 |
("user", {"prompt": ">>> ", "input": "!lst ["}), |
| 399 |
("user", {"prompt": ">>> ", "input": "lst ["}), |
| 400 |
("user", {"prompt": "... ", "input": " 0 ]"}), |
| 401 |
] |
| 402 |
self.do_test( |
| 403 |
incoming=incoming, |
| 404 |
expected_outgoing=[ |
| 405 |
{"reply": "lst [\n 0 ]"}, |
| 406 |
], |
| 407 |
expected_stdout_substring="*** SyntaxError", |
| 408 |
expected_state={"state": "interact"}, |
| 409 |
) |
| 410 |
|
| 411 |
def test_handling_unrecognized_prompt_type(self): |
| 412 |
"""Test fallback to "dumb" single-line mode for unknown states.""" |
| 413 |
incoming = [ |
| 414 |
("server", {"prompt": "Do it? ", "state": "confirm"}), |
| 415 |
("user", {"prompt": "Do it? ", "input": "! ["}), |
| 416 |
("server", {"prompt": "Do it? ", "state": "confirm"}), |
| 417 |
("user", {"prompt": "Do it? ", "input": "echo hello"}), |
| 418 |
("server", {"prompt": "Do it? ", "state": "confirm"}), |
| 419 |
("user", {"prompt": "Do it? ", "input": ""}), |
| 420 |
("server", {"prompt": "Do it? ", "state": "confirm"}), |
| 421 |
("user", {"prompt": "Do it? ", "input": "echo goodbye"}), |
| 422 |
] |
| 423 |
self.do_test( |
| 424 |
incoming=incoming, |
| 425 |
expected_outgoing=[ |
| 426 |
{"reply": "! ["}, |
| 427 |
{"reply": "echo hello"}, |
| 428 |
{"reply": ""}, |
| 429 |
{"reply": "echo goodbye"}, |
| 430 |
], |
| 431 |
expected_state={"state": "dumb"}, |
| 432 |
) |
| 433 |
|
| 434 |
def test_sigint_at_prompt(self): |
| 435 |
"""Test signaling when a prompt gets interrupted.""" |
| 436 |
incoming = [ |
| 437 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 438 |
( |
| 439 |
"user", |
| 440 |
{ |
| 441 |
"prompt": "(Pdb) ", |
| 442 |
"input": lambda: signal.raise_signal(signal.SIGINT), |
| 443 |
}, |
| 444 |
), |
| 445 |
] |
| 446 |
self.do_test( |
| 447 |
incoming=incoming, |
| 448 |
expected_outgoing=[ |
| 449 |
{"signal": "INT"}, |
| 450 |
], |
| 451 |
expected_state={"state": "pdb"}, |
| 452 |
) |
| 453 |
|
| 454 |
def test_sigint_at_continuation_prompt(self): |
| 455 |
"""Test signaling when a continuation prompt gets interrupted.""" |
| 456 |
incoming = [ |
| 457 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 458 |
("user", {"prompt": "(Pdb) ", "input": "if True:"}), |
| 459 |
( |
| 460 |
"user", |
| 461 |
{ |
| 462 |
"prompt": "... ", |
| 463 |
"input": lambda: signal.raise_signal(signal.SIGINT), |
| 464 |
}, |
| 465 |
), |
| 466 |
] |
| 467 |
self.do_test( |
| 468 |
incoming=incoming, |
| 469 |
expected_outgoing=[ |
| 470 |
{"signal": "INT"}, |
| 471 |
], |
| 472 |
expected_state={"state": "pdb"}, |
| 473 |
) |
| 474 |
|
| 475 |
def test_sigint_when_writing(self): |
| 476 |
"""Test siginaling when sys.stdout.write() gets interrupted.""" |
| 477 |
incoming = [ |
| 478 |
("server", {"message": "Some message or other\n", "type": "info"}), |
| 479 |
] |
| 480 |
for use_interrupt_socket in [False, True]: |
| 481 |
with self.subTest(use_interrupt_socket=use_interrupt_socket): |
| 482 |
self.do_test( |
| 483 |
incoming=incoming, |
| 484 |
simulate_sigint_during_stdout_write=True, |
| 485 |
use_interrupt_socket=use_interrupt_socket, |
| 486 |
expected_outgoing=[], |
| 487 |
expected_outgoing_signals=[signal.SIGINT], |
| 488 |
expected_stdout="Some message or other\n", |
| 489 |
) |
| 490 |
|
| 491 |
def test_eof_at_prompt(self): |
| 492 |
"""Test signaling when a prompt gets an EOFError.""" |
| 493 |
incoming = [ |
| 494 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 495 |
("user", {"prompt": "(Pdb) ", "input": EOFError()}), |
| 496 |
] |
| 497 |
self.do_test( |
| 498 |
incoming=incoming, |
| 499 |
expected_outgoing=[ |
| 500 |
{"signal": "EOF"}, |
| 501 |
], |
| 502 |
expected_state={"state": "pdb"}, |
| 503 |
) |
| 504 |
|
| 505 |
def test_unrecognized_json_message(self): |
| 506 |
"""Test failing after getting an unrecognized payload.""" |
| 507 |
incoming = [ |
| 508 |
("server", {"monty": "python"}), |
| 509 |
("server", {"message": "Some message or other\n", "type": "info"}), |
| 510 |
] |
| 511 |
self.do_test( |
| 512 |
incoming=incoming, |
| 513 |
expected_outgoing=[], |
| 514 |
expected_exception={ |
| 515 |
"exception": RuntimeError, |
| 516 |
"msg": 'Unrecognized payload b\'{"monty": "python"}\'', |
| 517 |
}, |
| 518 |
) |
| 519 |
|
| 520 |
def test_continuing_after_getting_a_non_json_payload(self): |
| 521 |
"""Test continuing after getting a non JSON payload.""" |
| 522 |
incoming = [ |
| 523 |
("server", b"spam"), |
| 524 |
("server", {"message": "Something", "type": "info"}), |
| 525 |
] |
| 526 |
self.do_test( |
| 527 |
incoming=incoming, |
| 528 |
expected_outgoing=[], |
| 529 |
expected_stdout="\n".join( |
| 530 |
[ |
| 531 |
"*** Invalid JSON from remote: b'spam\\n'", |
| 532 |
"Something", |
| 533 |
] |
| 534 |
), |
| 535 |
) |
| 536 |
|
| 537 |
def test_write_failing(self): |
| 538 |
"""Test terminating if write fails due to a half closed socket.""" |
| 539 |
incoming = [ |
| 540 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 541 |
("user", {"prompt": "(Pdb) ", "input": KeyboardInterrupt()}), |
| 542 |
] |
| 543 |
self.do_test( |
| 544 |
incoming=incoming, |
| 545 |
expected_outgoing=[{"signal": "INT"}], |
| 546 |
simulate_send_failure=True, |
| 547 |
expected_state={"write_failed": True}, |
| 548 |
) |
| 549 |
|
| 550 |
def test_completion_in_pdb_state(self): |
| 551 |
"""Test requesting tab completions at a (Pdb) prompt.""" |
| 552 |
# GIVEN |
| 553 |
incoming = [ |
| 554 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 555 |
( |
| 556 |
"user", |
| 557 |
{ |
| 558 |
"prompt": "(Pdb) ", |
| 559 |
"completion_request": { |
| 560 |
"line": " mod._", |
| 561 |
"begidx": 8, |
| 562 |
"endidx": 9, |
| 563 |
}, |
| 564 |
"input": "print(\n mod.__name__)", |
| 565 |
}, |
| 566 |
), |
| 567 |
("server", {"completions": ["__name__", "__file__"]}), |
| 568 |
] |
| 569 |
self.do_test( |
| 570 |
incoming=incoming, |
| 571 |
expected_outgoing=[ |
| 572 |
{ |
| 573 |
"complete": { |
| 574 |
"text": "_", |
| 575 |
"line": "mod._", |
| 576 |
"begidx": 4, |
| 577 |
"endidx": 5, |
| 578 |
} |
| 579 |
}, |
| 580 |
{"reply": "print(\n mod.__name__)"}, |
| 581 |
], |
| 582 |
expected_completions=["__name__", "__file__"], |
| 583 |
expected_state={"state": "pdb"}, |
| 584 |
) |
| 585 |
|
| 586 |
def test_multiline_completion_in_pdb_state(self): |
| 587 |
"""Test requesting tab completions at a (Pdb) continuation prompt.""" |
| 588 |
# GIVEN |
| 589 |
incoming = [ |
| 590 |
("server", {"prompt": "(Pdb) ", "state": "pdb"}), |
| 591 |
("user", {"prompt": "(Pdb) ", "input": "if True:"}), |
| 592 |
( |
| 593 |
"user", |
| 594 |
{ |
| 595 |
"prompt": "... ", |
| 596 |
"completion_request": { |
| 597 |
"line": " b", |
| 598 |
"begidx": 4, |
| 599 |
"endidx": 5, |
| 600 |
}, |
| 601 |
"input": " bool()", |
| 602 |
}, |
| 603 |
), |
| 604 |
("server", {"completions": ["bin", "bool", "bytes"]}), |
| 605 |
("user", {"prompt": "... ", "input": ""}), |
| 606 |
] |
| 607 |
self.do_test( |
| 608 |
incoming=incoming, |
| 609 |
expected_outgoing=[ |
| 610 |
{ |
| 611 |
"complete": { |
| 612 |
"text": "b", |
| 613 |
"line": "! b", |
| 614 |
"begidx": 2, |
| 615 |
"endidx": 3, |
| 616 |
} |
| 617 |
}, |
| 618 |
{"reply": "if True:\n bool()\n"}, |
| 619 |
], |
| 620 |
expected_completions=["bin", "bool", "bytes"], |
| 621 |
expected_state={"state": "pdb"}, |
| 622 |
) |
| 623 |
|
| 624 |
def test_completion_in_interact_state(self): |
| 625 |
"""Test requesting tab completions at a >>> prompt.""" |
| 626 |
incoming = [ |
| 627 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 628 |
( |
| 629 |
"user", |
| 630 |
{ |
| 631 |
"prompt": ">>> ", |
| 632 |
"completion_request": { |
| 633 |
"line": " mod.__", |
| 634 |
"begidx": 8, |
| 635 |
"endidx": 10, |
| 636 |
}, |
| 637 |
"input": "print(\n mod.__name__)", |
| 638 |
}, |
| 639 |
), |
| 640 |
("server", {"completions": ["__name__", "__file__"]}), |
| 641 |
] |
| 642 |
self.do_test( |
| 643 |
incoming=incoming, |
| 644 |
expected_outgoing=[ |
| 645 |
{ |
| 646 |
"complete": { |
| 647 |
"text": "__", |
| 648 |
"line": "mod.__", |
| 649 |
"begidx": 4, |
| 650 |
"endidx": 6, |
| 651 |
} |
| 652 |
}, |
| 653 |
{"reply": "print(\n mod.__name__)"}, |
| 654 |
], |
| 655 |
expected_completions=["__name__", "__file__"], |
| 656 |
expected_state={"state": "interact"}, |
| 657 |
) |
| 658 |
|
| 659 |
def test_completion_in_unknown_state(self): |
| 660 |
"""Test requesting tab completions at an unrecognized prompt.""" |
| 661 |
incoming = [ |
| 662 |
("server", {"command_list": ["p"]}), |
| 663 |
("server", {"prompt": "Do it? ", "state": "confirm"}), |
| 664 |
( |
| 665 |
"user", |
| 666 |
{ |
| 667 |
"prompt": "Do it? ", |
| 668 |
"completion_request": { |
| 669 |
"line": "_", |
| 670 |
"begidx": 0, |
| 671 |
"endidx": 1, |
| 672 |
}, |
| 673 |
"input": "__name__", |
| 674 |
}, |
| 675 |
), |
| 676 |
] |
| 677 |
self.do_test( |
| 678 |
incoming=incoming, |
| 679 |
expected_outgoing=[ |
| 680 |
{"reply": "__name__"}, |
| 681 |
], |
| 682 |
expected_state={"state": "dumb"}, |
| 683 |
) |
| 684 |
|
| 685 |
def test_write_failure_during_completion(self): |
| 686 |
"""Test failing to write to the socket to request tab completions.""" |
| 687 |
incoming = [ |
| 688 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 689 |
( |
| 690 |
"user", |
| 691 |
{ |
| 692 |
"prompt": ">>> ", |
| 693 |
"completion_request": { |
| 694 |
"line": "xy", |
| 695 |
"begidx": 0, |
| 696 |
"endidx": 2, |
| 697 |
}, |
| 698 |
"input": "xyz", |
| 699 |
}, |
| 700 |
), |
| 701 |
] |
| 702 |
self.do_test( |
| 703 |
incoming=incoming, |
| 704 |
expected_outgoing=[ |
| 705 |
{ |
| 706 |
"complete": { |
| 707 |
"text": "xy", |
| 708 |
"line": "xy", |
| 709 |
"begidx": 0, |
| 710 |
"endidx": 2, |
| 711 |
} |
| 712 |
}, |
| 713 |
{"reply": "xyz"}, |
| 714 |
], |
| 715 |
simulate_send_failure=True, |
| 716 |
expected_completions=[], |
| 717 |
expected_state={"state": "interact", "write_failed": True}, |
| 718 |
) |
| 719 |
|
| 720 |
def test_read_failure_during_completion(self): |
| 721 |
"""Test failing to read tab completions from the socket.""" |
| 722 |
incoming = [ |
| 723 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 724 |
( |
| 725 |
"user", |
| 726 |
{ |
| 727 |
"prompt": ">>> ", |
| 728 |
"completion_request": { |
| 729 |
"line": "xy", |
| 730 |
"begidx": 0, |
| 731 |
"endidx": 2, |
| 732 |
}, |
| 733 |
"input": "xyz", |
| 734 |
}, |
| 735 |
), |
| 736 |
] |
| 737 |
self.do_test( |
| 738 |
incoming=incoming, |
| 739 |
expected_outgoing=[ |
| 740 |
{ |
| 741 |
"complete": { |
| 742 |
"text": "xy", |
| 743 |
"line": "xy", |
| 744 |
"begidx": 0, |
| 745 |
"endidx": 2, |
| 746 |
} |
| 747 |
}, |
| 748 |
{"reply": "xyz"}, |
| 749 |
], |
| 750 |
expected_completions=[], |
| 751 |
expected_state={"state": "interact"}, |
| 752 |
) |
| 753 |
|
| 754 |
def test_reading_invalid_json_during_completion(self): |
| 755 |
"""Test receiving invalid JSON when getting tab completions.""" |
| 756 |
incoming = [ |
| 757 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 758 |
( |
| 759 |
"user", |
| 760 |
{ |
| 761 |
"prompt": ">>> ", |
| 762 |
"completion_request": { |
| 763 |
"line": "xy", |
| 764 |
"begidx": 0, |
| 765 |
"endidx": 2, |
| 766 |
}, |
| 767 |
"input": "xyz", |
| 768 |
}, |
| 769 |
), |
| 770 |
("server", b'{"completions": '), |
| 771 |
("user", {"prompt": ">>> ", "input": "xyz"}), |
| 772 |
] |
| 773 |
self.do_test( |
| 774 |
incoming=incoming, |
| 775 |
expected_outgoing=[ |
| 776 |
{ |
| 777 |
"complete": { |
| 778 |
"text": "xy", |
| 779 |
"line": "xy", |
| 780 |
"begidx": 0, |
| 781 |
"endidx": 2, |
| 782 |
} |
| 783 |
}, |
| 784 |
{"reply": "xyz"}, |
| 785 |
], |
| 786 |
expected_stdout_substring="*** json.decoder.JSONDecodeError", |
| 787 |
expected_completions=[], |
| 788 |
expected_state={"state": "interact"}, |
| 789 |
) |
| 790 |
|
| 791 |
def test_reading_empty_json_during_completion(self): |
| 792 |
"""Test receiving an empty JSON object when getting tab completions.""" |
| 793 |
incoming = [ |
| 794 |
("server", {"prompt": ">>> ", "state": "interact"}), |
| 795 |
( |
| 796 |
"user", |
| 797 |
{ |
| 798 |
"prompt": ">>> ", |
| 799 |
"completion_request": { |
| 800 |
"line": "xy", |
| 801 |
"begidx": 0, |
| 802 |
"endidx": 2, |
| 803 |
}, |
| 804 |
"input": "xyz", |
| 805 |
}, |
| 806 |
), |
| 807 |
("server", {}), |
| 808 |
("user", {"prompt": ">>> ", "input": "xyz"}), |
| 809 |
] |
| 810 |
self.do_test( |
| 811 |
incoming=incoming, |
| 812 |
expected_outgoing=[ |
| 813 |
{ |
| 814 |
"complete": { |
| 815 |
"text": "xy", |
| 816 |
"line": "xy", |
| 817 |
"begidx": 0, |
| 818 |
"endidx": 2, |
| 819 |
} |
| 820 |
}, |
| 821 |
{"reply": "xyz"}, |
| 822 |
], |
| 823 |
expected_stdout=( |
| 824 |
"*** RuntimeError: Failed to get valid completions." |
| 825 |
" Got: {}\n" |
| 826 |
), |
| 827 |
expected_completions=[], |
| 828 |
expected_state={"state": "interact"}, |
| 829 |
) |
| 830 |
|
| 831 |
|
| 832 |
class RemotePdbTestCase(unittest.TestCase): |
| 833 |
"""Tests for the _PdbServer class.""" |
| 834 |
|
| 835 |
def setUp(self): |
| 836 |
self.sockfile = MockSocketFile() |
| 837 |
self.pdb = _PdbServer(self.sockfile) |
| 838 |
|
| 839 |
# Mock some Bdb attributes that are lazily created when tracing starts |
| 840 |
self.pdb.botframe = None |
| 841 |
self.pdb.quitting = False |
| 842 |
|
| 843 |
# Create a frame for testing |
| 844 |
self.test_globals = {'a': 1, 'b': 2, '__pdb_convenience_variables': {'x': 100}} |
| 845 |
self.test_locals = {'c': 3, 'd': 4} |
| 846 |
|
| 847 |
# Create a simple test frame |
| 848 |
frame_info = unittest.mock.Mock() |
| 849 |
frame_info.f_globals = self.test_globals |
| 850 |
frame_info.f_locals = self.test_locals |
| 851 |
frame_info.f_lineno = 42 |
| 852 |
frame_info.f_code = unittest.mock.Mock() |
| 853 |
frame_info.f_code.co_filename = "test_file.py" |
| 854 |
frame_info.f_code.co_name = "test_function" |
| 855 |
|
| 856 |
self.pdb.curframe = frame_info |
| 857 |
|
| 858 |
def test_message_and_error(self): |
| 859 |
"""Test message and error methods send correct JSON.""" |
| 860 |
self.pdb.message("Test message") |
| 861 |
self.pdb.error("Test error") |
| 862 |
|
| 863 |
outputs = self.sockfile.get_output() |
| 864 |
self.assertEqual(len(outputs), 2) |
| 865 |
self.assertEqual(outputs[0], {"message": "Test message\n", "type": "info"}) |
| 866 |
self.assertEqual(outputs[1], {"message": "Test error", "type": "error"}) |
| 867 |
|
| 868 |
def test_read_command(self): |
| 869 |
"""Test reading commands from the socket.""" |
| 870 |
# Add test input |
| 871 |
self.sockfile.add_input({"reply": "help"}) |
| 872 |
|
| 873 |
# Read the command |
| 874 |
cmd = self.pdb._read_reply() |
| 875 |
self.assertEqual(cmd, "help") |
| 876 |
|
| 877 |
def test_read_command_EOF(self): |
| 878 |
"""Test reading EOF command.""" |
| 879 |
# Simulate socket closure |
| 880 |
self.pdb._write_failed = True |
| 881 |
with self.assertRaises(EOFError): |
| 882 |
self.pdb._read_reply() |
| 883 |
|
| 884 |
def test_completion(self): |
| 885 |
"""Test handling completion requests.""" |
| 886 |
# Mock completenames to return specific values |
| 887 |
with unittest.mock.patch.object(self.pdb, 'completenames', |
| 888 |
return_value=["continue", "clear"]): |
| 889 |
|
| 890 |
# Add a completion request |
| 891 |
self.sockfile.add_input({ |
| 892 |
"complete": { |
| 893 |
"text": "c", |
| 894 |
"line": "c", |
| 895 |
"begidx": 0, |
| 896 |
"endidx": 1 |
| 897 |
} |
| 898 |
}) |
| 899 |
|
| 900 |
# Add a regular command to break the loop |
| 901 |
self.sockfile.add_input({"reply": "help"}) |
| 902 |
|
| 903 |
# Read command - this should process the completion request first |
| 904 |
cmd = self.pdb._read_reply() |
| 905 |
|
| 906 |
# Verify completion response was sent |
| 907 |
outputs = self.sockfile.get_output() |
| 908 |
self.assertEqual(len(outputs), 1) |
| 909 |
self.assertEqual(outputs[0], {"completions": ["continue", "clear"]}) |
| 910 |
|
| 911 |
# The actual command should be returned |
| 912 |
self.assertEqual(cmd, "help") |
| 913 |
|
| 914 |
def test_do_help(self): |
| 915 |
"""Test that do_help sends the help message.""" |
| 916 |
self.pdb.do_help("break") |
| 917 |
|
| 918 |
outputs = self.sockfile.get_output() |
| 919 |
self.assertEqual(len(outputs), 1) |
| 920 |
self.assertEqual(outputs[0], {"help": "break"}) |
| 921 |
|
| 922 |
def test_interact_mode(self): |
| 923 |
"""Test interaction mode setup and execution.""" |
| 924 |
# First set up interact mode |
| 925 |
self.pdb.do_interact("") |
| 926 |
|
| 927 |
# Verify _interact_state is properly initialized |
| 928 |
self.assertIsNotNone(self.pdb._interact_state) |
| 929 |
self.assertIsInstance(self.pdb._interact_state, dict) |
| 930 |
|
| 931 |
# Test running code in interact mode |
| 932 |
with unittest.mock.patch.object(self.pdb, '_error_exc') as mock_error: |
| 933 |
self.pdb._run_in_python_repl("print('test')") |
| 934 |
mock_error.assert_not_called() |
| 935 |
|
| 936 |
# Test with syntax error |
| 937 |
self.pdb._run_in_python_repl("if:") |
| 938 |
mock_error.assert_called_once() |
| 939 |
|
| 940 |
def test_registering_commands(self): |
| 941 |
"""Test registering breakpoint commands.""" |
| 942 |
# Mock get_bpbynumber |
| 943 |
with unittest.mock.patch.object(self.pdb, 'get_bpbynumber'): |
| 944 |
# Queue up some input to send |
| 945 |
self.sockfile.add_input({"reply": "commands 1"}) |
| 946 |
self.sockfile.add_input({"reply": "silent"}) |
| 947 |
self.sockfile.add_input({"reply": "print('hi')"}) |
| 948 |
self.sockfile.add_input({"reply": "end"}) |
| 949 |
self.sockfile.add_input({"signal": "EOF"}) |
| 950 |
|
| 951 |
# Run the PDB command loop |
| 952 |
self.pdb.cmdloop() |
| 953 |
|
| 954 |
outputs = self.sockfile.get_output() |
| 955 |
self.assertIn('command_list', outputs[0]) |
| 956 |
self.assertEqual(outputs[1], {"prompt": "(Pdb) ", "state": "pdb"}) |
| 957 |
self.assertEqual(outputs[2], {"prompt": "(com) ", "state": "commands"}) |
| 958 |
self.assertEqual(outputs[3], {"prompt": "(com) ", "state": "commands"}) |
| 959 |
self.assertEqual(outputs[4], {"prompt": "(com) ", "state": "commands"}) |
| 960 |
self.assertEqual(outputs[5], {"prompt": "(Pdb) ", "state": "pdb"}) |
| 961 |
self.assertEqual(outputs[6], {"message": "\n", "type": "info"}) |
| 962 |
self.assertEqual(len(outputs), 7) |
| 963 |
|
| 964 |
self.assertEqual( |
| 965 |
self.pdb.commands[1], |
| 966 |
["_pdbcmd_silence_frame_status", "print('hi')"], |
| 967 |
) |
| 968 |
|
| 969 |
def test_detach(self): |
| 970 |
"""Test the detach method.""" |
| 971 |
with unittest.mock.patch.object(self.sockfile, 'close') as mock_close: |
| 972 |
self.pdb.detach() |
| 973 |
mock_close.assert_called_once() |
| 974 |
self.assertFalse(self.pdb.quitting) |
| 975 |
|
| 976 |
def test_cmdloop(self): |
| 977 |
"""Test the command loop with various commands.""" |
| 978 |
# Mock onecmd to track command execution |
| 979 |
with unittest.mock.patch.object(self.pdb, 'onecmd', return_value=False) as mock_onecmd: |
| 980 |
# Add commands to the queue |
| 981 |
self.pdb.cmdqueue = ['help', 'list'] |
| 982 |
|
| 983 |
# Add a command from the socket for when cmdqueue is empty |
| 984 |
self.sockfile.add_input({"reply": "next"}) |
| 985 |
|
| 986 |
# Add a second command to break the loop |
| 987 |
self.sockfile.add_input({"reply": "quit"}) |
| 988 |
|
| 989 |
# Configure onecmd to exit the loop on "quit" |
| 990 |
def side_effect(line): |
| 991 |
return line == 'quit' |
| 992 |
mock_onecmd.side_effect = side_effect |
| 993 |
|
| 994 |
# Run the command loop |
| 995 |
self.pdb.quitting = False # Set this by hand because we don't want to really call set_trace() |
| 996 |
self.pdb.cmdloop() |
| 997 |
|
| 998 |
# Should have processed 4 commands: 2 from cmdqueue, 2 from socket |
| 999 |
self.assertEqual(mock_onecmd.call_count, 4) |
| 1000 |
mock_onecmd.assert_any_call('help') |
| 1001 |
mock_onecmd.assert_any_call('list') |
| 1002 |
mock_onecmd.assert_any_call('next') |
| 1003 |
mock_onecmd.assert_any_call('quit') |
| 1004 |
|
| 1005 |
# Check if prompt was sent to client |
| 1006 |
outputs = self.sockfile.get_output() |
| 1007 |
prompts = [o for o in outputs if 'prompt' in o] |
| 1008 |
self.assertEqual(len(prompts), 2) # Should have sent 2 prompts |
| 1009 |
|
| 1010 |
|
| 1011 |
@requires_subprocess() |
| 1012 |
@unittest.skipIf(is_wasi, "WASI does not support TCP sockets") |
| 1013 |
class PdbConnectTestCase(unittest.TestCase): |
| 1014 |
"""Tests for the _connect mechanism using direct socket communication.""" |
| 1015 |
|
| 1016 |
def setUp(self): |
| 1017 |
# Create a server socket that will wait for the debugger to connect |
| 1018 |
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1019 |
self.server_sock.bind(('127.0.0.1', 0)) # Let OS assign port |
| 1020 |
self.server_sock.listen(1) |
| 1021 |
self.port = self.server_sock.getsockname()[1] |
| 1022 |
|
| 1023 |
def _create_script(self, script=None): |
| 1024 |
# Create a file for subprocess script |
| 1025 |
if script is None: |
| 1026 |
script = textwrap.dedent( |
| 1027 |
f""" |
| 1028 |
import pdb |
| 1029 |
import sys |
| 1030 |
import time |
| 1031 |
|
| 1032 |
def foo(): |
| 1033 |
x = 42 |
| 1034 |
return bar() |
| 1035 |
|
| 1036 |
def bar(): |
| 1037 |
return 42 |
| 1038 |
|
| 1039 |
def connect_to_debugger(): |
| 1040 |
# Create a frame to debug |
| 1041 |
def dummy_function(): |
| 1042 |
x = 42 |
| 1043 |
# Call connect to establish connection |
| 1044 |
# with the test server |
| 1045 |
frame = sys._getframe() # Get the current frame |
| 1046 |
pdb._connect( |
| 1047 |
host='127.0.0.1', |
| 1048 |
port={self.port}, |
| 1049 |
frame=frame, |
| 1050 |
commands="", |
| 1051 |
version=pdb._PdbServer.protocol_version(), |
| 1052 |
signal_raising_thread=False, |
| 1053 |
colorize=False, |
| 1054 |
) |
| 1055 |
return x # This line won't be reached in debugging |
| 1056 |
|
| 1057 |
return dummy_function() |
| 1058 |
|
| 1059 |
result = connect_to_debugger() |
| 1060 |
foo() |
| 1061 |
print(f"Function returned: {{result}}") |
| 1062 |
""") |
| 1063 |
|
| 1064 |
self.script_path = TESTFN + "_connect_test.py" |
| 1065 |
with open(self.script_path, 'w') as f: |
| 1066 |
f.write(script) |
| 1067 |
|
| 1068 |
def tearDown(self): |
| 1069 |
self.server_sock.close() |
| 1070 |
try: |
| 1071 |
unlink(self.script_path) |
| 1072 |
except OSError: |
| 1073 |
pass |
| 1074 |
|
| 1075 |
def _connect_and_get_client_file(self): |
| 1076 |
"""Helper to start subprocess and get connected client file.""" |
| 1077 |
# Start the subprocess that will connect to our socket |
| 1078 |
process = subprocess.Popen( |
| 1079 |
[sys.executable, self.script_path], |
| 1080 |
stdout=subprocess.PIPE, |
| 1081 |
stderr=subprocess.PIPE, |
| 1082 |
text=True |
| 1083 |
) |
| 1084 |
|
| 1085 |
# Accept the connection from the subprocess |
| 1086 |
client_sock, _ = self.server_sock.accept() |
| 1087 |
client_file = client_sock.makefile('rwb') |
| 1088 |
self.addCleanup(client_file.close) |
| 1089 |
self.addCleanup(client_sock.close) |
| 1090 |
|
| 1091 |
return process, client_file |
| 1092 |
|
| 1093 |
def _read_until_prompt(self, client_file): |
| 1094 |
"""Helper to read messages until a prompt is received.""" |
| 1095 |
messages = [] |
| 1096 |
while True: |
| 1097 |
data = client_file.readline() |
| 1098 |
if not data: |
| 1099 |
break |
| 1100 |
msg = json.loads(data.decode()) |
| 1101 |
messages.append(msg) |
| 1102 |
if 'prompt' in msg: |
| 1103 |
break |
| 1104 |
return messages |
| 1105 |
|
| 1106 |
def _send_command(self, client_file, command): |
| 1107 |
"""Helper to send a command to the debugger.""" |
| 1108 |
client_file.write(json.dumps({"reply": command}).encode() + b"\n") |
| 1109 |
client_file.flush() |
| 1110 |
|
| 1111 |
def test_connect_and_basic_commands(self): |
| 1112 |
"""Test connecting to a remote debugger and sending basic commands.""" |
| 1113 |
self._create_script() |
| 1114 |
process, client_file = self._connect_and_get_client_file() |
| 1115 |
|
| 1116 |
with kill_on_error(process): |
| 1117 |
# We should receive initial data from the debugger |
| 1118 |
data = client_file.readline() |
| 1119 |
initial_data = json.loads(data.decode()) |
| 1120 |
self.assertIn('message', initial_data) |
| 1121 |
self.assertIn('pdb._connect', initial_data['message']) |
| 1122 |
|
| 1123 |
# First, look for command_list message |
| 1124 |
data = client_file.readline() |
| 1125 |
command_list = json.loads(data.decode()) |
| 1126 |
self.assertIn('command_list', command_list) |
| 1127 |
|
| 1128 |
# Then, look for the first prompt |
| 1129 |
data = client_file.readline() |
| 1130 |
prompt_data = json.loads(data.decode()) |
| 1131 |
self.assertIn('prompt', prompt_data) |
| 1132 |
self.assertEqual(prompt_data['state'], 'pdb') |
| 1133 |
|
| 1134 |
# Send 'bt' (backtrace) command |
| 1135 |
self._send_command(client_file, "bt") |
| 1136 |
|
| 1137 |
# Check for response - we should get some stack frames |
| 1138 |
messages = self._read_until_prompt(client_file) |
| 1139 |
|
| 1140 |
# Extract text messages containing stack info |
| 1141 |
text_msg = [msg['message'] for msg in messages |
| 1142 |
if 'message' in msg and 'connect_to_debugger' in msg['message']] |
| 1143 |
got_stack_info = bool(text_msg) |
| 1144 |
|
| 1145 |
expected_stacks = [ |
| 1146 |
"<module>", |
| 1147 |
"connect_to_debugger", |
| 1148 |
] |
| 1149 |
|
| 1150 |
for stack, msg in zip(expected_stacks, text_msg, strict=True): |
| 1151 |
self.assertIn(stack, msg) |
| 1152 |
|
| 1153 |
self.assertTrue(got_stack_info, "Should have received stack trace information") |
| 1154 |
|
| 1155 |
# Send 'c' (continue) command to let the program finish |
| 1156 |
self._send_command(client_file, "c") |
| 1157 |
|
| 1158 |
# Wait for process to finish |
| 1159 |
stdout, _ = process.communicate(timeout=SHORT_TIMEOUT) |
| 1160 |
|
| 1161 |
# Check if we got the expected output |
| 1162 |
self.assertIn("Function returned: 42", stdout) |
| 1163 |
self.assertEqual(process.returncode, 0) |
| 1164 |
|
| 1165 |
def test_breakpoints(self): |
| 1166 |
"""Test setting and hitting breakpoints.""" |
| 1167 |
self._create_script() |
| 1168 |
process, client_file = self._connect_and_get_client_file() |
| 1169 |
with kill_on_error(process): |
| 1170 |
# Skip initial messages until we get to the prompt |
| 1171 |
self._read_until_prompt(client_file) |
| 1172 |
|
| 1173 |
# Set a breakpoint at the return statement |
| 1174 |
self._send_command(client_file, "break bar") |
| 1175 |
messages = self._read_until_prompt(client_file) |
| 1176 |
bp_msg = next(msg['message'] for msg in messages if 'message' in msg) |
| 1177 |
self.assertIn("Breakpoint", bp_msg) |
| 1178 |
|
| 1179 |
# Continue execution until breakpoint |
| 1180 |
self._send_command(client_file, "c") |
| 1181 |
messages = self._read_until_prompt(client_file) |
| 1182 |
|
| 1183 |
# Verify we hit the breakpoint |
| 1184 |
hit_msg = next(msg['message'] for msg in messages if 'message' in msg) |
| 1185 |
self.assertIn("bar()", hit_msg) |
| 1186 |
|
| 1187 |
# Check breakpoint list |
| 1188 |
self._send_command(client_file, "b") |
| 1189 |
messages = self._read_until_prompt(client_file) |
| 1190 |
list_msg = next(msg['message'] for msg in reversed(messages) if 'message' in msg) |
| 1191 |
self.assertIn("1 breakpoint", list_msg) |
| 1192 |
self.assertIn("breakpoint already hit 1 time", list_msg) |
| 1193 |
|
| 1194 |
# Clear breakpoint |
| 1195 |
self._send_command(client_file, "clear 1") |
| 1196 |
messages = self._read_until_prompt(client_file) |
| 1197 |
clear_msg = next(msg['message'] for msg in reversed(messages) if 'message' in msg) |
| 1198 |
self.assertIn("Deleted breakpoint", clear_msg) |
| 1199 |
|
| 1200 |
# Continue to end |
| 1201 |
self._send_command(client_file, "c") |
| 1202 |
stdout, _ = process.communicate(timeout=SHORT_TIMEOUT) |
| 1203 |
|
| 1204 |
self.assertIn("Function returned: 42", stdout) |
| 1205 |
self.assertEqual(process.returncode, 0) |
| 1206 |
|
| 1207 |
def test_keyboard_interrupt(self): |
| 1208 |
"""Test that sending keyboard interrupt breaks into pdb.""" |
| 1209 |
|
| 1210 |
script = textwrap.dedent(f""" |
| 1211 |
import time |
| 1212 |
import sys |
| 1213 |
import socket |
| 1214 |
import pdb |
| 1215 |
def bar(): |
| 1216 |
frame = sys._getframe() # Get the current frame |
| 1217 |
pdb._connect( |
| 1218 |
host='127.0.0.1', |
| 1219 |
port={self.port}, |
| 1220 |
frame=frame, |
| 1221 |
commands="", |
| 1222 |
version=pdb._PdbServer.protocol_version(), |
| 1223 |
signal_raising_thread=True, |
| 1224 |
colorize=False, |
| 1225 |
) |
| 1226 |
print("Connected to debugger") |
| 1227 |
iterations = 50 |
| 1228 |
while iterations > 0: |
| 1229 |
print("Iteration", iterations, flush=True) |
| 1230 |
time.sleep(0.2) |
| 1231 |
iterations -= 1 |
| 1232 |
return 42 |
| 1233 |
|
| 1234 |
if __name__ == "__main__": |
| 1235 |
print("Function returned:", bar()) |
| 1236 |
""") |
| 1237 |
self._create_script(script=script) |
| 1238 |
process, client_file = self._connect_and_get_client_file() |
| 1239 |
|
| 1240 |
# Accept a 2nd connection from the subprocess to tell it about signals |
| 1241 |
signal_sock, _ = self.server_sock.accept() |
| 1242 |
self.addCleanup(signal_sock.close) |
| 1243 |
|
| 1244 |
with kill_on_error(process): |
| 1245 |
# Skip initial messages until we get to the prompt |
| 1246 |
self._read_until_prompt(client_file) |
| 1247 |
|
| 1248 |
# Continue execution |
| 1249 |
self._send_command(client_file, "c") |
| 1250 |
|
| 1251 |
# Confirm that the remote is already in the while loop. We know |
| 1252 |
# it's in bar() and we can exit the loop immediately by setting |
| 1253 |
# iterations to 0. |
| 1254 |
while line := process.stdout.readline(): |
| 1255 |
if line.startswith("Iteration"): |
| 1256 |
break |
| 1257 |
|
| 1258 |
# Inject a script to interrupt the running process |
| 1259 |
signal_sock.sendall(signal.SIGINT.to_bytes()) |
| 1260 |
messages = self._read_until_prompt(client_file) |
| 1261 |
|
| 1262 |
# Verify we got the keyboard interrupt message. |
| 1263 |
interrupt_msgs = [msg['message'] for msg in messages if 'message' in msg] |
| 1264 |
expected_msg = [msg for msg in interrupt_msgs if "bar()" in msg] |
| 1265 |
self.assertGreater(len(expected_msg), 0) |
| 1266 |
|
| 1267 |
# Continue to end as fast as we can |
| 1268 |
self._send_command(client_file, "iterations = 0") |
| 1269 |
self._send_command(client_file, "c") |
| 1270 |
stdout, _ = process.communicate(timeout=SHORT_TIMEOUT) |
| 1271 |
self.assertIn("Function returned: 42", stdout) |
| 1272 |
self.assertEqual(process.returncode, 0) |
| 1273 |
|
| 1274 |
def test_handle_eof(self): |
| 1275 |
"""Test that EOF signal properly exits the debugger.""" |
| 1276 |
self._create_script() |
| 1277 |
process, client_file = self._connect_and_get_client_file() |
| 1278 |
|
| 1279 |
with kill_on_error(process): |
| 1280 |
# Skip initial messages until we get to the prompt |
| 1281 |
self._read_until_prompt(client_file) |
| 1282 |
|
| 1283 |
# Send EOF signal to exit the debugger |
| 1284 |
client_file.write(json.dumps({"signal": "EOF"}).encode() + b"\n") |
| 1285 |
client_file.flush() |
| 1286 |
|
| 1287 |
# The process should complete normally after receiving EOF |
| 1288 |
stdout, stderr = process.communicate(timeout=SHORT_TIMEOUT) |
| 1289 |
|
| 1290 |
# Verify process completed correctly |
| 1291 |
self.assertIn("Function returned: 42", stdout) |
| 1292 |
self.assertEqual(process.returncode, 0) |
| 1293 |
self.assertEqual(stderr, "") |
| 1294 |
|
| 1295 |
def test_protocol_version(self): |
| 1296 |
"""Test that incompatible protocol versions are properly detected.""" |
| 1297 |
# Create a script using an incompatible protocol version |
| 1298 |
script = textwrap.dedent(f''' |
| 1299 |
import sys |
| 1300 |
import pdb |
| 1301 |
|
| 1302 |
def run_test(): |
| 1303 |
frame = sys._getframe() |
| 1304 |
|
| 1305 |
# Use a fake version number that's definitely incompatible |
| 1306 |
fake_version = 0x01010101 # A fake version that doesn't match any real Python version |
| 1307 |
|
| 1308 |
# Connect with the wrong version |
| 1309 |
pdb._connect( |
| 1310 |
host='127.0.0.1', |
| 1311 |
port={self.port}, |
| 1312 |
frame=frame, |
| 1313 |
commands="", |
| 1314 |
version=fake_version, |
| 1315 |
signal_raising_thread=False, |
| 1316 |
colorize=False, |
| 1317 |
) |
| 1318 |
|
| 1319 |
# This should print if the debugger detaches correctly |
| 1320 |
print("Debugger properly detected version mismatch") |
| 1321 |
return True |
| 1322 |
|
| 1323 |
if __name__ == "__main__": |
| 1324 |
print("Test result:", run_test()) |
| 1325 |
''') |
| 1326 |
self._create_script(script=script) |
| 1327 |
process, client_file = self._connect_and_get_client_file() |
| 1328 |
|
| 1329 |
with kill_on_error(process): |
| 1330 |
# First message should be an error about protocol version mismatch |
| 1331 |
data = client_file.readline() |
| 1332 |
message = json.loads(data.decode()) |
| 1333 |
|
| 1334 |
self.assertIn('message', message) |
| 1335 |
self.assertEqual(message['type'], 'error') |
| 1336 |
self.assertIn('incompatible', message['message']) |
| 1337 |
self.assertIn('protocol version', message['message']) |
| 1338 |
|
| 1339 |
# The process should complete normally |
| 1340 |
stdout, stderr = process.communicate(timeout=SHORT_TIMEOUT) |
| 1341 |
|
| 1342 |
# Verify the process completed successfully |
| 1343 |
self.assertIn("Test result: True", stdout) |
| 1344 |
self.assertIn("Debugger properly detected version mismatch", stdout) |
| 1345 |
self.assertEqual(process.returncode, 0) |
| 1346 |
|
| 1347 |
def test_help_system(self): |
| 1348 |
"""Test that the help system properly sends help text to the client.""" |
| 1349 |
self._create_script() |
| 1350 |
process, client_file = self._connect_and_get_client_file() |
| 1351 |
|
| 1352 |
with kill_on_error(process): |
| 1353 |
# Skip initial messages until we get to the prompt |
| 1354 |
self._read_until_prompt(client_file) |
| 1355 |
|
| 1356 |
# Request help for different commands |
| 1357 |
help_commands = ["help", "help break", "help continue", "help pdb"] |
| 1358 |
|
| 1359 |
for cmd in help_commands: |
| 1360 |
self._send_command(client_file, cmd) |
| 1361 |
|
| 1362 |
# Look for help message |
| 1363 |
data = client_file.readline() |
| 1364 |
message = json.loads(data.decode()) |
| 1365 |
|
| 1366 |
self.assertIn('help', message) |
| 1367 |
|
| 1368 |
if cmd == "help": |
| 1369 |
# Should just contain the command itself |
| 1370 |
self.assertEqual(message['help'], "") |
| 1371 |
else: |
| 1372 |
# Should contain the specific command we asked for help with |
| 1373 |
command = cmd.split()[1] |
| 1374 |
self.assertEqual(message['help'], command) |
| 1375 |
|
| 1376 |
# Skip to the next prompt |
| 1377 |
self._read_until_prompt(client_file) |
| 1378 |
|
| 1379 |
# Continue execution to finish the program |
| 1380 |
self._send_command(client_file, "c") |
| 1381 |
|
| 1382 |
stdout, stderr = process.communicate(timeout=SHORT_TIMEOUT) |
| 1383 |
self.assertIn("Function returned: 42", stdout) |
| 1384 |
self.assertEqual(process.returncode, 0) |
| 1385 |
|
| 1386 |
def test_multi_line_commands(self): |
| 1387 |
"""Test that multi-line commands work properly over remote connection.""" |
| 1388 |
self._create_script() |
| 1389 |
process, client_file = self._connect_and_get_client_file() |
| 1390 |
|
| 1391 |
with kill_on_error(process): |
| 1392 |
# Skip initial messages until we get to the prompt |
| 1393 |
self._read_until_prompt(client_file) |
| 1394 |
|
| 1395 |
# Send a multi-line command |
| 1396 |
multi_line_commands = [ |
| 1397 |
# Define a function |
| 1398 |
"def test_func():\n return 42", |
| 1399 |
|
| 1400 |
# For loop |
| 1401 |
"for i in range(3):\n print(i)", |
| 1402 |
|
| 1403 |
# If statement |
| 1404 |
"if True:\n x = 42\nelse:\n x = 0", |
| 1405 |
|
| 1406 |
# Try/except |
| 1407 |
"try:\n result = 10/2\n print(result)\nexcept ZeroDivisionError:\n print('Error')", |
| 1408 |
|
| 1409 |
# Class definition |
| 1410 |
"class TestClass:\n def __init__(self):\n self.value = 100\n def get_value(self):\n return self.value" |
| 1411 |
] |
| 1412 |
|
| 1413 |
for cmd in multi_line_commands: |
| 1414 |
self._send_command(client_file, cmd) |
| 1415 |
self._read_until_prompt(client_file) |
| 1416 |
|
| 1417 |
# Test executing the defined function |
| 1418 |
self._send_command(client_file, "test_func()") |
| 1419 |
messages = self._read_until_prompt(client_file) |
| 1420 |
|
| 1421 |
# Find the result message |
| 1422 |
result_msg = next(msg['message'] for msg in messages if 'message' in msg) |
| 1423 |
self.assertIn("42", result_msg) |
| 1424 |
|
| 1425 |
# Test creating an instance of the defined class |
| 1426 |
self._send_command(client_file, "obj = TestClass()") |
| 1427 |
self._read_until_prompt(client_file) |
| 1428 |
|
| 1429 |
# Test calling a method on the instance |
| 1430 |
self._send_command(client_file, "obj.get_value()") |
| 1431 |
messages = self._read_until_prompt(client_file) |
| 1432 |
|
| 1433 |
# Find the result message |
| 1434 |
result_msg = next(msg['message'] for msg in messages if 'message' in msg) |
| 1435 |
self.assertIn("100", result_msg) |
| 1436 |
|
| 1437 |
# Continue execution to finish |
| 1438 |
self._send_command(client_file, "c") |
| 1439 |
|
| 1440 |
stdout, stderr = process.communicate(timeout=SHORT_TIMEOUT) |
| 1441 |
self.assertIn("Function returned: 42", stdout) |
| 1442 |
self.assertEqual(process.returncode, 0) |
| 1443 |
|
| 1444 |
def test_exec_in_closure_result_uses_pdb_stdout(self): |
| 1445 |
""" |
| 1446 |
Expression results executed via _exec_in_closure() should be written |
| 1447 |
to the debugger output stream (pdb stdout), not to sys.stdout. |
| 1448 |
""" |
| 1449 |
self._create_script() |
| 1450 |
process, client_file = self._connect_and_get_client_file() |
| 1451 |
|
| 1452 |
with kill_on_error(process): |
| 1453 |
self._read_until_prompt(client_file) |
| 1454 |
|
| 1455 |
self._send_command(client_file, "(lambda: 123)()") |
| 1456 |
messages = self._read_until_prompt(client_file) |
| 1457 |
result_msg = "".join(msg.get("message", "") for msg in messages) |
| 1458 |
self.assertIn("123", result_msg) |
| 1459 |
|
| 1460 |
self._send_command(client_file, "sum(i for i in (1, 2, 3))") |
| 1461 |
messages = self._read_until_prompt(client_file) |
| 1462 |
result_msg = "".join(msg.get("message", "") for msg in messages) |
| 1463 |
self.assertIn("6", result_msg) |
| 1464 |
|
| 1465 |
self._send_command(client_file, "c") |
| 1466 |
stdout, _ = process.communicate(timeout=SHORT_TIMEOUT) |
| 1467 |
|
| 1468 |
self.assertNotIn("\n123\n", stdout) |
| 1469 |
self.assertNotIn("\n6\n", stdout) |
| 1470 |
self.assertEqual(process.returncode, 0) |
| 1471 |
|
| 1472 |
|
| 1473 |
def _supports_remote_attaching(): |
| 1474 |
PROCESS_VM_READV_SUPPORTED = False |
| 1475 |
|
| 1476 |
try: |
| 1477 |
from _remote_debugging import PROCESS_VM_READV_SUPPORTED |
| 1478 |
except ImportError: |
| 1479 |
pass |
| 1480 |
|
| 1481 |
return PROCESS_VM_READV_SUPPORTED |
| 1482 |
|
| 1483 |
|
| 1484 |
@unittest.skipIf(not sys.is_remote_debug_enabled(), "Remote debugging is not enabled") |
| 1485 |
@unittest.skipIf(sys.platform != "darwin" and sys.platform != "linux" and sys.platform != "win32", |
| 1486 |
"Test only runs on Linux, Windows and MacOS") |
| 1487 |
@unittest.skipIf(sys.platform == "linux" and not _supports_remote_attaching(), |
| 1488 |
"Testing on Linux requires process_vm_readv support") |
| 1489 |
@cpython_only |
| 1490 |
@requires_subprocess() |
| 1491 |
class PdbAttachTestCase(unittest.TestCase): |
| 1492 |
def setUp(self): |
| 1493 |
# Create a server socket that will wait for the debugger to connect |
| 1494 |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1495 |
self.sock.bind(('127.0.0.1', 0)) # Let OS assign port |
| 1496 |
self.sock.listen(1) |
| 1497 |
self.port = self.sock.getsockname()[1] |
| 1498 |
self._create_script() |
| 1499 |
|
| 1500 |
def _create_script(self, script=None): |
| 1501 |
# Create a file for subprocess script |
| 1502 |
script = textwrap.dedent( |
| 1503 |
f""" |
| 1504 |
import socket |
| 1505 |
import time |
| 1506 |
|
| 1507 |
def foo(): |
| 1508 |
return bar() |
| 1509 |
|
| 1510 |
def bar(): |
| 1511 |
return baz() |
| 1512 |
|
| 1513 |
def baz(): |
| 1514 |
x = 1 |
| 1515 |
# Trigger attach |
| 1516 |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 1517 |
sock.connect(('127.0.0.1', {self.port})) |
| 1518 |
sock.close() |
| 1519 |
count = 0 |
| 1520 |
while x == 1 and count < 100: |
| 1521 |
count += 1 |
| 1522 |
time.sleep(0.1) |
| 1523 |
return x |
| 1524 |
|
| 1525 |
result = foo() |
| 1526 |
print(f"Function returned: {{result}}") |
| 1527 |
""" |
| 1528 |
) |
| 1529 |
|
| 1530 |
self.script_path = TESTFN + "_connect_test.py" |
| 1531 |
with open(self.script_path, 'w') as f: |
| 1532 |
f.write(script) |
| 1533 |
|
| 1534 |
def tearDown(self): |
| 1535 |
self.sock.close() |
| 1536 |
try: |
| 1537 |
unlink(self.script_path) |
| 1538 |
except OSError: |
| 1539 |
pass |
| 1540 |
|
| 1541 |
def do_integration_test(self, client_stdin): |
| 1542 |
process = subprocess.Popen( |
| 1543 |
[sys.executable, self.script_path], |
| 1544 |
stdout=subprocess.PIPE, |
| 1545 |
stderr=subprocess.PIPE, |
| 1546 |
text=True |
| 1547 |
) |
| 1548 |
self.addCleanup(process.stdout.close) |
| 1549 |
self.addCleanup(process.stderr.close) |
| 1550 |
|
| 1551 |
# Wait for the process to reach our attachment point |
| 1552 |
self.sock.settimeout(10) |
| 1553 |
conn, _ = self.sock.accept() |
| 1554 |
conn.close() |
| 1555 |
|
| 1556 |
client_stdin = io.StringIO(client_stdin) |
| 1557 |
client_stdout = io.StringIO() |
| 1558 |
client_stderr = io.StringIO() |
| 1559 |
|
| 1560 |
self.addCleanup(client_stdin.close) |
| 1561 |
self.addCleanup(client_stdout.close) |
| 1562 |
self.addCleanup(client_stderr.close) |
| 1563 |
self.addCleanup(process.wait) |
| 1564 |
|
| 1565 |
with ( |
| 1566 |
unittest.mock.patch("sys.stdin", client_stdin), |
| 1567 |
redirect_stdout(client_stdout), |
| 1568 |
redirect_stderr(client_stderr), |
| 1569 |
unittest.mock.patch("sys.argv", ["pdb", "-p", str(process.pid)]), |
| 1570 |
unittest.mock.patch( |
| 1571 |
"pdb.exit_with_permission_help_text", side_effect=PermissionError |
| 1572 |
), |
| 1573 |
): |
| 1574 |
try: |
| 1575 |
pdb.main() |
| 1576 |
except PermissionError: |
| 1577 |
self.skipTest("Insufficient permissions for remote execution") |
| 1578 |
|
| 1579 |
process.wait() |
| 1580 |
server_stdout = process.stdout.read() |
| 1581 |
server_stderr = process.stderr.read() |
| 1582 |
|
| 1583 |
if process.returncode != 0: |
| 1584 |
print("server failed") |
| 1585 |
print(f"server stdout:\n{server_stdout}") |
| 1586 |
print(f"server stderr:\n{server_stderr}") |
| 1587 |
|
| 1588 |
self.assertEqual(process.returncode, 0) |
| 1589 |
return { |
| 1590 |
"client": { |
| 1591 |
"stdout": client_stdout.getvalue(), |
| 1592 |
"stderr": client_stderr.getvalue(), |
| 1593 |
}, |
| 1594 |
"server": { |
| 1595 |
"stdout": server_stdout, |
| 1596 |
"stderr": server_stderr, |
| 1597 |
}, |
| 1598 |
} |
| 1599 |
|
| 1600 |
def test_attach_to_process_without_colors(self): |
| 1601 |
with force_color(False): |
| 1602 |
output = self.do_integration_test("ll\nx=42\n") |
| 1603 |
self.assertEqual(output["client"]["stderr"], "") |
| 1604 |
self.assertEqual(output["server"]["stderr"], "") |
| 1605 |
|
| 1606 |
self.assertEqual(output["server"]["stdout"], "Function returned: 42\n") |
| 1607 |
self.assertIn("while x == 1", output["client"]["stdout"]) |
| 1608 |
self.assertNotIn("\x1b", output["client"]["stdout"]) |
| 1609 |
|
| 1610 |
def test_attach_to_process_with_colors(self): |
| 1611 |
with force_color(True): |
| 1612 |
output = self.do_integration_test("ll\nx=42\n") |
| 1613 |
self.assertEqual(output["client"]["stderr"], "") |
| 1614 |
self.assertEqual(output["server"]["stderr"], "") |
| 1615 |
|
| 1616 |
self.assertEqual(output["server"]["stdout"], "Function returned: 42\n") |
| 1617 |
self.assertIn("\x1b", output["client"]["stdout"]) |
| 1618 |
self.assertNotIn("while x == 1", output["client"]["stdout"]) |
| 1619 |
self.assertIn("while x == 1", re.sub("\x1b[^m]*m", "", output["client"]["stdout"])) |
| 1620 |
|
| 1621 |
if __name__ == "__main__": |
| 1622 |
unittest.main() |