Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/chardet/pipeline/validity.py: 23%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

13 statements  

1"""Stage 2a: Byte sequence validity filtering. 

2 

3Note: ``from __future__ import annotations`` is intentionally omitted because 

4this module is compiled with mypyc, which does not support PEP 563 string 

5annotations. 

6""" 

7 

8from chardet.registry import EncodingInfo 

9 

10 

11def filter_by_validity( 

12 data: bytes, candidates: tuple[EncodingInfo, ...] 

13) -> tuple[EncodingInfo, ...]: 

14 """Filter candidates to only those where *data* decodes without errors. 

15 

16 :param data: The raw byte data to test. 

17 :param candidates: Encoding candidates to validate. 

18 :returns: The subset of *candidates* that can decode *data*. 

19 """ 

20 if not data: 

21 return candidates 

22 

23 valid = [] 

24 for enc in candidates: 

25 try: 

26 data.decode(enc.name, errors="strict") 

27 valid.append(enc) 

28 except (UnicodeDecodeError, LookupError): 

29 continue 

30 return tuple(valid)