Skip to content

Commit b9c8737

Browse files
MatrixManAtYrServiceinducer
authored andcommitted
Ensuring opened tty file is closed on exit
1 parent 440a5a7 commit b9c8737

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

Diff for: pudb/__init__.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def _get_debugger(**kwargs):
8383
kwargs.setdefault("stdin", tty_file)
8484
kwargs.setdefault("stdout", tty_file)
8585
kwargs.setdefault("term_size", term_size)
86+
kwargs.setdefault("tty_file", tty_file)
8687

8788
from pudb.debugger import Debugger
8889
dbg = Debugger(**kwargs)
@@ -111,12 +112,19 @@ def runmodule(*args, **kwargs):
111112
runscript(*args, **kwargs)
112113

113114

114-
def runscript(mainpyfile, args=None, pre_run="", steal_output=False,
115-
_continue_at_start=False, run_as_module=False):
116-
dbg = _get_debugger(
117-
steal_output=steal_output,
118-
_continue_at_start=_continue_at_start,
119-
)
115+
def runscript(mainpyfile, steal_output=False, _continue_at_start=False,
116+
**kwargs):
117+
try:
118+
dbg = _get_debugger(
119+
steal_output=steal_output,
120+
_continue_at_start=_continue_at_start,
121+
)
122+
_runscript(mainpyfile, dbg, **kwargs)
123+
finally:
124+
dbg.__del__()
125+
126+
127+
def _runscript(mainpyfile, dbg, args=None, pre_run="", run_as_module=False):
120128

121129
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was
122130
# modified by the script being debugged. It's a bad idea when it was

Diff for: pudb/debugger.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class Debugger(bdb.Bdb):
187187
_current_debugger = []
188188

189189
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):
191191

192192
if Debugger._current_debugger:
193193
raise ValueError("a Debugger instance already exists")
@@ -197,6 +197,7 @@ def __init__(self, stdin=None, stdout=None, term_size=None, steal_output=False,
197197
self.ui = DebuggerUI(self, stdin=stdin, stdout=stdout, term_size=term_size)
198198
self.steal_output = steal_output
199199
self._continue_at_start__setting = _continue_at_start
200+
self._tty_file = tty_file
200201

201202
self.setup_state()
202203

@@ -214,8 +215,17 @@ def __init__(self, stdin=None, stdout=None, term_size=None, steal_output=False,
214215
self._current_debugger.append(self)
215216

216217
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
219229

220230
# These (dispatch_line and set_continue) are copied from bdb with the
221231
# patch from https://bugs.python.org/issue16482 applied. See

0 commit comments

Comments
 (0)