Ticket #1569: 1569.unicode.m-r.diff

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

Unicode-aware iterator (magic-removal)

  • django/http/__init__.py

     
    154154        if not mimetype:
    155155            mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
    156156        if hasattr(content, '__iter__'):
    157             self.iterator = content
     157            self._iterator = content
    158158            self._is_string = False
    159159        else:
    160             self.iterator = [content]
     160            self._iterator = [content]
    161161            self._is_string = True
    162162        self.headers = {'Content-Type': mimetype}
    163163        self.cookies = SimpleCookie()
     
    203203            pass
    204204
    205205    def _get_content(self):
    206         content = ''.join(self.iterator)
     206        content = ''.join(self._iterator)
    207207        if isinstance(content, unicode):
    208208            content = content.encode(self._charset)
    209209        return content
    210210
    211211    def _set_content(self, value):
    212         self.iterator = [value]
     212        self._iterator = [value]
    213213        self._is_string = True
    214214
    215215    content = property(_get_content, _set_content)
    216216
     217    def _get_iterator(self):
     218        "Output iterator. Converts data into client charset if necessary."
     219        for chunk in self._iterator:
     220            if isinstance(chunk, unicode):
     221                chunk = chunk.encode(self._charset)
     222            yield chunk
     223
     224    iterator = property(_get_iterator)
     225
    217226    # The remaining methods partially implement the file-like object interface.
    218227    # See http://docs.python.org/lib/bltin-file-objects.html
    219228    def write(self, content):
    220229        if not self._is_string:
    221230            raise Exception, "This %s instance is not writable" % self.__class__
    222         self.iterator.append(content)
     231        self._iterator.append(content)
    223232
    224233    def flush(self):
    225234        pass
     
    227236    def tell(self):
    228237        if not self._is_string:
    229238            raise Exception, "This %s instance cannot tell its position" % self.__class__
    230         return sum([len(chunk) for chunk in self.iterator])
     239        return sum([len(chunk) for chunk in self._iterator])
    231240
    232241class HttpResponseRedirect(HttpResponse):
    233242    def __init__(self, redirect_to):
Back to Top