jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/801419 )
Change subject: [IMPR] use Response.json() instead of json.loads(Response.text)
......................................................................
[IMPR] use Response.json() instead of json.loads(Response.text)
- use Response.json() instead of json.loads(Response.text)
- ValueError is raised with Python 2; use json.JSONDecodeError
or for requests >= 2.27 requests.JSONDecodeError instead
- if Response.text is empty, a JSONDecodeError is raised with json() method
- update tests
Change-Id: I833026f3a8e1ad435a8130263c87edecd91fedba
---
M pywikibot/data/sparql.py
M pywikibot/proofreadpage.py
M pywikibot/site_detect.py
M tests/http_tests.py
M tests/sparql_tests.py
5 files changed, 31 insertions(+), 26 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index d08d323..c6e42f6 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -4,7 +4,7 @@
#
# Distributed under the terms of the MIT license.
#
-import json
+from contextlib import suppress
from typing import Optional
from urllib.parse import quote
@@ -16,6 +16,11 @@
from pywikibot.exceptions import Error, TimeoutError
+try:
+ from requests import JSONDecodeError
+except ImportError: # requests < 2.27.0
+ from json import JSONDecodeError
+
DEFAULT_HEADERS = {'cache-control': 'no-cache',
'Accept': 'application/sparql-results+json'}
@@ -148,13 +153,9 @@
self.wait()
continue
- if not self.last_response.text:
- break
-
- try:
- return json.loads(self.last_response.text)
- except ValueError:
- break
+ with suppress(JSONDecodeError):
+ return self.last_response.json()
+ break
return None
diff --git a/pywikibot/proofreadpage.py b/pywikibot/proofreadpage.py
index 566d4aa..3d6210b 100644
--- a/pywikibot/proofreadpage.py
+++ b/pywikibot/proofreadpage.py
@@ -648,7 +648,7 @@
if HTTPStatus.BAD_REQUEST <= response.status_code < 600:
return True, 'Http response status {}'.format(response.status_code)
- data = json.loads(response.text)
+ data = response.json()
if ocr_tool == self._PHETOOLS: # phetools
assert 'error' in data, 'Error from phetools:
{}'.format(data)
diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py
index 8bd4a78..0c20305 100644
--- a/pywikibot/site_detect.py
+++ b/pywikibot/site_detect.py
@@ -20,6 +20,12 @@
from pywikibot.tools import MediaWikiVersion
+try:
+ from requests import JSONDecodeError
+except ImportError: # requests < 2.27.0
+ from json import JSONDecodeError
+
+
SERVER_DB_ERROR_MSG = \
'<h1>Sorry! This site is experiencing technical
difficulties.</h1>'
@@ -101,7 +107,7 @@
self.api
+ '?action=query&meta=siteinfo&siprop=interwikimap'
'&sifilteriw=local&format=json')
- iw = json.loads(response.text)
+ iw = response.json()
if 'error' in iw:
raise RuntimeError('{} -
{}'.format(iw['error']['code'],
iw['error']['info']))
@@ -112,12 +118,12 @@
"""Extract the version from API help with ?version
enabled."""
if self.version is None:
try:
- d = fetch(self.api + '?version&format=json').text
+ r = fetch(self.api + '?version&format=json')
try:
- d = json.loads(d)
- except ValueError:
+ d = r.json()
+ except JSONDecodeError:
# Fallback for old versions which didn't wrap help in json
- d = {'error': {'*': d}}
+ d = {'error': {'*': r.text}}
self.version = list(filter(
lambda x: x.startswith('MediaWiki'),
diff --git a/tests/http_tests.py b/tests/http_tests.py
index fddad67..95c67d6 100755
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -5,7 +5,6 @@
#
# Distributed under the terms of the MIT license.
#
-import json
import re
import warnings
from contextlib import suppress
@@ -541,9 +540,7 @@
.format(status=fail_status, url=self.url))
self.assertEqual(r.status_code, HTTPStatus.OK)
-
- content = json.loads(r.text)
- self.assertEqual(content['args'], {})
+ self.assertEqual(r.json()['args'], {})
def test_unencoded_params(self):
"""
@@ -560,9 +557,7 @@
.format(status=fail_status, url=self.url))
self.assertEqual(r.status_code, HTTPStatus.OK)
-
- content = json.loads(r.text)
- self.assertEqual(content['args'], {'fish&chips':
'delicious'})
+ self.assertEqual(r.json()['args'], {'fish&chips':
'delicious'})
def test_encoded_params(self):
"""
@@ -579,9 +574,7 @@
.format(status=fail_status, url=self.url))
self.assertEqual(r.status_code, HTTPStatus.OK)
-
- content = json.loads(r.text)
- self.assertEqual(content['args'], {'fish%26chips':
'delicious'})
+ self.assertEqual(r.json()['args'], {'fish%26chips':
'delicious'})
class DataBodyParameterTestCase(HttpbinTestCase):
@@ -602,8 +595,8 @@
method='POST',
data={'fish&chips':
'delicious'})
- r_data = json.loads(r_data_request.text)
- r_body = json.loads(r_body_request.text)
+ r_data = r_data_request.json()
+ r_body = r_body_request.json()
# remove tracker ids if present (T243662, T255862)
for tracker_id in tracker:
diff --git a/tests/sparql_tests.py b/tests/sparql_tests.py
index d909805..bb63043 100755
--- a/tests/sparql_tests.py
+++ b/tests/sparql_tests.py
@@ -5,6 +5,7 @@
#
# Distributed under the terms of the MIT license.
#
+import json
import unittest
from contextlib import suppress
from unittest.mock import patch
@@ -90,6 +91,10 @@
"""Create container."""
self.text = value
+ def json(self):
+ """Simulate Response.json().""" # noqa: D402
+ return json.loads(self.text)
+
class TestSparql(WikidataTestCase):
"""Test SPARQL queries."""
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/801419
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I833026f3a8e1ad435a8130263c87edecd91fedba
Gerrit-Change-Number: 801419
Gerrit-PatchSet: 10
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Lokal Profil <andre.costa(a)wikimedia.se>
Gerrit-Reviewer: Smalyshev <smalyshev(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged