mercurial/util.py
changeset 33682 1d5e497c08b3
parent 33619 609606d21765
child 33736 86aca74a063b
--- 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('\\\\', '\\')