[Checkins] SVN: Zope/branches/ajung-death-to-unicode-errors/lib/python/Products/PageTemplates/Expressions.py some lame solution but that actually works pretty well

Andreas Jung andreas at andreas-jung.com
Sat Jan 6 11:49:22 EST 2007


Log message for revision 71744:
  some lame solution but that actually works pretty well
  

Changed:
  U   Zope/branches/ajung-death-to-unicode-errors/lib/python/Products/PageTemplates/Expressions.py

-=-
Modified: Zope/branches/ajung-death-to-unicode-errors/lib/python/Products/PageTemplates/Expressions.py
===================================================================
--- Zope/branches/ajung-death-to-unicode-errors/lib/python/Products/PageTemplates/Expressions.py	2007-01-06 16:39:19 UTC (rev 71743)
+++ Zope/branches/ajung-death-to-unicode-errors/lib/python/Products/PageTemplates/Expressions.py	2007-01-06 16:49:20 UTC (rev 71744)
@@ -17,6 +17,9 @@
 
 $Id$
 """
+
+import sys
+
 from zope.interface import implements
 from zope.tales.tales import Context, Iterator
 from zope.tales.expressions import PathExpr, StringExpr, NotExpr
@@ -173,6 +176,50 @@
             domain, msgid, mapping=mapping,
             context=context, default=default)
 
+
+    def evaluateText(self, expr):
+        """ customized version in order to get rid of unicode
+            errors for all and ever
+        """
+
+        text = self.evaluate(expr)
+#        print expr, repr(text), text.__class__
+
+        if text is self.getDefault() or text is None:
+            return text
+
+        if isinstance(text, unicode):
+            # we love unicode, nothing to do
+            return text
+
+        elif isinstance(text, str):
+            # waahhh...a standard string
+            # trying to be somewhat smart...this must be
+            # replaced with some kind of configurable
+            # UnicodeEncodingConflictResolver (what a name)
+
+            try:
+                return unicode(text)
+
+            except UnicodeDecodeError:
+                # check for management_page_charset property, default to Python's
+                # default encoding
+
+                encoding = getattr(self.contexts['context'], 'management_page_charset', sys.getdefaultencoding())
+
+                try:
+                    return unicode(text, encoding)
+                except UnicodeDecodeError:
+                    # errors='replace' sucks...it's fine for now
+                    return unicode(text, encoding, 'replace')
+
+        else:
+
+            # This is a weird culprit ...calling unicode() on non-string
+            # objects
+            return unicode(text)
+
+
 class ZopeEngine(zope.app.pagetemplate.engine.ZopeEngine):
 
     _create_context = ZopeContext



More information about the Checkins mailing list