jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1330568?usp=email )

Change subject: http: Simplify user-agent username quoting
......................................................................

http: Simplify user-agent username quoting

Use str.isascii() instead of exception-based ASCII detection.
Pass Unicode strings directly to urllib.parse.quote.

Change-Id: I75542833faec51724fd06413e70c640d6bf12fcb
---
M pywikibot/comms/http.py
1 file changed, 7 insertions(+), 13 deletions(-)

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




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 8eecccd..a410d7a 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -205,8 +205,7 @@
     To achieve that, this function:

     - replaces spaces (' ') with '_'
-    - encodes the username as 'utf-8' and if the username is not ASCII
-    - URL encodes the username if it is not ASCII, or contains '%'
+    - URL encodes the username if it is not ASCII or contains '%'

     .. version-changed:: 11.0
        If *username* is not given, get it from environment variables
@@ -219,17 +218,12 @@
         return ''

     username = username.replace(' ', '_')  # Avoid spaces or %20.
-    try:
-        username.encode('ascii')  # just test, but not actually use it
-    except UnicodeEncodeError:
-        username = quote(username.encode('utf-8'))
-    else:
-        # % is legal in the default $wgLegalTitleChars
-        # This is so that ops know the real pywikibot will not
-        # allow a useragent in the username to allow through a hand-coded
-        # percent-encoded value.
-        if '%' in username:
-            username = quote(username)
+    # % is legal in the default $wgLegalTitleChars
+    # This is so that ops know the real pywikibot will not
+    # allow a useragent in the username to allow through a hand-coded
+    # percent-encoded value.
+    if '%' in username or not username.isascii():
+        username = quote(username)
     return username



--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1330568?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I75542833faec51724fd06413e70c640d6bf12fcb
Gerrit-Change-Number: 1330568
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to