Skip to content

Latest commit

 

History

History
2654 lines (2040 loc) · 110 KB

File metadata and controls

2654 lines (2040 loc) · 110 KB

What's New In Python 3.13

Editor:Thomas Wouters

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

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

.. seealso::

   :pep:`719` -- Python 3.13 Release Schedule

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

Python 3.13 beta is the pre-release of the next version of the Python programming language, with a mix of changes to the language, the implementation and the standard library. The biggest changes to the implementation include a new interactive interpreter, and experimental support for dropping the Global Interpreter Lock (PEP 703) and a Just-In-Time compiler (PEP 744). The library changes contain removal of deprecated APIs and modules, as well as the usual improvements in user-friendliness and correctness.

Interpreter improvements:

New typing features:

Free-threading:

Platform support:

  • PEP 730: Apple's iOS is now an officially supported platform. Official Android support (PEP 738) is in the works as well.

Removed modules:

Release schedule changes:

  • PEP 602 ("Annual Release Cycle for Python") has been updated:
    • Python 3.9 - 3.12 have one and a half years of full support, followed by three and a half years of security fixes.
    • Python 3.13 and later have two years of full support, followed by three years of security fixes.

New Features

A Better Interactive Interpreter

On Unix-like systems like Linux or macOS as well as Windows, Python now uses a new :term:`interactive` shell. When the user starts the :term:`REPL` from an interactive terminal the interactive shell now supports the following new features:

  • Colorized prompts.
  • Multiline editing with history preservation.
  • Interactive help browsing using F1 with a separate command history.
  • History browsing using F2 that skips output as well as the :term:`>>>` and :term:`...` prompts.
  • "Paste mode" with F3 that makes pasting larger blocks of code easier (press F3 again to return to the regular prompt).
  • The ability to issue REPL-specific commands like help, exit, and quit without the need to use call parentheses after the command name.

If the new interactive shell is not desired, it can be disabled via the :envvar:`PYTHON_BASIC_REPL` environment variable.

The new shell requires :mod:`curses` on Unix-like systems.

For more on interactive mode, see :ref:`tut-interac`.

(Contributed by Pablo Galindo Salgado, Łukasz Langa, and Lysandros Nikolaou in :gh:`111201` based on code from the PyPy project. Windows support contributed by Dino Viehland and Anthony Shaw.)

Improved Error Messages

  • A common mistake is to write a script with the same name as a standard library module. When this results in errors, we now display a more helpful error message:

    $ python random.py
    Traceback (most recent call last):
      File "/home/random.py", line 1, in <module>
        import random; print(random.randint(5))
        ^^^^^^^^^^^^^
      File "/home/random.py", line 1, in <module>
        import random; print(random.randint(5))
                            ^^^^^^^^^^^^^^
    AttributeError: module 'random' has no attribute 'randint' (consider renaming '/home/random.py' since it has the same name as the standard library module named 'random' and the import system gives it precedence)
    

    Similarly, if a script has the same name as a third-party module it attempts to import, and this results in errors, we also display a more helpful error message:

    $ python numpy.py
    Traceback (most recent call last):
      File "/home/numpy.py", line 1, in <module>
        import numpy as np; np.array([1,2,3])
        ^^^^^^^^^^^^^^^^^^
      File "/home/numpy.py", line 1, in <module>
        import numpy as np; np.array([1,2,3])
                            ^^^^^^^^
    AttributeError: module 'numpy' has no attribute 'array' (consider renaming '/home/numpy.py' if it has the same name as a third-party module you intended to import)
    

    (Contributed by Shantanu Jain in :gh:`95754`.)

  • 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 :attr:`~class.__static_attributes__` attribute, 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. (Contributed by Irit Katriel in :gh:`115775`.)

Defined mutation semantics for locals()

Historically, the expected result of mutating the return value of :func:`locals` has been left to individual Python implementations to define.

Through PEP 667, Python 3.13 standardises the historical behaviour of CPython for most code execution scopes, but changes :term:`optimized scopes <optimized scope>` (functions, generators, coroutines, comprehensions, and generator expressions) to explicitly return independent snapshots of the currently assigned local variables, including locally referenced nonlocal variables captured in closures.

