Debian Codegraph @

documentation/index.rst

raw
1 Six: Python 2 and 3 Compatibility Library
2 =========================================
3
4 .. module:: six
5 :synopsis: Python 2 and 3 compatibility
6
7 .. moduleauthor:: Benjamin Peterson <[email protected]>
8 .. sectionauthor:: Benjamin Peterson <[email protected]>
9
10
11 Six provides simple utilities for wrapping over differences between Python 2 and
12 Python 3. It is intended to support codebases that work on both Python 2 and 3
13 without modification. six consists of only one Python file, so it is painless
14 to copy into a project.
15
16 Six can be downloaded on `PyPI <https://pypi.org/project/six/>`_. Its bug
17 tracker and code hosting is on `GitHub <https://github.com/benjaminp/six>`_.
18
19 The name, "six", comes from the fact that 2*3 equals 6. Why not addition?
20 Multiplication is more powerful, and, anyway, "five" has already been snatched
21 away by the (admittedly now moribund) Zope Five project.
22
23
24 Indices and tables
25 ------------------
26
27 * :ref:`genindex`
28 * :ref:`search`
29
30
31 Package contents
32 ----------------
33
34 .. data:: PY2
35
36 A boolean indicating if the code is running on Python 2.
37
38 .. data:: PY3
39
40 A boolean indicating if the code is running on Python 3.
41
42
43 Constants
44 >>>>>>>>>
45
46 Six provides constants that may differ between Python versions. Ones ending
47 ``_types`` are mostly useful as the second argument to ``isinstance`` or
48 ``issubclass``.
49
50
51 .. data:: class_types
52
53 Possible class types. In Python 2, this encompasses old-style
54 :data:`py2:types.ClassType` and new-style ``type`` classes. In Python 3,
55 this is just ``type``.
56
57
58 .. data:: integer_types
59
60 Possible integer types. In Python 2, this is :func:`py2:long` and
61 :func:`py2:int`, and in Python 3, just :func:`py3:int`.
62
63
64 .. data:: string_types
65
66 Possible types for text data. This is :func:`py2:basestring` in Python 2 and
67 :func:`py3:str` in Python 3.
68
69
70 .. data:: text_type
71
72 Type for representing (Unicode) textual data. This is :func:`py2:unicode` in
73 Python 2 and :func:`py3:str` in Python 3.
74
75
76 .. data:: binary_type
77
78 Type for representing binary data. This is :func:`py2:str` in Python 2 and
79 :func:`py3:bytes` in Python 3. Python 2.6 and 2.7 include ``bytes`` as a
80 builtin alias of ``str``, so six’s version is only necessary for Python 2.5
81 compatibility.
82
83
84 .. data:: MAXSIZE
85
86 The maximum size of a container like :func:`py3:list` or :func:`py3:dict`.
87 This is equivalent to :data:`py3:sys.maxsize` in Python 2.6 and later
88 (including 3.x). Note, this is temptingly similar to, but not the same as
89 :data:`py2:sys.maxint` in Python 2. There is no direct equivalent to
90 :data:`py2:sys.maxint` in Python 3 because its integer type has no limits
91 aside from memory.
92
93
94 Here's example usage of the module::
95
96 import six
97
98 def dispatch_types(value):
99 if isinstance(value, six.integer_types):
100 handle_integer(value)
101 elif isinstance(value, six.class_types):
102 handle_class(value)
103 elif isinstance(value, six.string_types):
104 handle_string(value)
105
106
107 Object model compatibility
108 >>>>>>>>>>>>>>>>>>>>>>>>>>
109
110 Python 3 renamed the attributes of several interpreter data structures. The
111 following accessors are available. Note that the recommended way to inspect
112 functions and methods is the stdlib :mod:`py3:inspect` module.
113
114
115 .. function:: get_unbound_function(meth)
116
117 Get the function out of unbound method *meth*. In Python 3, unbound methods
118 don't exist, so this function just returns *meth* unchanged. Example
119 usage::
120
121 from six import get_unbound_function
122
123 class X(object):
124 def method(self):
125 pass
126 method_function = get_unbound_function(X.method)
127
128
129 .. function:: get_method_function(meth)
130
131 Get the function out of method object *meth*.
132
133
134 .. function:: get_method_self(meth)
135
136 Get the ``self`` of bound method *meth*.
137
138
139 .. function:: get_function_closure(func)
140
141 Get the closure (list of cells) associated with *func*. This is equivalent
142 to ``func.__closure__`` on Python 2.6+ and ``func.func_closure`` on Python
143 2.5.
144
145
146 .. function:: get_function_code(func)
147
148 Get the code object associated with *func*. This is equivalent to
149 ``func.__code__`` on Python 2.6+ and ``func.func_code`` on Python 2.5.
150
151
152 .. function:: get_function_defaults(func)
153
154 Get the defaults tuple associated with *func*. This is equivalent to
155 ``func.__defaults__`` on Python 2.6+ and ``func.func_defaults`` on Python
156 2.5.
157
158
159 .. function:: get_function_globals(func)
160
161 Get the globals of *func*. This is equivalent to ``func.__globals__`` on
162 Python 2.6+ and ``func.func_globals`` on Python 2.5.
163
164
165 .. function:: next(it)
166 advance_iterator(it)
167
168 Get the next item of iterator *it*. :exc:`py3:StopIteration` is raised if
169 the iterator is exhausted. This is a replacement for calling ``it.next()``
170 in Python 2 and ``next(it)`` in Python 3. Python 2.6 and above have a
171 builtin ``next`` function, so six's version is only necessary for Python 2.5
172 compatibility.
173
174
175 .. function:: callable(obj)
176
177 Check if *obj* can be called. Note ``callable`` has returned in Python 3.2,
178 so using six's version is only necessary when supporting Python 3.0 or 3.1.
179
180
181 .. function:: iterkeys(dictionary, **kwargs)
182
183 Returns an iterator over *dictionary*\'s keys. This replaces
184 ``dictionary.iterkeys()`` on Python 2 and ``dictionary.keys()`` on
185 Python 3. *kwargs* are passed through to the underlying method.
186
187
188 .. function:: itervalues(dictionary, **kwargs)
189
190 Returns an iterator over *dictionary*\'s values. This replaces
191 ``dictionary.itervalues()`` on Python 2 and ``dictionary.values()`` on
192 Python 3. *kwargs* are passed through to the underlying method.
193
194
195 .. function:: iteritems(dictionary, **kwargs)
196
197 Returns an iterator over *dictionary*\'s items. This replaces
198 ``dictionary.iteritems()`` on Python 2 and ``dictionary.items()`` on
199 Python 3. *kwargs* are passed through to the underlying method.
200
201
202 .. function:: iterlists(dictionary, **kwargs)
203
204 Calls ``dictionary.iterlists()`` on Python 2 and ``dictionary.lists()`` on
205 Python 3. No builtin Python mapping type has such a method; this method is
206 intended for use with multi-valued dictionaries like `Werkzeug's
207 <http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict>`_.
208 *kwargs* are passed through to the underlying method.
209
210
211 .. function:: viewkeys(dictionary)
212
213 Return a view over *dictionary*\'s keys. This replaces
214 :meth:`py2:dict.viewkeys` on Python 2.7 and :meth:`py3:dict.keys` on
215 Python 3.
216
217
218 .. function:: viewvalues(dictionary)
219
220 Return a view over *dictionary*\'s values. This replaces
221 :meth:`py2:dict.viewvalues` on Python 2.7 and :meth:`py3:dict.values` on
222 Python 3.
223
224
225 .. function:: viewitems(dictionary)
226
227 Return a view over *dictionary*\'s items. This replaces
228 :meth:`py2:dict.viewitems` on Python 2.7 and :meth:`py3:dict.items` on
229 Python 3.
230
231
232 .. function:: create_bound_method(func, obj)
233
234 Return a method object wrapping *func* and bound to *obj*. On both Python 2
235 and 3, this will return a :func:`py3:types.MethodType` object. The reason
236 this wrapper exists is that on Python 2, the ``MethodType`` constructor
237 requires the *obj*'s class to be passed.
238
239
240 .. function:: create_unbound_method(func, cls)
241
242 Return an unbound method object wrapping *func*. In Python 2, this will
243 return a :func:`py2:types.MethodType` object. In Python 3, unbound methods
244 do not exist and this wrapper will simply return *func*.
245
246
247 .. class:: Iterator
248
249 A class for making portable iterators. The intention is that it be subclassed
250 and subclasses provide a ``__next__`` method. In Python 2, :class:`Iterator`
251 has one method: ``next``. It simply delegates to ``__next__``. An alternate
252 way to do this would be to simply alias ``next`` to ``__next__``. However,
253 this interacts badly with subclasses that override
254 ``__next__``. :class:`Iterator` is empty on Python 3. (In fact, it is just
255 aliased to :class:`py3:object`.)
256
257
258 .. decorator:: wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES)
259
260 This is Python 3.2's :func:`py3:functools.wraps` decorator. It sets the
261 ``__wrapped__`` attribute on what it decorates. It doesn't raise an error if
262 any of the attributes mentioned in ``assigned`` and ``updated`` are missing
263 on ``wrapped`` object.
264
265
266 Syntax compatibility
267 >>>>>>>>>>>>>>>>>>>>
268
269 These functions smooth over operations which have different syntaxes between
270 Python 2 and 3.
271
272
273 .. function:: exec_(code, globals=None, locals=None)
274
275 Execute *code* in the scope of *globals* and *locals*. *code* can be a
276 string or a code object. If *globals* or *locals* are not given, they will
277 default to the scope of the caller. If just *globals* is given, it will also
278 be used as *locals*.
279
280 .. note::
281
282 Python 3's :func:`py3:exec` doesn't take keyword arguments, so calling
283 :func:`exec` with them should be avoided.
284
285
286 .. function:: print_(*args, *, file=sys.stdout, end="\\n", sep=" ", flush=False)
287
288 Print *args* into *file*. Each argument will be separated with *sep* and
289 *end* will be written to the file after the last argument is printed. If
290 *flush* is true, ``file.flush()`` will be called after all data is written.
291
292 .. note::
293
294 In Python 2, this function imitates Python 3's :func:`py3:print` by not
295 having softspace support. If you don't know what that is, you're probably
296 ok. :)
297
298
299 .. function:: raise_from(exc_value, exc_value_from)
300
301 Raise an exception from a context. On Python 3, this is equivalent to
302 ``raise exc_value from exc_value_from``. On Python 2, which does not support
303 exception chaining, it is equivalent to ``raise exc_value``.
304
305
306 .. function:: reraise(exc_type, exc_value, exc_traceback=None)
307
308 Reraise an exception, possibly with a different traceback. In the simple
309 case, ``reraise(*sys.exc_info())`` with an active exception (in an except
310 block) reraises the current exception with the last traceback. A different
311 traceback can be specified with the *exc_traceback* parameter. Note that
312 since the exception reraising is done within the :func:`reraise` function,
313 Python will attach the call frame of :func:`reraise` to whatever traceback is
314 raised.
315
316
317 .. function:: with_metaclass(metaclass, *bases)
318
319 Create a new class with base classes *bases* and metaclass *metaclass*. This
320 is designed to be used in class declarations like this: ::
321
322 from six import with_metaclass
323
324 class Meta(type):
325 pass
326
327 class Base(object):
328 pass
329
330 class MyClass(with_metaclass(Meta, Base)):
331 pass
332
333 Another way to set a metaclass on a class is with the :func:`add_metaclass`
334 decorator.
335
336
337 .. decorator:: add_metaclass(metaclass)
338
339 Class decorator that replaces a normally-constructed class with a
340 metaclass-constructed one. Example usage: ::
341
342 @add_metaclass(Meta)
343 class MyClass(object):
344 pass
345
346 That code produces a class equivalent to ::
347
348 class MyClass(object, metaclass=Meta):
349 pass
350
351 on Python 3 or ::
352
353 class MyClass(object):
354 __metaclass__ = Meta
355
356 on Python 2.
357
358 Note that class decorators require Python 2.6. However, the effect of the
359 decorator can be emulated on Python 2.5 like so::
360
361 class MyClass(object):
362 pass
363 MyClass = add_metaclass(Meta)(MyClass)
364
365
366 Binary and text data
367 >>>>>>>>>>>>>>>>>>>>
368
369 Python 3 enforces the distinction between byte strings and text strings far more
370 rigorously than Python 2 does; binary data cannot be automatically coerced to
371 or from text data. six provides several functions to assist in classifying
372 string data in all Python versions.
373
374
375 .. function:: b(data)
376
377 A "fake" bytes literal. *data* should always be a normal string literal. In
378 Python 2, :func:`b` returns an 8-bit string. In Python 3, *data* is encoded
379 with the latin-1 encoding to bytes.
380
381
382 .. note::
383
384 Since all Python versions 2.6 and after support the ``b`` prefix,
385 code without 2.5 support doesn't need :func:`b`.
386
387
388 .. function:: u(text)
389
390 A "fake" unicode literal. *text* should always be a normal string literal.
391 In Python 2, :func:`u` returns unicode, and in Python 3, a string. Also, in
392 Python 2, the string is decoded with the ``unicode-escape`` codec, which
393 allows unicode escapes to be used in it.
394
395
396 .. note::
397
398 In Python 3.3, the ``u`` prefix has been reintroduced. Code that only
399 supports Python 3 versions of 3.3 and higher thus does not need
400 :func:`u`.
401
402 .. note::
403
404 On Python 2, :func:`u` doesn't know what the encoding of the literal
405 is. Each byte is converted directly to the unicode codepoint of the same
406 value. Because of this, it's only safe to use :func:`u` with strings of
407 ASCII data.
408
409
410 .. function:: unichr(c)
411
412 Return the (Unicode) string representing the codepoint *c*. This is
413 equivalent to :func:`py2:unichr` on Python 2 and :func:`py3:chr` on Python 3.
414
415
416 .. function:: int2byte(i)
417
418 Converts *i* to a byte. *i* must be in ``range(0, 256)``. This is
419 equivalent to :func:`py2:chr` in Python 2 and ``bytes((i,))`` in Python 3.
420
421
422 .. function:: byte2int(bs)
423
424 Converts the first byte of *bs* to an integer. This is equivalent to
425 ``ord(bs[0])`` on Python 2 and ``bs[0]`` on Python 3.
426
427
428 .. function:: indexbytes(buf, i)
429
430 Return the byte at index *i* of *buf* as an integer. This is equivalent to
431 indexing a bytes object in Python 3.
432
433
434 .. function:: iterbytes(buf)
435
436 Return an iterator over bytes in *buf* as integers. This is equivalent to
437 a bytes object iterator in Python 3.
438
439
440 .. function:: ensure_binary(s, encoding='utf-8', errors='strict')
441
442 Coerce *s* to :data:`binary_type`. *encoding*, *errors* are the same as
443 :meth:`py3:str.encode`
444
445
446 .. function:: ensure_str(s, encoding='utf-8', errors='strict')
447
448 Coerce *s* to ``str``. *encoding*, *errors* are the same as
449 :meth:`py3:str.encode`
450
451
452 .. function:: ensure_text(s, encoding='utf-8', errors='strict')
453
454 Coerce *s* to :data:`text_type`. *encoding*, *errors* are the same as
455 :meth:`py3:bytes.decode`
456
457
458 .. data:: StringIO
459
460 This is a fake file object for textual data. It's an alias for
461 :class:`py2:StringIO.StringIO` in Python 2 and :class:`py3:io.StringIO` in
462 Python 3.
463
464
465 .. data:: BytesIO
466
467 This is a fake file object for binary data. In Python 2, it's an alias for
468 :class:`py2:StringIO.StringIO`, but in Python 3, it's an alias for
469 :class:`py3:io.BytesIO`.
470
471
472 .. decorator:: python_2_unicode_compatible
473
474 A class decorator that takes a class defining a ``__str__`` method. On
475 Python 3, the decorator does nothing. On Python 2, it aliases the
476 ``__str__`` method to ``__unicode__`` and creates a new ``__str__`` method
477 that returns the result of ``__unicode__()`` encoded with UTF-8.
478
479
480 unittest assertions
481 >>>>>>>>>>>>>>>>>>>
482
483 Six contains compatibility shims for unittest assertions that have been renamed.
484 The parameters are the same as their aliases, but you must pass the test method
485 as the first argument. For example::
486
487 import six
488 import unittest
489
490 class TestAssertCountEqual(unittest.TestCase):
491 def test(self):
492 six.assertCountEqual(self, (1, 2), [2, 1])
493
494 Note these functions are only available on Python 2.7 or later.
495
496 .. function:: assertCountEqual()
497
498 Alias for :meth:`~py3:unittest.TestCase.assertCountEqual` on Python 3 and
499 :meth:`~py2:unittest.TestCase.assertItemsEqual` on Python 2.
500
501
502 .. function:: assertRaisesRegex()
503
504 Alias for :meth:`~py3:unittest.TestCase.assertRaisesRegex` on Python 3 and
505 :meth:`~py2:unittest.TestCase.assertRaisesRegexp` on Python 2.
506
507
508 .. function:: assertRegex()
509
510 Alias for :meth:`~py3:unittest.TestCase.assertRegex` on Python 3 and
511 :meth:`~py2:unittest.TestCase.assertRegexpMatches` on Python 2.
512
513 .. function:: assertNotRegex()
514
515 Alias for :meth:`~py3:unittest.TestCase.assertNotRegex` on Python 3 and
516 :meth:`~py2:unittest.TestCase.assertNotRegexpMatches` on Python 2.
517
518
519 Renamed modules and attributes compatibility
520 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
521
522 .. module:: six.moves
523 :synopsis: Renamed modules and attributes compatibility
524
525 Python 3 reorganized the standard library and moved several functions to
526 different modules. Six provides a consistent interface to them through the fake
527 :mod:`six.moves` module. For example, to load the module for parsing HTML on
528 Python 2 or 3, write::
529
530 from six.moves import html_parser
531
532 Similarly, to get the function to reload modules, which was moved from the
533 builtin module to the ``importlib`` module, use::
534
535 from six.moves import reload_module
536
537 For the most part, :mod:`six.moves` aliases are the names of the modules in
538 Python 3. When the new Python 3 name is a package, the components of the name
539 are separated by underscores. For example, ``html.parser`` becomes
540 ``html_parser``. In some cases where several modules have been combined, the
541 Python 2 name is retained. This is so the appropriate modules can be found when
542 running on Python 2. For example, ``BaseHTTPServer`` which is in
543 ``http.server`` in Python 3 is aliased as ``BaseHTTPServer``.
544
545 Some modules which had two implementations have been merged in Python 3. For
546 example, ``cPickle`` no longer exists in Python 3; it was merged with
547 ``pickle``. In these cases, fetching the fast version will load the fast one on
548 Python 2 and the merged module in Python 3.
549
550 The :mod:`py2:urllib`, :mod:`py2:urllib2`, and :mod:`py2:urlparse` modules have
551 been combined in the :mod:`py3:urllib` package in Python 3. The
552 :mod:`six.moves.urllib` package is a version-independent location for this
553 functionality; its structure mimics the structure of the Python 3
554 :mod:`py3:urllib` package.
555
556 .. note::
557
558 In order to make imports of the form::
559
560 from six.moves.cPickle import loads
561
562 work, six places special proxy objects in :data:`py3:sys.modules`. These
563 proxies lazily load the underlying module when an attribute is fetched. This
564 will fail if the underlying module is not available in the Python
565 interpreter. For example, ``sys.modules["six.moves.winreg"].LoadKey`` would
566 fail on any non-Windows platform. Unfortunately, some applications try to
567 load attributes on every module in :data:`py3:sys.modules`. six mitigates
568 this problem for some applications by pretending attributes on unimportable
569 modules do not exist. This hack does not work in every case, though. If you are
570 encountering problems with the lazy modules and don't use any from imports
571 directly from ``six.moves`` modules, you can workaround the issue by removing
572 the six proxy modules::
573
574 d = [name for name in sys.modules if name.startswith("six.moves.")]
575 for name in d:
576 del sys.modules[name]
577
578 Supported renames:
579
580 +------------------------------+---------------------------------------+---------------------------------------+
581 | Name | Python 2 name | Python 3 name |
582 +==============================+=======================================+=======================================+
583 | ``builtins`` | :mod:`py2:__builtin__` | :mod:`py3:builtins` |
584 +------------------------------+---------------------------------------+---------------------------------------+
585 | ``configparser`` | :mod:`py2:ConfigParser` | :mod:`py3:configparser` |
586 +------------------------------+---------------------------------------+---------------------------------------+
587 | ``copyreg`` | :mod:`py2:copy_reg` | :mod:`py3:copyreg` |
588 +------------------------------+---------------------------------------+---------------------------------------+
589 | ``cPickle`` | :mod:`py2:cPickle` | :mod:`py3:pickle` |
590 +------------------------------+---------------------------------------+---------------------------------------+
591 | ``cStringIO`` | :func:`py2:cStringIO.StringIO` | :class:`py3:io.StringIO` |
592 +------------------------------+---------------------------------------+---------------------------------------+
593 | ``collections_abc`` | :mod:`py2:collections` | :mod:`py3:collections.abc` (3.3+) |
594 +------------------------------+---------------------------------------+---------------------------------------+
595 | ``dbm_gnu`` | :mod:`py2:gdbm` | :mod:`py3:dbm.gnu` |
596 +------------------------------+---------------------------------------+---------------------------------------+
597 | ``dbm_ndbm`` | :mod:`py2:dbm` | :mod:`py3:dbm.ndbm` |
598 +------------------------------+---------------------------------------+---------------------------------------+
599 | ``_dummy_thread`` | :mod:`py2:dummy_thread` | :mod:`py3:_dummy_thread` (< 3.9) |
600 | | | :mod:`py3:_thread` (3.9+) |
601 +------------------------------+---------------------------------------+---------------------------------------+
602 | ``email_mime_base`` | :mod:`py2:email.MIMEBase` | :mod:`py3:email.mime.base` |
603 +------------------------------+---------------------------------------+---------------------------------------+
604 | ``email_mime_image`` | :mod:`py2:email.MIMEImage` | :mod:`py3:email.mime.image` |
605 +------------------------------+---------------------------------------+---------------------------------------+
606 | ``email_mime_multipart`` | :mod:`py2:email.MIMEMultipart` | :mod:`py3:email.mime.multipart` |
607 +------------------------------+---------------------------------------+---------------------------------------+
608 | ``email_mime_nonmultipart`` | :mod:`py2:email.MIMENonMultipart` | :mod:`py3:email.mime.nonmultipart` |
609 +------------------------------+---------------------------------------+---------------------------------------+
610 | ``email_mime_text`` | :mod:`py2:email.MIMEText` | :mod:`py3:email.mime.text` |
611 +------------------------------+---------------------------------------+---------------------------------------+
612 | ``filter`` | :func:`py2:itertools.ifilter` | :func:`py3:filter` |
613 +------------------------------+---------------------------------------+---------------------------------------+
614 | ``filterfalse`` | :func:`py2:itertools.ifilterfalse` | :func:`py3:itertools.filterfalse` |
615 +------------------------------+---------------------------------------+---------------------------------------+
616 | ``getcwd`` | :func:`py2:os.getcwdu` | :func:`py3:os.getcwd` |
617 +------------------------------+---------------------------------------+---------------------------------------+
618 | ``getcwdb`` | :func:`py2:os.getcwd` | :func:`py3:os.getcwdb` |
619 +------------------------------+---------------------------------------+---------------------------------------+
620 | ``getoutput`` | :func:`py2:commands.getoutput` | :func:`py3:subprocess.getoutput` |
621 +------------------------------+---------------------------------------+---------------------------------------+
622 | ``http_cookiejar`` | :mod:`py2:cookielib` | :mod:`py3:http.cookiejar` |
623 +------------------------------+---------------------------------------+---------------------------------------+
624 | ``http_cookies`` | :mod:`py2:Cookie` | :mod:`py3:http.cookies` |
625 +------------------------------+---------------------------------------+---------------------------------------+
626 | ``html_entities`` | :mod:`py2:htmlentitydefs` | :mod:`py3:html.entities` |
627 +------------------------------+---------------------------------------+---------------------------------------+
628 | ``html_parser`` | :mod:`py2:HTMLParser` | :mod:`py3:html.parser` |
629 +------------------------------+---------------------------------------+---------------------------------------+
630 | ``http_client`` | :mod:`py2:httplib` | :mod:`py3:http.client` |
631 +------------------------------+---------------------------------------+---------------------------------------+
632 | ``BaseHTTPServer`` | :mod:`py2:BaseHTTPServer` | :mod:`py3:http.server` |
633 +------------------------------+---------------------------------------+---------------------------------------+
634 | ``CGIHTTPServer`` | :mod:`py2:CGIHTTPServer` | :mod:`py3:http.server` |
635 +------------------------------+---------------------------------------+---------------------------------------+
636 | ``SimpleHTTPServer`` | :mod:`py2:SimpleHTTPServer` | :mod:`py3:http.server` |
637 +------------------------------+---------------------------------------+---------------------------------------+
638 | ``input`` | :func:`py2:raw_input` | :func:`py3:input` |
639 +------------------------------+---------------------------------------+---------------------------------------+
640 | ``intern`` | :func:`py2:intern` | :func:`py3:sys.intern` |
641 +------------------------------+---------------------------------------+---------------------------------------+
642 | ``map`` | :func:`py2:itertools.imap` | :func:`py3:map` |
643 +------------------------------+---------------------------------------+---------------------------------------+
644 | ``queue`` | :mod:`py2:Queue` | :mod:`py3:queue` |
645 +------------------------------+---------------------------------------+---------------------------------------+
646 | ``range`` | :func:`py2:xrange` | :func:`py3:range` |
647 +------------------------------+---------------------------------------+---------------------------------------+
648 | ``reduce`` | :func:`py2:reduce` | :func:`py3:functools.reduce` |
649 +------------------------------+---------------------------------------+---------------------------------------+
650 | ``reload_module`` | :func:`py2:reload` | :func:`py3:imp.reload`, |
651 | | | :func:`py3:importlib.reload` |
652 | | | on Python 3.4+ |
653 +------------------------------+---------------------------------------+---------------------------------------+
654 | ``reprlib`` | :mod:`py2:repr` | :mod:`py3:reprlib` |
655 +------------------------------+---------------------------------------+---------------------------------------+
656 | ``shlex_quote`` | :mod:`py2:pipes.quote` | :mod:`py3:shlex.quote` |
657 +------------------------------+---------------------------------------+---------------------------------------+
658 | ``socketserver`` | :mod:`py2:SocketServer` | :mod:`py3:socketserver` |
659 +------------------------------+---------------------------------------+---------------------------------------+
660 | ``_thread`` | :mod:`py2:thread` | :mod:`py3:_thread` |
661 +------------------------------+---------------------------------------+---------------------------------------+
662 | ``tkinter`` | :mod:`py2:Tkinter` | :mod:`py3:tkinter` |
663 +------------------------------+---------------------------------------+---------------------------------------+
664 | ``tkinter_dialog`` | :mod:`py2:Dialog` | :mod:`py3:tkinter.dialog` |
665 +------------------------------+---------------------------------------+---------------------------------------+
666 | ``tkinter_filedialog`` | :mod:`py2:FileDialog` | :mod:`py3:tkinter.FileDialog` |
667 +------------------------------+---------------------------------------+---------------------------------------+
668 | ``tkinter_scrolledtext`` | :mod:`py2:ScrolledText` | :mod:`py3:tkinter.scrolledtext` |
669 +------------------------------+---------------------------------------+---------------------------------------+
670 | ``tkinter_simpledialog`` | :mod:`py2:SimpleDialog` | :mod:`py3:tkinter.simpledialog` |
671 +------------------------------+---------------------------------------+---------------------------------------+
672 | ``tkinter_ttk`` | :mod:`py2:ttk` | :mod:`py3:tkinter.ttk` |
673 +------------------------------+---------------------------------------+---------------------------------------+
674 | ``tkinter_tix`` | :mod:`py2:Tix` | :mod:`py3:tkinter.tix` |
675 +------------------------------+---------------------------------------+---------------------------------------+
676 | ``tkinter_constants`` | :mod:`py2:Tkconstants` | :mod:`py3:tkinter.constants` |
677 +------------------------------+---------------------------------------+---------------------------------------+
678 | ``tkinter_dnd`` | :mod:`py2:Tkdnd` | :mod:`py3:tkinter.dnd` |
679 +------------------------------+---------------------------------------+---------------------------------------+
680 | ``tkinter_colorchooser`` | :mod:`py2:tkColorChooser` | :mod:`py3:tkinter.colorchooser` |
681 +------------------------------+---------------------------------------+---------------------------------------+
682 | ``tkinter_commondialog`` | :mod:`py2:tkCommonDialog` | :mod:`py3:tkinter.commondialog` |
683 +------------------------------+---------------------------------------+---------------------------------------+
684 | ``tkinter_tkfiledialog`` | :mod:`py2:tkFileDialog` | :mod:`py3:tkinter.filedialog` |
685 +------------------------------+---------------------------------------+---------------------------------------+
686 | ``tkinter_font`` | :mod:`py2:tkFont` | :mod:`py3:tkinter.font` |
687 +------------------------------+---------------------------------------+---------------------------------------+
688 | ``tkinter_messagebox`` | :mod:`py2:tkMessageBox` | :mod:`py3:tkinter.messagebox` |
689 +------------------------------+---------------------------------------+---------------------------------------+
690 | ``tkinter_tksimpledialog`` | :mod:`py2:tkSimpleDialog` | :mod:`py3:tkinter.simpledialog` |
691 +------------------------------+---------------------------------------+---------------------------------------+
692 | ``urllib.parse`` | See :mod:`six.moves.urllib.parse` | :mod:`py3:urllib.parse` |
693 +------------------------------+---------------------------------------+---------------------------------------+
694 | ``urllib.error`` | See :mod:`six.moves.urllib.error` | :mod:`py3:urllib.error` |
695 +------------------------------+---------------------------------------+---------------------------------------+
696 | ``urllib.request`` | See :mod:`six.moves.urllib.request` | :mod:`py3:urllib.request` |
697 +------------------------------+---------------------------------------+---------------------------------------+
698 | ``urllib.response`` | See :mod:`six.moves.urllib.response` | :mod:`py3:urllib.response` |
699 +------------------------------+---------------------------------------+---------------------------------------+
700 | ``urllib.robotparser`` | :mod:`py2:robotparser` | :mod:`py3:urllib.robotparser` |
701 +------------------------------+---------------------------------------+---------------------------------------+
702 | ``urllib_robotparser`` | :mod:`py2:robotparser` | :mod:`py3:urllib.robotparser` |
703 +------------------------------+---------------------------------------+---------------------------------------+
704 | ``UserDict`` | :class:`py2:UserDict.IterableUserDict`| :class:`py3:collections.UserDict` |
705 +------------------------------+---------------------------------------+---------------------------------------+
706 | ``UserList`` | :class:`py2:UserList.UserList` | :class:`py3:collections.UserList` |
707 +------------------------------+---------------------------------------+---------------------------------------+
708 | ``UserString`` | :class:`py2:UserString.UserString` | :class:`py3:collections.UserString` |
709 +------------------------------+---------------------------------------+---------------------------------------+
710 | ``winreg`` | :mod:`py2:_winreg` | :mod:`py3:winreg` |
711 +------------------------------+---------------------------------------+---------------------------------------+
712 | ``xmlrpc_client`` | :mod:`py2:xmlrpclib` | :mod:`py3:xmlrpc.client` |
713 +------------------------------+---------------------------------------+---------------------------------------+
714 | ``xmlrpc_server`` | :mod:`py2:SimpleXMLRPCServer` | :mod:`py3:xmlrpc.server` |
715 +------------------------------+---------------------------------------+---------------------------------------+
716 | ``xrange`` | :func:`py2:xrange` | :func:`py3:range` |
717 +------------------------------+---------------------------------------+---------------------------------------+
718 | ``zip`` | :func:`py2:itertools.izip` | :func:`py3:zip` |
719 +------------------------------+---------------------------------------+---------------------------------------+
720 | ``zip_longest`` | :func:`py2:itertools.izip_longest` | :func:`py3:itertools.zip_longest` |
721 +------------------------------+---------------------------------------+---------------------------------------+
722
723 urllib parse
724 <<<<<<<<<<<<
725
726 .. module:: six.moves.urllib.parse
727 :synopsis: Stuff from :mod:`py2:urlparse` and :mod:`py2:urllib` in Python 2 and :mod:`py3:urllib.parse` in Python 3
728
729 Contains functions from Python 3's :mod:`py3:urllib.parse` and Python 2's:
730
731 :mod:`py2:urlparse`:
732
733 * :func:`py2:urlparse.ParseResult`
734 * :func:`py2:urlparse.SplitResult`
735 * :func:`py2:urlparse.urlparse`
736 * :func:`py2:urlparse.urlunparse`
737 * :func:`py2:urlparse.parse_qs`
738 * :func:`py2:urlparse.parse_qsl`
739 * :func:`py2:urlparse.urljoin`
740 * :func:`py2:urlparse.urldefrag`
741 * :func:`py2:urlparse.urlsplit`
742 * :func:`py2:urlparse.urlunsplit`
743 * :func:`py2:urlparse.splitquery`
744 * :func:`py2:urlparse.uses_fragment`
745 * :func:`py2:urlparse.uses_netloc`
746 * :func:`py2:urlparse.uses_params`
747 * :func:`py2:urlparse.uses_query`
748 * :func:`py2:urlparse.uses_relative`
749
750 and :mod:`py2:urllib`:
751
752 * :func:`py2:urllib.quote`
753 * :func:`py2:urllib.quote_plus`
754 * :func:`py2:urllib.splittag`
755 * :func:`py2:urllib.splituser`
756 * :func:`py2:urllib.splitvalue`
757 * :func:`py2:urllib.unquote` (also exposed as :func:`py3:urllib.parse.unquote_to_bytes`)
758 * :func:`py2:urllib.unquote_plus`
759 * :func:`py2:urllib.urlencode`
760
761
762 urllib error
763 <<<<<<<<<<<<
764
765 .. module:: six.moves.urllib.error
766 :synopsis: Stuff from :mod:`py2:urllib` and :mod:`py2:urllib2` in Python 2 and :mod:`py3:urllib.error` in Python 3
767
768 Contains exceptions from Python 3's :mod:`py3:urllib.error` and Python 2's:
769
770 :mod:`py2:urllib`:
771
772 * :exc:`py2:urllib.ContentTooShortError`
773
774 and :mod:`py2:urllib2`:
775
776 * :exc:`py2:urllib2.URLError`
777 * :exc:`py2:urllib2.HTTPError`
778
779
780 urllib request
781 <<<<<<<<<<<<<<
782
783 .. module:: six.moves.urllib.request
784 :synopsis: Stuff from :mod:`py2:urllib` and :mod:`py2:urllib2` in Python 2 and :mod:`py3:urllib.request` in Python 3
785
786 Contains items from Python 3's :mod:`py3:urllib.request` and Python 2's:
787
788 :mod:`py2:urllib`:
789
790 * :func:`py2:urllib.pathname2url`
791 * :func:`py2:urllib.url2pathname`
792 * :func:`py2:urllib.getproxies`
793 * :func:`py2:urllib.urlretrieve`
794 * :func:`py2:urllib.urlcleanup`
795 * :class:`py2:urllib.URLopener`
796 * :class:`py2:urllib.FancyURLopener`
797 * :func:`py2:urllib.proxy_bypass`
798
799 and :mod:`py2:urllib2`:
800
801 * :func:`py2:urllib2.urlopen`
802 * :func:`py2:urllib2.install_opener`
803 * :func:`py2:urllib2.build_opener`
804 * :func:`py2:urllib2.parse_http_list`
805 * :func:`py2:urllib2.parse_keqv_list`
806 * :class:`py2:urllib2.Request`
807 * :class:`py2:urllib2.OpenerDirector`
808 * :class:`py2:urllib2.HTTPDefaultErrorHandler`
809 * :class:`py2:urllib2.HTTPRedirectHandler`
810 * :class:`py2:urllib2.HTTPCookieProcessor`
811 * :class:`py2:urllib2.ProxyHandler`
812 * :class:`py2:urllib2.BaseHandler`
813 * :class:`py2:urllib2.HTTPPasswordMgr`
814 * :class:`py2:urllib2.HTTPPasswordMgrWithDefaultRealm`
815 * :class:`py2:urllib2.AbstractBasicAuthHandler`
816 * :class:`py2:urllib2.HTTPBasicAuthHandler`
817 * :class:`py2:urllib2.ProxyBasicAuthHandler`
818 * :class:`py2:urllib2.AbstractDigestAuthHandler`
819 * :class:`py2:urllib2.HTTPDigestAuthHandler`
820 * :class:`py2:urllib2.ProxyDigestAuthHandler`
821 * :class:`py2:urllib2.HTTPHandler`
822 * :class:`py2:urllib2.HTTPSHandler`
823 * :class:`py2:urllib2.FileHandler`
824 * :class:`py2:urllib2.FTPHandler`
825 * :class:`py2:urllib2.CacheFTPHandler`
826 * :class:`py2:urllib2.UnknownHandler`
827 * :class:`py2:urllib2.HTTPErrorProcessor`
828
829
830 urllib response
831 <<<<<<<<<<<<<<<
832
833 .. module:: six.moves.urllib.response
834 :synopsis: Stuff from :mod:`py2:urllib` in Python 2 and :mod:`py3:urllib.response` in Python 3
835
836 Contains classes from Python 3's :mod:`py3:urllib.response` and Python 2's:
837
838 :mod:`py2:urllib`:
839
840 * :class:`py2:urllib.addbase`
841 * :class:`py2:urllib.addclosehook`
842 * :class:`py2:urllib.addinfo`
843 * :class:`py2:urllib.addinfourl`
844
845
846 Advanced - Customizing renames
847 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
848
849 .. currentmodule:: six
850
851 It is possible to add additional names to the :mod:`six.moves` namespace.
852
853
854 .. function:: add_move(item)
855
856 Add *item* to the :mod:`six.moves` mapping. *item* should be a
857 :class:`MovedAttribute` or :class:`MovedModule` instance.
858
859
860 .. function:: remove_move(name)
861
862 Remove the :mod:`six.moves` mapping called *name*. *name* should be a
863 string.
864
865
866 Instances of the following classes can be passed to :func:`add_move`. Neither
867 have any public members.
868
869
870 .. class:: MovedModule(name, old_mod, new_mod)
871
872 Create a mapping for :mod:`six.moves` called *name* that references different
873 modules in Python 2 and 3. *old_mod* is the name of the Python 2 module.
874 *new_mod* is the name of the Python 3 module.
875
876
877 .. class:: MovedAttribute(name, old_mod, new_mod, old_attr=None, new_attr=None)
878
879 Create a mapping for :mod:`six.moves` called *name* that references different
880 attributes in Python 2 and 3. *old_mod* is the name of the Python 2 module.
881 *new_mod* is the name of the Python 3 module. If *new_attr* is not given, it
882 defaults to *old_attr*. If neither is given, they both default to *name*.