]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Separate class docstring and default str() messages in DNSException.
authorPetr Spacek <pspacek@redhat.com>
Wed, 25 Mar 2015 08:22:35 +0000 (09:22 +0100)
committerPetr Viktorin <pviktori@redhat.com>
Thu, 21 May 2015 12:25:12 +0000 (14:25 +0200)
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.

dns/exception.py

index 0790aa8bfba17d919defd3599154a890fd11a370..4f7df346a0b4085916453818d71c9b4b72689ef1 100644 (file)
@@ -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.