From 72c7e7146101c9e75aa885a03322e5d6d3d02aa0 2013-03-04 23:29:37 From: MinRK Date: 2013-03-04 23:29:37 Subject: [PATCH] Backport PR #2717: One liner to fix debugger printing stack traces when lines of context are larger than source. Correctly print stack traces when context is larger than lines of source. To test: ``` In [6]: def bar(): ...: return 20 ...: ``` Before: ``` In [7]: ipdb.runcall(bar) (2)bar() 0 return 20 ``` And here's after: ``` In [6]: ipdb.runcall(bar) (2)bar() 1 def bar(): ----> 2 return 20 ``` Replaces gh-2697. (Adds a test.) --- diff --git a/IPython/core/debugger.py b/IPython/core/debugger.py index 8d10835..70aaf74 100644 --- a/IPython/core/debugger.py +++ b/IPython/core/debugger.py @@ -351,8 +351,8 @@ class Pdb(OldPdb): start = lineno - 1 - context//2 lines = linecache.getlines(filename) - start = max(start, 0) start = min(start, len(lines) - context) + start = max(start, 0) lines = lines[start : start + context] for i,line in enumerate(lines): diff --git a/IPython/core/tests/test_debugger.py b/IPython/core/tests/test_debugger.py index 7703235..115b75b 100644 --- a/IPython/core/tests/test_debugger.py +++ b/IPython/core/tests/test_debugger.py @@ -121,3 +121,22 @@ def test_ipdb_magics(): Constructor Docstring:Docstring for ExampleClass.__init__ ipdb> continue ''' + +def test_ipdb_magics(): + '''Test ipdb with a very short function. + + >>> def bar(): + ... pass + + Run ipdb. + + >>> with PdbTestInput([ + ... 'continue', + ... ]): + ... debugger.Pdb().runcall(bar) + > (2)bar() + 1 def bar(): + ----> 2 pass + + ipdb> continue + '''