Index: message.py =================================================================== --- message.py (Revision 48275) +++ message.py (Arbeitskopie) @@ -116,7 +116,10 @@ if _type == Message.REQUEST: type = 'REQUEST' if mimetype == MIMETYPE_JSON: - data = json.dumps( body ) + try: + data = json.dumps( body ) + except UnicodeDecodeError: + data = json.dumps(_fix_encoding(body)) else: data = body args = '' @@ -316,6 +319,21 @@ del body['options'] return Message._formattedMessage(self._id, self._type, self.mimetype, self.command, body, self.arguments) + +def _fix_encoding(data): + if isinstance(data, bytestring): + try: + data.decode('utf-8') + except UnicodeDecodeError: + data = data.decode('iso8859-1').encode('utf-8') + elif isinstance(data, (list, tuple)): + data = data.__class__(_fix_encoding(d) for d in data) + elif isinstance(data, dict): + data = data.__class__((_fix_encoding(k), _fix_encoding(v)) for k, v in data.iteritems()) + + return data + + if __name__ == '__main__': # encode auth = Request( 'AUTH' )