Ticket #1569: 1569.unicode.trunk.diff

File 1569.unicode.trunk.diff, 2.2 KB (added by Maniac <Maniac@…>, 19 years ago)

Unicode-aware iterator (trunk)

  • django/utils/httpwrappers.py

     
    151151        if not mimetype:
    152152            mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
    153153        if hasattr(content, '__iter__'):
    154             self.iterator = content
     154            self._iterator = content
    155155            self._is_string = False
    156156        else:
    157             self.iterator = [content]
     157            self._iterator = [content]
    158158            self._is_string = True
    159159        self.headers = {'Content-Type': mimetype}
    160160        self.cookies = SimpleCookie()
     
    200200            pass
    201201
    202202    def _get_content(self):
    203         content = ''.join(self.iterator)
     203        content = ''.join(self._iterator)
    204204        if isinstance(content, unicode):
    205205            content = content.encode(self._charset)
    206206        return content
    207207
    208208    def _set_content(self, value):
    209         self.iterator = [value]
     209        self._iterator = [value]
    210210        self._is_string = True
    211211
    212212    content = property(_get_content, _set_content)
    213213
     214    def _get_iterator(self):
     215        "Output iterator. Converts data into client charset if necessary."
     216        for chunk in self._iterator:
     217            if isinstance(chunk, unicode):
     218                chunk = chunk.encode(self._charset)
     219            yield chunk
     220
     221    iterator = property(_get_iterator)
     222
    214223    # The remaining methods partially implement the file-like object interface.
    215224    # See http://docs.python.org/lib/bltin-file-objects.html
    216225    def write(self, content):
    217226        if not self._is_string:
    218227            raise Exception, "This %s instance is not writable" % self.__class__
    219         self.iterator.append(content)
     228        self._iterator.append(content)
    220229
    221230    def flush(self):
    222231        pass
     
    224233    def tell(self):
    225234        if not self._is_string:
    226235            raise Exception, "This %s instance cannot tell its position" % self.__class__
    227         return sum([len(chunk) for chunk in self.iterator])
     236        return sum([len(chunk) for chunk in self._iterator])
    228237
    229238class HttpResponseRedirect(HttpResponse):
    230239    def __init__(self, redirect_to):
Back to Top