1313import unittest .mock
1414from contextlib import contextmanager
1515from pathlib import Path
16- from test .support import is_wasi , os_helper
16+ from test .support import is_wasi , os_helper , SHORT_TIMEOUT
1717from test .support .os_helper import temp_dir , TESTFN , unlink
1818from typing import Dict , List , Optional , Tuple , Union , Any
1919
2020import pdb
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."""
@@ -415,7 +419,7 @@ def test_connect_and_basic_commands(self):
415419 self ._send_command (client_file , "c" )
416420
417421 # Wait for process to finish
418- stdout , _ = process .communicate (timeout = 5 )
422+ stdout , _ = process .communicate (timeout = SHORT_TIMEOUT )
419423
420424 # Check if we got the expected output
421425 self .assertIn ("Function returned: 42" , stdout )
@@ -458,19 +462,13 @@ def test_breakpoints(self):
458462
459463 # Continue to end
460464 self ._send_command (client_file , "c" )
461- stdout , _ = process .communicate (timeout = 5 )
465+ stdout , _ = process .communicate (timeout = SHORT_TIMEOUT )
462466
463467 self .assertIn ("Function returned: 42" , stdout )
464468 self .assertEqual (process .returncode , 0 )
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 (5 )
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,28 +505,26 @@ 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
530525 self ._send_command (client_file , "iterations = 0" )
531526 self ._send_command (client_file , "c" )
532- stdout , _ = process .communicate (timeout = 5 )
527+ stdout , _ = process .communicate (timeout = SHORT_TIMEOUT )
533528 self .assertIn ("Function returned: 42" , stdout )
534529 self .assertEqual (process .returncode , 0 )
535530
@@ -547,7 +542,7 @@ def test_handle_eof(self):
547542 client_file .flush ()
548543
549544 # The process should complete normally after receiving EOF
550- stdout , stderr = process .communicate (timeout = 5 )
545+ stdout , stderr = process .communicate (timeout = SHORT_TIMEOUT )
551546
552547 # Verify process completed correctly
553548 self .assertIn ("Function returned: 42" , stdout )
@@ -597,7 +592,7 @@ def run_test():
597592 self .assertIn ('protocol version' , message ['message' ])
598593
599594 # The process should complete normally
600- stdout , stderr = process .communicate (timeout = 5 )
595+ stdout , stderr = process .communicate (timeout = SHORT_TIMEOUT )
601596
602597 # Verify the process completed successfully
603598 self .assertIn ("Test result: True" , stdout )
@@ -639,7 +634,7 @@ def test_help_system(self):
639634 # Continue execution to finish the program
640635 self ._send_command (client_file , "c" )
641636
642- stdout , stderr = process .communicate (timeout = 5 )
637+ stdout , stderr = process .communicate (timeout = SHORT_TIMEOUT )
643638 self .assertIn ("Function returned: 42" , stdout )
644639 self .assertEqual (process .returncode , 0 )
645640
@@ -697,7 +692,7 @@ def test_multi_line_commands(self):
697692 # Continue execution to finish
698693 self ._send_command (client_file , "c" )
699694
700- stdout , stderr = process .communicate (timeout = 5 )
695+ stdout , stderr = process .communicate (timeout = SHORT_TIMEOUT )
701696 self .assertIn ("Function returned: 42" , stdout )
702697 self .assertEqual (process .returncode , 0 )
703698
0 commit comments