does raw_input() return unicode?

Neil Cerutti horpner at yahoo.com
Wed Oct 11 08:21:03 EDT 2006


On 2006-10-11, Leo Kislov <Leo.Kislov at gmail.com> wrote:
>> Unfortunately, I can't tell you how to make sys.stdin return
>> unicode for use with raw_input. I tried what I thought should
>> work and as you can see it messed up the buffering on stdin.
>> Does anyone else know how to wrap sys.stdin so it returns
>> unicode but is still unbuffered?
>
> Considering that all consoles are ascii based, the following
> should work where python was able to determine terminal
> encoding:
>
> class ustdio(object):
>     def __init__(self, stream):
>         self.stream = stream
>         self.encoding = stream.encoding
>     def readline(self):
>         return self.stream.readline().decode(self.encoding)
>
> sys.stdin = ustdio(sys.stdin)
>
> answer = raw_input()
> print type(answer)

This interesting discussion led me to a weird discovery:

PythonWin 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond at skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> import sys
>>> sys.stdout.encoding
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "C:\edconn32\Python24\Lib\site-packages\pythonwin\pywin\mfc\object.py", line 18, in __getattr__
    return getattr(o, attr)
AttributeError: encoding
>>> sys.stdin.encoding
>>> 

I'm all mindboggley. Just when I thought I was starting to
understand how this character encoding stuff works. Are
PythonWin's stdout and stdin implementations is incomplete?

-- 
Neil Cerutti
A song fest was hell at the Methodist church Wednesday. --Church
Bulletin Blooper 



More information about the Python-list mailing list