[PATCH 1 of 6 py3 v4] extensions: tapdance to get reasonable import error formatting

Augie Fackler raf at durin42.com
Sat Mar 11 14:39:13 EST 2017


# HG changeset patch
# User Augie Fackler <raf at durin42.com>
# Date 1488568082 18000
#      Fri Mar 03 14:08:02 2017 -0500
# Node ID f498eb177f8d15d967f53ccc2a37fad5a2b785a4
# Parent  295625f1296bda9c507b9025177c4f2452408cc3
extensions: tapdance to get reasonable import error formatting

I'm not thrilled with this, but it seems to work.

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -18,6 +18,7 @@ from .i18n import (
 
 from . import (
     cmdutil,
+    encoding,
     error,
     pycompat,
     util,
@@ -104,11 +105,18 @@ def _importext(name, path=None, reportfu
                 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 @@ def loadall(ui):
         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))


More information about the Mercurial-devel mailing list