Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2 

3""" 

4requests.compat 

5~~~~~~~~~~~~~~~ 

6 

7This module handles import compatibility issues between Python 2 and 

8Python 3. 

9""" 

10 

11import chardet 

12 

13import sys 

14 

15# ------- 

16# Pythons 

17# ------- 

18 

19# Syntax sugar. 

20_ver = sys.version_info 

21 

22#: Python 2.x? 

23is_py2 = (_ver[0] == 2) 

24 

25#: Python 3.x? 

26is_py3 = (_ver[0] == 3) 

27 

28try: 

29 import simplejson as json 

30except ImportError: 

31 import json 

32 

33# --------- 

34# Specifics 

35# --------- 

36 

37if is_py2: 

38 from urllib import ( 

39 quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, 

40 proxy_bypass, proxy_bypass_environment, getproxies_environment) 

41 from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag 

42 from urllib2 import parse_http_list 

43 import cookielib 

44 from Cookie import Morsel 

45 from StringIO import StringIO 

46 # Keep OrderedDict for backwards compatibility. 

47 from collections import Callable, Mapping, MutableMapping, OrderedDict 

48 

49 

50 builtin_str = str 

51 bytes = str 

52 str = unicode 

53 basestring = basestring 

54 numeric_types = (int, long, float) 

55 integer_types = (int, long) 

56 

57elif is_py3: 

58 from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag 

59 from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment 

60 from http import cookiejar as cookielib 

61 from http.cookies import Morsel 

62 from io import StringIO 

63 # Keep OrderedDict for backwards compatibility. 

64 from collections import OrderedDict 

65 from collections.abc import Callable, Mapping, MutableMapping 

66 

67 builtin_str = str 

68 str = str 

69 bytes = bytes 

70 basestring = (str, bytes) 

71 numeric_types = (int, float) 

72 integer_types = (int,)