Handle chardet returning `None` as encoding

This commit is contained in:
rlaphoenix 2023-12-02 15:10:00 +00:00
parent c31ee338dc
commit 0be62541ba
1 changed files with 2 additions and 0 deletions

View File

@ -237,6 +237,8 @@ def try_ensure_utf8(data: bytes) -> bytes:
try:
# last ditch effort to detect encoding
detection_result = chardet.detect(data)
if not detection_result["encoding"]:
return data
return data.decode(detection_result["encoding"]).encode("utf8")
except UnicodeDecodeError:
return data