Skip to content

Commit

Permalink
Fix examples of fallback_charset_resolver function in client_advanc…
Browse files Browse the repository at this point in the history
…ed documentation (#7995)

(cherry picked from commit 43a5bc5)
  • Loading branch information
henry0312 authored and patchback[bot] committed Jan 21, 2024
1 parent 4b91b53 commit 60d518e
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 60d518e

Please sign in to comment.