Patchwork D1909: cmdutil: add a kludge to make bytes repr() the same on 2 and 3

login
register
mail settings
Submitter phabricator
Date Feb. 1, 2018, 11:34 p.m.
Message ID <1a85052aa57f6ee0b527d65aff69545b@localhost.localdomain>
Download mbox | patch
Permalink /patch/27201/
State Not Applicable
Headers show

Comments

phabricator - Feb. 1, 2018, 11:34 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG00466a8b4d13: cmdutil: add a kludge to make bytes repr() the same on 2 and 3 (authored by durin42, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D1909?vs=4954&id=5130

REVISION DETAIL
  https://phab.mercurial-scm.org/D1909

AFFECTED FILES
  mercurial/cmdutil.py

CHANGE DETAILS




To: durin42, #hg-reviewers, yuja
Cc: yuja, mercurial-devel

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -2077,6 +2077,19 @@ 
 
     return changeset_templater(ui, repo, spec, match, opts, buffered)
 
+class _regrettablereprbytes(bytes):
+    """Bytes subclass that makes the repr the same on Python 3 as Python 2.
+
+    This is a huge hack.
+    """
+    def __repr__(self):
+        return repr(pycompat.sysstr(self))
+
+def _maybebytestr(v):
+    if pycompat.ispy3 and isinstance(v, bytes):
+        return _regrettablereprbytes(v)
+    return v
+
 def showmarker(fm, marker, index=None):
     """utility function to display obsolescence marker in a readable way
 
@@ -2095,7 +2108,8 @@ 
     fm.write('date', '(%s) ', fm.formatdate(marker.date()))
     meta = marker.metadata().copy()
     meta.pop('date', None)
-    fm.write('metadata', '{%s}', fm.formatdict(meta, fmt='%r: %r', sep=', '))
+    smeta = {_maybebytestr(k): _maybebytestr(v) for k, v in meta.iteritems()}
+    fm.write('metadata', '{%s}', fm.formatdict(smeta, fmt='%r: %r', sep=', '))
     fm.plain('\n')
 
 def finddate(ui, repo, date):