##// END OF EJS Templates
py3: add a bytes version of os.name...
Pulkit Goyal -
r30302:3874ddba default
parent child Browse files
Show More
@@ -1,202 +1,206 b''
1 1 # pycompat.py - portability shim for python 3
2 2 #
3 3 # This software may be used and distributed according to the terms of the
4 4 # GNU General Public License version 2 or any later version.
5 5
6 6 """Mercurial portability shim for python 3.
7 7
8 8 This contains aliases to hide python version-specific details from the core.
9 9 """
10 10
11 11 from __future__ import absolute_import
12 12
13 import os
13 14 import sys
14 15
15 16 ispy3 = (sys.version_info[0] >= 3)
16 17
17 18 if not ispy3:
18 19 import cPickle as pickle
19 20 import cStringIO as io
20 21 import httplib
21 22 import Queue as _queue
22 23 import SocketServer as socketserver
23 24 import urlparse
24 25 import xmlrpclib
25 26 else:
26 27 import http.client as httplib
27 28 import io
28 29 import pickle
29 30 import queue as _queue
30 31 import socketserver
31 32 import urllib.parse as urlparse
32 33 import xmlrpc.client as xmlrpclib
33 34
34 35 if ispy3:
35 36 import builtins
36 37 import functools
37 import os
38 38 fsencode = os.fsencode
39 39 fsdecode = os.fsdecode
40 # A bytes version of os.name.
41 osname = os.name.encode('ascii')
40 42
41 43 def sysstr(s):
42 44 """Return a keyword str to be passed to Python functions such as
43 45 getattr() and str.encode()
44 46
45 47 This never raises UnicodeDecodeError. Non-ascii characters are
46 48 considered invalid and mapped to arbitrary but unique code points
47 49 such that 'sysstr(a) != sysstr(b)' for all 'a != b'.
48 50 """
49 51 if isinstance(s, builtins.str):
50 52 return s
51 53 return s.decode(u'latin-1')
52 54
53 55 def _wrapattrfunc(f):
54 56 @functools.wraps(f)
55 57 def w(object, name, *args):
56 58 return f(object, sysstr(name), *args)
57 59 return w
58 60
59 61 # these wrappers are automagically imported by hgloader
60 62 delattr = _wrapattrfunc(builtins.delattr)
61 63 getattr = _wrapattrfunc(builtins.getattr)
62 64 hasattr = _wrapattrfunc(builtins.hasattr)
63 65 setattr = _wrapattrfunc(builtins.setattr)
64 66 xrange = builtins.range
65 67
66 68 else:
67 69 def sysstr(s):
68 70 return s
69 71
70 72 # Partial backport from os.py in Python 3, which only accepts bytes.
71 73 # In Python 2, our paths should only ever be bytes, a unicode path
72 74 # indicates a bug.
73 75 def fsencode(filename):
74 76 if isinstance(filename, str):
75 77 return filename
76 78 else:
77 79 raise TypeError(
78 80 "expect str, not %s" % type(filename).__name__)
79 81
80 82 # In Python 2, fsdecode() has a very chance to receive bytes. So it's
81 83 # better not to touch Python 2 part as it's already working fine.
82 84 def fsdecode(filename):
83 85 return filename
84 86
87 osname = os.name
88
85 89 stringio = io.StringIO
86 90 empty = _queue.Empty
87 91 queue = _queue.Queue
88 92
89 93 class _pycompatstub(object):
90 94 def __init__(self):
91 95 self._aliases = {}
92 96
93 97 def _registeraliases(self, origin, items):
94 98 """Add items that will be populated at the first access"""
95 99 items = map(sysstr, items)
96 100 self._aliases.update(
97 101 (item.replace(sysstr('_'), sysstr('')).lower(), (origin, item))
98 102 for item in items)
99 103
100 104 def __getattr__(self, name):
101 105 try:
102 106 origin, item = self._aliases[name]
103 107 except KeyError:
104 108 raise AttributeError(name)
105 109 self.__dict__[name] = obj = getattr(origin, item)
106 110 return obj
107 111
108 112 httpserver = _pycompatstub()
109 113 urlreq = _pycompatstub()
110 114 urlerr = _pycompatstub()
111 115 if not ispy3:
112 116 import BaseHTTPServer
113 117 import CGIHTTPServer
114 118 import SimpleHTTPServer
115 119 import urllib2
116 120 import urllib
117 121 urlreq._registeraliases(urllib, (
118 122 "addclosehook",
119 123 "addinfourl",
120 124 "ftpwrapper",
121 125 "pathname2url",
122 126 "quote",
123 127 "splitattr",
124 128 "splitpasswd",
125 129 "splitport",
126 130 "splituser",
127 131 "unquote",
128 132 "url2pathname",
129 133 "urlencode",
130 134 ))
131 135 urlreq._registeraliases(urllib2, (
132 136 "AbstractHTTPHandler",
133 137 "BaseHandler",
134 138 "build_opener",
135 139 "FileHandler",
136 140 "FTPHandler",
137 141 "HTTPBasicAuthHandler",
138 142 "HTTPDigestAuthHandler",
139 143 "HTTPHandler",
140 144 "HTTPPasswordMgrWithDefaultRealm",
141 145 "HTTPSHandler",
142 146 "install_opener",
143 147 "ProxyHandler",
144 148 "Request",
145 149 "urlopen",
146 150 ))
147 151 urlerr._registeraliases(urllib2, (
148 152 "HTTPError",
149 153 "URLError",
150 154 ))
151 155 httpserver._registeraliases(BaseHTTPServer, (
152 156 "HTTPServer",
153 157 "BaseHTTPRequestHandler",
154 158 ))
155 159 httpserver._registeraliases(SimpleHTTPServer, (
156 160 "SimpleHTTPRequestHandler",
157 161 ))
158 162 httpserver._registeraliases(CGIHTTPServer, (
159 163 "CGIHTTPRequestHandler",
160 164 ))
161 165
162 166 else:
163 167 import urllib.request
164 168 urlreq._registeraliases(urllib.request, (
165 169 "AbstractHTTPHandler",
166 170 "addclosehook",
167 171 "addinfourl",
168 172 "BaseHandler",
169 173 "build_opener",
170 174 "FileHandler",
171 175 "FTPHandler",
172 176 "ftpwrapper",
173 177 "HTTPHandler",
174 178 "HTTPSHandler",
175 179 "install_opener",
176 180 "pathname2url",
177 181 "HTTPBasicAuthHandler",
178 182 "HTTPDigestAuthHandler",
179 183 "HTTPPasswordMgrWithDefaultRealm",
180 184 "ProxyHandler",
181 185 "quote",
182 186 "Request",
183 187 "splitattr",
184 188 "splitpasswd",
185 189 "splitport",
186 190 "splituser",
187 191 "unquote",
188 192 "url2pathname",
189 193 "urlopen",
190 194 ))
191 195 import urllib.error
192 196 urlerr._registeraliases(urllib.error, (
193 197 "HTTPError",
194 198 "URLError",
195 199 ))
196 200 import http.server
197 201 httpserver._registeraliases(http.server, (
198 202 "HTTPServer",
199 203 "BaseHTTPRequestHandler",
200 204 "SimpleHTTPRequestHandler",
201 205 "CGIHTTPRequestHandler",
202 206 ))
General Comments 0
You need to be logged in to leave comments. Login now