1639 lines
307 KiB
HTML
1639 lines
307 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>Python 3.8 有什么新变化 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="Python 3.7 有什么新变化" href="3.7.html" />
|
||
<link rel="prev" title="Python的新变化" href="index.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.8.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<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>
|
||
|
||
</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" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<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"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">Python 3.8 有什么新变化</a><ul>
|
||
<li><a class="reference internal" href="#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||
<li><a class="reference internal" href="#new-features">新的特性</a><ul>
|
||
<li><a class="reference internal" href="#assignment-expressions">赋值表达式</a></li>
|
||
<li><a class="reference internal" href="#positional-only-parameters">仅限位置形参</a></li>
|
||
<li><a class="reference internal" href="#parallel-filesystem-cache-for-compiled-bytecode-files">用于已编译字节码文件的并行文件系统缓存</a></li>
|
||
<li><a class="reference internal" href="#debug-build-uses-the-same-abi-as-release-build">调试构建使用与发布构建相同的 ABI</a></li>
|
||
<li><a class="reference internal" href="#f-strings-support-for-self-documenting-expressions-and-debugging">f-字符串支持 <code class="docutils literal notranslate"><span class="pre">=</span></code> 用于自动记录表达式和调试文档</a></li>
|
||
<li><a class="reference internal" href="#pep-578-python-runtime-audit-hooks">PEP 578: Python 运行时审核钩子</a></li>
|
||
<li><a class="reference internal" href="#pep-587-python-initialization-configuration">PEP 587: Python 初始化配置</a></li>
|
||
<li><a class="reference internal" href="#vectorcall-a-fast-calling-protocol-for-cpython">Vectorcall: 用于 CPython 的快速调用协议</a></li>
|
||
<li><a class="reference internal" href="#pickle-protocol-5-with-out-of-band-data-buffers">具有外部数据缓冲区的 pickle 协议 5</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
|
||
<li><a class="reference internal" href="#new-modules">新增模块</a></li>
|
||
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
|
||
<li><a class="reference internal" href="#ast">ast</a></li>
|
||
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
|
||
<li><a class="reference internal" href="#builtins">builtins</a></li>
|
||
<li><a class="reference internal" href="#collections">collections</a></li>
|
||
<li><a class="reference internal" href="#cprofile">cProfile</a></li>
|
||
<li><a class="reference internal" href="#csv">csv</a></li>
|
||
<li><a class="reference internal" href="#curses">curses</a></li>
|
||
<li><a class="reference internal" href="#ctypes">ctypes</a></li>
|
||
<li><a class="reference internal" href="#datetime">datetime</a></li>
|
||
<li><a class="reference internal" href="#functools">functools</a></li>
|
||
<li><a class="reference internal" href="#gc">gc</a></li>
|
||
<li><a class="reference internal" href="#gettext">gettext</a></li>
|
||
<li><a class="reference internal" href="#gzip">gzip</a></li>
|
||
<li><a class="reference internal" href="#idle-and-idlelib">IDLE 与 idlelib</a></li>
|
||
<li><a class="reference internal" href="#inspect">inspect</a></li>
|
||
<li><a class="reference internal" href="#io">io</a></li>
|
||
<li><a class="reference internal" href="#itertools">itertools</a></li>
|
||
<li><a class="reference internal" href="#json-tool">json.tool</a></li>
|
||
<li><a class="reference internal" href="#logging">logging</a></li>
|
||
<li><a class="reference internal" href="#math">math</a></li>
|
||
<li><a class="reference internal" href="#mmap">mmap</a></li>
|
||
<li><a class="reference internal" href="#multiprocessing">multiprocessing</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="#pickle">pickle</a></li>
|
||
<li><a class="reference internal" href="#plistlib">plistlib</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="#shlex">shlex</a></li>
|
||
<li><a class="reference internal" href="#shutil">shutil</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="#statistics">statistics</a></li>
|
||
<li><a class="reference internal" href="#sys">sys</a></li>
|
||
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
|
||
<li><a class="reference internal" href="#tempfile">tempfile</a></li>
|
||
<li><a class="reference internal" href="#threading">threading</a></li>
|
||
<li><a class="reference internal" href="#tokenize">tokenize</a></li>
|
||
<li><a class="reference internal" href="#tkinter">tkinter</a></li>
|
||
<li><a class="reference internal" href="#time">time</a></li>
|
||
<li><a class="reference internal" href="#typing">typing</a></li>
|
||
<li><a class="reference internal" href="#unicodedata">unicodedata</a></li>
|
||
<li><a class="reference internal" href="#unittest">unittest</a></li>
|
||
<li><a class="reference internal" href="#venv">venv</a></li>
|
||
<li><a class="reference internal" href="#weakref">weakref</a></li>
|
||
<li><a class="reference internal" href="#xml">xml</a></li>
|
||
<li><a class="reference internal" href="#xmlrpc">xmlrpc</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
|
||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||
<li><a class="reference internal" href="#deprecated">弃用</a></li>
|
||
<li><a class="reference internal" href="#api-and-feature-removals">API 与特性的移除</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-8">移植到 Python 3.8</a><ul>
|
||
<li><a class="reference internal" href="#changes-in-python-behavior">Python 行为的改变</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-python-api">Python API 的变化</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-c-api">C API 的变化</a></li>
|
||
<li><a class="reference internal" href="#cpython-bytecode-changes">CPython 字节码的改变</a></li>
|
||
<li><a class="reference internal" href="#demos-and-tools">演示和工具</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-1">Python 3.8.1 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-2">Python 3.8.2 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-3">Python 3.8.3 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-8">Python 3.8.8 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-9">Python 3.8.9 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-10">Python 3.8.10 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#macos-11-0-big-sur-and-apple-silicon-mac-support">macOS 11.0 (Big Sur) 与 Apple Silicon Mac 支持</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#id1">Python 3.8.10 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-12">Python 3.8.12 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#id2">Python API 的变化</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-8-14">3.8.14 中的重要安全特性</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-8-17">Notable Changes in 3.8.17</a><ul>
|
||
<li><a class="reference internal" href="#id3">tarfile</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-8-20">Notable changes in 3.8.20</a><ul>
|
||
<li><a class="reference internal" href="#ipaddress">ipaddress</a></li>
|
||
<li><a class="reference internal" href="#email">email</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="index.html"
|
||
title="上一章">Python的新变化</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="3.7.html"
|
||
title="下一章">Python 3.7 有什么新变化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/3.8.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/whatsnew/3.8.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.7.html" title="Python 3.7 有什么新变化"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="index.html" title="Python的新变化"
|
||
accesskey="P">上一页</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.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python的新变化</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</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-8">
|
||
<h1>Python 3.8 有什么新变化<a class="headerlink" href="#what-s-new-in-python-3-8" title="永久链接至标题">¶</a></h1>
|
||
<dl class="field-list simple">
|
||
<dt class="field-odd">编者</dt>
|
||
<dd class="field-odd"><p>Raymond Hettinger(译者:wh2099 at outlook dot com)</p>
|
||
</dd>
|
||
</dl>
|
||
<p>本文解释了 Python 3.8 相比 3.7 的新增特性。 完整的详情可参阅 <a class="reference internal" href="changelog.html#changelog"><span class="std std-ref">更新日志</span></a>。</p>
|
||
<p>Python 3.8已于2019年10月14日发布。</p>
|
||
<section id="summary-release-highlights">
|
||
<h2>摘要 -- 发布重点<a class="headerlink" href="#summary-release-highlights" title="永久链接至标题">¶</a></h2>
|
||
</section>
|
||
<section id="new-features">
|
||
<h2>新的特性<a class="headerlink" href="#new-features" title="永久链接至标题">¶</a></h2>
|
||
<section id="assignment-expressions">
|
||
<h3>赋值表达式<a class="headerlink" href="#assignment-expressions" title="永久链接至标题">¶</a></h3>
|
||
<p>新增的语法 <code class="docutils literal notranslate"><span class="pre">:=</span></code> 可在表达式内部为变量赋值。 它被昵称为“海象运算符”因为它很像是 <a class="reference external" href="https://en.wikipedia.org/wiki/Walrus#/media/File:Pacific_Walrus_-_Bull_(8247646168).jpg">海象的眼睛和长牙</a>。</p>
|
||
<p>在这个示例中,赋值表达式可以避免调用 <a class="reference internal" href="../library/functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a> 两次:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">:=</span> <span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">))</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"List is too long (</span><span class="si">{</span><span class="n">n</span><span class="si">}</span><span class="s2"> elements, expected <= 10)"</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>类似的益处还可出现在正则表达式匹配中需要使用两次匹配对象的情况中,一次检测用于匹配是否发生,另一次用于提取子分组:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">discount</span> <span class="o">=</span> <span class="mf">0.0</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">mo</span> <span class="o">:=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s1">'(\d+)</span><span class="si">% d</span><span class="s1">iscount'</span><span class="p">,</span> <span class="n">advertisement</span><span class="p">)):</span>
|
||
<span class="n">discount</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">mo</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="o">/</span> <span class="mf">100.0</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>此运算符也适用于配合 while 循环计算一个值来检测循环是否终止,而同一个值又在循环体中再次被使用的情况:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Loop over fixed length blocks</span>
|
||
<span class="k">while</span> <span class="p">(</span><span class="n">block</span> <span class="o">:=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">256</span><span class="p">))</span> <span class="o">!=</span> <span class="s1">''</span><span class="p">:</span>
|
||
<span class="n">process</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>另一个值得介绍的用例出现于列表推导式中,在筛选条件中计算一个值,而同一个值又在表达式中需要被使用:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">clean_name</span><span class="o">.</span><span class="n">title</span><span class="p">()</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="n">clean_name</span> <span class="o">:=</span> <span class="n">normalize</span><span class="p">(</span><span class="s1">'NFC'</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span> <span class="ow">in</span> <span class="n">allowed_names</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>请尽量将海象运算符的使用限制在清晰的场合中,以降低复杂性并提升可读性。</p>
|
||
<p>请参阅 <span class="target" id="index-53"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0572"><strong>PEP 572</strong></a> 了解详情。</p>
|
||
<p>(由 Morehouse 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35224">bpo-35224</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="positional-only-parameters">
|
||
<h3>仅限位置形参<a class="headerlink" href="#positional-only-parameters" title="永久链接至标题">¶</a></h3>
|
||
<p>新增了一个函数形参语法 <code class="docutils literal notranslate"><span class="pre">/</span></code> 用来指明某些函数形参必须使用仅限位置而非关键字参数的形式。 这种标记语法与通过 <code class="docutils literal notranslate"><span class="pre">help()</span></code> 所显示的使用 Larry Hastings 的 <a class="reference external" href="https://docs.python.org/3/howto/clinic.html">Argument Clinic</a> 工具标记的 C 函数相同。</p>
|
||
<p>在下面的例子中,形参 <em>a</em> 和 <em>b</em> 为仅限位置形参,<em>c</em> 或 <em>d</em> 可以是位置形参或关键字形参,而 <em>e</em> 或 <em>f</em> 要求为关键字形参:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">f</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>以下均为合法的调用:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">e</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>但是,以下均为不合法的调用:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">e</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span> <span class="c1"># b cannot be a keyword argument</span>
|
||
<span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="n">f</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span> <span class="c1"># e must be a keyword argument</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这种标记形式的一个用例是它允许纯 Python 函数完整模拟现有的用 C 代码编写的函数的行为。 例如,内置的 <a class="reference internal" href="../library/functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a> 函数不接受关键字参数:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">divmod</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">/</span><span class="p">):</span>
|
||
<span class="s2">"Emulate the built in divmod() function"</span>
|
||
<span class="k">return</span> <span class="p">(</span><span class="n">a</span> <span class="o">//</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span> <span class="o">%</span> <span class="n">b</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>另一个用例是在不需要形参名称时排除关键字参数。 例如,内置的 <a class="reference internal" href="../library/functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a> 函数的签名为 <code class="docutils literal notranslate"><span class="pre">len(obj,</span> <span class="pre">/)</span></code>。 这可以排除如下这种笨拙的调用形式:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">len</span><span class="p">(</span><span class="n">obj</span><span class="o">=</span><span class="s1">'hello'</span><span class="p">)</span> <span class="c1"># The "obj" keyword argument impairs readability</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>另一个益处是将形参标记为仅限位置形参将允许在未来修改形参名而不会破坏客户的代码。 例如,在 <a class="reference internal" href="../library/statistics.html#module-statistics" title="statistics: Mathematical statistics functions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">statistics</span></code></a> 模块中,形参名 <em>dist</em> 在未来可能被修改。 这使得以下函数描述成为可能:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">quantiles</span><span class="p">(</span><span class="n">dist</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'exclusive'</span><span class="p">)</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>由于在 <code class="docutils literal notranslate"><span class="pre">/</span></code> 左侧的形参不会被公开为可用关键字,其他形参名仍可在 <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> 中使用:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
||
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>
|
||
<span class="gp">...</span>
|
||
<span class="gp">>>> </span><span class="n">f</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">c</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># a and b are used in two ways</span>
|
||
<span class="go">10 20 {'a': 1, 'b': 2, 'c': 3}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这极大地简化了需要接受任意关键字参数的函数和方法的实现。 例如,以下是一段摘自 <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> 模块的代码:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Counter</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
|
||
|
||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span>
|
||
<span class="c1"># Note "iterable" is a possible keyword argument</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>请参阅 <span class="target" id="index-54"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0570"><strong>PEP 570</strong></a> 了解详情。</p>
|
||
<p>(由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36540">bpo-36540</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="parallel-filesystem-cache-for-compiled-bytecode-files">
|
||
<h3>用于已编译字节码文件的并行文件系统缓存<a class="headerlink" href="#parallel-filesystem-cache-for-compiled-bytecode-files" title="永久链接至标题">¶</a></h3>
|
||
<p>新增的 <span class="target" id="index-55"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPYCACHEPREFIX"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPYCACHEPREFIX</span></code></a> 设置 (也可使用 <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">pycache_prefix</span></code>) 可将隐式的字节码缓存配置为使用单独的并行文件系统树,而不是默认的每个源代码目录下的 <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> 子目录。</p>
|
||
<p>缓存的位置会在 <a class="reference internal" href="../library/sys.html#sys.pycache_prefix" title="sys.pycache_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.pycache_prefix</span></code></a> 中报告 (<a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> 表示默认位置即 <code class="docutils literal notranslate"><span class="pre">__pycache__</span></code> 子目录)。</p>
|
||
<p>(由 Carl Meyer 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33499">bpo-33499</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="debug-build-uses-the-same-abi-as-release-build">
|
||
<h3>调试构建使用与发布构建相同的 ABI<a class="headerlink" href="#debug-build-uses-the-same-abi-as-release-build" title="永久链接至标题">¶</a></h3>
|
||
<p>Python 现在不论是以发布模式还是调试模式进行构建都将使用相同的 ABI。 在 Unix 上,当 Python 以调试模式构建时,现在将可以加载以发布模式构建的 C 扩展和使用稳定版 ABI 构建的 C 扩展。</p>
|
||
<p>发布构建和调试构建现在都是 ABI 兼容的:定义 <code class="docutils literal notranslate"><span class="pre">Py_DEBUG</span></code> 宏不会再启用 <code class="docutils literal notranslate"><span class="pre">Py_TRACE_REFS</span></code> 宏,它引入了唯一的 ABI 不兼容性。 <code class="docutils literal notranslate"><span class="pre">Py_TRACE_REFS</span></code> 宏添加了 <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getobjects()</span></code> 函数和 <span class="target" id="index-56"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDUMPREFS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDUMPREFS</span></code></a> 环境变量,它可以使用新的 <code class="docutils literal notranslate"><span class="pre">./configure</span> <span class="pre">--with-trace-refs</span></code> 构建选项来设置。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36465">bpo-36465</a> 中贡献。)</p>
|
||
<p>在 Unix 上,C 扩展不会再被链接到 libpython,但 Android 和 Cygwin 例外。 现在静态链接的 Python 将可以加载使用共享库 Python 构建的 C 扩展。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=21536">bpo-21536</a> 中贡献。)</p>
|
||
<p>在 Unix 上,当 Python 以调试模式构建时,导入操作现在也会查找在发布模式下编译的 C 扩展以及使用稳定版 ABI 编译的 C 扩展。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36722">bpo-36722</a> 中贡献。)</p>
|
||
<p>要将 Python 嵌入到一个应用中,必须将新增的 <code class="docutils literal notranslate"><span class="pre">--embed</span></code> 选项传给 <code class="docutils literal notranslate"><span class="pre">python3-config</span> <span class="pre">--libs</span> <span class="pre">--embed</span></code> 以获得 <code class="docutils literal notranslate"><span class="pre">-lpython3.8</span></code> (将应用链接到 libpython)。 要同时支持 3.8 和旧版本,请先尝试 <code class="docutils literal notranslate"><span class="pre">python3-config</span> <span class="pre">--libs</span> <span class="pre">--embed</span></code> 并在此命令失败时回退到 <code class="docutils literal notranslate"><span class="pre">python3-config</span> <span class="pre">--libs</span></code> (即不带 <code class="docutils literal notranslate"><span class="pre">--embed</span></code>)。</p>
|
||
<p>增加一个 pkg-config <code class="docutils literal notranslate"><span class="pre">python-3.8-embed</span></code> 模块用来将 Python 嵌入到一个应用中: <code class="docutils literal notranslate"><span class="pre">pkg-config</span> <span class="pre">python-3.8-embed</span> <span class="pre">--libs</span></code> 包含 <code class="docutils literal notranslate"><span class="pre">-lpython3.8</span></code>。 要同时支持 3.8 和旧版本,请先尝试 <code class="docutils literal notranslate"><span class="pre">pkg-config</span> <span class="pre">python-X.Y-embed</span> <span class="pre">--libs</span></code> 并在此命令失败时回退到 <code class="docutils literal notranslate"><span class="pre">pkg-config</span> <span class="pre">python-X.Y</span> <span class="pre">--libs</span></code> (即不带 <code class="docutils literal notranslate"><span class="pre">--embed</span></code>) (请将 <code class="docutils literal notranslate"><span class="pre">X.Y</span></code> 替换为 Python 版本号)。</p>
|
||
<p>另一方面,<code class="docutils literal notranslate"><span class="pre">pkg-config</span> <span class="pre">python3.8</span> <span class="pre">--libs</span></code> 不再包含 <code class="docutils literal notranslate"><span class="pre">-lpython3.8</span></code>。 C 扩展不可被链接到 libpython (但 Android 和 Cygwin 例外,这两者的情况由脚本处理);此改变是故意被设为向下不兼容的。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36721">bpo-36721</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="f-strings-support-for-self-documenting-expressions-and-debugging">
|
||
<h3>f-字符串支持 <code class="docutils literal notranslate"><span class="pre">=</span></code> 用于自动记录表达式和调试文档<a class="headerlink" href="#f-strings-support-for-self-documenting-expressions-and-debugging" title="永久链接至标题">¶</a></h3>
|
||
<p>增加 <code class="docutils literal notranslate"><span class="pre">=</span></code> 说明符用于 <a class="reference internal" href="../glossary.html#term-f-string"><span class="xref std std-term">f-string</span></a>。 形式为 <code class="docutils literal notranslate"><span class="pre">f'{expr=}'</span></code> 的 f-字符串将扩展表示为表达式文本,加一个等于号,再加表达式的求值结果。 例如:</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">user</span> <span class="o">=</span> <span class="s1">'eric_idle'</span>
|
||
<span class="gp">>>> </span><span class="n">member_since</span> <span class="o">=</span> <span class="n">date</span><span class="p">(</span><span class="mi">1975</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">31</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">user</span><span class="si">=}</span><span class="s1"> </span><span class="si">{</span><span class="n">member_since</span><span class="si">=}</span><span class="s1">'</span>
|
||
<span class="go">"user='eric_idle' member_since=datetime.date(1975, 7, 31)"</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>通常的 <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">f-字符串格式说明符</span></a> 允许更细致地控制所要显示的表达式结果:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">delta</span> <span class="o">=</span> <span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span> <span class="o">-</span> <span class="n">member_since</span>
|
||
<span class="gp">>>> </span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">user</span><span class="si">=!s}</span><span class="s1"> </span><span class="si">{</span><span class="n">delta</span><span class="o">.</span><span class="n">days</span><span class="si">=:</span><span class="s1">,d</span><span class="si">}</span><span class="s1">'</span>
|
||
<span class="go">'user=eric_idle delta.days=16,075'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p><code class="docutils literal notranslate"><span class="pre">=</span></code> 说明符将输出整个表达式,以便详细演示计算过程:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">theta</span><span class="si">=}</span><span class="s1"> </span><span class="si">{</span><span class="n">cos</span><span class="p">(</span><span class="n">radians</span><span class="p">(</span><span class="n">theta</span><span class="p">))</span><span class="si">=:</span><span class="s1">.3f</span><span class="si">}</span><span class="s1">'</span><span class="p">)</span>
|
||
<span class="go">theta=30 cos(radians(theta))=0.866</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Eric V. Smith 和 Larry Hastings 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36817">bpo-36817</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="pep-578-python-runtime-audit-hooks">
|
||
<h3>PEP 578: Python 运行时审核钩子<a class="headerlink" href="#pep-578-python-runtime-audit-hooks" title="永久链接至标题">¶</a></h3>
|
||
<p>此 PEP 添加了审核钩子和已验证开放钩子。 两者在 Python 与本机代码中均可用。允许以纯 Python 代码编写的应用和框架利用额外的通知,同时允许嵌入开发人员或系统管理员部署始终启用审核的 Python 版本。</p>
|
||
<p>请参阅 <span class="target" id="index-57"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0578"><strong>PEP 578</strong></a> 了解详情。</p>
|
||
</section>
|
||
<section id="pep-587-python-initialization-configuration">
|
||
<h3>PEP 587: Python 初始化配置<a class="headerlink" href="#pep-587-python-initialization-configuration" title="永久链接至标题">¶</a></h3>
|
||
<p><span class="target" id="index-58"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0587"><strong>PEP 587</strong></a> 增加了一个新的 C API 用来配置 Python 初始化,提供对整个配置过程的更细致控制以及更好的错误报告。</p>
|
||
<p>新的结构:</p>
|
||
<ul class="simple">
|
||
<li><p><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></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyPreConfig" title="PyPreConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyPreConfig</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus" title="PyStatus"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyStatus</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyWideStringList" title="PyWideStringList"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyWideStringList</span></code></a></p></li>
|
||
</ul>
|
||
<p>新的函数:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_Clear" title="PyConfig_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Clear()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_InitIsolatedConfig" title="PyConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitIsolatedConfig()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_InitPythonConfig" title="PyConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_InitPythonConfig()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_Read" title="PyConfig_Read"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_Read()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_SetArgv" title="PyConfig_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetArgv()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_SetBytesArgv" title="PyConfig_SetBytesArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetBytesArgv()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_SetBytesString" title="PyConfig_SetBytesString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetBytesString()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyConfig_SetString" title="PyConfig_SetString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyConfig_SetString()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyPreConfig_InitIsolatedConfig" title="PyPreConfig_InitIsolatedConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitIsolatedConfig()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyPreConfig_InitPythonConfig" title="PyPreConfig_InitPythonConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyPreConfig_InitPythonConfig()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_Error" title="PyStatus_Error"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Error()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_Exception" title="PyStatus_Exception"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exception()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_Exit" title="PyStatus_Exit"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Exit()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_IsError" title="PyStatus_IsError"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_IsError()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_IsExit" title="PyStatus_IsExit"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_IsExit()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_NoMemory" title="PyStatus_NoMemory"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_NoMemory()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyStatus_Ok" title="PyStatus_Ok"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyStatus_Ok()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyWideStringList_Append" title="PyWideStringList_Append"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyWideStringList_Append()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.PyWideStringList_Insert" title="PyWideStringList_Insert"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyWideStringList_Insert()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/veryhigh.html#c.Py_BytesMain" title="Py_BytesMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BytesMain()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_ExitStatusException" title="Py_ExitStatusException"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_ExitStatusException()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_PreInitialize" title="Py_PreInitialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitialize()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_PreInitializeFromArgs" title="Py_PreInitializeFromArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromArgs()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_PreInitializeFromBytesArgs" title="Py_PreInitializeFromBytesArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_PreInitializeFromBytesArgs()</span></code></a></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init_config.html#c.Py_RunMain" title="Py_RunMain"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_RunMain()</span></code></a></p></li>
|
||
</ul>
|
||
<p>此 PEP 还为这些内部结构添加了 <code class="docutils literal notranslate"><span class="pre">_PyRuntimeState.preconfig</span></code> (<a class="reference internal" href="../c-api/init_config.html#c.PyPreConfig" title="PyPreConfig"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyPreConfig</span></code></a> 类型) 和 <code class="docutils literal notranslate"><span class="pre">PyInterpreterState.config</span></code> (<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> 类型) 字段。 <code class="docutils literal notranslate"><span class="pre">PyInterpreterState.config</span></code> 成为新的引用配置,替代全局配置变量和其他私有变量。</p>
|
||
<p>请参阅 <a class="reference internal" href="../c-api/init_config.html#init-config"><span class="std std-ref">Python 初始化配置</span></a> 获取详细文档。</p>
|
||
<p>请参阅 <span class="target" id="index-59"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0587"><strong>PEP 587</strong></a> 了解详情。</p>
|
||
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36763">bpo-36763</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="vectorcall-a-fast-calling-protocol-for-cpython">
|
||
<h3>Vectorcall: 用于 CPython 的快速调用协议<a class="headerlink" href="#vectorcall-a-fast-calling-protocol-for-cpython" title="永久链接至标题">¶</a></h3>
|
||
<p>添加 "vectorcall" 协议到 Python/C API。 它的目标是对已被应用于许多类的现有优化进行正式化。 任何实现可调用对象的扩展类型均可使用此协议。</p>
|
||
<p>此特性目前为暂定状态,计划在 Python 3.9 将其完全公开。</p>
|
||
<p>请参阅 <span class="target" id="index-60"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0590"><strong>PEP 590</strong></a> 了解详情。</p>
|
||
<p>(由 Jeroen Demeyer 和 Mark Shannon 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36974">bpo-36974</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="pickle-protocol-5-with-out-of-band-data-buffers">
|
||
<h3>具有外部数据缓冲区的 pickle 协议 5<a class="headerlink" href="#pickle-protocol-5-with-out-of-band-data-buffers" title="永久链接至标题">¶</a></h3>
|
||
<p>当使用 <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> 在 Python 进程间传输大量数据以充分发挥多核或多机处理的优势时,非常重要一点是通过减少内存拷贝来优化传输效率,并可能应用一些定制技巧例如针对特定数据的压缩。</p>
|
||
<p><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> 协议 5 引入了对于外部缓冲区的支持,这样 <span class="target" id="index-61"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a> 兼容的数据可以与主 pickle 流分开进行传输,这是由通信层来确定的。</p>
|
||
<p>请参阅 <span class="target" id="index-62"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0574"><strong>PEP 574</strong></a> 了解详情。</p>
|
||
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36785">bpo-36785</a> 中贡献。)</p>
|
||
</section>
|
||
</section>
|
||
<section id="other-language-changes">
|
||
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题">¶</a></h2>
|
||
<ul>
|
||
<li><p>在之前版本中 <a class="reference internal" href="../reference/simple_stmts.html#continue"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code></a> 语句不允许在 <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> 子句中使用,这是因为具体实现存在一个问题。 在 Python 3.8 中此限制已被取消。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32489">bpo-32489</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a>, <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> 和 <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">fractions.Fraction</span></code></a> 类型现在都有一个 <a class="reference internal" href="../library/stdtypes.html#int.as_integer_ratio" title="int.as_integer_ratio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_integer_ratio()</span></code></a> 方法,与 <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> 和 <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> 中的已有方法类似。 这个微小的 API 扩展使得 <code class="docutils literal notranslate"><span class="pre">numerator,</span> <span class="pre">denominator</span> <span class="pre">=</span> <span class="pre">x.as_integer_ratio()</span></code> 这样的写法在多种数字类型上通用成为可能。 (由 Lisa Roach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33073">bpo-33073</a> 和 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37819">bpo-37819</a> 中贡献。)</p></li>
|
||
<li><p><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>, <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> 和 <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> 的构造器现在会使用 <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 <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>, <a class="reference internal" href="../reference/datamodel.html#object.__float__" title="object.__float__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__float__()</span></code></a> 或 <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__complex__()</span></code></a> 方法不可用的话。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=20092">bpo-20092</a> 中贡献。)</p></li>
|
||
<li><p>添加 <code class="docutils literal notranslate"><span class="pre">\N{name}</span></code> 转义符在 <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">正则表达式</span></code></a> 中的支持:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">notice</span> <span class="o">=</span> <span class="s1">'Copyright © 2019'</span>
|
||
<span class="gp">>>> </span><span class="n">copyright_year_pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">'\N{copyright sign}\s*(\d</span><span class="si">{4}</span><span class="s1">)'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="nb">int</span><span class="p">(</span><span class="n">copyright_year_pattern</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">notice</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
|
||
<span class="go">2019</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Jonathan Eunice 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30688">bpo-30688</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>现在 dict 和 dictview 可以使用 <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> 按插入顺序反向迭代。 (由 Rémi Lapeyre 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33462">bpo-33462</a> 中贡献。)</p></li>
|
||
<li><p>在函数调用中允许使用的关键字名称语法受到进一步的限制。 特别地,<code class="docutils literal notranslate"><span class="pre">f((keyword)=arg)</span></code> 不再被允许。 关键字参数赋值形式的左侧绝不允许一般标识符以外的其他内容。 (由 Benjamin Peterson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34641">bpo-34641</a> 中贡献。)</p></li>
|
||
<li><p>在 <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> 和 <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 语句中的一般可迭代对象解包不再要求加圆括号。 这使得 <em>yield</em> 和 <em>return</em> 的语法与正常的赋值语法更为一致:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="n">family</span><span class="p">):</span>
|
||
<span class="go"> lastname, *members = family.split()</span>
|
||
<span class="go"> return lastname.upper(), *members</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">parse</span><span class="p">(</span><span class="s1">'simpsons homer marge bart lisa sally'</span><span class="p">)</span>
|
||
<span class="go">('SIMPSONS', 'homer', 'marge', 'bart', 'lisa', 'sally')</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 David Cuthbert 和 Jordan Chapman 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32117">bpo-32117</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>当类似 <code class="docutils literal notranslate"><span class="pre">[(10,</span> <span class="pre">20)</span> <span class="pre">(30,</span> <span class="pre">40)]</span></code> 这样在代码中少了一个逗号时,编译器将显示 <a class="reference internal" href="../library/exceptions.html#SyntaxWarning" title="SyntaxWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxWarning</span></code></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> 来提示第一个元组是不可调用的更容易被理解。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=15248">bpo-15248</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/datetime.html#datetime.date" title="datetime.date"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.date</span></code></a> 或 <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> 和 <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timedelta</span></code></a> 对象之间的算术运算现在将返回相应子类的实例而不是基类的实例。 这也会影响到在具体实现中(直接或间接地)使用了 <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timedelta</span></code></a> 算术运算的返回类型,例如 <a class="reference internal" href="../library/datetime.html#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">astimezone()</span></code></a>。 (由 Paul Ganssle 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32417">bpo-32417</a> 中贡献。)</p></li>
|
||
<li><p>当 Python 解释器通过 Ctrl-C (SIGINT) 被中断并且所产生的 <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> 异常未被捕获,Python 进程现在会通过一个 SIGINT 信号或是使得发起调用的进程能检测到它是由 Ctrl-C 操作杀死的正确退出代码来退出。 POSIX 和 Windows 上的终端会相应地使用此代码在交互式会话中终止脚本。 (由 Google 的 Gregory P. Smith 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1054041">bpo-1054041</a> 中贡献。)</p></li>
|
||
<li><p>某些高级编程风格要求为现有的函数更新 <a class="reference internal" href="../library/types.html#types.CodeType" title="types.CodeType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.CodeType</span></code></a> 对象。 由于代码对象是不可变的,需要基于现有代码对象模型创建一个新的代码对象。 使用 19 个形参将会相当繁琐。 现在,新的 <code class="docutils literal notranslate"><span class="pre">replace()</span></code> 方法使得通过少量修改的形参创建克隆对象成为可能。</p>
|
||
<p>下面是一个修改 <a class="reference internal" href="../library/statistics.html#statistics.mean" title="statistics.mean"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.mean()</span></code></a> 函数来防止 <em>data</em> 形参被用作关键字参数的例子:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">statistics</span> <span class="kn">import</span> <span class="n">mean</span>
|
||
<span class="gp">>>> </span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">90</span><span class="p">])</span>
|
||
<span class="go">40</span>
|
||
<span class="gp">>>> </span><span class="n">mean</span><span class="o">.</span><span class="vm">__code__</span> <span class="o">=</span> <span class="n">mean</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">co_posonlyargcount</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">mean</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">90</span><span class="p">])</span>
|
||
<span class="gt">Traceback (most recent call last):</span>
|
||
<span class="w"> </span><span class="c">...</span>
|
||
<span class="gr">TypeError</span>: <span class="n">mean() got some positional-only arguments passed as keyword arguments: 'data'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37032">bpo-37032</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>对于整数,现在 <a class="reference internal" href="../library/functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> 函数的三参数形式在底数与模数不可约的情况下允许指数为负值。 随后它会在指数为 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 时计算底数的模乘逆元,并对其他负指数计算反模的适当幂次。 例如,要计算 38 模 137 的 <a class="reference external" href="https://en.wikipedia.org/wiki/Modular_multiplicative_inverse">模乘逆元</a> 则可写为:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">pow</span><span class="p">(</span><span class="mi">38</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">137</span><span class="p">)</span>
|
||
<span class="go">119</span>
|
||
<span class="gp">>>> </span><span class="mi">119</span> <span class="o">*</span> <span class="mi">38</span> <span class="o">%</span> <span class="mi">137</span>
|
||
<span class="go">1</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>模乘逆元在求解 <a class="reference external" href="https://en.wikipedia.org/wiki/Diophantine_equation">线性丢番图方程</a> 会被用到。 例如,想要求出 <code class="docutils literal notranslate"><span class="pre">4258𝑥</span> <span class="pre">+</span> <span class="pre">147𝑦</span> <span class="pre">=</span> <span class="pre">369</span></code> 的整数解,首先应重写为 <code class="docutils literal notranslate"><span class="pre">4258𝑥</span> <span class="pre">≡</span> <span class="pre">369</span> <span class="pre">(mod</span> <span class="pre">147)</span></code> 然后求解:</p>
|
||
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="mi">369</span> <span class="o">*</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">4258</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">147</span><span class="p">)</span> <span class="o">%</span> <span class="mi">147</span>
|
||
<span class="gp">>>> </span><span class="n">y</span> <span class="o">=</span> <span class="p">(</span><span class="mi">4258</span> <span class="o">*</span> <span class="n">x</span> <span class="o">-</span> <span class="mi">369</span><span class="p">)</span> <span class="o">//</span> <span class="o">-</span><span class="mi">147</span>
|
||
<span class="gp">>>> </span><span class="mi">4258</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">147</span> <span class="o">*</span> <span class="n">y</span>
|
||
<span class="go">369</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36027">bpo-36027</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>字典推导式已与字典字面值实现同步,会先计算键再计算值:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Dict comprehension</span>
|
||
<span class="gp">>>> </span><span class="n">cast</span> <span class="o">=</span> <span class="p">{</span><span class="nb">input</span><span class="p">(</span><span class="s1">'role? '</span><span class="p">):</span> <span class="nb">input</span><span class="p">(</span><span class="s1">'actor? '</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">)}</span>
|
||
<span class="go">role? King Arthur</span>
|
||
<span class="go">actor? Chapman</span>
|
||
<span class="go">role? Black Knight</span>
|
||
<span class="go">actor? Cleese</span>
|
||
|
||
<span class="gp">>>> </span><span class="c1"># Dict literal</span>
|
||
<span class="gp">>>> </span><span class="n">cast</span> <span class="o">=</span> <span class="p">{</span><span class="nb">input</span><span class="p">(</span><span class="s1">'role? '</span><span class="p">):</span> <span class="nb">input</span><span class="p">(</span><span class="s1">'actor? '</span><span class="p">)}</span>
|
||
<span class="go">role? Sir Robin</span>
|
||
<span class="go">actor? Eric Idle</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>对执行顺序的保证对赋值表达式来说很有用,因为在键表达式中赋值的变量将可在值表达式中被使用:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'Martin von Löwis'</span><span class="p">,</span> <span class="s1">'Łukasz Langa'</span><span class="p">,</span> <span class="s1">'Walter Dörwald'</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="p">{(</span><span class="n">n</span> <span class="o">:=</span> <span class="n">normalize</span><span class="p">(</span><span class="s1">'NFC'</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span><span class="o">.</span><span class="n">casefold</span><span class="p">()</span> <span class="p">:</span> <span class="n">n</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">}</span>
|
||
<span class="go">{'martin von löwis': 'Martin von Löwis',</span>
|
||
<span class="go"> 'łukasz langa': 'Łukasz Langa',</span>
|
||
<span class="go"> 'walter dörwald': 'Walter Dörwald'}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Jörn Heissler 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35224">bpo-35224</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/pickle.html#object.__reduce__" title="object.__reduce__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__reduce__()</span></code></a> 方法现在可返回长度为二至六个元素的元组。 之前的上限为五个。 新增的第六个可选元素是签名为 <code class="docutils literal notranslate"><span class="pre">(obj,</span> <span class="pre">state)</span></code> 的可调用对象。 这样就允许直接控制特定对象的状态更新。 如果元素值不为 <em>None</em>,该可调用对象将优先于对象的 <a class="reference internal" href="../library/pickle.html#object.__setstate__" title="object.__setstate__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setstate__()</span></code></a> 方法。 (由 Pierre Glaser 和 Olivier Grisel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35900">bpo-35900</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="new-modules">
|
||
<h2>新增模块<a class="headerlink" href="#new-modules" title="永久链接至标题">¶</a></h2>
|
||
<ul>
|
||
<li><p>新增的 <a class="reference internal" href="../library/importlib.metadata.html#module-importlib.metadata" title="importlib.metadata: The implementation of the importlib metadata."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.metadata</span></code></a> 模块提供了从第三方包读取元数据的(临时)支持。 例如,它可以提取一个已安装软件包的版本号、入口点列表等等:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># Note following example requires that the popular "requests"</span>
|
||
<span class="gp">>>> </span><span class="c1"># package has been installed.</span>
|
||
<span class="gp">>>></span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">importlib.metadata</span> <span class="kn">import</span> <span class="n">version</span><span class="p">,</span> <span class="n">requires</span><span class="p">,</span> <span class="n">files</span>
|
||
<span class="gp">>>> </span><span class="n">version</span><span class="p">(</span><span class="s1">'requests'</span><span class="p">)</span>
|
||
<span class="go">'2.22.0'</span>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">requires</span><span class="p">(</span><span class="s1">'requests'</span><span class="p">))</span>
|
||
<span class="go">['chardet (<3.1.0,>=3.0.2)']</span>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">files</span><span class="p">(</span><span class="s1">'requests'</span><span class="p">))[:</span><span class="mi">5</span><span class="p">]</span>
|
||
<span class="go">[PackagePath('requests-2.22.0.dist-info/INSTALLER'),</span>
|
||
<span class="go"> PackagePath('requests-2.22.0.dist-info/LICENSE'),</span>
|
||
<span class="go"> PackagePath('requests-2.22.0.dist-info/METADATA'),</span>
|
||
<span class="go"> PackagePath('requests-2.22.0.dist-info/RECORD'),</span>
|
||
<span class="go"> PackagePath('requests-2.22.0.dist-info/WHEEL')]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Barry Warsaw 和 Jason R. Coombs 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34632">bpo-34632</a> 中贡献)。</p>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
<section id="improved-modules">
|
||
<h2>改进的模块<a class="headerlink" href="#improved-modules" title="永久链接至标题">¶</a></h2>
|
||
<section id="ast">
|
||
<h3>ast<a class="headerlink" href="#ast" title="永久链接至标题">¶</a></h3>
|
||
<p>AST 节点现在具有 <code class="docutils literal notranslate"><span class="pre">end_lineno</span></code> 和 <code class="docutils literal notranslate"><span class="pre">end_col_offset</span></code> 属性,它们给出节点结束的精确位置。 (这只适用于具有 <code class="docutils literal notranslate"><span class="pre">lineno</span></code> 和 <code class="docutils literal notranslate"><span class="pre">col_offset</span></code> 属性的节点。)</p>
|
||
<p>新增函数 <a class="reference internal" href="../library/ast.html#ast.get_source_segment" title="ast.get_source_segment"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.get_source_segment()</span></code></a> 返回指定 AST 节点的源代码。</p>
|
||
<p>(由 Ivan Levkivskyi 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33416">bpo-33416</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/ast.html#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.parse()</span></code></a> 函数具有一些新的旗标:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">type_comments=True</span></code> 导致其返回与特定 AST 节点相关联的 <span class="target" id="index-63"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> 和 <span class="target" id="index-64"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0526"><strong>PEP 526</strong></a> 类型注释文本;</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">mode='func_type'</span></code> 可被用于解析 <span class="target" id="index-65"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> "签名类型注释" (为函数定义 AST 节点而返回);</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">feature_version=(3,</span> <span class="pre">N)</span></code> 允许指定一个更早的 Python 3 版本。 例如,<code class="docutils literal notranslate"><span class="pre">feature_version=(3,</span> <span class="pre">4)</span></code> 将把 <a class="reference internal" href="../reference/compound_stmts.html#async"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span></code></a> 和 <a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a> 视为非保留字。</p></li>
|
||
</ul>
|
||
<p>(由 Guido van Rossum 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35766">bpo-35766</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="asyncio">
|
||
<h3>asyncio<a class="headerlink" href="#asyncio" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/asyncio-task.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a> 已经从暂定状态晋级为稳定 API。 此函数可被用于执行一个 <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> 并返回结果,同时自动管理事件循环。 例如:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
|
||
|
||
<span class="k">async</span> <span class="k">def</span> <span class="nf">main</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">0</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="mi">42</span>
|
||
|
||
<span class="n">asyncio</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>这 <em>大致</em> 等价于:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">asyncio</span>
|
||
|
||
<span class="k">async</span> <span class="k">def</span> <span class="nf">main</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">0</span><span class="p">)</span>
|
||
<span class="k">return</span> <span class="mi">42</span>
|
||
|
||
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">new_event_loop</span><span class="p">()</span>
|
||
<span class="n">asyncio</span><span class="o">.</span><span class="n">set_event_loop</span><span class="p">(</span><span class="n">loop</span><span class="p">)</span>
|
||
<span class="k">try</span><span class="p">:</span>
|
||
<span class="n">loop</span><span class="o">.</span><span class="n">run_until_complete</span><span class="p">(</span><span class="n">main</span><span class="p">())</span>
|
||
<span class="k">finally</span><span class="p">:</span>
|
||
<span class="n">asyncio</span><span class="o">.</span><span class="n">set_event_loop</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
|
||
<span class="n">loop</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>实际的实现要更复杂许多。 因此 <a class="reference internal" href="../library/asyncio-task.html#asyncio.run" title="asyncio.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.run()</span></code></a> 应该作为运行 asyncio 程序的首选方式。</p>
|
||
<p>(由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32314">bpo-32314</a> 中贡献。)</p>
|
||
<p>运行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">asyncio</span></code> 将启动一个原生异步 REPL。 这允许快速体验具有最高层级 <a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a> 的代码。 这时不再需要直接调用 <code class="docutils literal notranslate"><span class="pre">asyncio.run()</span></code>,因为此操作会在每次发起调用时产生一个新事件循环:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ python -m asyncio
|
||
asyncio REPL 3.8.0
|
||
Use "await" directly instead of "asyncio.run()".
|
||
Type "help", "copyright", "credits" or "license" for more information.
|
||
>>> import asyncio
|
||
>>> await asyncio.sleep(10, result='hello')
|
||
hello
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37028">bpo-37028</a> 中贡献。)</p>
|
||
<p>异常 <a class="reference internal" href="../library/asyncio-exceptions.html#asyncio.CancelledError" title="asyncio.CancelledError"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.CancelledError</span></code></a> 现在继承自 <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a> 而不是 <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-class docutils literal notranslate"><span class="pre">Exception</span></code></a> 并且不再继承自 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.CancelledError</span></code></a>。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32528">bpo-32528</a> 中贡献。)</p>
|
||
<p>在 Windows 上,现在默认的事件循环为 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a>。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34687">bpo-34687</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> 现在也支持 UDP。 (由 Adam Meily 和 Andrew Svetlov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29883">bpo-29883</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.ProactorEventLoop" title="asyncio.ProactorEventLoop"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProactorEventLoop</span></code></a> 现在可通过 <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> ("CTRL+C") 来中断。 (由 Vladimir Matveev 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23057">bpo-23057</a> 中贡献。)</p>
|
||
<p>添加了 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task.get_coro" title="asyncio.Task.get_coro"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.Task.get_coro()</span></code></a> 用来获取 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a> 中的已包装协程。 (由 Alex Grönholm 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36999">bpo-36999</a> 中贡献。)</p>
|
||
<p>asyncio 任务现在可以被命名,或者是通过将 <code class="docutils literal notranslate"><span class="pre">name</span></code> 关键字参数传给 <a class="reference internal" href="../library/asyncio-task.html#asyncio.create_task" title="asyncio.create_task"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.create_task()</span></code></a> 或 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_task" title="asyncio.loop.create_task"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_task()</span></code></a> 事件循环方法,或者是通过在任务对象上调用 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task.set_name" title="asyncio.Task.set_name"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_name()</span></code></a> 方法。 任务名称在 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">repr()</span></code> 输出中可见,并且还可以使用 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task.get_name" title="asyncio.Task.get_name"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_name()</span></code></a> 方法来获取。 (由 Alex Grönholm 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34270">bpo-34270</a> 中贡献。)</p>
|
||
<p>将对 <a class="reference external" href="https://en.wikipedia.org/wiki/Happy_Eyeballs">Happy Eyeballs</a> 的支持添加到 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.loop.create_connection()</span></code></a>。 要指定此行为,已增加了两个新的形参: <em>happy_eyeballs_delay</em> 和 <em>interleave</em>。 Happy Eyeballs 算法可提升支持 IPv4 和 IPv6 的应用的响应速度,具体做法是尝试同时使用两者进行连接。 (由 twisteroid ambassador 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33530">bpo-33530</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="builtins">
|
||
<h3>builtins<a class="headerlink" href="#builtins" title="永久链接至标题">¶</a></h3>
|
||
<p>内置的 <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 已改进为可接受 <code class="docutils literal notranslate"><span class="pre">ast.PyCF_ALLOW_TOP_LEVEL_AWAIT</span></code> 旗标。 当传入此新旗标时,<a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> 将允许通常被视为无效语法的最高层级 <code class="docutils literal notranslate"><span class="pre">await</span></code>, <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code> 和 <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code> 构造。 此后将可返回带有 <code class="docutils literal notranslate"><span class="pre">CO_COROUTINE</span></code> 旗标的异步代码对象。 (由 Matthias Bussonnier 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34616">bpo-34616</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="collections">
|
||
<h3>collections<a class="headerlink" href="#collections" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a> 的 <a class="reference internal" href="../library/collections.html#collections.somenamedtuple._asdict" title="collections.somenamedtuple._asdict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_asdict()</span></code></a> 方法现在将返回 <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 而不是 <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a>。 此项更改是由于普通字典自 Python 3.7 起已保证具有确定的元素顺序。 如果还需要 <code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code> 的额外特性,建议的解决方案是将结果转换为需要的类型: <code class="docutils literal notranslate"><span class="pre">OrderedDict(nt._asdict())</span></code>。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35864">bpo-35864</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="cprofile">
|
||
<h3>cProfile<a class="headerlink" href="#cprofile" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/profile.html#profile.Profile" title="profile.Profile"><code class="xref py py-class docutils literal notranslate"><span class="pre">cProfile.Profile</span></code></a> 类现在可被用作上下文管理器。 在运行时对一个代码块实现性能分析:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">cProfile</span>
|
||
|
||
<span class="k">with</span> <span class="n">cProfile</span><span class="o">.</span><span class="n">Profile</span><span class="p">()</span> <span class="k">as</span> <span class="n">profiler</span><span class="p">:</span>
|
||
<span class="c1"># code to be profiled</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Scott Sanderson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29235">bpo-29235</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="csv">
|
||
<h3>csv<a class="headerlink" href="#csv" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/csv.html#csv.DictReader" title="csv.DictReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">csv.DictReader</span></code></a> 现在将返回 <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 而不是 <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a>。 此工具现在会更快速且消耗更少内存同时仍然保留字段顺序。 (由 Michael Selik 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34003">bpo-34003</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="curses">
|
||
<h3>curses<a class="headerlink" href="#curses" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了一个新变量用于保存下层 ncurses 库的结构版信息: <a class="reference internal" href="../library/curses.html#curses.ncurses_version" title="curses.ncurses_version"><code class="xref py py-data docutils literal notranslate"><span class="pre">ncurses_version</span></code></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31680">bpo-31680</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="ctypes">
|
||
<h3>ctypes<a class="headerlink" href="#ctypes" title="永久链接至标题">¶</a></h3>
|
||
<p>在 Windows 上,<a class="reference internal" href="../library/ctypes.html#ctypes.CDLL" title="ctypes.CDLL"><code class="xref py py-class docutils literal notranslate"><span class="pre">CDLL</span></code></a> 及其子类现在接受 <em>winmode</em> 形参来指定用于底层 <code class="docutils literal notranslate"><span class="pre">LoadLibraryEx</span></code> 调用的旗标。 默认旗标被设为仅加载来自可信任位置的 DLL 依赖项,包括 DLL 的存放路径(如果加载初始 DLL 时使用了完整或部分路径)以及通过 <a class="reference internal" href="../library/os.html#os.add_dll_directory" title="os.add_dll_directory"><code class="xref py py-func docutils literal notranslate"><span class="pre">add_dll_directory()</span></code></a> 添加的路径。 (由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36085">bpo-36085</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="datetime">
|
||
<h3>datetime<a class="headerlink" href="#datetime" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的替代构造器 <a class="reference internal" href="../library/datetime.html#datetime.date.fromisocalendar" title="datetime.date.fromisocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.date.fromisocalendar()</span></code></a> 和 <a class="reference internal" href="../library/datetime.html#datetime.datetime.fromisocalendar" title="datetime.datetime.fromisocalendar"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.fromisocalendar()</span></code></a>,它们分别基于 ISO 年份、周序号和周内日序号来构造 <code class="xref py py-class docutils literal notranslate"><span class="pre">date</span></code> 和 <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象;这两者分别是其所对应类中 <code class="docutils literal notranslate"><span class="pre">isocalendar</span></code> 方法的逆操作。 (由 Paul Ganssle 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36004">bpo-36004</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="functools">
|
||
<h3>functools<a class="headerlink" href="#functools" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/functools.html#functools.lru_cache" title="functools.lru_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.lru_cache()</span></code></a> 现在可直接作为装饰器而不是作为返回装饰器的函数。 因此这两种写法现在都被支持:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@lru_cache</span>
|
||
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
|
||
<span class="nd">@lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
|
||
<span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36772">bpo-36772</a> 中贡献。)</p>
|
||
<p>添加了新的 <a class="reference internal" href="../library/functools.html#functools.cached_property" title="functools.cached_property"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.cached_property()</span></code></a> 装饰器,用于在实例生命周期内缓存的已计算特征属性。</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">functools</span>
|
||
<span class="kn">import</span> <span class="nn">statistics</span>
|
||
|
||
<span class="k">class</span> <span class="nc">Dataset</span><span class="p">:</span>
|
||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sequence_of_numbers</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">sequence_of_numbers</span>
|
||
|
||
<span class="nd">@functools</span><span class="o">.</span><span class="n">cached_property</span>
|
||
<span class="k">def</span> <span class="nf">variance</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">statistics</span><span class="o">.</span><span class="n">variance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Carl Meyer 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=21145">bpo-21145</a> 中贡献)</p>
|
||
<p>添加了新的 <a class="reference internal" href="../library/functools.html#functools.singledispatchmethod" title="functools.singledispatchmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.singledispatchmethod()</span></code></a> 装饰器可使用 <a class="reference internal" href="../glossary.html#term-single-dispatch"><span class="xref std std-term">single dispatch</span></a> 将方法转换为 <a class="reference internal" href="../glossary.html#term-generic-function"><span class="xref std std-term">泛型函数</span></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">singledispatchmethod</span>
|
||
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">suppress</span>
|
||
|
||
<span class="k">class</span> <span class="nc">TaskManager</span><span class="p">:</span>
|
||
|
||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tasks</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">tasks</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">tasks</span><span class="p">)</span>
|
||
|
||
<span class="nd">@singledispatchmethod</span>
|
||
<span class="k">def</span> <span class="nf">discard</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
|
||
<span class="k">with</span> <span class="n">suppress</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">tasks</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
|
||
|
||
<span class="nd">@discard</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
|
||
<span class="k">def</span> <span class="nf">_</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tasks</span><span class="p">):</span>
|
||
<span class="n">targets</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">tasks</span><span class="p">)</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">tasks</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tasks</span> <span class="k">if</span> <span class="n">x</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">targets</span><span class="p">]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Ethan Smith 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32380">bpo-32380</a> 中贡献)</p>
|
||
</section>
|
||
<section id="gc">
|
||
<h3>gc<a class="headerlink" href="#gc" title="永久链接至标题">¶</a></h3>
|
||
<p><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">get_objects()</span></code></a> 现在能接受一个可选的 <em>generation</em> 形参来指定一个用于获取对象的生成器。 (由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36016">bpo-36016</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="gettext">
|
||
<h3>gettext<a class="headerlink" href="#gettext" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了 <a class="reference internal" href="../library/gettext.html#gettext.pgettext" title="gettext.pgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">pgettext()</span></code></a> 及其变化形式。 (由 Franz Glasner, Éric Araujo 和 Cheryl Sabella 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=2504">bpo-2504</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="gzip">
|
||
<h3>gzip<a class="headerlink" href="#gzip" title="永久链接至标题">¶</a></h3>
|
||
<p>添加 <em>mtime</em> 形参到 <a class="reference internal" href="../library/gzip.html#gzip.compress" title="gzip.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">gzip.compress()</span></code></a> 用于可重现的输出。 (由 Guo Ci Teo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34898">bpo-34898</a> 中贡献。)</p>
|
||
<p>对于特定类型的无效或已损坏 gzip 文件现在将引发 <a class="reference internal" href="../library/gzip.html#gzip.BadGzipFile" title="gzip.BadGzipFile"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadGzipFile</span></code></a> 而不是 <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>。 (由 Filip Gruszczyński, Michele Orrù 和 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=6584">bpo-6584</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="idle-and-idlelib">
|
||
<h3>IDLE 与 idlelib<a class="headerlink" href="#idle-and-idlelib" title="永久链接至标题">¶</a></h3>
|
||
<p>超过 N 行(默认值为 50)的输出将被折叠为一个按钮。 N 可以在 Settings 对话框的 General 页的 PyShell 部分中进行修改。 数量较少但是超长的行可以通过在输出上右击来折叠。 被折叠的输出可通过双击按钮来展开,或是通过右击按钮来放入剪贴板或是单独的窗口。 (由 Tal Einat 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1529353">bpo-1529353</a> 中贡献。)</p>
|
||
<p>在 Run 菜单中增加了 "Run Customized" 以使用自定义设置来运行模块。 输入的任何命令行参数都会被加入 sys.argv。 它们在下次自定义运行时会再次显示在窗体中。 用户也可以禁用通常的 Shell 主模块重启。 (由 Cheryl Sabella, Terry Jan Reedy 等人在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=5680">bpo-5680</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37627">bpo-37627</a> 中贡献。)</p>
|
||
<p>在 IDLE 编辑器窗口中增加了可选的行号。 窗口打开时默认不显示行号,除非在配置对话框的 General 选项卡中特别设置。 已打开窗口中的行号可以在 Options 菜单中显示和隐藏。 (由 Tal Einat 和 Saimadhav Heblikar 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=17535">bpo-17535</a> 中贡献。)</p>
|
||
<p>现在会使用 OS 本机编码格式在 Python 字符串和 Tcl 对象间进行转换。 这允许在 IDLE 中处理 emoji 和其他非 BMP 字符。 这些字符将可被显示或是从剪贴板复制和粘贴。 字符串从 Tcl 到 Python 的来回转换现在不会再发生失败。 (过去八年有许多人都为此付出过努力,问题最终由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=13153">bpo-13153</a> 中解决。)</p>
|
||
<p>在 3.8.1 中新增:</p>
|
||
<p>添加切换光标闪烁停止的选项。 (由 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=4603">bpo-4603</a> 中贡献。)</p>
|
||
<p>Esc 键现在会关闭 IDLE 补全提示窗口。 (由 Johnny Najera 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38944">bpo-38944</a> 中贡献。)</p>
|
||
<p>上述修改已被反向移植到 3.7 维护发行版中。</p>
|
||
<p>添加关键字到模块名称补全列表。 (由 Terry J. Reedy 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37765">bpo-37765</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="inspect">
|
||
<h3>inspect<a class="headerlink" href="#inspect" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/inspect.html#inspect.getdoc" title="inspect.getdoc"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getdoc()</span></code></a> 函数现在可以找到 <code class="docutils literal notranslate"><span class="pre">__slots__</span></code> 的文档字符串,如果该属性是一个元素值为文档字符串的 <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> 的话。 这提供了类似于目前已有的 <a class="reference internal" href="../library/functions.html#property" title="property"><code class="xref py py-func docutils literal notranslate"><span class="pre">property()</span></code></a>, <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> 和 <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> 等函数的文档选项:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">AudioClip</span><span class="p">:</span>
|
||
<span class="vm">__slots__</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'bit_rate'</span><span class="p">:</span> <span class="s1">'expressed in kilohertz to one decimal place'</span><span class="p">,</span>
|
||
<span class="s1">'duration'</span><span class="p">:</span> <span class="s1">'in seconds, rounded up to an integer'</span><span class="p">}</span>
|
||
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">bit_rate</span><span class="p">,</span> <span class="n">duration</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">bit_rate</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">bit_rate</span> <span class="o">/</span> <span class="mf">1000.0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">duration</span> <span class="o">=</span> <span class="n">ceil</span><span class="p">(</span><span class="n">duration</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36326">bpo-36326</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="io">
|
||
<h3>io<a class="headerlink" href="#io" title="永久链接至标题">¶</a></h3>
|
||
<p>在开发模式 (<a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a> <code class="docutils literal notranslate"><span class="pre">env</span></code>) 和调试构建中,<a class="reference internal" href="../library/io.html#io.IOBase" title="io.IOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.IOBase</span></code></a> 终结器现在会在 <code class="docutils literal notranslate"><span class="pre">close()</span></code> 方法失败时将异常写入日志。 发生的异常在发布构建中默认会被静默忽略。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=18748">bpo-18748</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="itertools">
|
||
<h3>itertools<a class="headerlink" href="#itertools" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.accumulate()</span></code></a> 函数增加了可选的 <em>initial</em> 关键字参数用来指定一个初始值:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">accumulate</span>
|
||
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">accumulate</span><span class="p">([</span><span class="mi">10</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">15</span><span class="p">],</span> <span class="n">initial</span><span class="o">=</span><span class="mi">1000</span><span class="p">))</span>
|
||
<span class="go">[1000, 1010, 1015, 1045, 1060]</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Lisa Roach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34659">bpo-34659</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="json-tool">
|
||
<h3>json.tool<a class="headerlink" href="#json-tool" title="永久链接至标题">¶</a></h3>
|
||
<p>添加选项 <code class="docutils literal notranslate"><span class="pre">--json-lines</span></code> 用于将每个输入行解析为单独的 JSON 对象。 (由 Weipeng Hong 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31553">bpo-31553</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="logging">
|
||
<h3>logging<a class="headerlink" href="#logging" title="永久链接至标题">¶</a></h3>
|
||
<p>为 <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">logging.basicConfig()</span></code></a> 添加了 <em>force</em> 关键字参数,当设为真值时,关联到根日志记录器的任何现有处理程序都将在执行由其他参数所指定的配置之前被移除并关闭。</p>
|
||
<p>这解决了一个长期存在的问题。 当一个日志处理器或 <em>basicConfig()</em> 被调用时,对 <em>basicConfig()</em> 的后续调用会被静默地忽略。 这导致使用交互提示符或 Jupyter 笔记本更新、试验或讲解各种日志配置选项变得相当困难。</p>
|
||
<p>(由 Raymond Hettinger 提议,由 Dong-hee Na 实现,并由 Vinay Sajip 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33897">bpo-33897</a> 中完成审核。)</p>
|
||
</section>
|
||
<section id="math">
|
||
<h3>math<a class="headerlink" href="#math" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的函数 <a class="reference internal" href="../library/math.html#math.dist" title="math.dist"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.dist()</span></code></a> 用于计算两点之间的欧几里得距离。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33089">bpo-33089</a> 中贡献。)</p>
|
||
<p>扩展了 <a class="reference internal" href="../library/math.html#math.hypot" title="math.hypot"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.hypot()</span></code></a> 函数以便处理更多的维度。 之前它仅支持 2-D 的情况。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33089">bpo-33089</a> 中贡献。)</p>
|
||
<p>添加了新的函数 <a class="reference internal" href="../library/math.html#math.prod" title="math.prod"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.prod()</span></code></a> 作为的 <a class="reference internal" href="../library/functions.html#sum" title="sum"><code class="xref py py-func docutils literal notranslate"><span class="pre">sum()</span></code></a> 同类,该函数返回 'start' 值 (默认值: 1) 乘以一个数字可迭代对象的积:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">prior</span> <span class="o">=</span> <span class="mf">0.8</span>
|
||
<span class="gp">>>> </span><span class="n">likelihoods</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.625</span><span class="p">,</span> <span class="mf">0.84</span><span class="p">,</span> <span class="mf">0.30</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">math</span><span class="o">.</span><span class="n">prod</span><span class="p">(</span><span class="n">likelihoods</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="n">prior</span><span class="p">)</span>
|
||
<span class="go">0.126</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35606">bpo-35606</a> 中贡献。)</p>
|
||
<p>添加了两个新的组合函数 <a class="reference internal" href="../library/math.html#math.perm" title="math.perm"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.perm()</span></code></a> 和 <a class="reference internal" href="../library/math.html#math.comb" title="math.comb"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.comb()</span></code></a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">math</span><span class="o">.</span><span class="n">perm</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># Permutations of 10 things taken 3 at a time</span>
|
||
<span class="go">720</span>
|
||
<span class="gp">>>> </span><span class="n">math</span><span class="o">.</span><span class="n">comb</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="c1"># Combinations of 10 things taken 3 at a time</span>
|
||
<span class="go">120</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Yash Aggarwal, Keller Fuchs, Serhiy Storchaka 和 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37128">bpo-37128</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37178">bpo-37178</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35431">bpo-35431</a> 中贡献。)</p>
|
||
<p>添加了一个新函数 <a class="reference internal" href="../library/math.html#math.isqrt" title="math.isqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.isqrt()</span></code></a> 用于计算精确整数平方根而无需转换为浮点数。 该新函数支持任意大整数。 它的执行速度比 <code class="docutils literal notranslate"><span class="pre">floor(sqrt(n))</span></code> 快但是比 <a class="reference internal" href="../library/math.html#math.sqrt" title="math.sqrt"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.sqrt()</span></code></a> 慢:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="mi">650320427</span>
|
||
<span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">r</span> <span class="o">**</span> <span class="mi">2</span>
|
||
<span class="gp">>>> </span><span class="n">isqrt</span><span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># correct</span>
|
||
<span class="go">650320426</span>
|
||
<span class="gp">>>> </span><span class="n">floor</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">s</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="c1"># incorrect</span>
|
||
<span class="go">650320427</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36887">bpo-36887</a> 中贡献。)</p>
|
||
<p>函数 <a class="reference internal" href="../library/math.html#math.factorial" title="math.factorial"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.factorial()</span></code></a> 不再接受非整数类参数。 (由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33083">bpo-33083</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="mmap">
|
||
<h3>mmap<a class="headerlink" href="#mmap" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/mmap.html#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap.mmap</span></code></a> 类现在具有一个 <a class="reference internal" href="../library/mmap.html#mmap.mmap.madvise" title="mmap.mmap.madvise"><code class="xref py py-meth docutils literal notranslate"><span class="pre">madvise()</span></code></a> 方法用于访问 <code class="docutils literal notranslate"><span class="pre">madvise()</span></code> 系统调用。 (由 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32941">bpo-32941</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="multiprocessing">
|
||
<h3>multiprocessing<a class="headerlink" href="#multiprocessing" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的 <a class="reference internal" href="../library/multiprocessing.shared_memory.html#module-multiprocessing.shared_memory" title="multiprocessing.shared_memory: Provides shared memory for direct access across processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing.shared_memory</span></code></a> 模块。 (由 Davin Potts 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35813">bpo-35813</a> 中贡献。)</p>
|
||
<p>在macOS上,现在默认使用的启动方式是*spawn*启动方式。
|
||
(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33725">bpo-33725</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="os">
|
||
<h3>os<a class="headerlink" href="#os" title="永久链接至标题">¶</a></h3>
|
||
<p>在 Windows 上添加了新函数 <a class="reference internal" href="../library/os.html#os.add_dll_directory" title="os.add_dll_directory"><code class="xref py py-func docutils literal notranslate"><span class="pre">add_dll_directory()</span></code></a> 用于在导入扩展模块或使用 <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> 加载 DLL 时为本机依赖提供额外搜索路径 。 (由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36085">bpo-36085</a> 中贡献。)</p>
|
||
<p>添加了新的 <a class="reference internal" href="../library/os.html#os.memfd_create" title="os.memfd_create"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.memfd_create()</span></code></a> 函数用于包装 <code class="docutils literal notranslate"><span class="pre">memfd_create()</span></code> 系统调用。 (由 Zackery Spytz 和 Christian Heimes 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26836">bpo-26836</a> 中贡献。)</p>
|
||
<p>在 Windows 上,大部分用于处理重解析点,(包括符号链接和目录连接)的手动逻辑已被委托给操作系统。 特别地,<a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 现在将会遍历操作系统所支持的任何内容,而 <a class="reference internal" href="../library/os.html#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.lstat()</span></code></a> 将只打开被标识为“名称代理”的重解析点,而其要由 <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 打开其他的重解析点。 在所有情况下,<code class="xref py py-attr docutils literal notranslate"><span class="pre">stat_result.st_mode</span></code> 将只为符号链接而非其他种类的重解析点设置 <code class="docutils literal notranslate"><span class="pre">S_IFLNK</span></code>。 要标识其他种类的重解析点,请检查新的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">stat_result.st_reparse_tag</span></code> 属性。</p>
|
||
<p>在 Windows 上,<a class="reference internal" href="../library/os.html#os.readlink" title="os.readlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.readlink()</span></code></a> 现在能够读取目录连接。 请注意 <a class="reference internal" href="../library/os.path.html#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">islink()</span></code></a> 会对目录连接返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>,因此首先检查 <code class="docutils literal notranslate"><span class="pre">islink</span></code> 的代码将连续把连接视为目录,而会处理 <a class="reference internal" href="../library/os.html#os.readlink" title="os.readlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.readlink()</span></code></a> 所引发错误的代码现在会把连接视为链接。</p>
|
||
<p>(由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37834">bpo-37834</a> 中贡献。)</p>
|
||
<p>As of 3.8.20, <a class="reference internal" href="../library/os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.mkdir()</span></code></a> and <a class="reference internal" href="../library/os.html#os.makedirs" title="os.makedirs"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.makedirs()</span></code></a> on Windows now support
|
||
passing a <em>mode</em> value of <code class="docutils literal notranslate"><span class="pre">0o700</span></code> to apply access control to the new
|
||
directory. This implicitly affects <a class="reference internal" href="../library/tempfile.html#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempfile.mkdtemp()</span></code></a> and is a
|
||
mitigation for CVE-2024-4030. Other values for <em>mode</em> continue to be
|
||
ignored.
|
||
(Contributed by Steve Dower in <a class="reference external" href="https://github.com/python/cpython/issues/118486">gh-118486</a>.)</p>
|
||
</section>
|
||
<section id="os-path">
|
||
<h3>os.path<a class="headerlink" href="#os-path" title="永久链接至标题">¶</a></h3>
|
||
<p>返回布尔值结果的 <a class="reference internal" href="../library/os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> 函数例如 <a class="reference internal" href="../library/os.path.html#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">exists()</span></code></a>, <a class="reference internal" href="../library/os.path.html#os.path.lexists" title="os.path.lexists"><code class="xref py py-func docutils literal notranslate"><span class="pre">lexists()</span></code></a>, <a class="reference internal" href="../library/os.path.html#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">isdir()</span></code></a>, <a class="reference internal" href="../library/os.path.html#os.path.isfile" title="os.path.isfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfile()</span></code></a>, <a class="reference internal" href="../library/os.path.html#os.path.islink" title="os.path.islink"><code class="xref py py-func docutils literal notranslate"><span class="pre">islink()</span></code></a>, 以及 <a class="reference internal" href="../library/os.path.html#os.path.ismount" title="os.path.ismount"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismount()</span></code></a> 现在对于包含在 OS 层级无法表示的字符或字节的路径将会返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 而不是引发 <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> 或其子类 <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a> 和 <a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33721">bpo-33721</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">expanduser()</span></code></a> 在 Windows 上现在改用 <span class="target" id="index-66"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">USERPROFILE</span></code> 环境变量而不再使用 <span class="target" id="index-67"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOME</span></code>,后者通常不会为一般用户账户设置。 (由 Anthony Sottile 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36264">bpo-36264</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/os.path.html#os.path.isdir" title="os.path.isdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">isdir()</span></code></a> 在 Windows 上将不再为不存在的目录的链接返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
|
||
<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">realpath()</span></code></a> 在 Windows 上现在会识别重解析点,包括符号链接和目录连接。</p>
|
||
<p>(由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37834">bpo-37834</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="pathlib">
|
||
<h3>pathlib<a class="headerlink" href="#pathlib" title="永久链接至标题">¶</a></h3>
|
||
<p>返回布尔值结果的 <a class="reference internal" href="../library/pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> 方法例如 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.exists" title="pathlib.Path.exists"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exists()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_dir" title="pathlib.Path.is_dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_dir()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_file" title="pathlib.Path.is_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_file()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_mount" title="pathlib.Path.is_mount"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_mount()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_symlink" title="pathlib.Path.is_symlink"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_symlink()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_block_device" title="pathlib.Path.is_block_device"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_block_device()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_char_device" title="pathlib.Path.is_char_device"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_char_device()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_fifo" title="pathlib.Path.is_fifo"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_fifo()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.is_socket" title="pathlib.Path.is_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_socket()</span></code></a> 现在对于包含在 OS 层级无法表示的字符或字节的路径将会返回 <code class="docutils literal notranslate"><span class="pre">False</span></code> 而不是引发 <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> 或其子类 <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33721">bpo-33721</a> 中贡献。)</p>
|
||
<p>增加了 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.link_to" title="pathlib.Path.link_to"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.Path.link_to()</span></code></a> 用于创建指向路径的硬链接。 (由 Joannah Nanjekye 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26978">bpo-26978</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="pickle">
|
||
<h3>pickle<a class="headerlink" href="#pickle" title="永久链接至标题">¶</a></h3>
|
||
<p><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> 扩展子类化针对 C 优化的 <a class="reference internal" href="../library/pickle.html#pickle.Pickler" title="pickle.Pickler"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pickler</span></code></a> 现在可通过定义特殊的 <a class="reference internal" href="../library/pickle.html#pickle.Pickler.reducer_override" title="pickle.Pickler.reducer_override"><code class="xref py py-meth docutils literal notranslate"><span class="pre">reducer_override()</span></code></a> 方法来重载函数和类的封存逻辑。 (由 Pierre Glaser 和 Olivier Grisel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35900">bpo-35900</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="plistlib">
|
||
<h3>plistlib<a class="headerlink" href="#plistlib" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的 <a class="reference internal" href="../library/plistlib.html#plistlib.UID" title="plistlib.UID"><code class="xref py py-class docutils literal notranslate"><span class="pre">plistlib.UID</span></code></a> 并启动了对读取和写入经过 NSKeyedArchiver 编码的二进制 plists 的支持。 (由 Jon Janzen 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26707">bpo-26707</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="pprint">
|
||
<h3>pprint<a class="headerlink" href="#pprint" title="永久链接至标题">¶</a></h3>
|
||
<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> 模块为一些函数添加了 <em>sort_dicts</em> 形参。 默认情况下,这些函数会继续在渲染或打印之前对字典进行排序。 但是,如果 <em>sort_dicts</em> 设为假值,则字典将保持键插入时的顺序。 这在调试期间与 JSON 输入进行比较时会很有用。</p>
|
||
<p>除此之外,还增加了一个方便的新函数 <a class="reference internal" href="../library/pprint.html#pprint.pp" title="pprint.pp"><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint.pp()</span></code></a>,它类似于 <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> 但它的 <em>sort_dicts</em> 默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span><span class="p">,</span> <span class="n">pp</span>
|
||
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">source</span><span class="o">=</span><span class="s1">'input.txt'</span><span class="p">,</span> <span class="n">operation</span><span class="o">=</span><span class="s1">'filter'</span><span class="p">,</span> <span class="n">destination</span><span class="o">=</span><span class="s1">'output.txt'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">pp</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span> <span class="c1"># Original order</span>
|
||
<span class="go">{'source': 'input.txt',</span>
|
||
<span class="go"> 'operation': 'filter',</span>
|
||
<span class="go"> 'destination': 'output.txt'}</span>
|
||
<span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span> <span class="c1"># Keys sorted alphabetically</span>
|
||
<span class="go">{'destination': 'output.txt',</span>
|
||
<span class="go"> 'operation': 'filter',</span>
|
||
<span class="go"> 'source': 'input.txt'}</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Rémi Lapeyre 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30670">bpo-30670</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="py-compile">
|
||
<h3>py_compile<a class="headerlink" href="#py-compile" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/py_compile.html#py_compile.compile" title="py_compile.compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">py_compile.compile()</span></code></a> 现在支持静默模式。 (由 Joannah Nanjekye 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=22640">bpo-22640</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="shlex">
|
||
<h3>shlex<a class="headerlink" href="#shlex" title="永久链接至标题">¶</a></h3>
|
||
<p>新增了 <a class="reference internal" href="../library/shlex.html#shlex.join" title="shlex.join"><code class="xref py py-func docutils literal notranslate"><span class="pre">shlex.join()</span></code></a> 函数作为 <a class="reference internal" href="../library/shlex.html#shlex.split" title="shlex.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">shlex.split()</span></code></a> 的逆操作。 (由 Bo Bayles 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32102">bpo-32102</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="shutil">
|
||
<h3>shutil<a class="headerlink" href="#shutil" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 现在接受新的 <code class="docutils literal notranslate"><span class="pre">dirs_exist_ok</span></code> 关键字参数。 (由 Josh Bronson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=20849">bpo-20849</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/shutil.html#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.make_archive()</span></code></a> 现在对新的归档默认使用 modern pax (POSIX.1-2001) 格式以提升可移植性和标准一致性,此特性继承自对 <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> 模块的相应更改。 (由 C.A.M. Gerlach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30661">bpo-30661</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.rmtree()</span></code></a> 在 Windows 上现在会移除目录连接而不会递归地先移除其中的内容。 (由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37834">bpo-37834</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="socket">
|
||
<h3>socket<a class="headerlink" href="#socket" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了便捷的 <a class="reference internal" href="../library/socket.html#socket.create_server" title="socket.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_server()</span></code></a> 和 <a class="reference internal" href="../library/socket.html#socket.has_dualstack_ipv6" title="socket.has_dualstack_ipv6"><code class="xref py py-meth docutils literal notranslate"><span class="pre">has_dualstack_ipv6()</span></code></a> 函数以自动化在创建服务器套接字时通常情况下所必须的任务,包括在同一套接字中同时接受 IPv4 和 IPv6 连接。 (由 Giampaolo Rodolà 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=17561">bpo-17561</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/socket.html#socket.if_nameindex" title="socket.if_nameindex"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.if_nameindex()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.if_nametoindex" title="socket.if_nametoindex"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.if_nametoindex()</span></code></a> 和 <a class="reference internal" href="../library/socket.html#socket.if_indextoname" title="socket.if_indextoname"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.if_indextoname()</span></code></a> 函数已经在 Windows 上实现。 (由 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37007">bpo-37007</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="ssl">
|
||
<h3>ssl<a class="headerlink" href="#ssl" title="永久链接至标题">¶</a></h3>
|
||
<p>增加了 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.post_handshake_auth" title="ssl.SSLContext.post_handshake_auth"><code class="xref py py-attr docutils literal notranslate"><span class="pre">post_handshake_auth</span></code></a> 和 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.verify_client_post_handshake" title="ssl.SSLSocket.verify_client_post_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">verify_client_post_handshake()</span></code></a> 分别启用和初始化 TLS 1.3 握手后验证。 (由 Christian Heimes 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34670">bpo-34670</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="statistics">
|
||
<h3>statistics<a class="headerlink" href="#statistics" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了 <a class="reference internal" href="../library/statistics.html#statistics.fmean" title="statistics.fmean"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.fmean()</span></code></a> 作为 <a class="reference internal" href="../library/statistics.html#statistics.mean" title="statistics.mean"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.mean()</span></code></a> 的更快速的浮点数版版本。 (由 Raymond Hettinger 和 Steven D'Aprano 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35904">bpo-35904</a> 中贡献。)</p>
|
||
<p>添加了 <a class="reference internal" href="../library/statistics.html#statistics.geometric_mean" title="statistics.geometric_mean"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.geometric_mean()</span></code></a> (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=27181">bpo-27181</a> 中贡献。)</p>
|
||
<p>添加了 <a class="reference internal" href="../library/statistics.html#statistics.multimode" title="statistics.multimode"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.multimode()</span></code></a> 用于返回最常见值的列表。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35892">bpo-35892</a> 中贡献。)</p>
|
||
<p>添加了 <a class="reference internal" href="../library/statistics.html#statistics.quantiles" title="statistics.quantiles"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.quantiles()</span></code></a> 用于将数据或分布划分为多个等概率区间(例如四分位、十分位或百分位)。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36546">bpo-36546</a> 中贡献。)</p>
|
||
<p>添加了 <a class="reference internal" href="../library/statistics.html#statistics.NormalDist" title="statistics.NormalDist"><code class="xref py py-class docutils literal notranslate"><span class="pre">statistics.NormalDist</span></code></a> 用于创建和操纵随机变量的正态分布。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36018">bpo-36018</a> 中贡献。)</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">temperature_feb</span> <span class="o">=</span> <span class="n">NormalDist</span><span class="o">.</span><span class="n">from_samples</span><span class="p">([</span><span class="mi">4</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">14</span><span class="p">])</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span><span class="o">.</span><span class="n">mean</span>
|
||
<span class="go">6.0</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span><span class="o">.</span><span class="n">stdev</span>
|
||
<span class="go">6.356099432828281</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span><span class="o">.</span><span class="n">cdf</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># Chance of being under 3 degrees</span>
|
||
<span class="go">0.3184678262814532</span>
|
||
<span class="gp">>>> </span><span class="c1"># Relative chance of being 7 degrees versus 10 degrees</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span><span class="o">.</span><span class="n">pdf</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="o">/</span> <span class="n">temperature_feb</span><span class="o">.</span><span class="n">pdf</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
||
<span class="go">1.2039930378537762</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">el_niño</span> <span class="o">=</span> <span class="n">NormalDist</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span> <span class="o">+=</span> <span class="n">el_niño</span> <span class="c1"># Add in a climate effect</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span>
|
||
<span class="go">NormalDist(mu=10.0, sigma=6.830080526611674)</span>
|
||
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span> <span class="o">*</span> <span class="p">(</span><span class="mi">9</span><span class="o">/</span><span class="mi">5</span><span class="p">)</span> <span class="o">+</span> <span class="mi">32</span> <span class="c1"># Convert to Fahrenheit</span>
|
||
<span class="go">NormalDist(mu=50.0, sigma=12.294144947901014)</span>
|
||
<span class="gp">>>> </span><span class="n">temperature_feb</span><span class="o">.</span><span class="n">samples</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># Generate random samples</span>
|
||
<span class="go">[7.672102882379219, 12.000027119750287, 4.647488369766392]</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="sys">
|
||
<h3>sys<a class="headerlink" href="#sys" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的 <a class="reference internal" href="../library/sys.html#sys.unraisablehook" title="sys.unraisablehook"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.unraisablehook()</span></code></a> 函数,可被重载以便控制如何处理“不可引发的异常”。 它会在发生了一个异常但 Python 没有办法处理时被调用。 例如,当一个析构器在垃圾回收时 (<a class="reference internal" href="../library/gc.html#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.collect()</span></code></a>) 所引发的异常。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36829">bpo-36829</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="tarfile">
|
||
<h3>tarfile<a class="headerlink" href="#tarfile" title="永久链接至标题">¶</a></h3>
|
||
<p><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> 模块现在对新的归档默认使用 modern pax (POSIX.1-2001) 格式而不再是之前的 GNU 专属格式。 这通过标准化和可扩展格式的统一编码 (UTF-8) 提升了跨平台可移植性,还提供了其他一些益处。 (由 C.A.M. Gerlach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36268">bpo-36268</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="tempfile">
|
||
<h3>tempfile<a class="headerlink" href="#tempfile" title="永久链接至标题">¶</a></h3>
|
||
<p>As of 3.8.20 on Windows, the default mode <code class="docutils literal notranslate"><span class="pre">0o700</span></code> used by
|
||
<a class="reference internal" href="../library/tempfile.html#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">tempfile.mkdtemp()</span></code></a> now limits access to the new directory due to
|
||
changes to <a class="reference internal" href="../library/os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.mkdir()</span></code></a>. This is a mitigation for CVE-2024-4030.
|
||
(Contributed by Steve Dower in <a class="reference external" href="https://github.com/python/cpython/issues/118486">gh-118486</a>.)</p>
|
||
</section>
|
||
<section id="threading">
|
||
<h3>threading<a class="headerlink" href="#threading" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了新的 <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> 函数用来处理未捕获的 <a class="reference internal" href="../library/threading.html#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Thread.run()</span></code></a> 异常。 它可被重载以便控制如何处理未捕获的 <a class="reference internal" href="../library/threading.html#threading.Thread.run" title="threading.Thread.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Thread.run()</span></code></a> 异常。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=1230540">bpo-1230540</a> 中贡献。)</p>
|
||
<p>添加了新的 <a class="reference internal" href="../library/threading.html#threading.get_native_id" title="threading.get_native_id"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.get_native_id()</span></code></a> 函数以及 <a class="reference internal" href="../library/threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> 类的 <a class="reference internal" href="../library/threading.html#threading.Thread.native_id" title="threading.Thread.native_id"><code class="xref py py-data docutils literal notranslate"><span class="pre">native_id</span></code></a> 属性。 它们会返回内核所分配给当前线程的原生整数线程 ID。 此特性仅在特定平台上可用,参见 <a class="reference internal" href="../library/threading.html#threading.get_native_id" title="threading.get_native_id"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_native_id</span></code></a> 了解详情。 (由 Jake Tesler 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36084">bpo-36084</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="tokenize">
|
||
<h3>tokenize<a class="headerlink" href="#tokenize" title="永久链接至标题">¶</a></h3>
|
||
<p>当提供不带末尾新行的输入时,<a class="reference internal" href="../library/tokenize.html#module-tokenize" title="tokenize: Lexical scanner for Python source code."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tokenize</span></code></a> 模块现在会隐式地添加 <code class="docutils literal notranslate"><span class="pre">NEWLINE</span></code> 形符。 此行为现在已与 C 词法分析器的内部行为相匹配。 (由 Ammar Askar 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33899">bpo-33899</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="tkinter">
|
||
<h3>tkinter<a class="headerlink" href="#tkinter" title="永久链接至标题">¶</a></h3>
|
||
<p>在 <code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.Spinbox</span></code> 中添加了方法 <code class="xref py py-meth docutils literal notranslate"><span class="pre">selection_from()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">selection_present()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">selection_range()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">selection_to()</span></code>。 (由 Juliette Monsel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34829">bpo-34829</a> 中贡献。)</p>
|
||
<p>在 <code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.Canvas</span></code> 类中添加了方法 <code class="xref py py-meth docutils literal notranslate"><span class="pre">moveto()</span></code>。 (由 Juliette Monsel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23831">bpo-23831</a> 中贡献。)</p>
|
||
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.PhotoImage</span></code> 类现在具有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">transparency_get()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">transparency_set()</span></code> 方法。 (由 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=25451">bpo-25451</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="time">
|
||
<h3>time<a class="headerlink" href="#time" title="永久链接至标题">¶</a></h3>
|
||
<p>为 macOS 10.12 添加了新的时钟 <a class="reference internal" href="../library/time.html#time.CLOCK_UPTIME_RAW" title="time.CLOCK_UPTIME_RAW"><code class="xref py py-data docutils literal notranslate"><span class="pre">CLOCK_UPTIME_RAW</span></code></a>。 (由 Joannah Nanjekye 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35702">bpo-35702</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="typing">
|
||
<h3>typing<a class="headerlink" href="#typing" title="永久链接至标题">¶</a></h3>
|
||
<p><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> 模块加入了一些新特性:</p>
|
||
<ul>
|
||
<li><p>一个带有键专属类型的字典类型。 参见 <span class="target" id="index-68"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0589"><strong>PEP 589</strong></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>。 TypedDict 只使用字符串作为键。 默认情况下每个键都要求提供。 指定 "total=False" 以允许键作为可选项:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Location</span><span class="p">(</span><span class="n">TypedDict</span><span class="p">,</span> <span class="n">total</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
|
||
<span class="n">lat_long</span><span class="p">:</span> <span class="nb">tuple</span>
|
||
<span class="n">grid_square</span><span class="p">:</span> <span class="nb">str</span>
|
||
<span class="n">xy_coordinate</span><span class="p">:</span> <span class="nb">tuple</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>Literal 类型。 参见 <span class="target" id="index-69"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0586"><strong>PEP 586</strong></a> 和 <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>。 Literal 类型指明一个形参或返回值被限定为一个或多个特定的字面值:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_status</span><span class="p">(</span><span class="n">port</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-></span> <span class="n">Literal</span><span class="p">[</span><span class="s1">'connected'</span><span class="p">,</span> <span class="s1">'disconnected'</span><span class="p">]:</span>
|
||
<span class="o">...</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>"Final" 变量、函数、方法和类。 参见 <span class="target" id="index-70"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0591"><strong>PEP 591</strong></a>, <a class="reference internal" href="../library/typing.html#typing.Final" title="typing.Final"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Final</span></code></a> 和 <a class="reference internal" href="../library/typing.html#typing.final" title="typing.final"><code class="xref py py-func docutils literal notranslate"><span class="pre">typing.final()</span></code></a>。 final 限定符会指示静态类型检查器限制进行子类化、重载或重新赋值:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pi</span><span class="p">:</span> <span class="n">Final</span><span class="p">[</span><span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="mf">3.1415926536</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>协议定义。 参见 <span class="target" id="index-71"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0544"><strong>PEP 544</strong></a>, <a class="reference internal" href="../library/typing.html#typing.Protocol" title="typing.Protocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.Protocol</span></code></a> 和 <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">typing.runtime_checkable()</span></code></a>。 简单的 ABC 例如 <a class="reference internal" href="../library/typing.html#typing.SupportsInt" title="typing.SupportsInt"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.SupportsInt</span></code></a> 现在是 <code class="docutils literal notranslate"><span class="pre">Protocol</span></code> 的子类。</p></li>
|
||
<li><p>新的协议类 <a class="reference internal" href="../library/typing.html#typing.SupportsIndex" title="typing.SupportsIndex"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.SupportsIndex</span></code></a>。</p></li>
|
||
<li><p>新的函数 <a class="reference internal" href="../library/typing.html#typing.get_origin" title="typing.get_origin"><code class="xref py py-func docutils literal notranslate"><span class="pre">typing.get_origin()</span></code></a> 和 <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>。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="unicodedata">
|
||
<h3>unicodedata<a class="headerlink" href="#unicodedata" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> 模块现在已升级为使用 <a class="reference external" href="http://blog.unicode.org/2019/05/unicode-12-1-en.html">Unicode 12.1.0</a> 发布版。</p>
|
||
<p>新的函数 <a class="reference internal" href="../library/unicodedata.html#unicodedata.is_normalized" title="unicodedata.is_normalized"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_normalized()</span></code></a> 可被用来验证字符串是否为特定正规形式,通常会比实际进行字符串正规化要快得多。 (由 Max Belanger, David Euresti 和 Greg Price 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32285">bpo-32285</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37966">bpo-37966</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="unittest">
|
||
<h3>unittest<a class="headerlink" href="#unittest" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了 <a class="reference internal" href="../library/unittest.mock.html#unittest.mock.AsyncMock" title="unittest.mock.AsyncMock"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncMock</span></code></a> 以支持异步版本的 <a class="reference internal" href="../library/unittest.mock.html#unittest.mock.Mock" title="unittest.mock.Mock"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mock</span></code></a>。 同时也添加了相应的断言函数用于测试。 (由 Lisa Roach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26467">bpo-26467</a> 中贡献。)</p>
|
||
<p>unittest 添加了 <a class="reference internal" href="../library/unittest.html#unittest.addModuleCleanup" title="unittest.addModuleCleanup"><code class="xref py py-func docutils literal notranslate"><span class="pre">addModuleCleanup()</span></code></a> 和 <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addClassCleanup" title="unittest.TestCase.addClassCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addClassCleanup()</span></code></a> 以支持对 <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> 和 <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> 进行清理。 (由 Lisa Roach 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=24412">bpo-24412</a> 中贡献。)</p>
|
||
<p>一些模拟断言函数现在也会在失败时打印一个实际调用列表。 (由 Petter Strandmark 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35047">bpo-35047</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> 模块已支持通过 <a class="reference internal" href="../library/unittest.html#unittest.IsolatedAsyncioTestCase" title="unittest.IsolatedAsyncioTestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.IsolatedAsyncioTestCase</span></code></a> 来使用协程作为测试用例。 (由 Andrew Svetlov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32972">bpo-32972</a> 中贡献。)</p>
|
||
<p>示例:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
|
||
|
||
|
||
<span class="k">class</span> <span class="nc">TestRequest</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">IsolatedAsyncioTestCase</span><span class="p">):</span>
|
||
|
||
<span class="k">async</span> <span class="k">def</span> <span class="nf">asyncSetUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="k">await</span> <span class="n">AsyncConnection</span><span class="p">()</span>
|
||
|
||
<span class="k">async</span> <span class="k">def</span> <span class="nf">test_get</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="n">response</span> <span class="o">=</span> <span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"https://example.com"</span><span class="p">)</span>
|
||
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status_code</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
|
||
|
||
<span class="k">async</span> <span class="k">def</span> <span class="nf">asyncTearDown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||
<span class="k">await</span> <span class="bp">self</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||
|
||
|
||
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
||
<span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="venv">
|
||
<h3>venv<a class="headerlink" href="#venv" title="永久链接至标题">¶</a></h3>
|
||
<p>现在 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 在所有平台上都会包含 <code class="docutils literal notranslate"><span class="pre">Activate.ps1</span></code> 脚本用于在 PowerShell Core 6.1 下激活虚拟环境。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32718">bpo-32718</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="weakref">
|
||
<h3>weakref<a class="headerlink" href="#weakref" title="永久链接至标题">¶</a></h3>
|
||
<p>由 <a class="reference internal" href="../library/weakref.html#weakref.proxy" title="weakref.proxy"><code class="xref py py-func docutils literal notranslate"><span class="pre">weakref.proxy()</span></code></a> 返回的代理对象现在除其他算术运算符外也支持矩阵乘法运算符 <code class="docutils literal notranslate"><span class="pre">@</span></code> 和 <code class="docutils literal notranslate"><span class="pre">@=</span></code>。 (由 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36669">bpo-36669</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="xml">
|
||
<h3>xml<a class="headerlink" href="#xml" title="永久链接至标题">¶</a></h3>
|
||
<p>作为对 DTD 和外部实体检索的缓解,在默认情况下 <a class="reference internal" href="../library/xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code></a> 和 <a class="reference internal" href="../library/xml.sax.html#module-xml.sax" title="xml.sax: Package containing SAX2 base classes and convenience functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax</span></code></a> 模块不再处理外部实体。 (由 Christian Heimes 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=17239">bpo-17239</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> 模块中的 <code class="docutils literal notranslate"><span class="pre">.find*()</span></code> 方法支持通配符搜索例如 <code class="docutils literal notranslate"><span class="pre">{*}tag</span></code>,此搜索会忽略命名空间以及返回给定命名空间中所有标签的 <code class="docutils literal notranslate"><span class="pre">{namespace}*</span></code>。 (由 Stefan Behnel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=28238">bpo-28238</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> 模块提供了实现 C14N 2.0 的新函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">–xml.etree.ElementTree.canonicalize()</span></code>。 (由 Stefan Behnel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=13611">bpo-13611</a> 中贡献。)</p>
|
||
<p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.etree.ElementTree.XMLParser</span></code></a> 的目标对象可通过新的回调方法 <code class="docutils literal notranslate"><span class="pre">start_ns()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">end_ns()</span></code> 来接受命名空间声明事件。 此外,<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder" title="xml.etree.ElementTree.TreeBuilder"><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.etree.ElementTree.TreeBuilder</span></code></a> 目标可被配置为处理有关注释和处理指令事件以将它们包含在所生成的树当中。 (由 Stefan Behnel 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36676">bpo-36676</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36673">bpo-36673</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="xmlrpc">
|
||
<h3>xmlrpc<a class="headerlink" href="#xmlrpc" title="永久链接至标题">¶</a></h3>
|
||
<p><a class="reference internal" href="../library/xmlrpc.client.html#xmlrpc.client.ServerProxy" title="xmlrpc.client.ServerProxy"><code class="xref py py-class docutils literal notranslate"><span class="pre">xmlrpc.client.ServerProxy</span></code></a> 现在支持可选的 <em>headers</em> 关键字参数作为随同每次请求发送的 HTTP 标头序列。 此特征的作用之一是使得从默认的基础认证升级到更快速的会话认证成为可能。 (由 Cédric Krier 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35153">bpo-35153</a> 中贡献。)</p>
|
||
</section>
|
||
</section>
|
||
<section id="optimizations">
|
||
<h2>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题">¶</a></h2>
|
||
<ul>
|
||
<li><p><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> 模块现在能在某些情况下使用 <a class="reference internal" href="../library/os.html#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.posix_spawn()</span></code></a> 函数以获得更好的性能。 目前,它的使用仅限 macOS 和 Linux(使用 glibc 2.24 或更新版本),并要求满足以下条件:</p>
|
||
<ul class="simple">
|
||
<li><p><em>close_fds</em> 为假值;</p></li>
|
||
<li><p><em>preexec_fn</em>, <em>pass_fds</em>, <em>cwd</em> 和 <em>start_new_session</em> 形参未设置;</p></li>
|
||
<li><p><em>executable</em> 路径包含一个目录。</p></li>
|
||
</ul>
|
||
<p>(由 Joannah Nanjekye 和 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35537">bpo-35537</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfile()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 和 <a class="reference internal" href="../library/shutil.html#shutil.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.move()</span></code></a> 在 Linux 和 macOS 上会使用平台专属的 "fast-copy" 系统调用以提高效率。 "fast-copy" 意味着拷贝操作发生于内核中,从而避免在进行 "<code class="docutils literal notranslate"><span class="pre">outfd.write(infd.read())</span></code>" 等操作时使用 Python 中的用户空间缓冲区。 在 Windows 上 <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfile()</span></code></a> 会使用更大的默认缓冲区(1 MiB 而不是 16 KiB)并且使用基于 <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a> 的 <a class="reference internal" href="../library/shutil.html#shutil.copyfileobj" title="shutil.copyfileobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfileobj()</span></code></a> 版本。 在同一分区内拷贝一个 512 MiB 文件的速度提升在 Linux 上约为 +26%,在 macOS 上为 +50%,在 Windows 上为 +40%。 此外还将消耗更少的 CPU 周期。 参见 <a class="reference internal" href="../library/shutil.html#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节。 (由 Giampaolo Rodolà 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33671">bpo-33671</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 会根据其所用缓存的 <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 值使用 <a class="reference internal" href="../library/os.html#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.scandir()</span></code></a> 函数及所有拷贝函数。 拷贝一个包含 8000 文件的目录的速度提升在 Linux 上约为 +9%,在 Windows 上为 +20%,对于 Windows SMB 共享目录则为 +30%。 此外 <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> 系统调用的次数也减少了 38%,使得 <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 在网络文件系统上会特别快速。 (由Giampaolo Rodolà 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33695">bpo-33695</a> 中贡献。)</p></li>
|
||
<li><p><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 4,最早在 Python 3.4 中被引入。 它提供了比自 Python 3.0 起可用的 Protocol 3 更好的性能和更小的数据尺寸。</p></li>
|
||
<li><p>从 <code class="docutils literal notranslate"><span class="pre">PyGC_Head</span></code> 移除了一个 <code class="docutils literal notranslate"><span class="pre">Py_ssize_t</span></code> 成员。 所有跟踪 GC 的对象(例如 tuple, list, dict)大小减少了 4 或 8 字节。 (由 Inada Naoki 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33597">bpo-33597</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/uuid.html#uuid.UUID" title="uuid.UUID"><code class="xref py py-class docutils literal notranslate"><span class="pre">uuid.UUID</span></code></a> 现在会使用 <code class="docutils literal notranslate"><span class="pre">__slots__</span></code> 以减少内存足迹。 (由 Wouter Bolsterlee 和 Tal Einat 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30977">bpo-30977</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/operator.html#operator.itemgetter" title="operator.itemgetter"><code class="xref py py-func docutils literal notranslate"><span class="pre">operator.itemgetter()</span></code></a> 的性能提升了 33%。 优化了参数处理,并为常见的在元组中单个非负整数索引的情况新增了一条快速路径(这是标准库中的典型用例)。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35664">bpo-35664</a> 中贡献。</p></li>
|
||
<li><p>加快了在 <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a> 中的字段查找。 它们现在的速度快了两倍以上,成为 Python 中最快的实例变量查找形式。 (由 Raymond Hettinger, Pablo Galindo 和 Joe Jevnik, Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32492">bpo-32492</a> 中贡献。)</p></li>
|
||
<li><p>如果输入的可迭代对象的长度已知 (即输入对象实现了 <code class="docutils literal notranslate"><span class="pre">__len__</span></code>),<a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> 构造器不会过度分配内部项缓冲区。 这使得所创建的列表资源占用平均减少了 12%。 (由 Raymond Hettinger 和 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33234">bpo-33234</a> 中贡献。)</p></li>
|
||
<li><p>类变量写入速度加倍。 当一个非冗余属性被更新时,之前存在一个更新 slots 的非必要调用。 (由 Stefan Behnel, Pablo Galindo Salgado, Raymond Hettinger, Neil Schemenauer, 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36012">bpo-36012</a> 中贡献。)</p></li>
|
||
<li><p>减少了传递给许多内置函数和方法的参数转换的开销。 这使得某些简单内置函数和方法的速度提升了 20--50%。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23867">bpo-23867</a>, <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35582">bpo-35582</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36127">bpo-36127</a> 中贡献。)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">LOAD_GLOBAL</span></code> 指令现在会使用新的 "per opcode cache" 机制。 它的速度现在提升了大约 40%。 (由 Yury Selivanov 和 Inada Naoki 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=26219">bpo-26219</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="build-and-c-api-changes">
|
||
<h2>构建和 C API 的改变<a class="headerlink" href="#build-and-c-api-changes" title="永久链接至标题">¶</a></h2>
|
||
<ul>
|
||
<li><p>默认的 <a class="reference internal" href="../library/sys.html#sys.abiflags" title="sys.abiflags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.abiflags</span></code></a> 成为一个空字符串:pymalloc 的 <code class="docutils literal notranslate"><span class="pre">m</span></code> 旗标不再有意义(无论是否启用 pymalloc 构建都是兼容 ABI 的)因此已被移除。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36707">bpo-36707</a> 中贡献。)</p>
|
||
<p>改变的例子:</p>
|
||
<ul class="simple">
|
||
<li><p>只会安装 <code class="docutils literal notranslate"><span class="pre">python3.8</span></code> 程序,不再有 <code class="docutils literal notranslate"><span class="pre">python3.8m</span></code> 程序。</p></li>
|
||
<li><p>只会安装 <code class="docutils literal notranslate"><span class="pre">python3.8-config</span></code> 脚本,不再有 <code class="docutils literal notranslate"><span class="pre">python3.8m-config</span></code> 脚本。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">m</span></code> 旗标已经从动态库文件名后缀中移除:包括标准库中的扩展模块以及第三方包所产生和安装的模块例如从 PyPI 下载的模块。 以 Linux 为例,Python 3.7 的后缀 <code class="docutils literal notranslate"><span class="pre">.cpython-37m-x86_64-linux-gnu.so</span></code> 在 Python 3.8 中改为 <code class="docutils literal notranslate"><span class="pre">.cpython-38-x86_64-linux-gnu.so</span></code>。</p></li>
|
||
</ul>
|
||
</li>
|
||
<li><p>重新组织了所有头文件以更好地区分不同种类的 API:</p>
|
||
<ul class="simple">
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Include/*.h</span></code> 应为可移植的公有稳定版 C API。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Include/cpython/*.h</span></code> 应为 CPython 专属的不稳定版 C API;公有 API,部分私有 API 附加 <code class="docutils literal notranslate"><span class="pre">_Py</span></code> or <code class="docutils literal notranslate"><span class="pre">_PY</span></code> 前缀。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Include/internal/*.h</span></code> 应为 CPython 特别专属的私有内部 C API。 此 API 不具备向下兼容保证并且不应在 CPython 以外被使用。 它们的公开仅适用于特别限定的需求例如调试器和性能分析等必须直接访问 CPython 内部数据而不通过调用函数的应用。 此 API 现在是通过 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code> 安装的。</p></li>
|
||
</ul>
|
||
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35134">bpo-35134</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35081">bpo-35081</a> 中贡献,相关工作由 Eric Snow 在 Python 3.7 中发起。)</p>
|
||
</li>
|
||
<li><p>某些宏已被转换为静态内联函数:形参类型和返回类型定义良好,它们不再会有与宏相关的问题,变量具有局部作用域。 例如:</p>
|
||
<ul class="simple">
|
||
<li><p><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>, <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></p></li>
|
||
<li><p><a class="reference internal" href="../c-api/refcounting.html#c.Py_XINCREF" title="Py_XINCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XINCREF()</span></code></a>, <a class="reference internal" href="../c-api/refcounting.html#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a></p></li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_INIT()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_INIT_VAR()</span></code></p></li>
|
||
<li><p>私有函数: <code class="xref c c-func docutils literal notranslate"><span class="pre">_PyObject_GC_TRACK()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">_PyObject_GC_UNTRACK()</span></code>, <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_Dealloc()</span></code></p></li>
|
||
</ul>
|
||
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35059">bpo-35059</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyByteArray_Init()</span></code> 和 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyByteArray_Fini()</span></code> 函数已被移除。 它们自 Python 2.7.4 和 Python 3.2.0 起就没有任何用处,被排除在受限 API (稳定版 ABI) 之外,并且未被写入文档。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35713">bpo-35713</a> 中贡献。)</p></li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyExceptionClass_Name()</span></code> 的结果类型现在是 <code class="docutils literal notranslate"><span class="pre">const</span> <span class="pre">char</span> <span class="pre">*</span></code> 而非 <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33818">bpo-33818</a> 中贡献。)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">Modules/Setup.dist</span></code> 与 <code class="docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 两者的共存已被移除。 之前在更新 CPython 源码树时,开发者必须手动拷贝 <code class="docutils literal notranslate"><span class="pre">Modules/Setup.dist</span></code> (在源码树内) 到 <code class="docutils literal notranslate"><span class="pre">Modules/Setup</span></code> (在构建树内) 以反映上游的任何改变。 旧特性对打包者来说有一点益处,但代价是对追踪 CPython 开发进程的开发者造成经常性的麻烦,因为忘记拷贝该文件可能导致构建失败。</p>
|
||
<p>现在构建系统总是会从源码树内的 <code class="docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 读取数据。 建议希望定制该文件的开发者在 CPython 的一个 git 分叉或补丁文件中维护他们的更改,就如他们对源码树做任何其他改变时一样。</p>
|
||
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32430">bpo-32430</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>将 Python 数字转换为 C 整型的函数例如 <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLong" title="PyLong_AsLong"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLong()</span></code></a> 以及带有 <code class="docutils literal notranslate"><span class="pre">'i'</span></code> 之类整型转换格式单元的参数解析函数例如 <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> 现在如果可能将会使用 <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> 特殊方法而不是 <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>。 对于带有 <code class="docutils literal notranslate"><span class="pre">__int__()</span></code> 方法但没有 <code class="docutils literal notranslate"><span class="pre">__index__()</span></code> 方法的对象 (例如 <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> 和 <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>) 将会发出弃用警告。 对于实现了 <code class="docutils literal notranslate"><span class="pre">__index__()</span></code> 的对象 <a class="reference internal" href="../c-api/number.html#c.PyNumber_Check" title="PyNumber_Check"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Check()</span></code></a> 现在将返回 <code class="docutils literal notranslate"><span class="pre">1</span></code>。 <a class="reference internal" href="../c-api/number.html#c.PyNumber_Long" title="PyNumber_Long"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Long()</span></code></a>, <a class="reference internal" href="../c-api/number.html#c.PyNumber_Float" title="PyNumber_Float"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_Float()</span></code></a> 和 <a class="reference internal" href="../c-api/float.html#c.PyFloat_AsDouble" title="PyFloat_AsDouble"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFloat_AsDouble()</span></code></a> 现在如果可能也将会使用 <code class="docutils literal notranslate"><span class="pre">__index__()</span></code> 方法。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36048">bpo-36048</a> 和 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=20092">bpo-20092</a> 中贡献。)</p></li>
|
||
<li><p>堆分配类型对象现在将增加它们在 <a class="reference internal" href="../c-api/allocation.html#c.PyObject_Init" title="PyObject_Init"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Init()</span></code></a> (及其对应的宏 <code class="docutils literal notranslate"><span class="pre">PyObject_INIT</span></code>) 中的引用计数而不是在 <a class="reference internal" href="../c-api/type.html#c.PyType_GenericAlloc" title="PyType_GenericAlloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_GenericAlloc()</span></code></a> 中。 修改实例分配或中止分配的类型可能需要进行调整。 (由 Elizondo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35810">bpo-35810</a> 中贡献。)</p></li>
|
||
<li><p>新增函数 <a class="reference internal" href="../c-api/code.html#c.PyCode_NewWithPosOnlyArgs" title="PyCode_NewWithPosOnlyArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_NewWithPosOnlyArgs()</span></code></a> 允许创建代码对象例如 <a class="reference internal" href="../c-api/code.html#c.PyCode_New" title="PyCode_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_New()</span></code></a>,但带有一个额外的 <em>posonlyargcount</em> 形参以指明仅限位置参数的数量。 (由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37221">bpo-37221</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../c-api/init.html#c.Py_SetPath" title="Py_SetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></a> 现在会将 <a class="reference internal" href="../library/sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</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_GetProgramName" title="Py_GetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramName()</span></code></a>)。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=38234">bpo-38234</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="deprecated">
|
||
<h2>弃用<a class="headerlink" href="#deprecated" title="永久链接至标题">¶</a></h2>
|
||
<ul>
|
||
<li><p>distutils 的 <code class="docutils literal notranslate"><span class="pre">bdist_wininst</span></code> 命令现在已弃用,请改用 <code class="docutils literal notranslate"><span class="pre">bdist_wheel</span></code> (wheel 包)。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37481">bpo-37481</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ElementTree</span></code></a> 模块中的已弃用方法 <code class="docutils literal notranslate"><span class="pre">getchildren()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">getiterator()</span></code> 现在会引发 <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/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a>。 它们将在 Python 3.9 中被移除。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29209">bpo-29209</a> 中贡献。)</p></li>
|
||
<li><p>将不是 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ThreadPoolExecutor</span></code></a> 的实例的对象传给 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.set_default_executor" title="asyncio.loop.set_default_executor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.set_default_executor()</span></code></a> 已被弃用,并将在 Python 3.9 中被禁止。 (由 Elvis Pranskevichus 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34075">bpo-34075</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.dom.pulldom.html#xml.dom.pulldom.DOMEventStream" title="xml.dom.pulldom.DOMEventStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.dom.pulldom.DOMEventStream</span></code></a>, <a class="reference internal" href="../library/wsgiref.html#wsgiref.util.FileWrapper" title="wsgiref.util.FileWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">wsgiref.util.FileWrapper</span></code></a> 和 <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> 的 <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> 方法已被弃用。</p>
|
||
<p>这些方法的实现会忽略它们的 <em>index</em> 形参,并改为返回下一条目。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=9372">bpo-9372</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/typing.html#typing.NamedTuple" title="typing.NamedTuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">typing.NamedTuple</span></code></a> 类已弃用了 <code class="docutils literal notranslate"><span class="pre">_field_types</span></code> 属性而改用具有同样信息的 <code class="docutils literal notranslate"><span class="pre">__annotations__</span></code> 属性。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36320">bpo-36320</a> 中贡献。)</p></li>
|
||
<li><p><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> 类 <code class="docutils literal notranslate"><span class="pre">Num</span></code>, <code class="docutils literal notranslate"><span class="pre">Str</span></code>, <code class="docutils literal notranslate"><span class="pre">Bytes</span></code>, <code class="docutils literal notranslate"><span class="pre">NameConstant</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Ellipsis</span></code> 被视为已弃用并将在未来的 Python 版本中被移除。 应当改用 <code class="xref py py-class docutils literal notranslate"><span class="pre">Constant</span></code>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32892">bpo-32892</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/ast.html#ast.NodeVisitor" title="ast.NodeVisitor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.NodeVisitor</span></code></a> 的方法 <code class="docutils literal notranslate"><span class="pre">visit_Num()</span></code>, <code class="docutils literal notranslate"><span class="pre">visit_Str()</span></code>, <code class="docutils literal notranslate"><span class="pre">visit_Bytes()</span></code>, <code class="docutils literal notranslate"><span class="pre">visit_NameConstant()</span></code> 和 <code class="docutils literal notranslate"><span class="pre">visit_Ellipsis()</span></code> 现在已被弃用,并且在未来的 Python 版本中将不再被调用。 添加 <code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Constant()</span></code> 方法来处理所有常量节点。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36917">bpo-36917</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/asyncio-task.html#asyncio.coroutine" title="asyncio.coroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.coroutine()</span></code></a> <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> 已弃用并将在 3.10 版中被移除。 原有的 <code class="docutils literal notranslate"><span class="pre">@asyncio.coroutine</span></code> 请改用 <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> 来替代。 (由 Andrew Svetlov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36921">bpo-36921</a> 中贡献。)</p></li>
|
||
<li><p>在 <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> 中,<em>loop</em> 参数的显式传递已被弃用并且到 3.10 版时将会在以下函数中被移除: <a class="reference internal" href="../library/asyncio-task.html#asyncio.sleep" title="asyncio.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.sleep()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.gather" title="asyncio.gather"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.gather()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.shield" title="asyncio.shield"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.shield()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.wait_for" title="asyncio.wait_for"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.wait_for()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.wait" title="asyncio.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.wait()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.as_completed" title="asyncio.as_completed"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.as_completed()</span></code></a>, <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a>, <a class="reference internal" href="../library/asyncio-sync.html#asyncio.Lock" title="asyncio.Lock"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Lock</span></code></a>, <a class="reference internal" href="../library/asyncio-sync.html#asyncio.Event" title="asyncio.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Event</span></code></a>, <a class="reference internal" href="../library/asyncio-sync.html#asyncio.Condition" title="asyncio.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Condition</span></code></a>, <a class="reference internal" href="../library/asyncio-sync.html#asyncio.Semaphore" title="asyncio.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Semaphore</span></code></a>, <a class="reference internal" href="../library/asyncio-sync.html#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.BoundedSemaphore</span></code></a>, <a class="reference internal" href="../library/asyncio-queue.html#asyncio.Queue" title="asyncio.Queue"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Queue</span></code></a>, <a class="reference internal" href="../library/asyncio-subprocess.html#asyncio.create_subprocess_exec" title="asyncio.create_subprocess_exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.create_subprocess_exec()</span></code></a> 以及 <a class="reference internal" href="../library/asyncio-subprocess.html#asyncio.create_subprocess_shell" title="asyncio.create_subprocess_shell"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.create_subprocess_shell()</span></code></a>。</p></li>
|
||
<li><p>将协程对象显式传递给 <a class="reference internal" href="../library/asyncio-task.html#asyncio.wait" title="asyncio.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.wait()</span></code></a> 的做法已被弃用并且将在 3.11 版中被移除。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34790">bpo-34790</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> 模块中的下列函数和方法已被弃用: <a class="reference internal" href="../library/gettext.html#gettext.lgettext" title="gettext.lgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgettext()</span></code></a>, <a class="reference internal" href="../library/gettext.html#gettext.ldgettext" title="gettext.ldgettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ldgettext()</span></code></a>, <a class="reference internal" href="../library/gettext.html#gettext.lngettext" title="gettext.lngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">lngettext()</span></code></a> 和 <a class="reference internal" href="../library/gettext.html#gettext.ldngettext" title="gettext.ldngettext"><code class="xref py py-func docutils literal notranslate"><span class="pre">ldngettext()</span></code></a>。 它们返回已编码的字节串,如果转换后的字符串存在编码问题你将可能遭遇预期之外的异常。 更好的做法是使用 Python 3 中返回 Unicode 字符串的操作作为替代。 前面的函数都早已不宜使用。</p>
|
||
<p>函数 <a class="reference internal" href="../library/gettext.html#gettext.bind_textdomain_codeset" title="gettext.bind_textdomain_codeset"><code class="xref py py-func docutils literal notranslate"><span class="pre">bind_textdomain_codeset()</span></code></a>,方法 <a class="reference internal" href="../library/gettext.html#gettext.NullTranslations.output_charset" title="gettext.NullTranslations.output_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">output_charset()</span></code></a> 和 <a class="reference internal" href="../library/gettext.html#gettext.NullTranslations.set_output_charset" title="gettext.NullTranslations.set_output_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_output_charset()</span></code></a>,以及函数 <a class="reference internal" href="../library/gettext.html#gettext.translation" title="gettext.translation"><code class="xref py py-func docutils literal notranslate"><span class="pre">translation()</span></code></a> 和 <a class="reference internal" href="../library/gettext.html#gettext.install" title="gettext.install"><code class="xref py py-func docutils literal notranslate"><span class="pre">install()</span></code></a> 的 <em>codeset</em> 形参也已被弃用,因为它们仅适用于 <code class="docutils literal notranslate"><span class="pre">l*gettext()</span></code> 函数。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33710">bpo-33710</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> 的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">isAlive()</span></code> 方法已弃用。 (由 Dong-hee Na 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35283">bpo-35283</a> 中贡献。)</p></li>
|
||
<li><p>许多接受整数参数的内置和扩展模块的函数现在将对传入 <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>, <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> 以及任何其他可被转换为整数但会丢失精度(即具有 <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> 方法但没有 <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> 方法)的对象发出弃用警告。 在未来的版本中则将报错。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36048">bpo-36048</a> 中贡献。)</p></li>
|
||
<li><p>以下参数作为关键字参数传递的方式已被弃用:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../library/functools.html#functools.partialmethod" title="functools.partialmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partialmethod()</span></code></a>, <a class="reference internal" href="../library/weakref.html#weakref.finalize" title="weakref.finalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">weakref.finalize()</span></code></a>, <a class="reference internal" href="../library/profile.html#profile.Profile.runcall" title="profile.Profile.runcall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">profile.Profile.runcall()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">cProfile.Profile.runcall()</span></code>, <a class="reference internal" href="../library/bdb.html#bdb.Bdb.runcall" title="bdb.Bdb.runcall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bdb.Bdb.runcall()</span></code></a>, <a class="reference internal" href="../library/trace.html#trace.Trace.runfunc" title="trace.Trace.runfunc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">trace.Trace.runfunc()</span></code></a> 与 <a class="reference internal" href="../library/curses.html#curses.wrapper" title="curses.wrapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">curses.wrapper()</span></code></a> 中的 <em>func</em>。</p></li>
|
||
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unittest.TestCase.addCleanup()</span></code></a> 中的 <em>function</em>。</p></li>
|
||
<li><p><a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ThreadPoolExecutor</span></code></a> 和 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.ProcessPoolExecutor</span></code></a> 的 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a> 方法中的 <em>fn</em>。</p></li>
|
||
<li><p><a class="reference internal" href="../library/contextlib.html#contextlib.ExitStack.callback" title="contextlib.ExitStack.callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">contextlib.ExitStack.callback()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">contextlib.AsyncExitStack.callback()</span></code> 和 <a class="reference internal" href="../library/contextlib.html#contextlib.AsyncExitStack.push_async_callback" title="contextlib.AsyncExitStack.push_async_callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">contextlib.AsyncExitStack.push_async_callback()</span></code></a> 中的 <em>callback</em>。</p></li>
|
||
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.managers.Server</span></code> 和 <code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.managers.SharedMemoryServer</span></code> 的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">create()</span></code> 方法中的 <em>c</em> 和 <em>typeid</em>。</p></li>
|
||
<li><p><a class="reference internal" href="../library/weakref.html#weakref.finalize" title="weakref.finalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">weakref.finalize()</span></code></a> 中的 <em>obj</em>。</p></li>
|
||
</ul>
|
||
<p>在未来版本的 Python 中,它们将成为 <a class="reference internal" href="../glossary.html#positional-only-parameter"><span class="std std-ref">仅限位置参数</span></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36492">bpo-36492</a> 中贡献。)</p>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
<section id="api-and-feature-removals">
|
||
<h2>API 与特性的移除<a class="headerlink" href="#api-and-feature-removals" title="永久链接至标题">¶</a></h2>
|
||
<p>下列特性与 API 已从 Python 3.8 中移除:</p>
|
||
<ul class="simple">
|
||
<li><p>自 Python 3.3 起,从 <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> 导入 ABC 的做法已弃用,而应当从 <a class="reference internal" href="../library/collections.abc.html#module-collections.abc" title="collections.abc: Abstract base classes for containers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code></a> 执行导入。 从 collections 导入的操作在 3.8 中已标记为被移除,但将推迟到 3.9 中实施。 (参见 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36952">bpo-36952</a>。)</p></li>
|
||
<li><p><code class="xref py py-mod docutils literal notranslate"><span class="pre">macpath</span></code> 模块,在 Python 3.7 中弃用,现已被移除。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35471">bpo-35471</a> 中贡献。)</p></li>
|
||
<li><p>函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">platform.popen()</span></code> 已被移除,它自 Python 3.3 起就已被弃用:请改用 <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a>。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35345">bpo-35345</a> 中贡献。)</p></li>
|
||
<li><p>函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">time.clock()</span></code> 已被移除,它自 Python 3.3 起就已被弃用:请根据具体需求改用 <a class="reference internal" href="../library/time.html#time.perf_counter" title="time.perf_counter"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.perf_counter()</span></code></a> 或 <a class="reference internal" href="../library/time.html#time.process_time" title="time.process_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.process_time()</span></code></a> 以获得具有良好定义的行为。 (由 Matthias Bussonnier 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36895">bpo-36895</a> 中贡献。)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> 脚本已被移除,推荐改用 <code class="docutils literal notranslate"><span class="pre">python3.8</span> <span class="pre">-m</span> <span class="pre">venv</span></code> 来帮助消除容易混淆 <code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> 脚本所关联的 Python 解释器这一问题。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=25427">bpo-25427</a> 中贡献。)</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">parse_qs</span></code>, <code class="docutils literal notranslate"><span class="pre">parse_qsl</span></code> 和 <code class="docutils literal notranslate"><span class="pre">escape</span></code> 已从 <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cgi</span></code></a> 模块中被移除。 这些函数自 Python 3.2 或更早就已被弃用。 它们应改为从 <code class="docutils literal notranslate"><span class="pre">urllib.parse</span></code> 和 <code class="docutils literal notranslate"><span class="pre">html</span></code> 模块导入。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">filemode</span></code> 函数已从 <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> 模块中被移除。 该函数未被写入文档,自 Python 3.3 起就已弃用。</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> 构造器不再接受 <em>html</em> 参数。 它从来没有任何作用并在 Python 3.4 中已被弃用。 所有其他形参现在都是 <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">仅限关键字参数</span></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29209">bpo-29209</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">doctype()</span></code> 方法已被移除。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29209">bpo-29209</a> 中贡献。)</p></li>
|
||
<li><p>"unicode_internal" 编解码器已被移除。 (由 Inada Naoki 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36297">bpo-36297</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> 模块的 <code class="docutils literal notranslate"><span class="pre">Cache</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Statement</span></code> 对象已不再公开给用户。 (由 Aviv Palivoda 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=30262">bpo-30262</a> 中贡献。)</p></li>
|
||
<li><p><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> 和 <a class="reference internal" href="../library/fileinput.html#fileinput.FileInput" title="fileinput.FileInput"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileinput.FileInput()</span></code></a> 中自 Python 3.6 起就已被忽略并弃用的 <code class="docutils literal notranslate"><span class="pre">bufsize</span></code> 关键字参数已被移除。 由 Matthias Bussonnier 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36952">bpo-36952</a> 中贡献。)</p></li>
|
||
<li><p>在 Python 3.7 中弃用的函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.set_coroutine_wrapper()</span></code> 和 <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.get_coroutine_wrapper()</span></code> 已被移除。 (由 Matthias Bussonnier 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36933">bpo-36933</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="porting-to-python-3-8">
|
||
<h2>移植到 Python 3.8<a class="headerlink" href="#porting-to-python-3-8" title="永久链接至标题">¶</a></h2>
|
||
<p>本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.</p>
|
||
<section id="changes-in-python-behavior">
|
||
<h3>Python 行为的改变<a class="headerlink" href="#changes-in-python-behavior" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>yield 表达式(包括 <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="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code> 子句中的可迭代对象表达式除外)。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=10544">bpo-10544</a> 中贡献。)</p></li>
|
||
<li><p>当标识号检测 (<code class="docutils literal notranslate"><span class="pre">is</span></code> 和 <code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code>) 与特定类型的字面值 (例如字符串和数字) 一同使用时编译器现在会产生 <a class="reference internal" href="../library/exceptions.html#SyntaxWarning" title="SyntaxWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxWarning</span></code></a>。 这在 CPython 中通常是可行的,但并不被语言定义所保证。 该警告会建议用户改用相等性检测 (<code class="docutils literal notranslate"><span class="pre">==</span></code> and <code class="docutils literal notranslate"><span class="pre">!=</span></code>)。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34850">bpo-34850</a> 中贡献。)</p></li>
|
||
<li><p>CPython 解释器在某些情况下可以忽略异常。 在 Python 3.8 中这种情况会更少发生。 特别地,从类型字典获取属性时引发的异常不会再被忽略。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35459">bpo-35459</a> 中贡献。)</p></li>
|
||
<li><p>从内置类型 <a class="reference internal" href="../library/functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a>, <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>, <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>, <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> 和标准库的一些类中移除了 <code class="docutils literal notranslate"><span class="pre">__str__</span></code> 实现。 它们现在会从 <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> 继承 <code class="docutils literal notranslate"><span class="pre">__str__()</span></code>。 作为结果,在这些类的子类中定义 <code class="docutils literal notranslate"><span class="pre">__repr__()</span></code> 方法将会影响它们的字符串表示。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36793">bpo-36793</a> 中贡献。)</p></li>
|
||
<li><p>在 AIX 上,<a class="reference internal" href="../library/sys.html#sys.platform" title="sys.platform"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.platform</span></code></a> 将不再包含主要版本号。 它将总是 <code class="docutils literal notranslate"><span class="pre">'aix'</span></code> 而不再是 <code class="docutils literal notranslate"><span class="pre">'aix3'</span></code> .. <code class="docutils literal notranslate"><span class="pre">'aix7'</span></code>。 由于较旧的 Python 版本会包含该版本号,因此推荐总是使用 <code class="docutils literal notranslate"><span class="pre">sys.platform.startswith('aix')</span></code>。 (由 M. Felt 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36588">bpo-36588</a> 中贡献。)</p></li>
|
||
<li><p>现在 <a class="reference internal" href="../c-api/init.html#c.PyEval_AcquireLock" title="PyEval_AcquireLock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_AcquireLock()</span></code></a> 和 <a class="reference internal" href="../c-api/init.html#c.PyEval_AcquireThread" title="PyEval_AcquireThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_AcquireThread()</span></code></a> 如果当解释器终结化时被调用将会终结当前线程,以使它们与 <a class="reference internal" href="../c-api/init.html#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a>, <a class="reference internal" href="../c-api/init.html#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS()</span></code></a> 以及 <a class="reference internal" href="../c-api/init.html#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> 保持一致。 如果不想要这样的行为,请通过检测 <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_IsFinalizing()</span></code> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">sys.is_finalizing()</span></code> 来保护该调用。 (由 Joannah Nanjekye 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36475">bpo-36475</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changes-in-the-python-api">
|
||
<h3>Python API 的变化<a class="headerlink" href="#changes-in-the-python-api" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>在 Windows 上 <a class="reference internal" href="../library/os.html#os.getcwdb" title="os.getcwdb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getcwdb()</span></code></a> 函数现在会使用 UTF-8 编码格式而不是 ANSI 代码页:请参看 <span class="target" id="index-72"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0529"><strong>PEP 529</strong></a> 了解具体原因。 该函数在 Windows 上不再被弃用。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37412">bpo-37412</a> 中贡献。)</p></li>
|
||
<li><p>现在 <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> 在某些情况下会使用 <a class="reference internal" href="../library/os.html#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.posix_spawn()</span></code></a> 以获得更好的性能。 在适用于 Linux 的 Windows 子系统和 QEMU 用户模拟器上,使用 <a class="reference internal" href="../library/os.html#os.posix_spawn" title="os.posix_spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.posix_spawn()</span></code></a> 的 <code class="xref py py-class docutils literal notranslate"><span class="pre">Popen</span></code> 构造器不会再因为“找不到程序”这样的错误引发异常。 而是让子进程失败并返回一个非零的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">returncode</span></code>。 (由 Joannah Nanjekye 和 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35537">bpo-35537</a> 中贡献。)</p></li>
|
||
<li><p>The <em>preexec_fn</em> argument of * <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a> is no longer
|
||
compatible with subinterpreters. The use of the parameter in a
|
||
subinterpreter now raises <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.
|
||
(Contributed by Eric Snow in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34651">bpo-34651</a>, modified by Christian Heimes
|
||
in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37951">bpo-37951</a>.)</p></li>
|
||
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">imap.IMAP4.logout()</span></code> 方法不会再静默地忽略任意异常。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36348">bpo-36348</a> 中贡献。)</p></li>
|
||
<li><p>函数 <code class="xref py py-func docutils literal notranslate"><span class="pre">platform.popen()</span></code> 已被移除,它自 Python 3.3 起就已被弃用:请改用 <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a>。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35345">bpo-35345</a> 中贡献。)</p></li>
|
||
<li><p>当传入多模数据时 <a class="reference internal" href="../library/statistics.html#statistics.mode" title="statistics.mode"><code class="xref py py-func docutils literal notranslate"><span class="pre">statistics.mode()</span></code></a> 函数不会再引发异常。 它将改为返回在输入数据中遇到的第一个模式。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35892">bpo-35892</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/tkinter.ttk.html#tkinter.ttk.Treeview" title="tkinter.ttk.Treeview"><code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.ttk.Treeview</span></code></a> 类的 <a class="reference internal" href="../library/tkinter.ttk.html#tkinter.ttk.Treeview.selection" title="tkinter.ttk.Treeview.selection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">selection()</span></code></a> 方法不再接受参数。 带参数调用该方法来改变选择在 Python 3.6 中已弃用。 请使用专门方法例如 <a class="reference internal" href="../library/tkinter.ttk.html#tkinter.ttk.Treeview.selection_set" title="tkinter.ttk.Treeview.selection_set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">selection_set()</span></code></a> 来改变选择。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=31508">bpo-31508</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code></a> 的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">writexml()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">toxml()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">toprettyxml()</span></code> 方法以及 <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree</span></code> 的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> 方法现在会保留用户指定的属性顺序。 (由 Diego Rojas 和 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=34160">bpo-34160</a> 中贡献。)</p></li>
|
||
<li><p>附带 <code class="docutils literal notranslate"><span class="pre">'r'</span></code> 旗标打开的 <a class="reference internal" href="../library/dbm.html#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a> 数据库现在将是只读的。 如果数据库不存在,附带 <code class="docutils literal notranslate"><span class="pre">'r'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'w'</span></code> 旗标的 <a class="reference internal" href="../library/dbm.html#dbm.dumb.open" title="dbm.dumb.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">dbm.dumb.open()</span></code></a> 不会再创建数据库。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32749">bpo-32749</a> 中贡献。)</p></li>
|
||
<li><p>在 <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> 的子类中定义的 <code class="docutils literal notranslate"><span class="pre">doctype()</span></code> 方法将不会再被调用,并将导致发出 <a class="reference internal" href="../library/exceptions.html#RuntimeWarning" title="RuntimeWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeWarning</span></code></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/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doctype()</span></code></a> 方法来处理 XML doctype 声明。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29209">bpo-29209</a> 中贡献。)</p></li>
|
||
<li><p>现在当自定义元类未在传给 <code class="docutils literal notranslate"><span class="pre">type.__new__</span></code> 的命名空间中提供 <code class="docutils literal notranslate"><span class="pre">__classcell__</span></code> 入口时将引发 <a class="reference internal" href="../library/exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>。 在 Python 3.6--3.7 中是则是引发 <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>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=23722">bpo-23722</a> 中贡献。)</p></li>
|
||
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">cProfile.Profile</span></code> 类现在可被用作上下文管理器。 (由 Scott Sanderson 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=29235">bpo-29235</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfile()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copy" title="shutil.copy"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a>, <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 和 <a class="reference internal" href="../library/shutil.html#shutil.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.move()</span></code></a> 会使用平台专属的 "fast-copy" 系统调用(参见 <a class="reference internal" href="../library/shutil.html#shutil-platform-dependent-efficient-copy-operations"><span class="std std-ref">依赖于具体平台的高效拷贝操作</span></a> 一节)。</p></li>
|
||
<li><p><a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copyfile()</span></code></a> 在 Windows 上的默认缓冲区大小从 16 KiB 改为 1 MiB。</p></li>
|
||
<li><p><code class="docutils literal notranslate"><span class="pre">PyGC_Head</span></code> 结构已被完全改变。 所有接触到该结构的代码都应当被重写。 (参见 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33597">bpo-33597</a>。)</p></li>
|
||
<li><p><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> 结构已被移入 "internal" 头文件(特别是 Include/internal/pycore_pystate.h)。 不透明的 <code class="docutils literal notranslate"><span class="pre">PyInterpreterState</span></code> 作为仅有 API(以及稳定版 ABI)的一部分仍然可用。 文档指明该结构的任何字段都不是公有的,因此我们希望没人在使用它们。 但是,如果你确实依赖其中某一个或更多个私有字段并且没有其他替代选项,则请开一个 BPO 问题。 我们将尽力帮助你进行调整(可能包括向公有 API 添加访问器函数)。 (参见 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35886">bpo-35886</a>。)</p></li>
|
||
<li><p>现在所有平台下的 <a class="reference internal" href="../library/mmap.html#mmap.mmap.flush" title="mmap.mmap.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">mmap.flush()</span></code></a> 方法都会在成功时返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 并在错误时引发异常。 之前它的行为取决于具体平台: Windows 下会在成功时返回非零值;在失败时返回零。 Unix 下会在成功时返回零;在失败时引发错误。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=2122">bpo-2122</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/xml.dom.minidom.html#module-xml.dom.minidom" title="xml.dom.minidom: Minimal Document Object Model (DOM) implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.dom.minidom</span></code></a> 和 <a class="reference internal" href="../library/xml.sax.html#module-xml.sax" title="xml.sax: Package containing SAX2 base classes and convenience functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.sax</span></code></a> 模块默认将不再处理外部实体。 (由 Christian Heimes 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=17239">bpo-17239</a> 中贡献。)</p></li>
|
||
<li><p>从只读的 <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a> 数据库 (<a class="reference internal" href="../library/dbm.html#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.dumb</span></code></a>, <a class="reference internal" href="../library/dbm.html#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.gnu</span></code></a> 或 <a class="reference internal" href="../library/dbm.html#module-dbm.ndbm" title="dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm.ndbm</span></code></a>) 删除键将会引发 <code class="xref py py-attr docutils literal notranslate"><span class="pre">error</span></code> (<a class="reference internal" href="../library/dbm.html#dbm.dumb.error" title="dbm.dumb.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">dbm.dumb.error</span></code></a>, <a class="reference internal" href="../library/dbm.html#dbm.gnu.error" title="dbm.gnu.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">dbm.gnu.error</span></code></a> 或 <a class="reference internal" href="../library/dbm.html#dbm.ndbm.error" title="dbm.ndbm.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">dbm.ndbm.error</span></code></a>) 而不是 <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>。 (由 Xiang Zhang 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33106">bpo-33106</a> 中贡献。)</p></li>
|
||
<li><p>简化了字面值的 AST。 所有常量将被表示为 <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Constant</span></code> 的实例。 实例化旧类 <code class="docutils literal notranslate"><span class="pre">Num</span></code>, <code class="docutils literal notranslate"><span class="pre">Str</span></code>, <code class="docutils literal notranslate"><span class="pre">Bytes</span></code>, <code class="docutils literal notranslate"><span class="pre">NameConstant</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Ellipsis</span></code> 都将返回 <code class="docutils literal notranslate"><span class="pre">Constant</span></code> 的实例。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32892">bpo-32892</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">expanduser()</span></code></a> 在 Windows 上现在改用 <span class="target" id="index-73"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">USERPROFILE</span></code> 环境变量而不再使用 <span class="target" id="index-74"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOME</span></code>,后者通常不会为一般用户账户设置。 (由 Anthony Sottile 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36264">bpo-36264</a> 中贡献。)</p></li>
|
||
<li><p>异常 <a class="reference internal" href="../library/asyncio-exceptions.html#asyncio.CancelledError" title="asyncio.CancelledError"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.CancelledError</span></code></a> 现在继承自 <a class="reference internal" href="../library/exceptions.html#BaseException" title="BaseException"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a> 而不是 <a class="reference internal" href="../library/exceptions.html#Exception" title="Exception"><code class="xref py py-class docutils literal notranslate"><span class="pre">Exception</span></code></a> 并且不再继承自 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.CancelledError" title="concurrent.futures.CancelledError"><code class="xref py py-class docutils literal notranslate"><span class="pre">concurrent.futures.CancelledError</span></code></a>。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32528">bpo-32528</a> 中贡献。)</p></li>
|
||
<li><p>当使用 <a class="reference internal" href="../library/asyncio-task.html#asyncio.Task" title="asyncio.Task"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Task</span></code></a> 的实例时,函数 <a class="reference internal" href="../library/asyncio-task.html#asyncio.wait_for" title="asyncio.wait_for"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.wait_for()</span></code></a> 现在会正确地等待撤销。 在此之前当达到 <em>timeout</em> 时,它会被撤销并立即返回。 (由 Elvis Pranskevichus 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32751">bpo-32751</a> 中贡献。)</p></li>
|
||
<li><p>当将 'socket' 作为 <em>name</em> 形参传入时,函数 <a class="reference internal" href="../library/asyncio-protocol.html#asyncio.BaseTransport.get_extra_info" title="asyncio.BaseTransport.get_extra_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.BaseTransport.get_extra_info()</span></code></a> 现在会返回一个可安全使用的套接字对象。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37027">bpo-37027</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/asyncio-protocol.html#asyncio.BufferedProtocol" title="asyncio.BufferedProtocol"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.BufferedProtocol</span></code></a> 已经晋级为稳定 API。</p></li>
|
||
</ul>
|
||
<ul class="simple" id="bpo-36085-whatsnew">
|
||
<li><p>在 Windows 上对扩展模块的 DLL 依赖以及通过 <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> 加载的 DLL 的解析现在将更为安全。 只有系统路径、包含相信 DLL 或 PYD 文件的路径以及通过 <a class="reference internal" href="../library/os.html#os.add_dll_directory" title="os.add_dll_directory"><code class="xref py py-func docutils literal notranslate"><span class="pre">add_dll_directory()</span></code></a> 添加的目录才会被作为加载时依赖的搜索位置。 特别地,<span class="target" id="index-75"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code> 和当前工作目录将不再被使用,对它们的修改将不再对正常的 DLL 解析产生影响。 如果你的应用依赖于这些机制,你应当先检查 <a class="reference internal" href="../library/os.html#os.add_dll_directory" title="os.add_dll_directory"><code class="xref py py-func docutils literal notranslate"><span class="pre">add_dll_directory()</span></code></a>,如果它存在就用它在加载你的库时添加你的 DLL 目录。 请注意 Windows 7 用户还需要确保 Windows 更新包 KB2533623 已安装(这一点也会由安装器进行验证)。 (由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36085">bpo-36085</a> 中贡献。)</p></li>
|
||
<li><p>关联到 pgen 的头文件和函数在其被纯 Python 实现取代后已被移除。 (由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36623">bpo-36623</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/types.html#types.CodeType" title="types.CodeType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.CodeType</span></code></a> 在构造器的第二个位置新增了一个形参 (<em>posonlyargcount</em>) 以支持在 <span class="target" id="index-76"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0570"><strong>PEP 570</strong></a> 中定义的仅限位置参数。 第一个参数 (<em>argcount</em>) 现在表示位置参数的总数量 (包括仅限位置参数)。 <a class="reference internal" href="../library/types.html#types.CodeType" title="types.CodeType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.CodeType</span></code></a> 中新增的 <code class="docutils literal notranslate"><span class="pre">replace()</span></code> 方法可用于让代码支持 future 特性。</p></li>
|
||
<li><p><a class="reference internal" href="../library/hmac.html#hmac.new" title="hmac.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">hmac.new()</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">digestmod</span></code> 形参不再默认使用 MD5 摘要。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="changes-in-the-c-api">
|
||
<h3>C API 的变化<a class="headerlink" href="#changes-in-the-c-api" title="永久链接至标题">¶</a></h3>
|
||
<ul>
|
||
<li><p><a class="reference internal" href="../c-api/veryhigh.html#c.PyCompilerFlags" title="PyCompilerFlags"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCompilerFlags</span></code></a> 结构体添加了一个新的 <em>cf_feature_version</em> 字段。 它应当被初始化为 <code class="docutils literal notranslate"><span class="pre">PY_MINOR_VERSION</span></code>。 该字段默认会被忽略,当且仅当 <code class="docutils literal notranslate"><span class="pre">PyCF_ONLY_AST</span></code> 在 <em>cf_flags</em> 中设置旗标时才会被使用。 (由 Guido van Rossum 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35766">bpo-35766</a> 中贡献。)</p></li>
|
||
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ReInitThreads()</span></code> 函数已从 C API 中移除。 它不应当被显式地调用;请改用 <a class="reference internal" href="../c-api/sys.html#c.PyOS_AfterFork_Child" title="PyOS_AfterFork_Child"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_AfterFork_Child()</span></code></a>。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36728">bpo-36728</a> 中贡献。)</p></li>
|
||
<li><p>在 Unix 上,C 扩展不会再被链接到 libpython,但 Android 和 Cygwin 例外。 当 Python 被嵌入时,<code class="docutils literal notranslate"><span class="pre">libpython</span></code> 不可使用 <code class="docutils literal notranslate"><span class="pre">RTLD_LOCAL</span></code> 加载,而要改用 <code class="docutils literal notranslate"><span class="pre">RTLD_GLOBAL</span></code>。 之前使用 <code class="docutils literal notranslate"><span class="pre">RTLD_LOCAL</span></code> 已经不可能加载未链接到 <code class="docutils literal notranslate"><span class="pre">libpython</span></code> 的 C 扩展了,例如通过 <code class="docutils literal notranslate"><span class="pre">Modules/Setup</span></code> 的 <code class="docutils literal notranslate"><span class="pre">*shared*</span></code> 部分构建的标准库 C 扩展。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=21536">bpo-21536</a> 中贡献。)</p></li>
|
||
<li><p>在解析或构建值时(例如 <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>, <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>, <a class="reference internal" href="../c-api/object.html#c.PyObject_CallFunction" title="PyObject_CallFunction"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallFunction()</span></code></a> 等等)使用形如 <code class="docutils literal notranslate"><span class="pre">#</span></code> 的格式而不定义 <code class="docutils literal notranslate"><span class="pre">PY_SSIZE_T_CLEAN</span></code> 现在将会引发 <code class="docutils literal notranslate"><span class="pre">DeprecationWarning</span></code>。 它将在 3.10 或 4.0 中被移除。 请参阅 <a class="reference internal" href="../c-api/arg.html#arg-parsing"><span class="std std-ref">解析参数并构建值变量</span></a> 了解详情。 (由 Inada Naoki 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36381">bpo-36381</a> 中贡献。)</p></li>
|
||
<li><p>堆分配类型的实例(例如使用 <a class="reference internal" href="../c-api/type.html#c.PyType_FromSpec" title="PyType_FromSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpec()</span></code></a> 创建的实例)会保存一个对其类型对象的引用。 提升这些类型对象引用计数的操作已从 <a class="reference internal" href="../c-api/type.html#c.PyType_GenericAlloc" title="PyType_GenericAlloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_GenericAlloc()</span></code></a> 移至更低层级的函数 <a class="reference internal" href="../c-api/allocation.html#c.PyObject_Init" title="PyObject_Init"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Init()</span></code></a> 和 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_INIT()</span></code>。 这使用通过This makes types created through <a class="reference internal" href="../c-api/type.html#c.PyType_FromSpec" title="PyType_FromSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpec()</span></code></a> 所创建类型的行为与管理代码中的其他类保持一致。</p>
|
||
<p>静态分配类型将不受影响。</p>
|
||
<p>在大部分情况下,这应该都不会有附带影响。 但是,在分配实例后手动提升引用计数的类型(也许是为了绕过漏洞)现在可能永远不会被销毁。 要避免这种情况,这些类需要在实例撤销分配期间在类型对象上调用 Py_DECREF。</p>
|
||
<p>要正确地将这些类型移植到 3.8,请应用以下修改:</p>
|
||
<ul>
|
||
<li><p>在分配实例之后在类型对象上移除 <a class="reference internal" href="../c-api/refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_INCREF</span></code></a> —— 如果有的话。 这可以发生在调用 <a class="reference internal" href="../c-api/allocation.html#c.PyObject_New" title="PyObject_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_New()</span></code></a>, <a class="reference internal" href="../c-api/allocation.html#c.PyObject_NewVar" title="PyObject_NewVar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_NewVar()</span></code></a>, <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_New()</span></code></a>, <a class="reference internal" href="../c-api/gcsupport.html#c.PyObject_GC_NewVar" title="PyObject_GC_NewVar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_NewVar()</span></code></a> 或任何其他使用 <a class="reference internal" href="../c-api/allocation.html#c.PyObject_Init" title="PyObject_Init"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Init()</span></code></a> 或 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_INIT()</span></code> 的自定义分配器之后。</p>
|
||
<p>示例:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">foo_struct</span><span class="w"> </span><span class="o">*</span>
|
||
<span class="nf">foo_new</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||
<span class="w"> </span><span class="n">foo_struct</span><span class="w"> </span><span class="o">*</span><span class="n">foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyObject_GC_New</span><span class="p">(</span><span class="n">foo_struct</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">PyTypeObject</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">type</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
|
||
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
|
||
<span class="cp">#if PY_VERSION_HEX < 0x03080000</span>
|
||
<span class="w"> </span><span class="c1">// Workaround for Python issue 35810; no longer necessary in Python 3.8</span>
|
||
<span class="w"> </span><span class="n">PY_INCREF</span><span class="p">(</span><span class="n">type</span><span class="p">)</span>
|
||
<span class="cp">#endif</span>
|
||
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">foo</span><span class="p">;</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
<li><p>确保所有堆分配类型的自定义 <code class="docutils literal notranslate"><span class="pre">tp_dealloc</span></code> 函数会减少类型的引用计数。</p>
|
||
<p>示例:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">void</span>
|
||
<span class="nf">foo_dealloc</span><span class="p">(</span><span class="n">foo_struct</span><span class="w"> </span><span class="o">*</span><span class="n">instance</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
||
<span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">type</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_TYPE</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span>
|
||
<span class="w"> </span><span class="n">PyObject_GC_Del</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span>
|
||
<span class="cp">#if PY_VERSION_HEX >= 0x03080000</span>
|
||
<span class="w"> </span><span class="c1">// This was not needed before Python 3.8 (Python issue 35810)</span>
|
||
<span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">type</span><span class="p">);</span>
|
||
<span class="cp">#endif</span>
|
||
<span class="p">}</span>
|
||
</pre></div>
|
||
</div>
|
||
</li>
|
||
</ul>
|
||
<p>(由 Eddie Elizondo 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35810">bpo-35810</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEPRECATED()</span></code> 宏已经针对 MSVC 实现。 这个宏现在必须放在符号名称之前。</p>
|
||
<p>示例:</p>
|
||
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_DEPRECATED</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span><span class="w"> </span><span class="n">PyAPI_FUNC</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="n">Py_OldFunction</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>(由 Zackery Spytz 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33407">bpo-33407</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>解释器将不再假装支持跨发布版本的扩展类型二进制兼容性。 由第三方扩展模块所导出的 <a class="reference internal" href="../c-api/type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> 应该具有当前 Python 版本所要求的所有空位,包括 <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_finalize" title="PyTypeObject.tp_finalize"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_finalize</span></code></a> (<a class="reference internal" href="../c-api/typeobj.html#Py_TPFLAGS_HAVE_FINALIZE" title="Py_TPFLAGS_HAVE_FINALIZE"><code class="xref py py-const docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_FINALIZE</span></code></a> 不会再在读取 <a class="reference internal" href="../c-api/typeobj.html#c.PyTypeObject.tp_finalize" title="PyTypeObject.tp_finalize"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_finalize</span></code></a> 之前被检查)。</p>
|
||
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=32388">bpo-32388</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>函数 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyNode_AddChild()</span></code> 和 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyParser_AddToken()</span></code> 现在接受两个额外的 <code class="docutils literal notranslate"><span class="pre">int</span></code> 参数 <em>end_lineno</em> 和 <em>end_col_offset</em>。</p></li>
|
||
<li><p>允许 MinGW 工具直接链接到 <code class="file docutils literal notranslate"><span class="pre">python38.dll</span></code> 的 <code class="file docutils literal notranslate"><span class="pre">libpython38.a</span></code> 文件已不再包含于标准的 Windows 分发包中。 如果你需要此文件,可使用 <code class="docutils literal notranslate"><span class="pre">gendef</span></code> 和 <code class="docutils literal notranslate"><span class="pre">dlltool</span></code> 工具来生成它,这些工具是 MinGW binutils 包的一部分:</p>
|
||
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>gendef<span class="w"> </span>-<span class="w"> </span>python38.dll<span class="w"> </span>><span class="w"> </span>tmp.def
|
||
dlltool<span class="w"> </span>--dllname<span class="w"> </span>python38.dll<span class="w"> </span>--def<span class="w"> </span>tmp.def<span class="w"> </span>--output-lib<span class="w"> </span>libpython38.a
|
||
</pre></div>
|
||
</div>
|
||
<p>已安装的 <code class="file docutils literal notranslate"><span class="pre">pythonXY.dll</span></code> 所在位置将取决于安装选项以及 Windows 的版本和语言。 请参阅 <a class="reference internal" href="../using/windows.html#using-on-windows"><span class="std std-ref">在Windows上使用 Python</span></a> 了解更多信息。 该结果库应当放在与 <code class="file docutils literal notranslate"><span class="pre">pythonXY.lib</span></code> 相同的目录下,这通常是你的 Python 安装路径下的 <code class="file docutils literal notranslate"><span class="pre">libs</span></code> 目录。</p>
|
||
<p>(由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37351">bpo-37351</a> 中贡献。)</p>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
<section id="cpython-bytecode-changes">
|
||
<h3>CPython 字节码的改变<a class="headerlink" href="#cpython-bytecode-changes" title="永久链接至标题">¶</a></h3>
|
||
<ul>
|
||
<li><p>解释器循环已通过将块堆栈展开逻辑移入编译器获得了简化。 编译器现在会发出显式指令来调整值堆栈并为 <a class="reference internal" href="../reference/simple_stmts.html#break"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">break</span></code></a>, <a class="reference internal" href="../reference/simple_stmts.html#continue"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">continue</span></code></a> 和 <a class="reference internal" href="../reference/simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> 调用清除代码。</p>
|
||
<p>移除了操作码 <code class="xref std std-opcode docutils literal notranslate"><span class="pre">BREAK_LOOP</span></code>, <code class="xref std std-opcode docutils literal notranslate"><span class="pre">CONTINUE_LOOP</span></code>, <code class="xref std std-opcode docutils literal notranslate"><span class="pre">SETUP_LOOP</span></code> 和 <code class="xref std std-opcode docutils literal notranslate"><span class="pre">SETUP_EXCEPT</span></code>。 添加了新的操作码 <a class="reference internal" href="../library/dis.html#opcode-ROT_FOUR"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">ROT_FOUR</span></code></a>, <a class="reference internal" href="../library/dis.html#opcode-BEGIN_FINALLY"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">BEGIN_FINALLY</span></code></a>, <a class="reference internal" href="../library/dis.html#opcode-CALL_FINALLY"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">CALL_FINALLY</span></code></a> 和 <a class="reference internal" href="../library/dis.html#opcode-POP_FINALLY"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">POP_FINALLY</span></code></a>。 修改了 <a class="reference internal" href="../library/dis.html#opcode-END_FINALLY"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">END_FINALLY</span></code></a> 和 <a class="reference internal" href="../library/dis.html#opcode-WITH_CLEANUP_START"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">WITH_CLEANUP_START</span></code></a> 的行为。</p>
|
||
<p>(由 Mark Shannon, Antoine Pitrou 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=17611">bpo-17611</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p>添加了新的操作码 <a class="reference internal" href="../library/dis.html#opcode-END_ASYNC_FOR"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">END_ASYNC_FOR</span></code></a> 用于处理当等待 <a class="reference internal" href="../reference/compound_stmts.html#async-for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code></a> 循环的下一项时引发的异常。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=33041">bpo-33041</a> 中贡献。)</p></li>
|
||
<li><p><a class="reference internal" href="../library/dis.html#opcode-MAP_ADD"><code class="xref std std-opcode docutils literal notranslate"><span class="pre">MAP_ADD</span></code></a> 现在会预期值为栈的第一个元素而键为第二个元素。 作出此改变以使得字典推导式能如 <span class="target" id="index-77"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0572"><strong>PEP 572</strong></a> 所提议的那样,键总是会在值之前被求值。 (由 Jörn Heissler 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35224">bpo-35224</a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="demos-and-tools">
|
||
<h3>演示和工具<a class="headerlink" href="#demos-and-tools" title="永久链接至标题">¶</a></h3>
|
||
<p>添加了一个检测脚本用于对访问变量的不同方式进行计时: <code class="docutils literal notranslate"><span class="pre">Tools/scripts/var_access_benchmark.py</span></code>。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=35884">bpo-35884</a> 中贡献。)</p>
|
||
<p>以下是自 Python 3.3 以来性能提升情况的总结:</p>
|
||
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Python version 3.3 3.4 3.5 3.6 3.7 3.8
|
||
-------------- --- --- --- --- --- ---
|
||
|
||
Variable and attribute read access:
|
||
read_local 4.0 7.1 7.1 5.4 5.1 3.9
|
||
read_nonlocal 5.3 7.1 8.1 5.8 5.4 4.4
|
||
read_global 13.3 15.5 19.0 14.3 13.6 7.6
|
||
read_builtin 20.0 21.1 21.6 18.5 19.0 7.5
|
||
read_classvar_from_class 20.5 25.6 26.5 20.7 19.5 18.4
|
||
read_classvar_from_instance 18.5 22.8 23.5 18.8 17.1 16.4
|
||
read_instancevar 26.8 32.4 33.1 28.0 26.3 25.4
|
||
read_instancevar_slots 23.7 27.8 31.3 20.8 20.8 20.2
|
||
read_namedtuple 68.5 73.8 57.5 45.0 46.8 18.4
|
||
read_boundmethod 29.8 37.6 37.9 29.6 26.9 27.7
|
||
|
||
Variable and attribute write access:
|
||
write_local 4.6 8.7 9.3 5.5 5.3 4.3
|
||
write_nonlocal 7.3 10.5 11.1 5.6 5.5 4.7
|
||
write_global 15.9 19.7 21.2 18.0 18.0 15.8
|
||
write_classvar 81.9 92.9 96.0 104.6 102.1 39.2
|
||
write_instancevar 36.4 44.6 45.8 40.0 38.9 35.5
|
||
write_instancevar_slots 28.7 35.6 36.1 27.3 26.6 25.7
|
||
|
||
Data structure read access:
|
||
read_list 19.2 24.2 24.5 20.8 20.8 19.0
|
||
read_deque 19.9 24.7 25.5 20.2 20.6 19.8
|
||
read_dict 19.7 24.3 25.7 22.3 23.0 21.0
|
||
read_strdict 17.9 22.6 24.3 19.5 21.2 18.9
|
||
|
||
Data structure write access:
|
||
write_list 21.2 27.1 28.5 22.5 21.6 20.0
|
||
write_deque 23.8 28.7 30.1 22.7 21.8 23.5
|
||
write_dict 25.9 31.4 33.3 29.3 29.2 24.7
|
||
write_strdict 22.9 28.4 29.9 27.5 25.2 23.1
|
||
|
||
Stack (or queue) operations:
|
||
list_append_pop 144.2 93.4 112.7 75.4 74.2 50.8
|
||
deque_append_pop 30.4 43.5 57.0 49.4 49.2 42.5
|
||
deque_append_popleft 30.8 43.7 57.3 49.7 49.7 42.8
|
||
|
||
Timing loop:
|
||
loop_overhead 0.3 0.5 0.6 0.4 0.3 0.3
|
||
</pre></div>
|
||
</div>
|
||
<p>基准测试是在 <a class="reference external" href="https://ark.intel.com/content/www/us/en/ark/products/76088/intel-core-i7-4960hq-processor-6m-cache-up-to-3-80-ghz.html">Intel® Core™ i7-4960HQ 处理器</a> 上运行从 <a class="reference external" href="https://www.python.org/downloads/mac-osx/">python.org</a> 获取的 macOS 64 位版得到的数据。 基准测试脚本显示时间以纳秒为单位。</p>
|
||
</section>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-1">
|
||
<h2>Python 3.8.1 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-1" title="永久链接至标题">¶</a></h2>
|
||
<p>出于重要的安全性考量,<a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_datagram_endpoint" title="asyncio.loop.create_datagram_endpoint"><code class="xref py py-meth docutils literal notranslate"><span class="pre">asyncio.loop.create_datagram_endpoint()</span></code></a> 的 <em>reuse_address</em> 形参不再被支持。 这是由 UDP 中的套接字选项 <code class="docutils literal notranslate"><span class="pre">SO_REUSEADDR</span></code> 的行为导致的。 更多细节请参阅 <code class="docutils literal notranslate"><span class="pre">loop.create_datagram_endpoint()</span></code> 的文档。 (由 Kyle Stanley, Antoine Pitrou 和 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=37228">bpo-37228</a> 中贡献。。)</p>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-2">
|
||
<h2>Python 3.8.2 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-2" title="永久链接至标题">¶</a></h2>
|
||
<p>修复了 <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> 的 <code class="docutils literal notranslate"><span class="pre">ignore</span></code> 回调中的回归问题。 现在该参数的类型已改回 str 和 List[str]。 (由 Manuel Barkhau 和 Giampaolo Rodola 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39390">bpo-39390</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-3">
|
||
<h2>Python 3.8.3 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-3" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="../library/__future__.html#module-__future__" title="__future__: Future statement definitions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">__future__</span></code></a> 模块中的 future 旗标常量值已获得更新以防止与 compiler 旗标相冲突。 在之前版本中 <code class="docutils literal notranslate"><span class="pre">PyCF_ALLOW_TOP_LEVEL_AWAIT</span></code> 与 <code class="docutils literal notranslate"><span class="pre">CO_FUTURE_DIVISION</span></code> 存在冲突。 (由 Batuhan Taskaya 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=39562">bpo-39562</a> 中贡献)</p>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-8">
|
||
<h2>Python 3.8.8 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-8" title="永久链接至标题">¶</a></h2>
|
||
<p>早先的 Python 版本允许使用 <code class="docutils literal notranslate"><span class="pre">;</span></code> 和 <code class="docutils literal notranslate"><span class="pre">&</span></code> 作为 <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> 和 <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> 中 query 形参的分隔键。 出于安全考虑,也为了遵循更新的 W3C 推荐设置,这已被改为只允许单个分隔键,默认为 <code class="docutils literal notranslate"><span class="pre">&</span></code>。 这一改变还会影响 <a class="reference internal" href="../library/cgi.html#cgi.parse" title="cgi.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">cgi.parse()</span></code></a> 和 <a class="reference internal" href="../library/cgi.html#cgi.parse_multipart" title="cgi.parse_multipart"><code class="xref py py-func docutils literal notranslate"><span class="pre">cgi.parse_multipart()</span></code></a> 因为它们在内部使用了受影响的函数。 要了解更多细节,请查看它们各自的文档。 (由 Adam Goldschmidt, Senthil Kumaran 和 Ken Jin 在 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=42967">bpo-42967</a> 中贡献。)</p>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-9">
|
||
<h2>Python 3.8.9 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-9" title="永久链接至标题">¶</a></h2>
|
||
<p>新的安全修正将A security fix alters the <a class="reference internal" href="../library/ftplib.html#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">ftplib.FTP</span></code></a> 的行为改成当设置被动数据通道时不信任远程服务器所发送的 IPv4 地址。 我们会改为重用 FTP 服务器的 IP 地址。 对于需要原先行为的不常见代码,请在你的 FTP 实例上设置 <code class="docutils literal notranslate"><span class="pre">trust_server_pasv_ipv4_address</span></code> 属性为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。 (参见 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43285">bpo-43285</a>)</p>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-10">
|
||
<h2>Python 3.8.10 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-10" title="永久链接至标题">¶</a></h2>
|
||
<section id="macos-11-0-big-sur-and-apple-silicon-mac-support">
|
||
<h3>macOS 11.0 (Big Sur) 与 Apple Silicon Mac 支持<a class="headerlink" href="#macos-11-0-big-sur-and-apple-silicon-mac-support" title="永久链接至标题">¶</a></h3>
|
||
<p>对于 3.8.10,Python 现在支持在 macOS 11 (Big Sur) 和 Apple Silicon Mac (基于 <code class="docutils literal notranslate"><span class="pre">ARM64</span></code> 架构) 上构建和运行。 现在提供了一个新的通用构建形式 <code class="docutils literal notranslate"><span class="pre">universal2</span></code> 能够在一组可执行文件上同时原生支持 <code class="docutils literal notranslate"><span class="pre">ARM64</span></code> 和 <code class="docutils literal notranslate"><span class="pre">Intel</span> <span class="pre">64</span></code>。 请注意对 "弱链接" 的支持,即以较新 macOS 版本为目标构建的二进制文件通过在运行时测试缺失特性的方式也能在较旧版本上正确运行,尚未被包括在本次对 Python 3.9 的反向移植中;要支持更多的 macOS 版本,请继续以设定版本区间的最旧版本作为目标进行构建。</p>
|
||
<p>(Originally contributed by Ronald Oussoren and Lawrence D'Anna in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=41100">bpo-41100</a>,
|
||
with fixes by FX Coudert and Eli Rykoff, and backported to 3.8 by Maxime Bélanger
|
||
and Ned Deily)</p>
|
||
</section>
|
||
</section>
|
||
<section id="id1">
|
||
<h2>Python 3.8.10 中的重要变化<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
|
||
<section id="urllib-parse">
|
||
<h3>urllib.parse<a class="headerlink" href="#urllib-parse" title="永久链接至标题">¶</a></h3>
|
||
<p>在 URL 中存在换行符或制表符,可能会导致某些形式的攻击。根据 WHATWG 的规范更新了:rfc:<cite>3986</cite>, <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> 中的解析器将从 URL 中移除 ASCII 换行符 <code class="docutils literal notranslate"><span class="pre">\n</span></code> 、<code class="docutils literal notranslate"><span class="pre">\r</span></code> 和 <code class="docutils literal notranslate"><span class="pre">\t``字符,以防止这种攻击。将移除的字符由一个新的模块级变量``urllib.parse._UNSAFE_URL_BYTES_TO_REMOVE</span></code> 控制。(参阅 <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=43882">bpo-43882</a> )</p>
|
||
</section>
|
||
</section>
|
||
<section id="notable-changes-in-python-3-8-12">
|
||
<h2>Python 3.8.12 中的重要变化<a class="headerlink" href="#notable-changes-in-python-3-8-12" title="永久链接至标题">¶</a></h2>
|
||
<section id="id2">
|
||
<h3>Python API 的变化<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h3>
|
||
<p>从 Python 3.8.12 开始 <a class="reference internal" href="../library/ipaddress.html#module-ipaddress" title="ipaddress: IPv4/IPv6 manipulation library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ipaddress</span></code></a> 模块不再接受 IPv4 地址字符串中有任何前缀的零。 前缀的零有歧义且会被某些库解读为八进制数字。 例如旧版函数 <a class="reference internal" href="../library/socket.html#socket.inet_aton" title="socket.inet_aton"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket.inet_aton()</span></code></a> 就将前缀的零视为八进制数字。 最新 <a class="reference internal" href="../library/socket.html#socket.inet_pton" title="socket.inet_pton"><code class="xref py py-func docutils literal notranslate"><span class="pre">inet_pton()</span></code></a> 的 glibc 实现则不接受任何前缀的零。</p>
|
||
<p>(Originally contributed by Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?@action=redirect&bpo=36384">bpo-36384</a>, and backported
|
||
to 3.8 by Achraf Merzouki)</p>
|
||
</section>
|
||
</section>
|
||
<section id="notable-security-feature-in-3-8-14">
|
||
<h2>3.8.14 中的重要安全特性<a class="headerlink" href="#notable-security-feature-in-3-8-14" title="永久链接至标题">¶</a></h2>
|
||
<p>使用十进制以外的底,如 2(二进制)、4、8(八进制)、16(十六进制)、32 以外作为基数在 <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> 和 <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> 之间进行转换,如果字符串形式的数字数量超过一个限制,会抛出 <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>,以避免因算法复杂而导致的潜在拒绝服务攻击。这是对 <a class="reference external" href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2020-10735">CVE-2020-10735</a> 的缓解方案。这个限制可以通过环境变量、命令行旗标或 <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> API 进行配置或禁用。参见 <a class="reference internal" href="../library/stdtypes.html#int-max-str-digits"><span class="std std-ref">integer string conversion length limitation</span></a> 文档。默认限制是字符串形式的 4300 位数字。</p>
|
||
</section>
|
||
<section id="notable-changes-in-3-8-17">
|
||
<h2>Notable Changes in 3.8.17<a class="headerlink" href="#notable-changes-in-3-8-17" title="永久链接至标题">¶</a></h2>
|
||
<section id="id3">
|
||
<h3>tarfile<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p><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> 中的提取方法和 <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> 都新增了 <em>filter</em> 参数以允许限制可能令人意外或危险的 tar 特性,例如在目标目录之外创建文件。 相关细节参见 <a class="reference internal" href="../library/tarfile.html#tarfile-extraction-filter"><span class="std std-ref">解压缩过滤器</span></a>。 在 Python 3.12 中,不带 <em>filter</em> 参数的用法将显示 <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>。 在 Python 3.14 中,默认值将切换为 <code class="docutils literal notranslate"><span class="pre">'data'</span></code>。 (由 Petr Viktorin 在 <span class="target" id="index-78"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0706"><strong>PEP 706</strong></a> 中贡献。)</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
<section id="notable-changes-in-3-8-20">
|
||
<h2>Notable changes in 3.8.20<a class="headerlink" href="#notable-changes-in-3-8-20" title="永久链接至标题">¶</a></h2>
|
||
<section id="ipaddress">
|
||
<h3>ipaddress<a class="headerlink" href="#ipaddress" title="永久链接至标题">¶</a></h3>
|
||
<ul class="simple">
|
||
<li><p>修正了 <code class="docutils literal notranslate"><span class="pre">IPv4Address</span></code>, <code class="docutils literal notranslate"><span class="pre">IPv6Address</span></code>, <code class="docutils literal notranslate"><span class="pre">IPv4Network</span></code> 和 <code class="docutils literal notranslate"><span class="pre">IPv6Network</span></code> 中的 <code class="docutils literal notranslate"><span class="pre">is_global</span></code> 和 <code class="docutils literal notranslate"><span class="pre">is_private</span></code> 行为。</p></li>
|
||
</ul>
|
||
</section>
|
||
<section id="email">
|
||
<h3>email<a class="headerlink" href="#email" title="永久链接至标题">¶</a></h3>
|
||
<ul>
|
||
<li><p>带有嵌入的换行符的标头现在输出时会加引号。</p>
|
||
<p>现在 <a class="reference internal" href="../library/email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">generator</span></code></a> 会拒绝序列化(写入)不正确地折叠或分隔的标头,例如将被解析为多个标头或与相邻数据合并的标头等。 如果你需要禁用此安全特性,请设置 <a class="reference internal" href="../library/email.policy.html#email.policy.Policy.verify_generated_headers" title="email.policy.Policy.verify_generated_headers"><code class="xref py py-attr docutils literal notranslate"><span class="pre">verify_generated_headers</span></code></a>。 (由 Bas Bloemsaat 和 Petr Viktorin 在 <a class="reference external" href="https://github.com/python/cpython/issues/121650">gh-121650</a> 中贡献。)</p>
|
||
</li>
|
||
<li><p><a class="reference internal" href="../library/email.utils.html#email.utils.getaddresses" title="email.utils.getaddresses"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.getaddresses()</span></code></a> and <a class="reference internal" href="../library/email.utils.html#email.utils.parseaddr" title="email.utils.parseaddr"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.parseaddr()</span></code></a> now return
|
||
<code class="docutils literal notranslate"><span class="pre">('',</span> <span class="pre">'')</span></code> 2-tuples in more situations where invalid email addresses are
|
||
encountered, instead of potentially inaccurate values.
|
||
An optional <em>strict</em> parameter was added to these two functions:
|
||
use <code class="docutils literal notranslate"><span class="pre">strict=False</span></code> to get the old behavior, accepting malformed inputs.
|
||
<code class="docutils literal notranslate"><span class="pre">getattr(email.utils,</span> <span class="pre">'supports_strict_parsing',</span> <span class="pre">False)</span></code> can be used to
|
||
check if the <em>strict</em> paramater is available.
|
||
(Contributed by Thomas Dwyer and Victor Stinner for <a class="reference external" href="https://github.com/python/cpython/issues/102988">gh-102988</a> to improve
|
||
the CVE-2023-27043 fix.)</p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#">Python 3.8 有什么新变化</a><ul>
|
||
<li><a class="reference internal" href="#summary-release-highlights">摘要 -- 发布重点</a></li>
|
||
<li><a class="reference internal" href="#new-features">新的特性</a><ul>
|
||
<li><a class="reference internal" href="#assignment-expressions">赋值表达式</a></li>
|
||
<li><a class="reference internal" href="#positional-only-parameters">仅限位置形参</a></li>
|
||
<li><a class="reference internal" href="#parallel-filesystem-cache-for-compiled-bytecode-files">用于已编译字节码文件的并行文件系统缓存</a></li>
|
||
<li><a class="reference internal" href="#debug-build-uses-the-same-abi-as-release-build">调试构建使用与发布构建相同的 ABI</a></li>
|
||
<li><a class="reference internal" href="#f-strings-support-for-self-documenting-expressions-and-debugging">f-字符串支持 <code class="docutils literal notranslate"><span class="pre">=</span></code> 用于自动记录表达式和调试文档</a></li>
|
||
<li><a class="reference internal" href="#pep-578-python-runtime-audit-hooks">PEP 578: Python 运行时审核钩子</a></li>
|
||
<li><a class="reference internal" href="#pep-587-python-initialization-configuration">PEP 587: Python 初始化配置</a></li>
|
||
<li><a class="reference internal" href="#vectorcall-a-fast-calling-protocol-for-cpython">Vectorcall: 用于 CPython 的快速调用协议</a></li>
|
||
<li><a class="reference internal" href="#pickle-protocol-5-with-out-of-band-data-buffers">具有外部数据缓冲区的 pickle 协议 5</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
|
||
<li><a class="reference internal" href="#new-modules">新增模块</a></li>
|
||
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
|
||
<li><a class="reference internal" href="#ast">ast</a></li>
|
||
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
|
||
<li><a class="reference internal" href="#builtins">builtins</a></li>
|
||
<li><a class="reference internal" href="#collections">collections</a></li>
|
||
<li><a class="reference internal" href="#cprofile">cProfile</a></li>
|
||
<li><a class="reference internal" href="#csv">csv</a></li>
|
||
<li><a class="reference internal" href="#curses">curses</a></li>
|
||
<li><a class="reference internal" href="#ctypes">ctypes</a></li>
|
||
<li><a class="reference internal" href="#datetime">datetime</a></li>
|
||
<li><a class="reference internal" href="#functools">functools</a></li>
|
||
<li><a class="reference internal" href="#gc">gc</a></li>
|
||
<li><a class="reference internal" href="#gettext">gettext</a></li>
|
||
<li><a class="reference internal" href="#gzip">gzip</a></li>
|
||
<li><a class="reference internal" href="#idle-and-idlelib">IDLE 与 idlelib</a></li>
|
||
<li><a class="reference internal" href="#inspect">inspect</a></li>
|
||
<li><a class="reference internal" href="#io">io</a></li>
|
||
<li><a class="reference internal" href="#itertools">itertools</a></li>
|
||
<li><a class="reference internal" href="#json-tool">json.tool</a></li>
|
||
<li><a class="reference internal" href="#logging">logging</a></li>
|
||
<li><a class="reference internal" href="#math">math</a></li>
|
||
<li><a class="reference internal" href="#mmap">mmap</a></li>
|
||
<li><a class="reference internal" href="#multiprocessing">multiprocessing</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="#pickle">pickle</a></li>
|
||
<li><a class="reference internal" href="#plistlib">plistlib</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="#shlex">shlex</a></li>
|
||
<li><a class="reference internal" href="#shutil">shutil</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="#statistics">statistics</a></li>
|
||
<li><a class="reference internal" href="#sys">sys</a></li>
|
||
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
|
||
<li><a class="reference internal" href="#tempfile">tempfile</a></li>
|
||
<li><a class="reference internal" href="#threading">threading</a></li>
|
||
<li><a class="reference internal" href="#tokenize">tokenize</a></li>
|
||
<li><a class="reference internal" href="#tkinter">tkinter</a></li>
|
||
<li><a class="reference internal" href="#time">time</a></li>
|
||
<li><a class="reference internal" href="#typing">typing</a></li>
|
||
<li><a class="reference internal" href="#unicodedata">unicodedata</a></li>
|
||
<li><a class="reference internal" href="#unittest">unittest</a></li>
|
||
<li><a class="reference internal" href="#venv">venv</a></li>
|
||
<li><a class="reference internal" href="#weakref">weakref</a></li>
|
||
<li><a class="reference internal" href="#xml">xml</a></li>
|
||
<li><a class="reference internal" href="#xmlrpc">xmlrpc</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
|
||
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
|
||
<li><a class="reference internal" href="#deprecated">弃用</a></li>
|
||
<li><a class="reference internal" href="#api-and-feature-removals">API 与特性的移除</a></li>
|
||
<li><a class="reference internal" href="#porting-to-python-3-8">移植到 Python 3.8</a><ul>
|
||
<li><a class="reference internal" href="#changes-in-python-behavior">Python 行为的改变</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-python-api">Python API 的变化</a></li>
|
||
<li><a class="reference internal" href="#changes-in-the-c-api">C API 的变化</a></li>
|
||
<li><a class="reference internal" href="#cpython-bytecode-changes">CPython 字节码的改变</a></li>
|
||
<li><a class="reference internal" href="#demos-and-tools">演示和工具</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-1">Python 3.8.1 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-2">Python 3.8.2 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-3">Python 3.8.3 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-8">Python 3.8.8 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-9">Python 3.8.9 中的重要变化</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-10">Python 3.8.10 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#macos-11-0-big-sur-and-apple-silicon-mac-support">macOS 11.0 (Big Sur) 与 Apple Silicon Mac 支持</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#id1">Python 3.8.10 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-python-3-8-12">Python 3.8.12 中的重要变化</a><ul>
|
||
<li><a class="reference internal" href="#id2">Python API 的变化</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-security-feature-in-3-8-14">3.8.14 中的重要安全特性</a></li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-8-17">Notable Changes in 3.8.17</a><ul>
|
||
<li><a class="reference internal" href="#id3">tarfile</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#notable-changes-in-3-8-20">Notable changes in 3.8.20</a><ul>
|
||
<li><a class="reference internal" href="#ipaddress">ipaddress</a></li>
|
||
<li><a class="reference internal" href="#email">email</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="index.html"
|
||
title="上一章">Python的新变化</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="3.7.html"
|
||
title="下一章">Python 3.7 有什么新变化</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/whatsnew/3.8.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="3.7.html" title="Python 3.7 有什么新变化"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="index.html" title="Python的新变化"
|
||
>上一页</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.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python的新变化</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, 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 />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |