From b4f38655149e94c00f5407b7db5b51729c53b3ec Mon Sep 17 00:00:00 2001 From: Flaper Fesp Date: Fri, 2 Aug 2013 19:14:52 +0200 Subject: [PATCH] Added some docstrings and tests --- AUTHORS | 1 + falcon/response.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 371fa09..57ac3ef 100644 --- a/AUTHORS +++ b/AUTHORS @@ -7,6 +7,7 @@ by date of contribution: * Chad Lung (chadlung) * Josh Brand (joshbrand) * Jamie Painter (painterjd) +* Flavio Percoco (flaper87) * Randall Burt (rs-randallburt) * Zhihao Yuan (lichray) * Ashutosh Das (pyprism) diff --git a/falcon/response.py b/falcon/response.py index 942c611..a9ed6c2 100644 --- a/falcon/response.py +++ b/falcon/response.py @@ -65,9 +65,11 @@ class Response(object): self.stream_len = None def _get_body(self): + """Returns the body as-is.""" return self._body def _set_body(self, value): + """Sets the body and clears the encoded cache.""" self._body = value self._body_encoded = None @@ -78,12 +80,18 @@ class Response(object): @property def body_encoded(self): + """Encode the body and return it + + This property will encode `_body` and + cache the result in the `_body_encoded` + attribute. + """ # NOTE(flaper87): Notice this property # is not thread-safe. If body is modified # before this property returns, we might # end up returning None. body = self._body - if body and not self._body_encoded: + if body and self._body_encoded is None: # NOTE(flaper87): Assume it is an # encoded str, then check and encode @@ -91,6 +99,7 @@ class Response(object): self._body_encoded = body if isinstance(body, six.text_type): self._body_encoded = body.encode('utf-8') + return self._body_encoded def set_header(self, name, value):