python 3.3 repr

Terry Reedy tjreedy at
Sat Nov 16 00:49:49 CET 2013

On 11/15/2013 6:28 AM, Robin Becker wrote:
> I'm trying to understand what's going on with this simple program
> if __name__=='__main__':
>      print("repr=%s" % repr(u'\xc1'))
>      print("%%r=%r" % u'\xc1')
> On my windows XP box this fails miserably if run directly at a terminal
> C:\tmp> \Python33\python.exe
> Traceback (most recent call last):
>    File "", line 2, in <module>
>      print("repr=%s" % repr(u'\xc1'))
>    File "C:\Python33\lib\encodings\", line 19, in encode
>      return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\xc1' in
> position 6: character maps to <undefined>
> If I run the program redirected into a file then no error occurs and the
> the result looks like this
> C:\tmp>cat fff
> repr='┴'
> %r='┴'
> and if I run it into a pipe it works as though into a file.
> It seems that repr thinks it can render u'\xc1' directly which is a
> problem since print then seems to want to convert that to cp437 if
> directed into a terminal.
> I find the idea that print knows what it's printing to a bit dangerous,

print() just calls file.write(s), where file defaults to sys.stdout, for 
each string fragment it creates. write(s) *has* to encode s to bytes 
according to some encoding, and it uses the encoding associated with the 
file when it was opened.

> but it's the repr behaviour that strikes me as bad.
> What is responsible for defining the repr function's 'printable'
 > so that repr would give me say an Ascii rendering?

That is not repr's job. Perhaps you are looking for
 >>> repr(u'\xc1')
 >>> ascii(u'\xc1')
The above is with Idle on Win7. It is *much* better than the 
intentionally crippled console for working with the BMP subset of unicode.

Terry Jan Reedy

More information about the Python-list mailing list