six.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. """Utilities for writing code that runs on Python 2 and 3"""
  2. # Copyright (c) 2010-2013 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 operator
  22. import sys
  23. import types
  24. __author__ = "Benjamin Peterson <benjamin@python.org>"
  25. __version__ = "1.3.0"
  26. # True if we are running on Python 3.
  27. PY3 = sys.version_info[0] == 3
  28. if PY3:
  29. string_types = str,
  30. integer_types = int,
  31. class_types = type,
  32. text_type = str
  33. binary_type = bytes
  34. MAXSIZE = sys.maxsize
  35. else:
  36. string_types = basestring,
  37. integer_types = (int, long)
  38. class_types = (type, types.ClassType)
  39. text_type = unicode
  40. binary_type = str
  41. if sys.platform.startswith("java"):
  42. # Jython always uses 32 bits.
  43. MAXSIZE = int((1 << 31) - 1)
  44. else:
  45. # It's possible to have sizeof(long) != sizeof(Py_ssize_t).
  46. class X(object):
  47. def __len__(self):
  48. return 1 << 31
  49. try:
  50. len(X())
  51. except OverflowError:
  52. # 32-bit
  53. MAXSIZE = int((1 << 31) - 1)
  54. else:
  55. # 64-bit
  56. MAXSIZE = int((1 << 63) - 1)
  57. del X
  58. def _add_doc(func, doc):
  59. """Add documentation to a function."""
  60. func.__doc__ = doc
  61. def _import_module(name):
  62. """Import module, returning the module after the last dot."""
  63. __import__(name)
  64. return sys.modules[name]
  65. class _LazyDescr(object):
  66. def __init__(self, name):
  67. self.name = name
  68. def __get__(self, obj, tp):
  69. result = self._resolve()
  70. setattr(obj, self.name, result)
  71. # This is a bit ugly, but it avoids running this again.
  72. delattr(tp, self.name)
  73. return result
  74. class MovedModule(_LazyDescr):
  75. def __init__(self, name, old, new=None):
  76. super(MovedModule, self).__init__(name)
  77. if PY3:
  78. if new is None:
  79. new = name
  80. self.mod = new
  81. else:
  82. self.mod = old
  83. def _resolve(self):
  84. return _import_module(self.mod)
  85. class MovedAttribute(_LazyDescr):
  86. def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
  87. super(MovedAttribute, self).__init__(name)
  88. if PY3:
  89. if new_mod is None:
  90. new_mod = name
  91. self.mod = new_mod
  92. if new_attr is None:
  93. if old_attr is None:
  94. new_attr = name
  95. else:
  96. new_attr = old_attr
  97. self.attr = new_attr
  98. else:
  99. self.mod = old_mod
  100. if old_attr is None:
  101. old_attr = name
  102. self.attr = old_attr
  103. def _resolve(self):
  104. module = _import_module(self.mod)
  105. return getattr(module, self.attr)
  106. class _MovedItems(types.ModuleType):
  107. """Lazy loading of moved objects"""
  108. _moved_attributes = [
  109. MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
  110. MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
  111. MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
  112. MovedAttribute("map", "itertools", "builtins", "imap", "map"),
  113. MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
  114. MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
  115. MovedAttribute("reduce", "__builtin__", "functools"),
  116. MovedAttribute("StringIO", "StringIO", "io"),
  117. MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
  118. MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
  119. MovedModule("builtins", "__builtin__"),
  120. MovedModule("configparser", "ConfigParser"),
  121. MovedModule("copyreg", "copy_reg"),
  122. MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
  123. MovedModule("http_cookies", "Cookie", "http.cookies"),
  124. MovedModule("html_entities", "htmlentitydefs", "html.entities"),
  125. MovedModule("html_parser", "HTMLParser", "html.parser"),
  126. MovedModule("http_client", "httplib", "http.client"),
  127. MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
  128. MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
  129. MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
  130. MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
  131. MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
  132. MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
  133. MovedModule("cPickle", "cPickle", "pickle"),
  134. MovedModule("queue", "Queue"),
  135. MovedModule("reprlib", "repr"),
  136. MovedModule("socketserver", "SocketServer"),
  137. MovedModule("tkinter", "Tkinter"),
  138. MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
  139. MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
  140. MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
  141. MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
  142. MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
  143. MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
  144. MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
  145. MovedModule("tkinter_colorchooser", "tkColorChooser",
  146. "tkinter.colorchooser"),
  147. MovedModule("tkinter_commondialog", "tkCommonDialog",
  148. "tkinter.commondialog"),
  149. MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
  150. MovedModule("tkinter_font", "tkFont", "tkinter.font"),
  151. MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
  152. MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
  153. "tkinter.simpledialog"),
  154. MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
  155. MovedModule("winreg", "_winreg"),
  156. ]
  157. for attr in _moved_attributes:
  158. setattr(_MovedItems, attr.name, attr)
  159. del attr
  160. moves = sys.modules[__name__ + ".moves"] = _MovedItems("moves")
  161. def add_move(move):
  162. """Add an item to six.moves."""
  163. setattr(_MovedItems, move.name, move)
  164. def remove_move(name):
  165. """Remove item from webassets.six.moves."""
  166. try:
  167. delattr(_MovedItems, name)
  168. except AttributeError:
  169. try:
  170. del moves.__dict__[name]
  171. except KeyError:
  172. raise AttributeError("no such move, %r" % (name,))
  173. if PY3:
  174. _meth_func = "__func__"
  175. _meth_self = "__self__"
  176. _func_closure = "__closure__"
  177. _func_code = "__code__"
  178. _func_defaults = "__defaults__"
  179. _func_globals = "__globals__"
  180. _iterkeys = "keys"
  181. _itervalues = "values"
  182. _iteritems = "items"
  183. _iterlists = "lists"
  184. else:
  185. _meth_func = "im_func"
  186. _meth_self = "im_self"
  187. _func_closure = "func_closure"
  188. _func_code = "func_code"
  189. _func_defaults = "func_defaults"
  190. _func_globals = "func_globals"
  191. _iterkeys = "iterkeys"
  192. _itervalues = "itervalues"
  193. _iteritems = "iteritems"
  194. _iterlists = "iterlists"
  195. try:
  196. advance_iterator = next
  197. except NameError:
  198. def advance_iterator(it):
  199. return it.next()
  200. next = advance_iterator
  201. try:
  202. callable = callable
  203. except NameError:
  204. def callable(obj):
  205. return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
  206. if PY3:
  207. def get_unbound_function(unbound):
  208. return unbound
  209. create_bound_method = types.MethodType
  210. Iterator = object
  211. else:
  212. def get_unbound_function(unbound):
  213. return unbound.im_func
  214. def create_bound_method(func, obj):
  215. return types.MethodType(func, obj, obj.__class__)
  216. class Iterator(object):
  217. def next(self):
  218. return type(self).__next__(self)
  219. callable = callable
  220. _add_doc(get_unbound_function,
  221. """Get the function out of a possibly unbound function""")
  222. get_method_function = operator.attrgetter(_meth_func)
  223. get_method_self = operator.attrgetter(_meth_self)
  224. get_function_closure = operator.attrgetter(_func_closure)
  225. get_function_code = operator.attrgetter(_func_code)
  226. get_function_defaults = operator.attrgetter(_func_defaults)
  227. get_function_globals = operator.attrgetter(_func_globals)
  228. def iterkeys(d, **kw):
  229. """Return an iterator over the keys of a dictionary."""
  230. return iter(getattr(d, _iterkeys)(**kw))
  231. def itervalues(d, **kw):
  232. """Return an iterator over the values of a dictionary."""
  233. return iter(getattr(d, _itervalues)(**kw))
  234. def iteritems(d, **kw):
  235. """Return an iterator over the (key, value) pairs of a dictionary."""
  236. return iter(getattr(d, _iteritems)(**kw))
  237. def iterlists(d, **kw):
  238. """Return an iterator over the (key, [values]) pairs of a dictionary."""
  239. return iter(getattr(d, _iterlists)(**kw))
  240. if PY3:
  241. def b(s):
  242. return s.encode("latin-1")
  243. def u(s):
  244. return s
  245. if sys.version_info[1] <= 1:
  246. def int2byte(i):
  247. return bytes((i,))
  248. else:
  249. # This is about 2x faster than the implementation above on 3.2+
  250. int2byte = operator.methodcaller("to_bytes", 1, "big")
  251. indexbytes = operator.getitem
  252. iterbytes = iter
  253. import io
  254. StringIO = io.StringIO
  255. BytesIO = io.BytesIO
  256. else:
  257. def b(s):
  258. return s
  259. def u(s):
  260. return unicode(s, "unicode_escape")
  261. int2byte = chr
  262. def indexbytes(buf, i):
  263. return ord(buf[i])
  264. def iterbytes(buf):
  265. return (ord(byte) for byte in buf)
  266. import StringIO
  267. StringIO = BytesIO = StringIO.StringIO
  268. _add_doc(b, """Byte literal""")
  269. _add_doc(u, """Text literal""")
  270. if PY3:
  271. import builtins
  272. exec_ = getattr(builtins, "exec")
  273. def reraise(tp, value, tb=None):
  274. if value.__traceback__ is not tb:
  275. raise value.with_traceback(tb)
  276. raise value
  277. print_ = getattr(builtins, "print")
  278. del builtins
  279. else:
  280. def exec_(_code_, _globs_=None, _locs_=None):
  281. """Execute code in a namespace."""
  282. if _globs_ is None:
  283. frame = sys._getframe(1)
  284. _globs_ = frame.f_globals
  285. if _locs_ is None:
  286. _locs_ = frame.f_locals
  287. del frame
  288. elif _locs_ is None:
  289. _locs_ = _globs_
  290. exec("""exec _code_ in _globs_, _locs_""")
  291. exec_("""def reraise(tp, value, tb=None):
  292. raise tp, value, tb
  293. """)
  294. def print_(*args, **kwargs):
  295. """The new-style print function."""
  296. fp = kwargs.pop("file", sys.stdout)
  297. if fp is None:
  298. return
  299. def write(data):
  300. if not isinstance(data, basestring):
  301. data = str(data)
  302. fp.write(data)
  303. want_unicode = False
  304. sep = kwargs.pop("sep", None)
  305. if sep is not None:
  306. if isinstance(sep, unicode):
  307. want_unicode = True
  308. elif not isinstance(sep, str):
  309. raise TypeError("sep must be None or a string")
  310. end = kwargs.pop("end", None)
  311. if end is not None:
  312. if isinstance(end, unicode):
  313. want_unicode = True
  314. elif not isinstance(end, str):
  315. raise TypeError("end must be None or a string")
  316. if kwargs:
  317. raise TypeError("invalid keyword arguments to print()")
  318. if not want_unicode:
  319. for arg in args:
  320. if isinstance(arg, unicode):
  321. want_unicode = True
  322. break
  323. if want_unicode:
  324. newline = unicode("\n")
  325. space = unicode(" ")
  326. else:
  327. newline = "\n"
  328. space = " "
  329. if sep is None:
  330. sep = space
  331. if end is None:
  332. end = newline
  333. for i, arg in enumerate(args):
  334. if i:
  335. write(sep)
  336. write(arg)
  337. write(end)
  338. _add_doc(reraise, """Reraise an exception.""")
  339. def with_metaclass(meta, *bases):
  340. """Create a base class with a metaclass."""
  341. return meta("NewBase", bases, {})