D5426: error: implement __str__ on RevlogError to fix some output defects on Py3

yuja (Yuya Nishihara) phabricator at mercurial-scm.org
Fri Dec 14 20:10:58 EST 2018


yuja added a comment.


  >   class RevlogError(StorageError):
  >       __bytes__ = _tobytes
  > 
  > 
  > +    def __str__(self):
  >  +        # avoid cycle, and directly implement unimethod for this
  >  +        # __str__ to allow delaying the import of encoding until
  >  +        # someone actually wants the __str__ of a RevlogError (which
  >  +        # should be very rare).
  >  +        from . import encoding
  >  +        return encoding.unifromlocal(_tobytes(self))
  
  It breaks `str(err)` on Python 2 because __str__ shouldn't return a unicode
  containing non-ASCII characters.
  
  Suppose we just want to get rid of `b''`s on Python 3, we can add a helper
  function like this:
  
    if pycompat.ispy3:
        def _tostr(exc):
            return pycompat.sysstr(_tobytes(exc))
    else:
        _tostr = Exception.__str__
  
  It's safer than using `encoding.unifromlocal()` in that no other exception
  will be raised on encoding error.

REPOSITORY
  rHG Mercurial

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

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


More information about the Mercurial-devel mailing list