Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59574

Re: python 3.3 repr

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'defaults': 0.07; 'redirected': 0.07; 'skip:\\ 20': 0.07; 'string': 0.09; 'ascii': 0.09; 'encode': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subset': 0.09; 'jan': 0.12; 'windows': 0.15; 'codec': 0.16; 'dangerous,': 0.16; "function's": 0.16; 'opened.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'repr': 0.16; 'subject:3.3': 0.16; 'unicode.': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'seems': 0.21; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'bytes': 0.24; 'file.': 0.24; 'looks': 0.24; 'skip:" 30': 0.26; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'character': 0.29; "i'm": 0.30; 'bad.': 0.31; 'occurs': 0.31; 'pipe': 0.31; 'file': 0.32; 'run': 0.32; '(most': 0.33; 'problem': 0.35; "can't": 0.35; 'knows': 0.35; 'convert': 0.35; 'but': 0.35; 'idle': 0.36; 'responsible': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'skip:u 10': 0.60; 'skip:c 50': 0.60; 'received:173': 0.61; 'simple': 0.61; 'directed': 0.83; 'fragment': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: python 3.3 repr
Date Fri, 15 Nov 2013 18:49:49 -0500
References <5286054F.6000707@chamonix.reportlab.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-173-59-117-133.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0
In-Reply-To <5286054F.6000707@chamonix.reportlab.co.uk>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2690.1384559408.18130.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384559408 news.xs4all.nl 15891 [2001:888:2000:d::a6]:55517
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59574

Show key headers only | View raw


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 bang.py
> Traceback (most recent call last):
>    File "bang.py", line 2, in <module>
>      print("repr=%s" % repr(u'\xc1'))
>    File "C:\Python33\lib\encodings\cp437.py", 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')
"'\\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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python 3.3 repr Terry Reedy <tjreedy@udel.edu> - 2013-11-15 18:49 -0500

csiph-web