Skip to content

Commit

Permalink
Merge pull request #2782 from ellbur/run-breakpoints
Browse files Browse the repository at this point in the history
Allow the %run magic with '-b' to specify a file.
  • Loading branch information
takluyver committed Jan 13, 2013
2 parents 75dc8ae + 9ea7fe5 commit cdc7e66
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions IPython/core/magics/execution.py
Expand Up @@ -414,6 +414,10 @@ def run(self, parameter_s='', runner=None,
the first breakpoint must be set on a line which actually does
something (not a comment or docstring) for it to stop execution.
Or you can specify a breakpoint in a different file::
%run -d -b myotherfile.py:20 myscript
When the pdb debugger starts, you will see a (Pdb) prompt. You must
first enter 'c' (without quotes) to start execution up to the first
breakpoint.
Expand Down Expand Up @@ -551,11 +555,11 @@ def run(self, parameter_s='', runner=None,
bdb.Breakpoint.bpbynumber = [None]
# Set an initial breakpoint to stop execution
maxtries = 10
bp = int(opts.get('b', [1])[0])
checkline = deb.checkline(filename, bp)
bp_file, bp_line = parse_breakpoint(opts.get('b', [1])[0], filename)
checkline = deb.checkline(bp_file, bp_line)
if not checkline:
for bp in range(bp + 1, bp + maxtries + 1):
if deb.checkline(filename, bp):
for bp in range(bp_line + 1, bp_line + maxtries + 1):
if deb.checkline(bp_file, bp):
break
else:
msg = ("\nI failed to find a valid line to set "
Expand All @@ -566,7 +570,7 @@ def run(self, parameter_s='', runner=None,
error(msg)
return
# if we find a good linenumber, set the breakpoint
deb.do_break('%s:%s' % (filename, bp))
deb.do_break('%s:%s' % (bp_file, bp_line))

# Mimic Pdb._runscript(...)
deb._wait_for_mainpyfile = True
Expand Down Expand Up @@ -1079,3 +1083,11 @@ def capture(self, line, cell):
self.shell.run_cell(cell)
if args.output:
self.shell.user_ns[args.output] = io

def parse_breakpoint(text, current_file):
'''Returns (file, line) for file:line and (current_file, line) for line'''
colon = text.find(':')
if colon == -1:
return current_file, int(text)
else:
return text[:colon], int(text[colon+1:])

0 comments on commit cdc7e66

Please sign in to comment.