You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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