]> granicus.if.org Git - python/commitdiff
Issue #20766: Fix references leaked by pdb in the handling of SIGINT handlers.
authorXavier de Gaye <xdegaye@users.sourceforge.net>
Wed, 12 Oct 2016 18:13:24 +0000 (20:13 +0200)
committerXavier de Gaye <xdegaye@users.sourceforge.net>
Wed, 12 Oct 2016 18:13:24 +0000 (20:13 +0200)
Lib/pdb.py
Lib/test/test_pdb.py
Misc/NEWS

index b11ac0abd1682e3833fca0f489486e6595a2c1fa..47972655f969fc5aa9aea496e73d99c671cc02e2 100755 (executable)
@@ -134,6 +134,8 @@ line_prefix = '\n-> '   # Probably a better default
 
 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 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         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 @@ class Pdb(bdb.Bdb, cmd.Cmd):
                                  (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 @@ class Pdb(bdb.Bdb, cmd.Cmd):
         """
         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
index 45ba5a96853b9576c7d80b4b8c3bb592323af807..66a7b5573c05847d2d68b166b59fa9994c83df20 100644 (file)
@@ -911,6 +911,29 @@ def test_pdb_next_command_subiterator():
     (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()
+    > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
+    -> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
+    (Pdb) continue
+    pdb 1: <built-in function default_int_handler>
+    > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
+    -> sess.set_trace(sys._getframe())
+    (Pdb) continue
+    pdb 2: <built-in function default_int_handler>
+    """
 
 class PdbTestCase(unittest.TestCase):
 
index 44e63d42f2d270a817f3980445fe38727353d1b5..28a7d944efbda9bda5859112cd5035d39a5b5d0b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -107,6 +107,9 @@ Core and Builtins
 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.