does raw_input() return unicode?

Leo Kislov Leo.Kislov at gmail.com
Wed Oct 11 06:32:27 CEST 2006


Duncan Booth wrote:
> "Stuart McGraw" <smcg4191zz at friizz.RimoovAllZZs.com> wrote:
>
> > So, does raw_input() ever return unicode objects and if
> > so, under what conditions?
> >
> It returns unicode if reading from sys.stdin returns unicode.
>
> 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)




More information about the Python-list mailing list