Django

Code

Changeset 2675

Show
Ignore:
Timestamp:
04/11/06 08:43:29 (3 years ago)
Author:
adrian
Message:

Fixed #1569 -- Made streaming HttpResponse unicode-aware

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/httpwrappers.py

    r2642 r2675  
    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} 
     
    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) 
     
    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) 
     213 
     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) 
    213222 
    214223    # The remaining methods partially implement the file-like object interface. 
     
    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): 
     
    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):