##// END OF EJS Templates
py3: provide (del|get|has|set)attr wrappers that accepts bytes...
Yuya Nishihara -
r29799:45fa8de4 default
parent child Browse files
Show More
@@ -1,149 +1,163 b''
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 if sys.version_info[0] >= 3:
32 if sys.version_info[0] >= 3:
33 import builtins
33 import builtins
34 import functools
34 builtins.xrange = range
35 builtins.xrange = range
35
36
37 def _wrapattrfunc(f):
38 @functools.wraps(f)
39 def w(object, name, *args):
40 if isinstance(name, bytes):
41 name = name.decode(u'utf-8')
42 return f(object, name, *args)
43 return w
44
45 delattr = _wrapattrfunc(builtins.delattr)
46 getattr = _wrapattrfunc(builtins.getattr)
47 hasattr = _wrapattrfunc(builtins.hasattr)
48 setattr = _wrapattrfunc(builtins.setattr)
49
36 stringio = io.StringIO
50 stringio = io.StringIO
37 empty = _queue.Empty
51 empty = _queue.Empty
38 queue = _queue.Queue
52 queue = _queue.Queue
39
53
40 class _pycompatstub(object):
54 class _pycompatstub(object):
41 pass
55 pass
42
56
43 def _alias(alias, origin, items):
57 def _alias(alias, origin, items):
44 """ populate a _pycompatstub
58 """ populate a _pycompatstub
45
59
46 copies items from origin to alias
60 copies items from origin to alias
47 """
61 """
48 for item in items:
62 for item in items:
49 try:
63 try:
50 lcase = item.replace('_', '').lower()
64 lcase = item.replace('_', '').lower()
51 setattr(alias, lcase, getattr(origin, item))
65 setattr(alias, lcase, getattr(origin, item))
52 except AttributeError:
66 except AttributeError:
53 pass
67 pass
54
68
55 httpserver = _pycompatstub()
69 httpserver = _pycompatstub()
56 urlreq = _pycompatstub()
70 urlreq = _pycompatstub()
57 urlerr = _pycompatstub()
71 urlerr = _pycompatstub()
58 try:
72 try:
59 import BaseHTTPServer
73 import BaseHTTPServer
60 import CGIHTTPServer
74 import CGIHTTPServer
61 import SimpleHTTPServer
75 import SimpleHTTPServer
62 import urllib2
76 import urllib2
63 import urllib
77 import urllib
64 _alias(urlreq, urllib, (
78 _alias(urlreq, urllib, (
65 "addclosehook",
79 "addclosehook",
66 "addinfourl",
80 "addinfourl",
67 "ftpwrapper",
81 "ftpwrapper",
68 "pathname2url",
82 "pathname2url",
69 "quote",
83 "quote",
70 "splitattr",
84 "splitattr",
71 "splitpasswd",
85 "splitpasswd",
72 "splitport",
86 "splitport",
73 "splituser",
87 "splituser",
74 "unquote",
88 "unquote",
75 "url2pathname",
89 "url2pathname",
76 "urlencode",
90 "urlencode",
77 ))
91 ))
78 _alias(urlreq, urllib2, (
92 _alias(urlreq, urllib2, (
79 "AbstractHTTPHandler",
93 "AbstractHTTPHandler",
80 "BaseHandler",
94 "BaseHandler",
81 "build_opener",
95 "build_opener",
82 "FileHandler",
96 "FileHandler",
83 "FTPHandler",
97 "FTPHandler",
84 "HTTPBasicAuthHandler",
98 "HTTPBasicAuthHandler",
85 "HTTPDigestAuthHandler",
99 "HTTPDigestAuthHandler",
86 "HTTPHandler",
100 "HTTPHandler",
87 "HTTPPasswordMgrWithDefaultRealm",
101 "HTTPPasswordMgrWithDefaultRealm",
88 "HTTPSHandler",
102 "HTTPSHandler",
89 "install_opener",
103 "install_opener",
90 "ProxyHandler",
104 "ProxyHandler",
91 "Request",
105 "Request",
92 "urlopen",
106 "urlopen",
93 ))
107 ))
94 _alias(urlerr, urllib2, (
108 _alias(urlerr, urllib2, (
95 "HTTPError",
109 "HTTPError",
96 "URLError",
110 "URLError",
97 ))
111 ))
98 _alias(httpserver, BaseHTTPServer, (
112 _alias(httpserver, BaseHTTPServer, (
99 "HTTPServer",
113 "HTTPServer",
100 "BaseHTTPRequestHandler",
114 "BaseHTTPRequestHandler",
101 ))
115 ))
102 _alias(httpserver, SimpleHTTPServer, (
116 _alias(httpserver, SimpleHTTPServer, (
103 "SimpleHTTPRequestHandler",
117 "SimpleHTTPRequestHandler",
104 ))
118 ))
105 _alias(httpserver, CGIHTTPServer, (
119 _alias(httpserver, CGIHTTPServer, (
106 "CGIHTTPRequestHandler",
120 "CGIHTTPRequestHandler",
107 ))
121 ))
108
122
109 except ImportError:
123 except ImportError:
110 import urllib.request
124 import urllib.request
111 _alias(urlreq, urllib.request, (
125 _alias(urlreq, urllib.request, (
112 "AbstractHTTPHandler",
126 "AbstractHTTPHandler",
113 "addclosehook",
127 "addclosehook",
114 "addinfourl",
128 "addinfourl",
115 "BaseHandler",
129 "BaseHandler",
116 "build_opener",
130 "build_opener",
117 "FileHandler",
131 "FileHandler",
118 "FTPHandler",
132 "FTPHandler",
119 "ftpwrapper",
133 "ftpwrapper",
120 "HTTPHandler",
134 "HTTPHandler",
121 "HTTPSHandler",
135 "HTTPSHandler",
122 "install_opener",
136 "install_opener",
123 "pathname2url",
137 "pathname2url",
124 "HTTPBasicAuthHandler",
138 "HTTPBasicAuthHandler",
125 "HTTPDigestAuthHandler",
139 "HTTPDigestAuthHandler",
126 "HTTPPasswordMgrWithDefaultRealm",
140 "HTTPPasswordMgrWithDefaultRealm",
127 "ProxyHandler",
141 "ProxyHandler",
128 "quote",
142 "quote",
129 "Request",
143 "Request",
130 "splitattr",
144 "splitattr",
131 "splitpasswd",
145 "splitpasswd",
132 "splitport",
146 "splitport",
133 "splituser",
147 "splituser",
134 "unquote",
148 "unquote",
135 "url2pathname",
149 "url2pathname",
136 "urlopen",
150 "urlopen",
137 ))
151 ))
138 import urllib.error
152 import urllib.error
139 _alias(urlerr, urllib.error, (
153 _alias(urlerr, urllib.error, (
140 "HTTPError",
154 "HTTPError",
141 "URLError",
155 "URLError",
142 ))
156 ))
143 import http.server
157 import http.server
144 _alias(httpserver, http.server, (
158 _alias(httpserver, http.server, (
145 "HTTPServer",
159 "HTTPServer",
146 "BaseHTTPRequestHandler",
160 "BaseHTTPRequestHandler",
147 "SimpleHTTPRequestHandler",
161 "SimpleHTTPRequestHandler",
148 "CGIHTTPRequestHandler",
162 "CGIHTTPRequestHandler",
149 ))
163 ))
General Comments 0
You need to be logged in to leave comments. Login now