473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python 3.0 and repr

I don't understand the behavior of the interpreter in Python 3.0. I am
working at a command prompt in Windows (US English), which has a terminal
encoding of cp437.

In Python 2.5:

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>x=u'\u5000'
x
u'\u5000'

In Python 3.0:

Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit
(Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>x='\u5000'
x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\dev\python30\lib\io.py", line 1486, in write
b = encoder.encode(s)
File "c:\dev\python30\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_m ap)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u5000' in
position
1: character maps to <undefined>

Where I would have expected
>>x
'\u5000'

Shouldn't a repr() of x work regardless of output encoding? Another test:

Python 3.0rc1 (r30rc1:66507, Sep 18 2008, 14:47:08) [MSC v.1500 32 bit
(Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>bytes(range(256)).decode('cp437')
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\ r\x0e\x0f\x10\x11\x12\x13\x14\
x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
!"#$%&\'()*+,-./0123456789:;<=>?@ABC
DEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7fÇüéâä* çêëèïîìÄÅ
ÉæÆôöòûùÿÖÜ¢£¥₧ƒá*óúñѪº ⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║ ╝╜╛┐└┴┬├─┼╞╟╚╔╩╦ *═╬╧╨╤╥╙╘╒╓╫╪┘┌█ ▌▐▀
αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤* ÷≈°∙·√ⁿ²*\xa0'
>>bytes(range(256)).decode('cp437')[255]
'\xa0'

Characters that cannot be displayed in cp437 are being escaped, such as
0x00-0x1F, 0x7F, and 0xA0. Even if I incorrectly decode a value, if the
character exists in cp437, it is displayed:
>>bytes(range(256)).decode('latin-1')[255]
'ÿ'

However, for a character that isn't supported by cp437, incorrectly decoded:
>>bytes(range(256)).decode('cp437')[254]
'*'
>>bytes(range(256)).decode('latin-1')[254]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\dev\python30\lib\io.py", line 1486, in write
b = encoder.encode(s)
File "c:\dev\python30\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_m ap)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in
position 1:
character maps to <undefined>

Why not display '\xfe' here? It seems like this inconsistency would make it
difficult to write things like doctests that weren't dependent on the
tester's terminal. It also makes it difficult to inspect variables without
hex(ord(n)) on a character-by-character basis. Maybe repr() should always
display the ASCII representation with escapes for all other characters,
especially considering the "repr() should produce output suitable for eval()
when possible" rule.

What are others' opinions? Any insight to this design decision?

-Mark
Sep 28 '08 #1
2 2700
What are others' opinions? Any insight to this design decision?

The intention is that all printable characters in a string get displayed
in repr. This was in particular requested by Japanese users (but also by
other users of non-ASCII characters) which complained that repr() is
fairly useless if your strings actually contains *no* ASCII characters
(but all of them are printable).

Notice that repr() of the string actually succeeds; try
>>x='\u5000'
z=repr(x)
It is the printing of the repr that fails.
Maybe repr() should always display the ASCII representation with
escapes for all other characters
You can use the ascii() builtin if you want that.
especially considering the "repr() should produce output suitable for
eval() when possible" rule.
But that is preserved under the new behavior, also! Just try

pyx='\u5000'
pyeval(repr(x))==x
True

Regards,
Martin

P.S. How did you manage to get U+5000 into your data, on a system where
the terminal encoding is cp437? Google translates it as "Rash"; the
Unihan database also has "bewildered", "wildly".
Sep 28 '08 #2

""Martin v. Löwis"" <ma****@v.loewis.dewrote in message
news:48**********************@news.freenet.de...
>What are others' opinions? Any insight to this design decision?

The intention is that all printable characters in a string get displayed
in repr. This was in particular requested by Japanese users (but also by
other users of non-ASCII characters) which complained that repr() is
fairly useless if your strings actually contains *no* ASCII characters
(but all of them are printable).

Notice that repr() of the string actually succeeds; try
>>>x='\u5000'
z=repr(x)

It is the printing of the repr that fails.
>Maybe repr() should always display the ASCII representation with
escapes for all other characters

You can use the ascii() builtin if you want that.
>especially considering the "repr() should produce output suitable for
eval() when possible" rule.

But that is preserved under the new behavior, also! Just try

pyx='\u5000'
pyeval(repr(x))==x
True

Regards,
Martin
Thanks Martin, it's clear now. I just read about the new ascii() function
before seeing your reply.
P.S. How did you manage to get U+5000 into your data, on a system where
the terminal encoding is cp437? Google translates it as "Rash"; the
Unihan database also has "bewildered", "wildly".
I just picked that example out of the air. I study Chinese and knew it was
a character in that area of the Unicode map. My usual editors (PythonWin
and PyAlaMode from wxPython) don't work with Python 3, which was why I was
using the Windows cmd prompt.

Thanks,
Mark

Sep 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: el chupacabra | last post by:
I'm using mysqldb module and python 2.4. I'm a newbie. Thanks in advance. 1. Output desired: "hello" "world" I know that MySQL takes \n and \t and what not. But my python script, it...
17
by: dananrg | last post by:
I'm a little confused about what's out there for database modules at: http://python.org/topics/database/modules.html What I'd like to do is use Python to access an Oracle 9.X database for...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 385 open (+21) / 3790 closed (+21) / 4175 total (+42) Bugs : 1029 open (+43) / 6744 closed (+43) / 7773 total (+86) RFE : 262 open...
0
by: Terry Reedy | last post by:
Karen Tracey wrote: If answers here do not satisfy, the pydev list would be the place to request that this change be put off until 3.0, when changes that break are more permissible. I do not...
16
by: william tanksley | last post by:
I'm trying to convert the URLs contained in iTunes' XML file into a form comparable with the filenames returned by iTunes' COM interface. I'm writing a podcast sorter in Python; I'm using iTunes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.