From: Petr Spacek Date: Wed, 25 Mar 2015 08:22:35 +0000 (+0100) Subject: Separate class docstring and default str() messages in DNSException. X-Git-Tag: v1.13.0~27^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6532c257016f583c0dda14b4e354fb40811edb4;p=thirdparty%2Fdnspython.git 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. --- diff --git a/dns/exception.py b/dns/exception.py index 0790aa8b..4f7df346 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.