changeset: 104466:31a2d270c0c3 branch: 3.5 parent: 104449:5fa74d8c987b user: Xavier de Gaye date: Wed Oct 12 20:13:24 2016 +0200 files: Lib/pdb.py Lib/test/test_pdb.py Misc/NEWS description: Issue #20766: Fix references leaked by pdb in the handling of SIGINT handlers. diff -r 5fa74d8c987b -r 31a2d270c0c3 Lib/pdb.py --- a/Lib/pdb.py Mon Oct 10 22:11:12 2016 -0500 +++ b/Lib/pdb.py Wed Oct 12 20:13:24 2016 +0200 @@ -134,6 +134,8 @@ class Pdb(bdb.Bdb, cmd.Cmd): + _previous_sigint_handler = None + def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False): bdb.Bdb.__init__(self, skip=skip) @@ -187,8 +189,6 @@ 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 reset(self): bdb.Bdb.reset(self) @@ -337,6 +337,10 @@ (expr, newvalue, oldvalue)) def interaction(self, frame, traceback): + # Restore the previous signal handler at the Pdb prompt. + if Pdb._previous_sigint_handler: + signal.signal(signal.SIGINT, Pdb._previous_sigint_handler) + Pdb._previous_sigint_handler = None if self.setup(frame, traceback): # no interaction desired at this time (happens if .pdbrc contains # a command like "continue") @@ -1037,7 +1041,7 @@ """ if not self.nosigint: try: - self._previous_sigint_handler = \ + Pdb._previous_sigint_handler = \ signal.signal(signal.SIGINT, self.sigint_handler) except ValueError: # ValueError happens when do_continue() is invoked from diff -r 5fa74d8c987b -r 31a2d270c0c3 Lib/test/test_pdb.py --- a/Lib/test/test_pdb.py Mon Oct 10 22:11:12 2016 -0500 +++ b/Lib/test/test_pdb.py Wed Oct 12 20:13:24 2016 +0200 @@ -911,6 +911,29 @@ (Pdb) continue """ +def test_pdb_issue_20766(): + """Test for reference leaks when the SIGINT handler is set. + + >>> def test_function(): + ... i = 1 + ... while i <= 2: + ... sess = pdb.Pdb() + ... sess.set_trace(sys._getframe()) + ... print('pdb %d: %s' % (i, sess._previous_sigint_handler)) + ... i += 1 + + >>> with PdbTestInput(['continue', + ... 'continue']): + ... test_function() + > (6)test_function() + -> print('pdb %d: %s' % (i, sess._previous_sigint_handler)) + (Pdb) continue + pdb 1: + > (5)test_function() + -> sess.set_trace(sys._getframe()) + (Pdb) continue + pdb 2: + """ class PdbTestCase(unittest.TestCase): diff -r 5fa74d8c987b -r 31a2d270c0c3 Misc/NEWS --- a/Misc/NEWS Mon Oct 10 22:11:12 2016 -0500 +++ b/Misc/NEWS Wed Oct 12 20:13:24 2016 +0200 @@ -107,6 +107,9 @@ Library ------- +- Issue #20766: Fix references leaked by pdb in the handling of SIGINT + handlers. + - Issue #26293: Fixed writing ZIP files that starts not from the start of the file. Offsets in ZIP file now are relative to the start of the archive in conforming to the specification.