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

login
register
mail settings
Submitter Sean Farley
Date July 13, 2013, 10:16 p.m.
Message ID <42113a6e12bc864e044b.1373753816@laptop.local>
Download mbox | patch
Permalink /patch/1880/
State Superseded
Commit 472fa3b782b1d33d12738c288beac6f7a78e0514
Headers show

Comments

Sean Farley - July 13, 2013, 10:16 p.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1373752000 18000
#      Sat Jul 13 16:46:40 2013 -0500
# Node ID 42113a6e12bc864e044bacb57407a68a97d7f1da
# Parent  15a903bca551318cd0c3f42484374a8b8acd08e5
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.
Augie Fackler - July 16, 2013, 3:21 p.m.
On Sat, Jul 13, 2013 at 05:16:56PM -0500, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1373752000 18000
> #      Sat Jul 13 16:46:40 2013 -0500
> # Node ID 42113a6e12bc864e044bacb57407a68a97d7f1da
> # Parent  15a903bca551318cd0c3f42484374a8b8acd08e5
> dispatch: add ability to specify a custom pdb module as a debugger

+1 in principle, not willing to queue because dispatch makes my head
spin a little.

>
> 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.
>
> diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
> --- a/mercurial/dispatch.py
> +++ b/mercurial/dispatch.py
> @@ -97,24 +97,49 @@
>                  # 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:
> +                ipdb = __import__(debugger)
> +            except ImportError:
> +                ipdb = pdb
> +
> +            debugtrace = {
> +                'pdb'    : pdb.set_trace,
> +                debugger : ipdb.set_trace,
> +            }
> +
> +            debugmortem = {
> +                'pdb'    : pdb.post_mortem,
> +                debugger : ipdb.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
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
Sean Farley - Aug. 7, 2013, 8:45 p.m.
kbullock+mercurial@ringworld.org writes:

> On 13 Jul 2013, at 5:16 PM, Sean Farley wrote:
>
>> # HG changeset patch
>> # User Sean Farley <sean.michael.farley@gmail.com>
>> # Date 1373752000 18000
>> #      Sat Jul 13 16:46:40 2013 -0500
>> # Node ID 42113a6e12bc864e044bacb57407a68a97d7f1da
>> # Parent  15a903bca551318cd0c3f42484374a8b8acd08e5
>> 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.
>> 
>> diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
>> --- a/mercurial/dispatch.py
>> +++ b/mercurial/dispatch.py
>> @@ -97,24 +97,49 @@
>>                 # 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:
>> +                ipdb = __import__(debugger)
>> +            except ImportError:
>> +                ipdb = pdb
>
> Can't you just:
>
>             try:
>                 debugmod = __import__(debugger)
>             except ImportError:
>                 debugmod = pdb
>
> and adjust your dispatch tables appropriately from there, so that they can use 'ipdb' as the key directly where appropriate?

I think I see what you're saying here but I didn't use 'ipdb' as a key
because I wanted the config option to remain flexible. The code, as I
have it written, will work with 'debugger = foo' as long as the foo
module has the same functions as pdb.

Anyways, I've already sent V2 so I guess I'll look there for feedback.

Patch

diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -97,24 +97,49 @@ 
                 # 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:
+                ipdb = __import__(debugger)
+            except ImportError:
+                ipdb = pdb
+
+            debugtrace = {
+                'pdb'    : pdb.set_trace,
+                debugger : ipdb.set_trace,
+            }
+
+            debugmortem = {
+                'pdb'    : pdb.post_mortem,
+                debugger : ipdb.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