Ticket #21282: 21282-2.diff

File 21282-2.diff, 1.9 KB (added by Claude Paroz, 11 years ago)

py3 compatible version

  • django/http/response.py

    diff --git a/django/http/response.py b/django/http/response.py
    index 5cdedec..0049125 100644
    a b class HttpResponseBase(six.Iterator):  
    123123
    124124    def serialize_headers(self):
    125125        """HTTP headers as a bytestring."""
     126        def force_bytes(val, encoding):
     127            return val if isinstance(val, bytes) else val.encode(encoding)
     128
    126129        headers = [
    127             ('%s: %s' % (key, value)).encode('us-ascii')
     130            (b': '.join([force_bytes(key, 'ascii'), force_bytes(value, 'latin-1')]))
    128131            for key, value in self._headers.values()
    129132        ]
    130133        return b'\r\n'.join(headers)
  • tests/httpwrappers/tests.py

    diff --git a/tests/httpwrappers/tests.py b/tests/httpwrappers/tests.py
    index 287d800..1e099d2 100644
    a b class HttpResponseTests(unittest.TestCase):  
    254254        r['key'] = 'test'.encode('ascii')
    255255        self.assertEqual(r['key'], str('test'))
    256256        self.assertIsInstance(r['key'], str)
     257        self.assertIn(b'test', r.serialize_headers())
    257258
    258259        # Latin-1 unicode or bytes values are also converted to native strings.
    259260        r['key'] = 'café'
    class HttpResponseTests(unittest.TestCase):  
    262263        r['key'] = 'café'.encode('latin-1')
    263264        self.assertEqual(r['key'], smart_str('café', 'latin-1'))
    264265        self.assertIsInstance(r['key'], str)
     266        self.assertIn('café'.encode('latin-1'), r.serialize_headers())
    265267
    266268        # Other unicode values are MIME-encoded (there's no way to pass them as bytes).
    267269        r['key'] = '†'
    268270        self.assertEqual(r['key'], str('=?utf-8?b?4oCg?='))
    269271        self.assertIsInstance(r['key'], str)
     272        self.assertIn(b'=?utf-8?b?4oCg?=', r.serialize_headers())
    270273
    271274        # The response also converts unicode or bytes keys to strings, but requires
    272275        # them to contain ASCII
Back to Top