Xqt has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/831178 )
Change subject: [IMPR] Simplify _decide_encoding ......................................................................
[IMPR] Simplify _decide_encoding
Change-Id: I67bebe73180bf01d44e282a737aab42e722b528e --- M pywikibot/comms/http.py 1 file changed, 6 insertions(+), 12 deletions(-)
Approvals: Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py index c5c4b11..4948f15 100644 --- a/pywikibot/comms/http.py +++ b/pywikibot/comms/http.py @@ -480,25 +480,19 @@
# No charset requested, or in request headers or response headers. # Defaults to latin1. - if charset is None and header_encoding is None: - return _try_decode(response.content, 'latin1') + if charset is None: + return _try_decode(response.content, header_encoding or 'latin1')
- if charset is None and header_encoding is not None: - return _try_decode(response.content, header_encoding) - - if charset is not None and header_encoding is None: + if header_encoding is None: return _try_decode(response.content, charset)
# Both charset and header_encoding are available. - try: + header_codecs = charset_codecs = None + with suppress(LookupError): header_codecs = codecs.lookup(header_encoding) - except LookupError: - header_codecs = None
- try: + with suppress(LookupError): charset_codecs = codecs.lookup(charset) - except LookupError: - charset_codecs = None
if header_codecs and charset_codecs and header_codecs != charset_codecs: pywikibot.warning(
pywikibot-commits@lists.wikimedia.org