From 82c96fc2a864eb44ca06801ad4bf50b61d2eef2d 2012-08-27 06:06:47
From: Jörgen Stenarson <jorgen.stenarson@kroywen.se>
Date: 2012-08-27 06:06:47
Subject: [PATCH] fix for list pdb listcommand, broken for %run -d

when calling %run -d the filename available for pdb is <string> which is
not a proper filename. %run now adds a _exec_lines attribute containing the
correct filename.

---

diff --git a/IPython/core/debugger.py b/IPython/core/debugger.py
index b133944..ae847d9 100644
--- a/IPython/core/debugger.py
+++ b/IPython/core/debugger.py
@@ -441,11 +441,17 @@ class Pdb(OldPdb):
             tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
             tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
             src = []
-            lines = linecache.getlines(filename)
+            if filename == "<string>" and hasattr(self, "_exec_filename"):
+                lines = list(open(self._exec_filename))
+            else:
+                lines = linecache.getlines(filename)
             try:
                 encoding, _ = openpy.detect_encoding(_readline(lines))
             except SyntaxError:
                 encoding = "ascii"
+            if not lines:
+                print >>io.stdout, "No src could be located using filename: %r"%filename
+                return #Bailing out, there is nothing to see here
             for lineno in range(first, last+1):
                 line = py3compat.cast_unicode(lines[lineno])
                 if not line:
diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py
index 3ea8c79..572b223 100644
--- a/IPython/core/magics/execution.py
+++ b/IPython/core/magics/execution.py
@@ -554,6 +554,8 @@ python-profiler package from non-free.""")
                         print "%s prompt to start your script." % deb.prompt
                         ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
                         try:
+                            #save filename so it can be used by methods on the deb object
+                            deb._exec_filename = filename
                             deb.run('execfile("%s", prog_ns)' % filename, ns)
 
                         except: