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

43 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 

10import importlib 

11import sys 

12 

13# ------- 

14# urllib3 

15# ------- 

16from urllib3 import __version__ as urllib3_version 

17 

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

19try: 

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

21except (TypeError, AttributeError): 

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

23 is_urllib3_1 = True 

24 

25# ------------------- 

26# Character Detection 

27# ------------------- 

28 

29 

30def _resolve_char_detection(): 

31 """Find supported character detection libraries.""" 

32 chardet = None 

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

34 if chardet is None: 

35 try: 

36 chardet = importlib.import_module(lib) 

37 except ImportError: 

38 pass 

39 return chardet 

40 

41 

42chardet = _resolve_char_detection() 

43 

44# ------- 

45# Pythons 

46# ------- 

47 

48# Syntax sugar. 

49_ver = sys.version_info 

50 

51#: Python 2.x? 

52is_py2 = _ver[0] == 2 

53 

54#: Python 3.x? 

55is_py3 = _ver[0] == 3 

56 

57# json/simplejson module import resolution 

58has_simplejson = False 

59try: 

60 import simplejson as json 

61 

62 has_simplejson = True 

63except ImportError: 

64 import json 

65 

66if has_simplejson: 

67 from simplejson import JSONDecodeError 

68else: 

69 from json import JSONDecodeError 

70 

71# Keep OrderedDict for backwards compatibility. 

72from collections import OrderedDict 

73from collections.abc import Callable, Mapping, MutableMapping 

74from http import cookiejar as cookielib 

75from http.cookies import Morsel 

76from io import StringIO 

77 

78# -------------- 

79# Legacy Imports 

80# -------------- 

81from urllib.parse import ( 

82 quote, 

83 quote_plus, 

84 unquote, 

85 unquote_plus, 

86 urldefrag, 

87 urlencode, 

88 urljoin, 

89 urlparse, 

90 urlsplit, 

91 urlunparse, 

92) 

93from urllib.request import ( 

94 getproxies, 

95 getproxies_environment, 

96 parse_http_list, 

97 proxy_bypass, 

98 proxy_bypass_environment, 

99) 

100 

101builtin_str = str 

102str = str 

103bytes = bytes 

104basestring = (str, bytes) 

105numeric_types = (int, float) 

106integer_types = (int,)