[python] raw_input() bug (bylo ... vyvoj Pythonu, jimavy pribeh)

Petr Prikryl PrikrylP na skil.cz
Úterý Červenec 12 12:19:24 CEST 2005


pavel.kosina
> [...] Mě zase rozčiluje, že nejde 
> raw_input s unicodem - už tu 
> jednou bylo, od pepra, že hlásil  bug:
> 
> [ 1099364 ] raw_input() displays wrong unicode prompt
> https://sourceforge.net/tracker/index.php?func=detail&aid=1099364&group_id=5470&atid=105470
> 
> ale ted jsem ho tam hledal a vůbec tam není. Taky nechápu, 
> proč tam nejde vyhledávat podle ID.

Je to tam, ale je mezi uzavřenými (kliknutím na link 
se tam dá dostat). Mezi tím došlo k určitému posunu,
ale nejsem s tím tak docela spokojený. Částečné řešení
lze nalézt u poslední poznámky by birkenfeld, tj. explicitně

    raw_input(s.encode(sys.stdout.encoding))

Dá se to vyřešit centralizovaně například definicí vlastní funkce:

def my_input(s):
    import sys
    return raw_input(s.encode(sys.stdout.encoding))

Ale moc se mi to nelíbí, protože takhle by měla fungovat
raw_input() už "od narození".

Aby to celé nezapadlo, přidal jsem výzvu ke komentářům
níže zmíněného patch

https://sourceforge.net/tracker/index.php?func=detail&aid=1214889&group_id=5470&atid=305470

pepr

--------------------------------------------------------------------------------

Date: 2005-06-26 13:34
Sender: birkenfeld
Logged In: YES 
user_id=1188172

Actually, your sys.stdout.encoding is set to something
different than cp1250, which is why the result of DOS type
looks the same as the one of print.

This is because print observes sys.stdout.encoding, while
sys.stdout.write uses the system default encoding, which is,
as you set it, cp1250 and is displayed wrong on the console.

Closing this bug, as it is currently expected behaviour (but
will perhaps change when patch #1214889 is accepted).
 
--------------------------------------------------------------------------------

Date: 2005-06-27 22:56
Sender: prikryl
Logged In: YES 
user_id=771873

Should I understand it that there is no bug, but I do use it 
incorrectly? I cannot agree that this is expected behaviour. (I 
am not the only one who found this strange.) 

Of course, the sys.stdout.encoding is different for a DOS 
window (cp852) than the default encoding (cp1250). Windows 
simply behaves this way when working with DOS window 
(because of legacy DOS applications).

I do not complain on behaviour of sys.stdout.write() but on 
behaviour of raw_input(). The output of raw_input() prompt 
should be displayed the same way as the print diplays the 
results to the user. The raw_input() is used for building user 
interface. Its prompt should not be displayed differently in 
windows that use different encoding (i.e. DOS console vs. 
say IDLE console).

In other words, how should I use raw_input() to make it 
working correctly?
 
--------------------------------------------------------------------------------

Date: 2005-06-27 23:40
Sender: birkenfeld
Logged In: YES 
user_id=1188172

You'll have to explicitly encode the unicode string using
raw_input(s.encode(sys.stdout.encoding)).

As said, this behaviour will change if the patch mentioned
is accepted.
 
--------------------------------------------------------------------------------




Další informace o konferenci Python