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

2582 lines
320 KiB
HTML
Raw Permalink 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.3 有什么新变化 &#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.2 有什么新变化" href="3.2.html" />
<link rel="prev" title="Python 3.4 有什么新变化" href="3.4.html" />
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.3.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.3 有什么新变化</a><ul>
<li><a class="reference internal" href="#summary-release-highlights">摘要 -- 发布重点</a></li>
<li><a class="reference internal" href="#pep-405-virtual-environments">PEP 405: 虚拟环境</a></li>
<li><a class="reference internal" href="#pep-420-implicit-namespace-packages">PEP 420: 隐式命名空间包</a></li>
<li><a class="reference internal" href="#pep-3118-new-memoryview-implementation-and-buffer-protocol-documentation">PEP 3118: 新的内存视图实现和缓冲协议文档</a><ul>
<li><a class="reference internal" href="#features">相关特性</a></li>
<li><a class="reference internal" href="#api-changes">API 的变化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-393-flexible-string-representation">PEP 393: 灵活的字符串表示</a><ul>
<li><a class="reference internal" href="#functionality">功能</a></li>
<li><a class="reference internal" href="#performance-and-resource-usage">性能和资源使用情况</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-397-python-launcher-for-windows">PEP 397: 适用于Windows的Python启动器</a></li>
<li><a class="reference internal" href="#pep-3151-reworking-the-os-and-io-exception-hierarchy">PEP 3151: 重写 OS 和 IO 异常的层次结构</a></li>
<li><a class="reference internal" href="#pep-380-syntax-for-delegating-to-a-subgenerator">PEP 380: 委托给子生成器的语法</a></li>
<li><a class="reference internal" href="#pep-409-suppressing-exception-context">PEP 409: 清除异常上下文</a></li>
<li><a class="reference internal" href="#pep-414-explicit-unicode-literals">PEP 414: 显式的Unicode文本</a></li>
<li><a class="reference internal" href="#pep-3155-qualified-name-for-classes-and-functions">PEP 3155: 类和函数的限定名称</a></li>
<li><a class="reference internal" href="#pep-412-key-sharing-dictionary">PEP 412: Key-Sharing Dictionary</a></li>
<li><a class="reference internal" href="#pep-362-function-signature-object">PEP 362: 函数签名对象</a></li>
<li><a class="reference internal" href="#pep-421-adding-sys-implementation">PEP 421: 添加 sys.implementation</a><ul>
<li><a class="reference internal" href="#simplenamespace">SimpleNamespace</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-importlib-as-the-implementation-of-import">使用 importlib 作为导入的实现</a><ul>
<li><a class="reference internal" href="#new-apis">新的API</a></li>
<li><a class="reference internal" href="#visible-changes">可见的改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
<li><a class="reference internal" href="#a-finer-grained-import-lock">A Finer-Grained Import Lock</a></li>
<li><a class="reference internal" href="#builtin-functions-and-types">Builtin functions and types</a></li>
<li><a class="reference internal" href="#new-modules">新增模块</a><ul>
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
<li><a class="reference internal" href="#ipaddress">ipaddress</a></li>
<li><a class="reference internal" href="#lzma">lzma</a></li>
</ul>
</li>
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
<li><a class="reference internal" href="#abc">abc</a></li>
<li><a class="reference internal" href="#array">array</a></li>
<li><a class="reference internal" href="#base64">base64</a></li>
<li><a class="reference internal" href="#binascii">binascii</a></li>
<li><a class="reference internal" href="#bz2">bz2</a></li>
<li><a class="reference internal" href="#codecs">编码器</a></li>
<li><a class="reference internal" href="#collections">collections</a></li>
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
<li><a class="reference internal" href="#crypt">crypt</a></li>
<li><a class="reference internal" href="#curses">curses</a></li>
<li><a class="reference internal" href="#datetime">datetime</a></li>
<li><a class="reference internal" href="#decimal">decimal</a><ul>
<li><a class="reference internal" href="#id1">相关特性</a></li>
<li><a class="reference internal" href="#id2">API 的变化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#email">email</a><ul>
<li><a class="reference internal" href="#policy-framework">Policy Framework</a></li>
<li><a class="reference internal" href="#provisional-policy-with-new-header-api">Provisional Policy with New Header API</a></li>
<li><a class="reference internal" href="#other-api-changes">Other API Changes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#ftplib">ftplib</a></li>
<li><a class="reference internal" href="#functools">functools</a></li>
<li><a class="reference internal" href="#gc">gc</a></li>
<li><a class="reference internal" href="#hmac">hmac</a></li>
<li><a class="reference internal" href="#http">http</a></li>
<li><a class="reference internal" href="#html">html</a></li>
<li><a class="reference internal" href="#imaplib">imaplib</a></li>
<li><a class="reference internal" href="#inspect">inspect</a></li>
<li><a class="reference internal" href="#io">io</a></li>
<li><a class="reference internal" href="#itertools">itertools</a></li>
<li><a class="reference internal" href="#logging">logging</a></li>
<li><a class="reference internal" href="#math">math</a></li>
<li><a class="reference internal" href="#mmap">mmap</a></li>
<li><a class="reference internal" href="#multiprocessing">multiprocessing</a></li>
<li><a class="reference internal" href="#nntplib">nntplib</a></li>
<li><a class="reference internal" href="#os">os</a></li>
<li><a class="reference internal" href="#pdb">pdb</a></li>
<li><a class="reference internal" href="#pickle">pickle</a></li>
<li><a class="reference internal" href="#pydoc">pydoc</a></li>
<li><a class="reference internal" href="#re">re</a></li>
<li><a class="reference internal" href="#sched">sched</a></li>
<li><a class="reference internal" href="#select">select</a></li>
<li><a class="reference internal" href="#shlex">shlex</a></li>
<li><a class="reference internal" href="#shutil">shutil</a></li>
<li><a class="reference internal" href="#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="#socket">socket</a></li>
<li><a class="reference internal" href="#socketserver">socketserver</a></li>
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
<li><a class="reference internal" href="#ssl">ssl</a></li>
<li><a class="reference internal" href="#stat">stat</a></li>
<li><a class="reference internal" href="#struct">struct</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="#tarfile">tarfile</a></li>
<li><a class="reference internal" href="#tempfile">tempfile</a></li>
<li><a class="reference internal" href="#textwrap">textwrap</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="#types">types</a></li>
<li><a class="reference internal" href="#unittest">unittest</a></li>
<li><a class="reference internal" href="#urllib">urllib</a></li>
<li><a class="reference internal" href="#webbrowser">webbrowser</a></li>
<li><a class="reference internal" href="#xml-etree-elementtree">xml.etree.ElementTree</a></li>
<li><a class="reference internal" href="#zlib">zlib</a></li>
</ul>
</li>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
<li><a class="reference internal" href="#deprecated">弃用</a><ul>
<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>
<li><a class="reference internal" href="#deprecated-functions-and-types-of-the-c-api">已弃用的 C API 函数和类型</a></li>
<li><a class="reference internal" href="#deprecated-features">弃用的功能</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-3-3">移植到 Python 3.3</a><ul>
<li><a class="reference internal" href="#porting-python-code">移植 Python 代码</a></li>
<li><a class="reference internal" href="#porting-c-code">移植 C 代码</a></li>
<li><a class="reference internal" href="#building-c-extensions">Building C extensions</a></li>
<li><a class="reference internal" href="#command-line-switch-changes">命令行开关的变化</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.4.html"
title="上一章">Python 3.4 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="3.2.html"
title="下一章">Python 3.2 有什么新变化</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.3.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.3.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.2.html" title="Python 3.2 有什么新变化"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="3.4.html" title="Python 3.4 有什么新变化"
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-3">
<h1>Python 3.3 有什么新变化<a class="headerlink" href="#what-s-new-in-python-3-3" title="永久链接至标题"></a></h1>
<p>本文介绍了 Python 3.3 相比 3.2 的新增特性。 Python 3.3 于 2012 年 9 月 29 日 发布。 有关完整详细信息,请参见 <a class="reference external" href="https://docs.python.org/3.3/whatsnew/changelog.html">changelog</a></p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-60"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0398"><strong>PEP 398</strong></a> - Python 3.3 发布计划</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>新增 <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> 表达式用于 <a class="reference internal" href="#pep-380"><span class="std std-ref">生成器委托</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">u'unicode'</span></code> 语法重新被接受用于 <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> 对象。</p></li>
</ul>
<p>新的库模块:</p>
<ul class="simple">
<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/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> (代表 IP 地址和掩码的高层级对象)</p></li>
<li><p><a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> (使用 XZ / LZMA 算法压缩数据)</p></li>
<li><p><a class="reference internal" href="../library/unittest.mock.html#module-unittest.mock" title="unittest.mock: Mock object library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest.mock</span></code></a> (使用模拟对象替换你的受测试系统中的某些部分)</p></li>
<li><p><a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> (Python <a class="reference internal" href="#pep-405"><span class="std std-ref">虚拟环境</span></a>,类似于流行的 <code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> 包)</p></li>
</ul>
<p>新的内置特性:</p>
<ul class="simple">
<li><p>重写 <a class="reference internal" href="#pep-3151"><span class="std std-ref">I/O 异常的层次结构</span></a>.</p></li>
</ul>
<p>实现的改进:</p>
<ul class="simple">
<li><p>基于 <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> 重写 <a class="reference internal" href="#importlib"><span class="std std-ref">import machinery</span></a></p></li>
<li><p>更紧凑的 <a class="reference internal" href="#pep-393"><span class="std std-ref">Unicode 字符串</span></a></p></li>
<li><p>更紧凑的 <a class="reference internal" href="#pep-412"><span class="std std-ref">属性字典</span></a></p></li>
</ul>
<p>显著改进的库模块:</p>
<ul class="simple">
<li><p>针对 <a class="reference internal" href="#new-decimal"><span class="std std-ref">decimal</span></a> 模块的 C 加速器。</p></li>
<li><p><a class="reference internal" href="#new-email"><span class="std std-ref">email</span></a> 模块中更好的 Unicode 处理 (<a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">暂定</span></a>)。</p></li>
</ul>
<p>安全改进:</p>
<ul class="simple">
<li><p>哈希随机化被默认启用。</p></li>
</ul>
<p>请继续阅读有关面向用户的改变的详细清单。</p>
</section>
<section id="pep-405-virtual-environments">
<span id="pep-405"></span><h2>PEP 405: 虚拟环境<a class="headerlink" href="#pep-405-virtual-environments" title="永久链接至标题"></a></h2>
<p>Virtual environments help create separate Python setups while sharing a
system-wide base install, for ease of maintenance. Virtual environments
have their own set of private site packages (i.e. locally-installed
libraries), and are optionally segregated from the system-wide site
packages. Their concept and implementation are inspired by the popular
<code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> third-party package, but benefit from tighter integration
with the interpreter core.</p>
<p>本 PEP 添加了 <a class="reference internal" href="../library/venv.html#module-venv" title="venv: Creation of virtual environments."><code class="xref py py-mod docutils literal notranslate"><span class="pre">venv</span></code></a> 模块用于编程访问,以及 <code class="docutils literal notranslate"><span class="pre">pyvenv</span></code> 脚本用于命令在线访问和管理。 Python 解释器会检查 <code class="docutils literal notranslate"><span class="pre">pyvenv.cfg</span></code>,文件的存在标志着虚拟环境目录树的基础。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-61"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0405"><strong>PEP 405</strong></a> - Python虚拟环境</dt><dd><p>PEP 由 Carl Meyer 撰写 ; 由 Carl Meyer 和 Vinay Sajip 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-420-implicit-namespace-packages">
<h2>PEP 420: 隐式命名空间包<a class="headerlink" href="#pep-420-implicit-namespace-packages" title="永久链接至标题"></a></h2>
<p>原生支持不要求 <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> 标记文件和可以自动跨越多个路径节的包目录(灵感来自多个命名空间包的第三方方案,如 <span class="target" id="index-62"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0420"><strong>PEP 420</strong></a> 中所述)</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-63"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0420"><strong>PEP 420</strong></a> - 隐式命名空间包</dt><dd><p>PEP 由 Eric V. Smith 撰写,由 Eric V. Smith 和 Barry Warsaw 实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-3118-new-memoryview-implementation-and-buffer-protocol-documentation">
<span id="pep-3118-update"></span><h2>PEP 3118: 新的内存视图实现和缓冲协议文档<a class="headerlink" href="#pep-3118-new-memoryview-implementation-and-buffer-protocol-documentation" title="永久链接至标题"></a></h2>
<p><span class="target" id="index-64"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a> 的实现已获得大幅改进。</p>
<p>新的 memoryview 实现全面修复了 Py_buffer 结构体中曾导致多起崩溃报告的动态分配字段的所有权和生命周期问题。 此外,还修复了多个函数在非连续或多维输入时崩溃或返回不正确结果的问题。</p>
<p>现在 memoryview 对象具有符合 PEP-3118 标准的 getbufferproc(),可以检查使用者的请求类型。 新增了许多新的特性,其中的大部分已适用于非连续数组和带有子偏移量的数组。</p>
<p>文档已进行更新,清楚地列出了导出方和使用方的责任。 缓冲区请求旗标志被划分为基本旗标和复合旗标。 对非连续和多维的 NumPy 风格数组的内存布局进行了说明。</p>
<section id="features">
<h3>相关特性<a class="headerlink" href="#features" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>现在 struct 模块语法中所有原生单字符格式指示符(可以选择添加 '&#64;' 前缀)均受到支持。</p></li>
<li><p>在某些限制条件下cast() 方法允许改变 C 连续数组的格式和形状。</p></li>
<li><p>任何数组类型都支持多维列表的表示形式。</p></li>
<li><p>任何数组类型都支持多维比较操作。</p></li>
<li><p>格式为 B、b 或 c 的可哈希(只读)类型的一维 memoryview 现在将是可哈希的。 (由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13411">bpo-13411</a> 中贡献。)</p></li>
<li><p>Arbitrary slicing of any 1-D arrays type is supported. For example, it
is now possible to reverse a memoryview in O(1) by using a negative step.</p></li>
</ul>
</section>
<section id="api-changes">
<h3>API 的变化<a class="headerlink" href="#api-changes" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>官方的最大维度数量限制已更改为 64。</p></li>
<li><p>空形状、区间和子偏移量的表示形式现在是空元组而不是 <code class="docutils literal notranslate"><span class="pre">None</span></code></p></li>
<li><p>现在对格式为 'B' (无符号字节型) 的 memoryview 元素的访问将返回一个整数(遵循结构体模块语法)。 要返回字节串对象则必须先将视图强制转换为 'c'。</p></li>
<li><p>现在 memoryview 比较将使用操作数的逻辑结构并会按值来比较所有数组元素。 结构体模块语法中的所有格式化字符串均受到支持。 带有不可识别的格式化字符串的视图仍然被允许,但无论视图内容如何比较结果总是不相等。</p></li>
<li><p>更多改变请参阅 <a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><a class="reference internal" href="#porting-c-code">Porting C code</a></p></li>
</ul>
<p>(由 Stefan Krah 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10181">bpo-10181</a> 中贡献。)</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><span class="target" id="index-65"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a> - 修改缓冲区协议</p>
</div>
</section>
</section>
<section id="pep-393-flexible-string-representation">
<span id="pep-393"></span><h2>PEP 393: 灵活的字符串表示<a class="headerlink" href="#pep-393-flexible-string-representation" title="永久链接至标题"></a></h2>
<p>Unicode字符串类型已改为支持多种内部表示法具体取决于所表示的字符串中具有最大 Unicode 序号1、2 或 4 字节)的字符 。 这样,在常见情况下可以节省空间,但在所有系统上都能使用完整的 UCS-4。 对于使用现有应用程序接口的兼容性 来说,可能会并行存在几种表示法;随着时间的推移,这种兼容性 应逐步淘汰。</p>
<p>在 Python 一方,此项改变应当没有任何缺点。</p>
<p>在 C API 方面,<span class="target" id="index-66"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a> 完全向下兼容。 旧的 API 至少还能使用五年。 使用传统 API 的应用程序不会完全受益于内存的减少,或者更糟的是,可能会使用更多的内存,因为 Python 可能需要维护每个字符串的两个版本(传统格式和新的高效存储)。</p>
<section id="functionality">
<h3>功能<a class="headerlink" href="#functionality" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-67"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a> 引入的改变如下:</p>
<ul class="simple">
<li><p>Python 现在始终支持全部 Unicode 码位,包括非 BMP 码位 (即从``U+0000`` 到 <code class="docutils literal notranslate"><span class="pre">U+10FFFF</span></code>)。 窄编译版本和宽编译版本之间的区别已不复存在Python 现在的行为就像宽编译版本,甚至在 Windows 下也是如此。</p></li>
<li><p>随着窄编译版本的消亡,窄编译版本特有的问题也得到了解决,例如:</p>
<ul>
<li><p>现在 <a class="reference internal" href="../library/functions.html#len" title="len"><code class="xref py py-func docutils literal notranslate"><span class="pre">len()</span></code></a> 对于非 BMP 字符总是返回 1因此 <code class="docutils literal notranslate"><span class="pre">len('\U0010FFFF')</span> <span class="pre">==</span> <span class="pre">1</span></code></p></li>
<li><p>替换对不会在字符串字面值中重新合并,因此 <code class="docutils literal notranslate"><span class="pre">'\uDBFF\uDFFF'</span> <span class="pre">!=</span> <span class="pre">'\U0010FFFF'</span></code></p></li>
<li><p>索引或切分非 BMP 字符会返回预期的值,因此 <code class="docutils literal notranslate"><span class="pre">'\U0010FFFF'[0]</span></code> 现在会返回 <code class="docutils literal notranslate"><span class="pre">'\U0010FFFF'</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">'\uDBFF'</span></code></p></li>
<li><p>标准库中的所有其他函数现在都能正确处理非 BMP 代码点。</p></li>
</ul>
</li>
<li><p>The value of <a class="reference internal" href="../library/sys.html#sys.maxunicode" title="sys.maxunicode"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxunicode</span></code></a> is now always <code class="docutils literal notranslate"><span class="pre">1114111</span></code> (<code class="docutils literal notranslate"><span class="pre">0x10FFFF</span></code>
in hexadecimal). The <code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetMax()</span></code> function still returns
either <code class="docutils literal notranslate"><span class="pre">0xFFFF</span></code> or <code class="docutils literal notranslate"><span class="pre">0x10FFFF</span></code> for backward compatibility, and it should
not be used with the new Unicode API (see <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13054">bpo-13054</a>).</p></li>
<li><p><code class="file docutils literal notranslate"><span class="pre">./configure</span></code> 标志 <code class="docutils literal notranslate"><span class="pre">--with-wide-unicode</span></code> 已被移除。</p></li>
</ul>
</section>
<section id="performance-and-resource-usage">
<h3>性能和资源使用情况<a class="headerlink" href="#performance-and-resource-usage" title="永久链接至标题"></a></h3>
<p>现在Unicode 字符串的存储取决于字符串中的最高码位:</p>
<ul class="simple">
<li><p>纯 ASCII 和 Latin1 字符串 (<code class="docutils literal notranslate"><span class="pre">U+0000-U+00FF</span></code>) 每个码位使用 1 个字节;</p></li>
<li><p>BMP 字符串 (<code class="docutils literal notranslate"><span class="pre">U+0000-U+FFFF</span></code>) 每个码位使用 2 个字节;</p></li>
<li><p>非 BMP 字符串 (<code class="docutils literal notranslate"><span class="pre">U+10000-U+10FFFF</span></code>) 每个码位使用 4 个字节。</p></li>
</ul>
<p>这样做的效果是,对于大多数应用而言,字符串存储的内存使用量应该会大幅减少 —— 尤其是与以前的宽 unicode 版本相比 —— 因为在许多情况下,即使在国际环境中,字符串也将是纯 ASCII 格式(因为许多字符串存储的是非人类语言数据,如 XML 片段、HTTP 标头、JSON 编码数据等)。 出于同样的原因,我们还希望它能提高非小应用程序的 CPU 缓存效率。 在 Django 基准测试中Python 3.3 的内存使用量比 Python 3.2 少两到三倍,比 Python 2.7 略好一些(详情请参见 PEP</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-68"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a> - 灵活的字符串表示</dt><dd><p>PEP 由 Martin von Löwis 撰写 ; 由 Torsten Becker 和 Martin von Löwis 实现。</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="pep-397-python-launcher-for-windows">
<span id="pep-397"></span><h2>PEP 397: 适用于Windows的Python启动器<a class="headerlink" href="#pep-397-python-launcher-for-windows" title="永久链接至标题"></a></h2>
<p>Python 3.3 的 Windows 安装程序现在包含一个 <code class="docutils literal notranslate"><span class="pre">py</span></code> 启动程序,可用于以版本无关的方式启动 Python 应用程序。</p>
<p>双击 <code class="docutils literal notranslate"><span class="pre">*.py</span></code> 文件时会隐式调用该启动器。 如果系统中只安装了一个 Python 版本,则将使用该版本运行文件。 如果安装了多个版本,则默认使用最新版本,但也可以通过在 Python 脚本中加入 Unix 风格的“shebang 行”来覆盖该版本。</p>
<p>启动器也可以作为 <code class="docutils literal notranslate"><span class="pre">py</span></code> 应用程序在命令行中显式使用。运行 <code class="docutils literal notranslate"><span class="pre">py</span></code> 遵循与隐式启动脚本相同的版本选择规则,但可以通过传递适当的参数来选择更具体的版本(例如,当 Python 2 也已安装时,使用 <code class="docutils literal notranslate"><span class="pre">-3</span></code> 来请求 Python 3当安装了较新的 Python 版本时,使用 <code class="docutils literal notranslate"><span class="pre">-2.6</span></code> 来特别请求较早的 Python 版本)。</p>
<p>除了启动器之外Windows 安装程序现在还包含一个选项,可将新安装的 Python 添加到系统 PATH 中。 (由 Brian Curtin 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3561">bpo-3561</a> 中贡献)。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-69"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0397"><strong>PEP 397</strong></a> - 适用于Windows的Python启动器</dt><dd><p>PEP 由 Mark Hammond 和 Martin v. Löwis 撰写 ; 由 Vinay Sajip实现。</p>
</dd>
</dl>
<p>启动器文档: <a class="reference internal" href="../using/windows.html#launcher"><span class="std std-ref">适用于Windows的Python启动器</span></a></p>
<p>安装器 PATH 修改: <a class="reference internal" href="../using/windows.html#windows-path-mod"><span class="std std-ref">查找Python可执行文件</span></a></p>
</div>
</section>
<section id="pep-3151-reworking-the-os-and-io-exception-hierarchy">
<span id="pep-3151"></span><h2>PEP 3151: 重写 OS 和 IO 异常的层次结构<a class="headerlink" href="#pep-3151-reworking-the-os-and-io-exception-hierarchy" title="永久链接至标题"></a></h2>
<p>现在,由操作系统错误引发的异常层次结构既得到了简化,又更加精细。</p>
<p>您不必再为在 <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a><a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a><a class="reference internal" href="../library/exceptions.html#EnvironmentError" title="EnvironmentError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EnvironmentError</span></code></a><a class="reference internal" href="../library/exceptions.html#WindowsError" title="WindowsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">WindowsError</span></code></a><code class="xref py py-exc docutils literal notranslate"><span class="pre">mmap.error</span></code><a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a><a class="reference internal" href="../library/select.html#select.error" title="select.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">select.error</span></code></a> 之间选择合适的异常类型而烦恼。 所有这些异常类型现在都只有一个: <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>。 出于兼容性考虑,其他名称将作为别名保留。</p>
<p>此外,现在捕捉特定错误条件也更容易了。无需从 <a class="reference internal" href="../library/errno.html#module-errno" title="errno: Standard errno system symbols."><code class="xref py py-mod docutils literal notranslate"><span class="pre">errno</span></code></a> 模块中检查 <code class="docutils literal notranslate"><span class="pre">errno</span></code> 属性(或 <code class="docutils literal notranslate"><span class="pre">args[0]</span></code> )中的特定常量,您可以捕捉适当的 <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> 子类。可用的子类如下:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/exceptions.html#BlockingIOError" title="BlockingIOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BlockingIOError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ChildProcessError" title="ChildProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ChildProcessError</span></code></a></p></li>
<li><p><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></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></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></p></li>
<li><p><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><a class="reference internal" href="../library/exceptions.html#IsADirectoryError" title="IsADirectoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IsADirectoryError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#NotADirectoryError" title="NotADirectoryError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotADirectoryError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ProcessLookupError" title="ProcessLookupError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ProcessLookupError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#TimeoutError" title="TimeoutError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TimeoutError</span></code></a></p></li>
</ul>
<p>并且 <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> 本身具有细粒度的子类:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/exceptions.html#BrokenPipeError" title="BrokenPipeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenPipeError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ConnectionAbortedError" title="ConnectionAbortedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionAbortedError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ConnectionRefusedError" title="ConnectionRefusedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionRefusedError</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a></p></li>
</ul>
<p>有了新的异常,现在就可以避免 <a class="reference internal" href="../library/errno.html#module-errno" title="errno: Standard errno system symbols."><code class="xref py py-mod docutils literal notranslate"><span class="pre">errno</span></code></a> 的常见用法了。 例如,下面是为 Python 3.2 编写的代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">errno</span> <span class="kn">import</span> <span class="n">ENOENT</span><span class="p">,</span> <span class="n">EACCES</span><span class="p">,</span> <span class="n">EPERM</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;document.txt&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
<span class="k">if</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="o">==</span> <span class="n">ENOENT</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;document.txt file is missing&quot;</span><span class="p">)</span>
<span class="k">elif</span> <span class="n">err</span><span class="o">.</span><span class="n">errno</span> <span class="ow">in</span> <span class="p">(</span><span class="n">EACCES</span><span class="p">,</span> <span class="n">EPERM</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;You are not allowed to read document.txt&quot;</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">raise</span>
</pre></div>
</div>
<p>现在无需导入 <a class="reference internal" href="../library/errno.html#module-errno" title="errno: Standard errno system symbols."><code class="xref py py-mod docutils literal notranslate"><span class="pre">errno</span></code></a>,也无需手动检查异常属性:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;document.txt&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">FileNotFoundError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;document.txt file is missing&quot;</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">PermissionError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;You are not allowed to read document.txt&quot;</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-70"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3151"><strong>PEP 3151</strong></a> - 重写 OS 和 IO 异常的层次结构</dt><dd><p>PEP 由 Antoine Pitrou 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-380-syntax-for-delegating-to-a-subgenerator">
<span id="pep-380"></span><span id="index-11"></span><h2>PEP 380: 委托给子生成器的语法<a class="headerlink" href="#pep-380-syntax-for-delegating-to-a-subgenerator" title="永久链接至标题"></a></h2>
<p>PEP 380 增加了 <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> 表达式,允许 <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> 将其部分操作委托给另一个生成器。 这样,包含 <a class="reference internal" href="../reference/simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> 的代码部分就可以被分解出来,放在另一个生成器中。 此外,还允许子生成器返回一个值,并将该值提供给委托生成器。</p>
<p>虽然 <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> 表达式主要用于委托给子生成器,但它实际上允许委托给任意子生成器。</p>
<p>对于简单的迭代器而言,<code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span> <span class="pre">iterable</span></code> 本质上只是 <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">iterable:</span> <span class="pre">yield</span> <span class="pre">item</span></code> 的简写形式:</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">g</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">yield from</span> <span class="nb">range</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">yield from</span> <span class="nb">range</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="go">[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]</span>
</pre></div>
</div>
<p>However, unlike an ordinary loop, <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> allows subgenerators to
receive sent and thrown values directly from the calling scope, and
return a final value to the outer generator:</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">accumulate</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">tally</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">... </span> <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">next</span> <span class="o">=</span> <span class="k">yield</span>
<span class="gp">... </span> <span class="k">if</span> <span class="nb">next</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">tally</span>
<span class="gp">... </span> <span class="n">tally</span> <span class="o">+=</span> <span class="nb">next</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">gather_tallies</span><span class="p">(</span><span class="n">tallies</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">tally</span> <span class="o">=</span> <span class="k">yield from</span> <span class="n">accumulate</span><span class="p">()</span>
<span class="gp">... </span> <span class="n">tallies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tally</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tallies</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">acc</span> <span class="o">=</span> <span class="n">gather_tallies</span><span class="p">(</span><span class="n">tallies</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">acc</span><span class="p">)</span> <span class="c1"># Ensure the accumulator is ready to accept values</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">acc</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">acc</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span> <span class="c1"># Finish the first tally</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">acc</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">acc</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span> <span class="c1"># Finish the second tally</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tallies</span>
<span class="go">[6, 10]</span>
</pre></div>
</div>
<p>The main principle driving this change is to allow even generators that are
designed to be used with the <code class="docutils literal notranslate"><span class="pre">send</span></code> and <code class="docutils literal notranslate"><span class="pre">throw</span></code> methods to be split into
multiple subgenerators as easily as a single large function can be split into
multiple subfunctions.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-71"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0380"><strong>PEP 380</strong></a> - 委托给子生成器的语法</dt><dd><p>PEP 由 Greg Ewing 撰写,由 Greg Ewing 实现。由 Renaud BlanchRyan Kelly 和 Nick Coghlan 集成到3.3,由 Zbigniew Jędrzejewski-Szmek 和 Nick Coghlan 编写文档</p>
</dd>
</dl>
</div>
</section>
<section id="pep-409-suppressing-exception-context">
<h2>PEP 409: 清除异常上下文<a class="headerlink" href="#pep-409-suppressing-exception-context" title="永久链接至标题"></a></h2>
<p>PEP 409 introduces new syntax that allows the display of the chained
exception context to be disabled. This allows cleaner error messages in
applications that convert between exception types:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">D</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">extra</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_attributes</span> <span class="o">=</span> <span class="n">extra</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_attributes</span><span class="p">[</span><span class="n">attr</span><span class="p">]</span>
<span class="gp">... </span> <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span> <span class="kn">from</span> <span class="kc">None</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">D</span><span class="p">({})</span><span class="o">.</span><span class="n">x</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>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">8</span>, in <span class="n">__getattr__</span>
<span class="gr">AttributeError</span>: <span class="n">x</span>
</pre></div>
</div>
<p>Without the <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">None</span></code> suffix to suppress the cause, the original
exception would be displayed by default:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">extra</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_attributes</span> <span class="o">=</span> <span class="n">extra</span>
<span class="gp">... </span> <span class="k">def</span> <span class="fm">__getattr__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attr</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extra_attributes</span><span class="p">[</span><span class="n">attr</span><span class="p">]</span>
<span class="gp">... </span> <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">attr</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="p">({})</span><span class="o">.</span><span class="n">x</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">6</span>, in <span class="n">__getattr__</span>
<span class="gr">KeyError</span>: <span class="n">&#39;x&#39;</span>
<span class="gt">During handling of the above exception, another exception occurred:</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>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">8</span>, in <span class="n">__getattr__</span>
<span class="gr">AttributeError</span>: <span class="n">x</span>
</pre></div>
</div>
<p>No debugging capability is lost, as the original exception context remains
available if needed (for example, if an intervening library has incorrectly
suppressed valuable underlying details):</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">D</span><span class="p">({})</span><span class="o">.</span><span class="n">x</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">AttributeError</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">exc</span><span class="o">.</span><span class="n">__context__</span><span class="p">))</span>
<span class="gp">...</span>
<span class="go">KeyError(&#39;x&#39;,)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-72"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0409"><strong>PEP 409</strong></a> - 清除异常上下文</dt><dd><p>PEP 由 Ethan Furman 撰写 ,由 Ethan Furman 和 Nick Coghlan 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-414-explicit-unicode-literals">
<h2>PEP 414: 显式的Unicode文本<a class="headerlink" href="#pep-414-explicit-unicode-literals" title="永久链接至标题"></a></h2>
<p>To ease the transition from Python 2 for Unicode aware Python applications
that make heavy use of Unicode literals, Python 3.3 once again supports the
&quot;<code class="docutils literal notranslate"><span class="pre">u</span></code>&quot; prefix for string literals. This prefix has no semantic significance
in Python 3, it is provided solely to reduce the number of purely mechanical
changes in migrating to Python 3, making it easier for developers to focus on
the more significant semantic changes (such as the stricter default
separation of binary and text data).</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-73"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0414"><strong>PEP 414</strong></a> - 显式的Unicode文本</dt><dd><p>PEP 由 Armin Ronacher 撰写</p>
</dd>
</dl>
</div>
</section>
<section id="pep-3155-qualified-name-for-classes-and-functions">
<h2>PEP 3155: 类和函数的限定名称<a class="headerlink" href="#pep-3155-qualified-name-for-classes-and-functions" title="永久链接至标题"></a></h2>
<p>Functions and class objects have a new <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code> attribute representing
the &quot;path&quot; from the module top-level to their definition. For global functions
and classes, this is the same as <code class="docutils literal notranslate"><span class="pre">__name__</span></code>. For other functions and classes,
it provides better information about where they were actually defined, and
how they might be accessible from the global scope.</p>
<p>Example with (non-bound) methods:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">meth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">meth</span><span class="o">.</span><span class="vm">__name__</span>
<span class="go">&#39;meth&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">meth</span><span class="o">.</span><span class="vm">__qualname__</span>
<span class="go">&#39;C.meth&#39;</span>
</pre></div>
</div>
<p>Example with nested classes:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">class</span> <span class="nc">D</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">meth</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="o">.</span><span class="vm">__name__</span>
<span class="go">&#39;D&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="o">.</span><span class="vm">__qualname__</span>
<span class="go">&#39;C.D&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="o">.</span><span class="n">meth</span><span class="o">.</span><span class="vm">__name__</span>
<span class="go">&#39;meth&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="o">.</span><span class="n">meth</span><span class="o">.</span><span class="vm">__qualname__</span>
<span class="go">&#39;C.D.meth&#39;</span>
</pre></div>
</div>
<p>Example with nested functions:</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">outer</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">inner</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">... </span> <span class="k">return</span> <span class="n">inner</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">outer</span><span class="p">()</span><span class="o">.</span><span class="vm">__name__</span>
<span class="go">&#39;inner&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">outer</span><span class="p">()</span><span class="o">.</span><span class="vm">__qualname__</span>
<span class="go">&#39;outer.&lt;locals&gt;.inner&#39;</span>
</pre></div>
</div>
<p>The string representation of those objects is also changed to include the
new, more precise information:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="p">)</span>
<span class="go">&quot;&lt;class &#39;__main__.C.D&#39;&gt;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">C</span><span class="o">.</span><span class="n">D</span><span class="o">.</span><span class="n">meth</span><span class="p">)</span>
<span class="go">&#39;&lt;function C.D.meth at 0x7f46b9fe31e0&gt;&#39;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-74"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3155"><strong>PEP 3155</strong></a> - 类和函数的限定名称</dt><dd><p>PEP 由 Antoine Pitrou 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-412-key-sharing-dictionary">
<span id="pep-412"></span><h2>PEP 412: Key-Sharing Dictionary<a class="headerlink" href="#pep-412-key-sharing-dictionary" title="永久链接至标题"></a></h2>
<p>Dictionaries used for the storage of objects' attributes are now able to
share part of their internal storage between each other (namely, the part
which stores the keys and their respective hashes). This reduces the memory
consumption of programs creating many instances of non-builtin types.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0412"><strong>PEP 412</strong></a> - Key-Sharing Dictionary</dt><dd><p>PEP 由 Mark Shannon 撰写并实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-362-function-signature-object">
<h2>PEP 362: 函数签名对象<a class="headerlink" href="#pep-362-function-signature-object" title="永久链接至标题"></a></h2>
<p>A new function <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> makes introspection of python
callables easy and straightforward. A broad range of callables is supported:
python functions, decorated or not, classes, and <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>
objects. New classes <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>, <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">inspect.Parameter</span></code></a>
and <a class="reference internal" href="../library/inspect.html#inspect.BoundArguments" title="inspect.BoundArguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">inspect.BoundArguments</span></code></a> hold information about the call signatures,
such as, annotations, default values, parameters kinds, and bound arguments,
which considerably simplifies writing decorators and any code that validates
or amends calling signatures or arguments.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-75"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0362"><strong>PEP 362</strong></a>: - 函数签名对象</dt><dd><p>PEP 由 Brett CannonYury SelivanovLarry HastingsJiwon Seo 撰写,由 Yury Selivanov 实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-421-adding-sys-implementation">
<h2>PEP 421: 添加 sys.implementation<a class="headerlink" href="#pep-421-adding-sys-implementation" title="永久链接至标题"></a></h2>
<p>A new attribute on the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module exposes details specific to the
implementation of the currently running interpreter. The initial set of
attributes on <a class="reference internal" href="../library/sys.html#sys.implementation" title="sys.implementation"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.implementation</span></code></a> are <code class="docutils literal notranslate"><span class="pre">name</span></code>, <code class="docutils literal notranslate"><span class="pre">version</span></code>,
<code class="docutils literal notranslate"><span class="pre">hexversion</span></code>, and <code class="docutils literal notranslate"><span class="pre">cache_tag</span></code>.</p>
<p>The intention of <code class="docutils literal notranslate"><span class="pre">sys.implementation</span></code> is to consolidate into one namespace
the implementation-specific data used by the standard library. This allows
different Python implementations to share a single standard library code base
much more easily. In its initial state, <code class="docutils literal notranslate"><span class="pre">sys.implementation</span></code> holds only a
small portion of the implementation-specific data. Over time that ratio will
shift in order to make the standard library more portable.</p>
<p>One example of improved standard library portability is <code class="docutils literal notranslate"><span class="pre">cache_tag</span></code>. As of
Python 3.3, <code class="docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is used by <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> to
support <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> compliance. Any Python implementation that uses
<code class="docutils literal notranslate"><span class="pre">importlib</span></code> for its built-in import system may use <code class="docutils literal notranslate"><span class="pre">cache_tag</span></code> to control
the caching behavior for modules.</p>
<section id="simplenamespace">
<h3>SimpleNamespace<a class="headerlink" href="#simplenamespace" title="永久链接至标题"></a></h3>
<p>The implementation of <code class="docutils literal notranslate"><span class="pre">sys.implementation</span></code> also introduces a new type to
Python: <a class="reference internal" href="../library/types.html#types.SimpleNamespace" title="types.SimpleNamespace"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.SimpleNamespace</span></code></a>. In contrast to a mapping-based
namespace, like <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>, <code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> is attribute-based, like
<a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a>. However, unlike <code class="docutils literal notranslate"><span class="pre">object</span></code>, <code class="docutils literal notranslate"><span class="pre">SimpleNamespace</span></code> instances
are writable. This means that you can add, remove, and modify the namespace
through normal attribute access.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-76"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0421"><strong>PEP 421</strong></a> - 添加 sys.implementation</dt><dd><p>PEP 由 Eric Snow 撰写并实现</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="using-importlib-as-the-implementation-of-import">
<span id="importlib"></span><h2>使用 importlib 作为导入的实现<a class="headerlink" href="#using-importlib-as-the-implementation-of-import" title="永久链接至标题"></a></h2>
<p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2377">bpo-2377</a> - 替代 __import__ w/ importlib.__import__ <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13959">bpo-13959</a> - 以纯 Python 重新实现部分 <a class="reference internal" href="../library/imp.html#module-imp" title="imp: Access the implementation of the import statement. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code></a> <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14605">bpo-14605</a> - 使导入机制成为显式的 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14646">bpo-14646</a> - 要求加载器设置 __loader__ 和 __package__</p>
<p>The <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function is now powered by <a class="reference internal" href="../library/importlib.html#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code></a>.
This work leads to the completion of &quot;phase 2&quot; of <span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>. There are
multiple benefits to this change. First, it has allowed for more of the
machinery powering import to be exposed instead of being implicit and hidden
within the C code. It also provides a single implementation for all Python VMs
supporting Python 3.3 to use, helping to end any VM-specific deviations in
import semantics. And finally it eases the maintenance of import, allowing for
future growth to occur.</p>
<p>For the common user, there should be no visible change in semantics. For
those whose code currently manipulates import or calls import
programmatically, the code changes that might possibly be required are covered
in the <a class="reference internal" href="#porting-python-code">Porting Python code</a> section of this document.</p>
<section id="new-apis">
<h3>新的API<a class="headerlink" href="#new-apis" title="永久链接至标题"></a></h3>
<p>One of the large benefits of this work is the exposure of what goes into
making the import statement work. That means the various importers that were
once implicit are now fully exposed as part of the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> package.</p>
<p>The abstract base classes defined in <a class="reference internal" href="../library/importlib.html#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> have been expanded
to properly delineate between <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finders</span></a>
and <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finders</span></a> by introducing
<a class="reference internal" href="../library/importlib.html#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and
<a class="reference internal" href="../library/importlib.html#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder</span></code></a>, respectively. The old ABC of
<a class="reference internal" href="../library/importlib.html#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.Finder</span></code></a> is now only provided for backwards-compatibility
and does not enforce any method requirements.</p>
<p>In terms of finders, <a class="reference internal" href="../library/importlib.html#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.FileFinder</span></code></a> exposes the
mechanism used to search for source and bytecode files of a module. Previously
this class was an implicit member of <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>.</p>
<p>For loaders, the new abstract base class <a class="reference internal" href="../library/importlib.html#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.FileLoader</span></code></a> helps
write a loader that uses the file system as the storage mechanism for a module's
code. The loader for source files
(<a class="reference internal" href="../library/importlib.html#importlib.machinery.SourceFileLoader" title="importlib.machinery.SourceFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.SourceFileLoader</span></code></a>), sourceless bytecode files
(<a class="reference internal" href="../library/importlib.html#importlib.machinery.SourcelessFileLoader" title="importlib.machinery.SourcelessFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.SourcelessFileLoader</span></code></a>), and extension modules
(<a class="reference internal" href="../library/importlib.html#importlib.machinery.ExtensionFileLoader" title="importlib.machinery.ExtensionFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.ExtensionFileLoader</span></code></a>) are now available for
direct use.</p>
<p><a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> now has <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">path</span></code> attributes which are set when
there is relevant data to provide. The message for failed imports will also
provide the full name of the module now instead of just the tail end of the
module's name.</p>
<p>The <a class="reference internal" href="../library/importlib.html#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a> function will now call the method with
the same name on all finders cached in <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> to help
clean up any stored state as necessary.</p>
</section>
<section id="visible-changes">
<h3>可见的改变<a class="headerlink" href="#visible-changes" title="永久链接至标题"></a></h3>
<p>For potential required changes to code, see the <a class="reference internal" href="#porting-python-code">Porting Python code</a>
section.</p>
<p>Beyond the expanse of what <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> now exposes, there are other
visible changes to import. The biggest is that <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> and
<a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> now store all of the meta path finders and path entry
hooks used by import. Previously the finders were implicit and hidden within
the C code of import instead of being directly exposed. This means that one can
now easily remove or change the order of the various finders to fit one's needs.</p>
<p>Another change is that all modules have a <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> attribute, storing the
loader used to create the module. <span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> has been updated to make this
attribute mandatory for loaders to implement, so in the future once 3rd-party
loaders have been updated people will be able to rely on the existence of the
attribute. Until such time, though, import is setting the module post-load.</p>
<p>Loaders are also now expected to set the <code class="docutils literal notranslate"><span class="pre">__package__</span></code> attribute from
<span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0366"><strong>PEP 366</strong></a>. Once again, import itself is already setting this on all loaders
from <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> and import itself is setting the attribute post-load.</p>
<p><code class="docutils literal notranslate"><span class="pre">None</span></code> is now inserted into <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> when no finder
can be found on <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>. Since <a class="reference internal" href="../library/imp.html#imp.NullImporter" title="imp.NullImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">imp.NullImporter</span></code></a> is not
directly exposed on <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> it could no longer be relied upon to
always be available to use as a value representing no finder found.</p>
<p>All other changes relate to semantic changes which should be taken into
consideration when updating code for Python 3.3, and thus should be read about
in the <a class="reference internal" href="#porting-python-code">Porting Python code</a> section of this document.</p>
<p>(由 Brett Cannon 编写的实现)</p>
</section>
</section>
<section id="other-language-changes">
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题"></a></h2>
<p>对Python 语言核心进行的小改动:</p>
<ul>
<li><p>Added support for Unicode name aliases and named sequences.
Both <a class="reference internal" href="../library/unicodedata.html#unicodedata.lookup" title="unicodedata.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicodedata.lookup()</span></code></a> and <code class="docutils literal notranslate"><span class="pre">'\N{...}'</span></code> now resolve name aliases,
and <a class="reference internal" href="../library/unicodedata.html#unicodedata.lookup" title="unicodedata.lookup"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicodedata.lookup()</span></code></a> resolves named sequences too.</p>
<p>(由 Ezio Melotti 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12753">bpo-12753</a> 中贡献。)</p>
</li>
<li><p>Unicode 数据库更新至 UCD 版本 6.1.0</p></li>
<li><p>Equality comparisons on <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> objects now return a result reflecting
the equality of the underlying sequences generated by those range objects.
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13201">bpo-13201</a>)</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">count()</span></code>, <code class="docutils literal notranslate"><span class="pre">find()</span></code>, <code class="docutils literal notranslate"><span class="pre">rfind()</span></code>, <code class="docutils literal notranslate"><span class="pre">index()</span></code> and <code class="docutils literal notranslate"><span class="pre">rindex()</span></code>
methods of <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> and <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> objects now accept an
integer between 0 and 255 as their first argument.</p>
<p>(由 Petri Lehtinen 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12170">bpo-12170</a> 中贡献。)</p>
</li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">rjust()</span></code>, <code class="docutils literal notranslate"><span class="pre">ljust()</span></code>, and <code class="docutils literal notranslate"><span class="pre">center()</span></code> methods of <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>
and <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> now accept 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> for the <code class="docutils literal notranslate"><span class="pre">fill</span></code>
argument. (Contributed by Petri Lehtinen in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12380">bpo-12380</a>.)</p></li>
<li><p>New methods have been added to <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> and <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>:
<code class="docutils literal notranslate"><span class="pre">copy()</span></code> and <code class="docutils literal notranslate"><span class="pre">clear()</span></code> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10516">bpo-10516</a>). Consequently,
<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> now also defines a
<code class="xref py py-meth docutils literal notranslate"><span class="pre">clear()</span></code> method (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11388">bpo-11388</a>).</p></li>
<li><p>Raw bytes literals can now be written <code class="docutils literal notranslate"><span class="pre">rb&quot;...&quot;</span></code> as well as <code class="docutils literal notranslate"><span class="pre">br&quot;...&quot;</span></code>.</p>
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13748">bpo-13748</a> 中贡献。)</p>
</li>
<li><p><a class="reference internal" href="../library/stdtypes.html#dict.setdefault" title="dict.setdefault"><code class="xref py py-meth docutils literal notranslate"><span class="pre">dict.setdefault()</span></code></a> now does only one lookup for the given key, making
it atomic when used with built-in types.</p>
<p>(由 Filip Gruszczyński 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13521">bpo-13521</a> 中贡献。)</p>
</li>
<li><p>The error messages produced when a function call does not match the function
signature have been significantly improved.</p>
<p>(由 Benjamin Peterson 贡献。)</p>
</li>
</ul>
</section>
<section id="a-finer-grained-import-lock">
<h2>A Finer-Grained Import Lock<a class="headerlink" href="#a-finer-grained-import-lock" title="永久链接至标题"></a></h2>
<p>Previous versions of CPython have always relied on a global import lock.
This led to unexpected annoyances, such as deadlocks when importing a module
would trigger code execution in a different thread as a side-effect.
Clumsy workarounds were sometimes employed, such as the
<a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModuleNoBlock" title="PyImport_ImportModuleNoBlock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleNoBlock()</span></code></a> C API function.</p>
<p>In Python 3.3, importing a module takes a per-module lock. This correctly
serializes importation of a given module from multiple threads (preventing
the exposure of incompletely initialized modules), while eliminating the
aforementioned annoyances.</p>
<p>(由 Antoine Pitrou 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9260">bpo-9260</a> 中贡献。)</p>
</section>
<section id="builtin-functions-and-types">
<h2>Builtin functions and types<a class="headerlink" href="#builtin-functions-and-types" title="永久链接至标题"></a></h2>
<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> gets a new <em>opener</em> parameter: the underlying file descriptor
for the file object is then obtained by calling <em>opener</em> with (<em>file</em>,
<em>flags</em>). It can be used to use custom flags like <a class="reference internal" href="../library/os.html#os.O_CLOEXEC" title="os.O_CLOEXEC"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.O_CLOEXEC</span></code></a> for
example. The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode was added: open for exclusive creation, failing if
the file already exists.</p></li>
<li><p><a class="reference internal" href="../library/functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a>: added the <em>flush</em> keyword argument. If the <em>flush</em> keyword
argument is true, the stream is forcibly flushed.</p></li>
<li><p><a class="reference internal" href="../library/functions.html#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a>: hash randomization is enabled by default, see
<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__hash__()</span></code></a> and <span class="target" id="index-23"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a>.</p></li>
<li><p>The <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> type gets a new <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> method: return a
casefolded copy of the string, casefolded strings may be used for caseless
matching. For example, <code class="docutils literal notranslate"><span class="pre">'ß'.casefold()</span></code> returns <code class="docutils literal notranslate"><span class="pre">'ss'</span></code>.</p></li>
<li><p>The sequence documentation has been substantially rewritten to better
explain the binary/text sequence distinction and to provide specific
documentation sections for the individual builtin sequence types
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4966">bpo-4966</a>).</p></li>
</ul>
</section>
<section id="new-modules">
<h2>新增模块<a class="headerlink" href="#new-modules" title="永久链接至标题"></a></h2>
<section id="faulthandler">
<h3>faulthandler<a class="headerlink" href="#faulthandler" title="永久链接至标题"></a></h3>
<p>This new debug module <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> contains functions to dump Python tracebacks explicitly,
on a fault (a crash like a segmentation fault), after a timeout, or on a user
signal. Call <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">faulthandler.enable()</span></code></a> to install fault handlers for the
<code class="xref py py-const docutils literal notranslate"><span class="pre">SIGSEGV</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGFPE</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGABRT</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">SIGBUS</span></code>, and
<code class="xref py py-const docutils literal notranslate"><span class="pre">SIGILL</span></code> signals. You can also enable them at startup by setting the
<span class="target" id="index-24"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONFAULTHANDLER"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONFAULTHANDLER</span></code></a> environment variable or by using <a class="reference internal" href="../using/cmdline.html#id5"><code class="xref std std-option docutils literal notranslate"><span class="pre">-X</span></code></a>
<code class="docutils literal notranslate"><span class="pre">faulthandler</span></code> command line option.</p>
<p>Linux 上的段错误示例:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-q<span class="w"> </span>-X<span class="w"> </span>faulthandler
<span class="go">&gt;&gt;&gt; import ctypes</span>
<span class="go">&gt;&gt;&gt; ctypes.string_at(0)</span>
<span class="go">Fatal Python error: Segmentation fault</span>
<span class="go">Current thread 0x00007fb899f39700:</span>
<span class="go"> File &quot;/home/python/cpython/Lib/ctypes/__init__.py&quot;, line 486 in string_at</span>
<span class="go"> File &quot;&lt;stdin&gt;&quot;, line 1 in &lt;module&gt;</span>
<span class="go">Segmentation fault</span>
</pre></div>
</div>
</section>
<section id="ipaddress">
<h3>ipaddress<a class="headerlink" href="#ipaddress" title="永久链接至标题"></a></h3>
<p>The new <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> module provides tools for creating and manipulating
objects representing IPv4 and IPv6 addresses, networks and interfaces (i.e.
an IP address associated with a specific IP subnet).</p>
<p>(由 Google 和 Peter Moody 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3144">bpo-3144</a> 中贡献。)</p>
</section>
<section id="lzma">
<h3>lzma<a class="headerlink" href="#lzma" title="永久链接至标题"></a></h3>
<p>The newly-added <a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module provides data compression and decompression
using the LZMA algorithm, including support for the <code class="docutils literal notranslate"><span class="pre">.xz</span></code> and <code class="docutils literal notranslate"><span class="pre">.lzma</span></code>
file formats.</p>
<p>(由 Nadeem Vawda 和 Per Øyvind Karlsen 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6715">bpo-6715</a> 中贡献。)</p>
</section>
</section>
<section id="improved-modules">
<h2>改进的模块<a class="headerlink" href="#improved-modules" title="永久链接至标题"></a></h2>
<section id="abc">
<h3>abc<a class="headerlink" href="#abc" title="永久链接至标题"></a></h3>
<p>Improved support for abstract base classes containing descriptors composed with
abstract methods. The recommended approach to declaring abstract descriptors is
now to provide <code class="xref py py-attr docutils literal notranslate"><span class="pre">__isabstractmethod__</span></code> as a dynamically updated
property. The built-in descriptors have been updated accordingly.</p>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractproperty" title="abc.abstractproperty"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractproperty</span></code></a> has been deprecated, use <a class="reference internal" href="../library/functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a>
with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractclassmethod" title="abc.abstractclassmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractclassmethod</span></code></a> has been deprecated, use
<a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">classmethod</span></code></a> with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractstaticmethod" title="abc.abstractstaticmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractstaticmethod</span></code></a> has been deprecated, use
<a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">staticmethod</span></code></a> with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
</ul>
</div></blockquote>
<p>(由 Pablo Galindo 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11610">bpo-11610</a> 中贡献。)</p>
<p><a class="reference internal" href="../library/abc.html#abc.ABCMeta.register" title="abc.ABCMeta.register"><code class="xref py py-meth docutils literal notranslate"><span class="pre">abc.ABCMeta.register()</span></code></a> now returns the registered subclass, which means
it can now be used as a class decorator (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10868">bpo-10868</a>).</p>
</section>
<section id="array">
<h3>array<a class="headerlink" href="#array" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> module supports the <code class="xref c c-type docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code> type using <code class="docutils literal notranslate"><span class="pre">q</span></code> and
<code class="docutils literal notranslate"><span class="pre">Q</span></code> type codes.</p>
<p>(由 Oren Tirosh 和 Hirokazu Yamamoto 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1172711">bpo-1172711</a> 中贡献。)</p>
</section>
<section id="base64">
<h3>base64<a class="headerlink" href="#base64" title="永久链接至标题"></a></h3>
<p>ASCII-only Unicode strings are now accepted by the decoding functions of the
<a class="reference internal" href="../library/base64.html#module-base64" title="base64: RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a> modern interface. For example, <code class="docutils literal notranslate"><span class="pre">base64.b64decode('YWJj')</span></code>
returns <code class="docutils literal notranslate"><span class="pre">b'abc'</span></code>. (Contributed by Catalin Iacob in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13641">bpo-13641</a>.)</p>
</section>
<section id="binascii">
<h3>binascii<a class="headerlink" href="#binascii" title="永久链接至标题"></a></h3>
<p>In addition to the binary objects they normally accept, the <code class="docutils literal notranslate"><span class="pre">a2b_</span></code> functions
now all also accept ASCII-only strings as input. (Contributed by Antoine
Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13637">bpo-13637</a>.)</p>
</section>
<section id="bz2">
<h3>bz2<a class="headerlink" href="#bz2" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> 模块已被重新编写。 在此过程中,添加了一些新的特征:</p>
<ul>
<li><p>新的 <a class="reference internal" href="../library/bz2.html#bz2.open" title="bz2.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">bz2.open()</span></code></a> 函数:以二进制或文本模式打开 bzip2 压缩文件。</p></li>
<li><p><a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> 现在可以读写任意文件型对象,具体方式是通过其构造器的 <em>fileobj</em> 参数。</p>
<p>(由 Nadeem Vawda 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5863">bpo-5863</a> 中贡献。)</p>
</li>
<li><p><a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> and <a class="reference internal" href="../library/bz2.html#bz2.decompress" title="bz2.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">bz2.decompress()</span></code></a> can now decompress
multi-stream inputs (such as those produced by the <strong class="program">pbzip2</strong> tool).
<a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> can now also be used to create this type of file, using
the <code class="docutils literal notranslate"><span class="pre">'a'</span></code> (append) mode.</p>
<p>(由 Nir Aides 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1625">bpo-1625</a> 中贡献。)</p>
</li>
<li><p><a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> now implements all of the <a class="reference internal" href="../library/io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> API,
except for the <code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code> methods.</p></li>
</ul>
</section>
<section id="codecs">
<h3>编码器<a class="headerlink" href="#codecs" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/codecs.html#module-encodings.mbcs" title="encodings.mbcs: Windows ANSI codepage"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mbcs</span></code></a> codec has been rewritten to handle correctly
<code class="docutils literal notranslate"><span class="pre">replace</span></code> and <code class="docutils literal notranslate"><span class="pre">ignore</span></code> error handlers on all Windows versions. The
<a class="reference internal" href="../library/codecs.html#module-encodings.mbcs" title="encodings.mbcs: Windows ANSI codepage"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mbcs</span></code></a> codec now supports all error handlers, instead of only
<code class="docutils literal notranslate"><span class="pre">replace</span></code> to encode and <code class="docutils literal notranslate"><span class="pre">ignore</span></code> to decode.</p>
<p>A new Windows-only codec has been added: <code class="docutils literal notranslate"><span class="pre">cp65001</span></code> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13216">bpo-13216</a>). It is the
Windows code page 65001 (Windows UTF-8, <code class="docutils literal notranslate"><span class="pre">CP_UTF8</span></code>). For example, it is used
by <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> if the console output code page is set to cp65001 (e.g., using
<code class="docutils literal notranslate"><span class="pre">chcp</span> <span class="pre">65001</span></code> command).</p>
<p>Multibyte CJK decoders now resynchronize faster. They only ignore the first
byte of an invalid byte sequence. For example, <code class="docutils literal notranslate"><span class="pre">b'\xff\n'.decode('gb2312',</span>
<span class="pre">'replace')</span></code> now returns a <code class="docutils literal notranslate"><span class="pre">\n</span></code> after the replacement character.</p>
<p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12016">bpo-12016</a>)</p>
<p>Incremental CJK codec encoders are no longer reset at each call to their
encode() methods. For example:</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">codecs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoder</span> <span class="o">=</span> <span class="n">codecs</span><span class="o">.</span><span class="n">getincrementalencoder</span><span class="p">(</span><span class="s1">&#39;hz&#39;</span><span class="p">)(</span><span class="s1">&#39;strict&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="sa">b</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">encoder</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="s1">&#39;</span><span class="se">\u52ff\u65bd\u65bc\u4eba\u3002</span><span class="s1"> Bye.&#39;</span><span class="p">)</span>
<span class="go">b&#39;~{NpJ)l6HK!#~} Bye.&#39;</span>
</pre></div>
</div>
<p>This example gives <code class="docutils literal notranslate"><span class="pre">b'~{Np~}~{J)~}~{l6~}~{HK~}~{!#~}</span> <span class="pre">Bye.'</span></code> with older Python
versions.</p>
<p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12100">bpo-12100</a>)</p>
<p><code class="docutils literal notranslate"><span class="pre">unicode_internal</span></code> 编解码器已被弃用。</p>
</section>
<section id="collections">
<h3>collections<a class="headerlink" href="#collections" title="永久链接至标题"></a></h3>
<p>Addition of a new <a class="reference internal" href="../library/collections.html#collections.ChainMap" title="collections.ChainMap"><code class="xref py py-class docutils literal notranslate"><span class="pre">ChainMap</span></code></a> class to allow treating a
number of mappings as a single unit. (Written by Raymond Hettinger for
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11089">bpo-11089</a>, made public in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11297">bpo-11297</a>.)</p>
<p>The abstract base classes have been moved in a new <a class="reference internal" href="../library/collections.abc.html#module-collections.abc" title="collections.abc: Abstract base classes for containers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections.abc</span></code></a>
module, to better differentiate between the abstract and the concrete
collections classes. Aliases for ABCs are still present in the
<a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal notranslate"><span class="pre">collections</span></code></a> module to preserve existing imports. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11085">bpo-11085</a>)</p>
<p>The <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Counter</span></code></a> class now supports the unary <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">-</span></code>
operators, as well as the in-place operators <code class="docutils literal notranslate"><span class="pre">+=</span></code>, <code class="docutils literal notranslate"><span class="pre">-=</span></code>, <code class="docutils literal notranslate"><span class="pre">|=</span></code>, and
<code class="docutils literal notranslate"><span class="pre">&amp;=</span></code>. (Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13121">bpo-13121</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.ExitStack" title="contextlib.ExitStack"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExitStack</span></code></a> now provides a solid foundation for
programmatic manipulation of context managers and similar cleanup
functionality. Unlike the previous <code class="docutils literal notranslate"><span class="pre">contextlib.nested</span></code> API (which was
deprecated and removed), the new API is designed to work correctly
regardless of whether context managers acquire their resources in
their <code class="docutils literal notranslate"><span class="pre">__init__</span></code> method (for example, file objects) or in their
<code class="docutils literal notranslate"><span class="pre">__enter__</span></code> method (for example, synchronisation objects from the
<a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module).</p>
<p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13585">bpo-13585</a>)</p>
</section>
<section id="crypt">
<h3>crypt<a class="headerlink" href="#crypt" title="永久链接至标题"></a></h3>
<p>Addition of salt and modular crypt format (hashing method) and the <a class="reference internal" href="../library/crypt.html#crypt.mksalt" title="crypt.mksalt"><code class="xref py py-func docutils literal notranslate"><span class="pre">mksalt()</span></code></a>
function to the <a class="reference internal" href="../library/crypt.html#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> module.</p>
<p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10924">bpo-10924</a>)</p>
</section>
<section id="curses">
<h3>curses<a class="headerlink" href="#curses" title="永久链接至标题"></a></h3>
<blockquote>
<div><ul class="simple">
<li><p>If the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> module is linked to the ncursesw library, use Unicode
functions when Unicode strings or characters are passed (e.g.
<code class="xref c c-func docutils literal notranslate"><span class="pre">waddwstr()</span></code>), and bytes functions otherwise (e.g. <code class="xref c c-func docutils literal notranslate"><span class="pre">waddstr()</span></code>).</p></li>
<li><p>Use the locale encoding instead of <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> to encode Unicode strings.</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">curses.window</span></code> 添加了新的 <a class="reference internal" href="../library/curses.html#curses.window.encoding" title="curses.window.encoding"><code class="xref py py-attr docutils literal notranslate"><span class="pre">curses.window.encoding</span></code></a> 属性。</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">curses.window</span></code> 类有一个新的 <a class="reference internal" href="../library/curses.html#curses.window.get_wch" title="curses.window.get_wch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_wch()</span></code></a> 方法用来获取一个宽字符。</p></li>
<li><p><a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">curses</span></code></a> 模块有一个新的 <a class="reference internal" href="../library/curses.html#curses.unget_wch" title="curses.unget_wch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unget_wch()</span></code></a> 函数用来推入一个宽字符以便下一个 <a class="reference internal" href="../library/curses.html#curses.window.get_wch" title="curses.window.get_wch"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_wch()</span></code></a> 将返回它。</p></li>
</ul>
</div></blockquote>
<p>(由 Iñigo Serna 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6755">bpo-6755</a> 中贡献。)</p>
</section>
<section id="datetime">
<h3>datetime<a class="headerlink" href="#datetime" title="永久链接至标题"></a></h3>
<blockquote>
<div><ul class="simple">
<li><p>Equality comparisons between naive and aware <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
instances now return <a class="reference internal" href="../library/constants.html#False" title="False"><code class="xref py py-const docutils literal notranslate"><span class="pre">False</span></code></a> instead of raising <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15006">bpo-15006</a>).</p></li>
<li><p>New <a class="reference internal" href="../library/datetime.html#datetime.datetime.timestamp" title="datetime.datetime.timestamp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.timestamp()</span></code></a> method: Return POSIX timestamp
corresponding to the <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instance.</p></li>
<li><p>The <a class="reference internal" href="../library/datetime.html#datetime.datetime.strftime" title="datetime.datetime.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.strftime()</span></code></a> method supports formatting years
older than 1000.</p></li>
<li><p>The <a class="reference internal" href="../library/datetime.html#datetime.datetime.astimezone" title="datetime.datetime.astimezone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.datetime.astimezone()</span></code></a> method can now be
called without arguments to convert datetime instance to the system
timezone.</p></li>
</ul>
</div></blockquote>
</section>
<section id="decimal">
<span id="new-decimal"></span><h3>decimal<a class="headerlink" href="#decimal" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7652">bpo-7652</a> - integrate fast native decimal arithmetic.</dt><dd><p>C-module and libmpdec written by Stefan Krah.</p>
</dd>
</dl>
<p>The new C version of the decimal module integrates the high speed libmpdec
library for arbitrary precision correctly-rounded decimal floating point
arithmetic. libmpdec conforms to IBM's General Decimal Arithmetic Specification.</p>
<p>Performance gains range from 10x for database applications to 100x for
numerically intensive applications. These numbers are expected gains
for standard precisions used in decimal floating point arithmetic. Since
the precision is user configurable, the exact figures may vary. For example,
in integer bignum arithmetic the differences can be significantly higher.</p>
<p>The following table is meant as an illustration. Benchmarks are available
at <a class="reference external" href="http://www.bytereef.org/mpdecimal/quickstart.html">http://www.bytereef.org/mpdecimal/quickstart.html</a>.</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 18%" />
<col style="width: 27%" />
<col style="width: 29%" />
<col style="width: 27%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"></th>
<th class="head"><p>decimal.py</p></th>
<th class="head"><p>_decimal</p></th>
<th class="head"><p>加速</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>pi</p></td>
<td><p>42.02秒</p></td>
<td><p>0.345秒</p></td>
<td><p>120倍</p></td>
</tr>
<tr class="row-odd"><td><p>telco</p></td>
<td><p>172.19秒</p></td>
<td><p>5.68秒</p></td>
<td><p>30倍</p></td>
</tr>
<tr class="row-even"><td><p>psycopg</p></td>
<td><p>3.57秒</p></td>
<td><p>0.29秒</p></td>
<td><p>12倍</p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<section id="id1">
<h4>相关特性<a class="headerlink" href="#id1" title="永久链接至标题"></a></h4>
<ul class="simple">
<li><p>The <a class="reference internal" href="../library/decimal.html#decimal.FloatOperation" title="decimal.FloatOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FloatOperation</span></code></a> signal optionally enables stricter
semantics for mixing floats and Decimals.</p></li>
<li><p>If Python is compiled without threads, the C version automatically
disables the expensive thread local context machinery. In this case,
the variable <a class="reference internal" href="../library/decimal.html#decimal.HAVE_THREADS" title="decimal.HAVE_THREADS"><code class="xref py py-data docutils literal notranslate"><span class="pre">HAVE_THREADS</span></code></a> is set to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p></li>
</ul>
</section>
<section id="id2">
<h4>API 的变化<a class="headerlink" href="#id2" title="永久链接至标题"></a></h4>
<ul>
<li><p>C模块上下文限制如下表具体取决于计算机体系结构</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 27%" />
<col style="width: 30%" />
<col style="width: 43%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"></th>
<th class="head"><p>32位</p></th>
<th class="head"><p>64位</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">MAX_PREC</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">425000000</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">999999999999999999</span></code></p></td>
</tr>
<tr class="row-odd"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">MAX_EMAX</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">425000000</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">999999999999999999</span></code></p></td>
</tr>
<tr class="row-even"><td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">MIN_EMIN</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">-425000000</span></code></p></td>
<td><p><code class="xref py py-const docutils literal notranslate"><span class="pre">-999999999999999999</span></code></p></td>
</tr>
</tbody>
</table>
</div></blockquote>
</li>
<li><p>In the context templates (<a class="reference internal" href="../library/decimal.html#decimal.DefaultContext" title="decimal.DefaultContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">DefaultContext</span></code></a>,
<a class="reference internal" href="../library/decimal.html#decimal.BasicContext" title="decimal.BasicContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">BasicContext</span></code></a> and <a class="reference internal" href="../library/decimal.html#decimal.ExtendedContext" title="decimal.ExtendedContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExtendedContext</span></code></a>)
the magnitude of <code class="xref py py-attr docutils literal notranslate"><span class="pre">Emax</span></code> and
<code class="xref py py-attr docutils literal notranslate"><span class="pre">Emin</span></code> has changed to <code class="xref py py-const docutils literal notranslate"><span class="pre">999999</span></code>.</p></li>
<li><p>The <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> constructor in decimal.py does not observe
the context limits and converts values with arbitrary exponents or precision
exactly. Since the C version has internal limits, the following scheme is
used: If possible, values are converted exactly, otherwise
<a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-exc docutils literal notranslate"><span class="pre">InvalidOperation</span></code></a> is raised and the result is NaN. In the
latter case it is always possible to use <a class="reference internal" href="../library/decimal.html#decimal.Context.create_decimal" title="decimal.Context.create_decimal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_decimal()</span></code></a>
in order to obtain a rounded or inexact value.</p></li>
<li><p>The power function in decimal.py is always correctly-rounded. In the
C version, it is defined in terms of the correctly-rounded
<a class="reference internal" href="../library/decimal.html#decimal.Decimal.exp" title="decimal.Decimal.exp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exp()</span></code></a> and <a class="reference internal" href="../library/decimal.html#decimal.Decimal.ln" title="decimal.Decimal.ln"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ln()</span></code></a> functions,
but the final result is only &quot;almost always correctly rounded&quot;.</p></li>
<li><p>In the C version, the context dictionary containing the signals is a
<a class="reference internal" href="../library/collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableMapping</span></code></a>. For speed reasons,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">flags</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">traps</span></code> always
refer to the same <a class="reference internal" href="../library/collections.abc.html#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">MutableMapping</span></code></a> that the context
was initialized with. If a new signal dictionary is assigned,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">flags</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">traps</span></code>
are updated with the new values, but they do not reference the RHS
dictionary.</p></li>
<li><p>Pickling a <a class="reference internal" href="../library/decimal.html#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a> produces a different output in order
to have a common interchange format for the Python and C versions.</p></li>
<li><p>The order of arguments in the <a class="reference internal" href="../library/decimal.html#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a> constructor has been
changed to match the order displayed by <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>.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">watchexp</span></code> parameter in the <a class="reference internal" href="../library/decimal.html#decimal.Decimal.quantize" title="decimal.Decimal.quantize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">quantize()</span></code></a> method
is deprecated.</p></li>
</ul>
</section>
</section>
<section id="email">
<span id="new-email"></span><h3>email<a class="headerlink" href="#email" title="永久链接至标题"></a></h3>
<section id="policy-framework">
<h4>Policy Framework<a class="headerlink" href="#policy-framework" title="永久链接至标题"></a></h4>
<p>The email package now has a <a class="reference internal" href="../library/email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">policy</span></code></a> framework. A
<a class="reference internal" href="../library/email.policy.html#email.policy.Policy" title="email.policy.Policy"><code class="xref py py-class docutils literal notranslate"><span class="pre">Policy</span></code></a> is an object with several methods and properties
that control how the email package behaves. The primary policy for Python 3.3
is the <a class="reference internal" href="../library/email.policy.html#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compat32</span></code></a> policy, which provides backward
compatibility with the email package in Python 3.2. A <code class="docutils literal notranslate"><span class="pre">policy</span></code> can be
specified when an email message is parsed by a <a class="reference internal" href="../library/email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a>, or when a
<a class="reference internal" href="../library/email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object is created, or when an email is
serialized using a <a class="reference internal" href="../library/email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">generator</span></code></a>. Unless overridden, a policy passed
to a <code class="docutils literal notranslate"><span class="pre">parser</span></code> is inherited by all the <code class="docutils literal notranslate"><span class="pre">Message</span></code> object and sub-objects
created by the <code class="docutils literal notranslate"><span class="pre">parser</span></code>. By default a <code class="docutils literal notranslate"><span class="pre">generator</span></code> will use the policy of
the <code class="docutils literal notranslate"><span class="pre">Message</span></code> object it is serializing. The default policy is
<a class="reference internal" href="../library/email.policy.html#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-data docutils literal notranslate"><span class="pre">compat32</span></code></a>.</p>
<p>The minimum set of controls implemented by all <code class="docutils literal notranslate"><span class="pre">policy</span></code> objects are:</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 21%" />
<col style="width: 79%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>max_line_length</p></td>
<td><p>The maximum length, excluding the linesep character(s),
individual lines may have when a <code class="docutils literal notranslate"><span class="pre">Message</span></code> is
serialized. Defaults to 78.</p></td>
</tr>
<tr class="row-even"><td><p>linesep</p></td>
<td><p>The character used to separate individual lines when a
<code class="docutils literal notranslate"><span class="pre">Message</span></code> is serialized. Defaults to <code class="docutils literal notranslate"><span class="pre">\n</span></code>.</p></td>
</tr>
<tr class="row-odd"><td><p>cte_type</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">7bit</span></code> or <code class="docutils literal notranslate"><span class="pre">8bit</span></code>. <code class="docutils literal notranslate"><span class="pre">8bit</span></code> applies only to a
<code class="docutils literal notranslate"><span class="pre">Bytes</span></code> <code class="docutils literal notranslate"><span class="pre">generator</span></code>, and means that non-ASCII may
be used where allowed by the protocol (or where it
exists in the original input).</p></td>
</tr>
<tr class="row-even"><td><p>raise_on_defect</p></td>
<td><p>导致一个 <code class="docutils literal notranslate"><span class="pre">parser</span></code> 在遇到缺陷时引发错误而不是将它们添加到 <code class="docutils literal notranslate"><span class="pre">Message</span></code> 对象的 <code class="docutils literal notranslate"><span class="pre">defects</span></code> 列表。</p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>A new policy instance, with new settings, is created using the
<a class="reference internal" href="../library/email.policy.html#email.policy.Policy.clone" title="email.policy.Policy.clone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clone()</span></code></a> method of policy objects. <code class="docutils literal notranslate"><span class="pre">clone</span></code> takes
any of the above controls as keyword arguments. Any control not specified in
the call retains its default value. Thus you can create a policy that uses
<code class="docutils literal notranslate"><span class="pre">\r\n</span></code> linesep characters like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">mypolicy</span> <span class="o">=</span> <span class="n">compat32</span><span class="o">.</span><span class="n">clone</span><span class="p">(</span><span class="n">linesep</span><span class="o">=</span><span class="s1">&#39;</span><span class="se">\r\n</span><span class="s1">&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Policies can be used to make the generation of messages in the format needed by
your application simpler. Instead of having to remember to specify
<code class="docutils literal notranslate"><span class="pre">linesep='\r\n'</span></code> in all the places you call a <code class="docutils literal notranslate"><span class="pre">generator</span></code>, you can specify
it once, when you set the policy used by the <code class="docutils literal notranslate"><span class="pre">parser</span></code> or the <code class="docutils literal notranslate"><span class="pre">Message</span></code>,
whichever your program uses to create <code class="docutils literal notranslate"><span class="pre">Message</span></code> objects. On the other hand,
if you need to generate messages in multiple forms, you can still specify the
parameters in the appropriate <code class="docutils literal notranslate"><span class="pre">generator</span></code> call. Or you can have custom
policy instances for your different cases, and pass those in when you create
the <code class="docutils literal notranslate"><span class="pre">generator</span></code>.</p>
</section>
<section id="provisional-policy-with-new-header-api">
<h4>Provisional Policy with New Header API<a class="headerlink" href="#provisional-policy-with-new-header-api" title="永久链接至标题"></a></h4>
<p>While the policy framework is worthwhile all by itself, the main motivation for
introducing it is to allow the creation of new policies that implement new
features for the email package in a way that maintains backward compatibility
for those who do not use the new policies. Because the new policies introduce a
new API, we are releasing them in Python 3.3 as a <a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">provisional policy</span></a>. Backwards incompatible changes (up to and including
removal of the code) may occur if deemed necessary by the core developers.</p>
<p>The new policies are instances of <a class="reference internal" href="../library/email.policy.html#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a>,
and add the following additional controls:</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 21%" />
<col style="width: 79%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p>refold_source</p></td>
<td><p>Controls whether or not headers parsed by a
<a class="reference internal" href="../library/email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a> are refolded by the
<a class="reference internal" href="../library/email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">generator</span></code></a>. It can be <code class="docutils literal notranslate"><span class="pre">none</span></code>, <code class="docutils literal notranslate"><span class="pre">long</span></code>,
or <code class="docutils literal notranslate"><span class="pre">all</span></code>. The default is <code class="docutils literal notranslate"><span class="pre">long</span></code>, which means that
source headers with a line longer than
<code class="docutils literal notranslate"><span class="pre">max_line_length</span></code> get refolded. <code class="docutils literal notranslate"><span class="pre">none</span></code> means no
line get refolded, and <code class="docutils literal notranslate"><span class="pre">all</span></code> means that all lines
get refolded.</p></td>
</tr>
<tr class="row-even"><td><p>header_factory</p></td>
<td><p>A callable that take a <code class="docutils literal notranslate"><span class="pre">name</span></code> and <code class="docutils literal notranslate"><span class="pre">value</span></code> and
produces a custom header object.</p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>The <code class="docutils literal notranslate"><span class="pre">header_factory</span></code> is the key to the new features provided by the new
policies. When one of the new policies is used, any header retrieved from
a <code class="docutils literal notranslate"><span class="pre">Message</span></code> object is an object produced by the <code class="docutils literal notranslate"><span class="pre">header_factory</span></code>, and any
time you set a header on a <code class="docutils literal notranslate"><span class="pre">Message</span></code> it becomes an object produced by
<code class="docutils literal notranslate"><span class="pre">header_factory</span></code>. All such header objects have a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute equal
to the header name. Address and Date headers have additional attributes
that give you access to the parsed data of the header. This means you can now
do things like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">Message</span><span class="p">(</span><span class="n">policy</span><span class="o">=</span><span class="n">SMTP</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Éric &lt;foo@example.com&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span>
<span class="go">&#39;Éric &lt;foo@example.com&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span>
<span class="go">(Address(display_name=&#39;Éric&#39;, username=&#39;foo&#39;, domain=&#39;example.com&#39;),)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">username</span>
<span class="go">&#39;foo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">display_name</span>
<span class="go">&#39;Éric&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">localtime</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">datetime</span>
<span class="go">datetime.datetime(2012, 5, 25, 21, 39, 24, 465484, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000), &#39;EDT&#39;))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span>
<span class="go">&#39;Fri, 25 May 2012 21:44:27 -0400&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="go">To: =?utf-8?q?=C3=89ric?= &lt;foo@example.com&gt;</span>
<span class="go">Date: Fri, 25 May 2012 21:44:27 -0400</span>
</pre></div>
</div>
<p>You will note that the unicode display name is automatically encoded as
<code class="docutils literal notranslate"><span class="pre">utf-8</span></code> when the message is serialized, but that when the header is accessed
directly, you get the unicode version. This eliminates any need to deal with
the <a class="reference internal" href="../library/email.header.html#module-email.header" title="email.header: Representing non-ASCII headers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.header</span></code></a> <a class="reference internal" href="../library/email.header.html#email.header.decode_header" title="email.header.decode_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decode_header()</span></code></a> or
<a class="reference internal" href="../library/email.header.html#email.header.make_header" title="email.header.make_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make_header()</span></code></a> functions.</p>
<p>You can also create addresses from parts:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="s1">&#39;cc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="n">Group</span><span class="p">(</span><span class="s1">&#39;pals&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">Address</span><span class="p">(</span><span class="s1">&#39;Bob&#39;</span><span class="p">,</span> <span class="s1">&#39;bob&#39;</span><span class="p">,</span> <span class="s1">&#39;example.com&#39;</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Address</span><span class="p">(</span><span class="s1">&#39;Sally&#39;</span><span class="p">,</span> <span class="s1">&#39;sally&#39;</span><span class="p">,</span> <span class="s1">&#39;example.com&#39;</span><span class="p">)]),</span>
<span class="gp">... </span> <span class="n">Address</span><span class="p">(</span><span class="s1">&#39;Bonzo&#39;</span><span class="p">,</span> <span class="n">addr_spec</span><span class="o">=</span><span class="s1">&#39;bonz@laugh.com&#39;</span><span class="p">)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="go">To: =?utf-8?q?=C3=89ric?= &lt;foo@example.com&gt;</span>
<span class="go">Date: Fri, 25 May 2012 21:44:27 -0400</span>
<span class="go">cc: pals: Bob &lt;bob@example.com&gt;, Sally &lt;sally@example.com&gt;;, Bonzo &lt;bonz@laugh.com&gt;</span>
</pre></div>
</div>
<p>Decoding to unicode is done automatically:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span> <span class="o">=</span> <span class="n">message_from_string</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">m</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="p">[</span><span class="s1">&#39;to&#39;</span><span class="p">]</span>
<span class="go">&#39;Éric &lt;foo@example.com&gt;&#39;</span>
</pre></div>
</div>
<p>When you parse a message, you can use the <code class="docutils literal notranslate"><span class="pre">addresses</span></code> and <code class="docutils literal notranslate"><span class="pre">groups</span></code>
attributes of the header objects to access the groups and individual
addresses:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="p">[</span><span class="s1">&#39;cc&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">addresses</span>
<span class="go">(Address(display_name=&#39;Bob&#39;, username=&#39;bob&#39;, domain=&#39;example.com&#39;), Address(display_name=&#39;Sally&#39;, username=&#39;sally&#39;, domain=&#39;example.com&#39;), Address(display_name=&#39;Bonzo&#39;, username=&#39;bonz&#39;, domain=&#39;laugh.com&#39;))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="p">[</span><span class="s1">&#39;cc&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">groups</span>
<span class="go">(Group(display_name=&#39;pals&#39;, addresses=(Address(display_name=&#39;Bob&#39;, username=&#39;bob&#39;, domain=&#39;example.com&#39;), Address(display_name=&#39;Sally&#39;, username=&#39;sally&#39;, domain=&#39;example.com&#39;)), Group(display_name=None, addresses=(Address(display_name=&#39;Bonzo&#39;, username=&#39;bonz&#39;, domain=&#39;laugh.com&#39;),))</span>
</pre></div>
</div>
<p>In summary, if you use one of the new policies, header manipulation works the
way it ought to: your application works with unicode strings, and the email
package transparently encodes and decodes the unicode to and from the RFC
standard Content Transfer Encodings.</p>
</section>
<section id="other-api-changes">
<h4>Other API Changes<a class="headerlink" href="#other-api-changes" title="永久链接至标题"></a></h4>
<p>New <a class="reference internal" href="../library/email.parser.html#email.parser.BytesHeaderParser" title="email.parser.BytesHeaderParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesHeaderParser</span></code></a>, added to the <a class="reference internal" href="../library/email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code></a>
module to complement <a class="reference internal" href="../library/email.parser.html#email.parser.HeaderParser" title="email.parser.HeaderParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">HeaderParser</span></code></a> and complete the Bytes
API.</p>
<p>New utility functions:</p>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference internal" href="../library/email.utils.html#email.utils.format_datetime" title="email.utils.format_datetime"><code class="xref py py-func docutils literal notranslate"><span class="pre">format_datetime()</span></code></a>: given a <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>,
produce a string formatted for use in an email header.</p></li>
<li><p><a class="reference internal" href="../library/email.utils.html#email.utils.parsedate_to_datetime" title="email.utils.parsedate_to_datetime"><code class="xref py py-func docutils literal notranslate"><span class="pre">parsedate_to_datetime()</span></code></a>: given a date string from
an email header, convert it into an aware <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>,
or a naive <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> if the offset is <code class="docutils literal notranslate"><span class="pre">-0000</span></code>.</p></li>
<li><p><a class="reference internal" href="../library/email.utils.html#email.utils.localtime" title="email.utils.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a>: With no argument, returns the
current local time as an aware <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> using the local
<a class="reference internal" href="../library/datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>. Given an aware <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>,
converts it into an aware <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> using the
local <a class="reference internal" href="../library/datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a>.</p></li>
</ul>
</div></blockquote>
</section>
</section>
<section id="ftplib">
<h3>ftplib<a class="headerlink" href="#ftplib" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p><a class="reference internal" href="../library/ftplib.html#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">ftplib.FTP</span></code></a> now accepts a <code class="docutils literal notranslate"><span class="pre">source_address</span></code> keyword argument to
specify the <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> to use as the source address in the bind call
when creating the outgoing socket. (Contributed by Giampaolo Rodolà
in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8594">bpo-8594</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP_TLS</span></code></a> class now provides a new
<a class="reference internal" href="../library/ftplib.html#ftplib.FTP_TLS.ccc" title="ftplib.FTP_TLS.ccc"><code class="xref py py-func docutils literal notranslate"><span class="pre">ccc()</span></code></a> function to revert control channel back to
plaintext. This can be useful to take advantage of firewalls that know how
to handle NAT with non-secure FTP without opening fixed ports. (Contributed
by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12139">bpo-12139</a>.)</p></li>
<li><p>Added <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.mlsd" title="ftplib.FTP.mlsd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.mlsd()</span></code></a> method which provides a parsable directory
listing format and deprecates <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.nlst" title="ftplib.FTP.nlst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.nlst()</span></code></a> and
<a class="reference internal" href="../library/ftplib.html#ftplib.FTP.dir" title="ftplib.FTP.dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.dir()</span></code></a>. (Contributed by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11072">bpo-11072</a>.)</p></li>
</ul>
</section>
<section id="functools">
<h3>functools<a class="headerlink" href="#functools" title="永久链接至标题"></a></h3>
<p>The <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> decorator now accepts a <code class="docutils literal notranslate"><span class="pre">typed</span></code> keyword
argument (that defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code> to ensure that it caches values of
different types that compare equal in separate cache slots. (Contributed
by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13227">bpo-13227</a>.)</p>
</section>
<section id="gc">
<h3>gc<a class="headerlink" href="#gc" title="永久链接至标题"></a></h3>
<p>It is now possible to register callbacks invoked by the garbage collector
before and after collection using the new <a class="reference internal" href="../library/gc.html#gc.callbacks" title="gc.callbacks"><code class="xref py py-data docutils literal notranslate"><span class="pre">callbacks</span></code></a> list.</p>
</section>
<section id="hmac">
<h3>hmac<a class="headerlink" href="#hmac" title="永久链接至标题"></a></h3>
<p>A new <a class="reference internal" href="../library/hmac.html#hmac.compare_digest" title="hmac.compare_digest"><code class="xref py py-func docutils literal notranslate"><span class="pre">compare_digest()</span></code></a> function has been added to prevent side
channel attacks on digests through timing analysis. (Contributed by Nick
Coghlan and Christian Heimes in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15061">bpo-15061</a>.)</p>
</section>
<section id="http">
<h3>http<a class="headerlink" href="#http" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/http.server.html#http.server.BaseHTTPRequestHandler" title="http.server.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.server.BaseHTTPRequestHandler</span></code></a> now buffers the headers and writes
them all at once when <a class="reference internal" href="../library/http.server.html#http.server.BaseHTTPRequestHandler.end_headers" title="http.server.BaseHTTPRequestHandler.end_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">end_headers()</span></code></a> is
called. A new method <a class="reference internal" href="../library/http.server.html#http.server.BaseHTTPRequestHandler.flush_headers" title="http.server.BaseHTTPRequestHandler.flush_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush_headers()</span></code></a>
can be used to directly manage when the accumulated headers are sent.
(Contributed by Andrew Schaaf in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3709">bpo-3709</a>.)</p>
<p><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-class docutils literal notranslate"><span class="pre">http.server</span></code></a> now produces valid <code class="docutils literal notranslate"><span class="pre">HTML</span> <span class="pre">4.01</span> <span class="pre">strict</span></code> output.
(Contributed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13295">bpo-13295</a>.)</p>
<p><a class="reference internal" href="../library/http.client.html#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPResponse</span></code></a> now has a
<a class="reference internal" href="../library/http.client.html#http.client.HTTPResponse.readinto" title="http.client.HTTPResponse.readinto"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readinto()</span></code></a> method, which means it can be used
as an <a class="reference internal" href="../library/io.html#io.RawIOBase" title="io.RawIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.RawIOBase</span></code></a> class. (Contributed by John Kuhn in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13464">bpo-13464</a>.)</p>
</section>
<section id="html">
<h3>html<a class="headerlink" href="#html" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/html.parser.html#html.parser.HTMLParser" title="html.parser.HTMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">html.parser.HTMLParser</span></code></a> is now able to parse broken markup without
raising errors, therefore the <em>strict</em> argument of the constructor and the
<code class="xref py py-exc docutils literal notranslate"><span class="pre">HTMLParseError</span></code> exception are now deprecated.
The ability to parse broken markup is the result of a number of bug fixes that
are also available on the latest bug fix releases of Python 2.7/3.2.
(Contributed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15114">bpo-15114</a>, and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14538">bpo-14538</a>,
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13993">bpo-13993</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13960">bpo-13960</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13358">bpo-13358</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1745761">bpo-1745761</a>,
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=755670">bpo-755670</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13357">bpo-13357</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12629">bpo-12629</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1200313">bpo-1200313</a>,
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=670664">bpo-670664</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13273">bpo-13273</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12888">bpo-12888</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7311">bpo-7311</a>.)</p>
<p>A new <a class="reference internal" href="../library/html.entities.html#html.entities.html5" title="html.entities.html5"><code class="xref py py-data docutils literal notranslate"><span class="pre">html5</span></code></a> dictionary that maps HTML5 named character
references to the equivalent Unicode character(s) (e.g. <code class="docutils literal notranslate"><span class="pre">html5['gt;']</span> <span class="pre">==</span>
<span class="pre">'&gt;'</span></code>) has been added to the <a class="reference internal" href="../library/html.entities.html#module-html.entities" title="html.entities: Definitions of HTML general entities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html.entities</span></code></a> module. The dictionary is
now also used by <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>. (Contributed by Ezio
Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11113">bpo-11113</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15156">bpo-15156</a>.)</p>
</section>
<section id="imaplib">
<h3>imaplib<a class="headerlink" href="#imaplib" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4_SSL" title="imaplib.IMAP4_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">IMAP4_SSL</span></code></a> constructor now accepts an SSLContext
parameter to control parameters of the secure channel.</p>
<p>(由 Sijin Joseph 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8808">bpo-8808</a> 中贡献。)</p>
</section>
<section id="inspect">
<h3>inspect<a class="headerlink" href="#inspect" title="永久链接至标题"></a></h3>
<p>A new <a class="reference internal" href="../library/inspect.html#inspect.getclosurevars" title="inspect.getclosurevars"><code class="xref py py-func docutils literal notranslate"><span class="pre">getclosurevars()</span></code></a> function has been added. This function
reports the current binding of all names referenced from the function body and
where those names were resolved, making it easier to verify correct internal
state when testing code that relies on stateful closures.</p>
<p>(由 Meador Inge 和 Nick Coghlan 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13062">bpo-13062</a> 中贡献。)</p>
<p>A new <a class="reference internal" href="../library/inspect.html#inspect.getgeneratorlocals" title="inspect.getgeneratorlocals"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorlocals()</span></code></a> function has been added. This
function reports the current binding of local variables in the generator's
stack frame, making it easier to verify correct internal state when testing
generators.</p>
<p>(由 Meador Inge 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15153">bpo-15153</a> 中贡献。)</p>
</section>
<section id="io">
<h3>io<a class="headerlink" href="#io" title="永久链接至标题"></a></h3>
<p>The <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">open()</span></code></a> function has a new <code class="docutils literal notranslate"><span class="pre">'x'</span></code> mode that can be used to
exclusively create a new file, and raise a <a class="reference internal" href="../library/exceptions.html#FileExistsError" title="FileExistsError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FileExistsError</span></code></a> if the file
already exists. It is based on the C11 'x' mode to fopen().</p>
<p>(由 David Townshend 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12760">bpo-12760</a> 中贡献。)</p>
<p>The constructor of the <a class="reference internal" href="../library/io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> class has a new
<em>write_through</em> optional argument. If <em>write_through</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, calls to
<code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> are guaranteed not to be buffered: any data
written on the <a class="reference internal" href="../library/io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOWrapper</span></code></a> object is immediately handled to its
underlying binary buffer.</p>
</section>
<section id="itertools">
<h3>itertools<a class="headerlink" href="#itertools" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">accumulate()</span></code></a> now takes an optional <code class="docutils literal notranslate"><span class="pre">func</span></code> argument for
providing a user-supplied binary function.</p>
</section>
<section id="logging">
<h3>logging<a class="headerlink" href="#logging" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">basicConfig()</span></code></a> function now supports an optional <code class="docutils literal notranslate"><span class="pre">handlers</span></code>
argument taking an iterable of handlers to be added to the root logger.</p>
<p>A class level attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">append_nul</span></code> has
been added to <a class="reference internal" href="../library/logging.handlers.html#logging.handlers.SysLogHandler" title="logging.handlers.SysLogHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SysLogHandler</span></code></a> to allow control of the
appending of the <code class="docutils literal notranslate"><span class="pre">NUL</span></code> (<code class="docutils literal notranslate"><span class="pre">\000</span></code>) byte to syslog records, since for some
daemons it is required while for others it is passed through to the log.</p>
</section>
<section id="math">
<h3>math<a class="headerlink" href="#math" title="永久链接至标题"></a></h3>
<p>The <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> module has a new function, <a class="reference internal" href="../library/math.html#math.log2" title="math.log2"><code class="xref py py-func docutils literal notranslate"><span class="pre">log2()</span></code></a>, which returns
the base-2 logarithm of <em>x</em>.</p>
<p>(Written by Mark Dickinson in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11888">bpo-11888</a>.)</p>
</section>
<section id="mmap">
<h3>mmap<a class="headerlink" href="#mmap" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/mmap.html#mmap.mmap.read" title="mmap.mmap.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method is now more compatible with other file-like
objects: if the argument is omitted or specified as <code class="docutils literal notranslate"><span class="pre">None</span></code>, it returns the
bytes from the current file position to the end of the mapping. (Contributed
by Petri Lehtinen in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12021">bpo-12021</a>.)</p>
</section>
<section id="multiprocessing">
<h3>multiprocessing<a class="headerlink" href="#multiprocessing" title="永久链接至标题"></a></h3>
<p>The new <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.connection.wait" title="multiprocessing.connection.wait"><code class="xref py py-func docutils literal notranslate"><span class="pre">multiprocessing.connection.wait()</span></code></a> function allows polling
multiple objects (such as connections, sockets and pipes) with a timeout.
(Contributed by Richard Oudkerk in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12328">bpo-12328</a>.)</p>
<p><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Connection</span></code> objects can now be transferred over
multiprocessing connections.
(Contributed by Richard Oudkerk in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4892">bpo-4892</a>.)</p>
<p><a class="reference internal" href="../library/multiprocessing.html#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">multiprocessing.Process</span></code></a> now accepts a <code class="docutils literal notranslate"><span class="pre">daemon</span></code> keyword argument
to override the default behavior of inheriting the <code class="docutils literal notranslate"><span class="pre">daemon</span></code> flag from
the parent process (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6064">bpo-6064</a>).</p>
<p>New attribute <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.Process.sentinel" title="multiprocessing.Process.sentinel"><code class="xref py py-data docutils literal notranslate"><span class="pre">multiprocessing.Process.sentinel</span></code></a> allows a
program to wait on multiple <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.Process" title="multiprocessing.Process"><code class="xref py py-class docutils literal notranslate"><span class="pre">Process</span></code></a> objects at one
time using the appropriate OS primitives (for example, <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> on
posix systems).</p>
<p>New methods <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.pool.Pool.starmap" title="multiprocessing.pool.Pool.starmap"><code class="xref py py-meth docutils literal notranslate"><span class="pre">multiprocessing.pool.Pool.starmap()</span></code></a> and
<a class="reference internal" href="../library/multiprocessing.html#multiprocessing.pool.Pool.starmap_async" title="multiprocessing.pool.Pool.starmap_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">starmap_async()</span></code></a> provide
<a class="reference internal" href="../library/itertools.html#itertools.starmap" title="itertools.starmap"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.starmap()</span></code></a> equivalents to the existing
<a class="reference internal" href="../library/multiprocessing.html#multiprocessing.pool.Pool.map" title="multiprocessing.pool.Pool.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">multiprocessing.pool.Pool.map()</span></code></a> and
<a class="reference internal" href="../library/multiprocessing.html#multiprocessing.pool.Pool.map_async" title="multiprocessing.pool.Pool.map_async"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map_async()</span></code></a> functions. (Contributed by Hynek
Schlawack in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12708">bpo-12708</a>.)</p>
</section>
<section id="nntplib">
<h3>nntplib<a class="headerlink" href="#nntplib" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/nntplib.html#nntplib.NNTP" title="nntplib.NNTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">nntplib.NNTP</span></code></a> class now supports the context management protocol to
unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a> exceptions and to close the NNTP
connection when done:</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">nntplib</span> <span class="kn">import</span> <span class="n">NNTP</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">NNTP</span><span class="p">(</span><span class="s1">&#39;news.gmane.org&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">n</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">n</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">&#39;gmane.comp.python.committers&#39;</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">(&#39;211 1755 1 1755 gmane.comp.python.committers&#39;, 1755, 1, 1755, &#39;gmane.comp.python.committers&#39;)</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>(由 Giampaolo Rodolà 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9795">bpo-9795</a> 中贡献。)</p>
</section>
<section id="os">
<h3>os<a class="headerlink" href="#os" title="永久链接至标题"></a></h3>
<ul>
<li><p>The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module has a new <a class="reference internal" href="../library/os.html#os.pipe2" title="os.pipe2"><code class="xref py py-func docutils literal notranslate"><span class="pre">pipe2()</span></code></a> function that makes it
possible to create a pipe with <a class="reference internal" href="../library/os.html#os.O_CLOEXEC" title="os.O_CLOEXEC"><code class="xref py py-data docutils literal notranslate"><span class="pre">O_CLOEXEC</span></code></a> or
<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> flags set atomically. This is especially useful to
avoid race conditions in multi-threaded programs.</p></li>
<li><p>The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module has a new <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> function which provides
an efficient &quot;zero-copy&quot; way for copying data from one file (or socket)
descriptor to another. The phrase &quot;zero-copy&quot; refers to the fact that all of
the copying of data between the two descriptors is done entirely by the
kernel, with no copying of data into userspace buffers. <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>
can be used to efficiently copy data from a file on disk to a network socket,
e.g. for downloading a file.</p>
<p>(Patch submitted by Ross Lagerwall and Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10882">bpo-10882</a>.)</p>
</li>
<li><p>To avoid race conditions like symlink attacks and issues with temporary
files and directories, it is more reliable (and also faster) to manipulate
file descriptors instead of file names. Python 3.3 enhances existing functions
and introduces new functions to work on file descriptors (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4761">bpo-4761</a>,
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10755">bpo-10755</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14626">bpo-14626</a>).</p>
<ul class="simple">
<li><p>The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module has a new <a class="reference internal" href="../library/os.html#os.fwalk" title="os.fwalk"><code class="xref py py-func docutils literal notranslate"><span class="pre">fwalk()</span></code></a> function similar to
<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">walk()</span></code></a> except that it also yields file descriptors referring to the
directories visited. This is especially useful to avoid symlink races.</p></li>
<li><p>The following functions get new optional <em>dir_fd</em> (<a class="reference internal" href="../library/os.html#dir-fd"><span class="std std-ref">paths relative to
directory descriptors</span></a>) and/or <em>follow_symlinks</em> (<a class="reference internal" href="../library/os.html#follow-symlinks"><span class="std std-ref">not
following symlinks</span></a>):
<a class="reference internal" href="../library/os.html#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a>, <a class="reference internal" href="../library/os.html#os.chflags" title="os.chflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">chflags()</span></code></a>, <a class="reference internal" href="../library/os.html#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a>, <a class="reference internal" href="../library/os.html#os.chown" title="os.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">chown()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.link" title="os.link"><code class="xref py py-func docutils literal notranslate"><span class="pre">link()</span></code></a>, <a class="reference internal" href="../library/os.html#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">lstat()</span></code></a>, <a class="reference internal" href="../library/os.html#os.mkdir" title="os.mkdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdir()</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.readlink" title="os.readlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">readlink()</span></code></a>, <a class="reference internal" href="../library/os.html#os.remove" title="os.remove"><code class="xref py py-func docutils literal notranslate"><span class="pre">remove()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">rename()</span></code></a>, <a class="reference internal" href="../library/os.html#os.replace" title="os.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">replace()</span></code></a>, <a class="reference internal" href="../library/os.html#os.rmdir" title="os.rmdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmdir()</span></code></a>, <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.symlink" title="os.symlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">symlink()</span></code></a>, <a class="reference internal" href="../library/os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">unlink()</span></code></a>, <a class="reference internal" href="../library/os.html#os.utime" title="os.utime"><code class="xref py py-func docutils literal notranslate"><span class="pre">utime()</span></code></a>. Platform
support for using these parameters can be checked via the sets
<a class="reference internal" href="../library/os.html#os.supports_dir_fd" title="os.supports_dir_fd"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_dir_fd</span></code></a> and <code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_follows_symlinks</span></code>.</p></li>
<li><p>The following functions now support a file descriptor for their path argument:
<a class="reference internal" href="../library/os.html#os.chdir" title="os.chdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">chdir()</span></code></a>, <a class="reference internal" href="../library/os.html#os.chmod" title="os.chmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">chmod()</span></code></a>, <a class="reference internal" href="../library/os.html#os.chown" title="os.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">chown()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.execve" title="os.execve"><code class="xref py py-func docutils literal notranslate"><span class="pre">execve()</span></code></a>, <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">listdir()</span></code></a>, <a class="reference internal" href="../library/os.html#os.pathconf" title="os.pathconf"><code class="xref py py-func docutils literal notranslate"><span class="pre">pathconf()</span></code></a>, <a class="reference internal" href="../library/os.path.html#os.path.exists" title="os.path.exists"><code class="xref py py-func docutils literal notranslate"><span class="pre">exists()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code></a>, <a class="reference internal" href="../library/os.html#os.statvfs" title="os.statvfs"><code class="xref py py-func docutils literal notranslate"><span class="pre">statvfs()</span></code></a>, <a class="reference internal" href="../library/os.html#os.utime" title="os.utime"><code class="xref py py-func docutils literal notranslate"><span class="pre">utime()</span></code></a>. Platform support
for this can be checked via the <a class="reference internal" href="../library/os.html#os.supports_fd" title="os.supports_fd"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.supports_fd</span></code></a> set.</p></li>
</ul>
</li>
<li><p><a class="reference internal" href="../library/os.html#os.access" title="os.access"><code class="xref py py-func docutils literal notranslate"><span class="pre">access()</span></code></a> accepts an <code class="docutils literal notranslate"><span class="pre">effective_ids</span></code> keyword argument to turn on
using the effective uid/gid rather than the real uid/gid in the access check.
Platform support for this can be checked via the
<a class="reference internal" href="../library/os.html#os.supports_effective_ids" title="os.supports_effective_ids"><code class="xref py py-data docutils literal notranslate"><span class="pre">supports_effective_ids</span></code></a> set.</p></li>
<li><p>The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a> module has two new functions: <a class="reference internal" href="../library/os.html#os.getpriority" title="os.getpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">getpriority()</span></code></a> and
<a class="reference internal" href="../library/os.html#os.setpriority" title="os.setpriority"><code class="xref py py-func docutils literal notranslate"><span class="pre">setpriority()</span></code></a>. They can be used to get or set process
niceness/priority in a fashion similar to <a class="reference internal" href="../library/os.html#os.nice" title="os.nice"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.nice()</span></code></a> but extended to all
processes instead of just the current one.</p>
<p>(Patch submitted by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10784">bpo-10784</a>.)</p>
</li>
<li><p>The new <a class="reference internal" href="../library/os.html#os.replace" title="os.replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.replace()</span></code></a> function allows cross-platform renaming of a
file with overwriting the destination. With <a class="reference internal" href="../library/os.html#os.rename" title="os.rename"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.rename()</span></code></a>, an existing
destination file is overwritten under POSIX, but raises an error under
Windows.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8828">bpo-8828</a>.)</p></li>
<li><p>The stat family of functions (<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">stat()</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>,
and <a class="reference internal" href="../library/os.html#os.lstat" title="os.lstat"><code class="xref py py-func docutils literal notranslate"><span class="pre">lstat()</span></code></a>) now support reading a file's timestamps
with nanosecond precision. Symmetrically, <a class="reference internal" href="../library/os.html#os.utime" title="os.utime"><code class="xref py py-func docutils literal notranslate"><span class="pre">utime()</span></code></a>
can now write file timestamps with nanosecond precision. (Contributed by
Larry Hastings in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14127">bpo-14127</a>.)</p></li>
<li><p>The new <a class="reference internal" href="../library/os.html#os.get_terminal_size" title="os.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.get_terminal_size()</span></code></a> function queries the size of the
terminal attached to a file descriptor. See also
<a class="reference internal" href="../library/shutil.html#shutil.get_terminal_size" title="shutil.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.get_terminal_size()</span></code></a>.
(Contributed by Zbigniew Jędrzejewski-Szmek in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13609">bpo-13609</a>.)</p></li>
</ul>
<ul class="simple">
<li><p>New functions to support Linux extended attributes (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12720">bpo-12720</a>):
<a class="reference internal" href="../library/os.html#os.getxattr" title="os.getxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getxattr()</span></code></a>, <a class="reference internal" href="../library/os.html#os.listxattr" title="os.listxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">listxattr()</span></code></a>, <a class="reference internal" href="../library/os.html#os.removexattr" title="os.removexattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">removexattr()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.setxattr" title="os.setxattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">setxattr()</span></code></a>.</p></li>
<li><p>New interface to the scheduler. These functions
control how a process is allocated CPU time by the operating system. New
functions:
<a class="reference internal" href="../library/os.html#os.sched_get_priority_max" title="os.sched_get_priority_max"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_get_priority_max()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sched_get_priority_min" title="os.sched_get_priority_min"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_get_priority_min()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.sched_getaffinity" title="os.sched_getaffinity"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_getaffinity()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sched_getparam" title="os.sched_getparam"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_getparam()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.sched_getscheduler" title="os.sched_getscheduler"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_getscheduler()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sched_rr_get_interval" title="os.sched_rr_get_interval"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_rr_get_interval()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.sched_setaffinity" title="os.sched_setaffinity"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_setaffinity()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sched_setparam" title="os.sched_setparam"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_setparam()</span></code></a>,
<a class="reference internal" href="../library/os.html#os.sched_setscheduler" title="os.sched_setscheduler"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_setscheduler()</span></code></a>, <a class="reference internal" href="../library/os.html#os.sched_yield" title="os.sched_yield"><code class="xref py py-func docutils literal notranslate"><span class="pre">sched_yield()</span></code></a>,</p></li>
<li><p>New functions to control the file system:</p>
<ul>
<li><p><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>: Announces an intention to access data in a
specific pattern thus allowing the kernel to make optimizations.</p></li>
<li><p><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>: Ensures that enough disk space is allocated
for a file.</p></li>
<li><p><a class="reference internal" href="../library/os.html#os.sync" title="os.sync"><code class="xref py py-func docutils literal notranslate"><span class="pre">sync()</span></code></a>: Force write of everything to disk.</p></li>
</ul>
</li>
<li><p>Additional new posix functions:</p>
<ul>
<li><p><a class="reference internal" href="../library/os.html#os.lockf" title="os.lockf"><code class="xref py py-func docutils literal notranslate"><span class="pre">lockf()</span></code></a>: Apply, test or remove a POSIX lock on an open file descriptor.</p></li>
<li><p><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>: Read from a file descriptor at an offset, the file
offset remains unchanged.</p></li>
<li><p><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>: Write to a file descriptor from an offset, leaving
the file offset unchanged.</p></li>
<li><p><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>: Read from a file descriptor into a number of writable buffers.</p></li>
<li><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>: Truncate the file corresponding to <em>path</em>, so that
it is at most <em>length</em> bytes in size.</p></li>
<li><p><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>: Wait for the completion of one or more child processes.</p></li>
<li><p><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>: Write the contents of <em>buffers</em> to a file descriptor,
where <em>buffers</em> is an arbitrary sequence of buffers.</p></li>
<li><p><a class="reference internal" href="../library/os.html#os.getgrouplist" title="os.getgrouplist"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgrouplist()</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9344">bpo-9344</a>): Return list of group ids that
specified user belongs to.</p></li>
</ul>
</li>
<li><p><a class="reference internal" href="../library/os.html#os.times" title="os.times"><code class="xref py py-func docutils literal notranslate"><span class="pre">times()</span></code></a> and <a class="reference internal" href="../library/os.html#os.uname" title="os.uname"><code class="xref py py-func docutils literal notranslate"><span class="pre">uname()</span></code></a>: Return type changed from a tuple to
a tuple-like object with named attributes.</p></li>
<li><p>Some platforms now support additional constants for the <a class="reference internal" href="../library/os.html#os.lseek" title="os.lseek"><code class="xref py py-func docutils literal notranslate"><span class="pre">lseek()</span></code></a>
function, such as <code class="docutils literal notranslate"><span class="pre">os.SEEK_HOLE</span></code> and <code class="docutils literal notranslate"><span class="pre">os.SEEK_DATA</span></code>.</p></li>
<li><p>New constants <a class="reference internal" href="../library/os.html#os.RTLD_LAZY" title="os.RTLD_LAZY"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_LAZY</span></code></a>, <a class="reference internal" href="../library/os.html#os.RTLD_NOW" title="os.RTLD_NOW"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_NOW</span></code></a>,
<a class="reference internal" href="../library/os.html#os.RTLD_GLOBAL" title="os.RTLD_GLOBAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_GLOBAL</span></code></a>, <a class="reference internal" href="../library/os.html#os.RTLD_LOCAL" title="os.RTLD_LOCAL"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_LOCAL</span></code></a>, <a class="reference internal" href="../library/os.html#os.RTLD_NODELETE" title="os.RTLD_NODELETE"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_NODELETE</span></code></a>,
<a class="reference internal" href="../library/os.html#os.RTLD_NOLOAD" title="os.RTLD_NOLOAD"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_NOLOAD</span></code></a>, and <a class="reference internal" href="../library/os.html#os.RTLD_DEEPBIND" title="os.RTLD_DEEPBIND"><code class="xref py py-data docutils literal notranslate"><span class="pre">RTLD_DEEPBIND</span></code></a> are available on
platforms that support them. These are for use with the
<a class="reference internal" href="../library/sys.html#sys.setdlopenflags" title="sys.setdlopenflags"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setdlopenflags()</span></code></a> function, and supersede the similar constants
defined in <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">DLFCN</span></code>. (Contributed by Victor Stinner
in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13226">bpo-13226</a>.)</p></li>
<li><p><a class="reference internal" href="../library/os.html#os.symlink" title="os.symlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.symlink()</span></code></a> now accepts (and ignores) the <code class="docutils literal notranslate"><span class="pre">target_is_directory</span></code>
keyword argument on non-Windows platforms, to ease cross-platform support.</p></li>
</ul>
</section>
<section id="pdb">
<h3>pdb<a class="headerlink" href="#pdb" title="永久链接至标题"></a></h3>
<p>Tab-completion is now available not only for command names, but also their
arguments. For example, for the <code class="docutils literal notranslate"><span class="pre">break</span></code> command, function and file names
are completed.</p>
<p>(由 Georg Brandl 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14210">bpo-14210</a> 中贡献)</p>
</section>
<section id="pickle">
<h3>pickle<a class="headerlink" href="#pickle" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/pickle.html#pickle.Pickler" title="pickle.Pickler"><code class="xref py py-class docutils literal notranslate"><span class="pre">pickle.Pickler</span></code></a> objects now have an optional
<a class="reference internal" href="../library/pickle.html#pickle.Pickler.dispatch_table" title="pickle.Pickler.dispatch_table"><code class="xref py py-attr docutils literal notranslate"><span class="pre">dispatch_table</span></code></a> attribute allowing per-pickler
reduction functions to be set.</p>
<p>(由 Richard Oudkerk 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14166">bpo-14166</a> 中贡献。)</p>
</section>
<section id="pydoc">
<h3>pydoc<a class="headerlink" href="#pydoc" title="永久链接至标题"></a></h3>
<p>The Tk GUI and the <code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code> function have been removed from the
<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> module: <code class="docutils literal notranslate"><span class="pre">pydoc</span> <span class="pre">-g</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code> have been deprecated
in Python 3.2.</p>
</section>
<section id="re">
<h3>re<a class="headerlink" href="#re" title="永久链接至标题"></a></h3>
<p><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> regular expressions now support <code class="docutils literal notranslate"><span class="pre">\u</span></code> and <code class="docutils literal notranslate"><span class="pre">\U</span></code> escapes.</p>
<p>(由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3665">bpo-3665</a> 中贡献。)</p>
</section>
<section id="sched">
<h3>sched<a class="headerlink" href="#sched" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p><a class="reference internal" href="../library/sched.html#sched.scheduler.run" title="sched.scheduler.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">run()</span></code></a> now accepts a <em>blocking</em> parameter which when
set to false makes the method execute the scheduled events due to expire
soonest (if any) and then return immediately.
This is useful in case you want to use the <a class="reference internal" href="../library/sched.html#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> in
non-blocking applications. (Contributed by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13449">bpo-13449</a>.)</p></li>
<li><p><a class="reference internal" href="../library/sched.html#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> class can now be safely used in multi-threaded
environments. (Contributed by Josiah Carlson and Giampaolo Rodolà in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8684">bpo-8684</a>.)</p></li>
<li><p><em>timefunc</em> and <em>delayfunct</em> parameters of <a class="reference internal" href="../library/sched.html#sched.scheduler" title="sched.scheduler"><code class="xref py py-class docutils literal notranslate"><span class="pre">scheduler</span></code></a> class
constructor are now optional and defaults to <a class="reference internal" href="../library/time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a> and
<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> respectively. (Contributed by Chris Clark in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13245">bpo-13245</a>.)</p></li>
<li><p><a class="reference internal" href="../library/sched.html#sched.scheduler.enter" title="sched.scheduler.enter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enter()</span></code></a> and <a class="reference internal" href="../library/sched.html#sched.scheduler.enterabs" title="sched.scheduler.enterabs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enterabs()</span></code></a>
<em>argument</em> parameter is now optional. (Contributed by Chris Clark in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13245">bpo-13245</a>.)</p></li>
<li><p><a class="reference internal" href="../library/sched.html#sched.scheduler.enter" title="sched.scheduler.enter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enter()</span></code></a> and <a class="reference internal" href="../library/sched.html#sched.scheduler.enterabs" title="sched.scheduler.enterabs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">enterabs()</span></code></a>
now accept a <em>kwargs</em> parameter. (Contributed by Chris Clark in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13245">bpo-13245</a>.)</p></li>
</ul>
</section>
<section id="select">
<h3>select<a class="headerlink" href="#select" title="永久链接至标题"></a></h3>
<p>Solaris and derivative platforms have a new class <a class="reference internal" href="../library/select.html#select.devpoll" title="select.devpoll"><code class="xref py py-class docutils literal notranslate"><span class="pre">select.devpoll</span></code></a>
for high performance asynchronous sockets via <code class="file docutils literal notranslate"><span class="pre">/dev/poll</span></code>.
(Contributed by Jesús Cea Avión in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6397">bpo-6397</a>.)</p>
</section>
<section id="shlex">
<h3>shlex<a class="headerlink" href="#shlex" title="永久链接至标题"></a></h3>
<p>The previously undocumented helper function <code class="docutils literal notranslate"><span class="pre">quote</span></code> from the
<a class="reference internal" href="../library/pipes.html#module-pipes" title="pipes: A Python interface to Unix shell pipelines. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pipes</span></code></a> modules has been moved to the <a class="reference internal" href="../library/shlex.html#module-shlex" title="shlex: Simple lexical analysis for Unix shell-like languages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shlex</span></code></a> module and
documented. <a class="reference internal" href="../library/shlex.html#shlex.quote" title="shlex.quote"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote()</span></code></a> properly escapes all characters in a string
that might be otherwise given special meaning by the shell.</p>
</section>
<section id="shutil">
<h3>shutil<a class="headerlink" href="#shutil" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>新的函数:</p>
<ul>
<li><p><a class="reference internal" href="../library/shutil.html#shutil.disk_usage" title="shutil.disk_usage"><code class="xref py py-func docutils literal notranslate"><span class="pre">disk_usage()</span></code></a>: provides total, used and free disk space
statistics. (Contributed by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12442">bpo-12442</a>.)</p></li>
<li><p><a class="reference internal" href="../library/shutil.html#shutil.chown" title="shutil.chown"><code class="xref py py-func docutils literal notranslate"><span class="pre">chown()</span></code></a>: allows one to change user and/or group of the given
path also specifying the user/group names and not only their numeric
ids. (Contributed by Sandro Tosi in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12191">bpo-12191</a>.)</p></li>
<li><p><a class="reference internal" href="../library/shutil.html#shutil.get_terminal_size" title="shutil.get_terminal_size"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.get_terminal_size()</span></code></a>: returns the size of the terminal window
to which the interpreter is attached. (Contributed by Zbigniew
Jędrzejewski-Szmek in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13609">bpo-13609</a>.)</p></li>
</ul>
</li>
<li><p><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> and <a class="reference internal" href="../library/shutil.html#shutil.copystat" title="shutil.copystat"><code class="xref py py-func docutils literal notranslate"><span class="pre">copystat()</span></code></a> now preserve file
timestamps with nanosecond precision on platforms that support it.
They also preserve file &quot;extended attributes&quot; on Linux. (Contributed
by Larry Hastings in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14127">bpo-14127</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15238">bpo-15238</a>.)</p></li>
<li><p>Several functions now take an optional <code class="docutils literal notranslate"><span class="pre">symlinks</span></code> argument: when that
parameter is true, symlinks aren't dereferenced and the operation instead
acts on the symlink itself (or creates one, if relevant).
(Contributed by Hynek Schlawack in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12715">bpo-12715</a>.)</p></li>
<li><p>When copying files to a different file system, <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> now
handles symlinks the way the posix <code class="docutils literal notranslate"><span class="pre">mv</span></code> command does, recreating the
symlink rather than copying the target file contents. (Contributed by
Jonathan Niehof in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9993">bpo-9993</a>.) <a class="reference internal" href="../library/shutil.html#shutil.move" title="shutil.move"><code class="xref py py-func docutils literal notranslate"><span class="pre">move()</span></code></a> now also returns
the <code class="docutils literal notranslate"><span class="pre">dst</span></code> argument as its result.</p></li>
<li><p><a class="reference internal" href="../library/shutil.html#shutil.rmtree" title="shutil.rmtree"><code class="xref py py-func docutils literal notranslate"><span class="pre">rmtree()</span></code></a> is now resistant to symlink attacks on platforms
which support the new <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code> parameter in <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">os.open()</span></code></a> and
<a class="reference internal" href="../library/os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a>. (Contributed by Martin von Löwis and Hynek Schlawack
in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4489">bpo-4489</a>.)</p></li>
</ul>
</section>
<section id="signal">
<h3>signal<a class="headerlink" href="#signal" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><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> 模块新增的函数:</p>
<ul>
<li><p><a class="reference internal" href="../library/signal.html#signal.pthread_sigmask" title="signal.pthread_sigmask"><code class="xref py py-func docutils literal notranslate"><span class="pre">pthread_sigmask()</span></code></a>: 获取和/或改变调用方线程的信号掩码(由 Jean-Paul Calderone 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8407">bpo-8407</a> 中贡献);</p></li>
<li><p><a class="reference internal" href="../library/signal.html#signal.pthread_kill" title="signal.pthread_kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">pthread_kill()</span></code></a>: 向指定线程发送信号;</p></li>
<li><p><a class="reference internal" href="../library/signal.html#signal.sigpending" title="signal.sigpending"><code class="xref py py-func docutils literal notranslate"><span class="pre">sigpending()</span></code></a>: 检查挂起的函数;</p></li>
<li><p><a class="reference internal" href="../library/signal.html#signal.sigwait" title="signal.sigwait"><code class="xref py py-func docutils literal notranslate"><span class="pre">sigwait()</span></code></a>: 等待一个信号;</p></li>
<li><p><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">sigwaitinfo()</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">sigtimedwait()</span></code></a>: like <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">sigwaitinfo()</span></code></a> but with a
timeout.</p></li>
</ul>
</li>
<li><p>The signal handler writes the signal number as a single byte instead of
a nul byte into the wakeup file descriptor. So it is possible to wait more
than one signal and know which signals were raised.</p></li>
<li><p><a class="reference internal" href="../library/signal.html#signal.signal" title="signal.signal"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.signal()</span></code></a> and <a class="reference internal" href="../library/signal.html#signal.siginterrupt" title="signal.siginterrupt"><code class="xref py py-func docutils literal notranslate"><span class="pre">signal.siginterrupt()</span></code></a> raise an OSError,
instead of a RuntimeError: OSError has an errno attribute.</p></li>
</ul>
</section>
<section id="smtpd">
<h3>smtpd<a class="headerlink" href="#smtpd" title="永久链接至标题"></a></h3>
<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 now supports <span class="target" id="index-26"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5321.html"><strong>RFC 5321</strong></a> (extended SMTP) and <span class="target" id="index-27"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1870.html"><strong>RFC 1870</strong></a>
(size extension). Per the standard, these extensions are enabled if and only
if the client initiates the session with an <code class="docutils literal notranslate"><span class="pre">EHLO</span></code> command.</p>
<p>(Initial <code class="docutils literal notranslate"><span class="pre">ELHO</span></code> support by Alberto Trevino. Size extension by Juhana
Jauhiainen. Substantial additional work on the patch contributed by Michele
Orrù and Dan Boswell. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8739">bpo-8739</a>)</p>
</section>
<section id="smtplib">
<h3>smtplib<a class="headerlink" href="#smtplib" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a>, <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP_SSL" title="smtplib.SMTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP_SSL</span></code></a>, and
<a class="reference internal" href="../library/smtplib.html#smtplib.LMTP" title="smtplib.LMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">LMTP</span></code></a> classes now accept a <code class="docutils literal notranslate"><span class="pre">source_address</span></code> keyword argument
to specify the <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> to use as the source address in the bind call
when creating the outgoing socket. (Contributed by Paulo Scardine in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11281">bpo-11281</a>.)</p>
<p><a class="reference internal" href="../library/smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> now supports the context management protocol, allowing an
<code class="docutils literal notranslate"><span class="pre">SMTP</span></code> instance to be used in a <code class="docutils literal notranslate"><span class="pre">with</span></code> statement. (Contributed
by Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11289">bpo-11289</a>.)</p>
<p>The <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP_SSL" title="smtplib.SMTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP_SSL</span></code></a> constructor and the <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.starttls" title="smtplib.SMTP.starttls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">starttls()</span></code></a>
method now accept an SSLContext parameter to control parameters of the secure
channel. (Contributed by Kasun Herath in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8809">bpo-8809</a>.)</p>
</section>
<section id="socket">
<h3>socket<a class="headerlink" href="#socket" title="永久链接至标题"></a></h3>
<ul>
<li><p>The <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> class now exposes additional methods to process
ancillary data when supported by the underlying platform:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/socket.html#socket.socket.sendmsg" title="socket.socket.sendmsg"><code class="xref py py-func docutils literal notranslate"><span class="pre">sendmsg()</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/socket.html#socket.socket.recvmsg" title="socket.socket.recvmsg"><code class="xref py py-func docutils literal notranslate"><span class="pre">recvmsg()</span></code></a></p></li>
<li><p><a class="reference internal" href="../library/socket.html#socket.socket.recvmsg_into" title="socket.socket.recvmsg_into"><code class="xref py py-func docutils literal notranslate"><span class="pre">recvmsg_into()</span></code></a></p></li>
</ul>
<p>(Contributed by David Watson in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6560">bpo-6560</a>, based on an earlier patch by
Heiko Wundram)</p>
</li>
<li><p>The <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> class now supports the PF_CAN protocol family
(<a class="reference external" href="https://en.wikipedia.org/wiki/Socketcan">https://en.wikipedia.org/wiki/Socketcan</a>), on Linux
(<a class="reference external" href="https://lwn.net/Articles/253425">https://lwn.net/Articles/253425</a>).</p>
<p>(Contributed by Matthias Fuchs, updated by Tiago Gonçalves in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10141">bpo-10141</a>.)</p>
</li>
<li><p>The <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> class now supports the PF_RDS protocol family
(<a class="reference external" href="https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets">https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets</a> and
<a class="reference external" href="https://oss.oracle.com/projects/rds/">https://oss.oracle.com/projects/rds/</a>).</p></li>
<li><p>The <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> class now supports the <code class="docutils literal notranslate"><span class="pre">PF_SYSTEM</span></code> protocol
family on OS X. (Contributed by Michael Goderbauer in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13777">bpo-13777</a>.)</p></li>
<li><p>New function <a class="reference internal" href="../library/socket.html#socket.sethostname" title="socket.sethostname"><code class="xref py py-func docutils literal notranslate"><span class="pre">sethostname()</span></code></a> allows the hostname to be set
on unix systems if the calling process has sufficient privileges.
(Contributed by Ross Lagerwall in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10866">bpo-10866</a>.)</p></li>
</ul>
</section>
<section id="socketserver">
<h3>socketserver<a class="headerlink" href="#socketserver" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer" title="socketserver.BaseServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseServer</span></code></a> now has an overridable method
<a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer.service_actions" title="socketserver.BaseServer.service_actions"><code class="xref py py-meth docutils literal notranslate"><span class="pre">service_actions()</span></code></a> that is called by the
<a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer.serve_forever" title="socketserver.BaseServer.serve_forever"><code class="xref py py-meth docutils literal notranslate"><span class="pre">serve_forever()</span></code></a> method in the service loop.
<a class="reference internal" href="../library/socketserver.html#socketserver.ForkingMixIn" title="socketserver.ForkingMixIn"><code class="xref py py-class docutils literal notranslate"><span class="pre">ForkingMixIn</span></code></a> now uses this to clean up zombie
child processes. (Contributed by Justin Warkentin in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11109">bpo-11109</a>.)</p>
</section>
<section id="sqlite3">
<h3>sqlite3<a class="headerlink" href="#sqlite3" title="永久链接至标题"></a></h3>
<p>New <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection" title="sqlite3.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">sqlite3.Connection</span></code></a> method
<a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.set_trace_callback" title="sqlite3.Connection.set_trace_callback"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_trace_callback()</span></code></a> can be used to capture a trace of
all sql commands processed by sqlite. (Contributed by Torsten Landschoff
in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11688">bpo-11688</a>.)</p>
</section>
<section id="ssl">
<h3>ssl<a class="headerlink" href="#ssl" title="永久链接至标题"></a></h3>
<ul>
<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> module has two new random generation functions:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/ssl.html#ssl.RAND_bytes" title="ssl.RAND_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_bytes()</span></code></a>: generate cryptographically strong
pseudo-random bytes.</p></li>
<li><p><a class="reference internal" href="../library/ssl.html#ssl.RAND_pseudo_bytes" title="ssl.RAND_pseudo_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">RAND_pseudo_bytes()</span></code></a>: 生成伪随机字节。</p></li>
</ul>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12049">bpo-12049</a> 中贡献。)</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> module now exposes a finer-grained exception hierarchy
in order to make it easier to inspect the various kinds of errors.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11183">bpo-11183</a>.)</p></li>
<li><p><a class="reference internal" href="../library/ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_cert_chain()</span></code></a> now accepts a <em>password</em> argument
to be used if the private key is encrypted.
(Contributed by Adam Simpkins in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12803">bpo-12803</a>.)</p></li>
<li><p>Diffie-Hellman key exchange, both regular and Elliptic Curve-based, is
now supported through the <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.load_dh_params" title="ssl.SSLContext.load_dh_params"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_dh_params()</span></code></a> and
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext.set_ecdh_curve" title="ssl.SSLContext.set_ecdh_curve"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_ecdh_curve()</span></code></a> methods.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13626">bpo-13626</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13627">bpo-13627</a>.)</p></li>
<li><p>SSL sockets have a new <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.get_channel_binding" title="ssl.SSLSocket.get_channel_binding"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_channel_binding()</span></code></a> method
allowing the implementation of certain authentication mechanisms such as
SCRAM-SHA-1-PLUS. (Contributed by Jacek Konieczny in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12551">bpo-12551</a>.)</p></li>
<li><p>You can query the SSL compression algorithm used by an SSL socket, thanks
to its new <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket.compression" title="ssl.SSLSocket.compression"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compression()</span></code></a> method. The new attribute
<a class="reference internal" href="../library/ssl.html#ssl.OP_NO_COMPRESSION" title="ssl.OP_NO_COMPRESSION"><code class="xref py py-attr docutils literal notranslate"><span class="pre">OP_NO_COMPRESSION</span></code></a> can be used to disable compression.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13634">bpo-13634</a>.)</p></li>
<li><p>Support has been added for the Next Protocol Negotiation extension using
the <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.set_npn_protocols" title="ssl.SSLContext.set_npn_protocols"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.set_npn_protocols()</span></code></a> method.
(Contributed by Colin Marc in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14204">bpo-14204</a>.)</p></li>
<li><p>SSL errors can now be introspected more easily thanks to
<a class="reference internal" href="../library/ssl.html#ssl.SSLError.library" title="ssl.SSLError.library"><code class="xref py py-attr docutils literal notranslate"><span class="pre">library</span></code></a> and <a class="reference internal" href="../library/ssl.html#ssl.SSLError.reason" title="ssl.SSLError.reason"><code class="xref py py-attr docutils literal notranslate"><span class="pre">reason</span></code></a> attributes.
(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14837">bpo-14837</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/ssl.html#ssl.get_server_certificate" title="ssl.get_server_certificate"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_server_certificate()</span></code></a> function now supports IPv6.
(Contributed by Charles-François Natali in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11811">bpo-11811</a>.)</p></li>
<li><p>New attribute <a class="reference internal" href="../library/ssl.html#ssl.OP_CIPHER_SERVER_PREFERENCE" title="ssl.OP_CIPHER_SERVER_PREFERENCE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">OP_CIPHER_SERVER_PREFERENCE</span></code></a> allows setting
SSLv3 server sockets to use the server's cipher ordering preference rather
than the client's (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13635">bpo-13635</a>).</p></li>
</ul>
</section>
<section id="stat">
<h3>stat<a class="headerlink" href="#stat" title="永久链接至标题"></a></h3>
<p>The undocumented tarfile.filemode function has been moved to
<a class="reference internal" href="../library/stat.html#stat.filemode" title="stat.filemode"><code class="xref py py-func docutils literal notranslate"><span class="pre">stat.filemode()</span></code></a>. It can be used to convert a file's mode to a string of
the form '-rwxrwxrwx'.</p>
<p>(由 Giampaolo Rodolà 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14807">bpo-14807</a> 中贡献。)</p>
</section>
<section id="struct">
<h3>struct<a class="headerlink" href="#struct" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> module now supports <code class="docutils literal notranslate"><span class="pre">ssize_t</span></code> and <code class="docutils literal notranslate"><span class="pre">size_t</span></code> via the
new codes <code class="docutils literal notranslate"><span class="pre">n</span></code> and <code class="docutils literal notranslate"><span class="pre">N</span></code>, respectively. (Contributed by Antoine Pitrou
in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3163">bpo-3163</a>.)</p>
</section>
<section id="subprocess">
<h3>subprocess<a class="headerlink" href="#subprocess" title="永久链接至标题"></a></h3>
<p>Command strings can now be bytes objects on posix platforms. (Contributed by
Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8513">bpo-8513</a>.)</p>
<p>A new constant <a class="reference internal" href="../library/subprocess.html#subprocess.DEVNULL" title="subprocess.DEVNULL"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEVNULL</span></code></a> allows suppressing output in a
platform-independent fashion. (Contributed by Ross Lagerwall in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5870">bpo-5870</a>.)</p>
</section>
<section id="sys">
<h3>sys<a class="headerlink" href="#sys" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a> module has a new <a class="reference internal" href="../library/sys.html#sys.thread_info" title="sys.thread_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">thread_info</span></code></a> <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named
tuple</span></a> holding information about the thread implementation
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11223">bpo-11223</a>).</p>
</section>
<section id="tarfile">
<h3>tarfile<a class="headerlink" href="#tarfile" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tarfile</span></code></a> now supports <code class="docutils literal notranslate"><span class="pre">lzma</span></code> encoding via the <a class="reference internal" href="../library/lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> module.
(Contributed by Lars Gustäbel in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5689">bpo-5689</a>.)</p>
</section>
<section id="tempfile">
<h3>tempfile<a class="headerlink" href="#tempfile" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/tempfile.html#tempfile.SpooledTemporaryFile" title="tempfile.SpooledTemporaryFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">tempfile.SpooledTemporaryFile</span></code></a>'s
<code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code> method now accepts
a <code class="docutils literal notranslate"><span class="pre">size</span></code> parameter. (Contributed by Ryan Kelly in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9957">bpo-9957</a>.)</p>
</section>
<section id="textwrap">
<h3>textwrap<a class="headerlink" href="#textwrap" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/textwrap.html#module-textwrap" title="textwrap: Text wrapping and filling"><code class="xref py py-mod docutils literal notranslate"><span class="pre">textwrap</span></code></a> module has a new <a class="reference internal" href="../library/textwrap.html#textwrap.indent" title="textwrap.indent"><code class="xref py py-func docutils literal notranslate"><span class="pre">indent()</span></code></a> that makes
it straightforward to add a common prefix to selected lines in a block
of text (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13857">bpo-13857</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.Condition" title="threading.Condition"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Condition</span></code></a>, <a class="reference internal" href="../library/threading.html#threading.Semaphore" title="threading.Semaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Semaphore</span></code></a>,
<a class="reference internal" href="../library/threading.html#threading.BoundedSemaphore" title="threading.BoundedSemaphore"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.BoundedSemaphore</span></code></a>, <a class="reference internal" href="../library/threading.html#threading.Event" title="threading.Event"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Event</span></code></a>, and
<a class="reference internal" href="../library/threading.html#threading.Timer" title="threading.Timer"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Timer</span></code></a>, all of which used to be factory functions returning a
class instance, are now classes and may be subclassed. (Contributed by Éric
Araujo in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10968">bpo-10968</a>.)</p>
<p>The <a class="reference internal" href="../library/threading.html#threading.Thread" title="threading.Thread"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.Thread</span></code></a> constructor now accepts a <code class="docutils literal notranslate"><span class="pre">daemon</span></code> keyword
argument to override the default behavior of inheriting the <code class="docutils literal notranslate"><span class="pre">daemon</span></code> flag
value from the parent thread (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6064">bpo-6064</a>).</p>
<p>The formerly private function <code class="docutils literal notranslate"><span class="pre">_thread.get_ident</span></code> is now available as the
public function <a class="reference internal" href="../library/threading.html#threading.get_ident" title="threading.get_ident"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.get_ident()</span></code></a>. This eliminates several cases of
direct access to the <code class="docutils literal notranslate"><span class="pre">_thread</span></code> module in the stdlib. Third party code that
used <code class="docutils literal notranslate"><span class="pre">_thread.get_ident</span></code> should likewise be changed to use the new public
interface.</p>
</section>
<section id="time">
<h3>time<a class="headerlink" href="#time" title="永久链接至标题"></a></h3>
<p>The <span class="target" id="index-28"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0418"><strong>PEP 418</strong></a> added new functions to the <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a> module:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/time.html#time.get_clock_info" title="time.get_clock_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_clock_info()</span></code></a>: Get information on a clock.</p></li>
<li><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>: Monotonic clock (cannot go backward), not affected
by system clock updates.</p></li>
<li><p><a class="reference internal" href="../library/time.html#time.perf_counter" title="time.perf_counter"><code class="xref py py-func docutils literal notranslate"><span class="pre">perf_counter()</span></code></a>: Performance counter with the highest available
resolution to measure a short duration.</p></li>
<li><p><a class="reference internal" href="../library/time.html#time.process_time" title="time.process_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">process_time()</span></code></a>: Sum of the system and user CPU time of the
current process.</p></li>
</ul>
<p>Other new functions:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/time.html#time.clock_getres" title="time.clock_getres"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_getres()</span></code></a>, <a class="reference internal" href="../library/time.html#time.clock_gettime" title="time.clock_gettime"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_gettime()</span></code></a> and
<a class="reference internal" href="../library/time.html#time.clock_settime" title="time.clock_settime"><code class="xref py py-func docutils literal notranslate"><span class="pre">clock_settime()</span></code></a> functions with <code class="docutils literal notranslate"><span class="pre">CLOCK_xxx</span></code> constants.
(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10278">bpo-10278</a>.)</p></li>
</ul>
<p>To improve cross platform consistency, <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">sleep()</span></code></a> now raises a
<a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> when passed a negative sleep value. Previously this was an
error on posix, but produced an infinite sleep on Windows.</p>
</section>
<section id="types">
<h3>types<a class="headerlink" href="#types" title="永久链接至标题"></a></h3>
<p>Add a new <a class="reference internal" href="../library/types.html#types.MappingProxyType" title="types.MappingProxyType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.MappingProxyType</span></code></a> class: Read-only proxy of a mapping.
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14386">bpo-14386</a>)</p>
<p>The new functions <a class="reference internal" href="../library/types.html#types.new_class" title="types.new_class"><code class="xref py py-func docutils literal notranslate"><span class="pre">types.new_class()</span></code></a> and <a class="reference internal" href="../library/types.html#types.prepare_class" title="types.prepare_class"><code class="xref py py-func docutils literal notranslate"><span class="pre">types.prepare_class()</span></code></a> provide support
for <span class="target" id="index-29"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3115"><strong>PEP 3115</strong></a> compliant dynamic type creation. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14588">bpo-14588</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.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaises()</span></code></a>, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRaisesRegex" title="unittest.TestCase.assertRaisesRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegex()</span></code></a>, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a>, and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarnsRegex()</span></code></a> now accept a keyword argument <em>msg</em> when used as
context managers. (Contributed by Ezio Melotti and Winston Ewert in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10775">bpo-10775</a>.)</p>
<p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.run" title="unittest.TestCase.run"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unittest.TestCase.run()</span></code></a> now returns the <a class="reference internal" href="../library/unittest.html#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestResult</span></code></a>
object.</p>
</section>
<section id="urllib">
<h3>urllib<a class="headerlink" href="#urllib" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/urllib.request.html#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">Request</span></code></a> class, now accepts a <em>method</em> argument
used by <a class="reference internal" href="../library/urllib.request.html#urllib.request.Request.get_method" title="urllib.request.Request.get_method"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_method()</span></code></a> to determine what HTTP method
should be used. For example, this will send a <code class="docutils literal notranslate"><span class="pre">'HEAD'</span></code> request:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urlopen</span><span class="p">(</span><span class="n">Request</span><span class="p">(</span><span class="s1">&#39;https://www.python.org&#39;</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">&#39;HEAD&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1673007">bpo-1673007</a>)</p>
</section>
<section id="webbrowser">
<h3>webbrowser<a class="headerlink" href="#webbrowser" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/webbrowser.html#module-webbrowser" title="webbrowser: Easy-to-use controller for Web browsers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">webbrowser</span></code></a> module supports more &quot;browsers&quot;: Google Chrome (named
<strong class="program">chrome</strong>, <strong class="program">chromium</strong>, <strong class="program">chrome-browser</strong> or
<strong class="program">chromium-browser</strong> depending on the version and operating system),
and the generic launchers <strong class="program">xdg-open</strong>, from the FreeDesktop.org
project, and <strong class="program">gvfs-open</strong>, which is the default URI handler for GNOME
3. (The former contributed by Arnaud Calmettes in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13620">bpo-13620</a>, the latter
by Matthias Klose in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14493">bpo-14493</a>.)</p>
</section>
<section id="xml-etree-elementtree">
<h3>xml.etree.ElementTree<a class="headerlink" href="#xml-etree-elementtree" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> module now imports its C accelerator by
default; there is no longer a need to explicitly import
<code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code> (this module stays for backwards compatibility,
but is now deprecated). In addition, the <code class="docutils literal notranslate"><span class="pre">iter</span></code> family of methods of
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code></a> has been optimized (rewritten in C).
The module's documentation has also been greatly improved with added examples
and a more detailed reference.</p>
</section>
<section id="zlib">
<h3>zlib<a class="headerlink" href="#zlib" title="永久链接至标题"></a></h3>
<p>New attribute <a class="reference internal" href="../library/zlib.html#zlib.Decompress.eof" title="zlib.Decompress.eof"><code class="xref py py-attr docutils literal notranslate"><span class="pre">zlib.Decompress.eof</span></code></a> makes it possible to distinguish
between a properly-formed compressed stream and an incomplete or truncated one.
(Contributed by Nadeem Vawda in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12646">bpo-12646</a>.)</p>
<p>New attribute <a class="reference internal" href="../library/zlib.html#zlib.ZLIB_RUNTIME_VERSION" title="zlib.ZLIB_RUNTIME_VERSION"><code class="xref py py-attr docutils literal notranslate"><span class="pre">zlib.ZLIB_RUNTIME_VERSION</span></code></a> reports the version string of
the underlying <code class="docutils literal notranslate"><span class="pre">zlib</span></code> library that is loaded at runtime. (Contributed by
Torsten Landschoff in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12306">bpo-12306</a>.)</p>
</section>
</section>
<section id="optimizations">
<h2>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题"></a></h2>
<p>已增加的主要性能改善:</p>
<ul>
<li><p>得益于:pep:<cite>393</cite> Unicode 字符串的某些操作已得到优化:</p>
<ul class="simple">
<li><p>the memory footprint is divided by 2 to 4 depending on the text</p></li>
<li><p>将 ASCII 字符串编码为 UTF-8 不再需要对字符进行编码UTF-8 的表示法与 ASCII 的表示法是共享的</p></li>
<li><p>the UTF-8 encoder has been optimized</p></li>
<li><p>repeating a single ASCII letter and getting a substring of an ASCII string
is 4 times faster</p></li>
</ul>
</li>
<li><p>UTF-8 编码现在快 2 到 4 倍。 UTF-16 编码的速度现在提高了 10 倍。</p>
<p>(由 Serhiy Storchaka 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14624">bpo-14624</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14738">bpo-14738</a><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=15026">bpo-15026</a> 中贡献)。</p>
</li>
</ul>
</section>
<section id="build-and-c-api-changes">
<h2>构建和 C API 的改变<a class="headerlink" href="#build-and-c-api-changes" title="永久链接至标题"></a></h2>
<p>针对 Python 构建过程和 C API 的改变包括:</p>
<ul class="simple">
<li><p>新的 <span class="target" id="index-77"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a> 相关功能:</p>
<ul>
<li><p><a class="reference internal" href="../c-api/memoryview.html#c.PyMemoryView_FromMemory" title="PyMemoryView_FromMemory"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMemoryView_FromMemory()</span></code></a></p></li>
</ul>
</li>
<li><p><span class="target" id="index-78"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a> 添加了新的 Unicode 类型,宏和函数</p>
<ul>
<li><p>高阶 API</p>
<ul>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CopyCharacters" title="PyUnicode_CopyCharacters"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CopyCharacters()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FindChar" title="PyUnicode_FindChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FindChar()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GetLength" title="PyUnicode_GetLength"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetLength()</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_LENGTH" title="PyUnicode_GET_LENGTH"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_LENGTH</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_New" title="PyUnicode_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_New()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Substring" title="PyUnicode_Substring"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Substring()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_ReadChar" title="PyUnicode_ReadChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_ReadChar()</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_WriteChar" title="PyUnicode_WriteChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_WriteChar()</span></code></a></p></li>
</ul>
</li>
<li><p>低阶 API:</p>
<ul>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.Py_UCS1" title="Py_UCS1"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UCS1</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.Py_UCS2" title="Py_UCS2"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UCS2</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.Py_UCS4" title="Py_UCS4"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UCS4</span></code></a> 类型</p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyASCIIObject" title="PyASCIIObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyASCIIObject</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyCompactUnicodeObject" title="PyCompactUnicodeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCompactUnicodeObject</span></code></a> 结构</p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_READY" title="PyUnicode_READY"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_READY</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromKindAndData" title="PyUnicode_FromKindAndData"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromKindAndData()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUCS4" title="PyUnicode_AsUCS4"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUCS4()</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUCS4Copy" title="PyUnicode_AsUCS4Copy"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUCS4Copy()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_DATA" title="PyUnicode_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_DATA</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_1BYTE_DATA" title="PyUnicode_1BYTE_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_1BYTE_DATA</span></code></a>,
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_2BYTE_DATA" title="PyUnicode_2BYTE_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_2BYTE_DATA</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_4BYTE_DATA" title="PyUnicode_4BYTE_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_4BYTE_DATA</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_KIND" title="PyUnicode_KIND"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_KIND</span></code></a> 使用 <code class="xref c c-type docutils literal notranslate"><span class="pre">PyUnicode_Kind</span></code> 枚举: <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_WCHAR_KIND" title="PyUnicode_WCHAR_KIND"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyUnicode_WCHAR_KIND</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_1BYTE_KIND" title="PyUnicode_1BYTE_KIND"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyUnicode_1BYTE_KIND</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_2BYTE_KIND" title="PyUnicode_2BYTE_KIND"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyUnicode_2BYTE_KIND</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_4BYTE_KIND" title="PyUnicode_4BYTE_KIND"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyUnicode_4BYTE_KIND</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_READ" title="PyUnicode_READ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_READ</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_READ_CHAR" title="PyUnicode_READ_CHAR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_READ_CHAR</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_WRITE" title="PyUnicode_WRITE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_WRITE</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_MAX_CHAR_VALUE" title="PyUnicode_MAX_CHAR_VALUE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_MAX_CHAR_VALUE</span></code></a></p></li>
</ul>
</li>
</ul>
</li>
<li><p><a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyArg_ParseTuple</span></code></a> 现在接受 <code class="docutils literal notranslate"><span class="pre">c</span></code> 格式的 <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> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12380">bpo-12380</a>)。</p></li>
</ul>
</section>
<section id="deprecated">
<h2>弃用<a class="headerlink" href="#deprecated" title="永久链接至标题"></a></h2>
<section id="unsupported-operating-systems">
<h3>不支持的操作系统<a class="headerlink" href="#unsupported-operating-systems" title="永久链接至标题"></a></h3>
<p>由于缺少维护人员,不再支持 OS/2 和 VMS 系统 。</p>
<p>由于维护负担,将 <code class="docutils literal notranslate"><span class="pre">COMSPEC</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">command.com</span></code> 的 Windows平台含Windows 2000不再受支持。</p>
<p>OSF支持在3.2中被弃用,现在已经被完全删除。</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>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">object.__format__()</span></code> 传递非空字符串的做法已被弃用,在 Python 3.4 中会产生一个 <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9856">bpo-9856</a>)。</p></li>
<li><p>由于:pep:<cite>393</cite><code class="docutils literal notranslate"><span class="pre">unicode_internal</span></code> 编解码器已被弃用 。请使用 UTF-8、UTF-16 (<code class="docutils literal notranslate"><span class="pre">utf-16-le</span></code> 或``utf-16-be``) 或 UTF-32 (<code class="docutils literal notranslate"><span class="pre">utf-32-le</span></code> 或``utf-32-be``)</p></li>
<li><p><a class="reference internal" href="../library/ftplib.html#ftplib.FTP.nlst" title="ftplib.FTP.nlst"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.nlst()</span></code></a> and <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.dir" title="ftplib.FTP.dir"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.dir()</span></code></a>: use
<a class="reference internal" href="../library/ftplib.html#ftplib.FTP.mlsd" title="ftplib.FTP.mlsd"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ftplib.FTP.mlsd()</span></code></a></p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">platform.popen()</span></code>: use the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module. Check especially
the <a class="reference internal" href="../library/subprocess.html#subprocess-replacements"><span class="std std-ref">使用 subprocess 模块替换旧函数</span></a> section (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11377">bpo-11377</a>).</p></li>
<li><p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13374">bpo-13374</a>: The Windows bytes API has been deprecated in the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os</span></code></a>
module. Use Unicode filenames, instead of bytes filenames, to not depend on
the ANSI code page anymore and to support any filename.</p></li>
<li><p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13988">bpo-13988</a>: The <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code> module is deprecated. The
accelerator is used automatically whenever available.</p></li>
<li><p>The behaviour of <code class="xref py py-func docutils literal notranslate"><span class="pre">time.clock()</span></code> depends on the platform: use the new
<a class="reference internal" href="../library/time.html#time.perf_counter" title="time.perf_counter"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.perf_counter()</span></code></a> or <a class="reference internal" href="../library/time.html#time.process_time" title="time.process_time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.process_time()</span></code></a> function instead,
depending on your requirements, to have a well defined behaviour.</p></li>
<li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat_float_times()</span></code> function is deprecated.</p></li>
<li><p><a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to :pep:`3119`."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module:</p>
<ul>
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractproperty" title="abc.abstractproperty"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractproperty</span></code></a> has been deprecated, use <a class="reference internal" href="../library/functions.html#property" title="property"><code class="xref py py-class docutils literal notranslate"><span class="pre">property</span></code></a>
with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractclassmethod" title="abc.abstractclassmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractclassmethod</span></code></a> has been deprecated, use
<a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">classmethod</span></code></a> with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
<li><p><a class="reference internal" href="../library/abc.html#abc.abstractstaticmethod" title="abc.abstractstaticmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">abc.abstractstaticmethod</span></code></a> has been deprecated, use
<a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">staticmethod</span></code></a> with <a class="reference internal" href="../library/abc.html#abc.abstractmethod" title="abc.abstractmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abc.abstractmethod()</span></code></a> instead.</p></li>
</ul>
</li>
<li><p><a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> package:</p>
<ul>
<li><p><a class="reference internal" href="../library/importlib.html#importlib.abc.SourceLoader.path_mtime" title="importlib.abc.SourceLoader.path_mtime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.path_mtime()</span></code></a> is now deprecated in favour of
<a class="reference internal" href="../library/importlib.html#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.path_stats()</span></code></a> as bytecode files now store
both the modification time and size of the source file the bytecode file was
compiled from.</p></li>
</ul>
</li>
</ul>
</section>
<section id="deprecated-functions-and-types-of-the-c-api">
<h3>已弃用的 C API 函数和类型<a class="headerlink" href="#deprecated-functions-and-types-of-the-c-api" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> 已经在 <span class="target" id="index-79"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a> 弃用,并将于 Python 4 中移除。所有使用此类型的函数都已弃用:</p>
<p>Unicode functions and methods using <a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> and
<a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE*</span></code></a> types:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromUnicode" title="PyUnicode_FromUnicode"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_FromUnicode</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromWideChar" title="PyUnicode_FromWideChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromWideChar()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromKindAndData" title="PyUnicode_FromKindAndData"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromKindAndData()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AS_UNICODE" title="PyUnicode_AS_UNICODE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_AS_UNICODE</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUnicode" title="PyUnicode_AsUnicode"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUnicode()</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUnicodeAndSize" title="PyUnicode_AsUnicodeAndSize"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUnicodeAndSize()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsWideCharString" title="PyUnicode_AsWideCharString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsWideCharString()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AS_DATA" title="PyUnicode_AS_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_AS_DATA</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_DATA" title="PyUnicode_DATA"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_DATA</span></code></a> 以及 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_READ" title="PyUnicode_READ"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_READ</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_WRITE" title="PyUnicode_WRITE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_WRITE</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_SIZE" title="PyUnicode_GET_SIZE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_SIZE</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GetSize" title="PyUnicode_GetSize"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetSize()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_LENGTH" title="PyUnicode_GET_LENGTH"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_LENGTH</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GetLength" title="PyUnicode_GetLength"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetLength()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_DATA_SIZE" title="PyUnicode_GET_DATA_SIZE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_DATA_SIZE</span></code></a>: 使用 <code class="docutils literal notranslate"><span class="pre">PyUnicode_GET_LENGTH(str)</span> <span class="pre">*</span> <span class="pre">PyUnicode_KIND(str)</span></code> (仅适用于现成的字符串)</p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUnicodeCopy" title="PyUnicode_AsUnicodeCopy"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUnicodeCopy()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUCS4Copy" title="PyUnicode_AsUCS4Copy"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUCS4Copy()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsWideCharString" title="PyUnicode_AsWideCharString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsWideCharString()</span></code></a></p></li>
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetMax()</span></code></p></li>
</ul>
<p>Functions and macros manipulating Py_UNICODE* strings:</p>
<ul class="simple">
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strlen</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GetLength" title="PyUnicode_GetLength"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_GetLength()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_GET_LENGTH" title="PyUnicode_GET_LENGTH"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyUnicode_GET_LENGTH</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strcat</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CopyCharacters" title="PyUnicode_CopyCharacters"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CopyCharacters()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromFormat" title="PyUnicode_FromFormat"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromFormat()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strcpy</span></code>, <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strncpy</span></code>, <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_COPY</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CopyCharacters" title="PyUnicode_CopyCharacters"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CopyCharacters()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Substring" title="PyUnicode_Substring"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Substring()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strcmp</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Compare" title="PyUnicode_Compare"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Compare()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strncmp</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Tailmatch" title="PyUnicode_Tailmatch"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Tailmatch()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strchr</span></code>, <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_strrchr</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FindChar" title="PyUnicode_FindChar"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FindChar()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_FILL</span></code>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Fill" title="PyUnicode_Fill"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Fill()</span></code></a></p></li>
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_UNICODE_MATCH</span></code></p></li>
</ul>
<p>编码器:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Encode" title="PyUnicode_Encode"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Encode()</span></code></a>: 使用 <code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsEncodedObject()</span></code></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeUTF7" title="PyUnicode_EncodeUTF7"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeUTF7()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeUTF8" title="PyUnicode_EncodeUTF8"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeUTF8()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUTF8" title="PyUnicode_AsUTF8"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUTF8()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUTF8String" title="PyUnicode_AsUTF8String"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUTF8String()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeUTF32" title="PyUnicode_EncodeUTF32"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeUTF32()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeUTF16" title="PyUnicode_EncodeUTF16"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeUTF16()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeUnicodeEscape" title="PyUnicode_EncodeUnicodeEscape"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeUnicodeEscape()</span></code></a> use
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsUnicodeEscapeString" title="PyUnicode_AsUnicodeEscapeString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsUnicodeEscapeString()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeRawUnicodeEscape" title="PyUnicode_EncodeRawUnicodeEscape"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeRawUnicodeEscape()</span></code></a> use
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsRawUnicodeEscapeString" title="PyUnicode_AsRawUnicodeEscapeString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsRawUnicodeEscapeString()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeLatin1" title="PyUnicode_EncodeLatin1"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeLatin1()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsLatin1String" title="PyUnicode_AsLatin1String"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsLatin1String()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeASCII" title="PyUnicode_EncodeASCII"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeASCII()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsASCIIString" title="PyUnicode_AsASCIIString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsASCIIString()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeCharmap" title="PyUnicode_EncodeCharmap"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeCharmap()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_TranslateCharmap" title="PyUnicode_TranslateCharmap"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_TranslateCharmap()</span></code></a></p></li>
<li><p><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeMBCS" title="PyUnicode_EncodeMBCS"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeMBCS()</span></code></a>: 使用 <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_AsMBCSString" title="PyUnicode_AsMBCSString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_AsMBCSString()</span></code></a><a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_EncodeCodePage" title="PyUnicode_EncodeCodePage"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeCodePage()</span></code></a> (和 <code class="docutils literal notranslate"><span class="pre">CP_ACP</span></code> code_page)</p></li>
<li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_EncodeDecimal()</span></code>,
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_TransformDecimalToASCII" title="PyUnicode_TransformDecimalToASCII"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_TransformDecimalToASCII()</span></code></a></p></li>
</ul>
</section>
<section id="deprecated-features">
<h3>弃用的功能<a class="headerlink" href="#deprecated-features" title="永久链接至标题"></a></h3>
<p><a class="reference internal" href="../library/array.html#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> 模块的``'u'`` 格式代码现已弃用,将在 Python 4 中与 (<a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a>) API 的其他部分一起删除。</p>
</section>
</section>
<section id="porting-to-python-3-3">
<h2>移植到 Python 3.3<a class="headerlink" href="#porting-to-python-3-3" title="永久链接至标题"></a></h2>
<p>本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.</p>
<section id="porting-python-code">
<span id="portingpythoncode"></span><h3>移植 Python 代码<a class="headerlink" href="#porting-python-code" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>默认启用哈希随机化。 将 <span class="target" id="index-80"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> 环境变量设为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 可禁用哈希随机化。 另请参阅 <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__hash__()</span></code></a> 方法。</p></li>
<li><p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=12326">bpo-12326</a>: 在 Linux 上sys.platform 不再包含主要版本。现在它始终是 &quot;linux&quot;,而不是 &quot;linux2&quot;&quot;linux3&quot;,这取决于用于构建 Python 的 Linux 版本。请用 sys.platform.startswith('linux') 替换 sys.platform == 'linux2',如果不需要支持较旧的 Python 版本,则可直接替换成 sys.platform == 'linux'。</p></li>
<li><p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13847">bpo-13847</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14180">bpo-14180</a>: <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">time</span></code></a><a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">datetime</span></code></a>: 现在如果时间戳超出范围将会引发 <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> 而不是 <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>。 现在如果 C 函数 <code class="xref c c-func docutils literal notranslate"><span class="pre">gmtime()</span></code><code class="xref c c-func docutils literal notranslate"><span class="pre">localtime()</span></code> 失败 将会引发 <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a></p></li>
<li><p>The default finders used by import now utilize a cache of what is contained
within a specific directory. If you create a Python source file or sourceless
bytecode file, make sure to call <a class="reference internal" href="../library/importlib.html#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a> to clear
out the cache for the finders to notice the new file.</p></li>
<li><p><a class="reference internal" href="../library/exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> now uses the full name of the module that was attempted to
be imported. Doctests that check ImportErrors' message will need to be
updated to use the full name of the module instead of just the tail of the
name.</p></li>
<li><p>The <em>index</em> argument to <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> now defaults to 0 instead of -1
and no longer support negative values. It was an oversight when <span class="target" id="index-36"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a> was
implemented that the default value remained -1. If you need to continue to
perform a relative import followed by an absolute import, then perform the
relative import using an index of 1, followed by another import using an
index of 0. It is preferred, though, that you use
<a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> rather than call <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> directly.</p></li>
<li><p><a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> no longer allows one to use an index value other than 0
for top-level modules. E.g. <code class="docutils literal notranslate"><span class="pre">__import__('sys',</span> <span class="pre">level=1)</span></code> is now an error.</p></li>
<li><p>Because <a class="reference internal" href="../library/sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> and <a class="reference internal" href="../library/sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> now have finders on
them by default, you will most likely want to use <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.insert()</span></code> instead
of <code class="xref py py-meth docutils literal notranslate"><span class="pre">list.append()</span></code> to add to those lists.</p></li>
<li><p>Because <code class="docutils literal notranslate"><span class="pre">None</span></code> is now inserted into <a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>, if you
are clearing out entries in the dictionary of paths that do not have a
finder, you will need to remove keys paired with values of <code class="docutils literal notranslate"><span class="pre">None</span></code> <strong>and</strong>
<a class="reference internal" href="../library/imp.html#imp.NullImporter" title="imp.NullImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">imp.NullImporter</span></code></a> to be backwards-compatible. This will lead to extra
overhead on older versions of Python that re-insert <code class="docutils literal notranslate"><span class="pre">None</span></code> into
<a class="reference internal" href="../library/sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> where it represents the use of implicit
finders, but semantically it should not change anything.</p></li>
<li><p><a class="reference internal" href="../library/importlib.html#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.Finder</span></code></a> no longer specifies a <cite>find_module()</cite> abstract
method that must be implemented. If you were relying on subclasses to
implement that method, make sure to check for the method's existence first.
You will probably want to check for <cite>find_loader()</cite> first, though, in the
case of working with <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finders</span></a>.</p></li>
<li><p><a class="reference internal" href="../library/pkgutil.html#module-pkgutil" title="pkgutil: Utilities for the import system."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pkgutil</span></code></a> has been converted to use <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> internally. This
eliminates many edge cases where the old behaviour of the <span class="target" id="index-37"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> import
emulation failed to match the behaviour of the real import system. The
import emulation itself is still present, but is now deprecated. The
<a class="reference internal" href="../library/pkgutil.html#pkgutil.iter_importers" title="pkgutil.iter_importers"><code class="xref py py-func docutils literal notranslate"><span class="pre">pkgutil.iter_importers()</span></code></a> and <a class="reference internal" href="../library/pkgutil.html#pkgutil.walk_packages" title="pkgutil.walk_packages"><code class="xref py py-func docutils literal notranslate"><span class="pre">pkgutil.walk_packages()</span></code></a> functions
special case the standard import hooks so they are still supported even
though they do not provide the non-standard <code class="docutils literal notranslate"><span class="pre">iter_modules()</span></code> method.</p></li>
<li><p>A longstanding RFC-compliance bug (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1079">bpo-1079</a>) in the parsing done by
<a class="reference internal" href="../library/email.header.html#email.header.decode_header" title="email.header.decode_header"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.header.decode_header()</span></code></a> has been fixed. Code that uses the
standard idiom to convert encoded headers into unicode
(<code class="docutils literal notranslate"><span class="pre">str(make_header(decode_header(h))</span></code>) will see no change, but code that
looks at the individual tuples returned by decode_header will see that
whitespace that precedes or follows <code class="docutils literal notranslate"><span class="pre">ASCII</span></code> sections is now included in the
<code class="docutils literal notranslate"><span class="pre">ASCII</span></code> section. Code that builds headers using <code class="docutils literal notranslate"><span class="pre">make_header</span></code> should
also continue to work without change, since <code class="docutils literal notranslate"><span class="pre">make_header</span></code> continues to add
whitespace between <code class="docutils literal notranslate"><span class="pre">ASCII</span></code> and non-<code class="docutils literal notranslate"><span class="pre">ASCII</span></code> sections if it is not already
present in the input strings.</p></li>
<li><p><a class="reference internal" href="../library/email.utils.html#email.utils.formataddr" title="email.utils.formataddr"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.formataddr()</span></code></a> now does the correct content transfer
encoding when passed non-<code class="docutils literal notranslate"><span class="pre">ASCII</span></code> display names. Any code that depended on
the previous buggy behavior that preserved the non-<code class="docutils literal notranslate"><span class="pre">ASCII</span></code> unicode in the
formatted output string will need to be changed (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1690608">bpo-1690608</a>).</p></li>
<li><p><a class="reference internal" href="../library/poplib.html#poplib.POP3.quit" title="poplib.POP3.quit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">poplib.POP3.quit()</span></code></a> may now raise protocol errors like all other
<code class="docutils literal notranslate"><span class="pre">poplib</span></code> methods. Code that assumes <code class="docutils literal notranslate"><span class="pre">quit</span></code> does not raise
<a class="reference internal" href="../library/poplib.html#poplib.error_proto" title="poplib.error_proto"><code class="xref py py-exc docutils literal notranslate"><span class="pre">poplib.error_proto</span></code></a> errors may need to be changed if errors on <code class="docutils literal notranslate"><span class="pre">quit</span></code>
are encountered by a particular application (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11291">bpo-11291</a>).</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">strict</span></code> argument to <a class="reference internal" href="../library/email.parser.html#email.parser.Parser" title="email.parser.Parser"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.parser.Parser</span></code></a>, deprecated since
Python 2.4, has finally been removed.</p></li>
<li><p>The deprecated method <code class="docutils literal notranslate"><span class="pre">unittest.TestCase.assertSameElements</span></code> has been
removed.</p></li>
<li><p>The deprecated variable <code class="docutils literal notranslate"><span class="pre">time.accept2dyear</span></code> has been removed.</p></li>
<li><p>被弃用的 <code class="docutils literal notranslate"><span class="pre">Context._clamp</span></code> 属性已从 <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> 模块中移除。 在此之前它已被公有属性 <code class="xref py py-attr docutils literal notranslate"><span class="pre">clamp</span></code> 取代。 (参见 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8540">bpo-8540</a>。)</p></li>
<li><p>The undocumented internal helper class <code class="docutils literal notranslate"><span class="pre">SSLFakeFile</span></code> has been removed
from <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>, since its functionality has long been provided directly
by <a class="reference internal" href="../library/socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.makefile()</span></code></a>.</p></li>
<li><p>Passing a negative value to <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> on Windows now raises an
error instead of sleeping forever. It has always raised an error on posix.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">ast.__version__</span></code> constant has been removed. If you need to
make decisions affected by the AST version, use <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.version_info</span></code></a>
to make the decision.</p></li>
<li><p>Code that used to work around the fact that the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module used
factory functions by subclassing the private classes will need to change to
subclass the now-public classes.</p></li>
<li><p>The undocumented debugging machinery in the threading module has been
removed, simplifying the code. This should have no effect on production
code, but is mentioned here in case any application debug frameworks were
interacting with it (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=13550">bpo-13550</a>).</p></li>
</ul>
</section>
<section id="porting-c-code">
<h3>移植 C 代码<a class="headerlink" href="#porting-c-code" title="永久链接至标题"></a></h3>
<ul>
<li><p>In the course of changes to the buffer API the undocumented
<code class="xref c c-member docutils literal notranslate"><span class="pre">smalltable</span></code> member of the
<a class="reference internal" href="../c-api/buffer.html#c.Py_buffer" title="Py_buffer"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_buffer</span></code></a> structure has been removed and the
layout of the <code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemoryViewObject</span></code> has changed.</p>
<p>All extensions relying on the relevant parts in <code class="docutils literal notranslate"><span class="pre">memoryobject.h</span></code>
or <code class="docutils literal notranslate"><span class="pre">object.h</span></code> must be rebuilt.</p>
</li>
<li><p>Due to <a class="reference internal" href="#pep-393"><span class="std std-ref">PEP 393</span></a>, the <a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> type and all
functions using this type are deprecated (but will stay available for
at least five years). If you were using low-level Unicode APIs to
construct and access unicode objects and you want to benefit of the
memory footprint reduction provided by <span class="target" id="index-38"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0393"><strong>PEP 393</strong></a>, you have to convert
your code to the new <a class="reference internal" href="../c-api/unicode.html"><span class="doc">Unicode API</span></a>.</p>
<p>However, if you only have been using high-level functions such as
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Concat" title="PyUnicode_Concat"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Concat()</span></code></a>, <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_Join" title="PyUnicode_Join"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_Join()</span></code></a> or
<a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_FromFormat" title="PyUnicode_FromFormat"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_FromFormat()</span></code></a>, your code will automatically take
advantage of the new unicode representations.</p>
</li>
<li><p><a class="reference internal" href="../c-api/import.html#c.PyImport_GetMagicNumber" title="PyImport_GetMagicNumber"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_GetMagicNumber()</span></code></a> now returns <code class="docutils literal notranslate"><span class="pre">-1</span></code> upon failure.</p></li>
<li><p>As a negative value for the <em>level</em> argument to <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> is no
longer valid, the same now holds for <a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModuleLevel" title="PyImport_ImportModuleLevel"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleLevel()</span></code></a>.
This also means that the value of <em>level</em> used by
<a class="reference internal" href="../c-api/import.html#c.PyImport_ImportModuleEx" title="PyImport_ImportModuleEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ImportModuleEx()</span></code></a> is now <code class="docutils literal notranslate"><span class="pre">0</span></code> instead of <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p></li>
</ul>
</section>
<section id="building-c-extensions">
<h3>Building C extensions<a class="headerlink" href="#building-c-extensions" title="永久链接至标题"></a></h3>
<ul>
<li><p>The range of possible file names for C extensions has been narrowed.
Very rarely used spellings have been suppressed: under POSIX, files
named <code class="docutils literal notranslate"><span class="pre">xxxmodule.so</span></code>, <code class="docutils literal notranslate"><span class="pre">xxxmodule.abi3.so</span></code> and
<code class="docutils literal notranslate"><span class="pre">xxxmodule.cpython-*.so</span></code> are no longer recognized as implementing
the <code class="docutils literal notranslate"><span class="pre">xxx</span></code> module. If you had been generating such files, you have
to switch to the other spellings (i.e., remove the <code class="docutils literal notranslate"><span class="pre">module</span></code> string
from the file names).</p>
<p>(在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=14040">bpo-14040</a> 中实现。)</p>
</li>
</ul>
</section>
<section id="command-line-switch-changes">
<h3>命令行开关的变化<a class="headerlink" href="#command-line-switch-changes" title="永久链接至标题"></a></h3>
<ul>
<li><p>删除了 -Q命令-line旗标 和相关工具。 检查 sys.flags.division_warning 的代码需要更新。</p>
<p><a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10998">bpo-10998</a>,由 Éric Araujo 贡献。)</p>
</li>
<li><p><strong class="program">python</strong> 附带 <a class="reference internal" href="../using/cmdline.html#id3"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a> 启动时,<code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">site</span></code> 将不再向模块搜索路径添加 site 专属路径。 在之前版本中则会这样做。</p>
<p>(由 Carl Meyer 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=11591">bpo-11591</a> 中贡献并由 Éric Araujo 进行修改。)</p>
</li>
</ul>
</section>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">Python 3.3 有什么新变化</a><ul>
<li><a class="reference internal" href="#summary-release-highlights">摘要 -- 发布重点</a></li>
<li><a class="reference internal" href="#pep-405-virtual-environments">PEP 405: 虚拟环境</a></li>
<li><a class="reference internal" href="#pep-420-implicit-namespace-packages">PEP 420: 隐式命名空间包</a></li>
<li><a class="reference internal" href="#pep-3118-new-memoryview-implementation-and-buffer-protocol-documentation">PEP 3118: 新的内存视图实现和缓冲协议文档</a><ul>
<li><a class="reference internal" href="#features">相关特性</a></li>
<li><a class="reference internal" href="#api-changes">API 的变化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-393-flexible-string-representation">PEP 393: 灵活的字符串表示</a><ul>
<li><a class="reference internal" href="#functionality">功能</a></li>
<li><a class="reference internal" href="#performance-and-resource-usage">性能和资源使用情况</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-397-python-launcher-for-windows">PEP 397: 适用于Windows的Python启动器</a></li>
<li><a class="reference internal" href="#pep-3151-reworking-the-os-and-io-exception-hierarchy">PEP 3151: 重写 OS 和 IO 异常的层次结构</a></li>
<li><a class="reference internal" href="#pep-380-syntax-for-delegating-to-a-subgenerator">PEP 380: 委托给子生成器的语法</a></li>
<li><a class="reference internal" href="#pep-409-suppressing-exception-context">PEP 409: 清除异常上下文</a></li>
<li><a class="reference internal" href="#pep-414-explicit-unicode-literals">PEP 414: 显式的Unicode文本</a></li>
<li><a class="reference internal" href="#pep-3155-qualified-name-for-classes-and-functions">PEP 3155: 类和函数的限定名称</a></li>
<li><a class="reference internal" href="#pep-412-key-sharing-dictionary">PEP 412: Key-Sharing Dictionary</a></li>
<li><a class="reference internal" href="#pep-362-function-signature-object">PEP 362: 函数签名对象</a></li>
<li><a class="reference internal" href="#pep-421-adding-sys-implementation">PEP 421: 添加 sys.implementation</a><ul>
<li><a class="reference internal" href="#simplenamespace">SimpleNamespace</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-importlib-as-the-implementation-of-import">使用 importlib 作为导入的实现</a><ul>
<li><a class="reference internal" href="#new-apis">新的API</a></li>
<li><a class="reference internal" href="#visible-changes">可见的改变</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a></li>
<li><a class="reference internal" href="#a-finer-grained-import-lock">A Finer-Grained Import Lock</a></li>
<li><a class="reference internal" href="#builtin-functions-and-types">Builtin functions and types</a></li>
<li><a class="reference internal" href="#new-modules">新增模块</a><ul>
<li><a class="reference internal" href="#faulthandler">faulthandler</a></li>
<li><a class="reference internal" href="#ipaddress">ipaddress</a></li>
<li><a class="reference internal" href="#lzma">lzma</a></li>
</ul>
</li>
<li><a class="reference internal" href="#improved-modules">改进的模块</a><ul>
<li><a class="reference internal" href="#abc">abc</a></li>
<li><a class="reference internal" href="#array">array</a></li>
<li><a class="reference internal" href="#base64">base64</a></li>
<li><a class="reference internal" href="#binascii">binascii</a></li>
<li><a class="reference internal" href="#bz2">bz2</a></li>
<li><a class="reference internal" href="#codecs">编码器</a></li>
<li><a class="reference internal" href="#collections">collections</a></li>
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
<li><a class="reference internal" href="#crypt">crypt</a></li>
<li><a class="reference internal" href="#curses">curses</a></li>
<li><a class="reference internal" href="#datetime">datetime</a></li>
<li><a class="reference internal" href="#decimal">decimal</a><ul>
<li><a class="reference internal" href="#id1">相关特性</a></li>
<li><a class="reference internal" href="#id2">API 的变化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#email">email</a><ul>
<li><a class="reference internal" href="#policy-framework">Policy Framework</a></li>
<li><a class="reference internal" href="#provisional-policy-with-new-header-api">Provisional Policy with New Header API</a></li>
<li><a class="reference internal" href="#other-api-changes">Other API Changes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#ftplib">ftplib</a></li>
<li><a class="reference internal" href="#functools">functools</a></li>
<li><a class="reference internal" href="#gc">gc</a></li>
<li><a class="reference internal" href="#hmac">hmac</a></li>
<li><a class="reference internal" href="#http">http</a></li>
<li><a class="reference internal" href="#html">html</a></li>
<li><a class="reference internal" href="#imaplib">imaplib</a></li>
<li><a class="reference internal" href="#inspect">inspect</a></li>
<li><a class="reference internal" href="#io">io</a></li>
<li><a class="reference internal" href="#itertools">itertools</a></li>
<li><a class="reference internal" href="#logging">logging</a></li>
<li><a class="reference internal" href="#math">math</a></li>
<li><a class="reference internal" href="#mmap">mmap</a></li>
<li><a class="reference internal" href="#multiprocessing">multiprocessing</a></li>
<li><a class="reference internal" href="#nntplib">nntplib</a></li>
<li><a class="reference internal" href="#os">os</a></li>
<li><a class="reference internal" href="#pdb">pdb</a></li>
<li><a class="reference internal" href="#pickle">pickle</a></li>
<li><a class="reference internal" href="#pydoc">pydoc</a></li>
<li><a class="reference internal" href="#re">re</a></li>
<li><a class="reference internal" href="#sched">sched</a></li>
<li><a class="reference internal" href="#select">select</a></li>
<li><a class="reference internal" href="#shlex">shlex</a></li>
<li><a class="reference internal" href="#shutil">shutil</a></li>
<li><a class="reference internal" href="#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="#socket">socket</a></li>
<li><a class="reference internal" href="#socketserver">socketserver</a></li>
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
<li><a class="reference internal" href="#ssl">ssl</a></li>
<li><a class="reference internal" href="#stat">stat</a></li>
<li><a class="reference internal" href="#struct">struct</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="#tarfile">tarfile</a></li>
<li><a class="reference internal" href="#tempfile">tempfile</a></li>
<li><a class="reference internal" href="#textwrap">textwrap</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="#types">types</a></li>
<li><a class="reference internal" href="#unittest">unittest</a></li>
<li><a class="reference internal" href="#urllib">urllib</a></li>
<li><a class="reference internal" href="#webbrowser">webbrowser</a></li>
<li><a class="reference internal" href="#xml-etree-elementtree">xml.etree.ElementTree</a></li>
<li><a class="reference internal" href="#zlib">zlib</a></li>
</ul>
</li>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a></li>
<li><a class="reference internal" href="#deprecated">弃用</a><ul>
<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>
<li><a class="reference internal" href="#deprecated-functions-and-types-of-the-c-api">已弃用的 C API 函数和类型</a></li>
<li><a class="reference internal" href="#deprecated-features">弃用的功能</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-3-3">移植到 Python 3.3</a><ul>
<li><a class="reference internal" href="#porting-python-code">移植 Python 代码</a></li>
<li><a class="reference internal" href="#porting-c-code">移植 C 代码</a></li>
<li><a class="reference internal" href="#building-c-extensions">Building C extensions</a></li>
<li><a class="reference internal" href="#command-line-switch-changes">命令行开关的变化</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.4.html"
title="上一章">Python 3.4 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="3.2.html"
title="下一章">Python 3.2 有什么新变化</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.3.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.2.html" title="Python 3.2 有什么新变化"
>下一页</a> |</li>
<li class="right" >
<a href="3.4.html" title="Python 3.4 有什么新变化"
>上一页</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>