[Checkins] SVN: Sandbox/shane/republish/zope.publisher/src/zope/publisher/browser.py - Don't fail silently if input text can't be converted to Unicode

Shane Hathaway shane at hathawaymix.org
Sun Feb 8 02:34:35 EST 2009


Log message for revision 96230:
  - Don't fail silently if input text can't be converted to Unicode
  
  - charsets is now a read-only request property.  Its value is
    cached.
  

Changed:
  U   Sandbox/shane/republish/zope.publisher/src/zope/publisher/browser.py

-=-
Modified: Sandbox/shane/republish/zope.publisher/src/zope/publisher/browser.py
===================================================================
--- Sandbox/shane/republish/zope.publisher/src/zope/publisher/browser.py	2009-02-08 07:33:16 UTC (rev 96229)
+++ Sandbox/shane/republish/zope.publisher/src/zope/publisher/browser.py	2009-02-08 07:34:34 UTC (rev 96230)
@@ -89,7 +89,7 @@
     __slots__ = (
         '__provides__', # Allow request to directly provide interfaces
         'form', # Form data
-        'charsets', # helper attribute
+        '_charsets', # helper attribute
         '__annotations__',
         )
 
@@ -99,25 +99,32 @@
 
     def __init__(self, body_instream, environ, response=None):
         self.form = {}
-        self.charsets = None
+        self._charsets = None
         super(BrowserRequest, self).__init__(body_instream, environ, response)
 
+    def _get_charsets(self):
+        charsets = self._charsets
+        if charsets is None:
+            envadapter = IUserPreferredCharsets(self)
+            charsets = envadapter.getPreferredCharsets() or ['utf-8']
+            self._charsets = charsets
+        return charsets
 
+    charsets = property(_get_charsets)
+
     def _createResponse(self):
         return BrowserResponse()
 
     def _decode(self, text):
         """Try to decode the text using one of the available charsets."""
-        if self.charsets is None:
-            envadapter = IUserPreferredCharsets(self)
-            self.charsets = envadapter.getPreferredCharsets() or ['utf-8']
         for charset in self.charsets:
             try:
-                text = unicode(text, charset)
-                break
+                return unicode(text, charset)
             except UnicodeError:
                 pass
-        return text
+        raise UnicodeError(
+            "Unable to decode %s using any available character set"
+            % repr(text))
 
     def processInputs(self):
         'See IPublisherRequest'



More information about the Checkins mailing list