From 74860b86950f3e321a6e137deca449c3b1c78b1a 2016-01-05 14:18:43 From: Matthias Bussonnier Date: 2016-01-05 14:18:43 Subject: [PATCH] Merge pull request #9097 from JamshedVesuna/master Added user defined context for Pdb --- diff --git a/IPython/core/debugger.py b/IPython/core/debugger.py index f3a619b..a1ba878 100644 --- a/IPython/core/debugger.py +++ b/IPython/core/debugger.py @@ -29,7 +29,6 @@ from __future__ import print_function import bdb import functools import inspect -import linecache import sys from IPython import get_ipython @@ -54,7 +53,6 @@ if '--pydb' in sys.argv: if has_pydb: from pydb import Pdb as OldPdb - #print "Using pydb for %run -d and post-mortem" #dbg prompt = 'ipydb> ' else: from pdb import Pdb as OldPdb @@ -94,7 +92,7 @@ class Tracer(object): """ @skip_doctest - def __init__(self,colors=None): + def __init__(self, colors=None): """Create a local debugger instance. Parameters @@ -202,9 +200,16 @@ class Pdb(OldPdb): """Modified Pdb class, does not load readline.""" def __init__(self,color_scheme='NoColor',completekey=None, - stdin=None, stdout=None): + stdin=None, stdout=None, context=5): # Parent constructor: + try: + self.context=int(context) + if self.context <= 0: + raise ValueError("Context must be a positive integer") + except (TypeError, ValueError): + raise ValueError("Context must be a positive integer") + if has_pydb and completekey is None: OldPdb.__init__(self,stdin=stdin,stdout=io.stdout) else: @@ -324,16 +329,31 @@ class Pdb(OldPdb): def postloop(self): self.shell.set_completer_frame(None) - def print_stack_trace(self): + def print_stack_trace(self, context=None): + if context is None: + context = self.context + try: + context=int(context) + if context <= 0: + raise ValueError("Context must be a positive integer") + except (TypeError, ValueError): + raise ValueError("Context must be a positive integer") try: for frame_lineno in self.stack: - self.print_stack_entry(frame_lineno, context = 5) + self.print_stack_entry(frame_lineno, context=context) except KeyboardInterrupt: pass def print_stack_entry(self,frame_lineno,prompt_prefix='\n-> ', - context = 3): - #frame, lineno = frame_lineno + context=None): + if context is None: + context = self.context + try: + context=int(context) + if context <= 0: + raise ValueError("Context must be a positive integer") + except (TypeError, ValueError): + raise ValueError("Context must be a positive integer") print(self.format_stack_entry(frame_lineno, '', context), file=io.stdout) # vds: >> @@ -342,7 +362,15 @@ class Pdb(OldPdb): self.shell.hooks.synchronize_with_editor(filename, lineno, 0) # vds: << - def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3): + def format_stack_entry(self, frame_lineno, lprefix=': ', context=None): + if context is None: + context = self.context + try: + context=int(context) + if context <= 0: + print("Context must be a positive integer") + except (TypeError, ValueError): + print("Context must be a positive integer") try: import reprlib # Py 3 except ImportError: @@ -530,7 +558,6 @@ class Pdb(OldPdb): def do_longlist(self, arg): self.lastcmd = 'longlist' filename = self.curframe.f_code.co_filename - breaklist = self.get_file_breaks(filename) try: lines, lineno = self.getsourcelines(self.curframe) except OSError as err: @@ -586,3 +613,20 @@ class Pdb(OldPdb): namespaces = [('Locals', self.curframe.f_locals), ('Globals', self.curframe.f_globals)] self.shell.find_line_magic('psource')(arg, namespaces=namespaces) + + if sys.version_info > (3, ): + def do_where(self, arg): + """w(here) + Print a stack trace, with the most recent frame at the bottom. + An arrow indicates the "current frame", which determines the + context of most commands. 'bt' is an alias for this command. + + Take a number as argument as an (optional) number of context line to + print""" + if arg: + context = int(arg) + self.print_stack_trace(context) + else: + self.print_stack_trace() + + do_w = do_where