##// END OF EJS Templates
Make it possible to debug failed hook imports via use of --traceback...
Bryan O'Sullivan -
r9851:9e7b2c49 default
parent child Browse files
Show More
@@ -37,10 +37,18 b' def _pythonhook(ui, repo, name, hname, f'
37 try:
37 try:
38 obj = __import__(modname)
38 obj = __import__(modname)
39 except ImportError:
39 except ImportError:
40 e1 = sys.exc_type, sys.exc_value, sys.exc_traceback
40 try:
41 try:
41 # extensions are loaded with hgext_ prefix
42 # extensions are loaded with hgext_ prefix
42 obj = __import__("hgext_%s" % modname)
43 obj = __import__("hgext_%s" % modname)
43 except ImportError:
44 except ImportError:
45 e2 = sys.exc_type, sys.exc_value, sys.exc_traceback
46 if ui.tracebackflag:
47 ui.warn(_('exception from first failed import attempt:\n'))
48 ui.traceback(e1)
49 if ui.tracebackflag:
50 ui.warn(_('exception from second failed import attempt:\n'))
51 ui.traceback(e2)
44 raise util.Abort(_('%s hook is invalid '
52 raise util.Abort(_('%s hook is invalid '
45 '(import of "%s" failed)') %
53 '(import of "%s" failed)') %
46 (hname, modname))
54 (hname, modname))
@@ -15,7 +15,7 b' import config, util, error'
15 class ui(object):
15 class ui(object):
16 def __init__(self, src=None):
16 def __init__(self, src=None):
17 self._buffers = []
17 self._buffers = []
18 self.quiet = self.verbose = self.debugflag = self._traceback = False
18 self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
19 self._reportuntrusted = True
19 self._reportuntrusted = True
20 self._ocfg = config.config() # overlay
20 self._ocfg = config.config() # overlay
21 self._tcfg = config.config() # trusted
21 self._tcfg = config.config() # trusted
@@ -101,7 +101,7 b' class ui(object):'
101 if self.verbose and self.quiet:
101 if self.verbose and self.quiet:
102 self.quiet = self.verbose = False
102 self.quiet = self.verbose = False
103 self._reportuntrusted = self.configbool("ui", "report_untrusted", True)
103 self._reportuntrusted = self.configbool("ui", "report_untrusted", True)
104 self._traceback = self.configbool('ui', 'traceback', False)
104 self.tracebackflag = self.configbool('ui', 'traceback', False)
105
105
106 # update trust information
106 # update trust information
107 self._trustusers.update(self.configlist('trusted', 'users'))
107 self._trustusers.update(self.configlist('trusted', 'users'))
@@ -337,13 +337,16 b' class ui(object):'
337
337
338 return t
338 return t
339
339
340 def traceback(self):
340 def traceback(self, exc=None):
341 '''print exception traceback if traceback printing enabled.
341 '''print exception traceback if traceback printing enabled.
342 only to call in exception handler. returns true if traceback
342 only to call in exception handler. returns true if traceback
343 printed.'''
343 printed.'''
344 if self._traceback:
344 if self.tracebackflag:
345 traceback.print_exc()
345 if exc:
346 return self._traceback
346 traceback.print_exception(exc[0], exc[1], exc[2])
347 else:
348 traceback.print_exc()
349 return self.tracebackflag
347
350
348 def geteditor(self):
351 def geteditor(self):
349 '''return editor to use'''
352 '''return editor to use'''
@@ -248,4 +248,18 b' echo "pre-commit.test = python:`pwd`/tes'
248 cd ../repo
248 cd ../repo
249 hg commit
249 hg commit
250
250
251 cd ../../b
252 echo '# make sure --traceback works on hook import failure'
253 cat > importfail.py <<EOF
254 import somebogusmodule
255 # dereference something in the module to force demandimport to load it
256 somebogusmodule.whatever
257 EOF
258
259 echo '[hooks]' > .hg/hgrc
260 echo 'precommit.importfail = python:importfail.whatever' >> .hg/hgrc
261
262 echo a >> a
263 hg --traceback commit -Ama 2>&1 | grep '^\(exception\|Traceback\|ImportError\)'
264
251 exit 0
265 exit 0
@@ -163,3 +163,11 b' hooks.commit.auto=<function autohook>'
163 # test python hook configured with python:[file]:[hook] syntax
163 # test python hook configured with python:[file]:[hook] syntax
164 hook works
164 hook works
165 nothing changed
165 nothing changed
166 # make sure --traceback works on hook import failure
167 exception from first failed import attempt:
168 Traceback (most recent call last):
169 ImportError: No module named somebogusmodule
170 exception from second failed import attempt:
171 Traceback (most recent call last):
172 ImportError: No module named hgext_importfail
173 Traceback (most recent call last):
General Comments 0
You need to be logged in to leave comments. Login now