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

Change subject: api.py: fix UnicodeError in url_encode()
......................................................................


api.py: fix UnicodeError in url_encode()

Bug: T121318
Change-Id: Ifa4ca0268b4d0d50a3148e709d652aff9216e7e8
---
M pywikibot/data/api.py
M tests/api_tests.py
2 files changed, 19 insertions(+), 2 deletions(-)

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



diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index abeafdf..8723021 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -38,6 +38,7 @@
 )
 from pywikibot.tools import (
     MediaWikiVersion, deprecated, itergroup, ip, PY2, getargspec,
+    UnicodeType
 )
 from pywikibot.tools.formatter import color_format
 
@@ -3046,7 +3047,12 @@
         query = list(query.items())
 
     if PY2:
-        query = [(pair[0], pair[1].encode('utf-8')) for pair in query]
+        def _encode(x):
+            if isinstance(x, UnicodeType):
+                return x.encode('utf-8')
+            else:
+                return x
+        query = [(pair[0], _encode(pair[1])) for pair in query]
 
     # parameters ending on 'token' should go last
     # wpEditToken should go very last
diff --git a/tests/api_tests.py b/tests/api_tests.py
index 1cbb9de..1b0ee7d 100644
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -975,7 +975,7 @@
 
 class TestUrlEncoding(TestCase):
 
-    """Test url_encode."""
+    """Test encode_url() function."""
 
     net = False
 
@@ -1005,6 +1005,17 @@
         self.assertEqual(result, expect)
         self.assertIsInstance(result, str)
 
+    def test_url_encoding_from_basestring(self):
+        """Test encoding basestring values."""
+        if PY2:
+            query = {'token': str('test\xe2\x80\x94test'.encode('utf-8'))}
+        else:
+            query = {'token': 'test\xe2\x80\x94test'}
+        expect = str('token=test%C3%A2%C2%80%C2%94test')
+        result = api.encode_url(query)
+        self.assertEqual(result, expect)
+        self.assertIsInstance(result, str)
+
     def test_moving_special_tokens(self):
         """Test moving wpEditToken to the very end."""
         query = {'wpEditToken': 'c', 'token': 'b', 'text': 'a'}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ifa4ca0268b4d0d50a3148e709d652aff9216e7e8
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.w...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: XZise <commodorefabia...@gmx.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
Pywikibot-commits mailing list
Pywikibot-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/pywikibot-commits

Reply via email to