stavo scrivendo un setup.py per un pacchetto Python utilizzando setuptools e volevano includere un carattere non ASCII nel campo long_description:Qual è il modo giusto per utilizzare i metadati Unicode in setup.py?
#!/usr/bin/env python
from setuptools import setup
setup(...
long_description=u"...", # in real code this value is read from a text file
...)
Purtroppo, passando un oggetto unicode di setup() rompe una delle dopo due comandi con un UnicodeEncodeError
python setup.py --long-description | rst2html python setup.py upload
Se uso una stringa UTF-8 prima per il campo long_description, quindi le seguenti pause di comando con un UnicodeDecodeError:
python setup.py register
Generalmente rilascia software eseguendo "python setup.py sdist register upload", il che significa che i brutti hack che guardano a sys.argv e passano il giusto tipo di oggetto sono fuori proprio.
Alla fine ho rinunciato e implementato un diverso brutto hack:
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("..."),
...)
non c'è un modo migliore?
Non sono sicuro di poter incollare il traceback completo in un commento qui; il traceback termina in /usr/lib/python2.6/distutils/command/register.py riga 264 (nel post_to_server) dove tenta di farlo: value = unicode (value) .encode ("utf-8"). Come puoi vedere, sto usando Python 2.6; una versione successiva di distutils dovrebbe essere davvero roba del genere. –
Si noterà che la riproduzione richiede effettivamente di avere almeno un carattere non ASCII nel campo. –
Posso riprodurre l'installazione di Python.errore py register 'con tutte e tre le versioni di Python che ho qui: 2.4, 2.5 e 2.6. –