hooks: replace if-try-finally with a "with" statement
authorJordi Gutiérrez Hermoso <jordigh@octave.org>
Thu, 28 May 2015 16:42:04 -0400
changeset 25328 2cfb0bbf83a1
parent 25327 2e7804110b14
child 25329 101e84121c13
hooks: replace if-try-finally with a "with" statement This seems like a textbook case for the new demandimport.deactivated context manager: check if something must be done, do it, and cleanup at the end regardless of exceptions. The diff isn't as bad as it seems. It's just all the whitespace changes due to needing an extra level of indentation. It looks cleaner with `hg diff -w`.
mercurial/hook.py
--- a/mercurial/hook.py	Thu May 28 16:11:26 2015 -0400
+++ b/mercurial/hook.py	Thu May 28 16:42:04 2015 -0400
@@ -35,32 +35,27 @@
             if modpath and modfile:
                 sys.path = sys.path[:] + [modpath]
                 modname = modfile
-        demandimportenabled = demandimport.isenabled()
-        if demandimportenabled:
-            demandimport.disable()
-        try:
-            obj = __import__(modname)
-        except ImportError:
-            e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
+        with demandimport.deactivated():
             try:
-                # extensions are loaded with hgext_ prefix
-                obj = __import__("hgext_%s" % modname)
+                obj = __import__(modname)
             except ImportError:
-                e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
-                if ui.tracebackflag:
-                    ui.warn(_('exception from first failed import '
-                              'attempt:\n'))
-                ui.traceback(e1)
-                if ui.tracebackflag:
-                    ui.warn(_('exception from second failed import '
-                              'attempt:\n'))
-                ui.traceback(e2)
-                raise util.Abort(_('%s hook is invalid '
-                                   '(import of "%s" failed)') %
-                                 (hname, modname))
-        finally:
-            if demandimportenabled:
-                demandimport.enable()
+                e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
+                try:
+                    # extensions are loaded with hgext_ prefix
+                    obj = __import__("hgext_%s" % modname)
+                except ImportError:
+                    e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
+                    if ui.tracebackflag:
+                        ui.warn(_('exception from first failed import '
+                                  'attempt:\n'))
+                    ui.traceback(e1)
+                    if ui.tracebackflag:
+                        ui.warn(_('exception from second failed import '
+                                  'attempt:\n'))
+                    ui.traceback(e2)
+                    raise util.Abort(_('%s hook is invalid '
+                                       '(import of "%s" failed)') %
+                                     (hname, modname))
         sys.path = oldpaths
         try:
             for p in funcname.split('.')[1:]: