Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/w3lib/util.py: 22%

36 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-07 06:38 +0000

1from warnings import warn 

2from typing import Optional 

3 

4from w3lib._types import StrOrBytes 

5 

6 

7def str_to_unicode( 

8 text: StrOrBytes, encoding: Optional[str] = None, errors: str = "strict" 

9) -> str: 

10 warn( 

11 "The w3lib.utils.str_to_unicode function is deprecated and " 

12 "will be removed in a future release.", 

13 DeprecationWarning, 

14 stacklevel=2, 

15 ) 

16 if encoding is None: 

17 encoding = "utf-8" 

18 if isinstance(text, bytes): 

19 return text.decode(encoding, errors) 

20 return text 

21 

22 

23def unicode_to_str( 

24 text: StrOrBytes, encoding: Optional[str] = None, errors: str = "strict" 

25) -> bytes: 

26 warn( 

27 "The w3lib.utils.unicode_to_str function is deprecated and " 

28 "will be removed in a future release.", 

29 DeprecationWarning, 

30 stacklevel=2, 

31 ) 

32 if encoding is None: 

33 encoding = "utf-8" 

34 if isinstance(text, str): 

35 return text.encode(encoding, errors) 

36 return text 

37 

38 

39def to_unicode( 

40 text: StrOrBytes, encoding: Optional[str] = None, errors: str = "strict" 

41) -> str: 

42 """Return the unicode representation of a bytes object `text`. If `text` 

43 is already an unicode object, return it as-is.""" 

44 if isinstance(text, str): 

45 return text 

46 if not isinstance(text, (bytes, str)): 

47 raise TypeError( 

48 f"to_unicode must receive bytes or str, got {type(text).__name__}" 

49 ) 

50 if encoding is None: 

51 encoding = "utf-8" 

52 return text.decode(encoding, errors) 

53 

54 

55def to_bytes( 

56 text: StrOrBytes, encoding: Optional[str] = None, errors: str = "strict" 

57) -> bytes: 

58 """Return the binary representation of `text`. If `text` 

59 is already a bytes object, return it as-is.""" 

60 if isinstance(text, bytes): 

61 return text 

62 if not isinstance(text, str): 

63 raise TypeError( 

64 f"to_bytes must receive str or bytes, got {type(text).__name__}" 

65 ) 

66 if encoding is None: 

67 encoding = "utf-8" 

68 return text.encode(encoding, errors) 

69 

70 

71def to_native_str( 

72 text: StrOrBytes, encoding: Optional[str] = None, errors: str = "strict" 

73) -> str: 

74 """Return str representation of `text`""" 

75 warn( 

76 "The w3lib.utils.to_native_str function is deprecated and " 

77 "will be removed in a future release. Please use " 

78 "w3lib.utils.to_unicode instead.", 

79 DeprecationWarning, 

80 stacklevel=2, 

81 ) 

82 return to_unicode(text, encoding, errors)