Skip to content

Commit 10e54ae

Browse files
author
Xavier de Gaye
committed
Issue #20766: Fix references leaked by pdb in the handling of SIGINT handlers.
1 parent fd28cbe commit 10e54ae

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

‎Lib/pdb.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ def __repr__(self):
134134

135135
class Pdb(bdb.Bdb, cmd.Cmd):
136136

137+
_previous_sigint_handler = None
138+
137139
def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
138140
nosigint=False):
139141
bdb.Bdb.__init__(self, skip=skip)
@@ -187,8 +189,6 @@ def sigint_handler(self, signum, frame):
187189
self.message("\nProgram interrupted. (Use 'cont' to resume).")
188190
self.set_step()
189191
self.set_trace(frame)
190-
# restore previous signal handler
191-
signal.signal(signal.SIGINT, self._previous_sigint_handler)
192192

193193
def reset(self):
194194
bdb.Bdb.reset(self)
@@ -337,6 +337,10 @@ def preloop(self):
337337
(expr, newvalue, oldvalue))
338338

339339
def interaction(self, frame, traceback):
340+
# Restore the previous signal handler at the Pdb prompt.
341+
if Pdb._previous_sigint_handler:
342+
signal.signal(signal.SIGINT, Pdb._previous_sigint_handler)
343+
Pdb._previous_sigint_handler = None
340344
if self.setup(frame, traceback):
341345
# no interaction desired at this time (happens if .pdbrc contains
342346
# a command like "continue")
@@ -1037,7 +1041,7 @@ def do_continue(self, arg):
10371041
"""
10381042
if not self.nosigint:
10391043
try:
1040-
self._previous_sigint_handler = \
1044+
Pdb._previous_sigint_handler = \
10411045
signal.signal(signal.SIGINT, self.sigint_handler)
10421046
except ValueError:
10431047
# ValueError happens when do_continue() is invoked from

‎Lib/test/test_pdb.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,29 @@ def test_pdb_next_command_subiterator():
911911
(Pdb) continue
912912
"""
913913

914+
def test_pdb_issue_20766():
915+
"""Test for reference leaks when the SIGINT handler is set.
916+
917+
>>> def test_function():
918+
... i = 1
919+
... while i <= 2:
920+
... sess = pdb.Pdb()
921+
... sess.set_trace(sys._getframe())
922+
... print('pdb %d: %s' % (i, sess._previous_sigint_handler))
923+
... i += 1
924+
925+
>>> with PdbTestInput(['continue',
926+
... 'continue']):
927+
... test_function()
928+
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function()
929+
-> print('pdb %d: %s' % (i, sess._previous_sigint_handler))
930+
(Pdb) continue
931+
pdb 1: <built-in function default_int_handler>
932+
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function()
933+
-> sess.set_trace(sys._getframe())
934+
(Pdb) continue
935+
pdb 2: <built-in function default_int_handler>
936+
"""
914937

915938
class PdbTestCase(unittest.TestCase):
916939

‎Misc/NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ Core and Builtins
107107
Library
108108
-------
109109

110+
- Issue #20766: Fix references leaked by pdb in the handling of SIGINT
111+
handlers.
112+
110113
- Issue #26293: Fixed writing ZIP files that starts not from the start of the
111114
file. Offsets in ZIP file now are relative to the start of the archive in
112115
conforming to the specification.

0 commit comments

Comments
 (0)