annotate unixSoft/lib/python/pdb.py @ 86:68f53fa09e99

Moving all clojure stuff to a more self-contained area.
author Augie Fackler <durin42@gmail.com>
date Wed, 15 Apr 2009 22:28:20 -0500
parents c30d68fbd368
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
1 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
2 Pdb++, a fancier version of pdb
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
3 ===============================
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
4
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
5 This module extends the stdlib pdb in numerous ways, e.g. by providing
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
6 real completion of Python values instead of pdb's own commands, or by
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
7 adding few convenience commands like ``longlist``, ``interact`` or
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
8 ``watch``.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
9
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
10 For a full explanation of each command, refer to the docstring or type
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
11 help <command> at the prompt.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
12
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
13 Installation
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
14 ------------
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
15
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
16 This module is meant to replace stdlib's pdb.py from the outsite;
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
17 simply put it in a directory in your PYTHONPATH, and you can start
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
18 using it immediately. Since it's named pdb.py, every place which
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
19 imports pdb will now find the new module.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
20
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
21 Dependencies
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
22 ------------
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
23
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
24 To work properly, this module needs `rlcompleter_ng`_ to be installed.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
25
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
26 To enable syntax highlighting, you must install `pygments`.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
27
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
28 .. _pygments: http://pygments.org/
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
29 .. _`rlcompleter_ng`: http://codespeak.net/svn/user/antocuni/hack/rlcompleter_ng.py
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
30
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
31 Configuration
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
32 -------------
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
33
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
34 To customize the configuration of Pdb++, you need to put a file named
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
35 .pdbrc.py in your home directory. The file must contain a class named
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
36 ``Config`` inheriting from ``DefaultConfig`` and overridding the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
37 desired values.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
38
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
39 To know which options are available, look at the comment in the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
40 source.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
41
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
42 You can find a sample configuration file, here:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
43 http://codespeak.net/svn/user/antocuni/hack/pdbrc.py
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
44 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
45
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
46 __version__='0.1'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
47 __author__ ='Antonio Cuni <anto.cuni@gmail.com>'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
48 __url__='http://codespeak.net/svn/user/antocuni/hack/pdb.py'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
49
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
50 import sys
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
51 import os.path
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
52 import inspect
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
53 import code
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
54 import types
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
55 from rlcompleter_ng import Completer, ConfigurableClass, setcolor, colors
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
56
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
57
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
58 def import_from_stdlib(name):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
59 import code # arbitrary module which stays in the same dir as pdb
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
60 stdlibdir, _ = os.path.split(code.__file__)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
61 pyfile = os.path.join(stdlibdir, name + '.py')
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
62 result = types.ModuleType(name)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
63 mydict = execfile(pyfile, result.__dict__)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
64 return result
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
65
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
66 pdb = import_from_stdlib('pdb')
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
67
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
68
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
69 class DefaultConfig:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
70 prompt = '(Pdb++) '
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
71 completekey = 'tab'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
72 highlight = True
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
73 bg = 'dark'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
74 colorscheme = None
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
75
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
76 line_number_color = colors.turquoise
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
77 current_line_color = 44 # blue
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
78
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
79 def getsourcelines(obj):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
80 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
81 return inspect.getsourcelines(obj)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
82 except IOError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
83 pass
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
84
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
85 if isinstance(obj, types.FrameType):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
86 filename = obj.f_code.co_filename
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
87 if hasattr(filename, '__source__'):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
88 first = max(1, obj.f_lineno - 5)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
89 lines = [line + '\n' for line in filename.__source__.lines]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
90 return lines, first
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
91 raise IOError('could not get source code')
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
92
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
93 def setbgcolor(line, color):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
94 # hack hack hack
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
95 # add a bgcolor attribute to all escape sequences found
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
96 import re
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
97 setbg = '\x1b[%dm' % color
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
98 regexbg = '\\1;%dm' % color
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
99 return setbg + re.sub('(\x1b\\[.*?)m', regexbg, line) + '\x1b[00m'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
100
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
101 CLEARSCREEN = '\033[2J\033[1;1H'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
102
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
103 def lasti2lineno(code, lasti):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
104 import dis
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
105 linestarts = list(dis.findlinestarts(code))
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
106 linestarts.reverse()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
107 for i, lineno in linestarts:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
108 if lasti >= i:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
109 return lineno
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
110 assert False, 'Invalid instruction number: %s' % lasti
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
111
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
112 class Undefined:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
113 def __repr__(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
114 return '<undefined>'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
115 undefined = Undefined()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
116
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
117 class Pdb(pdb.Pdb, ConfigurableClass):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
118
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
119 DefaultConfig = DefaultConfig
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
120 config_filename = '.pdbrc.py'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
121
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
122 def __init__(self, *args, **kwds):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
123 Config = kwds.pop('Config', None)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
124 pdb.Pdb.__init__(self, *args, **kwds)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
125 self.config = self.get_config(Config)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
126 self.prompt = self.config.prompt
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
127 self.completekey = self.config.completekey
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
128
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
129 self.mycompleter = None
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
130 self.watching = {} # frame --> (name --> last seen value)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
131 self.sticky = False
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
132 self.sticky_ranges = {} # frame --> (start, end)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
133 self.tb_lineno = {} # frame --> lineno where the exception raised
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
134
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
135 def interaction(self, frame, traceback, orig_traceback=None):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
136 self.setup(frame, traceback, orig_traceback)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
137 self.print_stack_entry(self.stack[self.curindex])
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
138 self.cmdloop()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
139 self.forget()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
140
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
141 def setup(self, frame, tb, orig_tb=None):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
142 pdb.Pdb.setup(self, frame, tb)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
143 tb = orig_tb
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
144 while tb:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
145 lineno = lasti2lineno(tb.tb_frame.f_code, tb.tb_lasti)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
146 self.tb_lineno[tb.tb_frame] = lineno
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
147 tb = tb.tb_next
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
148
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
149 def forget(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
150 pdb.Pdb.forget(self)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
151 self.raise_lineno = {}
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
152
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
153 def complete(self, text, state):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
154 if state == 0:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
155 mydict = self.curframe.f_globals.copy()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
156 mydict.update(self.curframe.f_locals)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
157 self.mycompleter = Completer(mydict)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
158 return self.mycompleter.complete(text, state)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
159
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
160 def _init_pygments(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
161 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
162 from pygments.lexers import PythonLexer
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
163 from pygments.formatters import TerminalFormatter
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
164 except ImportError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
165 return False
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
166
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
167 if hasattr(self, '_fmt'):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
168 return True
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
169
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
170 self._fmt = TerminalFormatter(bg=self.config.bg,
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
171 colorscheme=self.config.colorscheme)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
172 self._lexer = PythonLexer()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
173 return True
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
174
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
175 def format_source(self, src):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
176 if not self._init_pygments():
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
177 return src
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
178 from pygments import highlight, lex
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
179 return highlight(src, self._lexer, self._fmt)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
180
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
181 def format_line(self, lineno, marker, line):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
182 lineno = '%4d' % lineno
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
183 if self.config.highlight:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
184 lineno = setcolor(lineno, self.config.line_number_color)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
185 line = '%s %2s %s' % (lineno, marker, line)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
186 if self.config.highlight and marker == '->':
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
187 line = setbgcolor(line, self.config.current_line_color)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
188 return line
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
189
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
190 def parseline(self, line):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
191 cmd, arg, newline = pdb.Pdb.parseline(self, line)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
192 # don't execute short disruptive commands if a variable with
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
193 # the name exits in the current contex; this prevents pdb to
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
194 # quit if you type e.g. 'r[0]' by mystake.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
195 if cmd in ['c', 'r', 'q'] and (cmd in self.curframe.f_globals or
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
196 cmd in self.curframe.f_locals):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
197 line = '!' + line
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
198 return pdb.Pdb.parseline(self, line)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
199 return cmd, arg, newline
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
200
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
201 def do_longlist(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
202 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
203 {longlist|ll}
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
204 List source code for the current function.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
205
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
206 Differently that list, the whole function is displayed; the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
207 current line is marked with '->'. In case of post-mortem
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
208 debugging, the line which effectively raised the exception is
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
209 marked with '>>'.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
210
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
211 If the 'highlight' config option is set and pygments is
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
212 installed, the source code is colorized.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
213 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
214 self.lastcmd = 'longlist'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
215 self._printlonglist()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
216
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
217 def _printlonglist(self, linerange=None):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
218 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
219 lines, lineno = getsourcelines(self.curframe)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
220 except IOError, e:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
221 print '** Error: %s **' % e
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
222 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
223 if linerange:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
224 start, end = linerange
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
225 start = max(start, lineno)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
226 end = min(end, lineno+len(lines))
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
227 lines = lines[start-lineno:end-lineno]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
228 lineno = start
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
229 self._print_lines(lines, lineno)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
230
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
231 def _print_lines(self, lines, lineno, print_markers=True):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
232 exc_lineno = self.tb_lineno.get(self.curframe, None)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
233 lines = [line[:-1] for line in lines] # remove the trailing '\n'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
234 if self.config.highlight:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
235 maxlength = max(map(len, lines))
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
236 lines = [line.ljust(maxlength) for line in lines]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
237 src = self.format_source('\n'.join(lines))
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
238 lines = src.splitlines()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
239 for i, line in enumerate(lines):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
240 marker = ''
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
241 if lineno == self.curframe.f_lineno and print_markers:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
242 marker = '->'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
243 elif lineno == exc_lineno and print_markers:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
244 marker = '>>'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
245 lines[i] = self.format_line(lineno, marker, line)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
246 lineno += 1
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
247 print '\n'.join(lines)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
248
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
249 do_ll = do_longlist
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
250
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
251 def do_list(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
252 from StringIO import StringIO
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
253 oldstdout = sys.stdout
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
254 sys.stdout = StringIO()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
255 pdb.Pdb.do_list(self, arg)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
256 src = self.format_source(sys.stdout.getvalue())
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
257 sys.stdout = oldstdout
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
258 print src,
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
259
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
260 do_l = do_list
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
261
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
262 def do_interact(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
263 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
264 interact
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
265
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
266 Start an interative interpreter whose global namespace
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
267 contains all the names found in the current scope.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
268 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
269 ns = self.curframe.f_globals.copy()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
270 ns.update(self.curframe.f_locals)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
271 code.interact("*interactive*", local=ns)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
272
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
273 def do_track(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
274 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
275 track expression
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
276
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
277 Display a graph showing which objects are referred by the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
278 value of the expression. This command requires pypy to be in
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
279 the current PYTHONPATH.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
280 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
281 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
282 from pypy.translator.tool.reftracker import track
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
283 except ImportError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
284 print '** cannot import pypy.translator.tool.reftracker **'
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
285 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
286 val = self._getval(arg)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
287 track(val)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
288
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
289 def _get_watching(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
290 return self.watching.setdefault(self.curframe, {})
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
291
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
292 def _getval_or_undefined(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
293 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
294 return eval(arg, self.curframe.f_globals,
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
295 self.curframe.f_locals)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
296 except NameError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
297 return undefined
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
298
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
299 def do_watch(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
300 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
301 watch expression
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
302
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
303 Add expression to the watching list; expressions in this list
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
304 are evaluated at each step, and printed every time its value
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
305 changes.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
306
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
307 WARNING: since the expressions is evaluated multiple time, pay
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
308 attention not to put expressions with side-effects in the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
309 watching list.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
310 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
311 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
312 value = self._getval_or_undefined(arg)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
313 except:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
314 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
315 self._get_watching()[arg] = value
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
316
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
317 def do_unwatch(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
318 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
319 unwatch expression
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
320
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
321 Remove expression from the watching list.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
322 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
323 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
324 del self._get_watching()[arg]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
325 except KeyError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
326 print '** not watching %s **' % arg
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
327
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
328 def _print_if_sticky(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
329 if self.sticky:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
330 sys.stdout.write(CLEARSCREEN)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
331 frame, lineno = self.stack[self.curindex]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
332 filename = self.canonic(frame.f_code.co_filename)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
333 s = '> %s(%r)' % (filename, lineno)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
334 print s
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
335 print
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
336 sticky_range = self.sticky_ranges.get(self.curframe, None)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
337 self._printlonglist(sticky_range)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
338
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
339 def do_sticky(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
340 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
341 sticky [start end]
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
342
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
343 Toggle sticky mode. When in sticky mode, it clear the screen
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
344 and longlist the current functions, making the source
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
345 appearing always in the same position. Useful to follow the
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
346 flow control of a function when doing step-by-step execution.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
347
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
348 If ``start`` and ``end`` are given, sticky mode is enabled and
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
349 only lines within that range (extremes included) will be
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
350 displayed.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
351 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
352 if arg:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
353 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
354 start, end = map(int, arg.split())
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
355 except ValueError:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
356 print '** Error when parsing argument: %s **' % arg
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
357 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
358 self.sticky = True
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
359 self.sticky_ranges[self.curframe] = start, end+1
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
360 else:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
361 self.sticky = not self.sticky
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
362 self.sticky_range = None
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
363 self._print_if_sticky()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
364
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
365 def preloop(self):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
366 self._print_if_sticky()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
367 watching = self._get_watching()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
368 for expr, oldvalue in watching.iteritems():
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
369 newvalue = self._getval_or_undefined(expr)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
370 # check for identity first; this prevents custom __eq__ to
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
371 # be called at every loop, and also prevents instances
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
372 # whose fields are changed to be displayed
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
373 if newvalue is not oldvalue or newvalue != oldvalue:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
374 watching[expr] = newvalue
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
375 print '%s: %r --> %r' % (expr, oldvalue, newvalue)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
376
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
377 def do_source(self, arg):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
378 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
379 obj = self._getval(arg)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
380 except:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
381 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
382 try:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
383 lines, lineno = getsourcelines(obj)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
384 except (IOError, TypeError), e:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
385 print '** Error: %s **' % e
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
386 return
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
387 self._print_lines(lines, lineno, print_markers=False)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
388
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
389 # Simplified interface
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
390
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
391 # copy some functions from pdb.py, but rebind the global dictionary
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
392 for name in 'run runeval runctx runcall set_trace pm'.split():
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
393 func = getattr(pdb, name)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
394 newfunc = types.FunctionType(func.func_code, globals(), func.func_name)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
395 globals()[name] = newfunc
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
396 del name, func, newfunc
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
397
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
398 def post_mortem(t, Pdb=Pdb):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
399 p = Pdb()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
400 p.reset()
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
401 orig_tb = t
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
402 while t.tb_next is not None:
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
403 t = t.tb_next
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
404 p.interaction(t.tb_frame, t, orig_tb)
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
405
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
406 # pdb++ specific interface
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
407
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
408 def xpm(Pdb=Pdb):
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
409 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
410 To be used inside an except clause, enter a post-mortem pdb
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
411 related to the just catched exception.
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
412 """
c30d68fbd368 Initial import from svn.
Augie Fackler <durin42@gmail.com>
parents:
diff changeset
413 post_mortem(sys.exc_info()[2], Pdb)