Tags

, , , ,


I dont like giving cursory descriptions about problems, but I got plagued with the following error message when using xmppp to send messages after database lookups:-


File "D:\Python25\Lib\site-packages\xmpp\protocol.py", line 418, in __init__
if body: self.setBody(body)
File "D:\Python25\Lib\site-packages\xmpp\protocol.py", line 431, in setBody
self.setTagData('body',val)
File "D:\Python25\Lib\site-packages\xmpp\simplexml.py", line 243, in setTagDat
a
except: self.addChild(tag,attrs,payload=[ustr(val)])
File "D:\Python25\Lib\site-packages\xmpp\simplexml.py", line 32, in ustr
if type(r)type(u''): return unicode(r,ENCODING)tBody
self.setTagData('body',val)
File "D:\Python25\Lib\site-packages\xmpp\simplexml.py", line 243, in setTagDat
a
except: self.addChild(tag,attrs,payload=[ustr(val)])
File "D:\Python25\Lib\site-packages\xmpp\simplexml.py", line 32, in ustr
if type(r)type(u''): return unicode(r,ENCODING)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 20-21: invalid data

I was trying to convert the data being received by the sql connector to a unicode string and it was giving invalid data exceptions. The tables contained Unicode fields and I was retreiving unicode however the data coming from the connector were normal python ASCII Strings. Some other errors I saw while playing around with my code included:-


UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
52: ordinal not in range(128)

The solution was simple. Rather than using


self.conn = MySQLdb.connect(host=self.host,
user = self.user,
db = self.db)

I had to use


self.conn = MySQLdb.connect(host=self.host,
user = self.user,
db = self.db,
use_unicode = True, charset="utf8"
)

And viola things worked seamlessly eversince and I didnt need to do any conversion.

I didnt find the time to investigate more on what was actually happening in terms of how the data was actually being stored. I hope this fix was good enough for most, but for the more technically inclined, maybe I’ll investigate further and update this article in the future.

Advertisements