From 19867659f3eb8a3e018e9b38fd9520f9fa4392cd Mon Sep 17 00:00:00 2001 From: Jalil SA <61639983+jxlil@users.noreply.github.com> Date: Thu, 17 Aug 2023 10:22:17 -0600 Subject: [PATCH 1/3] fix: response.json() call makes unnecessary memory allocation --- scrapy/http/response/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrapy/http/response/text.py b/scrapy/http/response/text.py index 7fc54b5d3..47d7bc10f 100644 --- a/scrapy/http/response/text.py +++ b/scrapy/http/response/text.py @@ -82,7 +82,7 @@ class TextResponse(Response): Deserialize a JSON document to a Python object. """ if self._cached_decoded_json is _NONE: - self._cached_decoded_json = json.loads(self.text) + self._cached_decoded_json = json.loads(self.body) return self._cached_decoded_json @property From fd4292b722edfa5aaf08f6e64035271222771fa2 Mon Sep 17 00:00:00 2001 From: Jalil SA <61639983+jxlil@users.noreply.github.com> Date: Thu, 17 Aug 2023 10:24:34 -0600 Subject: [PATCH 2/3] fix: test_cache_json_response --- tests/test_http_response.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_http_response.py b/tests/test_http_response.py index 54f0461e8..80d46274b 100644 --- a/tests/test_http_response.py +++ b/tests/test_http_response.py @@ -844,7 +844,7 @@ class TextResponseTest(BaseResponseTest): with mock.patch("json.loads") as mock_json: for _ in range(2): json_response.json() - mock_json.assert_called_once_with(json_body.decode()) + mock_json.assert_called_once_with(json_body) class HtmlResponseTest(TextResponseTest): From 4dd32672ede77e2dbf58749800c48b3404b04b8c Mon Sep 17 00:00:00 2001 From: Jalil SA <61639983+jxlil@users.noreply.github.com> Date: Mon, 21 Aug 2023 08:28:08 -0600 Subject: [PATCH 3/3] added entry to Backward-incompatible changes --- docs/news.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/news.rst b/docs/news.rst index c55c0b222..0154e64ff 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -3,6 +3,23 @@ Release notes ============= +.. _release-2.11.0: + +Scrapy 2.11.0 (to be released) +------------------------------ + +Backward-incompatible changes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- The :meth:`TextResponse.json ` method now + requires the response to be in a valid JSON encoding (UTF-8, UTF-16, or + UTF-32). + + If you need to deal with JSON documents in an invalid encoding, use + ``json.loads(response.text)`` instead. + + (:issue:`5968`) + .. _release-2.10.0: Scrapy 2.10.0 (2023-08-04)