MCPcopy Index your code
hub / github.com/python/cpython / test_keyboard_interrupt

Method test_keyboard_interrupt

Lib/test/test_remote_pdb.py:1207–1272  ·  view source on GitHub ↗

Test that sending keyboard interrupt breaks into pdb.

(self)

Source from the content-addressed store, hash-verified

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]

Callers

nothing calls this directly

Calls 15

_create_scriptMethod · 0.95
_read_until_promptMethod · 0.95
_send_commandMethod · 0.95
addCleanupMethod · 0.80
assertGreaterMethod · 0.80
assertInMethod · 0.80
kill_on_errorFunction · 0.70
dedentMethod · 0.45
acceptMethod · 0.45
readlineMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected