Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/requests/compat.py: 90%

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

48 statements  

1""" 

2requests.compat 

3~~~~~~~~~~~~~~~ 

4 

5This module previously handled import compatibility issues 

6between Python 2 and Python 3. It remains for backwards 

7compatibility until the next major version. 

8""" 

9 

10# pyright: reportUnusedImport=false 

11 

12from __future__ import annotations 

13 

14import importlib 

15import sys 

16from types import ModuleType 

17from typing import TYPE_CHECKING 

18 

19# ------- 

20# urllib3 

21# ------- 

22from urllib3 import ( 

23 __version__ as urllib3_version, # type: ignore[reportPrivateImportUsage] 

24) 

25 

26# Detect which major version of urllib3 is being used. 

27try: 

28 is_urllib3_1 = int(urllib3_version.split(".")[0]) == 1 

29except (TypeError, AttributeError): 

30 # If we can't discern a version, prefer old functionality. 

31 is_urllib3_1 = True 

32 

33# ------------------- 

34# Character Detection 

35# ------------------- 

36 

37 

38def _resolve_char_detection() -> ModuleType | None: 

39 """Find supported character detection libraries.""" 

40 chardet = None 

41 for lib in ("chardet", "charset_normalizer"): 

42 if chardet is None: 

43 try: 

44 chardet = importlib.import_module(lib) 

45 except ImportError: 

46 pass 

47 return chardet 

48 

49 

50if TYPE_CHECKING: 

51 import chardet 

52else: 

53 chardet = _resolve_char_detection() 

54 

55# ------- 

56# Pythons 

57# ------- 

58 

59# Syntax sugar. 

60_ver = sys.version_info 

61 

62#: Python 2.x? 

63is_py2 = _ver[0] == 2 

64 

65#: Python 3.x? 

66is_py3 = _ver[0] == 3 

67 

68# json/simplejson module import resolution 

69has_simplejson = False 

70try: 

71 import simplejson as json # type: ignore[import-not-found] 

72 

73 has_simplejson = True 

74except ImportError: 

75 import json 

76 

77if has_simplejson: 

78 from simplejson import JSONDecodeError # type: ignore[import-not-found] 

79else: 

80 from json import JSONDecodeError 

81 

82# Keep OrderedDict for backwards compatibility. 

83from collections import OrderedDict 

84from collections.abc import Callable, Mapping, MutableMapping 

85from http import cookiejar as cookielib 

86from http.cookies import Morsel 

87from io import StringIO 

88 

89# -------------- 

90# Legacy Imports 

91# -------------- 

92from urllib.parse import ( 

93 quote, 

94 quote_plus, 

95 unquote, 

96 unquote_plus, 

97 urldefrag, 

98 urlencode, 

99 urljoin, 

100 urlparse, 

101 urlsplit, 

102 urlunparse, 

103) 

104from urllib.request import ( 

105 getproxies, 

106 getproxies_environment, 

107 parse_http_list, 

108 proxy_bypass, 

109 proxy_bypass_environment, # type: ignore[attr-defined] # https://github.com/python/cpython/issues/145331 

110) 

111 

112builtin_str = str 

113str = str 

114bytes = bytes 

115basestring = (str, bytes) 

116numeric_types = (int, float) 

117integer_types = (int,)