Files
hustoj/web/python-docs/whatsnew/3.5.html
2025-03-09 17:10:56 +08:00

1839 lines
319 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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.5 有什么新变化 &#8212; 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.4 有什么新变化" href="3.4.html" />
<link rel="prev" title="Python 3.6 有什么新变化" href="3.6.html" />
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.5.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.5 有什么新变化</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="#pep-492-coroutines-with-async-and-await-syntax">PEP 492 - 使用 async 和 await 语法实现协程</a></li>
<li><a class="reference internal" href="#pep-465-a-dedicated-infix-operator-for-matrix-multiplication">PEP 465 - 用于矩阵乘法的专用中缀运算符</a></li>
<li><a class="reference internal" href="#pep-448-additional-unpacking-generalizations">PEP 448 - 进一步的解包标准化</a></li>
<li><a class="reference internal" href="#pep-461-percent-formatting-support-for-bytes-and-bytearray">PEP 461 - 针对 bytes 和 bytearray 的百分号格式化支持</a></li>
<li><a class="reference internal" href="#pep-484-type-hints">PEP 484 —— 类型提示</a></li>
<li><a class="reference internal" href="#pep-471-os-scandir-function-a-better-and-faster-directory-iterator">PEP 471 - os.scandir() 函数 -- 一个更好且更快的目录迭代器</a></li>
<li><a class="reference internal" href="#pep-475-retry-system-calls-failing-with-eintr">PEP 475: 重试返回 EINTR 失败码的系统调用</a></li>
<li><a class="reference internal" href="#pep-479-change-stopiteration-handling-inside-generators">PEP 479更改生成器内部的 StopIteration 处理</a></li>
<li><a class="reference internal" href="#pep-485-a-function-for-testing-approximate-equality">PEP 485用于测试近似相等的函数</a></li>
<li><a class="reference internal" href="#pep-486-make-the-python-launcher-aware-of-virtual-environments">PEP 486让 Python 启动器识别虚拟环境</a></li>
<li><a class="reference internal" href="#pep-488-elimination-of-pyo-files">PEP 488去除 PYO 文件</a></li>
<li><a class="reference internal" href="#pep-489-multi-phase-extension-module-initialization">PEP 489多阶段扩展模块初始化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
<li><a class="reference internal" href="#new-modules">新增模块</a><ul>
<li><a class="reference internal" href="#typing">typing</a></li>
<li><a class="reference internal" href="#zipapp">zipapp</a></li>
</ul>
</li>
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
<li><a class="reference internal" href="#argparse">argparse</a></li>
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
<li><a class="reference internal" href="#bz2">bz2</a></li>
<li><a class="reference internal" href="#cgi">cgi</a></li>
<li><a class="reference internal" href="#cmath">cmath</a></li>
<li><a class="reference internal" href="#code">code</a></li>
<li><a class="reference internal" href="#collections">collections</a></li>
<li><a class="reference internal" href="#collections-abc">collections.abc</a></li>
<li><a class="reference internal" href="#compileall">compileall</a></li>
<li><a class="reference internal" href="#concurrent-futures">concurrent.futures</a></li>
<li><a class="reference internal" href="#configparser">configparser</a></li>
<li><a class="reference internal" href="#contextlib">contextlib</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="#dbm">dbm</a></li>
<li><a class="reference internal" href="#difflib">difflib</a></li>
<li><a class="reference internal" href="#distutils">distutils</a></li>
<li><a class="reference internal" href="#doctest">doctest</a></li>
<li><a class="reference internal" href="#email">email</a></li>
<li><a class="reference internal" href="#enum">enum</a></li>
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
<li><a class="reference internal" href="#functools">functools</a></li>
<li><a class="reference internal" href="#glob">glob</a></li>
<li><a class="reference internal" href="#gzip">gzip</a></li>
<li><a class="reference internal" href="#heapq">heapq</a></li>
<li><a class="reference internal" href="#http">http</a></li>
<li><a class="reference internal" href="#http-client">http.client</a></li>
<li><a class="reference internal" href="#idlelib-and-idle">idlelib 与 IDLE</a></li>
<li><a class="reference internal" href="#imaplib">imaplib</a></li>
<li><a class="reference internal" href="#imghdr">imghdr</a></li>
<li><a class="reference internal" href="#importlib">importlib</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="#ipaddress">ipaddress</a></li>
<li><a class="reference internal" href="#json">json</a></li>
<li><a class="reference internal" href="#linecache">linecache</a></li>
<li><a class="reference internal" href="#locale">locale</a></li>
<li><a class="reference internal" href="#logging">logging</a></li>
<li><a class="reference internal" href="#lzma">lzma</a></li>
<li><a class="reference internal" href="#math">math</a></li>
<li><a class="reference internal" href="#multiprocessing">multiprocessing</a></li>
<li><a class="reference internal" href="#operator">operator</a></li>
<li><a class="reference internal" href="#os">os</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="#poplib">poplib</a></li>
<li><a class="reference internal" href="#re">re</a></li>
<li><a class="reference internal" href="#readline">readline</a></li>
<li><a class="reference internal" href="#selectors">selectors</a></li>
<li><a class="reference internal" href="#shutil">shutil</a></li>
<li><a class="reference internal" href="#signal">signal</a></li>
<li><a class="reference internal" href="#smtpd">smtpd</a></li>
<li><a class="reference internal" href="#smtplib">smtplib</a></li>
<li><a class="reference internal" href="#sndhdr">sndhdr</a></li>
<li><a class="reference internal" href="#socket">socket</a></li>
<li><a class="reference internal" href="#ssl">ssl</a><ul>
<li><a class="reference internal" href="#memory-bio-support">内存 BIO 支持</a></li>
<li><a class="reference internal" href="#application-layer-protocol-negotiation-support">应用层协议协商支持</a></li>
<li><a class="reference internal" href="#other-changes">其他改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
<li><a class="reference internal" href="#subprocess">subprocess</a></li>
<li><a class="reference internal" href="#sys">sys</a></li>
<li><a class="reference internal" href="#sysconfig">sysconfig</a></li>
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
<li><a class="reference internal" href="#threading">threading</a></li>
<li><a class="reference internal" href="#time">time</a></li>
<li><a class="reference internal" href="#timeit">timeit</a></li>
<li><a class="reference internal" href="#tkinter">tkinter</a></li>
<li><a class="reference internal" href="#traceback">回溯</a></li>
<li><a class="reference internal" href="#types">types</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="#unittest-mock">unittest.mock</a></li>
<li><a class="reference internal" href="#urllib">urllib</a></li>
<li><a class="reference internal" href="#wsgiref">wsgiref</a></li>
<li><a class="reference internal" href="#xmlrpc">xmlrpc</a></li>
<li><a class="reference internal" href="#xml-sax">xml.sax</a></li>
<li><a class="reference internal" href="#zipfile">zipfile</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-module-level-changes">其他模块级更改</a></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><ul>
<li><a class="reference internal" href="#new-keywords">新关键字</a></li>
<li><a class="reference internal" href="#deprecated-python-behavior">已弃用的 Python 行为</a></li>
<li><a class="reference internal" href="#unsupported-operating-systems">不支持的操作系统</a></li>
<li><a class="reference internal" href="#deprecated-python-modules-functions-and-methods">已弃用的 Python 模块、函数和方法</a></li>
</ul>
</li>
<li><a class="reference internal" href="#removed">移除</a><ul>
<li><a class="reference internal" href="#api-and-feature-removals">API 与特性的移除</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-3-5">移植到Python 3.5</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>
</ul>
</li>
<li><a class="reference internal" href="#notable-changes-in-python-3-5-4">Python 3.5.4 的显著变化</a><ul>
<li><a class="reference internal" href="#new-make-regen-all-build-target">新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标</a></li>
<li><a class="reference internal" href="#removal-of-make-touch-build-target">移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.6.html"
title="上一章">Python 3.6 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="3.4.html"
title="下一章">Python 3.4 有什么新变化</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.5.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.5.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.4.html" title="Python 3.4 有什么新变化"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="3.6.html" title="Python 3.6 有什么新变化"
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> &#187;</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> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python的新变化</a> &#187;</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-5">
<h1>Python 3.5 有什么新变化<a class="headerlink" href="#what-s-new-in-python-3-5" title="永久链接至标题"></a></h1>
<dl class="field-list simple">
<dt class="field-odd">编者</dt>
<dd class="field-odd"><p>Elvis Pranskevichus &lt;<a class="reference external" href="mailto:elvis&#37;&#52;&#48;magic&#46;io">elvis<span>&#64;</span>magic<span>&#46;</span>io</a>&gt;, Yury Selivanov &lt;<a class="reference external" href="mailto:yury&#37;&#52;&#48;magic&#46;io">yury<span>&#64;</span>magic<span>&#46;</span>io</a>&gt;</p>
</dd>
</dl>
<p>这篇文章介绍了 Python 3.5 相比 3.4 增加的新特性。 Python 3.5 发布于 2015 年 9 月 13 日。 更完整的变化清单请参阅 <a class="reference external" href="https://docs.python.org/3.5/whatsnew/changelog.html">changelog</a></p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-104"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0478"><strong>PEP 478</strong></a> - Python 3.5 发布计划</p>
</div>
<section id="summary-release-highlights">
<h2>摘要 -- 发布重点<a class="headerlink" href="#summary-release-highlights" title="永久链接至标题"></a></h2>
<p>新的语法特性:</p>
<ul class="simple">
<li><p><a class="reference internal" href="#whatsnew-pep-492"><span class="std std-ref">PEP 492</span></a>, 使用 async 和 await 语法实现协程。</p></li>
<li><p><a class="reference internal" href="#whatsnew-pep-465"><span class="std std-ref">PEP 465</span></a>, 新的矩阵乘法运算符: <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&#64;</span> <span class="pre">b</span></code>.</p></li>
<li><p><a class="reference internal" href="#whatsnew-pep-448"><span class="std std-ref">PEP 448</span></a>, 额外的解包通用化。</p></li>
</ul>
<p>新的库模块:</p>
<ul class="simple">
<li><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>: <a class="reference internal" href="#whatsnew-pep-484"><span class="std std-ref">PEP 484 —— 类型注解</span></a>.</p></li>
<li><p><a class="reference internal" href="../library/zipapp.html#module-zipapp" title="zipapp: Manage executable Python zip archives"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code></a>: <a class="reference internal" href="#whatsnew-zipapp"><span class="std std-ref">PEP 441 改进Python ZIP应用程序支持</span></a>.</p></li>
</ul>
<p>新的内置特性:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">bytes</span> <span class="pre">%</span> <span class="pre">args</span></code>, <code class="docutils literal notranslate"><span class="pre">bytearray</span> <span class="pre">%</span> <span class="pre">args</span></code>: <a class="reference internal" href="#whatsnew-pep-461"><span class="std std-ref">PEP 461</span></a> -- 为字节串和字节数组增加 <code class="docutils literal notranslate"><span class="pre">%</span></code> 格式化。</p></li>
<li><p>新增 <a class="reference internal" href="../library/stdtypes.html#bytes.hex" title="bytes.hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.hex()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#bytearray.hex" title="bytearray.hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytearray.hex()</span></code></a><a class="reference internal" href="../library/stdtypes.html#memoryview.hex" title="memoryview.hex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">memoryview.hex()</span></code></a> 方法。 (由 Arnon Yaari 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9951">bpo-9951</a> 中贡献。)</p></li>
<li><p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> 现在支持元组索引(包括多维度)。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23632">bpo-23632</a> 中贡献。)</p></li>
<li><p>生成器具有一个新的 <code class="docutils literal notranslate"><span class="pre">gi_yieldfrom</span></code> 属性,它将返回 <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> 表达式所迭代的对象。 (由 Benno Leslie 和 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24450">bpo-24450</a> 中贡献。)</p></li>
<li><p>现在当达到最大递归尝试时将引发新的 <a class="reference internal" href="../library/exceptions.html#RecursionError" title="RecursionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RecursionError</span></code></a> 异常。 (由 Georg Brandl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19235">bpo-19235</a> 中贡献。)</p></li>
</ul>
<p>CPython 实现的改进:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">LC_TYPE</span></code> 语言区域为 POSIX 语言区域(即 <code class="docutils literal notranslate"><span class="pre">C</span></code> 语言区域)时,<a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a><a class="reference internal" href="../library/sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> 现在将使用 <code class="docutils literal notranslate"><span class="pre">surrogateescape</span></code> 错误处理器,而不是 <code class="docutils literal notranslate"><span class="pre">strict</span></code> 错误处理器。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19977">bpo-19977</a> 中贡献。)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件已不再被使用而是被替换为一个更灵活的方案即在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 名称中显式地包括优化级别。 (参见 <a class="reference internal" href="#whatsnew-pep-488"><span class="std std-ref">PEP 488 概览</span></a>。)</p></li>
<li><p>内置与扩展模块现在将经过多阶段的过程被初始化,这类似于 Python 模块的加载方式。 (参见 <a class="reference internal" href="#whatsnew-pep-489"><span class="std std-ref">PEP 489 概览</span></a>。)</p></li>
</ul>
<p>标准库中的重大改进:</p>
<ul class="simple">
<li><p><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> 现在已 <a class="reference internal" href="#whatsnew-ordereddict"><span class="std std-ref">用 C 实现</span></a>,这使它的速度快了 4 到 100 部。</p></li>
<li><p>The <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> 模块获得了 <a class="reference internal" href="#whatsnew-sslmemorybio"><span class="std std-ref">对内存 BIO 的支持</span></a>,它使得 SSL 协议处理与网络 IO 实现了解耦。</p></li>
<li><p>新的 <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> 函数提供了对于目录遍历 <a class="reference internal" href="#whatsnew-pep-471"><span class="std std-ref">更好和明显更快速的方式</span></a></p></li>
<li><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> 已经大部分 <a class="reference internal" href="#whatsnew-lrucache"><span class="std std-ref">用 C 重新实现</span></a>,产生了更好的性能。</p></li>
<li><p>新的 <a class="reference internal" href="../library/subprocess.html#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.run()</span></code></a> 函数提供了一个 <a class="reference internal" href="#whatsnew-subprocess"><span class="std std-ref">运行子进程的简便方式</span></a></p></li>
<li><p><a class="reference internal" href="../library/traceback.html#module-traceback" title="traceback: Print or retrieve a stack traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">traceback</span></code></a> 模块已被显著 <a class="reference internal" href="#whatsnew-traceback"><span class="std std-ref">增强</span></a> 以改善性能和开发者便捷度。</p></li>
</ul>
<p>安全改进:</p>
<ul class="simple">
<li><p>SSLv3 目前在整个标准库中被禁用。 它仍然可以通过手动实例化一个 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 来启用。 (请参阅 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22638">bpo-22638</a> 了解详情;此修改已向下移植到 CPython 3.4 和 2.7。)</p></li>
<li><p>HTTP cookie 解析现在将更严格,以防止潜在的注入攻击。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22796">bpo-22796</a> 中贡献。)</p></li>
</ul>
<p>Windows改进</p>
<ul class="simple">
<li><p>使用新的 Windows 安装器替代了旧版 MSI。 请参阅 <a class="reference internal" href="../using/windows.html#using-on-windows"><span class="std std-ref">在Windows上使用 Python</span></a> 了解详情。</p></li>
<li><p>Windows 编译版现在使用 Microsoft Visual C++ 14.0,扩展模块也应当使用同一版本。</p></li>
</ul>
<p>请继续阅读有关针对用户的改变的完整清单包括许多其他较小的改进、CPython 优化、弃用以及潜在的移植问题。</p>
</section>
<section id="new-features">
<h2>新的特性<a class="headerlink" href="#new-features" title="永久链接至标题"></a></h2>
<section id="pep-492-coroutines-with-async-and-await-syntax">
<span id="whatsnew-pep-492"></span><h3>PEP 492 - 使用 async 和 await 语法实现协程<a class="headerlink" href="#pep-492-coroutines-with-async-and-await-syntax" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-105"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> 通过添加 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">可等待对象</span></a>, <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">协程函数</span></a>, <a class="reference internal" href="../glossary.html#term-asynchronous-iterable"><span class="xref std std-term">异步迭代</span></a><a class="reference internal" href="../glossary.html#term-asynchronous-context-manager"><span class="xref std std-term">异步上下文管理器</span></a> 极大地改善了 Python 对异步编程的支持。</p>
<p>协程函数是使用新的 <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> 语法来声明的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">async</span> <span class="k">def</span> <span class="nf">coro</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">return</span> <span class="s1">&#39;spam&#39;</span>
</pre></div>
</div>
<p>在协程函数内部,新的 <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> 表达式可用于挂起协程的执行直到其结果可用。 任何对象都可以被 <em>等待</em>,只要它通过定义 <a class="reference internal" href="../reference/datamodel.html#object.__await__" title="object.__await__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__await__()</span></code></a> 方法实现了 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></a> 协议。</p>
<p>PEP 492 还增加了 <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> 语句用于方便地迭代异步可迭代对象。</p>
<p>一个使用新语法编写的基本 HTTP 客户端示例:</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">http_get</span><span class="p">(</span><span class="n">domain</span><span class="p">):</span>
<span class="n">reader</span><span class="p">,</span> <span class="n">writer</span> <span class="o">=</span> <span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">open_connection</span><span class="p">(</span><span class="n">domain</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;</span><span class="se">\r\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span>
<span class="sa">b</span><span class="s1">&#39;GET / HTTP/1.1&#39;</span><span class="p">,</span>
<span class="sa">b</span><span class="s1">&#39;Host: %b&#39;</span> <span class="o">%</span> <span class="n">domain</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;latin-1&#39;</span><span class="p">),</span>
<span class="sa">b</span><span class="s1">&#39;Connection: close&#39;</span><span class="p">,</span>
<span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
<span class="p">]))</span>
<span class="k">async</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;&gt;&gt;&gt;&#39;</span><span class="p">,</span> <span class="n">line</span><span class="p">)</span>
<span class="n">writer</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_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">http_get</span><span class="p">(</span><span class="s1">&#39;example.com&#39;</span><span class="p">))</span>
<span class="k">finally</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>与异步迭代类似,增加了用于异步上下文管理器的新语法。 以下代码:</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">coro</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">lock</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;coro </span><span class="si">{}</span><span class="s1">: waiting for lock&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
<span class="k">async</span> <span class="k">with</span> <span class="n">lock</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;coro </span><span class="si">{}</span><span class="s1">: holding the lock&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
<span class="k">await</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;coro </span><span class="si">{}</span><span class="s1">: releasing the lock&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
<span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
<span class="n">lock</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="n">coros</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">gather</span><span class="p">(</span><span class="n">coro</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">lock</span><span class="p">),</span> <span class="n">coro</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">lock</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">coros</span><span class="p">)</span>
<span class="k">finally</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>将输出:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">coro</span> <span class="mi">2</span><span class="p">:</span> <span class="n">waiting</span> <span class="k">for</span> <span class="n">lock</span>
<span class="n">coro</span> <span class="mi">2</span><span class="p">:</span> <span class="n">holding</span> <span class="n">the</span> <span class="n">lock</span>
<span class="n">coro</span> <span class="mi">1</span><span class="p">:</span> <span class="n">waiting</span> <span class="k">for</span> <span class="n">lock</span>
<span class="n">coro</span> <span class="mi">2</span><span class="p">:</span> <span class="n">releasing</span> <span class="n">the</span> <span class="n">lock</span>
<span class="n">coro</span> <span class="mi">1</span><span class="p">:</span> <span class="n">holding</span> <span class="n">the</span> <span class="n">lock</span>
<span class="n">coro</span> <span class="mi">1</span><span class="p">:</span> <span class="n">releasing</span> <span class="n">the</span> <span class="n">lock</span>
</pre></div>
</div>
<p>请注意 <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><a class="reference internal" href="../reference/compound_stmts.html#async-with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code></a> 都只能在通过 <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> 声明的协程函数中使用。</p>
<p>协程函数应当在兼容的事件循环内部运行,例如 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio-event-loop"><span class="std std-ref">asyncio 循环</span></a></p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5.2 版更改: </span>从 CPython 3.5.2 开始,<code class="docutils literal notranslate"><span class="pre">__aiter__</span></code> 可以直接返回 <a class="reference internal" href="../glossary.html#term-asynchronous-iterator"><span class="xref std std-term">异步迭代器</span></a>。 返回 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">awaitable</span></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></p>
<p>详情参见文档的 <a class="reference internal" href="../reference/datamodel.html#async-iterators"><span class="std std-ref">异步迭代器</span></a> 一节。</p>
</div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-106"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> -- 使用 async 和 await 语法实现协程</dt><dd><p>PEP 由 Yury Selivanov 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-465-a-dedicated-infix-operator-for-matrix-multiplication">
<span id="whatsnew-pep-465"></span><h3>PEP 465 - 用于矩阵乘法的专用中缀运算符<a class="headerlink" href="#pep-465-a-dedicated-infix-operator-for-matrix-multiplication" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-107"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0465"><strong>PEP 465</strong></a> 增加了用于矩阵乘法的 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> 中缀运算符。 目前,还没有内置的 Python 类型实现这个新运算符,不过,它可通过定义 <a class="reference internal" href="../reference/datamodel.html#object.__matmul__" title="object.__matmul__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__matmul__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__rmatmul__" title="object.__rmatmul__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__rmatmul__()</span></code></a><a class="reference internal" href="../reference/datamodel.html#object.__imatmul__" title="object.__imatmul__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__imatmul__()</span></code></a> 分别用于常规、反射和原地矩阵乘法来实现。 这些方法的语义与定义其他中缀算术运算符的方法类似。</p>
<p>矩阵乘法在数学,科学,工程学的许多领域中是一种常见的操作,使用 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> 运算符可以编写更简洁的代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">S</span> <span class="o">=</span> <span class="p">(</span><span class="n">H</span> <span class="o">@</span> <span class="n">beta</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span><span class="o">.</span><span class="n">T</span> <span class="o">@</span> <span class="n">inv</span><span class="p">(</span><span class="n">H</span> <span class="o">@</span> <span class="n">V</span> <span class="o">@</span> <span class="n">H</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="o">@</span> <span class="p">(</span><span class="n">H</span> <span class="o">@</span> <span class="n">beta</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span>
</pre></div>
</div>
<p>代替:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">S</span> <span class="o">=</span> <span class="n">dot</span><span class="p">((</span><span class="n">dot</span><span class="p">(</span><span class="n">H</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span> <span class="o">-</span> <span class="n">r</span><span class="p">)</span><span class="o">.</span><span class="n">T</span><span class="p">,</span>
<span class="n">dot</span><span class="p">(</span><span class="n">inv</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">dot</span><span class="p">(</span><span class="n">H</span><span class="p">,</span> <span class="n">V</span><span class="p">),</span> <span class="n">H</span><span class="o">.</span><span class="n">T</span><span class="p">)),</span> <span class="n">dot</span><span class="p">(</span><span class="n">H</span><span class="p">,</span> <span class="n">beta</span><span class="p">)</span> <span class="o">-</span> <span class="n">r</span><span class="p">))</span>
</pre></div>
</div>
<p>NumPy 1.10 支持新的运算符:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">array([ 1., 1., 1.])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">eye</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
<span class="go">array([[ 1., 0., 0.],</span>
<span class="go"> [ 0., 1., 0.],</span>
<span class="go"> [ 0., 0., 1.]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">@</span> <span class="n">m</span>
<span class="go">array([ 1., 1., 1.])</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-108"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0465"><strong>PEP 465</strong></a> -- 用于矩阵乘法的专用中缀运算符</dt><dd><p>PEP 由 Nathaniel J. Smith 撰写,由 Benjamin Peterson 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-448-additional-unpacking-generalizations">
<span id="whatsnew-pep-448"></span><h3>PEP 448 - 进一步的解包标准化<a class="headerlink" href="#pep-448-additional-unpacking-generalizations" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-109"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0448"><strong>PEP 448</strong></a> 扩展了 <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="../reference/expressions.html#calls"><span class="std std-ref">函数调用</span></a> 中使用任意数量的解包操作:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">*</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="mi">3</span><span class="p">,</span> <span class="o">*</span><span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">1 2 3 4 5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">fn</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="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">c</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fn</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">},</span> <span class="o">**</span><span class="p">{</span><span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">})</span>
<span class="go">1 2 3 4</span>
</pre></div>
</div>
<p>类似地,元组、列表、集合与字典表示形式也允许多重解包 (参见 <a class="reference internal" href="../reference/expressions.html#exprlists"><span class="std std-ref">表达式列表</span></a><a class="reference internal" href="../reference/expressions.html#dict"><span class="std std-ref">字典显示</span></a>):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="o">*</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="mi">4</span>
<span class="go">(0, 1, 2, 3, 4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="o">*</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="mi">4</span><span class="p">]</span>
<span class="go">[0, 1, 2, 3, 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="o">*</span><span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="mi">4</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">)}</span>
<span class="go">{0, 1, 2, 3, 4, 5, 6, 7}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="s1">&#39;x&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="p">{</span><span class="s1">&#39;y&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}}</span>
<span class="go">{&#39;x&#39;: 1, &#39;y&#39;: 2}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-110"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0448"><strong>PEP 448</strong></a> -- 进一步的解包标准化</dt><dd><p>PEP 由 Joshua Landau 撰写 ,由 Neil GirdharThomas Wouters 和 Joshua Landau 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-461-percent-formatting-support-for-bytes-and-bytearray">
<span id="whatsnew-pep-461"></span><h3>PEP 461 - 针对 bytes 和 bytearray 的百分号格式化支持<a class="headerlink" href="#pep-461-percent-formatting-support-for-bytes-and-bytearray" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-111"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0461"><strong>PEP 461</strong></a> 增加了 <code class="docutils literal notranslate"><span class="pre">%</span></code> <a class="reference internal" href="../library/stdtypes.html#bytes-formatting"><span class="std std-ref">插值运算符</span></a><a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a><a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> 的支持。</p>
<p>虽然插值通常被认为是一种字符串操作,但在某些情况下针对 <code class="docutils literal notranslate"><span class="pre">bytes</span></code><code class="docutils literal notranslate"><span class="pre">bytearrays</span></code> 的插值操作也是有意义,而弥补这种功能缺失所需的工作可能会影响代码的整体可读性。 在处理通常会混合二进制和 ASCII 兼容文本的 wire 格式化协议时,这个问题尤为重要。</p>
<p>示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="sa">b</span><span class="s1">&#39;Hello %b!&#39;</span> <span class="o">%</span> <span class="sa">b</span><span class="s1">&#39;World&#39;</span>
<span class="go">b&#39;Hello World!&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="sa">b</span><span class="s1">&#39;x=</span><span class="si">%i</span><span class="s1"> y=</span><span class="si">%f</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">)</span>
<span class="go">b&#39;x=1 y=2.500000&#39;</span>
</pre></div>
</div>
<p>Unicode 对于 <code class="docutils literal notranslate"><span class="pre">%b</span></code> 来说是不允许的,但对 <code class="docutils literal notranslate"><span class="pre">%a</span></code> 来说则是可接受的 (等价于 <code class="docutils literal notranslate"><span class="pre">repr(obj).encode('ascii',</span> <span class="pre">'backslashreplace')</span></code>):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="sa">b</span><span class="s1">&#39;Hello %b!&#39;</span> <span class="o">%</span> <span class="s1">&#39;World&#39;</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">%b requires bytes, or an object that implements __bytes__, not &#39;str&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="sa">b</span><span class="s1">&#39;price: </span><span class="si">%a</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="s1">&#39;10€&#39;</span>
<span class="go">b&quot;price: &#39;10\\u20ac&#39;&quot;</span>
</pre></div>
</div>
<p>请注意 <code class="docutils literal notranslate"><span class="pre">%s</span></code><code class="docutils literal notranslate"><span class="pre">%r</span></code> 转换类型虽然受到支持,但应当只被用于需要与 Python 2 保持兼容性的代码中。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-112"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0461"><strong>PEP 461</strong></a> -- 为 bytes 和 bytearray 添加 % 格式化</dt><dd><p>PEP 由 Ethan Furman 撰写 ,由 Neil Schemenauer 和 Ethan Furman 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-484-type-hints">
<span id="whatsnew-pep-484"></span><h3>PEP 484 —— 类型提示<a class="headerlink" href="#pep-484-type-hints" title="永久链接至标题"></a></h3>
<p>函数标注语法自 3.0 版起即已成为 Python 的特性 (<span class="target" id="index-113"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3107"><strong>PEP 3107</strong></a>),标注的语义尚未得到定义。</p>
<p>经验表明大多数函数标注的使用都是为了向函数形参和返回值提供类型提示。 很显然如果标准库可以包括用于类型标注的基本定义和工具,对 Python 用户来说将大有裨益。</p>
<p><span class="target" id="index-114"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> 引入了一个 <a class="reference internal" href="../glossary.html#term-provisional-api"><span class="xref std std-term">暂定模块</span></a> 来提供这些标准定义和工具,以及针对标注不可用的场合的一些惯例。</p>
<p>例如,以下是一个在标注中声明了参数和返回值类型的简单函数:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">greeting</span><span class="p">(</span><span class="n">name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;Hello &#39;</span> <span class="o">+</span> <span class="n">name</span>
</pre></div>
</div>
<p>While these annotations are available at runtime through the usual
<code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code> attribute, <em>no automatic type checking happens at
runtime</em>. Instead, it is assumed that a separate off-line type checker
(e.g. <a class="reference external" href="http://mypy-lang.org">mypy</a>) will be used for on-demand
source code analysis.</p>
<p>类型系统支持合并、泛型类型以及名为 <a class="reference internal" href="../library/typing.html#typing.Any" title="typing.Any"><code class="xref py py-class docutils literal notranslate"><span class="pre">Any</span></code></a> 的能够适用于所有类型(即作为赋值的来源和目标)的特殊类型。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<ul class="simple">
<li><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></li>
<li><dl class="simple">
<dt><span class="target" id="index-115"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> —— 类型提示</dt><dd><p>PEP 由 Guido van RossumJukka Lehtosalo 和 Łukasz Langa 撰写;由 Guido van Rossum 实现。</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt><span class="target" id="index-116"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0483"><strong>PEP 483</strong></a> -- 类型提示理论</dt><dd><p>PEP 由 Yury Selivanov 撰写</p>
</dd>
</dl>
</li>
</ul>
</div>
</section>
<section id="pep-471-os-scandir-function-a-better-and-faster-directory-iterator">
<span id="whatsnew-pep-471"></span><h3>PEP 471 - os.scandir() 函数 -- 一个更好且更快的目录迭代器<a class="headerlink" href="#pep-471-os-scandir-function-a-better-and-faster-directory-iterator" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-117"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0471"><strong>PEP 471</strong></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>。 此外,<a class="reference internal" href="../library/os.html#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.walk()</span></code></a> 现在是使用 <code class="docutils literal notranslate"><span class="pre">scandir</span></code> 来实现,这使它在 POSIX 系统上可提速 3 至 5 倍而在 Windows 系统上可提速 7 至 20 倍。 这样的效果主要是通过大幅减少遍历目录树所需调用 <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> 的次数来达成的。</p>
<p>此外,<code class="docutils literal notranslate"><span class="pre">scandir</span></code> 是返回一个迭代器,而不是返回一个文件名列表,这提升了迭代非常大的目录时的内存效率。</p>
<p>下面的例子演示了 <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> 的简单用法,显示给定 <em>path</em> 中所有不以 <code class="docutils literal notranslate"><span class="pre">'.'</span></code> 开头的文件(不包括目录)。 <a class="reference internal" href="../library/os.html#os.DirEntry.is_file" title="os.DirEntry.is_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">entry.is_file()</span></code></a> 调用通常不会执行额外的系统调用:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">scandir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">entry</span><span class="o">.</span><span class="n">is_file</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-118"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0471"><strong>PEP 471</strong></a> -- os.scandir() 函数 -- 一个更好且更快的目录迭代器</dt><dd><p>PEP 由 Ben Hoyt 在 Victor Stinner 的帮助下撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-475-retry-system-calls-failing-with-eintr">
<span id="whatsnew-pep-475"></span><h3>PEP 475: 重试返回 EINTR 失败码的系统调用<a class="headerlink" href="#pep-475-retry-system-calls-failing-with-eintr" title="永久链接至标题"></a></h3>
<p>An <a class="reference internal" href="../library/errno.html#errno.EINTR" title="errno.EINTR"><code class="xref py py-data docutils literal notranslate"><span class="pre">errno.EINTR</span></code></a> error code is returned whenever a system call, that
is waiting for I/O, is interrupted by a signal. Previously, Python would
raise <a class="reference internal" href="../library/exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> in such cases. This meant that, when writing a
Python application, the developer had two choices:</p>
<ol class="arabic simple">
<li><p>忽略 <code class="docutils literal notranslate"><span class="pre">InterruptedError</span></code></p></li>
<li><p>处理 <code class="docutils literal notranslate"><span class="pre">InterruptedError</span></code> 并在每个调用位置尝试重新启动被中断的系统调用。</p></li>
</ol>
<p>第一个选项会使应用程序中途出错。 第二个选项添加了大量的额外处理使得代码几乎不可读。 比较:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Hello World&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>和:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Hello World&quot;</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">except</span> <span class="ne">InterruptedError</span><span class="p">:</span>
<span class="k">continue</span>
</pre></div>
</div>
<p><span class="target" id="index-119"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0475"><strong>PEP 475</strong></a> 实现了在 <code class="docutils literal notranslate"><span class="pre">EINTR</span></code> 时自动重试系统调用。 这移除了大部分场合下在用户代码中处理 <code class="docutils literal notranslate"><span class="pre">EINTR</span></code><a class="reference internal" href="../library/exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a> 的负担,并使得 Python 程序,包括标准库的程序更为健壮。 请注意只有在信号处理器未引发异常时系统调用才会被重试。</p>
<p>以下是现在当被信号中断时会被重试的函数的列表:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a><a class="reference internal" href="../library/io.html#io.open" title="io.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">io.open()</span></code></a>;</p></li>
<li><p><a class="reference internal" href="../library/faulthandler.html#module-faulthandler" title="faulthandler: Dump the Python traceback."><code class="xref py py-mod docutils literal notranslate"><span class="pre">faulthandler</span></code></a> 模块的功能</p></li>
<li><p><a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> 函数: <a class="reference internal" href="../library/os.html#os.fchdir" title="os.fchdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">fchdir()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fchmod" title="os.fchmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">fchmod()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fchown" title="os.fchown"><code class="xref py py-func docutils literal notranslate"><span class="pre">fchown()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fdatasync" title="os.fdatasync"><code class="xref py py-func docutils literal notranslate"><span class="pre">fdatasync()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fstat" title="os.fstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">fstat()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fstatvfs" title="os.fstatvfs"><code class="xref py py-func docutils literal notranslate"><span class="pre">fstatvfs()</span></code></a>, <a class="reference internal" href="../library/os.html#os.fsync" title="os.fsync"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsync()</span></code></a>, <a class="reference internal" href="../library/os.html#os.ftruncate" title="os.ftruncate"><code class="xref py py-func docutils literal notranslate"><span class="pre">ftruncate()</span></code></a>, <a class="reference internal" href="../library/os.html#os.mkfifo" title="os.mkfifo"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkfifo()</span></code></a>, <a class="reference internal" href="../library/os.html#os.mknod" title="os.mknod"><code class="xref py py-func docutils literal notranslate"><span class="pre">mknod()</span></code></a>, <a class="reference internal" href="../library/os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="../library/os.html#os.posix_fadvise" title="os.posix_fadvise"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_fadvise()</span></code></a>, <a class="reference internal" href="../library/os.html#os.posix_fallocate" title="os.posix_fallocate"><code class="xref py py-func docutils literal notranslate"><span class="pre">posix_fallocate()</span></code></a>, <a class="reference internal" href="../library/os.html#os.pread" title="os.pread"><code class="xref py py-func docutils literal notranslate"><span class="pre">pread()</span></code></a>, <a class="reference internal" href="../library/os.html#os.pwrite" title="os.pwrite"><code class="xref py py-func docutils literal notranslate"><span class="pre">pwrite()</span></code></a>, <a class="reference internal" href="../library/os.html#os.read" title="os.read"><code class="xref py py-func docutils literal notranslate"><span class="pre">read()</span></code></a>, <a class="reference internal" href="../library/os.html#os.readv" title="os.readv"><code class="xref py py-func docutils literal notranslate"><span class="pre">readv()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendfile()</span></code></a>, <a class="reference internal" href="../library/os.html#os.wait3" title="os.wait3"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait3()</span></code></a>, <a class="reference internal" href="../library/os.html#os.wait4" title="os.wait4"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait4()</span></code></a>, <a class="reference internal" href="../library/os.html#os.wait" title="os.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">wait()</span></code></a>, <a class="reference internal" href="../library/os.html#os.waitid" title="os.waitid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitid()</span></code></a>, <a class="reference internal" href="../library/os.html#os.waitpid" title="os.waitpid"><code class="xref py py-func docutils literal notranslate"><span class="pre">waitpid()</span></code></a>, <a class="reference internal" href="../library/os.html#os.write" title="os.write"><code class="xref py py-func docutils literal notranslate"><span class="pre">write()</span></code></a>, <a class="reference internal" href="../library/os.html#os.writev" title="os.writev"><code class="xref py py-func docutils literal notranslate"><span class="pre">writev()</span></code></a>;</p></li>
<li><p>特例: <a class="reference internal" href="../library/os.html#os.close" title="os.close"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.close()</span></code></a><a class="reference internal" href="../library/os.html#os.dup2" title="os.dup2"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.dup2()</span></code></a> 现在会忽略 <a class="reference internal" href="../library/errno.html#errno.EINTR" title="errno.EINTR"><code class="xref py py-data docutils literal notranslate"><span class="pre">EINTR</span></code></a> 错误; 不重试系统调用请参阅PEP了解基本原理</p></li>
<li><p><a class="reference internal" href="../library/select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> 函数: <a class="reference internal" href="../library/select.html#select.devpoll.poll" title="select.devpoll.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">devpoll.poll()</span></code></a>, <a class="reference internal" href="../library/select.html#select.epoll.poll" title="select.epoll.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">epoll.poll()</span></code></a>, <a class="reference internal" href="../library/select.html#select.kqueue.control" title="select.kqueue.control"><code class="xref py py-func docutils literal notranslate"><span class="pre">kqueue.control()</span></code></a>, <a class="reference internal" href="../library/select.html#select.poll.poll" title="select.poll.poll"><code class="xref py py-func docutils literal notranslate"><span class="pre">poll.poll()</span></code></a>, <a class="reference internal" href="../library/select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select()</span></code></a>;</p></li>
<li><p><a class="reference internal" href="../library/socket.html#socket.socket" title="socket.socket"><code class="xref py py-class docutils literal notranslate"><span class="pre">socket</span></code></a> 类的方法: <a class="reference internal" href="../library/socket.html#socket.socket.accept" title="socket.socket.accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.connect" title="socket.socket.connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">connect()</span></code></a> (除了非阻塞套接字), <a class="reference internal" href="../library/socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.recvfrom" title="socket.socket.recvfrom"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recvfrom()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.recvmsg" title="socket.socket.recvmsg"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recvmsg()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.sendall" title="socket.socket.sendall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendall()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.sendmsg" title="socket.socket.sendmsg"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmsg()</span></code></a>, <a class="reference internal" href="../library/socket.html#socket.socket.sendto" title="socket.socket.sendto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendto()</span></code></a>;</p></li>
<li><p><a class="reference internal" href="../library/signal.html#signal.sigtimedwait" title="signal.sigtimedwait"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.sigtimedwait()</span></code></a><a class="reference internal" href="../library/signal.html#signal.sigwaitinfo" title="signal.sigwaitinfo"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.sigwaitinfo()</span></code></a>;</p></li>
<li><p><a class="reference internal" href="../library/time.html#time.sleep" title="time.sleep"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.sleep()</span></code></a>.</p></li>
</ul>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-120"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0475"><strong>PEP 475</strong></a> -- 重试返回 EINTR 失败码的系统调用</dt><dd><p>PEP 和具体实现由 Charles-François Natali 和 Victor Stinner 撰写,并获得 Antoine Pitrou 的协助(同为法国人)。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-479-change-stopiteration-handling-inside-generators">
<span id="whatsnew-pep-479"></span><h3>PEP 479更改生成器内部的 StopIteration 处理<a class="headerlink" href="#pep-479-change-stopiteration-handling-inside-generators" title="永久链接至标题"></a></h3>
<p>生成器与 <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> 的交互在 Python 3.4 及更早版本中有时会令人惊讶,并可能隐藏难以觉察的程序错误。 在之前版本中,一个生成器函数内部意外引发的 <code class="docutils literal notranslate"><span class="pre">StopIteration</span></code> 会被驱动该生成器的循环构造解读为迭代的结束。</p>
<p><span class="target" id="index-121"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0479"><strong>PEP 479</strong></a> 更改了生成器的行为:当一个 <code class="docutils literal notranslate"><span class="pre">StopIteration</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>。 这一更改的主要目的是方便在无防护的 <a class="reference internal" href="../library/functions.html#next" title="next"><code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code></a> 调用引发了 <code class="docutils literal notranslate"><span class="pre">StopIteration</span></code> 并导致生成器所控制的迭代静默地终结的情况下进行调试。 此情况在与 <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> 构造相结合时会特别令人困扰。</p>
<p>这是一个向下不兼容的更改,所以想要启用这个新行为,必须执行 <a class="reference internal" href="../glossary.html#term-future"><span class="xref std std-term">__future__</span></a> 导入:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">generator_stop</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">next</span><span class="p">(</span><span class="nb">iter</span><span class="p">([]))</span>
<span class="gp">... </span> <span class="k">yield</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">gen</span><span class="p">())</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">gen</span>
<span class="gr">StopIteration</span>
<span class="gt">The above exception was the direct cause of the following exception:</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>: <span class="n">generator raised StopIteration</span>
</pre></div>
</div>
<p>如果未执行 <code class="docutils literal notranslate"><span class="pre">__future__</span></code> 导入,当在生成器内部引发 <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</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> 被引发。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-122"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0479"><strong>PEP 479</strong></a> -- 更改生成器内部的 StopIteration 处理</dt><dd><p>PEP 由 Chris Angelico 和 Guido van Rossum 撰写,由 Chris AngelicoYury Selivanov 和 Nick Coghlan 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-485-a-function-for-testing-approximate-equality">
<span id="whatsnew-pep-485"></span><h3>PEP 485用于测试近似相等的函数<a class="headerlink" href="#pep-485-a-function-for-testing-approximate-equality" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-123"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0485"><strong>PEP 485</strong></a> 增加了 <a class="reference internal" href="../library/math.html#math.isclose" title="math.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.isclose()</span></code></a><a class="reference internal" href="../library/cmath.html#cmath.isclose" title="cmath.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">cmath.isclose()</span></code></a> 函数用于检测两个值是否近似相等或称“接近”。 两个值是否接近是根据给定的绝对和相对范围来确定的。 相对范围是 <code class="docutils literal notranslate"><span class="pre">isclose</span></code> 参数之间的最大允许差值,即相对于较大的那个绝对数值的距离:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mf">5.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="mf">4.99998</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isclose</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">rel_tol</span><span class="o">=</span><span class="mf">1e-5</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isclose</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">rel_tol</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>也可以使用绝对范围来比较两个值,它必须是一个非负数值:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mf">5.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="mf">4.99998</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isclose</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">abs_tol</span><span class="o">=</span><span class="mf">0.00003</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">math</span><span class="o">.</span><span class="n">isclose</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">abs_tol</span><span class="o">=</span><span class="mf">0.00001</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-124"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0485"><strong>PEP 485</strong></a> —— 用于测试近似相等的函数</dt><dd><p>PEP 由 Christopher Barker 撰写,由 Chris Barker 和 Tal Einat 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-486-make-the-python-launcher-aware-of-virtual-environments">
<span id="whatsnew-pep-486"></span><h3>PEP 486让 Python 启动器识别虚拟环境<a class="headerlink" href="#pep-486-make-the-python-launcher-aware-of-virtual-environments" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-125"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0486"><strong>PEP 486</strong></a> 让 Windows 版启动器 (参见 <span class="target" id="index-126"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0397"><strong>PEP 397</strong></a>) 能够识别激活的虚拟环境。 当要使用默认解释器并且设置了 <code class="docutils literal notranslate"><span class="pre">VIRTUAL_ENV</span></code> 环境变量时,将使用虚拟环境中的解释器。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-127"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0486"><strong>PEP 486</strong></a> -- 让 Python 启动器识别虚拟环境</dt><dd><p>PEP 由 Paul Moore 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-488-elimination-of-pyo-files">
<span id="whatsnew-pep-488"></span><h3>PEP 488去除 PYO 文件<a class="headerlink" href="#pep-488-elimination-of-pyo-files" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-128"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> 取消了 <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件的概念。 这意味着 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件将同时代表未优化和已优化的字节码。 为了防止经常需要重新生成字节码文件,现在 <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> 文件可以在其名称中设置可选的 <code class="docutils literal notranslate"><span class="pre">opt-</span></code> 标签来表示已优化字节码。 该设置的附带效果是当以 <a class="reference internal" href="../using/cmdline.html#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a><a class="reference internal" href="../using/cmdline.html#cmdoption-oo"><code class="xref std std-option docutils literal notranslate"><span class="pre">-OO</span></code></a> 运行时将不会有字节码文件名冲突。 其结果是,使用 <a class="reference internal" href="../using/cmdline.html#cmdoption-o"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a><a class="reference internal" href="../using/cmdline.html#cmdoption-oo"><code class="xref std std-option docutils literal notranslate"><span class="pre">-OO</span></code></a> 生成的字节码现在可以同时存在。 <a class="reference internal" href="../library/importlib.html#importlib.util.cache_from_source" title="importlib.util.cache_from_source"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.cache_from_source()</span></code></a> 专门针对此项变化更新了 API。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-129"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> -- 去除 PYO 文件</dt><dd><p>PEP 由 Brett Cannon 撰写并实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-489-multi-phase-extension-module-initialization">
<span id="whatsnew-pep-489"></span><h3>PEP 489多阶段扩展模块初始化<a class="headerlink" href="#pep-489-multi-phase-extension-module-initialization" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-130"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a> 更新了扩展模块初始化操作以便利用 Python 3.4 中通过 <span class="target" id="index-131"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0451"><strong>PEP 451</strong></a> 引入的两步模块加载机制的优势。</p>
<p>这一变化让选择使用新机制的扩展模块的导入语义与 Python 源代码和字节码模块的更为接近,包括可以使用任何有效标识符作为模块名称,而不是仅限于 ASCII。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-132"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a> -- 多阶段扩展模块初始化</dt><dd><p>PEP 由 Petr Viktorin , Stefan Behnel 和 Nick Coghlan 撰写,由 Petr Viktorin 实现。</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="other-language-changes">
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题"></a></h2>
<p>对Python 语言核心进行的小改动:</p>
<ul class="simple">
<li><p>增加了 <code class="docutils literal notranslate"><span class="pre">&quot;namereplace&quot;</span></code> 错误处理器。 <code class="docutils literal notranslate"><span class="pre">&quot;backslashreplace&quot;</span></code> 错误处理器现在可用于解码和转译。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19676">bpo-19676</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22286">bpo-22286</a> 中贡献。)</p></li>
<li><p>现在 <a class="reference internal" href="../using/cmdline.html#cmdoption-b"><code class="xref std std-option docutils literal notranslate"><span class="pre">-b</span></code></a> 选项会影响 <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</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> 的比较。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23681">bpo-23681</a> 中贡献。)</p></li>
<li><p>新增 Kazakh <code class="docutils literal notranslate"><span class="pre">kz1048</span></code> 和 Tajik <code class="docutils literal notranslate"><span class="pre">koi8_t</span></code> <a class="reference internal" href="../library/codecs.html#standard-encodings"><span class="std std-ref">编解码器</span></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22682">bpo-22682</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22681">bpo-22681</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> 文档字符串特别适用。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24064">bpo-24064</a> 中贡献。)</p></li>
<li><p>涉及相对导入的环形导入现在已受到支持。 (由 Brett Cannon 和 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17636">bpo-17636</a> 中贡献。)</p></li>
</ul>
</section>
<section id="new-modules">
<h2>新增模块<a class="headerlink" href="#new-modules" title="永久链接至标题"></a></h2>
<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> <a class="reference internal" href="../glossary.html#term-provisional-api"><span class="xref std std-term">暂定</span></a> 模块提供了对函数类型标注的标准定义和工具。 详情参见 <a class="reference internal" href="#whatsnew-pep-484"><span class="std std-ref">类型提示</span></a></p>
</section>
<section id="zipapp">
<span id="whatsnew-zipapp"></span><h3>zipapp<a class="headerlink" href="#zipapp" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/zipapp.html#module-zipapp" title="zipapp: Manage executable Python zip archives"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipapp</span></code></a> 模块(在 <span class="target" id="index-133"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0441"><strong>PEP 441</strong></a> 中描述)提供了用于创建可执行 Python Zip 应用程序的 API 和命令行工具,它是根据 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1739468">bpo-1739468</a> 在 Python 2.6 中引入的,但在当时和之后都没有足够的推广。</p>
<p>使用这个新模块,想要打包你的应用程序只需简单地将所有文件,包括一个 <code class="docutils literal notranslate"><span class="pre">__main__.py</span></code> 文件放到一个目录 <code class="docutils literal notranslate"><span class="pre">myapp</span></code> 中并运行:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>zipapp<span class="w"> </span>myapp
<span class="gp">$ </span>python<span class="w"> </span>myapp.pyz
</pre></div>
</div>
<p>该模块的实现由 Paul Moore 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23491">bpo-23491</a> 中贡献。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-134"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0441"><strong>PEP 441</strong></a> -- 改进 Python ZIP 应用程序支持</p>
</div>
</section>
</section>
<section id="improved-modules">
<h2>改进的模块<a class="headerlink" href="#improved-modules" title="永久链接至标题"></a></h2>
<section id="argparse">
<h3>argparse<a class="headerlink" href="#argparse" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/argparse.html#argparse.ArgumentParser" title="argparse.ArgumentParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ArgumentParser</span></code></a> 类允许通过将 <a class="reference internal" href="../library/argparse.html#allow-abbrev"><span class="std std-ref">allow_abbrev</span></a> 设为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 来禁用长选项的 <a class="reference internal" href="../library/argparse.html#prefix-matching"><span class="std std-ref">缩写用法</span></a>。 (由 Jonathan Paugh, Steven Bethard, paul j3 和 Daniel Eriksson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14910">bpo-14910</a> 中贡献。)</p>
</section>
<section id="asyncio">
<h3>asyncio<a class="headerlink" href="#asyncio" title="永久链接至标题"></a></h3>
<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> 模块处于 <a class="reference internal" href="../glossary.html#term-provisional-api"><span class="xref std std-term">暂定状态</span></a>,在 Python 3.5 中引入的所有改变都已被向下移植到 Python 3.4.x。</p>
<p>自 Python 3.4.0 开始 <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> 模块中的重要变化:</p>
<ul class="simple">
<li><p>新增的调试 API: <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.set_debug" title="asyncio.loop.set_debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.set_debug()</span></code></a><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.get_debug" title="asyncio.loop.get_debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.get_debug()</span></code></a> 方法。 (由 Victor Stinner 贡献。)</p></li>
<li><p>现在 proactor 事件循环已支持 SSL。 (由 Antoine Pitrou 和 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22560">bpo-22560</a> 中贡献。)</p></li>
<li><p>新增的 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.is_closed" title="asyncio.loop.is_closed"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.is_closed()</span></code></a> 方法可检测事件循环是否已关闭。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21326">bpo-21326</a> 中贡献。).)</p></li>
<li><p>新增的 <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">loop.create_task()</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">Task</span></code></a> 并将其排入协程的计划任务。 <code class="docutils literal notranslate"><span class="pre">create_task</span></code> 方法还可供所有将协程包装为任务的 asyncio 函数使用,例如 <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.gather" title="asyncio.gather"><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.gather()</span></code></a> 等。 (由 Victor Stinner 贡献。)</p></li>
<li><p>新增的 <a class="reference internal" href="../library/asyncio-protocol.html#asyncio.WriteTransport.get_write_buffer_limits" title="asyncio.WriteTransport.get_write_buffer_limits"><code class="xref py py-meth docutils literal notranslate"><span class="pre">transport.get_write_buffer_limits()</span></code></a> 方法用于查询流程控制的 <em></em><em></em> 水位限制。 (由 Victor Stinner 贡献。)</p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">async()</span></code> 函数已被弃用并由 <a class="reference internal" href="../library/asyncio-future.html#asyncio.ensure_future" title="asyncio.ensure_future"><code class="xref py py-func docutils literal notranslate"><span class="pre">ensure_future()</span></code></a> 取代。 (由 Yury Selivanov 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.set_task_factory" title="asyncio.loop.set_task_factory"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.set_task_factory()</span></code></a><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.get_task_factory" title="asyncio.loop.get_task_factory"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.get_task_factory()</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">loop.create_task()</span></code></a> 方法使用的任务工厂。 (由 Yury Selivanov 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-queue.html#asyncio.Queue.join" title="asyncio.Queue.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.join()</span></code></a><a class="reference internal" href="../library/asyncio-queue.html#asyncio.Queue.task_done" title="asyncio.Queue.task_done"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Queue.task_done()</span></code></a> 队列方法。 (由 Victor Stinner 贡献。)</p></li>
<li><p>移除了 <code class="docutils literal notranslate"><span class="pre">JoinableQueue</span></code> 类,建议改用 <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> 类。 (由 Victor Stinner 贡献。)</p></li>
</ul>
<p>3.5.1 中的更新:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/asyncio-future.html#asyncio.ensure_future" title="asyncio.ensure_future"><code class="xref py py-func docutils literal notranslate"><span class="pre">ensure_future()</span></code></a> 函数以及所有用到它的函数,比如 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.run_until_complete" title="asyncio.loop.run_until_complete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.run_until_complete()</span></code></a>,现在将接受所有种类的 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">可等待对象</span></a>。 (由 Yury Selivanov 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-task.html#asyncio.run_coroutine_threadsafe" title="asyncio.run_coroutine_threadsafe"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_coroutine_threadsafe()</span></code></a> 函数用于从其他线程向事件循环提交协程。(由 Vincent Michel 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-protocol.html#asyncio.BaseTransport.is_closing" title="asyncio.BaseTransport.is_closing"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Transport.is_closing()</span></code></a> 方法用于检查传输是否正在关闭或已经关闭。 (由 Yury Selivanov 贡献。)</p></li>
<li><p><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> 方法现在可以接受一个主机列表。 (由 Yann Sionneau 贡献。)</p></li>
</ul>
<p>3.5.2 中的更新:</p>
<ul class="simple">
<li><p>新增 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_future" title="asyncio.loop.create_future"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_future()</span></code></a> 方法用来创建 Future 对象。 这允许替代性的事件循环实现,比如 <a class="reference external" href="https://github.com/MagicStack/uvloop">uvloop</a>,以提供更快速的 <a class="reference internal" href="../library/asyncio-future.html#asyncio.Future" title="asyncio.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncio.Future</span></code></a> 实现。 (由 Yury Selivanov 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.get_exception_handler" title="asyncio.loop.get_exception_handler"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.get_exception_handler()</span></code></a> 方法用于获取当前异常处理器。 (由 Yury Selivanov 贡献。)</p></li>
<li><p>新增 <a class="reference internal" href="../library/asyncio-stream.html#asyncio.StreamReader.readuntil" title="asyncio.StreamReader.readuntil"><code class="xref py py-meth docutils literal notranslate"><span class="pre">StreamReader.readuntil()</span></code></a> 方法用于从流读取数据直到出现作为分隔符的字节序列。 (由 Mark Korenberg 贡献。)</p></li>
<li><p><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_connection" title="asyncio.loop.create_connection"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_connection()</span></code></a><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.create_server" title="asyncio.loop.create_server"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.create_server()</span></code></a> 方法已获得优化以避免当地址已被解析时调用系统的 <code class="docutils literal notranslate"><span class="pre">getaddrinfo</span></code> 函数。 (由 A. Jesse Jiryu Davis 贡献。)</p></li>
<li><p><a class="reference internal" href="../library/asyncio-eventloop.html#asyncio.loop.sock_connect" title="asyncio.loop.sock_connect"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loop.sock_connect(sock,</span> <span class="pre">address)</span></code></a> 不再要求在其被调用之前已解析 <em>address</em>。 (由 A. Jesse Jiryu Davis 贡献。)</p></li>
</ul>
</section>
<section id="bz2">
<h3>bz2<a class="headerlink" href="#bz2" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/bz2.html#bz2.BZ2Decompressor.decompress" title="bz2.BZ2Decompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BZ2Decompressor.decompress</span></code></a> 方法接受可选的 <em>max_length</em> 参数用来限制解压缩数据的大小上限。 (由 Nikolaus Rath 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15955">bpo-15955</a> 中贡献。)</p>
</section>
<section id="cgi">
<h3>cgi<a class="headerlink" href="#cgi" title="永久链接至标题"></a></h3>
<p>现在 <code class="xref py py-class docutils literal notranslate"><span class="pre">FieldStorage</span></code> 类已支持 <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> 协议。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20289">bpo-20289</a> 中贡献。)</p>
</section>
<section id="cmath">
<h3>cmath<a class="headerlink" href="#cmath" title="永久链接至标题"></a></h3>
<p>新增函数 <a class="reference internal" href="../library/cmath.html#cmath.isclose" title="cmath.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclose()</span></code></a> 提供了一种测试近似相等的方式。 (由 Chris Barker 和 Tal Einat 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24270">bpo-24270</a> 中贡献。)</p>
</section>
<section id="code">
<h3>code<a class="headerlink" href="#code" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/code.html#code.InteractiveInterpreter.showtraceback" title="code.InteractiveInterpreter.showtraceback"><code class="xref py py-func docutils literal notranslate"><span class="pre">InteractiveInterpreter.showtraceback()</span></code></a> 方法将打印完整的回溯链,就像在交互模式解释器中一样。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17442">bpo-17442</a> 中贡献。)</p>
</section>
<section id="collections">
<h3>collections<a class="headerlink" href="#collections" title="永久链接至标题"></a></h3>
<p id="whatsnew-ordereddict">现在 <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">OrderedDict</span></code></a> 类使用 C 来实现,令其可提速 4 至 100 倍。 (由 Eric Snow 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16991">bpo-16991</a> 中贡献。)</p>
<p><code class="xref py py-meth docutils literal notranslate"><span class="pre">OrderedDict.items()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">OrderedDict.keys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">OrderedDict.values()</span></code> 等视图现在支持 <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> 迭代。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19505">bpo-19505</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">deque</span></code></a> 类现在定义了 <a class="reference internal" href="../library/collections.html#collections.deque.index" title="collections.deque.index"><code class="xref py py-meth docutils literal notranslate"><span class="pre">index()</span></code></a>, <a class="reference internal" href="../library/collections.html#collections.deque.insert" title="collections.deque.insert"><code class="xref py py-meth docutils literal notranslate"><span class="pre">insert()</span></code></a><a class="reference internal" href="../library/collections.html#collections.deque.copy" title="collections.deque.copy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code></a>,并且支持 <code class="docutils literal notranslate"><span class="pre">+</span></code><code class="docutils literal notranslate"><span class="pre">*</span></code> 运算符。 这允许 deque 被识别为 <a class="reference internal" href="../library/collections.abc.html#collections.abc.MutableSequence" title="collections.abc.MutableSequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableSequence</span></code></a> 并提升其替代列表的能力。 (由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23704">bpo-23704</a> 中贡献。)</p>
<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">namedtuple()</span></code></a> 产生的文档字符串现在可以被更新:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Point</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Point&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="s1">&#39;y&#39;</span><span class="p">])</span>
<span class="n">Point</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">+=</span> <span class="s1">&#39;: Cartesian coodinate&#39;</span>
<span class="n">Point</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s1">&#39;abscissa&#39;</span>
<span class="n">Point</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="s1">&#39;ordinate&#39;</span>
</pre></div>
</div>
<p>(由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24064">bpo-24064</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/collections.html#collections.UserString" title="collections.UserString"><code class="xref py py-class docutils literal notranslate"><span class="pre">UserString</span></code></a> 类已实现 <a class="reference internal" href="../library/pickle.html#object.__getnewargs__" title="object.__getnewargs__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getnewargs__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__rmod__" title="object.__rmod__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__rmod__()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.casefold" title="str.casefold"><code class="xref py py-meth docutils literal notranslate"><span class="pre">casefold()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.format_map" title="str.format_map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format_map()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.isprintable" title="str.isprintable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isprintable()</span></code></a><a class="reference internal" href="../library/stdtypes.html#str.maketrans" title="str.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">maketrans()</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> 方法相匹配。 (由 Joe Jevnik 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22189">bpo-22189</a> 中贡献。)</p>
</section>
<section id="collections-abc">
<h3>collections.abc<a class="headerlink" href="#collections-abc" title="永久链接至标题"></a></h3>
<p>现在 <code class="xref py py-meth docutils literal notranslate"><span class="pre">Sequence.index()</span></code> 方法接受 <em>start</em><em>stop</em> 参数以与对应的 <a class="reference internal" href="../library/stdtypes.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a>, <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> 等的方法相匹配。 (由 Devin Jeanpierre 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23086">bpo-23086</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/collections.abc.html#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> 抽象基类。 (由 Stefan Behnel 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24018">bpo-24018</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/collections.abc.html#collections.abc.Awaitable" title="collections.abc.Awaitable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Awaitable</span></code></a>, <a class="reference internal" href="../library/collections.abc.html#collections.abc.Coroutine" title="collections.abc.Coroutine"><code class="xref py py-class docutils literal notranslate"><span class="pre">Coroutine</span></code></a>, <a class="reference internal" href="../library/collections.abc.html#collections.abc.AsyncIterator" title="collections.abc.AsyncIterator"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterator</span></code></a><a class="reference internal" href="../library/collections.abc.html#collections.abc.AsyncIterable" title="collections.abc.AsyncIterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncIterable</span></code></a> 抽象基类。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24184">bpo-24184</a> 中贡献。)</p>
<p>对于更早的 Python这些新 ABC 的向下移植版可通过一个外部的 <a class="reference external" href="https://pypi.org/project/backports_abc">PyPI 包</a> 来获取。</p>
</section>
<section id="compileall">
<h3>compileall<a class="headerlink" href="#compileall" title="永久链接至标题"></a></h3>
<p>新增 <a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> 选项 <code class="samp docutils literal notranslate"><span class="pre">-j</span> <em><span class="pre">N</span></em></code>,允许同时运行 <em>N</em> 个工作进程来执行并行的字节码编译。 <a class="reference internal" href="../library/compileall.html#compileall.compile_dir" title="compileall.compile_dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_dir()</span></code></a> 函数增加了相应的 <code class="docutils literal notranslate"><span class="pre">workers</span></code> 形参。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16104">bpo-16104</a> 中贡献。)</p>
<p>另一个新选项 <code class="docutils literal notranslate"><span class="pre">-r</span></code>,允许控制最大的子目录递归层级。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19628">bpo-19628</a> 中贡献。)</p>
<p>现在可以多次指定 <code class="docutils literal notranslate"><span class="pre">-q</span></code> 命令行选项,在此情况下,所有输出包括错误都将被抑制。 在 <a class="reference internal" href="../library/compileall.html#compileall.compile_dir" title="compileall.compile_dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_dir()</span></code></a>, <a class="reference internal" href="../library/compileall.html#compileall.compile_file" title="compileall.compile_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_file()</span></code></a><a class="reference internal" href="../library/compileall.html#compileall.compile_path" title="compileall.compile_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile_path()</span></code></a> 中相应的 <code class="docutils literal notranslate"><span class="pre">quiet</span></code> 形参现在可接受一个整数值来指明输出抑制的级别。 (由 Thomas Kluyver 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21338">bpo-21338</a> 中贡献。)</p>
</section>
<section id="concurrent-futures">
<h3>concurrent.futures<a class="headerlink" href="#concurrent-futures" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Executor.map()</span></code></a> 方法接受一个 <em>chunksize</em> 参数以允许在使用 <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ProcessPoolExecutor()</span></code></a> 设置任务批次来提升运行效率。 (由 Dan O'Reilly 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11271">bpo-11271</a> 中贡献。)</p>
<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">ThreadPoolExecutor</span></code></a> 构造器中的工作线程数量是可选的。 默认值为 CPU 数量的 5 倍。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21527">bpo-21527</a> 中贡献。)</p>
</section>
<section id="configparser">
<h3>configparser<a class="headerlink" href="#configparser" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a> 提供了一种方式通过在 <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a> 构造器中指定一个由转换器组成的字典,或将它们定义为 <code class="docutils literal notranslate"><span class="pre">ConfigParser</span></code> 子类中的方法来定制值的转换。 在解析器实例中定义的转换器将被它的节代理所继承。</p>
<p>示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">configparser</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conv</span> <span class="o">=</span> <span class="p">{}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conv</span><span class="p">[</span><span class="s1">&#39;list&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span> <span class="p">[</span><span class="n">e</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">v</span><span class="o">.</span><span class="n">split</span><span class="p">()</span> <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">strip</span><span class="p">()]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cfg</span> <span class="o">=</span> <span class="n">configparser</span><span class="o">.</span><span class="n">ConfigParser</span><span class="p">(</span><span class="n">converters</span><span class="o">=</span><span class="n">conv</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cfg</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">[s]</span>
<span class="gp">... </span><span class="s2">list = a b c d e f g</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cfg</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">,</span> <span class="s1">&#39;list&#39;</span><span class="p">)</span>
<span class="go">&#39;a b c d e f g&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">cfg</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">,</span> <span class="s1">&#39;list&#39;</span><span class="p">)</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span> <span class="o">=</span> <span class="n">cfg</span><span class="p">[</span><span class="s1">&#39;s&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="o">.</span><span class="n">getlist</span><span class="p">(</span><span class="s1">&#39;list&#39;</span><span class="p">)</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;]</span>
</pre></div>
</div>
<p>(由 Łukasz Langa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18159">bpo-18159</a> 中贡献。)</p>
</section>
<section id="contextlib">
<h3>contextlib<a class="headerlink" href="#contextlib" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/contextlib.html#contextlib.redirect_stderr" title="contextlib.redirect_stderr"><code class="xref py py-func docutils literal notranslate"><span class="pre">redirect_stderr()</span></code></a> <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> (类似于 <a class="reference internal" href="../library/contextlib.html#contextlib.redirect_stdout" title="contextlib.redirect_stdout"><code class="xref py py-func docutils literal notranslate"><span class="pre">redirect_stdout()</span></code></a>) 使得工具脚本更容易处理那些将输出写入到 <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> 并且不提供任何重定向选项的不灵活 API。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">contextlib</span><span class="o">,</span> <span class="nn">io</span><span class="o">,</span> <span class="nn">logging</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">contextlib</span><span class="o">.</span><span class="n">redirect_stderr</span><span class="p">(</span><span class="n">f</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s1">&#39;warning&#39;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
<span class="go">&#39;WARNING:root:warning\n&#39;</span>
</pre></div>
</div>
<p>(由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22389">bpo-22389</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.csvwriter.writerow" title="csv.csvwriter.writerow"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writerow()</span></code></a> 方法可支持任意可迭代对象,而不仅是序列。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23171">bpo-23171</a> 中贡献。)</p>
</section>
<section id="curses">
<h3>curses<a class="headerlink" href="#curses" title="永久链接至标题"></a></h3>
<p>The new <a class="reference internal" href="../library/curses.html#curses.update_lines_cols" title="curses.update_lines_cols"><code class="xref py py-func docutils literal notranslate"><span class="pre">update_lines_cols()</span></code></a> function updates the <span class="target" id="index-31"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LINES</span></code>
and <span class="target" id="index-32"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">COLS</span></code> environment variables. This is useful for detecting
manual screen resizing. (Contributed by Arnon Yaari in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4254">bpo-4254</a>.)</p>
</section>
<section id="dbm">
<h3>dbm<a class="headerlink" href="#dbm" title="永久链接至标题"></a></h3>
<p>当设置旗标值为 <code class="docutils literal notranslate"><span class="pre">&quot;n&quot;</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">dumb.open</span></code></a> 将总是创建新的数据库。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18039">bpo-18039</a> 中贡献。)</p>
</section>
<section id="difflib">
<h3>difflib<a class="headerlink" href="#difflib" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/difflib.html#difflib.HtmlDiff.make_file" title="difflib.HtmlDiff.make_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">HtmlDiff.make_file()</span></code></a> 生成的 HTML 文档的字符集现在可通过使用新增的 <em>charset</em> 仅限关键字参数来自定义。 HTML 文档的默认字符集已从 <code class="docutils literal notranslate"><span class="pre">&quot;ISO-8859-1&quot;</span></code> 改为 <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code>。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2052">bpo-2052</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/difflib.html#difflib.diff_bytes" title="difflib.diff_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">diff_bytes()</span></code></a> 函数可能比较字节串的列表。 这修复了一个来自 Python 2 遗留总是。 (由 Terry J. Reedy 和 Greg Ward 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17445">bpo-17445</a> 中贡献。)</p>
</section>
<section id="distutils">
<h3>distutils<a class="headerlink" href="#distutils" title="永久链接至标题"></a></h3>
<p>现在 <code class="docutils literal notranslate"><span class="pre">build</span></code><code class="docutils literal notranslate"><span class="pre">build_ext</span></code> 命令都接受一个 <code class="docutils literal notranslate"><span class="pre">-j</span></code> 选项以启用扩展模块的并行编译。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5309">bpo-5309</a> 中贡献。)</p>
<p>The <a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a> module now supports <code class="docutils literal notranslate"><span class="pre">xz</span></code> compression, and can be
enabled by passing <code class="docutils literal notranslate"><span class="pre">xztar</span></code> as an argument to <code class="docutils literal notranslate"><span class="pre">bdist</span> <span class="pre">--format</span></code>.
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16314">bpo-16314</a>.)</p>
</section>
<section id="doctest">
<h3>doctest<a class="headerlink" href="#doctest" title="永久链接至标题"></a></h3>
<p>如果 <em>module</em> 不包含任何文档字符串则 <a class="reference internal" href="../library/doctest.html#doctest.DocTestSuite" title="doctest.DocTestSuite"><code class="xref py py-func docutils literal notranslate"><span class="pre">DocTestSuite()</span></code></a> 函数将返回一个空的 <a class="reference internal" href="../library/unittest.html#unittest.TestSuite" title="unittest.TestSuite"><code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.TestSuite</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>。 (由 Glenn Jones 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15916">bpo-15916</a> 中贡献。)</p>
</section>
<section id="email">
<h3>email<a class="headerlink" href="#email" title="永久链接至标题"></a></h3>
<p>新增的策略选项 <a class="reference internal" href="../library/email.policy.html#email.policy.Policy.mangle_from_" title="email.policy.Policy.mangle_from_"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Policy.mangle_from_</span></code></a> 可控制生成器是否要为电子邮件消息体中以controls whether or not lines that start with <code class="docutils literal notranslate"><span class="pre">&quot;From</span> <span class="pre">&quot;</span></code> 打头的行添加 <code class="docutils literal notranslate"><span class="pre">&quot;&gt;&quot;</span></code> 字符前缀。 默认 <a class="reference internal" href="../library/email.policy.html#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-attr docutils literal notranslate"><span class="pre">compat32</span></code></a><code class="docutils literal notranslate"><span class="pre">True</span></code> 而所有其他策略均为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。 (由 Milan Oberkirch 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20098">bpo-20098</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/email.compat32-message.html#email.message.Message.get_content_disposition" title="email.message.Message.get_content_disposition"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Message.get_content_disposition()</span></code></a> 方法可提供对 <em class="mailheader">Content-Disposition</em> 标头规范值的便捷访问。 (由 Abhilash Raj 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21083">bpo-21083</a> 中贡献。)</p>
<p>新增的策略选项 <a class="reference internal" href="../library/email.policy.html#email.policy.EmailPolicy.utf8" title="email.policy.EmailPolicy.utf8"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EmailPolicy.utf8</span></code></a> 可被设为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 以使用 UTF-8 字符集而不是使用已编码的字来编码电子邮件标头。 这将允许根据 <span class="target" id="index-135"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6532.html"><strong>RFC 6532</strong></a> 来格式化 <code class="docutils literal notranslate"><span class="pre">Messages</span></code> 并配合支持 <span class="target" id="index-136"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6531.html"><strong>RFC 6531</strong></a> <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> 扩展的 SMTP 服务器使用。 (由 R. David Murray 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24211">bpo-24211</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/email.mime.html#email.mime.text.MIMEText" title="email.mime.text.MIMEText"><code class="xref py py-class docutils literal notranslate"><span class="pre">mime.text.MIMEText</span></code></a> 构造器可接受一个 <a class="reference internal" href="../library/email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">charset.Charset</span></code></a> 实例。 (由 Claude Paroz 和 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16324">bpo-16324</a> 中贡献。)</p>
</section>
<section id="enum">
<h3>enum<a class="headerlink" href="#enum" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/enum.html#enum.Enum" title="enum.Enum"><code class="xref py py-class docutils literal notranslate"><span class="pre">Enum</span></code></a> 可调用对象新增了一个形参 <em>start</em> 用于在只提供了 <em>names</em> 的情况下指定枚举值的初始数量:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span> <span class="o">=</span> <span class="n">enum</span><span class="o">.</span><span class="n">Enum</span><span class="p">(</span><span class="s1">&#39;Animal&#39;</span><span class="p">,</span> <span class="s1">&#39;cat dog&#39;</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span><span class="o">.</span><span class="n">cat</span>
<span class="go">&lt;Animal.cat: 10&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Animal</span><span class="o">.</span><span class="n">dog</span>
<span class="go">&lt;Animal.dog: 11&gt;</span>
</pre></div>
</div>
<p>(由 Ethan Furman 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21706">bpo-21706</a> 中贡献。)</p>
</section>
<section id="faulthandler">
<h3>faulthandler<a class="headerlink" href="#faulthandler" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/faulthandler.html#faulthandler.enable" title="faulthandler.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">enable()</span></code></a>, <a class="reference internal" href="../library/faulthandler.html#faulthandler.register" title="faulthandler.register"><code class="xref py py-func docutils literal notranslate"><span class="pre">register()</span></code></a>, <a class="reference internal" href="../library/faulthandler.html#faulthandler.dump_traceback" title="faulthandler.dump_traceback"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump_traceback()</span></code></a><a class="reference internal" href="../library/faulthandler.html#faulthandler.dump_traceback_later" title="faulthandler.dump_traceback_later"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump_traceback_later()</span></code></a> 函数除了接受文件型对象以外也接受文件描述符。 (由 Wei Wu 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23566">bpo-23566</a> 中贡献。)</p>
</section>
<section id="functools">
<h3>functools<a class="headerlink" href="#functools" title="永久链接至标题"></a></h3>
<p id="whatsnew-lrucache">现在大部分 <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">lru_cache()</span></code></a> 机制已使用 C 实现,使其速度显著提升。 (由 Matt Joiner, Alexey Kachayev 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14373">bpo-14373</a> 中贡献。)</p>
</section>
<section id="glob">
<h3>glob<a class="headerlink" href="#glob" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/glob.html#glob.iglob" title="glob.iglob"><code class="xref py py-func docutils literal notranslate"><span class="pre">iglob()</span></code></a><a class="reference internal" href="../library/glob.html#glob.glob" title="glob.glob"><code class="xref py py-func docutils literal notranslate"><span class="pre">glob()</span></code></a> 函数支持通过使用 <code class="docutils literal notranslate"><span class="pre">&quot;**&quot;</span></code> 模式在子目录中递归搜索。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13968">bpo-13968</a> 中贡献。)</p>
</section>
<section id="gzip">
<h3>gzip<a class="headerlink" href="#gzip" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">GzipFile</span></code></a> 构造器的 <em>mode</em> 参数可接受 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> 来请求独占式的创建。 (由 Tim Heaney 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19222">bpo-19222</a> 中贡献。)</p>
</section>
<section id="heapq">
<h3>heapq<a class="headerlink" href="#heapq" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/heapq.html#heapq.merge" title="heapq.merge"><code class="xref py py-func docutils literal notranslate"><span class="pre">merge()</span></code></a> 中的元素比较可通过在新增的可选关键字参数 <em>key</em> 中传入一个 <a class="reference internal" href="../glossary.html#term-key-function"><span class="xref std std-term">key function</span></a> 来进行定制,并且新增了可选关键字参数 <em>reverse</em> 用于反向的元素比较:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">heapq</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;9&#39;</span><span class="p">,</span> <span class="s1">&#39;777&#39;</span><span class="p">,</span> <span class="s1">&#39;55555&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;88&#39;</span><span class="p">,</span> <span class="s1">&#39;6666&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">heapq</span><span class="o">.</span><span class="n">merge</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">key</span><span class="o">=</span><span class="nb">len</span><span class="p">))</span>
<span class="go">[&#39;9&#39;, &#39;88&#39;, &#39;777&#39;, &#39;6666&#39;, &#39;55555&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">heapq</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">b</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="nb">len</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[&#39;55555&#39;, &#39;6666&#39;, &#39;777&#39;, &#39;88&#39;, &#39;9&#39;]</span>
</pre></div>
</div>
<p>(由 Raymond Hettinger 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13742">bpo-13742</a> 中贡献。)</p>
</section>
<section id="http">
<h3>http<a class="headerlink" href="#http" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/http.html#http.HTTPStatus" title="http.HTTPStatus"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPStatus</span></code></a> 枚举定义了一组 HTTP 状态码、英文短说明和长描述。 (由 Demian Brecht 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21793">bpo-21793</a> 中贡献。)</p>
</section>
<section id="http-client">
<h3>http.client<a class="headerlink" href="#http-client" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.getresponse" title="http.client.HTTPConnection.getresponse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">HTTPConnection.getresponse()</span></code></a> 在远程服务器连接被意外关闭时会引发 <a class="reference internal" href="../library/http.client.html#http.client.RemoteDisconnected" title="http.client.RemoteDisconnected"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RemoteDisconnected</span></code></a> 异常。 此外,如果引发了 <a class="reference internal" href="../library/exceptions.html#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a> (<code class="docutils literal notranslate"><span class="pre">RemoteDisconnected</span></code> 是其子类),客户端套接字现在会自动关闭,并将在下次请求时重新连接:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">retries</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s1">&#39;GET&#39;</span><span class="p">,</span> <span class="s1">&#39;/&#39;</span><span class="p">)</span>
<span class="n">resp</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="k">except</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">RemoteDisconnected</span><span class="p">:</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>(由 Martin Panter 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3566">bpo-3566</a> 中贡献。)</p>
</section>
<section id="idlelib-and-idle">
<h3>idlelib 与 IDLE<a class="headerlink" href="#idlelib-and-idle" title="永久链接至标题"></a></h3>
<p>由于 idlelib 实现了 IDLE 命令行界面和编辑器且不应被其他程序导入,它将随每个发布版获得不断改进。 请参阅 <code class="file docutils literal notranslate"><span class="pre">Lib/idlelib/NEWS.txt</span></code> 查看 3.4.0 以来的累积改变列表,以及未来 3.5.x 发布版将进行的改变。 此文件也可通过 IDLE <span class="menuselection">Help ‣ About IDLE</span> 对话框查看。</p>
</section>
<section id="imaplib">
<h3>imaplib<a class="headerlink" href="#imaplib" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4" title="imaplib.IMAP4"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4</span></code></a> 类已支持 <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> 协议。 当用于 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句时IMAP4 <code class="docutils literal notranslate"><span class="pre">LOGOUT</span></code> 命令将在代码块的末尾被自动调用。 (由 Tarek Ziadé 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4972">bpo-4972</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> 模块已通过 <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4.enable" title="imaplib.IMAP4.enable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">IMAP4.enable()</span></code></a> 方法支持 <span class="target" id="index-137"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5161.html"><strong>RFC 5161</strong></a> (ENABLE 扩展) 和 <span class="target" id="index-138"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6855.html"><strong>RFC 6855</strong></a> (UTF-8 支持)。 新增的 <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4.utf8_enabled" title="imaplib.IMAP4.utf8_enabled"><code class="xref py py-attr docutils literal notranslate"><span class="pre">IMAP4.utf8_enabled</span></code></a> 属性可跟踪 <span class="target" id="index-139"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6855.html"><strong>RFC 6855</strong></a> 支持是否被启用。 (由 Milan Oberkirch, R. David Murray 和 Maciej Szulik 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21800">bpo-21800</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> 模块会自动使用 UTF-8 自动编码非 ASCII 字符串的用户名和密码,如 RFC 所建议的那样。 (由 Milan Oberkirch 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21800">bpo-21800</a> 中贡献。)</p>
</section>
<section id="imghdr">
<h3>imghdr<a class="headerlink" href="#imghdr" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/imghdr.html#imghdr.what" title="imghdr.what"><code class="xref py py-func docutils literal notranslate"><span class="pre">what()</span></code></a> function now recognizes the
<a class="reference external" href="http://www.openexr.com">OpenEXR</a> format
(contributed by Martin Vignali and Claudiu Popa in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20295">bpo-20295</a>),
and the <a class="reference external" href="https://en.wikipedia.org/wiki/WebP">WebP</a> format
(contributed by Fabrice Aneche and Claudiu Popa in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20197">bpo-20197</a>.)</p>
</section>
<section id="importlib">
<h3>importlib<a class="headerlink" href="#importlib" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/importlib.html#importlib.util.LazyLoader" title="importlib.util.LazyLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">util.LazyLoader</span></code></a> 类允许在对启动时间敏感的应用程序中使用模块的惰性加载。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17621">bpo-17621</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/importlib.html#importlib.abc.InspectLoader.source_to_code" title="importlib.abc.InspectLoader.source_to_code"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.InspectLoader.source_to_code()</span></code></a> 方法属于静态方法。 这样将可更方便地通过运行 <code class="docutils literal notranslate"><span class="pre">exec(code,</span> <span class="pre">module.__dict__)</span></code> 基于字符串编译的代码来初始化模块。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21156">bpo-21156</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/importlib.html#importlib.util.module_from_spec" title="importlib.util.module_from_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">util.module_from_spec()</span></code></a> 函数现在是创建新模块的首先方式。 相比直接创建 <a class="reference internal" href="../library/types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.ModuleType</span></code></a> 实例,这个新函数将基于传入的规格说明对象设置各种导入控制的属性。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20383">bpo-20383</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.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> 类都是可封存和可哈希的对象。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20726">bpo-20726</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20334">bpo-20334</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/inspect.html#inspect.BoundArguments.apply_defaults" title="inspect.BoundArguments.apply_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BoundArguments.apply_defaults()</span></code></a> 方法提供了一种为缺失的参数设置默认值的方式:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="s1">&#39;ham&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">pass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s1">&#39;spam&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span><span class="o">.</span><span class="n">apply_defaults</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ba</span><span class="o">.</span><span class="n">arguments</span>
<span class="go">OrderedDict([(&#39;a&#39;, &#39;spam&#39;), (&#39;b&#39;, &#39;ham&#39;), (&#39;args&#39;, ())])</span>
</pre></div>
</div>
<p>(由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24190">bpo-24190</a> 中贡献。)</p>
<p>新增的类方法 <a class="reference internal" href="../library/inspect.html#inspect.Signature.from_callable" title="inspect.Signature.from_callable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.from_callable()</span></code></a> 使得 <a class="reference internal" href="../library/inspect.html#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> 的子类化更为容易。 (由 Yury Selivanov 和 Eric Snow 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17373">bpo-17373</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> 函数接受 <em>follow_wrapped</em> 可选关键字参数,当其设为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 时,将禁用 <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> 链接的自动跟随。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20691">bpo-20691</a> 中贡献。)</p>
<p>新增了一组用于检查 <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">协程函数</span></a><a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">协程对象</span></a> 的函数: <a class="reference internal" href="../library/inspect.html#inspect.iscoroutine" title="inspect.iscoroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutine()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.iscoroutinefunction" title="inspect.iscoroutinefunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">iscoroutinefunction()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.isawaitable" title="inspect.isawaitable"><code class="xref py py-func docutils literal notranslate"><span class="pre">isawaitable()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.getcoroutinelocals" title="inspect.getcoroutinelocals"><code class="xref py py-func docutils literal notranslate"><span class="pre">getcoroutinelocals()</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.getcoroutinestate" title="inspect.getcoroutinestate"><code class="xref py py-func docutils literal notranslate"><span class="pre">getcoroutinestate()</span></code></a>。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24017">bpo-24017</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24400">bpo-24400</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/inspect.html#inspect.stack" title="inspect.stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">stack()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.trace" title="inspect.trace"><code class="xref py py-func docutils literal notranslate"><span class="pre">trace()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.getouterframes" title="inspect.getouterframes"><code class="xref py py-func docutils literal notranslate"><span class="pre">getouterframes()</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.getinnerframes" title="inspect.getinnerframes"><code class="xref py py-func docutils literal notranslate"><span class="pre">getinnerframes()</span></code></a> 函数将返回具名元组的列表。 (由 Daniel Shahaf 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16808">bpo-16808</a> 中贡献。)</p>
</section>
<section id="io">
<h3>io<a class="headerlink" href="#io" title="永久链接至标题"></a></h3>
<p>新增 <a class="reference internal" href="../library/io.html#io.BufferedIOBase.readinto1" title="io.BufferedIOBase.readinto1"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BufferedIOBase.readinto1()</span></code></a> 方法,该方法会使用至少一次对下层原始流的调用 <a class="reference internal" href="../library/io.html#io.RawIOBase.read" title="io.RawIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">RawIOBase.read()</span></code></a><a class="reference internal" href="../library/io.html#io.RawIOBase.readinto" title="io.RawIOBase.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">RawIOBase.readinto()</span></code></a> 方法。 (由 Nikolaus Rath 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20578">bpo-20578</a> 中贡献。)</p>
</section>
<section id="ipaddress">
<h3>ipaddress<a class="headerlink" href="#ipaddress" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a><a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> 类均接受一个 <code class="docutils literal notranslate"><span class="pre">(address,</span> <span class="pre">netmask)</span></code> 元组参数,可以根据现有地址方便地构造网络对象:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ipaddress</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">((</span><span class="s1">&#39;127.0.0.0&#39;</span><span class="p">,</span> <span class="mi">8</span><span class="p">))</span>
<span class="go">IPv4Network(&#39;127.0.0.0/8&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Network</span><span class="p">((</span><span class="s1">&#39;127.0.0.0&#39;</span><span class="p">,</span> <span class="s1">&#39;255.0.0.0&#39;</span><span class="p">))</span>
<span class="go">IPv4Network(&#39;127.0.0.0/8&#39;)</span>
</pre></div>
</div>
<p>(由 Peter Moody 和 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16531">bpo-16531</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a><a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> 类的 <code class="xref py py-attr docutils literal notranslate"><span class="pre">reverse_pointer</span></code> 属性用于返回 DNS PTR 反向记录的名称:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ipaddress</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">addr</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv4Address</span><span class="p">(</span><span class="s1">&#39;127.0.0.1&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">addr</span><span class="o">.</span><span class="n">reverse_pointer</span>
<span class="go">&#39;1.0.0.127.in-addr.arpa&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span> <span class="o">=</span> <span class="n">ipaddress</span><span class="o">.</span><span class="n">IPv6Address</span><span class="p">(</span><span class="s1">&#39;::1&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">addr6</span><span class="o">.</span><span class="n">reverse_pointer</span>
<span class="go">&#39;1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa&#39;</span>
</pre></div>
</div>
<p>(由 Leon Weber 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20480">bpo-20480</a> 中贡献。)</p>
</section>
<section id="json">
<h3>json<a class="headerlink" href="#json" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/json.html#module-json.tool" title="json.tool: A command line to validate and pretty-print JSON."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json.tool</span></code></a> 命令行接口会保留输入中传入的 JSON 对象中键的顺序。 新的 <code class="docutils literal notranslate"><span class="pre">--sort-keys</span></code> 选项可用于按字母顺序对键进行排序。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21650">bpo-21650</a> 中贡献。)</p>
<p>现在 JSON 解码器会引发 <a class="reference internal" href="../library/json.html#json.JSONDecodeError" title="json.JSONDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">JSONDecodeError</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> 以提供有关错误的更好的上下文信息。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19361">bpo-19361</a> 中贡献。)</p>
</section>
<section id="linecache">
<h3>linecache<a class="headerlink" href="#linecache" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/linecache.html#linecache.lazycache" title="linecache.lazycache"><code class="xref py py-func docutils literal notranslate"><span class="pre">lazycache()</span></code></a> 函数可用于捕获非基于文件的模块 的信息以允许在此后通过 <a class="reference internal" href="../library/linecache.html#linecache.getline" title="linecache.getline"><code class="xref py py-func docutils literal notranslate"><span class="pre">getline()</span></code></a> 获取其行数据。 这样就可以避免在实际需要某一行之前执行 I/O 操作,从而不需要无限期地持有模块 globals。 (由 Robert Collins 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17911">bpo-17911</a> 中贡献。)</p>
</section>
<section id="locale">
<h3>locale<a class="headerlink" href="#locale" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/locale.html#locale.delocalize" title="locale.delocalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">delocalize()</span></code></a> 函数可用于将字符串转换为表示规范化数字的字符串 ,并会考虑 <code class="docutils literal notranslate"><span class="pre">LC_NUMERIC</span></code> 的设置:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">locale</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_NUMERIC</span><span class="p">,</span> <span class="s1">&#39;de_DE.UTF-8&#39;</span><span class="p">)</span>
<span class="go">&#39;de_DE.UTF-8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">delocalize</span><span class="p">(</span><span class="s1">&#39;1.234,56&#39;</span><span class="p">)</span>
<span class="go">&#39;1234.56&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">setlocale</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">LC_NUMERIC</span><span class="p">,</span> <span class="s1">&#39;en_US.UTF-8&#39;</span><span class="p">)</span>
<span class="go">&#39;en_US.UTF-8&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">locale</span><span class="o">.</span><span class="n">delocalize</span><span class="p">(</span><span class="s1">&#39;1,234.56&#39;</span><span class="p">)</span>
<span class="go">&#39;1234.56&#39;</span>
</pre></div>
</div>
<p>(由 Cédric Krier 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13918">bpo-13918</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.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> <a class="reference internal" href="../library/logging.html#logging.Logger.log" title="logging.Logger.log"><code class="xref py py-meth docutils literal notranslate"><span class="pre">log()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.exception" title="logging.Logger.exception"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exception()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.critical" title="logging.Logger.critical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">critical()</span></code></a>, <a class="reference internal" href="../library/logging.html#logging.Logger.debug" title="logging.Logger.debug"><code class="xref py py-meth docutils literal notranslate"><span class="pre">debug()</span></code></a> 等)的 <em>exc_info</em> 参数现在除接受布尔值和异常元组以外,也接受异常实例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">logging</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">ZeroDivisionError</span> <span class="k">as</span> <span class="n">ex</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;exception&#39;</span><span class="p">,</span> <span class="n">exc_info</span><span class="o">=</span><span class="n">ex</span><span class="p">)</span>
<span class="go">ERROR:root:exception</span>
</pre></div>
</div>
<p>(由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20537">bpo-20537</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/logging.handlers.html#logging.handlers.HTTPHandler" title="logging.handlers.HTTPHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">handlers.HTTPHandler</span></code></a> 类接受一个可选的 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 实例来配置在 HTTP 连接中使用的 SSL 设置。 (由 Alex Gaynor 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22788">bpo-22788</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/logging.handlers.html#logging.handlers.QueueListener" title="logging.handlers.QueueListener"><code class="xref py py-class docutils literal notranslate"><span class="pre">handlers.QueueListener</span></code></a> 类接受一个 <em>respect_handler_level</em> 关键字参数,如果将该参数设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,会向处理器传递消息以将处理器级别纳入考量。 (由 Vinay Sajip 贡献。)</p>
</section>
<section id="lzma">
<h3>lzma<a class="headerlink" href="#lzma" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/lzma.html#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">LZMADecompressor.decompress()</span></code></a> 方法接受一个可选的 <em>max_length</em> 参数用来限制解压缩数据的大小上限。 (由 Martin Panter 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15955">bpo-15955</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#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> 模块增加了两个新常量: <a class="reference internal" href="../library/math.html#math.inf" title="math.inf"><code class="xref py py-data docutils literal notranslate"><span class="pre">inf</span></code></a><a class="reference internal" href="../library/math.html#math.nan" title="math.nan"><code class="xref py py-data docutils literal notranslate"><span class="pre">nan</span></code></a>。 (由 Mark Dickinson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23185">bpo-23185</a> 中贡献。)</p>
<p>新增函数 <a class="reference internal" href="../library/math.html#math.isclose" title="math.isclose"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclose()</span></code></a> 提供了一种测试近似相等的方式。 (由 Chris Barker 和 Tal Einat 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24270">bpo-24270</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/math.html#math.gcd" title="math.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">gcd()</span></code></a> 函数。 现在 <a class="reference internal" href="../library/fractions.html#fractions.gcd" title="fractions.gcd"><code class="xref py py-func docutils literal notranslate"><span class="pre">fractions.gcd()</span></code></a> 函数已被弃用。 (由 Mark Dickinson 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22486">bpo-22486</a> 中贡献。)</p>
</section>
<section id="multiprocessing">
<h3>multiprocessing<a class="headerlink" href="#multiprocessing" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.sharedctypes.synchronized" title="multiprocessing.sharedctypes.synchronized"><code class="xref py py-func docutils literal notranslate"><span class="pre">sharedctypes.synchronized()</span></code></a> 对象支持 <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> 协议。 (由 Charles-François Natali 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21565">bpo-21565</a> 中贡献。)</p>
</section>
<section id="operator">
<h3>operator<a class="headerlink" href="#operator" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/operator.html#operator.attrgetter" title="operator.attrgetter"><code class="xref py py-func docutils literal notranslate"><span class="pre">attrgetter()</span></code></a>, <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">itemgetter()</span></code></a><a class="reference internal" href="../library/operator.html#operator.methodcaller" title="operator.methodcaller"><code class="xref py py-func docutils literal notranslate"><span class="pre">methodcaller()</span></code></a> 对象支持封存操作。 (由 Josh Rosenberg 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22955">bpo-22955</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/operator.html#operator.matmul" title="operator.matmul"><code class="xref py py-func docutils literal notranslate"><span class="pre">matmul()</span></code></a><a class="reference internal" href="../library/operator.html#operator.imatmul" title="operator.imatmul"><code class="xref py py-func docutils literal notranslate"><span class="pre">imatmul()</span></code></a> 函数用于执行矩阵乘法。 (由 Benjamin Peterson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21176">bpo-21176</a> 中贡献。)</p>
</section>
<section id="os">
<h3>os<a class="headerlink" href="#os" title="永久链接至标题"></a></h3>
<p>新增 <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">scandir()</span></code></a> 函数可返回一个 <a class="reference internal" href="../library/os.html#os.DirEntry" title="os.DirEntry"><code class="xref py py-class docutils literal notranslate"><span class="pre">DirEntry</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">scandir()</span></code></a> 会在扫描目录时提取文件属性,不必再执行后续的系统调用来确定文件类型或属性,这可以显著提升运行效率。 (由 Ben Hoyt 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22524">bpo-22524</a> 并得到 Victor Stinner 的协助。)</p>
<p>在 Windows 上,现在可使用新的 <a class="reference internal" href="../library/os.html#os.stat_result.st_file_attributes" title="os.stat_result.st_file_attributes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">stat_result.st_file_attributes</span></code></a> 属性。 它对应于 <code class="docutils literal notranslate"><span class="pre">GetFileInformationByHandle()</span></code> 所返回的 <code class="docutils literal notranslate"><span class="pre">BY_HANDLE_FILE_INFORMATION</span></code> 结构体的 <code class="docutils literal notranslate"><span class="pre">dwFileAttributes</span></code> 成员。 (由 Ben Hoyt 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21719">bpo-21719</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">urandom()</span></code></a> 函数在 Linux 3.17 或更新的系统上会使用 <code class="docutils literal notranslate"><span class="pre">getrandom()</span></code> 系统调用,而在 OpenBSD 5.6 或更新的系统上会使用 <code class="docutils literal notranslate"><span class="pre">getentropy()</span></code>,不必再使用 <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> 并避免由于潜在的文件描述符耗尽导致的执行失败。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22181">bpo-22181</a> 中贡献。)</p>
<p>New <a class="reference internal" href="../library/os.html#os.get_blocking" title="os.get_blocking"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_blocking()</span></code></a> and <a class="reference internal" href="../library/os.html#os.set_blocking" title="os.set_blocking"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_blocking()</span></code></a> functions allow
getting and setting a file descriptor's blocking mode (<a class="reference internal" href="../library/os.html#os.O_NONBLOCK" title="os.O_NONBLOCK"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_NONBLOCK</span></code></a>.)
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22054">bpo-22054</a>.)</p>
<p>现在 <a class="reference internal" href="../library/os.html#os.truncate" title="os.truncate"><code class="xref py py-func docutils literal notranslate"><span class="pre">truncate()</span></code></a><a class="reference internal" href="../library/os.html#os.ftruncate" title="os.ftruncate"><code class="xref py py-func docutils literal notranslate"><span class="pre">ftruncate()</span></code></a> 函数在 Windows 上已受到支持。 (由 Steve Dower 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23668">bpo-23668</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/os.path.html#os.path.commonpath" title="os.path.commonpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.commonpath()</span></code></a> 函数可返回所传入的每个路径名的最长共同子路径。 与 <a class="reference internal" href="../library/os.path.html#os.path.commonprefix" title="os.path.commonprefix"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.commonprefix()</span></code></a> 函数不同,该函数总是会返回一个有效的路径:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">commonprefix</span><span class="p">([</span><span class="s1">&#39;/usr/lib&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr/local/lib&#39;</span><span class="p">])</span>
<span class="go">&#39;/usr/l&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">commonpath</span><span class="p">([</span><span class="s1">&#39;/usr/lib&#39;</span><span class="p">,</span> <span class="s1">&#39;/usr/local/lib&#39;</span><span class="p">])</span>
<span class="go">&#39;/usr&#39;</span>
</pre></div>
</div>
<p>(由 Rafik Draoui 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10395">bpo-10395</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.samefile" title="pathlib.Path.samefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.samefile()</span></code></a> 方法可被用来检查一个路径是否与另一个路径指向相同的文件,两个路径可以是 <a class="reference internal" href="../library/pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> 对象,也可以是字符串:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pathlib</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p1</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;/etc/hosts&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p2</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;/etc/../etc/hosts&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p1</span><span class="o">.</span><span class="n">samefile</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>(由 Vajrasky Kok 和 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19775">bpo-19775</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.mkdir" title="pathlib.Path.mkdir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.mkdir()</span></code></a> 方法接受一个新的 <em>exist_ok</em> 可选参数以与 <code class="docutils literal notranslate"><span class="pre">mkdir</span> <span class="pre">-p</span></code><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> 的功能相匹配。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21539">bpo-21539</a> 中贡献。)</p>
<p>新增了一个 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.expanduser" title="pathlib.Path.expanduser"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.expanduser()</span></code></a> 方法用于扩展 <code class="docutils literal notranslate"><span class="pre">~</span></code><code class="docutils literal notranslate"><span class="pre">~user</span></code> 前缀。 (由 Serhiy Storchaka 和 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19776">bpo-19776</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.home" title="pathlib.Path.home"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.home()</span></code></a> 类方法可被用于获取代表用户家目录的 <a class="reference internal" href="../library/pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">Path</span></code></a> 实例。 (由 Victor Salgado 和 Mayank Tripathi 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19777">bpo-19777</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../library/pathlib.html#pathlib.Path.write_text" title="pathlib.Path.write_text"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.write_text()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.read_text" title="pathlib.Path.read_text"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.read_text()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.write_bytes" title="pathlib.Path.write_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.write_bytes()</span></code></a>, <a class="reference internal" href="../library/pathlib.html#pathlib.Path.read_bytes" title="pathlib.Path.read_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Path.read_bytes()</span></code></a> 方法用于简化对文件的读/写操作。</p>
<p>以下代码片段将创建或重写现有的文件 <code class="docutils literal notranslate"><span class="pre">~/spam42</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pathlib</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span><span class="p">(</span><span class="s1">&#39;~/spam42&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span><span class="o">.</span><span class="n">expanduser</span><span class="p">()</span><span class="o">.</span><span class="n">write_text</span><span class="p">(</span><span class="s1">&#39;ham&#39;</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<p>(由 Christopher Welborn 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20218">bpo-20218</a> 中贡献。)</p>
</section>
<section id="pickle">
<h3>pickle<a class="headerlink" href="#pickle" title="永久链接至标题"></a></h3>
<p>嵌套的对象,例如未绑定方法或嵌套的类,现在可以使用早于协议版本 4 的 <a class="reference internal" href="../library/pickle.html#pickle-protocols"><span class="std std-ref">pickle 协议</span></a>。 协议版本 4 在此前已支持此种场景。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23611">bpo-23611</a> 中贡献。)</p>
</section>
<section id="poplib">
<h3>poplib<a class="headerlink" href="#poplib" title="永久链接至标题"></a></h3>
<p>新增 <a class="reference internal" href="../library/poplib.html#poplib.POP3.utf8" title="poplib.POP3.utf8"><code class="xref py py-meth docutils literal notranslate"><span class="pre">POP3.utf8()</span></code></a> 命令可以启用 <span class="target" id="index-140"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6856.html"><strong>RFC 6856</strong></a> (国际化 Email) 支持,如果 POP 服务器支持的话。 (由 Milan OberKirch 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21804">bpo-21804</a> 中贡献。)</p>
</section>
<section id="re">
<h3>re<a class="headerlink" href="#re" title="永久链接至标题"></a></h3>
<p>现在对固定长度分组的引用和有条件引用在后视断言中已被允许:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">re</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pat</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">&#39;(a|b).(?&lt;=\1)c&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pat</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;aac&#39;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object; span=(0, 3), match=&#39;aac&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pat</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;bbc&#39;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object; span=(0, 3), match=&#39;bbc&#39;&gt;</span>
</pre></div>
</div>
<p>(由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9179">bpo-9179</a> 中贡献。)</p>
<p>在正则表达式中捕获分组的数量不再有 100 个的上限。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22437">bpo-22437</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><code class="xref py py-func docutils literal notranslate"><span class="pre">sub()</span></code></a><a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><code class="xref py py-func docutils literal notranslate"><span class="pre">subn()</span></code></a> 函数将用空字符串替换不匹配的分组而不是引发异常。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1519638">bpo-1519638</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/re.html#re.error" title="re.error"><code class="xref py py-class docutils literal notranslate"><span class="pre">re.error</span></code></a> 异常添加了新的属性 <a class="reference internal" href="../library/re.html#re.error.msg" title="re.error.msg"><code class="xref py py-attr docutils literal notranslate"><span class="pre">msg</span></code></a>, <a class="reference internal" href="../library/re.html#re.error.pattern" title="re.error.pattern"><code class="xref py py-attr docutils literal notranslate"><span class="pre">pattern</span></code></a>, <a class="reference internal" href="../library/re.html#re.error.pos" title="re.error.pos"><code class="xref py py-attr docutils literal notranslate"><span class="pre">pos</span></code></a>, <a class="reference internal" href="../library/re.html#re.error.lineno" title="re.error.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a><a class="reference internal" href="../library/re.html#re.error.colno" title="re.error.colno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">colno</span></code></a>,以提供更详细的错误上下文信息:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2"> (?x)</span>
<span class="gp">... </span><span class="s2"> .++</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</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">sre_constants.error</span>: <span class="n">multiple repeat at position 16 (line 3, column 7)</span>
</pre></div>
</div>
<p>(由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22578">bpo-22578</a> 中贡献。)</p>
</section>
<section id="readline">
<h3>readline<a class="headerlink" href="#readline" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/readline.html#readline.append_history_file" title="readline.append_history_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">append_history_file()</span></code></a> 函数可被用于将历史数据中指定数量的末尾元素添加到给定的文件中。 (由 Bruno Cauet 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22940">bpo-22940</a> 中贡献。)</p>
</section>
<section id="selectors">
<h3>selectors<a class="headerlink" href="#selectors" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/selectors.html#selectors.DevpollSelector" title="selectors.DevpollSelector"><code class="xref py py-class docutils literal notranslate"><span class="pre">DevpollSelector</span></code></a> 在 Solaris 上支持高效的 <code class="docutils literal notranslate"><span class="pre">/dev/poll</span></code> 轮询。 (由 Giampaolo Rodola' 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18931">bpo-18931</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.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">move()</span></code></a> 函数现在接受一个 <em>copy_function</em> 参数,举例来说,如果需要在移动文件时忽略其元数据,可以使用 <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">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">copy2()</span></code></a>。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19840">bpo-19840</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">make_archive()</span></code></a> 函数已支持 <em>xztar</em> 格式。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5411">bpo-5411</a> 中贡献。)</p>
</section>
<section id="signal">
<h3>signal<a class="headerlink" href="#signal" title="永久链接至标题"></a></h3>
<p>在 Windows 上,现在 <a class="reference internal" href="../library/signal.html#signal.set_wakeup_fd" title="signal.set_wakeup_fd"><code class="xref py py-func docutils literal notranslate"><span class="pre">set_wakeup_fd()</span></code></a> 函数还可以支持套接字句柄。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22018">bpo-22018</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal notranslate"><span class="pre">signal</span></code></a> 模块中的各个 <code class="docutils literal notranslate"><span class="pre">SIG*</span></code> 常量已被转换为 <a class="reference internal" href="../library/enum.html#module-enum" title="enum: Implementation of an enumeration class."><code class="xref py py-mod docutils literal notranslate"><span class="pre">枚举</span></code></a>。 这允许在调试期间打印有意义的名称,而不是整数类型的“魔法数字”。 (由 Giampaolo Rodola' 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21076">bpo-21076</a> 中贡献。)</p>
</section>
<section id="smtpd">
<h3>smtpd<a class="headerlink" href="#smtpd" title="永久链接至标题"></a></h3>
<p>Both the <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer" title="smtpd.SMTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPServer</span></code></a> and <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPChannel</span></code></a> classes now
accept a <em>decode_data</em> keyword argument to determine if the <code class="docutils literal notranslate"><span class="pre">DATA</span></code> portion of
the SMTP transaction is decoded using the <code class="docutils literal notranslate"><span class="pre">&quot;utf-8&quot;</span></code> codec or is instead
provided to the
<a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer.process_message" title="smtpd.SMTPServer.process_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTPServer.process_message()</span></code></a>
method as a byte string. The default is <code class="docutils literal notranslate"><span class="pre">True</span></code> for backward compatibility
reasons, but will change to <code class="docutils literal notranslate"><span class="pre">False</span></code> in Python 3.6. If <em>decode_data</em> is set
to <code class="docutils literal notranslate"><span class="pre">False</span></code>, the <code class="docutils literal notranslate"><span class="pre">process_message</span></code> method must be prepared to accept keyword
arguments.
(Contributed by Maciej Szulik in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19662">bpo-19662</a>.)</p>
<p>The <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer" title="smtpd.SMTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPServer</span></code></a> class now advertises the <code class="docutils literal notranslate"><span class="pre">8BITMIME</span></code> extension
(<span class="target" id="index-39"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6152.html"><strong>RFC 6152</strong></a>) if <em>decode_data</em> has been set <code class="docutils literal notranslate"><span class="pre">True</span></code>. If the client
specifies <code class="docutils literal notranslate"><span class="pre">BODY=8BITMIME</span></code> on the <code class="docutils literal notranslate"><span class="pre">MAIL</span></code> command, it is passed to
<a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer.process_message" title="smtpd.SMTPServer.process_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTPServer.process_message()</span></code></a>
via the <em>mail_options</em> keyword.
(Contributed by Milan Oberkirch and R. David Murray in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21795">bpo-21795</a>.)</p>
<p>The <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer" title="smtpd.SMTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPServer</span></code></a> class now also supports the <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code>
extension (<span class="target" id="index-40"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6531.html"><strong>RFC 6531</strong></a>: Internationalized Email). If the client specified
<code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span> <span class="pre">BODY=8BITMIME</span></code> on the <code class="docutils literal notranslate"><span class="pre">MAIL</span></code> command, they are passed to
<a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer.process_message" title="smtpd.SMTPServer.process_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTPServer.process_message()</span></code></a>
via the <em>mail_options</em> keyword. It is the responsibility of the
<code class="docutils literal notranslate"><span class="pre">process_message</span></code> method to correctly handle the <code class="docutils literal notranslate"><span class="pre">SMTPUTF8</span></code> data.
(Contributed by Milan Oberkirch in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21725">bpo-21725</a>.)</p>
<p>It is now possible to provide, directly or via name resolution, IPv6
addresses in the <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer" title="smtpd.SMTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPServer</span></code></a> constructor, and have it
successfully connect. (Contributed by Milan Oberkirch in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14758">bpo-14758</a>.)</p>
</section>
<section id="smtplib">
<h3>smtplib<a class="headerlink" href="#smtplib" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.auth" title="smtplib.SMTP.auth"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTP.auth()</span></code></a> 方法为实现自定义身份验证机制提供了一个便捷的方式。 (由 Milan Oberkirch 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15014">bpo-15014</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.set_debuglevel" title="smtplib.SMTP.set_debuglevel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTP.set_debuglevel()</span></code></a> 方法可接受额外的 debuglevel (2),它将在调试消息中启用时间戳。 (由 Gavin Chappell 和 Maciej Szulik 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16914">bpo-16914</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTP.sendmail()</span></code></a><a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SMTP.send_message()</span></code></a> 方法均支持 <span class="target" id="index-141"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc6531.html"><strong>RFC 6531</strong></a> (SMTPUTF8)。 (由 Milan Oberkirch 和 R. David Murray 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22027">bpo-22027</a> 中贡献。)</p>
</section>
<section id="sndhdr">
<h3>sndhdr<a class="headerlink" href="#sndhdr" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/sndhdr.html#sndhdr.what" title="sndhdr.what"><code class="xref py py-func docutils literal notranslate"><span class="pre">what()</span></code></a><a class="reference internal" href="../library/sndhdr.html#sndhdr.whathdr" title="sndhdr.whathdr"><code class="xref py py-func docutils literal notranslate"><span class="pre">whathdr()</span></code></a> 函数将返回 <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">namedtuple()</span></code></a>。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18615">bpo-18615</a> 中贡献。).)</p>
</section>
<section id="socket">
<h3>socket<a class="headerlink" href="#socket" title="永久链接至标题"></a></h3>
<p>现在函数的超时设置将使用单调时钟,而不是系统时钟。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22043">bpo-22043</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/socket.html#socket.socket.sendfile" title="socket.socket.sendfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.sendfile()</span></code></a> 方法允许在 UNIX 上使用高性能的 <a class="reference internal" href="../library/os.html#os.sendfile" title="os.sendfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.sendfile()</span></code></a> 函数通过套接字发送文件,使得上传速度比使用简单的 <a class="reference internal" href="../library/socket.html#socket.socket.send" title="socket.socket.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.send()</span></code></a> 快 2 至 3 倍。 (由 Giampaolo Rodola' 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17552">bpo-17552</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/socket.html#socket.socket.sendall" title="socket.socket.sendall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.sendall()</span></code></a> 方法每次接受或发送字节数据时将不再重置套接字超时。 现在套接字超时将为发送所有数据最大总计持续时间。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23853">bpo-23853</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/socket.html#socket.socket.listen" title="socket.socket.listen"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.listen()</span></code></a> 方法的 <em>backlog</em> 参数将是可选的。 在默认情况下它会被设为 <a class="reference internal" href="../library/socket.html#socket.SOMAXCONN" title="socket.SOMAXCONN"><code class="xref py py-data docutils literal notranslate"><span class="pre">SOMAXCONN</span></code></a><code class="docutils literal notranslate"><span class="pre">128</span></code>,取其中的较小值。 (由 Charles-François Natali 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21455">bpo-21455</a> 中贡献。)</p>
</section>
<section id="ssl">
<h3>ssl<a class="headerlink" href="#ssl" title="永久链接至标题"></a></h3>
<section id="memory-bio-support">
<span id="whatsnew-sslmemorybio"></span><h4>内存 BIO 支持<a class="headerlink" href="#memory-bio-support" title="永久链接至标题"></a></h4>
<p>(由 Geert Jansen 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21965">bpo-21965</a> 中贡献。)</p>
<p>新增了 <a class="reference internal" href="../library/ssl.html#ssl.SSLObject" title="ssl.SSLObject"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLObject</span></code></a> 类以针对 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> 的网络 I/O 能力是非必要或未优化的情况提供 SSL 协议支持。 <code class="docutils literal notranslate"><span class="pre">SSLObject</span></code> 代表一个 SSL 协议实例,但不实现任何网络 I/O 方法,而是提供一个内存缓冲区接口。 新增的 <a class="reference internal" href="../library/ssl.html#ssl.MemoryBIO" title="ssl.MemoryBIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">MemoryBIO</span></code></a> 类可被用于在 Python 和 SSL 协议实例之间传递数据。</p>
<p>内存 BIO SSL 支持主要被用来为对 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> 的就绪模型(&quot;选择/轮询&quot;)来说效率较低的框架实现异步 I/O。</p>
<p>新增的 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_bio" title="ssl.SSLContext.wrap_bio"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.wrap_bio()</span></code></a> 方法可被用于创建新的 <code class="docutils literal notranslate"><span class="pre">SSLObject</span></code> 实例。</p>
</section>
<section id="application-layer-protocol-negotiation-support">
<h4>应用层协议协商支持<a class="headerlink" href="#application-layer-protocol-negotiation-support" title="永久链接至标题"></a></h4>
<p>(由 Benjamin Peterson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20188">bpo-20188</a> 中贡献。)</p>
<p>如果有 OpenSSL 支持,现在 <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> 模块可实现 <span class="target" id="index-142"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7301.html"><strong>RFC 7301</strong></a> 中描述的 <em>应用层协议协商</em> TLS 扩展。</p>
<p>新增的 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.set_alpn_protocols" title="ssl.SSLContext.set_alpn_protocols"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLContext.set_alpn_protocols()</span></code></a> 可被用来指定在 TLS 握手期间套接字所要应用的协议。</p>
<p>The new
<a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.selected_alpn_protocol" title="ssl.SSLSocket.selected_alpn_protocol"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.selected_alpn_protocol()</span></code></a>
returns the protocol that was selected during the TLS handshake.
The <a class="reference internal" href="../library/ssl.html#ssl.HAS_ALPN" title="ssl.HAS_ALPN"><code class="xref py py-data docutils literal notranslate"><span class="pre">HAS_ALPN</span></code></a> flag indicates whether ALPN support is present.</p>
</section>
<section id="other-changes">
<h4>其他改变<a class="headerlink" href="#other-changes" title="永久链接至标题"></a></h4>
<p>新增 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.version" title="ssl.SSLSocket.version"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.version()</span></code></a> 方法以便查询实际使用的协议。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20421">bpo-20421</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> 类已实现了 <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.sendfile()</span></code> 方法。 (由 Giampaolo Rodola' 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17552">bpo-17552</a> 中贡献。)</p>
<p>现在当操作将要阻塞时 <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.send()</span></code> 方法将在非阻塞的套接字上引发 <a class="reference internal" href="../library/ssl.html#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ssl.SSLWantReadError</span></code></a><a class="reference internal" href="../library/ssl.html#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ssl.SSLWantWriteError</span></code></a> 异常。 在之前版本中,它将返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 (由 Nikolaus Rath 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20951">bpo-20951</a> 中贡献。)</p>
<p>根据 <span class="target" id="index-143"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5280.html"><strong>RFC 5280</strong></a>,现在 <a class="reference internal" href="../library/ssl.html#ssl.cert_time_to_seconds" title="ssl.cert_time_to_seconds"><code class="xref py py-func docutils literal notranslate"><span class="pre">cert_time_to_seconds()</span></code></a> 函数会将输入的时间解读为 UTC 而不是本地时间。 此外,其返回值必定为 <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>。 (由 Akira Li 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19940">bpo-19940</a> 中贡献。)</p>
<p>新增的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLObject.shared_ciphers()</span></code><a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.shared_ciphers" title="ssl.SSLSocket.shared_ciphers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.shared_ciphers()</span></code></a> 方法将返回客户端在握手期间中发送的密码列表。 (由 Benjamin Peterson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23186">bpo-23186</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> 类的 <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.do_handshake" title="ssl.SSLSocket.do_handshake"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.do_handshake()</span></code></a>, <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.read" title="ssl.SSLSocket.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.read()</span></code></a>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.shutdown()</span></code><a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.write" title="ssl.SSLSocket.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">SSLSocket.write()</span></code></a> 方法在每次接收或发送字节数据时将不再重置套接字超时。 现在套接字超时将为方法的最长运行时间。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23853">bpo-23853</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/ssl.html#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal notranslate"><span class="pre">match_hostname()</span></code></a> 函数也支持 IP 地址的匹配。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23239">bpo-23239</a> 中贡献。)</p>
</section>
</section>
<section id="sqlite3">
<h3>sqlite3<a class="headerlink" href="#sqlite3" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/sqlite3.html#sqlite3.Row" title="sqlite3.Row"><code class="xref py py-class docutils literal notranslate"><span class="pre">Row</span></code></a> 类可完整支持序列协议,特别是 <a class="reference internal" href="../library/functions.html#reversed" title="reversed"><code class="xref py py-func docutils literal notranslate"><span class="pre">reversed()</span></code></a> 迭代和切片索引。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10203">bpo-10203</a> 中贡献;由 Lucas Sinclair, Jessica McKellar 和 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13583">bpo-13583</a> 中贡献。)</p>
</section>
<section id="subprocess">
<span id="whatsnew-subprocess"></span><h3>subprocess<a class="headerlink" href="#subprocess" title="永久链接至标题"></a></h3>
<p>新增了 <a class="reference internal" href="../library/subprocess.html#subprocess.run" title="subprocess.run"><code class="xref py py-func docutils literal notranslate"><span class="pre">run()</span></code></a> 函数。 它可运行指定的命令并返回一个 <a class="reference internal" href="../library/subprocess.html#subprocess.CompletedProcess" title="subprocess.CompletedProcess"><code class="xref py py-class docutils literal notranslate"><span class="pre">CompletedProcess</span></code></a> 对象,该对象表示已结束的进程。 这个新 API 有更好的一致性并且是在 Python 中发起调用子进程的推荐方式,它不需要维持与更早的 Python 的兼容性。 (由 Thomas Kluyver 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23342">bpo-23342</a> 中贡献。)</p>
<p>示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">&quot;ls&quot;</span><span class="p">,</span> <span class="s2">&quot;-l&quot;</span><span class="p">])</span> <span class="c1"># doesn&#39;t capture output</span>
<span class="go">CompletedProcess(args=[&#39;ls&#39;, &#39;-l&#39;], returncode=0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="s2">&quot;exit 1&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">check</span><span class="o">=</span><span class="kc">True</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">subprocess.CalledProcessError</span>: <span class="n">Command &#39;exit 1&#39; returned non-zero exit status 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">run</span><span class="p">([</span><span class="s2">&quot;ls&quot;</span><span class="p">,</span> <span class="s2">&quot;-l&quot;</span><span class="p">,</span> <span class="s2">&quot;/dev/null&quot;</span><span class="p">],</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
<span class="go">CompletedProcess(args=[&#39;ls&#39;, &#39;-l&#39;, &#39;/dev/null&#39;], returncode=0,</span>
<span class="go">stdout=b&#39;crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n&#39;)</span>
</pre></div>
</div>
</section>
<section id="sys">
<h3>sys<a class="headerlink" href="#sys" title="永久链接至标题"></a></h3>
<p>新增的 <code class="xref py py-func docutils literal notranslate"><span class="pre">set_coroutine_wrapper()</span></code> 函数允许设置一个要在 <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">协程对象</span></a><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> 函数创建时被调用的全局钩子。 可以使用相应的 <code class="xref py py-func docutils literal notranslate"><span class="pre">get_coroutine_wrapper()</span></code> 来获取当前设置的包装器。 这两个函数均为 <a class="reference internal" href="../glossary.html#term-provisional-api"><span class="xref std std-term">暂定状态</span></a>,并应当仅用于调试目的。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24017">bpo-24017</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../library/sys.html#sys.is_finalizing" title="sys.is_finalizing"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_finalizing()</span></code></a> 函数可用于检测 Python 解释器是否 <a class="reference internal" href="../glossary.html#term-interpreter-shutdown"><span class="xref std std-term">正在关闭</span></a>。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22696">bpo-22696</a> 中贡献。)</p>
</section>
<section id="sysconfig">
<h3>sysconfig<a class="headerlink" href="#sysconfig" title="永久链接至标题"></a></h3>
<p>在 Windows 上用户脚本的名称现在将包括 Python 版本号的前两个数字。 (由 Paul Moore 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23437">bpo-23437</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#tarfile.open" title="tarfile.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> 函数的 <em>mode</em> 参数可接受 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> 来请求独占式的创建。 (由 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21717">bpo-21717</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.extractall" title="tarfile.TarFile.extractall"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extractall()</span></code></a><a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.extract" title="tarfile.TarFile.extract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.extract()</span></code></a> 方法可接受关键字参数 <em>numeric_owner</em>。 如果设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>,解压的文件和目录将归属于 tar 文件保存的数字 <code class="docutils literal notranslate"><span class="pre">uid</span></code><code class="docutils literal notranslate"><span class="pre">gid</span></code>。 如果设为 <code class="docutils literal notranslate"><span class="pre">False</span></code> (默认值,也是 3.5 之前版本的行为),则它们将归属于 tar 文件保存的用户和组名称。 (由 Michael Vogt 和 Eric Smith 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23193">bpo-23193</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.list" title="tarfile.TarFile.list"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.list()</span></code></a> 接受可选的 <em>members</em> 关键字参数,它可被设为 <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.getmembers" title="tarfile.TarFile.getmembers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TarFile.getmembers()</span></code></a> 所返回的列表的一个子集。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21549">bpo-21549</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.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Lock.acquire()</span></code></a><a class="reference internal" href="../library/threading.html#threading.RLock.acquire" title="threading.RLock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">RLock.acquire()</span></code></a> 方法均使用单调时钟进行超时管理。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22043">bpo-22043</a> 中贡献。)</p>
</section>
<section id="time">
<h3>time<a class="headerlink" href="#time" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/time.html#time.monotonic" title="time.monotonic"><code class="xref py py-func docutils literal notranslate"><span class="pre">monotonic()</span></code></a> 函数将总是可用。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22043">bpo-22043</a> 中贡献。)</p>
</section>
<section id="timeit">
<h3>timeit<a class="headerlink" href="#timeit" title="永久链接至标题"></a></h3>
<p>新增的命令行选项 <code class="docutils literal notranslate"><span class="pre">-u</span></code><code class="samp docutils literal notranslate"><span class="pre">--unit=</span><em><span class="pre">U</span></em></code> 可被用于指定计时器输出的时间单位。 受支持的选项有 <code class="docutils literal notranslate"><span class="pre">usec</span></code>, <code class="docutils literal notranslate"><span class="pre">msec</span></code><code class="docutils literal notranslate"><span class="pre">sec</span></code>。 (由 Julian Gindi 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18983">bpo-18983</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/timeit.html#timeit.timeit" title="timeit.timeit"><code class="xref py py-func docutils literal notranslate"><span class="pre">timeit()</span></code></a> 函数新增了 <em>globals</em> 形参用于指定代码运行所在的命名空间。 (由 Ben Roberts 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2527">bpo-2527</a> 中贡献。).)</p>
</section>
<section id="tkinter">
<h3>tkinter<a class="headerlink" href="#tkinter" title="永久链接至标题"></a></h3>
<p>用于在 Windows 上设置 Tcl/Tk 环境的 <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter._fix</span></code> 模块已被 <code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code> 模块中的私有函数所替换,该函数不会永久性修改环境变量。 (由 Zachary Ware 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20035">bpo-20035</a> 中贡献。)</p>
</section>
<section id="traceback">
<span id="whatsnew-traceback"></span><h3>回溯<a class="headerlink" href="#traceback" title="永久链接至标题"></a></h3>
<p>New <a class="reference internal" href="../library/traceback.html#traceback.walk_stack" title="traceback.walk_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk_stack()</span></code></a> and <a class="reference internal" href="../library/traceback.html#traceback.walk_tb" title="traceback.walk_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">walk_tb()</span></code></a>
functions to conveniently traverse frame and traceback objects.
(Contributed by Robert Collins in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17911">bpo-17911</a>.)</p>
<p>新增轻量级的类: <a class="reference internal" href="../library/traceback.html#traceback.TracebackException" title="traceback.TracebackException"><code class="xref py py-class docutils literal notranslate"><span class="pre">TracebackException</span></code></a>, <a class="reference internal" href="../library/traceback.html#traceback.StackSummary" title="traceback.StackSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">StackSummary</span></code></a><a class="reference internal" href="../library/traceback.html#traceback.FrameSummary" title="traceback.FrameSummary"><code class="xref py py-class docutils literal notranslate"><span class="pre">FrameSummary</span></code></a>。 (由 Robert Collins 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17911">bpo-17911</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/traceback.html#traceback.print_tb" title="traceback.print_tb"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_tb()</span></code></a><a class="reference internal" href="../library/traceback.html#traceback.print_stack" title="traceback.print_stack"><code class="xref py py-func docutils literal notranslate"><span class="pre">print_stack()</span></code></a> 函数均支持 <em>limit</em> 参数使用负值。 (由 Dmitry Kazakov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22619">bpo-22619</a> 中贡献。).)</p>
</section>
<section id="types">
<h3>types<a class="headerlink" href="#types" title="永久链接至标题"></a></h3>
<p>新增 <a class="reference internal" href="../library/types.html#types.coroutine" title="types.coroutine"><code class="xref py py-func docutils literal notranslate"><span class="pre">coroutine()</span></code></a> 函数用于将 <a class="reference internal" href="../glossary.html#term-generator-iterator"><span class="xref std std-term">生成器</span></a><a class="reference internal" href="../library/collections.abc.html#collections.abc.Generator" title="collections.abc.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">生成器型</span></code></a> 对象转换为 <a class="reference internal" href="../glossary.html#term-awaitable"><span class="xref std std-term">可等待对象</span></a>。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24017">bpo-24017</a> 中贡献。)</p>
<p>新增一个名为 <a class="reference internal" href="../library/types.html#types.CoroutineType" title="types.CoroutineType"><code class="xref py py-class docutils literal notranslate"><span class="pre">CoroutineType</span></code></a> 的类型,用于由 <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> 函数创建的 <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> 对象。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24400">bpo-24400</a> 中贡献。)</p>
</section>
<section id="unicodedata">
<h3>unicodedata<a class="headerlink" href="#unicodedata" title="永久链接至标题"></a></h3>
<p>The <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> module now uses data from <a class="reference external" href="http://unicode.org/versions/Unicode8.0.0/">Unicode 8.0.0</a>.</p>
</section>
<section id="unittest">
<h3>unittest<a class="headerlink" href="#unittest" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromModule" title="unittest.TestLoader.loadTestsFromModule"><code class="xref py py-meth docutils literal notranslate"><span class="pre">TestLoader.loadTestsFromModule()</span></code></a> 方法可接受仅限关键字参数 <em>pattern</em>,它将作为传给 <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> 的第三个参数。 找到的包无论其路径是否匹配 <em>pattern</em> 都会针对 <code class="docutils literal notranslate"><span class="pre">load_tests</span></code> 进行检查,因为包名不可能与默认模式相匹配。 (由 Robert Collins 和 Barry A. Warsaw 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16662">bpo-16662</a> 中贡献。)</p>
<p>单元测试发现错误现在将会暴露为 <a class="reference internal" href="../library/unittest.html#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a><a class="reference internal" href="../library/unittest.html#unittest.TestLoader.errors" title="unittest.TestLoader.errors"><code class="xref py py-data docutils literal notranslate"><span class="pre">TestLoader.errors</span></code></a> 属性。 (由 Robert Collins 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19746">bpo-19746</a> 中贡献。)</p>
<p>新增的命令行选项 <code class="docutils literal notranslate"><span class="pre">--locals</span></code> 可显示回溯中的局部变量。 (由 Robert Collins 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22936">bpo-22936</a> 中贡献。)</p>
</section>
<section id="unittest-mock">
<h3>unittest.mock<a class="headerlink" href="#unittest-mock" title="永久链接至标题"></a></h3>
<p><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> 类具有以下改进:</p>
<ul class="simple">
<li><p>类构造器新增了 <em>unsafe</em> 形参,它可导致模拟对象在名称以 <code class="docutils literal notranslate"><span class="pre">&quot;assert&quot;</span></code> 打头的属性名上引发 <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>。 (由 Kushal Das 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21238">bpo-21238</a> 中贡献。)</p></li>
<li><p>新增了 <a class="reference internal" href="../library/unittest.mock.html#unittest.mock.Mock.assert_not_called" title="unittest.mock.Mock.assert_not_called"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Mock.assert_not_called()</span></code></a> 方法用于检测模拟对象是否已被调用。 (由 Kushal Das 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21262">bpo-21262</a> 中贡献。)</p></li>
</ul>
<p>现在 <a class="reference internal" href="../library/unittest.mock.html#unittest.mock.MagicMock" title="unittest.mock.MagicMock"><code class="xref py py-class docutils literal notranslate"><span class="pre">MagicMock</span></code></a> 类已支持 <a class="reference internal" href="../reference/datamodel.html#object.__truediv__" title="object.__truediv__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__truediv__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__divmod__" title="object.__divmod__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__divmod__()</span></code></a><a class="reference internal" href="../reference/datamodel.html#object.__matmul__" title="object.__matmul__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__matmul__()</span></code></a> 运算符。 (由 Johannes Baiter 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20968">bpo-20968</a> 中贡献,并由 Håkan Lövdahl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23581">bpo-23581</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23568">bpo-23568</a> 中贡献。)</p>
<p>在对内置名称打补丁时不再需要显式地将 <code class="docutils literal notranslate"><span class="pre">create=True</span></code> 传给 <a class="reference internal" href="../library/unittest.mock.html#unittest.mock.patch" title="unittest.mock.patch"><code class="xref py py-func docutils literal notranslate"><span class="pre">patch()</span></code></a> 函数。 (由 Kushal Das 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=17660">bpo-17660</a> 中贡献。)</p>
</section>
<section id="urllib">
<h3>urllib<a class="headerlink" href="#urllib" title="永久链接至标题"></a></h3>
<p>新增的 <a class="reference internal" href="../library/urllib.request.html#urllib.request.HTTPPasswordMgrWithPriorAuth" title="urllib.request.HTTPPasswordMgrWithPriorAuth"><code class="xref py py-class docutils literal notranslate"><span class="pre">request.HTTPPasswordMgrWithPriorAuth</span></code></a> 类允许对 HTTP 基本认证凭据进行管理以消除不必要的 <code class="docutils literal notranslate"><span class="pre">401</span></code> 响应处理,或在第一次请求时无条件地发送凭据,以便与返回 <code class="docutils literal notranslate"><span class="pre">404</span></code> 响应的服务器通信而不是在未发送 <code class="docutils literal notranslate"><span class="pre">Authorization</span></code> 头信息的情况下发送 <code class="docutils literal notranslate"><span class="pre">401</span></code>。 (由 Matej Cepl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19494">bpo-19494</a> 以及 Akshit Khurana 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7159">bpo-7159</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse.urlencode()</span></code></a> 函数新增的 <em>quote_via</em> 参数提供了一种在需要时控制查询部分的编码格式的手段。 (由 Samwyse 和 Arnon Yaari 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13866">bpo-13866</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">request.urlopen()</span></code></a> 函数接受 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 对象作为 <em>context</em> 参数,它将被用于 HTTPS 连接。 (由 Alex Gaynor 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22366">bpo-22366</a> 中贡献。).)</p>
<p><a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urljoin" title="urllib.parse.urljoin"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse.urljoin()</span></code></a> 已获得更新已使用 <span class="target" id="index-144"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a> 语义来解析相对 URL而不是 <span class="target" id="index-145"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1808.html"><strong>RFC 1808</strong></a><span class="target" id="index-146"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2396.html"><strong>RFC 2396</strong></a>。 (由 Demian Brecht 和 Senthil Kumaran 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22118">bpo-22118</a> 中贡献。)</p>
</section>
<section id="wsgiref">
<h3>wsgiref<a class="headerlink" href="#wsgiref" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/wsgiref.html#wsgiref.headers.Headers" title="wsgiref.headers.Headers"><code class="xref py py-class docutils literal notranslate"><span class="pre">headers.Headers</span></code></a> 类构造器的 <em>headers</em> 参数将是可选的。 (由 Pablo Torres Navarrete 和 SilentGhost 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5800">bpo-5800</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">client.ServerProxy</span></code></a> 类已支持 <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> 协议。 (由 Claudiu Popa 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20627">bpo-20627</a> 中贡献。)</p>
<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">client.ServerProxy</span></code></a> 构造器接受可选的 <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 实例。 (由 Alex Gaynor 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22960">bpo-22960</a> 中贡献。)</p>
</section>
<section id="xml-sax">
<h3>xml.sax<a class="headerlink" href="#xml-sax" title="永久链接至标题"></a></h3>
<p>现在 SAX 解析器已支持 <a class="reference internal" href="../library/xml.sax.reader.html#xml.sax.xmlreader.InputSource" title="xml.sax.xmlreader.InputSource"><code class="xref py py-class docutils literal notranslate"><span class="pre">xmlreader.InputSource</span></code></a> 对象的字符流。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2175">bpo-2175</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/xml.sax.html#xml.sax.parseString" title="xml.sax.parseString"><code class="xref py py-func docutils literal notranslate"><span class="pre">parseString()</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> 实例。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10590">bpo-10590</a> 中贡献。)</p>
</section>
<section id="zipfile">
<h3>zipfile<a class="headerlink" href="#zipfile" title="永久链接至标题"></a></h3>
<p>现在可以将 ZIP 输出写入到不可定位的流。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23252">bpo-23252</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ZipFile.open()</span></code></a> 方法的 <em>mode</em> 参数可接受 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> 来请求独占式的创建。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21717">bpo-21717</a> 中贡献。)</p>
</section>
</section>
<section id="other-module-level-changes">
<h2>其他模块级更改<a class="headerlink" href="#other-module-level-changes" title="永久链接至标题"></a></h2>
<p>现在 <a class="reference internal" href="../library/mmap.html#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mmap</span></code></a>, <a class="reference internal" href="../library/ossaudiodev.html#module-ossaudiodev" title="ossaudiodev: Access to OSS-compatible audio devices. (Linux, FreeBSD)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ossaudiodev</span></code></a>, <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>, <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a><a class="reference internal" href="../library/codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">codecs</span></code></a> 模块中的许多函数都接受可写的 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">字节型对象</span></a>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23001">bpo-23001</a> 中贡献。)</p>
</section>
<section id="optimizations">
<h2>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="../library/os.html#os.walk" title="os.walk"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.walk()</span></code></a> 函数在 POSIX 系统上可提速 3 至 5 倍,而在 Windows 上可提速 7 至 20 倍。 这是因为使用了新的 <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> 函数,它可以暴露来自下层 <code class="docutils literal notranslate"><span class="pre">readdir</span></code><code class="docutils literal notranslate"><span class="pre">FindFirstFile</span></code>/<code class="docutils literal notranslate"><span class="pre">FindNextFile</span></code> 系统调用的文件信息。 (由 Ben Hoyt 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23605">bpo-23605</a> 中贡献并得到 Victor Stinner 的协助。)</p>
<p>对于大对象 <code class="docutils literal notranslate"><span class="pre">bytes(int)</span></code> (以零字节填充) 的构建速度更快且使用更少内存。 将使用 <code class="docutils literal notranslate"><span class="pre">calloc()</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">malloc()</span></code> 为这些对象分配内存。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21233">bpo-21233</a> 中贡献。)</p>
<p><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> 中的 <a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv4Network" title="ipaddress.IPv4Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv4Network</span></code></a><a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv6Network" title="ipaddress.IPv6Network"><code class="xref py py-class docutils literal notranslate"><span class="pre">IPv6Network</span></code></a> 等操作获得了大幅度的加速,比如 <a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv4Network.subnets" title="ipaddress.IPv4Network.subnets"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subnets()</span></code></a>, <a class="reference internal" href="../library/ipaddress.html#ipaddress.IPv4Network.supernet" title="ipaddress.IPv4Network.supernet"><code class="xref py py-meth docutils literal notranslate"><span class="pre">supernet()</span></code></a>, <a class="reference internal" href="../library/ipaddress.html#ipaddress.summarize_address_range" title="ipaddress.summarize_address_range"><code class="xref py py-func docutils literal notranslate"><span class="pre">summarize_address_range()</span></code></a>, <a class="reference internal" href="../library/ipaddress.html#ipaddress.collapse_addresses" title="ipaddress.collapse_addresses"><code class="xref py py-func docutils literal notranslate"><span class="pre">collapse_addresses()</span></code></a>。 加速幅度从 3 到 15 倍不等。 (由 Antoine Pitrou, Michel Albert 和 Markus 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21486">bpo-21486</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21487">bpo-21487</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20826">bpo-20826</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23266">bpo-23266</a> 中贡献。)</p>
<p><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> 对象的封存操作获得了优化以产生显著减小的输出。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23133">bpo-23133</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> 上的许多操作可提速 50% 至 100%。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15381">bpo-15381</a> 以及 David Wilson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22003">bpo-22003</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/marshal.html#marshal.dumps" title="marshal.dumps"><code class="xref py py-func docutils literal notranslate"><span class="pre">marshal.dumps()</span></code></a> 函数速度可提升:对于典型数据来说版本 3 和 4 为 65--85%,版本 0 至 2 为 20--25%,最佳场景下最多可达 5 倍。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20416">bpo-20416</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23344">bpo-23344</a> 中贡献。)</p>
<p>现在 UTF-32 编码器可提速 3 至 7 倍。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15027">bpo-15027</a> 中贡献。)</p>
<p>现在正则表达式的解析可提速至多 10%。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19380">bpo-19380</a> 中贡献。).)</p>
<p><a class="reference internal" href="../library/json.html#json.dumps" title="json.dumps"><code class="xref py py-func docutils literal notranslate"><span class="pre">json.dumps()</span></code></a> 函数已获优化使得设置 <code class="docutils literal notranslate"><span class="pre">ensure_ascii=False</span></code> 时与设置 <code class="docutils literal notranslate"><span class="pre">ensure_ascii=True</span></code> 时运行得一样快。 (由 Naoki Inada 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23206">bpo-23206</a> 中贡献。)</p>
<p><a class="reference internal" href="../c-api/object.html#c.PyObject_IsInstance" title="PyObject_IsInstance"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_IsInstance()</span></code></a><a class="reference internal" href="../c-api/object.html#c.PyObject_IsSubclass" title="PyObject_IsSubclass"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_IsSubclass()</span></code></a> 函数在第二个参数以 <a class="reference internal" href="../library/functions.html#type" title="type"><code class="xref py py-class docutils literal notranslate"><span class="pre">type</span></code></a> 作为其元类的常见情况下已获得加速。 (由 Georg Brandl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22540">bpo-22540</a> 中贡献。)</p>
<p>方法缓存操作已获少量改进,在某些基准测试中可产生至多 5% 的性能提升。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22847">bpo-22847</a> 中贡献。)</p>
<p>现在来自 <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> 模块的对象在 64 位编译版上使用的内存将减少 50%。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23488">bpo-23488</a> 中贡献。)</p>
<p><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> getter 调用可提速 25%。 (由 Joe Jevnik 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23910">bpo-23910</a> 中贡献。)</p>
<p>现在 <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> 的实例化可提速至多 30%。 (由 Stefan Behnel 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22464">bpo-22464</a> 中贡献。)</p>
<p>现在字符串方法 <a class="reference internal" href="../library/stdtypes.html#str.find" title="str.find"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.rfind" title="str.rfind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rfind()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.split" title="str.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.partition" title="str.partition"><code class="xref py py-meth docutils literal notranslate"><span class="pre">partition()</span></code></a><a class="reference internal" href="../reference/expressions.html#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> 字符串运算符在搜索 1 个字符的子字符串时将显著提速。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23573">bpo-23573</a> 中贡献。)</p>
</section>
<section id="build-and-c-api-changes">
<h2>构建和 C API 的改变<a class="headerlink" href="#build-and-c-api-changes" title="永久链接至标题"></a></h2>
<p>增加了 <code class="docutils literal notranslate"><span class="pre">calloc</span></code> 函数</p>
<ul class="simple">
<li><p><a class="reference internal" href="../c-api/memory.html#c.PyMem_RawCalloc" title="PyMem_RawCalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawCalloc()</span></code></a>,</p></li>
<li><p><a class="reference internal" href="../c-api/memory.html#c.PyMem_Calloc" title="PyMem_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_Calloc()</span></code></a>,</p></li>
<li><p><a class="reference internal" href="../c-api/memory.html#c.PyObject_Calloc" title="PyObject_Calloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Calloc()</span></code></a>.</p></li>
</ul>
<p>(Victor Stinner 贡献于 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21233">bpo-21233</a>.)</p>
<p>新的 encoding/decoding 帮助函数:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../c-api/sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> (替代 <code class="docutils literal notranslate"><span class="pre">_Py_char2wchar()</span></code>),</p></li>
<li><p><a class="reference internal" href="../c-api/sys.html#c.Py_EncodeLocale" title="Py_EncodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_EncodeLocale()</span></code></a> (替代 <code class="docutils literal notranslate"><span class="pre">_Py_wchar2char()</span></code>).</p></li>
</ul>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18395">bpo-18395</a> 中贡献。)</p>
<p>新增的 <a class="reference internal" href="../c-api/codec.html#c.PyCodec_NameReplaceErrors" title="PyCodec_NameReplaceErrors"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCodec_NameReplaceErrors()</span></code></a> 函数可以将 unicode 编码错误替换为 <code class="docutils literal notranslate"><span class="pre">\N{...}</span></code> 转义符号。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19676">bpo-19676</a> 中贡献。)</p>
<p>A new <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_FormatV" title="PyErr_FormatV"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_FormatV()</span></code></a> function similar to <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_Format" title="PyErr_Format"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Format()</span></code></a>,
but accepts a <code class="docutils literal notranslate"><span class="pre">va_list</span></code> argument.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=18711">bpo-18711</a>.)</p>
<p>新增 <code class="xref c c-data docutils literal notranslate"><span class="pre">PyExc_RecursionError</span></code> 异常。 (由 Georg Brandl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19235">bpo-19235</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../c-api/module.html#c.PyModule_FromDefAndSpec" title="PyModule_FromDefAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec()</span></code></a>, <a class="reference internal" href="../c-api/module.html#c.PyModule_FromDefAndSpec2" title="PyModule_FromDefAndSpec2"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec2()</span></code></a><a class="reference internal" href="../c-api/module.html#c.PyModule_ExecDef" title="PyModule_ExecDef"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_ExecDef()</span></code></a> 函数,来自 <span class="target" id="index-147"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a> -- 多阶段扩展模块初始化。 (由 Petr Viktorin 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24268">bpo-24268</a> 中贡献。)</p>
<p>新增 <a class="reference internal" href="../c-api/number.html#c.PyNumber_MatrixMultiply" title="PyNumber_MatrixMultiply"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_MatrixMultiply()</span></code></a><a class="reference internal" href="../c-api/number.html#c.PyNumber_InPlaceMatrixMultiply" title="PyNumber_InPlaceMatrixMultiply"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyNumber_InPlaceMatrixMultiply()</span></code></a> 函数用于执行矩阵乘法。 (由 Benjamin Peterson 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21176">bpo-21176</a> 中贡献。 另请参阅 <span class="target" id="index-148"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0465"><strong>PEP 465</strong></a> 了解详情。)</p>
<p>现在 <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">PyTypeObject.tp_finalize</span></code></a> 槽位已成为稳定 ABI 的一部分。</p>
<p>Windows builds now require Microsoft Visual C++ 14.0, which
is available as part of <a class="reference external" href="https://www.visualstudio.com/">Visual Studio 2015</a>.</p>
<p>现在扩展模块在某些平台上的文件名将包括平台信息标签(该标签是可选的,即使没有它 CPython 也会导入扩展,但是如果存在不匹配的标签,扩展将不会被加载):</p>
<ul class="simple">
<li><p>在 Linux 上,扩展模块文件名将以 <code class="docutils literal notranslate"><span class="pre">.cpython-&lt;major&gt;&lt;minor&gt;m-&lt;architecture&gt;-&lt;os&gt;.pyd</span></code> 结束:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;major&gt;</span></code> 为 Python 主版本号;对于 Python 3.5 即为 <code class="docutils literal notranslate"><span class="pre">3</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;minor&gt;</span></code> 为 Python 次版本号;对于 Python 3.5 即为 <code class="docutils literal notranslate"><span class="pre">5</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;architecture&gt;</span></code> 为扩展模块构建在运行时所使用的硬件架构。 最常见的值是 <code class="docutils literal notranslate"><span class="pre">i386</span></code> 表示 32 位 Intel 平台或 <code class="docutils literal notranslate"><span class="pre">x86_64</span></code> 表示 64 位 Intel (和 AMD) 平台。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;os&gt;</span></code> 将始终为 <code class="docutils literal notranslate"><span class="pre">linux-gnu</span></code>,除非扩展构建是针对 64 位平台上的 32 位 ABI在此情况下它将为 <code class="docutils literal notranslate"><span class="pre">linux-gnu32</span></code> (而 <code class="docutils literal notranslate"><span class="pre">&lt;architecture&gt;</span></code> 将为 <code class="docutils literal notranslate"><span class="pre">x86_64</span></code>)。</p></li>
</ul>
</li>
<li><p>在 Windows 上,扩展模块文件名将以 <code class="docutils literal notranslate"><span class="pre">&lt;debug&gt;.cp&lt;major&gt;&lt;minor&gt;-&lt;platform&gt;.pyd</span></code> 结束:</p>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;major&gt;</span></code> 为 Python 主版本号;对于 Python 3.5 即为 <code class="docutils literal notranslate"><span class="pre">3</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;minor&gt;</span></code> 为 Python 次版本号;对于 Python 3.5 即为 <code class="docutils literal notranslate"><span class="pre">5</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">&lt;platform&gt;</span></code> 是扩展模块构建所针对的平台,可以为 <code class="docutils literal notranslate"><span class="pre">win32</span></code> 表示 Win32<code class="docutils literal notranslate"><span class="pre">win_amd64</span></code> 表示 Win64<code class="docutils literal notranslate"><span class="pre">win_ia64</span></code> 表示 Windows Itanium 64<code class="docutils literal notranslate"><span class="pre">win_arm</span></code> 表示 ARM 版 Windows。</p></li>
<li><p>如果以调试模式构建,则 <code class="docutils literal notranslate"><span class="pre">&lt;debug&gt;</span></code> 将为 <code class="docutils literal notranslate"><span class="pre">_d</span></code>,在其他情况下将为空白。</p></li>
</ul>
</li>
<li><p>在 OS X 平台上,扩展模块文件名现在将以 <code class="docutils literal notranslate"><span class="pre">-darwin.so</span></code> 结束。</p></li>
<li><p>在所有其他平台上,扩展模块文件名与 Python 3.4 时一样。</p></li>
</ul>
</section>
<section id="deprecated">
<h2>弃用<a class="headerlink" href="#deprecated" title="永久链接至标题"></a></h2>
<section id="new-keywords">
<h3>新关键字<a class="headerlink" href="#new-keywords" title="永久链接至标题"></a></h3>
<p>不建议将 <code class="docutils literal notranslate"><span class="pre">async</span></code><code class="docutils literal notranslate"><span class="pre">await</span></code> 用作变量、类、函数或模块的名称。 它们根据 <span class="target" id="index-149"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> 在 Python 3.5 中被引入,并将在 Python 3.7 成为保留关键字。</p>
</section>
<section id="deprecated-python-behavior">
<h3>已弃用的 Python 行为<a class="headerlink" href="#deprecated-python-behavior" title="永久链接至标题"></a></h3>
<p>在生成器内部引发 <a class="reference internal" href="../library/exceptions.html#StopIteration" title="StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</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.6 中改为非静默的弃用警告并在 Python 3.7 中触发 <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>。 请参阅 <a class="reference internal" href="#whatsnew-pep-479"><span class="std std-ref">PEP 479: 修改生成器内部的 StopIteration 处理方式</span></a> 了解详情。</p>
</section>
<section id="unsupported-operating-systems">
<h3>不支持的操作系统<a class="headerlink" href="#unsupported-operating-systems" title="永久链接至标题"></a></h3>
<p>Windows XP 已不再被 Microsoft 所支持,因此根据 <span class="target" id="index-150"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0011"><strong>PEP 11</strong></a>CPython 3.5 不再对该操作系统提供官方支持。</p>
</section>
<section id="deprecated-python-modules-functions-and-methods">
<h3>已弃用的 Python 模块、函数和方法<a class="headerlink" href="#deprecated-python-modules-functions-and-methods" title="永久链接至标题"></a></h3>
<p>现在 <a class="reference internal" href="../library/formatter.html#module-formatter" title="formatter: Generic output formatter and device interface. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">formatter</span></code></a> 模块状态已转为完全弃用并仍计划在 Python 3.6 中移除。</p>
<p><code class="xref py py-func docutils literal notranslate"><span class="pre">asyncio.async()</span></code> 函数已被弃用并由 of <a class="reference internal" href="../library/asyncio-future.html#asyncio.ensure_future" title="asyncio.ensure_future"><code class="xref py py-func docutils literal notranslate"><span class="pre">ensure_future()</span></code></a> 取代。</p>
<p>The <a class="reference internal" href="../library/smtpd.html#module-smtpd" title="smtpd: A SMTP server implementation in Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtpd</span></code></a> module has in the past always decoded the DATA portion of
email messages using the <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> codec. This can now be controlled by the
new <em>decode_data</em> keyword to <a class="reference internal" href="../library/smtpd.html#smtpd.SMTPServer" title="smtpd.SMTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTPServer</span></code></a>. The default value is
<code class="docutils literal notranslate"><span class="pre">True</span></code>, but this default is deprecated. Specify the <em>decode_data</em> keyword
with an appropriate value to avoid the deprecation warning.</p>
<p>直接向 <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.cookies.Morsel</span></code></a> 对象的 <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.key" title="http.cookies.Morsel.key"><code class="xref py py-attr docutils literal notranslate"><span class="pre">key</span></code></a>, <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.value" title="http.cookies.Morsel.value"><code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code></a><a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.coded_value" title="http.cookies.Morsel.coded_value"><code class="xref py py-attr docutils literal notranslate"><span class="pre">coded_value</span></code></a> 赋值操作已弃用。 请改用 <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a>。 此外,<a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> 未写入文档的 <em>LegalChars</em> 形参也已弃用,现在将会被忽略。</p>
<p>将格式字符串作为关键字参数 <em>format_string</em> 传给 <a class="reference internal" href="../library/string.html#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Formatter</span></code></a> 类的 <a class="reference internal" href="../library/string.html#string.Formatter.format" title="string.Formatter.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> 方法的操作已被弃用。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23671">bpo-23671</a> 贡献。)</p>
<p>现在 <code class="xref py py-func docutils literal notranslate"><span class="pre">platform.dist()</span></code><code class="xref py py-func docutils literal notranslate"><span class="pre">platform.linux_distribution()</span></code> 函数已被弃用。 各种 Linux 发行版使用太多不同的方式来描述自己,所以此功能将留给具体的包来实现。 (由 Vajrasky Kok 和 Berker Peksag 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1322">bpo-1322</a> 中贡献。)</p>
<p>之前未写入文档的 <a class="reference internal" href="../library/inspect.html#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">inspect.Signature</span></code></a><code class="docutils literal notranslate"><span class="pre">from_function</span></code><code class="docutils literal notranslate"><span class="pre">from_builtin</span></code> 方法已被弃用。 请改用新的 <a class="reference internal" href="../library/inspect.html#inspect.Signature.from_callable" title="inspect.Signature.from_callable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.from_callable()</span></code></a> 方法。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24248">bpo-24248</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/inspect.html#inspect.getargspec" title="inspect.getargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getargspec()</span></code></a> 函数已被弃用并计划在 Python 3.6 中移除。 (请参阅 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20438">bpo-20438</a> 了解详情。)</p>
<p><a class="reference internal" href="../library/inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.getfullargspec" title="inspect.getfullargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">getfullargspec()</span></code></a>, <a class="reference internal" href="../library/inspect.html#inspect.getcallargs" title="inspect.getcallargs"><code class="xref py py-func docutils literal notranslate"><span class="pre">getcallargs()</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.formatargspec" title="inspect.formatargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">formatargspec()</span></code></a> 函数已被弃用而应改用 <a class="reference internal" href="../library/inspect.html#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.signature()</span></code></a> API。 (由 Yury Selivanov 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20438">bpo-20438</a> 中贡献。)</p>
<p>在 Python 3.5.0 发布时 <a class="reference internal" href="../library/inspect.html#inspect.getargvalues" title="inspect.getargvalues"><code class="xref py py-func docutils literal notranslate"><span class="pre">getargvalues()</span></code></a><a class="reference internal" href="../library/inspect.html#inspect.formatargvalues" title="inspect.formatargvalues"><code class="xref py py-func docutils literal notranslate"><span class="pre">formatargvalues()</span></code></a> 函数被误标记为已弃用。</p>
<p>在 str 模式中使用 <a class="reference internal" href="../library/re.html#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal notranslate"><span class="pre">re.LOCALE</span></code></a> 旗标或 <a class="reference internal" href="../library/re.html#re.ASCII" title="re.ASCII"><code class="xref py py-const docutils literal notranslate"><span class="pre">re.ASCII</span></code></a> 的做法现在已被弃用。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22407">bpo-22407</a> 中贡献。)</p>
<p>在正则表达式模式和替换模式中使用不可识别的由 <code class="docutils literal notranslate"><span class="pre">'\'</span></code> 加一个 ASCII 字母组成的序列的做法现在会引发弃用警告并将在 Python 3.6 中被禁止。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23622">bpo-23622</a> 中贡献。)</p>
<p>现在 <a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromModule" title="unittest.TestLoader.loadTestsFromModule"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unittest.TestLoader.loadTestsFromModule()</span></code></a> 方法未写入文档的非官方 <em>use_load_tests</em> 默认参数已被弃用并会被忽略。 (由 Robert Collins 和 Barry A. Warsaw 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16662">bpo-16662</a> 中贡献。)</p>
</section>
</section>
<section id="removed">
<h2>移除<a class="headerlink" href="#removed" title="永久链接至标题"></a></h2>
<section id="api-and-feature-removals">
<h3>API 与特性的移除<a class="headerlink" href="#api-and-feature-removals" title="永久链接至标题"></a></h3>
<p>以下过时并在之前版本中弃用的 API 和特性现已被移除:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">__version__</span></code> 属性已从 mail 包中被去除。 email 包的代码已经很久没有与 stdlib 分开发布了,而 <code class="docutils literal notranslate"><span class="pre">__version__</span></code> 字符串在最近几次发布版中也没有被更新。</p></li>
<li><p><a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> 模块中的内部 <code class="docutils literal notranslate"><span class="pre">Netrc</span></code> 类在 3.4 中被弃用,现在已被移除。 (由 Matt Chaput 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6623">bpo-6623</a> 中贡献。)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">.pyo</span></code> 文件的概念已被移除。</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> 模块中的 JoinableQueue 类在 3.4.4 中被弃用,现在已被移除。 (由 A. Jesse Jiryu Davis 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23464">bpo-23464</a> 中贡献。)</p></li>
</ul>
</section>
</section>
<section id="porting-to-python-3-5">
<h2>移植到Python 3.5<a class="headerlink" href="#porting-to-python-3-5" 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>
<li><p>由于一个疏忽,之前的 Python 版本会错误地接受以下语法:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="n">f</span><span class="p">(</span><span class="mi">1</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
<p>对于生成器表达式不是传给函数的唯一参数时必须放在圆括号内的情况Python 3.5 现在会正确地引发 <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a></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><span class="target" id="index-151"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0475"><strong>PEP 475</strong></a>: 现在当系统调用被信号中断时如果 Python 信号处理器没有引发异常则会执行重试而不是引发 <a class="reference internal" href="../library/exceptions.html#InterruptedError" title="InterruptedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InterruptedError</span></code></a></p></li>
<li><p>在 Python 3.5 之前,如果一个 <a class="reference internal" href="../library/datetime.html#datetime.time" title="datetime.time"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.time</span></code></a> 对象是表示 UTC 时间的午夜则会被视为假值。 此行为被认为容易造成困惑和错误因而在 Python 3.5 中已被去除。 详情参见 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13936">bpo-13936</a></p></li>
<li><p>现在当操作将要阻塞时 <code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLSocket.send()</span></code> 方法将在非阻塞的套接字上引发 <a class="reference internal" href="../library/ssl.html#ssl.SSLWantReadError" title="ssl.SSLWantReadError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ssl.SSLWantReadError</span></code></a><a class="reference internal" href="../library/ssl.html#ssl.SSLWantWriteError" title="ssl.SSLWantWriteError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ssl.SSLWantWriteError</span></code></a>。 在之前版本中,它将返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。 (由 Nikolaus Rath 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20951">bpo-20951</a> 中贡献。)</p></li>
<li><p>现在生成器的 <code class="docutils literal notranslate"><span class="pre">__name__</span></code> 属性是根据函数名设置的,而不是根据代码名设置的。 请使用 <code class="docutils literal notranslate"><span class="pre">gen.gi_code.co_name</span></code> 来获取代码名。 生成器还有一个新的 <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code> 属性,即限定名称,它现在用于生成器的表示形式 (<code class="docutils literal notranslate"><span class="pre">repr(gen)</span></code>)。 (由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21205">bpo-21205</a> 中贡献。)</p></li>
<li><p>The deprecated &quot;strict&quot; mode and argument of <a class="reference internal" href="../library/html.parser.html#html.parser.HTMLParser" title="html.parser.HTMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTMLParser</span></code></a>,
<code class="xref py py-meth docutils literal notranslate"><span class="pre">HTMLParser.error()</span></code>, and the <code class="xref py py-exc docutils literal notranslate"><span class="pre">HTMLParserError</span></code> exception have been
removed. (Contributed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15114">bpo-15114</a>.)
The <em>convert_charrefs</em> argument of <a class="reference internal" href="../library/html.parser.html#html.parser.HTMLParser" title="html.parser.HTMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTMLParser</span></code></a> is
now <code class="docutils literal notranslate"><span class="pre">True</span></code> by default. (Contributed by Berker Peksag in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21047">bpo-21047</a>.)</p></li>
<li><p>虽然不是 API 的正式组成部分,但对于移植目的(例如:修复测试)来说需要注意之前 &quot;'sometype' does not support the buffer protocol&quot; 形式的错误消息现在将为 &quot;a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> is required, not 'sometype'&quot; 形式。 (由 Ezio Melotti 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=16518">bpo-16518</a> 中贡献。)</p></li>
<li><p>在当前目录被设为已不存在的目录时将不再引发 <a class="reference internal" href="../library/exceptions.html#FileNotFoundError" title="FileNotFoundError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileNotFoundError</span></code></a> 而是改为 <a class="reference internal" href="../library/importlib.html#importlib.machinery.FileFinder.find_spec" title="importlib.machinery.FileFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> 返回 <code class="docutils literal notranslate"><span class="pre">None</span></code> 并且 <strong>不在</strong> <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> 中缓存 <code class="docutils literal notranslate"><span class="pre">None</span></code>,这与典型的场景存在区别 (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22834">bpo-22834</a>)。</p></li>
<li><p><a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a><a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a> 中的 HTTP 状态码和消息被重构为一个通用的 <a class="reference internal" href="../library/http.html#http.HTTPStatus" title="http.HTTPStatus"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPStatus</span></code></a> 枚举。 <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a><a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a> 中的值仍可被用于向下兼容性。 (由 Demian Brecht 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21793">bpo-21793</a> 中贡献。)</p></li>
<li><p>现在当一个导入加载器定义了 <code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.machinery.Loader.exec_module()</span></code> 时它也应该定义 <code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</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>,在 Python 3.6 中将引发错误)。 如果加载器是继承自 <a class="reference internal" href="../library/importlib.html#importlib.abc.Loader" title="importlib.abc.Loader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.Loader</span></code></a> 那么就不需要做什么,在其他情况下只须定义 <code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code> 来返回 <code class="docutils literal notranslate"><span class="pre">None</span></code>。 (由 Brett Cannon 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23014">bpo-23014</a> 中贡献。)</p></li>
<li><p><a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.split()</span></code></a> 函数总是会忽略空的模式匹配,因此 <code class="docutils literal notranslate"><span class="pre">&quot;x*&quot;</span></code> 模式的效果与 <code class="docutils literal notranslate"><span class="pre">&quot;x+&quot;</span></code> 相同,而 <code class="docutils literal notranslate"><span class="pre">&quot;\b&quot;</span></code> 模式则没有效果。 现在当模式将匹配空字符串时 <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.split()</span></code></a> 会引发一个警告。 为保持兼容性,请使用绝不会匹配空字符串的模式 (例如使用 <code class="docutils literal notranslate"><span class="pre">&quot;x+&quot;</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">&quot;x*&quot;</span></code>)。 只匹配空字符串的模式 (例如 <code class="docutils literal notranslate"><span class="pre">&quot;\b&quot;</span></code>) 现在会引发一个错误。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22818">bpo-22818</a> 中贡献。)</p></li>
<li><p><a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.cookies.Morsel</span></code></a> 字典型接口已更为自洽morsel 比较现在会将 <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.key" title="http.cookies.Morsel.key"><code class="xref py py-attr docutils literal notranslate"><span class="pre">key</span></code></a><a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.value" title="http.cookies.Morsel.value"><code class="xref py py-attr docutils literal notranslate"><span class="pre">value</span></code></a> 都纳入考虑,<a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.copy" title="http.cookies.Morsel.copy"><code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code></a> 现在将得到一个 <a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel" title="http.cookies.Morsel"><code class="xref py py-class docutils literal notranslate"><span class="pre">Morsel</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/http.cookies.html#http.cookies.Morsel.update" title="http.cookies.Morsel.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> 现在当更新字典中的任一个键无效时将引发一个异常。 此外,<a class="reference internal" href="../library/http.cookies.html#http.cookies.Morsel.set" title="http.cookies.Morsel.set"><code class="xref py py-func docutils literal notranslate"><span class="pre">set()</span></code></a> 中未写入文档的 <em>LegalChars</em> 形参已被弃用并且现在会被忽略。 (由 Demian Brecht 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2211">bpo-2211</a> 中贡献。)</p></li>
<li><p><span class="target" id="index-52"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> has removed <code class="docutils literal notranslate"><span class="pre">.pyo</span></code> files from Python and introduced the optional
<code class="docutils literal notranslate"><span class="pre">opt-</span></code> tag in <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> file names. The
<a class="reference internal" href="../library/importlib.html#importlib.util.cache_from_source" title="importlib.util.cache_from_source"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.cache_from_source()</span></code></a> has gained an <em>optimization</em>
parameter to help control the <code class="docutils literal notranslate"><span class="pre">opt-</span></code> tag. Because of this, the
<em>debug_override</em> parameter of the function is now deprecated. <cite>.pyo</cite> files
are also no longer supported as a file argument to the Python interpreter and
thus serve no purpose when distributed on their own (i.e. sourceless code
distribution). Due to the fact that the magic number for bytecode has changed
in Python 3.5, all old <cite>.pyo</cite> files from previous versions of Python are
invalid regardless of this PEP.</p></li>
<li><p>The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module now exports the <a class="reference internal" href="../library/socket.html#socket.CAN_RAW_FD_FRAMES" title="socket.CAN_RAW_FD_FRAMES"><code class="xref py py-data docutils literal notranslate"><span class="pre">CAN_RAW_FD_FRAMES</span></code></a>
constant on linux 3.6 and greater.</p></li>
<li><p>根据 <span class="target" id="index-152"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5280.html"><strong>RFC 5280</strong></a>,现在 <a class="reference internal" href="../library/ssl.html#ssl.cert_time_to_seconds" title="ssl.cert_time_to_seconds"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.cert_time_to_seconds()</span></code></a> 函数会将输入的时间解读为 UTC 而不是本地时间。 此外,其返回值必须为 <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>。 (由 Akira Li 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=19940">bpo-19940</a> 中贡献。)</p></li>
<li><p>现在 <code class="docutils literal notranslate"><span class="pre">pygettext.py</span></code> 工具将为 POT-Creation-Date 标头中的时区使用标准的 +NNNN 格式。</p></li>
<li><p>现在 <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> 模块对于调试输出将使用 <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> 而不是之前的模块级 <code class="xref py py-data docutils literal notranslate"><span class="pre">stderr</span></code> 变量。 如果你的(测试)程序依赖于对模块级变量打补丁来捕获调试输出,你将需要更新代码将其改为捕获 sys.stderr。</p></li>
<li><p>当发现空字符串且索引完全超出范围时 <a class="reference internal" href="../library/stdtypes.html#str.startswith" title="str.startswith"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.startswith()</span></code></a><a class="reference internal" href="../library/stdtypes.html#str.endswith" title="str.endswith"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.endswith()</span></code></a> 方法将不再返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=24284">bpo-24284</a> 中贡献。)</p></li>
<li><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> 将返回从基类继承的文档字符串。 如果继承的文档字符串可用就不需要再复制文档字符串。 要屏蔽继承的字符串,必须指定一个空字符串(否则将会填充文档)。 此项改变将影响 <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pydoc</span></code></a> 模块和 <a class="reference internal" href="../library/functions.html#help" title="help"><code class="xref py py-func docutils literal notranslate"><span class="pre">help()</span></code></a> 函数的输出。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15582">bpo-15582</a> 中贡献。)</p></li>
<li><p>嵌套的 <a class="reference internal" href="../library/functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> 调用现在已被展平。 如果你需要之前的行为,现在你可以为 <a class="reference internal" href="../library/functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> 对象添加一个属性或者创建一个 <a class="reference internal" href="../library/functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> 的子类。 (由 Alexander Belopolsky 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7830">bpo-7830</a> 中贡献。)</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 class="simple">
<li><p>The undocumented <code class="xref c c-member docutils literal notranslate"><span class="pre">format</span></code> member of the
(non-public) <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemoryViewObject</span></code> structure has been removed.
All extensions relying on the relevant parts in <code class="docutils literal notranslate"><span class="pre">memoryobject.h</span></code>
must be rebuilt.</p></li>
<li><p>The <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemAllocator</span></code> structure was renamed to
<a class="reference internal" href="../c-api/memory.html#c.PyMemAllocatorEx" title="PyMemAllocatorEx"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemAllocatorEx</span></code></a> and a new <code class="docutils literal notranslate"><span class="pre">calloc</span></code> field was added.</p></li>
<li><p>Removed non-documented macro <code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_REPR</span></code> which leaked references.
Use format character <code class="docutils literal notranslate"><span class="pre">%R</span></code> in <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromFormat" title="PyUnicode_FromFormat"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromFormat()</span></code></a>-like functions
to format the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of the object.
(Contributed by Serhiy Storchaka in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=22453">bpo-22453</a>.)</p></li>
<li><p>由于缺少 <code class="xref py py-attr docutils literal notranslate"><span class="pre">__module__</span></code> 属性会破坏封存和内省特性,现在对于没有 <code class="xref py py-attr docutils literal notranslate"><span class="pre">__module__</span></code> 属性的内置类型会引发弃用警告。 这在将来会改为 AttributeError。 (由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=20204">bpo-20204</a> 中贡献。)</p></li>
<li><p>As part of the <span class="target" id="index-54"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> implementation, the <code class="docutils literal notranslate"><span class="pre">tp_reserved</span></code> slot of
<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> was replaced with a
<code class="xref c c-member docutils literal notranslate"><span class="pre">tp_as_async</span></code> slot. Refer to <a class="reference internal" href="../c-api/coro.html#coro-objects"><span class="std std-ref">协程对象</span></a> for
new types, structures and functions.</p></li>
</ul>
</section>
</section>
<section id="notable-changes-in-python-3-5-4">
<h2>Python 3.5.4 的显著变化<a class="headerlink" href="#notable-changes-in-python-3-5-4" title="永久链接至标题"></a></h2>
<section id="new-make-regen-all-build-target">
<h3>新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标<a class="headerlink" href="#new-make-regen-all-build-target" title="永久链接至标题"></a></h3>
<p>为了简化交叉编译,并确保 CPython 能够可靠地编译而不需要已存在可用的 Python 版本,基于 autotools 的构建系统将不再尝试根据文件修改时间隐式地重新编译已生成的文件。</p>
<p>取而代之的是,新增了一个 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 命令以便在需要时强制重新生成这些文件(例如在基于预生成版本构建了 Python 的初始版本之后)。</p>
<p>还定义了其他一些更具选择性的重生成目标 —— 详情参见 <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Makefile.pre.in">Makefile.pre.in</a></p>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a> 中贡献。)</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5.4 新版功能.</span></p>
</div>
</section>
<section id="removal-of-make-touch-build-target">
<h3>移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标<a class="headerlink" href="#removal-of-make-touch-build-target" title="永久链接至标题"></a></h3>
<p>之前用于通过更新生成文件的修改时间来请求隐式的重新生成这些文件的 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标已被移除。</p>
<p>它已被新的 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 目标所替代。</p>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a> 中贡献。)</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5.4 版更改.</span></p>
</div>
</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.5 有什么新变化</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="#pep-492-coroutines-with-async-and-await-syntax">PEP 492 - 使用 async 和 await 语法实现协程</a></li>
<li><a class="reference internal" href="#pep-465-a-dedicated-infix-operator-for-matrix-multiplication">PEP 465 - 用于矩阵乘法的专用中缀运算符</a></li>
<li><a class="reference internal" href="#pep-448-additional-unpacking-generalizations">PEP 448 - 进一步的解包标准化</a></li>
<li><a class="reference internal" href="#pep-461-percent-formatting-support-for-bytes-and-bytearray">PEP 461 - 针对 bytes 和 bytearray 的百分号格式化支持</a></li>
<li><a class="reference internal" href="#pep-484-type-hints">PEP 484 —— 类型提示</a></li>
<li><a class="reference internal" href="#pep-471-os-scandir-function-a-better-and-faster-directory-iterator">PEP 471 - os.scandir() 函数 -- 一个更好且更快的目录迭代器</a></li>
<li><a class="reference internal" href="#pep-475-retry-system-calls-failing-with-eintr">PEP 475: 重试返回 EINTR 失败码的系统调用</a></li>
<li><a class="reference internal" href="#pep-479-change-stopiteration-handling-inside-generators">PEP 479更改生成器内部的 StopIteration 处理</a></li>
<li><a class="reference internal" href="#pep-485-a-function-for-testing-approximate-equality">PEP 485用于测试近似相等的函数</a></li>
<li><a class="reference internal" href="#pep-486-make-the-python-launcher-aware-of-virtual-environments">PEP 486让 Python 启动器识别虚拟环境</a></li>
<li><a class="reference internal" href="#pep-488-elimination-of-pyo-files">PEP 488去除 PYO 文件</a></li>
<li><a class="reference internal" href="#pep-489-multi-phase-extension-module-initialization">PEP 489多阶段扩展模块初始化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
<li><a class="reference internal" href="#new-modules">新增模块</a><ul>
<li><a class="reference internal" href="#typing">typing</a></li>
<li><a class="reference internal" href="#zipapp">zipapp</a></li>
</ul>
</li>
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
<li><a class="reference internal" href="#argparse">argparse</a></li>
<li><a class="reference internal" href="#asyncio">asyncio</a></li>
<li><a class="reference internal" href="#bz2">bz2</a></li>
<li><a class="reference internal" href="#cgi">cgi</a></li>
<li><a class="reference internal" href="#cmath">cmath</a></li>
<li><a class="reference internal" href="#code">code</a></li>
<li><a class="reference internal" href="#collections">collections</a></li>
<li><a class="reference internal" href="#collections-abc">collections.abc</a></li>
<li><a class="reference internal" href="#compileall">compileall</a></li>
<li><a class="reference internal" href="#concurrent-futures">concurrent.futures</a></li>
<li><a class="reference internal" href="#configparser">configparser</a></li>
<li><a class="reference internal" href="#contextlib">contextlib</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="#dbm">dbm</a></li>
<li><a class="reference internal" href="#difflib">difflib</a></li>
<li><a class="reference internal" href="#distutils">distutils</a></li>
<li><a class="reference internal" href="#doctest">doctest</a></li>
<li><a class="reference internal" href="#email">email</a></li>
<li><a class="reference internal" href="#enum">enum</a></li>
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
<li><a class="reference internal" href="#functools">functools</a></li>
<li><a class="reference internal" href="#glob">glob</a></li>
<li><a class="reference internal" href="#gzip">gzip</a></li>
<li><a class="reference internal" href="#heapq">heapq</a></li>
<li><a class="reference internal" href="#http">http</a></li>
<li><a class="reference internal" href="#http-client">http.client</a></li>
<li><a class="reference internal" href="#idlelib-and-idle">idlelib 与 IDLE</a></li>
<li><a class="reference internal" href="#imaplib">imaplib</a></li>
<li><a class="reference internal" href="#imghdr">imghdr</a></li>
<li><a class="reference internal" href="#importlib">importlib</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="#ipaddress">ipaddress</a></li>
<li><a class="reference internal" href="#json">json</a></li>
<li><a class="reference internal" href="#linecache">linecache</a></li>
<li><a class="reference internal" href="#locale">locale</a></li>
<li><a class="reference internal" href="#logging">logging</a></li>
<li><a class="reference internal" href="#lzma">lzma</a></li>
<li><a class="reference internal" href="#math">math</a></li>
<li><a class="reference internal" href="#multiprocessing">multiprocessing</a></li>
<li><a class="reference internal" href="#operator">operator</a></li>
<li><a class="reference internal" href="#os">os</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="#poplib">poplib</a></li>
<li><a class="reference internal" href="#re">re</a></li>
<li><a class="reference internal" href="#readline">readline</a></li>
<li><a class="reference internal" href="#selectors">selectors</a></li>
<li><a class="reference internal" href="#shutil">shutil</a></li>
<li><a class="reference internal" href="#signal">signal</a></li>
<li><a class="reference internal" href="#smtpd">smtpd</a></li>
<li><a class="reference internal" href="#smtplib">smtplib</a></li>
<li><a class="reference internal" href="#sndhdr">sndhdr</a></li>
<li><a class="reference internal" href="#socket">socket</a></li>
<li><a class="reference internal" href="#ssl">ssl</a><ul>
<li><a class="reference internal" href="#memory-bio-support">内存 BIO 支持</a></li>
<li><a class="reference internal" href="#application-layer-protocol-negotiation-support">应用层协议协商支持</a></li>
<li><a class="reference internal" href="#other-changes">其他改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
<li><a class="reference internal" href="#subprocess">subprocess</a></li>
<li><a class="reference internal" href="#sys">sys</a></li>
<li><a class="reference internal" href="#sysconfig">sysconfig</a></li>
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
<li><a class="reference internal" href="#threading">threading</a></li>
<li><a class="reference internal" href="#time">time</a></li>
<li><a class="reference internal" href="#timeit">timeit</a></li>
<li><a class="reference internal" href="#tkinter">tkinter</a></li>
<li><a class="reference internal" href="#traceback">回溯</a></li>
<li><a class="reference internal" href="#types">types</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="#unittest-mock">unittest.mock</a></li>
<li><a class="reference internal" href="#urllib">urllib</a></li>
<li><a class="reference internal" href="#wsgiref">wsgiref</a></li>
<li><a class="reference internal" href="#xmlrpc">xmlrpc</a></li>
<li><a class="reference internal" href="#xml-sax">xml.sax</a></li>
<li><a class="reference internal" href="#zipfile">zipfile</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-module-level-changes">其他模块级更改</a></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><ul>
<li><a class="reference internal" href="#new-keywords">新关键字</a></li>
<li><a class="reference internal" href="#deprecated-python-behavior">已弃用的 Python 行为</a></li>
<li><a class="reference internal" href="#unsupported-operating-systems">不支持的操作系统</a></li>
<li><a class="reference internal" href="#deprecated-python-modules-functions-and-methods">已弃用的 Python 模块、函数和方法</a></li>
</ul>
</li>
<li><a class="reference internal" href="#removed">移除</a><ul>
<li><a class="reference internal" href="#api-and-feature-removals">API 与特性的移除</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-3-5">移植到Python 3.5</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>
</ul>
</li>
<li><a class="reference internal" href="#notable-changes-in-python-3-5-4">Python 3.5.4 的显著变化</a><ul>
<li><a class="reference internal" href="#new-make-regen-all-build-target">新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标</a></li>
<li><a class="reference internal" href="#removal-of-make-touch-build-target">移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.6.html"
title="上一章">Python 3.6 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="3.4.html"
title="下一章">Python 3.4 有什么新变化</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.5.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.4.html" title="Python 3.4 有什么新变化"
>下一页</a> |</li>
<li class="right" >
<a href="3.6.html" title="Python 3.6 有什么新变化"
>上一页</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> &#187;</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> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python的新变化</a> &#187;</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">
&copy; <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>