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