Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

does raw_input() return unicode?

15 views
Skip to first unread message

Stuart McGraw

unread,
Oct 9, 2006, 7:36:12 PM10/9/06
to
In the announcement for Python-2.3
http://groups.google.com/group/comp.lang.python/msg/287e94d9fe25388d?hl=en
it says "raw_input(): can now return Unicode objects".

But I didn't see anything about this in Andrew Kuchling's
"2.3 What's New", nor does the current python docs for
raw_input() say anything about this. A test on a MS
Windows system with a cp932 (japanese) default locale
shows the object returned by raw_input() is a str() object
containing cp932 encoded text. This remained true even
when I set Python's default encoding to cp932 (in
sitecustomize.py).

So, does raw_input() ever return unicode objects and if
so, under what conditions?

Duncan Booth

unread,
Oct 10, 2006, 3:45:01 AM10/10/06
to
"Stuart McGraw" <smcg4...@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?

Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, codecs
>>> sys.stdin.encoding
'cp437'
>>> sys.stdin = codecs.getreader(sys.stdin.encoding)(sys.stdin)
>>> raw_input()
hello world
still going?
^Z
^Z
u'hello world'
>>>

"Martin v. Löwis"

unread,
Oct 10, 2006, 3:53:52 AM10/10/06
to
Stuart McGraw schrieb:

> So, does raw_input() ever return unicode objects and if
> so, under what conditions?

At the moment, it only returns unicode objects when invoked
in the IDLE shell, and only if the character entered cannot
be represented in the locale's charset.

Regards,
Martin

Theerasak Photha

unread,
Oct 10, 2006, 4:09:31 AM10/10/06
to pytho...@python.org

Why only IDLE? Does urwid or another console UI toolkit avoid this somehow?

-- Theerasak

Fredrik Lundh

unread,
Oct 10, 2006, 4:20:18 AM10/10/06
to pytho...@python.org
Theerasak Photha wrote:

>>> So, does raw_input() ever return unicode objects and if
>>> so, under what conditions?
>>
>> At the moment, it only returns unicode objects when invoked
>> in the IDLE shell, and only if the character entered cannot
>> be represented in the locale's charset.
>

> Why only IDLE? Does urwid or another console UI toolkit avoid this somehow?

Martin was probably thinking of the standard distribution.

The 2.3 note says that "raw_input() *can* return Unicode", not that it
"should" or "must" do it.

</F>

Theerasak Photha

unread,
Oct 10, 2006, 4:32:25 AM10/10/06
to pytho...@python.org
On 10/10/06, Fredrik Lundh <fre...@pythonware.com> wrote:

> Martin was probably thinking of the standard distribution.
>
> The 2.3 note says that "raw_input() *can* return Unicode", not that it
> "should" or "must" do it.

Practically speaking, at the heart of the matter: as of Python 2.5
final, does or can raw_input() return Unicode under the appropriate
circumstances, according to user wishes?

(Yes, I would test, but I am presently away from my Linux box with
Python, and can't install it here.)

-- Theerasak

Fredrik Lundh

unread,
Oct 10, 2006, 4:42:00 AM10/10/06
to pytho...@python.org
Theerasak Photha wrote:

> Practically speaking, at the heart of the matter: as of Python 2.5
> final, does or can raw_input() return Unicode under the appropriate
> circumstances, according to user wishes?

didn't Martin just answer that question?

</F>

Theerasak Photha

unread,
Oct 10, 2006, 4:51:04 AM10/10/06
to pytho...@python.org
On 10/10/06, Fredrik Lundh <fre...@pythonware.com> wrote:

*slaps forehead* D'oh!

-- Theerasak

"Martin v. Löwis"

unread,
Oct 10, 2006, 1:30:10 PM10/10/06
to Theerasak Photha, pytho...@python.org
Theerasak Photha schrieb:

>> At the moment, it only returns unicode objects when invoked
>> in the IDLE shell, and only if the character entered cannot
>> be represented in the locale's charset.
>
> Why only IDLE? Does urwid or another console UI toolkit avoid this somehow?

I admit I don't know what urwid is; from a shallow description I find
("a console user interface library") I can't see the connection to
raw_input(). How would raw_input() ever use urwid?

Regards,
Martin

"Martin v. Löwis"

unread,
Oct 10, 2006, 1:30:10 PM10/10/06
to Theerasak Photha, pytho...@python.org
Theerasak Photha schrieb:

>> At the moment, it only returns unicode objects when invoked
>> in the IDLE shell, and only if the character entered cannot
>> be represented in the locale's charset.
>
> Why only IDLE? Does urwid or another console UI toolkit avoid this somehow?

I admit I don't know what urwid is; from a shallow description I find

Stuart McGraw

unread,
Oct 10, 2006, 7:02:45 PM10/10/06
to

"Martin v. Löwis" <mar...@v.loewis.de> wrote in message news:452b5190$0$29833$9b62...@news.freenet.de...

Thanks for the answer.

Also, if anyone has a solution for Duncan Booth's attempt
to wrap stdin, I would find it very useful too!

"Duncan Booth" <duncan...@invalid.invalid> wrote:


> "Stuart McGraw" <smcg4...@friizz.RimoovAllZZs.com> wrote:
>
> > So, does raw_input() ever return unicode objects and if
> > so, under what conditions?
> >

Theerasak Photha

unread,
Oct 10, 2006, 9:59:47 PM10/10/06
to pytho...@python.org
On 10/10/06, "Martin v. Löwis" <mar...@v.loewis.de> wrote:

The other way around: would urwid use raw_input() or other Python
input functions anywhere?

And what causes Unicode input to work in IDLE alone?

-- Theerasak

Leo Kislov

unread,
Oct 11, 2006, 12:27:06 AM10/11/06
to

Other applications except python are actually free to implement unicode
stdin. python cannot do it because of backward compatibility. You can
argue that python interactive console could do it too, but think about
it this way: python interactive console deliberately behaves like a
running python program would.

Leo Kislov

unread,
Oct 11, 2006, 12:32:27 AM10/11/06
to

Duncan Booth wrote:
> "Stuart McGraw" <smcg4...@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)

"Martin v. Löwis"

unread,
Oct 11, 2006, 12:47:29 AM10/11/06
to Theerasak Photha
Theerasak Photha schrieb:

> The other way around: would urwid use raw_input() or other Python
> input functions anywhere?

Since I still don't know what urwid is, I can't answer the question.
It should be easy enough to grep its source code to find out whether
it ever uses raw_input.

> And what causes Unicode input to work in IDLE alone?

Because in IDLE, it is possible to enter characters that are not
in the user's charset. For example, if the user's charset is
cp-1252 (western european), you can still enter cyrillic characters
into IDLE. This is not possible in a regular terminal.

Regards,
Martin

Neil Cerutti

unread,
Oct 11, 2006, 8:21:03 AM10/11/06
to
On 2006-10-11, Leo Kislov <Leo.K...@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 (mham...@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

"Martin v. Löwis"

unread,
Oct 11, 2006, 3:49:15 PM10/11/06
to Neil Cerutti
Neil Cerutti schrieb:

> 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?

Simple and easy: yes, they are.

Regards,
Martin

Neil Cerutti

unread,
Oct 12, 2006, 8:47:42 AM10/12/06
to

Oh, phew!

--
Neil Cerutti
Any time I've taken the mound, it's always been the old
Samson-and-Goliath story written about me. --Randy Johnson

0 new messages