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.
Also fixes a resource warning that occurred when the fallback is taken.
Patch by Serhiy Storchaka.
(We couldn't figure out how to write tests for this.)