This change to the semantics of :func:`locals` in optimized scopes also affects the default behaviour of code execution functions that implicitly target locals() if no explicit namespace is provided (such as :func:`exec` and :func:`eval`). In previous versions, whether or not changes could be accessed by calling locals() after calling the code execution function was implementation dependent. In CPython specifically, such code would typically appear to work as desired, but could sometimes fail in optimized scopes based on other code (including debuggers and code execution tracing tools) potentially resetting the shared snapshot in that scope. Now, the code will always run against an independent snapshot of the local variables in optimized scopes, and hence the changes will never be visible in subsequent calls to locals(). To access the changes made in these cases, an explicit namespace reference must now be passed to the relevant function. Alternatively, it may make sense to update affected code to use a higher level code execution API that returns the resulting code execution namespace (e.g. :func:`runpy.run_path` when executing Python files from disk).

To ensure debuggers and similar tools can reliably update local variables in scopes affected by this change, :attr:`FrameType.f_locals <frame.f_locals>` now returns a write-through proxy to the frame's local and locally referenced nonlocal variables in these scopes, rather than returning an inconsistently updated shared dict instance with undefined runtime semantics.

See PEP 667 for more details, including related C API changes and deprecations. Porting notes are also provided below for the affected :ref:`Python APIs <pep667-porting-notes-py>` and :ref:`C APIs <pep667-porting-notes-c>`.

(PEP and implementation contributed by Mark Shannon and Tian Gao in :gh:`74929`. Documentation updates provided by Guido van Rossum and Alyssa Coghlan.)

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.

Support For Mobile Platforms

  • iOS is now a PEP 11 supported platform. arm64-apple-ios (iPhone and iPad devices released after 2013) and arm64-apple-ios-simulator (Xcode iOS simulator running on Apple Silicon hardware) are now tier 3 platforms.

    x86_64-apple-ios-simulator (Xcode iOS simulator running on older x86_64 hardware) is not a tier 3 supported platform, but will be supported on a best-effort basis.

    See PEP 730: for more details.

    (PEP written and implementation contributed by Russell Keith-Magee in :gh:`114099`.)

Experimental JIT Compiler

When CPython is configured using the --enable-experimental-jit option, a just-in-time compiler is added which may 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. The Tier 2 interpreter can be enabled by configuring Python with --enable-experimental-jit=interpreter.
  • When the JIT is enabled, the optimized Tier 2 IR is translated to machine code, which is then executed.
  • The machine code translation process uses a technique called copy-and-patch. It has no runtime dependencies, but there is a new build-time dependency on LLVM.

The --enable-experimental-jit flag has the following optional values:

  • no (default) -- Disable the entire Tier 2 and JIT pipeline.
  • yes (default if the flag is present without optional value) -- Enable the JIT. To disable the JIT at runtime, pass the environment variable PYTHON_JIT=0.
  • yes-off -- Build the JIT but disable it by default. To enable the JIT at runtime, pass the environment variable PYTHON_JIT=1.
  • interpreter -- Enable the Tier 2 interpreter but disable the JIT. The interpreter can be disabled by running with PYTHON_JIT=0.

(On Windows, use PCbuild/build.bat --experimental-jit to enable the JIT or --experimental-jit-interpreter to enable the Tier 2 interpreter.)

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.)

Free-threaded CPython

CPython will run with the :term:`global interpreter lock` (GIL) disabled when configured using the --disable-gil option at build time. This is an experimental feature and therefore isn't used by default. Users need to either compile their own interpreter, or install one of the experimental builds that are marked as free-threaded. See PEP 703 "Making the Global Interpreter Lock Optional in CPython" for more detail.

Free-threaded execution allows for full utilization of the available processing power by running threads in parallel on available CPU cores. While not all software will benefit from this automatically, programs designed with threading in mind will run faster on multicore hardware.

Work is still ongoing: expect some bugs and a substantial single-threaded performance hit.

The free-threaded build still supports optionally running with the GIL enabled at runtime using the environment variable :envvar:`PYTHON_GIL` or the command line option :option:`-X gil`.

To check if the current interpreter is configured with --disable-gil, use sysconfig.get_config_var("Py_GIL_DISABLED"). To check if the :term:`GIL` is actually disabled in the running process, the :func:`!sys._is_gil_enabled` function can be used.

