comparison mercurial/pycompat.py @ 29812:c63ab0524db7

pycompat: delay loading modules registered to stub Replacement _pycompatstub designed to be compatible with our demandimporter. try-except is replaced by version comparison because ImportError will no longer be raised immediately.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 14 Aug 2016 14:46:24 +0900
parents 178c89e8519a
children 0f6d6fdd3c2a
comparison
equal deleted inserted replaced
29811:178c89e8519a 29812:c63ab0524db7
51 stringio = io.StringIO 51 stringio = io.StringIO
52 empty = _queue.Empty 52 empty = _queue.Empty
53 queue = _queue.Queue 53 queue = _queue.Queue
54 54
55 class _pycompatstub(object): 55 class _pycompatstub(object):
56 pass 56 def __init__(self):
57 self._aliases = {}
57 58
58 def _alias(alias, origin, items): 59 def _registeraliases(self, origin, items):
59 """ populate a _pycompatstub 60 """Add items that will be populated at the first access"""
61 self._aliases.update((item.replace('_', '').lower(), (origin, item))
62 for item in items)
60 63
61 copies items from origin to alias 64 def __getattr__(self, name):
62 """
63 for item in items:
64 try: 65 try:
65 lcase = item.replace('_', '').lower() 66 origin, item = self._aliases[name]
66 setattr(alias, lcase, getattr(origin, item)) 67 except KeyError:
67 except AttributeError: 68 raise AttributeError(name)
68 pass 69 self.__dict__[name] = obj = getattr(origin, item)
70 return obj
69 71
70 httpserver = _pycompatstub() 72 httpserver = _pycompatstub()
71 urlreq = _pycompatstub() 73 urlreq = _pycompatstub()
72 urlerr = _pycompatstub() 74 urlerr = _pycompatstub()
73 try: 75 if sys.version_info[0] < 3:
74 import BaseHTTPServer 76 import BaseHTTPServer
75 import CGIHTTPServer 77 import CGIHTTPServer
76 import SimpleHTTPServer 78 import SimpleHTTPServer
77 import urllib2 79 import urllib2
78 import urllib 80 import urllib
79 _alias(urlreq, urllib, ( 81 urlreq._registeraliases(urllib, (
80 "addclosehook", 82 "addclosehook",
81 "addinfourl", 83 "addinfourl",
82 "ftpwrapper", 84 "ftpwrapper",
83 "pathname2url", 85 "pathname2url",
84 "quote", 86 "quote",
88 "splituser", 90 "splituser",
89 "unquote", 91 "unquote",
90 "url2pathname", 92 "url2pathname",
91 "urlencode", 93 "urlencode",
92 )) 94 ))
93 _alias(urlreq, urllib2, ( 95 urlreq._registeraliases(urllib2, (
94 "AbstractHTTPHandler", 96 "AbstractHTTPHandler",
95 "BaseHandler", 97 "BaseHandler",
96 "build_opener", 98 "build_opener",
97 "FileHandler", 99 "FileHandler",
98 "FTPHandler", 100 "FTPHandler",
104 "install_opener", 106 "install_opener",
105 "ProxyHandler", 107 "ProxyHandler",
106 "Request", 108 "Request",
107 "urlopen", 109 "urlopen",
108 )) 110 ))
109 _alias(urlerr, urllib2, ( 111 urlerr._registeraliases(urllib2, (
110 "HTTPError", 112 "HTTPError",
111 "URLError", 113 "URLError",
112 )) 114 ))
113 _alias(httpserver, BaseHTTPServer, ( 115 httpserver._registeraliases(BaseHTTPServer, (
114 "HTTPServer", 116 "HTTPServer",
115 "BaseHTTPRequestHandler", 117 "BaseHTTPRequestHandler",
116 )) 118 ))
117 _alias(httpserver, SimpleHTTPServer, ( 119 httpserver._registeraliases(SimpleHTTPServer, (
118 "SimpleHTTPRequestHandler", 120 "SimpleHTTPRequestHandler",
119 )) 121 ))
120 _alias(httpserver, CGIHTTPServer, ( 122 httpserver._registeraliases(CGIHTTPServer, (
121 "CGIHTTPRequestHandler", 123 "CGIHTTPRequestHandler",
122 )) 124 ))
123 125
124 except ImportError: 126 else:
125 import urllib.request 127 import urllib.request
126 _alias(urlreq, urllib.request, ( 128 urlreq._registeraliases(urllib.request, (
127 "AbstractHTTPHandler", 129 "AbstractHTTPHandler",
128 "addclosehook", 130 "addclosehook",
129 "addinfourl", 131 "addinfourl",
130 "BaseHandler", 132 "BaseHandler",
131 "build_opener", 133 "build_opener",
149 "unquote", 151 "unquote",
150 "url2pathname", 152 "url2pathname",
151 "urlopen", 153 "urlopen",
152 )) 154 ))
153 import urllib.error 155 import urllib.error
154 _alias(urlerr, urllib.error, ( 156 urlerr._registeraliases(urllib.error, (
155 "HTTPError", 157 "HTTPError",
156 "URLError", 158 "URLError",
157 )) 159 ))
158 import http.server 160 import http.server
159 _alias(httpserver, http.server, ( 161 httpserver._registeraliases(http.server, (
160 "HTTPServer", 162 "HTTPServer",
161 "BaseHTTPRequestHandler", 163 "BaseHTTPRequestHandler",
162 "SimpleHTTPRequestHandler", 164 "SimpleHTTPRequestHandler",
163 "CGIHTTPRequestHandler", 165 "CGIHTTPRequestHandler",
164 )) 166 ))