##// END OF EJS Templates
hooks: use try/except/finally
Matt Mackall -
r25084:7046c7e7 default
parent child Browse files
Show More
@@ -39,26 +39,25 b' def _pythonhook(ui, repo, name, hname, f'
39 39 if demandimportenabled:
40 40 demandimport.disable()
41 41 try:
42 obj = __import__(modname)
43 except ImportError:
44 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
42 45 try:
43 obj = __import__(modname)
46 # extensions are loaded with hgext_ prefix
47 obj = __import__("hgext_%s" % modname)
44 48 except ImportError:
45 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
46 try:
47 # extensions are loaded with hgext_ prefix
48 obj = __import__("hgext_%s" % modname)
49 except ImportError:
50 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
51 if ui.tracebackflag:
52 ui.warn(_('exception from first failed import '
53 'attempt:\n'))
54 ui.traceback(e1)
55 if ui.tracebackflag:
56 ui.warn(_('exception from second failed import '
57 'attempt:\n'))
58 ui.traceback(e2)
59 raise util.Abort(_('%s hook is invalid '
60 '(import of "%s" failed)') %
61 (hname, modname))
49 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
50 if ui.tracebackflag:
51 ui.warn(_('exception from first failed import '
52 'attempt:\n'))
53 ui.traceback(e1)
54 if ui.tracebackflag:
55 ui.warn(_('exception from second failed import '
56 'attempt:\n'))
57 ui.traceback(e2)
58 raise util.Abort(_('%s hook is invalid '
59 '(import of "%s" failed)') %
60 (hname, modname))
62 61 finally:
63 62 if demandimportenabled:
64 63 demandimport.enable()
@@ -79,27 +78,26 b' def _pythonhook(ui, repo, name, hname, f'
79 78 starttime = time.time()
80 79
81 80 try:
82 try:
83 # redirect IO descriptors to the ui descriptors so hooks
84 # that write directly to these don't mess up the command
85 # protocol when running through the command server
86 old = sys.stdout, sys.stderr, sys.stdin
87 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin
81 # redirect IO descriptors to the ui descriptors so hooks
82 # that write directly to these don't mess up the command
83 # protocol when running through the command server
84 old = sys.stdout, sys.stderr, sys.stdin
85 sys.stdout, sys.stderr, sys.stdin = ui.fout, ui.ferr, ui.fin
88 86
89 r = obj(ui=ui, repo=repo, hooktype=name, **args)
90 except KeyboardInterrupt:
87 r = obj(ui=ui, repo=repo, hooktype=name, **args)
88 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 98 raise
92 except Exception, exc:
93 if isinstance(exc, util.Abort):
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
99 ui.traceback()
100 return True
103 101 finally:
104 102 sys.stdout, sys.stderr, sys.stdin = old
105 103 duration = time.time() - starttime
General Comments 0
You need to be logged in to leave comments. Login now