This is an archive of the discontinued Mercurial Phabricator instance.

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

Authored by durin42 on Dec 14 2018, 12:14 PM.

Details

Summary

We open-code encoding.unimethod here to avoid cycles, and do a local
import of encoding when someone str()s a RevlogError. It's not my
favorite solution, but it gets the job done.

Diff Detail

Repository
rHG Mercurial
Lint
Lint Skipped
Unit
Unit Tests Skipped

Event Timeline

durin42 created this revision.Dec 14 2018, 12:14 PM
pulkit accepted this revision.Dec 14 2018, 12:21 PM
This revision was automatically updated to reflect the committed changes.
yuja added a subscriber: yuja.Dec 14 2018, 8:10 PM

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.