index.rst 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. Six: Python 2 and 3 Compatibility Library
  2. =========================================
  3. .. module:: six
  4. :synopsis: Python 2 and 3 compatibility
  5. .. moduleauthor:: Benjamin Peterson <benjamin@python.org>
  6. .. sectionauthor:: Benjamin Peterson <benjamin@python.org>
  7. Six provides simple utilities for wrapping over differences between Python 2 and
  8. Python 3. It is intended to support codebases that work on both Python 2 and 3
  9. without modification. six consists of only one Python file, so it is painless
  10. to copy into a project.
  11. Six can be downloaded on `PyPi <http://pypi.python.org/pypi/six/>`_. Its bug
  12. tracker and code hosting is on `BitBucket <http://bitbucket.org/gutworth/six>`_.
  13. The name, "six", comes from the fact that 2*3 equals 6. Why not addition?
  14. Multiplication is more powerful, and, anyway, "five" has already been snatched
  15. away by the (admittedly now moribund) Zope Five project.
  16. Indices and tables
  17. ------------------
  18. * :ref:`genindex`
  19. * :ref:`search`
  20. Package contents
  21. ----------------
  22. .. data:: PY2
  23. A boolean indicating if the code is running on Python 2.
  24. .. data:: PY3
  25. A boolean indicating if the code is running on Python 3.
  26. Constants
  27. >>>>>>>>>
  28. Six provides constants that may differ between Python versions. Ones ending
  29. ``_types`` are mostly useful as the second argument to ``isinstance`` or
  30. ``issubclass``.
  31. .. data:: class_types
  32. Possible class types. In Python 2, this encompasses old-style and new-style
  33. classes. In Python 3, this is just new-styles.
  34. .. data:: integer_types
  35. Possible integer types. In Python 2, this is :func:`py2:long` and
  36. :func:`py2:int`, and in Python 3, just :func:`py3:int`.
  37. .. data:: string_types
  38. Possible types for text data. This is :func:`py2:basestring` in Python 2 and
  39. :func:`py3:str` in Python 3.
  40. .. data:: text_type
  41. Type for representing (Unicode) textual data. This is :func:`py2:unicode` in
  42. Python 2 and :func:`py3:str` in Python 3.
  43. .. data:: binary_type
  44. Type for representing binary data. This is :func:`py2:str` in Python 2 and
  45. :func:`py3:bytes` in Python 3.
  46. .. data:: MAXSIZE
  47. The maximum size of a container like :func:`py3:list` or :func:`py3:dict`.
  48. This is equivalent to :data:`py3:sys.maxsize` in Python 2.6 and later
  49. (including 3.x). Note, this is temptingly similar to, but not the same as
  50. :data:`py2:sys.maxint` in Python 2. There is no direct equivalent to
  51. :data:`py2:sys.maxint` in Python 3 because its integer type has no limits
  52. aside from memory.
  53. Here's example usage of the module::
  54. import six
  55. def dispatch_types(value):
  56. if isinstance(value, six.integer_types):
  57. handle_integer(value)
  58. elif isinstance(value, six.class_types):
  59. handle_class(value)
  60. elif isinstance(value, six.string_types):
  61. handle_string(value)
  62. Object model compatibility
  63. >>>>>>>>>>>>>>>>>>>>>>>>>>
  64. Python 3 renamed the attributes of several intepreter data structures. The
  65. following accessors are available. Note that the recommended way to inspect
  66. functions and methods is the stdlib :mod:`py3:inspect` module.
  67. .. function:: get_unbound_function(meth)
  68. Get the function out of unbound method *meth*. In Python 3, unbound methods
  69. don't exist, so this function just returns *meth* unchanged. Example
  70. usage::
  71. from six import get_unbound_function
  72. class X(object):
  73. def method(self):
  74. pass
  75. method_function = get_unbound_function(X.method)
  76. .. function:: get_method_function(meth)
  77. Get the function out of method object *meth*.
  78. .. function:: get_method_self(meth)
  79. Get the ``self`` of bound method *meth*.
  80. .. function:: get_function_closure(func)
  81. Get the closure (list of cells) associated with *func*. This is equivalent
  82. to ``func.__closure__`` on Python 2.6+ and ``func.func_closure`` on Python
  83. 2.5.
  84. .. function:: get_function_code(func)
  85. Get the code object associated with *func*. This is equivalent to
  86. ``func.__code__`` on Python 2.6+ and ``func.func_code`` on Python 2.5.
  87. .. function:: get_function_defaults(func)
  88. Get the defaults tuple associated with *func*. This is equivalent to
  89. ``func.__defaults__`` on Python 2.6+ and ``func.func_defaults`` on Python
  90. 2.5.
  91. .. function:: get_function_globals(func)
  92. Get the globals of *func*. This is equivalent to ``func.__globals__`` on
  93. Python 2.6+ and ``func.func_globals`` on Python 2.5.
  94. .. function:: next(it)
  95. advance_iterator(it)
  96. Get the next item of iterator *it*. :exc:`py3:StopIteration` is raised if
  97. the iterator is exhausted. This is a replacement for calling ``it.next()``
  98. in Python 2 and ``next(it)`` in Python 3.
  99. .. function:: callable(obj)
  100. Check if *obj* can be called. Note ``callable`` has returned in Python 3.2,
  101. so using six's version is only necessary when supporting Python 3.0 or 3.1.
  102. .. function:: iterkeys(dictionary, **kwargs)
  103. Returns an iterator over *dictionary*\'s keys. This replaces
  104. ``dictionary.iterkeys()`` on Python 2 and ``dictionary.keys()`` on
  105. Python 3. *kwargs* are passed through to the underlying method.
  106. .. function:: itervalues(dictionary, **kwargs)
  107. Returns an iterator over *dictionary*\'s values. This replaces
  108. ``dictionary.itervalues()`` on Python 2 and ``dictionary.values()`` on
  109. Python 3. *kwargs* are passed through to the underlying method.
  110. .. function:: iteritems(dictionary, **kwargs)
  111. Returns an iterator over *dictionary*\'s items. This replaces
  112. ``dictionary.iteritems()`` on Python 2 and ``dictionary.items()`` on
  113. Python 3. *kwargs* are passed through to the underlying method.
  114. .. function:: iterlists(dictionary, **kwargs)
  115. Calls ``dictionary.iterlists()`` on Python 2 and ``dictionary.lists()`` on
  116. Python 3. No builtin Python mapping type has such a method; this method is
  117. intended for use with multi-valued dictionaries like `Werkzeug's
  118. <http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict>`_.
  119. *kwargs* are passed through to the underlying method.
  120. .. function:: viewkeys(dictionary)
  121. Return a view over *dictionary*\'s keys. This replaces
  122. :meth:`py2:dict.viewkeys` on Python 2.7 and :meth:`py3:dict.keys` on
  123. Python 3.
  124. .. function:: viewvalues(dictionary)
  125. Return a view over *dictionary*\'s values. This replaces
  126. :meth:`py2:dict.viewvalues` on Python 2.7 and :meth:`py3:dict.values` on
  127. Python 3.
  128. .. function:: viewitems(dictionary)
  129. Return a view over *dictionary*\'s items. This replaces
  130. :meth:`py2:dict.viewitems` on Python 2.7 and :meth:`py3:dict.items` on
  131. Python 3.
  132. .. function:: create_bound_method(func, obj)
  133. Return a method object wrapping *func* and bound to *obj*. On both Python 2
  134. and 3, this will return a :func:`py3:types.MethodType` object. The reason
  135. this wrapper exists is that on Python 2, the ``MethodType`` constructor
  136. requires the *obj*'s class to be passed.
  137. .. class:: Iterator
  138. A class for making portable iterators. The intention is that it be subclassed
  139. and subclasses provide a ``__next__`` method. In Python 2, :class:`Iterator`
  140. has one method: ``next``. It simply delegates to ``__next__``. An alternate
  141. way to do this would be to simply alias ``next`` to ``__next__``. However,
  142. this interacts badly with subclasses that override
  143. ``__next__``. :class:`Iterator` is empty on Python 3. (In fact, it is just
  144. aliased to :class:`py3:object`.)
  145. .. decorator:: wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES)
  146. This is exactly the :func:`py3:functools.wraps` decorator, but it sets the
  147. ``__wrapped__`` attribute on what it decorates as :func:`py3:functools.wraps`
  148. does on Python versions after 3.2.
  149. Syntax compatibility
  150. >>>>>>>>>>>>>>>>>>>>
  151. These functions smooth over operations which have different syntaxes between
  152. Python 2 and 3.
  153. .. function:: exec_(code, globals=None, locals=None)
  154. Execute *code* in the scope of *globals* and *locals*. *code* can be a
  155. string or a code object. If *globals* or *locals* are not given, they will
  156. default to the scope of the caller. If just *globals* is given, it will also
  157. be used as *locals*.
  158. .. note::
  159. Python 3's :func:`py3:exec` doesn't take keyword arguments, so calling
  160. :func:`exec` with them should be avoided.
  161. .. function:: print_(*args, *, file=sys.stdout, end="\\n", sep=" ", flush=False)
  162. Print *args* into *file*. Each argument will be separated with *sep* and
  163. *end* will be written to the file after the last argument is printed. If
  164. *flush* is true, ``file.flush()`` will be called after all data is written.
  165. .. note::
  166. In Python 2, this function imitates Python 3's :func:`py3:print` by not
  167. having softspace support. If you don't know what that is, you're probably
  168. ok. :)
  169. .. function:: raise_from(exc_value, exc_value_from)
  170. Raise an exception from a context. On Python 3, this is equivalent to
  171. ``raise exc_value from exc_value_from``. On Python 2, which does not support
  172. exception chaining, it is equivalent to ``raise exc_value``.
  173. .. function:: reraise(exc_type, exc_value, exc_traceback=None)
  174. Reraise an exception, possibly with a different traceback. In the simple
  175. case, ``reraise(*sys.exc_info())`` with an active exception (in an except
  176. block) reraises the current exception with the last traceback. A different
  177. traceback can be specified with the *exc_traceback* parameter. Note that
  178. since the exception reraising is done within the :func:`reraise` function,
  179. Python will attach the call frame of :func:`reraise` to whatever traceback is
  180. raised.
  181. .. function:: with_metaclass(metaclass, *bases)
  182. Create a new class with base classes *bases* and metaclass *metaclass*. This
  183. is designed to be used in class declarations like this: ::
  184. from six import with_metaclass
  185. class Meta(type):
  186. pass
  187. class Base(object):
  188. pass
  189. class MyClass(with_metaclass(Meta, Base)):
  190. pass
  191. Another way to set a metaclass on a class is with the :func:`add_metaclass`
  192. decorator.
  193. .. decorator:: add_metaclass(metaclass)
  194. Class decorator that replaces a normally-constructed class with a
  195. metaclass-constructed one. Example usage: ::
  196. @add_metaclass(Meta)
  197. class MyClass(object):
  198. pass
  199. That code produces a class equivalent to ::
  200. class MyClass(object, metaclass=Meta):
  201. pass
  202. on Python 3 or ::
  203. class MyClass(object):
  204. __metaclass__ = MyMeta
  205. on Python 2.
  206. Note that class decorators require Python 2.6. However, the effect of the
  207. decorator can be emulated on Python 2.5 like so::
  208. class MyClass(object):
  209. pass
  210. MyClass = add_metaclass(Meta)(MyClass)
  211. Binary and text data
  212. >>>>>>>>>>>>>>>>>>>>
  213. Python 3 enforces the distinction between byte strings and text strings far more
  214. rigoriously than Python 2 does; binary data cannot be automatically coerced to
  215. or from text data. six provides several functions to assist in classifying
  216. string data in all Python versions.
  217. .. function:: b(data)
  218. A "fake" bytes literal. *data* should always be a normal string literal. In
  219. Python 2, :func:`b` returns a 8-bit string. In Python 3, *data* is encoded
  220. with the latin-1 encoding to bytes.
  221. .. note::
  222. Since all Python versions 2.6 and after support the ``b`` prefix,
  223. :func:`b`, code without 2.5 support doesn't need :func:`b`.
  224. .. function:: u(text)
  225. A "fake" unicode literal. *text* should always be a normal string literal.
  226. In Python 2, :func:`u` returns unicode, and in Python 3, a string. Also, in
  227. Python 2, the string is decoded with the ``unicode-escape`` codec, which
  228. allows unicode escapes to be used in it.
  229. .. note::
  230. In Python 3.3, the ``u`` prefix has been reintroduced. Code that only
  231. supports Python 3 versions greater than 3.3 thus does not need
  232. :func:`u`.
  233. .. note::
  234. On Python 2, :func:`u` doesn't know what the encoding of the literal
  235. is. Each byte is converted directly to the unicode codepoint of the same
  236. value. Because of this, it's only safe to use :func:`u` with strings of
  237. ASCII data.
  238. .. function:: unichr(c)
  239. Return the (Unicode) string representing the codepoint *c*. This is
  240. equivalent to :func:`py2:unichr` on Python 2 and :func:`py3:chr` on Python 3.
  241. .. function:: int2byte(i)
  242. Converts *i* to a byte. *i* must be in ``range(0, 256)``. This is
  243. equivalent to :func:`py2:chr` in Python 2 and ``bytes((i,))`` in Python 3.
  244. .. function:: byte2int(bs)
  245. Converts the first byte of *bs* to an integer. This is equivalent to
  246. ``ord(bs[0])`` on Python 2 and ``bs[0]`` on Python 3.
  247. .. function:: indexbytes(buf, i)
  248. Return the byte at index *i* of *buf* as an integer. This is equivalent to
  249. indexing a bytes object in Python 3.
  250. .. function:: iterbytes(buf)
  251. Return an iterator over bytes in *buf* as integers. This is equivalent to
  252. a bytes object iterator in Python 3.
  253. .. data:: StringIO
  254. This is an fake file object for textual data. It's an alias for
  255. :class:`py2:StringIO.StringIO` in Python 2 and :class:`py3:io.StringIO` in
  256. Python 3.
  257. .. data:: BytesIO
  258. This is a fake file object for binary data. In Python 2, it's an alias for
  259. :class:`py2:StringIO.StringIO`, but in Python 3, it's an alias for
  260. :class:`py3:io.BytesIO`.
  261. .. decorator:: python_2_unicode_compatible
  262. A class decorator that takes a class defining a ``__str__`` method. On
  263. Python 3, the decorator does nothing. On Python 2, it aliases the
  264. ``__str__`` method to ``__unicode__`` and creates a new ``__str__`` method
  265. that returns the result of ``__unicode__()`` encoded with UTF-8.
  266. unittest assertions
  267. >>>>>>>>>>>>>>>>>>>
  268. Six contains compatibility shims for unittest assertions that have been renamed.
  269. The parameters are the same as their aliases, but you must pass the test method
  270. as the first argument. For example::
  271. import six
  272. import unittest
  273. class TestAssertCountEqual(unittest.TestCase):
  274. def test(self):
  275. six.assertCountEqual(self, (1, 2), [2, 1])
  276. Note these functions are only available on Python 2.7 or later.
  277. .. function:: assertCountEqual()
  278. Alias for :meth:`~py3:unittest.TestCase.assertCountEqual` on Python 3 and
  279. :meth:`~py2:unittest.TestCase.assertItemsEqual` on Python 2.
  280. .. function:: assertRaisesRegex()
  281. Alias for :meth:`~py3:unittest.TestCase.assertRaisesRegex` on Python 3 and
  282. :meth:`~py2:unittest.TestCase.assertRaisesRegexp` on Python 2.
  283. .. function:: assertRegex()
  284. Alias for :meth:`~py3:unittest.TestCase.assertRegex` on Python 3 and
  285. :meth:`~py2:unittest.TestCase.assertRegexpMatches` on Python 2.
  286. Renamed modules and attributes compatibility
  287. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  288. .. module:: six.moves
  289. :synopsis: Renamed modules and attributes compatibility
  290. Python 3 reorganized the standard library and moved several functions to
  291. different modules. Six provides a consistent interface to them through the fake
  292. :mod:`six.moves` module. For example, to load the module for parsing HTML on
  293. Python 2 or 3, write::
  294. from six.moves import html_parser
  295. Similarly, to get the function to reload modules, which was moved from the
  296. builtin module to the ``imp`` module, use::
  297. from six.moves import reload_module
  298. For the most part, :mod:`six.moves` aliases are the names of the modules in
  299. Python 3. When the new Python 3 name is a package, the components of the name
  300. are separated by underscores. For example, ``html.parser`` becomes
  301. ``html_parser``. In some cases where several modules have been combined, the
  302. Python 2 name is retained. This is so the appropiate modules can be found when
  303. running on Python 2. For example, ``BaseHTTPServer`` which is in
  304. ``http.server`` in Python 3 is aliased as ``BaseHTTPServer``.
  305. Some modules which had two implementations have been merged in Python 3. For
  306. example, ``cPickle`` no longer exists in Python 3; it was merged with
  307. ``pickle``. In these cases, fetching the fast version will load the fast one on
  308. Python 2 and the merged module in Python 3.
  309. The :mod:`py2:urllib`, :mod:`py2:urllib2`, and :mod:`py2:urlparse` modules have
  310. been combined in the :mod:`py3:urllib` package in Python 3. The
  311. :mod:`six.moves.urllib` package is a version-independent location for this
  312. functionality; its structure mimics the structure of the Python 3
  313. :mod:`py3:urllib` package.
  314. .. note::
  315. In order to make imports of the form::
  316. from six.moves.cPickle import loads
  317. work, six places special proxy objects in in :data:`py3:sys.modules`. These
  318. proxies lazily load the underlying module when an attribute is fetched. This
  319. will fail if the underlying module is not available in the Python
  320. interpreter. For example, ``sys.modules["six.moves.winreg"].LoadKey`` would
  321. fail on any non-Windows platform. Unfortunately, some applications try to
  322. load attributes on every module in :data:`py3:sys.modules`. six mitigates
  323. this problem for some applications by pretending attributes on unimportable
  324. modules don't exist. This hack doesn't work in every case, though. If you are
  325. encountering problems with the lazy modules and don't use any from imports
  326. directly from ``six.moves`` modules, you can workaround the issue by removing
  327. the six proxy modules::
  328. d = [name for name in sys.modules if name.startswith("six.moves.")]
  329. for name in d:
  330. del sys.modules[name]
  331. Supported renames:
  332. +------------------------------+-------------------------------------+-------------------------------------+
  333. | Name | Python 2 name | Python 3 name |
  334. +==============================+=====================================+=====================================+
  335. | ``builtins`` | :mod:`py2:__builtin__` | :mod:`py3:builtins` |
  336. +------------------------------+-------------------------------------+-------------------------------------+
  337. | ``configparser`` | :mod:`py2:ConfigParser` | :mod:`py3:configparser` |
  338. +------------------------------+-------------------------------------+-------------------------------------+
  339. | ``copyreg`` | :mod:`py2:copy_reg` | :mod:`py3:copyreg` |
  340. +------------------------------+-------------------------------------+-------------------------------------+
  341. | ``cPickle`` | :mod:`py2:cPickle` | :mod:`py3:pickle` |
  342. +------------------------------+-------------------------------------+-------------------------------------+
  343. | ``cStringIO`` | :func:`py2:cStringIO.StringIO` | :class:`py3:io.StringIO` |
  344. +------------------------------+-------------------------------------+-------------------------------------+
  345. | ``dbm_gnu`` | :func:`py2:gdbm` | :class:`py3:dbm.gnu` |
  346. +------------------------------+-------------------------------------+-------------------------------------+
  347. | ``_dummy_thread`` | :mod:`py2:dummy_thread` | :mod:`py3:_dummy_thread` |
  348. +------------------------------+-------------------------------------+-------------------------------------+
  349. | ``email_mime_multipart`` | :mod:`py2:email.MIMEMultipart` | :mod:`py3:email.mime.multipart` |
  350. +------------------------------+-------------------------------------+-------------------------------------+
  351. | ``email_mime_nonmultipart`` | :mod:`py2:email.MIMENonMultipart` | :mod:`py3:email.mime.nonmultipart` |
  352. +------------------------------+-------------------------------------+-------------------------------------+
  353. | ``email_mime_text`` | :mod:`py2:email.MIMEText` | :mod:`py3:email.mime.text` |
  354. +------------------------------+-------------------------------------+-------------------------------------+
  355. | ``email_mime_base`` | :mod:`py2:email.MIMEBase` | :mod:`py3:email.mime.base` |
  356. +------------------------------+-------------------------------------+-------------------------------------+
  357. | ``filter`` | :func:`py2:itertools.ifilter` | :func:`py3:filter` |
  358. +------------------------------+-------------------------------------+-------------------------------------+
  359. | ``filterfalse`` | :func:`py2:itertools.ifilterfalse` | :func:`py3:itertools.filterfalse` |
  360. +------------------------------+-------------------------------------+-------------------------------------+
  361. | ``http_cookiejar`` | :mod:`py2:cookielib` | :mod:`py3:http.cookiejar` |
  362. +------------------------------+-------------------------------------+-------------------------------------+
  363. | ``http_cookies`` | :mod:`py2:Cookie` | :mod:`py3:http.cookies` |
  364. +------------------------------+-------------------------------------+-------------------------------------+
  365. | ``html_entities`` | :mod:`py2:htmlentitydefs` | :mod:`py3:html.entities` |
  366. +------------------------------+-------------------------------------+-------------------------------------+
  367. | ``html_parser`` | :mod:`py2:HTMLParser` | :mod:`py3:html.parser` |
  368. +------------------------------+-------------------------------------+-------------------------------------+
  369. | ``http_client`` | :mod:`py2:httplib` | :mod:`py3:http.client` |
  370. +------------------------------+-------------------------------------+-------------------------------------+
  371. | ``BaseHTTPServer`` | :mod:`py2:BaseHTTPServer` | :mod:`py3:http.server` |
  372. +------------------------------+-------------------------------------+-------------------------------------+
  373. | ``CGIHTTPServer`` | :mod:`py2:CGIHTTPServer` | :mod:`py3:http.server` |
  374. +------------------------------+-------------------------------------+-------------------------------------+
  375. | ``SimpleHTTPServer`` | :mod:`py2:SimpleHTTPServer` | :mod:`py3:http.server` |
  376. +------------------------------+-------------------------------------+-------------------------------------+
  377. | ``input`` | :func:`py2:raw_input` | :func:`py3:input` |
  378. +------------------------------+-------------------------------------+-------------------------------------+
  379. | ``intern`` | :func:`py2:intern` | :func:`py3:sys.intern` |
  380. +------------------------------+-------------------------------------+-------------------------------------+
  381. | ``map`` | :func:`py2:itertools.imap` | :func:`py3:map` |
  382. +------------------------------+-------------------------------------+-------------------------------------+
  383. | ``queue`` | :mod:`py2:Queue` | :mod:`py3:queue` |
  384. +------------------------------+-------------------------------------+-------------------------------------+
  385. | ``range`` | :func:`py2:xrange` | :func:`py3:range` |
  386. +------------------------------+-------------------------------------+-------------------------------------+
  387. | ``reduce`` | :func:`py2:reduce` | :func:`py3:functools.reduce` |
  388. +------------------------------+-------------------------------------+-------------------------------------+
  389. | ``reload_module`` | :func:`py2:reload` | :func:`py3:imp.reload` |
  390. +------------------------------+-------------------------------------+-------------------------------------+
  391. | ``reprlib`` | :mod:`py2:repr` | :mod:`py3:reprlib` |
  392. +------------------------------+-------------------------------------+-------------------------------------+
  393. | ``shlex_quote`` | :mod:`py2:pipes.quote` | :mod:`py3:shlex.quote` |
  394. +------------------------------+-------------------------------------+-------------------------------------+
  395. | ``socketserver`` | :mod:`py2:SocketServer` | :mod:`py3:socketserver` |
  396. +------------------------------+-------------------------------------+-------------------------------------+
  397. | ``_thread`` | :mod:`py2:thread` | :mod:`py3:_thread` |
  398. +------------------------------+-------------------------------------+-------------------------------------+
  399. | ``tkinter`` | :mod:`py2:Tkinter` | :mod:`py3:tkinter` |
  400. +------------------------------+-------------------------------------+-------------------------------------+
  401. | ``tkinter_dialog`` | :mod:`py2:Dialog` | :mod:`py3:tkinter.dialog` |
  402. +------------------------------+-------------------------------------+-------------------------------------+
  403. | ``tkinter_filedialog`` | :mod:`py2:FileDialog` | :mod:`py3:tkinter.FileDialog` |
  404. +------------------------------+-------------------------------------+-------------------------------------+
  405. | ``tkinter_scrolledtext`` | :mod:`py2:ScrolledText` | :mod:`py3:tkinter.scrolledtext` |
  406. +------------------------------+-------------------------------------+-------------------------------------+
  407. | ``tkinter_simpledialog`` | :mod:`py2:SimpleDialog` | :mod:`py3:tkinter.simpledialog` |
  408. +------------------------------+-------------------------------------+-------------------------------------+
  409. | ``tkinter_ttk`` | :mod:`py2:ttk` | :mod:`py3:tkinter.ttk` |
  410. +------------------------------+-------------------------------------+-------------------------------------+
  411. | ``tkinter_tix`` | :mod:`py2:Tix` | :mod:`py3:tkinter.tix` |
  412. +------------------------------+-------------------------------------+-------------------------------------+
  413. | ``tkinter_constants`` | :mod:`py2:Tkconstants` | :mod:`py3:tkinter.constants` |
  414. +------------------------------+-------------------------------------+-------------------------------------+
  415. | ``tkinter_dnd`` | :mod:`py2:Tkdnd` | :mod:`py3:tkinter.dnd` |
  416. +------------------------------+-------------------------------------+-------------------------------------+
  417. | ``tkinter_colorchooser`` | :mod:`py2:tkColorChooser` | :mod:`py3:tkinter.colorchooser` |
  418. +------------------------------+-------------------------------------+-------------------------------------+
  419. | ``tkinter_commondialog`` | :mod:`py2:tkCommonDialog` | :mod:`py3:tkinter.commondialog` |
  420. +------------------------------+-------------------------------------+-------------------------------------+
  421. | ``tkinter_tkfiledialog`` | :mod:`py2:tkFileDialog` | :mod:`py3:tkinter.filedialog` |
  422. +------------------------------+-------------------------------------+-------------------------------------+
  423. | ``tkinter_font`` | :mod:`py2:tkFont` | :mod:`py3:tkinter.font` |
  424. +------------------------------+-------------------------------------+-------------------------------------+
  425. | ``tkinter_messagebox`` | :mod:`py2:tkMessageBox` | :mod:`py3:tkinter.messagebox` |
  426. +------------------------------+-------------------------------------+-------------------------------------+
  427. | ``tkinter_tksimpledialog`` | :mod:`py2:tkSimpleDialog` | :mod:`py3:tkinter.simpledialog` |
  428. +------------------------------+-------------------------------------+-------------------------------------+
  429. | ``urllib.parse`` | See :mod:`six.moves.urllib.parse` | :mod:`py3:urllib.parse` |
  430. +------------------------------+-------------------------------------+-------------------------------------+
  431. | ``urllib.error`` | See :mod:`six.moves.urllib.error` | :mod:`py3:urllib.error` |
  432. +------------------------------+-------------------------------------+-------------------------------------+
  433. | ``urllib.request`` | See :mod:`six.moves.urllib.request` | :mod:`py3:urllib.request` |
  434. +------------------------------+-------------------------------------+-------------------------------------+
  435. | ``urllib.response`` | See :mod:`six.moves.urllib.response`| :mod:`py3:urllib.response` |
  436. +------------------------------+-------------------------------------+-------------------------------------+
  437. | ``urllib.robotparser`` | :mod:`py2:robotparser` | :mod:`py3:urllib.robotparser` |
  438. +------------------------------+-------------------------------------+-------------------------------------+
  439. | ``urllib_robotparser`` | :mod:`py2:robotparser` | :mod:`py3:urllib.robotparser` |
  440. +------------------------------+-------------------------------------+-------------------------------------+
  441. | ``UserDict`` | :class:`py2:UserDict.UserDict` | :class:`py3:collections.UserDict` |
  442. +------------------------------+-------------------------------------+-------------------------------------+
  443. | ``UserList`` | :class:`py2:UserList.UserList` | :class:`py3:collections.UserList` |
  444. +------------------------------+-------------------------------------+-------------------------------------+
  445. | ``UserString`` | :class:`py2:UserString.UserString` | :class:`py3:collections.UserString` |
  446. +------------------------------+-------------------------------------+-------------------------------------+
  447. | ``winreg`` | :mod:`py2:_winreg` | :mod:`py3:winreg` |
  448. +------------------------------+-------------------------------------+-------------------------------------+
  449. | ``xmlrpc_client`` | :mod:`py2:xmlrpclib` | :mod:`py3:xmlrpc.client` |
  450. +------------------------------+-------------------------------------+-------------------------------------+
  451. | ``xmlrpc_server`` | :mod:`py2:SimpleXMLRPCServer` | :mod:`py3:xmlrpc.server` |
  452. +------------------------------+-------------------------------------+-------------------------------------+
  453. | ``xrange`` | :func:`py2:xrange` | :func:`py3:range` |
  454. +------------------------------+-------------------------------------+-------------------------------------+
  455. | ``zip`` | :func:`py2:itertools.izip` | :func:`py3:zip` |
  456. +------------------------------+-------------------------------------+-------------------------------------+
  457. | ``zip_longest`` | :func:`py2:itertools.izip_longest` | :func:`py3:itertools.zip_longest` |
  458. +------------------------------+-------------------------------------+-------------------------------------+
  459. urllib parse
  460. <<<<<<<<<<<<
  461. .. module:: six.moves.urllib.parse
  462. :synopsis: Stuff from :mod:`py2:urlparse` and :mod:`py2:urllib` in Python 2 and :mod:`py3:urllib.parse` in Python 3
  463. Contains functions from Python 3's :mod:`py3:urllib.parse` and Python 2's:
  464. :mod:`py2:urlparse`:
  465. * :func:`py2:urlparse.ParseResult`
  466. * :func:`py2:urlparse.SplitResult`
  467. * :func:`py2:urlparse.urlparse`
  468. * :func:`py2:urlparse.urlunparse`
  469. * :func:`py2:urlparse.parse_qs`
  470. * :func:`py2:urlparse.parse_qsl`
  471. * :func:`py2:urlparse.urljoin`
  472. * :func:`py2:urlparse.urldefrag`
  473. * :func:`py2:urlparse.urlsplit`
  474. * :func:`py2:urlparse.urlunsplit`
  475. * :func:`py2:urlparse.splitquery`
  476. * :func:`py2:urlparse.uses_fragment`
  477. * :func:`py2:urlparse.uses_netloc`
  478. * :func:`py2:urlparse.uses_params`
  479. * :func:`py2:urlparse.uses_query`
  480. * :func:`py2:urlparse.uses_relative`
  481. and :mod:`py2:urllib`:
  482. * :func:`py2:urllib.quote`
  483. * :func:`py2:urllib.quote_plus`
  484. * :func:`py2:urllib.splittag`
  485. * :func:`py2:urllib.splituser`
  486. * :func:`py2:urllib.unquote`
  487. * :func:`py2:urllib.unquote_plus`
  488. * :func:`py2:urllib.urlencode`
  489. urllib error
  490. <<<<<<<<<<<<
  491. .. module:: six.moves.urllib.error
  492. :synopsis: Stuff from :mod:`py2:urllib` and :mod:`py2:urllib2` in Python 2 and :mod:`py3:urllib.error` in Python 3
  493. Contains exceptions from Python 3's :mod:`py3:urllib.error` and Python 2's:
  494. :mod:`py2:urllib`:
  495. * :exc:`py2:urllib.ContentTooShortError`
  496. and :mod:`py2:urllib2`:
  497. * :exc:`py2:urllib2.URLError`
  498. * :exc:`py2:urllib2.HTTPError`
  499. urllib request
  500. <<<<<<<<<<<<<<
  501. .. module:: six.moves.urllib.request
  502. :synopsis: Stuff from :mod:`py2:urllib` and :mod:`py2:urllib2` in Python 2 and :mod:`py3:urllib.request` in Python 3
  503. Contains items from Python 3's :mod:`py3:urllib.request` and Python 2's:
  504. :mod:`py2:urllib`:
  505. * :func:`py2:urllib.pathname2url`
  506. * :func:`py2:urllib.url2pathname`
  507. * :func:`py2:urllib.getproxies`
  508. * :func:`py2:urllib.urlretrieve`
  509. * :func:`py2:urllib.urlcleanup`
  510. * :class:`py2:urllib.URLopener`
  511. * :class:`py2:urllib.FancyURLopener`
  512. * :func:`py2:urllib.proxy_bypass`
  513. and :mod:`py2:urllib2`:
  514. * :func:`py2:urllib2.urlopen`
  515. * :func:`py2:urllib2.install_opener`
  516. * :func:`py2:urllib2.build_opener`
  517. * :class:`py2:urllib2.Request`
  518. * :class:`py2:urllib2.OpenerDirector`
  519. * :class:`py2:urllib2.HTTPDefaultErrorHandler`
  520. * :class:`py2:urllib2.HTTPRedirectHandler`
  521. * :class:`py2:urllib2.HTTPCookieProcessor`
  522. * :class:`py2:urllib2.ProxyHandler`
  523. * :class:`py2:urllib2.BaseHandler`
  524. * :class:`py2:urllib2.HTTPPasswordMgr`
  525. * :class:`py2:urllib2.HTTPPasswordMgrWithDefaultRealm`
  526. * :class:`py2:urllib2.AbstractBasicAuthHandler`
  527. * :class:`py2:urllib2.HTTPBasicAuthHandler`
  528. * :class:`py2:urllib2.ProxyBasicAuthHandler`
  529. * :class:`py2:urllib2.AbstractDigestAuthHandler`
  530. * :class:`py2:urllib2.HTTPDigestAuthHandler`
  531. * :class:`py2:urllib2.ProxyDigestAuthHandler`
  532. * :class:`py2:urllib2.HTTPHandler`
  533. * :class:`py2:urllib2.HTTPSHandler`
  534. * :class:`py2:urllib2.FileHandler`
  535. * :class:`py2:urllib2.FTPHandler`
  536. * :class:`py2:urllib2.CacheFTPHandler`
  537. * :class:`py2:urllib2.UnknownHandler`
  538. * :class:`py2:urllib2.HTTPErrorProcessor`
  539. urllib response
  540. <<<<<<<<<<<<<<<
  541. .. module:: six.moves.urllib.response
  542. :synopsis: Stuff from :mod:`py2:urllib` in Python 2 and :mod:`py3:urllib.response` in Python 3
  543. Contains classes from Python 3's :mod:`py3:urllib.response` and Python 2's:
  544. :mod:`py2:urllib`:
  545. * :class:`py2:urllib.addbase`
  546. * :class:`py2:urllib.addclosehook`
  547. * :class:`py2:urllib.addinfo`
  548. * :class:`py2:urllib.addinfourl`
  549. Advanced - Customizing renames
  550. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  551. .. currentmodule:: six
  552. It is possible to add additional names to the :mod:`six.moves` namespace.
  553. .. function:: add_move(item)
  554. Add *item* to the :mod:`six.moves` mapping. *item* should be a
  555. :class:`MovedAttribute` or :class:`MovedModule` instance.
  556. .. function:: remove_move(name)
  557. Remove the :mod:`six.moves` mapping called *name*. *name* should be a
  558. string.
  559. Instances of the following classes can be passed to :func:`add_move`. Neither
  560. have any public members.
  561. .. class:: MovedModule(name, old_mod, new_mod)
  562. Create a mapping for :mod:`six.moves` called *name* that references different
  563. modules in Python 2 and 3. *old_mod* is the name of the Python 2 module.
  564. *new_mod* is the name of the Python 3 module.
  565. .. class:: MovedAttribute(name, old_mod, new_mod, old_attr=None, new_attr=None)
  566. Create a mapping for :mod:`six.moves` called *name* that references different
  567. attributes in Python 2 and 3. *old_mod* is the name of the Python 2 module.
  568. *new_mod* is the name of the Python 3 module. If *new_attr* is not given, it
  569. defaults to *old_attr*. If neither is given, they both default to *name*.