##// END OF EJS Templates
pycompat: make pycompat demandimport friendly...
Pulkit Goyal -
r29584:06587edd default
parent child Browse files
Show More
@@ -1,180 +1,153 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 try:
13 import sys
14
15 if sys.version_info[0] < 3:
14 import cPickle as pickle
16 import cPickle as pickle
15 pickle.dumps
17 import cStringIO as io
16 except ImportError:
17 import pickle
18 pickle.dumps # silence pyflakes
19
20 try:
21 import httplib
18 import httplib
22 httplib.HTTPException
19 import Queue as _queue
23 except ImportError:
24 import http.client as httplib
25 httplib.HTTPException
26
27 try:
28 import SocketServer as socketserver
20 import SocketServer as socketserver
29 socketserver.ThreadingMixIn
21 import urlparse
30 except ImportError:
31 import socketserver
32 socketserver.ThreadingMixIn
33
34 try:
35 import xmlrpclib
22 import xmlrpclib
36 xmlrpclib.Transport
23 else:
37 except ImportError:
24 import http.client as httplib
25 import io
26 import pickle
27 import queue as _queue
28 import socketserver
29 import urllib.parse as urlparse
38 import xmlrpc.client as xmlrpclib
30 import xmlrpc.client as xmlrpclib
39 xmlrpclib.Transport
40
41 try:
42 import urlparse
43 urlparse.urlparse
44 except ImportError:
45 import urllib.parse as urlparse
46 urlparse.urlparse
47
31
48 try:
32 stringio = io.StringIO
49 import cStringIO as io
50 stringio = io.StringIO
51 except ImportError:
52 import io
53 stringio = io.StringIO
54
55 try:
56 import Queue as _queue
57 _queue.Queue
58 except ImportError:
59 import queue as _queue
60 empty = _queue.Empty
33 empty = _queue.Empty
61 queue = _queue.Queue
34 queue = _queue.Queue
62
35
63 class _pycompatstub(object):
36 class _pycompatstub(object):
64 pass
37 pass
65
38
66 def _alias(alias, origin, items):
39 def _alias(alias, origin, items):
67 """ populate a _pycompatstub
40 """ populate a _pycompatstub
68
41
69 copies items from origin to alias
42 copies items from origin to alias
70 """
43 """
71 def hgcase(item):
44 def hgcase(item):
72 return item.replace('_', '').lower()
45 return item.replace('_', '').lower()
73 for item in items:
46 for item in items:
74 try:
47 try:
75 setattr(alias, hgcase(item), getattr(origin, item))
48 setattr(alias, hgcase(item), getattr(origin, item))
76 except AttributeError:
49 except AttributeError:
77 pass
50 pass
78
51
79 httpserver = _pycompatstub()
52 httpserver = _pycompatstub()
80 urlreq = _pycompatstub()
53 urlreq = _pycompatstub()
81 urlerr = _pycompatstub()
54 urlerr = _pycompatstub()
82 try:
55 try:
83 import BaseHTTPServer
56 import BaseHTTPServer
84 import CGIHTTPServer
57 import CGIHTTPServer
85 import SimpleHTTPServer
58 import SimpleHTTPServer
86 import urllib2
59 import urllib2
87 import urllib
60 import urllib
88 _alias(urlreq, urllib, (
61 _alias(urlreq, urllib, (
89 "addclosehook",
62 "addclosehook",
90 "addinfourl",
63 "addinfourl",
91 "ftpwrapper",
64 "ftpwrapper",
92 "pathname2url",
65 "pathname2url",
93 "quote",
66 "quote",
94 "splitattr",
67 "splitattr",
95 "splitpasswd",
68 "splitpasswd",
96 "splitport",
69 "splitport",
97 "splituser",
70 "splituser",
98 "unquote",
71 "unquote",
99 "url2pathname",
72 "url2pathname",
100 "urlencode",
73 "urlencode",
101 "urlencode",
74 "urlencode",
102 ))
75 ))
103 _alias(urlreq, urllib2, (
76 _alias(urlreq, urllib2, (
104 "AbstractHTTPHandler",
77 "AbstractHTTPHandler",
105 "BaseHandler",
78 "BaseHandler",
106 "build_opener",
79 "build_opener",
107 "FileHandler",
80 "FileHandler",
108 "FTPHandler",
81 "FTPHandler",
109 "HTTPBasicAuthHandler",
82 "HTTPBasicAuthHandler",
110 "HTTPDigestAuthHandler",
83 "HTTPDigestAuthHandler",
111 "HTTPHandler",
84 "HTTPHandler",
112 "HTTPPasswordMgrWithDefaultRealm",
85 "HTTPPasswordMgrWithDefaultRealm",
113 "HTTPSHandler",
86 "HTTPSHandler",
114 "install_opener",
87 "install_opener",
115 "ProxyHandler",
88 "ProxyHandler",
116 "Request",
89 "Request",
117 "urlopen",
90 "urlopen",
118 ))
91 ))
119 _alias(urlerr, urllib2, (
92 _alias(urlerr, urllib2, (
120 "HTTPError",
93 "HTTPError",
121 "URLError",
94 "URLError",
122 ))
95 ))
123 _alias(httpserver, BaseHTTPServer, (
96 _alias(httpserver, BaseHTTPServer, (
124 "HTTPServer",
97 "HTTPServer",
125 "BaseHTTPRequestHandler",
98 "BaseHTTPRequestHandler",
126 ))
99 ))
127 _alias(httpserver, SimpleHTTPServer, (
100 _alias(httpserver, SimpleHTTPServer, (
128 "SimpleHTTPRequestHandler",
101 "SimpleHTTPRequestHandler",
129 ))
102 ))
130 _alias(httpserver, CGIHTTPServer, (
103 _alias(httpserver, CGIHTTPServer, (
131 "CGIHTTPRequestHandler",
104 "CGIHTTPRequestHandler",
132 ))
105 ))
133
106
134 except ImportError:
107 except ImportError:
135 import urllib.request
108 import urllib.request
136 _alias(urlreq, urllib.request, (
109 _alias(urlreq, urllib.request, (
137 "AbstractHTTPHandler",
110 "AbstractHTTPHandler",
138 "addclosehook",
111 "addclosehook",
139 "addinfourl",
112 "addinfourl",
140 "BaseHandler",
113 "BaseHandler",
141 "build_opener",
114 "build_opener",
142 "FileHandler",
115 "FileHandler",
143 "FTPHandler",
116 "FTPHandler",
144 "ftpwrapper",
117 "ftpwrapper",
145 "HTTPHandler",
118 "HTTPHandler",
146 "HTTPSHandler",
119 "HTTPSHandler",
147 "install_opener",
120 "install_opener",
148 "pathname2url",
121 "pathname2url",
149 "HTTPBasicAuthHandler",
122 "HTTPBasicAuthHandler",
150 "HTTPDigestAuthHandler",
123 "HTTPDigestAuthHandler",
151 "HTTPPasswordMgrWithDefaultRealm",
124 "HTTPPasswordMgrWithDefaultRealm",
152 "ProxyHandler",
125 "ProxyHandler",
153 "quote",
126 "quote",
154 "Request",
127 "Request",
155 "splitattr",
128 "splitattr",
156 "splitpasswd",
129 "splitpasswd",
157 "splitport",
130 "splitport",
158 "splituser",
131 "splituser",
159 "unquote",
132 "unquote",
160 "url2pathname",
133 "url2pathname",
161 "urlopen",
134 "urlopen",
162 ))
135 ))
163 import urllib.error
136 import urllib.error
164 _alias(urlerr, urllib.error, (
137 _alias(urlerr, urllib.error, (
165 "HTTPError",
138 "HTTPError",
166 "URLError",
139 "URLError",
167 ))
140 ))
168 import http.server
141 import http.server
169 _alias(httpserver, http.server, (
142 _alias(httpserver, http.server, (
170 "HTTPServer",
143 "HTTPServer",
171 "BaseHTTPRequestHandler",
144 "BaseHTTPRequestHandler",
172 "SimpleHTTPRequestHandler",
145 "SimpleHTTPRequestHandler",
173 "CGIHTTPRequestHandler",
146 "CGIHTTPRequestHandler",
174 ))
147 ))
175
148
176 try:
149 try:
177 xrange
150 xrange
178 except NameError:
151 except NameError:
179 import builtins
152 import builtins
180 builtins.xrange = range
153 builtins.xrange = range
@@ -1,13 +1,15 b''
1 #require test-repo pyflakes hg10
1 #require test-repo pyflakes hg10
2
2
3 $ . "$TESTDIR/helpers-testrepo.sh"
3 $ . "$TESTDIR/helpers-testrepo.sh"
4 $ cd "`dirname "$TESTDIR"`"
4 $ cd "`dirname "$TESTDIR"`"
5
5
6 run pyflakes on all tracked files ending in .py or without a file ending
6 run pyflakes on all tracked files ending in .py or without a file ending
7 (skipping binary file random-seed)
7 (skipping binary file random-seed)
8
8
9 $ hg locate 'set:**.py or grep("^#!.*python")' 2>/dev/null \
9 $ hg locate 'set:**.py or grep("^#!.*python")' \
10 > -X mercurial/pycompat.py \
11 > 2>/dev/null \
10 > | xargs pyflakes 2>/dev/null | "$TESTDIR/filterpyflakes.py"
12 > | xargs pyflakes 2>/dev/null | "$TESTDIR/filterpyflakes.py"
11 tests/filterpyflakes.py:61: undefined name 'undefinedname'
13 tests/filterpyflakes.py:61: undefined name 'undefinedname'
12
14
13
15
General Comments 0
You need to be logged in to leave comments. Login now