##// 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 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 13 import sys
14 14
15 15 if sys.version_info[0] < 3:
16 16 import cPickle as pickle
17 17 import cStringIO as io
18 18 import httplib
19 19 import Queue as _queue
20 20 import SocketServer as socketserver
21 21 import urlparse
22 22 import xmlrpclib
23 23 else:
24 24 import http.client as httplib
25 25 import io
26 26 import pickle
27 27 import queue as _queue
28 28 import socketserver
29 29 import urllib.parse as urlparse
30 30 import xmlrpc.client as xmlrpclib
31 31
32 32 stringio = io.StringIO
33 33 empty = _queue.Empty
34 34 queue = _queue.Queue
35 35
36 36 class _pycompatstub(object):
37 37 pass
38 38
39 39 def _alias(alias, origin, items):
40 40 """ populate a _pycompatstub
41 41
42 42 copies items from origin to alias
43 43 """
44 def hgcase(item):
45 return item.replace('_', '').lower()
46 44 for item in items:
47 45 try:
48 setattr(alias, hgcase(item), getattr(origin, item))
46 lcase = item.replace('_', '').lower()
47 setattr(alias, lcase, getattr(origin, item))
49 48 except AttributeError:
50 49 pass
51 50
52 51 httpserver = _pycompatstub()
53 52 urlreq = _pycompatstub()
54 53 urlerr = _pycompatstub()
55 54 try:
56 55 import BaseHTTPServer
57 56 import CGIHTTPServer
58 57 import SimpleHTTPServer
59 58 import urllib2
60 59 import urllib
61 60 _alias(urlreq, urllib, (
62 61 "addclosehook",
63 62 "addinfourl",
64 63 "ftpwrapper",
65 64 "pathname2url",
66 65 "quote",
67 66 "splitattr",
68 67 "splitpasswd",
69 68 "splitport",
70 69 "splituser",
71 70 "unquote",
72 71 "url2pathname",
73 72 "urlencode",
74 73 ))
75 74 _alias(urlreq, urllib2, (
76 75 "AbstractHTTPHandler",
77 76 "BaseHandler",
78 77 "build_opener",
79 78 "FileHandler",
80 79 "FTPHandler",
81 80 "HTTPBasicAuthHandler",
82 81 "HTTPDigestAuthHandler",
83 82 "HTTPHandler",
84 83 "HTTPPasswordMgrWithDefaultRealm",
85 84 "HTTPSHandler",
86 85 "install_opener",
87 86 "ProxyHandler",
88 87 "Request",
89 88 "urlopen",
90 89 ))
91 90 _alias(urlerr, urllib2, (
92 91 "HTTPError",
93 92 "URLError",
94 93 ))
95 94 _alias(httpserver, BaseHTTPServer, (
96 95 "HTTPServer",
97 96 "BaseHTTPRequestHandler",
98 97 ))
99 98 _alias(httpserver, SimpleHTTPServer, (
100 99 "SimpleHTTPRequestHandler",
101 100 ))
102 101 _alias(httpserver, CGIHTTPServer, (
103 102 "CGIHTTPRequestHandler",
104 103 ))
105 104
106 105 except ImportError:
107 106 import urllib.request
108 107 _alias(urlreq, urllib.request, (
109 108 "AbstractHTTPHandler",
110 109 "addclosehook",
111 110 "addinfourl",
112 111 "BaseHandler",
113 112 "build_opener",
114 113 "FileHandler",
115 114 "FTPHandler",
116 115 "ftpwrapper",
117 116 "HTTPHandler",
118 117 "HTTPSHandler",
119 118 "install_opener",
120 119 "pathname2url",
121 120 "HTTPBasicAuthHandler",
122 121 "HTTPDigestAuthHandler",
123 122 "HTTPPasswordMgrWithDefaultRealm",
124 123 "ProxyHandler",
125 124 "quote",
126 125 "Request",
127 126 "splitattr",
128 127 "splitpasswd",
129 128 "splitport",
130 129 "splituser",
131 130 "unquote",
132 131 "url2pathname",
133 132 "urlopen",
134 133 ))
135 134 import urllib.error
136 135 _alias(urlerr, urllib.error, (
137 136 "HTTPError",
138 137 "URLError",
139 138 ))
140 139 import http.server
141 140 _alias(httpserver, http.server, (
142 141 "HTTPServer",
143 142 "BaseHTTPRequestHandler",
144 143 "SimpleHTTPRequestHandler",
145 144 "CGIHTTPRequestHandler",
146 145 ))
147 146
148 147 try:
149 148 xrange
150 149 except NameError:
151 150 import builtins
152 151 builtins.xrange = range
General Comments 0
You need to be logged in to leave comments. Login now