[CalendarServer-changes] [11727] CalendarServer/branches/users/glyph/log-cleanups/twext/python

source_changes at macosforge.org source_changes at macosforge.org
Tue Sep 17 16:02:34 PDT 2013


Revision: 11727
          http://trac.calendarserver.org//changeset/11727
Author:   glyph at apple.com
Date:     2013-09-17 16:02:34 -0700 (Tue, 17 Sep 2013)
Log Message:
-----------
Remove an unnecessary try/except by using safe_repr.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/log-cleanups/twext/python/log.py
    CalendarServer/branches/users/glyph/log-cleanups/twext/python/test/test_log.py

Modified: CalendarServer/branches/users/glyph/log-cleanups/twext/python/log.py
===================================================================
--- CalendarServer/branches/users/glyph/log-cleanups/twext/python/log.py	2013-09-17 23:02:33 UTC (rev 11726)
+++ CalendarServer/branches/users/glyph/log-cleanups/twext/python/log.py	2013-09-17 23:02:34 UTC (rev 11727)
@@ -77,7 +77,7 @@
 from zope.interface import Interface, implementer
 from twisted.python.constants import NamedConstant, Names
 from twisted.python.failure import Failure
-from twisted.python.reflect import safe_str
+from twisted.python.reflect import safe_str, safe_repr
 import twisted.python.log
 from twisted.python.log import msg as twistedLogMessage
 from twisted.python.log import addObserver, removeObserver
@@ -233,42 +233,36 @@
             .format(event=event, error=error)
         )
     except BaseException:
-        #
         # Yikes, something really nasty happened.
         #
-        # Try to recover as much formattable data as possible;
-        # hopefully at least the namespace is sane, which will
-        # help you find the offending logger.
-        #
-        try:
-            failure = Failure()
+        # Try to recover as much formattable data as possible; hopefully at
+        # least the namespace is sane, which will help you find the offending
+        # logger.
+        failure = Failure()
 
-            items = []
+        items = []
 
-            for key, value in event.items():
-                try:
-                    keyFormatted = u"{key!r}".format(key=key)
-                except BaseException:
-                    keyFormatted = u"<UNFORMATTABLE KEY>"
+        for key, value in event.items():
+            try:
+                keyFormatted = u"{key!r}".format(key=key)
+            except BaseException:
+                keyFormatted = u"<UNFORMATTABLE KEY>"
 
-                try:
-                    valueFormatted = u"{value!r}".format(value=value)
-                except BaseException:
-                    valueFormatted = u"<UNFORMATTABLE VALUE>"
+            try:
+                valueFormatted = u"{value!r}".format(value=value)
+            except BaseException:
+                valueFormatted = u"<UNFORMATTABLE VALUE>"
 
-                items.append(" = ".join((keyFormatted, valueFormatted)))
+            items.append(" = ".join((keyFormatted, valueFormatted)))
 
-            text = ", ".join(items)
+        text = ", ".join(items)
 
-            return (
-                u"MESSAGE LOST: unformattable object logged: {error}\n"
-                u"Recoverable data: {text}\n"
-                u"Exception during formatting:\n{failure}"
-                .format(error=error, failure=failure, text=text)
-            )
-        except BaseException as e:
-            # This should never happen...
-            return u"MESSAGE LOST: unable to recover any data from message: {e}".format(e=e)
+        return (
+            u"MESSAGE LOST: unformattable object logged: {error}\n"
+            u"Recoverable data: {text}\n"
+            u"Exception during formatting:\n{failure}"
+            .format(error=safe_repr(error), failure=failure, text=text)
+        )
 
 
 

Modified: CalendarServer/branches/users/glyph/log-cleanups/twext/python/test/test_log.py
===================================================================
--- CalendarServer/branches/users/glyph/log-cleanups/twext/python/test/test_log.py	2013-09-17 23:02:33 UTC (rev 11726)
+++ CalendarServer/branches/users/glyph/log-cleanups/twext/python/test/test_log.py	2013-09-17 23:02:34 UTC (rev 11727)
@@ -295,7 +295,7 @@
         event = {
             "log_format": "{evil()}",
             "evil": lambda: 1/0,
-            Gurk(): "gurk",
+            Unformattable(): "gurk",
         }
         result = formatEvent(event)
         self.assertIn("MESSAGE LOST: unformattable object logged:", result)
@@ -310,7 +310,7 @@
         event = dict(
             log_format="{evil()}",
             evil=lambda: 1/0,
-            gurk=Gurk(),
+            gurk=Unformattable(),
         )
         result = formatEvent(event)
         self.assertIn("MESSAGE LOST: unformattable object logged:", result)
@@ -325,11 +325,12 @@
         event = dict(
             log_format="{evil()}",
             evil=lambda: 1/0,
+            recoverable="okay",
         )
         # Call formatUnformattableEvent() directly with a bogus exception.
-        result = formatUnformattableEvent(event, Gurk())
-        self.assertIn("MESSAGE LOST: unable to recover any data from message:",
-                      result)
+        result = formatUnformattableEvent(event, Unformattable())
+        self.assertIn("MESSAGE LOST: unformattable object logged:", result)
+        self.assertIn(repr("recoverable") + " = " + repr("okay"), result)
 
 
 
@@ -1013,7 +1014,10 @@
 
 
 
-class Gurk(object):
-    # Class that raises in C{__repr__()}.
+class Unformattable(object):
+    """
+    An object that raises an exception from C{__repr__}.
+    """
+
     def __repr__(self):
         return str(1/0)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130917/d452f808/attachment-0001.html>


More information about the calendarserver-changes mailing list