Repository: ipython/ipython

Issue:
can't restart pdb session in ipython
```
In [1]: %run -d ~/t.py
Breakpoint 1 at /home/sk/t.py:1
NOTE: Enter 'c' at the ipdb>  prompt to start your script.
> /home/sk/t.py(1)<module>()
1---> 1 from sympy import *
      2 x = Symbol('x')
      3 #bessely(0,x).series(x)

ipdb> n
> /home/sk/t.py(2)<module>()
1     1 from sympy import *
----> 2 x = Symbol('x')
      3 #bessely(0,x).series(x)

ipdb> n
> /home/sk/t.py(5)<module>()
      4 #sin(asin(3), evaluate=False).is_rational
----> 5 n = Symbol('n', integer=True)
      6 #Sum(x**n, (n, 0, S.Infinity)).doit()

ipdb> restart
---------------------------------------------------------------------------
Restart                                   Traceback (most recent call last)
/home/sk/src/ipython/IPython/utils/py3compat.pyc in execfile(fname, *where)
    181             else:
    182                 filename = fname
--> 183             __builtin__.execfile(filename, *where)

/home/sk/t.py in <module>()
      3 #bessely(0,x).series(x)
      4 #sin(asin(3), evaluate=False).is_rational
----> 5 n = Symbol('n', integer=True)
      6 #Sum(x**n, (n, 0, S.Infinity)).doit()
      7 summation(2**n - 10,(n,0,oo))

/home/sk/t.py in <module>()
      3 #bessely(0,x).series(x)
      4 #sin(asin(3), evaluate=False).is_rational
----> 5 n = Symbol('n', integer=True)
      6 #Sum(x**n, (n, 0, S.Infinity)).doit()
      7 summation(2**n - 10,(n,0,oo))

/usr/lib/python2.6/bdb.pyc in trace_dispatch(self, frame, event, arg)
     44             return # None
     45         if event == 'line':
---> 46             return self.dispatch_line(frame)
     47         if event == 'call':
     48             return self.dispatch_call(frame, arg)

/usr/lib/python2.6/bdb.pyc in dispatch_line(self, frame)
     62     def dispatch_line(self, frame):
     63         if self.stop_here(frame) or self.break_here(frame):
---> 64             self.user_line(frame)
     65             if self.quitting: raise BdbQuit
     66         return self.trace_dispatch

/usr/lib/python2.6/pdb.pyc in user_line(self, frame)
    148             self._wait_for_mainpyfile = 0
    149         if self.bp_commands(frame):
--> 150             self.interaction(frame, None)
    151 
    152     def bp_commands(self,frame):

/home/sk/src/ipython/IPython/core/debugger.pyc in interaction(self, frame, traceback)
    259     def interaction(self, frame, traceback):
    260         self.shell.set_completer_frame(frame)
--> 261         OldPdb.interaction(self, frame, traceback)
    262 
    263     def new_do_up(self, arg):

/usr/lib/python2.6/pdb.pyc in interaction(self, frame, traceback)
    196         self.setup(frame, traceback)
    197         self.print_stack_entry(self.stack[self.curindex])
--> 198         self.cmdloop()
    199         self.forget()
    200 

/usr/lib/python2.6/cmd.pyc in cmdloop(self, intro)
    140                             line = line.rstrip('\r\n')
    141                 line = self.precmd(line)
--> 142                 stop = self.onecmd(line)
    143                 stop = self.postcmd(stop, line)
    144             self.postloop()

/usr/lib/python2.6/pdb.pyc in onecmd(self, line)
    265         """
    266         if not self.commands_defining:
--> 267             return cmd.Cmd.onecmd(self, line)
    268         else:
    269             return self.handle_command_def(line)

/usr/lib/python2.6/cmd.pyc in onecmd(self, line)
    217             except AttributeError:
    218                 return self.default(line)
--> 219             return func(arg)
    220 
    221     def emptyline(self):

/usr/lib/python2.6/pdb.pyc in do_run(self, arg)
    659             sys.argv = shlex.split(arg)
    660             sys.argv[:0] = argv0
--> 661         raise Restart
    662 
    663     do_restart = do_run

Restart: 

In [2]: 
```



Discussion:
can you post the content of t.py?

I don't think that's important for this issue.  I was able to reproduce this problem with any content.  For example:

