##// END OF EJS Templates
pycompat: avoid using an extra function...
Pulkit Goyal -
r29779:997e8cf4 default
parent child Browse files
Show More
@@ -1,152 +1,151
1 # pycompat.py - portability shim for python 3
1 # pycompat.py - portability shim for python 3
2 #
2 #
3 # This software may be used and distributed according to the terms of the
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
4 # GNU General Public License version 2 or any later version.
5
5
6 """Mercurial portability shim for python 3.
6 """Mercurial portability shim for python 3.
7
7
8 This contains aliases to hide python version-specific details from the core.
8 This contains aliases to hide python version-specific details from the core.
9 """
9 """
10
10
11 from __future__ import absolute_import
11 from __future__ import absolute_import
12
12
13 import sys
13 import sys
14
14
15 if sys.version_info[0] < 3:
15 if sys.version_info[0] < 3:
16 import cPickle as pickle
16 import cPickle as pickle
17 import cStringIO as io
17 import cStringIO as io
18 import httplib
18 import httplib
19 import Queue as _queue
19 import Queue as _queue
20 import SocketServer as socketserver
20 import SocketServer as socketserver
21 import urlparse
21 import urlparse
22 import xmlrpclib
22 import xmlrpclib
23 else:
23 else:
24 import http.client as httplib
24 import http.client as httplib
25 import io
25 import io
26 import pickle
26 import pickle
27 import queue as _queue
27 import queue as _queue
28 import socketserver
28 import socketserver
29 import urllib.parse as urlparse
29 import urllib.parse as urlparse
30 import xmlrpc.client as xmlrpclib
30 import xmlrpc.client as xmlrpclib
31
31
32 stringio = io.StringIO
32 stringio = io.StringIO
33 empty = _queue.Empty
33 empty = _queue.Empty
34 queue = _queue.Queue
34 queue = _queue.Queue
35
35
36 class _pycompatstub(object):
36 class _pycompatstub(object):
37 pass
37 pass
38
38
39 def _alias(alias, origin, items):
39 def _alias(alias, origin, items):
40 """ populate a _pycompatstub
40 """ populate a _pycompatstub
41
41
42 copies items from origin to alias
42 copies items from origin to alias
43 """
43 """
44 def hgcase(item):
45 return item.replace('_', '').lower()
46 for item in items:
44 for item in items:
47 try:
45 try:
48 setattr(alias, hgcase(item), getattr(origin, item))
46 lcase = item.replace('_', '').lower()
47 setattr(alias, lcase, getattr(origin, item))
49 except AttributeError:
48 except AttributeError:
50 pass
49 pass
51
50
52 httpserver = _pycompatstub()
51 httpserver = _pycompatstub()
53 urlreq = _pycompatstub()
52 urlreq = _pycompatstub()
54 urlerr = _pycompatstub()
53 urlerr = _pycompatstub()
55 try:
54 try:
56 import BaseHTTPServer
55 import BaseHTTPServer
57 import CGIHTTPServer
56 import CGIHTTPServer
58 import SimpleHTTPServer
57 import SimpleHTTPServer
59 import urllib2
58 import urllib2
60 import urllib
59 import urllib
61 _alias(urlreq, urllib, (
60 _alias(urlreq, urllib, (
62 "addclosehook",
61 "addclosehook",
63 "addinfourl",
62 "addinfourl",
64 "ftpwrapper",
63 "ftpwrapper",
65 "pathname2url",
64 "pathname2url",
66 "quote",
65 "quote",
67 "splitattr",
66 "splitattr",
68 "splitpasswd",
67 "splitpasswd",
69 "splitport",
68 "splitport",
70 "splituser",
69 "splituser",
71 "unquote",
70 "unquote",
72 "url2pathname",
71 "url2pathname",
73 "urlencode",
72 "urlencode",
74 ))
73 ))
75 _alias(urlreq, urllib2, (
74 _alias(urlreq, urllib2, (
76 "AbstractHTTPHandler",
75 "AbstractHTTPHandler",
77 "BaseHandler",
76 "BaseHandler",
78 "build_opener",
77 "build_opener",
79 "FileHandler",
78 "FileHandler",
80 "FTPHandler",
79 "FTPHandler",
81 "HTTPBasicAuthHandler",
80 "HTTPBasicAuthHandler",
82 "HTTPDigestAuthHandler",
81 "HTTPDigestAuthHandler",
83 "HTTPHandler",
82 "HTTPHandler",
84 "HTTPPasswordMgrWithDefaultRealm",
83 "HTTPPasswordMgrWithDefaultRealm",
85 "HTTPSHandler",
84 "HTTPSHandler",
86 "install_opener",
85 "install_opener",
87 "ProxyHandler",
86 "ProxyHandler",
88 "Request",
87 "Request",
89 "urlopen",
88 "urlopen",
90 ))
89 ))
91 _alias(urlerr, urllib2, (
90 _alias(urlerr, urllib2, (
92 "HTTPError",
91 "HTTPError",
93 "URLError",
92 "URLError",
94 ))
93 ))
95 _alias(httpserver, BaseHTTPServer, (
94 _alias(httpserver, BaseHTTPServer, (
96 "HTTPServer",
95 "HTTPServer",
97 "BaseHTTPRequestHandler",
96 "BaseHTTPRequestHandler",
98 ))
97 ))
99 _alias(httpserver, SimpleHTTPServer, (
98 _alias(httpserver, SimpleHTTPServer, (
100 "SimpleHTTPRequestHandler",
99 "SimpleHTTPRequestHandler",
101 ))
100 ))
102 _alias(httpserver, CGIHTTPServer, (
101 _alias(httpserver, CGIHTTPServer, (
103 "CGIHTTPRequestHandler",
102 "CGIHTTPRequestHandler",
104 ))
103 ))
105
104
106 except ImportError:
105 except ImportError:
107 import urllib.request
106 import urllib.request
108 _alias(urlreq, urllib.request, (
107 _alias(urlreq, urllib.request, (
109 "AbstractHTTPHandler",
108 "AbstractHTTPHandler",
110 "addclosehook",
109 "addclosehook",
111 "addinfourl",
110 "addinfourl",
112 "BaseHandler",
111 "BaseHandler",
113 "build_opener",
112 "build_opener",
114 "FileHandler",
113 "FileHandler",
115 "FTPHandler",
114 "FTPHandler",
116 "ftpwrapper",
115 "ftpwrapper",
117 "HTTPHandler",
116 "HTTPHandler",
118 "HTTPSHandler",
117 "HTTPSHandler",
119 "install_opener",
118 "install_opener",
120 "pathname2url",
119 "pathname2url",
121 "HTTPBasicAuthHandler",
120 "HTTPBasicAuthHandler",
122 "HTTPDigestAuthHandler",
121 "HTTPDigestAuthHandler",
123 "HTTPPasswordMgrWithDefaultRealm",
122 "HTTPPasswordMgrWithDefaultRealm",
124 "ProxyHandler",
123 "ProxyHandler",
125 "quote",
124 "quote",
126 "Request",
125 "Request",
127 "splitattr",
126 "splitattr",
128 "splitpasswd",
127 "splitpasswd",
129 "splitport",
128 "splitport",
130 "splituser",
129 "splituser",
131 "unquote",
130 "unquote",
132 "url2pathname",
131 "url2pathname",
133 "urlopen",
132 "urlopen",
134 ))
133 ))
135 import urllib.error
134 import urllib.error
136 _alias(urlerr, urllib.error, (
135 _alias(urlerr, urllib.error, (
137 "HTTPError",
136 "HTTPError",
138 "URLError",
137 "URLError",
139 ))
138 ))
140 import http.server
139 import http.server
141 _alias(httpserver, http.server, (
140 _alias(httpserver, http.server, (
142 "HTTPServer",
141 "HTTPServer",
143 "BaseHTTPRequestHandler",
142 "BaseHTTPRequestHandler",
144 "SimpleHTTPRequestHandler",
143 "SimpleHTTPRequestHandler",
145 "CGIHTTPRequestHandler",
144 "CGIHTTPRequestHandler",
146 ))
145 ))
147
146
148 try:
147 try:
149 xrange
148 xrange
150 except NameError:
149 except NameError:
151 import builtins
150 import builtins
152 builtins.xrange = range
151 builtins.xrange = range
General Comments 0
You need to be logged in to leave comments. Login now