jenkins-bot has submitted this change and it was merged.

Change subject: [FIX] HTTP: Decode Python 3 stream
......................................................................


[FIX] HTTP: Decode Python 3 stream

httplib2 returns a bytes object in Python 3 which must be decoded
first. It reads the encoding from the header and applies it. If the
encoding wasn't transmitted, it defaults to ASCII.

Change-Id: I2191170f3793d00f6cb32e818d0387ff5208565d
---
M pywikibot/comms/http.py
M tests/http_tests.py
2 files changed, 13 insertions(+), 4 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index e9703ff..3fd9cbc 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -256,4 +256,13 @@
         pywikibot.warning(u"Http response status %(status)s"
                           % {'status': request.data[0].status})
 
-    return request.data[1]
+    pos = request.data[0]['content-type'].find('charset=')
+    if pos >= 0:
+        pos += len('charset=')
+        encoding = request.data[0]['content-type'][pos:]
+    else:
+        encoding = 'ascii'
+        # Don't warn, many pages don't contain one
+        pywikibot.log(u"Http response doesn't contain a charset.")
+
+    return request.data[1].decode(encoding)
diff --git a/tests/http_tests.py b/tests/http_tests.py
index 9229cdd..2c90b65 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -20,7 +20,7 @@
 
     def test_get(self):
         r = http.request(site=None, uri='http://www.wikipedia.org/')
-        self.assertIsInstance(r, str)
+        self.assertIsInstance(r, str if sys.version_info[0] >= 3 else unicode)
         self.assertIn('<html lang="mul"', r)
 
     def test_request(self):
@@ -32,8 +32,8 @@
         self.assertIsInstance(r[0]['status'], str)
         self.assertEqual(r[0]['status'], '200')
 
-        self.assertIsInstance(r[1], str)
-        self.assertIn('<html lang="mul"', r[1])
+        self.assertIsInstance(r[1], bytes if sys.version_info[0] >= 3 else str)
+        self.assertIn(b'<html lang="mul"', r[1])
         self.assertEqual(int(r[0]['content-length']), len(r[1]))
 
     def test_gzip(self):

-- 
To view, visit https://gerrit.wikimedia.org/r/158358
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2191170f3793d00f6cb32e818d0387ff5208565d
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <commodorefabia...@gmx.de>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to