[Python-3000] Displaying strings containing unicode escapes at the interactive prompt (original) (raw)
atsuo ishimoto ishimoto at gembook.org
Thu Apr 17 01:09:25 CEST 2008
- Previous message: [Python-3000] Displaying strings containing unicode escapes
- Next message: [Python-3000] Displaying strings containing unicode escapes at the interactive prompt
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
2008/4/16, Nick Coghlan <ncoghlan at gmail.com>:
Oleg Broytmann wrote: > On Wed, Apr 16, 2008 at 10:11:13PM +1000, Nick Coghlan wrote: >> atsuo ishimoto wrote: >>> IOError: [Errno 2] No such file or directory: '\u65e5\u672c\u8a9e' >> This is starting to seem to me more like something to be addressed >> through sys.displayhook/excepthook at the interactive interpreter level > > The problem manifests itself in scripts, too: > > Traceback (most recent call last): > File "./ttt.py", line 4, in > open("тест") # filename is in koi8-r encoding > IOError: [Errno 2] No such file or directory: '\xd4\xc5\xd3\xd4'
Hmm, the io module along with sys.stdout/err may be a better way to attack the problem then. Given: import sys, io class ParseUnicodeEscapes(io.TextIOWrapper): def write(self, text): super().write(text.encode('latin-1').decode('unicodeescape')) args = (sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, None, sys.stdout.linebuffering) sys.stdout = ParseUnicodeEscapes(*args) args = (sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, None, sys.stderr.linebuffering) sys.stderr = ParseUnicodeEscapes(*args) You get: >>> "тест" 'тест' >>> open("тест")
I got:
print("あ") Traceback (most recent call last): File "", line 1, in File "", line 3, in write UnicodeEncodeError: 'latin-1' codec can't encode character 'あ' in position 0: ordinal not in range(256) print('\'+'u0041') A
Your hack doesn't work. Displayhook hack doesn't work, too.
Question: Are you happy if you are forced to live with these hacks forever? If not, why do you think I'll accept your suggestion?
- Previous message: [Python-3000] Displaying strings containing unicode escapes
- Next message: [Python-3000] Displaying strings containing unicode escapes at the interactive prompt
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]