Skip to content

Commit

Permalink
Cache the value of the Response.text property
Browse files Browse the repository at this point in the history
  • Loading branch information
slingamn committed May 2, 2012
1 parent 35d5ac6 commit d232948
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,11 @@ class Response(object):

def __init__(self):

# bytes of the page:
self._content = False
self._content_consumed = False
# unicode of the page:
self._text = None

#: Integer Code of responded HTTP Status.
self.status_code = None
Expand Down Expand Up @@ -793,7 +796,6 @@ def _detected_encoding(self):
except Exception:
return 'utf-8'


@property
def text(self):
"""Content of the response, in unicode.
Expand All @@ -802,6 +804,9 @@ def text(self):
will be guessed.
"""

if self._text is not None:
return self._text

# Try charset from content-type
content = None
encoding = self.encoding
Expand All @@ -822,6 +827,7 @@ def text(self):
except (UnicodeError, TypeError):
pass

self._text = content
return content

def raise_for_status(self, allow_redirects=True):
Expand Down

0 comments on commit d232948

Please sign in to comment.