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)
  • Loading branch information
henry0312 authored Jan 21, 2024
1 parent 54ceb6f commit 43a5bc5
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 @@ -761,12 +761,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 43a5bc5

Please sign in to comment.