The serialization layer was making hardcoded assumptions about how default tip
authorBarry Pederson <bp@barryp.org>
Thu, 10 Dec 2009 23:43:40 -0600
changeset 1739f9c3e41559c
parent 172 9c606fbd881d
The serialization layer was making hardcoded assumptions about how
short strings were encoded (utf-8). Add 'encoding' parameters
to AMQPWriter.write_shortstr() and AMQPReader.read_shortstr()
so the encoding can be controlled at a higher level. By default
though those two methods act as before. Thanks to 'angrybaldguy'
for bringing this up. (Copied from main branch)
CHANGES
amqplib/client_0_8/serialization.py
     1.1 --- a/CHANGES	Mon Oct 05 09:38:02 2009 -0500
     1.2 +++ b/CHANGES	Thu Dec 10 23:43:40 2009 -0600
     1.3 @@ -16,6 +16,10 @@
     1.4      may be specified.  If no message was processed then wait() throws a
     1.5      TimeoutException.
     1.6  
     1.7 +    Add an 'encoding' parameter to AMQPWriter.write_shortstr() and
     1.8 +    AMQPReader.read_shortstr() in serialization.py, the default
     1.9 +    value of 'utf-8' gives the same behavior as before.
    1.10 +
    1.11  
    1.12  Version 0.6.1
    1.13  
     2.1 --- a/amqplib/client_0_8/serialization.py	Mon Oct 05 09:38:02 2009 -0500
     2.2 +++ b/amqplib/client_0_8/serialization.py	Thu Dec 10 23:43:40 2009 -0600
     2.3 @@ -139,15 +139,19 @@
     2.4          return unpack('>Q', self.input.read(8))[0]
     2.5  
     2.6  
     2.7 -    def read_shortstr(self):
     2.8 +    def read_shortstr(self, encoding='utf-8'):
     2.9          """
    2.10 -        Read a utf-8 encoded string that's stored in up to
    2.11 -        255 bytes.  Return it decoded as a Python unicode object.
    2.12 +        Read a short string that's stored in up to 255 bytes. Return
    2.13 +        it as a decoded Python unicode object, under the encoding
    2.14 +        passed as 'encoding', or as a byte string if 'encoding' is None.
    2.15  
    2.16          """
    2.17          self.bitcount = self.bits = 0
    2.18          slen = unpack('B', self.input.read(1))[0]
    2.19 -        return self.input.read(slen).decode('utf-8')
    2.20 +        raw = self.input.read(slen)
    2.21 +        if encoding:
    2.22 +            return raw.decode(encoding)
    2.23 +        return raw
    2.24  
    2.25  
    2.26      def read_longstr(self):
    2.27 @@ -325,15 +329,17 @@
    2.28          self.out.write(pack('>Q', n))
    2.29  
    2.30  
    2.31 -    def write_shortstr(self, s):
    2.32 +    def write_shortstr(self, s, encoding='utf-8'):
    2.33          """
    2.34 -        Write a string up to 255 bytes long after encoding.  If passed
    2.35 -        a unicode string, encode as UTF-8.
    2.36 +        Write a string up to 255 bytes long (after any encoding).
    2.37 +
    2.38 +        If passed a unicode string, encode with the specified
    2.39 +        encoding (defaults to utf-8).
    2.40  
    2.41          """
    2.42          self._flushbits()
    2.43          if isinstance(s, unicode):
    2.44 -            s = s.encode('utf-8')
    2.45 +            s = s.encode(encoding)
    2.46          if len(s) > 255:
    2.47              raise ValueError('String too long')
    2.48          self.write_octet(len(s))