C-API extension modules need to be built specifically for the free-threaded build. Extensions that support running with the :term:`GIL` disabled should use the :c:data:`Py_mod_gil` slot. Extensions using single-phase init should use :c:func:`PyUnstable_Module_SetGIL` to indicate whether they support running with the GIL disabled. Importing C extensions that don't use these mechanisms will cause the GIL to be enabled, unless the GIL was explicitly disabled with the :envvar:`PYTHON_GIL` environment variable or the :option:`-X gil=0` option.

pip 24.1b1 or newer is required to install packages with C extensions in the free-threaded build.

Other Language Changes

New Modules

  • None.

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, and if a :class:`!ast.expr_context` field is omitted, it defaults to :class:`Load() <ast.Load>`. (Previously, in all 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.

    These changes do not apply to user-defined subclasses of :class:`ast.AST`, unless the class opts in to the new behavior by setting the attribute :attr:`ast.AST._field_types`.

    (Contributed by Jelle Zijlstra in :gh:`105858`, :gh:`117486`, and :gh:`118851`.)

  • :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

ctypes

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`.)
  • :meth:`~dis.get_instructions` no longer represents cache entries as separate instructions. Instead, it returns them as part of the :class:`~dis.Instruction`, in the new cache_info field. The show_caches argument to :meth:`~dis.get_instructions` is deprecated and no longer has any effect. (Contributed by Irit Katriel in :gh:`112962`.)

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`.)

mimetypes

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

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`.)
  • :mod:`zipapp` is supported as a debugging target. (Contributed by Tian Gao in :gh:`118501`.)
  • breakpoint() and pdb.set_trace() now enter the debugger immediately rather than on the next line of code to be executed. This change prevents the debugger from breaking outside of the context when breakpoint() is positioned at the end of the context. (Contributed by Tian Gao in :gh:`118579`.)

queue

random

re

site

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. Also added :func:`statistics.kde_random` for sampling from the estimated probability density function. (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`.)

tempfile

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

types

  • :class:`~types.SimpleNamespace` constructor now allows specifying initial values of attributes as a positional argument which must be a mapping or an iterable of key-value pairs. (Contributed by Serhiy Storchaka in :gh:`108191`.)

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

Removed Modules And APIs

PEP 594: dead batteries (and other module removals)

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`.)

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

New Deprecations

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.

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`.)

C API Changes

New Features

Build Changes

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

  • Calling :func:`locals` in an :term:`optimized scope` now produces an independent snapshot on each call, and hence no longer implicitly updates previously returned references. Obtaining the legacy CPython behaviour now requires explicit calls to update the initially returned dictionary with the results of subsequent calls to locals(). Code execution functions that implicitly target locals() (such as exec and eval) must be passed an explicit namespace to access their results in an optimized scope. (Changed as part of PEP 667.)
  • Calling :func:`locals` from a comprehension at module or class scope (including via exec or eval) once more behaves as if the comprehension were running as an independent nested function (i.e. the local variables from the containing scope are not included). In Python 3.12, this had changed to include the local variables from the containing scope when implementing PEP 709. (Changed as part of PEP 667.)
  • Accessing :attr:`FrameType.f_locals <frame.f_locals>` in an :term:`optimized scope` now returns a write-through proxy rather than a snapshot that gets updated at ill-specified times. If a snapshot is desired, it must be created explicitly with dict or the proxy's .copy() method. (Changed as part of PEP 667.)

Changes in the C API

  • 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. The new macros were added in Python 3.8 and the old macros were deprecated in Python 3.11. (Contributed by Irit Katriel in :gh:`105111`.)

  • Functions :c:func:`PyDict_GetItem`, :c:func:`PyDict_GetItemString`, :c:func:`PyMapping_HasKey`, :c:func:`PyMapping_HasKeyString`, :c:func:`PyObject_HasAttr`, :c:func:`PyObject_HasAttrString`, and :c:func:`PySys_GetObject`, which clear all errors which occurred when calling them, now report them using :func:`sys.unraisablehook`. You may replace them with other functions as recommended in the documentation. (Contributed by Serhiy Storchaka in :gh:`106672`.)

  • :c:func:`!PyCode_GetFirstFree` is an unstable API now and has been renamed to :c:func:`PyUnstable_Code_GetFirstFree`. (Contributed by Bogdan Romanyuk in :gh:`115781`.)

Removed C APIs

Deprecated C APIs

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