@@ -187,7 +187,7 @@ class Debugger(bdb.Bdb):
187
187
_current_debugger = []
188
188
189
189
def __init__ (self , stdin = None , stdout = None , term_size = None , steal_output = False ,
190
- _continue_at_start = False , ** kwargs ):
190
+ _continue_at_start = False , tty_file = None , ** kwargs ):
191
191
192
192
if Debugger ._current_debugger :
193
193
raise ValueError ("a Debugger instance already exists" )
@@ -197,6 +197,7 @@ def __init__(self, stdin=None, stdout=None, term_size=None, steal_output=False,
197
197
self .ui = DebuggerUI (self , stdin = stdin , stdout = stdout , term_size = term_size )
198
198
self .steal_output = steal_output
199
199
self ._continue_at_start__setting = _continue_at_start
200
+ self ._tty_file = tty_file
200
201
201
202
self .setup_state ()
202
203
@@ -214,8 +215,17 @@ def __init__(self, stdin=None, stdout=None, term_size=None, steal_output=False,
214
215
self ._current_debugger .append (self )
215
216
216
217
def __del__ (self ):
217
- assert self ._current_debugger == [self ]
218
- self ._current_debugger .pop ()
218
+ # according to https://stackoverflow.com/a/1481512/1054322, the garbage
219
+ # collector cannot be relied on to call this, so we call it explicitly
220
+ # in a finally (see __init__.py:runscript). But then, the garbage
221
+ # collector *might* call it, so it should tolerate being called twice.
222
+
223
+ if self ._current_debugger :
224
+ assert self ._current_debugger == [self ]
225
+ self ._current_debugger .pop ()
226
+ if self ._tty_file :
227
+ self ._tty_file .close ()
228
+ self ._tty_file = None
219
229
220
230
# These (dispatch_line and set_continue) are copied from bdb with the
221
231
# patch from https://bugs.python.org/issue16482 applied. See
0 commit comments