Show More
@@ -25,6 +25,97 b' except ImportError:' | |||
|
25 | 25 | empty = _queue.Empty |
|
26 | 26 | queue = _queue.Queue |
|
27 | 27 | |
|
28 | class _pycompatstub(object): | |
|
29 | pass | |
|
30 | ||
|
31 | def _alias(alias, origin, items): | |
|
32 | """ populate a _pycompatstub | |
|
33 | ||
|
34 | copies items from origin to alias | |
|
35 | """ | |
|
36 | def hgcase(item): | |
|
37 | return item.replace('_', '').lower() | |
|
38 | for item in items: | |
|
39 | try: | |
|
40 | setattr(alias, hgcase(item), getattr(origin, item)) | |
|
41 | except AttributeError: | |
|
42 | pass | |
|
43 | ||
|
44 | urlreq = _pycompatstub() | |
|
45 | urlerr = _pycompatstub() | |
|
46 | try: | |
|
47 | import urllib2 | |
|
48 | import urllib | |
|
49 | _alias(urlreq, urllib, ( | |
|
50 | "addclosehook", | |
|
51 | "addinfourl", | |
|
52 | "ftpwrapper", | |
|
53 | "pathname2url", | |
|
54 | "quote", | |
|
55 | "splitattr", | |
|
56 | "splitpasswd", | |
|
57 | "splitport", | |
|
58 | "splituser", | |
|
59 | "unquote", | |
|
60 | "url2pathname", | |
|
61 | "urlencode", | |
|
62 | "urlencode", | |
|
63 | )) | |
|
64 | _alias(urlreq, urllib2, ( | |
|
65 | "AbstractHTTPHandler", | |
|
66 | "BaseHandler", | |
|
67 | "build_opener", | |
|
68 | "FileHandler", | |
|
69 | "FTPHandler", | |
|
70 | "HTTPBasicAuthHandler", | |
|
71 | "HTTPDigestAuthHandler", | |
|
72 | "HTTPHandler", | |
|
73 | "HTTPPasswordMgrWithDefaultRealm", | |
|
74 | "HTTPSHandler", | |
|
75 | "install_opener", | |
|
76 | "ProxyHandler", | |
|
77 | "Request", | |
|
78 | "urlopen", | |
|
79 | )) | |
|
80 | _alias(urlerr, urllib2, ( | |
|
81 | "HTTPError", | |
|
82 | "URLError", | |
|
83 | )) | |
|
84 | ||
|
85 | except ImportError: | |
|
86 | import urllib.request | |
|
87 | _alias(urlreq, urllib.request, ( | |
|
88 | "AbstractHTTPHandler", | |
|
89 | "addclosehook", | |
|
90 | "addinfourl", | |
|
91 | "BaseHandler", | |
|
92 | "build_opener", | |
|
93 | "FileHandler", | |
|
94 | "FTPHandler", | |
|
95 | "ftpwrapper", | |
|
96 | "HTTPHandler", | |
|
97 | "HTTPSHandler", | |
|
98 | "install_opener", | |
|
99 | "pathname2url", | |
|
100 | "HTTPBasicAuthHandler", | |
|
101 | "HTTPDigestAuthHandler", | |
|
102 | "ProxyHandler", | |
|
103 | "quote", | |
|
104 | "Request", | |
|
105 | "splitattr", | |
|
106 | "splitpasswd", | |
|
107 | "splitport", | |
|
108 | "splituser", | |
|
109 | "unquote", | |
|
110 | "url2pathname", | |
|
111 | "urlopen", | |
|
112 | )) | |
|
113 | import urllib.error | |
|
114 | _alias(urlerr, urllib.error, ( | |
|
115 | "HTTPError", | |
|
116 | "URLError", | |
|
117 | )) | |
|
118 | ||
|
28 | 119 | try: |
|
29 | 120 | xrange |
|
30 | 121 | except NameError: |
General Comments 0
You need to be logged in to leave comments.
Login now