Skip to content

Commit

Permalink
[PR #7995/43a5bc50 backport][3.9] Fix examples of `fallback_charset_r…
Browse files Browse the repository at this point in the history
…esolver` function in client_advanced documentation (#8044)

**This is a backport of PR #7995 as merged into master
(43a5bc5).**

Co-authored-by: OMOTO Tsukasa <[email protected]>
  • Loading branch information
patchback[bot] and henry0312 authored Jan 21, 2024
1 parent 034e5e3 commit 437ac47
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/7995.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix examples of `fallback_charset_resolver` function in client_advanced documentation. -- by :user:`henry0312`
4 changes: 2 additions & 2 deletions docs/client_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,12 @@ example, this can be used with the ``chardetng_py`` library.::

def charset_resolver(resp: ClientResponse, body: bytes) -> str:
tld = resp.url.host.rsplit(".", maxsplit=1)[-1]
return detect(body, allow_utf8=True, tld=tld)
return detect(body, allow_utf8=True, tld=tld.encode())

ClientSession(fallback_charset_resolver=charset_resolver)

Or, if ``chardetng_py`` doesn't work for you, then ``charset-normalizer`` is another option::

from charset_normalizer import detect

ClientSession(fallback_charset_resolver=lamba r, b: detect(b)["encoding"] or "utf-8")
ClientSession(fallback_charset_resolver=lambda r, b: detect(b)["encoding"] or "utf-8")

0 comments on commit 437ac47

Please sign in to comment.