six.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. # Copyright (c) 2010-2020 Benjamin Peterson
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in all
  11. # copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. """Utilities for writing code that runs on Python 2 and 3"""
  21. from __future__ import absolute_import
  22. import functools
  23. import itertools
  24. import operator
  25. import sys
  26. import types
  27. __author__ = "Benjamin Peterson <benjamin@python.org>"
  28. __version__ = "1.16.0"
  29. # Useful for very coarse version differentiation.
  30. PY2 = sys.version_info[0] == 2
  31. PY3 = sys.version_info[0] == 3
  32. PY34 = sys.version_info[0:2] >= (3, 4)
  33. if PY3:
  34. string_types = (str,)
  35. integer_types = (int,)
  36. class_types = (type,)
  37. text_type = str
  38. binary_type = bytes
  39. MAXSIZE = sys.maxsize
  40. else:
  41. string_types = (basestring,)
  42. integer_types = (int, long)
  43. class_types = (type, types.ClassType)
  44. text_type = unicode
  45. binary_type = str
  46. if sys.platform.startswith("java"):
  47. # Jython always uses 32 bits.
  48. MAXSIZE = int((1 << 31) - 1)
  49. else:
  50. # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
  51. class X(object):
  52. def __len__(self):
  53. return 1 << 31
  54. try:
  55. len(X())
  56. except OverflowError:
  57. # 32-bit
  58. MAXSIZE = int((1 << 31) - 1)
  59. else:
  60. # 64-bit
  61. MAXSIZE = int((1 << 63) - 1)
  62. del X
  63. if PY34:
  64. from importlib.util import spec_from_loader
  65. else:
  66. spec_from_loader = None
  67. def _add_doc(func, doc):
  68. """Add documentation to a function."""
  69. func.__doc__ = doc
  70. def _import_module(name):
  71. """Import module, returning the module after the last dot."""
  72. __import__(name)
  73. return sys.modules[name]
  74. class _LazyDescr(object):
  75. def __init__(self, name):
  76. self.name = name
  77. def __get__(self, obj, tp):
  78. result = self._resolve()
  79. setattr(obj, self.name, result) # Invokes __set__.
  80. try:
  81. # This is a bit ugly, but it avoids running this again by
  82. # removing this descriptor.
  83. delattr(obj.__class__, self.name)
  84. except AttributeError:
  85. pass
  86. return result
  87. class MovedModule(_LazyDescr):
  88. def __init__(self, name, old, new=None):
  89. super(MovedModule, self).__init__(name)
  90. if PY3:
  91. if new is None:
  92. new = name
  93. self.mod = new
  94. else:
  95. self.mod = old
  96. def _resolve(self):
  97. return _import_module(self.mod)
  98. def __getattr__(self, attr):
  99. _module = self._resolve()
  100. value = getattr(_module, attr)
  101. setattr(self, attr, value)
  102. return value
  103. class _LazyModule(types.ModuleType):
  104. def __init__(self, name):
  105. super(_LazyModule, self).__init__(name)
  106. self.__doc__ = self.__class__.__doc__
  107. def __dir__(self):
  108. attrs = ["__doc__", "__name__"]
  109. attrs += [attr.name for attr in self._moved_attributes]
  110. return attrs
  111. # Subclasses should override this
  112. _moved_attributes = []
  113. class MovedAttribute(_LazyDescr):
  114. def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
  115. super(MovedAttribute, self).__init__(name)
  116. if PY3:
  117. if new_mod is None:
  118. new_mod = name
  119. self.mod = new_mod
  120. if new_attr is None:
  121. if old_attr is None:
  122. new_attr = name
  123. else:
  124. new_attr = old_attr
  125. self.attr = new_attr
  126. else:
  127. self.mod = old_mod
  128. if old_attr is None:
  129. old_attr = name
  130. self.attr = old_attr
  131. def _resolve(self):
  132. module = _import_module(self.mod)
  133. return getattr(module, self.attr)
  134. class _SixMetaPathImporter(object):
  135. """
  136. A meta path importer to import six.moves and its submodules.
  137. This class implements a PEP302 finder and loader. It should be compatible
  138. with Python 2.5 and all existing versions of Python3
  139. """
  140. def __init__(self, six_module_name):
  141. self.name = six_module_name
  142. self.known_modules = {}
  143. def _add_module(self, mod, *fullnames):
  144. for fullname in fullnames:
  145. self.known_modules[self.name + "." + fullname] = mod
  146. def _get_module(self, fullname):
  147. return self.known_modules[self.name + "." + fullname]
  148. def find_module(self, fullname, path=None):
  149. if fullname in self.known_modules:
  150. return self
  151. return None
  152. def find_spec(self, fullname, path, target=None):
  153. if fullname in self.known_modules:
  154. return spec_from_loader(fullname, self)
  155. return None
  156. def __get_module(self, fullname):
  157. try:
  158. return self.known_modules[fullname]
  159. except KeyError:
  160. raise ImportError("This loader does not know module " + fullname)
  161. def load_module(self, fullname):
  162. try:
  163. # in case of a reload
  164. return sys.modules[fullname]
  165. except KeyError:
  166. pass
  167. mod = self.__get_module(fullname)
  168. if isinstance(mod, MovedModule):
  169. mod = mod._resolve()
  170. else:
  171. mod.__loader__ = self
  172. sys.modules[fullname] = mod
  173. return mod
  174. def is_package(self, fullname):
  175. """
  176. Return true, if the named module is a package.
  177. We need this method to get correct spec objects with
  178. Python 3.4 (see PEP451)
  179. """
  180. return hasattr(self.__get_module(fullname), "__path__")
  181. def get_code(self, fullname):
  182. """Return None
  183. Required, if is_package is implemented"""
  184. self.__get_module(fullname) # eventually raises ImportError
  185. return None
  186. get_source = get_code # same as get_code
  187. def create_module(self, spec):
  188. return self.load_module(spec.name)
  189. def exec_module(self, module):
  190. pass
  191. _importer = _SixMetaPathImporter(__name__)
  192. class _MovedItems(_LazyModule):
  193. """Lazy loading of moved objects"""
  194. __path__ = [] # mark as package
  195. _moved_attributes = [
  196. MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
  197. MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
  198. MovedAttribute(
  199. "filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"
  200. ),
  201. MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
  202. MovedAttribute("intern", "__builtin__", "sys"),
  203. MovedAttribute("map", "itertools", "builtins", "imap", "map"),
  204. MovedAttribute("getcwd", "os", "os", "getcwdu", "getcwd"),
  205. MovedAttribute("getcwdb", "os", "os", "getcwd", "getcwdb"),
  206. MovedAttribute("getoutput", "commands", "subprocess"),
  207. MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
  208. MovedAttribute(
  209. "reload_module", "__builtin__", "importlib" if PY34 else "imp", "reload"
  210. ),
  211. MovedAttribute("reduce", "__builtin__", "functools"),
  212. MovedAttribute("shlex_quote", "pipes", "shlex", "quote"),
  213. MovedAttribute("StringIO", "StringIO", "io"),
  214. MovedAttribute("UserDict", "UserDict", "collections"),
  215. MovedAttribute("UserList", "UserList", "collections"),
  216. MovedAttribute("UserString", "UserString", "collections"),
  217. MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
  218. MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
  219. MovedAttribute(
  220. "zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"
  221. ),
  222. MovedModule("builtins", "__builtin__"),
  223. MovedModule("configparser", "ConfigParser"),
  224. MovedModule(
  225. "collections_abc",
  226. "collections",
  227. "collections.abc" if sys.version_info >= (3, 3) else "collections",
  228. ),
  229. MovedModule("copyreg", "copy_reg"),
  230. MovedModule("dbm_gnu", "gdbm", "dbm.gnu"),
  231. MovedModule("dbm_ndbm", "dbm", "dbm.ndbm"),
  232. MovedModule(
  233. "_dummy_thread",
  234. "dummy_thread",
  235. "_dummy_thread" if sys.version_info < (3, 9) else "_thread",
  236. ),
  237. MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
  238. MovedModule("http_cookies", "Cookie", "http.cookies"),
  239. MovedModule("html_entities", "htmlentitydefs", "html.entities"),
  240. MovedModule("html_parser", "HTMLParser", "html.parser"),
  241. MovedModule("http_client", "httplib", "http.client"),
  242. MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
  243. MovedModule("email_mime_image", "email.MIMEImage", "email.mime.image"),
  244. MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
  245. MovedModule(
  246. "email_mime_nonmultipart", "email.MIMENonMultipart", "email.mime.nonmultipart"
  247. ),
  248. MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
  249. MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
  250. MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
  251. MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
  252. MovedModule("cPickle", "cPickle", "pickle"),
  253. MovedModule("queue", "Queue"),
  254. MovedModule("reprlib", "repr"),
  255. MovedModule("socketserver", "SocketServer"),
  256. MovedModule("_thread", "thread", "_thread"),
  257. MovedModule("tkinter", "Tkinter"),
  258. MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
  259. MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
  260. MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
  261. MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
  262. MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
  263. MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"),
  264. MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
  265. MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
  266. MovedModule("tkinter_colorchooser", "tkColorChooser", "tkinter.colorchooser"),
  267. MovedModule("tkinter_commondialog", "tkCommonDialog", "tkinter.commondialog"),
  268. MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
  269. MovedModule("tkinter_font", "tkFont", "tkinter.font"),
  270. MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
  271. MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", "tkinter.simpledialog"),
  272. MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"),
  273. MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"),
  274. MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"),
  275. MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
  276. MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"),
  277. MovedModule("xmlrpc_server", "SimpleXMLRPCServer", "xmlrpc.server"),
  278. ]
  279. # Add windows specific modules.
  280. if sys.platform == "win32":
  281. _moved_attributes += [
  282. MovedModule("winreg", "_winreg"),
  283. ]
  284. for attr in _moved_attributes:
  285. setattr(_MovedItems, attr.name, attr)
  286. if isinstance(attr, MovedModule):
  287. _importer._add_module(attr, "moves." + attr.name)
  288. del attr
  289. _MovedItems._moved_attributes = _moved_attributes
  290. moves = _MovedItems(__name__ + ".moves")
  291. _importer._add_module(moves, "moves")
  292. class Module_six_moves_urllib_parse(_LazyModule):
  293. """Lazy loading of moved objects in six.moves.urllib_parse"""
  294. _urllib_parse_moved_attributes = [
  295. MovedAttribute("ParseResult", "urlparse", "urllib.parse"),
  296. MovedAttribute("SplitResult", "urlparse", "urllib.parse"),
  297. MovedAttribute("parse_qs", "urlparse", "urllib.parse"),
  298. MovedAttribute("parse_qsl", "urlparse", "urllib.parse"),
  299. MovedAttribute("urldefrag", "urlparse", "urllib.parse"),
  300. MovedAttribute("urljoin", "urlparse", "urllib.parse"),
  301. MovedAttribute("urlparse", "urlparse", "urllib.parse"),
  302. MovedAttribute("urlsplit", "urlparse", "urllib.parse"),
  303. MovedAttribute("urlunparse", "urlparse", "urllib.parse"),
  304. MovedAttribute("urlunsplit", "urlparse", "urllib.parse"),
  305. MovedAttribute("quote", "urllib", "urllib.parse"),
  306. MovedAttribute("quote_plus", "urllib", "urllib.parse"),
  307. MovedAttribute("unquote", "urllib", "urllib.parse"),
  308. MovedAttribute("unquote_plus", "urllib", "urllib.parse"),
  309. MovedAttribute(
  310. "unquote_to_bytes", "urllib", "urllib.parse", "unquote", "unquote_to_bytes"
  311. ),
  312. MovedAttribute("urlencode", "urllib", "urllib.parse"),
  313. MovedAttribute("splitquery", "urllib", "urllib.parse"),
  314. MovedAttribute("splittag", "urllib", "urllib.parse"),
  315. MovedAttribute("splituser", "urllib", "urllib.parse"),
  316. MovedAttribute("splitvalue", "urllib", "urllib.parse"),
  317. MovedAttribute("uses_fragment", "urlparse", "urllib.parse"),
  318. MovedAttribute("uses_netloc", "urlparse", "urllib.parse"),
  319. MovedAttribute("uses_params", "urlparse", "urllib.parse"),
  320. MovedAttribute("uses_query", "urlparse", "urllib.parse"),
  321. MovedAttribute("uses_relative", "urlparse", "urllib.parse"),
  322. ]
  323. for attr in _urllib_parse_moved_attributes:
  324. setattr(Module_six_moves_urllib_parse, attr.name, attr)
  325. del attr
  326. Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes
  327. _importer._add_module(
  328. Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse"),
  329. "moves.urllib_parse",
  330. "moves.urllib.parse",
  331. )
  332. class Module_six_moves_urllib_error(_LazyModule):
  333. """Lazy loading of moved objects in six.moves.urllib_error"""
  334. _urllib_error_moved_attributes = [
  335. MovedAttribute("URLError", "urllib2", "urllib.error"),
  336. MovedAttribute("HTTPError", "urllib2", "urllib.error"),
  337. MovedAttribute("ContentTooShortError", "urllib", "urllib.error"),
  338. ]
  339. for attr in _urllib_error_moved_attributes:
  340. setattr(Module_six_moves_urllib_error, attr.name, attr)
  341. del attr
  342. Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes
  343. _importer._add_module(
  344. Module_six_moves_urllib_error(__name__ + ".moves.urllib.error"),
  345. "moves.urllib_error",
  346. "moves.urllib.error",
  347. )
  348. class Module_six_moves_urllib_request(_LazyModule):
  349. """Lazy loading of moved objects in six.moves.urllib_request"""
  350. _urllib_request_moved_attributes = [
  351. MovedAttribute("urlopen", "urllib2", "urllib.request"),
  352. MovedAttribute("install_opener", "urllib2", "urllib.request"),
  353. MovedAttribute("build_opener", "urllib2", "urllib.request"),
  354. MovedAttribute("pathname2url", "urllib", "urllib.request"),
  355. MovedAttribute("url2pathname", "urllib", "urllib.request"),
  356. MovedAttribute("getproxies", "urllib", "urllib.request"),
  357. MovedAttribute("Request", "urllib2", "urllib.request"),
  358. MovedAttribute("OpenerDirector", "urllib2", "urllib.request"),
  359. MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"),
  360. MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"),
  361. MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"),
  362. MovedAttribute("ProxyHandler", "urllib2", "urllib.request"),
  363. MovedAttribute("BaseHandler", "urllib2", "urllib.request"),
  364. MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"),
  365. MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"),
  366. MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"),
  367. MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"),
  368. MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"),
  369. MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"),
  370. MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"),
  371. MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"),
  372. MovedAttribute("HTTPHandler", "urllib2", "urllib.request"),
  373. MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"),
  374. MovedAttribute("FileHandler", "urllib2", "urllib.request"),
  375. MovedAttribute("FTPHandler", "urllib2", "urllib.request"),
  376. MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"),
  377. MovedAttribute("UnknownHandler", "urllib2", "urllib.request"),
  378. MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"),
  379. MovedAttribute("urlretrieve", "urllib", "urllib.request"),
  380. MovedAttribute("urlcleanup", "urllib", "urllib.request"),
  381. MovedAttribute("URLopener", "urllib", "urllib.request"),
  382. MovedAttribute("FancyURLopener", "urllib", "urllib.request"),
  383. MovedAttribute("proxy_bypass", "urllib", "urllib.request"),
  384. MovedAttribute("parse_http_list", "urllib2", "urllib.request"),
  385. MovedAttribute("parse_keqv_list", "urllib2", "urllib.request"),
  386. ]
  387. for attr in _urllib_request_moved_attributes:
  388. setattr(Module_six_moves_urllib_request, attr.name, attr)
  389. del attr
  390. Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes
  391. _importer._add_module(
  392. Module_six_moves_urllib_request(__name__ + ".moves.urllib.request"),
  393. "moves.urllib_request",
  394. "moves.urllib.request",
  395. )
  396. class Module_six_moves_urllib_response(_LazyModule):
  397. """Lazy loading of moved objects in six.moves.urllib_response"""
  398. _urllib_response_moved_attributes = [
  399. MovedAttribute("addbase", "urllib", "urllib.response"),
  400. MovedAttribute("addclosehook", "urllib", "urllib.response"),
  401. MovedAttribute("addinfo", "urllib", "urllib.response"),
  402. MovedAttribute("addinfourl", "urllib", "urllib.response"),
  403. ]
  404. for attr in _urllib_response_moved_attributes:
  405. setattr(Module_six_moves_urllib_response, attr.name, attr)
  406. del attr
  407. Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes
  408. _importer._add_module(
  409. Module_six_moves_urllib_response(__name__ + ".moves.urllib.response"),
  410. "moves.urllib_response",
  411. "moves.urllib.response",
  412. )
  413. class Module_six_moves_urllib_robotparser(_LazyModule):
  414. """Lazy loading of moved objects in six.moves.urllib_robotparser"""
  415. _urllib_robotparser_moved_attributes = [
  416. MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"),
  417. ]
  418. for attr in _urllib_robotparser_moved_attributes:
  419. setattr(Module_six_moves_urllib_robotparser, attr.name, attr)
  420. del attr
  421. Module_six_moves_urllib_robotparser._moved_attributes = (
  422. _urllib_robotparser_moved_attributes
  423. )
  424. _importer._add_module(
  425. Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser"),
  426. "moves.urllib_robotparser",
  427. "moves.urllib.robotparser",
  428. )
  429. class Module_six_moves_urllib(types.ModuleType):
  430. """Create a six.moves.urllib namespace that resembles the Python 3 namespace"""
  431. __path__ = [] # mark as package
  432. parse = _importer._get_module("moves.urllib_parse")
  433. error = _importer._get_module("moves.urllib_error")
  434. request = _importer._get_module("moves.urllib_request")
  435. response = _importer._get_module("moves.urllib_response")
  436. robotparser = _importer._get_module("moves.urllib_robotparser")
  437. def __dir__(self):
  438. return ["parse", "error", "request", "response", "robotparser"]
  439. _importer._add_module(
  440. Module_six_moves_urllib(__name__ + ".moves.urllib"), "moves.urllib"
  441. )
  442. def add_move(move):
  443. """Add an item to six.moves."""
  444. setattr(_MovedItems, move.name, move)
  445. def remove_move(name):
  446. """Remove item from six.moves."""
  447. try:
  448. delattr(_MovedItems, name)
  449. except AttributeError:
  450. try:
  451. del moves.__dict__[name]
  452. except KeyError:
  453. raise AttributeError("no such move, %r" % (name,))
  454. if PY3:
  455. _meth_func = "__func__"
  456. _meth_self = "__self__"
  457. _func_closure = "__closure__"
  458. _func_code = "__code__"
  459. _func_defaults = "__defaults__"
  460. _func_globals = "__globals__"
  461. else:
  462. _meth_func = "im_func"
  463. _meth_self = "im_self"
  464. _func_closure = "func_closure"
  465. _func_code = "func_code"
  466. _func_defaults = "func_defaults"
  467. _func_globals = "func_globals"
  468. try:
  469. advance_iterator = next
  470. except NameError:
  471. def advance_iterator(it):
  472. return it.next()
  473. next = advance_iterator
  474. try:
  475. callable = callable
  476. except NameError:
  477. def callable(obj):
  478. return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
  479. if PY3:
  480. def get_unbound_function(unbound):
  481. return unbound
  482. create_bound_method = types.MethodType
  483. def create_unbound_method(func, cls):
  484. return func
  485. Iterator = object
  486. else:
  487. def get_unbound_function(unbound):
  488. return unbound.im_func
  489. def create_bound_method(func, obj):
  490. return types.MethodType(func, obj, obj.__class__)
  491. def create_unbound_method(func, cls):
  492. return types.MethodType(func, None, cls)
  493. class Iterator(object):
  494. def next(self):
  495. return type(self).__next__(self)
  496. callable = callable
  497. _add_doc(
  498. get_unbound_function, """Get the function out of a possibly unbound function"""
  499. )
  500. get_method_function = operator.attrgetter(_meth_func)
  501. get_method_self = operator.attrgetter(_meth_self)
  502. get_function_closure = operator.attrgetter(_func_closure)
  503. get_function_code = operator.attrgetter(_func_code)
  504. get_function_defaults = operator.attrgetter(_func_defaults)
  505. get_function_globals = operator.attrgetter(_func_globals)
  506. if PY3:
  507. def iterkeys(d, **kw):
  508. return iter(d.keys(**kw))
  509. def itervalues(d, **kw):
  510. return iter(d.values(**kw))
  511. def iteritems(d, **kw):
  512. return iter(d.items(**kw))
  513. def iterlists(d, **kw):
  514. return iter(d.lists(**kw))
  515. viewkeys = operator.methodcaller("keys")
  516. viewvalues = operator.methodcaller("values")
  517. viewitems = operator.methodcaller("items")
  518. else:
  519. def iterkeys(d, **kw):
  520. return d.iterkeys(**kw)
  521. def itervalues(d, **kw):
  522. return d.itervalues(**kw)
  523. def iteritems(d, **kw):
  524. return d.iteritems(**kw)
  525. def iterlists(d, **kw):
  526. return d.iterlists(**kw)
  527. viewkeys = operator.methodcaller("viewkeys")
  528. viewvalues = operator.methodcaller("viewvalues")
  529. viewitems = operator.methodcaller("viewitems")
  530. _add_doc(iterkeys, "Return an iterator over the keys of a dictionary.")
  531. _add_doc(itervalues, "Return an iterator over the values of a dictionary.")
  532. _add_doc(iteritems, "Return an iterator over the (key, value) pairs of a dictionary.")
  533. _add_doc(
  534. iterlists, "Return an iterator over the (key, [values]) pairs of a dictionary."
  535. )
  536. if PY3:
  537. def b(s):
  538. return s.encode("latin-1")
  539. def u(s):
  540. return s
  541. unichr = chr
  542. import struct
  543. int2byte = struct.Struct(">B").pack
  544. del struct
  545. byte2int = operator.itemgetter(0)
  546. indexbytes = operator.getitem
  547. iterbytes = iter
  548. import io
  549. StringIO = io.StringIO
  550. BytesIO = io.BytesIO
  551. del io
  552. _assertCountEqual = "assertCountEqual"
  553. if sys.version_info[1] <= 1:
  554. _assertRaisesRegex = "assertRaisesRegexp"
  555. _assertRegex = "assertRegexpMatches"
  556. _assertNotRegex = "assertNotRegexpMatches"
  557. else:
  558. _assertRaisesRegex = "assertRaisesRegex"
  559. _assertRegex = "assertRegex"
  560. _assertNotRegex = "assertNotRegex"
  561. else:
  562. def b(s):
  563. return s
  564. # Workaround for standalone backslash
  565. def u(s):
  566. return unicode(s.replace(r"\\", r"\\\\"), "unicode_escape")
  567. unichr = unichr
  568. int2byte = chr
  569. def byte2int(bs):
  570. return ord(bs[0])
  571. def indexbytes(buf, i):
  572. return ord(buf[i])
  573. iterbytes = functools.partial(itertools.imap, ord)
  574. import StringIO
  575. StringIO = BytesIO = StringIO.StringIO
  576. _assertCountEqual = "assertItemsEqual"
  577. _assertRaisesRegex = "assertRaisesRegexp"
  578. _assertRegex = "assertRegexpMatches"
  579. _assertNotRegex = "assertNotRegexpMatches"
  580. _add_doc(b, """Byte literal""")
  581. _add_doc(u, """Text literal""")
  582. def assertCountEqual(self, *args, **kwargs):
  583. return getattr(self, _assertCountEqual)(*args, **kwargs)
  584. def assertRaisesRegex(self, *args, **kwargs):
  585. return getattr(self, _assertRaisesRegex)(*args, **kwargs)
  586. def assertRegex(self, *args, **kwargs):
  587. return getattr(self, _assertRegex)(*args, **kwargs)
  588. def assertNotRegex(self, *args, **kwargs):
  589. return getattr(self, _assertNotRegex)(*args, **kwargs)
  590. if PY3:
  591. exec_ = getattr(moves.builtins, "exec")
  592. def reraise(tp, value, tb=None):
  593. try:
  594. if value is None:
  595. value = tp()
  596. if value.__traceback__ is not tb:
  597. raise value.with_traceback(tb)
  598. raise value
  599. finally:
  600. value = None
  601. tb = None
  602. else:
  603. def exec_(_code_, _globs_=None, _locs_=None):
  604. """Execute code in a namespace."""
  605. if _globs_ is None:
  606. frame = sys._getframe(1)
  607. _globs_ = frame.f_globals
  608. if _locs_ is None:
  609. _locs_ = frame.f_locals
  610. del frame
  611. elif _locs_ is None:
  612. _locs_ = _globs_
  613. exec ("""exec _code_ in _globs_, _locs_""")
  614. exec_(
  615. """def reraise(tp, value, tb=None):
  616. try:
  617. raise tp, value, tb
  618. finally:
  619. tb = None
  620. """
  621. )
  622. if sys.version_info[:2] > (3,):
  623. exec_(
  624. """def raise_from(value, from_value):
  625. try:
  626. raise value from from_value
  627. finally:
  628. value = None
  629. """
  630. )
  631. else:
  632. def raise_from(value, from_value):
  633. raise value
  634. print_ = getattr(moves.builtins, "print", None)
  635. if print_ is None:
  636. def print_(*args, **kwargs):
  637. """The new-style print function for Python 2.4 and 2.5."""
  638. fp = kwargs.pop("file", sys.stdout)
  639. if fp is None:
  640. return
  641. def write(data):
  642. if not isinstance(data, basestring):
  643. data = str(data)
  644. # If the file has an encoding, encode unicode with it.
  645. if (
  646. isinstance(fp, file)
  647. and isinstance(data, unicode)
  648. and fp.encoding is not None
  649. ):
  650. errors = getattr(fp, "errors", None)
  651. if errors is None:
  652. errors = "strict"
  653. data = data.encode(fp.encoding, errors)
  654. fp.write(data)
  655. want_unicode = False
  656. sep = kwargs.pop("sep", None)
  657. if sep is not None:
  658. if isinstance(sep, unicode):
  659. want_unicode = True
  660. elif not isinstance(sep, str):
  661. raise TypeError("sep must be None or a string")
  662. end = kwargs.pop("end", None)
  663. if end is not None:
  664. if isinstance(end, unicode):
  665. want_unicode = True
  666. elif not isinstance(end, str):
  667. raise TypeError("end must be None or a string")
  668. if kwargs:
  669. raise TypeError("invalid keyword arguments to print()")
  670. if not want_unicode:
  671. for arg in args:
  672. if isinstance(arg, unicode):
  673. want_unicode = True
  674. break
  675. if want_unicode:
  676. newline = unicode("\n")
  677. space = unicode(" ")
  678. else:
  679. newline = "\n"
  680. space = " "
  681. if sep is None:
  682. sep = space
  683. if end is None:
  684. end = newline
  685. for i, arg in enumerate(args):
  686. if i:
  687. write(sep)
  688. write(arg)
  689. write(end)
  690. if sys.version_info[:2] < (3, 3):
  691. _print = print_
  692. def print_(*args, **kwargs):
  693. fp = kwargs.get("file", sys.stdout)
  694. flush = kwargs.pop("flush", False)
  695. _print(*args, **kwargs)
  696. if flush and fp is not None:
  697. fp.flush()
  698. _add_doc(reraise, """Reraise an exception.""")
  699. if sys.version_info[0:2] < (3, 4):
  700. # This does exactly the same what the :func:`py3:functools.update_wrapper`
  701. # function does on Python versions after 3.2. It sets the ``__wrapped__``
  702. # attribute on ``wrapper`` object and it doesn't raise an error if any of
  703. # the attributes mentioned in ``assigned`` and ``updated`` are missing on
  704. # ``wrapped`` object.
  705. def _update_wrapper(
  706. wrapper,
  707. wrapped,
  708. assigned=functools.WRAPPER_ASSIGNMENTS,
  709. updated=functools.WRAPPER_UPDATES,
  710. ):
  711. for attr in assigned:
  712. try:
  713. value = getattr(wrapped, attr)
  714. except AttributeError:
  715. continue
  716. else:
  717. setattr(wrapper, attr, value)
  718. for attr in updated:
  719. getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
  720. wrapper.__wrapped__ = wrapped
  721. return wrapper
  722. _update_wrapper.__doc__ = functools.update_wrapper.__doc__
  723. def wraps(
  724. wrapped,
  725. assigned=functools.WRAPPER_ASSIGNMENTS,
  726. updated=functools.WRAPPER_UPDATES,
  727. ):
  728. return functools.partial(
  729. _update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated
  730. )
  731. wraps.__doc__ = functools.wraps.__doc__
  732. else:
  733. wraps = functools.wraps
  734. def with_metaclass(meta, *bases):
  735. """Create a base class with a metaclass."""
  736. # This requires a bit of explanation: the basic idea is to make a dummy
  737. # metaclass for one level of class instantiation that replaces itself with
  738. # the actual metaclass.
  739. class metaclass(type):
  740. def __new__(cls, name, this_bases, d):
  741. if sys.version_info[:2] >= (3, 7):
  742. # This version introduced PEP 560 that requires a bit
  743. # of extra care (we mimic what is done by __build_class__).
  744. resolved_bases = types.resolve_bases(bases)
  745. if resolved_bases is not bases:
  746. d["__orig_bases__"] = bases
  747. else:
  748. resolved_bases = bases
  749. return meta(name, resolved_bases, d)
  750. @classmethod
  751. def __prepare__(cls, name, this_bases):
  752. return meta.__prepare__(name, bases)
  753. return type.__new__(metaclass, "temporary_class", (), {})
  754. def add_metaclass(metaclass):
  755. """Class decorator for creating a class with a metaclass."""
  756. def wrapper(cls):
  757. orig_vars = cls.__dict__.copy()
  758. slots = orig_vars.get("__slots__")
  759. if slots is not None:
  760. if isinstance(slots, str):
  761. slots = [slots]
  762. for slots_var in slots:
  763. orig_vars.pop(slots_var)
  764. orig_vars.pop("__dict__", None)
  765. orig_vars.pop("__weakref__", None)
  766. if hasattr(cls, "__qualname__"):
  767. orig_vars["__qualname__"] = cls.__qualname__
  768. return metaclass(cls.__name__, cls.__bases__, orig_vars)
  769. return wrapper
  770. def ensure_binary(s, encoding="utf-8", errors="strict"):
  771. """Coerce **s** to six.binary_type.
  772. For Python 2:
  773. - `unicode` -> encoded to `str`
  774. - `str` -> `str`
  775. For Python 3:
  776. - `str` -> encoded to `bytes`
  777. - `bytes` -> `bytes`
  778. """
  779. if isinstance(s, binary_type):
  780. return s
  781. if isinstance(s, text_type):
  782. return s.encode(encoding, errors)
  783. raise TypeError("not expecting type '%s'" % type(s))
  784. def ensure_str(s, encoding="utf-8", errors="strict"):
  785. """Coerce *s* to `str`.
  786. For Python 2:
  787. - `unicode` -> encoded to `str`
  788. - `str` -> `str`
  789. For Python 3:
  790. - `str` -> `str`
  791. - `bytes` -> decoded to `str`
  792. """
  793. # Optimization: Fast return for the common case.
  794. if type(s) is str:
  795. return s
  796. if PY2 and isinstance(s, text_type):
  797. return s.encode(encoding, errors)
  798. elif PY3 and isinstance(s, binary_type):
  799. return s.decode(encoding, errors)
  800. elif not isinstance(s, (text_type, binary_type)):
  801. raise TypeError("not expecting type '%s'" % type(s))
  802. return s
  803. def ensure_text(s, encoding="utf-8", errors="strict"):
  804. """Coerce *s* to six.text_type.
  805. For Python 2:
  806. - `unicode` -> `unicode`
  807. - `str` -> `unicode`
  808. For Python 3:
  809. - `str` -> `str`
  810. - `bytes` -> decoded to `str`
  811. """
  812. if isinstance(s, binary_type):
  813. return s.decode(encoding, errors)
  814. elif isinstance(s, text_type):
  815. return s
  816. else:
  817. raise TypeError("not expecting type '%s'" % type(s))
  818. def python_2_unicode_compatible(klass):
  819. """
  820. A class decorator that defines __unicode__ and __str__ methods under Python 2.
  821. Under Python 3 it does nothing.
  822. To support Python 2 and 3 with a single code base, define a __str__ method
  823. returning text and apply this decorator to the class.
  824. """
  825. if PY2:
  826. if "__str__" not in klass.__dict__:
  827. raise ValueError(
  828. "@python_2_unicode_compatible cannot be applied "
  829. "to %s because it doesn't define __str__()." % klass.__name__
  830. )
  831. klass.__unicode__ = klass.__str__
  832. klass.__str__ = lambda self: self.__unicode__().encode("utf-8")
  833. return klass
  834. # Complete the moves implementation.
  835. # This code is at the end of this module to speed up module loading.
  836. # Turn this module into a package.
  837. __path__ = [] # required for PEP 302 and PEP 451
  838. __package__ = __name__ # see PEP 366 @ReservedAssignment
  839. if globals().get("__spec__") is not None:
  840. __spec__.submodule_search_locations = [] # PEP 451 @UndefinedVariable
  841. # Remove other six meta path importers, since they cause problems. This can
  842. # happen if six is removed from sys.modules and then reloaded. (Setuptools does
  843. # this for some reason.)
  844. if sys.meta_path:
  845. for i, importer in enumerate(sys.meta_path):
  846. # Here's some real nastiness: Another "instance" of the six module might
  847. # be floating around. Therefore, we can't use isinstance() to check for
  848. # the six meta path importer, since the other six instance will have
  849. # inserted an importer with different class.
  850. if (
  851. type(importer).__name__ == "_SixMetaPathImporter"
  852. and importer.name == __name__
  853. ):
  854. del sys.meta_path[i]
  855. break
  856. del i, importer
  857. # Finally, add the importer to the meta path import hook.
  858. sys.meta_path.append(_importer)