Skip to content

Latest commit

 

History

History
852 lines (645 loc) · 33.2 KB

3.12.rst

File metadata and controls

852 lines (645 loc) · 33.2 KB

What's New In Python 3.12

Release:|release|
Date: |today|

This article explains the new features in Python 3.12, compared to 3.11.

For full details, see the :ref:`changelog <changelog>`.

Note

Prerelease users should be aware that this document is currently in draft form. It will be updated substantially as Python 3.12 moves towards release, so it's worth checking back even after reading earlier versions.

Summary -- Release highlights

Important deprecations, removals or restrictions:

  • PEP 623, Remove wstr from Unicode
  • PEP 632, Remove the distutils package.

Improved Error Messages

  • Modules from the standard library are now potentially suggested as part of the error messages displayed by the interpreter when a :exc:`NameError` is raised to the top level. Contributed by Pablo Galindo in :gh:`98254`.

    >>> sys.version_info
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'sys' is not defined. Did you forget to import 'sys'?
  • Improve the error suggestion for :exc:`NameError` exceptions for instances. Now if a :exc:`NameError` is raised in a method and the instance has an attribute that's exactly equal to the name in the exception, the suggestion will include self.<NAME> instead of the closest match in the method scope. Contributed by Pablo Galindo in :gh:`99139`.

    >>> class A:
    ...    def __init__(self):
    ...        self.blech = 1
    ...
    ...    def foo(self):
    ...        somethin = blech
    >>> A().foo()
    Traceback (most recent call last):
      File "<stdin>", line 1
        somethin = blech
                   ^^^^^
    NameError: name 'blech' is not defined. Did you mean: 'self.blech'?
  • Improve the :exc:`SyntaxError` error message when the user types import x from y instead of from y import x. Contributed by Pablo Galindo in :gh:`98931`.

    >>> import a.y.z from b.y.z
    Traceback (most recent call last):
      File "<stdin>", line 1
        import a.y.z from b.y.z
        ^^^^^^^^^^^^^^^^^^^^^^^
    SyntaxError: Did you mean to use 'from ... import ...' instead?
  • :exc:`ImportError` exceptions raised from failed from <module> import <name> statements now include suggestions for the value of <name> based on the available names in <module>. Contributed by Pablo Galindo in :gh:`91058`.

    >>> from collections import chainmap
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: cannot import name 'chainmap' from 'collections'. Did you mean: 'ChainMap'?

New Features

Other Language Changes

New Modules

  • None yet.

Improved Modules

array

asyncio

pathlib

dis

  • Pseudo instruction opcodes (which are used by the compiler but do not appear in executable bytecode) are now exposed in the :mod:`dis` module. :data:`~dis.HAVE_ARGUMENT` is still relevant to real opcodes, but it is not useful for pseudo instructions. Use the new :data:`~dis.hasarg` collection instead. (Contributed by Irit Katriel in :gh:`94216`.)

os

shutil

  • :func:`shutil.make_archive` now passes the root_dir argument to custom archivers which support it. In this case it no longer temporarily changes the current working directory of the process to root_dir to perform archiving. (Contributed by Serhiy Storchaka in :gh:`74696`.)

sqlite3

threading

unicodedata

  • The Unicode database has been updated to version 15.0.0. (Contributed by Benjamin Peterson in :gh:`96734`).

tempfile

The :class:`tempfile.NamedTemporaryFile` function has a new optional parameter delete_on_close (Contributed by Evgeny Zorin in :gh:`58451`.)

sys

Optimizations

  • Removed wstr and wstr_length members from Unicode objects. It reduces object size by 8 or 16 bytes on 64bit platform. (PEP 623) (Contributed by Inada Naoki in :gh:`92536`.)
  • Added experimental support for using the BOLT binary optimizer in the build process, which improves performance by 1-5%. (Contributed by Kevin Modzelewski in :gh:`90536`.)
  • Speed up the regular expression substitution (functions :func:`re.sub` and :func:`re.subn` and corresponding :class:`re.Pattern` methods) for replacement strings containing group references by 2--3 times. (Contributed by Serhiy Storchaka in :gh:`91524`.)

CPython bytecode changes

Demos and Tools

  • Remove the Tools/demo/ directory which contained old demo scripts. A copy can be found in the old-demos project. (Contributed by Victor Stinner in :gh:`97681`.)
  • Remove outdated example scripts of the Tools/scripts/ directory. A copy can be found in the old-demos project. (Contributed by Victor Stinner in :gh:`97669`.)

Deprecated

Pending Removal in Python 3.13

The following modules and APIs have been deprecated in earlier Python releases, and will be removed in Python 3.13.

Modules (see PEP 594):

APIs:

Pending Removal in Python 3.14

Pending Removal in Future Versions

The following APIs were deprecated in earlier Python versions and will be removed, although there is currently no date scheduled for their removal.

Removed

Porting to Python 3.12

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

Changes in the Python API

  • More strict rules are now applied for numerical group references and group names in regular expressions. Only sequence of ASCII digits is now accepted as a numerical reference. The group name in bytes patterns and replacement strings can now only contain ASCII letters and digits and underscore. (Contributed by Serhiy Storchaka in :gh:`91760`.)
  • Removed randrange() functionality deprecated since Python 3.10. Formerly, randrange(10.0) losslessly converted to randrange(10). Now, it raises a TypeError. Also, the exception raised for non-integral values such as randrange(10.5) or randrange('10') has been changed from ValueError to TypeError. This also prevents bugs where randrange(1e25) would silently select from a larger range than randrange(10**25). (Originally suggested by Serhiy Storchaka gh-86388.)
  • :class:`argparse.ArgumentParser` changed encoding and error handler for reading arguments from file (e.g. fromfile_prefix_chars option) from default text encoding (e.g. :func:`locale.getpreferredencoding(False) <locale.getpreferredencoding>`) to :term:`filesystem encoding and error handler`. Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.
  • Removed the asyncore-based smtpd module deprecated in Python 3.4.7 and 3.5.4. A recommended replacement is the :mod:`asyncio`-based aiosmtpd PyPI module.
  • :func:`shlex.split`: Passing None for s argument now raises an exception, rather than reading :data:`sys.stdin`. The feature was deprecated in Python 3.9. (Contributed by Victor Stinner in :gh:`94352`.)
  • The :mod:`os` module no longer accepts bytes-like paths, like :class:`bytearray` and :class:`memoryview` types: only the exact :class:`bytes` type is accepted for bytes strings. (Contributed by Victor Stinner in :gh:`98393`.)

Build Changes

  • Python no longer uses setup.py to build shared C extension modules. Build parameters like headers and libraries are detected in configure script. Extensions are built by Makefile. Most extensions use pkg-config and fall back to manual detection. (Contributed by Christian Heimes in :gh:`93939`.)
  • va_start() with two parameters, like va_start(args, format), is now required to build Python. va_start() is no longer called with a single parameter. (Contributed by Kumar Aditya in :gh:`93207`.)
  • CPython now uses the ThinLTO option as the default link time optimization policy if the Clang compiler accepts the flag. (Contributed by Dong-hee Na in :gh:`89536`.)

C API Changes

New Features

Porting to Python 3.12

Deprecated

Removed