summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshimizukawa <shimizukawa@gmail.com>2013-06-09 09:06:21 +0900
committershimizukawa <shimizukawa@gmail.com>2013-06-09 09:06:21 +0900
commitc48fd4ee27448b18581549d689e523b6e7060891 (patch)
tree2de37c2ed00d9a6b751a14bb0c313fb1e90d5e32
parent73d184b87a7ac1bfe7da983415f17b2454e05edb (diff)
downloadsphinx-c48fd4ee27448b18581549d689e523b6e7060891.tar.gz
Fix sphinx-quickstart raises UnicodeEncodeError if "Project version" includes non-ASCII characters. Closes #1188
-rw-r--r--CHANGES2
-rw-r--r--sphinx/quickstart.py19
-rw-r--r--tests/test_quickstart.py16
3 files changed, 35 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index e34b61d4..0b8cc80d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -26,6 +26,8 @@ Bugs fixed
Marcin Wojdyr.
* PR#145: In parallel builds, sphinx drops second document file to write.
Thanks to tychoish.
+* #1188: sphinx-quickstart raises UnicodeEncodeError if "Project version"
+ includes non-ASCII characters.
Release 1.2 (beta1 released Mar 31, 2013)
diff --git a/sphinx/quickstart.py b/sphinx/quickstart.py
index e7a28c56..41b4f3c5 100644
--- a/sphinx/quickstart.py
+++ b/sphinx/quickstart.py
@@ -866,9 +866,24 @@ def ok(x):
def do_prompt(d, key, text, default=None, validator=nonempty):
while True:
if default:
- prompt = purple(PROMPT_PREFIX + '%s [%s]: ' % (text, default))
+ prompt = PROMPT_PREFIX + '%s [%s]: ' % (text, default)
else:
- prompt = purple(PROMPT_PREFIX + text + ': ')
+ prompt = PROMPT_PREFIX + text + ': '
+ if sys.version_info < (3, 0):
+ # for Python 2.x, try to get a Unicode string out of it
+ if prompt.encode('ascii', 'replace').decode('ascii', 'replace') \
+ != prompt:
+ if TERM_ENCODING:
+ prompt = prompt.encode(TERM_ENCODING)
+ else:
+ print turquoise('* Note: non-ASCII default value provided '
+ 'and terminal encoding unknown -- assuming '
+ 'UTF-8 or Latin-1.')
+ try:
+ prompt = prompt.encode('utf-8')
+ except UnicodeEncodeError:
+ prompt = prompt.encode('latin1')
+ prompt = purple(prompt)
x = term_input(prompt).strip()
if default and not x:
x = default
diff --git a/tests/test_quickstart.py b/tests/test_quickstart.py
index 2340e04b..f2de2f56 100644
--- a/tests/test_quickstart.py
+++ b/tests/test_quickstart.py
@@ -28,6 +28,12 @@ def mock_raw_input(answers, needanswer=False):
raise AssertionError('answer for %r missing and no default '
'present' % prompt)
called.add(prompt)
+ if sys.version_info < (3, 0):
+ prompt = str(prompt) # Python2.x raw_input emulation
+ # `raw_input` encode `prompt` by default encoding to print.
+ else:
+ prompt = unicode(prompt) # Python3.x input emulation
+ # `input` decode prompt by default encoding before print.
for question in answers:
if prompt.startswith(qs.PROMPT_PREFIX + question):
return answers[question]
@@ -95,6 +101,16 @@ def test_do_prompt():
raises(AssertionError, qs.do_prompt, d, 'k6', 'Q6', validator=qs.boolean)
+def test_do_prompt_with_multibyte():
+ d = {}
+ answers = {
+ 'Q1': ur'\u30c9\u30a4\u30c4',
+ }
+ qs.term_input = mock_raw_input(answers)
+ qs.do_prompt(d, 'k1', 'Q1', default=ur'\u65e5\u672c')
+ assert d['k1'] == ur'\u30c9\u30a4\u30c4'
+
+
@with_tempdir
def test_quickstart_defaults(tempdir):
answers = {