jenkins-bot submitted this change.

View Change

Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
[IMPR] Replace HttpRequest.raw with HttpRequest.content

- HttpRequest.content was deprecated for a long period and text
attribute should be used instead. Change the meaning of content
to response in bytes like we have already in HttpRequest.raw.
- Deprecate HttpRequest.raw in favour of content attribute
- Replace all occurrences of HttpRequest.raw with content

Reason:
comms.threadedhttp.HttpRequest will be removed
in favour of direct requests response.

Bug: T265206
Change-Id: Iefdbc9acfe40cb963a49e617bff103934b1c9d03
---
M pywikibot/comms/threadedhttp.py
M pywikibot/data/wikistats.py
M pywikibot/version.py
M scripts/data_ingestion.py
M scripts/flickrripper.py
M scripts/match_images.py
M scripts/reflinks.py
M tests/http_tests.py
8 files changed, 33 insertions(+), 30 deletions(-)

diff --git a/pywikibot/comms/threadedhttp.py b/pywikibot/comms/threadedhttp.py
index 659acab..c5c0e38 100644
--- a/pywikibot/comms/threadedhttp.py
+++ b/pywikibot/comms/threadedhttp.py
@@ -121,9 +121,14 @@
return self.data.headers if not self.exception else None

@property
- def raw(self) -> Optional[bytes]:
- """Return the raw response body."""
- return self.data.content if not self.exception else None
+ @deprecated('the `content` property', since='20201210',
+ future_warning=True)
+ def raw(self) -> Optional[bytes]: # pragma: no cover
+ """DEPRECATED. Return the raw response body.
+
+ @note: The behaviour will be changed.
+ """
+ return self.content

