# HG changeset patch # User Simon Heimberg # Date 2012-07-06 16:41:25 # Node ID 1b2b727a885f0fc71f36afe6a0b15e8c6bd823f3 # Parent 01c1ee4bd1dd1eb60307575b4c603fe9511ce50c hooks: print out more information when loading a python hook fails When loading a python hook with file syntax fails, there is no information that this happened while loading a hook. When the python file does not exist even the file name is not printed. (Only that a file is missing.) This patch adds this information and a test for loading a non existing file and a directory not being a python module. diff --git a/mercurial/extensions.py b/mercurial/extensions.py --- a/mercurial/extensions.py +++ b/mercurial/extensions.py @@ -42,7 +42,12 @@ def loadpath(path, module_name): fd, fpath, desc = imp.find_module(f, [d]) return imp.load_module(module_name, fd, fpath, desc) else: - return imp.load_source(module_name, path) + try: + return imp.load_source(module_name, path) + except IOError, exc: + if not exc.filename: + exc.filename = path # python does not fill this + raise def load(ui, name, path): # unused ui argument kept for backwards compatibility diff --git a/mercurial/hook.py b/mercurial/hook.py --- a/mercurial/hook.py +++ b/mercurial/hook.py @@ -169,7 +169,11 @@ def hook(ui, repo, name, throw=False, ** path = util.expandpath(path) if repo: path = os.path.join(repo.root, path) - mod = extensions.loadpath(path, 'hghook.%s' % hname) + try: + mod = extensions.loadpath(path, 'hghook.%s' % hname) + except Exception: + ui.write(_("loading %s hook failed:\n") % hname) + raise hookfn = getattr(mod, cmd) else: hookfn = cmd[7:].strip() diff --git a/tests/test-hook.t b/tests/test-hook.t --- a/tests/test-hook.t +++ b/tests/test-hook.t @@ -530,6 +530,20 @@ test python hook configured with python: nothing changed [1] + $ echo '[hooks]' > .hg/hgrc + $ echo "update.ne = python:`pwd`/nonexisting.py:testhook" >> .hg/hgrc + $ echo "pre-identify.npmd = python:`pwd`/:no_python_module_dir" >> .hg/hgrc + + $ hg up null + loading update.ne hook failed: + abort: No such file or directory: $TESTTMP/d/repo/nonexisting.py + [255] + + $ hg id + loading pre-identify.npmd hook failed: + abort: No module named repo! + [255] + $ cd ../../b make sure --traceback works on hook import failure