2121from pdb import _PdbServer , _PdbClient
2222
2323
24+ if not sys .is_remote_debug_enabled ():
25+ raise unittest .SkipTest ('remote debugging is disabled' )
26+
27+
2428@contextmanager
2529def kill_on_error (proc ):
2630 """Context manager killing the subprocess if a Python exception is raised."""
@@ -465,12 +469,6 @@ def test_breakpoints(self):
465469
466470 def test_keyboard_interrupt (self ):
467471 """Test that sending keyboard interrupt breaks into pdb."""
468- synchronizer_sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
469- synchronizer_sock .bind (('127.0.0.1' , 0 )) # Let OS assign port
470- synchronizer_sock .settimeout (SHORT_TIMEOUT )
471- synchronizer_sock .listen (1 )
472- self .addCleanup (synchronizer_sock .close )
473- sync_port = synchronizer_sock .getsockname ()[1 ]
474472
475473 script = textwrap .dedent (f"""
476474 import time
@@ -487,11 +485,10 @@ def bar():
487485 version=pdb._PdbServer.protocol_version(),
488486 )
489487 print("Connected to debugger")
490- iterations = 10
491- socket.create_connection(('127.0.0.1', { sync_port } )).close()
488+ iterations = 50
492489 while iterations > 0:
493- print("Iteration", iterations)
494- time.sleep(1 )
490+ print("Iteration", iterations, flush=True )
491+ time.sleep(0.2 )
495492 iterations -= 1
496493 return 42
497494
@@ -508,22 +505,20 @@ def bar():
508505 # Continue execution
509506 self ._send_command (client_file , "c" )
510507
511- # Wait until execution has continued
512- synchronizer_sock .accept ()[0 ].close ()
513-
514- # Wait a bit so the remote leaves create_connection(). This is not
515- # required but makes the rest of the test faster as we will exit the main
516- # loop immediately by setting iterations to 0.
517- time .sleep (0.1 )
508+ # Confirm that the remote is already in the while loop. We know
509+ # it's in bar() and we can exit the loop immediately by setting
510+ # iterations to 0.
511+ while line := process .stdout .readline ():
512+ if line .startswith ("Iteration" ):
513+ break
518514
519515 # Inject a script to interrupt the running process
520516 self ._send_interrupt (process .pid )
521517 messages = self ._read_until_prompt (client_file )
522518
523- # Verify we got the keyboard interrupt message. Is possible that we get interrupted somewhere
524- # in bar() or when leving create_connection()
519+ # Verify we got the keyboard interrupt message.
525520 interrupt_msgs = [msg ['message' ] for msg in messages if 'message' in msg ]
526- expected_msg = [msg for msg in interrupt_msgs if "bar()" in msg or "create_connection()" in msg ]
521+ expected_msg = [msg for msg in interrupt_msgs if "bar()" in msg ]
527522 self .assertGreater (len (expected_msg ), 0 )
528523
529524 # Continue to end as fast as we can
0 commit comments