Patchwork [stable,V2] hook: disable demandimport before importing hooks

login
register
mail settings
Submitter Dirkjan Ochtman
Date Dec. 20, 2012, 8:27 p.m.
Message ID <3b56bf3b05f70052ae8c.1356035228@hertz>
Download mbox | patch
Permalink /patch/224/
State Accepted
Commit d7c28954d9010d639de9ea856c29ff3889440c3f
Headers show

Comments

Dirkjan Ochtman - Dec. 20, 2012, 8:27 p.m.
# HG changeset patch
# User Dirkjan Ochtman <dirkjan at ochtman.nl>
# Date 1356035190 -3600
# Node ID 3b56bf3b05f70052ae8c3d09ee7c11feb84a8986
# Parent  e853d27956fb0a25dcaac29b18bb0d5719297830
hook: disable demandimport before importing hooks

This solved an obscure bug for me. In upgrading Distribute on the server, a
patch was added to has a try: import a except ImportError thing that's only
supposed to work with Python 3.3. I'm using 2.7. My hook failed with an
ImportError because of this. It seems kind of sensible to turn off
demandimport before importing the hook, since the except ImportError pattern
is used quite a bit in Python code (including in other Distribute code).
Matt Mackall - Dec. 20, 2012, 10:19 p.m.
On Thu, 2012-12-20 at 21:27 +0100, Dirkjan Ochtman wrote:
> # HG changeset patch
> # User Dirkjan Ochtman <dirkjan at ochtman.nl>
> # Date 1356035190 -3600
> # Node ID 3b56bf3b05f70052ae8c3d09ee7c11feb84a8986
> # Parent  e853d27956fb0a25dcaac29b18bb0d5719297830
> hook: disable demandimport before importing hooks
> 
> This solved an obscure bug for me. In upgrading Distribute on the server, a
> patch was added to has a try: import a except ImportError thing that's only
> supposed to work with Python 3.3. I'm using 2.7. My hook failed with an
> ImportError because of this. It seems kind of sensible to turn off
> demandimport before importing the hook, since the except ImportError pattern
> is used quite a bit in Python code (including in other Distribute code).

I'd rather document this as a caveat of writing in-process Mercurial
hooks, if only to further discourage people from such an undertaking.

Patch

diff --git a/mercurial/hook.py b/mercurial/hook.py
--- a/mercurial/hook.py
+++ b/mercurial/hook.py
@@ -7,7 +7,7 @@ 
 
 from i18n import _
 import os, sys
-import extensions, util
+import extensions, util, demandimport
 
 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
     '''call python hook. hook is callable object, looked up as
@@ -35,13 +35,17 @@ 
                 sys.path = sys.path[:] + [modpath]
                 modname = modfile
         try:
+            demandimport.disable()
             obj = __import__(modname)
+            demandimport.enable()
         except ImportError:
             e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
             try:
                 # extensions are loaded with hgext_ prefix
                 obj = __import__("hgext_%s" % modname)
+                demandimport.enable()
             except ImportError:
+                demandimport.enable()
                 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
                 if ui.tracebackflag:
                     ui.warn(_('exception from first failed import attempt:\n'))