Commit b5699c72 authored by Guido van Rossum's avatar Guido van Rossum

Added support for specifying a filename for a breakpoint, roughly

according to an idea by Harri Pasanen (but with different syntax).
This affects the 'break' and 'clear' commands and their help
functions.  Also added a helper method lookupmodule().

Also:

- Try to import readline (important when pdb is used from/as a script).
- Get rid of reference to ancient __privileged__ magic variable.
- Moved all import out of functions to the top.
- When used as a script, check that the script file exists.
parent c612681b
...@@ -10,6 +10,7 @@ import linecache ...@@ -10,6 +10,7 @@ import linecache
import cmd import cmd
import bdb import bdb
import repr import repr
import os
# Interaction prompt line will separate file and call info from code # Interaction prompt line will separate file and call info from code
...@@ -25,6 +26,11 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -25,6 +26,11 @@ class Pdb(bdb.Bdb, cmd.Cmd):
bdb.Bdb.__init__(self) bdb.Bdb.__init__(self)
cmd.Cmd.__init__(self) cmd.Cmd.__init__(self)
self.prompt = '(Pdb) ' self.prompt = '(Pdb) '
# Try to load readline if it exists
try:
import readline
except ImportError:
pass
def reset(self): def reset(self):
bdb.Bdb.reset(self) bdb.Bdb.reset(self)
...@@ -75,7 +81,6 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -75,7 +81,6 @@ class Pdb(bdb.Bdb, cmd.Cmd):
if line[:1] == '!': line = line[1:] if line[:1] == '!': line = line[1:]
locals = self.curframe.f_locals locals = self.curframe.f_locals
globals = self.curframe.f_globals globals = self.curframe.f_globals
globals['__privileged__'] = 1
try: try:
code = compile(line + '\n', '<stdin>', 'single') code = compile(line + '\n', '<stdin>', 'single')
exec code in globals, locals exec code in globals, locals
...@@ -93,28 +98,53 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -93,28 +98,53 @@ class Pdb(bdb.Bdb, cmd.Cmd):
do_h = cmd.Cmd.do_help do_h = cmd.Cmd.do_help
def do_break(self, arg): def do_break(self, arg):
# break [ ([filename:]lineno | function) [, "condition"] ]
if not arg: if not arg:
print self.get_all_breaks() # XXX print self.get_all_breaks() # XXX
return return
# Try line number as argument # parse arguments; comma has lowest precendence
# and cannot occur in filename
filename = None
lineno = None
cond = None
comma = string.find(arg, ',')
if comma > 0:
# parse stuff after comma: "condition"
cond = string.lstrip(arg[comma+1:])
arg = string.rstrip(arg[:comma])
try: try:
arg = eval(arg, self.curframe.f_globals, cond = eval(
cond,
self.curframe.f_globals,
self.curframe.f_locals) self.curframe.f_locals)
except: except:
print '*** Could not eval argument:', arg print '*** Could not eval condition:', cond
return return
# parse stuff before comma: [filename:]lineno | function
# Check for condition colon = string.rfind(arg, ':')
try: arg, cond = arg if colon >= 0:
except: arg, cond = arg, None filename = string.rstrip(arg[:colon])
filename = self.lookupmodule(filename)
arg = string.lstrip(arg[colon+1:])
try: try:
lineno = int(arg) lineno = int(arg)
filename = self.curframe.f_code.co_filename except ValueError, msg:
print '*** Bad lineno:', arg
return
else:
# no colon; can be lineno or function
try:
lineno = int(arg)
except ValueError:
try:
func = eval(arg,
self.curframe.f_globals,
self.curframe.f_locals)
except: except:
# Try function name as the argument print '*** Could not eval argument:',
print arg
return
try: try:
func = arg
if hasattr(func, 'im_func'): if hasattr(func, 'im_func'):
func = func.im_func func = func.im_func
code = func.func_code code = func.func_code
...@@ -123,8 +153,11 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -123,8 +153,11 @@ class Pdb(bdb.Bdb, cmd.Cmd):
print 'is not a function', arg print 'is not a function', arg
return return
lineno = code.co_firstlineno lineno = code.co_firstlineno
if not filename:
filename = code.co_filename filename = code.co_filename
# supply default filename if necessary
if not filename:
filename = self.curframe.f_code.co_filename
# now set the break point # now set the break point
err = self.set_break(filename, lineno, cond) err = self.set_break(filename, lineno, cond)
if err: print '***', err if err: print '***', err
...@@ -141,11 +174,18 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -141,11 +174,18 @@ class Pdb(bdb.Bdb, cmd.Cmd):
if reply in ('y', 'yes'): if reply in ('y', 'yes'):
self.clear_all_breaks() self.clear_all_breaks()
return return
filename = None
colon = string.rfind(arg, ':')
if colon >= 0:
filename = string.rstrip(arg[:colon])
filename = self.lookupmodule(filename)
arg = string.lstrip(arg[colon+1:])
try: try:
lineno = int(eval(arg)) lineno = int(arg)
except: except:
print '*** Error in argument:', `arg` print '*** Bad lineno:', `arg`
return return
if not filename:
filename = self.curframe.f_code.co_filename filename = self.curframe.f_code.co_filename
err = self.clear_break(filename, lineno) err = self.clear_break(filename, lineno)
if err: print '***', err if err: print '***', err
...@@ -222,7 +262,6 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -222,7 +262,6 @@ class Pdb(bdb.Bdb, cmd.Cmd):
do_rv = do_retval do_rv = do_retval
def do_p(self, arg): def do_p(self, arg):
self.curframe.f_globals['__privileged__'] = 1
try: try:
value = eval(arg, self.curframe.f_globals, \ value = eval(arg, self.curframe.f_globals, \
self.curframe.f_locals) self.curframe.f_locals)
...@@ -373,13 +412,16 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -373,13 +412,16 @@ class Pdb(bdb.Bdb, cmd.Cmd):
self.help_b() self.help_b()
def help_b(self): def help_b(self):
print """b(reak) [lineno | function] [, "condition"] print """b(reak) ([file:]lineno | function) [, "condition"]
With a line number argument, set a break there in the current With a line number argument, set a break there in the current
file. With a function name, set a break at the entry of that file. With a function name, set a break at the entry of that
function. Without argument, list all breaks. If a second function. Without argument, list all breaks. If a second
argument is present, it is a string specifying an expression argument is present, it is a string specifying an expression
which must evaluate to true before the breakpoint is honored. which must evaluate to true before the breakpoint is honored.
"""
The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet). The file is searched on sys.path."""
def help_clear(self): def help_clear(self):
self.help_cl() self.help_cl()
...@@ -387,7 +429,11 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -387,7 +429,11 @@ class Pdb(bdb.Bdb, cmd.Cmd):
def help_cl(self): def help_cl(self):
print """cl(ear) [lineno] print """cl(ear) [lineno]
With a line number argument, clear that break in the current file. With a line number argument, clear that break in the current file.
Without argument, clear all breaks (but first ask confirmation).""" Without argument, clear all breaks (but first ask confirmation).
The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet). The file is searched on sys.path."""
def help_step(self): def help_step(self):
self.help_s() self.help_s()
...@@ -466,6 +512,19 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -466,6 +512,19 @@ class Pdb(bdb.Bdb, cmd.Cmd):
def help_pdb(self): def help_pdb(self):
help() help()
# Helper function for break/clear parsing -- may be overridden
def lookupmodule(self, filename):
if filename == mainmodule:
return mainpyfile
for dirname in sys.path:
fullname = os.path.join(dirname, filename)
if os.path.exists(fullname):
return fullname
print 'Warning:', `filename`, 'not found from sys.path'
return filename
# Simplified interface # Simplified interface
def run(statement, globals=None, locals=None): def run(statement, globals=None, locals=None):
...@@ -493,7 +552,6 @@ def post_mortem(t): ...@@ -493,7 +552,6 @@ def post_mortem(t):
p.interaction(t.tb_frame, t) p.interaction(t.tb_frame, t)
def pm(): def pm():
import sys
post_mortem(sys.last_traceback) post_mortem(sys.last_traceback)
...@@ -506,7 +564,6 @@ def test(): ...@@ -506,7 +564,6 @@ def test():
# print help # print help
def help(): def help():
import os
for dirname in sys.path: for dirname in sys.path:
fullname = os.path.join(dirname, 'pdb.doc') fullname = os.path.join(dirname, 'pdb.doc')
if os.path.exists(fullname): if os.path.exists(fullname):
...@@ -517,16 +574,21 @@ def help(): ...@@ -517,16 +574,21 @@ def help():
print 'Sorry, can\'t find the help file "pdb.doc"', print 'Sorry, can\'t find the help file "pdb.doc"',
print 'along the Python search path' print 'along the Python search path'
mainmodule = ''
mainpyfile = ''
# When invoked as main program, invoke the debugger on a script # When invoked as main program, invoke the debugger on a script
if __name__=='__main__': if __name__=='__main__':
import sys global mainmodule, mainpyfile
import os
if not sys.argv[1:]: if not sys.argv[1:]:
print "usage: pdb.py scriptfile [arg] ..." print "usage: pdb.py scriptfile [arg] ..."
sys.exit(2) sys.exit(2)
filename = sys.argv[1] # Get script filename mainpyfile = filename = sys.argv[1] # Get script filename
if not os.path.exists(filename):
print 'Error:', `filename`, 'does not exist'
sys.exit(1)
mainmodule = os.path.basename(filename)
del sys.argv[0] # Hide "pdb.py" from argument list del sys.argv[0] # Hide "pdb.py" from argument list
# Insert script directory in front of module search path # Insert script directory in front of module search path
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment