diff --git a/Lib/pdb.py b/Lib/pdb.py --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -145,6 +145,7 @@ self.displaying = {} self.mainpyfile = '' self._wait_for_mainpyfile = False + self._previous_sigint_handler = None self.tb_lineno = {} # Try to load readline if it exists try: @@ -187,8 +188,17 @@ self.message("\nProgram interrupted. (Use 'cont' to resume).") self.set_step() self.set_trace(frame) - # restore previous signal handler - signal.signal(signal.SIGINT, self._previous_sigint_handler) + + def set_sigint_handler(self): + if not self.nosigint: + try: + self._previous_sigint_handler = \ + signal.signal(signal.SIGINT, self.sigint_handler) + except ValueError: + # ValueError happens when set_sigint_handler() is invoked from + # a non-main thread. Would printing a message here (once) make + # sense? + pass def reset(self): bdb.Bdb.reset(self) @@ -337,6 +347,9 @@ (expr, newvalue, oldvalue)) def interaction(self, frame, traceback): + # restore previous signal handler + if self._previous_sigint_handler: + signal.signal(signal.SIGINT, self._previous_sigint_handler) if self.setup(frame, traceback): # no interaction desired at this time (happens if .pdbrc contains # a command like "continue") @@ -984,6 +997,7 @@ else: lineno = None self.set_until(self.curframe, lineno) + self.set_sigint_handler() return 1 do_unt = do_until @@ -994,6 +1008,7 @@ function). """ self.set_step() + self.set_sigint_handler() return 1 do_s = do_step @@ -1003,6 +1018,7 @@ is reached or it returns. """ self.set_next(self.curframe) + self.set_sigint_handler() return 1 do_n = do_next @@ -1028,6 +1044,7 @@ Continue execution until the current function returns. """ self.set_return(self.curframe) + self.set_sigint_handler() return 1 do_r = do_return @@ -1035,17 +1052,8 @@ """c(ont(inue)) Continue execution, only stop when a breakpoint is encountered. """ - if not self.nosigint: - try: - self._previous_sigint_handler = \ - signal.signal(signal.SIGINT, self.sigint_handler) - except ValueError: - # ValueError happens when do_continue() is invoked from - # a non-main thread in which case we just continue without - # SIGINT set. Would printing a message here (once) make - # sense? - pass self.set_continue() + self.set_sigint_handler() return 1 do_c = do_cont = do_continue