@@ -222,11 +222,24 @@ def cmdfinalization_hook_exception(self, data: cmd2.plugin.CommandFinalizationDa
222222 self .called_cmdfinalization += 1
223223 raise ValueError
224224
225+ def cmdfinalization_hook_system_exit (self , data : cmd2 .plugin .CommandFinalizationData ) -> \
226+ cmd2 .plugin .CommandFinalizationData :
227+ """A command finalization hook which raises a SystemExit"""
228+ self .called_cmdfinalization += 1
229+ raise SystemExit
230+
231+ def cmdfinalization_hook_keyboard_interrupt (self , data : cmd2 .plugin .CommandFinalizationData ) -> \
232+ cmd2 .plugin .CommandFinalizationData :
233+ """A command finalization hook which raises a KeyboardInterrupt"""
234+ self .called_cmdfinalization += 1
235+ raise KeyboardInterrupt
236+
225237 def cmdfinalization_hook_not_enough_parameters (self ) -> plugin .CommandFinalizationData :
226238 """A command finalization hook with no parameters."""
227239 pass
228240
229- def cmdfinalization_hook_too_many_parameters (self , one : plugin .CommandFinalizationData , two : str ) -> plugin .CommandFinalizationData :
241+ def cmdfinalization_hook_too_many_parameters (self , one : plugin .CommandFinalizationData , two : str ) -> \
242+ plugin .CommandFinalizationData :
230243 """A command finalization hook with too many parameters."""
231244 return one
232245
@@ -851,6 +864,35 @@ def test_cmdfinalization_hook_exception(capsys):
851864 assert err
852865 assert app .called_cmdfinalization == 1
853866
867+ def test_cmdfinalization_hook_system_exit (capsys ):
868+ app = PluggedApp ()
869+ app .register_cmdfinalization_hook (app .cmdfinalization_hook_system_exit )
870+ stop = app .onecmd_plus_hooks ('say hello' )
871+ assert stop
872+ assert app .called_cmdfinalization == 1
873+
874+ def test_cmdfinalization_hook_keyboard_interrupt (capsys ):
875+ app = PluggedApp ()
876+ app .register_cmdfinalization_hook (app .cmdfinalization_hook_keyboard_interrupt )
877+
878+ # First make sure KeyboardInterrupt isn't raised unless told to
879+ stop = app .onecmd_plus_hooks ('say hello' , raise_keyboard_interrupt = False )
880+ assert not stop
881+ assert app .called_cmdfinalization == 1
882+
883+ # Now enable raising the KeyboardInterrupt
884+ app .reset_counters ()
885+ with pytest .raises (KeyboardInterrupt ):
886+ stop = app .onecmd_plus_hooks ('say hello' , raise_keyboard_interrupt = True )
887+ assert not stop
888+ assert app .called_cmdfinalization == 1
889+
890+ # Now make sure KeyboardInterrupt isn't raised if stop is already True
891+ app .reset_counters ()
892+ stop = app .onecmd_plus_hooks ('quit' , raise_keyboard_interrupt = True )
893+ assert stop
894+ assert app .called_cmdfinalization == 1
895+
854896def test_skip_postcmd_hooks (capsys ):
855897 app = PluggedApp ()
856898 app .register_postcmd_hook (app .postcmd_hook )
0 commit comments