Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hook.py @ 25084:7046c7e7fcb4
hooks: use try/except/finally
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Fri, 15 May 2015 09:56:43 -0500 |
parents | 2abbf4750915 |
children | 819cd397e306 |
comparison
equal
deleted
inserted
replaced
25083:ef36536abea3 | 25084:7046c7e7fcb4 |
---|---|
37 modname = modfile | 37 modname = modfile |
38 demandimportenabled = demandimport.isenabled() | 38 demandimportenabled = demandimport.isenabled() |
39 if demandimportenabled: | 39 if demandimportenabled: |
40 demandimport.disable() | 40 demandimport.disable() |
41 try: | 41 try: |
42 obj = __import__(modname) | |
43 except ImportError: | |
44 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback | |
42 try: | 45 try: |
43 obj = __import__(modname) | 46 # extensions are loaded with hgext_ prefix |
47 obj = __import__("hgext_%s" % modname) | |
44 except ImportError: | 48 except ImportError: |
45 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback | 49 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback |
46 try: | 50 if ui.tracebackflag: |
47 # extensions are loaded with hgext_ prefix | 51 ui.warn(_('exception from first failed import ' |
48 obj = __import__("hgext_%s" % modname) | 52 'attempt:\n')) |
49 except ImportError: | 53 ui.traceback(e1) |
50 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback | 54 if ui.tracebackflag: |
51 if ui.tracebackflag: | 55 ui.warn(_('exception from second failed import ' |
52 ui.warn(_('exception from first failed import ' | 56 'attempt:\n')) |
53 'attempt:\n')) | 57 ui.traceback(e2) |
54 ui.traceback(e1) | 58 raise util.Abort(_('%s hook is invalid ' |
55 if ui.tracebackflag: | 59 '(import of "%s" failed)') % |
56 ui.warn(_('exception from second failed import ' | 60 (hname, modname)) |
57 'attempt:\n')) | |
58 ui.traceback(e2) | |
59 raise util.Abort(_('%s hook is invalid ' | |
60 '(import of "%s" failed)') % | |
61 (hname, modname)) | |
62 finally: | 61 finally: |
63 if demandimportenabled: | 62 if demandimportenabled: |
64 demandimport.enable() | 63 demandimport.enable() |
65 sys.path = oldpaths | 64 sys.path = oldpaths |
66 try: | 65 try: |
77 | 76 |
78 ui.note(_("calling hook %s: %s\n") % (hname, funcname)) | 77 ui.note(_("calling hook %s: %s\n") % (hname, funcname)) |
79 starttime = time.time() | 78 starttime = time.time() |
80 | 79 |
81 try: | 80 try: |
82 try: | 81 # redirect IO descriptors to the ui descriptors so hooks |
83 # redirect IO descriptors to the ui descriptors so hooks | 82 # that write directly to these don't mess up the command |
84 # that write directly to these don't mess up the command | 83 # protocol when running through the command server |
85 # protocol when running through the command server | 84 old = sys.stdout, sys.stderr, sys.stdin |
86 old = sys.stdout, sys.stderr, sys.stdin | 85 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin |
87 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin | 86 |
88 | 87 r = obj(ui=ui, repo=repo, hooktype=name, **args) |
89 r = obj(ui=ui, repo=repo, hooktype=name, **args) | 88 except KeyboardInterrupt: |
90 except KeyboardInterrupt: | 89 raise |
90 except Exception, exc: | |
91 if isinstance(exc, util.Abort): | |
92 ui.warn(_('error: %s hook failed: %s\n') % | |
93 (hname, exc.args[0])) | |
94 else: | |
95 ui.warn(_('error: %s hook raised an exception: ' | |
96 '%s\n') % (hname, exc)) | |
97 if throw: | |
91 raise | 98 raise |
92 except Exception, exc: | 99 ui.traceback() |
93 if isinstance(exc, util.Abort): | 100 return True |
94 ui.warn(_('error: %s hook failed: %s\n') % | |
95 (hname, exc.args[0])) | |
96 else: | |
97 ui.warn(_('error: %s hook raised an exception: ' | |
98 '%s\n') % (hname, exc)) | |
99 if throw: | |
100 raise | |
101 ui.traceback() | |
102 return True | |
103 finally: | 101 finally: |
104 sys.stdout, sys.stderr, sys.stdin = old | 102 sys.stdout, sys.stderr, sys.stdin = old |
105 duration = time.time() - starttime | 103 duration = time.time() - starttime |
106 ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n', | 104 ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n', |
107 name, funcname, duration) | 105 name, funcname, duration) |