Dylan Gregersen brings us another nice code snippet which might be especially welcome for IDL users who miss the STOP procedure:

Here's a snippet I'm using often while writing scripts, executing via run -i <file> or execfile(<file>), and I want to quickly truncate the code so that the remaining part of the script doesn't run. Then I can access the variables and do other interactive scripting execution.

class ManualStop(Exception):
  def __init__(self, message='Manual Stop', errors=None):
    Exception.__init__(self, message)
    self.errors = errors

def stop(msg=""):
  Insert a manual stop
  raise ManualStop("User called stop({})".format(msg))

Of course the above can be achieved by injecting any simple code error, e.g. stop, here = 0 or the ever-popular 1 / 0, but that wouldn't be so nice.

And it should be mentioned that this is not a substitute for pdb which is an actual debug tool and blows IDL's `stop` out of the water.  But this stop() is good for quickly scripting and testing portions of your scripts.  As an example:

print("testing script")
print("lots of other code you don't want to run right now")code here

One cool thing is that once you've stopped like this, in IPython you can issue the %debug magic command and re-enter the context of your script via IPython's enhanced debugger.  This lets you walk back up the stack trace and examine variables.

Finally it's worth mentioning that I have this in my ~/.ipython/profile_default/startup/misc.py  which means it's loaded every time on startup (like a bashrc or tcshrc) and I can just use it without importing, copying into script, etc.

    4           12