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

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

45 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 

17 

18# ------- 

19# urllib3 

20# ------- 

21from urllib3 import ( 

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

23) 

24 

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

26try: 

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

28except (TypeError, AttributeError): 

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

30 is_urllib3_1 = True 

31 

32# ------------------- 

33# Character Detection 

34# ------------------- 

35 

36 

37def _resolve_char_detection() -> ModuleType | None: 

38 """Find supported character detection libraries.""" 

39 chardet = None 

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

41 if chardet is None: 

42 try: 

43 chardet = importlib.import_module(lib) 

44 except ImportError: 

45 pass 

46 return chardet 

47 

48 

49chardet = _resolve_char_detection() 

50 

51# ------- 

52# Pythons 

53# ------- 

54 

55# Syntax sugar. 

56_ver = sys.version_info 

57 

58#: Python 2.x? 

59is_py2 = _ver[0] == 2 

60 

61#: Python 3.x? 

62is_py3 = _ver[0] == 3 

63 

64# json/simplejson module import resolution 

65has_simplejson = False 

66try: 

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

68 

69 has_simplejson = True 

70except ImportError: 

71 import json 

72 

73if has_simplejson: 

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

75else: 

76 from json import JSONDecodeError 

77 

78# Keep OrderedDict for backwards compatibility. 

79from collections import OrderedDict 

80from collections.abc import Callable, Mapping, MutableMapping 

81from http import cookiejar as cookielib 

82from http.cookies import Morsel 

83from io import StringIO 

84 

85# -------------- 

86# Legacy Imports 

87# -------------- 

88from urllib.parse import ( 

89 quote, 

90 quote_plus, 

91 unquote, 

92 unquote_plus, 

93 urldefrag, 

94 urlencode, 

95 urljoin, 

96 urlparse, 

97 urlsplit, 

98 urlunparse, 

99) 

100from urllib.request import ( 

101 getproxies, 

102 getproxies_environment, 

103 parse_http_list, 

104 proxy_bypass, 

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

106) 

107 

108builtin_str = str 

109str = str 

110bytes = bytes 

111basestring = (str, bytes) 

112numeric_types = (int, float) 

113integer_types = (int,)