classification
Title: getpass.getpass() should allow Unicode prompts
Type: enhancement Stage: resolved
Components: Library (Lib), Unicode Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, kxroberto, vstinner, wikipedian
Priority: normal Keywords: easy

Created on 2006-02-21 21:52 by wikipedian, last changed 2010-08-02 09:15 by eric.araujo. This issue is now closed.

Messages (4)
msg61217 - (view) Author: Daniel Herding (wikipedian) Date: 2006-02-21 21:52
Hi,

when using Python 2.4.1 on Linux, I get this:

>>> import getpass
>>> getpass.getpass(u'Contraseña: ')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python2.4/getpass.py", line 35, in
unix_getpass
    passwd = _raw_input(prompt)
  File "/usr/lib/python2.4/getpass.py", line 74, in
_raw_input
    prompt = str(prompt)
UnicodeEncodeError: 'ascii' codec can't encode
character u'\xf1' in position 8: ordinal not in range(128)

This one works, but only if the user's console is using
UTF-8 encoding:

>>> getpass.getpass(u'Contraseña: '.encode('utf-8'))
Contraseña:

I think you should be able to directly give a Unicode
string to getpass(), as you can also give a Unicode
string to print.


Daniel
msg61218 - (view) Author: kxroberto (kxroberto) Date: 2006-04-24 10:39
Logged In: YES 
user_id=972995

That error line in getpass should probably simply be
commented out?
Whats your sys.stdout.encoding ? (Probably not enough for
spanish chars as well)

The others will probably recommend to create sitecustomize.py

I'd say: Its a general trouble with Python that (console)
output encoding is in 'strict' mode by default. 
That crashes more apps, than it helps for discipline ...
And its hard even to grasp it and switch it for 'replace':
you'd need to replace sys.stdout with a custom formatter etc..

On MS Windows OS the MBCS conversion is kind of 'replace'
for good reason.  Even in Python on Win:
u"abc\u034adef".encode('mbcs') does replacing.

tty's,browser,windows,... and maybe most text mode files
(and even bin ones (latin1/replace?) ?) should not break.
The cases, where encoding should break strictly are
naturally cases where the programmer is aware (and puts the
conversion into strict mode expicitely)

"Python cannot print": Even a harmless 

>>> print u"abc\u034adef" 

throws an exception. That is questionable enough:

http://groups.google.de/group/comp.lang.python/msg/eac9b025b93e0642

Maybe in future Python:
* encoding tuples should be accepted everywhere as
alternative to second argument: .encode((enc,error))
* text file's .encoding/.write_encoding=(xy,'replace') or
(xy,'backslashreplace')
* bin file's .encoding=('latin1','strict')
* site(customize).py's defaultencoding should be/accept a
tuple ('ascii','replace')

-robert
msg106279 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2010-05-22 01:41
As I wrote in issue #7768: this issue is already fixed in Python3 (getpass supports unicode prompt) and you should use Python3 instead of Python2 because Python3 has a much better unicode support. It would be harder to fix Python2, and this issue has no patch. Since we are close to 2.7rc1 (and 2.7 should be the last major version of the 3.x branch), I would like to say that this issue should be closed as "wontfix".
msg112436 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-02 09:15
I agree with Victor. Use 3.1 if you don’t depend on unported libraries, else explicitly encode with sys.stdout.encoding or set PYTHONIOENCODING in the environment.
History
Date User Action Args
2010-08-02 09:15:52eric.araujosetstatus: open -> closed
resolution: out of date
messages: + msg112436

stage: test needed -> resolved
2010-05-22 01:41:31vstinnersetnosy: + vstinner
messages: + msg106279
2010-02-16 04:20:57eric.araujosetnosy: + eric.araujo
2009-04-22 12:47:10ajaksu2setkeywords: + easy
2009-03-21 03:25:38ajaksu2setstage: test needed
components: + Library (Lib)
versions: + Python 2.7
2006-02-21 21:52:10wikipediancreate