[Python-3000] Displaying strings containing unicode escapes at the interactive prompt

Guido van Rossum guido at python.org
Thu Apr 17 07:38:15 CEST 2008


On Wed, Apr 16, 2008 at 10:20 PM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Alex Martelli wrote:
>  > I disagree: I always recommend using %r to display (in an error
>  > message, log entry, etc), a string that may be in error,
>
>  For debugging messages, yes, but not output produced
>  in the normal course of operation. And "File Not Found"
>  I consider to be in the latter category -- the user
>  typed in the wrong file name, but it's still a string,
>  and should be displayed to him as such.

I respectfully disagree. Control characters and such in the string
should *definitely* be escaped. Regarding printable characters outside
the ASCII range, see my post in another thread (which somehow nearly
everybody appears to have missed); in Py3k I propose to pass printable
Unicode characters unchanged through repr(). stdout/stderr will set
their error attribute to backslashreplace so that if their encoding is
ASCII or some such, out-of-range characters will be printed as \uxxxx
rather than raising an exception during printing. But as I said,
please follow up to my other post.

Another reason to use %r is that if someone manages to include \n in a
filename, with %s the log message might be spread across two lines,
possibly confusing log parsers and even providing ways to hide illegal
activities from log scanners.

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


More information about the Python-3000 mailing list