From 3d7a4c890e25d0e145c6f6f5e1e675aacb67c3ee Mon Sep 17 00:00:00 2001 From: Daniel Grana Date: Wed, 21 Oct 2009 13:57:06 -0200 Subject: [PATCH] fix get_meta_refresh bug raised for TextResponses without encoding --- scrapy/tests/test_utils_response.py | 6 ++++++ scrapy/utils/response.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/scrapy/tests/test_utils_response.py b/scrapy/tests/test_utils_response.py index 8a58a68ca..9281f4f40 100644 --- a/scrapy/tests/test_utils_response.py +++ b/scrapy/tests/test_utils_response.py @@ -93,6 +93,12 @@ class ResponseUtilsTest(unittest.TestCase): response = Response(url='http://example.com', body=body) self.assertEqual(get_meta_refresh(response), (3, 'http://example.com/thisTHAT')) + # responses without refresh tag should return None None + response = Response(url='http://example.org') + self.assertEqual(get_meta_refresh(response), (None, None)) + response = TextResponse(url='http://example.org') + self.assertEqual(get_meta_refresh(response), (None, None)) + def test_response_httprepr(self): r1 = Response("http://www.example.com") self.assertEqual(response_httprepr(r1), 'HTTP/1.1 200 OK\r\n\r\n') diff --git a/scrapy/utils/response.py b/scrapy/utils/response.py index c3fa69a6e..a1a83d2ac 100644 --- a/scrapy/utils/response.py +++ b/scrapy/utils/response.py @@ -47,7 +47,7 @@ def get_meta_refresh(response): If no meta redirect is found, (None, None) is returned. """ if response not in _metaref_cache: - encoding = getattr(response, 'encoding', 'utf-8') + encoding = getattr(response, 'encoding', None) or 'utf-8' body_chunk = remove_entities(unicode(response.body[0:4096], encoding, \ errors='ignore')) match = META_REFRESH_RE.search(body_chunk)