目の前に僕らの道がある

勉強会とか、技術的にはまったことのメモ

Sphinxでユニコードエラーが出た時の対処のメモ

Sphinxで日本語を含む文章をmake htmlした時、下記のようなエラーが出た。

masasuzu@leon% make html
mkdir -p build/html build/doctrees
sphinx-build -b html -d build/doctrees   source build/html
Sphinx v0.4.2, building html
trying to load pickled env... not found
building [html]: targets for 4 source files that are out of date
updating environment: 4 added, 0 changed, 0 removed
reading... acl index snmp vlan_stp
pickling the env... done
checking consistency...
writing output... acl Exception occurred:
  File "/usr/lib/python2.5/site-packages/docutils/writers/html4css1/__init__.py", line 305, in astext
    + self.body + self.body_suffix)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 170: ordinal not in range(128)
The full traceback has been saved in /tmp/sphinx-err-XJn9_Q.log, if you want to report the issue to the author.
Please also report this if it was a user error, so that a better error message can be provided next time.
Send reports to sphinx-dev@googlegroups.com. Thanks!
make: *** [html] エラー 1

とりあえず、ユニコードをうまく扱えていないっぽいので、デフォルトエンコーディングを調べてみる。

masasuzu@leon% python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'

たぶんコレが原因ぽいので、/usr/lib/python2.5/sitecustomize.pyに2行コードを追記して、Pythonのデフォルトエンコーディングをutf8に変更してみる。

#/usr/lib/python2.5/sitecustomize.py
import sys
sys.setdefaultencoding("utf-8")

今回はうまくいったぽい。ただ、エンコーディングの設定を変えなくてもうまくmakeできたファイルもあるんだけど、その辺が少し謎。

masasuzu@leon% make html
mkdir -p build/html build/doctrees
sphinx-build -b html -d build/doctrees   source build/html
Sphinx v0.4.2, building html
trying to load pickled env... not found
building [html]: targets for 4 source files that are out of date
updating environment: 4 added, 0 changed, 0 removed
reading... acl index snmp vlan_stp
pickling the env... done
checking consistency...
writing output... acl index snmp vlan_stp
finishing...
writing additional files... genindex modindex search
copying images... images/acl_vacl.png images/acl_racl.png
copying static files...
dumping search index...
build succeeded.

Build finished. The HTML pages are in build/html.