diferences between 22 and python 23

Peter Hansen peter at engcorp.com
Wed Dec 3 22:46:14 EST 2003


Fredrik Lundh wrote:
> 
> Peter Hansen wrote:
> 
> > Wait a sec... are you telling me that my code, which has strings containing
> > binary data (which I believe has *always* been permitted), and which from
> > time to time might, say, produce an error traceback containing the content
> > from one such string and write it to a log file, then continue processing
> > safely, will now fail with an ugly crash because I haven't changed it to
> > specify a default encoding? (!!!)
> 
> 8-bit strings can still contain 8-bit data.
> 
> The only way you'll get a warning is if you use non-ASCII characters in
> source code, without specifying a source code encoding.  The warning
> is issued by the compiler, not the runtime.
> 
> (if you've used non-ASCII characters to embed *binary* string literals
> in your program, you deserve to be punished).

Ah, so using the proper escapes (\xnn and \nnn) will of course still work.

> The only way you'll get the "crash" the original poster had, is if you're
> trying to print Unicode strings containing non-ASCII data to an ASCII-
> only output stream.
> 
> (if you print 8-bit strings to an ASCII stream, Python assumes you know
> what you're doing).

Meaning I won't necessarily see anything meaningful or printable, but 
since I knew that and wanted that behaviour, I'll still get it.  I can
send 8-bit binary strings to a file or stdout, but not *Unicode* strings
unless they're properly encoded.

In that case, I did misunderstand and I don't see a problem.  Thanks, /F

-Peter




More information about the Python-list mailing list