Reputation: 1507
Imagine I have the following code:
# -*- coding: utf-8 -*-
from getpass import getpass
#print u'Введите пароль!'.encode('cp866')
passwd = getpass (u'Введите пароль!'.encode('cp866'))
This is for asking user to enter his password in Windows console (thus encoding is 'cp866'). The user sees the following prompt: "???¤?a? ? aR?i!" But if you uncomment the line with print, you will see the correct text. I already have a workaround, first make a print statement, then issue getpass with empty prompt, but I just want to know what exactly is wrong with my code and why do I get this result? One hint if it make it clearer: getpass uses msvcrt.putch (char) to put characters on the console.
Upvotes: 5
Views: 1525
Reputation: 930
The state of unicode is really embarassing in Python in my experience. From time to time I encounter these type of problems in several libraries suffering from UTF myopia. As in kichik's answer, most of the time changing the encoding to an archaic one is the only solution. See http://bugs.python.org/issue1436203 for your particular bug.
However this might not be applicable in a site-wide scheme that uses unicode exclusively. If this is the case, your only option would be duplicating the functionality of the so called 'protable' getpass using raw_input, sys.stdin or the likes.
Upvotes: 0
Reputation: 34734
putch()
might be doing its own translation from your ANSI codepage (cp1251) to your OEM codepage (cp866). Try encoding with cp1251 instead.
Upvotes: 1