Skip to content

Commit ff967e5

Browse files
committed
Avoid the chardet.universaldetector deprecation warning
chardet 7 moved UniversalDetector to chardet.detector and turned the old chardet.universaldetector import path into a stub that raises a DeprecationWarning on import. Projects that run their test suites with warnings turned into errors (like Flexget, per the linked issue) hit a hard failure the moment html5lib falls back to chardet for encoding detection. Try the new import path first and fall back to the old one, so this keeps working across both old and new chardet releases without triggering the warning on the new ones. Fixes #601.
1 parent fd4f032 commit ff967e5

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ Patches and suggestions
6363
- Ville Skyttä
6464
- Hugo van Kemenade
6565
- Mark Vasilkov
66+
- Afonso Januário
6667

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Bug fixes:
2020

2121
* The sanitizer now permits ``<summary>`` tags. It used to allow ``<details>``
2222
already. (#423)
23+
* Encoding detection no longer triggers a ``DeprecationWarning`` under
24+
chardet 7+, which moved ``UniversalDetector`` out of
25+
``chardet.universaldetector``. (#601)
2326

2427
1.1
2528
~~~

html5lib/_inputstream.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,16 @@ def determineEncoding(self, chardet=True):
483483
# Guess with chardet, if available
484484
if chardet:
485485
try:
486-
from chardet.universaldetector import UniversalDetector
486+
# chardet 7+ moved UniversalDetector here; the older
487+
# chardet.universaldetector path still works but now raises
488+
# a DeprecationWarning.
489+
from chardet.detector import UniversalDetector
487490
except ImportError:
488-
pass
489-
else:
491+
try:
492+
from chardet.universaldetector import UniversalDetector
493+
except ImportError:
494+
UniversalDetector = None
495+
if UniversalDetector is not None:
490496
buffers = []
491497
detector = UniversalDetector()
492498
while not detector.done:

0 commit comments

Comments
 (0)