[ZCM] [ZC] 2079/ 2 Resolve "UnicodeDecodeError when editing PageTemplate after customizing it"

"Collector: Zope Bugs, Features, and Patches ..." <[email protected]>
Newsgroups gmane.comp.web.zope.devel.collector-monitor
Message-ID <[email protected]>
Issue #2079 Update (Resolve) "UnicodeDecodeError when editing PageTemplate after customizing it"
 Status Resolved, Zope/bug medium
To followup, visit:
  http://www.zope.org/Collectors/Zope/2079

==============================================================
= Resolve - Entry #2 by ajung on Jan 13, 2007 10:37 am

 Status: Pending => Resolved

Unicode errors should be gone with Zope 2.10.2
________________________________________
= Request - Entry #1 by jmikulasek on May 4, 2006 4:12 pm

I have a page template in the skins folder in my Plone product (however I believe
this is not a Plone or CMF problem) with UTF-8 encoded content with accented characters 
(not clean us-ascii). The management_page_charset property is set to UTF-8 at the root object.
When I customize the page template in the portal_skins tool 
and then try to edit it through ZMI I get a UnicodeDecodeError with a following traceback:

2006-05-04 21:35:15 ERROR Zope.SiteErrorLog http://localhost:8080/vh/vhjm/zubstejn.krestanstvi.net/portal_skins/custom/main_template/pt_editAction
Traceback (most recent call last):
  File "/home/jirka/System/zope-2.8.6/lib/python/ZPublisher/Publish.py", line 114, in publish
    request, bind=1)
  File "/home/jirka/System/zope-2.8.6/lib/python/ZPublisher/mapply.py", line 88, in mapply
    if debug is not None: return debug(object,args,context)
  File "/home/jirka/System/zope-2.8.6/lib/python/ZPublisher/Publish.py", line 40, in call_object
    result=apply(object,args) # Type s<cr> to step into published object.
  File "/home/jirka/System/zope-2.8.6/lib/python/Products/PageTemplates/ZopePageTemplate.py", line 135, in pt_editAction
    self.pt_edit(text, content_type)
  File "/home/jirka/System/zope-2.8.6/lib/python/Products/PageTemplates/PageTemplate.py", line 66, in pt_edit
    self.write(text)
  File "/home/jirka/System/zope-2.8.6/lib/python/Products/PageTemplates/ZopePageTemplate.py", line 224, in write
    ZopePageTemplate.inheritedAttribute('write')(self, text)
  File "/home/jirka/System/zope-2.8.6/lib/python/Products/PageTemplates/PageTemplate.py", line 150, in write
    if self._text != text:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2053: ordinal not in range(128)

The self._text was of a str type while text was unicode so the self._text was 
probably decoded and a default encoding (instead of management_page_charset) was used which failed. When I added the following lines before the comparison

        charset = getattr(self, 'management_page_charset', None)
        if type(text) == types.UnicodeType and charset:
            text = text.encode(charset)

everything went smooth. However I don't know whether my solution is a good one
in a sense "not an ugly hack".
==============================================================
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.