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