2020from pdb import _PdbServer , _PdbClient
2121
2222
23+ @contextmanager
24+ def kill_on_error (proc ):
25+ """Context manager killing the subprocess if a Python exception is raised."""
26+ with proc :
27+ try :
28+ yield proc
29+ except :
30+ proc .kill ()
31+ raise
32+
33+
2334class MockSocketFile :
2435 """Mock socket file for testing _PdbServer without actual socket connections."""
2536
@@ -360,7 +371,7 @@ def test_connect_and_basic_commands(self):
360371 self ._create_script ()
361372 process , client_file = self ._connect_and_get_client_file ()
362373
363- with process :
374+ with kill_on_error ( process ) :
364375 # We should receive initial data from the debugger
365376 data = client_file .readline ()
366377 initial_data = json .loads (data .decode ())
@@ -413,7 +424,7 @@ def test_breakpoints(self):
413424 """Test setting and hitting breakpoints."""
414425 self ._create_script ()
415426 process , client_file = self ._connect_and_get_client_file ()
416- with process :
427+ with kill_on_error ( process ) :
417428 # Skip initial messages until we get to the prompt
418429 self ._read_until_prompt (client_file )
419430
@@ -489,8 +500,7 @@ def bar():
489500 self ._create_script (script = script )
490501 process , client_file = self ._connect_and_get_client_file ()
491502
492- with process :
493-
503+ with kill_on_error (process ):
494504 # Skip initial messages until we get to the prompt
495505 self ._read_until_prompt (client_file )
496506
@@ -520,7 +530,7 @@ def test_handle_eof(self):
520530 self ._create_script ()
521531 process , client_file = self ._connect_and_get_client_file ()
522532
523- with process :
533+ with kill_on_error ( process ) :
524534 # Skip initial messages until we get to the prompt
525535 self ._read_until_prompt (client_file )
526536
@@ -568,7 +578,7 @@ def run_test():
568578 self ._create_script (script = script )
569579 process , client_file = self ._connect_and_get_client_file ()
570580
571- with process :
581+ with kill_on_error ( process ) :
572582 # First message should be an error about protocol version mismatch
573583 data = client_file .readline ()
574584 message = json .loads (data .decode ())
@@ -591,7 +601,7 @@ def test_help_system(self):
591601 self ._create_script ()
592602 process , client_file = self ._connect_and_get_client_file ()
593603
594- with process :
604+ with kill_on_error ( process ) :
595605 # Skip initial messages until we get to the prompt
596606 self ._read_until_prompt (client_file )
597607
@@ -630,7 +640,7 @@ def test_multi_line_commands(self):
630640 self ._create_script ()
631641 process , client_file = self ._connect_and_get_client_file ()
632642
633- with process :
643+ with kill_on_error ( process ) :
634644 # Skip initial messages until we get to the prompt
635645 self ._read_until_prompt (client_file )
636646
0 commit comments