[ZPT] Unicode error in PageTemplates/XML

Dieter Maurer dieter at handshake.de
Mon Dec 15 14:01:34 EST 2003


Santi Camps wrote at 2003-12-15 17:06 +0100:
>I've a problem using Zope Page Templates to generate XML content with
>8bits-strings.  Here there is a minimalist example of what happens:
>
>give_me_content (Python Script):
>return 'maó'
>
>test_html (PageTemplate):
><html>
>  <body tal:content="here/give_me_content" />
></html>
>
>test_xml (PageTemplate):
><?xml version="1.0" encoding="ISO-8859-15"?>
><doc xmlns:tal="http://xml.zope.org/namespaces/tal">
>  <body tal:content="here/give_me_content" />
></doc>
>
>test2_xml (PageTemplate):
><?xml version="1.0" encoding="ISO-8859-15"?>
><doc xmlns:tal="http://xml.zope.org/namespaces/tal">
>  <body>maó</body>
></doc>
>....
>test_html works fine.  No problems here.
>test_xml causes an error "UnicodeError: ASCII decoding error: ordinal
>not in range(128)".  My surprise is that test2_xml works fine, so it
>seems to be a problem of TALES.

I am surprised, too.

The error means, that the page template sees Unicode somewhere
(I do not see where). It then tries to convert everything to
unicode. Unless you set Python's default encoding (with
"sys.setdefaultencoding" in "site_customize.py"), this will
only work for ASCII strings.

I would set the default encoding (frowned about by many Python gods).
When you want to be more in line with best
practices: ensure you work throughout with Unicode
and set the correct "Content-Type" (the ZPublisher will then
encode the Unicode result correctly).

-- 
Dieter



More information about the ZPT mailing list