Initial commands are read from .pdbrc files in your home directory andin the current directory, if they exist. Commands supplied with -c are executed after commands from .pdbrc files.
To let the script run until an exception occurs, use "-c continue". To let the script run up to a given line X in the debugged file, use "-c 'until X'" ipdb version 0.10.3.
# __init__.py # Copyright (c) 2007-2016 Godefroid Chapelle and ipdb development team # # This file is part of ipdb. # Redistributable under the revised BSD license # https://opensource.org/licenses/BSD-3-Clause
from ipdb.__main__ import set_trace, post_mortem, pm, run # noqa from ipdb.__main__ import runcall, runeval, launch_ipdb_on_exception # noqa
from ipdb.stdout import sset_trace, spost_mortem, spm # noqa from ipdb.stdout import slaunch_ipdb_on_exception # noqa
# Copyright (c) 2011-2016 Godefroid Chapelle and ipdb development team # # This file is part of ipdb. # Redistributable under the revised BSD license # https://opensource.org/licenses/BSD-3-Clause
from IPython.terminal.embed import InteractiveShellEmbed from IPython.terminal.ipapp import TerminalIPythonApp from IPython.core.debugger import BdbQuit_excepthook from IPython import get_ipython import os import sys
from contextlib import contextmanager
__version__ = "0.10.3"
shell = get_ipython() if shell isNone: # Not inside IPython # Build a terminal app in order to force ipython to load the # configuration ipapp = TerminalIPythonApp() # Avoid output (banner, prints) ipapp.interact = False ipapp.initialize([]) shell = ipapp.shell else: # Running inside IPython
# Detect if embed shell or not and display a message if isinstance(shell, InteractiveShellEmbed): sys.stderr.write( "\nYou are currently into an embedded ipython shell,\n" "the configuration will not be loaded.\n\n" )
# Let IPython decide about which debugger class to use # This is especially important for tools that fiddle with stdout debugger_cls = shell.debugger_cls def_colors = shell.colors
def_init_pdb(context=3, commands=[]): try: p = debugger_cls(def_colors, context=context) except TypeError: p = debugger_cls(def_colors) p.rcLines.extend(commands) return p
defwrap_sys_excepthook(): # make sure we wrap it only once or we would end up with a cycle # BdbQuit_excepthook.excepthook_ori == BdbQuit_excepthook if sys.excepthook != BdbQuit_excepthook: BdbQuit_excepthook.excepthook_ori = sys.excepthook sys.excepthook = BdbQuit_excepthook
defset_trace(frame=None, context=3): wrap_sys_excepthook() if frame isNone: frame = sys._getframe().f_back p = _init_pdb(context).set_trace(frame) if p and hasattr(p, 'shell'): p.shell.restore_sys_module_state()
defpost_mortem(tb=None): wrap_sys_excepthook() p = _init_pdb() p.reset() if tb isNone: # sys.exc_info() returns (type, value, traceback) if an exception is # being handled, otherwise it returns None tb = sys.exc_info()[2] if tb: p.interaction(None, tb)
@contextmanager deflaunch_ipdb_on_exception(): try: yield except Exception: e, m, tb = sys.exc_info() print(m.__repr__(), file=sys.stderr) post_mortem(tb) finally: pass
_usage = """\ usage: python -m ipdb [-c command] ... pyfile [arg] ... Debug the Python program given by pyfile. Initial commands are read from .pdbrc files in your home directory and in the current directory, if they exist. Commands supplied with -c are executed after commands from .pdbrc files. To let the script run until an exception occurs, use "-c continue". To let the script run up to a given line X in the debugged file, use "-c 'until X'" ipdb version %s.""" % __version__
commands = [] for opt, optarg in opts: if opt in ['-h', '--help']: print(_usage) sys.exit() elif opt in ['-c', '--command']: commands.append(optarg)
mainpyfile = args[0] # Get script filename ifnot os.path.exists(mainpyfile): print('Error:', mainpyfile, 'does not exist') sys.exit(1)
sys.argv = args # Hide "pdb.py" from argument list
# Replace pdb's dir with script's dir in front of module search path. sys.path[0] = os.path.dirname(mainpyfile)
# Note on saving/restoring sys.argv: it's a good idea when sys.argv was # modified by the script being debugged. It's a bad idea when it was # changed by the user from the command line. There is a "restart" command # which allows explicit specification of command line arguments. pdb = _init_pdb(commands=commands) while1: try: pdb._runscript(mainpyfile) if pdb._user_requested_quit: break print("The program finished and will be restarted") except Restart: print("Restarting", mainpyfile, "with arguments:") print("\t" + " ".join(sys.argv[1:])) except SystemExit: # In most cases SystemExit does not warrant a post-mortem session. print("The program exited via sys.exit(). Exit status: ", end='') print(sys.exc_info()[1]) except: traceback.print_exc() print("Uncaught exception. Entering post mortem debugging") print("Running 'cont' or 'step' will restart the program") t = sys.exc_info()[2] pdb.interaction(None, t) print("Post mortem debugger finished. The " + mainpyfile + " will be restarted")
4.判断这个shell是否存在:如果不存在,强制性的创建一个ipython环境;如果存在,则检测其是否为InteractiveShellEmbed的一个对象,如果是,则输出标准错误语句“You are currently into an embedded ipython shell""the configuration will not be loaded."