1 **************************** 2 What's New In Python 3.3 3 **************************** 4 5 .. Rules for maintenance: 6 7 * Anyone can add text to this document. Do not spend very much time 8 on the wording of your changes, because your text will probably 9 get rewritten to some degree. 10 11 * The maintainer will go through Misc/NEWS periodically and add 12 changes; it's therefore more important to add your changes to 13 Misc/NEWS than to this file. 14 15 * This is not a complete list of every single change; completeness 16 is the purpose of Misc/NEWS. Some changes I consider too small 17 or esoteric to include. If such a change is added to the text, 18 I'll just remove it. (This is another reason you shouldn't spend 19 too much time on writing your addition.) 20 21 * If you want to draw your new text to the attention of the 22 maintainer, add 'XXX' to the beginning of the paragraph or 23 section. 24 25 * It's OK to just add a fragmentary note about a change. For 26 example: "XXX Describe the transmogrify() function added to the 27 socket module." The maintainer will research the change and 28 write the necessary text. 29 30 * You can comment out your additions if you like, but it's not 31 necessary (especially when a final release is some months away). 32 33 * Credit the author of a patch or bugfix. Just the name is 34 sufficient; the e-mail address isn't necessary. 35 36 * It's helpful to add the bug/patch number as a comment: 37 38 XXX Describe the transmogrify() function added to the socket 39 module. 40 (Contributed by P.Y. Developer in :issue:`12345`.) 41 42 This saves the maintainer the effort of going through the Mercurial log 43 when researching a change. 44 45 This article explains the new features in Python 3.3, compared to 3.2. 46 Python 3.3 was released on September 29, 2012. For full details, 47 see the `changelog <https://docs.python.org/3.3/whatsnew/changelog.html>`_. 48 49 .. seealso:: 50 51 :pep:`398` - Python 3.3 Release Schedule 52 53 54 Summary -- Release highlights 55 ============================= 56 57 .. This section singles out the most important changes in Python 3.3. 58 Brevity is key. 59 60 New syntax features: 61 62 * New ``yield from`` expression for :ref:`generator delegation <pep-380>`. 63 * The ``u'unicode'`` syntax is accepted again for :class:`str` objects. 64 65 New library modules: 66 67 * :mod:`faulthandler` (helps debugging low-level crashes) 68 * :mod:`ipaddress` (high-level objects representing IP addresses and masks) 69 * :mod:`lzma` (compress data using the XZ / LZMA algorithm) 70 * :mod:`unittest.mock` (replace parts of your system under test with mock objects) 71 * :mod:`venv` (Python :ref:`virtual environments <pep-405>`, as in the 72 popular ``virtualenv`` package) 73 74 New built-in features: 75 76 * Reworked :ref:`I/O exception hierarchy <pep-3151>`. 77 78 Implementation improvements: 79 80 * Rewritten :ref:`import machinery <importlib>` based on :mod:`importlib`. 81 * More compact :ref:`unicode strings <pep-393>`. 82 * More compact :ref:`attribute dictionaries <pep-412>`. 83 84 Significantly Improved Library Modules: 85 86 * C Accelerator for the :ref:`decimal <new-decimal>` module. 87 * Better unicode handling in the :ref:`email <new-email>` module 88 (:term:`provisional <provisional package>`). 89 90 Security improvements: 91 92 * Hash randomization is switched on by default. 93 94 Please read on for a comprehensive list of user-facing changes. 95 96 97 .. _pep-405: 98 99 PEP 405: Virtual Environments 100 ============================= 101 102 Virtual environments help create separate Python setups while sharing a 103 system-wide base install, for ease of maintenance. Virtual environments 104 have their own set of private site packages (i.e. locally-installed 105 libraries), and are optionally segregated from the system-wide site 106 packages. Their concept and implementation are inspired by the popular 107 ``virtualenv`` third-party package, but benefit from tighter integration 108 with the interpreter core. 109 110 This PEP adds the :mod:`venv` module for programmatic access, and the 111 ``pyvenv`` script for command-line access and 112 administration. The Python interpreter checks for a ``pyvenv.cfg``, 113 file whose existence signals the base of a virtual environment's directory 114 tree. 115 116 .. seealso:: 117 118 :pep:`405` - Python Virtual Environments 119 PEP written by Carl Meyer; implementation by Carl Meyer and Vinay Sajip 120 121 122 PEP 420: Implicit Namespace Packages 123 ==================================== 124 125 Native support for package directories that don't require ``__init__.py`` 126 marker files and can automatically span multiple path segments (inspired by 127 various third party approaches to namespace packages, as described in 128 :pep:`420`) 129 130 .. seealso:: 131 132 :pep:`420` - Implicit Namespace Packages 133 PEP written by Eric V. Smith; implementation by Eric V. Smith 134 and Barry Warsaw 135 136 137 .. _pep-3118-update: 138 139 PEP 3118: New memoryview implementation and buffer protocol documentation 140 ========================================================================= 141 142 The implementation of :pep:`3118` has been significantly improved. 143 144 The new memoryview implementation comprehensively fixes all ownership and 145 lifetime issues of dynamically allocated fields in the Py_buffer struct 146 that led to multiple crash reports. Additionally, several functions that 147 crashed or returned incorrect results for non-contiguous or multi-dimensional 148 input have been fixed. 149 150 The memoryview object now has a PEP-3118 compliant getbufferproc() 151 that checks the consumer's request type. Many new features have been 152 added, most of them work in full generality for non-contiguous arrays 153 and arrays with suboffsets. 154 155 The documentation has been updated, clearly spelling out responsibilities 156 for both exporters and consumers. Buffer request flags are grouped into 157 basic and compound flags. The memory layout of non-contiguous and 158 multi-dimensional NumPy-style arrays is explained. 159 160 Features 161 -------- 162 163 * All native single character format specifiers in struct module syntax 164 (optionally prefixed with '@') are now supported. 165 166 * With some restrictions, the cast() method allows changing of format and 167 shape of C-contiguous arrays. 168 169 * Multi-dimensional list representations are supported for any array type. 170 171 * Multi-dimensional comparisons are supported for any array type. 172 173 * One-dimensional memoryviews of hashable (read-only) types with formats B, 174 b or c are now hashable. (Contributed by Antoine Pitrou in :issue:`13411`.) 175 176 * Arbitrary slicing of any 1-D arrays type is supported. For example, it 177 is now possible to reverse a memoryview in O(1) by using a negative step. 178 179 API changes 180 ----------- 181 182 * The maximum number of dimensions is officially limited to 64. 183 184 * The representation of empty shape, strides and suboffsets is now 185 an empty tuple instead of ``None``. 186 187 * Accessing a memoryview element with format 'B' (unsigned bytes) 188 now returns an integer (in accordance with the struct module syntax). 189 For returning a bytes object the view must be cast to 'c' first. 190 191 * memoryview comparisons now use the logical structure of the operands 192 and compare all array elements by value. All format strings in struct 193 module syntax are supported. Views with unrecognised format strings 194 are still permitted, but will always compare as unequal, regardless 195 of view contents. 196 197 * For further changes see `Build and C API Changes`_ and `Porting C code`_. 198 199 (Contributed by Stefan Krah in :issue:`10181`.) 200 201 .. seealso:: 202 203 :pep:`3118` - Revising the Buffer Protocol 204 205 206 .. _pep-393: 207 208 PEP 393: Flexible String Representation 209 ======================================= 210 211 The Unicode string type is changed to support multiple internal 212 representations, depending on the character with the largest Unicode ordinal 213 (1, 2, or 4 bytes) in the represented string. This allows a space-efficient 214 representation in common cases, but gives access to full UCS-4 on all 215 systems. For compatibility with existing APIs, several representations may 216 exist in parallel; over time, this compatibility should be phased out. 217 218 On the Python side, there should be no downside to this change. 219 220 On the C API side, PEP 393 is fully backward compatible. The legacy API 221 should remain available at least five years. Applications using the legacy 222 API will not fully benefit of the memory reduction, or - worse - may use 223 a bit more memory, because Python may have to maintain two versions of each 224 string (in the legacy format and in the new efficient storage). 225 226 Functionality 227 ------------- 228 229 Changes introduced by :pep:`393` are the following: 230 231 * Python now always supports the full range of Unicode code points, including 232 non-BMP ones (i.e. from ``U+0000`` to ``U+10FFFF``). The distinction between 233 narrow and wide builds no longer exists and Python now behaves like a wide 234 build, even under Windows. 235 236 * With the death of narrow builds, the problems specific to narrow builds have 237 also been fixed, for example: 238 239 * :func:`len` now always returns 1 for non-BMP characters, 240 so ``len('\U0010FFFF') == 1``; 241 242 * surrogate pairs are not recombined in string literals, 243 so ``'\uDBFF\uDFFF' != '\U0010FFFF'``; 244 245 * indexing or slicing non-BMP characters returns the expected value, 246 so ``'\U0010FFFF'[0]`` now returns ``'\U0010FFFF'`` and not ``'\uDBFF'``; 247 248 * all other functions in the standard library now correctly handle 249 non-BMP code points. 250 251 * The value of :data:`sys.maxunicode` is now always ``1114111`` (``0x10FFFF`` 252 in hexadecimal). The :c:func:`PyUnicode_GetMax` function still returns 253 either ``0xFFFF`` or ``0x10FFFF`` for backward compatibility, and it should 254 not be used with the new Unicode API (see :issue:`13054`). 255 256 * The :file:`./configure` flag ``--with-wide-unicode`` has been removed. 257 258 Performance and resource usage 259 ------------------------------ 260 261 The storage of Unicode strings now depends on the highest code point in the string: 262 263 * pure ASCII and Latin1 strings (``U+0000-U+00FF``) use 1 byte per code point; 264 265 * BMP strings (``U+0000-U+FFFF``) use 2 bytes per code point; 266 267 * non-BMP strings (``U+10000-U+10FFFF``) use 4 bytes per code point. 268 269 The net effect is that for most applications, memory usage of string 270 storage should decrease significantly - especially compared to former 271 wide unicode builds - as, in many cases, strings will be pure ASCII 272 even in international contexts (because many strings store non-human 273 language data, such as XML fragments, HTTP headers, JSON-encoded data, 274 etc.). We also hope that it will, for the same reasons, increase CPU 275 cache efficiency on non-trivial applications. The memory usage of 276 Python 3.3 is two to three times smaller than Python 3.2, and a little 277 bit better than Python 2.7, on a Django benchmark (see the PEP for 278 details). 279 280 .. seealso:: 281 282 :pep:`393` - Flexible String Representation 283 PEP written by Martin von Lwis; implementation by Torsten Becker 284 and Martin von Lwis. 285 286 287 .. _pep-397: 288 289 PEP 397: Python Launcher for Windows 290 ==================================== 291 292 The Python 3.3 Windows installer now includes a ``py`` launcher application 293 that can be used to launch Python applications in a version independent 294 fashion. 295 296 This launcher is invoked implicitly when double-clicking ``*.py`` files. 297 If only a single Python version is installed on the system, that version 298 will be used to run the file. If multiple versions are installed, the most 299 recent version is used by default, but this can be overridden by including 300 a Unix-style "shebang line" in the Python script. 301 302 The launcher can also be used explicitly from the command line as the ``py`` 303 application. Running ``py`` follows the same version selection rules as 304 implicitly launching scripts, but a more specific version can be selected 305 by passing appropriate arguments (such as ``-3`` to request Python 3 when 306 Python 2 is also installed, or ``-2.6`` to specifically request an earlier 307 Python version when a more recent version is installed). 308 309 In addition to the launcher, the Windows installer now includes an 310 option to add the newly installed Python to the system PATH. (Contributed 311 by Brian Curtin in :issue:`3561`.) 312 313 .. seealso:: 314 315 :pep:`397` - Python Launcher for Windows 316 PEP written by Mark Hammond and Martin v. Lwis; implementation by 317 Vinay Sajip. 318 319 Launcher documentation: :ref:`launcher` 320 321 Installer PATH modification: :ref:`windows-path-mod` 322 323 324 .. _pep-3151: 325 326 PEP 3151: Reworking the OS and IO exception hierarchy 327 ===================================================== 328 329 The hierarchy of exceptions raised by operating system errors is now both 330 simplified and finer-grained. 331 332 You don't have to worry anymore about choosing the appropriate exception 333 type between :exc:`OSError`, :exc:`IOError`, :exc:`EnvironmentError`, 334 :exc:`WindowsError`, :exc:`mmap.error`, :exc:`socket.error` or 335 :exc:`select.error`. All these exception types are now only one: 336 :exc:`OSError`. The other names are kept as aliases for compatibility 337 reasons. 338 339 Also, it is now easier to catch a specific error condition. Instead of 340 inspecting the ``errno`` attribute (or ``args[0]``) for a particular 341 constant from the :mod:`errno` module, you can catch the adequate 342 :exc:`OSError` subclass. The available subclasses are the following: 343 344 * :exc:`BlockingIOError` 345 * :exc:`ChildProcessError` 346 * :exc:`ConnectionError` 347 * :exc:`FileExistsError` 348 * :exc:`FileNotFoundError` 349 * :exc:`InterruptedError` 350 * :exc:`IsADirectoryError` 351 * :exc:`NotADirectoryError` 352 * :exc:`PermissionError` 353 * :exc:`ProcessLookupError` 354 * :exc:`TimeoutError` 355 356 And the :exc:`ConnectionError` itself has finer-grained subclasses: 357 358 * :exc:`BrokenPipeError` 359 * :exc:`ConnectionAbortedError` 360 * :exc:`ConnectionRefusedError` 361 * :exc:`ConnectionResetError` 362 363 Thanks to the new exceptions, common usages of the :mod:`errno` can now be 364 avoided. For example, the following code written for Python 3.2:: 365 366 from errno import ENOENT, EACCES, EPERM 367 368 try: 369 with open("document.txt") as f: 370 content = f.read() 371 except IOError as err: 372 if err.errno == ENOENT: 373 print("document.txt file is missing") 374 elif err.errno in (EACCES, EPERM): 375 print("You are not allowed to read document.txt") 376 else: 377 raise 378 379 can now be written without the :mod:`errno` import and without manual 380 inspection of exception attributes:: 381 382 try: 383 with open("document.txt") as f: 384 content = f.read() 385 except FileNotFoundError: 386 print("document.txt file is missing") 387 except PermissionError: 388 print("You are not allowed to read document.txt") 389 390 .. seealso:: 391 392 :pep:`3151` - Reworking the OS and IO Exception Hierarchy 393 PEP written and implemented by Antoine Pitrou 394 395 396 .. index:: 397 single: yield; yield from (in What's New) 398 399 .. _pep-380: 400 401 PEP 380: Syntax for Delegating to a Subgenerator 402 ================================================ 403 404 PEP 380 adds the ``yield from`` expression, allowing a :term:`generator` to 405 delegate 406 part of its operations to another generator. This allows a section of code 407 containing :keyword:`yield` to be factored out and placed in another generator. 408 Additionally, the subgenerator is allowed to return with a value, and the 409 value is made available to the delegating generator. 410 411 While designed primarily for use in delegating to a subgenerator, the ``yield 412 from`` expression actually allows delegation to arbitrary subiterators. 413 414 For simple iterators, ``yield from iterable`` is essentially just a shortened 415 form of ``for item in iterable: yield item``:: 416 417 >>> def g(x): 418 ... yield from range(x, 0, -1) 419 ... yield from range(x) 420 ... 421 >>> list(g(5)) 422 [5, 4, 3, 2, 1, 0, 1, 2, 3, 4] 423 424 However, unlike an ordinary loop, ``yield from`` allows subgenerators to 425 receive sent and thrown values directly from the calling scope, and 426 return a final value to the outer generator:: 427 428 >>> def accumulate(): 429 ... tally = 0 430 ... while 1: 431 ... next = yield 432 ... if next is None: 433 ... return tally 434 ... tally += next 435 ... 436 >>> def gather_tallies(tallies): 437 ... while 1: 438 ... tally = yield from accumulate() 439 ... tallies.append(tally) 440 ... 441 >>> tallies = [] 442 >>> acc = gather_tallies(tallies) 443 >>> next(acc) # Ensure the accumulator is ready to accept values 444 >>> for i in range(4): 445 ... acc.send(i) 446 ... 447 >>> acc.send(None) # Finish the first tally 448 >>> for i in range(5): 449 ... acc.send(i) 450 ... 451 >>> acc.send(None) # Finish the second tally 452 >>> tallies 453 [6, 10] 454 455 The main principle driving this change is to allow even generators that are 456 designed to be used with the ``send`` and ``throw`` methods to be split into 457 multiple subgenerators as easily as a single large function can be split into 458 multiple subfunctions. 459 460 .. seealso:: 461 462 :pep:`380` - Syntax for Delegating to a Subgenerator 463 PEP written by Greg Ewing; implementation by Greg Ewing, integrated into 464 3.3 by Renaud Blanch, Ryan Kelly and Nick Coghlan; documentation by 465 Zbigniew Jdrzejewski-Szmek and Nick Coghlan 466 467 468 PEP 409: Suppressing exception context 469 ====================================== 470 471 PEP 409 introduces new syntax that allows the display of the chained 472 exception context to be disabled. This allows cleaner error messages in 473 applications that convert between exception types:: 474 475 >>> class D: 476 ... def __init__(self, extra): 477 ... self._extra_attributes = extra 478 ... def __getattr__(self, attr): 479 ... try: 480 ... return self._extra_attributes[attr] 481 ... except KeyError: 482 ... raise AttributeError(attr) from None 483 ... 484 >>> D({}).x 485 Traceback (most recent call last): 486 File "<stdin>", line 1, in <module> 487 File "<stdin>", line 8, in __getattr__ 488 AttributeError: x 489 490 Without the ``from None`` suffix to suppress the cause, the original 491 exception would be displayed by default:: 492 493 >>> class C: 494 ... def __init__(self, extra): 495 ... self._extra_attributes = extra 496 ... def __getattr__(self, attr): 497 ... try: 498 ... return self._extra_attributes[attr] 499 ... except KeyError: 500 ... raise AttributeError(attr) 501 ... 502 >>> C({}).x 503 Traceback (most recent call last): 504 File "<stdin>", line 6, in __getattr__ 505 KeyError: 'x' 506 507 During handling of the above exception, another exception occurred: 508 509 Traceback (most recent call last): 510 File "<stdin>", line 1, in <module> 511 File "<stdin>", line 8, in __getattr__ 512 AttributeError: x 513 514 No debugging capability is lost, as the original exception context remains 515 available if needed (for example, if an intervening library has incorrectly 516 suppressed valuable underlying details):: 517 518 >>> try: 519 ... D({}).x 520 ... except AttributeError as exc: 521 ... print(repr(exc.__context__)) 522 ... 523 KeyError('x',) 524 525 .. seealso:: 526 527 :pep:`409` - Suppressing exception context 528 PEP written by Ethan Furman; implemented by Ethan Furman and Nick 529 Coghlan. 530 531 532 PEP 414: Explicit Unicode literals 533 ====================================== 534 535 To ease the transition from Python 2 for Unicode aware Python applications 536 that make heavy use of Unicode literals, Python 3.3 once again supports the 537 "``u``" prefix for string literals. This prefix has no semantic significance 538 in Python 3, it is provided solely to reduce the number of purely mechanical 539 changes in migrating to Python 3, making it easier for developers to focus on 540 the more significant semantic changes (such as the stricter default 541 separation of binary and text data). 542 543 .. seealso:: 544 545 :pep:`414` - Explicit Unicode literals 546 PEP written by Armin Ronacher. 547 548 549 PEP 3155: Qualified name for classes and functions 550 ================================================== 551 552 Functions and class objects have a new ``__qualname__`` attribute representing 553 the "path" from the module top-level to their definition. For global functions 554 and classes, this is the same as ``__name__``. For other functions and classes, 555 it provides better information about where they were actually defined, and 556 how they might be accessible from the global scope. 557 558 Example with (non-bound) methods:: 559 560 >>> class C: 561 ... def meth(self): 562 ... pass 563 >>> C.meth.__name__ 564 'meth' 565 >>> C.meth.__qualname__ 566 'C.meth' 567 568 Example with nested classes:: 569 570 >>> class C: 571 ... class D: 572 ... def meth(self): 573 ... pass 574 ... 575 >>> C.D.__name__ 576 'D' 577 >>> C.D.__qualname__ 578 'C.D' 579 >>> C.D.meth.__name__ 580 'meth' 581 >>> C.D.meth.__qualname__ 582 'C.D.meth' 583 584 Example with nested functions:: 585 586 >>> def outer(): 587 ... def inner(): 588 ... pass 589 ... return inner 590 ... 591 >>> outer().__name__ 592 'inner' 593 >>> outer().__qualname__ 594 'outer.<locals>.inner' 595 596 The string representation of those objects is also changed to include the 597 new, more precise information:: 598 599 >>> str(C.D) 600 "<class '__main__.C.D'>" 601 >>> str(C.D.meth) 602 '<function C.D.meth at 0x7f46b9fe31e0>' 603 604 .. seealso:: 605 606 :pep:`3155` - Qualified name for classes and functions 607 PEP written and implemented by Antoine Pitrou. 608 609 610 .. _pep-412: 611 612 PEP 412: Key-Sharing Dictionary 613 =============================== 614 615 Dictionaries used for the storage of objects' attributes are now able to 616 share part of their internal storage between each other (namely, the part 617 which stores the keys and their respective hashes). This reduces the memory 618 consumption of programs creating many instances of non-builtin types. 619 620 .. seealso:: 621 622 :pep:`412` - Key-Sharing Dictionary 623 PEP written and implemented by Mark Shannon. 624 625 626 PEP 362: Function Signature Object 627 ================================== 628 629 A new function :func:`inspect.signature` makes introspection of python 630 callables easy and straightforward. A broad range of callables is supported: 631 python functions, decorated or not, classes, and :func:`functools.partial` 632 objects. New classes :class:`inspect.Signature`, :class:`inspect.Parameter` 633 and :class:`inspect.BoundArguments` hold information about the call signatures, 634 such as, annotations, default values, parameters kinds, and bound arguments, 635 which considerably simplifies writing decorators and any code that validates 636 or amends calling signatures or arguments. 637 638 .. seealso:: 639 640 :pep:`362`: - Function Signature Object 641 PEP written by Brett Cannon, Yury Selivanov, Larry Hastings, Jiwon Seo; 642 implemented by Yury Selivanov. 643 644 645 PEP 421: Adding sys.implementation 646 ================================== 647 648 A new attribute on the :mod:`sys` module exposes details specific to the 649 implementation of the currently running interpreter. The initial set of 650 attributes on :attr:`sys.implementation` are ``name``, ``version``, 651 ``hexversion``, and ``cache_tag``. 652 653 The intention of ``sys.implementation`` is to consolidate into one namespace 654 the implementation-specific data used by the standard library. This allows 655 different Python implementations to share a single standard library code base 656 much more easily. In its initial state, ``sys.implementation`` holds only a 657 small portion of the implementation-specific data. Over time that ratio will 658 shift in order to make the standard library more portable. 659 660 One example of improved standard library portability is ``cache_tag``. As of 661 Python 3.3, ``sys.implementation.cache_tag`` is used by :mod:`importlib` to 662 support :pep:`3147` compliance. Any Python implementation that uses 663 ``importlib`` for its built-in import system may use ``cache_tag`` to control 664 the caching behavior for modules. 665 666 SimpleNamespace 667 --------------- 668 669 The implementation of ``sys.implementation`` also introduces a new type to 670 Python: :class:`types.SimpleNamespace`. In contrast to a mapping-based 671 namespace, like :class:`dict`, ``SimpleNamespace`` is attribute-based, like 672 :class:`object`. However, unlike ``object``, ``SimpleNamespace`` instances 673 are writable. This means that you can add, remove, and modify the namespace 674 through normal attribute access. 675 676 .. seealso:: 677 678 :pep:`421` - Adding sys.implementation 679 PEP written and implemented by Eric Snow. 680 681 682 .. _importlib: 683 684 Using importlib as the Implementation of Import 685 =============================================== 686 :issue:`2377` - Replace __import__ w/ importlib.__import__ 687 :issue:`13959` - Re-implement parts of :mod:`imp` in pure Python 688 :issue:`14605` - Make import machinery explicit 689 :issue:`14646` - Require loaders set __loader__ and __package__ 690 691 The :func:`__import__` function is now powered by :func:`importlib.__import__`. 692 This work leads to the completion of "phase 2" of :pep:`302`. There are 693 multiple benefits to this change. First, it has allowed for more of the 694 machinery powering import to be exposed instead of being implicit and hidden 695 within the C code. It also provides a single implementation for all Python VMs 696 supporting Python 3.3 to use, helping to end any VM-specific deviations in 697 import semantics. And finally it eases the maintenance of import, allowing for 698 future growth to occur. 699 700 For the common user, there should be no visible change in semantics. For 701 those whose code currently manipulates import or calls import 702 programmatically, the code changes that might possibly be required are covered 703 in the `Porting Python code`_ section of this document. 704 705 New APIs 706 -------- 707 One of the large benefits of this work is the exposure of what goes into 708 making the import statement work. That means the various importers that were 709 once implicit are now fully exposed as part of the :mod:`importlib` package. 710 711 The abstract base classes defined in :mod:`importlib.abc` have been expanded 712 to properly delineate between :term:`meta path finders <meta path finder>` 713 and :term:`path entry finders <path entry finder>` by introducing 714 :class:`importlib.abc.MetaPathFinder` and 715 :class:`importlib.abc.PathEntryFinder`, respectively. The old ABC of 716 :class:`importlib.abc.Finder` is now only provided for backwards-compatibility 717 and does not enforce any method requirements. 718 719 In terms of finders, :class:`importlib.machinery.FileFinder` exposes the 720 mechanism used to search for source and bytecode files of a module. Previously 721 this class was an implicit member of :attr:`sys.path_hooks`. 722 723 For loaders, the new abstract base class :class:`importlib.abc.FileLoader` helps 724 write a loader that uses the file system as the storage mechanism for a module's 725 code. The loader for source files 726 (:class:`importlib.machinery.SourceFileLoader`), sourceless bytecode files 727 (:class:`importlib.machinery.SourcelessFileLoader`), and extension modules 728 (:class:`importlib.machinery.ExtensionFileLoader`) are now available for 729 direct use. 730 731 :exc:`ImportError` now has ``name`` and ``path`` attributes which are set when 732 there is relevant data to provide. The message for failed imports will also 733 provide the full name of the module now instead of just the tail end of the 734 module's name. 735 736 The :func:`importlib.invalidate_caches` function will now call the method with 737 the same name on all finders cached in :attr:`sys.path_importer_cache` to help 738 clean up any stored state as necessary. 739 740 Visible Changes 741 --------------- 742 743 For potential required changes to code, see the `Porting Python code`_ 744 section. 745 746 Beyond the expanse of what :mod:`importlib` now exposes, there are other 747 visible changes to import. The biggest is that :attr:`sys.meta_path` and 748 :attr:`sys.path_hooks` now store all of the meta path finders and path entry 749 hooks used by import. Previously the finders were implicit and hidden within 750 the C code of import instead of being directly exposed. This means that one can 751 now easily remove or change the order of the various finders to fit one's needs. 752 753 Another change is that all modules have a ``__loader__`` attribute, storing the 754 loader used to create the module. :pep:`302` has been updated to make this 755 attribute mandatory for loaders to implement, so in the future once 3rd-party 756 loaders have been updated people will be able to rely on the existence of the 757 attribute. Until such time, though, import is setting the module post-load. 758 759 Loaders are also now expected to set the ``__package__`` attribute from 760 :pep:`366`. Once again, import itself is already setting this on all loaders 761 from :mod:`importlib` and import itself is setting the attribute post-load. 762 763 ``None`` is now inserted into :attr:`sys.path_importer_cache` when no finder 764 can be found on :attr:`sys.path_hooks`. Since :class:`imp.NullImporter` is not 765 directly exposed on :attr:`sys.path_hooks` it could no longer be relied upon to 766 always be available to use as a value representing no finder found. 767 768 All other changes relate to semantic changes which should be taken into 769 consideration when updating code for Python 3.3, and thus should be read about 770 in the `Porting Python code`_ section of this document. 771 772 (Implementation by Brett Cannon) 773 774 775 Other Language Changes 776 ====================== 777 778 Some smaller changes made to the core Python language are: 779 780 * Added support for Unicode name aliases and named sequences. 781 Both :func:`unicodedata.lookup()` and ``'\N{...}'`` now resolve name aliases, 782 and :func:`unicodedata.lookup()` resolves named sequences too. 783 784 (Contributed by Ezio Melotti in :issue:`12753`.) 785 786 * Unicode database updated to UCD version 6.1.0 787 788 * Equality comparisons on :func:`range` objects now return a result reflecting 789 the equality of the underlying sequences generated by those range objects. 790 (:issue:`13201`) 791 792 * The ``count()``, ``find()``, ``rfind()``, ``index()`` and ``rindex()`` 793 methods of :class:`bytes` and :class:`bytearray` objects now accept an 794 integer between 0 and 255 as their first argument. 795 796 (Contributed by Petri Lehtinen in :issue:`12170`.) 797 798 * The ``rjust()``, ``ljust()``, and ``center()`` methods of :class:`bytes` 799 and :class:`bytearray` now accept a :class:`bytearray` for the ``fill`` 800 argument. (Contributed by Petri Lehtinen in :issue:`12380`.) 801 802 * New methods have been added to :class:`list` and :class:`bytearray`: 803 ``copy()`` and ``clear()`` (:issue:`10516`). Consequently, 804 :class:`~collections.abc.MutableSequence` now also defines a 805 :meth:`~collections.abc.MutableSequence.clear` method (:issue:`11388`). 806 807 * Raw bytes literals can now be written ``rb"..."`` as well as ``br"..."``. 808 809 (Contributed by Antoine Pitrou in :issue:`13748`.) 810 811 * :meth:`dict.setdefault` now does only one lookup for the given key, making 812 it atomic when used with built-in types. 813 814 (Contributed by Filip Gruszczyski in :issue:`13521`.) 815 816 * The error messages produced when a function call does not match the function 817 signature have been significantly improved. 818 819 (Contributed by Benjamin Peterson.) 820 821 822 A Finer-Grained Import Lock 823 =========================== 824 825 Previous versions of CPython have always relied on a global import lock. 826 This led to unexpected annoyances, such as deadlocks when importing a module 827 would trigger code execution in a different thread as a side-effect. 828 Clumsy workarounds were sometimes employed, such as the 829 :c:func:`PyImport_ImportModuleNoBlock` C API function. 830 831 In Python 3.3, importing a module takes a per-module lock. This correctly 832 serializes importation of a given module from multiple threads (preventing 833 the exposure of incompletely initialized modules), while eliminating the 834 aforementioned annoyances. 835 836 (Contributed by Antoine Pitrou in :issue:`9260`.) 837 838 839 Builtin functions and types 840 =========================== 841 842 * :func:`open` gets a new *opener* parameter: the underlying file descriptor 843 for the file object is then obtained by calling *opener* with (*file*, 844 *flags*). It can be used to use custom flags like :data:`os.O_CLOEXEC` for 845 example. The ``'x'`` mode was added: open for exclusive creation, failing if 846 the file already exists. 847 * :func:`print`: added the *flush* keyword argument. If the *flush* keyword 848 argument is true, the stream is forcibly flushed. 849 * :func:`hash`: hash randomization is enabled by default, see 850 :meth:`object.__hash__` and :envvar:`PYTHONHASHSEED`. 851 * The :class:`str` type gets a new :meth:`~str.casefold` method: return a 852 casefolded copy of the string, casefolded strings may be used for caseless 853 matching. For example, ``''.casefold()`` returns ``'ss'``. 854 * The sequence documentation has been substantially rewritten to better 855 explain the binary/text sequence distinction and to provide specific 856 documentation sections for the individual builtin sequence types 857 (:issue:`4966`). 858 859 860 New Modules 861 =========== 862 863 faulthandler 864 ------------ 865 866 This new debug module :mod:`faulthandler` contains functions to dump Python tracebacks explicitly, 867 on a fault (a crash like a segmentation fault), after a timeout, or on a user 868 signal. Call :func:`faulthandler.enable` to install fault handlers for the 869 :const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS`, and 870 :const:`SIGILL` signals. You can also enable them at startup by setting the 871 :envvar:`PYTHONFAULTHANDLER` environment variable or by using :option:`-X` 872 ``faulthandler`` command line option. 873 874 Example of a segmentation fault on Linux: 875 876 .. code-block:: shell-session 877 878 $ python -q -X faulthandler 879 >>> import ctypes 880 >>> ctypes.string_at(0) 881 Fatal Python error: Segmentation fault 882 883 Current thread 0x00007fb899f39700: 884 File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at 885 File "<stdin>", line 1 in <module> 886 Segmentation fault 887 888 889 ipaddress 890 --------- 891 892 The new :mod:`ipaddress` module provides tools for creating and manipulating 893 objects representing IPv4 and IPv6 addresses, networks and interfaces (i.e. 894 an IP address associated with a specific IP subnet). 895 896 (Contributed by Google and Peter Moody in :pep:`3144`.) 897 898 lzma 899 ---- 900 901 The newly-added :mod:`lzma` module provides data compression and decompression 902 using the LZMA algorithm, including support for the ``.xz`` and ``.lzma`` 903 file formats. 904 905 (Contributed by Nadeem Vawda and Per yvind Karlsen in :issue:`6715`.) 906 907 908 Improved Modules 909 ================ 910 911 abc 912 --- 913 914 Improved support for abstract base classes containing descriptors composed with 915 abstract methods. The recommended approach to declaring abstract descriptors is 916 now to provide :attr:`__isabstractmethod__` as a dynamically updated 917 property. The built-in descriptors have been updated accordingly. 918 919 * :class:`abc.abstractproperty` has been deprecated, use :class:`property` 920 with :func:`abc.abstractmethod` instead. 921 * :class:`abc.abstractclassmethod` has been deprecated, use 922 :class:`classmethod` with :func:`abc.abstractmethod` instead. 923 * :class:`abc.abstractstaticmethod` has been deprecated, use 924 :class:`staticmethod` with :func:`abc.abstractmethod` instead. 925 926 (Contributed by Darren Dale in :issue:`11610`.) 927 928 :meth:`abc.ABCMeta.register` now returns the registered subclass, which means 929 it can now be used as a class decorator (:issue:`10868`). 930 931 932 array 933 ----- 934 935 The :mod:`array` module supports the :c:type:`long long` type using ``q`` and 936 ``Q`` type codes. 937 938 (Contributed by Oren Tirosh and Hirokazu Yamamoto in :issue:`1172711`.) 939 940 941 base64 942 ------ 943 944 ASCII-only Unicode strings are now accepted by the decoding functions of the 945 :mod:`base64` modern interface. For example, ``base64.b64decode('YWJj')`` 946 returns ``b'abc'``. (Contributed by Catalin Iacob in :issue:`13641`.) 947 948 949 binascii 950 -------- 951 952 In addition to the binary objects they normally accept, the ``a2b_`` functions 953 now all also accept ASCII-only strings as input. (Contributed by Antoine 954 Pitrou in :issue:`13637`.) 955 956 957 bz2 958 --- 959 960 The :mod:`bz2` module has been rewritten from scratch. In the process, several 961 new features have been added: 962 963 * New :func:`bz2.open` function: open a bzip2-compressed file in binary or 964 text mode. 965 966 * :class:`bz2.BZ2File` can now read from and write to arbitrary file-like 967 objects, by means of its constructor's *fileobj* argument. 968 969 (Contributed by Nadeem Vawda in :issue:`5863`.) 970 971 * :class:`bz2.BZ2File` and :func:`bz2.decompress` can now decompress 972 multi-stream inputs (such as those produced by the :program:`pbzip2` tool). 973 :class:`bz2.BZ2File` can now also be used to create this type of file, using 974 the ``'a'`` (append) mode. 975 976 (Contributed by Nir Aides in :issue:`1625`.) 977 978 * :class:`bz2.BZ2File` now implements all of the :class:`io.BufferedIOBase` API, 979 except for the :meth:`detach` and :meth:`truncate` methods. 980 981 982 codecs 983 ------ 984 985 The :mod:`~encodings.mbcs` codec has been rewritten to handle correctly 986 ``replace`` and ``ignore`` error handlers on all Windows versions. The 987 :mod:`~encodings.mbcs` codec now supports all error handlers, instead of only 988 ``replace`` to encode and ``ignore`` to decode. 989 990 A new Windows-only codec has been added: ``cp65001`` (:issue:`13216`). It is the 991 Windows code page 65001 (Windows UTF-8, ``CP_UTF8``). For example, it is used 992 by ``sys.stdout`` if the console output code page is set to cp65001 (e.g., using 993 ``chcp 65001`` command). 994 995 Multibyte CJK decoders now resynchronize faster. They only ignore the first 996 byte of an invalid byte sequence. For example, ``b'\xff\n'.decode('gb2312', 997 'replace')`` now returns a ``\n`` after the replacement character. 998 999 (:issue:`12016`) 1000 1001 Incremental CJK codec encoders are no longer reset at each call to their 1002 encode() methods. For example:: 1003 1004 >>> import codecs 1005 >>> encoder = codecs.getincrementalencoder('hz')('strict') 1006 >>> b''.join(encoder.encode(x) for x in '\u52ff\u65bd\u65bc\u4eba\u3002 Bye.') 1007 b'~{NpJ)l6HK!#~} Bye.' 1008 1009 This example gives ``b'~{Np~}~{J)~}~{l6~}~{HK~}~{!#~} Bye.'`` with older Python 1010 versions. 1011 1012 (:issue:`12100`) 1013 1014 The ``unicode_internal`` codec has been deprecated. 1015 1016 1017 collections 1018 ----------- 1019 1020 Addition of a new :class:`~collections.ChainMap` class to allow treating a 1021 number of mappings as a single unit. (Written by Raymond Hettinger for 1022 :issue:`11089`, made public in :issue:`11297`.) 1023 1024 The abstract base classes have been moved in a new :mod:`collections.abc` 1025 module, to better differentiate between the abstract and the concrete 1026 collections classes. Aliases for ABCs are still present in the 1027 :mod:`collections` module to preserve existing imports. (:issue:`11085`) 1028 1029 .. XXX addition of __slots__ to ABCs not recorded here: internal detail 1030 1031 The :class:`~collections.Counter` class now supports the unary ``+`` and ``-`` 1032 operators, as well as the in-place operators ``+=``, ``-=``, ``|=``, and 1033 ``&=``. (Contributed by Raymond Hettinger in :issue:`13121`.) 1034 1035 1036 contextlib 1037 ---------- 1038 1039 :class:`~contextlib.ExitStack` now provides a solid foundation for 1040 programmatic manipulation of context managers and similar cleanup 1041 functionality. Unlike the previous ``contextlib.nested`` API (which was 1042 deprecated and removed), the new API is designed to work correctly 1043 regardless of whether context managers acquire their resources in 1044 their ``__init__`` method (for example, file objects) or in their 1045 ``__enter__`` method (for example, synchronisation objects from the 1046 :mod:`threading` module). 1047 1048 (:issue:`13585`) 1049 1050 1051 crypt 1052 ----- 1053 1054 Addition of salt and modular crypt format (hashing method) and the :func:`~crypt.mksalt` 1055 function to the :mod:`crypt` module. 1056 1057 (:issue:`10924`) 1058 1059 curses 1060 ------ 1061 1062 * If the :mod:`curses` module is linked to the ncursesw library, use Unicode 1063 functions when Unicode strings or characters are passed (e.g. 1064 :c:func:`waddwstr`), and bytes functions otherwise (e.g. :c:func:`waddstr`). 1065 * Use the locale encoding instead of ``utf-8`` to encode Unicode strings. 1066 * :class:`curses.window` has a new :attr:`curses.window.encoding` attribute. 1067 * The :class:`curses.window` class has a new :meth:`~curses.window.get_wch` 1068 method to get a wide character 1069 * The :mod:`curses` module has a new :meth:`~curses.unget_wch` function to 1070 push a wide character so the next :meth:`~curses.window.get_wch` will return 1071 it 1072 1073 (Contributed by Iigo Serna in :issue:`6755`.) 1074 1075 datetime 1076 -------- 1077 1078 * Equality comparisons between naive and aware :class:`~datetime.datetime` 1079 instances now return :const:`False` instead of raising :exc:`TypeError` 1080 (:issue:`15006`). 1081 * New :meth:`datetime.datetime.timestamp` method: Return POSIX timestamp 1082 corresponding to the :class:`~datetime.datetime` instance. 1083 * The :meth:`datetime.datetime.strftime` method supports formatting years 1084 older than 1000. 1085 * The :meth:`datetime.datetime.astimezone` method can now be 1086 called without arguments to convert datetime instance to the system 1087 timezone. 1088 1089 1090 .. _new-decimal: 1091 1092 decimal 1093 ------- 1094 1095 :issue:`7652` - integrate fast native decimal arithmetic. 1096 C-module and libmpdec written by Stefan Krah. 1097 1098 The new C version of the decimal module integrates the high speed libmpdec 1099 library for arbitrary precision correctly-rounded decimal floating point 1100 arithmetic. libmpdec conforms to IBM's General Decimal Arithmetic Specification. 1101 1102 Performance gains range from 10x for database applications to 100x for 1103 numerically intensive applications. These numbers are expected gains 1104 for standard precisions used in decimal floating point arithmetic. Since 1105 the precision is user configurable, the exact figures may vary. For example, 1106 in integer bignum arithmetic the differences can be significantly higher. 1107 1108 The following table is meant as an illustration. Benchmarks are available 1109 at http://www.bytereef.org/mpdecimal/quickstart.html. 1110 1111 +---------+-------------+--------------+-------------+ 1112 | | decimal.py | _decimal | speedup | 1113 +=========+=============+==============+=============+ 1114 | pi | 42.02s | 0.345s | 120x | 1115 +---------+-------------+--------------+-------------+ 1116 | telco | 172.19s | 5.68s | 30x | 1117 +---------+-------------+--------------+-------------+ 1118 | psycopg | 3.57s | 0.29s | 12x | 1119 +---------+-------------+--------------+-------------+ 1120 1121 Features 1122 ~~~~~~~~ 1123 1124 * The :exc:`~decimal.FloatOperation` signal optionally enables stricter 1125 semantics for mixing floats and Decimals. 1126 1127 * If Python is compiled without threads, the C version automatically 1128 disables the expensive thread local context machinery. In this case, 1129 the variable :data:`~decimal.HAVE_THREADS` is set to ``False``. 1130 1131 API changes 1132 ~~~~~~~~~~~ 1133 1134 * The C module has the following context limits, depending on the machine 1135 architecture: 1136 1137 +-------------------+---------------------+------------------------------+ 1138 | | 32-bit | 64-bit | 1139 +===================+=====================+==============================+ 1140 | :const:`MAX_PREC` | :const:`425000000` | :const:`999999999999999999` | 1141 +-------------------+---------------------+------------------------------+ 1142 | :const:`MAX_EMAX` | :const:`425000000` | :const:`999999999999999999` | 1143 +-------------------+---------------------+------------------------------+ 1144 | :const:`MIN_EMIN` | :const:`-425000000` | :const:`-999999999999999999` | 1145 +-------------------+---------------------+------------------------------+ 1146 1147 * In the context templates (:class:`~decimal.DefaultContext`, 1148 :class:`~decimal.BasicContext` and :class:`~decimal.ExtendedContext`) 1149 the magnitude of :attr:`~decimal.Context.Emax` and 1150 :attr:`~decimal.Context.Emin` has changed to :const:`999999`. 1151 1152 * The :class:`~decimal.Decimal` constructor in decimal.py does not observe 1153 the context limits and converts values with arbitrary exponents or precision 1154 exactly. Since the C version has internal limits, the following scheme is 1155 used: If possible, values are converted exactly, otherwise 1156 :exc:`~decimal.InvalidOperation` is raised and the result is NaN. In the 1157 latter case it is always possible to use :meth:`~decimal.Context.create_decimal` 1158 in order to obtain a rounded or inexact value. 1159 1160 1161 * The power function in decimal.py is always correctly-rounded. In the 1162 C version, it is defined in terms of the correctly-rounded 1163 :meth:`~decimal.Decimal.exp` and :meth:`~decimal.Decimal.ln` functions, 1164 but the final result is only "almost always correctly rounded". 1165 1166 1167 * In the C version, the context dictionary containing the signals is a 1168 :class:`~collections.abc.MutableMapping`. For speed reasons, 1169 :attr:`~decimal.Context.flags` and :attr:`~decimal.Context.traps` always 1170 refer to the same :class:`~collections.abc.MutableMapping` that the context 1171 was initialized with. If a new signal dictionary is assigned, 1172 :attr:`~decimal.Context.flags` and :attr:`~decimal.Context.traps` 1173 are updated with the new values, but they do not reference the RHS 1174 dictionary. 1175 1176 1177 * Pickling a :class:`~decimal.Context` produces a different output in order 1178 to have a common interchange format for the Python and C versions. 1179 1180 1181 * The order of arguments in the :class:`~decimal.Context` constructor has been 1182 changed to match the order displayed by :func:`repr`. 1183 1184 1185 * The ``watchexp`` parameter in the :meth:`~decimal.Decimal.quantize` method 1186 is deprecated. 1187 1188 1189 .. _new-email: 1190 1191 email 1192 ----- 1193 1194 Policy Framework 1195 ~~~~~~~~~~~~~~~~ 1196 1197 The email package now has a :mod:`~email.policy` framework. A 1198 :class:`~email.policy.Policy` is an object with several methods and properties 1199 that control how the email package behaves. The primary policy for Python 3.3 1200 is the :class:`~email.policy.Compat32` policy, which provides backward 1201 compatibility with the email package in Python 3.2. A ``policy`` can be 1202 specified when an email message is parsed by a :mod:`~email.parser`, or when a 1203 :class:`~email.message.Message` object is created, or when an email is 1204 serialized using a :mod:`~email.generator`. Unless overridden, a policy passed 1205 to a ``parser`` is inherited by all the ``Message`` object and sub-objects 1206 created by the ``parser``. By default a ``generator`` will use the policy of 1207 the ``Message`` object it is serializing. The default policy is 1208 :data:`~email.policy.compat32`. 1209 1210 The minimum set of controls implemented by all ``policy`` objects are: 1211 1212 .. tabularcolumns:: |l|L| 1213 1214 =============== ======================================================= 1215 max_line_length The maximum length, excluding the linesep character(s), 1216 individual lines may have when a ``Message`` is 1217 serialized. Defaults to 78. 1218 1219 linesep The character used to separate individual lines when a 1220 ``Message`` is serialized. Defaults to ``\n``. 1221 1222 cte_type ``7bit`` or ``8bit``. ``8bit`` applies only to a 1223 ``Bytes`` ``generator``, and means that non-ASCII may 1224 be used where allowed by the protocol (or where it 1225 exists in the original input). 1226 1227 raise_on_defect Causes a ``parser`` to raise error when defects are 1228 encountered instead of adding them to the ``Message`` 1229 object's ``defects`` list. 1230 =============== ======================================================= 1231 1232 A new policy instance, with new settings, is created using the 1233 :meth:`~email.policy.Policy.clone` method of policy objects. ``clone`` takes 1234 any of the above controls as keyword arguments. Any control not specified in 1235 the call retains its default value. Thus you can create a policy that uses 1236 ``\r\n`` linesep characters like this:: 1237 1238 mypolicy = compat32.clone(linesep='\r\n') 1239 1240 Policies can be used to make the generation of messages in the format needed by 1241 your application simpler. Instead of having to remember to specify 1242 ``linesep='\r\n'`` in all the places you call a ``generator``, you can specify 1243 it once, when you set the policy used by the ``parser`` or the ``Message``, 1244 whichever your program uses to create ``Message`` objects. On the other hand, 1245 if you need to generate messages in multiple forms, you can still specify the 1246 parameters in the appropriate ``generator`` call. Or you can have custom 1247 policy instances for your different cases, and pass those in when you create 1248 the ``generator``. 1249 1250 1251 Provisional Policy with New Header API 1252 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1253 1254 While the policy framework is worthwhile all by itself, the main motivation for 1255 introducing it is to allow the creation of new policies that implement new 1256 features for the email package in a way that maintains backward compatibility 1257 for those who do not use the new policies. Because the new policies introduce a 1258 new API, we are releasing them in Python 3.3 as a :term:`provisional policy 1259 <provisional package>`. Backwards incompatible changes (up to and including 1260 removal of the code) may occur if deemed necessary by the core developers. 1261 1262 The new policies are instances of :class:`~email.policy.EmailPolicy`, 1263 and add the following additional controls: 1264 1265 .. tabularcolumns:: |l|L| 1266 1267 =============== ======================================================= 1268 refold_source Controls whether or not headers parsed by a 1269 :mod:`~email.parser` are refolded by the 1270 :mod:`~email.generator`. It can be ``none``, ``long``, 1271 or ``all``. The default is ``long``, which means that 1272 source headers with a line longer than 1273 ``max_line_length`` get refolded. ``none`` means no 1274 line get refolded, and ``all`` means that all lines 1275 get refolded. 1276 1277 header_factory A callable that take a ``name`` and ``value`` and 1278 produces a custom header object. 1279 =============== ======================================================= 1280 1281 The ``header_factory`` is the key to the new features provided by the new 1282 policies. When one of the new policies is used, any header retrieved from 1283 a ``Message`` object is an object produced by the ``header_factory``, and any 1284 time you set a header on a ``Message`` it becomes an object produced by 1285 ``header_factory``. All such header objects have a ``name`` attribute equal 1286 to the header name. Address and Date headers have additional attributes 1287 that give you access to the parsed data of the header. This means you can now 1288 do things like this:: 1289 1290 >>> m = Message(policy=SMTP) 1291 >>> m['To'] = 'ric <foo (a] example.com>' 1292 >>> m['to'] 1293 'ric <foo (a] example.com>' 1294 >>> m['to'].addresses 1295 (Address(display_name='ric', username='foo', domain='example.com'),) 1296 >>> m['to'].addresses[0].username 1297 'foo' 1298 >>> m['to'].addresses[0].display_name 1299 'ric' 1300 >>> m['Date'] = email.utils.localtime() 1301 >>> m['Date'].datetime 1302 datetime.datetime(2012, 5, 25, 21, 39, 24, 465484, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000), 'EDT')) 1303 >>> m['Date'] 1304 'Fri, 25 May 2012 21:44:27 -0400' 1305 >>> print(m) 1306 To: =?utf-8?q?=C3=89ric?= <foo (a] example.com> 1307 Date: Fri, 25 May 2012 21:44:27 -0400 1308 1309 You will note that the unicode display name is automatically encoded as 1310 ``utf-8`` when the message is serialized, but that when the header is accessed 1311 directly, you get the unicode version. This eliminates any need to deal with 1312 the :mod:`email.header` :meth:`~email.header.decode_header` or 1313 :meth:`~email.header.make_header` functions. 1314 1315 You can also create addresses from parts:: 1316 1317 >>> m['cc'] = [Group('pals', [Address('Bob', 'bob', 'example.com'), 1318 ... Address('Sally', 'sally', 'example.com')]), 1319 ... Address('Bonzo', addr_spec='bonz (a] laugh.com')] 1320 >>> print(m) 1321 To: =?utf-8?q?=C3=89ric?= <foo (a] example.com> 1322 Date: Fri, 25 May 2012 21:44:27 -0400 1323 cc: pals: Bob <bob (a] example.com>, Sally <sally (a] example.com>;, Bonzo <bonz (a] laugh.com> 1324 1325 Decoding to unicode is done automatically:: 1326 1327 >>> m2 = message_from_string(str(m)) 1328 >>> m2['to'] 1329 'ric <foo (a] example.com>' 1330 1331 When you parse a message, you can use the ``addresses`` and ``groups`` 1332 attributes of the header objects to access the groups and individual 1333 addresses:: 1334 1335 >>> m2['cc'].addresses 1336 (Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com'), Address(display_name='Bonzo', username='bonz', domain='laugh.com')) 1337 >>> m2['cc'].groups 1338 (Group(display_name='pals', addresses=(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com')), Group(display_name=None, addresses=(Address(display_name='Bonzo', username='bonz', domain='laugh.com'),)) 1339 1340 In summary, if you use one of the new policies, header manipulation works the 1341 way it ought to: your application works with unicode strings, and the email 1342 package transparently encodes and decodes the unicode to and from the RFC 1343 standard Content Transfer Encodings. 1344 1345 Other API Changes 1346 ~~~~~~~~~~~~~~~~~ 1347 1348 New :class:`~email.parser.BytesHeaderParser`, added to the :mod:`~email.parser` 1349 module to complement :class:`~email.parser.HeaderParser` and complete the Bytes 1350 API. 1351 1352 New utility functions: 1353 1354 * :func:`~email.utils.format_datetime`: given a :class:`~datetime.datetime`, 1355 produce a string formatted for use in an email header. 1356 1357 * :func:`~email.utils.parsedate_to_datetime`: given a date string from 1358 an email header, convert it into an aware :class:`~datetime.datetime`, 1359 or a naive :class:`~datetime.datetime` if the offset is ``-0000``. 1360 1361 * :func:`~email.utils.localtime`: With no argument, returns the 1362 current local time as an aware :class:`~datetime.datetime` using the local 1363 :class:`~datetime.timezone`. Given an aware :class:`~datetime.datetime`, 1364 converts it into an aware :class:`~datetime.datetime` using the 1365 local :class:`~datetime.timezone`. 1366 1367 1368 ftplib 1369 ------ 1370 1371 * :class:`ftplib.FTP` now accepts a ``source_address`` keyword argument to 1372 specify the ``(host, port)`` to use as the source address in the bind call 1373 when creating the outgoing socket. (Contributed by Giampaolo Rodol 1374 in :issue:`8594`.) 1375 1376 * The :class:`~ftplib.FTP_TLS` class now provides a new 1377 :func:`~ftplib.FTP_TLS.ccc` function to revert control channel back to 1378 plaintext. This can be useful to take advantage of firewalls that know how 1379 to handle NAT with non-secure FTP without opening fixed ports. (Contributed 1380 by Giampaolo Rodol in :issue:`12139`.) 1381 1382 * Added :meth:`ftplib.FTP.mlsd` method which provides a parsable directory 1383 listing format and deprecates :meth:`ftplib.FTP.nlst` and 1384 :meth:`ftplib.FTP.dir`. (Contributed by Giampaolo Rodol in :issue:`11072`.) 1385 1386 1387 functools 1388 --------- 1389 1390 The :func:`functools.lru_cache` decorator now accepts a ``typed`` keyword 1391 argument (that defaults to ``False`` to ensure that it caches values of 1392 different types that compare equal in separate cache slots. (Contributed 1393 by Raymond Hettinger in :issue:`13227`.) 1394 1395 1396 gc 1397 -- 1398 1399 It is now possible to register callbacks invoked by the garbage collector 1400 before and after collection using the new :data:`~gc.callbacks` list. 1401 1402 1403 hmac 1404 ---- 1405 1406 A new :func:`~hmac.compare_digest` function has been added to prevent side 1407 channel attacks on digests through timing analysis. (Contributed by Nick 1408 Coghlan and Christian Heimes in :issue:`15061`.) 1409 1410 1411 http 1412 ---- 1413 1414 :class:`http.server.BaseHTTPRequestHandler` now buffers the headers and writes 1415 them all at once when :meth:`~http.server.BaseHTTPRequestHandler.end_headers` is 1416 called. A new method :meth:`~http.server.BaseHTTPRequestHandler.flush_headers` 1417 can be used to directly manage when the accumulated headers are sent. 1418 (Contributed by Andrew Schaaf in :issue:`3709`.) 1419 1420 :class:`http.server` now produces valid ``HTML 4.01 strict`` output. 1421 (Contributed by Ezio Melotti in :issue:`13295`.) 1422 1423 :class:`http.client.HTTPResponse` now has a 1424 :meth:`~http.client.HTTPResponse.readinto` method, which means it can be used 1425 as an :class:`io.RawIOBase` class. (Contributed by John Kuhn in 1426 :issue:`13464`.) 1427 1428 1429 html 1430 ---- 1431 1432 :class:`html.parser.HTMLParser` is now able to parse broken markup without 1433 raising errors, therefore the *strict* argument of the constructor and the 1434 :exc:`~html.parser.HTMLParseError` exception are now deprecated. 1435 The ability to parse broken markup is the result of a number of bug fixes that 1436 are also available on the latest bug fix releases of Python 2.7/3.2. 1437 (Contributed by Ezio Melotti in :issue:`15114`, and :issue:`14538`, 1438 :issue:`13993`, :issue:`13960`, :issue:`13358`, :issue:`1745761`, 1439 :issue:`755670`, :issue:`13357`, :issue:`12629`, :issue:`1200313`, 1440 :issue:`670664`, :issue:`13273`, :issue:`12888`, :issue:`7311`.) 1441 1442 A new :data:`~html.entities.html5` dictionary that maps HTML5 named character 1443 references to the equivalent Unicode character(s) (e.g. ``html5['gt;'] == 1444 '>'``) has been added to the :mod:`html.entities` module. The dictionary is 1445 now also used by :class:`~html.parser.HTMLParser`. (Contributed by Ezio 1446 Melotti in :issue:`11113` and :issue:`15156`.) 1447 1448 1449 imaplib 1450 ------- 1451 1452 The :class:`~imaplib.IMAP4_SSL` constructor now accepts an SSLContext 1453 parameter to control parameters of the secure channel. 1454 1455 (Contributed by Sijin Joseph in :issue:`8808`.) 1456 1457 1458 inspect 1459 ------- 1460 1461 A new :func:`~inspect.getclosurevars` function has been added. This function 1462 reports the current binding of all names referenced from the function body and 1463 where those names were resolved, making it easier to verify correct internal 1464 state when testing code that relies on stateful closures. 1465 1466 (Contributed by Meador Inge and Nick Coghlan in :issue:`13062`.) 1467 1468 A new :func:`~inspect.getgeneratorlocals` function has been added. This 1469 function reports the current binding of local variables in the generator's 1470 stack frame, making it easier to verify correct internal state when testing 1471 generators. 1472 1473 (Contributed by Meador Inge in :issue:`15153`.) 1474 1475 io 1476 -- 1477 1478 The :func:`~io.open` function has a new ``'x'`` mode that can be used to 1479 exclusively create a new file, and raise a :exc:`FileExistsError` if the file 1480 already exists. It is based on the C11 'x' mode to fopen(). 1481 1482 (Contributed by David Townshend in :issue:`12760`.) 1483 1484 The constructor of the :class:`~io.TextIOWrapper` class has a new 1485 *write_through* optional argument. If *write_through* is ``True``, calls to 1486 :meth:`~io.TextIOWrapper.write` are guaranteed not to be buffered: any data 1487 written on the :class:`~io.TextIOWrapper` object is immediately handled to its 1488 underlying binary buffer. 1489 1490 1491 itertools 1492 --------- 1493 1494 :func:`~itertools.accumulate` now takes an optional ``func`` argument for 1495 providing a user-supplied binary function. 1496 1497 1498 logging 1499 ------- 1500 1501 The :func:`~logging.basicConfig` function now supports an optional ``handlers`` 1502 argument taking an iterable of handlers to be added to the root logger. 1503 1504 A class level attribute :attr:`~logging.handlers.SysLogHandler.append_nul` has 1505 been added to :class:`~logging.handlers.SysLogHandler` to allow control of the 1506 appending of the ``NUL`` (``\000``) byte to syslog records, since for some 1507 daemons it is required while for others it is passed through to the log. 1508 1509 1510 1511 math 1512 ---- 1513 1514 The :mod:`math` module has a new function, :func:`~math.log2`, which returns 1515 the base-2 logarithm of *x*. 1516 1517 (Written by Mark Dickinson in :issue:`11888`.) 1518 1519 1520 mmap 1521 ---- 1522 1523 The :meth:`~mmap.mmap.read` method is now more compatible with other file-like 1524 objects: if the argument is omitted or specified as ``None``, it returns the 1525 bytes from the current file position to the end of the mapping. (Contributed 1526 by Petri Lehtinen in :issue:`12021`.) 1527 1528 1529 multiprocessing 1530 --------------- 1531 1532 The new :func:`multiprocessing.connection.wait` function allows polling 1533 multiple objects (such as connections, sockets and pipes) with a timeout. 1534 (Contributed by Richard Oudkerk in :issue:`12328`.) 1535 1536 :class:`multiprocessing.Connection` objects can now be transferred over 1537 multiprocessing connections. 1538 (Contributed by Richard Oudkerk in :issue:`4892`.) 1539 1540 :class:`multiprocessing.Process` now accepts a ``daemon`` keyword argument 1541 to override the default behavior of inheriting the ``daemon`` flag from 1542 the parent process (:issue:`6064`). 1543 1544 New attribute :data:`multiprocessing.Process.sentinel` allows a 1545 program to wait on multiple :class:`~multiprocessing.Process` objects at one 1546 time using the appropriate OS primitives (for example, :mod:`select` on 1547 posix systems). 1548 1549 New methods :meth:`multiprocessing.pool.Pool.starmap` and 1550 :meth:`~multiprocessing.pool.Pool.starmap_async` provide 1551 :func:`itertools.starmap` equivalents to the existing 1552 :meth:`multiprocessing.pool.Pool.map` and 1553 :meth:`~multiprocessing.pool.Pool.map_async` functions. (Contributed by Hynek 1554 Schlawack in :issue:`12708`.) 1555 1556 1557 nntplib 1558 ------- 1559 1560 The :class:`nntplib.NNTP` class now supports the context management protocol to 1561 unconditionally consume :exc:`socket.error` exceptions and to close the NNTP 1562 connection when done:: 1563 1564 >>> from nntplib import NNTP 1565 >>> with NNTP('news.gmane.org') as n: 1566 ... n.group('gmane.comp.python.committers') 1567 ... 1568 ('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers') 1569 >>> 1570 1571 (Contributed by Giampaolo Rodol in :issue:`9795`.) 1572 1573 1574 os 1575 -- 1576 1577 * The :mod:`os` module has a new :func:`~os.pipe2` function that makes it 1578 possible to create a pipe with :data:`~os.O_CLOEXEC` or 1579 :data:`~os.O_NONBLOCK` flags set atomically. This is especially useful to 1580 avoid race conditions in multi-threaded programs. 1581 1582 * The :mod:`os` module has a new :func:`~os.sendfile` function which provides 1583 an efficient "zero-copy" way for copying data from one file (or socket) 1584 descriptor to another. The phrase "zero-copy" refers to the fact that all of 1585 the copying of data between the two descriptors is done entirely by the 1586 kernel, with no copying of data into userspace buffers. :func:`~os.sendfile` 1587 can be used to efficiently copy data from a file on disk to a network socket, 1588 e.g. for downloading a file. 1589 1590 (Patch submitted by Ross Lagerwall and Giampaolo Rodol in :issue:`10882`.) 1591 1592 * To avoid race conditions like symlink attacks and issues with temporary 1593 files and directories, it is more reliable (and also faster) to manipulate 1594 file descriptors instead of file names. Python 3.3 enhances existing functions 1595 and introduces new functions to work on file descriptors (:issue:`4761`, 1596 :issue:`10755` and :issue:`14626`). 1597 1598 - The :mod:`os` module has a new :func:`~os.fwalk` function similar to 1599 :func:`~os.walk` except that it also yields file descriptors referring to the 1600 directories visited. This is especially useful to avoid symlink races. 1601 1602 - The following functions get new optional *dir_fd* (:ref:`paths relative to 1603 directory descriptors <dir_fd>`) and/or *follow_symlinks* (:ref:`not 1604 following symlinks <follow_symlinks>`): 1605 :func:`~os.access`, :func:`~os.chflags`, :func:`~os.chmod`, :func:`~os.chown`, 1606 :func:`~os.link`, :func:`~os.lstat`, :func:`~os.mkdir`, :func:`~os.mkfifo`, 1607 :func:`~os.mknod`, :func:`~os.open`, :func:`~os.readlink`, :func:`~os.remove`, 1608 :func:`~os.rename`, :func:`~os.replace`, :func:`~os.rmdir`, :func:`~os.stat`, 1609 :func:`~os.symlink`, :func:`~os.unlink`, :func:`~os.utime`. Platform 1610 support for using these parameters can be checked via the sets 1611 :data:`os.supports_dir_fd` and :data:`os.supports_follows_symlinks`. 1612 1613 - The following functions now support a file descriptor for their path argument: 1614 :func:`~os.chdir`, :func:`~os.chmod`, :func:`~os.chown`, 1615 :func:`~os.execve`, :func:`~os.listdir`, :func:`~os.pathconf`, :func:`~os.path.exists`, 1616 :func:`~os.stat`, :func:`~os.statvfs`, :func:`~os.utime`. Platform support 1617 for this can be checked via the :data:`os.supports_fd` set. 1618 1619 * :func:`~os.access` accepts an ``effective_ids`` keyword argument to turn on 1620 using the effective uid/gid rather than the real uid/gid in the access check. 1621 Platform support for this can be checked via the 1622 :data:`~os.supports_effective_ids` set. 1623 1624 * The :mod:`os` module has two new functions: :func:`~os.getpriority` and 1625 :func:`~os.setpriority`. They can be used to get or set process 1626 niceness/priority in a fashion similar to :func:`os.nice` but extended to all 1627 processes instead of just the current one. 1628 1629 (Patch submitted by Giampaolo Rodol in :issue:`10784`.) 1630 1631 * The new :func:`os.replace` function allows cross-platform renaming of a 1632 file with overwriting the destination. With :func:`os.rename`, an existing 1633 destination file is overwritten under POSIX, but raises an error under 1634 Windows. 1635 (Contributed by Antoine Pitrou in :issue:`8828`.) 1636 1637 * The stat family of functions (:func:`~os.stat`, :func:`~os.fstat`, 1638 and :func:`~os.lstat`) now support reading a file's timestamps 1639 with nanosecond precision. Symmetrically, :func:`~os.utime` 1640 can now write file timestamps with nanosecond precision. (Contributed by 1641 Larry Hastings in :issue:`14127`.) 1642 1643 * The new :func:`os.get_terminal_size` function queries the size of the 1644 terminal attached to a file descriptor. See also 1645 :func:`shutil.get_terminal_size`. 1646 (Contributed by Zbigniew Jdrzejewski-Szmek in :issue:`13609`.) 1647 1648 .. XXX sort out this mess after beta1 1649 1650 * New functions to support Linux extended attributes (:issue:`12720`): 1651 :func:`~os.getxattr`, :func:`~os.listxattr`, :func:`~os.removexattr`, 1652 :func:`~os.setxattr`. 1653 1654 * New interface to the scheduler. These functions 1655 control how a process is allocated CPU time by the operating system. New 1656 functions: 1657 :func:`~os.sched_get_priority_max`, :func:`~os.sched_get_priority_min`, 1658 :func:`~os.sched_getaffinity`, :func:`~os.sched_getparam`, 1659 :func:`~os.sched_getscheduler`, :func:`~os.sched_rr_get_interval`, 1660 :func:`~os.sched_setaffinity`, :func:`~os.sched_setparam`, 1661 :func:`~os.sched_setscheduler`, :func:`~os.sched_yield`, 1662 1663 * New functions to control the file system: 1664 1665 * :func:`~os.posix_fadvise`: Announces an intention to access data in a 1666 specific pattern thus allowing the kernel to make optimizations. 1667 * :func:`~os.posix_fallocate`: Ensures that enough disk space is allocated 1668 for a file. 1669 * :func:`~os.sync`: Force write of everything to disk. 1670 1671 * Additional new posix functions: 1672 1673 * :func:`~os.lockf`: Apply, test or remove a POSIX lock on an open file descriptor. 1674 * :func:`~os.pread`: Read from a file descriptor at an offset, the file 1675 offset remains unchanged. 1676 * :func:`~os.pwrite`: Write to a file descriptor from an offset, leaving 1677 the file offset unchanged. 1678 * :func:`~os.readv`: Read from a file descriptor into a number of writable buffers. 1679 * :func:`~os.truncate`: Truncate the file corresponding to *path*, so that 1680 it is at most *length* bytes in size. 1681 * :func:`~os.waitid`: Wait for the completion of one or more child processes. 1682 * :func:`~os.writev`: Write the contents of *buffers* to a file descriptor, 1683 where *buffers* is an arbitrary sequence of buffers. 1684 * :func:`~os.getgrouplist` (:issue:`9344`): Return list of group ids that 1685 specified user belongs to. 1686 1687 * :func:`~os.times` and :func:`~os.uname`: Return type changed from a tuple to 1688 a tuple-like object with named attributes. 1689 1690 * Some platforms now support additional constants for the :func:`~os.lseek` 1691 function, such as ``os.SEEK_HOLE`` and ``os.SEEK_DATA``. 1692 1693 * New constants :data:`~os.RTLD_LAZY`, :data:`~os.RTLD_NOW`, 1694 :data:`~os.RTLD_GLOBAL`, :data:`~os.RTLD_LOCAL`, :data:`~os.RTLD_NODELETE`, 1695 :data:`~os.RTLD_NOLOAD`, and :data:`~os.RTLD_DEEPBIND` are available on 1696 platforms that support them. These are for use with the 1697 :func:`sys.setdlopenflags` function, and supersede the similar constants 1698 defined in :mod:`ctypes` and :mod:`DLFCN`. (Contributed by Victor Stinner 1699 in :issue:`13226`.) 1700 1701 * :func:`os.symlink` now accepts (and ignores) the ``target_is_directory`` 1702 keyword argument on non-Windows platforms, to ease cross-platform support. 1703 1704 1705 pdb 1706 --- 1707 1708 Tab-completion is now available not only for command names, but also their 1709 arguments. For example, for the ``break`` command, function and file names 1710 are completed. 1711 1712 (Contributed by Georg Brandl in :issue:`14210`) 1713 1714 1715 pickle 1716 ------ 1717 1718 :class:`pickle.Pickler` objects now have an optional 1719 :attr:`~pickle.Pickler.dispatch_table` attribute allowing per-pickler 1720 reduction functions to be set. 1721 1722 (Contributed by Richard Oudkerk in :issue:`14166`.) 1723 1724 1725 pydoc 1726 ----- 1727 1728 The Tk GUI and the :func:`~pydoc.serve` function have been removed from the 1729 :mod:`pydoc` module: ``pydoc -g`` and :func:`~pydoc.serve` have been deprecated 1730 in Python 3.2. 1731 1732 1733 re 1734 -- 1735 1736 :class:`str` regular expressions now support ``\u`` and ``\U`` escapes. 1737 1738 (Contributed by Serhiy Storchaka in :issue:`3665`.) 1739 1740 1741 sched 1742 ----- 1743 1744 * :meth:`~sched.scheduler.run` now accepts a *blocking* parameter which when 1745 set to false makes the method execute the scheduled events due to expire 1746 soonest (if any) and then return immediately. 1747 This is useful in case you want to use the :class:`~sched.scheduler` in 1748 non-blocking applications. (Contributed by Giampaolo Rodol in :issue:`13449`.) 1749 1750 * :class:`~sched.scheduler` class can now be safely used in multi-threaded 1751 environments. (Contributed by Josiah Carlson and Giampaolo Rodol in 1752 :issue:`8684`.) 1753 1754 * *timefunc* and *delayfunct* parameters of :class:`~sched.scheduler` class 1755 constructor are now optional and defaults to :func:`time.time` and 1756 :func:`time.sleep` respectively. (Contributed by Chris Clark in 1757 :issue:`13245`.) 1758 1759 * :meth:`~sched.scheduler.enter` and :meth:`~sched.scheduler.enterabs` 1760 *argument* parameter is now optional. (Contributed by Chris Clark in 1761 :issue:`13245`.) 1762 1763 * :meth:`~sched.scheduler.enter` and :meth:`~sched.scheduler.enterabs` 1764 now accept a *kwargs* parameter. (Contributed by Chris Clark in 1765 :issue:`13245`.) 1766 1767 1768 select 1769 ------ 1770 1771 Solaris and derivative platforms have a new class :class:`select.devpoll` 1772 for high performance asynchronous sockets via :file:`/dev/poll`. 1773 (Contributed by Jess Cea Avin in :issue:`6397`.) 1774 1775 1776 shlex 1777 ----- 1778 1779 The previously undocumented helper function ``quote`` from the 1780 :mod:`pipes` modules has been moved to the :mod:`shlex` module and 1781 documented. :func:`~shlex.quote` properly escapes all characters in a string 1782 that might be otherwise given special meaning by the shell. 1783 1784 1785 shutil 1786 ------ 1787 1788 * New functions: 1789 1790 * :func:`~shutil.disk_usage`: provides total, used and free disk space 1791 statistics. (Contributed by Giampaolo Rodol in :issue:`12442`.) 1792 * :func:`~shutil.chown`: allows one to change user and/or group of the given 1793 path also specifying the user/group names and not only their numeric 1794 ids. (Contributed by Sandro Tosi in :issue:`12191`.) 1795 * :func:`shutil.get_terminal_size`: returns the size of the terminal window 1796 to which the interpreter is attached. (Contributed by Zbigniew 1797 Jdrzejewski-Szmek in :issue:`13609`.) 1798 1799 * :func:`~shutil.copy2` and :func:`~shutil.copystat` now preserve file 1800 timestamps with nanosecond precision on platforms that support it. 1801 They also preserve file "extended attributes" on Linux. (Contributed 1802 by Larry Hastings in :issue:`14127` and :issue:`15238`.) 1803 1804 * Several functions now take an optional ``symlinks`` argument: when that 1805 parameter is true, symlinks aren't dereferenced and the operation instead 1806 acts on the symlink itself (or creates one, if relevant). 1807 (Contributed by Hynek Schlawack in :issue:`12715`.) 1808 1809 * When copying files to a different file system, :func:`~shutil.move` now 1810 handles symlinks the way the posix ``mv`` command does, recreating the 1811 symlink rather than copying the target file contents. (Contributed by 1812 Jonathan Niehof in :issue:`9993`.) :func:`~shutil.move` now also returns 1813 the ``dst`` argument as its result. 1814 1815 * :func:`~shutil.rmtree` is now resistant to symlink attacks on platforms 1816 which support the new ``dir_fd`` parameter in :func:`os.open` and 1817 :func:`os.unlink`. (Contributed by Martin von Lwis and Hynek Schlawack 1818 in :issue:`4489`.) 1819 1820 1821 signal 1822 ------ 1823 1824 * The :mod:`signal` module has new functions: 1825 1826 * :func:`~signal.pthread_sigmask`: fetch and/or change the signal mask of the 1827 calling thread (Contributed by Jean-Paul Calderone in :issue:`8407`); 1828 * :func:`~signal.pthread_kill`: send a signal to a thread; 1829 * :func:`~signal.sigpending`: examine pending functions; 1830 * :func:`~signal.sigwait`: wait a signal; 1831 * :func:`~signal.sigwaitinfo`: wait for a signal, returning detailed 1832 information about it; 1833 * :func:`~signal.sigtimedwait`: like :func:`~signal.sigwaitinfo` but with a 1834 timeout. 1835 1836 * The signal handler writes the signal number as a single byte instead of 1837 a nul byte into the wakeup file descriptor. So it is possible to wait more 1838 than one signal and know which signals were raised. 1839 1840 * :func:`signal.signal` and :func:`signal.siginterrupt` raise an OSError, 1841 instead of a RuntimeError: OSError has an errno attribute. 1842 1843 1844 smtpd 1845 ----- 1846 1847 The :mod:`smtpd` module now supports :rfc:`5321` (extended SMTP) and :rfc:`1870` 1848 (size extension). Per the standard, these extensions are enabled if and only 1849 if the client initiates the session with an ``EHLO`` command. 1850 1851 (Initial ``ELHO`` support by Alberto Trevino. Size extension by Juhana 1852 Jauhiainen. Substantial additional work on the patch contributed by Michele 1853 Orr and Dan Boswell. :issue:`8739`) 1854 1855 1856 smtplib 1857 ------- 1858 1859 The :class:`~smtplib.SMTP`, :class:`~smtplib.SMTP_SSL`, and 1860 :class:`~smtplib.LMTP` classes now accept a ``source_address`` keyword argument 1861 to specify the ``(host, port)`` to use as the source address in the bind call 1862 when creating the outgoing socket. (Contributed by Paulo Scardine in 1863 :issue:`11281`.) 1864 1865 :class:`~smtplib.SMTP` now supports the context management protocol, allowing an 1866 ``SMTP`` instance to be used in a ``with`` statement. (Contributed 1867 by Giampaolo Rodol in :issue:`11289`.) 1868 1869 The :class:`~smtplib.SMTP_SSL` constructor and the :meth:`~smtplib.SMTP.starttls` 1870 method now accept an SSLContext parameter to control parameters of the secure 1871 channel. (Contributed by Kasun Herath in :issue:`8809`.) 1872 1873 1874 socket 1875 ------ 1876 1877 * The :class:`~socket.socket` class now exposes additional methods to process 1878 ancillary data when supported by the underlying platform: 1879 1880 * :func:`~socket.socket.sendmsg` 1881 * :func:`~socket.socket.recvmsg` 1882 * :func:`~socket.socket.recvmsg_into` 1883 1884 (Contributed by David Watson in :issue:`6560`, based on an earlier patch by 1885 Heiko Wundram) 1886 1887 * The :class:`~socket.socket` class now supports the PF_CAN protocol family 1888 (https://en.wikipedia.org/wiki/Socketcan), on Linux 1889 (https://lwn.net/Articles/253425). 1890 1891 (Contributed by Matthias Fuchs, updated by Tiago Gonalves in :issue:`10141`.) 1892 1893 * The :class:`~socket.socket` class now supports the PF_RDS protocol family 1894 (https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets and 1895 https://oss.oracle.com/projects/rds/). 1896 1897 * The :class:`~socket.socket` class now supports the ``PF_SYSTEM`` protocol 1898 family on OS X. (Contributed by Michael Goderbauer in :issue:`13777`.) 1899 1900 * New function :func:`~socket.sethostname` allows the hostname to be set 1901 on unix systems if the calling process has sufficient privileges. 1902 (Contributed by Ross Lagerwall in :issue:`10866`.) 1903 1904 1905 socketserver 1906 ------------ 1907 1908 :class:`~socketserver.BaseServer` now has an overridable method 1909 :meth:`~socketserver.BaseServer.service_actions` that is called by the 1910 :meth:`~socketserver.BaseServer.serve_forever` method in the service loop. 1911 :class:`~socketserver.ForkingMixIn` now uses this to clean up zombie 1912 child processes. (Contributed by Justin Warkentin in :issue:`11109`.) 1913 1914 1915 sqlite3 1916 ------- 1917 1918 New :class:`sqlite3.Connection` method 1919 :meth:`~sqlite3.Connection.set_trace_callback` can be used to capture a trace of 1920 all sql commands processed by sqlite. (Contributed by Torsten Landschoff 1921 in :issue:`11688`.) 1922 1923 1924 ssl 1925 --- 1926 1927 * The :mod:`ssl` module has two new random generation functions: 1928 1929 * :func:`~ssl.RAND_bytes`: generate cryptographically strong 1930 pseudo-random bytes. 1931 * :func:`~ssl.RAND_pseudo_bytes`: generate pseudo-random bytes. 1932 1933 (Contributed by Victor Stinner in :issue:`12049`.) 1934 1935 * The :mod:`ssl` module now exposes a finer-grained exception hierarchy 1936 in order to make it easier to inspect the various kinds of errors. 1937 (Contributed by Antoine Pitrou in :issue:`11183`.) 1938 1939 * :meth:`~ssl.SSLContext.load_cert_chain` now accepts a *password* argument 1940 to be used if the private key is encrypted. 1941 (Contributed by Adam Simpkins in :issue:`12803`.) 1942 1943 * Diffie-Hellman key exchange, both regular and Elliptic Curve-based, is 1944 now supported through the :meth:`~ssl.SSLContext.load_dh_params` and 1945 :meth:`~ssl.SSLContext.set_ecdh_curve` methods. 1946 (Contributed by Antoine Pitrou in :issue:`13626` and :issue:`13627`.) 1947 1948 * SSL sockets have a new :meth:`~ssl.SSLSocket.get_channel_binding` method 1949 allowing the implementation of certain authentication mechanisms such as 1950 SCRAM-SHA-1-PLUS. (Contributed by Jacek Konieczny in :issue:`12551`.) 1951 1952 * You can query the SSL compression algorithm used by an SSL socket, thanks 1953 to its new :meth:`~ssl.SSLSocket.compression` method. The new attribute 1954 :attr:`~ssl.OP_NO_COMPRESSION` can be used to disable compression. 1955 (Contributed by Antoine Pitrou in :issue:`13634`.) 1956 1957 * Support has been added for the Next Protocol Negotiation extension using 1958 the :meth:`ssl.SSLContext.set_npn_protocols` method. 1959 (Contributed by Colin Marc in :issue:`14204`.) 1960 1961 * SSL errors can now be introspected more easily thanks to 1962 :attr:`~ssl.SSLError.library` and :attr:`~ssl.SSLError.reason` attributes. 1963 (Contributed by Antoine Pitrou in :issue:`14837`.) 1964 1965 * The :func:`~ssl.get_server_certificate` function now supports IPv6. 1966 (Contributed by Charles-Franois Natali in :issue:`11811`.) 1967 1968 * New attribute :attr:`~ssl.OP_CIPHER_SERVER_PREFERENCE` allows setting 1969 SSLv3 server sockets to use the server's cipher ordering preference rather 1970 than the client's (:issue:`13635`). 1971 1972 1973 stat 1974 ---- 1975 1976 The undocumented tarfile.filemode function has been moved to 1977 :func:`stat.filemode`. It can be used to convert a file's mode to a string of 1978 the form '-rwxrwxrwx'. 1979 1980 (Contributed by Giampaolo Rodol in :issue:`14807`.) 1981 1982 1983 struct 1984 ------ 1985 1986 The :mod:`struct` module now supports ``ssize_t`` and ``size_t`` via the 1987 new codes ``n`` and ``N``, respectively. (Contributed by Antoine Pitrou 1988 in :issue:`3163`.) 1989 1990 1991 subprocess 1992 ---------- 1993 1994 Command strings can now be bytes objects on posix platforms. (Contributed by 1995 Victor Stinner in :issue:`8513`.) 1996 1997 A new constant :data:`~subprocess.DEVNULL` allows suppressing output in a 1998 platform-independent fashion. (Contributed by Ross Lagerwall in 1999 :issue:`5870`.) 2000 2001 2002 sys 2003 --- 2004 2005 The :mod:`sys` module has a new :data:`~sys.thread_info` :term:`struct 2006 sequence` holding information about the thread implementation 2007 (:issue:`11223`). 2008 2009 2010 tarfile 2011 ------- 2012 2013 :mod:`tarfile` now supports ``lzma`` encoding via the :mod:`lzma` module. 2014 (Contributed by Lars Gustbel in :issue:`5689`.) 2015 2016 2017 tempfile 2018 -------- 2019 2020 :class:`tempfile.SpooledTemporaryFile`\'s 2021 :meth:`~tempfile.SpooledTemporaryFile.truncate` method now accepts 2022 a ``size`` parameter. (Contributed by Ryan Kelly in :issue:`9957`.) 2023 2024 2025 textwrap 2026 -------- 2027 2028 The :mod:`textwrap` module has a new :func:`~textwrap.indent` that makes 2029 it straightforward to add a common prefix to selected lines in a block 2030 of text (:issue:`13857`). 2031 2032 2033 threading 2034 --------- 2035 2036 :class:`threading.Condition`, :class:`threading.Semaphore`, 2037 :class:`threading.BoundedSemaphore`, :class:`threading.Event`, and 2038 :class:`threading.Timer`, all of which used to be factory functions returning a 2039 class instance, are now classes and may be subclassed. (Contributed by ric 2040 Araujo in :issue:`10968`.) 2041 2042 The :class:`threading.Thread` constructor now accepts a ``daemon`` keyword 2043 argument to override the default behavior of inheriting the ``daemon`` flag 2044 value from the parent thread (:issue:`6064`). 2045 2046 The formerly private function ``_thread.get_ident`` is now available as the 2047 public function :func:`threading.get_ident`. This eliminates several cases of 2048 direct access to the ``_thread`` module in the stdlib. Third party code that 2049 used ``_thread.get_ident`` should likewise be changed to use the new public 2050 interface. 2051 2052 2053 time 2054 ---- 2055 2056 The :pep:`418` added new functions to the :mod:`time` module: 2057 2058 * :func:`~time.get_clock_info`: Get information on a clock. 2059 * :func:`~time.monotonic`: Monotonic clock (cannot go backward), not affected 2060 by system clock updates. 2061 * :func:`~time.perf_counter`: Performance counter with the highest available 2062 resolution to measure a short duration. 2063 * :func:`~time.process_time`: Sum of the system and user CPU time of the 2064 current process. 2065 2066 Other new functions: 2067 2068 * :func:`~time.clock_getres`, :func:`~time.clock_gettime` and 2069 :func:`~time.clock_settime` functions with ``CLOCK_xxx`` constants. 2070 (Contributed by Victor Stinner in :issue:`10278`.) 2071 2072 To improve cross platform consistency, :func:`~time.sleep` now raises a 2073 :exc:`ValueError` when passed a negative sleep value. Previously this was an 2074 error on posix, but produced an infinite sleep on Windows. 2075 2076 2077 types 2078 ----- 2079 2080 Add a new :class:`types.MappingProxyType` class: Read-only proxy of a mapping. 2081 (:issue:`14386`) 2082 2083 2084 The new functions :func:`types.new_class` and :func:`types.prepare_class` provide support 2085 for PEP 3115 compliant dynamic type creation. (:issue:`14588`) 2086 2087 2088 unittest 2089 -------- 2090 2091 :meth:`.assertRaises`, :meth:`.assertRaisesRegex`, :meth:`.assertWarns`, and 2092 :meth:`.assertWarnsRegex` now accept a keyword argument *msg* when used as 2093 context managers. (Contributed by Ezio Melotti and Winston Ewert in 2094 :issue:`10775`.) 2095 2096 :meth:`unittest.TestCase.run` now returns the :class:`~unittest.TestResult` 2097 object. 2098 2099 2100 urllib 2101 ------ 2102 2103 The :class:`~urllib.request.Request` class, now accepts a *method* argument 2104 used by :meth:`~urllib.request.Request.get_method` to determine what HTTP method 2105 should be used. For example, this will send a ``'HEAD'`` request:: 2106 2107 >>> urlopen(Request('https://www.python.org', method='HEAD')) 2108 2109 (:issue:`1673007`) 2110 2111 2112 webbrowser 2113 ---------- 2114 2115 The :mod:`webbrowser` module supports more "browsers": Google Chrome (named 2116 :program:`chrome`, :program:`chromium`, :program:`chrome-browser` or 2117 :program:`chromium-browser` depending on the version and operating system), 2118 and the generic launchers :program:`xdg-open`, from the FreeDesktop.org 2119 project, and :program:`gvfs-open`, which is the default URI handler for GNOME 2120 3. (The former contributed by Arnaud Calmettes in :issue:`13620`, the latter 2121 by Matthias Klose in :issue:`14493`.) 2122 2123 2124 xml.etree.ElementTree 2125 --------------------- 2126 2127 The :mod:`xml.etree.ElementTree` module now imports its C accelerator by 2128 default; there is no longer a need to explicitly import 2129 :mod:`xml.etree.cElementTree` (this module stays for backwards compatibility, 2130 but is now deprecated). In addition, the ``iter`` family of methods of 2131 :class:`~xml.etree.ElementTree.Element` has been optimized (rewritten in C). 2132 The module's documentation has also been greatly improved with added examples 2133 and a more detailed reference. 2134 2135 2136 zlib 2137 ---- 2138 2139 New attribute :attr:`zlib.Decompress.eof` makes it possible to distinguish 2140 between a properly-formed compressed stream and an incomplete or truncated one. 2141 (Contributed by Nadeem Vawda in :issue:`12646`.) 2142 2143 New attribute :attr:`zlib.ZLIB_RUNTIME_VERSION` reports the version string of 2144 the underlying ``zlib`` library that is loaded at runtime. (Contributed by 2145 Torsten Landschoff in :issue:`12306`.) 2146 2147 2148 Optimizations 2149 ============= 2150 2151 Major performance enhancements have been added: 2152 2153 * Thanks to :pep:`393`, some operations on Unicode strings have been optimized: 2154 2155 * the memory footprint is divided by 2 to 4 depending on the text 2156 * encode an ASCII string to UTF-8 doesn't need to encode characters anymore, 2157 the UTF-8 representation is shared with the ASCII representation 2158 * the UTF-8 encoder has been optimized 2159 * repeating a single ASCII letter and getting a substring of an ASCII string 2160 is 4 times faster 2161 2162 * UTF-8 is now 2x to 4x faster. UTF-16 encoding is now up to 10x faster. 2163 2164 (Contributed by Serhiy Storchaka, :issue:`14624`, :issue:`14738` and 2165 :issue:`15026`.) 2166 2167 2168 Build and C API Changes 2169 ======================= 2170 2171 Changes to Python's build process and to the C API include: 2172 2173 * New :pep:`3118` related function: 2174 2175 * :c:func:`PyMemoryView_FromMemory` 2176 2177 * :pep:`393` added new Unicode types, macros and functions: 2178 2179 * High-level API: 2180 2181 * :c:func:`PyUnicode_CopyCharacters` 2182 * :c:func:`PyUnicode_FindChar` 2183 * :c:func:`PyUnicode_GetLength`, :c:macro:`PyUnicode_GET_LENGTH` 2184 * :c:func:`PyUnicode_New` 2185 * :c:func:`PyUnicode_Substring` 2186 * :c:func:`PyUnicode_ReadChar`, :c:func:`PyUnicode_WriteChar` 2187 2188 * Low-level API: 2189 2190 * :c:type:`Py_UCS1`, :c:type:`Py_UCS2`, :c:type:`Py_UCS4` types 2191 * :c:type:`PyASCIIObject` and :c:type:`PyCompactUnicodeObject` structures 2192 * :c:macro:`PyUnicode_READY` 2193 * :c:func:`PyUnicode_FromKindAndData` 2194 * :c:func:`PyUnicode_AsUCS4`, :c:func:`PyUnicode_AsUCS4Copy` 2195 * :c:macro:`PyUnicode_DATA`, :c:macro:`PyUnicode_1BYTE_DATA`, 2196 :c:macro:`PyUnicode_2BYTE_DATA`, :c:macro:`PyUnicode_4BYTE_DATA` 2197 * :c:macro:`PyUnicode_KIND` with :c:type:`PyUnicode_Kind` enum: 2198 :c:data:`PyUnicode_WCHAR_KIND`, :c:data:`PyUnicode_1BYTE_KIND`, 2199 :c:data:`PyUnicode_2BYTE_KIND`, :c:data:`PyUnicode_4BYTE_KIND` 2200 * :c:macro:`PyUnicode_READ`, :c:macro:`PyUnicode_READ_CHAR`, :c:macro:`PyUnicode_WRITE` 2201 * :c:macro:`PyUnicode_MAX_CHAR_VALUE` 2202 2203 * :c:macro:`PyArg_ParseTuple` now accepts a :class:`bytearray` for the ``c`` 2204 format (:issue:`12380`). 2205 2206 2207 2208 Deprecated 2209 ========== 2210 2211 Unsupported Operating Systems 2212 ----------------------------- 2213 2214 OS/2 and VMS are no longer supported due to the lack of a maintainer. 2215 2216 Windows 2000 and Windows platforms which set ``COMSPEC`` to ``command.com`` 2217 are no longer supported due to maintenance burden. 2218 2219 OSF support, which was deprecated in 3.2, has been completely removed. 2220 2221 2222 Deprecated Python modules, functions and methods 2223 ------------------------------------------------ 2224 2225 * Passing a non-empty string to ``object.__format__()`` is deprecated, and 2226 will produce a :exc:`TypeError` in Python 3.4 (:issue:`9856`). 2227 * The ``unicode_internal`` codec has been deprecated because of the 2228 :pep:`393`, use UTF-8, UTF-16 (``utf-16-le`` or ``utf-16-be``), or UTF-32 2229 (``utf-32-le`` or ``utf-32-be``) 2230 * :meth:`ftplib.FTP.nlst` and :meth:`ftplib.FTP.dir`: use 2231 :meth:`ftplib.FTP.mlsd` 2232 * :func:`platform.popen`: use the :mod:`subprocess` module. Check especially 2233 the :ref:`subprocess-replacements` section (:issue:`11377`). 2234 * :issue:`13374`: The Windows bytes API has been deprecated in the :mod:`os` 2235 module. Use Unicode filenames, instead of bytes filenames, to not depend on 2236 the ANSI code page anymore and to support any filename. 2237 * :issue:`13988`: The :mod:`xml.etree.cElementTree` module is deprecated. The 2238 accelerator is used automatically whenever available. 2239 * The behaviour of :func:`time.clock` depends on the platform: use the new 2240 :func:`time.perf_counter` or :func:`time.process_time` function instead, 2241 depending on your requirements, to have a well defined behaviour. 2242 * The :func:`os.stat_float_times` function is deprecated. 2243 * :mod:`abc` module: 2244 2245 * :class:`abc.abstractproperty` has been deprecated, use :class:`property` 2246 with :func:`abc.abstractmethod` instead. 2247 * :class:`abc.abstractclassmethod` has been deprecated, use 2248 :class:`classmethod` with :func:`abc.abstractmethod` instead. 2249 * :class:`abc.abstractstaticmethod` has been deprecated, use 2250 :class:`staticmethod` with :func:`abc.abstractmethod` instead. 2251 2252 * :mod:`importlib` package: 2253 2254 * :meth:`importlib.abc.SourceLoader.path_mtime` is now deprecated in favour of 2255 :meth:`importlib.abc.SourceLoader.path_stats` as bytecode files now store 2256 both the modification time and size of the source file the bytecode file was 2257 compiled from. 2258 2259 2260 2261 2262 2263 Deprecated functions and types of the C API 2264 ------------------------------------------- 2265 2266 The :c:type:`Py_UNICODE` has been deprecated by :pep:`393` and will be 2267 removed in Python 4. All functions using this type are deprecated: 2268 2269 Unicode functions and methods using :c:type:`Py_UNICODE` and 2270 :c:type:`Py_UNICODE*` types: 2271 2272 * :c:macro:`PyUnicode_FromUnicode`: use :c:func:`PyUnicode_FromWideChar` or 2273 :c:func:`PyUnicode_FromKindAndData` 2274 * :c:macro:`PyUnicode_AS_UNICODE`, :c:func:`PyUnicode_AsUnicode`, 2275 :c:func:`PyUnicode_AsUnicodeAndSize`: use :c:func:`PyUnicode_AsWideCharString` 2276 * :c:macro:`PyUnicode_AS_DATA`: use :c:macro:`PyUnicode_DATA` with 2277 :c:macro:`PyUnicode_READ` and :c:macro:`PyUnicode_WRITE` 2278 * :c:macro:`PyUnicode_GET_SIZE`, :c:func:`PyUnicode_GetSize`: use 2279 :c:macro:`PyUnicode_GET_LENGTH` or :c:func:`PyUnicode_GetLength` 2280 * :c:macro:`PyUnicode_GET_DATA_SIZE`: use 2281 ``PyUnicode_GET_LENGTH(str) * PyUnicode_KIND(str)`` (only work on ready 2282 strings) 2283 * :c:func:`PyUnicode_AsUnicodeCopy`: use :c:func:`PyUnicode_AsUCS4Copy` or 2284 :c:func:`PyUnicode_AsWideCharString` 2285 * :c:func:`PyUnicode_GetMax` 2286 2287 2288 Functions and macros manipulating Py_UNICODE* strings: 2289 2290 * :c:macro:`Py_UNICODE_strlen`: use :c:func:`PyUnicode_GetLength` or 2291 :c:macro:`PyUnicode_GET_LENGTH` 2292 * :c:macro:`Py_UNICODE_strcat`: use :c:func:`PyUnicode_CopyCharacters` or 2293 :c:func:`PyUnicode_FromFormat` 2294 * :c:macro:`Py_UNICODE_strcpy`, :c:macro:`Py_UNICODE_strncpy`, 2295 :c:macro:`Py_UNICODE_COPY`: use :c:func:`PyUnicode_CopyCharacters` or 2296 :c:func:`PyUnicode_Substring` 2297 * :c:macro:`Py_UNICODE_strcmp`: use :c:func:`PyUnicode_Compare` 2298 * :c:macro:`Py_UNICODE_strncmp`: use :c:func:`PyUnicode_Tailmatch` 2299 * :c:macro:`Py_UNICODE_strchr`, :c:macro:`Py_UNICODE_strrchr`: use 2300 :c:func:`PyUnicode_FindChar` 2301 * :c:macro:`Py_UNICODE_FILL`: use :c:func:`PyUnicode_Fill` 2302 * :c:macro:`Py_UNICODE_MATCH` 2303 2304 Encoders: 2305 2306 * :c:func:`PyUnicode_Encode`: use :c:func:`PyUnicode_AsEncodedObject` 2307 * :c:func:`PyUnicode_EncodeUTF7` 2308 * :c:func:`PyUnicode_EncodeUTF8`: use :c:func:`PyUnicode_AsUTF8` or 2309 :c:func:`PyUnicode_AsUTF8String` 2310 * :c:func:`PyUnicode_EncodeUTF32` 2311 * :c:func:`PyUnicode_EncodeUTF16` 2312 * :c:func:`PyUnicode_EncodeUnicodeEscape:` use 2313 :c:func:`PyUnicode_AsUnicodeEscapeString` 2314 * :c:func:`PyUnicode_EncodeRawUnicodeEscape:` use 2315 :c:func:`PyUnicode_AsRawUnicodeEscapeString` 2316 * :c:func:`PyUnicode_EncodeLatin1`: use :c:func:`PyUnicode_AsLatin1String` 2317 * :c:func:`PyUnicode_EncodeASCII`: use :c:func:`PyUnicode_AsASCIIString` 2318 * :c:func:`PyUnicode_EncodeCharmap` 2319 * :c:func:`PyUnicode_TranslateCharmap` 2320 * :c:func:`PyUnicode_EncodeMBCS`: use :c:func:`PyUnicode_AsMBCSString` or 2321 :c:func:`PyUnicode_EncodeCodePage` (with ``CP_ACP`` code_page) 2322 * :c:func:`PyUnicode_EncodeDecimal`, 2323 :c:func:`PyUnicode_TransformDecimalToASCII` 2324 2325 2326 Deprecated features 2327 ------------------- 2328 2329 The :mod:`array` module's ``'u'`` format code is now deprecated and will be 2330 removed in Python 4 together with the rest of the (:c:type:`Py_UNICODE`) API. 2331 2332 2333 Porting to Python 3.3 2334 ===================== 2335 2336 This section lists previously described changes and other bugfixes 2337 that may require changes to your code. 2338 2339 .. _portingpythoncode: 2340 2341 Porting Python code 2342 ------------------- 2343 2344 * Hash randomization is enabled by default. Set the :envvar:`PYTHONHASHSEED` 2345 environment variable to ``0`` to disable hash randomization. See also the 2346 :meth:`object.__hash__` method. 2347 2348 * :issue:`12326`: On Linux, sys.platform doesn't contain the major version 2349 anymore. It is now always 'linux', instead of 'linux2' or 'linux3' depending 2350 on the Linux version used to build Python. Replace sys.platform == 'linux2' 2351 with sys.platform.startswith('linux'), or directly sys.platform == 'linux' if 2352 you don't need to support older Python versions. 2353 2354 * :issue:`13847`, :issue:`14180`: :mod:`time` and :mod:`datetime`: 2355 :exc:`OverflowError` is now raised instead of :exc:`ValueError` if a 2356 timestamp is out of range. :exc:`OSError` is now raised if C functions 2357 :c:func:`gmtime` or :c:func:`localtime` failed. 2358 2359 * The default finders used by import now utilize a cache of what is contained 2360 within a specific directory. If you create a Python source file or sourceless 2361 bytecode file, make sure to call :func:`importlib.invalidate_caches` to clear 2362 out the cache for the finders to notice the new file. 2363 2364 * :exc:`ImportError` now uses the full name of the module that was attempted to 2365 be imported. Doctests that check ImportErrors' message will need to be 2366 updated to use the full name of the module instead of just the tail of the 2367 name. 2368 2369 * The *index* argument to :func:`__import__` now defaults to 0 instead of -1 2370 and no longer support negative values. It was an oversight when :pep:`328` was 2371 implemented that the default value remained -1. If you need to continue to 2372 perform a relative import followed by an absolute import, then perform the 2373 relative import using an index of 1, followed by another import using an 2374 index of 0. It is preferred, though, that you use 2375 :func:`importlib.import_module` rather than call :func:`__import__` directly. 2376 2377 * :func:`__import__` no longer allows one to use an index value other than 0 2378 for top-level modules. E.g. ``__import__('sys', level=1)`` is now an error. 2379 2380 * Because :attr:`sys.meta_path` and :attr:`sys.path_hooks` now have finders on 2381 them by default, you will most likely want to use :meth:`list.insert` instead 2382 of :meth:`list.append` to add to those lists. 2383 2384 * Because ``None`` is now inserted into :attr:`sys.path_importer_cache`, if you 2385 are clearing out entries in the dictionary of paths that do not have a 2386 finder, you will need to remove keys paired with values of ``None`` **and** 2387 :class:`imp.NullImporter` to be backwards-compatible. This will lead to extra 2388 overhead on older versions of Python that re-insert ``None`` into 2389 :attr:`sys.path_importer_cache` where it represents the use of implicit 2390 finders, but semantically it should not change anything. 2391 2392 * :class:`importlib.abc.Finder` no longer specifies a `find_module()` abstract 2393 method that must be implemented. If you were relying on subclasses to 2394 implement that method, make sure to check for the method's existence first. 2395 You will probably want to check for `find_loader()` first, though, in the 2396 case of working with :term:`path entry finders <path entry finder>`. 2397 2398 * :mod:`pkgutil` has been converted to use :mod:`importlib` internally. This 2399 eliminates many edge cases where the old behaviour of the PEP 302 import 2400 emulation failed to match the behaviour of the real import system. The 2401 import emulation itself is still present, but is now deprecated. The 2402 :func:`pkgutil.iter_importers` and :func:`pkgutil.walk_packages` functions 2403 special case the standard import hooks so they are still supported even 2404 though they do not provide the non-standard ``iter_modules()`` method. 2405 2406 * A longstanding RFC-compliance bug (:issue:`1079`) in the parsing done by 2407 :func:`email.header.decode_header` has been fixed. Code that uses the 2408 standard idiom to convert encoded headers into unicode 2409 (``str(make_header(decode_header(h))``) will see no change, but code that 2410 looks at the individual tuples returned by decode_header will see that 2411 whitespace that precedes or follows ``ASCII`` sections is now included in the 2412 ``ASCII`` section. Code that builds headers using ``make_header`` should 2413 also continue to work without change, since ``make_header`` continues to add 2414 whitespace between ``ASCII`` and non-``ASCII`` sections if it is not already 2415 present in the input strings. 2416 2417 * :func:`email.utils.formataddr` now does the correct content transfer 2418 encoding when passed non-``ASCII`` display names. Any code that depended on 2419 the previous buggy behavior that preserved the non-``ASCII`` unicode in the 2420 formatted output string will need to be changed (:issue:`1690608`). 2421 2422 * :meth:`poplib.POP3.quit` may now raise protocol errors like all other 2423 ``poplib`` methods. Code that assumes ``quit`` does not raise 2424 :exc:`poplib.error_proto` errors may need to be changed if errors on ``quit`` 2425 are encountered by a particular application (:issue:`11291`). 2426 2427 * The ``strict`` argument to :class:`email.parser.Parser`, deprecated since 2428 Python 2.4, has finally been removed. 2429 2430 * The deprecated method ``unittest.TestCase.assertSameElements`` has been 2431 removed. 2432 2433 * The deprecated variable ``time.accept2dyear`` has been removed. 2434 2435 * The deprecated ``Context._clamp`` attribute has been removed from the 2436 :mod:`decimal` module. It was previously replaced by the public attribute 2437 :attr:`~decimal.Context.clamp`. (See :issue:`8540`.) 2438 2439 * The undocumented internal helper class ``SSLFakeFile`` has been removed 2440 from :mod:`smtplib`, since its functionality has long been provided directly 2441 by :meth:`socket.socket.makefile`. 2442 2443 * Passing a negative value to :func:`time.sleep` on Windows now raises an 2444 error instead of sleeping forever. It has always raised an error on posix. 2445 2446 * The ``ast.__version__`` constant has been removed. If you need to 2447 make decisions affected by the AST version, use :attr:`sys.version_info` 2448 to make the decision. 2449 2450 * Code that used to work around the fact that the :mod:`threading` module used 2451 factory functions by subclassing the private classes will need to change to 2452 subclass the now-public classes. 2453 2454 * The undocumented debugging machinery in the threading module has been 2455 removed, simplifying the code. This should have no effect on production 2456 code, but is mentioned here in case any application debug frameworks were 2457 interacting with it (:issue:`13550`). 2458 2459 2460 Porting C code 2461 -------------- 2462 2463 * In the course of changes to the buffer API the undocumented 2464 :c:member:`~Py_buffer.smalltable` member of the 2465 :c:type:`Py_buffer` structure has been removed and the 2466 layout of the :c:type:`PyMemoryViewObject` has changed. 2467 2468 All extensions relying on the relevant parts in ``memoryobject.h`` 2469 or ``object.h`` must be rebuilt. 2470 2471 * Due to :ref:`PEP 393 <pep-393>`, the :c:type:`Py_UNICODE` type and all 2472 functions using this type are deprecated (but will stay available for 2473 at least five years). If you were using low-level Unicode APIs to 2474 construct and access unicode objects and you want to benefit of the 2475 memory footprint reduction provided by PEP 393, you have to convert 2476 your code to the new :doc:`Unicode API <../c-api/unicode>`. 2477 2478 However, if you only have been using high-level functions such as 2479 :c:func:`PyUnicode_Concat()`, :c:func:`PyUnicode_Join` or 2480 :c:func:`PyUnicode_FromFormat()`, your code will automatically take 2481 advantage of the new unicode representations. 2482 2483 * :c:func:`PyImport_GetMagicNumber` now returns ``-1`` upon failure. 2484 2485 * As a negative value for the *level* argument to :func:`__import__` is no 2486 longer valid, the same now holds for :c:func:`PyImport_ImportModuleLevel`. 2487 This also means that the value of *level* used by 2488 :c:func:`PyImport_ImportModuleEx` is now ``0`` instead of ``-1``. 2489 2490 2491 Building C extensions 2492 --------------------- 2493 2494 * The range of possible file names for C extensions has been narrowed. 2495 Very rarely used spellings have been suppressed: under POSIX, files 2496 named ``xxxmodule.so``, ``xxxmodule.abi3.so`` and 2497 ``xxxmodule.cpython-*.so`` are no longer recognized as implementing 2498 the ``xxx`` module. If you had been generating such files, you have 2499 to switch to the other spellings (i.e., remove the ``module`` string 2500 from the file names). 2501 2502 (implemented in :issue:`14040`.) 2503 2504 2505 Command Line Switch Changes 2506 --------------------------- 2507 2508 * The -Q command-line flag and related artifacts have been removed. Code 2509 checking sys.flags.division_warning will need updating. 2510 2511 (:issue:`10998`, contributed by ric Araujo.) 2512 2513 * When :program:`python` is started with :option:`-S`, ``import site`` 2514 will no longer add site-specific paths to the module search paths. In 2515 previous versions, it did. 2516 2517 (:issue:`11591`, contributed by Carl Meyer with editions by ric Araujo.) 2518