diff options
| author | Petr Spacek <pspacek@redhat.com> | 2015-03-25 09:22:35 +0100 |
|---|---|---|
| committer | Petr Viktorin <pviktori@redhat.com> | 2015-05-21 14:25:12 +0200 |
| commit | 73544fe7ad500d792325fba01bebdedaf9f11f23 (patch) | |
| tree | 14d2352be0f52a73ffff72caf69228d3cd27fc42 | |
| parent | 0bda8c7d89079945c5873f87b8c45e9469a87d38 (diff) | |
| download | dnspython-73544fe7ad500d792325fba01bebdedaf9f11f23.tar.gz | |
Separate class docstring and default str() messages in DNSException.
str() for non-parametrized exceptions still defaults to class docstring
but now it is possible to simply override the message by defining
class.msg variable.
| -rw-r--r-- | dns/exception.py | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/dns/exception.py b/dns/exception.py index 0790aa8..4f7df34 100644 --- a/dns/exception.py +++ b/dns/exception.py @@ -25,6 +25,8 @@ class DNSException(Exception): empty **kwargs. In compatible mode all *args are passed to standard Python Exception class as before and all *args are printed by standard __str__ implementation. + Class variable msg (or doc string if msg is None) is returned from str() + if *args is empty. b) New/parametrized mode is used if __init__ was called with non-empty **kwargs. @@ -36,18 +38,21 @@ class DNSException(Exception): In the simplest case it is enough to override supp_kwargs and fmt class variables to get nice parametrized messages. """ - supp_kwargs = set() - fmt = None + msg = None # non-parametrized message + supp_kwargs = set() # accepted parameters for _fmt_kwargs (sanity check) + fmt = None # message parametrized with results from _fmt_kwargs def __init__(self, *args, **kwargs): self._check_params(*args, **kwargs) self._check_kwargs(**kwargs) self.kwargs = kwargs + if self.msg is None: + # doc string is better implicit message than empty string + self.msg = self.__doc__ if args: super(DNSException, self).__init__(*args) else: - # doc string is better implicit message than empty string - super(DNSException, self).__init__(self.__doc__) + super(DNSException, self).__init__(self.msg) def _check_params(self, *args, **kwargs): """Old exceptions supported only args and not kwargs. |
