New bytes/unicode separation.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Response.content for bytes. New Response.text for unicode.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 6d071f4 · Browse files · 1 file·+22 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+22 added−24 removed
- other:+22 added−24 removed
Provenance
- Commit
49a38ac- Tree
050d414- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
6d071f4
1 file changed+22 added−24 removed
modified:requests/models.py+22 added-24 removed
526 unmodified lines527 # If prefetch is True, mark content as consumed.528 if prefetch:529 # Save the response.530 self.response.content531- 531+532 if self.config.get('danger_mode'):533 self.response.raise_for_status()534 535 return self.sent61 unmodified lines597 return False598 return True599 600 601- def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):601+ def iter_content(self, chunk_size=10 * 1024, decode_unicode=False):602 """Iterates over the response data. This avoids reading the content603 at once into memory for large responses. The chunk size is the number604 of bytes it should read into memory. This is not necessarily the605 length of each item returned as decoding can take place.9 unmodified lines615 if not chunk:616 break617 yield chunk618 self._content_consumed = True619- 619+620 def generate_chunked():621 resp = self.raw._original_response622 fp = resp.fp623 if resp.chunk_left is not None:27 unmodified lines651 gen = stream_decompress(gen, mode='gzip')652 elif 'deflate' in self.headers.get('content-encoding', ''):653 gen = stream_decompress(gen, mode='deflate')654 655- if decode_unicode is None:656- decode_unicode = self.config.get('decode_unicode')657-655 if decode_unicode:656 gen = stream_decode_response_unicode(gen, self)657 658 return gen29 unmodified lines688 689 690 @property691 def content(self):695- """Content of the response, in bytes or unicode696- (if available).697- """692+ """Content of the response, in bytes."""693 694 if self._content is None:695 # Read the contents.696 try:4 unmodified lines701 self._content = self.raw.read()702 except AttributeError:703 self._content = None704 705+ self._content_consumed = True710- content = self._content706+ return self._content707 712- # Decode unicode content.713- if self.config.get('decode_unicode'):708 715- # Try charset from content-type709+ @property710+ def text(self):711+ """Content of the response, in unicode."""712 717- if self.encoding:718- try:719- content = unicode(content, self.encoding)720- except UnicodeError:721- pass713+ # Try charset from content-type714 723- # Fall back:724- try:725- content = unicode(content, self.encoding, errors='replace')726- except TypeError:727- pass715+ content = u''716 729- self._content_consumed = True717+ try:718+ content = unicode(self.content, self.encoding)719+ except UnicodeError:720+ pass721+722+ # Try to fall back:723+ try:724+ content = unicode(content, self.encoding, errors='replace')725+ except TypeError:726+ pass727+728 return content729 730 731 def raise_for_status(self):15 unmodified lines