[Zope] SUMMARY: strange unicode behaviour, unicode and ZCTextIndex

Toby Dickenson tdickenson@geminidataloggers.com
Fri, 25 Jul 2003 12:32:52 +0100


On Friday 25 July 2003 11:09, Giuseppe Bonelli wrote:


> **1. sys.setdefaultconfig**
> <quote from="Toby Dickenson">
> <original msg from="Giuseppe Bonelli">
> I have utf-8 as sys.defaultencoding and I do not load any locale when
> starting Zope.
> </original mg>
> That is old advice that predates Zope 2.6. It was never a particularly
> good
> idea, because it affects all of pythons internals. You only need to
> encode
> your unicode as utf-8 (or other encoding) before sending it over the
> network,
> and ZPublisher is capable of doing that itself if you tell it the
> encoding in
> the header.
> </quote>
>
> That's true, but you will definitely need to set a default encoding if
> you are going to use python code. If not set, the default encoding is
> ascii and you will get the usual "encode error, ordinal not in range
> (128)" error when doing as a simple thing as
>     Print string_with_some_special_chars_inside.

you wont get any error when doing....
print string_with_some_special_chars_inside
... but you may if doing.....
print unicode_string_with_some_non_ascii_chars_inside

I find this inconvenient too, but that is the way that the Python language is 
defined. There is code inside Zope, and other libraries, that assumes Python 
behaves this way. Your call to sys.setdefaultencoding will break these 
libraries, because they change the Python language globally.

You need....
print unicode_string_with_some_non_ascii_chars_inside.encode('utf-8')

-- 
Toby Dickenson - http://www.geminidataloggers.com/people/tdickenson

Want a job like mine?  http://www.geminidataloggers.com/jobs for Software
Engineering jobs at Gemini Data Loggers in Chichester, West Sussex, England