##// 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,7 +121,6 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 125 ip.set_custom_exc((bdb.BdbQuit,),BdbQuit_IPython_excepthook)
128 126
@@ -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,10 +164,10 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 171 def __init__(self,color_scheme='NoColor',completekey=None,
172 172 stdin=None, stdout=None):
173 173
@@ -182,6 +182,8 b' class Pdb(OldPdb):'
182 182 # IPython changes...
183 183 self.is_pydb = has_pydb
184 184
185 self.shell = ipapi.get()
186
185 187 if self.is_pydb:
186 188
187 189 # iplib.py's ipalias seems to want pdb's checkline
@@ -193,8 +195,8 b' class Pdb(OldPdb):'
193 195 self.curframe = None
194 196 self.do_restart = self.new_do_restart
195 197
196 self.old_all_completions = __IPYTHON__.Completer.all_completions
197 __IPYTHON__.Completer.all_completions=self.all_completions
198 self.old_all_completions = self.shell.Completer.all_completions
199 self.shell.Completer.all_completions=self.all_completions
198 200
199 201 self.do_list = decorate_fn_with_doc(self.list_command_pydb,
200 202 OldPdb.do_list)
@@ -227,80 +229,33 b' class Pdb(OldPdb):'
227 229 # debugging.
228 230 self.parser = PyColorize.Parser()
229 231
230
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"))
253
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()
257
258 # shorthands
259 C = coloransi.TermColors
260 cst = self.color_scheme_table
261
262 cst['NoColor'].colors.breakpoint_enabled = C.NoColor
263 cst['NoColor'].colors.breakpoint_disabled = C.NoColor
264
265 cst['Linux'].colors.breakpoint_enabled = C.LightRed
266 cst['Linux'].colors.breakpoint_disabled = C.Red
267
268 cst['LightBG'].colors.breakpoint_enabled = C.LightRed
269 cst['LightBG'].colors.breakpoint_disabled = C.Red
270
271 self.set_colors(color_scheme)
272
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