Skip to content

Latest commit

 

History

History
2158 lines (1652 loc) · 87 KB

3.13.rst

File metadata and controls

2158 lines (1652 loc) · 87 KB

What's New In Python 3.13

Editor:TBD

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

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.13 moves towards release, so it's worth checking back even after reading earlier versions.

Summary -- Release highlights

Important deprecations, removals or restrictions:

Interpreter improvements:

New typing features:

New Features

Improved Error Messages

  • The interpreter now colorizes error messages when displaying tracebacks by default. This feature can be controlled via the new :envvar:`PYTHON_COLORS` environment variable as well as the canonical NO_COLOR and FORCE_COLOR environment variables. See also :ref:`using-on-controlling-color`. (Contributed by Pablo Galindo Salgado in :gh:`112730`.)

  • When an incorrect keyword argument is passed to a function, the error message now potentially suggests the correct keyword argument. (Contributed by Pablo Galindo Salgado and Shantanu Jain in :gh:`107944`.)

    >>> "better error messages!".split(max_split=1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        "better error messages!".split(max_split=1)
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
    TypeError: split() got an unexpected keyword argument 'max_split'. Did you mean 'maxsplit'?
  • Classes have a new __static_attributes__, populated by the compiler, with a tuple of names of attributes of this class which are accessed through self.X from any function in its body.

Incremental Garbage Collection

  • The cycle garbage collector is now incremental. This means that maximum pause times are reduced by an order of magnitude or more for larger heaps.

Other Language Changes

New Modules

  • None yet.

Improved Modules

argparse

array

  • Add 'w' type code (Py_UCS4) that can be used for Unicode strings. It can be used instead of 'u' type code, which is deprecated. (Contributed by Inada Naoki in :gh:`80480`.)
  • Add clear() method in order to implement MutableSequence. (Contributed by Mike Zimin in :gh:`114894`.)

ast

  • The constructors of node types in the :mod:`ast` module are now stricter in the arguments they accept, and have more intuitive behaviour when arguments are omitted.

    If an optional field on an AST node is not included as an argument when constructing an instance, the field will now be set to None. Similarly, if a list field is omitted, that field will now be set to an empty list. (Previously, in both cases, the attribute would be missing on the newly constructed AST node instance.)

    If other arguments are omitted, a :exc:`DeprecationWarning` is emitted. This will cause an exception in Python 3.15. Similarly, passing a keyword argument that does not map to a field on the AST node is now deprecated, and will raise an exception in Python 3.15.

  • :func:`ast.parse` now accepts an optional argument optimize which is passed on to the :func:`compile` built-in. This makes it possible to obtain an optimized AST. (Contributed by Irit Katriel in :gh:`108113`.)

asyncio

base64

copy

dbm

dis

  • Change the output of :mod:`dis` module functions to show logical labels for jump targets and exception handlers, rather than offsets. The offsets can be added with the new -O command line option or the show_offsets parameter. (Contributed by Irit Katriel in :gh:`112137`.)

doctest

email

  • :func:`email.utils.getaddresses` and :func:`email.utils.parseaddr` now return ('', '') 2-tuples in more situations where invalid email addresses are encountered instead of potentially inaccurate values. Add optional strict parameter to these two functions: use strict=False to get the old behavior, accept malformed inputs. getattr(email.utils, 'supports_strict_parsing', False) can be used to check if the strict parameter is available. (Contributed by Thomas Dwyer and Victor Stinner for :gh:`102988` to improve the :cve:`2023-27043` fix.)

fractions

  • Formatting for objects of type :class:`fractions.Fraction` now supports the standard format specification mini-language rules for fill, alignment, sign handling, minimum width and grouping. (Contributed by Mark Dickinson in :gh:`111320`.)

gc

  • The cyclic garbage collector is now incremental, which changes the meanings of the results of :meth:`gc.get_threshold` and :meth:`gc.set_threshold` as well as :meth:`gc.get_count` and :meth:`gc.get_stats`.

    • :meth:`gc.get_threshold` returns a three-item tuple for backwards compatibility. The first value is the threshold for young collections, as before; the second value determines the rate at which the old collection is scanned (the default is 10, and higher values mean that the old collection is scanned more slowly). The third value is meaningless and is always zero.
    • :meth:`gc.set_threshold` ignores any items after the second.
    • :meth:`gc.get_count` and :meth:`gc.get_stats` return the same format of results as before. The only difference is that instead of the results referring to the young, aging and old generations, the results refer to the young generation and the aging and collecting spaces of the old generation.

    In summary, code that attempted to manipulate the behavior of the cycle GC may not work exactly as intended, but it is very unlikely to be harmful. All other code will work just fine.

glob

  • Add :func:`glob.translate` function that converts a path specification with shell-style wildcards to a regular expression. (Contributed by Barney Gale in :gh:`72904`.)

importlib

io

ipaddress

itertools

marshal

  • Add the allow_code parameter in module functions. Passing allow_code=False prevents serialization and de-serialization of code objects which are incompatible between Python versions. (Contributed by Serhiy Storchaka in :gh:`113626`.)

math

  • A new function :func:`~math.fma` for fused multiply-add operations has been added. This function computes x * y + z with only a single round, and so avoids any intermediate loss of precision. It wraps the fma() function provided by C99, and follows the specification of the IEEE 754 "fusedMultiplyAdd" operation for special cases. (Contributed by Mark Dickinson and Victor Stinner in :gh:`73468`.)

mmap

opcode

  • Move opcode.ENABLE_SPECIALIZATION to _opcode.ENABLE_SPECIALIZATION. This field was added in 3.12, it was never documented and is not intended for external usage. (Contributed by Irit Katriel in :gh:`105481`.)
  • Removed opcode.is_pseudo, opcode.MIN_PSEUDO_OPCODE and opcode.MAX_PSEUDO_OPCODE, which were added in 3.12, were never documented or exposed through dis, and were not intended to be used externally.

os

os.path

  • Add :func:`os.path.isreserved` to check if a path is reserved on the current system. This function is only available on Windows. (Contributed by Barney Gale in :gh:`88569`.)
  • On Windows, :func:`os.path.isabs` no longer considers paths starting with exactly one (back)slash to be absolute. (Contributed by Barney Gale and Jon Foster in :gh:`44626`.)

pathlib

pdb

  • Add ability to move between chained exceptions during post mortem debugging in :func:`~pdb.pm` using the new exceptions [exc_number] command for Pdb. (Contributed by Matthias Bussonnier in :gh:`106676`.)
  • Expressions/statements whose prefix is a pdb command are now correctly identified and executed. (Contributed by Tian Gao in :gh:`108464`.)
  • sys.path[0] will no longer be replaced by the directory of the script being debugged when sys.flags.safe_path is set (via the :option:`-P` command line option or :envvar:`PYTHONSAFEPATH` environment variable). (Contributed by Tian Gao and Christian Walther in :gh:`111762`.)

queue

re

sqlite3

statistics

  • Add :func:`statistics.kde` for kernel density estimation. This makes it possible to estimate a continuous probability density function from a fixed number of discrete samples. (Contributed by Raymond Hettinger in :gh:`115863`.)

subprocess

sys

  • Add the :func:`sys._is_interned` function to test if the string was interned. This function is not guaranteed to exist in all implementations of Python. (Contributed by Serhiy Storchaka in :gh:`78573`.)

time

  • On Windows, :func:`time.monotonic()` now uses the QueryPerformanceCounter() clock to have a resolution better than 1 us, instead of the GetTickCount64() clock which has a resolution of 15.6 ms. (Contributed by Victor Stinner in :gh:`88494`.)
  • On Windows, :func:`time.time()` now uses the GetSystemTimePreciseAsFileTime() clock to have a resolution better than 1 μs, instead of the GetSystemTimeAsFileTime() clock which has a resolution of 15.6 ms. (Contributed by Victor Stinner in :gh:`63207`.)

tkinter

traceback

typing

unicodedata

  • The Unicode database has been updated to version 15.1.0. (Contributed by James Gerity in :gh:`109559`.)

venv

  • Add support for adding source control management (SCM) ignore files to a virtual environment's directory. By default, Git is supported. This is implemented as opt-in via the API which can be extended to support other SCMs (:class:`venv.EnvBuilder` and :func:`venv.create`), and opt-out via the CLI (using --without-scm-ignore-files). (Contributed by Brett Cannon in :gh:`108125`.)

warnings

xml.etree.ElementTree

zipimport

  • Gains support for ZIP64 format files. Everybody loves huge code right? (Contributed by Tim Hatch in :gh:`94146`.)

Optimizations

Experimental JIT Compiler

When CPython is configured using the --enable-experimental-jit option, a just-in-time compiler is added which can speed up some Python programs.

The internal architecture is roughly as follows.

  • We start with specialized Tier 1 bytecode. See :ref:`What's new in 3.11 <whatsnew311-pep659>` for details.
  • When the Tier 1 bytecode gets hot enough, it gets translated to a new, purely internal Tier 2 IR, a.k.a. micro-ops ("uops").
  • The Tier 2 IR uses the same stack-based VM as Tier 1, but the instruction format is better suited to translation to machine code.
  • We have several optimization passes for Tier 2 IR, which are applied before it is interpreted or translated to machine code.
  • There is a Tier 2 interpreter, but it is mostly intended for debugging the earlier stages of the optimization pipeline. If the JIT is not enabled, the Tier 2 interpreter can be invoked by passing Python the -X uops option or by setting the PYTHON_UOPS environment variable to 1.
  • When the --enable-experimental-jit option is used, the optimized Tier 2 IR is translated to machine code, which is then executed. This does not require additional runtime options.
  • The machine code translation process uses an architecture called copy-and-patch. It has no runtime dependencies, but there is a new build-time dependency on LLVM.

See PEP 744 for more details.

(JIT by Brandt Bucher, inspired by a paper by Haoran Xu and Fredrik Kjolstad. Tier 2 IR by Mark Shannon and Guido van Rossum. Tier 2 optimizer by Ken Jin.)

Deprecated

Pending Removal in Python 3.14

Pending Removal in Python 3.15

Pending Removal in Python 3.16

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

PEP 594: dead batteries

2to3

configparser

importlib

locale

  • Remove locale.resetlocale() function deprecated in Python 3.11: use locale.setlocale(locale.LC_ALL, "") instead. (Contributed by Victor Stinner in :gh:`104783`.)

logging

pathlib

  • Remove support for using :class:`pathlib.Path` objects as context managers. This functionality was deprecated and made a no-op in Python 3.9.

re

  • Remove undocumented, never working, and deprecated re.template function and re.TEMPLATE flag (and re.T alias). (Contributed by Serhiy Storchaka and Nikita Sobolev in :gh:`105687`.)

tkinter

  • Remove the :mod:`!tkinter.tix` module, deprecated in Python 3.6. The third-party Tix library which the module wrapped is unmaintained. (Contributed by Zachary Ware in :gh:`75552`.)

turtle

typing

  • Namespaces typing.io and typing.re, deprecated in Python 3.8, are now removed. The items in those namespaces can be imported directly from :mod:`typing`. (Contributed by Sebastian Rittau in :gh:`92871`.)
  • Remove support for the keyword-argument method of creating :class:`typing.TypedDict` types, deprecated in Python 3.11. (Contributed by Tomas Roun in :gh:`104786`.)

unittest

urllib

webbrowser

Others

  • None yet

CPython bytecode changes

  • The oparg of YIELD_VALUE is now 1 if the yield is part of a yield-from or await, and 0 otherwise. The oparg of RESUME was changed to add a bit indicating whether the except-depth is 1, which is needed to optimize closing of generators. (Contributed by Irit Katriel in :gh:`111354`.)

Porting to Python 3.13

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

Changes in the Python API

Build Changes

  • Autoconf 2.71 and aclocal 1.16.4 are now required to regenerate the :file:`configure` script. (Contributed by Christian Heimes in :gh:`89886`.)
  • SQLite 3.15.2 or newer is required to build the :mod:`sqlite3` extension module. (Contributed by Erlend Aasland in :gh:`105875`.)
  • Python built with :file:`configure` :option:`--with-trace-refs` (tracing references) is now ABI compatible with the Python release build and :ref:`debug build <debug-build>`. (Contributed by Victor Stinner in :gh:`108634`.)
  • Building CPython now requires a compiler with support for the C11 atomic library, GCC built-in atomic functions, or MSVC interlocked intrinsics.
  • The errno, fcntl, grp, md5, pwd, resource, termios, winsound, _ctypes_test, _multiprocessing.posixshmem, _scproxy, _stat, _statistics, _testconsole, _testimportmultiple and _uuid C extensions are now built with the :ref:`limited C API <limited-c-api>`. (Contributed by Victor Stinner in :gh:`85283`.)
  • wasm32-wasi is now a PEP 11 tier 2 platform. (Contributed by Brett Cannon in :gh:`115192`.)
  • wasm32-emscripten is no longer a PEP 11 supported platform. (Contributed by Brett Cannon in :gh:`115192`.)

C API Changes

New Features

Porting to Python 3.13

  • Python.h no longer includes the <ieeefp.h> standard header. It was included for the finite() function which is now provided by the <math.h> header. It should now be included explicitly if needed. Remove also the HAVE_IEEEFP_H macro. (Contributed by Victor Stinner in :gh:`108765`.)

  • Python.h no longer includes these standard header files: <time.h>, <sys/select.h> and <sys/time.h>. If needed, they should now be included explicitly. For example, <time.h> provides the clock() and gmtime() functions, <sys/select.h> provides the select() function, and <sys/time.h> provides the futimes(), gettimeofday() and setitimer() functions. (Contributed by Victor Stinner in :gh:`108765`.)

  • On Windows, Python.h no longer includes the <stddef.h> standard header file. If needed, it should now be included explicitly. For example, it provides offsetof() function, and size_t and ptrdiff_t types. Including <stddef.h> explicitly was already needed by all other platforms, the HAVE_STDDEF_H macro is only defined on Windows. (Contributed by Victor Stinner in :gh:`108765`.)

  • If the :c:macro:`Py_LIMITED_API` macro is defined, :c:macro:`!Py_BUILD_CORE`, :c:macro:`!Py_BUILD_CORE_BUILTIN` and :c:macro:`!Py_BUILD_CORE_MODULE` macros are now undefined by <Python.h>. (Contributed by Victor Stinner in :gh:`85283`.)

  • The old trashcan macros Py_TRASHCAN_SAFE_BEGIN and Py_TRASHCAN_SAFE_END were removed. They should be replaced by the new macros Py_TRASHCAN_BEGIN and Py_TRASHCAN_END.

    A tp_dealloc function that has the old macros, such as:

    static void
    mytype_dealloc(mytype *p)
    {
        PyObject_GC_UnTrack(p);
        Py_TRASHCAN_SAFE_BEGIN(p);
        ...
        Py_TRASHCAN_SAFE_END
    }
    

    should migrate to the new macros as follows:

    static void
    mytype_dealloc(mytype *p)
    {
        PyObject_GC_UnTrack(p);
        Py_TRASHCAN_BEGIN(p, mytype_dealloc)
        ...
        Py_TRASHCAN_END
    }
    

    Note that Py_TRASHCAN_BEGIN has a second argument which should be the deallocation function it is in.

Deprecated

Removed

Pending Removal in Python 3.14

Pending Removal in Python 3.15

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.

Regression Test Changes