20
20
from pdb import _PdbServer , _PdbClient
21
21
22
22
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
+
23
34
class MockSocketFile :
24
35
"""Mock socket file for testing _PdbServer without actual socket connections."""
25
36
@@ -360,7 +371,7 @@ def test_connect_and_basic_commands(self):
360
371
self ._create_script ()
361
372
process , client_file = self ._connect_and_get_client_file ()
362
373
363
- with process :
374
+ with kill_on_error ( process ) :
364
375
# We should receive initial data from the debugger
365
376
data = client_file .readline ()
366
377
initial_data = json .loads (data .decode ())
@@ -413,7 +424,7 @@ def test_breakpoints(self):
413
424
"""Test setting and hitting breakpoints."""
414
425
self ._create_script ()
415
426
process , client_file = self ._connect_and_get_client_file ()
416
- with process :
427
+ with kill_on_error ( process ) :
417
428
# Skip initial messages until we get to the prompt
418
429
self ._read_until_prompt (client_file )
419
430
@@ -489,8 +500,7 @@ def bar():
489
500
self ._create_script (script = script )
490
501
process , client_file = self ._connect_and_get_client_file ()
491
502
492
- with process :
493
-
503
+ with kill_on_error (process ):
494
504
# Skip initial messages until we get to the prompt
495
505
self ._read_until_prompt (client_file )
496
506
@@ -520,7 +530,7 @@ def test_handle_eof(self):
520
530
self ._create_script ()
521
531
process , client_file = self ._connect_and_get_client_file ()
522
532
523
- with process :
533
+ with kill_on_error ( process ) :
524
534
# Skip initial messages until we get to the prompt
525
535
self ._read_until_prompt (client_file )
526
536
@@ -568,7 +578,7 @@ def run_test():
568
578
self ._create_script (script = script )
569
579
process , client_file = self ._connect_and_get_client_file ()
570
580
571
- with process :
581
+ with kill_on_error ( process ) :
572
582
# First message should be an error about protocol version mismatch
573
583
data = client_file .readline ()
574
584
message = json .loads (data .decode ())
@@ -591,7 +601,7 @@ def test_help_system(self):
591
601
self ._create_script ()
592
602
process , client_file = self ._connect_and_get_client_file ()
593
603
594
- with process :
604
+ with kill_on_error ( process ) :
595
605
# Skip initial messages until we get to the prompt
596
606
self ._read_until_prompt (client_file )
597
607
@@ -630,7 +640,7 @@ def test_multi_line_commands(self):
630
640
self ._create_script ()
631
641
process , client_file = self ._connect_and_get_client_file ()
632
642
633
- with process :
643
+ with kill_on_error ( process ) :
634
644
# Skip initial messages until we get to the prompt
635
645
self ._read_until_prompt (client_file )
636
646
0 commit comments