logexceptions.py
75 lines
| 1.8 KiB
| text/x-python
|
PythonLexer
/ tests / logexceptions.py
Gregory Szorc
|
r35191 | # logexceptions.py - Write files containing info about Mercurial exceptions | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2017 Olivia Mackall <olivia@selenic.com> | ||
Gregory Szorc
|
r35191 | # | ||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
import inspect | ||||
import os | ||||
import sys | ||||
import traceback | ||||
import uuid | ||||
from mercurial import ( | ||||
dispatch, | ||||
extensions, | ||||
) | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r35191 | def handleexception(orig, ui): | ||
res = orig(ui) | ||||
if not ui.environ.get(b'HGEXCEPTIONSDIR'): | ||||
return res | ||||
Augie Fackler
|
r43346 | dest = os.path.join( | ||
ui.environ[b'HGEXCEPTIONSDIR'], str(uuid.uuid4()).encode('ascii') | ||||
) | ||||
Gregory Szorc
|
r35191 | |||
exc_type, exc_value, exc_tb = sys.exc_info() | ||||
stack = [] | ||||
tb = exc_tb | ||||
while tb: | ||||
stack.append(tb) | ||||
tb = tb.tb_next | ||||
stack.reverse() | ||||
hgframe = 'unknown' | ||||
hgline = 'unknown' | ||||
# Find the first Mercurial frame in the stack. | ||||
for tb in stack: | ||||
mod = inspect.getmodule(tb) | ||||
if not mod.__name__.startswith(('hg', 'mercurial')): | ||||
continue | ||||
frame = tb.tb_frame | ||||
try: | ||||
with open(inspect.getsourcefile(tb), 'r') as fh: | ||||
hgline = fh.readlines()[frame.f_lineno - 1].strip() | ||||
except (IndexError, OSError): | ||||
pass | ||||
hgframe = '%s:%d' % (frame.f_code.co_filename, frame.f_lineno) | ||||
break | ||||
primary = traceback.extract_tb(exc_tb)[-1] | ||||
primaryframe = '%s:%d' % (primary.filename, primary.lineno) | ||||
with open(dest, 'wb') as fh: | ||||
parts = [ | ||||
str(exc_value), | ||||
primaryframe, | ||||
hgframe, | ||||
hgline, | ||||
Gregory Szorc
|
r36054 | ui.environ[b'TESTNAME'].decode('utf-8', 'replace'), | ||
Gregory Szorc
|
r35191 | ] | ||
fh.write(b'\0'.join(p.encode('utf-8', 'replace') for p in parts)) | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r35191 | def extsetup(ui): | ||
Augie Fackler
|
r43346 | extensions.wrapfunction(dispatch, 'handlecommandexception', handleexception) | ||