Skip to content
Myr

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

1 file+22 added−24 removed

modified:requests/models.py+22 added-24 removed
526 unmodified lines
527 # If prefetch is True, mark content as consumed.528 if prefetch:529 # Save the response.530 self.response.content531 531532 if self.config.get('danger_mode'):533 self.response.raise_for_status()534535 return self.sent
61 unmodified lines
597 return False598 return True599600601 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 lines
615 if not chunk:616 break617 yield chunk618 self._content_consumed = True619 619620 def generate_chunked():621 resp = self.raw._original_response622 fp = resp.fp623 if resp.chunk_left is not None:
27 unmodified lines
651 gen = stream_decompress(gen, mode='gzip')652 elif 'deflate' in self.headers.get('content-encoding', ''):653 gen = stream_decompress(gen, mode='deflate')654655 if decode_unicode is None:656 decode_unicode = self.config.get('decode_unicode')657655 if decode_unicode:656 gen = stream_decode_response_unicode(gen, self)657658 return gen
29 unmodified lines
688689690 @property691 def content(self):695 """Content of the response, in bytes or unicode696 (if available).697 """692 """Content of the response, in bytes."""693694 if self._content is None:695 # Read the contents.696 try:
4 unmodified lines
701 self._content = self.raw.read()702 except AttributeError:703 self._content = None704705 self._content_consumed = True710 content = self._content706 return self._content707712 # Decode unicode content.713 if self.config.get('decode_unicode'):708715 # Try charset from content-type709 @property710 def text(self):711 """Content of the response, in unicode."""712717 if self.encoding:718 try:719 content = unicode(content, self.encoding)720 except UnicodeError:721 pass713 # Try charset from content-type714723 # Fall back:724 try:725 content = unicode(content, self.encoding, errors='replace')726 except TypeError:727 pass715 content = u''716729 self._content_consumed = True717 try:718 content = unicode(self.content, self.encoding)719 except UnicodeError:720 pass721722 # Try to fall back:723 try:724 content = unicode(content, self.encoding, errors='replace')725 except TypeError:726 pass727728 return content729730731 def raise_for_status(self):
15 unmodified lines