index.rst 37 KB

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