Skip to content
Myr

Refactor response.content into a property. This makes content attribute discoverable through dir(response) and also guards from errors like forgetting to raise AttributeError in __getattr__. Also you don't have to create fake self.content to fool Sphinx any more.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Den Shabalin · committed by Den Shabalin · parent d4bdd84 · Browse files · 1 file+22 added−26 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−26 removed

  • other:+22 added−26 removed

1 file+22 added−26 removed

modified:requests/models.py+22 added-26 removed
394 unmodified lines
395 def __init__(self):396 #: Content of the response, in bytes or unicode (if available).397 self.content = None398399 # Hack for Sphinx.400 del self.content401399 #: Integer Code of responded HTTP Status.400 self.status_code = None401402 #: Case-insensitive Dictionary of Response Headers.
23 unmodified lines
426427 #: A dictionary of Cookies the server sent back.428 self.cookies = None429433 self._content = None430435431 def __repr__(self):432 return '<Response [%s]>' % (self.status_code)433434
2 unmodified lines
437438 return not self.error439440446 def __getattr__(self, name):447 """Read and returns the full stream when accessing to448 :attr: `content`449 """441 @property442 def content(self):443 """Content of the response, in bytes or unicode444 (if available)."""445 446 if self._content is not None:447 return self._content448451 if name == 'content':452 if self._content is not None:453 return self._content449 # Read the contents.450 self._content = self.fo.read()451455 # Read the contents.456 self._content = self.fo.read()452 # Decode GZip'd content.453 if 'gzip' in self.headers.get('content-encoding', ''):454 try:455 self._content = decode_gzip(self._content)456 except zlib.error:457 pass458458 # Decode GZip'd content.459 if 'gzip' in self.headers.get('content-encoding', ''):460 try:461 self._content = decode_gzip(self._content)462 except zlib.error:463 pass459 # Decode unicode content.460 if settings.decode_unicode:461 self._content = get_unicode_from_response(self)462465 # Decode unicode content.466 if settings.decode_unicode:467 self._content = get_unicode_from_response(self)463 return self._content464469 return self._content465471 else:472 raise AttributeError466 @content.setter467 def content(self, value):468 self._content = value469470471 def raise_for_status(self):472 """Raises stored :class:`HTTPError` or :class:`URLError`, if one occured."""
179 unmodified lines