[Twisted-Python] logging and Unicode strings

Manlio Perillo manlio_perillo at libero.it
Sat Jul 14 12:14:11 EDT 2007

It seems that it is not possibile to pass Unicode strings to the logging 

2007/07/14 18:10 CEST [HTTPChannel,0,] <unicode instance at 
-1254556832 with str error Traceback (most recent call last):

"/usr/lib/python2.4/site-packages/twisted/python/reflect.py", line 463, 
in safe_str
             return str(o)

         UnicodeEncodeError: 'ascii' codec can't encode character 
u'\xf2' in position 31: ordinal not in range(128)

Since I do not want to encode every string, I have changed safe_str:
def safe_str(o):
         return str(o)
         if isinstance(o, unicode):
             import sys
             encoding = sys.getdefaultencoding()
             return o.encode(encoding, 'backslashreplace')

         strExc = '\n'.join(traceback.format_exception(*sys.exc_info()))
         clsName = _determineClassName(o)
         obId = id(o)
         return '<%s instance at %s with str error %s>' % (
             clsName, obId, strExc)

Is this a feature or a bug?

Thanks and regards  Manlio Perillo

More information about the Twisted-Python mailing list