Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hook.py @ 25328:2cfb0bbf83a1
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`.
author | Jordi Guti?rrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 28 May 2015 16:42:04 -0400 |
parents | 819cd397e306 |
children | 328739ea70c3 |
comparison
equal
deleted
inserted
replaced
25327:2e7804110b14 | 25328:2cfb0bbf83a1 |
---|---|
33 # binary installs require sys.path manipulation | 33 # binary installs require sys.path manipulation |
34 modpath, modfile = os.path.split(modname) | 34 modpath, modfile = os.path.split(modname) |
35 if modpath and modfile: | 35 if modpath and modfile: |
36 sys.path = sys.path[:] + [modpath] | 36 sys.path = sys.path[:] + [modpath] |
37 modname = modfile | 37 modname = modfile |
38 demandimportenabled = demandimport.isenabled() | 38 with demandimport.deactivated(): |
39 if demandimportenabled: | |
40 demandimport.disable() | |
41 try: | |
42 obj = __import__(modname) | |
43 except ImportError: | |
44 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback | |
45 try: | 39 try: |
46 # extensions are loaded with hgext_ prefix | 40 obj = __import__(modname) |
47 obj = __import__("hgext_%s" % modname) | |
48 except ImportError: | 41 except ImportError: |
49 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback | 42 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback |
50 if ui.tracebackflag: | 43 try: |
51 ui.warn(_('exception from first failed import ' | 44 # extensions are loaded with hgext_ prefix |
52 'attempt:\n')) | 45 obj = __import__("hgext_%s" % modname) |
53 ui.traceback(e1) | 46 except ImportError: |
54 if ui.tracebackflag: | 47 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback |
55 ui.warn(_('exception from second failed import ' | 48 if ui.tracebackflag: |
56 'attempt:\n')) | 49 ui.warn(_('exception from first failed import ' |
57 ui.traceback(e2) | 50 'attempt:\n')) |
58 raise util.Abort(_('%s hook is invalid ' | 51 ui.traceback(e1) |
59 '(import of "%s" failed)') % | 52 if ui.tracebackflag: |
60 (hname, modname)) | 53 ui.warn(_('exception from second failed import ' |
61 finally: | 54 'attempt:\n')) |
62 if demandimportenabled: | 55 ui.traceback(e2) |
63 demandimport.enable() | 56 raise util.Abort(_('%s hook is invalid ' |
57 '(import of "%s" failed)') % | |
58 (hname, modname)) | |
64 sys.path = oldpaths | 59 sys.path = oldpaths |
65 try: | 60 try: |
66 for p in funcname.split('.')[1:]: | 61 for p in funcname.split('.')[1:]: |
67 obj = getattr(obj, p) | 62 obj = getattr(obj, p) |
68 except AttributeError: | 63 except AttributeError: |