Ticket #6527: 6527 V2.patch

File 6527 V2.patch, 5.8 KB (added by John DeRosa, 17 years ago)

Updated patch

  • AUTHORS

     
    373373    ymasuda@ethercube.com
    374374    Jarek Zgoda <jarek.zgoda@gmail.com>
    375375    Cheng Zhang
     376    John DeRosa <stugots@qwest.net>
    376377
     378
    377379A big THANK YOU goes to:
    378380
    379381    Rob Curley and Ralph Gage for letting us open-source Django.
  • django/http/__init__.py

     
    257257    status_code = 200
    258258
    259259    def __init__(self, content='', mimetype=None, status=None,
    260             content_type=None):
     260                 content_type=None):
    261261        from django.conf import settings
     262
    262263        self._charset = settings.DEFAULT_CHARSET
     264       
    263265        if mimetype:
    264266            content_type = mimetype     # For backwards compatibility
    265267        if not content_type:
    266268            content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
    267269                    settings.DEFAULT_CHARSET)
    268         if not isinstance(content, basestring) and hasattr(content, '__iter__'):
    269             self._container = content
    270             self._is_string = False
    271         else:
    272             self._container = [content]
    273             self._is_string = True
     270
     271        # Note - We expect content to be an iterable container, or a string.
     272        self._container = [''.join(content)]
     273        if hasattr(content, 'close'):
     274            content.close()
     275       
    274276        self.cookies = SimpleCookie()
     277
    275278        if status:
    276279            self.status_code = status
    277280
     
    283286    def __str__(self):
    284287        """Full HTTP message, including headers."""
    285288        return '\n'.join(['%s: %s' % (key, value)
    286             for key, value in self._headers.values()]) \
    287             + '\n\n' + self.content
     289                         for key, value in self._headers.values()]) \
     290               + '\n\n' + self.content
    288291
    289292    def _convert_to_ascii(self, *values):
    290293        """Converts all values to ascii strings."""
     
    347350        return smart_str(''.join(self._container), self._charset)
    348351
    349352    def _set_content(self, value):
    350         self._container = [value]
    351         self._is_string = True
     353        self._container = [''.join(value)]
    352354
    353355    content = property(_get_content, _set_content)
    354356
     
    363365        return str(chunk)
    364366
    365367    def close(self):
    366         if hasattr(self._container, 'close'):
    367             self._container.close()
     368        """This became a noop in the patch for ticket
     369        http://code.djangoproject.com/ticket/6527.  It's here for backwards-
     370        compatibility.
     371        """
     372        pass
    368373
    369374    # The remaining methods partially implement the file-like object interface.
    370375    # See http://docs.python.org/lib/bltin-file-objects.html
    371376    def write(self, content):
    372         if not self._is_string:
    373             raise Exception("This %s instance is not writable" % self.__class__)
    374377        self._container.append(content)
    375378
    376379    def flush(self):
    377380        pass
    378381
    379382    def tell(self):
    380         if not self._is_string:
    381             raise Exception("This %s instance cannot tell its position" % self.__class__)
    382383        return sum([len(chunk) for chunk in self._container])
    383384
    384385class HttpResponseRedirect(HttpResponse):
  • docs/request_response.txt

     
    369369~~~~~~~~~~~~~~~~~
    370370
    371371Finally, you can pass ``HttpResponse`` an iterator rather than passing it
    372 hard-coded strings. If you use this technique, follow these guidelines:
     372hard-coded strings. If you use this technique, note the following:
    373373
    374374    * The iterator should return strings.
    375     * If an ``HttpResponse`` has been initialized with an iterator as its
    376       content, you can't use the ``HttpResponse`` instance as a file-like
    377       object. Doing so will raise ``Exception``.
     375    * ``HttpResponse.__init__()`` will read and store the iterator's contents.
    378376
    379377Methods
    380378-------
  • tests/regressiontests/httpwrappers/helloworld.txt

     
     1Hello world.
  • tests/regressiontests/httpwrappers/tests.py

    Property changes on: tests\regressiontests\httpwrappers\helloworld.txt
    ___________________________________________________________________
    Name: svn:eol-style
       + native
    
     
    427427...
    428428UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format
    429429 
     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.
     440<BLANKLINE>
     441
     442>>> print response
     443Content-Type: text/html; charset=utf-8
     444<BLANKLINE>
     445Hello world.
     446<BLANKLINE>
     447
     448>>> print response
     449Content-Type: text/html; charset=utf-8
     450<BLANKLINE>
     451Hello world.
     452<BLANKLINE>
     453
     454>>> response = HttpResponse("abc")
     455>>> print response
     456Content-Type: text/html; charset=utf-8
     457<BLANKLINE>
     458abc
     459>>> print response
     460Content-Type: text/html; charset=utf-8
     461<BLANKLINE>
     462abc
     463>>> print response
     464Content-Type: text/html; charset=utf-8
     465<BLANKLINE>
     466abc
     467
    430468"""
    431469
    432470from django.http import QueryDict, HttpResponse
Back to Top