2582 lines
306 KiB
HTML
2582 lines
306 KiB
HTML
<!DOCTYPE html>
|
||
|
||
<html lang="en" data-content_root="../">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<meta property="og:title" content="What’s New In Python 3.10" />
|
||
<meta property="og:type" content="website" />
|
||
<meta property="og:url" content="https://docs.python.org/3/whatsnew/3.10.html" />
|
||
<meta property="og:site_name" content="Python documentation" />
|
||
<meta property="og:description" content="Editor, Pablo Galindo Salgado,. This article explains the new features in Python 3.10, compared to 3.9. Python 3.10 was released on October 4, 2021. For full details, see the changelog. Summary – R..." />
|
||
<meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
|
||
<meta property="og:image:alt" content="Python documentation" />
|
||
<meta name="description" content="Editor, Pablo Galindo Salgado,. This article explains the new features in Python 3.10, compared to 3.9. Python 3.10 was released on October 4, 2021. For full details, see the changelog. Summary – R..." />
|
||
<meta property="og:image:width" content="200">
|
||
<meta property="og:image:height" content="200">
|
||
<meta name="theme-color" content="#3776ab">
|
||
|
||
<title>What’s New In Python 3.10 — Python 3.13.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
|
||
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=23252803" />
|
||
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
|
||
|
||
<script src="../_static/documentation_options.js?v=5d57ca2d"></script>
|
||
<script src="../_static/doctools.js?v=9bcbadda"></script>
|
||
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="Search within Python 3.13.3 documentation"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="About these documents" href="../about.html" />
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
||
<link rel="next" title="What’s New In Python 3.9" href="3.9.html" />
|
||
<link rel="prev" title="What’s New In Python 3.11" href="3.11.html" />
|
||
|
||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.10.html">
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
<script type="text/javascript" src="../_static/search-focus.js"></script>
|
||
<script type="text/javascript" src="../_static/themetoggle.js"></script>
|
||
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
|
||
<meta name="readthedocs-addons-api-version" content="1">
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<nav class="nav-content" role="navigation">
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<span class="nav-items-wrapper">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Python logo"/>
|
||
</a>
|
||
<span class="version_switcher_placeholder"></span>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
|
||
</svg>
|
||
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
|
||
<input type="submit" value="Go"/>
|
||
</form>
|
||
</span>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
|
||
<label class="theme-selector-label">
|
||
Theme
|
||
<select class="theme-selector" oninput="activateTheme(this.value)">
|
||
<option value="auto" selected>Auto</option>
|
||
<option value="light">Light</option>
|
||
<option value="dark">Dark</option>
|
||
</select>
|
||
</label>
|
||
<div>
|
||
<h3><a href="../contents.html">Table of Contents</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">What’s New In Python 3.10</a><ul>
|
||
<li><a class="reference internal" href="#summary-release-highlights">Summary – Release highlights</a></li>
|
||
<li><a class="reference internal" href="#new-features">New Features</a><ul>
|
||
<li><a class="reference internal" href="#parenthesized-context-managers">Parenthesized context managers</a></li>
|
||
<li><a class="reference internal" href="#better-error-messages">Better error messages</a><ul>
|
||
<li><a class="reference internal" href="#syntaxerrors">SyntaxErrors</a></li>
|
||
<li><a class="reference internal" href="#indentationerrors">IndentationErrors</a></li>
|
||
<li><a class="reference internal" href="#attributeerrors">AttributeErrors</a></li>
|
||
<li><a class="reference internal" href="#nameerrors">NameErrors</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#pep-626-precise-line-numbers-for-debugging-and-other-tools">PEP 626: Precise line numbers for debugging and other tools</a></li>
|
||
<li><a class="reference internal" href="#pep-634-structural-pattern-matching">PEP 634: Structural Pattern Matching</a><ul>
|
||
<li><a class="reference internal" href="#syntax-and-operations">Syntax and operations</a></li>
|
||
<li><a class="reference internal" href="#declarative-approach">Declarative approach</a></li>
|
||
<li><a class="reference internal" href="#simple-pattern-match-to-a-literal">Simple pattern: match to a literal</a><ul>
|
||
<li><a class="reference internal" href="#behavior-without-the-wildcard">Behavior without the wildcard</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#patterns-with-a-literal-and-variable">Patterns with a literal and variable</a></li>
|
||
<li><a class="reference internal" href="#patterns-and-classes">Patterns and classes</a><ul>
|
||
<li><a class="reference internal" href="#patterns-with-positional-parameters">Patterns with positional parameters</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#nested-patterns">Nested patterns</a></li>
|
||
<li><a class="reference internal" href="#complex-patterns-and-the-wildcard">Complex patterns and the wildcard</a></li>
|
||
<li><a class="reference internal" href="#guard">Guard</a></li>
|
||
<li><a class="reference internal" href="#other-key-features">Other Key Features</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optional-encodingwarning-and-encoding-locale-option">Optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> and <code class="docutils literal notranslate"><span class="pre">encoding="locale"</span></code> option</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#new-features-related-to-type-hints">New Features Related to Type Hints</a><ul>
|
||
<li><a class="reference internal" href="#pep-604-new-type-union-operator">PEP 604: New Type Union Operator</a></li>
|
||
<li><a class="reference internal" href="#pep-612-parameter-specification-variables">PEP 612: Parameter Specification Variables</a></li>
|
||
<li><a class="reference internal" href="#pep-613-typealias">PEP 613: TypeAlias</a></li>
|
||
<li><a class="reference internal" href="#pep-647-user-defined-type-guards">PEP 647: User-Defined Type Guards</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
|
||
<li><a class="reference internal" href="#new-modules">New Modules</a></li>
|
||
<li><a class="reference internal" href="#improved-modules">Improved Modules</a><ul>
|
||
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
|
||
<li><a class="reference internal" href="#argparse">argparse</a></li>
|
||
<li><a class="reference internal" href="#array">array</a></li>
|
||
<li><a class="reference internal" href="#asynchat-asyncore-smtpd">asynchat, asyncore, smtpd</a></li>
|
||
<li><a class="reference internal" href="#base64">base64</a></li>
|
||
<li><a class="reference internal" href="#bdb">bdb</a></li>
|
||
<li><a class="reference internal" href="#bisect">bisect</a></li>
|
||
<li><a class="reference internal" href="#codecs">codecs</a></li>
|
||
<li><a class="reference internal" href="#collections-abc">collections.abc</a></li>
|
||
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
|
||
<li><a class="reference internal" href="#curses">curses</a></li>
|
||
<li><a class="reference internal" href="#dataclasses">dataclasses</a><ul>
|
||
<li><a class="reference internal" href="#slots">__slots__</a></li>
|
||
<li><a class="reference internal" href="#keyword-only-fields">Keyword-only fields</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#distutils">distutils</a></li>
|
||
<li><a class="reference internal" href="#doctest">doctest</a></li>
|
||
<li><a class="reference internal" href="#encodings">encodings</a></li>
|
||
<li><a class="reference internal" href="#enum">enum</a></li>
|
||
<li><a class="reference internal" href="#fileinput">fileinput</a></li>
|
||
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
|
||
<li><a class="reference internal" href="#gc">gc</a></li>
|
||
<li><a class="reference internal" href="#glob">glob</a></li>
|
||
<li><a class="reference internal" href="#hashlib">hashlib</a></li>
|
||
<li><a class="reference internal" href="#hmac">hmac</a></li>
|
||
<li><a class="reference internal" href="#idle-and-idlelib">IDLE and idlelib</a></li>
|
||
<li><a class="reference internal" href="#importlib-metadata">importlib.metadata</a></li>
|
||
<li><a class="reference internal" href="#inspect">inspect</a></li>
|
||
<li><a class="reference internal" href="#itertools">itertools</a></li>
|
||
<li><a class="reference internal" href="#linecache">linecache</a></li>
|
||
<li><a class="reference internal" href="#os">os</a></li>
|
||
<li><a class="reference internal" href="#os-path">os.path</a></li>
|
||
<li><a class="reference internal" href="#pathlib">pathlib</a></li>
|
||
<li><a class="reference internal" href="#platform">platform</a></li>
|
||
<li><a class="reference internal" href="#pprint">pprint</a></li>
|
||
<li><a class="reference internal" href="#py-compile">py_compile</a></li>
|
||
<li><a class="reference internal" href="#pyclbr">pyclbr</a></li>
|
||
<li><a class="reference internal" href="#shelve">shelve</a></li>
|
||
<li><a class="reference internal" href="#statistics">statistics</a></li>
|
||
<li><a class="reference internal" href="#site">site</a></li>
|
||
<li><a class="reference internal" href="#socket">socket</a></li>
|
||
<li><a class="reference internal" href="#ssl">ssl</a></li>
|
||
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
|
||
<li><a class="reference internal" href="#sys">sys</a></li>
|
||
<li><a class="reference internal" href="#thread">_thread</a></li>
|
||
<li><a class="reference internal" href="#threading">threading</a></li>
|
||
<li><a class="reference internal" href="#traceback">traceback</a></li>
|
||
<li><a class="reference internal" href="#types">types</a></li>
|
||
<li><a class="reference internal" href="#typing">typing</a></li>
|
||
<li><a class="reference internal" href="#unittest">unittest</a></li>
|
||
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
|
||
<li><a class="reference internal" href="#xml">xml</a></li>
|
||
<li><a class="reference internal" href="#zipimport">zipimport</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
|
||
<li><a class="reference internal" href="#deprecated">Deprecated</a></li>
|
||
<li><a class="reference internal" href="#removed">Removed</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-10">Porting to Python 3.10</a><ul>
|
||
<li><a class="reference internal" href="#changes-in-the-python-syntax">Changes in the Python syntax</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-python-api">Changes in the Python API</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-c-api">Changes in the C API</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#cpython-bytecode-changes">CPython bytecode changes</a></li>
|
||
<li><a class="reference internal" href="#build-changes">Build Changes</a></li>
|
||
<li><a class="reference internal" href="#c-api-changes">C API Changes</a><ul>
|
||
<li><a class="reference internal" href="#pep-652-maintaining-the-stable-abi">PEP 652: Maintaining the Stable ABI</a></li>
|
||
<li><a class="reference internal" href="#id1">New Features</a></li>
|
||
<li><a class="reference internal" href="#id2">Porting to Python 3.10</a></li>
|
||
<li><a class="reference internal" href="#id3">Deprecated</a></li>
|
||
<li><a class="reference internal" href="#id4">Removed</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-10-7">Notable security feature in 3.10.7</a></li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-10-8">Notable security feature in 3.10.8</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-10-12">Notable changes in 3.10.12</a><ul>
|
||
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
<div>
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="3.11.html"
|
||
title="previous chapter">What’s New In Python 3.11</a></p>
|
||
</div>
|
||
<div>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="3.9.html"
|
||
title="next chapter">What’s New In Python 3.9</a></p>
|
||
</div>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/3.10.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
<div class="related" role="navigation" aria-label="Related">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
accesskey="I">index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.9.html" title="What’s New In Python 3.9"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.11.html" title="What’s New In Python 3.11"
|
||
accesskey="P">previous</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.13.3 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> »</li>
|
||
<li class="nav-item nav-item-this"><a href="">What’s New In Python 3.10</a></li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
|
||
<input type="submit" value="Go" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
<li class="right">
|
||
<label class="theme-selector-label">
|
||
Theme
|
||
<select class="theme-selector" oninput="activateTheme(this.value)">
|
||
<option value="auto" selected>Auto</option>
|
||
<option value="light">Light</option>
|
||
<option value="dark">Dark</option>
|
||
</select>
|
||
</label> |</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="what-s-new-in-python-3-10">
|
||
<h1>What’s New In Python 3.10<a class="headerlink" href="#what-s-new-in-python-3-10" title="Link to this heading">¶</a></h1>
|
||
<dl class="field-list simple">
|
||
<dt class="field-odd">Editor<span class="colon">:</span></dt>
|
||
<dd class="field-odd"><p>Pablo Galindo Salgado</p>
|
||
</dd>
|
||
</dl>
|
||
<p>This article explains the new features in Python 3.10, compared to 3.9.
|
||
Python 3.10 was released on October 4, 2021.
|
||
For full details, see the <a class="reference internal" href="changelog.html#changelog"><span class="std std-ref">changelog</span></a>.</p>
|
||
<section id="summary-release-highlights">
|
||
<h2>Summary – Release highlights<a class="headerlink" href="#summary-release-highlights" title="Link to this heading">¶</a></h2>
|
||
<p>New syntax features:</p>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0634/"><strong>PEP 634</strong></a>, Structural Pattern Matching: Specification</p></li>
|
||
<li><p><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0635/"><strong>PEP 635</strong></a>, Structural Pattern Matching: Motivation and Rationale</p></li>
|
||
<li><p><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0636/"><strong>PEP 636</strong></a>, Structural Pattern Matching: Tutorial</p></li>
|
||
<li><p><a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=12782">bpo-12782</a>, Parenthesized context managers are now officially allowed.</p></li>
|
||
</ul>
|
||
<p>New features in the standard library:</p>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0618/"><strong>PEP 618</strong></a>, Add Optional Length-Checking To zip.</p></li>
|
||
</ul>
|
||
<p>Interpreter improvements:</p>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0626/"><strong>PEP 626</strong></a>, Precise line numbers for debugging and other tools.</p></li>
|
||
</ul>
|
||
<p>New typing features:</p>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0604/"><strong>PEP 604</strong></a>, Allow writing union types as X | Y</p></li>
|
||
<li><p><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0612/"><strong>PEP 612</strong></a>, Parameter Specification Variables</p></li>
|
||
<li><p><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-0613/"><strong>PEP 613</strong></a>, Explicit Type Aliases</p></li>
|
||
<li><p><span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0647/"><strong>PEP 647</strong></a>, User-Defined Type Guards</p></li>
|
||
</ul>
|
||
<p>Important deprecations, removals or restrictions:</p>
|
||
<ul class="simple">
|
||
<li><p><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a>, Require OpenSSL 1.1.1 or newer</p></li>
|
||
<li><p><span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0632/"><strong>PEP 632</strong></a>, Deprecate distutils module.</p></li>
|
||
<li><p><span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0623/"><strong>PEP 623</strong></a>, Deprecate and prepare for the removal of the wstr member in PyUnicodeObject.</p></li>
|
||
<li><p><span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0624/"><strong>PEP 624</strong></a>, Remove Py_UNICODE encoder APIs</p></li>
|
||
<li><p><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0597/"><strong>PEP 597</strong></a>, Add optional EncodingWarning</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="new-features">
|
||
<h2>New Features<a class="headerlink" href="#new-features" title="Link to this heading">¶</a></h2>
|
||
<section id="parenthesized-context-managers">
|
||
<span id="whatsnew310-pep563"></span><h3>Parenthesized context managers<a class="headerlink" href="#parenthesized-context-managers" title="Link to this heading">¶</a></h3>
|
||
<p>Using enclosing parentheses for continuation across multiple lines
|
||
in context managers is now supported. This allows formatting a long
|
||
collection of context managers in multiple lines in a similar way
|
||
as it was previously possible with import statements. For instance,
|
||
all these examples are now valid:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="p">(</span><span class="n">CtxManager</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">with</span> <span class="p">(</span>
|
||
<span class="n">CtxManager1</span><span class="p">(),</span>
|
||
<span class="n">CtxManager2</span><span class="p">()</span>
|
||
<span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">with</span> <span class="p">(</span><span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">,</span>
|
||
<span class="n">CtxManager2</span><span class="p">()):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">with</span> <span class="p">(</span><span class="n">CtxManager1</span><span class="p">(),</span>
|
||
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="k">with</span> <span class="p">(</span>
|
||
<span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example1</span><span class="p">,</span>
|
||
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example2</span>
|
||
<span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>it is also possible to use a trailing comma at the end of the
|
||
enclosed group:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="p">(</span>
|
||
<span class="n">CtxManager1</span><span class="p">()</span> <span class="k">as</span> <span class="n">example1</span><span class="p">,</span>
|
||
<span class="n">CtxManager2</span><span class="p">()</span> <span class="k">as</span> <span class="n">example2</span><span class="p">,</span>
|
||
<span class="n">CtxManager3</span><span class="p">()</span> <span class="k">as</span> <span class="n">example3</span><span class="p">,</span>
|
||
<span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This new syntax uses the non LL(1) capacities of the new parser.
|
||
Check <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0617/"><strong>PEP 617</strong></a> for more details.</p>
|
||
<p>(Contributed by Guido van Rossum, Pablo Galindo and Lysandros Nikolaou
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=12782">bpo-12782</a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40334">bpo-40334</a>.)</p>
|
||
</section>
|
||
<section id="better-error-messages">
|
||
<h3>Better error messages<a class="headerlink" href="#better-error-messages" title="Link to this heading">¶</a></h3>
|
||
<section id="syntaxerrors">
|
||
<h4>SyntaxErrors<a class="headerlink" href="#syntaxerrors" title="Link to this heading">¶</a></h4>
|
||
<p>When parsing code that contains unclosed parentheses or brackets the interpreter
|
||
now includes the location of the unclosed bracket of parentheses instead of displaying
|
||
<em>SyntaxError: unexpected EOF while parsing</em> or pointing to some incorrect location.
|
||
For instance, consider the following code (notice the unclosed ‘{‘):</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">expected</span> <span class="o">=</span> <span class="p">{</span><span class="mi">9</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">18</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">27</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">28</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">29</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">36</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">37</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
|
||
<span class="mi">38</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">39</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">45</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">46</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">47</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">48</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">49</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">54</span><span class="p">:</span> <span class="mi">6</span><span class="p">,</span>
|
||
<span class="n">some_other_code</span> <span class="o">=</span> <span class="n">foo</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Previous versions of the interpreter reported confusing places as the location of
|
||
the syntax error:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"example.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">3</span>
|
||
<span class="n">some_other_code</span> <span class="o">=</span> <span class="n">foo</span><span class="p">()</span>
|
||
<span class="o">^</span>
|
||
<span class="ne">SyntaxError</span><span class="p">:</span> <span class="n">invalid</span> <span class="n">syntax</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>but in Python 3.10 a more informative error is emitted:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">File</span> <span class="s2">"example.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span>
|
||
<span class="n">expected</span> <span class="o">=</span> <span class="p">{</span><span class="mi">9</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">18</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">19</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">27</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">28</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">29</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">36</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">37</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
|
||
<span class="o">^</span>
|
||
<span class="ne">SyntaxError</span><span class="p">:</span> <span class="s1">'{'</span> <span class="n">was</span> <span class="n">never</span> <span class="n">closed</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In a similar way, errors involving unclosed string literals (single and triple
|
||
quoted) now point to the start of the string instead of reporting EOF/EOL.</p>
|
||
<p>These improvements are inspired by previous work in the PyPy interpreter.</p>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42864">bpo-42864</a> and Batuhan Taskaya in
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40176">bpo-40176</a>.)</p>
|
||
<p><a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exceptions raised by the interpreter will now highlight the
|
||
full error range of the expression that constitutes the syntax error itself,
|
||
instead of just where the problem is detected. In this way, instead of displaying
|
||
(before Python 3.10):</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">Generator expression must be parenthesized</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>now Python 3.10 will display the exception as:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">z</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">t</span><span class="p">,</span> <span class="n">w</span><span class="p">)</span>
|
||
<span class="w"> </span><span class="pm">^^^^^^^^^^^^^^^^^^^^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">Generator expression must be parenthesized</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This improvement was contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43914">bpo-43914</a>.</p>
|
||
<p>A considerable amount of new specialized messages for <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exceptions
|
||
have been incorporated. Some of the most notable ones are as follows:</p>
|
||
<ul>
|
||
<li><p>Missing <code class="docutils literal notranslate"><span class="pre">:</span></code> before blocks:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">></span> <span class="n">event_horizon</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">></span> <span class="n">event_horizon</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">expected ':'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42997">bpo-42997</a>.)</p>
|
||
</li>
|
||
<li><p>Unparenthesised tuples in comprehensions targets:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">{</span><span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'1234'</span><span class="p">)}</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="p">{</span><span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">'abcd'</span><span class="p">,</span> <span class="s1">'1234'</span><span class="p">)}</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">did you forget parentheses around the comprehension target?</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43017">bpo-43017</a>.)</p>
|
||
</li>
|
||
<li><p>Missing commas in collection literals and between expressions:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">items</span> <span class="o">=</span> <span class="p">{</span>
|
||
<span class="gp">... </span><span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
||
<span class="gp">... </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span>
|
||
<span class="gp">... </span><span class="n">z</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
|
||
<span class="w"> </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">invalid syntax. Perhaps you forgot a comma?</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43822">bpo-43822</a>.)</p>
|
||
</li>
|
||
<li><p>Multiple Exception types without parentheses:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="n">build_dyson_sphere</span><span class="p">()</span>
|
||
<span class="gp">... </span><span class="k">except</span> <span class="n">NotEnoughScienceError</span><span class="p">,</span> <span class="n">NotEnoughResourcesError</span><span class="p">:</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
|
||
<span class="w"> </span><span class="k">except</span> <span class="n">NotEnoughScienceError</span><span class="p">,</span> <span class="n">NotEnoughResourcesError</span><span class="p">:</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">multiple exception types must be parenthesized</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43149">bpo-43149</a>.)</p>
|
||
</li>
|
||
<li><p>Missing <code class="docutils literal notranslate"><span class="pre">:</span></code> and values in dictionary literals:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span>
|
||
<span class="gp">... </span><span class="n">x</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
||
<span class="gp">... </span><span class="n">y</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
|
||
<span class="gp">... </span><span class="n">z</span><span class="p">:</span>
|
||
<span class="gp">... </span><span class="p">}</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">4</span>
|
||
<span class="w"> </span><span class="n">z</span><span class="p">:</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">expression expected after dictionary key and ':'</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span> <span class="n">w</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="n">values</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="n">z</span> <span class="n">w</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">':' expected after dictionary key</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43823">bpo-43823</a>.)</p>
|
||
</li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">try</span></code> blocks without <code class="docutils literal notranslate"><span class="pre">except</span></code> or <code class="docutils literal notranslate"><span class="pre">finally</span></code> blocks:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
|
||
<span class="gp">... </span><span class="n">something</span> <span class="o">=</span> <span class="mi">3</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
|
||
<span class="w"> </span><span class="n">something</span> <span class="o">=</span> <span class="mi">3</span>
|
||
<span class="w"> </span><span class="pm">^^^^^^^^^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">expected 'except' or 'finally' block</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44305">bpo-44305</a>.)</p>
|
||
</li>
|
||
<li><p>Usage of <code class="docutils literal notranslate"><span class="pre">=</span></code> instead of <code class="docutils literal notranslate"><span class="pre">==</span></code> in comparisons:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">event_horizon</span><span class="p">:</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="k">if</span> <span class="n">rocket</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">event_horizon</span><span class="p">:</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">cannot assign to attribute here. Maybe you meant '==' instead of '='?</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43797">bpo-43797</a>.)</p>
|
||
</li>
|
||
<li><p>Usage of <code class="docutils literal notranslate"><span class="pre">*</span></code> in f-strings:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">f</span><span class="s2">"Black holes </span><span class="si">{</span><span class="o">*</span><span class="n">all_black_holes</span><span class="si">}</span><span class="s2"> and revelations"</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>
|
||
<span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">all_black_holes</span><span class="p">)</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">SyntaxError</span>: <span class="n">f-string: cannot use starred expression here</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41064">bpo-41064</a>.)</p>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
<section id="indentationerrors">
|
||
<h4>IndentationErrors<a class="headerlink" href="#indentationerrors" title="Link to this heading">¶</a></h4>
|
||
<p>Many <a class="reference internal" href="../library/exceptions.html#IndentationError" title="IndentationError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndentationError</span></code></a> exceptions now have more context regarding what kind of block
|
||
was expecting an indentation, including the location of the statement:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">foo</span><span class="p">():</span>
|
||
<span class="gp">... </span> <span class="k">if</span> <span class="n">lel</span><span class="p">:</span>
|
||
<span class="gp">... </span> <span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">3</span>
|
||
<span class="w"> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
|
||
<span class="w"> </span><span class="pm">^</span>
|
||
<span class="gr">IndentationError</span>: <span class="n">expected an indented block after 'if' statement in line 2</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="attributeerrors">
|
||
<h4>AttributeErrors<a class="headerlink" href="#attributeerrors" title="Link to this heading">¶</a></h4>
|
||
<p>When printing <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> will offer
|
||
suggestions of similar attribute names in the object that the exception was
|
||
raised from:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">collections</span><span class="o">.</span><span class="n">namedtoplo</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
<span class="gr">AttributeError</span>: <span class="n">module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38530">bpo-38530</a>.)</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">Warning</p>
|
||
<p>Notice this won’t work if <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> is not called to display the error
|
||
which can happen if some other custom error display function is used. This is a common
|
||
scenario in some REPLs like IPython.</p>
|
||
</div>
|
||
</section>
|
||
<section id="nameerrors">
|
||
<h4>NameErrors<a class="headerlink" href="#nameerrors" title="Link to this heading">¶</a></h4>
|
||
<p>When printing <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a> raised by the interpreter, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code>
|
||
will offer suggestions of similar variable names in the function that the exception
|
||
was raised from:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">schwarzschild_black_hole</span> <span class="o">=</span> <span class="kc">None</span>
|
||
<span class="gp">>>> </span><span class="n">schwarschild_black_hole</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
<span class="gr">NameError</span>: <span class="n">name 'schwarschild_black_hole' is not defined. Did you mean: schwarzschild_black_hole?</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38530">bpo-38530</a>.)</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">Warning</p>
|
||
<p>Notice this won’t work if <code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Display()</span></code> is not called to display the error,
|
||
which can happen if some other custom error display function is used. This is a common
|
||
scenario in some REPLs like IPython.</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="pep-626-precise-line-numbers-for-debugging-and-other-tools">
|
||
<h3>PEP 626: Precise line numbers for debugging and other tools<a class="headerlink" href="#pep-626-precise-line-numbers-for-debugging-and-other-tools" title="Link to this heading">¶</a></h3>
|
||
<p>PEP 626 brings more precise and reliable line numbers for debugging, profiling and coverage tools.
|
||
Tracing events, with the correct line number, are generated for all lines of code executed and only for lines of code that are executed.</p>
|
||
<p>The <a class="reference internal" href="../reference/datamodel.html#frame.f_lineno" title="frame.f_lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f_lineno</span></code></a> attribute of frame objects will always contain the
|
||
expected line number.</p>
|
||
<p>The <a class="reference internal" href="../reference/datamodel.html#codeobject.co_lnotab" title="codeobject.co_lnotab"><code class="xref py py-attr docutils literal notranslate"><span class="pre">co_lnotab</span></code></a> attribute of
|
||
<a class="reference internal" href="../reference/datamodel.html#code-objects"><span class="std std-ref">code objects</span></a> is deprecated and
|
||
will be removed in 3.12.
|
||
Code that needs to convert from offset to line number should use the new
|
||
<a class="reference internal" href="../reference/datamodel.html#codeobject.co_lines" title="codeobject.co_lines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">co_lines()</span></code></a> method instead.</p>
|
||
</section>
|
||
<section id="pep-634-structural-pattern-matching">
|
||
<h3>PEP 634: Structural Pattern Matching<a class="headerlink" href="#pep-634-structural-pattern-matching" title="Link to this heading">¶</a></h3>
|
||
<p>Structural pattern matching has been added in the form of a <em>match statement</em>
|
||
and <em>case statements</em> of patterns with associated actions. Patterns
|
||
consist of sequences, mappings, primitive data types as well as class instances.
|
||
Pattern matching enables programs to extract information from complex data types,
|
||
branch on the structure of data, and apply specific actions based on different
|
||
forms of data.</p>
|
||
<section id="syntax-and-operations">
|
||
<h4>Syntax and operations<a class="headerlink" href="#syntax-and-operations" title="Link to this heading">¶</a></h4>
|
||
<p>The generic syntax of pattern matching is:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">subject</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_1</span><span class="o">></span><span class="p">:</span>
|
||
<span class="o"><</span><span class="n">action_1</span><span class="o">></span>
|
||
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_2</span><span class="o">></span><span class="p">:</span>
|
||
<span class="o"><</span><span class="n">action_2</span><span class="o">></span>
|
||
<span class="k">case</span> <span class="o"><</span><span class="n">pattern_3</span><span class="o">></span><span class="p">:</span>
|
||
<span class="o"><</span><span class="n">action_3</span><span class="o">></span>
|
||
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
|
||
<span class="o"><</span><span class="n">action_wildcard</span><span class="o">></span>
|
||
</pre></div>
|
||
</div>
|
||
<p>A match statement takes an expression and compares its value to successive
|
||
patterns given as one or more case blocks. Specifically, pattern matching
|
||
operates by:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>using data with type and shape (the <code class="docutils literal notranslate"><span class="pre">subject</span></code>)</p></li>
|
||
<li><p>evaluating the <code class="docutils literal notranslate"><span class="pre">subject</span></code> in the <code class="docutils literal notranslate"><span class="pre">match</span></code> statement</p></li>
|
||
<li><p>comparing the subject with each pattern in a <code class="docutils literal notranslate"><span class="pre">case</span></code> statement
|
||
from top to bottom until a match is confirmed.</p></li>
|
||
<li><p>executing the action associated with the pattern of the confirmed
|
||
match</p></li>
|
||
<li><p>If an exact match is not confirmed, the last case, a wildcard <code class="docutils literal notranslate"><span class="pre">_</span></code>,
|
||
if provided, will be used as the matching case. If an exact match is
|
||
not confirmed and a wildcard case does not exist, the entire match
|
||
block is a no-op.</p></li>
|
||
</ol>
|
||
</section>
|
||
<section id="declarative-approach">
|
||
<h4>Declarative approach<a class="headerlink" href="#declarative-approach" title="Link to this heading">¶</a></h4>
|
||
<p>Readers may be aware of pattern matching through the simple example of matching
|
||
a subject (data object) to a literal (pattern) with the switch statement found
|
||
in C, Java or JavaScript (and many other languages). Often the switch statement
|
||
is used for comparison of an object/expression with case statements containing
|
||
literals.</p>
|
||
<p>More powerful examples of pattern matching can be found in languages such as
|
||
Scala and Elixir. With structural pattern matching, the approach is “declarative” and
|
||
explicitly states the conditions (the patterns) for data to match.</p>
|
||
<p>While an “imperative” series of instructions using nested “if” statements
|
||
could be used to accomplish something similar to structural pattern matching,
|
||
it is less clear than the “declarative” approach. Instead the “declarative”
|
||
approach states the conditions to meet for a match and is more readable through
|
||
its explicit patterns. While structural pattern matching can be used in its
|
||
simplest form comparing a variable to a literal in a case statement, its
|
||
true value for Python lies in its handling of the subject’s type and shape.</p>
|
||
</section>
|
||
<section id="simple-pattern-match-to-a-literal">
|
||
<h4>Simple pattern: match to a literal<a class="headerlink" href="#simple-pattern-match-to-a-literal" title="Link to this heading">¶</a></h4>
|
||
<p>Let’s look at this example as pattern matching in its simplest form: a value,
|
||
the subject, being matched to several literals, the patterns. In the example
|
||
below, <code class="docutils literal notranslate"><span class="pre">status</span></code> is the subject of the match statement. The patterns are
|
||
each of the case statements, where literals represent request status codes.
|
||
The associated action to the case is executed after a match:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">http_error</span><span class="p">(</span><span class="n">status</span><span class="p">):</span>
|
||
<span class="k">match</span> <span class="n">status</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="mi">400</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Bad request"</span>
|
||
<span class="k">case</span> <span class="mi">404</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Not found"</span>
|
||
<span class="k">case</span> <span class="mi">418</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"I'm a teapot"</span>
|
||
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Something's wrong with the internet"</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If the above function is passed a <code class="docutils literal notranslate"><span class="pre">status</span></code> of 418, “I’m a teapot” is returned.
|
||
If the above function is passed a <code class="docutils literal notranslate"><span class="pre">status</span></code> of 500, the case statement with
|
||
<code class="docutils literal notranslate"><span class="pre">_</span></code> will match as a wildcard, and “Something’s wrong with the internet” is
|
||
returned.
|
||
Note the last block: the variable name, <code class="docutils literal notranslate"><span class="pre">_</span></code>, acts as a <em>wildcard</em> and insures
|
||
the subject will always match. The use of <code class="docutils literal notranslate"><span class="pre">_</span></code> is optional.</p>
|
||
<p>You can combine several literals in a single pattern using <code class="docutils literal notranslate"><span class="pre">|</span></code> (“or”):</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">case</span> <span class="mi">401</span> <span class="o">|</span> <span class="mi">403</span> <span class="o">|</span> <span class="mi">404</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Not allowed"</span>
|
||
</pre></div>
|
||
</div>
|
||
<section id="behavior-without-the-wildcard">
|
||
<h5>Behavior without the wildcard<a class="headerlink" href="#behavior-without-the-wildcard" title="Link to this heading">¶</a></h5>
|
||
<p>If we modify the above example by removing the last case block, the example
|
||
becomes:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">http_error</span><span class="p">(</span><span class="n">status</span><span class="p">):</span>
|
||
<span class="k">match</span> <span class="n">status</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="mi">400</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Bad request"</span>
|
||
<span class="k">case</span> <span class="mi">404</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"Not found"</span>
|
||
<span class="k">case</span> <span class="mi">418</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="s2">"I'm a teapot"</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Without the use of <code class="docutils literal notranslate"><span class="pre">_</span></code> in a case statement, a match may not exist. If no
|
||
match exists, the behavior is a no-op. For example, if <code class="docutils literal notranslate"><span class="pre">status</span></code> of 500 is
|
||
passed, a no-op occurs.</p>
|
||
</section>
|
||
</section>
|
||
<section id="patterns-with-a-literal-and-variable">
|
||
<h4>Patterns with a literal and variable<a class="headerlink" href="#patterns-with-a-literal-and-variable" title="Link to this heading">¶</a></h4>
|
||
<p>Patterns can look like unpacking assignments, and a pattern may be used to bind
|
||
variables. In this example, a data point can be unpacked to its x-coordinate
|
||
and y-coordinate:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># point is an (x, y) tuple</span>
|
||
<span class="k">match</span> <span class="n">point</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Origin"</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">, Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
|
||
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
|
||
<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"Not a point"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The first pattern has two literals, <code class="docutils literal notranslate"><span class="pre">(0,</span> <span class="pre">0)</span></code>, and may be thought of as an
|
||
extension of the literal pattern shown above. The next two patterns combine a
|
||
literal and a variable, and the variable <em>binds</em> a value from the subject
|
||
(<code class="docutils literal notranslate"><span class="pre">point</span></code>). The fourth pattern captures two values, which makes it
|
||
conceptually similar to the unpacking assignment <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y)</span> <span class="pre">=</span> <span class="pre">point</span></code>.</p>
|
||
</section>
|
||
<section id="patterns-and-classes">
|
||
<h4>Patterns and classes<a class="headerlink" href="#patterns-and-classes" title="Link to this heading">¶</a></h4>
|
||
<p>If you are using classes to structure your data, you can use as a pattern
|
||
the class name followed by an argument list resembling a constructor. This
|
||
pattern has the ability to capture class attributes into variables:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Point</span><span class="p">:</span>
|
||
<span class="n">x</span><span class="p">:</span> <span class="nb">int</span>
|
||
<span class="n">y</span><span class="p">:</span> <span class="nb">int</span>
|
||
|
||
<span class="k">def</span><span class="w"> </span><span class="nf">location</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
|
||
<span class="k">match</span> <span class="n">point</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Origin is the point's location."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">y</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Y=</span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2"> and the point is on the y-axis."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"X=</span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2"> and the point is on the x-axis."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">():</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"The point is located somewhere else on the plane."</span><span class="p">)</span>
|
||
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Not a point"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<section id="patterns-with-positional-parameters">
|
||
<h5>Patterns with positional parameters<a class="headerlink" href="#patterns-with-positional-parameters" title="Link to this heading">¶</a></h5>
|
||
<p>You can use positional parameters with some builtin classes that provide an
|
||
ordering for their attributes (e.g. dataclasses). You can also define a specific
|
||
position for attributes in patterns by setting the <code class="docutils literal notranslate"><span class="pre">__match_args__</span></code> special
|
||
attribute in your classes. If it’s set to (“x”, “y”), the following patterns
|
||
are all equivalent (and all bind the <code class="docutils literal notranslate"><span class="pre">y</span></code> attribute to the <code class="docutils literal notranslate"><span class="pre">var</span></code> variable):</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">var</span><span class="p">)</span>
|
||
<span class="n">Point</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">)</span>
|
||
<span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">)</span>
|
||
<span class="n">Point</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">var</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="nested-patterns">
|
||
<h4>Nested patterns<a class="headerlink" href="#nested-patterns" title="Link to this heading">¶</a></h4>
|
||
<p>Patterns can be arbitrarily nested. For example, if our data is a short
|
||
list of points, it could be matched like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">points</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="p">[]:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"No points in the list."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">)]:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"The origin is the only point in the list."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)]:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"A single point </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">y</span><span class="si">}</span><span class="s2"> is in the list."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="p">[</span><span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y1</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">y2</span><span class="p">)]:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Two points on the Y axis at </span><span class="si">{</span><span class="n">y1</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span><span class="n">y2</span><span class="si">}</span><span class="s2"> are in the list."</span><span class="p">)</span>
|
||
<span class="k">case</span><span class="w"> </span><span class="k">_</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Something else is found in the list."</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="complex-patterns-and-the-wildcard">
|
||
<h4>Complex patterns and the wildcard<a class="headerlink" href="#complex-patterns-and-the-wildcard" title="Link to this heading">¶</a></h4>
|
||
<p>To this point, the examples have used <code class="docutils literal notranslate"><span class="pre">_</span></code> alone in the last case statement.
|
||
A wildcard can be used in more complex patterns, such as <code class="docutils literal notranslate"><span class="pre">('error',</span> <span class="pre">code,</span> <span class="pre">_)</span></code>.
|
||
For example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">test_variable</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="p">(</span><span class="s1">'warning'</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="mi">40</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"A warning has been received."</span><span class="p">)</span>
|
||
<span class="k">case</span><span class="w"> </span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="n">code</span><span class="p">,</span> <span class="k">_</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"An error </span><span class="si">{</span><span class="n">code</span><span class="si">}</span><span class="s2"> occurred."</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>In the above case, <code class="docutils literal notranslate"><span class="pre">test_variable</span></code> will match for (‘error’, code, 100) and
|
||
(‘error’, code, 800).</p>
|
||
</section>
|
||
<section id="guard">
|
||
<h4>Guard<a class="headerlink" href="#guard" title="Link to this heading">¶</a></h4>
|
||
<p>We can add an <code class="docutils literal notranslate"><span class="pre">if</span></code> clause to a pattern, known as a “guard”. If the
|
||
guard is false, <code class="docutils literal notranslate"><span class="pre">match</span></code> goes on to try the next case block. Note
|
||
that value capture happens before the guard is evaluated:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">match</span> <span class="n">point</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="n">y</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"The point is located on the diagonal Y=X at </span><span class="si">{</span><span class="n">x</span><span class="si">}</span><span class="s2">."</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"Point is not on the diagonal."</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="other-key-features">
|
||
<h4>Other Key Features<a class="headerlink" href="#other-key-features" title="Link to this heading">¶</a></h4>
|
||
<p>Several other key features:</p>
|
||
<ul>
|
||
<li><p>Like unpacking assignments, tuple and list patterns have exactly the
|
||
same meaning and actually match arbitrary sequences. Technically,
|
||
the subject must be a sequence.
|
||
Therefore, an important exception is that patterns don’t match iterators.
|
||
Also, to prevent a common mistake, sequence patterns don’t match strings.</p></li>
|
||
<li><p>Sequence patterns support wildcards: <code class="docutils literal notranslate"><span class="pre">[x,</span> <span class="pre">y,</span> <span class="pre">*rest]</span></code> and <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y,</span>
|
||
<span class="pre">*rest)</span></code> work similar to wildcards in unpacking assignments. The
|
||
name after <code class="docutils literal notranslate"><span class="pre">*</span></code> may also be <code class="docutils literal notranslate"><span class="pre">_</span></code>, so <code class="docutils literal notranslate"><span class="pre">(x,</span> <span class="pre">y,</span> <span class="pre">*_)</span></code> matches a sequence
|
||
of at least two items without binding the remaining items.</p></li>
|
||
<li><p>Mapping patterns: <code class="docutils literal notranslate"><span class="pre">{"bandwidth":</span> <span class="pre">b,</span> <span class="pre">"latency":</span> <span class="pre">l}</span></code> captures the
|
||
<code class="docutils literal notranslate"><span class="pre">"bandwidth"</span></code> and <code class="docutils literal notranslate"><span class="pre">"latency"</span></code> values from a dict. Unlike sequence
|
||
patterns, extra keys are ignored. A wildcard <code class="docutils literal notranslate"><span class="pre">**rest</span></code> is also
|
||
supported. (But <code class="docutils literal notranslate"><span class="pre">**_</span></code> would be redundant, so is not allowed.)</p></li>
|
||
<li><p>Subpatterns may be captured using the <code class="docutils literal notranslate"><span class="pre">as</span></code> keyword:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">case</span> <span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">y1</span><span class="p">),</span> <span class="n">Point</span><span class="p">(</span><span class="n">x2</span><span class="p">,</span> <span class="n">y2</span><span class="p">)</span> <span class="k">as</span> <span class="n">p2</span><span class="p">):</span> <span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This binds x1, y1, x2, y2 like you would expect without the <code class="docutils literal notranslate"><span class="pre">as</span></code> clause,
|
||
and p2 to the entire second item of the subject.</p>
|
||
</li>
|
||
<li><p>Most literals are compared by equality. However, the singletons <code class="docutils literal notranslate"><span class="pre">True</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">False</span></code> and <code class="docutils literal notranslate"><span class="pre">None</span></code> are compared by identity.</p></li>
|
||
<li><p>Named constants may be used in patterns. These named constants must be
|
||
dotted names to prevent the constant from being interpreted as a capture
|
||
variable:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">enum</span><span class="w"> </span><span class="kn">import</span> <span class="n">Enum</span>
|
||
<span class="k">class</span><span class="w"> </span><span class="nc">Color</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span>
|
||
<span class="n">RED</span> <span class="o">=</span> <span class="mi">0</span>
|
||
<span class="n">GREEN</span> <span class="o">=</span> <span class="mi">1</span>
|
||
<span class="n">BLUE</span> <span class="o">=</span> <span class="mi">2</span>
|
||
|
||
<span class="n">color</span> <span class="o">=</span> <span class="n">Color</span><span class="o">.</span><span class="n">GREEN</span>
|
||
<span class="k">match</span> <span class="n">color</span><span class="p">:</span>
|
||
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">RED</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"I see red!"</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">GREEN</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"Grass is green"</span><span class="p">)</span>
|
||
<span class="k">case</span> <span class="n">Color</span><span class="o">.</span><span class="n">BLUE</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="s2">"I'm feeling the blues :("</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
<p>For the full specification see <span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0634/"><strong>PEP 634</strong></a>. Motivation and rationale
|
||
are in <span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0635/"><strong>PEP 635</strong></a>, and a longer tutorial is in <span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0636/"><strong>PEP 636</strong></a>.</p>
|
||
</section>
|
||
</section>
|
||
<section id="optional-encodingwarning-and-encoding-locale-option">
|
||
<span id="whatsnew310-pep597"></span><h3>Optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> and <code class="docutils literal notranslate"><span class="pre">encoding="locale"</span></code> option<a class="headerlink" href="#optional-encodingwarning-and-encoding-locale-option" title="Link to this heading">¶</a></h3>
|
||
<p>The default encoding of <a class="reference internal" href="../library/io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> and <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> is
|
||
platform and locale dependent. Since UTF-8 is used on most Unix
|
||
platforms, omitting <code class="docutils literal notranslate"><span class="pre">encoding</span></code> option when opening UTF-8 files
|
||
(e.g. JSON, YAML, TOML, Markdown) is a very common bug. For example:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># BUG: "rb" mode or encoding="utf-8" should be used.</span>
|
||
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"data.json"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
||
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>To find this type of bug, an optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> is added.
|
||
It is emitted when <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags.warn_default_encoding</span></code></a>
|
||
is true and locale-specific default encoding is used.</p>
|
||
<p><code class="docutils literal notranslate"><span class="pre">-X</span> <span class="pre">warn_default_encoding</span></code> option and <span class="target" id="index-18"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNDEFAULTENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNDEFAULTENCODING</span></code></a>
|
||
are added to enable the warning.</p>
|
||
<p>See <a class="reference internal" href="../library/io.html#io-text-encoding"><span class="std std-ref">Text Encoding</span></a> for more information.</p>
|
||
</section>
|
||
</section>
|
||
<section id="new-features-related-to-type-hints">
|
||
<span id="new-feat-related-type-hints"></span><h2>New Features Related to Type Hints<a class="headerlink" href="#new-features-related-to-type-hints" title="Link to this heading">¶</a></h2>
|
||
<p>This section covers major changes affecting <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a> type hints and
|
||
the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module.</p>
|
||
<section id="pep-604-new-type-union-operator">
|
||
<h3>PEP 604: New Type Union Operator<a class="headerlink" href="#pep-604-new-type-union-operator" title="Link to this heading">¶</a></h3>
|
||
<p>A new type union operator was introduced which enables the syntax <code class="docutils literal notranslate"><span class="pre">X</span> <span class="pre">|</span> <span class="pre">Y</span></code>.
|
||
This provides a cleaner way of expressing ‘either type X or type Y’ instead of
|
||
using <a class="reference internal" href="../library/typing.html#typing.Union" title="typing.Union"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Union</span></code></a>, especially in type hints.</p>
|
||
<p>In previous versions of Python, to apply a type hint for functions accepting
|
||
arguments of multiple types, <a class="reference internal" href="../library/typing.html#typing.Union" title="typing.Union"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Union</span></code></a> was used:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">square</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">])</span> <span class="o">-></span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]:</span>
|
||
<span class="k">return</span> <span class="n">number</span> <span class="o">**</span> <span class="mi">2</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Type hints can now be written in a more succinct manner:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">square</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">float</span><span class="p">)</span> <span class="o">-></span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">float</span><span class="p">:</span>
|
||
<span class="k">return</span> <span class="n">number</span> <span class="o">**</span> <span class="mi">2</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This new syntax is also accepted as the second argument to <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><code class="xref py py-func docutils literal notranslate"><span class="pre">isinstance()</span></code></a>
|
||
and <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">issubclass()</span></code></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">int</span> <span class="o">|</span> <span class="nb">str</span><span class="p">)</span>
|
||
<span class="go">True</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>See <a class="reference internal" href="../library/stdtypes.html#types-union"><span class="std std-ref">Union Type</span></a> and <span class="target" id="index-20"></span><a class="pep reference external" href="https://peps.python.org/pep-0604/"><strong>PEP 604</strong></a> for more details.</p>
|
||
<p>(Contributed by Maggie Moss and Philippe Prados in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41428">bpo-41428</a>,
|
||
with additions by Yurii Karabas and Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44490">bpo-44490</a>.)</p>
|
||
</section>
|
||
<section id="pep-612-parameter-specification-variables">
|
||
<h3>PEP 612: Parameter Specification Variables<a class="headerlink" href="#pep-612-parameter-specification-variables" title="Link to this heading">¶</a></h3>
|
||
<p>Two new options to improve the information provided to static type checkers for
|
||
<span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a>‘s <code class="docutils literal notranslate"><span class="pre">Callable</span></code> have been added to the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module.</p>
|
||
<p>The first is the parameter specification variable. They are used to forward the
|
||
parameter types of one callable to another callable – a pattern commonly
|
||
found in higher order functions and decorators. Examples of usage can be found
|
||
in <a class="reference internal" href="../library/typing.html#typing.ParamSpec" title="typing.ParamSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpec</span></code></a>. Previously, there was no easy way to type annotate
|
||
dependency of parameter types in such a precise manner.</p>
|
||
<p>The second option is the new <code class="docutils literal notranslate"><span class="pre">Concatenate</span></code> operator. It’s used in conjunction
|
||
with parameter specification variables to type annotate a higher order callable
|
||
which adds or removes parameters of another callable. Examples of usage can
|
||
be found in <a class="reference internal" href="../library/typing.html#typing.Concatenate" title="typing.Concatenate"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Concatenate</span></code></a>.</p>
|
||
<p>See <a class="reference internal" href="../library/typing.html#typing.Callable" title="typing.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Callable</span></code></a>, <a class="reference internal" href="../library/typing.html#typing.ParamSpec" title="typing.ParamSpec"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpec</span></code></a>,
|
||
<a class="reference internal" href="../library/typing.html#typing.Concatenate" title="typing.Concatenate"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Concatenate</span></code></a>, <a class="reference internal" href="../library/typing.html#typing.ParamSpecArgs" title="typing.ParamSpecArgs"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpecArgs</span></code></a>,
|
||
<a class="reference internal" href="../library/typing.html#typing.ParamSpecKwargs" title="typing.ParamSpecKwargs"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.ParamSpecKwargs</span></code></a>, and <span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0612/"><strong>PEP 612</strong></a> for more details.</p>
|
||
<p>(Contributed by Ken Jin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41559">bpo-41559</a>, with minor enhancements by Jelle
|
||
Zijlstra in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43783">bpo-43783</a>. PEP written by Mark Mendoza.)</p>
|
||
</section>
|
||
<section id="pep-613-typealias">
|
||
<h3>PEP 613: TypeAlias<a class="headerlink" href="#pep-613-typealias" title="Link to this heading">¶</a></h3>
|
||
<p><span class="target" id="index-23"></span><a class="pep reference external" href="https://peps.python.org/pep-0484/"><strong>PEP 484</strong></a> introduced the concept of type aliases, only requiring them to be
|
||
top-level unannotated assignments. This simplicity sometimes made it difficult
|
||
for type checkers to distinguish between type aliases and ordinary assignments,
|
||
especially when forward references or invalid types were involved. Compare:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">StrCache</span> <span class="o">=</span> <span class="s1">'Cache[str]'</span> <span class="c1"># a type alias</span>
|
||
<span class="n">LOG_PREFIX</span> <span class="o">=</span> <span class="s1">'LOG[DEBUG]'</span> <span class="c1"># a module constant</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Now the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module has a special value <a class="reference internal" href="../library/typing.html#typing.TypeAlias" title="typing.TypeAlias"><code class="xref py py-data docutils literal notranslate"><span class="pre">TypeAlias</span></code></a>
|
||
which lets you declare type aliases more explicitly:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">StrCache</span><span class="p">:</span> <span class="n">TypeAlias</span> <span class="o">=</span> <span class="s1">'Cache[str]'</span> <span class="c1"># a type alias</span>
|
||
<span class="n">LOG_PREFIX</span> <span class="o">=</span> <span class="s1">'LOG[DEBUG]'</span> <span class="c1"># a module constant</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>See <span class="target" id="index-24"></span><a class="pep reference external" href="https://peps.python.org/pep-0613/"><strong>PEP 613</strong></a> for more details.</p>
|
||
<p>(Contributed by Mikhail Golubev in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41923">bpo-41923</a>.)</p>
|
||
</section>
|
||
<section id="pep-647-user-defined-type-guards">
|
||
<h3>PEP 647: User-Defined Type Guards<a class="headerlink" href="#pep-647-user-defined-type-guards" title="Link to this heading">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/typing.html#typing.TypeGuard" title="typing.TypeGuard"><code class="xref py py-data docutils literal notranslate"><span class="pre">TypeGuard</span></code></a> has been added to the <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> module to annotate
|
||
type guard functions and improve information provided to static type checkers
|
||
during type narrowing. For more information, please see
|
||
<a class="reference internal" href="../library/typing.html#typing.TypeGuard" title="typing.TypeGuard"><code class="xref py py-data docutils literal notranslate"><span class="pre">TypeGuard</span></code></a>‘s documentation, and <span class="target" id="index-25"></span><a class="pep reference external" href="https://peps.python.org/pep-0647/"><strong>PEP 647</strong></a>.</p>
|
||
<p>(Contributed by Ken Jin and Guido van Rossum in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43766">bpo-43766</a>.
|
||
PEP written by Eric Traut.)</p>
|
||
</section>
|
||
</section>
|
||
<section id="other-language-changes">
|
||
<h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Link to this heading">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> type has a new method <a class="reference internal" href="../library/stdtypes.html#int.bit_count" title="int.bit_count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">int.bit_count()</span></code></a>, returning the
|
||
number of ones in the binary expansion of a given integer, also known
|
||
as the population count. (Contributed by Niklas Fiekas in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29882">bpo-29882</a>.)</p></li>
|
||
<li><p>The views returned by <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.keys()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.values()</span></code></a> and
|
||
<a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.items()</span></code></a> now all have a <code class="docutils literal notranslate"><span class="pre">mapping</span></code> attribute that gives a
|
||
<a class="reference internal" href="../library/types.html#types.MappingProxyType" title="types.MappingProxyType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.MappingProxyType</span></code></a> object wrapping the original
|
||
dictionary. (Contributed by Dennis Sweeney in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40890">bpo-40890</a>.)</p></li>
|
||
<li><p><span class="target" id="index-26"></span><a class="pep reference external" href="https://peps.python.org/pep-0618/"><strong>PEP 618</strong></a>: The <a class="reference internal" href="../library/functions.html#zip" title="zip"><code class="xref py py-func docutils literal notranslate"><span class="pre">zip()</span></code></a> function now has an optional <code class="docutils literal notranslate"><span class="pre">strict</span></code> flag, used
|
||
to require that all the iterables have an equal length.</p></li>
|
||
<li><p>Builtin and extension functions that take integer arguments no longer accept
|
||
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>s, <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">Fraction</span></code></a>s and other
|
||
objects that can be converted to integers only with a loss (e.g. that have
|
||
the <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a> method but do not have the
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> method).
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37999">bpo-37999</a>.)</p></li>
|
||
<li><p>If <a class="reference internal" href="../reference/datamodel.html#object.__ipow__" title="object.__ipow__"><code class="xref py py-func docutils literal notranslate"><span class="pre">object.__ipow__()</span></code></a> returns <a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a>, the operator will
|
||
correctly fall back to <a class="reference internal" href="../reference/datamodel.html#object.__pow__" title="object.__pow__"><code class="xref py py-func docutils literal notranslate"><span class="pre">object.__pow__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__rpow__" title="object.__rpow__"><code class="xref py py-func docutils literal notranslate"><span class="pre">object.__rpow__()</span></code></a> as expected.
|
||
(Contributed by Alex Shkop in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38302">bpo-38302</a>.)</p></li>
|
||
<li><p>Assignment expressions can now be used unparenthesized within set literals
|
||
and set comprehensions, as well as in sequence indexes (but not slices).</p></li>
|
||
<li><p>Functions have a new <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> attribute which is used to look for
|
||
builtin symbols when a function is executed, instead of looking into
|
||
<code class="docutils literal notranslate"><span class="pre">__globals__['__builtins__']</span></code>. The attribute is initialized from
|
||
<code class="docutils literal notranslate"><span class="pre">__globals__["__builtins__"]</span></code> if it exists, else from the current builtins.
|
||
(Contributed by Mark Shannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42990">bpo-42990</a>.)</p></li>
|
||
<li><p>Two new builtin functions – <a class="reference internal" href="../library/functions.html#aiter" title="aiter"><code class="xref py py-func docutils literal notranslate"><span class="pre">aiter()</span></code></a> and <a class="reference internal" href="../library/functions.html#anext" title="anext"><code class="xref py py-func docutils literal notranslate"><span class="pre">anext()</span></code></a> have been added
|
||
to provide asynchronous counterparts to <a class="reference internal" href="../library/functions.html#iter" title="iter"><code class="xref py py-func docutils literal notranslate"><span class="pre">iter()</span></code></a> and <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a>,
|
||
respectively.
|
||
(Contributed by Joshua Bronson, Daniel Pope, and Justin Wang in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31861">bpo-31861</a>.)</p></li>
|
||
<li><p>Static methods (<a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">@staticmethod</span></code></a>) and class methods
|
||
(<a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">@classmethod</span></code></a>) now inherit the method attributes
|
||
(<code class="docutils literal notranslate"><span class="pre">__module__</span></code>, <code class="docutils literal notranslate"><span class="pre">__name__</span></code>, <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code>, <code class="docutils literal notranslate"><span class="pre">__doc__</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">__annotations__</span></code>) and have a new <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> attribute.
|
||
Moreover, static methods are now callable as regular functions.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43682">bpo-43682</a>.)</p></li>
|
||
<li><p>Annotations for complex targets (everything beside <code class="docutils literal notranslate"><span class="pre">simple</span> <span class="pre">name</span></code> targets
|
||
defined by <span class="target" id="index-27"></span><a class="pep reference external" href="https://peps.python.org/pep-0526/"><strong>PEP 526</strong></a>) no longer cause any runtime effects with <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>.
|
||
(Contributed by Batuhan Taskaya in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42737">bpo-42737</a>.)</p></li>
|
||
<li><p>Class and module objects now lazy-create empty annotations dicts on demand.
|
||
The annotations dicts are stored in the object’s <code class="docutils literal notranslate"><span class="pre">__dict__</span></code> for
|
||
backwards compatibility. This improves the best practices for working
|
||
with <code class="docutils literal notranslate"><span class="pre">__annotations__</span></code>; for more information, please see
|
||
<a class="reference internal" href="../howto/annotations.html#annotations-howto"><span class="std std-ref">Annotations Best Practices</span></a>.
|
||
(Contributed by Larry Hastings in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43901">bpo-43901</a>.)</p></li>
|
||
<li><p>Annotations consist of <code class="docutils literal notranslate"><span class="pre">yield</span></code>, <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code>, <code class="docutils literal notranslate"><span class="pre">await</span></code> or named expressions
|
||
are now forbidden under <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code> due to their side
|
||
effects.
|
||
(Contributed by Batuhan Taskaya in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42725">bpo-42725</a>.)</p></li>
|
||
<li><p>Usage of unbound variables, <code class="docutils literal notranslate"><span class="pre">super()</span></code> and other expressions that might
|
||
alter the processing of symbol table as annotations are now rendered
|
||
effectless under <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>.
|
||
(Contributed by Batuhan Taskaya in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42725">bpo-42725</a>.)</p></li>
|
||
<li><p>Hashes of NaN values of both <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> type and
|
||
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a> type now depend on object identity. Formerly, they
|
||
always hashed to <code class="docutils literal notranslate"><span class="pre">0</span></code> even though NaN values are not equal to one another.
|
||
This caused potentially quadratic runtime behavior due to excessive hash
|
||
collisions when creating dictionaries and sets containing multiple NaNs.
|
||
(Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43475">bpo-43475</a>.)</p></li>
|
||
<li><p>A <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> (instead of a <a class="reference internal" href="../library/exceptions.html#NameError" title="NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a>) will be raised when deleting
|
||
the <a class="reference internal" href="../library/constants.html#debug__" title="__debug__"><code class="xref py py-const docutils literal notranslate"><span class="pre">__debug__</span></code></a> constant. (Contributed by Donghee Na in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=45000">bpo-45000</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> exceptions now have <code class="docutils literal notranslate"><span class="pre">end_lineno</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">end_offset</span></code> attributes. They will be <code class="docutils literal notranslate"><span class="pre">None</span></code> if not determined.
|
||
(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43914">bpo-43914</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="new-modules">
|
||
<h2>New Modules<a class="headerlink" href="#new-modules" title="Link to this heading">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>None.</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="improved-modules">
|
||
<h2>Improved Modules<a class="headerlink" href="#improved-modules" title="Link to this heading">¶</a></h2>
|
||
<section id="asyncio">
|
||
<h3>asyncio<a class="headerlink" href="#asyncio" title="Link to this heading">¶</a></h3>
|
||
<p>Add missing <code class="xref py py-meth docutils literal notranslate"><span class="pre">connect_accepted_socket()</span></code>
|
||
method.
|
||
(Contributed by Alex Grönholm in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41332">bpo-41332</a>.)</p>
|
||
</section>
|
||
<section id="argparse">
|
||
<h3>argparse<a class="headerlink" href="#argparse" title="Link to this heading">¶</a></h3>
|
||
<p>Misleading phrase “optional arguments” was replaced with “options” in argparse help. Some tests might require adaptation if they rely on exact output match.
|
||
(Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=9694">bpo-9694</a>.)</p>
|
||
</section>
|
||
<section id="array">
|
||
<h3>array<a class="headerlink" href="#array" title="Link to this heading">¶</a></h3>
|
||
<p>The <a class="reference internal" href="../library/array.html#array.array.index" title="array.array.index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">index()</span></code></a> method of <a class="reference internal" href="../library/array.html#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array.array</span></code></a> now has
|
||
optional <em>start</em> and <em>stop</em> parameters.
|
||
(Contributed by Anders Lorentsen and Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31956">bpo-31956</a>.)</p>
|
||
</section>
|
||
<section id="asynchat-asyncore-smtpd">
|
||
<h3>asynchat, asyncore, smtpd<a class="headerlink" href="#asynchat-asyncore-smtpd" title="Link to this heading">¶</a></h3>
|
||
<p>These modules have been marked as deprecated in their module documentation
|
||
since Python 3.6. An import-time <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-class docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> has now been
|
||
added to all three of these modules.</p>
|
||
</section>
|
||
<section id="base64">
|
||
<h3>base64<a class="headerlink" href="#base64" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/base64.html#base64.b32hexencode" title="base64.b32hexencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">base64.b32hexencode()</span></code></a> and <a class="reference internal" href="../library/base64.html#base64.b32hexdecode" title="base64.b32hexdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">base64.b32hexdecode()</span></code></a> to support the
|
||
Base32 Encoding with Extended Hex Alphabet.</p>
|
||
</section>
|
||
<section id="bdb">
|
||
<h3>bdb<a class="headerlink" href="#bdb" title="Link to this heading">¶</a></h3>
|
||
<p>Add <code class="xref py py-meth docutils literal notranslate"><span class="pre">clearBreakpoints()</span></code> to reset all set breakpoints.
|
||
(Contributed by Irit Katriel in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=24160">bpo-24160</a>.)</p>
|
||
</section>
|
||
<section id="bisect">
|
||
<h3>bisect<a class="headerlink" href="#bisect" title="Link to this heading">¶</a></h3>
|
||
<p>Added the possibility of providing a <em>key</em> function to the APIs in the <a class="reference internal" href="../library/bisect.html#module-bisect" title="bisect: Array bisection algorithms for binary searching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code></a>
|
||
module. (Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4356">bpo-4356</a>.)</p>
|
||
</section>
|
||
<section id="codecs">
|
||
<h3>codecs<a class="headerlink" href="#codecs" title="Link to this heading">¶</a></h3>
|
||
<p>Add a <a class="reference internal" href="../library/codecs.html#codecs.unregister" title="codecs.unregister"><code class="xref py py-func docutils literal notranslate"><span class="pre">codecs.unregister()</span></code></a> function to unregister a codec search function.
|
||
(Contributed by Hai Shi in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41842">bpo-41842</a>.)</p>
|
||
</section>
|
||
<section id="collections-abc">
|
||
<h3>collections.abc<a class="headerlink" href="#collections-abc" title="Link to this heading">¶</a></h3>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">__args__</span></code> of the <a class="reference internal" href="../library/stdtypes.html#types-genericalias"><span class="std std-ref">parameterized generic</span></a> for
|
||
<a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> are now consistent with <a class="reference internal" href="../library/typing.html#typing.Callable" title="typing.Callable"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Callable</span></code></a>.
|
||
<a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> generic now flattens type parameters, similar
|
||
to what <a class="reference internal" href="../library/typing.html#typing.Callable" title="typing.Callable"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Callable</span></code></a> currently does. This means that
|
||
<code class="docutils literal notranslate"><span class="pre">collections.abc.Callable[[int,</span> <span class="pre">str],</span> <span class="pre">str]</span></code> will have <code class="docutils literal notranslate"><span class="pre">__args__</span></code> of
|
||
<code class="docutils literal notranslate"><span class="pre">(int,</span> <span class="pre">str,</span> <span class="pre">str)</span></code>; previously this was <code class="docutils literal notranslate"><span class="pre">([int,</span> <span class="pre">str],</span> <span class="pre">str)</span></code>. To allow this
|
||
change, <a class="reference internal" href="../library/types.html#types.GenericAlias" title="types.GenericAlias"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.GenericAlias</span></code></a> can now be subclassed, and a subclass will
|
||
be returned when subscripting the <a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> type. Note
|
||
that a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> may be raised for invalid forms of parameterizing
|
||
<a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> which may have passed silently in Python 3.9.
|
||
(Contributed by Ken Jin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42195">bpo-42195</a>.)</p>
|
||
</section>
|
||
<section id="contextlib">
|
||
<h3>contextlib<a class="headerlink" href="#contextlib" title="Link to this heading">¶</a></h3>
|
||
<p>Add a <a class="reference internal" href="../library/contextlib.html#contextlib.aclosing" title="contextlib.aclosing"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.aclosing()</span></code></a> context manager to safely close async generators
|
||
and objects representing asynchronously released resources.
|
||
(Contributed by Joongi Kim and John Belmonte in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41229">bpo-41229</a>.)</p>
|
||
<p>Add asynchronous context manager support to <a class="reference internal" href="../library/contextlib.html#contextlib.nullcontext" title="contextlib.nullcontext"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nullcontext()</span></code></a>.
|
||
(Contributed by Tom Gringauz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41543">bpo-41543</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/contextlib.html#contextlib.AsyncContextDecorator" title="contextlib.AsyncContextDecorator"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncContextDecorator</span></code></a>, for supporting usage of async
|
||
context managers as decorators.</p>
|
||
</section>
|
||
<section id="curses">
|
||
<h3>curses<a class="headerlink" href="#curses" title="Link to this heading">¶</a></h3>
|
||
<p>The extended color functions added in ncurses 6.1 will be used transparently
|
||
by <a class="reference internal" href="../library/curses.html#curses.color_content" title="curses.color_content"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.color_content()</span></code></a>, <a class="reference internal" href="../library/curses.html#curses.init_color" title="curses.init_color"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.init_color()</span></code></a>,
|
||
<a class="reference internal" href="../library/curses.html#curses.init_pair" title="curses.init_pair"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.init_pair()</span></code></a>, and <a class="reference internal" href="../library/curses.html#curses.pair_content" title="curses.pair_content"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.pair_content()</span></code></a>. A new function,
|
||
<a class="reference internal" href="../library/curses.html#curses.has_extended_color_support" title="curses.has_extended_color_support"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.has_extended_color_support()</span></code></a>, indicates whether extended color
|
||
support is provided by the underlying ncurses library.
|
||
(Contributed by Jeffrey Kintscher and Hans Petter Jansson in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36982">bpo-36982</a>.)</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">BUTTON5_*</span></code> constants are now exposed in the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module if
|
||
they are provided by the underlying curses library.
|
||
(Contributed by Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39273">bpo-39273</a>.)</p>
|
||
</section>
|
||
<section id="dataclasses">
|
||
<h3>dataclasses<a class="headerlink" href="#dataclasses" title="Link to this heading">¶</a></h3>
|
||
<section id="slots">
|
||
<h4>__slots__<a class="headerlink" href="#slots" title="Link to this heading">¶</a></h4>
|
||
<p>Added <code class="docutils literal notranslate"><span class="pre">slots</span></code> parameter in <a class="reference internal" href="../library/dataclasses.html#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">dataclasses.dataclass()</span></code></a> decorator.
|
||
(Contributed by Yurii Karabas in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42269">bpo-42269</a>)</p>
|
||
</section>
|
||
<section id="keyword-only-fields">
|
||
<h4>Keyword-only fields<a class="headerlink" href="#keyword-only-fields" title="Link to this heading">¶</a></h4>
|
||
<p>dataclasses now supports fields that are keyword-only in the
|
||
generated __init__ method. There are a number of ways of specifying
|
||
keyword-only fields.</p>
|
||
<p>You can say that every field is keyword-only:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">dataclasses</span><span class="w"> </span><span class="kn">import</span> <span class="n">dataclass</span>
|
||
|
||
<span class="nd">@dataclass</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
<span class="k">class</span><span class="w"> </span><span class="nc">Birthday</span><span class="p">:</span>
|
||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||
<span class="n">birthday</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Both <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">birthday</span></code> are keyword-only parameters to the
|
||
generated __init__ method.</p>
|
||
<p>You can specify keyword-only on a per-field basis:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">dataclasses</span><span class="w"> </span><span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">field</span>
|
||
|
||
<span class="nd">@dataclass</span>
|
||
<span class="k">class</span><span class="w"> </span><span class="nc">Birthday</span><span class="p">:</span>
|
||
<span class="n">name</span><span class="p">:</span> <span class="nb">str</span>
|
||
<span class="n">birthday</span><span class="p">:</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">field</span><span class="p">(</span><span class="n">kw_only</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Here only <code class="docutils literal notranslate"><span class="pre">birthday</span></code> is keyword-only. If you set <code class="docutils literal notranslate"><span class="pre">kw_only</span></code> on
|
||
individual fields, be aware that there are rules about re-ordering
|
||
fields due to keyword-only fields needing to follow non-keyword-only
|
||
fields. See the full dataclasses documentation for details.</p>
|
||
<p>You can also specify that all fields following a KW_ONLY marker are
|
||
keyword-only. This will probably be the most common usage:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">dataclasses</span><span class="w"> </span><span class="kn">import</span> <span class="n">dataclass</span><span class="p">,</span> <span class="n">KW_ONLY</span>
|
||
|
||
<span class="nd">@dataclass</span>
|
||
<span class="k">class</span><span class="w"> </span><span class="nc">Point</span><span class="p">:</span>
|
||
<span class="n">x</span><span class="p">:</span> <span class="nb">float</span>
|
||
<span class="n">y</span><span class="p">:</span> <span class="nb">float</span>
|
||
<span class="n">_</span><span class="p">:</span> <span class="n">KW_ONLY</span>
|
||
<span class="n">z</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.0</span>
|
||
<span class="n">t</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.0</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Here, <code class="docutils literal notranslate"><span class="pre">z</span></code> and <code class="docutils literal notranslate"><span class="pre">t</span></code> are keyword-only parameters, while <code class="docutils literal notranslate"><span class="pre">x</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">y</span></code> are not.
|
||
(Contributed by Eric V. Smith in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43532">bpo-43532</a>.)</p>
|
||
</section>
|
||
</section>
|
||
<section id="distutils">
|
||
<span id="distutils-deprecated"></span><h3>distutils<a class="headerlink" href="#distutils" title="Link to this heading">¶</a></h3>
|
||
<p>The entire <code class="docutils literal notranslate"><span class="pre">distutils</span></code> package is deprecated, to be removed in Python
|
||
3.12. Its functionality for specifying package builds has already been
|
||
completely replaced by third-party packages <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">packaging</span></code>, and most other commonly used APIs are available elsewhere
|
||
in the standard library (such as <a class="reference internal" href="../library/platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code class="xref py py-mod docutils literal notranslate"><span class="pre">platform</span></code></a>, <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a>,
|
||
<a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> or <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a>). There are no plans to migrate
|
||
any other functionality from <code class="docutils literal notranslate"><span class="pre">distutils</span></code>, and applications that are
|
||
using other functions should plan to make private copies of the code.
|
||
Refer to <span class="target" id="index-28"></span><a class="pep reference external" href="https://peps.python.org/pep-0632/"><strong>PEP 632</strong></a> for discussion.</p>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">bdist_wininst</span></code> command deprecated in Python 3.8 has been removed.
|
||
The <code class="docutils literal notranslate"><span class="pre">bdist_wheel</span></code> command is now recommended to distribute binary packages
|
||
on Windows.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42802">bpo-42802</a>.)</p>
|
||
</section>
|
||
<section id="doctest">
|
||
<h3>doctest<a class="headerlink" href="#doctest" title="Link to this heading">¶</a></h3>
|
||
<p>When a module does not define <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, fall back to <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42133">bpo-42133</a>.)</p>
|
||
</section>
|
||
<section id="encodings">
|
||
<h3>encodings<a class="headerlink" href="#encodings" title="Link to this heading">¶</a></h3>
|
||
<p><code class="xref py py-func docutils literal notranslate"><span class="pre">encodings.normalize_encoding()</span></code> now ignores non-ASCII characters.
|
||
(Contributed by Hai Shi in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39337">bpo-39337</a>.)</p>
|
||
</section>
|
||
<section id="enum">
|
||
<h3>enum<a class="headerlink" href="#enum" title="Link to this heading">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/enum.html#enum.Enum" title="enum.Enum"><code class="xref py py-class docutils literal notranslate"><span class="pre">Enum</span></code></a> <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__repr__()</span></code></a> now returns <code class="docutils literal notranslate"><span class="pre">enum_name.member_name</span></code> and
|
||
<a class="reference internal" href="../reference/datamodel.html#object.__str__" title="object.__str__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__str__()</span></code></a> now returns <code class="docutils literal notranslate"><span class="pre">member_name</span></code>. Stdlib enums available as
|
||
module constants have a <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of <code class="docutils literal notranslate"><span class="pre">module_name.member_name</span></code>.
|
||
(Contributed by Ethan Furman in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40066">bpo-40066</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/enum.html#enum.StrEnum" title="enum.StrEnum"><code class="xref py py-class docutils literal notranslate"><span class="pre">enum.StrEnum</span></code></a> for enums where all members are strings.
|
||
(Contributed by Ethan Furman in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41816">bpo-41816</a>.)</p>
|
||
</section>
|
||
<section id="fileinput">
|
||
<h3>fileinput<a class="headerlink" href="#fileinput" title="Link to this heading">¶</a></h3>
|
||
<p>Add <em>encoding</em> and <em>errors</em> parameters in <a class="reference internal" href="../library/fileinput.html#fileinput.input" title="fileinput.input"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileinput.input()</span></code></a> and
|
||
<a class="reference internal" href="../library/fileinput.html#fileinput.FileInput" title="fileinput.FileInput"><code class="xref py py-class docutils literal notranslate"><span class="pre">fileinput.FileInput</span></code></a>.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43712">bpo-43712</a>.)</p>
|
||
<p><a class="reference internal" href="../library/fileinput.html#fileinput.hook_compressed" title="fileinput.hook_compressed"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileinput.hook_compressed()</span></code></a> now returns <a class="reference internal" href="../library/io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> object
|
||
when <em>mode</em> is “r” and file is compressed, like uncompressed files.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5758">bpo-5758</a>.)</p>
|
||
</section>
|
||
<section id="faulthandler">
|
||
<h3>faulthandler<a class="headerlink" href="#faulthandler" title="Link to this heading">¶</a></h3>
|
||
<p>The <a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a> module now detects if a fatal error occurs during a
|
||
garbage collector collection.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44466">bpo-44466</a>.)</p>
|
||
</section>
|
||
<section id="gc">
|
||
<h3>gc<a class="headerlink" href="#gc" title="Link to this heading">¶</a></h3>
|
||
<p>Add audit hooks for <a class="reference internal" href="../library/gc.html#gc.get_objects" title="gc.get_objects"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.get_objects()</span></code></a>, <a class="reference internal" href="../library/gc.html#gc.get_referrers" title="gc.get_referrers"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.get_referrers()</span></code></a> and
|
||
<a class="reference internal" href="../library/gc.html#gc.get_referents" title="gc.get_referents"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.get_referents()</span></code></a>. (Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43439">bpo-43439</a>.)</p>
|
||
</section>
|
||
<section id="glob">
|
||
<h3>glob<a class="headerlink" href="#glob" title="Link to this heading">¶</a></h3>
|
||
<p>Add the <em>root_dir</em> and <em>dir_fd</em> parameters in <a class="reference internal" href="../library/glob.html#glob.glob" title="glob.glob"><code class="xref py py-func docutils literal notranslate"><span class="pre">glob()</span></code></a> and
|
||
<a class="reference internal" href="../library/glob.html#glob.iglob" title="glob.iglob"><code class="xref py py-func docutils literal notranslate"><span class="pre">iglob()</span></code></a> which allow to specify the root directory for searching.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38144">bpo-38144</a>.)</p>
|
||
</section>
|
||
<section id="hashlib">
|
||
<h3>hashlib<a class="headerlink" href="#hashlib" title="Link to this heading">¶</a></h3>
|
||
<p>The hashlib module requires OpenSSL 1.1.1 or newer.
|
||
(Contributed by Christian Heimes in <span class="target" id="index-29"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43669">bpo-43669</a>.)</p>
|
||
<p>The hashlib module has preliminary support for OpenSSL 3.0.0.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38820">bpo-38820</a> and other issues.)</p>
|
||
<p>The pure-Python fallback of <a class="reference internal" href="../library/hashlib.html#hashlib.pbkdf2_hmac" title="hashlib.pbkdf2_hmac"><code class="xref py py-func docutils literal notranslate"><span class="pre">pbkdf2_hmac()</span></code></a> is deprecated. In
|
||
the future PBKDF2-HMAC will only be available when Python has been built with
|
||
OpenSSL support.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43880">bpo-43880</a>.)</p>
|
||
</section>
|
||
<section id="hmac">
|
||
<h3>hmac<a class="headerlink" href="#hmac" title="Link to this heading">¶</a></h3>
|
||
<p>The hmac module now uses OpenSSL’s HMAC implementation internally.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40645">bpo-40645</a>.)</p>
|
||
</section>
|
||
<section id="idle-and-idlelib">
|
||
<h3>IDLE and idlelib<a class="headerlink" href="#idle-and-idlelib" title="Link to this heading">¶</a></h3>
|
||
<p>Make IDLE invoke <a class="reference internal" href="../library/sys.html#sys.excepthook" title="sys.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.excepthook()</span></code></a> (when started without ‘-n’).
|
||
User hooks were previously ignored. (Contributed by Ken Hilton in
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43008">bpo-43008</a>.)</p>
|
||
<p>Rearrange the settings dialog. Split the General tab into Windows
|
||
and Shell/Ed tabs. Move help sources, which extend the Help menu, to the
|
||
Extensions tab. Make space for new options and shorten the dialog. The
|
||
latter makes the dialog better fit small screens. (Contributed by Terry Jan
|
||
Reedy in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40468">bpo-40468</a>.) Move the indent space setting from the Font tab to
|
||
the new Windows tab. (Contributed by Mark Roseman and Terry Jan Reedy in
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33962">bpo-33962</a>.)</p>
|
||
<p>The changes above were backported to a 3.9 maintenance release.</p>
|
||
<p>Add a Shell sidebar. Move the primary prompt (‘>>>’) to the sidebar.
|
||
Add secondary prompts (’…’) to the sidebar. Left click and optional
|
||
drag selects one or more lines of text, as with the editor
|
||
line number sidebar. Right click after selecting text lines displays
|
||
a context menu with ‘copy with prompts’. This zips together prompts
|
||
from the sidebar with lines from the selected text. This option also
|
||
appears on the context menu for the text. (Contributed by Tal Einat
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37903">bpo-37903</a>.)</p>
|
||
<p>Use spaces instead of tabs to indent interactive code. This makes
|
||
interactive code entries ‘look right’. Making this feasible was a
|
||
major motivation for adding the shell sidebar. (Contributed by
|
||
Terry Jan Reedy in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37892">bpo-37892</a>.)</p>
|
||
<p>Highlight the new <a class="reference internal" href="../reference/lexical_analysis.html#soft-keywords"><span class="std std-ref">soft keywords</span></a> <a class="reference internal" href="../reference/compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a>,
|
||
<a class="reference internal" href="../reference/compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">case</span></code></a>, and <a class="reference internal" href="../reference/compound_stmts.html#wildcard-patterns"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">_</span></code></a> in
|
||
pattern-matching statements. However, this highlighting is not perfect
|
||
and will be incorrect in some rare cases, including some <code class="docutils literal notranslate"><span class="pre">_</span></code>-s in
|
||
<code class="docutils literal notranslate"><span class="pre">case</span></code> patterns. (Contributed by Tal Einat in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44010">bpo-44010</a>.)</p>
|
||
<p>New in 3.10 maintenance releases.</p>
|
||
<p>Apply syntax highlighting to <code class="docutils literal notranslate"><span class="pre">.pyi</span></code> files. (Contributed by Alex
|
||
Waygood and Terry Jan Reedy in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=45447">bpo-45447</a>.)</p>
|
||
<p>Include prompts when saving Shell with inputs and outputs.
|
||
(Contributed by Terry Jan Reedy in <a class="reference external" href="https://github.com/python/cpython/issues/95191">gh-95191</a>.)</p>
|
||
</section>
|
||
<section id="importlib-metadata">
|
||
<h3>importlib.metadata<a class="headerlink" href="#importlib-metadata" title="Link to this heading">¶</a></h3>
|
||
<p>Feature parity with <code class="docutils literal notranslate"><span class="pre">importlib_metadata</span></code> 4.6
|
||
(<a class="reference external" href="https://importlib-metadata.readthedocs.io/en/latest/history.html">history</a>).</p>
|
||
<p><a class="reference internal" href="../library/importlib.metadata.html#entry-points"><span class="std std-ref">importlib.metadata entry points</span></a>
|
||
now provide a nicer experience
|
||
for selecting entry points by group and name through a new
|
||
<a class="reference internal" href="../library/importlib.metadata.html#entry-points"><span class="std std-ref">importlib.metadata.EntryPoints</span></a> class. See the Compatibility
|
||
Note in the docs for more info on the deprecation and usage.</p>
|
||
<p>Added <a class="reference internal" href="../library/importlib.metadata.html#package-distributions"><span class="std std-ref">importlib.metadata.packages_distributions()</span></a>
|
||
for resolving top-level Python modules and packages to their
|
||
<a class="reference internal" href="../library/importlib.metadata.html#distributions"><span class="std std-ref">importlib.metadata.Distribution</span></a>.</p>
|
||
</section>
|
||
<section id="inspect">
|
||
<h3>inspect<a class="headerlink" href="#inspect" title="Link to this heading">¶</a></h3>
|
||
<p>When a module does not define <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, fall back to <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42133">bpo-42133</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/inspect.html#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a>, which safely computes the annotations
|
||
defined on an object. It works around the quirks of accessing the annotations
|
||
on various types of objects, and makes very few assumptions about the object
|
||
it examines. <a class="reference internal" href="../library/inspect.html#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a> can also correctly un-stringize
|
||
stringized annotations. <a class="reference internal" href="../library/inspect.html#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a> is now considered
|
||
best practice for accessing the annotations dict defined on any Python object;
|
||
for more information on best practices for working with annotations, please see
|
||
<a class="reference internal" href="../howto/annotations.html#annotations-howto"><span class="std std-ref">Annotations Best Practices</span></a>.
|
||
Relatedly, <a class="reference internal" href="../library/inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.signature()</span></code></a>,
|
||
<a class="reference internal" href="../library/inspect.html#inspect.Signature.from_callable" title="inspect.Signature.from_callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.Signature.from_callable()</span></code></a>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.Signature.from_function()</span></code>
|
||
now call <a class="reference internal" href="../library/inspect.html#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a> to retrieve annotations. This means
|
||
<a class="reference internal" href="../library/inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.signature()</span></code></a> and <a class="reference internal" href="../library/inspect.html#inspect.Signature.from_callable" title="inspect.Signature.from_callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.Signature.from_callable()</span></code></a> can
|
||
also now un-stringize stringized annotations.
|
||
(Contributed by Larry Hastings in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43817">bpo-43817</a>.)</p>
|
||
</section>
|
||
<section id="itertools">
|
||
<h3>itertools<a class="headerlink" href="#itertools" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/itertools.html#itertools.pairwise" title="itertools.pairwise"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.pairwise()</span></code></a>.
|
||
(Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38200">bpo-38200</a>.)</p>
|
||
</section>
|
||
<section id="linecache">
|
||
<h3>linecache<a class="headerlink" href="#linecache" title="Link to this heading">¶</a></h3>
|
||
<p>When a module does not define <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, fall back to <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42133">bpo-42133</a>.)</p>
|
||
</section>
|
||
<section id="os">
|
||
<h3>os<a class="headerlink" href="#os" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/os.html#os.cpu_count" title="os.cpu_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.cpu_count()</span></code></a> support for VxWorks RTOS.
|
||
(Contributed by Peixing Xin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41440">bpo-41440</a>.)</p>
|
||
<p>Add a new function <a class="reference internal" href="../library/os.html#os.eventfd" title="os.eventfd"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.eventfd()</span></code></a> and related helpers to wrap the
|
||
<code class="docutils literal notranslate"><span class="pre">eventfd2</span></code> syscall on Linux.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41001">bpo-41001</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/os.html#os.splice" title="os.splice"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.splice()</span></code></a> that allows to move data between two file
|
||
descriptors without copying between kernel address space and user
|
||
address space, where one of the file descriptors must refer to a
|
||
pipe. (Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41625">bpo-41625</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/os.html#os.O_EVTONLY" title="os.O_EVTONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_EVTONLY</span></code></a>, <a class="reference internal" href="../library/os.html#os.O_FSYNC" title="os.O_FSYNC"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_FSYNC</span></code></a>, <a class="reference internal" href="../library/os.html#os.O_SYMLINK" title="os.O_SYMLINK"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_SYMLINK</span></code></a>
|
||
and <a class="reference internal" href="../library/os.html#os.O_NOFOLLOW_ANY" title="os.O_NOFOLLOW_ANY"><code class="xref py py-const docutils literal notranslate"><span class="pre">O_NOFOLLOW_ANY</span></code></a> for macOS.
|
||
(Contributed by Donghee Na in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43106">bpo-43106</a>.)</p>
|
||
</section>
|
||
<section id="os-path">
|
||
<h3>os.path<a class="headerlink" href="#os-path" title="Link to this heading">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/os.path.html#os.path.realpath" title="os.path.realpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.realpath()</span></code></a> now accepts a <em>strict</em> keyword-only argument. When set
|
||
to <code class="docutils literal notranslate"><span class="pre">True</span></code>, <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if a path doesn’t exist or a symlink loop
|
||
is encountered.
|
||
(Contributed by Barney Gale in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43757">bpo-43757</a>.)</p>
|
||
</section>
|
||
<section id="pathlib">
|
||
<h3>pathlib<a class="headerlink" href="#pathlib" title="Link to this heading">¶</a></h3>
|
||
<p>Add slice support to <a class="reference internal" href="../library/pathlib.html#pathlib.PurePath.parents" title="pathlib.PurePath.parents"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.parents</span></code></a>.
|
||
(Contributed by Joshua Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35498">bpo-35498</a>.)</p>
|
||
<p>Add negative indexing support to <a class="reference internal" href="../library/pathlib.html#pathlib.PurePath.parents" title="pathlib.PurePath.parents"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PurePath.parents</span></code></a>.
|
||
(Contributed by Yaroslav Pankovych in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=21041">bpo-21041</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/pathlib.html#pathlib.Path.hardlink_to" title="pathlib.Path.hardlink_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.hardlink_to</span></code></a> method that
|
||
supersedes <code class="xref py py-meth docutils literal notranslate"><span class="pre">link_to()</span></code>. The new method has the same argument
|
||
order as <a class="reference internal" href="../library/pathlib.html#pathlib.Path.symlink_to" title="pathlib.Path.symlink_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">symlink_to()</span></code></a>.
|
||
(Contributed by Barney Gale in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39950">bpo-39950</a>.)</p>
|
||
<p><a class="reference internal" href="../library/pathlib.html#pathlib.Path.stat" title="pathlib.Path.stat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.Path.stat()</span></code></a> and <a class="reference internal" href="../library/pathlib.html#pathlib.Path.chmod" title="pathlib.Path.chmod"><code class="xref py py-meth docutils literal notranslate"><span class="pre">chmod()</span></code></a> now accept a
|
||
<em>follow_symlinks</em> keyword-only argument for consistency with corresponding
|
||
functions in the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module.
|
||
(Contributed by Barney Gale in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39906">bpo-39906</a>.)</p>
|
||
</section>
|
||
<section id="platform">
|
||
<h3>platform<a class="headerlink" href="#platform" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/platform.html#platform.freedesktop_os_release" title="platform.freedesktop_os_release"><code class="xref py py-func docutils literal notranslate"><span class="pre">platform.freedesktop_os_release()</span></code></a> to retrieve operation system
|
||
identification from <a class="reference external" href="https://www.freedesktop.org/software/systemd/man/os-release.html">freedesktop.org os-release</a> standard file.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=28468">bpo-28468</a>.)</p>
|
||
</section>
|
||
<section id="pprint">
|
||
<h3>pprint<a class="headerlink" href="#pprint" title="Link to this heading">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/pprint.html#pprint.pprint" title="pprint.pprint"><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint.pprint()</span></code></a> now accepts a new <code class="docutils literal notranslate"><span class="pre">underscore_numbers</span></code> keyword argument.
|
||
(Contributed by sblondon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42914">bpo-42914</a>.)</p>
|
||
<p><a class="reference internal" href="../library/pprint.html#module-pprint" title="pprint: Data pretty printer."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pprint</span></code></a> can now pretty-print <a class="reference internal" href="../library/dataclasses.html#dataclasses.dataclass" title="dataclasses.dataclass"><code class="xref py py-class docutils literal notranslate"><span class="pre">dataclasses.dataclass</span></code></a> instances.
|
||
(Contributed by Lewis Gaul in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43080">bpo-43080</a>.)</p>
|
||
</section>
|
||
<section id="py-compile">
|
||
<h3>py_compile<a class="headerlink" href="#py-compile" title="Link to this heading">¶</a></h3>
|
||
<p>Add <code class="docutils literal notranslate"><span class="pre">--quiet</span></code> option to command-line interface of <a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a>.
|
||
(Contributed by Gregory Schevchenko in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38731">bpo-38731</a>.)</p>
|
||
</section>
|
||
<section id="pyclbr">
|
||
<h3>pyclbr<a class="headerlink" href="#pyclbr" title="Link to this heading">¶</a></h3>
|
||
<p>Add an <code class="docutils literal notranslate"><span class="pre">end_lineno</span></code> attribute to the <code class="docutils literal notranslate"><span class="pre">Function</span></code> and <code class="docutils literal notranslate"><span class="pre">Class</span></code>
|
||
objects in the tree returned by <a class="reference internal" href="../library/pyclbr.html#pyclbr.readmodule" title="pyclbr.readmodule"><code class="xref py py-func docutils literal notranslate"><span class="pre">pyclbr.readmodule()</span></code></a> and
|
||
<a class="reference internal" href="../library/pyclbr.html#pyclbr.readmodule_ex" title="pyclbr.readmodule_ex"><code class="xref py py-func docutils literal notranslate"><span class="pre">pyclbr.readmodule_ex()</span></code></a>. It matches the existing (start) <code class="docutils literal notranslate"><span class="pre">lineno</span></code>.
|
||
(Contributed by Aviral Srivastava in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38307">bpo-38307</a>.)</p>
|
||
</section>
|
||
<section id="shelve">
|
||
<h3>shelve<a class="headerlink" href="#shelve" title="Link to this heading">¶</a></h3>
|
||
<p>The <a class="reference internal" href="../library/shelve.html#module-shelve" title="shelve: Python object persistence."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shelve</span></code></a> module now uses <a class="reference internal" href="../library/pickle.html#pickle.DEFAULT_PROTOCOL" title="pickle.DEFAULT_PROTOCOL"><code class="xref py py-const docutils literal notranslate"><span class="pre">pickle.DEFAULT_PROTOCOL</span></code></a> by default
|
||
instead of <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> protocol <code class="docutils literal notranslate"><span class="pre">3</span></code> when creating shelves.
|
||
(Contributed by Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34204">bpo-34204</a>.)</p>
|
||
</section>
|
||
<section id="statistics">
|
||
<h3>statistics<a class="headerlink" href="#statistics" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/statistics.html#statistics.covariance" title="statistics.covariance"><code class="xref py py-func docutils literal notranslate"><span class="pre">covariance()</span></code></a>, Pearson’s
|
||
<a class="reference internal" href="../library/statistics.html#statistics.correlation" title="statistics.correlation"><code class="xref py py-func docutils literal notranslate"><span class="pre">correlation()</span></code></a>, and simple
|
||
<a class="reference internal" href="../library/statistics.html#statistics.linear_regression" title="statistics.linear_regression"><code class="xref py py-func docutils literal notranslate"><span class="pre">linear_regression()</span></code></a> functions.
|
||
(Contributed by Tymoteusz Wołodźko in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38490">bpo-38490</a>.)</p>
|
||
</section>
|
||
<section id="site">
|
||
<h3>site<a class="headerlink" href="#site" title="Link to this heading">¶</a></h3>
|
||
<p>When a module does not define <code class="docutils literal notranslate"><span class="pre">__loader__</span></code>, fall back to <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42133">bpo-42133</a>.)</p>
|
||
</section>
|
||
<section id="socket">
|
||
<h3>socket<a class="headerlink" href="#socket" title="Link to this heading">¶</a></h3>
|
||
<p>The exception <a class="reference internal" href="../library/socket.html#socket.timeout" title="socket.timeout"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.timeout</span></code></a> is now an alias of <a class="reference internal" href="../library/exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a>.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42413">bpo-42413</a>.)</p>
|
||
<p>Add option to create MPTCP sockets with <code class="docutils literal notranslate"><span class="pre">IPPROTO_MPTCP</span></code>
|
||
(Contributed by Rui Cunha in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43571">bpo-43571</a>.)</p>
|
||
<p>Add <code class="docutils literal notranslate"><span class="pre">IP_RECVTOS</span></code> option to receive the type of service (ToS) or DSCP/ECN fields
|
||
(Contributed by Georg Sauthoff in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44077">bpo-44077</a>.)</p>
|
||
</section>
|
||
<section id="ssl">
|
||
<h3>ssl<a class="headerlink" href="#ssl" title="Link to this heading">¶</a></h3>
|
||
<p>The ssl module requires OpenSSL 1.1.1 or newer.
|
||
(Contributed by Christian Heimes in <span class="target" id="index-30"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43669">bpo-43669</a>.)</p>
|
||
<p>The ssl module has preliminary support for OpenSSL 3.0.0 and new option
|
||
<a class="reference internal" href="../library/ssl.html#ssl.OP_IGNORE_UNEXPECTED_EOF" title="ssl.OP_IGNORE_UNEXPECTED_EOF"><code class="xref py py-const docutils literal notranslate"><span class="pre">OP_IGNORE_UNEXPECTED_EOF</span></code></a>.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38820">bpo-38820</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43794">bpo-43794</a>,
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43788">bpo-43788</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43791">bpo-43791</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43799">bpo-43799</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43920">bpo-43920</a>,
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43789">bpo-43789</a>, and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43811">bpo-43811</a>.)</p>
|
||
<p>Deprecated function and use of deprecated constants now result in
|
||
a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.options" title="ssl.SSLContext.options"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.options</span></code></a> has
|
||
<a class="reference internal" href="../library/ssl.html#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a> and <a class="reference internal" href="../library/ssl.html#ssl.OP_NO_SSLv3" title="ssl.OP_NO_SSLv3"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv3</span></code></a> set by default and
|
||
therefore cannot warn about setting the flag again. The
|
||
<a class="reference internal" href="#whatsnew310-deprecated"><span class="std std-ref">deprecation section</span></a> has a list of deprecated
|
||
features.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43880">bpo-43880</a>.)</p>
|
||
<p>The ssl module now has more secure default settings. Ciphers without forward
|
||
secrecy or SHA-1 MAC are disabled by default. Security level 2 prohibits
|
||
weak RSA, DH, and ECC keys with less than 112 bits of security.
|
||
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> defaults to minimum protocol version TLS 1.2.
|
||
Settings are based on Hynek Schlawack’s research.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43998">bpo-43998</a>.)</p>
|
||
<p>The deprecated protocols SSL 3.0, TLS 1.0, and TLS 1.1 are no longer
|
||
officially supported. Python does not block them actively. However
|
||
OpenSSL build options, distro configurations, vendor patches, and cipher
|
||
suites may prevent a successful handshake.</p>
|
||
<p>Add a <em>timeout</em> parameter to the <a class="reference internal" href="../library/ssl.html#ssl.get_server_certificate" title="ssl.get_server_certificate"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.get_server_certificate()</span></code></a> function.
|
||
(Contributed by Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31870">bpo-31870</a>.)</p>
|
||
<p>The ssl module uses heap-types and multi-phase initialization.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42333">bpo-42333</a>.)</p>
|
||
<p>A new verify flag <a class="reference internal" href="../library/ssl.html#ssl.VERIFY_X509_PARTIAL_CHAIN" title="ssl.VERIFY_X509_PARTIAL_CHAIN"><code class="xref py py-const docutils literal notranslate"><span class="pre">VERIFY_X509_PARTIAL_CHAIN</span></code></a> has been added.
|
||
(Contributed by l0x in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40849">bpo-40849</a>.)</p>
|
||
</section>
|
||
<section id="sqlite3">
|
||
<h3>sqlite3<a class="headerlink" href="#sqlite3" title="Link to this heading">¶</a></h3>
|
||
<p>Add audit events for <code class="xref py py-func docutils literal notranslate"><span class="pre">connect/handle()</span></code>,
|
||
<a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.enable_load_extension" title="sqlite3.Connection.enable_load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enable_load_extension()</span></code></a>, and
|
||
<a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.load_extension" title="sqlite3.Connection.load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_extension()</span></code></a>.
|
||
(Contributed by Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43762">bpo-43762</a>.)</p>
|
||
</section>
|
||
<section id="sys">
|
||
<h3>sys<a class="headerlink" href="#sys" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/sys.html#sys.orig_argv" title="sys.orig_argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.orig_argv</span></code></a> attribute: the list of the original command line
|
||
arguments passed to the Python executable.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23427">bpo-23427</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/sys.html#sys.stdlib_module_names" title="sys.stdlib_module_names"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdlib_module_names</span></code></a>, containing the list of the standard library
|
||
module names.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42955">bpo-42955</a>.)</p>
|
||
</section>
|
||
<section id="thread">
|
||
<h3>_thread<a class="headerlink" href="#thread" title="Link to this heading">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/_thread.html#thread.interrupt_main" title="_thread.interrupt_main"><code class="xref py py-func docutils literal notranslate"><span class="pre">_thread.interrupt_main()</span></code></a> now takes an optional signal number to
|
||
simulate (the default is still <a class="reference internal" href="../library/signal.html#signal.SIGINT" title="signal.SIGINT"><code class="xref py py-const docutils literal notranslate"><span class="pre">signal.SIGINT</span></code></a>).
|
||
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43356">bpo-43356</a>.)</p>
|
||
</section>
|
||
<section id="threading">
|
||
<h3>threading<a class="headerlink" href="#threading" title="Link to this heading">¶</a></h3>
|
||
<p>Add <a class="reference internal" href="../library/threading.html#threading.gettrace" title="threading.gettrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.gettrace()</span></code></a> and <a class="reference internal" href="../library/threading.html#threading.getprofile" title="threading.getprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.getprofile()</span></code></a> to
|
||
retrieve the functions set by <a class="reference internal" href="../library/threading.html#threading.settrace" title="threading.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.settrace()</span></code></a> and
|
||
<a class="reference internal" href="../library/threading.html#threading.setprofile" title="threading.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.setprofile()</span></code></a> respectively.
|
||
(Contributed by Mario Corchero in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42251">bpo-42251</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/threading.html#threading.__excepthook__" title="threading.__excepthook__"><code class="xref py py-data docutils literal notranslate"><span class="pre">threading.__excepthook__</span></code></a> to allow retrieving the original value
|
||
of <a class="reference internal" href="../library/threading.html#threading.excepthook" title="threading.excepthook"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.excepthook()</span></code></a> in case it is set to a broken or a different
|
||
value.
|
||
(Contributed by Mario Corchero in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42308">bpo-42308</a>.)</p>
|
||
</section>
|
||
<section id="traceback">
|
||
<h3>traceback<a class="headerlink" href="#traceback" title="Link to this heading">¶</a></h3>
|
||
<p>The <a class="reference internal" href="../library/traceback.html#traceback.format_exception" title="traceback.format_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_exception()</span></code></a>,
|
||
<a class="reference internal" href="../library/traceback.html#traceback.format_exception_only" title="traceback.format_exception_only"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_exception_only()</span></code></a>, and
|
||
<a class="reference internal" href="../library/traceback.html#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code></a> functions can now take an exception object
|
||
as a positional-only argument.
|
||
(Contributed by Zackery Spytz and Matthias Bussonnier in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26389">bpo-26389</a>.)</p>
|
||
</section>
|
||
<section id="types">
|
||
<h3>types<a class="headerlink" href="#types" title="Link to this heading">¶</a></h3>
|
||
<p>Reintroduce the <a class="reference internal" href="../library/types.html#types.EllipsisType" title="types.EllipsisType"><code class="xref py py-data docutils literal notranslate"><span class="pre">types.EllipsisType</span></code></a>, <a class="reference internal" href="../library/types.html#types.NoneType" title="types.NoneType"><code class="xref py py-data docutils literal notranslate"><span class="pre">types.NoneType</span></code></a>
|
||
and <a class="reference internal" href="../library/types.html#types.NotImplementedType" title="types.NotImplementedType"><code class="xref py py-data docutils literal notranslate"><span class="pre">types.NotImplementedType</span></code></a> classes, providing a new set
|
||
of types readily interpretable by type checkers.
|
||
(Contributed by Bas van Beek in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41810">bpo-41810</a>.)</p>
|
||
</section>
|
||
<section id="typing">
|
||
<h3>typing<a class="headerlink" href="#typing" title="Link to this heading">¶</a></h3>
|
||
<p>For major changes, see <a class="reference internal" href="#new-feat-related-type-hints"><span class="std std-ref">New Features Related to Type Hints</span></a>.</p>
|
||
<p>The behavior of <a class="reference internal" href="../library/typing.html#typing.Literal" title="typing.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Literal</span></code></a> was changed to conform with <span class="target" id="index-31"></span><a class="pep reference external" href="https://peps.python.org/pep-0586/"><strong>PEP 586</strong></a>
|
||
and to match the behavior of static type checkers specified in the PEP.</p>
|
||
<ol class="arabic">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Literal</span></code> now de-duplicates parameters.</p></li>
|
||
<li><p>Equality comparisons between <code class="docutils literal notranslate"><span class="pre">Literal</span></code> objects are now order independent.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Literal</span></code> comparisons now respect types. For example,
|
||
<code class="docutils literal notranslate"><span class="pre">Literal[0]</span> <span class="pre">==</span> <span class="pre">Literal[False]</span></code> previously evaluated to <code class="docutils literal notranslate"><span class="pre">True</span></code>. It is
|
||
now <code class="docutils literal notranslate"><span class="pre">False</span></code>. To support this change, the internally used type cache now
|
||
supports differentiating types.</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Literal</span></code> objects will now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception during
|
||
equality comparisons if any of their parameters are not <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.
|
||
Note that declaring <code class="docutils literal notranslate"><span class="pre">Literal</span></code> with unhashable parameters will not throw
|
||
an error:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">typing</span><span class="w"> </span><span class="kn">import</span> <span class="n">Literal</span>
|
||
<span class="gp">>>> </span><span class="n">Literal</span><span class="p">[{</span><span class="mi">0</span><span class="p">}]</span>
|
||
<span class="gp">>>> </span><span class="n">Literal</span><span class="p">[{</span><span class="mi">0</span><span class="p">}]</span> <span class="o">==</span> <span class="n">Literal</span><span class="p">[{</span><span class="kc">False</span><span class="p">}]</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
|
||
<span class="gr">TypeError</span>: <span class="n">unhashable type: 'set'</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
</ol>
|
||
<p>(Contributed by Yurii Karabas in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42345">bpo-42345</a>.)</p>
|
||
<p>Add new function <a class="reference internal" href="../library/typing.html#typing.is_typeddict" title="typing.is_typeddict"><code class="xref py py-func docutils literal notranslate"><span class="pre">typing.is_typeddict()</span></code></a> to introspect if an annotation
|
||
is a <a class="reference internal" href="../library/typing.html#typing.TypedDict" title="typing.TypedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.TypedDict</span></code></a>.
|
||
(Contributed by Patrick Reader in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41792">bpo-41792</a>.)</p>
|
||
<p>Subclasses of <code class="docutils literal notranslate"><span class="pre">typing.Protocol</span></code> which only have data variables declared
|
||
will now raise a <code class="docutils literal notranslate"><span class="pre">TypeError</span></code> when checked with <code class="docutils literal notranslate"><span class="pre">isinstance</span></code> unless they
|
||
are decorated with <a class="reference internal" href="../library/typing.html#typing.runtime_checkable" title="typing.runtime_checkable"><code class="xref py py-func docutils literal notranslate"><span class="pre">runtime_checkable()</span></code></a>. Previously, these checks
|
||
passed silently. Users should decorate their
|
||
subclasses with the <code class="xref py py-func docutils literal notranslate"><span class="pre">runtime_checkable()</span></code> decorator
|
||
if they want runtime protocols.
|
||
(Contributed by Yurii Karabas in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38908">bpo-38908</a>.)</p>
|
||
<p>Importing from the <code class="docutils literal notranslate"><span class="pre">typing.io</span></code> and <code class="docutils literal notranslate"><span class="pre">typing.re</span></code> submodules will now emit
|
||
<a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. These submodules have been deprecated since
|
||
Python 3.8 and will be removed in a future version of Python. Anything
|
||
belonging to those submodules should be imported directly from
|
||
<a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> instead.
|
||
(Contributed by Sebastian Rittau in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38291">bpo-38291</a>.)</p>
|
||
</section>
|
||
<section id="unittest">
|
||
<h3>unittest<a class="headerlink" href="#unittest" title="Link to this heading">¶</a></h3>
|
||
<p>Add new method <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNoLogs" title="unittest.TestCase.assertNoLogs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNoLogs()</span></code></a> to complement the
|
||
existing <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLogs" title="unittest.TestCase.assertLogs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLogs()</span></code></a>. (Contributed by Kit Yan Choi
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39385">bpo-39385</a>.)</p>
|
||
</section>
|
||
<section id="urllib-parse">
|
||
<h3>urllib.parse<a class="headerlink" href="#urllib-parse" title="Link to this heading">¶</a></h3>
|
||
<p>Python versions earlier than Python 3.10 allowed using both <code class="docutils literal notranslate"><span class="pre">;</span></code> and <code class="docutils literal notranslate"><span class="pre">&</span></code> as
|
||
query parameter separators in <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.parse_qs" title="urllib.parse.parse_qs"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.parse_qs()</span></code></a> and
|
||
<a class="reference internal" href="../library/urllib.parse.html#urllib.parse.parse_qsl" title="urllib.parse.parse_qsl"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.parse.parse_qsl()</span></code></a>. Due to security concerns, and to conform with
|
||
newer W3C recommendations, this has been changed to allow only a single
|
||
separator key, with <code class="docutils literal notranslate"><span class="pre">&</span></code> as the default. This change also affects
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">cgi.parse()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">cgi.parse_multipart()</span></code> as they use the affected
|
||
functions internally. For more details, please see their respective
|
||
documentation.
|
||
(Contributed by Adam Goldschmidt, Senthil Kumaran and Ken Jin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42967">bpo-42967</a>.)</p>
|
||
<p>The presence of newline or tab characters in parts of a URL allows for some
|
||
forms of attacks. Following the WHATWG specification that updates <span class="target" id="index-32"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3986.html"><strong>RFC 3986</strong></a>,
|
||
ASCII newline <code class="docutils literal notranslate"><span class="pre">\n</span></code>, <code class="docutils literal notranslate"><span class="pre">\r</span></code> and tab <code class="docutils literal notranslate"><span class="pre">\t</span></code> characters are stripped from the
|
||
URL by the parser in <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code></a> preventing such attacks. The removal
|
||
characters are controlled by a new module level variable
|
||
<code class="docutils literal notranslate"><span class="pre">urllib.parse._UNSAFE_URL_BYTES_TO_REMOVE</span></code>. (See <a class="reference external" href="https://github.com/python/cpython/issues/88048">gh-88048</a>)</p>
|
||
</section>
|
||
<section id="xml">
|
||
<h3>xml<a class="headerlink" href="#xml" title="Link to this heading">¶</a></h3>
|
||
<p>Add a <a class="reference internal" href="../library/xml.sax.handler.html#xml.sax.handler.LexicalHandler" title="xml.sax.handler.LexicalHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">LexicalHandler</span></code></a> class to the
|
||
<a class="reference internal" href="../library/xml.sax.handler.html#module-xml.sax.handler" title="xml.sax.handler: Base classes for SAX event handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax.handler</span></code></a> module.
|
||
(Contributed by Jonathan Gossage and Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35018">bpo-35018</a>.)</p>
|
||
</section>
|
||
<section id="zipimport">
|
||
<h3>zipimport<a class="headerlink" href="#zipimport" title="Link to this heading">¶</a></h3>
|
||
<p>Add methods related to <span class="target" id="index-33"></span><a class="pep reference external" href="https://peps.python.org/pep-0451/"><strong>PEP 451</strong></a>: <a class="reference internal" href="../library/zipimport.html#zipimport.zipimporter.find_spec" title="zipimport.zipimporter.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a>,
|
||
<a class="reference internal" href="../library/zipimport.html#zipimport.zipimporter.create_module" title="zipimport.zipimporter.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">zipimport.zipimporter.create_module()</span></code></a>, and
|
||
<a class="reference internal" href="../library/zipimport.html#zipimport.zipimporter.exec_module" title="zipimport.zipimporter.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">zipimport.zipimporter.exec_module()</span></code></a>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42131">bpo-42131</a>.)</p>
|
||
<p>Add <a class="reference internal" href="../library/zipimport.html#zipimport.zipimporter.invalidate_caches" title="zipimport.zipimporter.invalidate_caches"><code class="xref py py-meth docutils literal notranslate"><span class="pre">invalidate_caches()</span></code></a> method.
|
||
(Contributed by Desmond Cheong in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=14678">bpo-14678</a>.)</p>
|
||
</section>
|
||
</section>
|
||
<section id="optimizations">
|
||
<h2>Optimizations<a class="headerlink" href="#optimizations" title="Link to this heading">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>Constructors <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytes()</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-func docutils literal notranslate"><span class="pre">bytearray()</span></code></a> are now faster
|
||
(around 30–40% for small objects).
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41334">bpo-41334</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> module now imports fewer modules.
|
||
The <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-m</span> <span class="pre">module-name</span></code> command startup time is 1.4x faster in
|
||
average. On Linux, <code class="docutils literal notranslate"><span class="pre">python3</span> <span class="pre">-I</span> <span class="pre">-m</span> <span class="pre">module-name</span></code> imports 69 modules on Python
|
||
3.9, whereas it only imports 51 modules (-18) on Python 3.10.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41006">bpo-41006</a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41718">bpo-41718</a>.)</p></li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">LOAD_ATTR</span></code> instruction now uses new “per opcode cache” mechanism. It
|
||
is about 36% faster now for regular attributes and 44% faster for slots.
|
||
(Contributed by Pablo Galindo and Yury Selivanov in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42093">bpo-42093</a> and Guido
|
||
van Rossum in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42927">bpo-42927</a>, based on ideas implemented originally in PyPy
|
||
and MicroPython.)</p></li>
|
||
<li><p>When building Python with <a class="reference internal" href="../using/configure.html#cmdoption-enable-optimizations"><code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-optimizations</span></code></a> now
|
||
<code class="docutils literal notranslate"><span class="pre">-fno-semantic-interposition</span></code> is added to both the compile and link line.
|
||
This speeds builds of the Python interpreter created with <a class="reference internal" href="../using/configure.html#cmdoption-enable-shared"><code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-shared</span></code></a>
|
||
with <code class="docutils literal notranslate"><span class="pre">gcc</span></code> by up to 30%. See <a class="reference external" href="https://developers.redhat.com/blog/2020/06/25/red-hat-enterprise-linux-8-2-brings-faster-python-3-8-run-speeds/">this article</a>
|
||
for more details. (Contributed by Victor Stinner and Pablo Galindo in
|
||
<a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38980">bpo-38980</a>.)</p></li>
|
||
<li><p>Use a new output buffer management code for <a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> / <a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> /
|
||
<a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> modules, and add <code class="docutils literal notranslate"><span class="pre">.readall()</span></code> function to
|
||
<code class="docutils literal notranslate"><span class="pre">_compression.DecompressReader</span></code> class. bz2 decompression is now 1.09x ~ 1.17x
|
||
faster, lzma decompression 1.20x ~ 1.32x faster, <code class="docutils literal notranslate"><span class="pre">GzipFile.read(-1)</span></code> 1.11x
|
||
~ 1.18x faster. (Contributed by Ma Lin, reviewed by Gregory P. Smith, in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41486">bpo-41486</a>)</p></li>
|
||
<li><p>When using stringized annotations, annotations dicts for functions are no longer
|
||
created when the function is created. Instead, they are stored as a tuple of
|
||
strings, and the function object lazily converts this into the annotations dict
|
||
on demand. This optimization cuts the CPU time needed to define an annotated
|
||
function by half.
|
||
(Contributed by Yurii Karabas and Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42202">bpo-42202</a>.)</p></li>
|
||
<li><p>Substring search functions such as <code class="docutils literal notranslate"><span class="pre">str1</span> <span class="pre">in</span> <span class="pre">str2</span></code> and <code class="docutils literal notranslate"><span class="pre">str2.find(str1)</span></code>
|
||
now sometimes use Crochemore & Perrin’s “Two-Way” string searching
|
||
algorithm to avoid quadratic behavior on long strings. (Contributed
|
||
by Dennis Sweeney in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41972">bpo-41972</a>)</p></li>
|
||
<li><p>Add micro-optimizations to <code class="docutils literal notranslate"><span class="pre">_PyType_Lookup()</span></code> to improve type attribute cache lookup
|
||
performance in the common case of cache hits. This makes the interpreter 1.04 times faster
|
||
on average. (Contributed by Dino Viehland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43452">bpo-43452</a>.)</p></li>
|
||
<li><p>The following built-in functions now support the faster <span class="target" id="index-34"></span><a class="pep reference external" href="https://peps.python.org/pep-0590/"><strong>PEP 590</strong></a> vectorcall calling convention:
|
||
<a class="reference internal" href="../library/functions.html#map" title="map"><code class="xref py py-func docutils literal notranslate"><span class="pre">map()</span></code></a>, <a class="reference internal" href="../library/functions.html#filter" title="filter"><code class="xref py py-func docutils literal notranslate"><span class="pre">filter()</span></code></a>, <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a>, <a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-func docutils literal notranslate"><span class="pre">bool()</span></code></a> and <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a>.
|
||
(Contributed by Donghee Na and Jeroen Demeyer in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43575">bpo-43575</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43287">bpo-43287</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41922">bpo-41922</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41873">bpo-41873</a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41870">bpo-41870</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> performance is improved by removing internal <code class="docutils literal notranslate"><span class="pre">RLock</span></code>.
|
||
This makes <code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code> thread unsafe in the face of multiple simultaneous
|
||
readers or writers, just like its equivalent classes in <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> and
|
||
<a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> have always been. (Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43785">bpo-43785</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="deprecated">
|
||
<span id="whatsnew310-deprecated"></span><h2>Deprecated<a class="headerlink" href="#deprecated" title="Link to this heading">¶</a></h2>
|
||
<ul>
|
||
<li><p>Currently Python accepts numeric literals immediately followed by keywords,
|
||
for example <code class="docutils literal notranslate"><span class="pre">0in</span> <span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">1or</span> <span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">0if</span> <span class="pre">1else</span> <span class="pre">2</span></code>. It allows confusing
|
||
and ambiguous expressions like <code class="docutils literal notranslate"><span class="pre">[0x1for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">y]</span></code> (which can be
|
||
interpreted as <code class="docutils literal notranslate"><span class="pre">[0x1</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">y]</span></code> or <code class="docutils literal notranslate"><span class="pre">[0x1f</span> <span class="pre">or</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">y]</span></code>). Starting in
|
||
this release, a deprecation warning is raised if the numeric literal is
|
||
immediately followed by one of keywords <a class="reference internal" href="../reference/expressions.html#and"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">and</span></code></a>, <a class="reference internal" href="../reference/compound_stmts.html#else"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a>,
|
||
<a class="reference internal" href="../reference/compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a>, <a class="reference internal" href="../reference/compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a>, <a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a>, <a class="reference internal" href="../reference/expressions.html#is"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span></code></a> and <a class="reference internal" href="../reference/expressions.html#or"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">or</span></code></a>.
|
||
In future releases it will be changed to syntax warning, and finally to
|
||
syntax error.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43833">bpo-43833</a>.)</p></li>
|
||
<li><p>Starting in this release, there will be a concerted effort to begin
|
||
cleaning up old import semantics that were kept for Python 2.7
|
||
compatibility. Specifically,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">find_loader()</span></code>/<code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code>
|
||
(superseded by <a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a>),
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_module()</span></code></a>
|
||
(superseded by <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a>),
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">module_repr()</span></code> (which the import system
|
||
takes care of for you), the <code class="docutils literal notranslate"><span class="pre">__package__</span></code> attribute
|
||
(superseded by <code class="docutils literal notranslate"><span class="pre">__spec__.parent</span></code>), the <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> attribute
|
||
(superseded by <code class="docutils literal notranslate"><span class="pre">__spec__.loader</span></code>), and the <code class="docutils literal notranslate"><span class="pre">__cached__</span></code> attribute
|
||
(superseded by <code class="docutils literal notranslate"><span class="pre">__spec__.cached</span></code>) will slowly be removed (as well
|
||
as other classes and methods in <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>).
|
||
<a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> and/or <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> will be raised
|
||
as appropriate to help identify code which needs updating during
|
||
this transition.</p></li>
|
||
<li><p>The entire <code class="docutils literal notranslate"><span class="pre">distutils</span></code> namespace is deprecated, to be removed in
|
||
Python 3.12. Refer to the <a class="reference internal" href="#distutils-deprecated"><span class="std std-ref">module changes</span></a>
|
||
section for more information.</p></li>
|
||
<li><p>Non-integer arguments to <a class="reference internal" href="../library/random.html#random.randrange" title="random.randrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">random.randrange()</span></code></a> are deprecated.
|
||
The <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is deprecated in favor of a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.
|
||
(Contributed by Serhiy Storchaka and Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37319">bpo-37319</a>.)</p></li>
|
||
<li><p>The various <code class="docutils literal notranslate"><span class="pre">load_module()</span></code> methods of <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> have been
|
||
documented as deprecated since Python 3.6, but will now also trigger
|
||
a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. Use
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> instead.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26131">bpo-26131</a>.)</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">zimport.zipimporter.load_module()</span></code> has been deprecated in
|
||
preference for <a class="reference internal" href="../library/zipimport.html#zipimport.zipimporter.exec_module" title="zipimport.zipimporter.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a>.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26131">bpo-26131</a>.)</p></li>
|
||
<li><p>The use of <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_module()</span></code></a> by the import
|
||
system now triggers an <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> as
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is preferred.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26131">bpo-26131</a>.)</p></li>
|
||
<li><p>The use of <code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder.find_module()</span></code> and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_module()</span></code> by the import system now
|
||
trigger an <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> as
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder.find_spec()</span></code></a> and
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_spec()</span></code></a>
|
||
are preferred, respectively. You can use
|
||
<a class="reference internal" href="../library/importlib.html#importlib.util.spec_from_loader" title="importlib.util.spec_from_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.spec_from_loader()</span></code></a> to help in porting.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42134">bpo-42134</a>.)</p></li>
|
||
<li><p>The use of <code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_loader()</span></code> by the import
|
||
system now triggers an <a class="reference internal" href="../library/exceptions.html#ImportWarning" title="ImportWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportWarning</span></code></a> as
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_spec()</span></code></a> is preferred. You can use
|
||
<a class="reference internal" href="../library/importlib.html#importlib.util.spec_from_loader" title="importlib.util.spec_from_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.spec_from_loader()</span></code></a> to help in porting.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43672">bpo-43672</a>.)</p></li>
|
||
<li><p>The various implementations of
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder.find_module()</span></code> (
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.BuiltinImporter.find_module()</span></code>,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.FrozenImporter.find_module()</span></code>,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.WindowsRegistryFinder.find_module()</span></code>,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.PathFinder.find_module()</span></code>,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder.find_module()</span></code> ),
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_module()</span></code> (
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.FileFinder.find_module()</span></code> ), and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.find_loader()</span></code> (
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.FileFinder.find_loader()</span></code> )
|
||
now raise <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> and are slated for removal in
|
||
Python 3.12 (previously they were documented as deprecated in Python 3.4).
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42135">bpo-42135</a>.)</p></li>
|
||
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.Finder</span></code> is deprecated (including its sole method,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">find_module()</span></code>). Both
|
||
<a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and <a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder</span></code></a>
|
||
no longer inherit from the class. Users should inherit from one of these two
|
||
classes as appropriate instead.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42135">bpo-42135</a>.)</p></li>
|
||
<li><p>The deprecations of <code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.find_loader()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.set_package_wrapper()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.set_loader_wrapper()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_for_loader()</span></code>,
|
||
<code class="xref py py-class docutils literal notranslate"><span class="pre">pkgutil.ImpImporter</span></code>, and
|
||
<code class="xref py py-class docutils literal notranslate"><span class="pre">pkgutil.ImpLoader</span></code> have all been updated to list Python 3.12 as the
|
||
slated version of removal (they began raising <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> in
|
||
previous versions of Python).
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43720">bpo-43720</a>.)</p></li>
|
||
<li><p>The import system now uses the <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> attribute on modules before
|
||
falling back on <code class="xref py py-meth docutils literal notranslate"><span class="pre">module_repr()</span></code> for a module’s
|
||
<code class="docutils literal notranslate"><span class="pre">__repr__()</span></code> method. Removal of the use of <code class="docutils literal notranslate"><span class="pre">module_repr()</span></code> is scheduled
|
||
for Python 3.12.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42137">bpo-42137</a>.)</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.module_repr()</span></code>,
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.FrozenLoader.module_repr()</span></code>, and
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.BuiltinLoader.module_repr()</span></code> are deprecated and
|
||
slated for removal in Python 3.12.
|
||
(Contributed by Brett Cannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42136">bpo-42136</a>.)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">sqlite3.OptimizedUnicode</span></code> has been undocumented and obsolete since Python
|
||
3.3, when it was made an alias to <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. It is now deprecated,
|
||
scheduled for removal in Python 3.12.
|
||
(Contributed by Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42264">bpo-42264</a>.)</p></li>
|
||
<li><p>The undocumented built-in function <code class="docutils literal notranslate"><span class="pre">sqlite3.enable_shared_cache</span></code> is now
|
||
deprecated, scheduled for removal in Python 3.12. Its use is strongly
|
||
discouraged by the SQLite3 documentation. See <a class="reference external" href="https://sqlite.org/c3ref/enable_shared_cache.html">the SQLite3 docs</a> for more details.
|
||
If a shared cache must be used, open the database in URI mode using the
|
||
<code class="docutils literal notranslate"><span class="pre">cache=shared</span></code> query parameter.
|
||
(Contributed by Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=24464">bpo-24464</a>.)</p></li>
|
||
<li><p>The following <code class="docutils literal notranslate"><span class="pre">threading</span></code> methods are now deprecated:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.currentThread</span></code> => <a class="reference internal" href="../library/threading.html#threading.current_thread" title="threading.current_thread"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.current_thread()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.activeCount</span></code> => <a class="reference internal" href="../library/threading.html#threading.active_count" title="threading.active_count"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.active_count()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.Condition.notifyAll</span></code> =>
|
||
<a class="reference internal" href="../library/threading.html#threading.Condition.notify_all" title="threading.Condition.notify_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Condition.notify_all()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.Event.isSet</span></code> => <a class="reference internal" href="../library/threading.html#threading.Event.is_set" title="threading.Event.is_set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Event.is_set()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.Thread.setName</span></code> => <a class="reference internal" href="../library/threading.html#threading.Thread.name" title="threading.Thread.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">threading.Thread.name</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.thread.getName</span></code> => <a class="reference internal" href="../library/threading.html#threading.Thread.name" title="threading.Thread.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">threading.Thread.name</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.Thread.isDaemon</span></code> => <a class="reference internal" href="../library/threading.html#threading.Thread.daemon" title="threading.Thread.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">threading.Thread.daemon</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">threading.Thread.setDaemon</span></code> => <a class="reference internal" href="../library/threading.html#threading.Thread.daemon" title="threading.Thread.daemon"><code class="xref py py-attr docutils literal notranslate"><span class="pre">threading.Thread.daemon</span></code></a></p></li>
|
||
</ul>
|
||
<p>(Contributed by Jelle Zijlstra in <a class="reference external" href="https://github.com/python/cpython/issues/87889">gh-87889</a>.)</p>
|
||
</li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.Path.link_to()</span></code> is deprecated and slated for removal in
|
||
Python 3.12. Use <a class="reference internal" href="../library/pathlib.html#pathlib.Path.hardlink_to" title="pathlib.Path.hardlink_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.Path.hardlink_to()</span></code></a> instead.
|
||
(Contributed by Barney Gale in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39950">bpo-39950</a>.)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">cgi.log()</span></code> is deprecated and slated for removal in Python 3.12.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41139">bpo-41139</a>.)</p></li>
|
||
<li><p>The following <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> features have been deprecated since Python 3.6,
|
||
Python 3.7, or OpenSSL 1.1.0 and will be removed in 3.11:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv3</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_TLSv1</span></code>,
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_TLSv1_1</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_TLSv1_2</span></code>, and
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_TLSv1_3</span></code> are replaced by
|
||
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext.minimum_version" title="ssl.SSLContext.minimum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">minimum_version</span></code></a> and
|
||
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext.maximum_version" title="ssl.SSLContext.maximum_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maximum_version</span></code></a>.</p></li>
|
||
<li><p><code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_SSLv2</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_SSLv3</span></code>,
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_SSLv23</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLSv1</span></code>,
|
||
<code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLSv1_1</span></code>, <code class="xref py py-data docutils literal notranslate"><span class="pre">PROTOCOL_TLSv1_2</span></code>, and
|
||
<code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS</span></code> are deprecated in favor of
|
||
<a class="reference internal" href="../library/ssl.html#ssl.PROTOCOL_TLS_CLIENT" title="ssl.PROTOCOL_TLS_CLIENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_CLIENT</span></code></a> and <a class="reference internal" href="../library/ssl.html#ssl.PROTOCOL_TLS_SERVER" title="ssl.PROTOCOL_TLS_SERVER"><code class="xref py py-const docutils literal notranslate"><span class="pre">PROTOCOL_TLS_SERVER</span></code></a></p></li>
|
||
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">wrap_socket()</span></code> is replaced by <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.wrap_socket()</span></code></a></p></li>
|
||
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">match_hostname()</span></code></p></li>
|
||
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_pseudo_bytes()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_egd()</span></code></p></li>
|
||
<li><p>NPN features like <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.selected_npn_protocol" title="ssl.SSLSocket.selected_npn_protocol"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLSocket.selected_npn_protocol()</span></code></a> and
|
||
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext.set_npn_protocols" title="ssl.SSLContext.set_npn_protocols"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.set_npn_protocols()</span></code></a> are replaced by ALPN.</p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p>The threading debug (<code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONTHREADDEBUG</span></code> environment variable) is
|
||
deprecated in Python 3.10 and will be removed in Python 3.12. This feature
|
||
requires a <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">debug build of Python</span></a>.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=44584">bpo-44584</a>.)</p></li>
|
||
<li><p>Importing from the <code class="docutils literal notranslate"><span class="pre">typing.io</span></code> and <code class="docutils literal notranslate"><span class="pre">typing.re</span></code> submodules will now emit
|
||
<a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. These submodules will be removed in a future version
|
||
of Python. Anything belonging to these submodules should be imported directly
|
||
from <a class="reference internal" href="../library/typing.html#module-typing" title="typing: Support for type hints (see :pep:`484`)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">typing</span></code></a> instead.
|
||
(Contributed by Sebastian Rittau in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38291">bpo-38291</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="removed">
|
||
<span id="whatsnew310-removed"></span><h2>Removed<a class="headerlink" href="#removed" title="Link to this heading">¶</a></h2>
|
||
<ul>
|
||
<li><p>Removed special methods <code class="docutils literal notranslate"><span class="pre">__int__</span></code>, <code class="docutils literal notranslate"><span class="pre">__float__</span></code>, <code class="docutils literal notranslate"><span class="pre">__floordiv__</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">__mod__</span></code>, <code class="docutils literal notranslate"><span class="pre">__divmod__</span></code>, <code class="docutils literal notranslate"><span class="pre">__rfloordiv__</span></code>, <code class="docutils literal notranslate"><span class="pre">__rmod__</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">__rdivmod__</span></code> of the <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a> class. They always raised
|
||
a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41974">bpo-41974</a>.)</p></li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">ParserBase.error()</span></code> method from the private and undocumented <code class="docutils literal notranslate"><span class="pre">_markupbase</span></code>
|
||
module has been removed. <a class="reference internal" href="../library/html.parser.html#html.parser.HTMLParser" title="html.parser.HTMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">html.parser.HTMLParser</span></code></a> is the only subclass of
|
||
<code class="docutils literal notranslate"><span class="pre">ParserBase</span></code> and its <code class="docutils literal notranslate"><span class="pre">error()</span></code> implementation was already removed in
|
||
Python 3.5.
|
||
(Contributed by Berker Peksag in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31844">bpo-31844</a>.)</p></li>
|
||
<li><p>Removed the <code class="docutils literal notranslate"><span class="pre">unicodedata.ucnhash_CAPI</span></code> attribute which was an internal
|
||
PyCapsule object. The related private <code class="docutils literal notranslate"><span class="pre">_PyUnicode_Name_CAPI</span></code> structure was
|
||
moved to the internal C API.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42157">bpo-42157</a>.)</p></li>
|
||
<li><p>Removed the <code class="docutils literal notranslate"><span class="pre">parser</span></code> module, which was deprecated in 3.9 due to the
|
||
switch to the new PEG parser, as well as all the C source and header files
|
||
that were only being used by the old parser, including <code class="docutils literal notranslate"><span class="pre">node.h</span></code>, <code class="docutils literal notranslate"><span class="pre">parser.h</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">graminit.h</span></code> and <code class="docutils literal notranslate"><span class="pre">grammar.h</span></code>.</p></li>
|
||
<li><p>Removed the Public C API functions <code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseStringFlags</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseStringFlagsFilename</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseFileFlags</span></code> and <code class="docutils literal notranslate"><span class="pre">PyNode_Compile</span></code>
|
||
that were deprecated in 3.9 due to the switch to the new PEG parser.</p></li>
|
||
<li><p>Removed the <code class="docutils literal notranslate"><span class="pre">formatter</span></code> module, which was deprecated in Python 3.4.
|
||
It is somewhat obsolete, little used, and not tested. It was originally
|
||
scheduled to be removed in Python 3.6, but such removals were delayed until
|
||
after Python 2.7 EOL. Existing users should copy whatever classes they use
|
||
into their code.
|
||
(Contributed by Donghee Na and Terry J. Reedy in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42299">bpo-42299</a>.)</p></li>
|
||
<li><p>Removed the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetWarningsModule()</span></code> function that was useless
|
||
now due to the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_warnings</span></code> module was converted to a builtin module in 2.6.
|
||
(Contributed by Hai Shi in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42599">bpo-42599</a>.)</p></li>
|
||
<li><p>Remove deprecated aliases to <a class="reference internal" href="../library/collections.abc.html#collections-abstract-base-classes"><span class="std std-ref">Collections Abstract Base Classes</span></a> from
|
||
the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37324">bpo-37324</a>.)</p></li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">loop</span></code> parameter has been removed from most of <a class="reference internal" href="../library/asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a>‘s
|
||
<a class="reference internal" href="../library/asyncio-api-index.html"><span class="doc">high-level API</span></a> following deprecation
|
||
in Python 3.8. The motivation behind this change is multifold:</p>
|
||
<ol class="arabic simple">
|
||
<li><p>This simplifies the high-level API.</p></li>
|
||
<li><p>The functions in the high-level API have been implicitly getting the
|
||
current thread’s running event loop since Python 3.7. There isn’t a need to
|
||
pass the event loop to the API in most normal use cases.</p></li>
|
||
<li><p>Event loop passing is error-prone especially when dealing with loops
|
||
running in different threads.</p></li>
|
||
</ol>
|
||
<p>Note that the low-level API will still accept <code class="docutils literal notranslate"><span class="pre">loop</span></code>.
|
||
See <a class="reference internal" href="#changes-python-api"><span class="std std-ref">Changes in the Python API</span></a> for examples of how to replace existing code.</p>
|
||
<p>(Contributed by Yurii Karabas, Andrew Svetlov, Yury Selivanov and Kyle Stanley
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42392">bpo-42392</a>.)</p>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
<section id="porting-to-python-3-10">
|
||
<h2>Porting to Python 3.10<a class="headerlink" href="#porting-to-python-3-10" title="Link to this heading">¶</a></h2>
|
||
<p>This section lists previously described changes and other bugfixes
|
||
that may require changes to your code.</p>
|
||
<section id="changes-in-the-python-syntax">
|
||
<h3>Changes in the Python syntax<a class="headerlink" href="#changes-in-the-python-syntax" title="Link to this heading">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>Deprecation warning is now emitted when compiling previously valid syntax
|
||
if the numeric literal is immediately followed by a keyword (like in <code class="docutils literal notranslate"><span class="pre">0in</span> <span class="pre">x</span></code>).
|
||
In future releases it will be changed to syntax warning, and finally to a
|
||
syntax error. To get rid of the warning and make the code compatible with
|
||
future releases just add a space between the numeric literal and the
|
||
following keyword.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43833">bpo-43833</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changes-in-the-python-api">
|
||
<span id="changes-python-api"></span><h3>Changes in the Python API<a class="headerlink" href="#changes-in-the-python-api" title="Link to this heading">¶</a></h3>
|
||
<ul>
|
||
<li><p>The <em>etype</em> parameters of the <a class="reference internal" href="../library/traceback.html#traceback.format_exception" title="traceback.format_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_exception()</span></code></a>,
|
||
<a class="reference internal" href="../library/traceback.html#traceback.format_exception_only" title="traceback.format_exception_only"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_exception_only()</span></code></a>, and
|
||
<a class="reference internal" href="../library/traceback.html#traceback.print_exception" title="traceback.print_exception"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_exception()</span></code></a> functions in the <a class="reference internal" href="../library/traceback.html#module-traceback" title="traceback: Print or retrieve a stack traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code></a> module
|
||
have been renamed to <em>exc</em>.
|
||
(Contributed by Zackery Spytz and Matthias Bussonnier in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26389">bpo-26389</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a>: At Python exit, if a callback registered with
|
||
<a class="reference internal" href="../library/atexit.html#atexit.register" title="atexit.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">atexit.register()</span></code></a> fails, its exception is now logged. Previously, only
|
||
some exceptions were logged, and the last exception was always silently
|
||
ignored.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42639">bpo-42639</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> generic now flattens type parameters, similar
|
||
to what <a class="reference internal" href="../library/typing.html#typing.Callable" title="typing.Callable"><code class="xref py py-data docutils literal notranslate"><span class="pre">typing.Callable</span></code></a> currently does. This means that
|
||
<code class="docutils literal notranslate"><span class="pre">collections.abc.Callable[[int,</span> <span class="pre">str],</span> <span class="pre">str]</span></code> will have <code class="docutils literal notranslate"><span class="pre">__args__</span></code> of
|
||
<code class="docutils literal notranslate"><span class="pre">(int,</span> <span class="pre">str,</span> <span class="pre">str)</span></code>; previously this was <code class="docutils literal notranslate"><span class="pre">([int,</span> <span class="pre">str],</span> <span class="pre">str)</span></code>. Code which
|
||
accesses the arguments via <a class="reference internal" href="../library/typing.html#typing.get_args" title="typing.get_args"><code class="xref py py-func docutils literal notranslate"><span class="pre">typing.get_args()</span></code></a> or <code class="docutils literal notranslate"><span class="pre">__args__</span></code> need to account
|
||
for this change. Furthermore, <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> may be raised for invalid forms
|
||
of parameterizing <a class="reference internal" href="../library/collections.abc.html#collections.abc.Callable" title="collections.abc.Callable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Callable</span></code></a> which may have passed
|
||
silently in Python 3.9.
|
||
(Contributed by Ken Jin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42195">bpo-42195</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/socket.html#socket.htons" title="socket.htons"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.htons()</span></code></a> and <a class="reference internal" href="../library/socket.html#socket.ntohs" title="socket.ntohs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.ntohs()</span></code></a> now raise <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>
|
||
instead of <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> if the given parameter will not fit in
|
||
a 16-bit unsigned integer.
|
||
(Contributed by Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42393">bpo-42393</a>.)</p></li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">loop</span></code> parameter has been removed from most of <a class="reference internal" href="../library/asyncio.html#module-asyncio" title="asyncio: Asynchronous I/O."><code class="xref py py-mod docutils literal notranslate"><span class="pre">asyncio</span></code></a>‘s
|
||
<a class="reference internal" href="../library/asyncio-api-index.html"><span class="doc">high-level API</span></a> following deprecation
|
||
in Python 3.8.</p>
|
||
<p>A coroutine that currently looks like this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">foo</span><span class="p">(</span><span class="n">loop</span><span class="p">):</span>
|
||
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">loop</span><span class="o">=</span><span class="n">loop</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Should be replaced with this:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">async</span> <span class="k">def</span><span class="w"> </span><span class="nf">foo</span><span class="p">():</span>
|
||
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>If <code class="docutils literal notranslate"><span class="pre">foo()</span></code> was specifically designed <em>not</em> to run in the current thread’s
|
||
running event loop (e.g. running in another thread’s event loop), consider
|
||
using <a class="reference internal" href="../library/asyncio-task.html#asyncio.run_coroutine_threadsafe" title="asyncio.run_coroutine_threadsafe"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run_coroutine_threadsafe()</span></code></a> instead.</p>
|
||
<p>(Contributed by Yurii Karabas, Andrew Svetlov, Yury Selivanov and Kyle Stanley
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42392">bpo-42392</a>.)</p>
|
||
</li>
|
||
<li><p>The <a class="reference internal" href="../library/types.html#types.FunctionType" title="types.FunctionType"><code class="xref py py-data docutils literal notranslate"><span class="pre">types.FunctionType</span></code></a> constructor now inherits the current builtins
|
||
if the <em>globals</em> dictionary has no <code class="docutils literal notranslate"><span class="pre">"__builtins__"</span></code> key, rather than using
|
||
<code class="docutils literal notranslate"><span class="pre">{"None":</span> <span class="pre">None}</span></code> as builtins: same behavior as <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> and
|
||
<a class="reference internal" href="../library/functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> functions. Defining a function with <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">function(...):</span> <span class="pre">...</span></code>
|
||
in Python is not affected, globals cannot be overridden with this syntax: it
|
||
also inherits the current builtins.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42990">bpo-42990</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changes-in-the-c-api">
|
||
<h3>Changes in the C API<a class="headerlink" href="#changes-in-the-c-api" title="Link to this heading">¶</a></h3>
|
||
<ul>
|
||
<li><p>The C API functions <code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseStringFlags</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseStringFlagsFilename</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseFileFlags</span></code>, <code class="docutils literal notranslate"><span class="pre">PyNode_Compile</span></code> and the type
|
||
used by these functions, <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">_node</span></code>, were removed due to the switch
|
||
to the new PEG parser.</p>
|
||
<p>Source should be now be compiled directly to a code object using, for
|
||
example, <a class="reference internal" href="../c-api/veryhigh.html#c.Py_CompileString" title="Py_CompileString"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CompileString()</span></code></a>. The resulting code object can then be
|
||
evaluated using, for example, <a class="reference internal" href="../c-api/veryhigh.html#c.PyEval_EvalCode" title="PyEval_EvalCode"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_EvalCode()</span></code></a>.</p>
|
||
<p>Specifically:</p>
|
||
<ul>
|
||
<li><p>A call to <code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseStringFlags</span></code> followed by
|
||
<code class="docutils literal notranslate"><span class="pre">PyNode_Compile</span></code> can be replaced by calling <a class="reference internal" href="../c-api/veryhigh.html#c.Py_CompileString" title="Py_CompileString"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CompileString()</span></code></a>.</p></li>
|
||
<li><p>There is no direct replacement for <code class="docutils literal notranslate"><span class="pre">PyParser_SimpleParseFileFlags</span></code>.
|
||
To compile code from a <code class="docutils literal notranslate"><span class="pre">FILE</span> <span class="pre">*</span></code> argument, you will need to read
|
||
the file in C and pass the resulting buffer to <a class="reference internal" href="../c-api/veryhigh.html#c.Py_CompileString" title="Py_CompileString"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CompileString()</span></code></a>.</p></li>
|
||
<li><p>To compile a file given a <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code> filename, explicitly open the file, read
|
||
it and compile the result. One way to do this is using the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a>
|
||
module with <a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModule" title="PyImport_ImportModule"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModule()</span></code></a>, <a class="reference internal" href="../c-api/call.html#c.PyObject_CallMethod" title="PyObject_CallMethod"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethod()</span></code></a>,
|
||
<a class="reference internal" href="../c-api/bytes.html#c.PyBytes_AsString" title="PyBytes_AsString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyBytes_AsString()</span></code></a> and <a class="reference internal" href="../c-api/veryhigh.html#c.Py_CompileString" title="Py_CompileString"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CompileString()</span></code></a>,
|
||
as sketched below. (Declarations and error handling are omitted.)</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">io_module</span> <span class="o">=</span> <span class="n">Import_ImportModule</span><span class="p">(</span><span class="s2">"io"</span><span class="p">);</span>
|
||
<span class="n">fileobject</span> <span class="o">=</span> <span class="n">PyObject_CallMethod</span><span class="p">(</span><span class="n">io_module</span><span class="p">,</span> <span class="s2">"open"</span><span class="p">,</span> <span class="s2">"ss"</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="s2">"rb"</span><span class="p">);</span>
|
||
<span class="n">source_bytes_object</span> <span class="o">=</span> <span class="n">PyObject_CallMethod</span><span class="p">(</span><span class="n">fileobject</span><span class="p">,</span> <span class="s2">"read"</span><span class="p">,</span> <span class="s2">""</span><span class="p">);</span>
|
||
<span class="n">result</span> <span class="o">=</span> <span class="n">PyObject_CallMethod</span><span class="p">(</span><span class="n">fileobject</span><span class="p">,</span> <span class="s2">"close"</span><span class="p">,</span> <span class="s2">""</span><span class="p">);</span>
|
||
<span class="n">source_buf</span> <span class="o">=</span> <span class="n">PyBytes_AsString</span><span class="p">(</span><span class="n">source_bytes_object</span><span class="p">);</span>
|
||
<span class="n">code</span> <span class="o">=</span> <span class="n">Py_CompileString</span><span class="p">(</span><span class="n">source_buf</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">Py_file_input</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>For <code class="docutils literal notranslate"><span class="pre">FrameObject</span></code> objects, the <a class="reference internal" href="../reference/datamodel.html#frame.f_lasti" title="frame.f_lasti"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f_lasti</span></code></a> member now represents a wordcode
|
||
offset instead of a simple offset into the bytecode string. This means that this
|
||
number needs to be multiplied by 2 to be used with APIs that expect a byte offset
|
||
instead (like <a class="reference internal" href="../c-api/code.html#c.PyCode_Addr2Line" title="PyCode_Addr2Line"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_Addr2Line()</span></code></a> for example). Notice as well that the
|
||
<code class="xref py py-attr docutils literal notranslate"><span class="pre">f_lasti</span></code> member of <code class="docutils literal notranslate"><span class="pre">FrameObject</span></code> objects is not considered stable: please
|
||
use <a class="reference internal" href="../c-api/frame.html#c.PyFrame_GetLineNumber" title="PyFrame_GetLineNumber"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFrame_GetLineNumber()</span></code></a> instead.</p></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="cpython-bytecode-changes">
|
||
<h2>CPython bytecode changes<a class="headerlink" href="#cpython-bytecode-changes" title="Link to this heading">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">MAKE_FUNCTION</span></code> instruction now accepts either a dict or a tuple of
|
||
strings as the function’s annotations.
|
||
(Contributed by Yurii Karabas and Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42202">bpo-42202</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="build-changes">
|
||
<h2>Build Changes<a class="headerlink" href="#build-changes" title="Link to this heading">¶</a></h2>
|
||
<ul>
|
||
<li><p><span class="target" id="index-35"></span><a class="pep reference external" href="https://peps.python.org/pep-0644/"><strong>PEP 644</strong></a>: Python now requires OpenSSL 1.1.1 or newer. OpenSSL 1.0.2 is no
|
||
longer supported.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43669">bpo-43669</a>.)</p></li>
|
||
<li><p>The C99 functions <code class="xref c c-func docutils literal notranslate"><span class="pre">snprintf()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">vsnprintf()</span></code> are now required
|
||
to build Python.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36020">bpo-36020</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> requires SQLite 3.7.15 or higher. (Contributed by Sergey Fedoseev
|
||
and Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40744">bpo-40744</a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40810">bpo-40810</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../library/atexit.html#module-atexit" title="atexit: Register and execute cleanup functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">atexit</span></code></a> module must now always be built as a built-in module.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42639">bpo-42639</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../using/configure.html#cmdoption-disable-test-modules"><code class="xref std std-option docutils literal notranslate"><span class="pre">--disable-test-modules</span></code></a> option to the <code class="docutils literal notranslate"><span class="pre">configure</span></code> script:
|
||
don’t build nor install test modules.
|
||
(Contributed by Xavier de Gaye, Thomas Petazzoni and Peixing Xin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=27640">bpo-27640</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../using/configure.html#cmdoption-with-wheel-pkg-dir"><code class="xref std std-option docutils literal notranslate"><span class="pre">--with-wheel-pkg-dir=PATH</span> <span class="pre">option</span></code></a>
|
||
to the <code class="docutils literal notranslate"><span class="pre">./configure</span></code> script. If
|
||
specified, the <a class="reference internal" href="../library/ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the "pip" installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> module looks for <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> and <code class="docutils literal notranslate"><span class="pre">pip</span></code>
|
||
wheel packages in this directory: if both are present, these wheel packages
|
||
are used instead of ensurepip bundled wheel packages.</p>
|
||
<p>Some Linux distribution packaging policies recommend against bundling
|
||
dependencies. For example, Fedora installs wheel packages in the
|
||
<code class="docutils literal notranslate"><span class="pre">/usr/share/python-wheels/</span></code> directory and don’t install the
|
||
<code class="docutils literal notranslate"><span class="pre">ensurepip._bundled</span></code> package.</p>
|
||
<p>(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42856">bpo-42856</a>.)</p>
|
||
</li>
|
||
<li><p>Add a new <a class="reference internal" href="../using/configure.html#cmdoption-without-static-libpython"><code class="xref std std-option docutils literal notranslate"><span class="pre">configure</span> <span class="pre">--without-static-libpython</span> <span class="pre">option</span></code></a> to not build the <code class="docutils literal notranslate"><span class="pre">libpythonMAJOR.MINOR.a</span></code>
|
||
static library and not install the <code class="docutils literal notranslate"><span class="pre">python.o</span></code> object file.</p>
|
||
<p>(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43103">bpo-43103</a>.)</p>
|
||
</li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">configure</span></code> script now uses the <code class="docutils literal notranslate"><span class="pre">pkg-config</span></code> utility, if available,
|
||
to detect the location of Tcl/Tk headers and libraries. As before, those
|
||
locations can be explicitly specified with the <code class="docutils literal notranslate"><span class="pre">--with-tcltk-includes</span></code>
|
||
and <code class="docutils literal notranslate"><span class="pre">--with-tcltk-libs</span></code> configuration options.
|
||
(Contributed by Manolis Stamatogiannakis in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42603">bpo-42603</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../using/configure.html#cmdoption-with-openssl-rpath"><code class="xref std std-option docutils literal notranslate"><span class="pre">--with-openssl-rpath</span></code></a> option to <code class="docutils literal notranslate"><span class="pre">configure</span></code> script. The option
|
||
simplifies building Python with a custom OpenSSL installation, e.g.
|
||
<code class="docutils literal notranslate"><span class="pre">./configure</span> <span class="pre">--with-openssl=/path/to/openssl</span> <span class="pre">--with-openssl-rpath=auto</span></code>.
|
||
(Contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43466">bpo-43466</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="c-api-changes">
|
||
<h2>C API Changes<a class="headerlink" href="#c-api-changes" title="Link to this heading">¶</a></h2>
|
||
<section id="pep-652-maintaining-the-stable-abi">
|
||
<h3>PEP 652: Maintaining the Stable ABI<a class="headerlink" href="#pep-652-maintaining-the-stable-abi" title="Link to this heading">¶</a></h3>
|
||
<p>The Stable ABI (Application Binary Interface) for extension modules or
|
||
embedding Python is now explicitly defined.
|
||
<a class="reference internal" href="../c-api/stable.html#stable"><span class="std std-ref">C API Stability</span></a> describes C API and ABI stability guarantees along with best
|
||
practices for using the Stable ABI.</p>
|
||
<p>(Contributed by Petr Viktorin in <span class="target" id="index-36"></span><a class="pep reference external" href="https://peps.python.org/pep-0652/"><strong>PEP 652</strong></a> and <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43795">bpo-43795</a>.)</p>
|
||
</section>
|
||
<section id="id1">
|
||
<h3>New Features<a class="headerlink" href="#id1" title="Link to this heading">¶</a></h3>
|
||
<ul>
|
||
<li><p>The result of <a class="reference internal" href="../c-api/number.html#c.PyNumber_Index" title="PyNumber_Index"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Index()</span></code></a> now always has exact type <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>.
|
||
Previously, the result could have been an instance of a subclass of <code class="docutils literal notranslate"><span class="pre">int</span></code>.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40792">bpo-40792</a>.)</p></li>
|
||
<li><p>Add a new <a class="reference internal" href="../c-api/init_config.html#c.PyConfig.orig_argv" title="PyConfig.orig_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">orig_argv</span></code></a> member to the <a class="reference internal" href="../c-api/init_config.html#c.PyConfig" title="PyConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyConfig</span></code></a>
|
||
structure: the list of the original command line arguments passed to the
|
||
Python executable.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23427">bpo-23427</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../c-api/datetime.html#c.PyDateTime_DATE_GET_TZINFO" title="PyDateTime_DATE_GET_TZINFO"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDateTime_DATE_GET_TZINFO()</span></code></a> and
|
||
<a class="reference internal" href="../c-api/datetime.html#c.PyDateTime_TIME_GET_TZINFO" title="PyDateTime_TIME_GET_TZINFO"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDateTime_TIME_GET_TZINFO()</span></code></a> macros have been added for accessing
|
||
the <code class="docutils literal notranslate"><span class="pre">tzinfo</span></code> attributes of <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.datetime</span></code></a> and
|
||
<a class="reference internal" href="../library/datetime.html#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.time</span></code></a> objects.
|
||
(Contributed by Zackery Spytz in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30155">bpo-30155</a>.)</p></li>
|
||
<li><p>Add a <a class="reference internal" href="../c-api/codec.html#c.PyCodec_Unregister" title="PyCodec_Unregister"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCodec_Unregister()</span></code></a> function to unregister a codec
|
||
search function.
|
||
(Contributed by Hai Shi in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41842">bpo-41842</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../c-api/iter.html#c.PyIter_Send" title="PyIter_Send"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyIter_Send()</span></code></a> function was added to allow
|
||
sending value into iterator without raising <code class="docutils literal notranslate"><span class="pre">StopIteration</span></code> exception.
|
||
(Contributed by Vladimir Matveev in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41756">bpo-41756</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUTF8AndSize" title="PyUnicode_AsUTF8AndSize"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUTF8AndSize()</span></code></a> to the limited C API.
|
||
(Contributed by Alex Gaynor in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41784">bpo-41784</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../c-api/module.html#c.PyModule_AddObjectRef" title="PyModule_AddObjectRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_AddObjectRef()</span></code></a> function: similar to
|
||
<a class="reference internal" href="../c-api/module.html#c.PyModule_AddObject" title="PyModule_AddObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_AddObject()</span></code></a> but don’t steal a reference to the value on
|
||
success.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1635741">bpo-1635741</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../c-api/refcounting.html#c.Py_NewRef" title="Py_NewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewRef()</span></code></a> and <a class="reference internal" href="../c-api/refcounting.html#c.Py_XNewRef" title="Py_XNewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XNewRef()</span></code></a> functions to increment the
|
||
reference count of an object and return the object.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42262">bpo-42262</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../c-api/type.html#c.PyType_FromSpecWithBases" title="PyType_FromSpecWithBases"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpecWithBases()</span></code></a> and <a class="reference internal" href="../c-api/type.html#c.PyType_FromModuleAndSpec" title="PyType_FromModuleAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromModuleAndSpec()</span></code></a>
|
||
functions now accept a single class as the <em>bases</em> argument.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42423">bpo-42423</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../c-api/type.html#c.PyType_FromModuleAndSpec" title="PyType_FromModuleAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromModuleAndSpec()</span></code></a> function now accepts NULL <code class="docutils literal notranslate"><span class="pre">tp_doc</span></code>
|
||
slot.
|
||
(Contributed by Hai Shi in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41832">bpo-41832</a>.)</p></li>
|
||
<li><p>The <a class="reference internal" href="../c-api/type.html#c.PyType_GetSlot" title="PyType_GetSlot"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_GetSlot()</span></code></a> function can accept
|
||
<a class="reference internal" href="../c-api/typeobj.html#static-types"><span class="std std-ref">static types</span></a>.
|
||
(Contributed by Hai Shi and Petr Viktorin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41073">bpo-41073</a>.)</p></li>
|
||
<li><p>Add a new <a class="reference internal" href="../c-api/set.html#c.PySet_CheckExact" title="PySet_CheckExact"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySet_CheckExact()</span></code></a> function to the C-API to check if an
|
||
object is an instance of <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> but not an instance of a subtype.
|
||
(Contributed by Pablo Galindo in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43277">bpo-43277</a>.)</p></li>
|
||
<li><p>Add <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_SetInterruptEx" title="PyErr_SetInterruptEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_SetInterruptEx()</span></code></a> which allows passing a signal number
|
||
to simulate.
|
||
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43356">bpo-43356</a>.)</p></li>
|
||
<li><p>The limited C API is now supported if <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">Python is built in debug mode</span></a> (if the <code class="docutils literal notranslate"><span class="pre">Py_DEBUG</span></code> macro is defined). In the limited C API,
|
||
the <a class="reference internal" href="../c-api/refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> and <a class="reference internal" href="../c-api/refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> functions are now implemented
|
||
as opaque function
|
||
calls, rather than accessing directly the <a class="reference internal" href="../c-api/typeobj.html#c.PyObject.ob_refcnt" title="PyObject.ob_refcnt"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyObject.ob_refcnt</span></code></a>
|
||
member, if Python is built in debug mode and the <code class="docutils literal notranslate"><span class="pre">Py_LIMITED_API</span></code> macro
|
||
targets Python 3.10 or newer. It became possible to support the limited C API
|
||
in debug mode because the <a class="reference internal" href="../c-api/structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> structure is the same in release
|
||
and debug mode since Python 3.8 (see <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36465">bpo-36465</a>).</p>
|
||
<p>The limited C API is still not supported in the <a class="reference internal" href="../using/configure.html#cmdoption-with-trace-refs"><code class="xref std std-option docutils literal notranslate"><span class="pre">--with-trace-refs</span></code></a>
|
||
special build (<code class="docutils literal notranslate"><span class="pre">Py_TRACE_REFS</span></code> macro).
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43688">bpo-43688</a>.)</p>
|
||
</li>
|
||
<li><p>Add the <a class="reference internal" href="../c-api/structures.html#c.Py_Is" title="Py_Is"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Is(x,</span> <span class="pre">y)</span></code></a> function to test if the <em>x</em> object is
|
||
the <em>y</em> object, the same as <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">y</span></code> in Python. Add also the
|
||
<a class="reference internal" href="../c-api/structures.html#c.Py_IsNone" title="Py_IsNone"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_IsNone()</span></code></a>, <a class="reference internal" href="../c-api/structures.html#c.Py_IsTrue" title="Py_IsTrue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_IsTrue()</span></code></a>, <a class="reference internal" href="../c-api/structures.html#c.Py_IsFalse" title="Py_IsFalse"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_IsFalse()</span></code></a> functions to
|
||
test if an object is, respectively, the <code class="docutils literal notranslate"><span class="pre">None</span></code> singleton, the <code class="docutils literal notranslate"><span class="pre">True</span></code>
|
||
singleton or the <code class="docutils literal notranslate"><span class="pre">False</span></code> singleton.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43753">bpo-43753</a>.)</p></li>
|
||
<li><p>Add new functions to control the garbage collector from C code:
|
||
<a class="reference internal" href="../c-api/gcsupport.html#c.PyGC_Enable" title="PyGC_Enable"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGC_Enable()</span></code></a>,
|
||
<a class="reference internal" href="../c-api/gcsupport.html#c.PyGC_Disable" title="PyGC_Disable"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGC_Disable()</span></code></a>,
|
||
<a class="reference internal" href="../c-api/gcsupport.html#c.PyGC_IsEnabled" title="PyGC_IsEnabled"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGC_IsEnabled()</span></code></a>.
|
||
These functions allow to activate, deactivate and query the state of the garbage collector from C code without
|
||
having to import the <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> module.</p></li>
|
||
<li><p>Add a new <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_DISALLOW_INSTANTIATION" title="Py_TPFLAGS_DISALLOW_INSTANTIATION"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_DISALLOW_INSTANTIATION</span></code></a> type flag to disallow
|
||
creating type instances.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43916">bpo-43916</a>.)</p></li>
|
||
<li><p>Add a new <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE" title="Py_TPFLAGS_IMMUTABLETYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_IMMUTABLETYPE</span></code></a> type flag for creating immutable
|
||
type objects: type attributes cannot be set nor deleted.
|
||
(Contributed by Victor Stinner and Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43908">bpo-43908</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="id2">
|
||
<h3>Porting to Python 3.10<a class="headerlink" href="#id2" title="Link to this heading">¶</a></h3>
|
||
<ul>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">PY_SSIZE_T_CLEAN</span></code> macro must now be defined to use
|
||
<a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> and <a class="reference internal" href="../c-api/arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a> formats which use
|
||
<code class="docutils literal notranslate"><span class="pre">#</span></code>: <code class="docutils literal notranslate"><span class="pre">es#</span></code>, <code class="docutils literal notranslate"><span class="pre">et#</span></code>, <code class="docutils literal notranslate"><span class="pre">s#</span></code>, <code class="docutils literal notranslate"><span class="pre">u#</span></code>, <code class="docutils literal notranslate"><span class="pre">y#</span></code>, <code class="docutils literal notranslate"><span class="pre">z#</span></code>, <code class="docutils literal notranslate"><span class="pre">U#</span></code> and <code class="docutils literal notranslate"><span class="pre">Z#</span></code>.
|
||
See <a class="reference internal" href="../c-api/arg.html#arg-parsing"><span class="std std-ref">Parsing arguments and building values</span></a> and <span class="target" id="index-37"></span><a class="pep reference external" href="https://peps.python.org/pep-0353/"><strong>PEP 353</strong></a>.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40943">bpo-40943</a>.)</p></li>
|
||
<li><p>Since <a class="reference internal" href="../c-api/refcounting.html#c.Py_REFCNT" title="Py_REFCNT"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_REFCNT()</span></code></a> is changed to the inline static function,
|
||
<code class="docutils literal notranslate"><span class="pre">Py_REFCNT(obj)</span> <span class="pre">=</span> <span class="pre">new_refcnt</span></code> must be replaced with <code class="docutils literal notranslate"><span class="pre">Py_SET_REFCNT(obj,</span> <span class="pre">new_refcnt)</span></code>:
|
||
see <a class="reference internal" href="../c-api/refcounting.html#c.Py_SET_REFCNT" title="Py_SET_REFCNT"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SET_REFCNT()</span></code></a> (available since Python 3.9). For backward
|
||
compatibility, this macro can be used:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#if PY_VERSION_HEX < 0x030900A4</span>
|
||
<span class="c1"># define Py_SET_REFCNT(obj, refcnt) ((Py_REFCNT(obj) = (refcnt)), (void)0)</span>
|
||
<span class="c1">#endif</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39573">bpo-39573</a>.)</p>
|
||
</li>
|
||
<li><p>Calling <a class="reference internal" href="../c-api/dict.html#c.PyDict_GetItem" title="PyDict_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyDict_GetItem()</span></code></a> without <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> held had been allowed
|
||
for historical reason. It is no longer allowed.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=40839">bpo-40839</a>.)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyUnicode_FromUnicode(NULL,</span> <span class="pre">size)</span></code> and <code class="docutils literal notranslate"><span class="pre">PyUnicode_FromStringAndSize(NULL,</span> <span class="pre">size)</span></code>
|
||
raise <code class="docutils literal notranslate"><span class="pre">DeprecationWarning</span></code> now. Use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_New" title="PyUnicode_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_New()</span></code></a> to allocate
|
||
Unicode object without initial data.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36346">bpo-36346</a>.)</p></li>
|
||
<li><p>The private <code class="docutils literal notranslate"><span class="pre">_PyUnicode_Name_CAPI</span></code> structure of the PyCapsule API
|
||
<code class="docutils literal notranslate"><span class="pre">unicodedata.ucnhash_CAPI</span></code> has been moved to the internal C API.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42157">bpo-42157</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init.html#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a>, <a class="reference internal" href="../c-api/init.html#c.Py_GetPrefix" title="Py_GetPrefix"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPrefix()</span></code></a>, <a class="reference internal" href="../c-api/init.html#c.Py_GetExecPrefix" title="Py_GetExecPrefix"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetExecPrefix()</span></code></a>,
|
||
<a class="reference internal" href="../c-api/init.html#c.Py_GetProgramFullPath" title="Py_GetProgramFullPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramFullPath()</span></code></a>, <a class="reference internal" href="../c-api/init.html#c.Py_GetPythonHome" title="Py_GetPythonHome"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPythonHome()</span></code></a> and
|
||
<a class="reference internal" href="../c-api/init.html#c.Py_GetProgramName" title="Py_GetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramName()</span></code></a> functions now return <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before
|
||
<a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> (before Python is initialized). Use the new
|
||
<a class="reference internal" href="../c-api/init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a> API to get the <a class="reference internal" href="../c-api/init_config.html#init-path-config"><span class="std std-ref">Python Path Configuration</span></a>.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42260">bpo-42260</a>.)</p></li>
|
||
<li><p><a class="reference internal" href="../c-api/list.html#c.PyList_SET_ITEM" title="PyList_SET_ITEM"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_SET_ITEM()</span></code></a>, <a class="reference internal" href="../c-api/tuple.html#c.PyTuple_SET_ITEM" title="PyTuple_SET_ITEM"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SET_ITEM()</span></code></a> and
|
||
<a class="reference internal" href="../c-api/cell.html#c.PyCell_SET" title="PyCell_SET"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCell_SET()</span></code></a> macros can no longer be used as l-value or r-value.
|
||
For example, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">=</span> <span class="pre">PyList_SET_ITEM(a,</span> <span class="pre">b,</span> <span class="pre">c)</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">PyList_SET_ITEM(a,</span> <span class="pre">b,</span> <span class="pre">c)</span> <span class="pre">=</span> <span class="pre">x</span></code> now fail with a compiler error. It prevents
|
||
bugs like <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">(PyList_SET_ITEM</span> <span class="pre">(a,</span> <span class="pre">b,</span> <span class="pre">c)</span> <span class="pre"><</span> <span class="pre">0)</span> <span class="pre">...</span></code> test.
|
||
(Contributed by Zackery Spytz and Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30459">bpo-30459</a>.)</p></li>
|
||
<li><p>The non-limited API files <code class="docutils literal notranslate"><span class="pre">odictobject.h</span></code>, <code class="docutils literal notranslate"><span class="pre">parser_interface.h</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">picklebufobject.h</span></code>, <code class="docutils literal notranslate"><span class="pre">pyarena.h</span></code>, <code class="docutils literal notranslate"><span class="pre">pyctype.h</span></code>, <code class="docutils literal notranslate"><span class="pre">pydebug.h</span></code>,
|
||
<code class="docutils literal notranslate"><span class="pre">pyfpe.h</span></code>, and <code class="docutils literal notranslate"><span class="pre">pytime.h</span></code> have been moved to the <code class="docutils literal notranslate"><span class="pre">Include/cpython</span></code>
|
||
directory. These files must not be included directly, as they are already
|
||
included in <code class="docutils literal notranslate"><span class="pre">Python.h</span></code>; see <a class="reference internal" href="../c-api/intro.html#api-includes"><span class="std std-ref">Include Files</span></a>. If they have
|
||
been included directly, consider including <code class="docutils literal notranslate"><span class="pre">Python.h</span></code> instead.
|
||
(Contributed by Nicholas Sim in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35134">bpo-35134</a>.)</p></li>
|
||
<li><p>Use the <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE" title="Py_TPFLAGS_IMMUTABLETYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_IMMUTABLETYPE</span></code></a> type flag to create immutable type
|
||
objects. Do not rely on <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_HEAPTYPE" title="Py_TPFLAGS_HEAPTYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HEAPTYPE</span></code></a> to decide if a type
|
||
object is mutable or not; check if <a class="reference internal" href="../c-api/typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE" title="Py_TPFLAGS_IMMUTABLETYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_IMMUTABLETYPE</span></code></a> is set
|
||
instead.
|
||
(Contributed by Victor Stinner and Erlend E. Aasland in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43908">bpo-43908</a>.)</p></li>
|
||
<li><p>The undocumented function <code class="docutils literal notranslate"><span class="pre">Py_FrozenMain</span></code> has been removed from the
|
||
limited API. The function is mainly useful for custom builds of Python.
|
||
(Contributed by Petr Viktorin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26241">bpo-26241</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="id3">
|
||
<h3>Deprecated<a class="headerlink" href="#id3" title="Link to this heading">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">PyUnicode_InternImmortal()</span></code> function is now deprecated
|
||
and will be removed in Python 3.12: use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_InternInPlace" title="PyUnicode_InternInPlace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_InternInPlace()</span></code></a>
|
||
instead.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41692">bpo-41692</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="id4">
|
||
<h3>Removed<a class="headerlink" href="#id4" title="Link to this heading">¶</a></h3>
|
||
<ul>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre">Py_UNICODE_str*</span></code> functions manipulating <code class="docutils literal notranslate"><span class="pre">Py_UNICODE*</span></code> strings.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41123">bpo-41123</a>.)</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strlen</span></code>: use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GetLength" title="PyUnicode_GetLength"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetLength()</span></code></a> or
|
||
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_LENGTH" title="PyUnicode_GET_LENGTH"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_LENGTH</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strcat</span></code>: use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CopyCharacters" title="PyUnicode_CopyCharacters"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CopyCharacters()</span></code></a> or
|
||
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromFormat" title="PyUnicode_FromFormat"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromFormat()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strcpy</span></code>, <code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strncpy</span></code>: use
|
||
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CopyCharacters" title="PyUnicode_CopyCharacters"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CopyCharacters()</span></code></a> or <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Substring" title="PyUnicode_Substring"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Substring()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strcmp</span></code>: use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Compare" title="PyUnicode_Compare"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Compare()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strncmp</span></code>: use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Tailmatch" title="PyUnicode_Tailmatch"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Tailmatch()</span></code></a></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strchr</span></code>, <code class="docutils literal notranslate"><span class="pre">Py_UNICODE_strrchr</span></code>: use
|
||
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FindChar" title="PyUnicode_FindChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FindChar()</span></code></a></p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre">PyUnicode_GetMax()</span></code>. Please migrate to new (<span class="target" id="index-38"></span><a class="pep reference external" href="https://peps.python.org/pep-0393/"><strong>PEP 393</strong></a>) APIs.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41103">bpo-41103</a>.)</p></li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre">PyLong_FromUnicode()</span></code>. Please migrate to <a class="reference internal" href="../c-api/long.html#c.PyLong_FromUnicodeObject" title="PyLong_FromUnicodeObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_FromUnicodeObject()</span></code></a>.
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41103">bpo-41103</a>.)</p></li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre">PyUnicode_AsUnicodeCopy()</span></code>. Please use <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUCS4Copy" title="PyUnicode_AsUCS4Copy"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUCS4Copy()</span></code></a> or
|
||
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsWideCharString" title="PyUnicode_AsWideCharString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsWideCharString()</span></code></a>
|
||
(Contributed by Inada Naoki in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41103">bpo-41103</a>.)</p></li>
|
||
<li><p>Removed <code class="docutils literal notranslate"><span class="pre">_Py_CheckRecursionLimit</span></code> variable: it has been replaced by
|
||
<code class="docutils literal notranslate"><span class="pre">ceval.recursion_limit</span></code> of the <a class="reference internal" href="../c-api/init.html#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a> structure.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41834">bpo-41834</a>.)</p></li>
|
||
<li><p>Removed undocumented macros <code class="docutils literal notranslate"><span class="pre">Py_ALLOW_RECURSION</span></code> and
|
||
<code class="docutils literal notranslate"><span class="pre">Py_END_ALLOW_RECURSION</span></code> and the <code class="docutils literal notranslate"><span class="pre">recursion_critical</span></code> field of the
|
||
<a class="reference internal" href="../c-api/init.html#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a> structure.
|
||
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41936">bpo-41936</a>.)</p></li>
|
||
<li><p>Removed the undocumented <code class="docutils literal notranslate"><span class="pre">PyOS_InitInterrupts()</span></code> function. Initializing
|
||
Python already implicitly installs signal handlers: see
|
||
<a class="reference internal" href="../c-api/init_config.html#c.PyConfig.install_signal_handlers" title="PyConfig.install_signal_handlers"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.install_signal_handlers</span></code></a>.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41713">bpo-41713</a>.)</p></li>
|
||
<li><p>Remove the <code class="docutils literal notranslate"><span class="pre">PyAST_Validate()</span></code> function. It is no longer possible to build a
|
||
AST object (<code class="docutils literal notranslate"><span class="pre">mod_ty</span></code> type) with the public C API. The function was already
|
||
excluded from the limited C API (<span class="target" id="index-39"></span><a class="pep reference external" href="https://peps.python.org/pep-0384/"><strong>PEP 384</strong></a>).
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43244">bpo-43244</a>.)</p></li>
|
||
<li><p>Remove the <code class="docutils literal notranslate"><span class="pre">symtable.h</span></code> header file and the undocumented functions:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyST_GetScope()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PySymtable_Build()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PySymtable_BuildObject()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PySymtable_Free()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_SymtableString()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Py_SymtableStringObject()</span></code></p></li>
|
||
</ul>
|
||
<p>The <code class="docutils literal notranslate"><span class="pre">Py_SymtableString()</span></code> function was part the stable ABI by mistake but
|
||
it could not be used, because the <code class="docutils literal notranslate"><span class="pre">symtable.h</span></code> header file was excluded
|
||
from the limited C API.</p>
|
||
<p>Use Python <a class="reference internal" href="../library/symtable.html#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><code class="xref py py-mod docutils literal notranslate"><span class="pre">symtable</span></code></a> module instead.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43244">bpo-43244</a>.)</p>
|
||
</li>
|
||
<li><p>Remove <a class="reference internal" href="../c-api/veryhigh.html#c.PyOS_ReadlineFunctionPointer" title="PyOS_ReadlineFunctionPointer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ReadlineFunctionPointer()</span></code></a> from the limited C API headers
|
||
and from <code class="docutils literal notranslate"><span class="pre">python3.dll</span></code>, the library that provides the stable ABI on
|
||
Windows. Since the function takes a <code class="docutils literal notranslate"><span class="pre">FILE*</span></code> argument, its ABI stability
|
||
cannot be guaranteed.
|
||
(Contributed by Petr Viktorin in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43868">bpo-43868</a>.)</p></li>
|
||
<li><p>Remove <code class="docutils literal notranslate"><span class="pre">ast.h</span></code>, <code class="docutils literal notranslate"><span class="pre">asdl.h</span></code>, and <code class="docutils literal notranslate"><span class="pre">Python-ast.h</span></code> header files.
|
||
These functions were undocumented and excluded from the limited C API.
|
||
Most names defined by these header files were not prefixed by <code class="docutils literal notranslate"><span class="pre">Py</span></code> and so
|
||
could create names conflicts. For example, <code class="docutils literal notranslate"><span class="pre">Python-ast.h</span></code> defined a
|
||
<code class="docutils literal notranslate"><span class="pre">Yield</span></code> macro which was conflict with the <code class="docutils literal notranslate"><span class="pre">Yield</span></code> name used by the
|
||
Windows <code class="docutils literal notranslate"><span class="pre"><winbase.h></span></code> header. Use the Python <a class="reference internal" href="../library/ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module instead.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43244">bpo-43244</a>.)</p></li>
|
||
<li><p>Remove the compiler and parser functions using <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">_mod</span></code> type, because
|
||
the public AST C API was removed:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyAST_Compile()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyAST_CompileEx()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyAST_CompileObject()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyFuture_FromAST()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyFuture_FromASTObject()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyParser_ASTFromFile()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyParser_ASTFromFileObject()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyParser_ASTFromFilename()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyParser_ASTFromString()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyParser_ASTFromStringObject()</span></code></p></li>
|
||
</ul>
|
||
<p>These functions were undocumented and excluded from the limited C API.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43244">bpo-43244</a>.)</p>
|
||
</li>
|
||
<li><p>Remove the <code class="docutils literal notranslate"><span class="pre">pyarena.h</span></code> header file with functions:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyArena_New()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyArena_Free()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyArena_Malloc()</span></code></p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyArena_AddPyObject()</span></code></p></li>
|
||
</ul>
|
||
<p>These functions were undocumented, excluded from the limited C API, and were
|
||
only used internally by the compiler.
|
||
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43244">bpo-43244</a>.)</p>
|
||
</li>
|
||
<li><p>The <code class="docutils literal notranslate"><span class="pre">PyThreadState.use_tracing</span></code> member has been removed to optimize Python.
|
||
(Contributed by Mark Shannon in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43760">bpo-43760</a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="notable-security-feature-in-3-10-7">
|
||
<h2>Notable security feature in 3.10.7<a class="headerlink" href="#notable-security-feature-in-3-10-7" title="Link to this heading">¶</a></h2>
|
||
<p>Converting between <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> in bases other than 2
|
||
(binary), 4, 8 (octal), 16 (hexadecimal), or 32 such as base 10 (decimal)
|
||
now raises a <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the number of digits in string form is
|
||
above a limit to avoid potential denial of service attacks due to the
|
||
algorithmic complexity. This is a mitigation for <span class="target" id="index-40"></span><a class="cve reference external" href="https://www.cve.org/CVERecord?id=CVE-2020-10735"><strong>CVE 2020-10735</strong></a>.
|
||
This limit can be configured or disabled by environment variable, command
|
||
line flag, or <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> APIs. See the <a class="reference internal" href="../library/stdtypes.html#int-max-str-digits"><span class="std std-ref">integer string conversion
|
||
length limitation</span></a> documentation. The default limit
|
||
is 4300 digits in string form.</p>
|
||
</section>
|
||
<section id="notable-security-feature-in-3-10-8">
|
||
<h2>Notable security feature in 3.10.8<a class="headerlink" href="#notable-security-feature-in-3-10-8" title="Link to this heading">¶</a></h2>
|
||
<p>The deprecated <code class="xref py py-mod docutils literal notranslate"><span class="pre">mailcap</span></code> module now refuses to inject unsafe text
|
||
(filenames, MIME types, parameters) into shell commands. Instead of using such
|
||
text, it will warn and act as if a match was not found (or for test commands,
|
||
as if the test failed).
|
||
(Contributed by Petr Viktorin in <a class="reference external" href="https://github.com/python/cpython/issues/98966">gh-98966</a>.)</p>
|
||
</section>
|
||
<section id="notable-changes-in-3-10-12">
|
||
<h2>Notable changes in 3.10.12<a class="headerlink" href="#notable-changes-in-3-10-12" title="Link to this heading">¶</a></h2>
|
||
<section id="tarfile">
|
||
<h3>tarfile<a class="headerlink" href="#tarfile" title="Link to this heading">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>The extraction methods in <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a>, and <a class="reference internal" href="../library/shutil.html#shutil.unpack_archive" title="shutil.unpack_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.unpack_archive()</span></code></a>,
|
||
have a new a <em>filter</em> argument that allows limiting tar features than may be
|
||
surprising or dangerous, such as creating files outside the destination
|
||
directory.
|
||
See <a class="reference internal" href="../library/tarfile.html#tarfile-extraction-filter"><span class="std std-ref">Extraction filters</span></a> for details.
|
||
In Python 3.12, use without the <em>filter</em> argument will show a
|
||
<a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>.
|
||
In Python 3.14, the default will switch to <code class="docutils literal notranslate"><span class="pre">'data'</span></code>.
|
||
(Contributed by Petr Viktorin in <span class="target" id="index-41"></span><a class="pep reference external" href="https://peps.python.org/pep-0706/"><strong>PEP 706</strong></a>.)</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
<div class="clearer"></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="Main">
|
||
<div class="sphinxsidebarwrapper">
|
||
<div>
|
||
<h3><a href="../contents.html">Table of Contents</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">What’s New In Python 3.10</a><ul>
|
||
<li><a class="reference internal" href="#summary-release-highlights">Summary – Release highlights</a></li>
|
||
<li><a class="reference internal" href="#new-features">New Features</a><ul>
|
||
<li><a class="reference internal" href="#parenthesized-context-managers">Parenthesized context managers</a></li>
|
||
<li><a class="reference internal" href="#better-error-messages">Better error messages</a><ul>
|
||
<li><a class="reference internal" href="#syntaxerrors">SyntaxErrors</a></li>
|
||
<li><a class="reference internal" href="#indentationerrors">IndentationErrors</a></li>
|
||
<li><a class="reference internal" href="#attributeerrors">AttributeErrors</a></li>
|
||
<li><a class="reference internal" href="#nameerrors">NameErrors</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#pep-626-precise-line-numbers-for-debugging-and-other-tools">PEP 626: Precise line numbers for debugging and other tools</a></li>
|
||
<li><a class="reference internal" href="#pep-634-structural-pattern-matching">PEP 634: Structural Pattern Matching</a><ul>
|
||
<li><a class="reference internal" href="#syntax-and-operations">Syntax and operations</a></li>
|
||
<li><a class="reference internal" href="#declarative-approach">Declarative approach</a></li>
|
||
<li><a class="reference internal" href="#simple-pattern-match-to-a-literal">Simple pattern: match to a literal</a><ul>
|
||
<li><a class="reference internal" href="#behavior-without-the-wildcard">Behavior without the wildcard</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#patterns-with-a-literal-and-variable">Patterns with a literal and variable</a></li>
|
||
<li><a class="reference internal" href="#patterns-and-classes">Patterns and classes</a><ul>
|
||
<li><a class="reference internal" href="#patterns-with-positional-parameters">Patterns with positional parameters</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#nested-patterns">Nested patterns</a></li>
|
||
<li><a class="reference internal" href="#complex-patterns-and-the-wildcard">Complex patterns and the wildcard</a></li>
|
||
<li><a class="reference internal" href="#guard">Guard</a></li>
|
||
<li><a class="reference internal" href="#other-key-features">Other Key Features</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optional-encodingwarning-and-encoding-locale-option">Optional <code class="docutils literal notranslate"><span class="pre">EncodingWarning</span></code> and <code class="docutils literal notranslate"><span class="pre">encoding="locale"</span></code> option</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#new-features-related-to-type-hints">New Features Related to Type Hints</a><ul>
|
||
<li><a class="reference internal" href="#pep-604-new-type-union-operator">PEP 604: New Type Union Operator</a></li>
|
||
<li><a class="reference internal" href="#pep-612-parameter-specification-variables">PEP 612: Parameter Specification Variables</a></li>
|
||
<li><a class="reference internal" href="#pep-613-typealias">PEP 613: TypeAlias</a></li>
|
||
<li><a class="reference internal" href="#pep-647-user-defined-type-guards">PEP 647: User-Defined Type Guards</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
|
||
<li><a class="reference internal" href="#new-modules">New Modules</a></li>
|
||
<li><a class="reference internal" href="#improved-modules">Improved Modules</a><ul>
|
||
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
|
||
<li><a class="reference internal" href="#argparse">argparse</a></li>
|
||
<li><a class="reference internal" href="#array">array</a></li>
|
||
<li><a class="reference internal" href="#asynchat-asyncore-smtpd">asynchat, asyncore, smtpd</a></li>
|
||
<li><a class="reference internal" href="#base64">base64</a></li>
|
||
<li><a class="reference internal" href="#bdb">bdb</a></li>
|
||
<li><a class="reference internal" href="#bisect">bisect</a></li>
|
||
<li><a class="reference internal" href="#codecs">codecs</a></li>
|
||
<li><a class="reference internal" href="#collections-abc">collections.abc</a></li>
|
||
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
|
||
<li><a class="reference internal" href="#curses">curses</a></li>
|
||
<li><a class="reference internal" href="#dataclasses">dataclasses</a><ul>
|
||
<li><a class="reference internal" href="#slots">__slots__</a></li>
|
||
<li><a class="reference internal" href="#keyword-only-fields">Keyword-only fields</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#distutils">distutils</a></li>
|
||
<li><a class="reference internal" href="#doctest">doctest</a></li>
|
||
<li><a class="reference internal" href="#encodings">encodings</a></li>
|
||
<li><a class="reference internal" href="#enum">enum</a></li>
|
||
<li><a class="reference internal" href="#fileinput">fileinput</a></li>
|
||
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
|
||
<li><a class="reference internal" href="#gc">gc</a></li>
|
||
<li><a class="reference internal" href="#glob">glob</a></li>
|
||
<li><a class="reference internal" href="#hashlib">hashlib</a></li>
|
||
<li><a class="reference internal" href="#hmac">hmac</a></li>
|
||
<li><a class="reference internal" href="#idle-and-idlelib">IDLE and idlelib</a></li>
|
||
<li><a class="reference internal" href="#importlib-metadata">importlib.metadata</a></li>
|
||
<li><a class="reference internal" href="#inspect">inspect</a></li>
|
||
<li><a class="reference internal" href="#itertools">itertools</a></li>
|
||
<li><a class="reference internal" href="#linecache">linecache</a></li>
|
||
<li><a class="reference internal" href="#os">os</a></li>
|
||
<li><a class="reference internal" href="#os-path">os.path</a></li>
|
||
<li><a class="reference internal" href="#pathlib">pathlib</a></li>
|
||
<li><a class="reference internal" href="#platform">platform</a></li>
|
||
<li><a class="reference internal" href="#pprint">pprint</a></li>
|
||
<li><a class="reference internal" href="#py-compile">py_compile</a></li>
|
||
<li><a class="reference internal" href="#pyclbr">pyclbr</a></li>
|
||
<li><a class="reference internal" href="#shelve">shelve</a></li>
|
||
<li><a class="reference internal" href="#statistics">statistics</a></li>
|
||
<li><a class="reference internal" href="#site">site</a></li>
|
||
<li><a class="reference internal" href="#socket">socket</a></li>
|
||
<li><a class="reference internal" href="#ssl">ssl</a></li>
|
||
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
|
||
<li><a class="reference internal" href="#sys">sys</a></li>
|
||
<li><a class="reference internal" href="#thread">_thread</a></li>
|
||
<li><a class="reference internal" href="#threading">threading</a></li>
|
||
<li><a class="reference internal" href="#traceback">traceback</a></li>
|
||
<li><a class="reference internal" href="#types">types</a></li>
|
||
<li><a class="reference internal" href="#typing">typing</a></li>
|
||
<li><a class="reference internal" href="#unittest">unittest</a></li>
|
||
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
|
||
<li><a class="reference internal" href="#xml">xml</a></li>
|
||
<li><a class="reference internal" href="#zipimport">zipimport</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
|
||
<li><a class="reference internal" href="#deprecated">Deprecated</a></li>
|
||
<li><a class="reference internal" href="#removed">Removed</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-10">Porting to Python 3.10</a><ul>
|
||
<li><a class="reference internal" href="#changes-in-the-python-syntax">Changes in the Python syntax</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-python-api">Changes in the Python API</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-c-api">Changes in the C API</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#cpython-bytecode-changes">CPython bytecode changes</a></li>
|
||
<li><a class="reference internal" href="#build-changes">Build Changes</a></li>
|
||
<li><a class="reference internal" href="#c-api-changes">C API Changes</a><ul>
|
||
<li><a class="reference internal" href="#pep-652-maintaining-the-stable-abi">PEP 652: Maintaining the Stable ABI</a></li>
|
||
<li><a class="reference internal" href="#id1">New Features</a></li>
|
||
<li><a class="reference internal" href="#id2">Porting to Python 3.10</a></li>
|
||
<li><a class="reference internal" href="#id3">Deprecated</a></li>
|
||
<li><a class="reference internal" href="#id4">Removed</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-10-7">Notable security feature in 3.10.7</a></li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-10-8">Notable security feature in 3.10.8</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-10-12">Notable changes in 3.10.12</a><ul>
|
||
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
<div>
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="3.11.html"
|
||
title="previous chapter">What’s New In Python 3.11</a></p>
|
||
</div>
|
||
<div>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="3.9.html"
|
||
title="next chapter">What’s New In Python 3.9</a></p>
|
||
</div>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/3.10.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
<div id="sidebarbutton" title="Collapse sidebar">
|
||
<span>«</span>
|
||
</div>
|
||
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="Related">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
>index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.9.html" title="What’s New In Python 3.9"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.11.html" title="What’s New In Python 3.11"
|
||
>previous</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.13.3 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> »</li>
|
||
<li class="nav-item nav-item-this"><a href="">What’s New In Python 3.10</a></li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
|
||
<input type="submit" value="Go" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
<li class="right">
|
||
<label class="theme-selector-label">
|
||
Theme
|
||
<select class="theme-selector" oninput="activateTheme(this.value)">
|
||
<option value="auto" selected>Auto</option>
|
||
<option value="light">Light</option>
|
||
<option value="dark">Dark</option>
|
||
</select>
|
||
</label> |</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
©
|
||
<a href="../copyright.html">
|
||
|
||
Copyright
|
||
|
||
</a>
|
||
2001-2025, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
See <a href="/license.html">History and License</a> for more information.<br />
|
||
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
Last updated on Apr 08, 2025 (14:33 UTC).
|
||
|
||
<a href="/bugs.html">Found a bug</a>?
|
||
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |