|
27 | 27 |
|
28 | 28 | import sys
|
29 | 29 |
|
30 |
| -# ------- |
31 |
| -# Pythons |
32 |
| -# ------- |
33 |
| - |
34 |
| -# Syntax sugar. |
35 |
| -_ver = sys.version_info |
36 |
| - |
37 | 30 | #: Python 2.x?
|
38 |
| -is_py2 = (_ver[0] == 2) |
| 31 | +_is_py2 = (sys.version_info[0] == 2) |
39 | 32 |
|
40 | 33 | #: Python 3.x?
|
41 |
| -is_py3 = (_ver[0] == 3) |
| 34 | +_is_py3 = (sys.version_info[0] == 3) |
42 | 35 |
|
43 |
| -if is_py2: |
44 |
| - from urllib import pathname2url as urlencode |
45 |
| - from urllib import url2pathname as urldecode |
46 |
| - from urlparse import urlsplit, parse_qs |
| 36 | +if _is_py2: |
| 37 | + from urllib import pathname2url |
| 38 | + urlencode = pathname2url |
47 | 39 |
|
48 |
| - builtin_str = str |
| 40 | + from urllib import url2pathname |
| 41 | + urldecode = url2pathname |
| 42 | + |
| 43 | + import urlparse |
| 44 | + urlsplit = urlparse.urlsplit |
| 45 | + parse_qs = urlparse.parse_qs |
| 46 | + |
| 47 | + ## Create missing types. |
49 | 48 | bytes = str
|
| 49 | + |
| 50 | + ## Update better types. |
| 51 | + builtin_range = range |
| 52 | + range = xrange |
| 53 | + builtin_str = str |
50 | 54 | str = unicode
|
| 55 | + |
| 56 | + ## Add missing imports |
51 | 57 | basestring = basestring
|
52 |
| - numeric_types = (int, long, float) |
53 |
| - range = xrange |
| 58 | +elif _is_py3: |
| 59 | + from urllib.request import pathname2url |
| 60 | + urlencode = pathname2url |
| 61 | + |
| 62 | + from urllib.request import url2pathname |
| 63 | + urldecode = url2pathname |
54 | 64 |
|
55 |
| -elif is_py3: |
56 |
| - from urllib.request import pathname2url as urlencode |
57 |
| - from urllib.request import url2pathname as urldecode |
58 |
| - from urllib.parse import urlsplit, parse_qs |
| 65 | + import urllib.parse |
| 66 | + urlsplit = urllib.parse.urlsplit |
| 67 | + parse_qs = urllib.parse.parse_qs |
59 | 68 |
|
| 69 | + ## Create types to compat with py2. |
| 70 | + builtin_range = range |
60 | 71 | builtin_str = str
|
61 |
| - str = str |
62 |
| - bytes = bytes |
| 72 | + |
| 73 | + ## Create missing types. |
63 | 74 | basestring = (str, bytes)
|
64 |
| - numeric_types = (int, float) |
| 75 | + long = int |
| 76 | + |
| 77 | + ## Add missing imports |
| 78 | + bytes = bytes |
65 | 79 | range = range
|
| 80 | + str = str |
| 81 | + |
| 82 | +numeric_types = (int, long, float) |
0 commit comments