##// END OF EJS Templates
Fixed the %debug magic.
Brian Granger -
Show More
@@ -70,6 +70,7 b' def BdbQuit_excepthook(et,ev,tb):'
70 70 def BdbQuit_IPython_excepthook(self,et,ev,tb):
71 71 print 'Exiting Debugger.'
72 72
73
73 74 class Tracer(object):
74 75 """Class for local debugging, similar to pdb.set_trace.
75 76
@@ -105,12 +106,10 b' class Tracer(object):'
105 106 from the Python standard library for usage details.
106 107 """
107 108
108 global __IPYTHON__
109 109 try:
110 __IPYTHON__
111 except NameError:
110 ip = ipapi.get()
111 except:
112 112 # Outside of ipython, we set our own exception hook manually
113 __IPYTHON__ = ipapi.get()
114 113 BdbQuit_excepthook.excepthook_ori = sys.excepthook
115 114 sys.excepthook = BdbQuit_excepthook
116 115 def_colors = 'NoColor'
@@ -122,9 +121,8 b' class Tracer(object):'
122 121 pass
123 122 else:
124 123 # In ipython, we use its custom exception handler mechanism
125 ip = ipapi.get()
126 124 def_colors = ip.colors
127 ip.set_custom_exc((bdb.BdbQuit,),BdbQuit_IPython_excepthook)
125 ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
128 126
129 127 if colors is None:
130 128 colors = def_colors
@@ -138,6 +136,7 b' class Tracer(object):'
138 136
139 137 self.debugger.set_trace(sys._getframe().f_back)
140 138
139
141 140 def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
142 141 """Make new_fn have old_fn's doc string. This is particularly useful
143 142 for the do_... commands that hook into the help system.
@@ -149,6 +148,7 b' def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):'
149 148 wrapper.__doc__ = old_fn.__doc__ + additional_text
150 149 return wrapper
151 150
151
152 152 def _file_lines(fname):
153 153 """Return the contents of a named file as a list of lines.
154 154
@@ -164,143 +164,98 b' def _file_lines(fname):'
164 164 outfile.close()
165 165 return out
166 166
167
167 168 class Pdb(OldPdb):
168 169 """Modified Pdb class, does not load readline."""
169 170
170 if sys.version[:3] >= '2.5' or has_pydb:
171 def __init__(self,color_scheme='NoColor',completekey=None,
172 stdin=None, stdout=None):
171 def __init__(self,color_scheme='NoColor',completekey=None,
172 stdin=None, stdout=None):
173 173
174 # Parent constructor:
175 if has_pydb and completekey is None:
176 OldPdb.__init__(self,stdin=stdin,stdout=Term.cout)
177 else:
178 OldPdb.__init__(self,completekey,stdin,stdout)
179
180 self.prompt = prompt # The default prompt is '(Pdb)'
174 # Parent constructor:
175 if has_pydb and completekey is None:
176 OldPdb.__init__(self,stdin=stdin,stdout=Term.cout)
177 else:
178 OldPdb.__init__(self,completekey,stdin,stdout)
181 179
182 # IPython changes...
183 self.is_pydb = has_pydb
184
185 if self.is_pydb:
186
187 # iplib.py's ipalias seems to want pdb's checkline
188 # which located in pydb.fn
189 import pydb.fns
190 self.checkline = lambda filename, lineno: \
191 pydb.fns.checkline(self, filename, lineno)
192
193 self.curframe = None
194 self.do_restart = self.new_do_restart
195
196 self.old_all_completions = __IPYTHON__.Completer.all_completions
197 __IPYTHON__.Completer.all_completions=self.all_completions
198
199 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
200 OldPdb.do_list)
201 self.do_l = self.do_list
202 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
203 OldPdb.do_frame)
204
205 self.aliases = {}
206
207 # Create color table: we copy the default one from the traceback
208 # module and add a few attributes needed for debugging
209 self.color_scheme_table = exception_colors()
180 self.prompt = prompt # The default prompt is '(Pdb)'
181
182 # IPython changes...
183 self.is_pydb = has_pydb
210 184
211 # shorthands
212 C = coloransi.TermColors
213 cst = self.color_scheme_table
185 self.shell = ipapi.get()
214 186
215 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
216 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
187 if self.is_pydb:
217 188
218 cst['Linux'].colors.breakpoint_enabled = C.LightRed
219 cst['Linux'].colors.breakpoint_disabled = C.Red
189 # iplib.py's ipalias seems to want pdb's checkline
190 # which located in pydb.fn
191 import pydb.fns
192 self.checkline = lambda filename, lineno: \
193 pydb.fns.checkline(self, filename, lineno)
220 194
221 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
222 cst['LightBG'].colors.breakpoint_disabled = C.Red
195 self.curframe = None
196 self.do_restart = self.new_do_restart
223 197
224 self.set_colors(color_scheme)
198 self.old_all_completions = self.shell.Completer.all_completions
199 self.shell.Completer.all_completions=self.all_completions
225 200
226 # Add a python parser so we can syntax highlight source while
227 # debugging.
228 self.parser = PyColorize.Parser()
201 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
202 OldPdb.do_list)
203 self.do_l = self.do_list
204 self.do_frame = decorate_fn_with_doc(self.new_do_frame,
205 OldPdb.do_frame)
229 206
207 self.aliases = {}
230 208
231 else:
232 # Ugly hack: for Python 2.3-2.4, we can't call the parent constructor,
233 # because it binds readline and breaks tab-completion. This means we
234 # have to COPY the constructor here.
235 def __init__(self,color_scheme='NoColor'):
236 bdb.Bdb.__init__(self)
237 cmd.Cmd.__init__(self,completekey=None) # don't load readline
238 self.prompt = 'ipdb> ' # The default prompt is '(Pdb)'
239 self.aliases = {}
240
241 # These two lines are part of the py2.4 constructor, let's put them
242 # unconditionally here as they won't cause any problems in 2.3.
243 self.mainpyfile = ''
244 self._wait_for_mainpyfile = 0
245
246 # Read $HOME/.pdbrc and ./.pdbrc
247 try:
248 self.rcLines = _file_lines(os.path.join(os.environ['HOME'],
249 ".pdbrc"))
250 except KeyError:
251 self.rcLines = []
252 self.rcLines.extend(_file_lines(".pdbrc"))
209 # Create color table: we copy the default one from the traceback
210 # module and add a few attributes needed for debugging
211 self.color_scheme_table = exception_colors()
253 212
254 # Create color table: we copy the default one from the traceback
255 # module and add a few attributes needed for debugging
256 self.color_scheme_table = exception_colors()
213 # shorthands
214 C = coloransi.TermColors
215 cst = self.color_scheme_table
257 216
258 # shorthands
259 C = coloransi.TermColors
260 cst = self.color_scheme_table
217 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
218 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
261 219
262 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
263 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
220 cst['Linux'].colors.breakpoint_enabled = C.LightRed
221 cst['Linux'].colors.breakpoint_disabled = C.Red
264 222
265 cst['Linux'].colors.breakpoint_enabled = C.LightRed
266 cst['Linux'].colors.breakpoint_disabled = C.Red
223 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
224 cst['LightBG'].colors.breakpoint_disabled = C.Red
267 225
268 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
269 cst['LightBG'].colors.breakpoint_disabled = C.Red
226 self.set_colors(color_scheme)
270 227
271 self.set_colors(color_scheme)
228 # Add a python parser so we can syntax highlight source while
229 # debugging.
230 self.parser = PyColorize.Parser()
272 231
273 # Add a python parser so we can syntax highlight source while
274 # debugging.
275 self.parser = PyColorize.Parser()
276
277 232 def set_colors(self, scheme):
278 233 """Shorthand access to the color table scheme selector method."""
279 234 self.color_scheme_table.set_active_scheme(scheme)
280 235
281 236 def interaction(self, frame, traceback):
282 __IPYTHON__.set_completer_frame(frame)
237 self.shell.set_completer_frame(frame)
283 238 OldPdb.interaction(self, frame, traceback)
284 239
285 240 def new_do_up(self, arg):
286 241 OldPdb.do_up(self, arg)
287 __IPYTHON__.set_completer_frame(self.curframe)
242 self.shell.set_completer_frame(self.curframe)
288 243 do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
289 244
290 245 def new_do_down(self, arg):
291 246 OldPdb.do_down(self, arg)
292 __IPYTHON__.set_completer_frame(self.curframe)
247 self.shell.set_completer_frame(self.curframe)
293 248
294 249 do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
295 250
296 251 def new_do_frame(self, arg):
297 252 OldPdb.do_frame(self, arg)
298 __IPYTHON__.set_completer_frame(self.curframe)
253 self.shell.set_completer_frame(self.curframe)
299 254
300 255 def new_do_quit(self, arg):
301 256
302 257 if hasattr(self, 'old_all_completions'):
303 __IPYTHON__.Completer.all_completions=self.old_all_completions
258 self.shell.Completer.all_completions=self.old_all_completions
304 259
305 260
306 261 return OldPdb.do_quit(self, arg)
@@ -314,7 +269,7 b' class Pdb(OldPdb):'
314 269 return self.do_quit(arg)
315 270
316 271 def postloop(self):
317 __IPYTHON__.set_completer_frame(None)
272 self.shell.set_completer_frame(None)
318 273
319 274 def print_stack_trace(self):
320 275 try:
@@ -331,7 +286,7 b' class Pdb(OldPdb):'
331 286 # vds: >>
332 287 frame, lineno = frame_lineno
333 288 filename = frame.f_code.co_filename
334 __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0)
289 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
335 290 # vds: <<
336 291
337 292 def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
@@ -500,7 +455,7 b' class Pdb(OldPdb):'
500 455 # vds: >>
501 456 lineno = first
502 457 filename = self.curframe.f_code.co_filename
503 __IPYTHON__.hooks.synchronize_with_editor(filename, lineno, 0)
458 self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
504 459 # vds: <<
505 460
506 461 do_l = do_list
@@ -509,16 +464,16 b' class Pdb(OldPdb):'
509 464 """The debugger interface to magic_pdef"""
510 465 namespaces = [('Locals', self.curframe.f_locals),
511 466 ('Globals', self.curframe.f_globals)]
512 __IPYTHON__.magic_pdef(arg, namespaces=namespaces)
467 self.shell.magic_pdef(arg, namespaces=namespaces)
513 468
514 469 def do_pdoc(self, arg):
515 470 """The debugger interface to magic_pdoc"""
516 471 namespaces = [('Locals', self.curframe.f_locals),
517 472 ('Globals', self.curframe.f_globals)]
518 __IPYTHON__.magic_pdoc(arg, namespaces=namespaces)
473 self.shell.magic_pdoc(arg, namespaces=namespaces)
519 474
520 475 def do_pinfo(self, arg):
521 476 """The debugger equivalant of ?obj"""
522 477 namespaces = [('Locals', self.curframe.f_locals),
523 478 ('Globals', self.curframe.f_globals)]
524 __IPYTHON__.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
479 self.shell.magic_pinfo("pinfo %s" % arg, namespaces=namespaces)
@@ -233,14 +233,6 b' class InteractiveShellEmbed(InteractiveShell):'
233 233 for var in local_varnames:
234 234 delvar(var,None)
235 235
236 def set_completer_frame(self, frame=None):
237 if frame:
238 self.Completer.namespace = frame.f_locals
239 self.Completer.global_namespace = frame.f_globals
240 else:
241 self.Completer.namespace = self.user_ns
242 self.Completer.global_namespace = self.user_global_ns
243
244 236
245 237 _embedded_shell = None
246 238
@@ -1426,9 +1426,7 b' class InteractiveShell(Component, Magic):'
1426 1426 return outcomps
1427 1427
1428 1428 def set_custom_completer(self,completer,pos=0):
1429 """set_custom_completer(completer,pos=0)
1430
1431 Adds a new custom completer function.
1429 """Adds a new custom completer function.
1432 1430
1433 1431 The position argument (defaults to 0) is the index in the completers
1434 1432 list where you want the completer to be inserted."""
@@ -1438,9 +1436,18 b' class InteractiveShell(Component, Magic):'
1438 1436 self.Completer.matchers.insert(pos,newcomp)
1439 1437
1440 1438 def set_completer(self):
1441 """reset readline's completer to be our own."""
1439 """Reset readline's completer to be our own."""
1442 1440 self.readline.set_completer(self.Completer.complete)
1443 1441
1442 def set_completer_frame(self, frame=None):
1443 """Set the frame of the completer."""
1444 if frame:
1445 self.Completer.namespace = frame.f_locals
1446 self.Completer.global_namespace = frame.f_globals
1447 else:
1448 self.Completer.namespace = self.user_ns
1449 self.Completer.global_namespace = self.user_global_ns
1450
1444 1451 #-------------------------------------------------------------------------
1445 1452 # Things related to readline
1446 1453 #-------------------------------------------------------------------------
General Comments 0
You need to be logged in to leave comments. Login now