@@ -469,12 +469,6 @@ def test_breakpoints(self):
469
469
470
470
def test_keyboard_interrupt (self ):
471
471
"""Test that sending keyboard interrupt breaks into pdb."""
472
- synchronizer_sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
473
- synchronizer_sock .bind (('127.0.0.1' , 0 )) # Let OS assign port
474
- synchronizer_sock .settimeout (SHORT_TIMEOUT )
475
- synchronizer_sock .listen (1 )
476
- self .addCleanup (synchronizer_sock .close )
477
- sync_port = synchronizer_sock .getsockname ()[1 ]
478
472
479
473
script = textwrap .dedent (f"""
480
474
import time
@@ -491,11 +485,10 @@ def bar():
491
485
version=pdb._PdbServer.protocol_version(),
492
486
)
493
487
print("Connected to debugger")
494
- iterations = 10
495
- socket.create_connection(('127.0.0.1', { sync_port } )).close()
488
+ iterations = 50
496
489
while iterations > 0:
497
- print("Iteration", iterations)
498
- time.sleep(1 )
490
+ print("Iteration", iterations, flush=True )
491
+ time.sleep(0.2 )
499
492
iterations -= 1
500
493
return 42
501
494
@@ -512,22 +505,20 @@ def bar():
512
505
# Continue execution
513
506
self ._send_command (client_file , "c" )
514
507
515
- # Wait until execution has continued
516
- synchronizer_sock .accept ()[0 ].close ()
517
-
518
- # Wait a bit so the remote leaves create_connection(). This is not
519
- # required but makes the rest of the test faster as we will exit the main
520
- # loop immediately by setting iterations to 0.
521
- 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
522
514
523
515
# Inject a script to interrupt the running process
524
516
self ._send_interrupt (process .pid )
525
517
messages = self ._read_until_prompt (client_file )
526
518
527
- # Verify we got the keyboard interrupt message. Is possible that we get interrupted somewhere
528
- # in bar() or when leving create_connection()
519
+ # Verify we got the keyboard interrupt message.
529
520
interrupt_msgs = [msg ['message' ] for msg in messages if 'message' in msg ]
530
- 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 ]
531
522
self .assertGreater (len (expected_msg ), 0 )
532
523
533
524
# Continue to end as fast as we can
0 commit comments