ITookAPill's picture
PyComp First Commit
9273228
|
Raw
History Blame Contribute Delete
133 kB

A newer version of the Gradio SDK is available: 6.22.0

Upgrade

What's New In Python 3.4

Author : R. David Murray <rdmurray@bitdance.com> (Editor)

* Anyone can add text to this document, but the maintainer reserves the right to rewrite any additions. In particular, for obscure or esoteric features, the maintainer may reduce any addition to a simple reference to the new documentation rather than explaining the feature inline.

* While the maintainer will periodically go through Misc/NEWS and add changes, it's best not to rely on this. We know from experience that any changes that aren't in the What's New documentation around the time of the original release will remain largely unknown to the community for years, even if they're added later. We also know from experience that other priorities can arise, and the maintainer will run out of time to do updates -- in such cases, end users will be much better served by partial notifications that at least give a hint about new features to investigate.

* This is not a complete list of every single change; completeness is the purpose of Misc/NEWS. The What's New should focus on changes that are visible to Python users and that require a feature release (i.e. most bug fixes should only be recorded in Misc/NEWS)

* PEPs should not be marked Final until they have an entry in What's New. A placeholder entry that is just a section header and a link to the PEP (e.g "397{.interpreted-text role="pep"} has been implemented") is acceptable. If a PEP has been implemented and noted in What's New, don't forget to mark it as Final!

* If you want to draw your new text to the attention of the maintainer, add 'XXX' to the beginning of the paragraph or section.

* It's OK to add just a very brief note about a change. For example: "The ~socket.transmogrify(){.interpreted-text role="ref"} function was added to the socket{.interpreted-text role="mod"} module." The maintainer will research the change and write the necessary text (if appropriate). The advantage of doing this is that even if no more descriptive text is ever added, readers will at least have a notification that the new feature exists and a link to the relevant documentation.

* You can comment out your additions if you like, but it's not necessary (especially when a final release is some months away).

* Credit the author of a patch or bugfix. Just the name is sufficient; the e-mail address isn't necessary.

  • It's helpful to add the bug/patch number as a comment:

The ~socket.transmogrify(){.interpreted-text role="ref"} function was added to the socket{.interpreted-text role="mod"} module. (Contributed by P.Y. Developer in 12345{.interpreted-text role="issue"}.)

This saves the maintainer the effort of going through the Mercurial log when researching a change.

* Cross referencing tip: mod.attr{.interpreted-text role="ref"} will display as mod.attr, while ~mod.attr{.interpreted-text role="ref"} will display as attr.

This article explains the new features in Python 3.4, compared to 3.3. Python 3.4 was released on March 16, 2014. For full details, see the changelog.

::: seealso 429{.interpreted-text role="pep"} -- Python 3.4 Release Schedule :::

Summary -- Release Highlights

New syntax features:

  • No new syntax features were added in Python 3.4.

Other new features:

  • pip should always be available <whatsnew-pep-453>{.interpreted-text role="ref"} (453{.interpreted-text role="pep"}).
  • Newly created file descriptors are non-inheritable <whatsnew-pep-446>{.interpreted-text role="ref"} (446{.interpreted-text role="pep"}).
  • command line option for isolated mode <whatsnew-isolated-mode>{.interpreted-text role="ref"} (16499{.interpreted-text role="issue"}).
  • improvements in the handling of codecs <codec-handling-improvements>{.interpreted-text role="ref"} that are not text encodings (multiple issues).
  • A ModuleSpec Type <whatsnew-pep-451>{.interpreted-text role="ref"} for the Import System (451{.interpreted-text role="pep"}). (Affects importer authors.)
  • The marshal{.interpreted-text role="mod"} format has been made more compact and efficient <whatsnew-marshal-3>{.interpreted-text role="ref"} (16475{.interpreted-text role="issue"}).

New library modules:

  • asyncio{.interpreted-text role="mod"}: New provisional API for asynchronous IO <whatsnew-asyncio>{.interpreted-text role="ref"} (3156{.interpreted-text role="pep"}).
  • ensurepip{.interpreted-text role="mod"}: Bootstrapping the pip installer <whatsnew-ensurepip>{.interpreted-text role="ref"} (453{.interpreted-text role="pep"}).
  • enum{.interpreted-text role="mod"}: Support for enumeration types <whatsnew-enum>{.interpreted-text role="ref"} (435{.interpreted-text role="pep"}).
  • pathlib{.interpreted-text role="mod"}: Object-oriented filesystem paths <whatsnew-pathlib>{.interpreted-text role="ref"} (428{.interpreted-text role="pep"}).
  • selectors{.interpreted-text role="mod"}: High-level and efficient I/O multiplexing <whatsnew-selectors>{.interpreted-text role="ref"}, built upon the select{.interpreted-text role="mod"} module primitives (part of 3156{.interpreted-text role="pep"}).
  • statistics{.interpreted-text role="mod"}: A basic numerically stable statistics library <whatsnew-statistics>{.interpreted-text role="ref"} (450{.interpreted-text role="pep"}).
  • tracemalloc{.interpreted-text role="mod"}: Trace Python memory allocations <whatsnew-tracemalloc>{.interpreted-text role="ref"} (454{.interpreted-text role="pep"}).

Significantly improved library modules:

  • Single-dispatch generic functions <whatsnew-singledispatch>{.interpreted-text role="ref"} in functools{.interpreted-text role="mod"} (443{.interpreted-text role="pep"}).
  • New pickle{.interpreted-text role="mod"} protocol 4 <whatsnew-protocol-4>{.interpreted-text role="ref"} (3154{.interpreted-text role="pep"}).
  • multiprocessing{.interpreted-text role="mod"} now has an option to avoid using os.fork on Unix <whatsnew-multiprocessing-no-fork>{.interpreted-text role="ref"} (8713{.interpreted-text role="issue"}).
  • email{.interpreted-text role="mod"} has a new submodule, ~email.contentmanager{.interpreted-text role="mod"}, and a new ~email.message.Message{.interpreted-text role="mod"} subclass (~email.message.EmailMessage{.interpreted-text role="class"}) that simplify MIME handling <whatsnew_email_contentmanager>{.interpreted-text role="ref"} (18891{.interpreted-text role="issue"}).
  • The inspect{.interpreted-text role="mod"} and pydoc{.interpreted-text role="mod"} modules are now capable of correct introspection of a much wider variety of callable objects, which improves the output of the Python help{.interpreted-text role="func"} system.
  • The ipaddress{.interpreted-text role="mod"} module API has been declared stable

Security improvements:

  • Secure and interchangeable hash algorithm <whatsnew-pep-456>{.interpreted-text role="ref"} (456{.interpreted-text role="pep"}).
  • Make newly created file descriptors non-inheritable <whatsnew-pep-446>{.interpreted-text role="ref"} (446{.interpreted-text role="pep"}) to avoid leaking file descriptors to child processes.
  • New command line option for isolated mode <whatsnew-isolated-mode>{.interpreted-text role="ref"}, (16499{.interpreted-text role="issue"}).
  • multiprocessing{.interpreted-text role="mod"} now has an option to avoid using os.fork on Unix <whatsnew-multiprocessing-no-fork>{.interpreted-text role="ref"}. spawn and forkserver are more secure because they avoid sharing data with child processes.
  • multiprocessing{.interpreted-text role="mod"} child processes on Windows no longer inherit all of the parent's inheritable handles, only the necessary ones.
  • A new hashlib.pbkdf2_hmac{.interpreted-text role="func"} function provides the PKCS#5 password-based key derivation function 2.
  • TLSv1.1 and TLSv1.2 support <whatsnew-tls-11-12>{.interpreted-text role="ref"} for ssl{.interpreted-text role="mod"}.
  • Retrieving certificates from the Windows system cert store support <whatsnew34-win-cert-store>{.interpreted-text role="ref"} for ssl{.interpreted-text role="mod"}.
  • Server-side SNI (Server Name Indication) support <whatsnew34-sni>{.interpreted-text role="ref"} for ssl{.interpreted-text role="mod"}.
  • The ssl.SSLContext{.interpreted-text role="class"} class has a lot of improvements <whatsnew34-sslcontext>{.interpreted-text role="ref"}.
  • All modules in the standard library that support SSL now support server certificate verification, including hostname matching (!ssl.match_hostname{.interpreted-text role="func"}) and CRLs (Certificate Revocation lists, see ssl.SSLContext.load_verify_locations{.interpreted-text role="func"}).

CPython implementation improvements:

  • Safe object finalization <whatsnew-pep-442>{.interpreted-text role="ref"} (442{.interpreted-text role="pep"}).
  • Leveraging 442{.interpreted-text role="pep"}, in most cases module globals are no longer set to None during finalization <whatsnew-pep-442>{.interpreted-text role="ref"} (18214{.interpreted-text role="issue"}).
  • Configurable memory allocators <whatsnew-pep-445>{.interpreted-text role="ref"} (445{.interpreted-text role="pep"}).
  • Argument Clinic <whatsnew-pep-436>{.interpreted-text role="ref"} (436{.interpreted-text role="pep"}).

Please read on for a comprehensive list of user-facing changes, including many other smaller improvements, CPython optimizations, deprecations, and potential porting issues.

New Features

