Revised PEP 349: Allow str() to return unicode strings

Dieter Maurer dieter at handshake.de
Wed Aug 24 15:11:18 EDT 2005


Neil Schemenauer <nas at arctrix.com> writes on Mon, 22 Aug 2005 15:31:42 -0600:
> ...
>     Some code may require that str() returns a str instance.  In the
>     standard library, only one such case has been found so far.  The
>     function email.header_decode() requires a str instance and the
>     email.Header.decode_header() function tries to ensure this by
>     calling str() on its argument.  The code was fixed by changing
>     the line "header = str(header)" to:
> 
>         if isinstance(header, unicode):
>             header = header.encode('ascii')

Note, that this is not equivalent to the old "str(header)":

  "str(header)" used Python's "default encoding" while the
  new code uses 'ascii'.

  The new code might be more correct than the old one has been.


> ...
> Alternative Solutions
> 
>     A new built-in function could be added instead of changing str().
>     Doing so would introduce virtually no backwards compatibility
>     problems.  However, since the compatibility problems are expected to
>     rare, changing str() seems preferable to adding a new built-in.

Can we get a new builtin with the exact same behaviour as
the current "str" which can be used when we do require an "str"
(and cannot use a "unicode").



Dieter



More information about the Python-list mailing list