summaryrefslogtreecommitdiff
path: root/sphinx/cmd/quickstart.py
diff options
context:
space:
mode:
authorEric Wieser <wieser.eric@gmail.com>2017-10-20 23:18:46 -0700
committerGitHub <noreply@github.com>2017-10-20 23:18:46 -0700
commita4b7927a29cb0b38c00c7e83c2130cb426105bfa (patch)
treef9bf716895f6c1ca1295c91c15416853e6483211 /sphinx/cmd/quickstart.py
parent67981527a18e25a2aaf061cc470268b1074d235e (diff)
downloadsphinx-git-a4b7927a29cb0b38c00c7e83c2130cb426105bfa.tar.gz
quickstart: fix return type of term_decode
term_decode is documented as `(unicode) -> unicode`, but actually: * Accepts `bytes` arguments, despite not being documented to * Returns `bytes` when it shouldn't This is extracted from the more controversial #3584
Diffstat (limited to 'sphinx/cmd/quickstart.py')
-rw-r--r--sphinx/cmd/quickstart.py30
1 files changed, 15 insertions, 15 deletions
diff --git a/sphinx/cmd/quickstart.py b/sphinx/cmd/quickstart.py
index ac0859c31..af0333d65 100644
--- a/sphinx/cmd/quickstart.py
+++ b/sphinx/cmd/quickstart.py
@@ -138,25 +138,25 @@ def ok(x):
def term_decode(text):
- # type: (unicode) -> unicode
+ # type: (Union[bytes,unicode]) -> unicode
if isinstance(text, text_type):
return text
- # for Python 2.x, try to get a Unicode string out of it
- if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
- return text
-
+ # Use the known encoding, if possible
if TERM_ENCODING:
- text = text.decode(TERM_ENCODING)
- else:
- print(turquoise('* Note: non-ASCII characters entered '
- 'and terminal encoding unknown -- assuming '
- 'UTF-8 or Latin-1.'))
- try:
- text = text.decode('utf-8')
- except UnicodeDecodeError:
- text = text.decode('latin1')
- return text
+ return text.decode(TERM_ENCODING)
+
+ # If ascii is safe, use it with no warning
+ if text.decode('ascii', 'replace').encode('ascii', 'replace') == text:
+ return text.decode('ascii')
+
+ print(turquoise('* Note: non-ASCII characters entered '
+ 'and terminal encoding unknown -- assuming '
+ 'UTF-8 or Latin-1.'))
+ try:
+ return text.decode('utf-8')
+ except UnicodeDecodeError:
+ return text.decode('latin1')
def do_prompt(d, key, text, default=None, validator=nonempty):