[PATCH 01 of 14 RFC] dispatch: add ability to specify ipdb as a debugger

Sean Farley sean.michael.farley at gmail.com
Tue Jul 9 16:54:32 CDT 2013


# HG changeset patch
# User Sean Farley <sean.michael.farley at gmail.com>
# Date 1372876982 18000
#      Wed Jul 03 13:43:02 2013 -0500
# Node ID 2bda9c94e65cff8c6968d82cfc07ec6049b194c0
# Parent  648d1974b3f328947ee6cf2d00c66815a33cd208
dispatch: add ability to specify ipdb as a debugger

Question: is there a better way to test a module's availability with
demandimport?

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -37,11 +37,11 @@
     ('v', 'verbose', None, _('enable additional output')),
     ('', 'config', [],
      _('set/override config option (use \'section.name=value\')'),
      _('CONFIG')),
     ('', 'debug', None, _('enable debugging output')),
-    ('', 'debugger', None, _('start debugger')),
+    ('', 'debugger', 'pdb', _('start debugger')),
     ('', 'encoding', encoding.encoding, _('set the charset encoding'),
      _('ENCODE')),
     ('', 'encodingmode', encoding.encodingmode,
      _('set the charset encoding mode'), _('MODE')),
     ('', 'traceback', None, _('always print a traceback on exception')),
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -9,10 +9,16 @@
 import os, sys, atexit, signal, pdb, socket, errno, shlex, time, traceback, re
 import util, commands, hg, fancyopts, extensions, hook, error
 import cmdutil, encoding
 import ui as uimod
 
+try:
+    import ipdb
+    ipdb.set_trace # should we really be forcing this load in dispatch.py?
+except ImportError:
+    ipdb = pdb
+
 class request(object):
     def __init__(self, args, ui=None, repo=None, fin=None, fout=None,
                  ferr=None):
         self.args = args
         self.ui = ui
@@ -84,26 +90,48 @@
             if num:
                 signal.signal(num, catchterm)
     except ValueError:
         pass # happens if called in a thread
 
+    debugger = None
+    if '--debugger' in req.args:
+        debugger = 'pdb'
+    debugarg = _earlygetopt(['--debugger'], req.args)
+    if debugarg:
+        debugger = debugarg[0]
+
+    debugtrace = {
+        'pdb'  : pdb.set_trace,
+        'ipdb' : ipdb.set_trace,
+    }
+
+    debugmortem = {
+        'pdb'  : pdb.post_mortem,
+        'ipdb' : ipdb.post_mortem,
+    }
+
     try:
         try:
             # enter the debugger before command execution
-            if '--debugger' in req.args:
+            if debugger:
                 ui.warn(_("entering debugger - "
                         "type c to continue starting hg or h for help\n"))
-                pdb.set_trace()
+
+                if debugger != 'pdb' and debugtrace[debugger] == debugtrace['pdb']:
+                    ui.warn(_("%s debugger specified "
+                              "but its module was not found\n") % debugger)
+
+                debugtrace[debugger]()
             try:
                 return _dispatch(req)
             finally:
                 ui.flush()
         except: # re-raises
             # enter the debugger when we hit an exception
-            if '--debugger' in req.args:
+            if debugger:
                 traceback.print_exc()
-                pdb.post_mortem(sys.exc_info()[2])
+                debugmortem[debugger](sys.exc_info()[2])
             ui.traceback()
             raise
 
     # Global exception handling, alphabetically
     # Mercurial-specific first, followed by built-in and library exceptions


More information about the Mercurial-devel mailing list