[CalendarServer-changes] [11674] CalendarServer/trunk/twext/python

source_changes at macosforge.org source_changes at macosforge.org
Thu Sep 12 11:01:16 PDT 2013


Revision: 11674
          http://trac.calendarserver.org//changeset/11674
Author:   wsanchez at apple.com
Date:     2013-09-12 11:01:15 -0700 (Thu, 12 Sep 2013)
Log Message:
-----------
Add test for unformattable value in log event.

Modified Paths:
--------------
    CalendarServer/trunk/twext/python/log.py
    CalendarServer/trunk/twext/python/test/test_log.py

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-09-12 02:20:06 UTC (rev 11673)
+++ CalendarServer/trunk/twext/python/log.py	2013-09-12 18:01:15 UTC (rev 11674)
@@ -213,7 +213,8 @@
         try:
             return formatUnformattableEvent(event, e)
         except:
-            return u"MESSAGE LOST"
+            # This should never happen...
+            return u"MESSAGE LOST COMPLETELY"
 
 
 
@@ -233,7 +234,7 @@
             u"Unable to format event {event!r}: {error}"
             .format(event=event, error=error)
         )
-    except BaseException as error:
+    except BaseException:
         #
         # Yikes, something really nasty happened.
         #
@@ -241,27 +242,32 @@
         # hopefully at least the namespace is sane, which will
         # help you find the offending logger.
         #
+        failure = Failure()
         try:
             items = []
 
             for key, value in event.items():
                 try:
-                    items.append(u"{key!r} = ".format(key=key))
+                    keyFormatted = u"{key!r}".format(key=key)
                 except:
-                    items.append(u"<UNFORMATTABLE KEY> = ")
+                    keyFormatted = u"<UNFORMATTABLE KEY>"
+
                 try:
-                    items.append(u"{value!r}".format(value=value))
+                    valueFormatted = u"{value!r}".format(value=value)
                 except:
-                    items.append(u"<UNFORMATTABLE VALUE>")
+                    valueFormatted = u"<UNFORMATTABLE VALUE>"
 
+                items.append(" = ".join((keyFormatted, valueFormatted)))
+
             text = ", ".join(items)
-        except:
-            text = ""
+        except BaseException as e:
+            text = "UNABLE TO RECOVER ANY DATA FROM MESSAGE: {e}".format(e=e)
 
         return (
-            u"MESSAGE LOST: Unformattable object logged: {error}\n"
-            u"Recoverable data: {text}"
-            .format(text=text)
+            u"MESSAGE LOST: unformattable object logged.\n"
+            u"Recoverable data: {text}\n"
+            u"Exception during formatting:\n{failure}"
+            .format(error=error, failure=failure, text=text)
         )
 
 

Modified: CalendarServer/trunk/twext/python/test/test_log.py
===================================================================
--- CalendarServer/trunk/twext/python/test/test_log.py	2013-09-12 02:20:06 UTC (rev 11673)
+++ CalendarServer/trunk/twext/python/test/test_log.py	2013-09-12 18:01:15 UTC (rev 11674)
@@ -164,7 +164,7 @@
         """
         Clearing log levels.
         """
-        setLogLevelForNamespace("twext.web2", LogLevel.debug)
+        setLogLevelForNamespace("twext.web2"    , LogLevel.debug)
         setLogLevelForNamespace("twext.web2.dav", LogLevel.error)
 
         clearLogLevels()
@@ -191,14 +191,17 @@
         mean that the format key ought to be I{called} rather than stringified.
         """
         self.assertEquals(
-            formatWithCall(u"Hello, {world}. {callme()}.",
-                           dict(world="earth",
-                                callme=lambda: "maybe")),
+            formatWithCall(
+                u"Hello, {world}. {callme()}.",
+                dict(world="earth", callme=lambda: "maybe")
+            ),
             "Hello, earth. maybe."
         )
         self.assertEquals(
-            formatWithCall(u"Hello, {repr()!r}.",
-                           dict(repr=lambda: 'repr')),
+            formatWithCall(
+                u"Hello, {repr()!r}.",
+                dict(repr=lambda: 'repr')
+            ),
             "Hello, 'repr'."
         )
 
@@ -273,21 +276,27 @@
         self.assertIn(repr(event), result)
 
 
-#     def test_formatEventYouSoNastyOMGMakeItStop(self):
-#         """
-#         Formatting an event that's just plain out to get us and is
-#         really determined.
-#         """
-#         badRepr = 
+    def test_formatEventYouSoNastyUnformattableValue(self):
+         """
+         Formatting an event that's just plain out to get us and is
+         really determined.
+         """
+         class Gurk(object):
+             # Class that raises in C{__repr__()}.
+             def __repr__(self):
+                 return str(1/0)
 
-#         event = dict(
-#             log_format="{evil()}",
-#             evil=lambda: 1/0,
-#         )
-#         result = formatEvent(event)
+         event = dict(
+             log_format="{evil()}",
+             evil=lambda: 1/0,
+             gurk=Gurk(),
+         )
+         result = formatEvent(event)
 
-#         self.assertIn("Unable to format event", result)
-#         self.assertIn(repr(event), result)
+         self.assertIn("MESSAGE LOST: unformattable object logged.", result)
+         self.assertIn("Recoverable data:", result)
+         self.assertIn("Exception during formatting:", result)
+         #self.assertIn(repr(event), result)
 
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130912/4cbd14c2/attachment.html>


More information about the calendarserver-changes mailing list