Skip to content
Snippets Groups Projects
Commit 1d5e497c authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

py3: convert arbitrary exception object to byte string more reliably

Our exception types implement __bytes__(), which should be tried first. Do
lossy encoding conversion as a last resort.
parent 8626b445
No related branches found
No related tags found
No related merge requests found
......@@ -1045,7 +1045,7 @@
ui.debug('bundle2-generatorexit\n')
raise
except BaseException as exc:
bexc = pycompat.bytestr(exc)
bexc = util.forcebytestr(exc)
# backup exception data for later
ui.debug('bundle2-input-stream-interrupt: encoding exception %s'
% bexc)
......
......@@ -19,7 +19,6 @@
from . import (
cmdutil,
configitems,
encoding,
error,
pycompat,
util,
......@@ -114,12 +113,7 @@
mod = _importh(name)
return mod
def _forbytes(inst):
"""Portably format an import error into a form suitable for
%-formatting into bytestrings."""
return encoding.strtolocal(str(inst))
def _reportimporterror(ui, err, failed, next):
# note: this ui.debug happens before --debug is processed,
# Use --config ui.debug=1 to see them.
ui.debug('could not import %s (%s): trying %s\n'
......@@ -122,8 +116,8 @@
def _reportimporterror(ui, err, failed, next):
# note: this ui.debug happens before --debug is processed,
# Use --config ui.debug=1 to see them.
ui.debug('could not import %s (%s): trying %s\n'
% (failed, _forbytes(err), next))
% (failed, util.forcebytestr(err), next))
if ui.debugflag:
ui.traceback()
......@@ -180,7 +174,7 @@
uisetup(ui)
except Exception as inst:
ui.traceback()
msg = _forbytes(inst)
msg = util.forcebytestr(inst)
ui.warn(_("*** failed to set up extension %s: %s\n") % (name, msg))
return False
return True
......@@ -197,7 +191,7 @@
extsetup() # old extsetup with no ui argument
except Exception as inst:
ui.traceback()
msg = _forbytes(inst)
msg = util.forcebytestr(inst)
ui.warn(_("*** failed to set up extension %s: %s\n") % (name, msg))
return False
return True
......@@ -215,7 +209,7 @@
try:
load(ui, name, path)
except Exception as inst:
msg = _forbytes(inst)
msg = util.forcebytestr(inst)
if path:
ui.warn(_("*** failed to import extension %s from %s: %s\n")
% (name, path, msg))
......
......@@ -2268,6 +2268,15 @@
def unescapestr(s):
return codecs.escape_decode(s)[0]
def forcebytestr(obj):
"""Portably format an arbitrary object (e.g. exception) into a byte
string."""
try:
return pycompat.bytestr(obj)
except UnicodeEncodeError:
# non-ascii string, may be lossy
return pycompat.bytestr(encoding.strtolocal(str(obj)))
def uirepr(s):
# Avoid double backslash in Windows path repr()
return repr(s).replace('\\\\', '\\')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment