hooks: print out more information when loading a python hook fails
authorSimon Heimberg <simohe@besonet.ch>
Fri, 06 Jul 2012 18:41:25 +0200
changeset 17217 1b2b727a885f
parent 17216 01c1ee4bd1dd
child 17218 b8661d7c940f
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.
mercurial/extensions.py
mercurial/hook.py
tests/test-hook.t
--- a/mercurial/extensions.py	Wed Jul 18 07:51:20 2012 -0700
+++ b/mercurial/extensions.py	Fri Jul 06 18:41:25 2012 +0200
@@ -42,7 +42,12 @@
         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
--- a/mercurial/hook.py	Wed Jul 18 07:51:20 2012 -0700
+++ b/mercurial/hook.py	Fri Jul 06 18:41:25 2012 +0200
@@ -169,7 +169,11 @@
                     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()
--- a/tests/test-hook.t	Wed Jul 18 07:51:20 2012 -0700
+++ b/tests/test-hook.t	Fri Jul 06 18:41:25 2012 +0200
@@ -530,6 +530,20 @@
   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