[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/error/ getPrintable() now returns more pretty representation for a non-ascii data

Dmitry Vasiliev dima at hlabs.spb.ru
Tue Feb 21 08:27:44 EST 2006


Log message for revision 41724:
  getPrintable() now returns more pretty representation for a non-ascii data
  

Changed:
  U   Zope3/trunk/src/zope/app/error/error.py
  U   Zope3/trunk/src/zope/app/error/tests.py

-=-
Modified: Zope3/trunk/src/zope/app/error/error.py
===================================================================
--- Zope3/trunk/src/zope/app/error/error.py	2006-02-21 12:21:26 UTC (rev 41723)
+++ Zope3/trunk/src/zope/app/error/error.py	2006-02-21 13:27:44 UTC (rev 41724)
@@ -21,10 +21,11 @@
 
 import time
 import logging
+import codecs
+
 from persistent import Persistent
 from random import random
-from thread import allocate_lock
-from types import StringTypes
+from threading import Lock
 
 from zope.exceptions.exceptionformatter import format_exception
 from zope.interface import implements
@@ -49,32 +50,32 @@
 # _temp_logs holds the logs.
 _temp_logs = {}  # { oid -> [ traceback string ] }
 
-cleanup_lock = allocate_lock()
+cleanup_lock = Lock()
 
 logger = logging.getLogger('SiteError')
 
+def printedreplace(error):
+    symbols = (ur"\x%02x" % ord(s)
+        for s in error.object[error.start:error.end])
+    return u"".join(symbols), error.end
+
+codecs.register_error("zope.app.error.printedreplace", printedreplace)
+
 def getPrintable(value):
-    # A call to unicode(obj) could raise anything at all.
-    # We'll ignore these errors, and print something
-    # useful instead, but also log the error.
-    try:
-        printable = unicode(value)
-    except:
-        logger.exception(
-            "Error in ErrorReportingUtility while getting a unicode"
-            " representation of an object")
-        printable = u"<unprintable %s object>" % type(value).__name__
-        if type(value) is str:
+    if not isinstance(value, unicode):
+        if not isinstance(value, str):
+            # A call to str(obj) could raise anything at all.
+            # We'll ignore these errors, and print something
+            # useful instead, but also log the error.
             try:
-                r = repr(value)
+                value = str(value)
             except:
-                pass
-            else:
-                try:
-                    printable = unicode(r)
-                except:
-                    pass
-    return printable
+                logger.exception(
+                    "Error in ErrorReportingUtility while getting a str"
+                    " representation of an object")
+                return u"<unprintable %s object>" % type(value).__name__
+        value = unicode(value, errors="zope.app.error.printedreplace")
+    return value
 
 def getFormattedException(info, as_html=False):
     lines = []
@@ -163,7 +164,7 @@
 
             tb_text = None
             tb_html = None
-            if not isinstance(info[2], StringTypes):
+            if not isinstance(info[2], basestring):
                 tb_text = getFormattedException(info)
                 tb_html = getFormattedException(info, True)
             else:

Modified: Zope3/trunk/src/zope/app/error/tests.py
===================================================================
--- Zope3/trunk/src/zope/app/error/tests.py	2006-02-21 12:21:26 UTC (rev 41723)
+++ Zope3/trunk/src/zope/app/error/tests.py	2006-02-21 13:27:44 UTC (rev 41724)
@@ -125,8 +125,7 @@
                           errUtility.getLogEntryById(err_id)['tb_text'])
 
         username = getErrLog[0]['username']
-        self.assertEquals(username, "unauthenticated,"
-            " '\\xe1', '\\xe1', '\\xe1'")
+        self.assertEquals(username, r"unauthenticated, \xe1, \xe1, \xe1")
 
 
 def test_suite():



More information about the Zope3-Checkins mailing list