diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-04-14 02:07:39 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-04-14 02:07:39 (GMT) |
commit | d5aa487cceac54fdf2b5c073157b899b3c8c9c94 (patch) | |
tree | e9532a4bb5bd51eeefd53dc818b8b448cd411a44 /Lib/getpass.py | |
parent | e544f9a27eec202d4a959dd2a255d8ce05aee663 (diff) | |
download | cpython-d5aa487cceac54fdf2b5c073157b899b3c8c9c94.zip cpython-d5aa487cceac54fdf2b5c073157b899b3c8c9c94.tar.gz cpython-d5aa487cceac54fdf2b5c073157b899b3c8c9c94.tar.bz2 |
#21169: fix getpass to use replace error handler on UnicodeEncodeError.
If the input stream encoding couldn't encode one or more of the
non-ascii characters in the prompt, it would fail, throwing a
UnicodeEncodeError. Now if that happens we re-encoding using the
'replace' error handler.
Patch by Kushal Das.
Diffstat (limited to 'Lib/getpass.py')
-rw-r--r-- | Lib/getpass.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/Lib/getpass.py b/Lib/getpass.py index 53c38b8..2740363 100644 --- a/Lib/getpass.py +++ b/Lib/getpass.py @@ -135,7 +135,12 @@ def _raw_input(prompt="", stream=None, input=None): input = sys.stdin prompt = str(prompt) if prompt: - stream.write(prompt) + try: + stream.write(prompt) + except UnicodeEncodeError: + prompt = prompt.encode(stream.encoding, 'replace') + prompt = prompt.decode(stream.encoding) + stream.write(prompt) stream.flush() # NOTE: The Python C API calls flockfile() (and unlock) during readline. line = input.readline() |