##// 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 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()
@@ -79,27 +78,26 b' def _pythonhook(ui, repo, name, hname, f'
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
88
86
89 r = obj(ui=ui, repo=repo, hooktype=name, **args)
87 r = obj(ui=ui, repo=repo, hooktype=name, **args)
90 except KeyboardInterrupt:
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 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
General Comments 0
You need to be logged in to leave comments. Login now