[PATCH] hooks: print out more information when loading a python hook fails

Simon Heimberg simohe at besonet.ch
Fri Jul 6 11:42:20 CDT 2012


# HG changeset patch
# User Simon Heimberg <simohe at besonet.ch>
# Date 1341592885 -7200
# Node ID 4fc6820bda6fb92dcbbf6ca90fa26370ef5f2d1c
# Parent  fba17a64fa4978bfea19222da5e64a18cfddeecd
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 -r fba17a64fa49 -r 4fc6820bda6f mercurial/extensions.py
--- a/mercurial/extensions.py	Fre Jun 29 00:40:52 2012 -0500
+++ b/mercurial/extensions.py	Fre 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
diff -r fba17a64fa49 -r 4fc6820bda6f mercurial/hook.py
--- a/mercurial/hook.py	Fre Jun 29 00:40:52 2012 -0500
+++ b/mercurial/hook.py	Fre 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, exc:
+                        ui.write(_("loading %s hook failed:\n") % hname)
+                        raise
                     hookfn = getattr(mod, cmd)
                 else:
                     hookfn = cmd[7:].strip()
diff -r fba17a64fa49 -r 4fc6820bda6f tests/test-hook.t
--- a/tests/test-hook.t	Fre Jun 29 00:40:52 2012 -0500
+++ b/tests/test-hook.t	Fre Jul 06 18:41:25 2012 +0200
@@ -528,6 +528,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


More information about the Mercurial-devel mailing list