PEP 453: Explicit Bootstrapping of PIP in Python Installations {#whatsnew-pep-453}

Bootstrapping pip By Default

The new ensurepip{.interpreted-text role="mod"} module (defined in 453{.interpreted-text role="pep"}) provides a standard cross-platform mechanism to bootstrap the pip installer into Python installations and virtual environments. The version of pip included with Python 3.4.0 is pip 1.5.4, and future 3.4.x maintenance releases will update the bundled version to the latest version of pip that is available at the time of creating the release candidate.

By default, the commands pipX and pipX.Y will be installed on all platforms (where X.Y stands for the version of the Python installation), along with the pip Python package and its dependencies. On Windows and in virtual environments on all platforms, the unversioned pip command will also be installed. On other platforms, the system wide unversioned pip command typically refers to the separately installed Python 2 version.

The pyvenv command line utility and the venv{.interpreted-text role="mod"} module make use of the ensurepip{.interpreted-text role="mod"} module to make pip readily available in virtual environments. When using the command line utility, pip is installed by default, while when using the venv{.interpreted-text role="mod"} module venv-api{.interpreted-text role="ref"} installation of pip must be requested explicitly.

For CPython source builds on POSIX systems <building-python-on-unix>{.interpreted-text role="ref"}, the make install and make altinstall commands bootstrap pip by default. This behaviour can be controlled through configure options, and overridden through Makefile options.

On Windows and Mac OS X, the CPython installers now default to installing pip along with CPython itself (users may opt out of installing it during the installation process). Window users will need to opt in to the automatic PATH modifications to have pip available from the command line by default, otherwise it can still be accessed through the Python launcher for Windows as py -m pip.

As discussed in the PEP <0453#recommendations-for-downstream-distributors>{.interpreted-text role="pep"} platform packagers may choose not to install these commands by default, as long as, when invoked, they provide clear and simple directions on how to install them on that platform (usually using the system package manager).

:::: note ::: title Note :::

To avoid conflicts between parallel Python 2 and Python 3 installations, only the versioned pip3 and pip3.4 commands are bootstrapped by default when ensurepip is invoked directly - the --default-pip option is needed to also request the unversioned pip command. pyvenv and the Windows installer ensure that the unqualified pip command is made available in those environments, and pip can always be invoked via the -m switch rather than directly to avoid ambiguity on systems with multiple Python installations. ::::

Documentation Changes

As part of this change, the installing-index{.interpreted-text role="ref"} and distributing-index{.interpreted-text role="ref"} sections of the documentation have been completely redesigned as short getting started and FAQ documents. Most packaging documentation has now been moved out to the Python Packaging Authority maintained Python Packaging User Guide and the documentation of the individual projects.

However, as this migration is currently still incomplete, the legacy versions of those guides remaining available as install-index{.interpreted-text role="ref"} and setuptools-index{.interpreted-text role="ref"}.

::: seealso

453{.interpreted-text role="pep"} -- Explicit bootstrapping of pip in Python installations

: PEP written by Donald Stufft and Nick Coghlan, implemented by Donald Stufft, Nick Coghlan, Martin von Löwis and Ned Deily. :::

PEP 446: Newly Created File Descriptors Are Non-Inheritable {#whatsnew-pep-446}

446{.interpreted-text role="pep"} makes newly created file descriptors non-inheritable <fd_inheritance>{.interpreted-text role="ref"}. In general, this is the behavior an application will want: when launching a new process, having currently open files also open in the new process can lead to all sorts of hard to find bugs, and potentially to security issues.

However, there are occasions when inheritance is desired. To support these cases, the following new functions and methods are available:

  • os.get_inheritable{.interpreted-text role="func"}, os.set_inheritable{.interpreted-text role="func"}
  • os.get_handle_inheritable{.interpreted-text role="func"}, os.set_handle_inheritable{.interpreted-text role="func"}
  • socket.socket.get_inheritable{.interpreted-text role="meth"}, socket.socket.set_inheritable{.interpreted-text role="meth"}

::: seealso

446{.interpreted-text role="pep"} -- Make newly created file descriptors non-inheritable

: PEP written and implemented by Victor Stinner. :::

Improvements to Codec Handling {#codec-handling-improvements}

Since it was first introduced, the codecs{.interpreted-text role="mod"} module has always been intended to operate as a type-neutral dynamic encoding and decoding system. However, its close coupling with the Python text model, especially the type restricted convenience methods on the builtin str{.interpreted-text role="class"}, bytes{.interpreted-text role="class"} and bytearray{.interpreted-text role="class"} types, has historically obscured that fact.

As a key step in clarifying the situation, the codecs.encode{.interpreted-text role="meth"} and codecs.decode{.interpreted-text role="meth"} convenience functions are now properly documented in Python 2.7, 3.3 and 3.4. These functions have existed in the codecs{.interpreted-text role="mod"} module (and have been covered by the regression test suite) since Python 2.4, but were previously only discoverable through runtime introspection.

Unlike the convenience methods on str{.interpreted-text role="class"}, bytes{.interpreted-text role="class"} and bytearray{.interpreted-text role="class"}, the codecs{.interpreted-text role="mod"} convenience functions support arbitrary codecs in both Python 2 and Python 3, rather than being limited to Unicode text encodings (in Python 3) or basestring <-> basestring conversions (in Python 2).

In Python 3.4, the interpreter is able to identify the known non-text encodings provided in the standard library and direct users towards these general purpose convenience functions when appropriate:

>>> b"abcdef".decode("hex")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs

>>> "hello".encode("rot13")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs

>>> open("foo.txt", encoding="hex")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.open() to handle arbitrary codecs

In a related change, whenever it is feasible without breaking backwards compatibility, exceptions raised during encoding and decoding operations are wrapped in a chained exception of the same type that mentions the name of the codec responsible for producing the error:

>>> import codecs

>>> codecs.decode(b"abcdefgh", "hex")
Traceback (most recent call last):
  File "/usr/lib/python3.4/encodings/hex_codec.py", line 20, in hex_decode
    return (binascii.a2b_hex(input), len(input))
binascii.Error: Non-hexadecimal digit found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: decoding with 'hex' codec failed (Error: Non-hexadecimal digit found)

>>> codecs.encode("hello", "bz2")
Traceback (most recent call last):
  File "/usr/lib/python3.4/encodings/bz2_codec.py", line 17, in bz2_encode
    return (bz2.compress(input), len(input))
  File "/usr/lib/python3.4/bz2.py", line 498, in compress
    return comp.compress(data) + comp.flush()
TypeError: 'str' does not support the buffer interface

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface)

Finally, as the examples above show, these improvements have permitted the restoration of the convenience aliases for the non-Unicode codecs that were themselves restored in Python 3.2. This means that encoding binary data to and from its hexadecimal representation (for example) can now be written as:

>>> from codecs import encode, decode
>>> encode(b"hello", "hex")
b'68656c6c6f'
>>> decode(b"68656c6c6f", "hex")
b'hello'

The binary and text transforms provided in the standard library are detailed in binary-transforms{.interpreted-text role="ref"} and text-transforms{.interpreted-text role="ref"}.

(Contributed by Nick Coghlan in 7475{.interpreted-text role="issue"}, 17827{.interpreted-text role="issue"}, 17828{.interpreted-text role="issue"} and 19619{.interpreted-text role="issue"}.)

PEP 451: A ModuleSpec Type for the Import System {#whatsnew-pep-451}

451{.interpreted-text role="pep"} provides an encapsulation of the information about a module that the import machinery will use to load it (that is, a module specification). This helps simplify both the import implementation and several import-related APIs. The change is also a stepping stone for several future import-related improvements.

The public-facing changes from the PEP are entirely backward-compatible. Furthermore, they should be transparent to everyone but importer authors. Key finder and loader methods have been deprecated, but they will continue working. New importers should use the new methods described in the PEP. Existing importers should be updated to implement the new methods. See the deprecated-3.4{.interpreted-text role="ref"} section for a list of methods that should be replaced and their replacements.

Other Language Changes

Some smaller changes made to the core Python language are:

  • Unicode database updated to UCD version 6.3.
  • min{.interpreted-text role="func"} and max{.interpreted-text role="func"} now accept a default keyword-only argument that can be used to specify the value they return if the iterable they are evaluating has no elements. (Contributed by Julian Berman in 18111{.interpreted-text role="issue"}.)
  • Module objects are now weakly referenceable <mod-weakref>{.interpreted-text role="ref"}.
  • Module __file__ attributes (and related values) should now always contain absolute paths by default, with the sole exception of __main__.__file__ when a script has been executed directly using a relative path. (Contributed by Brett Cannon in 18416{.interpreted-text role="issue"}.)
  • All the UTF-* codecs (except UTF-7) now reject surrogates during both encoding and decoding unless the surrogatepass error handler is used, with the exception of the UTF-16 decoder (which accepts valid surrogate pairs) and the UTF-16 encoder (which produces them while encoding non-BMP characters). (Contributed by Victor Stinner, Kang-Hao (Kenny) Lu and Serhiy Storchaka in 12892{.interpreted-text role="issue"}.)
  • New German EBCDIC codec <standard-encodings>{.interpreted-text role="ref"} cp273. (Contributed by Michael Bierenfeld and Andrew Kuchling in 1097797{.interpreted-text role="issue"}.)
  • New Ukrainian codec <standard-encodings>{.interpreted-text role="ref"} cp1125. (Contributed by Serhiy Storchaka in 19668{.interpreted-text role="issue"}.)
  • bytes{.interpreted-text role="class"}.join() and bytearray{.interpreted-text role="class"}.join() now accept arbitrary buffer objects as arguments. (Contributed by Antoine Pitrou in 15958{.interpreted-text role="issue"}.)
  • The int{.interpreted-text role="class"} constructor now accepts any object that has an __index__ method for its base argument. (Contributed by Mark Dickinson in 16772{.interpreted-text role="issue"}.)
  • Frame objects now have a ~frame.clear{.interpreted-text role="func"} method that clears all references to local variables from the frame. (Contributed by Antoine Pitrou in 17934{.interpreted-text role="issue"}.)
  • memoryview{.interpreted-text role="class"} is now registered as a Sequence <collections.abc>{.interpreted-text role="class"}, and supports the reversed{.interpreted-text role="func"} builtin. (Contributed by Nick Coghlan and Claudiu Popa in 18690{.interpreted-text role="issue"} and 19078{.interpreted-text role="issue"}.)
  • Signatures reported by help{.interpreted-text role="func"} have been modified and improved in several cases as a result of the introduction of Argument Clinic and other changes to the inspect{.interpreted-text role="mod"} and pydoc{.interpreted-text role="mod"} modules.
  • ~object.__length_hint__{.interpreted-text role="meth"} is now part of the formal language specification (see 424{.interpreted-text role="pep"}). (Contributed by Armin Ronacher in 16148{.interpreted-text role="issue"}.)

New Modules

asyncio {#whatsnew-asyncio}

The new asyncio{.interpreted-text role="mod"} module (defined in 3156{.interpreted-text role="pep"}) provides a standard pluggable event loop model for Python, providing solid asynchronous IO support in the standard library, and making it easier for other event loop implementations to interoperate with the standard library and each other.

For Python 3.4, this module is considered a provisional API{.interpreted-text role="term"}.

::: seealso

3156{.interpreted-text role="pep"} -- Asynchronous IO Support Rebooted: the "asyncio" Module

: PEP written and implementation led by Guido van Rossum. :::

ensurepip {#whatsnew-ensurepip}

The new ensurepip{.interpreted-text role="mod"} module is the primary infrastructure for the 453{.interpreted-text role="pep"} implementation. In the normal course of events end users will not need to interact with this module, but it can be used to manually bootstrap pip if the automated bootstrapping into an installation or virtual environment was declined.

ensurepip{.interpreted-text role="mod"} includes a bundled copy of pip, up-to-date as of the first release candidate of the release of CPython with which it ships (this applies to both maintenance releases and feature releases). ensurepip does not access the internet. If the installation has internet access, after ensurepip is run the bundled pip can be used to upgrade pip to a more recent release than the bundled one. (Note that such an upgraded version of pip is considered to be a separately installed package and will not be removed if Python is uninstalled.)

The module is named ensurepip because if called when pip is already installed, it does nothing. It also has an --upgrade option that will cause it to install the bundled copy of pip if the existing installed version of pip is older than the bundled copy.

enum {#whatsnew-enum}

The new enum{.interpreted-text role="mod"} module (defined in 435{.interpreted-text role="pep"}) provides a standard implementation of enumeration types, allowing other modules (such as socket{.interpreted-text role="mod"}) to provide more informative error messages and better debugging support by replacing opaque integer constants with backwards compatible enumeration values.

::: seealso

435{.interpreted-text role="pep"} -- Adding an Enum type to the Python standard library

: PEP written by Barry Warsaw, Eli Bendersky and Ethan Furman, implemented by Ethan Furman. :::

pathlib {#whatsnew-pathlib}

The new pathlib{.interpreted-text role="mod"} module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

For Python 3.4, this module is considered a provisional API{.interpreted-text role="term"}.

::: seealso

428{.interpreted-text role="pep"} -- The pathlib module -- object-oriented filesystem paths

: PEP written and implemented by Antoine Pitrou. :::

selectors {#whatsnew-selectors}

The new selectors{.interpreted-text role="mod"} module (created as part of implementing 3156{.interpreted-text role="pep"}) allows high-level and efficient I/O multiplexing, built upon the select{.interpreted-text role="mod"} module primitives.

statistics {#whatsnew-statistics}

The new statistics{.interpreted-text role="mod"} module (defined in 450{.interpreted-text role="pep"}) offers some core statistics functionality directly in the standard library. This module supports calculation of the mean, median, mode, variance and standard deviation of a data series.

::: seealso

450{.interpreted-text role="pep"} -- Adding A Statistics Module To The Standard Library

: PEP written and implemented by Steven D'Aprano :::

tracemalloc {#whatsnew-tracemalloc}

The new tracemalloc{.interpreted-text role="mod"} module (defined in 454{.interpreted-text role="pep"}) is a debug tool to trace memory blocks allocated by Python. It provides the following information:

  • Trace where an object was allocated
  • Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks
  • Compute the differences between two snapshots to detect memory leaks

::: seealso

454{.interpreted-text role="pep"} -- Add a new tracemalloc module to trace Python memory allocations

: PEP written and implemented by Victor Stinner :::

Improved Modules

abc

New function abc.get_cache_token{.interpreted-text role="func"} can be used to know when to invalidate caches that are affected by changes in the object graph. (Contributed by Łukasz Langa in 16832{.interpreted-text role="issue"}.)

New class ~abc.ABC{.interpreted-text role="class"} has ~abc.ABCMeta{.interpreted-text role="class"} as its meta class. Using ABC as a base class has essentially the same effect as specifying metaclass=abc.ABCMeta, but is simpler to type and easier to read. (Contributed by Bruno Dupuis in 16049{.interpreted-text role="issue"}.)

aifc

The !getparams{.interpreted-text role="meth"} method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in 17818{.interpreted-text role="issue"}.)

!aifc.open{.interpreted-text role="func"} now supports the context management protocol: when used in a with{.interpreted-text role="keyword"} block, the !close{.interpreted-text role="meth"} method of the returned object will be called automatically at the end of the block. (Contributed by Serhiy Storchacha in 16486{.interpreted-text role="issue"}.)

The !writeframesraw{.interpreted-text role="meth"} and !writeframes{.interpreted-text role="meth"} methods now accept any bytes-like object{.interpreted-text role="term"}. (Contributed by Serhiy Storchaka in 8311{.interpreted-text role="issue"}.)

argparse

The ~argparse.FileType{.interpreted-text role="class"} class now accepts encoding and errors arguments, which are passed through to open{.interpreted-text role="func"}. (Contributed by Lucas Maystre in 11175{.interpreted-text role="issue"}.)

audioop

!audioop{.interpreted-text role="mod"} now supports 24-bit samples. (Contributed by Serhiy Storchaka in 12866{.interpreted-text role="issue"}.)

New !byteswap{.interpreted-text role="func"} function converts big-endian samples to little-endian and vice versa. (Contributed by Serhiy Storchaka in 19641{.interpreted-text role="issue"}.)

All !audioop{.interpreted-text role="mod"} functions now accept any bytes-like object{.interpreted-text role="term"}. Strings are not accepted: they didn't work before, now they raise an error right away. (Contributed by Serhiy Storchaka in 16685{.interpreted-text role="issue"}.)

base64

The encoding and decoding functions in base64{.interpreted-text role="mod"} now accept any bytes-like object{.interpreted-text role="term"} in cases where it previously required a bytes{.interpreted-text role="class"} or bytearray{.interpreted-text role="class"} instance. (Contributed by Nick Coghlan in 17839{.interpreted-text role="issue"}.)

New functions ~base64.a85encode{.interpreted-text role="func"}, ~base64.a85decode{.interpreted-text role="func"}, ~base64.b85encode{.interpreted-text role="func"}, and ~base64.b85decode{.interpreted-text role="func"} provide the ability to encode and decode binary data from and to Ascii85 and the git/mercurial Base85 formats, respectively. The a85 functions have options that can be used to make them compatible with the variants of the Ascii85 encoding, including the Adobe variant. (Contributed by Martin Morrison, the Mercurial project, Serhiy Storchaka, and Antoine Pitrou in 17618{.interpreted-text role="issue"}.)

collections

The .ChainMap.new_child{.interpreted-text role="meth"} method now accepts an m argument specifying the child map to add to the chain. This allows an existing mapping and/or a custom mapping type to be used for the child. (Contributed by Vinay Sajip in 16613{.interpreted-text role="issue"}.)

colorsys

The number of digits in the coefficients for the RGB --- YIQ conversions have been expanded so that they match the FCC NTSC versions. The change in results should be less than 1% and may better match results found elsewhere. (Contributed by Brian Landers and Serhiy Storchaka in 14323{.interpreted-text role="issue"}.)

contextlib

The new contextlib.suppress{.interpreted-text role="class"} context manager helps to clarify the intent of code that deliberately suppresses exceptions from a single statement. (Contributed by Raymond Hettinger in 15806{.interpreted-text role="issue"} and Zero Piraeus in 19266{.interpreted-text role="issue"}.)

The new contextlib.redirect_stdout{.interpreted-text role="func"} context manager makes it easier for utility scripts to handle inflexible APIs that write their output to sys.stdout{.interpreted-text role="data"} and don't provide any options to redirect it. Using the context manager, the sys.stdout{.interpreted-text role="data"} output can be redirected to any other stream or, in conjunction with io.StringIO{.interpreted-text role="class"}, to a string. The latter can be especially useful, for example, to capture output from a function that was written to implement a command line interface. It is recommended only for utility scripts because it affects the global state of sys.stdout{.interpreted-text role="data"}. (Contributed by Raymond Hettinger in 15805{.interpreted-text role="issue"}.)

The contextlib{.interpreted-text role="mod"} documentation has also been updated to include a discussion <single-use-reusable-and-reentrant-cms>{.interpreted-text role="ref"} of the differences between single use, reusable and reentrant context managers.

dbm

dbm.open{.interpreted-text role="func"} objects now support the context management protocol. When used in a with{.interpreted-text role="keyword"} statement, the close method of the database object will be called automatically at the end of the block. (Contributed by Claudiu Popa and Nick Coghlan in 19282{.interpreted-text role="issue"}.)

dis

Functions ~dis.show_code{.interpreted-text role="func"}, ~dis.dis{.interpreted-text role="func"}, ~dis.distb{.interpreted-text role="func"}, and ~dis.disassemble{.interpreted-text role="func"} now accept a keyword-only file argument that controls where they write their output.

The dis{.interpreted-text role="mod"} module is now built around an ~dis.Instruction{.interpreted-text role="class"} class that provides object oriented access to the details of each individual bytecode operation.

A new method, ~dis.get_instructions{.interpreted-text role="func"}, provides an iterator that emits the Instruction stream for a given piece of Python code. Thus it is now possible to write a program that inspects and manipulates a bytecode object in ways different from those provided by the ~dis{.interpreted-text role="mod"} module itself. For example:

>>> import dis
>>> for instr in dis.get_instructions(lambda x: x + 1):
...     print(instr.opname)
LOAD_FAST
LOAD_CONST
BINARY_ADD
RETURN_VALUE

The various display tools in the dis{.interpreted-text role="mod"} module have been rewritten to use these new components.

In addition, a new application-friendly class ~dis.Bytecode{.interpreted-text role="class"} provides an object-oriented API for inspecting bytecode in both in human-readable form and for iterating over instructions. The ~dis.Bytecode{.interpreted-text role="class"} constructor takes the same arguments that ~dis.get_instructions{.interpreted-text role="func"} does (plus an optional current_offset), and the resulting object can be iterated to produce ~dis.Instruction{.interpreted-text role="class"} objects. But it also has a ~dis.Bytecode.dis{.interpreted-text role="mod"} method, equivalent to calling ~dis.dis{.interpreted-text role="mod"} on the constructor argument, but returned as a multi-line string:

>>> bytecode = dis.Bytecode(lambda x: x + 1, current_offset=3)
>>> for instr in bytecode:
...     print('{} ({})'.format(instr.opname, instr.opcode))
LOAD_FAST (124)
LOAD_CONST (100)
BINARY_ADD (23)
RETURN_VALUE (83)
>>> bytecode.dis().splitlines()       # doctest: +NORMALIZE_WHITESPACE
['  1           0 LOAD_FAST                0 (x)',
 '      -->     3 LOAD_CONST               1 (1)',
 '              6 BINARY_ADD',
 '              7 RETURN_VALUE']

~dis.Bytecode{.interpreted-text role="class"} also has a class method, ~dis.Bytecode.from_traceback{.interpreted-text role="meth"}, that provides the ability to manipulate a traceback (that is, print(Bytecode.from_traceback(tb).dis()) is equivalent to distb(tb)).

(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in 11816{.interpreted-text role="issue"} and Claudiu Popa in 17916{.interpreted-text role="issue"}.)

New function ~dis.stack_effect{.interpreted-text role="func"} computes the effect on the Python stack of a given opcode and argument, information that is not otherwise available. (Contributed by Larry Hastings in 19722{.interpreted-text role="issue"}.)

doctest

A new option flag <doctest-options>{.interpreted-text role="ref"}, ~doctest.FAIL_FAST{.interpreted-text role="const"}, halts test running as soon as the first failure is detected. (Contributed by R. David Murray and Daniel Urban in 16522{.interpreted-text role="issue"}.)

The doctest{.interpreted-text role="mod"} command line interface now uses argparse{.interpreted-text role="mod"}, and has two new options, -o and -f. -o allows doctest options <doctest-options>{.interpreted-text role="ref"} to be specified on the command line, and -f is a shorthand for -o FAIL_FAST (to parallel the similar option supported by the unittest{.interpreted-text role="mod"} CLI). (Contributed by R. David Murray in 11390{.interpreted-text role="issue"}.)

doctest{.interpreted-text role="mod"} will now find doctests in extension module __doc__ strings. (Contributed by Zachary Ware in 3158{.interpreted-text role="issue"}.)

email

~email.message.Message.as_string{.interpreted-text role="meth"} now accepts a policy argument to override the default policy of the message when generating a string representation of it. This means that as_string can now be used in more circumstances, instead of having to create and use a ~email.generator{.interpreted-text role="mod"} in order to pass formatting parameters to its flatten method. (Contributed by R. David Murray in 18600{.interpreted-text role="issue"}.)

New method ~email.message.Message.as_bytes{.interpreted-text role="meth"} added to produce a bytes representation of the message in a fashion similar to how as_string produces a string representation. It does not accept the maxheaderlen argument, but does accept the unixfrom and policy arguments. The ~email.message.Message{.interpreted-text role="class"} ~email.message.Message.__bytes__{.interpreted-text role="meth"} method calls it, meaning that bytes(mymsg) will now produce the intuitive result: a bytes object containing the fully formatted message. (Contributed by R. David Murray in 18600{.interpreted-text role="issue"}.)

The .Message.set_param{.interpreted-text role="meth"} message now accepts a replace keyword argument. When specified, the associated header will be updated without changing its location in the list of headers. For backward compatibility, the default is False. (Contributed by R. David Murray in 18891{.interpreted-text role="issue"}.)

::: {#whatsnew_email_contentmanager} A pair of new subclasses of ~email.message.Message{.interpreted-text role="class"} have been added (.EmailMessage{.interpreted-text role="class"} and .MIMEPart{.interpreted-text role="class"}), along with a new sub-module, ~email.contentmanager{.interpreted-text role="mod"} and a new ~email.policy{.interpreted-text role="mod"} attribute ~email.policy.EmailPolicy.content_manager{.interpreted-text role="attr"}. All documentation is currently in the new module, which is being added as part of email's new provisional API{.interpreted-text role="term"}. These classes provide a number of new methods that make extracting content from and inserting content into email messages much easier. For details, see the ~email.contentmanager{.interpreted-text role="mod"} documentation and the email-examples{.interpreted-text role="ref"}. These API additions complete the bulk of the work that was planned as part of the email6 project. The currently provisional API is scheduled to become final in Python 3.5 (possibly with a few minor additions in the area of error handling). (Contributed by R. David Murray in 18891{.interpreted-text role="issue"}.) :::

filecmp

A new ~filecmp.clear_cache{.interpreted-text role="func"} function provides the ability to clear the filecmp{.interpreted-text role="mod"} comparison cache, which uses os.stat{.interpreted-text role="func"} information to determine if the file has changed since the last compare. This can be used, for example, if the file might have been changed and re-checked in less time than the resolution of a particular filesystem's file modification time field. (Contributed by Mark Levitt in 18149{.interpreted-text role="issue"}.)

New module attribute ~filecmp.DEFAULT_IGNORES{.interpreted-text role="const"} provides the list of directories that are used as the default value for the ignore parameter of the ~filecmp.dircmp{.interpreted-text role="func"} function. (Contributed by Eli Bendersky in 15442{.interpreted-text role="issue"}.)

functools

The new ~functools.partialmethod{.interpreted-text role="func"} descriptor brings partial argument application to descriptors, just as ~functools.partial{.interpreted-text role="func"} provides for normal callables. The new descriptor also makes it easier to get arbitrary callables (including ~functools.partial{.interpreted-text role="func"} instances) to behave like normal instance methods when included in a class definition. (Contributed by Alon Horev and Nick Coghlan in 4331{.interpreted-text role="issue"}.)

::: {#whatsnew-singledispatch} The new ~functools.singledispatch{.interpreted-text role="func"} decorator brings support for single-dispatch generic functions to the Python standard library. Where object oriented programming focuses on grouping multiple operations on a common set of data into a class, a generic function focuses on grouping multiple implementations of an operation that allows it to work with different kinds of data. :::

::: seealso

443{.interpreted-text role="pep"} -- Single-dispatch generic functions

: PEP written and implemented by Łukasz Langa. :::

~functools.total_ordering{.interpreted-text role="func"} now supports a return value of NotImplemented{.interpreted-text role="data"} from the underlying comparison function. (Contributed by Katie Miller in 10042{.interpreted-text role="issue"}.)

A pure-python version of the ~functools.partial{.interpreted-text role="func"} function is now in the stdlib; in CPython it is overridden by the C accelerated version, but it is available for other implementations to use. (Contributed by Brian Thorne in 12428{.interpreted-text role="issue"}.)

gc

New function ~gc.get_stats{.interpreted-text role="func"} returns a list of three per-generation dictionaries containing the collections statistics since interpreter startup. (Contributed by Antoine Pitrou in 16351{.interpreted-text role="issue"}.)

glob

A new function ~glob.escape{.interpreted-text role="func"} provides a way to escape special characters in a filename so that they do not become part of the globbing expansion but are instead matched literally. (Contributed by Serhiy Storchaka in 8402{.interpreted-text role="issue"}.)

hashlib

A new hashlib.pbkdf2_hmac{.interpreted-text role="func"} function provides the PKCS#5 password-based key derivation function 2. (Contributed by Christian Heimes in 18582{.interpreted-text role="issue"}.)

The ~hashlib.hash.name{.interpreted-text role="attr"} attribute of hashlib{.interpreted-text role="mod"} hash objects is now a formally supported interface. It has always existed in CPython's hashlib{.interpreted-text role="mod"} (although it did not return lower case names for all supported hashes), but it was not a public interface and so some other Python implementations have not previously supported it. (Contributed by Jason R. Coombs in 18532{.interpreted-text role="issue"}.)

hmac

hmac{.interpreted-text role="mod"} now accepts bytearray as well as bytes for the key argument to the ~hmac.new{.interpreted-text role="func"} function, and the msg parameter to both the ~hmac.new{.interpreted-text role="func"} function and the ~hmac.HMAC.update{.interpreted-text role="meth"} method now accepts any type supported by the hashlib{.interpreted-text role="mod"} module. (Contributed by Jonas Borgström in 18240{.interpreted-text role="issue"}.)

The digestmod argument to the hmac.new{.interpreted-text role="func"} function may now be any hash digest name recognized by hashlib{.interpreted-text role="mod"}. In addition, the current behavior in which the value of digestmod defaults to MD5 is deprecated: in a future version of Python there will be no default value. (Contributed by Christian Heimes in 17276{.interpreted-text role="issue"}.)

With the addition of ~hmac.HMAC.block_size{.interpreted-text role="attr"} and ~hmac.HMAC.name{.interpreted-text role="attr"} attributes (and the formal documentation of the ~hmac.HMAC.digest_size{.interpreted-text role="attr"} attribute), the hmac{.interpreted-text role="mod"} module now conforms fully to the 247{.interpreted-text role="pep"} API. (Contributed by Christian Heimes in 18775{.interpreted-text role="issue"}.)

html

New function ~html.unescape{.interpreted-text role="func"} function converts HTML5 character references to the corresponding Unicode characters. (Contributed by Ezio Melotti in 2927{.interpreted-text role="issue"}.)

~html.parser.HTMLParser{.interpreted-text role="class"} accepts a new keyword argument convert_charrefs that, when True, automatically converts all character references. For backward-compatibility, its value defaults to False, but it will change to True in a future version of Python, so you are invited to set it explicitly and update your code to use this new feature. (Contributed by Ezio Melotti in 13633{.interpreted-text role="issue"}.)

The strict argument of ~html.parser.HTMLParser{.interpreted-text role="class"} is now deprecated. (Contributed by Ezio Melotti in 15114{.interpreted-text role="issue"}.)

http

~http.server.BaseHTTPRequestHandler.send_error{.interpreted-text role="meth"} now accepts an optional additional explain parameter which can be used to provide an extended error description, overriding the hardcoded default if there is one. This extended error description will be formatted using the ~http.server.BaseHTTPRequestHandler.error_message_format{.interpreted-text role="attr"} attribute and sent as the body of the error response. (Contributed by Karl Cow in 12921{.interpreted-text role="issue"}.)

The http.server{.interpreted-text role="mod"} command line interface <http-server-cli>{.interpreted-text role="ref"} now has a -b/--bind option that causes the server to listen on a specific address. (Contributed by Malte Swart in 17764{.interpreted-text role="issue"}.)

idlelib and IDLE

Since idlelib implements the IDLE shell and editor and is not intended for import by other programs, it gets improvements with every release. See Lib/idlelib/NEWS.txt{.interpreted-text role="file"} for a cumulative list of changes since 3.3.0, as well as changes made in future 3.4.x releases. This file is also available from the IDLE Help --> About IDLE{.interpreted-text role="menuselection"} dialog.

importlib

The ~importlib.abc.InspectLoader{.interpreted-text role="class"} ABC defines a new method, ~importlib.abc.InspectLoader.source_to_code{.interpreted-text role="meth"} that accepts source data and a path and returns a code object. The default implementation is equivalent to compile(data, path, 'exec', dont_inherit=True). (Contributed by Eric Snow and Brett Cannon in 15627{.interpreted-text role="issue"}.)

~importlib.abc.InspectLoader{.interpreted-text role="class"} also now has a default implementation for the ~importlib.abc.InspectLoader.get_code{.interpreted-text role="meth"} method. However, it will normally be desirable to override the default implementation for performance reasons. (Contributed by Brett Cannon in 18072{.interpreted-text role="issue"}.)

The ~importlib.reload{.interpreted-text role="func"} function has been moved from !imp{.interpreted-text role="mod"} to importlib{.interpreted-text role="mod"} as part of the !imp{.interpreted-text role="mod"} module deprecation. (Contributed by Berker Peksag in 18193{.interpreted-text role="issue"}.)

importlib.util{.interpreted-text role="mod"} now has a ~importlib.util.MAGIC_NUMBER{.interpreted-text role="const"} attribute providing access to the bytecode version number. This replaces the !get_magic{.interpreted-text role="func"} function in the deprecated !imp{.interpreted-text role="mod"} module. (Contributed by Brett Cannon in 18192{.interpreted-text role="issue"}.)

New importlib.util{.interpreted-text role="mod"} functions ~importlib.util.cache_from_source{.interpreted-text role="func"} and ~importlib.util.source_from_cache{.interpreted-text role="func"} replace the same-named functions in the deprecated !imp{.interpreted-text role="mod"} module. (Contributed by Brett Cannon in 18194{.interpreted-text role="issue"}.)

The importlib{.interpreted-text role="mod"} bootstrap .NamespaceLoader{.interpreted-text role="class"} now conforms to the .InspectLoader{.interpreted-text role="class"} ABC, which means that runpy and python -m can now be used with namespace packages. (Contributed by Brett Cannon in 18058{.interpreted-text role="issue"}.)

importlib.util{.interpreted-text role="mod"} has a new function ~importlib.util.decode_source{.interpreted-text role="func"} that decodes source from bytes using universal newline processing. This is useful for implementing .InspectLoader.get_source{.interpreted-text role="meth"} methods.

importlib.machinery.ExtensionFileLoader{.interpreted-text role="class"} now has a ~importlib.machinery.ExtensionFileLoader.get_filename{.interpreted-text role="meth"} method. This was inadvertently omitted in the original implementation. (Contributed by Eric Snow in 19152{.interpreted-text role="issue"}.)

inspect

The inspect{.interpreted-text role="mod"} module now offers a basic command line interface <inspect-module-cli>{.interpreted-text role="ref"} to quickly display source code and other information for modules, classes and functions. (Contributed by Claudiu Popa and Nick Coghlan in 18626{.interpreted-text role="issue"}.)

~inspect.unwrap{.interpreted-text role="func"} makes it easy to unravel wrapper function chains created by functools.wraps{.interpreted-text role="func"} (and any other API that sets the __wrapped__ attribute on a wrapper function). (Contributed by Daniel Urban, Aaron Iles and Nick Coghlan in 13266{.interpreted-text role="issue"}.)

As part of the implementation of the new enum{.interpreted-text role="mod"} module, the inspect{.interpreted-text role="mod"} module now has substantially better support for custom __dir__ methods and dynamic class attributes provided through metaclasses. (Contributed by Ethan Furman in 18929{.interpreted-text role="issue"} and 19030{.interpreted-text role="issue"}.)

~inspect.getfullargspec{.interpreted-text role="func"} and !getargspec{.interpreted-text role="func"} now use the ~inspect.signature{.interpreted-text role="func"} API. This allows them to support a much broader range of callables, including those with __signature__ attributes, those with metadata provided by argument clinic, functools.partial{.interpreted-text role="func"} objects and more. Note that, unlike ~inspect.signature{.interpreted-text role="func"}, these functions still ignore __wrapped__ attributes, and report the already bound first argument for bound methods, so it is still necessary to update your code to use ~inspect.signature{.interpreted-text role="func"} directly if those features are desired. (Contributed by Yury Selivanov in 17481{.interpreted-text role="issue"}.)

~inspect.signature{.interpreted-text role="func"} now supports duck types of CPython functions, which adds support for functions compiled with Cython. (Contributed by Stefan Behnel and Yury Selivanov in 17159{.interpreted-text role="issue"}.)

ipaddress

ipaddress{.interpreted-text role="mod"} was added to the standard library in Python 3.3 as a provisional API{.interpreted-text role="term"}. With the release of Python 3.4, this qualification has been removed: ipaddress{.interpreted-text role="mod"} is now considered a stable API, covered by the normal standard library requirements to maintain backwards compatibility.

A new ~ipaddress.IPv4Address.is_global{.interpreted-text role="attr"} property is True if an address is globally routeable. (Contributed by Peter Moody in 17400{.interpreted-text role="issue"}.)

logging

The ~logging.handlers.TimedRotatingFileHandler{.interpreted-text role="class"} has a new atTime parameter that can be used to specify the time of day when rollover should happen. (Contributed by Ronald Oussoren in 9556{.interpreted-text role="issue"}.)

~logging.handlers.SocketHandler{.interpreted-text role="class"} and ~logging.handlers.DatagramHandler{.interpreted-text role="class"} now support Unix domain sockets (by setting port to None). (Contributed by Vinay Sajip in commit ce46195b56a9.)

~logging.config.fileConfig{.interpreted-text role="func"} now accepts a configparser.RawConfigParser{.interpreted-text role="class"} subclass instance for the fname parameter. This facilitates using a configuration file when logging configuration is just a part of the overall application configuration, or where the application modifies the configuration before passing it to ~logging.config.fileConfig{.interpreted-text role="func"}. (Contributed by Vinay Sajip in 16110{.interpreted-text role="issue"}.)

Logging configuration data received from a socket via the logging.config.listen{.interpreted-text role="func"} function can now be validated before being processed by supplying a verification function as the argument to the new verify keyword argument. (Contributed by Vinay Sajip in 15452{.interpreted-text role="issue"}.)

marshal {#whatsnew-marshal-3}

The default marshal{.interpreted-text role="mod"} version has been bumped to 3. The code implementing the new version restores the Python2 behavior of recording only one copy of interned strings and preserving the interning on deserialization, and extends this "one copy" ability to any object type (including handling recursive references). This reduces both the size of .pyc files and the amount of memory a module occupies in memory when it is loaded from a .pyc (or .pyo) file. (Contributed by Kristján Valur Jónsson in 16475{.interpreted-text role="issue"}, with additional speedups by Antoine Pitrou in 19219{.interpreted-text role="issue"}.)

mmap

mmap objects are now weakly referenceable <mod-weakref>{.interpreted-text role="ref"}. (Contributed by Valerie Lambert in 4885{.interpreted-text role="issue"}.)

multiprocessing

::: {#whatsnew-multiprocessing-no-fork} On Unix two new start methods <multiprocessing-start-methods>{.interpreted-text role="ref"}, spawn and forkserver, have been added for starting processes using multiprocessing{.interpreted-text role="mod"}. These make the mixing of processes with threads more robust, and the spawn method matches the semantics that multiprocessing has always used on Windows. New function ~multiprocessing.get_all_start_methods{.interpreted-text role="func"} reports all start methods available on the platform, ~multiprocessing.get_start_method{.interpreted-text role="func"} reports the current start method, and ~multiprocessing.set_start_method{.interpreted-text role="func"} sets the start method. (Contributed by Richard Oudkerk in 8713{.interpreted-text role="issue"}.) :::

multiprocessing{.interpreted-text role="mod"} also now has the concept of a context, which determines how child processes are created. New function ~multiprocessing.get_context{.interpreted-text role="func"} returns a context that uses a specified start method. It has the same API as the multiprocessing{.interpreted-text role="mod"} module itself, so you can use it to create ~multiprocessing.pool.Pool{.interpreted-text role="class"}s and other objects that will operate within that context. This allows a framework and an application or different parts of the same application to use multiprocessing without interfering with each other. (Contributed by Richard Oudkerk in 18999{.interpreted-text role="issue"}.)

Except when using the old fork start method, child processes no longer inherit unneeded handles/file descriptors from their parents (part of 8713{.interpreted-text role="issue"}).

multiprocessing{.interpreted-text role="mod"} now relies on runpy{.interpreted-text role="mod"} (which implements the -m switch) to initialise __main__ appropriately in child processes when using the spawn or forkserver start methods. This resolves some edge cases where combining multiprocessing, the -m command line switch, and explicit relative imports could cause obscure failures in child processes. (Contributed by Nick Coghlan in 19946{.interpreted-text role="issue"}.)

operator

New function ~operator.length_hint{.interpreted-text role="func"} provides an implementation of the specification for how the ~object.__length_hint__{.interpreted-text role="meth"} special method should be used, as part of the 424{.interpreted-text role="pep"} formal specification of this language feature. (Contributed by Armin Ronacher in 16148{.interpreted-text role="issue"}.)

There is now a pure-python version of the operator{.interpreted-text role="mod"} module available for reference and for use by alternate implementations of Python. (Contributed by Zachary Ware in 16694{.interpreted-text role="issue"}.)

os

There are new functions to get and set the inheritable flag <fd_inheritance>{.interpreted-text role="ref"} of a file descriptor (os.get_inheritable{.interpreted-text role="func"}, os.set_inheritable{.interpreted-text role="func"}) or a Windows handle (os.get_handle_inheritable{.interpreted-text role="func"}, os.set_handle_inheritable{.interpreted-text role="func"}).

New function ~os.cpu_count{.interpreted-text role="func"} reports the number of CPUs available on the platform on which Python is running (or None if the count can't be determined). The multiprocessing.cpu_count{.interpreted-text role="func"} function is now implemented in terms of this function). (Contributed by Trent Nelson, Yogesh Chaudhari, Victor Stinner, and Charles-François Natali in 17914{.interpreted-text role="issue"}.)

os.path.samestat{.interpreted-text role="func"} is now available on the Windows platform (and the os.path.samefile{.interpreted-text role="func"} implementation is now shared between Unix and Windows). (Contributed by Brian Curtin in 11939{.interpreted-text role="issue"}.)

os.path.ismount{.interpreted-text role="func"} now recognizes volumes mounted below a drive root on Windows. (Contributed by Tim Golden in 9035{.interpreted-text role="issue"}.)

os.open{.interpreted-text role="func"} supports two new flags on platforms that provide them, ~os.O_PATH{.interpreted-text role="const"} (un-opened file descriptor), and ~os.O_TMPFILE{.interpreted-text role="const"} (unnamed temporary file; as of 3.4.0 release available only on Linux systems with a kernel version of 3.11 or newer that have uapi headers). (Contributed by Christian Heimes in 18673{.interpreted-text role="issue"} and Benjamin Peterson, respectively.)

pdb

pdb{.interpreted-text role="mod"} has been enhanced to handle generators, yield{.interpreted-text role="keyword"}, and yield from in a more useful fashion. This is especially helpful when debugging asyncio{.interpreted-text role="mod"} based programs. (Contributed by Andrew Svetlov and Xavier de Gaye in 16596{.interpreted-text role="issue"}.)

The print command has been removed from pdb{.interpreted-text role="mod"}, restoring access to the Python print{.interpreted-text role="func"} function from the pdb command line. Python2's pdb did not have a print command; instead, entering print executed the print statement. In Python3 print was mistakenly made an alias for the pdb p{.interpreted-text role="pdbcmd"} command. p, however, prints the repr of its argument, not the str like the Python2 print command did. Worse, the Python3 pdb print command shadowed the Python3 print function, making it inaccessible at the pdb prompt. (Contributed by Connor Osborn in 18764{.interpreted-text role="issue"}.)

pickle {#whatsnew-protocol-4}

pickle{.interpreted-text role="mod"} now supports (but does not use by default) a new pickle protocol, protocol 4. This new protocol addresses a number of issues that were present in previous protocols, such as the serialization of nested classes, very large strings and containers, and classes whose ~object.__new__{.interpreted-text role="meth"} method takes keyword-only arguments. It also provides some efficiency improvements.

::: seealso

3154{.interpreted-text role="pep"} -- Pickle protocol 4

: PEP written by Antoine Pitrou and implemented by Alexandre Vassalotti. :::

plistlib

plistlib{.interpreted-text role="mod"} now has an API that is similar to the standard pattern for stdlib serialization protocols, with new ~plistlib.load{.interpreted-text role="func"}, ~plistlib.dump{.interpreted-text role="func"}, ~plistlib.loads{.interpreted-text role="func"}, and ~plistlib.dumps{.interpreted-text role="func"} functions. (The older API is now deprecated.) In addition to the already supported XML plist format (~plistlib.FMT_XML{.interpreted-text role="const"}), it also now supports the binary plist format (~plistlib.FMT_BINARY{.interpreted-text role="const"}). (Contributed by Ronald Oussoren and others in 14455{.interpreted-text role="issue"}.)

poplib

Two new methods have been added to poplib{.interpreted-text role="mod"}: ~poplib.POP3.capa{.interpreted-text role="meth"}, which returns the list of capabilities advertised by the POP server, and ~poplib.POP3.stls{.interpreted-text role="meth"}, which switches a clear-text POP3 session into an encrypted POP3 session if the POP server supports it. (Contributed by Lorenzo Catucci in 4473{.interpreted-text role="issue"}.)

pprint

The pprint{.interpreted-text role="mod"} module's ~pprint.PrettyPrinter{.interpreted-text role="class"} class and its ~pprint.pformat{.interpreted-text role="func"}, and ~pprint.pprint{.interpreted-text role="func"} functions have a new option, compact, that controls how the output is formatted. Currently setting compact to True means that sequences will be printed with as many sequence elements as will fit within width on each (indented) line. (Contributed by Serhiy Storchaka in 19132{.interpreted-text role="issue"}.)

Long strings are now wrapped using Python's normal line continuation syntax. (Contributed by Antoine Pitrou in 17150{.interpreted-text role="issue"}.)

pty

pty.spawn{.interpreted-text role="func"} now returns the status value from os.waitpid{.interpreted-text role="func"} on the child process, instead of None. (Contributed by Gregory P. Smith.)

pydoc

The pydoc{.interpreted-text role="mod"} module is now based directly on the inspect.signature{.interpreted-text role="func"} introspection API, allowing it to provide signature information for a wider variety of callable objects. This change also means that __wrapped__ attributes are now taken into account when displaying help information. (Contributed by Larry Hastings in 19674{.interpreted-text role="issue"}.)

The pydoc{.interpreted-text role="mod"} module no longer displays the self parameter for already bound methods. Instead, it aims to always display the exact current signature of the supplied callable. (Contributed by Larry Hastings in 20710{.interpreted-text role="issue"}.)

In addition to the changes that have been made to pydoc{.interpreted-text role="mod"} directly, its handling of custom __dir__ methods and various descriptor behaviours has also been improved substantially by the underlying changes in the inspect{.interpreted-text role="mod"} module.

As the help{.interpreted-text role="func"} builtin is based on pydoc{.interpreted-text role="mod"}, the above changes also affect the behaviour of help{.interpreted-text role="func"}.

re

New ~re.fullmatch{.interpreted-text role="func"} function and .Pattern.fullmatch{.interpreted-text role="meth"} method anchor the pattern at both ends of the string to match. This provides a way to be explicit about the goal of the match, which avoids a class of subtle bugs where $ characters get lost during code changes or the addition of alternatives to an existing regular expression. (Contributed by Matthew Barnett in 16203{.interpreted-text role="issue"}.)

The repr of regex objects <re-objects>{.interpreted-text role="ref"} now includes the pattern and the flags; the repr of match objects <match-objects>{.interpreted-text role="ref"} now includes the start, end, and the part of the string that matched. (Contributed by Hugo Lopes Tavares and Serhiy Storchaka in 13592{.interpreted-text role="issue"} and 17087{.interpreted-text role="issue"}.)

resource

New ~resource.prlimit{.interpreted-text role="func"} function, available on Linux platforms with a kernel version of 2.6.36 or later and glibc of 2.13 or later, provides the ability to query or set the resource limits for processes other than the one making the call. (Contributed by Christian Heimes in 16595{.interpreted-text role="issue"}.)

On Linux kernel version 2.6.36 or later, there are also some new Linux specific constants: ~resource.RLIMIT_MSGQUEUE{.interpreted-text role="const"}, ~resource.RLIMIT_NICE{.interpreted-text role="const"}, ~resource.RLIMIT_RTPRIO{.interpreted-text role="const"}, ~resource.RLIMIT_RTTIME{.interpreted-text role="const"}, and ~resource.RLIMIT_SIGPENDING{.interpreted-text role="const"}. (Contributed by Christian Heimes in 19324{.interpreted-text role="issue"}.)

On FreeBSD version 9 and later, there some new FreeBSD specific constants: ~resource.RLIMIT_SBSIZE{.interpreted-text role="const"}, ~resource.RLIMIT_SWAP{.interpreted-text role="const"}, and ~resource.RLIMIT_NPTS{.interpreted-text role="const"}. (Contributed by Claudiu Popa in 19343{.interpreted-text role="issue"}.)

select

~select.epoll{.interpreted-text role="class"} objects now support the context management protocol. When used in a with{.interpreted-text role="keyword"} statement, the ~select.epoll.close{.interpreted-text role="meth"} method will be called automatically at the end of the block. (Contributed by Serhiy Storchaka in 16488{.interpreted-text role="issue"}.)

~select.devpoll{.interpreted-text role="class"} objects now have ~select.devpoll.fileno{.interpreted-text role="meth"} and ~select.devpoll.close{.interpreted-text role="meth"} methods, as well as a new attribute ~select.devpoll.closed{.interpreted-text role="attr"}. (Contributed by Victor Stinner in 18794{.interpreted-text role="issue"}.)

shelve

~shelve.Shelf{.interpreted-text role="class"} instances may now be used in with{.interpreted-text role="keyword"} statements, and will be automatically closed at the end of the !with{.interpreted-text role="keyword"} block. (Contributed by Filip Gruszczyński in 13896{.interpreted-text role="issue"}.)

shutil

~shutil.copyfile{.interpreted-text role="func"} now raises a specific ~shutil.Error{.interpreted-text role="exc"} subclass, ~shutil.SameFileError{.interpreted-text role="exc"}, when the source and destination are the same file, which allows an application to take appropriate action on this specific error. (Contributed by Atsuo Ishimoto and Hynek Schlawack in 1492704{.interpreted-text role="issue"}.)

smtpd

The !SMTPServer{.interpreted-text role="class"} and !SMTPChannel{.interpreted-text role="class"} classes now accept a map keyword argument which, if specified, is passed in to !asynchat.async_chat{.interpreted-text role="class"} as its map argument. This allows an application to avoid affecting the global socket map. (Contributed by Vinay Sajip in 11959{.interpreted-text role="issue"}.)

smtplib

~smtplib.SMTPException{.interpreted-text role="exc"} is now a subclass of OSError{.interpreted-text role="exc"}, which allows both socket level errors and SMTP protocol level errors to be caught in one try/except statement by code that only cares whether or not an error occurred. (Contributed by Ned Jackson Lovely in 2118{.interpreted-text role="issue"}.)

socket

The socket module now supports the ~socket.CAN_BCM{.interpreted-text role="const"} protocol on platforms that support it. (Contributed by Brian Thorne in 15359{.interpreted-text role="issue"}.)

Socket objects have new methods to get or set their inheritable flag <fd_inheritance>{.interpreted-text role="ref"}, ~socket.socket.get_inheritable{.interpreted-text role="meth"} and ~socket.socket.set_inheritable{.interpreted-text role="meth"}.

The socket.AF_* and socket.SOCK_* constants are now enumeration values using the new enum{.interpreted-text role="mod"} module. This allows meaningful names to be printed during debugging, instead of integer "magic numbers".

The ~socket.AF_LINK{.interpreted-text role="const"} constant is now available on BSD and OSX.

~socket.inet_pton{.interpreted-text role="func"} and ~socket.inet_ntop{.interpreted-text role="func"} are now supported on Windows. (Contributed by Atsuo Ishimoto in 7171{.interpreted-text role="issue"}.)

sqlite3

A new boolean parameter to the ~sqlite3.connect{.interpreted-text role="func"} function, uri, can be used to indicate that the database parameter is a uri (see the SQLite URI documentation). (Contributed by poq in 13773{.interpreted-text role="issue"}.)

ssl

::: {#whatsnew-tls-11-12} ~ssl.PROTOCOL_TLSv1_1{.interpreted-text role="data"} and ~ssl.PROTOCOL_TLSv1_2{.interpreted-text role="data"} (TLSv1.1 and TLSv1.2 support) have been added; support for these protocols is only available if Python is linked with OpenSSL 1.0.1 or later. (Contributed by Michele Orrù and Antoine Pitrou in 16692{.interpreted-text role="issue"}.) :::

::: {#whatsnew34-sslcontext} New function ~ssl.create_default_context{.interpreted-text role="func"} provides a standard way to obtain an ~ssl.SSLContext{.interpreted-text role="class"} whose settings are intended to be a reasonable balance between compatibility and security. These settings are more stringent than the defaults provided by the ~ssl.SSLContext{.interpreted-text role="class"} constructor, and may be adjusted in the future, without prior deprecation, if best-practice security requirements change. The new recommended best practice for using stdlib libraries that support SSL is to use ~ssl.create_default_context{.interpreted-text role="func"} to obtain an ~ssl.SSLContext{.interpreted-text role="class"} object, modify it if needed, and then pass it as the context argument of the appropriate stdlib API. (Contributed by Christian Heimes in 19689{.interpreted-text role="issue"}.) :::

~ssl.SSLContext{.interpreted-text role="class"} method ~ssl.SSLContext.load_verify_locations{.interpreted-text role="meth"} accepts a new optional argument cadata, which can be used to provide PEM or DER encoded certificates directly via strings or bytes, respectively. (Contributed by Christian Heimes in 18138{.interpreted-text role="issue"}.)

New function ~ssl.get_default_verify_paths{.interpreted-text role="func"} returns a named tuple of the paths and environment variables that the ~ssl.SSLContext.set_default_verify_paths{.interpreted-text role="meth"} method uses to set OpenSSL's default cafile and capath. This can be an aid in debugging default verification issues. (Contributed by Christian Heimes in 18143{.interpreted-text role="issue"}.)

~ssl.SSLContext{.interpreted-text role="class"} has a new method, ~ssl.SSLContext.cert_store_stats{.interpreted-text role="meth"}, that reports the number of loaded X.509 certs, X.509 CA certs, and certificate revocation lists (crls), as well as a ~ssl.SSLContext.get_ca_certs{.interpreted-text role="meth"} method that returns a list of the loaded CA certificates. (Contributed by Christian Heimes in 18147{.interpreted-text role="issue"}.)

If OpenSSL 0.9.8 or later is available, ~ssl.SSLContext{.interpreted-text role="class"} has a new attribute ~ssl.SSLContext.verify_flags{.interpreted-text role="attr"} that can be used to control the certificate verification process by setting it to some combination of the new constants ~ssl.VERIFY_DEFAULT{.interpreted-text role="const"}, ~ssl.VERIFY_CRL_CHECK_LEAF{.interpreted-text role="const"}, ~ssl.VERIFY_CRL_CHECK_CHAIN{.interpreted-text role="const"}, or ~ssl.VERIFY_X509_STRICT{.interpreted-text role="const"}. OpenSSL does not do any CRL verification by default. (Contributed by Christien Heimes in 8813{.interpreted-text role="issue"}.)

New ~ssl.SSLContext{.interpreted-text role="class"} method ~ssl.SSLContext.load_default_certs{.interpreted-text role="meth"} loads a set of default "certificate authority" (CA) certificates from default locations, which vary according to the platform. It can be used to load both TLS web server authentication certificates (purpose=``~ssl.Purpose.SERVER_AUTH{.interpreted-text role="data"}) for a client to use to verify a server, and certificates for a server to use in verifying client certificates (purpose=``~ssl.Purpose.CLIENT_AUTH{.interpreted-text role="data"}). (Contributed by Christian Heimes in 19292{.interpreted-text role="issue"}.)

::: {#whatsnew34-win-cert-store} Two new windows-only functions, ~ssl.enum_certificates{.interpreted-text role="func"} and ~ssl.enum_crls{.interpreted-text role="func"} provide the ability to retrieve certificates, certificate information, and CRLs from the Windows cert store. (Contributed by Christian Heimes in 17134{.interpreted-text role="issue"}.) :::

::: {#whatsnew34-sni} Support for server-side SNI (Server Name Indication) using the new ssl.SSLContext.set_servername_callback{.interpreted-text role="meth"} method. (Contributed by Daniel Black in 8109{.interpreted-text role="issue"}.) :::

The dictionary returned by .SSLSocket.getpeercert{.interpreted-text role="meth"} contains additional X509v3 extension items: crlDistributionPoints, calIssuers, and OCSP URIs. (Contributed by Christian Heimes in 18379{.interpreted-text role="issue"}.)

stat

The stat{.interpreted-text role="mod"} module is now backed by a C implementation in !_stat{.interpreted-text role="mod"}. A C implementation is required as most of the values aren't standardized and are platform-dependent. (Contributed by Christian Heimes in 11016{.interpreted-text role="issue"}.)

The module supports new ~stat.ST_MODE{.interpreted-text role="mod"} flags, ~stat.S_IFDOOR{.interpreted-text role="mod"}, ~stat.S_IFPORT{.interpreted-text role="const"}, and ~stat.S_IFWHT{.interpreted-text role="const"}. (Contributed by Christian Hiemes in 11016{.interpreted-text role="issue"}.)

struct

New function ~struct.iter_unpack{.interpreted-text role="mod"} and a new struct.Struct.iter_unpack{.interpreted-text role="meth"} method on compiled formats provide streamed unpacking of a buffer containing repeated instances of a given format of data. (Contributed by Antoine Pitrou in 17804{.interpreted-text role="issue"}.)

subprocess

~subprocess.check_output{.interpreted-text role="func"} now accepts an input argument that can be used to provide the contents of stdin for the command that is run. (Contributed by Zack Weinberg in 16624{.interpreted-text role="issue"}.)

~subprocess.getoutput{.interpreted-text role="func"} and ~subprocess.getstatusoutput{.interpreted-text role="func"} now work on Windows. This change was actually inadvertently made in 3.3.4. (Contributed by Tim Golden in 10197{.interpreted-text role="issue"}.)

sunau

The !getparams{.interpreted-text role="meth"} method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in 18901{.interpreted-text role="issue"}.)

!sunau.open{.interpreted-text role="meth"} now supports the context management protocol: when used in a with{.interpreted-text role="keyword"} block, the close method of the returned object will be called automatically at the end of the block. (Contributed by Serhiy Storchaka in 18878{.interpreted-text role="issue"}.)

!AU_write.setsampwidth{.interpreted-text role="meth"} now supports 24 bit samples, thus adding support for writing 24 sample using the module. (Contributed by Serhiy Storchaka in 19261{.interpreted-text role="issue"}.)

The !writeframesraw{.interpreted-text role="meth"} and !writeframes{.interpreted-text role="meth"} methods now accept any bytes-like object{.interpreted-text role="term"}. (Contributed by Serhiy Storchaka in 8311{.interpreted-text role="issue"}.)

sys

New function sys.getallocatedblocks{.interpreted-text role="func"} returns the current number of blocks allocated by the interpreter. (In CPython with the default --with-pymalloc setting, this is allocations made through the PyObject_Malloc{.interpreted-text role="c:func"} API.) This can be useful for tracking memory leaks, especially if automated via a test suite. (Contributed by Antoine Pitrou in 13390{.interpreted-text role="issue"}.)

When the Python interpreter starts in interactive mode <tut-interactive>{.interpreted-text role="ref"}, it checks for an ~sys.__interactivehook__{.interpreted-text role="data"} attribute on the sys{.interpreted-text role="mod"} module. If the attribute exists, its value is called with no arguments just before interactive mode is started. The check is made after the PYTHONSTARTUP{.interpreted-text role="envvar"} file is read, so it can be set there. The site{.interpreted-text role="mod"} module sets it <rlcompleter-config>{.interpreted-text role="ref"} to a function that enables tab completion and history saving (in ~/.python-history{.interpreted-text role="file"}) if the platform supports readline{.interpreted-text role="mod"}. If you do not want this (new) behavior, you can override it in PYTHONSTARTUP{.interpreted-text role="envvar"}, sitecustomize{.interpreted-text role="mod"}, or usercustomize{.interpreted-text role="mod"} by deleting this attribute from sys{.interpreted-text role="mod"} (or setting it to some other callable). (Contributed by Éric Araujo and Antoine Pitrou in 5845{.interpreted-text role="issue"}.)

tarfile

The tarfile{.interpreted-text role="mod"} module now supports a simple tarfile-commandline{.interpreted-text role="ref"} when called as a script directly or via -m. This can be used to create and extract tarfile archives. (Contributed by Berker Peksag in 13477{.interpreted-text role="issue"}.)

textwrap

The ~textwrap.TextWrapper{.interpreted-text role="class"} class has two new attributes/constructor arguments: ~textwrap.TextWrapper.max_lines{.interpreted-text role="attr"}, which limits the number of lines in the output, and ~textwrap.TextWrapper.placeholder{.interpreted-text role="attr"}, which is a string that will appear at the end of the output if it has been truncated because of max_lines. Building on these capabilities, a new convenience function ~textwrap.shorten{.interpreted-text role="func"} collapses all of the whitespace in the input to single spaces and produces a single line of a given width that ends with the placeholder (by default, [...]). (Contributed by Antoine Pitrou and Serhiy Storchaka in 18585{.interpreted-text role="issue"} and 18725{.interpreted-text role="issue"}.)

threading

The ~threading.Thread{.interpreted-text role="class"} object representing the main thread can be obtained from the new ~threading.main_thread{.interpreted-text role="func"} function. In normal conditions this will be the thread from which the Python interpreter was started. (Contributed by Andrew Svetlov in 18882{.interpreted-text role="issue"}.)

traceback

A new traceback.clear_frames{.interpreted-text role="func"} function takes a traceback object and clears the local variables in all of the frames it references, reducing the amount of memory consumed. (Contributed by Andrew Kuchling in 1565525{.interpreted-text role="issue"}.)

types

A new ~types.DynamicClassAttribute{.interpreted-text role="func"} descriptor provides a way to define an attribute that acts normally when looked up through an instance object, but which is routed to the class __getattr__ when looked up through the class. This allows one to have properties active on a class, and have virtual attributes on the class with the same name (see enum{.interpreted-text role="mod"} for an example). (Contributed by Ethan Furman in 19030{.interpreted-text role="issue"}.)

urllib

urllib.request{.interpreted-text role="mod"} now supports data: URLs via the ~urllib.request.DataHandler{.interpreted-text role="class"} class. (Contributed by Mathias Panzenböck in 16423{.interpreted-text role="issue"}.)

The http method that will be used by a ~urllib.request.Request{.interpreted-text role="class"} class can now be specified by setting a ~urllib.request.Request.method{.interpreted-text role="class"} class attribute on the subclass. (Contributed by Jason R Coombs in 18978{.interpreted-text role="issue"}.)

~urllib.request.Request{.interpreted-text role="class"} objects are now reusable: if the ~urllib.request.Request.full_url{.interpreted-text role="attr"} or ~urllib.request.Request.data{.interpreted-text role="attr"} attributes are modified, all relevant internal properties are updated. This means, for example, that it is now possible to use the same ~urllib.request.Request{.interpreted-text role="class"} object in more than one .OpenerDirector.open{.interpreted-text role="meth"} call with different data arguments, or to modify a ~urllib.request.Request{.interpreted-text role="class"}'s url rather than recomputing it from scratch. There is also a new ~urllib.request.Request.remove_header{.interpreted-text role="meth"} method that can be used to remove headers from a ~urllib.request.Request{.interpreted-text role="class"}. (Contributed by Alexey Kachayev in 16464{.interpreted-text role="issue"}, Daniel Wozniak in 17485{.interpreted-text role="issue"}, and Damien Brecht and Senthil Kumaran in 17272{.interpreted-text role="issue"}.)

~urllib.error.HTTPError{.interpreted-text role="class"} objects now have a ~urllib.error.HTTPError.headers{.interpreted-text role="attr"} attribute that provides access to the HTTP response headers associated with the error. (Contributed by Berker Peksag in 15701{.interpreted-text role="issue"}.)

unittest

The ~unittest.TestCase{.interpreted-text role="class"} class has a new method, ~unittest.TestCase.subTest{.interpreted-text role="meth"}, that produces a context manager whose with{.interpreted-text role="keyword"} block becomes a "sub-test". This context manager allows a test method to dynamically generate subtests by, say, calling the subTest context manager inside a loop. A single test method can thereby produce an indefinite number of separately identified and separately counted tests, all of which will run even if one or more of them fail. For example:

class NumbersTest(unittest.TestCase):
    def test_even(self):
        for i in range(6):
            with self.subTest(i=i):
                self.assertEqual(i % 2, 0)

will result in six subtests, each identified in the unittest verbose output with a label consisting of the variable name i and a particular value for that variable (i=0, i=1, etc). See subtests{.interpreted-text role="ref"} for the full version of this example. (Contributed by Antoine Pitrou in 16997{.interpreted-text role="issue"}.)

unittest.main{.interpreted-text role="func"} now accepts an iterable of test names for defaultTest, where previously it only accepted a single test name as a string. (Contributed by Jyrki Pulliainen in 15132{.interpreted-text role="issue"}.)

If ~unittest.SkipTest{.interpreted-text role="class"} is raised during test discovery (that is, at the module level in the test file), it is now reported as a skip instead of an error. (Contributed by Zach Ware in 16935{.interpreted-text role="issue"}.)

~unittest.TestLoader.discover{.interpreted-text role="meth"} now sorts the discovered files to provide consistent test ordering. (Contributed by Martin Melin and Jeff Ramnani in 16709{.interpreted-text role="issue"}.)

~unittest.TestSuite{.interpreted-text role="class"} now drops references to tests as soon as the test has been run, if the test is successful. On Python interpreters that do garbage collection, this allows the tests to be garbage collected if nothing else is holding a reference to the test. It is possible to override this behavior by creating a ~unittest.TestSuite{.interpreted-text role="class"} subclass that defines a custom _removeTestAtIndex method. (Contributed by Tom Wardill, Matt McClure, and Andrew Svetlov in 11798{.interpreted-text role="issue"}.)

A new test assertion context-manager, ~unittest.TestCase.assertLogs{.interpreted-text role="meth"}, will ensure that a given block of code emits a log message using the logging{.interpreted-text role="mod"} module. By default the message can come from any logger and have a priority of INFO or higher, but both the logger name and an alternative minimum logging level may be specified. The object returned by the context manager can be queried for the ~logging.LogRecord{.interpreted-text role="class"}s and/or formatted messages that were logged. (Contributed by Antoine Pitrou in 18937{.interpreted-text role="issue"}.)

Test discovery now works with namespace packages (Contributed by Claudiu Popa in 17457{.interpreted-text role="issue"}.)

unittest.mock{.interpreted-text role="mod"} objects now inspect their specification signatures when matching calls, which means an argument can now be matched by either position or name, instead of only by position. (Contributed by Antoine Pitrou in 17015{.interpreted-text role="issue"}.)

~unittest.mock.mock_open{.interpreted-text role="func"} objects now have readline and readlines methods. (Contributed by Toshio Kuratomi in 17467{.interpreted-text role="issue"}.)

venv

venv{.interpreted-text role="mod"} now includes activation scripts for the csh and fish shells. (Contributed by Andrew Svetlov in 15417{.interpreted-text role="issue"}.)

~venv.EnvBuilder{.interpreted-text role="class"} and the ~venv.create{.interpreted-text role="func"} convenience function take a new keyword argument with_pip, which defaults to False, that controls whether or not ~venv.EnvBuilder{.interpreted-text role="class"} ensures that pip is installed in the virtual environment. (Contributed by Nick Coghlan in 19552{.interpreted-text role="issue"} as part of the 453{.interpreted-text role="pep"} implementation.)

wave

The ~wave.Wave_read.getparams{.interpreted-text role="meth"} method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in 17487{.interpreted-text role="issue"}.)

wave.open{.interpreted-text role="meth"} now supports the context management protocol. (Contributed by Claudiu Popa in 17616{.interpreted-text role="issue"}.)

wave{.interpreted-text role="mod"} can now write output to unseekable files <wave-write-objects>{.interpreted-text role="ref"}. (Contributed by David Jones, Guilherme Polo, and Serhiy Storchaka in 5202{.interpreted-text role="issue"}.)

The ~wave.Wave_write.writeframesraw{.interpreted-text role="meth"} and ~wave.Wave_write.writeframes{.interpreted-text role="meth"} methods now accept any bytes-like object{.interpreted-text role="term"}. (Contributed by Serhiy Storchaka in 8311{.interpreted-text role="issue"}.)

weakref

New ~weakref.WeakMethod{.interpreted-text role="class"} class simulates weak references to bound methods. (Contributed by Antoine Pitrou in 14631{.interpreted-text role="issue"}.)

New ~weakref.finalize{.interpreted-text role="class"} class makes it possible to register a callback to be invoked when an object is garbage collected, without needing to carefully manage the lifecycle of the weak reference itself. (Contributed by Richard Oudkerk in 15528{.interpreted-text role="issue"}.)

The callback, if any, associated with a ~weakref.ref{.interpreted-text role="class"} is now exposed via the ~weakref.ref.__callback__{.interpreted-text role="attr"} attribute. (Contributed by Mark Dickinson in 17643{.interpreted-text role="issue"}.)

xml.etree

A new parser, ~xml.etree.ElementTree.XMLPullParser{.interpreted-text role="class"}, allows a non-blocking applications to parse XML documents. An example can be seen at elementtree-pull-parsing{.interpreted-text role="ref"}. (Contributed by Antoine Pitrou in 17741{.interpreted-text role="issue"}.)

The xml.etree.ElementTree{.interpreted-text role="mod"} ~xml.etree.ElementTree.tostring{.interpreted-text role="func"} and ~xml.etree.ElementTree.tostringlist{.interpreted-text role="func"} functions, and the ~xml.etree.ElementTree.ElementTree{.interpreted-text role="class"} ~xml.etree.ElementTree.ElementTree.write{.interpreted-text role="meth"} method, now have a short_empty_elements keyword-only parameter <keyword-only_parameter>{.interpreted-text role="ref"} providing control over whether elements with no content are written in abbreviated (<tag />) or expanded (<tag></tag>) form. (Contributed by Ariel Poliak and Serhiy Storchaka in 14377{.interpreted-text role="issue"}.)

zipfile

The ~zipfile.PyZipFile.writepy{.interpreted-text role="meth"} method of the ~zipfile.PyZipFile{.interpreted-text role="class"} class has a new filterfunc option that can be used to control which directories and files are added to the archive. For example, this could be used to exclude test files from the archive. (Contributed by Christian Tismer in 19274{.interpreted-text role="issue"}.)

The allowZip64 parameter to ~zipfile.ZipFile{.interpreted-text role="class"} and ~zipfile.PyZipFile{.interpreted-text role="class"} is now True by default. (Contributed by William Mallard in 17201{.interpreted-text role="issue"}.)

CPython Implementation Changes

PEP 445: Customization of CPython Memory Allocators {#whatsnew-pep-445}

445{.interpreted-text role="pep"} adds new C level interfaces to customize memory allocation in the CPython interpreter.

::: seealso

445{.interpreted-text role="pep"} -- Add new APIs to customize Python memory allocators

: PEP written and implemented by Victor Stinner. :::

PEP 442: Safe Object Finalization {#whatsnew-pep-442}

442{.interpreted-text role="pep"} removes the current limitations and quirks of object finalization in CPython. With it, objects with ~object.__del__{.interpreted-text role="meth"} methods, as well as generators with finally{.interpreted-text role="keyword"} clauses, can be finalized when they are part of a reference cycle.

As part of this change, module globals are no longer forcibly set to None{.interpreted-text role="const"} during interpreter shutdown in most cases, instead relying on the normal operation of the cyclic garbage collector. This avoids a whole class of interpreter-shutdown-time errors, usually involving __del__ methods, that have plagued Python since the cyclic GC was first introduced.

::: seealso

442{.interpreted-text role="pep"} -- Safe object finalization

: PEP written and implemented by Antoine Pitrou. :::

PEP 456: Secure and Interchangeable Hash Algorithm {#whatsnew-pep-456}

456{.interpreted-text role="pep"} follows up on earlier security fix work done on Python's hash algorithm to address certain DOS attacks to which public facing APIs backed by dictionary lookups may be subject. (See 14621{.interpreted-text role="issue"} for the start of the current round of improvements.) The PEP unifies CPython's hash code to make it easier for a packager to substitute a different hash algorithm, and switches Python's default implementation to a SipHash implementation on platforms that have a 64 bit data type. Any performance differences in comparison with the older FNV algorithm are trivial.

The PEP adds additional fields to the sys.hash_info{.interpreted-text role="data"} named tuple to describe the hash algorithm in use by the currently executing binary. Otherwise, the PEP does not alter any existing CPython APIs.

PEP 436: Argument Clinic {#whatsnew-pep-436}

"Argument Clinic" (436{.interpreted-text role="pep"}) is now part of the CPython build process and can be used to simplify the process of defining and maintaining accurate signatures for builtins and standard library extension modules implemented in C.

Some standard library extension modules have been converted to use Argument Clinic in Python 3.4, and pydoc{.interpreted-text role="mod"} and inspect{.interpreted-text role="mod"} have been updated accordingly.

It is expected that signature metadata for programmatic introspection will be added to additional callables implemented in C as part of Python 3.4 maintenance releases.

:::: note ::: title Note :::

The Argument Clinic PEP is not fully up to date with the state of the implementation. This has been deemed acceptable by the release manager and core development team in this case, as Argument Clinic will not be made available as a public API for third party use in Python 3.4. ::::

::: seealso

436{.interpreted-text role="pep"} -- The Argument Clinic DSL

: PEP written and implemented by Larry Hastings. :::

Other Build and C API Changes

  • The new PyType_GetSlot{.interpreted-text role="c:func"} function has been added to the stable ABI, allowing retrieval of function pointers from named type slots when using the limited API. (Contributed by Martin von Löwis in 17162{.interpreted-text role="issue"}.)
  • The new !Py_SetStandardStreamEncoding{.interpreted-text role="c:func"} pre-initialization API allows applications embedding the CPython interpreter to reliably force a particular encoding and error handler for the standard streams. (Contributed by Bastien Montagne and Nick Coghlan in 16129{.interpreted-text role="issue"}.)
  • Most Python C APIs that don't mutate string arguments are now correctly marked as accepting const char * rather than char *. (Contributed by Serhiy Storchaka in 1772673{.interpreted-text role="issue"}.)
  • A new shell version of python-config can be used even when a python interpreter is not available (for example, in cross compilation scenarios).
  • PyUnicode_FromFormat{.interpreted-text role="c:func"} now supports width and precision specifications for %s, %A, %U, %V, %S, and %R. (Contributed by Ysj Ray and Victor Stinner in 7330{.interpreted-text role="issue"}.)
  • New function PyStructSequence_InitType2{.interpreted-text role="c:func"} supplements the existing PyStructSequence_InitType{.interpreted-text role="c:func"} function. The difference is that it returns 0 on success and -1 on failure.
  • The CPython source can now be compiled using the address sanity checking features of recent versions of GCC and clang: the false alarms in the small object allocator have been silenced. (Contributed by Dhiru Kholia in 18596{.interpreted-text role="issue"}.)
  • The Windows build now uses Address Space Layout Randomization and Data Execution Prevention. (Contributed by Christian Heimes in 16632{.interpreted-text role="issue"}.)
  • New function PyObject_LengthHint{.interpreted-text role="c:func"} is the C API equivalent of operator.length_hint{.interpreted-text role="func"}. (Contributed by Armin Ronacher in 16148{.interpreted-text role="issue"}.)

Other Improvements {#other-improvements-3.4}

::: {#whatsnew-isolated-mode}

  • The python <using-on-cmdline>{.interpreted-text role="ref"} command has a new option <using-on-misc-options>{.interpreted-text role="ref"}, -I, which causes it to run in "isolated mode", which means that sys.path{.interpreted-text role="data"} contains neither the script's directory nor the user's site-packages directory, and all !PYTHON*{.interpreted-text role="envvar"} environment variables are ignored (it implies both -s and -E). Other restrictions may also be applied in the future, with the goal being to isolate the execution of a script from the user's environment. This is appropriate, for example, when Python is used to run a system script. On most POSIX systems it can and should be used in the #! line of system scripts. (Contributed by Christian Heimes in 16499{.interpreted-text role="issue"}.)
  • Tab-completion is now enabled by default in the interactive interpreter on systems that support readline{.interpreted-text role="mod"}. History is also enabled by default, and is written to (and read from) the file ~/.python-history{.interpreted-text role="file"}. (Contributed by Antoine Pitrou and Éric Araujo in 5845{.interpreted-text role="issue"}.)
  • Invoking the Python interpreter with --version now outputs the version to standard output instead of standard error (18338{.interpreted-text role="issue"}). Similar changes were made to argparse{.interpreted-text role="mod"} (18920{.interpreted-text role="issue"}) and other modules that have script-like invocation capabilities (18922{.interpreted-text role="issue"}).
  • The CPython Windows installer now adds .py to the PATHEXT{.interpreted-text role="envvar"} variable when extensions are registered, allowing users to run a python script at the windows command prompt by just typing its name without the .py extension. (Contributed by Paul Moore in 18569{.interpreted-text role="issue"}.)
  • A new make target coverage-report will build python, run the test suite, and generate an HTML coverage report for the C codebase using gcov and lcov.
  • The -R option to the python regression test suite <regrtest>{.interpreted-text role="ref"} now also checks for memory allocation leaks, using sys.getallocatedblocks{.interpreted-text role="func"}. (Contributed by Antoine Pitrou in 13390{.interpreted-text role="issue"}.)
  • python -m now works with namespace packages.
  • The stat{.interpreted-text role="mod"} module is now implemented in C, which means it gets the values for its constants from the C header files, instead of having the values hard-coded in the python module as was previously the case.
  • Loading multiple python modules from a single OS module (.so, .dll) now works correctly (previously it silently returned the first python module in the file). (Contributed by Václav Šmilauer in 16421{.interpreted-text role="issue"}.)
  • A new opcode, !LOAD_CLASSDEREF{.interpreted-text role="opcode"}, has been added to fix a bug in the loading of free variables in class bodies that could be triggered by certain uses of __prepare__ <prepare>{.interpreted-text role="ref"}. (Contributed by Benjamin Peterson in 17853{.interpreted-text role="issue"}.)
  • A number of MemoryError-related crashes were identified and fixed by Victor Stinner using his 445{.interpreted-text role="pep"}-based pyfailmalloc tool (18408{.interpreted-text role="issue"}, 18520{.interpreted-text role="issue"}).
  • The pyvenv command now accepts a --copies option to use copies rather than symlinks even on systems where symlinks are the default. (Contributed by Vinay Sajip in 18807{.interpreted-text role="issue"}.)
  • The pyvenv command also accepts a --without-pip option to suppress the otherwise-automatic bootstrapping of pip into the virtual environment. (Contributed by Nick Coghlan in 19552{.interpreted-text role="issue"} as part of the 453{.interpreted-text role="pep"} implementation.)
  • The encoding name is now optional in the value set for the PYTHONIOENCODING{.interpreted-text role="envvar"} environment variable. This makes it possible to set just the error handler, without changing the default encoding. (Contributed by Serhiy Storchaka in 18818{.interpreted-text role="issue"}.)
  • The bz2{.interpreted-text role="mod"}, lzma{.interpreted-text role="mod"}, and gzip{.interpreted-text role="mod"} module open functions now support x (exclusive creation) mode. (Contributed by Tim Heaney and Vajrasky Kok in 19201{.interpreted-text role="issue"}, 19222{.interpreted-text role="issue"}, and 19223{.interpreted-text role="issue"}.) :::

Significant Optimizations

  • The UTF-32 decoder is now 3x to 4x faster. (Contributed by Serhiy Storchaka in 14625{.interpreted-text role="issue"}.)
  • The cost of hash collisions for sets is now reduced. Each hash table probe now checks a series of consecutive, adjacent key/hash pairs before continuing to make random probes through the hash table. This exploits cache locality to make collision resolution less expensive. The collision resolution scheme can be described as a hybrid of linear probing and open addressing. The number of additional linear probes defaults to nine. This can be changed at compile-time by defining LINEAR_PROBES to be any value. Set LINEAR_PROBES=0 to turn-off linear probing entirely. (Contributed by Raymond Hettinger in 18771{.interpreted-text role="issue"}.)
  • The interpreter starts about 30% faster. A couple of measures lead to the speedup. The interpreter loads fewer modules on startup, e.g. the re{.interpreted-text role="mod"}, collections{.interpreted-text role="mod"} and locale{.interpreted-text role="mod"} modules and their dependencies are no longer imported by default. The marshal module has been improved to load compiled Python code faster. (Contributed by Antoine Pitrou, Christian Heimes and Victor Stinner in 19219{.interpreted-text role="issue"}, 19218{.interpreted-text role="issue"}, 19209{.interpreted-text role="issue"}, 19205{.interpreted-text role="issue"} and 9548{.interpreted-text role="issue"}.)
  • bz2.BZ2File{.interpreted-text role="class"} is now as fast or faster than the Python2 version for most cases. lzma.LZMAFile{.interpreted-text role="class"} has also been optimized. (Contributed by Serhiy Storchaka and Nadeem Vawda in 16034{.interpreted-text role="issue"}.)
  • random.getrandbits{.interpreted-text role="func"} is 20%-40% faster for small integers (the most common use case). (Contributed by Serhiy Storchaka in 16674{.interpreted-text role="issue"}.)
  • By taking advantage of the new storage format for strings, pickling of strings is now significantly faster. (Contributed by Victor Stinner and Antoine Pitrou in 15596{.interpreted-text role="issue"}.)
  • A performance issue in !io.FileIO.readall{.interpreted-text role="meth"} has been solved. This particularly affects Windows, and significantly speeds up the case of piping significant amounts of data through subprocess{.interpreted-text role="mod"}. (Contributed by Richard Oudkerk in 15758{.interpreted-text role="issue"}.)
  • html.escape{.interpreted-text role="func"} is now 10x faster. (Contributed by Matt Bryant in 18020{.interpreted-text role="issue"}.)
  • On Windows, the native VirtualAlloc is now used instead of the CRT malloc in obmalloc. Artificial benchmarks show about a 3% memory savings.
  • os.urandom{.interpreted-text role="func"} now uses a lazily opened persistent file descriptor so as to avoid using many file descriptors when run in parallel from multiple threads. (Contributed by Antoine Pitrou in 18756{.interpreted-text role="issue"}.)

Deprecated {#deprecated-3.4}

This section covers various APIs and other features that have been deprecated in Python 3.4, and will be removed in Python 3.5 or later. In most (but not all) cases, using the deprecated APIs will produce a DeprecationWarning{.interpreted-text role="exc"} when the interpreter is run with deprecation warnings enabled (for example, by using -Wd).

Deprecations in the Python API

  • As mentioned in whatsnew-pep-451{.interpreted-text role="ref"}, a number of importlib{.interpreted-text role="mod"} methods and functions are deprecated: !importlib.find_loader{.interpreted-text role="meth"} is replaced by importlib.util.find_spec{.interpreted-text role="func"}; !importlib.machinery.PathFinder.find_module{.interpreted-text role="meth"} is replaced by importlib.machinery.PathFinder.find_spec{.interpreted-text role="meth"}; !importlib.abc.MetaPathFinder.find_module{.interpreted-text role="meth"} is replaced by importlib.abc.MetaPathFinder.find_spec{.interpreted-text role="meth"}; !importlib.abc.PathEntryFinder.find_loader{.interpreted-text role="meth"} and !find_module{.interpreted-text role="meth"} are replaced by importlib.abc.PathEntryFinder.find_spec{.interpreted-text role="meth"}; all of the {xxx}Loader{.interpreted-text role="samp"} ABC load_module methods (importlib.abc.Loader.load_module, importlib.abc.InspectLoader.load_module, importlib.abc.FileLoader.load_module, importlib.abc.SourceLoader.load_module) should no longer be implemented, instead loaders should implement an exec_module method (importlib.abc.Loader.exec_module{.interpreted-text role="meth"}, importlib.abc.InspectLoader.exec_module{.interpreted-text role="meth"} importlib.abc.SourceLoader.exec_module{.interpreted-text role="meth"}) and let the import system take care of the rest; and !importlib.abc.Loader.module_repr{.interpreted-text role="meth"}, !importlib.util.module_for_loader{.interpreted-text role="meth"}, !importlib.util.set_loader{.interpreted-text role="meth"}, and !importlib.util.set_package{.interpreted-text role="meth"} are no longer needed because their functions are now handled automatically by the import system.
  • The !imp{.interpreted-text role="mod"} module is pending deprecation. To keep compatibility with Python 2/3 code bases, the module's removal is currently not scheduled.
  • The !formatter{.interpreted-text role="mod"} module is pending deprecation and is slated for removal in Python 3.6.
  • MD5 as the default digestmod for the hmac.new{.interpreted-text role="func"} function is deprecated. Python 3.6 will require an explicit digest name or constructor as digestmod argument.
  • The internal Netrc class in the ftplib{.interpreted-text role="mod"} module has been documented as deprecated in its docstring for quite some time. It now emits a DeprecationWarning{.interpreted-text role="exc"} and will be removed completely in Python 3.5.
  • The undocumented endtime argument to subprocess.Popen.wait{.interpreted-text role="meth"} should not have been exposed and is hopefully not in use; it is deprecated and will mostly likely be removed in Python 3.5.
  • The strict argument of ~html.parser.HTMLParser{.interpreted-text role="class"} is deprecated.
  • The plistlib{.interpreted-text role="mod"} !readPlist{.interpreted-text role="func"}, !writePlist{.interpreted-text role="func"}, !readPlistFromBytes{.interpreted-text role="func"}, and !writePlistToBytes{.interpreted-text role="func"} functions are deprecated in favor of the corresponding new functions ~plistlib.load{.interpreted-text role="func"}, ~plistlib.dump{.interpreted-text role="func"}, ~plistlib.loads{.interpreted-text role="func"}, and ~plistlib.dumps{.interpreted-text role="func"}. !Data{.interpreted-text role="func"} is deprecated in favor of just using the bytes{.interpreted-text role="class"} constructor.
  • The sysconfig{.interpreted-text role="mod"} key SO is deprecated, it has been replaced by EXT_SUFFIX.
  • The U mode accepted by various open functions is deprecated. In Python3 it does not do anything useful, and should be replaced by appropriate uses of io.TextIOWrapper{.interpreted-text role="class"} (if needed) and its newline argument.
  • The parser argument of xml.etree.ElementTree.iterparse{.interpreted-text role="func"} has been deprecated, as has the html argument of ~xml.etree.ElementTree.XMLParser{.interpreted-text role="func"}. To prepare for the removal of the latter, all arguments to XMLParser should be passed by keyword.

Deprecated Features

  • Running idle{.interpreted-text role="ref"} with the -n flag (no subprocess) is deprecated. However, the feature will not be removed until 18823{.interpreted-text role="issue"} is resolved.
  • The site module adding a "site-python" directory to sys.path, if it exists, is deprecated (19375{.interpreted-text role="issue"}).

Removed

Operating Systems No Longer Supported

Support for the following operating systems has been removed from the source and build tools:

  • OS/2 (16135{.interpreted-text role="issue"}).
  • Windows 2000 (changeset e52df05b496a).
  • Windows systems where COMSPEC points to command.com (14470{.interpreted-text role="issue"}).
  • VMS (16136{.interpreted-text role="issue"}).

API and Feature Removals

The following obsolete and previously deprecated APIs and features have been removed:

  • The unmaintained Misc/TextMate and Misc/vim directories have been removed (see the devguide for suggestions on what to use instead).
  • The SO makefile macro is removed (it was replaced by the SHLIB_SUFFIX and EXT_SUFFIX macros) (16754{.interpreted-text role="issue"}).
  • The PyThreadState.tick_counter field has been removed; its value has been meaningless since Python 3.2, when the "new GIL" was introduced (19199{.interpreted-text role="issue"}).
  • PyLoader and PyPycLoader have been removed from importlib{.interpreted-text role="mod"}. (Contributed by Taras Lyapun in 15641{.interpreted-text role="issue"}.)
  • The strict argument to ~http.client.HTTPConnection{.interpreted-text role="class"} and ~http.client.HTTPSConnection{.interpreted-text role="class"} has been removed. HTTP 0.9-style "Simple Responses" are no longer supported.
  • The deprecated urllib.request.Request{.interpreted-text role="mod"} getter and setter methods add_data, has_data, get_data, get_type, get_host, get_selector, set_proxy, get_origin_req_host, and is_unverifiable have been removed (use direct attribute access instead).
  • Support for loading the deprecated TYPE_INT64 has been removed from marshal{.interpreted-text role="mod"}. (Contributed by Dan Riti in 15480{.interpreted-text role="issue"}.)
  • inspect.Signature{.interpreted-text role="class"}: positional-only parameters are now required to have a valid name.
  • object.__format__{.interpreted-text role="meth"} no longer accepts non-empty format strings, it now raises a TypeError{.interpreted-text role="exc"} instead. Using a non-empty string has been deprecated since Python 3.2. This change has been made to prevent a situation where previously working (but incorrect) code would start failing if an object gained a __format__ method, which means that your code may now raise a TypeError{.interpreted-text role="exc"} if you are using an 's' format code with objects that do not have a __format__ method that handles it. See 7994{.interpreted-text role="issue"} for background.
  • !difflib.SequenceMatcher.isbjunk{.interpreted-text role="meth"} and !difflib.SequenceMatcher.isbpopular{.interpreted-text role="meth"} were deprecated in 3.2, and have now been removed: use x in sm.bjunk and x in sm.bpopular, where sm is a ~difflib.SequenceMatcher{.interpreted-text role="class"} object (13248{.interpreted-text role="issue"}).

Code Cleanups

  • The unused and undocumented internal Scanner class has been removed from the pydoc{.interpreted-text role="mod"} module.
  • The private and effectively unused _gestalt module has been removed, along with the private platform{.interpreted-text role="mod"} functions _mac_ver_lookup, _mac_ver_gstalt, and _bcd2str, which would only have ever been called on badly broken OSX systems (see 18393{.interpreted-text role="issue"}).
  • The hardcoded copies of certain stat{.interpreted-text role="mod"} constants that were included in the tarfile{.interpreted-text role="mod"} module namespace have been removed.

Porting to Python 3.4

This section lists previously described changes and other bugfixes that may require changes to your code.

Changes in 'python' Command Behavior

  • In a posix shell, setting the PATH{.interpreted-text role="envvar"} environment variable to an empty value is equivalent to not setting it at all. However, setting PYTHONPATH{.interpreted-text role="envvar"} to an empty value was not equivalent to not setting it at all: setting PYTHONPATH{.interpreted-text role="envvar"} to an empty value was equivalent to setting it to ., which leads to confusion when reasoning by analogy to how PATH{.interpreted-text role="envvar"} works. The behavior now conforms to the posix convention for PATH{.interpreted-text role="envvar"}.
  • The [X refs, Y blocks] output of a debug (--with-pydebug) build of the CPython interpreter is now off by default. It can be re-enabled using the -X showrefcount option. (Contributed by Ezio Melotti in 17323{.interpreted-text role="issue"}.)
  • The python command and most stdlib scripts (as well as argparse{.interpreted-text role="mod"}) now output --version information to stdout instead of stderr (for issue list see other-improvements-3.4{.interpreted-text role="ref"} above).

Changes in the Python API

  • The ABCs defined in importlib.abc{.interpreted-text role="mod"} now either raise the appropriate exception or return a default value instead of raising NotImplementedError{.interpreted-text role="exc"} blindly. This will only affect code calling super{.interpreted-text role="func"} and falling through all the way to the ABCs. For compatibility, catch both NotImplementedError{.interpreted-text role="exc"} or the appropriate exception as needed.
  • The module type now initializes the ~module.__package__{.interpreted-text role="attr"} and ~module.__loader__{.interpreted-text role="attr"} attributes to None by default. To determine if these attributes were set in a backwards-compatible fashion, use e.g. getattr(module, '__loader__', None) is not None. (17115{.interpreted-text role="issue"}.)
  • !importlib.util.module_for_loader{.interpreted-text role="meth"} now sets __loader__ and __package__ unconditionally to properly support reloading. If this is not desired then you will need to set these attributes manually. You can use !importlib.util.module_to_load{.interpreted-text role="func"} for module management.
  • Import now resets relevant attributes (e.g. __name__, __loader__, __package__, __file__, __cached__) unconditionally when reloading. Note that this restores a pre-3.3 behavior in that it means a module is re-found when re-loaded (19413{.interpreted-text role="issue"}).
  • Frozen packages no longer set __path__ to a list containing the package name, they now set it to an empty list. The previous behavior could cause the import system to do the wrong thing on submodule imports if there was also a directory with the same name as the frozen package. The correct way to determine if a module is a package or not is to use hasattr(module, '__path__') (18065{.interpreted-text role="issue"}).
  • Frozen modules no longer define a __file__ attribute. It's semantically incorrect for frozen modules to set the attribute as they are not loaded from any explicit location. If you must know that a module comes from frozen code then you can see if the module's __spec__.location is set to 'frozen', check if the loader is a subclass of importlib.machinery.FrozenImporter{.interpreted-text role="class"}, or if Python 2 compatibility is necessary you can use !imp.is_frozen{.interpreted-text role="func"}.
  • py_compile.compile{.interpreted-text role="func"} now raises FileExistsError{.interpreted-text role="exc"} if the file path it would write to is a symlink or a non-regular file. This is to act as a warning that import will overwrite those files with a regular file regardless of what type of file path they were originally.
  • importlib.abc.SourceLoader.get_source{.interpreted-text role="meth"} no longer raises ImportError{.interpreted-text role="exc"} when the source code being loaded triggers a SyntaxError{.interpreted-text role="exc"} or UnicodeDecodeError{.interpreted-text role="exc"}. As ImportError{.interpreted-text role="exc"} is meant to be raised only when source code cannot be found but it should, it was felt to be over-reaching/overloading of that meaning when the source code is found but improperly structured. If you were catching ImportError before and wish to continue to ignore syntax or decoding issues, catch all three exceptions now.
  • functools.update_wrapper{.interpreted-text role="func"} and functools.wraps{.interpreted-text role="func"} now correctly set the __wrapped__ attribute to the function being wrapped, even if that function also had its __wrapped__ attribute set. This means __wrapped__ attributes now correctly link a stack of decorated functions rather than every __wrapped__ attribute in the chain referring to the innermost function. Introspection libraries that assumed the previous behaviour was intentional can use inspect.unwrap{.interpreted-text role="func"} to access the first function in the chain that has no __wrapped__ attribute.
  • inspect.getfullargspec{.interpreted-text role="func"} has been reimplemented on top of inspect.signature{.interpreted-text role="func"} and hence handles a much wider variety of callable objects than it did in the past. It is expected that additional builtin and extension module callables will gain signature metadata over the course of the Python 3.4 series. Code that assumes that inspect.getfullargspec{.interpreted-text role="func"} will fail on non-Python callables may need to be adjusted accordingly.
  • importlib.machinery.PathFinder{.interpreted-text role="class"} now passes on the current working directory to objects in sys.path_hooks{.interpreted-text role="data"} for the empty string. This results in sys.path_importer_cache{.interpreted-text role="data"} never containing '', thus iterating through sys.path_importer_cache{.interpreted-text role="data"} based on sys.path{.interpreted-text role="data"} will not find all keys. A module's __file__ when imported in the current working directory will also now have an absolute path, including when using -m with the interpreter (except for __main__.__file__ when a script has been executed directly using a relative path) (Contributed by Brett Cannon in 18416{.interpreted-text role="issue"}). is specified on the command-line) (18416{.interpreted-text role="issue"}).
  • The removal of the strict argument to ~http.client.HTTPConnection{.interpreted-text role="class"} and ~http.client.HTTPSConnection{.interpreted-text role="class"} changes the meaning of the remaining arguments if you are specifying them positionally rather than by keyword. If you've been paying attention to deprecation warnings your code should already be specifying any additional arguments via keywords.
  • Strings between from __future__ import ... statements now always raise a SyntaxError{.interpreted-text role="exc"}. Previously if there was no leading docstring, an interstitial string would sometimes be ignored. This brings CPython into compliance with the language spec; Jython and PyPy already were. (17434{.interpreted-text role="issue"}).
  • ssl.SSLSocket.getpeercert{.interpreted-text role="meth"} and ssl.SSLSocket.do_handshake{.interpreted-text role="meth"} now raise an OSError{.interpreted-text role="exc"} with ENOTCONN when the SSLSocket is not connected, instead of the previous behavior of raising an AttributeError{.interpreted-text role="exc"}. In addition, ~ssl.SSLSocket.getpeercert{.interpreted-text role="meth"} will raise a ValueError{.interpreted-text role="exc"} if the handshake has not yet been done.
  • base64.b32decode{.interpreted-text role="func"} now raises a binascii.Error{.interpreted-text role="exc"} when the input string contains non-b32-alphabet characters, instead of a TypeError{.interpreted-text role="exc"}. This particular TypeError{.interpreted-text role="exc"} was missed when the other TypeError{.interpreted-text role="exc"}s were converted. (Contributed by Serhiy Storchaka in 18011{.interpreted-text role="issue"}.) Note: this change was also inadvertently applied in Python 3.3.3.
  • The !file{.interpreted-text role="attr"} attribute is now automatically closed when the creating !cgi.FieldStorage{.interpreted-text role="class"} instance is garbage collected. If you were pulling the file object out separately from the !cgi.FieldStorage{.interpreted-text role="class"} instance and not keeping the instance alive, then you should either store the entire !cgi.FieldStorage{.interpreted-text role="class"} instance or read the contents of the file before the !cgi.FieldStorage{.interpreted-text role="class"} instance is garbage collected.
  • Calling read or write on a closed SSL socket now raises an informative ValueError{.interpreted-text role="exc"} rather than the previous more mysterious AttributeError{.interpreted-text role="exc"} (9177{.interpreted-text role="issue"}).
  • slice.indices{.interpreted-text role="meth"} no longer produces an OverflowError{.interpreted-text role="exc"} for huge values. As a consequence of this fix, slice.indices{.interpreted-text role="meth"} now raises a ValueError{.interpreted-text role="exc"} if given a negative length; previously it returned nonsense values (14794{.interpreted-text role="issue"}).
  • The complex{.interpreted-text role="class"} constructor, unlike the cmath{.interpreted-text role="mod"} functions, was incorrectly accepting float{.interpreted-text role="class"} values if an object's __complex__ special method returned one. This now raises a TypeError{.interpreted-text role="exc"}. (16290{.interpreted-text role="issue"}.)
  • The int{.interpreted-text role="class"} constructor in 3.2 and 3.3 erroneously accepts float{.interpreted-text role="class"} values for the base parameter. It is unlikely anyone was doing this, but if so, it will now raise a TypeError{.interpreted-text role="exc"} (16772{.interpreted-text role="issue"}).
  • Defaults for keyword-only arguments are now evaluated after defaults for regular keyword arguments, instead of before. Hopefully no one wrote any code that depends on the previous buggy behavior (16967{.interpreted-text role="issue"}).
  • Stale thread states are now cleared after ~os.fork{.interpreted-text role="func"}. This may cause some system resources to be released that previously were incorrectly kept perpetually alive (for example, database connections kept in thread-local storage). (17094{.interpreted-text role="issue"}.)
  • Parameter names in __annotations__ dicts are now mangled properly, similarly to ~function.__kwdefaults__{.interpreted-text role="attr"}. (Contributed by Yury Selivanov in 20625{.interpreted-text role="issue"}.)
  • hashlib.hash.name{.interpreted-text role="attr"} now always returns the identifier in lower case. Previously some builtin hashes had uppercase names, but now that it is a formal public interface the naming has been made consistent (18532{.interpreted-text role="issue"}).
  • Because unittest.TestSuite{.interpreted-text role="mod"} now drops references to tests after they are run, test harnesses that reuse a ~unittest.TestSuite{.interpreted-text role="class"} to re-run a set of tests may fail. Test suites should not be re-used in this fashion since it means state is retained between test runs, breaking the test isolation that unittest{.interpreted-text role="mod"} is designed to provide. However, if the lack of isolation is considered acceptable, the old behavior can be restored by creating a ~unittest.TestSuite{.interpreted-text role="mod"} subclass that defines a _removeTestAtIndex method that does nothing (see .TestSuite.__iter__{.interpreted-text role="meth"}) (11798{.interpreted-text role="issue"}).
  • unittest{.interpreted-text role="mod"} now uses argparse{.interpreted-text role="mod"} for command line parsing. There are certain invalid command forms that used to work that are no longer allowed; in theory this should not cause backward compatibility issues since the disallowed command forms didn't make any sense and are unlikely to be in use.
  • The re.split{.interpreted-text role="func"}, re.findall{.interpreted-text role="func"}, and re.sub{.interpreted-text role="func"} functions, and the ~re.Match.group{.interpreted-text role="meth"} and ~re.Match.groups{.interpreted-text role="meth"} methods of match objects now always return a bytes object when the string to be matched is a bytes-like object{.interpreted-text role="term"}. Previously the return type matched the input type, so if your code was depending on the return value being, say, a bytearray, you will need to change your code.
  • !audioop{.interpreted-text role="mod"} functions now raise an error immediately if passed string input, instead of failing randomly later on (16685{.interpreted-text role="issue"}).
  • The new convert_charrefs argument to ~html.parser.HTMLParser{.interpreted-text role="class"} currently defaults to False for backward compatibility, but will eventually be changed to default to True. It is recommended that you add this keyword, with the appropriate value, to any ~html.parser.HTMLParser{.interpreted-text role="class"} calls in your code (13633{.interpreted-text role="issue"}).
  • Since the digestmod argument to the hmac.new{.interpreted-text role="func"} function will in the future have no default, all calls to hmac.new{.interpreted-text role="func"} should be changed to explicitly specify a digestmod (17276{.interpreted-text role="issue"}).
  • Calling sysconfig.get_config_var{.interpreted-text role="func"} with the SO key, or looking SO up in the results of a call to sysconfig.get_config_vars{.interpreted-text role="func"} is deprecated. This key should be replaced by EXT_SUFFIX or SHLIB_SUFFIX, depending on the context (19555{.interpreted-text role="issue"}).
  • Any calls to open functions that specify U should be modified. U is ineffective in Python3 and will eventually raise an error if used. Depending on the function, the equivalent of its old Python2 behavior can be achieved using either a newline argument, or if necessary by wrapping the stream in ~io.TextIOWrapper{.interpreted-text role="mod"} to use its newline argument (15204{.interpreted-text role="issue"}).
  • If you use pyvenv in a script and desire that pip not be installed, you must add --without-pip to your command invocation.
  • The default behavior of json.dump{.interpreted-text role="func"} and json.dumps{.interpreted-text role="func"} when an indent is specified has changed: it no longer produces trailing spaces after the item separating commas at the ends of lines. This will matter only if you have tests that are doing white-space-sensitive comparisons of such output (16333{.interpreted-text role="issue"}).
  • doctest{.interpreted-text role="mod"} now looks for doctests in extension module __doc__ strings, so if your doctest test discovery includes extension modules that have things that look like doctests in them you may see test failures you've never seen before when running your tests (3158{.interpreted-text role="issue"}).
  • The collections.abc{.interpreted-text role="mod"} module has been slightly refactored as part of the Python startup improvements. As a consequence of this, it is no longer the case that importing collections{.interpreted-text role="mod"} automatically imports collections.abc{.interpreted-text role="mod"}. If your program depended on the (undocumented) implicit import, you will need to add an explicit import collections.abc (20784{.interpreted-text role="issue"}).

Changes in the C API

  • PyEval_EvalFrameEx{.interpreted-text role="c:func"}, PyObject_Repr{.interpreted-text role="c:func"}, and PyObject_Str{.interpreted-text role="c:func"}, along with some other internal C APIs, now include a debugging assertion that ensures they are not used in situations where they may silently discard a currently active exception. In cases where discarding the active exception is expected and desired (for example, because it has already been saved locally with PyErr_Fetch{.interpreted-text role="c:func"} or is being deliberately replaced with a different exception), an explicit PyErr_Clear{.interpreted-text role="c:func"} call will be needed to avoid triggering the assertion when invoking these operations (directly or indirectly) and running against a version of Python that is compiled with assertions enabled.
  • PyErr_SetImportError{.interpreted-text role="c:func"} now sets TypeError{.interpreted-text role="exc"} when its msg argument is not set. Previously only NULL was returned with no exception set.
  • The result of the PyOS_ReadlineFunctionPointer{.interpreted-text role="c:data"} callback must now be a string allocated by PyMem_RawMalloc{.interpreted-text role="c:func"} or PyMem_RawRealloc{.interpreted-text role="c:func"}, or NULL if an error occurred, instead of a string allocated by PyMem_Malloc{.interpreted-text role="c:func"} or PyMem_Realloc{.interpreted-text role="c:func"} (16742{.interpreted-text role="issue"})
  • PyThread_set_key_value{.interpreted-text role="c:func"} now always set the value. In Python 3.3, the function did nothing if the key already exists (if the current value is a non-NULL pointer).
  • The f_tstate (thread state) field of the PyFrameObject{.interpreted-text role="c:type"} structure has been removed to fix a bug: see 14432{.interpreted-text role="issue"} for the rationale.

Changed in 3.4.3

PEP 476: Enabling certificate verification by default for stdlib http clients {#pep-476}

http.client{.interpreted-text role="mod"} and modules which use it, such as urllib.request{.interpreted-text role="mod"} and xmlrpc.client{.interpreted-text role="mod"}, will now verify that the server presents a certificate which is signed by a CA in the platform trust store and whose hostname matches the hostname being requested by default, significantly improving security for many applications.

For applications which require the old previous behavior, they can pass an alternate context:

import urllib.request
import ssl

# This disables all verification
context = ssl._create_unverified_context()

# This allows using a specific certificate for the host, which doesn't need
# to be in the trust store
context = ssl.create_default_context(cafile="/path/to/file.crt")

urllib.request.urlopen("https://invalid-cert", context=context)