diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -65,15 +65,11 @@ class basectx(object): return o - def __str__(self): - r = short(self.node()) - if pycompat.ispy3: - return r.decode('ascii') - return r - def __bytes__(self): return short(self.node()) + __str__ = encoding.strmethod(__bytes__) + def __int__(self): return self.rev() @@ -710,17 +706,13 @@ class basefilectx(object): __bool__ = __nonzero__ - def __str__(self): + def __bytes__(self): try: return "%s@%s" % (self.path(), self._changectx) except error.LookupError: return "%s@???" % self.path() - def __bytes__(self): - try: - return "%s@%s" % (self.path(), self._changectx) - except error.LookupError: - return "%s@???" % self.path() + __str__ = encoding.strmethod(__bytes__) def __repr__(self): return "<%s %s>" % (type(self).__name__, str(self)) @@ -1306,12 +1298,11 @@ class committablectx(basectx): if self._extra['branch'] == '': self._extra['branch'] = 'default' - def __str__(self): - return str(self._parents[0]) + r"+" - def __bytes__(self): return bytes(self._parents[0]) + "+" + __str__ = encoding.strmethod(__bytes__) + def __nonzero__(self): return True diff --git a/mercurial/encoding.py b/mercurial/encoding.py --- a/mercurial/encoding.py +++ b/mercurial/encoding.py @@ -177,15 +177,24 @@ def unifromlocal(s): """Convert a byte string of local encoding to a unicode string""" return fromlocal(s).decode('utf-8') +def unimethod(bytesfunc): + """Create a proxy method that forwards __unicode__() and __str__() of + Python 3 to __bytes__()""" + def unifunc(obj): + return unifromlocal(bytesfunc(obj)) + return unifunc + # converter functions between native str and byte string. use these if the # character encoding is not aware (e.g. exception message) or is known to # be locale dependent (e.g. date formatting.) if pycompat.ispy3: strtolocal = unitolocal strfromlocal = unifromlocal + strmethod = unimethod else: strtolocal = pycompat.identity strfromlocal = pycompat.identity + strmethod = pycompat.identity if not _nativeenviron: # now encoding and helper functions are available, recreate the environ diff --git a/mercurial/util.py b/mercurial/util.py --- a/mercurial/util.py +++ b/mercurial/util.py @@ -2740,7 +2740,7 @@ class url(object): attrs.append('%s: %r' % (a, v)) return '' % ', '.join(attrs) - def __str__(self): + def __bytes__(self): r"""Join the URL's components back into a URL string. Examples: @@ -2774,9 +2774,6 @@ class url(object): >>> print url(r'file:///D:\data\hg') file:///D:\data\hg """ - return encoding.strfromlocal(self.__bytes__()) - - def __bytes__(self): if self._localpath: s = self.path if self.scheme == 'bundle': @@ -2820,6 +2817,8 @@ class url(object): s += '#' + urlreq.quote(self.fragment, safe=self._safepchars) return s + __str__ = encoding.strmethod(__bytes__) + def authinfo(self): user, passwd = self.user, self.passwd try: