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

Change subject: [bugfix] Retieve charset from accept-charset header entry
......................................................................

[bugfix] Retieve charset from accept-charset header entry

accept-charset may be a list of valid charsets like
'ISO-8859-1,utf-8;q=0.7,*;q=0.7'

Bug: T392345
Change-Id: Ib63ad7a7f0f4d1d8a15c14bbe966314dbfa7880c
---
M pywikibot/comms/http.py
1 file changed, 13 insertions(+), 4 deletions(-)

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




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 67a89d3..50dd2eb 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -25,7 +25,7 @@
    Cookies are lazy loaded when logging to site.
 """
 #
-# (C) Pywikibot team, 2007-2024
+# (C) Pywikibot team, 2007-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -512,7 +512,14 @@

 def _decide_encoding(response: requests.Response,
                      charset: str | None = None) -> str | None:
-    """Detect the response encoding."""
+    """Detect the response encoding.
+
+    .. versionchanged:: 10.1
+       retrieve charset from `Accept-Charset` list which may look like
+       `'ISO-8859-1,utf-8;q=0.7,*;q=0.7'`.
+
+    :meta public:
+    """
     def _try_decode(content: bytes, encoding: str | None) -> str | None:
         """Helper function to try decoding."""
         if encoding is None:
@@ -521,7 +528,8 @@
         try:
             content.decode(encoding)
         except LookupError:
-            pywikibot.warning(f'Unknown or invalid encoding {encoding!r}')
+            pywikibot.warning(
+                f'Unknown or invalid encoding {encoding!r} for {response.url}')
         except UnicodeDecodeError as e:
             pywikibot.warning(f'{e} found in {content}')
         else:
@@ -534,7 +542,8 @@
         pywikibot.log('Http response does not contain a charset.')

     if charset is None:
-        charset = response.request.headers.get('accept-charset')
+        charset = response.request.headers.get('accept-charset', '')
+        charset = charset.split(',', 1)[0].split(';', 1)[0].strip() or None

     # No charset requested, or in request headers or response headers.
     # Defaults to latin1.

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1137561?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: Ib63ad7a7f0f4d1d8a15c14bbe966314dbfa7880c
Gerrit-Change-Number: 1137561
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <[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