我正在使用 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 破坏以下两个命令中的任何一个
python setup.py --long-description | rst2html python setup.py 上传
如果我对 long_description 字段使用原始 UTF-8 字符串,则以下命令会因 UnicodeDecodeError 中断:
python setup.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("..."),
...)
没有更好的方法吗?