Ticket #6527: 6527-2.diff

File 6527-2.diff, 3.0 KB (added by daonb <bennydaon@…>, 11 years ago)

Less code here, includes original regression tests

  • django/http/__init__.py

     
    257257            content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
    258258                    settings.DEFAULT_CHARSET)
    259259        if not isinstance(content, basestring) and hasattr(content, '__iter__'):
    260             self._container = content
    261             self._is_string = False
     260            self._container = [''.join(content) ]
    262261        else:
    263262            self._container = [content]
    264             self._is_string = True
    265263        self.cookies = SimpleCookie()
    266264        if status:
    267265            self.status_code = status
     
    332330
    333331    def _set_content(self, value):
    334332        self._container = [value]
    335         self._is_string = True
    336333
    337334    content = property(_get_content, _set_content)
    338335
     
    353350    # The remaining methods partially implement the file-like object interface.
    354351    # See http://docs.python.org/lib/bltin-file-objects.html
    355352    def write(self, content):
    356         if not self._is_string:
    357             raise Exception, "This %s instance is not writable" % self.__class__
    358353        self._container.append(content)
    359354
    360355    def flush(self):
    361356        pass
    362357
    363358    def tell(self):
    364         if not self._is_string:
    365             raise Exception, "This %s instance cannot tell its position" % self.__class__
    366359        return sum([len(chunk) for chunk in self._container])
    367360
    368361class HttpResponseRedirect(HttpResponse):
  • tests/regressiontests/httpwrappers/tests.py

     
    426426Traceback (most recent call last):
    427427...
    428428UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
     429
     430######################################
     431# HttpResponse with iterable content #
     432######################################
     433
     434>>> from django.http import HttpResponse
     435>>> response = HttpResponse(file('regressiontests/httpwrappers/helloworld.txt','r'))
     436>>> print response
     437Content-Type: text/html; charset=utf-8
     438<BLANKLINE>
     439Hello world.
     440Hello, world!
     441
     442>>> print response
     443Content-Type: text/html; charset=utf-8
     444<BLANKLINE>
     445Hello world.
     446Hello, world!
     447
     448>>> print response
     449Content-Type: text/html; charset=utf-8
     450<BLANKLINE>
     451Hello world.
     452Hello, world!
     453>>> response = HttpResponse("abc")
     454>>> print response
     455Content-Type: text/html; charset=utf-8
     456<BLANKLINE>
     457abc
     458>>> print response
     459Content-Type: text/html; charset=utf-8
     460<BLANKLINE>
     461abc
     462>>> print response
     463Content-Type: text/html; charset=utf-8
     464<BLANKLINE>
     465abc
     466
    429467 
    430468"""
    431469
  • tests/regressiontests/httpwrappers/helloworld.txt

     
     1Hello world.
     2Hello, world!
     3 No newline at end of file
Back to Top