```
In [1]: cat ~/t.py
from sympy import *
f = Function("f")
n = Symbol("n", integer=True)
rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})
In [2]: %run -d ~/t.py
Breakpoint 1 at /home/sk/t.py:1
NOTE: Enter 'c' at the ipdb>  prompt to start your script.
> /home/sk/t.py(1)<module>()
1---> 1 from sympy import *
      2 f = Function("f")
      3 n = Symbol("n", integer=True)

ipdb> n
> /home/sk/t.py(2)<module>()
1     1 from sympy import *
----> 2 f = Function("f")
      3 n = Symbol("n", integer=True)

ipdb> n
> /home/sk/t.py(3)<module>()
      2 f = Function("f")
----> 3 n = Symbol("n", integer=True)
      4 rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})

/usr/lib/python2.6/bdb.pyc in trace_dispatch(self, frame, event, arg)
     44             return # None
     45         if event == 'line':
---> 46             return self.dispatch_line(frame)
     47         if event == 'call':
     48             return self.dispatch_call(frame, arg)

/usr/lib/python2.6/bdb.pyc in dispatch_line(self, frame)
     62     def dispatch_line(self, frame):
     63         if self.stop_here(frame) or self.break_here(frame):
---> 64             self.user_line(frame)
     65             if self.quitting: raise BdbQuit
     66         return self.trace_dispatch

/usr/lib/python2.6/pdb.pyc in user_line(self, frame)
    148             self._wait_for_mainpyfile = 0
    149         if self.bp_commands(frame):
--> 150             self.interaction(frame, None)
    151 
    152     def bp_commands(self,frame):

/home/sk/src/ipython/IPython/core/debugger.pyc in interaction(self, frame, traceback)
    259     def interaction(self, frame, traceback):
    260         self.shell.set_completer_frame(frame)
--> 261         OldPdb.interaction(self, frame, traceback)
    262 
    263     def new_do_up(self, arg):

/usr/lib/python2.6/pdb.pyc in interaction(self, frame, traceback)
    196         self.setup(frame, traceback)
    197         self.print_stack_entry(self.stack[self.curindex])
--> 198         self.cmdloop()
    199         self.forget()
    200 

/usr/lib/python2.6/cmd.pyc in cmdloop(self, intro)
    140                             line = line.rstrip('\r\n')
    141                 line = self.precmd(line)
--> 142                 stop = self.onecmd(line)
    143                 stop = self.postcmd(stop, line)
    144             self.postloop()

/usr/lib/python2.6/pdb.pyc in onecmd(self, line)
    265         """
    266         if not self.commands_defining:
--> 267             return cmd.Cmd.onecmd(self, line)
    268         else:
    269             return self.handle_command_def(line)

/usr/lib/python2.6/cmd.pyc in onecmd(self, line)
    217             except AttributeError:
    218                 return self.default(line)
--> 219             return func(arg)
    220 
    221     def emptyline(self):

/usr/lib/python2.6/pdb.pyc in do_run(self, arg)
    659             sys.argv = shlex.split(arg)
    660             sys.argv[:0] = argv0
--> 661         raise Restart
    662 
    663     do_restart = do_run

Restart: 

In [3]:
```

BTW, this still tagged as "needs-info".

@minrk, do able to reproduce an issue?

No, I haven't reproduced it.  With your code exactly as it is, it seems to run to completion without issue:

``` python
In [1]: cat t.py
from sympy import *
f = Function("f")
n = Symbol("n", integer=True)
rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})

In [2]: %run -d t.py
Breakpoint 1 at /Users/minrk/temp/t.py:1
NOTE: Enter 'c' at the ipdb>  prompt to continue execution.
> /Users/minrk/temp/t.py(1)<module>()
1---> 1 from sympy import *
      2 f = Function("f")
      3 n = Symbol("n", integer=True)

ipdb> n
> /Users/minrk/temp/t.py(2)<module>()
1     1 from sympy import *
----> 2 f = Function("f")
      3 n = Symbol("n", integer=True)

ipdb> n
> /Users/minrk/temp/t.py(3)<module>()
      2 f = Function("f")
----> 3 n = Symbol("n", integer=True)
      4 rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})

ipdb> n
> /Users/minrk/temp/t.py(4)<module>()
      2 f = Function("f")
      3 n = Symbol("n", integer=True)
----> 4 rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})

ipdb> n
--Return--
None
> /Users/minrk/temp/t.py(4)<module>()
      2 f = Function("f")
      3 n = Symbol("n", integer=True)
----> 4 rsolve(f(n + 2) - f(n + 1) + f(n)/4, f(n), {f(0):0, f(1):1})
```

On Fri, Nov 08, 2013 at 09:42:00AM -0800, Min RK wrote:

>    No, I haven't reproduced it. With your code exactly as it is, it seems to
>    run to completion without issue:

The problem is not with _my_ code.  Just take look
on the provided above my example of ipdb session.

You should try to _restart_ pdb session somewhere in the middle.

Ahhh, I misread the example and thought `n` was the last issued command.  Now I see what's probably supposed to happen.  Thanks for clarifying.


Resolving patch:
diff --git a/IPython/core/magics/execution.py b/IPython/core/magics/execution.py
--- a/IPython/core/magics/execution.py
+++ b/IPython/core/magics/execution.py
@@ -20,6 +20,7 @@
 import os
 import sys
 import time
+from pdb import Restart
 
 # cProfile was added in Python2.5
 try:
@@ -665,7 +666,11 @@ def run(self, parameter_s='', runner=None,
                     'modulename': modulename,
                 }
             else:
-                code = 'execfile(filename, prog_ns)'
+                if 'd' in opts:
+                    # allow exceptions to raise in debug mode
+                    code = 'execfile(filename, prog_ns, raise_exceptions=True)'
+                else:
+                    code = 'execfile(filename, prog_ns)'
                 code_ns = {
                     'execfile': self.shell.safe_execfile,
                     'prog_ns': prog_ns,
@@ -807,7 +812,18 @@ def _run_with_debugger(self, code, code_ns, filename=None,
             if filename:
                 # save filename so it can be used by methods on the deb object
                 deb._exec_filename = filename
-            deb.run(code, code_ns)
+            while True:
+                try:
+                    deb.run(code, code_ns)
+                except Restart:
+                    print("Restarting")
+                    if filename:
+                        deb._wait_for_mainpyfile = True
+                        deb.mainpyfile = deb.canonic(filename)
+                    continue
+                else:
+                    break
+            
 
         except:
             etype, value, tb = sys.exc_info()


Test patch: