From c8494cdc39186250e4f814dfc9f86707bc4476c3 Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Mon, 18 Aug 2014 22:04:25 -0500 Subject: [PATCH] Set prompt encoding to a sane value when sys.stdout.encoding is None Fixes #8644 --- lib/ansible/callbacks.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index 44d2404d78..54c84a2420 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -25,6 +25,7 @@ import fnmatch import tempfile import fcntl import constants +import locale from ansible.color import stringc import logging @@ -645,7 +646,13 @@ class PlaybookCallbacks(object): msg = 'input for %s: ' % varname def prompt(prompt, private): - msg = prompt.encode(sys.stdout.encoding) + if sys.stdout.encoding: + msg = prompt.encode(sys.stdout.encoding) + else: + # when piping the output, or at other times when stdout + # may not be the standard file descriptor, the stdout + # encoding may not be set, so default to something sane + msg = prompt.encode(locale.getpreferredencoding()) if private: return getpass.getpass(msg) return raw_input(msg)