|
4 | 4 | requests.compat
|
5 | 5 | ~~~~~~~~~~~~~~~
|
6 | 6 |
|
7 |
| -This module handles import compatibility issues between Python 2 and |
8 |
| -Python 3. |
| 7 | +This module previously handled import compatibility issues |
| 8 | +between Python 2 and Python 3. It remains for backwards |
| 9 | +compatibility until the next major version. |
9 | 10 | """
|
10 | 11 |
|
11 | 12 | try:
|
|
28 | 29 | #: Python 3.x?
|
29 | 30 | is_py3 = (_ver[0] == 3)
|
30 | 31 |
|
| 32 | +# json/simplejson module import resolution |
31 | 33 | has_simplejson = False
|
32 | 34 | try:
|
33 | 35 | import simplejson as json
|
34 | 36 | has_simplejson = True
|
35 | 37 | except ImportError:
|
36 | 38 | import json
|
37 | 39 |
|
| 40 | +if has_simplejson: |
| 41 | + from simplejson import JSONDecodeError |
| 42 | +else: |
| 43 | + from json import JSONDecodeError |
| 44 | + |
38 | 45 | # ---------
|
39 |
| -# Specifics |
| 46 | +# Legacy Imports |
40 | 47 | # ---------
|
41 |
| - |
42 |
| -if is_py2: |
43 |
| - from urllib import ( |
44 |
| - quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, |
45 |
| - proxy_bypass, proxy_bypass_environment, getproxies_environment) |
46 |
| - from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag |
47 |
| - from urllib2 import parse_http_list |
48 |
| - import cookielib |
49 |
| - from Cookie import Morsel |
50 |
| - from StringIO import StringIO |
51 |
| - # Keep OrderedDict for backwards compatibility. |
52 |
| - from collections import Callable, Mapping, MutableMapping, OrderedDict |
53 |
| - |
54 |
| - builtin_str = str |
55 |
| - bytes = str |
56 |
| - str = unicode |
57 |
| - basestring = basestring |
58 |
| - numeric_types = (int, long, float) |
59 |
| - integer_types = (int, long) |
60 |
| - JSONDecodeError = ValueError |
61 |
| - |
62 |
| -elif is_py3: |
63 |
| - from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag |
64 |
| - from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment |
65 |
| - from http import cookiejar as cookielib |
66 |
| - from http.cookies import Morsel |
67 |
| - from io import StringIO |
68 |
| - # Keep OrderedDict for backwards compatibility. |
69 |
| - from collections import OrderedDict |
70 |
| - from collections.abc import Callable, Mapping, MutableMapping |
71 |
| - if has_simplejson: |
72 |
| - from simplejson import JSONDecodeError |
73 |
| - else: |
74 |
| - from json import JSONDecodeError |
75 |
| - |
76 |
| - builtin_str = str |
77 |
| - str = str |
78 |
| - bytes = bytes |
79 |
| - basestring = (str, bytes) |
80 |
| - numeric_types = (int, float) |
81 |
| - integer_types = (int,) |
| 48 | +from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag |
| 49 | +from urllib.request import parse_http_list, getproxies, proxy_bypass, proxy_bypass_environment, getproxies_environment |
| 50 | +from http import cookiejar as cookielib |
| 51 | +from http.cookies import Morsel |
| 52 | +from io import StringIO |
| 53 | + |
| 54 | +# Keep OrderedDict for backwards compatibility. |
| 55 | +from collections import OrderedDict |
| 56 | +from collections.abc import Callable, Mapping, MutableMapping |
| 57 | + |
| 58 | +builtin_str = str |
| 59 | +str = str |
| 60 | +bytes = bytes |
| 61 | +basestring = (str, bytes) |
| 62 | +numeric_types = (int, float) |
| 63 | +integer_types = (int,) |
0 commit comments