ask / carrot

AMQP Messaging Framework for Python (discontinued; Use Kombu instead)
http://ask.github.com/carrot/
BSD 3-Clause "New" or "Revised" License
197 stars 34 forks source link

content_encoding and pyamqplib's auto_decode #14

Closed p2k closed 15 years ago

p2k commented 15 years ago

Line 55 and following of amqplib/client_0_8/channel.py state that there is a "auto_decode" parameter for AMQP channels, which defaults to True. This will automatically decode incoming messages which have the "content_encoding" property set. Because of this fact, the "decode" method of carrot's built-in deserializer (line 141 of carrot/serialization.py) will receive an already decoded unicode object and tries to decode it again (line 160), which will work just fine if there are only ASCII characters but will crash if there are non-ASCII characters in the data (happened to me).

My suggestions: Either instantiate the Channel object with "auto_decode=False" in carrot/backends/pyamqplib.py line 89 like this: self.channel = Channel(self.connection.connection, auto_decode=False)

Or check if "data" already is an unicode object in carrot/serialization.py line 158-160 like that:

Don't decode 8-bit strings or unicode objects

if content_encoding not in ('binary', 'ascii-8bit') and not isinstance(data, unicode):
    data = codecs.decode(data, content_encoding)

I would prefer the latter method and leave the decoding to pyamqplib.

ask commented 15 years ago

Nice catch. Would you like to submit a patch for it?

ask commented 15 years ago

Fixed by 90c89aeec9de7ee02c77695ce337e0b75ccc370a