Skip to content

Latest commit

 

History

History
1999 lines (1531 loc) · 80.3 KB

3.13.rst

File metadata and controls

1999 lines (1531 loc) · 80.3 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:

  • A basic :ref:`JIT compiler <whatsnew313-jit-compiler>` was added. It is currently disabled by default (though we may turn it on later). Performance improvements are modest -- we expect to be improving this over the next few releases.

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'?

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

dbm

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

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

io

The :class:`io.IOBase` finalizer now logs the close() method errors with :data:`sys.unraisablehook`. Previously, errors were ignored silently by default, and only logged in :ref:`Python Development Mode <devmode>` or on :ref:`Python built on debug mode <debug-build>`. (Contributed by Victor Stinner in :gh:`62948`.)

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

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 us, instead of the GetSystemTimeAsFileTime() clock which has a resolution of 15.6 ms. (Contributed by Victor Stinner in :gh:`88494`.)

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

Optimizations

  • :func:`textwrap.indent` is now ~30% faster than before for large input. (Contributed by Inada Naoki in :gh:`107369`.)
  • The :mod:`subprocess` module uses :func:`os.posix_spawn` in more situations including the default where close_fds=True on many modern platforms. This should provide a noteworthy performance increase launching processes on FreeBSD and Solaris. See the subprocess section above for details. (Contributed by Jakub Kulik in :gh:`113117`.)

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.

(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

  • :class:`http.server.CGIHTTPRequestHandler` will be removed along with its related --cgi flag to python -m http.server. It was obsolete and rarely used. No direct replacement exists. Anything is better than CGI to interface a web server with a request handler.
  • :class:`locale`: :func:`locale.getdefaultlocale` was deprecated in Python 3.11 and originally planned for removal in Python 3.13 (:gh:`90817`), but removal has been postponed to Python 3.15. Use :func:`locale.setlocale()`, :func:`locale.getencoding()` and :func:`locale.getlocale()` instead. (Contributed by Hugo van Kemenade in :gh:`111187`.)
  • :mod:`pathlib`: :meth:`pathlib.PurePath.is_reserved` is deprecated and scheduled for removal in Python 3.15. Use :func:`os.path.isreserved` to detect reserved paths on Windows.
  • :mod:`threading`: Passing any arguments to :func:`threading.RLock` is now deprecated. C version allows any numbers of args and kwargs, but they are just ignored. Python version does not allow any arguments. All arguments will be removed from :func:`threading.RLock` in Python 3.15. (Contributed by Nikita Sobolev in :gh:`102029`.)
  • :class:`typing.NamedTuple`:
    • The undocumented keyword argument syntax for creating NamedTuple classes (NT = NamedTuple("NT", x=int)) is deprecated, and will be disallowed in 3.15. Use the class-based syntax or the functional syntax instead.
    • When using the functional syntax to create a NamedTuple class, failing to pass a value to the 'fields' parameter (NT = NamedTuple("NT")) is deprecated. Passing None to the 'fields' parameter (NT = NamedTuple("NT", None)) is also deprecated. Both will be disallowed in Python 3.15. To create a NamedTuple class with 0 fields, use class NT(NamedTuple): pass or NT = NamedTuple("NT", []).
  • :class:`typing.TypedDict`: When using the functional syntax to create a TypedDict class, failing to pass a value to the 'fields' parameter (TD = TypedDict("TD")) is deprecated. Passing None to the 'fields' parameter (TD = TypedDict("TD", None)) is also deprecated. Both will be disallowed in Python 3.15. To create a TypedDict class with 0 fields, use class TD(TypedDict): pass or TD = TypedDict("TD", {}).
  • :mod:`wave`: Deprecate the getmark(), setmark() and getmarkers() methods of the :class:`wave.Wave_read` and :class:`wave.Wave_write` classes. They will be removed in Python 3.15. (Contributed by Victor Stinner in :gh:`105096`.)
  • :func:`platform.java_ver` is deprecated and will be removed in 3.15. It was largely untested, had a confusing API, and was only useful for Jython support. (Contributed by Nikita Sobolev in :gh:`116349`.)

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 is 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, resource, winsound, _ctypes_test, _multiprocessing.posixshmem, _scproxy, _stat, _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 tier 2 platform. (Contributed by Brett Cannon in :gh:`115192`.)
  • wasm32-emscripten is no longer a 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`.)

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

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

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