[PATCH v2] dispatch: refactor tortured debugger setup logic

Bryan O'Sullivan bos at serpentine.com
Fri Jan 1 22:46:23 UTC 2016


# HG changeset patch
# User Bryan O'Sullivan <bos at serpentine.com>
# Date 1451688323 28800
#      Fri Jan 01 14:45:23 2016 -0800
# Node ID f019056c88a34d9cf9d01e10f1354e049ce07521
# Parent  44f717c879033f28d3f7e7dc9f34aa394d2fea3f
dispatch: refactor tortured debugger setup logic

This patch was triggered by observing that the pdb debugger was
always loaded regardless of whether a debugger was to be used.

The logic as it existed was a thicket of accidental complexity.
It's now much easier to follow (and only loads a debugger if needed).

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -137,14 +137,6 @@ def _runcatch(req):
 
     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))
@@ -155,13 +147,13 @@ def _runcatch(req):
                 for sec, name, val in cfgs:
                     req.repo.ui.setconfig(sec, name, val, source='--config')
 
-            # developer config: ui.debugger
-            debugger = ui.config("ui", "debugger")
-            debugmod = pdb
-            if not debugger or ui.plain():
-                # if we are in HGPLAIN mode, then disable custom debugging
+            if '--debugger' in req.args:
+                # if we are in HGPLAIN mode, ignore custom debugging
                 debugger = 'pdb'
-            elif '--debugger' in req.args:
+                if not ui.plain():
+                    # developer config: ui.debugger
+                    debugger = ui.config('ui', 'debugger', default='pdb')
+
                 # This import can be slow for fancy debuggers, so only
                 # do it when absolutely necessary, i.e. when actual
                 # debugging has been requested
@@ -169,22 +161,15 @@ def _runcatch(req):
                     try:
                         debugmod = __import__(debugger)
                     except ImportError:
-                        pass # Leave debugmod = pdb
+                        ui.warn(_("%s debugger specified "
+                                  "but its module was not found\n") % debugger)
+                        debugmod = pdb
 
-            debugtrace[debugger] = debugmod.set_trace
-            debugmortem[debugger] = debugmod.post_mortem
+                    # enter the debugger before command execution
+                    ui.warn(_("entering debugger - "
+                              "type c to continue starting hg or h for help\n"))
 
-            # 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"))
-
-                if (debugger != 'pdb' and
-                    debugtrace[debugger] == debugtrace['pdb']):
-                    ui.warn(_("%s debugger specified "
-                              "but its module was not found\n") % debugger)
-                with demandimport.deactivated():
-                    debugtrace[debugger]()
+                    debugmod.set_trace()
             try:
                 return _dispatch(req)
             finally:
@@ -193,7 +178,7 @@ def _runcatch(req):
             # enter the debugger when we hit an exception
             if '--debugger' in req.args:
                 traceback.print_exc()
-                debugmortem[debugger](sys.exc_info()[2])
+                debugmod.post_mortem(sys.exc_info()[2])
             ui.traceback()
             raise
 


More information about the Mercurial-devel mailing list