Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate body as unicode #4555

Merged
merged 10 commits into from May 11, 2020
5 changes: 0 additions & 5 deletions docs/topics/request-response.rst
Expand Up @@ -834,11 +834,6 @@ TextResponse objects

.. automethod:: TextResponse.follow_all

Gallaecio marked this conversation as resolved.
Show resolved Hide resolved
.. method:: TextResponse.body_as_unicode()

The same as :attr:`text`, but available as a method. This method is
kept for backward compatibility; please prefer ``response.text``.


HtmlResponse objects
--------------------
Expand Down
5 changes: 5 additions & 0 deletions scrapy/http/response/text.py
Expand Up @@ -5,6 +5,7 @@
See documentation in docs/topics/request-response.rst
"""

import warnings
from contextlib import suppress
from typing import Generator
from urllib.parse import urljoin
Expand All @@ -14,6 +15,7 @@
http_content_type_encoding, resolve_encoding)
from w3lib.html import strip_html5_whitespace

from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import Request
from scrapy.http.response import Response
from scrapy.utils.python import memoizemethod_noargs, to_unicode
Expand Down Expand Up @@ -61,6 +63,9 @@ def _declared_encoding(self):

def body_as_unicode(self):
"""Return body as unicode"""
warnings.warn('Response.body_as_unicode() is deprecated, '
'please use Response.text instead.',
ScrapyDeprecationWarning)
return self.text

@property
Expand Down
9 changes: 9 additions & 0 deletions tests/test_http_response.py
@@ -1,7 +1,9 @@
import unittest
from warnings import catch_warnings

from w3lib.encoding import resolve_encoding

from scrapy.exceptions import ScrapyDeprecationWarning
from scrapy.http import (Request, Response, TextResponse, HtmlResponse,
XmlResponse, Headers)
from scrapy.selector import Selector
Expand Down Expand Up @@ -660,6 +662,13 @@ def test_follow_all_too_many_arguments(self):
with self.assertRaises(ValueError):
response.follow_all(css='a[href*="example.com"]', xpath='//a[contains(@href, "example.com")]')

def test_body_as_unicode_deprecation_warning(self):
with catch_warnings(record=True) as warnings:
r1 = self.response_class("http://www.example.com", body=u'Hello', encoding='utf-8')
self.assertEqual(r1.body_as_unicode(), u'Hello')
self.assertEqual(len(warnings), 1)
self.assertEqual(warnings[0].category, ScrapyDeprecationWarning)


class HtmlResponseTest(TextResponseTest):

Expand Down