Skip to content

Commit

Permalink
Merge pull request #315 from swarmer/separate-tty
Browse files Browse the repository at this point in the history
Add ability to control pudb from a separate tty
  • Loading branch information
inducer committed Oct 2, 2018
2 parents 6f059ad + b51592f commit 3672990
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rst
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions doc/starting.rst
Expand Up @@ -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
^^^^^^^^^^^^^^^^

Expand Down
25 changes: 25 additions & 0 deletions pudb/__init__.py
Expand Up @@ -70,8 +70,33 @@ def go(self):
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)

Expand Down

0 comments on commit 3672990

Please sign in to comment.