extensions: tapdance to get reasonable import error formatting
authorAugie Fackler <raf@durin42.com>
Fri, 03 Mar 2017 14:08:02 -0500
changeset 31312 90f439823b99
parent 31311 681046de87f1
child 31313 dab92f3dbd98
extensions: tapdance to get reasonable import error formatting I'm not thrilled with this, but it seems to work.
mercurial/extensions.py
--- a/mercurial/extensions.py	Sat Mar 11 10:35:44 2017 -0800
+++ b/mercurial/extensions.py	Fri Mar 03 14:08:02 2017 -0500
@@ -18,6 +18,7 @@
 
 from . import (
     cmdutil,
+    encoding,
     error,
     pycompat,
     util,
@@ -104,11 +105,18 @@
                 mod = _importh(name)
     return mod
 
+def _forbytes(inst):
+    """Portably format an import error into a form suitable for
+    %-formatting into bytestrings."""
+    if pycompat.ispy3:
+        return encoding.tolocal(str(inst).encode('utf-8'))
+    return 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'
-             % (failed, err, next))
+             % (failed, _forbytes(err), next))
     if ui.debugflag:
         ui.traceback()
 
@@ -168,6 +176,7 @@
         except KeyboardInterrupt:
             raise
         except Exception as inst:
+            inst = _forbytes(inst)
             if path:
                 ui.warn(_("*** failed to import extension %s from %s: %s\n")
                         % (name, path, inst))