Merge pull request #315 from swarmer/separate-tty

Add ability to control pudb from a separate tty
This commit is contained in:
Andreas Klöckner 2018-10-01 23:26:41 -05:00 committed by GitHub
commit 3672990912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

View File

@ -43,6 +43,8 @@ Features
* PuDB places special emphasis on exception handling. A post-mortem mode makes
it easy to retrace a crashing program's last steps.
* Ability to control the debugger from a separate terminal.
* IPython integration (see `wiki <http://wiki.tiker.net/PuDB>`_)
* Should work with Python 2.6 and newer, including Python 3.

View File

@ -29,6 +29,32 @@ This is equivalent to::
which is useful if you want to run PuDB in a version of Python other than the
one you most recently installed PuDB with.
Debugging from a separate terminal
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It's possible to control the debugger from a separate terminal. This is useful
if there are several threads running that are printing to stdout while
you're debugging and messing up the terminal, or if you want to keep the
original terminal available for any other reason.
Open a new terminal. First, you need to get the path of the tty of the
terminal you want to debug from. To do that, use the standard unix
command `tty`. It will print something like `/dev/pts/3`.
Then you need to make sure that your terminal doesn't have a shell actively
reading and possibly capturing some of the input that should go to pudb.
To do that run a placeholder command that does nothing,
such as `perl -MPOSIX -e pause`.
Then set the PUDB_TTY environment variable to the path tty gave you,
for example::
PUDB_TTY=/dev/pts/3 pudb my-script.py
Now instead of using the current terminal, pudb will use this tty for its UI.
You may want to use the internal shell in pudb, as others will still use the
original terminal.
Remote debugging
^^^^^^^^^^^^^^^^

View File

@ -70,8 +70,33 @@ else:
CURRENT_DEBUGGER = []
def _tty_override():
import os
return os.environ.get('PUDB_TTY')
def _open_tty(tty_path):
import io, os, sys
if sys.version_info[0] == 2:
tty_file = open(tty_path, 'r+b', buffering=0)
term_size = None
else:
tty_file = io.TextIOWrapper(open(tty_path, 'r+b', buffering=0))
term_size = os.get_terminal_size(tty_file.fileno())
return tty_file, term_size
def _get_debugger(**kwargs):
if not CURRENT_DEBUGGER:
tty_path = _tty_override()
if tty_path and ('stdin' not in kwargs or 'stdout' not in kwargs):
tty_file, term_size = _open_tty(tty_path)
kwargs.setdefault('stdin', tty_file)
kwargs.setdefault('stdout', tty_file)
kwargs.setdefault('term_size', term_size)
from pudb.debugger import Debugger
dbg = Debugger(**kwargs)