six.py 32 KB

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