[Zope3-dev] Re: [Zope-Checkins] CVS: Zope3/lib/python/Zope/PageTemplate - TALES.py:1.3

Guido van Rossum guido@python.org
Sat, 15 Jun 2002 21:15:39 -0400


> > Sounds like FUD to me.  Maybe mixing string types causes problems in
> > other languages, but Python's rules for combining ASCII and Unicode
> > strings are designed to make mixing of the two always do the right
> > thing. 
> 
> They fail miserably.
> 
> They allow program errors to go undetected until the cause of the
> errors cannot be determined. I just got bitten hard by this when
> unicode from XML processing leaked into HTTP response data. This
> promoted the HTTP response data to unicode, which "worked" until I
> decided I wanted to output image data, which couldn't be promoted to
> unicode.

I don't understand how a proposal to make all text literals Unicode
would help here -- it would have the exact same problem from your
example.

> I've heard a number of horror stories like this.
> 
> This is an example where implicit is worse than explicit.

If you want all default promotions from 8-bit strings to Unicode
without an explicit encoding to fail, it's easy to accomplish by
setting the default encoding to an encoding that always raises an
error.

To do this:

import sys
reload(sys) # This gives you back sys.setdefaultencoding()
sys.setdefaultencoding("whatever")

(You'd have to define and register a decoding named "whatever" too.)

--Guido van Rossum (home page: http://www.python.org/~guido/)