contrib/debugshell.py
author Gregory Szorc <gregory.szorc@gmail.com>
Mon, 11 Jan 2016 18:16:38 -0800
changeset 27721 e4b512bb6386
parent 21243 8b5c039f2b4f
child 28476 e28dc6de38e7
permissions -rw-r--r--
debugshell: disable demand importer when importing debugger For reasons I can't explain (but likely have something to do with a combination of __import__ inferring default values for arguments and the demand importer mechanism further assuming defaults), the demand importer isn't playing well with IPython. Without this patch, we get a failure "ValueError: Attempted relative import in non-package" when attempting to import "IPython." The stack has numerous demandimport calls on it and adding "IPython" to the exclude list in demandimport isn't enough to make the problem go away, which means the issue is likely somewhere in the bowells of IPython. It's easier to just disable the demand importer when importing the debugger.

# debugshell extension
"""a python shell with repo, changelog & manifest objects"""

import sys
import mercurial
import code
from mercurial import (
    cmdutil,
    demandimport,
)

cmdtable = {}
command = cmdutil.command(cmdtable)

def pdb(ui, repo, msg, **opts):
    objects = {
        'mercurial': mercurial,
        'repo': repo,
        'cl': repo.changelog,
        'mf': repo.manifest,
    }

    code.interact(msg, local=objects)

def ipdb(ui, repo, msg, **opts):
    import IPython

    cl = repo.changelog
    mf = repo.manifest
    cl, mf # use variables to appease pyflakes

    IPython.embed()

@command('debugshell|dbsh', [])
def debugshell(ui, repo, **opts):
    bannermsg = "loaded repo : %s\n" \
                "using source: %s" % (repo.root,
                                      mercurial.__path__[0])

    pdbmap = {
        'pdb'  : 'code',
        'ipdb' : 'IPython'
    }

    debugger = ui.config("ui", "debugger")
    if not debugger:
        debugger = 'pdb'

    # if IPython doesn't exist, fallback to code.interact
    try:
        with demandimport.deactivated():
            __import__(pdbmap[debugger])
    except ImportError:
        ui.warn("%s debugger specified but %s module was not found\n"
                % (debugger, pdbmap[debugger]))
        debugger = 'pdb'

    getattr(sys.modules[__name__], debugger)(ui, repo, bannermsg, **opts)