[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/presentation/ Fixed ZPT Templates to deal with Unicode correctly. Thanks to Stuart Bishop

Marius Gedminas marius at pov.lt
Sat Jun 12 12:36:17 EDT 2004


Log message for revision 25406:
Fixed ZPT Templates to deal with Unicode correctly.  Thanks to Stuart Bishop
for his advice.




-=-
Modified: Zope3/trunk/src/zope/app/presentation/tests/test_zpt.py
===================================================================
--- Zope3/trunk/src/zope/app/presentation/tests/test_zpt.py	2004-06-12 15:59:28 UTC (rev 25405)
+++ Zope3/trunk/src/zope/app/presentation/tests/test_zpt.py	2004-06-12 16:36:17 UTC (rev 25406)
@@ -40,9 +40,15 @@
     # XXX We need tests for the template class itself and for the
     # SearchableText adapter.
 
+    def test_unicode_required(self):
+        template = ZPTTemplate()
+        self.assertRaises(TypeError, template.setSource, 'not unicode')
+        template.source = source = u'123\u1234'
+        self.assertEquals(template.source, source)
+
     def test_ReadFile(self):
         template = ZPTTemplate()
-        source = '<p>Test content</p>'
+        source = u'<p>Test content</p>'
         template.source = source
         adapter = ReadFile(template)
         self.assertEqual(adapter.read(), source)
@@ -50,15 +56,15 @@
 
     def test_WriteFile(self):
         template = ZPTTemplate()
-        source = '<p>Test content</p>'
-        template.source = '<p>Old content</p>'
+        source = u'<p>Test content</p>'
+        template.source = u'<p>Old content</p>'
         adapter = WriteFile(template)
         adapter.write(source)
         self.assertEqual(template.source, source)
 
     def test_ZPTFactory(self):
         factory = ZPTFactory(None)
-        source = '<p>Test content</p>'
+        source = u'<p>Test content</p>'
         template = factory('foo', 'text/html', source)
         self.assertEqual(template.source, source)
 
@@ -87,7 +93,7 @@
 
     def test_debug_flags(self):
         template = self.pageInContext(ZPTTemplate())
-        template.source = '<tal:p>Test</tal:p>'
+        template.source = u'<tal:p>Test</tal:p>'
 
         self.request = TestRequest()
         self.context = None

Modified: Zope3/trunk/src/zope/app/presentation/zpt.py
===================================================================
--- Zope3/trunk/src/zope/app/presentation/zpt.py	2004-06-12 15:59:28 UTC (rev 25405)
+++ Zope3/trunk/src/zope/app/presentation/zpt.py	2004-06-12 16:36:17 UTC (rev 25406)
@@ -73,13 +73,20 @@
     expand = False
     usage = u''
 
-    source = property(
-        # get
-        lambda self: self.read(),
-        # set
-        lambda self, text: self.pt_edit(text.encode('utf-8'), self.contentType)
-        )
+    def getSource(self):
+        """See zope.app.presentation.zpt.IZPTInfo"""
+        return self.read()
 
+    def setSource(self, text):
+        """See zope.app.presentation.zpt.IZPTInfo"""
+        if not isinstance(text, unicode):
+            raise TypeError("source text must be Unicode" , text)
+        self.pt_edit(text, self.contentType)
+
+    # See zope.app.presentation.zpt.IZPTInfo
+    source = property(getSource, setSource, None,
+                      """Source of the Page Template.""")
+
     def pt_getContext(self, view, **_kw):
         # instance is a View component
         namespace = super(ZPTTemplate, self).pt_getContext(**_kw)




More information about the Zope3-Checkins mailing list