Commit 75db0249 authored by Andreas Jung's avatar Andreas Jung

write() now auto-converts to unicode instead of throwing an exception

parent ff875758
...@@ -108,7 +108,6 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -108,7 +108,6 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
self.expand = 0 self.expand = 0
self.ZBindings_edit(self._default_bindings) self.ZBindings_edit(self._default_bindings)
self.output_encoding = output_encoding self.output_encoding = output_encoding
# default content # default content
if not text: if not text:
text = open(self._default_content_fn).read() text = open(self._default_content_fn).read()
...@@ -286,8 +285,11 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable, ...@@ -286,8 +285,11 @@ class ZopePageTemplate(Script, PageTemplate, Historical, Cacheable,
return c return c
def write(self, text): def write(self, text):
if not isinstance(text, unicode): if not isinstance(text, unicode):
raise TypeError("'text' parameter must be unicode") text, encoding = convertToUnicode(text, self.content_type, preferred_encodings)
self.output_encoding = encoding
self.ZCacheable_invalidate() self.ZCacheable_invalidate()
ZopePageTemplate.inheritedAttribute('write')(self, text) ZopePageTemplate.inheritedAttribute('write')(self, text)
......
...@@ -112,11 +112,6 @@ class ZopePageTemplateFileTests(ZopeTestCase): ...@@ -112,11 +112,6 @@ class ZopePageTemplateFileTests(ZopeTestCase):
self.assertEqual(zpt.read(), s) self.assertEqual(zpt.read(), s)
self.assertEqual(isinstance(zpt.read(), unicode), True) self.assertEqual(isinstance(zpt.read(), unicode), True)
def testWriteWontAcceptsNonUnicode(self):
manage_addPageTemplate(self.app, 'test', '', encoding='utf-8')
zpt = self.app['test']
self.assertRaises(TypeError, zpt.write, 'this is not unicode')
def _createZPT(self): def _createZPT(self):
manage_addPageTemplate(self.app, 'test', text=utf8_str, encoding='utf-8') manage_addPageTemplate(self.app, 'test', text=utf8_str, encoding='utf-8')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment