From ad2b979e0f21561c48dc7ac593faea003128f8b8 Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Thu, 9 Sep 2010 18:01:00 -0300 Subject: [PATCH] Changed TextResponse.replace() behaviour by keeping previous encoding when not specified --- scrapy/http/response/text.py | 2 +- scrapy/tests/test_http_response.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/scrapy/http/response/text.py b/scrapy/http/response/text.py index b54c3e279..aa1c2cb51 100644 --- a/scrapy/http/response/text.py +++ b/scrapy/http/response/text.py @@ -53,7 +53,7 @@ class TextResponse(Response): super(TextResponse, self)._set_body(body) def replace(self, *args, **kwargs): - kwargs.setdefault('encoding', getattr(self, '_encoding', None)) + kwargs.setdefault('encoding', self.encoding) return Response.replace(self, *args, **kwargs) @property diff --git a/scrapy/tests/test_http_response.py b/scrapy/tests/test_http_response.py index eb48d0a86..3c95c548f 100644 --- a/scrapy/tests/test_http_response.py +++ b/scrapy/tests/test_http_response.py @@ -287,13 +287,16 @@ class XmlResponseTest(TextResponseTest): r4 = r3.replace(body=body2) self._assert_response_values(r4, 'utf-8', body2) - # make sure replace() rediscovers the encoding (if not given explicitly) when changing the body + def test_replace_encoding(self): + # make sure replace() keeps the previous encoding unless overridden explicitly body = """""" - r5 = self.response_class("http://www.example.com", body=body) body2 = """""" + r5 = self.response_class("http://www.example.com", body=body) r6 = r5.replace(body=body2) + r7 = r5.replace(body=body2, encoding='utf-8') self._assert_response_values(r5, 'iso-8859-1', body) - self._assert_response_values(r6, 'utf-8', body2) + self._assert_response_values(r6, 'iso-8859-1', body2) + self._assert_response_values(r7, 'utf-8', body2) if __name__ == "__main__": -- GitLab