@property
@deprecated('urlparse(HttpRequest.url)',
@@ -167,7 +172,7 @@
# application/json | application/sparql-results+json
self._header_encoding = 'utf-8'
elif 'xml' in content_type:
- header = self.raw[:100].splitlines()[0] # bytes
+ header = self.content[:100].splitlines()[0] # bytes
m = re.search(
br'encoding=(["\'])(?P<encoding>.+?)\1', header)
if m:
@@ -217,7 +222,7 @@
def _try_decode(self, encoding):
"""Helper function to try decoding."""
try:
- self.raw.decode(encoding)
+ self.content.decode(encoding)
except UnicodeError as e:
result = e
else:
@@ -227,23 +232,21 @@
@deprecated('the `text` property', since='20201011', future_warning=True)
def decode(self, encoding, errors='strict') -> str: # pragma: no cover
"""Return the decoded response."""
- return self.raw.decode(encoding,
- errors) if not self.exception else None
+ return self.content.decode(
+ encoding, errors) if not self.exception else None

@property
- @deprecated('the `text` property', since='20180321', future_warning=True)
- def content(self) -> str: # pragma: no cover
- """DEPRECATED. Return the response decoded by the detected encoding.
+ def content(self) -> bytes:
+ """Return the response in bytes.

- @note: The behaviour will be changed.
- This method will return the content of the response in bytes.
+ @note: The behaviour has been changed.
"""
- return self.text
+ return self.data.content if not self.exception else None

@property
def text(self) -> str:
"""Return the response decoded by the detected encoding."""
- return self.raw.decode(self.encoding)
+ return self.content.decode(self.encoding)

@deprecated('the `text` property', since='20201011', future_warning=True)
def __str__(self) -> str: # pragma: no cover
@@ -253,4 +256,4 @@
@deprecated(since='20201011', future_warning=True)
def __bytes__(self) -> Optional[bytes]: # pragma: no cover
"""Return the undecoded response."""
- return self.raw
+ return self.content
diff --git a/pywikibot/data/wikistats.py b/pywikibot/data/wikistats.py
index 5d70500..8f20292 100644
--- a/pywikibot/data/wikistats.py
+++ b/pywikibot/data/wikistats.py
@@ -102,7 +102,7 @@
table = self.FAMILY_MAPPING[table]

r = http.fetch(url.format(table=table, format=format))
- return r.raw
+ return r.content

@deprecated('get', since='20201017', future_warning=True)
def raw_cached(self, table: str,
diff --git a/pywikibot/version.py b/pywikibot/version.py
index ce66b6f..ab51901 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -204,7 +204,7 @@
'<propfind xmlns=\"DAV:\"><allprop/></propfind>',
headers={'label': str(rev),
'user-agent': 'SVN/1.7.5 {pwb}'})
- dom = xml.dom.minidom.parse(BytesIO(request.raw))
+ dom = xml.dom.minidom.parse(BytesIO(request.content))
hsh = dom.getElementsByTagName('C:git-commit')[0].firstChild.nodeValue
date = dom.getElementsByTagName('S:date')[0].firstChild.nodeValue
date = time.strptime(date[:19], '%Y-%m-%dT%H:%M:%S')
diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py
index 608aa2a..224058f 100755
--- a/scripts/data_ingestion.py
+++ b/scripts/data_ingestion.py
@@ -160,7 +160,7 @@
TODO: Add exception handling
"""
if not self.contents:
- imageFile = fetch(self.URL).raw
+ imageFile = fetch(self.URL).content
self.contents = io.BytesIO(imageFile)
return self.contents

diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py
index 757cd48..4b098ac 100755
--- a/scripts/flickrripper.py
+++ b/scripts/flickrripper.py
@@ -108,7 +108,7 @@
TODO: Add exception handling

"""
- imageFile = fetch(photoUrl).raw
+ imageFile = fetch(photoUrl).content
return io.BytesIO(imageFile)


diff --git a/scripts/match_images.py b/scripts/match_images.py
index 22e44c1..7283f2c 100755
--- a/scripts/match_images.py
+++ b/scripts/match_images.py
@@ -110,7 +110,7 @@
"""Get the image object to work based on an imagePage object."""
imageURL = imagePage.get_file_url()
imageURLopener = http.fetch(imageURL)
- imageBuffer = io.BytesIO(imageURLopener.raw[:])
+ imageBuffer = io.BytesIO(imageURLopener.content[:])
image = Image.open(imageBuffer)
return image

diff --git a/scripts/reflinks.py b/scripts/reflinks.py
index dd5420b..9ac0292 100755
--- a/scripts/reflinks.py
+++ b/scripts/reflinks.py
@@ -595,7 +595,7 @@
new_text = new_text.replace(match.group(), repl)
return

- linkedpagetext = f.raw
+ linkedpagetext = f.content

except UnicodeError:
# example:
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 6b48725..70d2efe 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -46,7 +46,7 @@
self.assertEqual(r.status_code, 200)
self.assertIn('<html lang="mul"', r.text)
self.assertIsInstance(r.text, str)
- self.assertIsInstance(r.raw, bytes)
+ self.assertIsInstance(r.content, bytes)


class HttpRequestURI(DeprecationTestCase):
@@ -419,7 +419,7 @@
req._data = resp
self.assertIsNone(req.charset)
self.assertEqual('latin1', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.LATIN1_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.LATIN1_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_no_charset(self):
@@ -431,7 +431,7 @@
req._data = resp
self.assertIsNone(req.charset)
self.assertEqual('latin1', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.LATIN1_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.LATIN1_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_content_type_application_json_without_charset(self):
@@ -503,7 +503,7 @@
req = CharsetTestCase._create_request()
self.assertIsNone(req.charset)
self.assertEqual('utf-8', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.UTF8_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.UTF8_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_same_charset(self):
@@ -511,7 +511,7 @@
req = CharsetTestCase._create_request('utf-8')
self.assertEqual('utf-8', req.charset)
self.assertEqual('utf-8', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.UTF8_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.UTF8_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_header_charset(self):
@@ -521,7 +521,7 @@
# Ignore WARNING: Encoding "latin1" requested but "utf-8" received
with patch('pywikibot.warning'):
self.assertEqual('utf-8', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.UTF8_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.UTF8_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_code_charset(self):
@@ -532,7 +532,7 @@
# Ignore WARNING: Encoding "latin1" requested but "utf-8" received
with patch('pywikibot.warning'):
self.assertEqual('latin1', req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.LATIN1_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.LATIN1_BYTES)
self.assertEqual(req.text, CharsetTestCase.STR)

def test_invalid_charset(self):
@@ -545,7 +545,7 @@
self.assertRaisesRegex(
UnicodeDecodeError, self.CODEC_CANT_DECODE_RE,
lambda: req.encoding)
- self.assertEqual(req.raw, CharsetTestCase.LATIN1_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.LATIN1_BYTES)
self.assertRaisesRegex(
UnicodeDecodeError, self.CODEC_CANT_DECODE_RE, lambda: req.text)

@@ -579,7 +579,7 @@
"""Test with http, standard http interface for pywikibot."""
r = http.fetch(uri=self.url)

- self.assertEqual(r.raw, self.png)
+ self.assertEqual(r.content, self.png)


class TestDeprecatedGlobalCookieJar(DeprecationTestCase):

To view, visit change 647709. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Iefdbc9acfe40cb963a49e617bff103934b1c9d03
Gerrit-Change-Number: 647709
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki@aol.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged