/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/errors.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-10 01:43:31 UTC
  • mfrom: (6676 work)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610014331-1xalwmij33imwidq
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
from __future__ import absolute_import
21
21
 
 
22
from .sixish import (
 
23
    PY3,
 
24
    )
 
25
 
22
26
# TODO: is there any value in providing the .args field used by standard
23
27
# python exceptions?   A list of values with no names seems less useful
24
28
# to me.
87
91
        if s is not None:
88
92
            # contains a preformatted message
89
93
            return s
 
94
        err = None
90
95
        try:
91
96
            fmt = self._get_format_string()
92
97
            if fmt:
96
101
                # never a 'unicode' object.
97
102
                return s
98
103
        except Exception as e:
99
 
            pass # just bind to 'e' for formatting below
100
 
        else:
101
 
            e = None
 
104
            err = e
102
105
        return 'Unprintable exception %s: dict=%r, fmt=%r, error=%r' \
103
106
            % (self.__class__.__name__,
104
107
               self.__dict__,
105
108
               getattr(self, '_fmt', None),
106
 
               e)
107
 
 
108
 
    def __unicode__(self):
109
 
        u = self._format()
110
 
        if isinstance(u, str):
111
 
            # Try decoding the str using the default encoding.
112
 
            u = unicode(u)
113
 
        elif not isinstance(u, unicode):
114
 
            # Try to make a unicode object from it, because __unicode__ must
115
 
            # return a unicode object.
116
 
            u = unicode(u)
117
 
        return u
118
 
 
119
 
    def __str__(self):
120
 
        s = self._format()
121
 
        if isinstance(s, unicode):
122
 
            s = s.encode('utf8')
123
 
        else:
124
 
            # __str__ must return a str.
125
 
            s = str(s)
126
 
        return s
 
109
               err)
 
110
 
 
111
    if PY3:
 
112
        __str__ = _format
 
113
    else:
 
114
        def __str__(self):
 
115
            return self._format().encode('utf-8')
 
116
 
 
117
        __unicode__ = _format
127
118
 
128
119
    def __repr__(self):
129
120
        return '%s(%s)' % (self.__class__.__name__, str(self))
133
124
        fmt = getattr(self, '_fmt', None)
134
125
        if fmt is not None:
135
126
            from breezy.i18n import gettext
136
 
            return gettext(unicode(fmt)) # _fmt strings should be ascii
 
127
            return gettext(fmt) # _fmt strings should be ascii
137
128
 
138
129
    def __eq__(self, other):
139
130
        if self.__class__ is not other.__class__:
140
131
            return NotImplemented
141
132
        return self.__dict__ == other.__dict__
142
133
 
 
134
    def __hash__(self):
 
135
        return id(self)
 
136
 
143
137
 
144
138
class InternalBzrError(BzrError):
145
139
    """Base class for errors that are internal in nature.
665
659
    def __repr__(self):
666
660
        return '<%s %r>' % (self.__class__.__name__, self.__dict__)
667
661
 
668
 
    def _format(self):
669
 
        # XXX: Ideally self.detail would be a property, but Exceptions in
670
 
        # Python 2.4 have to be old-style classes so properties don't work.
671
 
        # Instead we override _format.
 
662
    def _get_format_string(self):
 
663
        # GZ 2017-06-08: Not the best place to lazy fill detail in.
672
664
        if self.detail is None:
673
 
            if self.bzrdir is not None:
674
 
                try:
675
 
                    self.bzrdir.open_repository()
676
 
                except NoRepositoryPresent:
677
 
                    self.detail = ''
678
 
                except Exception:
679
 
                    # Just ignore unexpected errors.  Raising arbitrary errors
680
 
                    # during str(err) can provoke strange bugs.  Concretely
681
 
                    # Launchpad's codehosting managed to raise NotBranchError
682
 
                    # here, and then get stuck in an infinite loop/recursion
683
 
                    # trying to str() that error.  All this error really cares
684
 
                    # about that there's no working repository there, and if
685
 
                    # open_repository() fails, there probably isn't.
686
 
                    self.detail = ''
687
 
                else:
688
 
                    self.detail = ': location is a repository'
 
665
           self.detail = self._get_detail()
 
666
        return super(NotBranchError, self)._get_format_string()
 
667
 
 
668
    def _get_detail(self):
 
669
        if self.bzrdir is not None:
 
670
            try:
 
671
                self.bzrdir.open_repository()
 
672
            except NoRepositoryPresent:
 
673
                return ''
 
674
            except Exception as e:
 
675
                # Just ignore unexpected errors.  Raising arbitrary errors
 
676
                # during str(err) can provoke strange bugs.  Concretely
 
677
                # Launchpad's codehosting managed to raise NotBranchError
 
678
                # here, and then get stuck in an infinite loop/recursion
 
679
                # trying to str() that error.  All this error really cares
 
680
                # about that there's no working repository there, and if
 
681
                # open_repository() fails, there probably isn't.
 
682
                return ': ' + e.__class__.__name__
689
683
            else:
690
 
                self.detail = ''
691
 
        return PathError._format(self)
 
684
                return ': location is a repository'
 
685
        return ''
692
686
 
693
687
 
694
688
class NoSubmitBranch(PathError):