setuptoolsを使用してPythonパッケージのsetup.pyを作成していて、long_descriptionフィールドに非ASCII文字を含めたいと考えていました。
#!/usr/bin/env python
from setuptools import setup
setup(...
long_description=u"...", # in real code this value is read from a text file
...)
残念ながら、unicodeオブジェクトをsetup()に渡すと、UnicodeEncodeErrorで次の2つのコマンドのいずれかが壊れます。
python setup.py --long-description | rst2html pythonsetup.pyアップロード
long_descriptionフィールドに生のUTF-8文字列を使用すると、次のコマンドがUnicodeDecodeErrorで中断します。
pythonsetup.pyレジスタ
私は通常、「python setup.py sdist register upload」を実行してソフトウェアをリリースします。これは、sys.argvを調べて、適切なオブジェクトタイプを渡す醜いハックがすぐに実行されることを意味します。
結局、私はあきらめて、別の醜いハックを実装しました:
class UltraMagicString(object):
# Catch-22:
# - if I return Unicode, python setup.py --long-description as well
# as python setup.py upload fail with a UnicodeEncodeError
# - if I return UTF-8 string, python setup.py sdist register
# fails with an UnicodeDecodeError
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def __unicode__(self):
return self.value.decode('UTF-8')
def __add__(self, other):
return UltraMagicString(self.value + str(other))
def split(self, *args, **kw):
return self.value.split(*args, **kw)
...
setup(...
long_description=UltraMagicString("..."),
...)
もっと良い方法はありませんか?