[PATCH 2 of 2] dispatch: add ability to specify a custom pdb module as a debugger

Sean Farley sean.michael.farley at gmail.com
Wed Aug 7 13:10:17 CDT 2013


# HG changeset patch
# User Sean Farley <sean.michael.farley at gmail.com>
# Date 1374809314 18000
#      Thu Jul 25 22:28:34 2013 -0500
# Node ID 2bd946fe541c6c6a95b79fce2c17cce92a791a98
# Parent  5f4deef41fd1f5cee36d06e7b9422a87abf4664e
dispatch: add ability to specify a custom pdb module as a debugger

This adds the ability to specify a config option, ui.debugger, to a custom pdb
module, such as ipdb, and have mercurial use that as its debugger. As long as
the value of ui.debugger is a loadable module with the set_trace and
post_mortem functions, then dispatch will be able to use the custom module.

Debugging _parseconfig is still available in the case of an error since it will
be caught with a default the value of pdb.post_mortem.

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -86,10 +86,17 @@
     except ValueError:
         pass # happens if called in a thread
 
     try:
         try:
+            debugger = 'pdb'
+            debugtrace = {
+                'pdb' : pdb.set_trace
+            }
+            debugmortem = {
+                'pdb' : pdb.post_mortem
+            }
 
             # read --config before doing anything else
             # (e.g. to change trust settings for reading .hg/hgrc)
             cfgs = _parseconfig(req.ui, _earlygetopt(['--config'], req.args))
 
@@ -97,24 +104,42 @@
                 # copy configs that were passed on the cmdline (--config) to
                 # the repo ui
                 for cfg in cfgs:
                     req.repo.ui.setconfig(*cfg)
 
+            debugger = ui.config("ui", "debugger")
+            if not debugger:
+                debugger = 'pdb'
+
+            try:
+                debugmod = __import__(debugger)
+            except ImportError:
+                debugmod = pdb
+
+            debugtrace[debugger] = debugmod.set_trace
+            debugmortem[debugger] = debugmod.post_mortem
+
             # enter the debugger before command execution
             if '--debugger' in req.args:
                 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:
                 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