我在写一篇设置.py对于使用setuptools并希望在long_description字段中包含非ASCII字符的Python包:
#!/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
^{pr2}$如果在long_description字段中使用原始UTF-8字符串,则以下命令将以UnicodeDecodeError中断:
python setup.py register
我通常通过运行“python”来发布软件设置.pysdist register upload“,这意味着丑陋的黑客系统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("..."),
...)
难道没有更好的方法吗?在
您需要将unicode长描述
u"bläh bläh bläh"
更改为普通字符串"bläh bläh bläh"
,并添加一个编码头作为文件的第二行:显然,你也需要用UTF-8来保存文件。在
我用上面的代码进行测试-长描述没有错误,只有rst2html;upload似乎可以正常工作(尽管我取消了实际上传),register要求我输入我没有的用户名。但是您的注释中的回溯是有帮助的-是在
register
命令中自动转换为unicode
导致问题的。在请参见the illusive setdefaultencoding了解更多信息-基本上您希望Python中的默认编码能够将编码后的字符串转换回unicode,但是设置起来很麻烦。在这种情况下,我认为值得努力:
^{pr2}$或者甚至是正确的,你可以从
locale
中得到它,/usr/lib/python2.6/site.py
中有注释掉的代码,你可以找到它,但我现在就不讨论这个问题了。在显然,这是一个distutils错误,已经在python2.6中修复了:http://mail.python.org/pipermail/distutils-sig/2009-September/013275.html
Tarek建议给post-Tu服务器打补丁。修补程序应预处理 “data”参数,并将它们转换为unicode,然后调用原始方法。见http://mail.python.org/pipermail/distutils-sig/2009-September/013277.html
相关问题 更多 >
编程相关推荐