Mercurial > public > mercurial-scm > hg-stable
diff mercurial/util.py @ 33708:1d5e497c08b3
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.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 03 Aug 2017 23:02:32 +0900 |
parents | 609606d21765 |
children | 86aca74a063b |
line wrap: on
line diff
--- a/mercurial/util.py Thu Aug 03 20:08:31 2017 -0700 +++ b/mercurial/util.py Thu Aug 03 23:02:32 2017 +0900 @@ -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('\\\\', '\\')