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

2582 lines
326 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Python 2.7 有什么新变化 &#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 2.6 有什么新变化" href="2.6.html" />
<link rel="prev" title="Python 3.0 有什么新变化" href="3.0.html" />
<link rel="canonical" href="https://docs.python.org/3/whatsnew/2.7.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 2.7 有什么新变化</a><ul>
<li><a class="reference internal" href="#the-future-for-python-2-x">Python 2.x的未来</a></li>
<li><a class="reference internal" href="#changes-to-the-handling-of-deprecation-warnings">对于弃用警告处理方式的改变</a></li>
<li><a class="reference internal" href="#python-3-1-features">Python 3.1 特性</a></li>
<li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372将有序字典 添加到收藏集</a></li>
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
<li><a class="reference internal" href="#pep-389-the-argparse-module-for-parsing-command-lines">PEP 389用于解析命令行的 argparse 模块</a></li>
<li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: 基于字典的日志配置</a></li>
<li><a class="reference internal" href="#pep-3106-dictionary-views">PEP 3106: 字典视图</a></li>
<li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: memoryview 对象</a></li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a><ul>
<li><a class="reference internal" href="#interpreter-changes">解释器改动</a></li>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-and-improved-modules">新增和改进的模块</a><ul>
<li><a class="reference internal" href="#new-module-importlib">新模块importlib</a></li>
<li><a class="reference internal" href="#new-module-sysconfig">新模块sysconfig</a></li>
<li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttkTk 主题组件</a></li>
<li><a class="reference internal" href="#updated-module-unittest">更新的模块unittest</a></li>
<li><a class="reference internal" href="#updated-module-elementtree-1-3">更新的模块ElementTree 1.3</a></li>
</ul>
</li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a><ul>
<li><a class="reference internal" href="#capsules">Capsule 对象</a></li>
<li><a class="reference internal" href="#port-specific-changes-windows">特定于 Windows 的更改:</a></li>
<li><a class="reference internal" href="#port-specific-changes-mac-os-x">特定于 Mac OS X 的更改:</a></li>
<li><a class="reference internal" href="#port-specific-changes-freebsd">特定于 FreeBSD 的更改:</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-changes-and-fixes">其他的改变和修正</a></li>
<li><a class="reference internal" href="#porting-to-python-2-7">移植到 Python 2.7</a></li>
<li><a class="reference internal" href="#new-features-added-to-python-2-7-maintenance-releases">New Features Added to Python 2.7 Maintenance Releases</a><ul>
<li><a class="reference internal" href="#two-new-environment-variables-for-debug-mode">Two new environment variables for debug mode</a></li>
<li><a class="reference internal" href="#pep-434-idle-enhancement-exception-for-all-branches">PEP 434: IDLE Enhancement Exception for All Branches</a></li>
<li><a class="reference internal" href="#pep-466-network-security-enhancements-for-python-2-7">PEP 466: 针对 Python 2.7 的网络安全加固</a></li>
<li><a class="reference internal" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7">PEP 477: 将 ensurepip (PEP 453) 向下移植到 Python 2.7</a><ul>
<li><a class="reference internal" href="#bootstrapping-pip-by-default">默认对 pip 进行初始设置</a></li>
<li><a class="reference internal" href="#documentation-changes">文档更改</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">PEP 476: 默认为 stdlib http 客户端启用证书验证</a></li>
<li><a class="reference internal" href="#pep-493-https-verification-migration-tools-for-python-2-7">PEP 493适用于Python 2.7 的 HTTPS 验证迁移工具</a></li>
<li><a class="reference internal" href="#new-make-regen-all-build-target">新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标</a></li>
<li><a class="reference internal" href="#removal-of-make-touch-build-target">移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标</a></li>
</ul>
</li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.0.html"
title="上一章">Python 3.0 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="2.6.html"
title="下一章">Python 2.6 有什么新变化</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/2.7.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/2.7.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="2.6.html" title="Python 2.6 有什么新变化"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="3.0.html" title="Python 3.0 有什么新变化"
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-2-7">
<h1>Python 2.7 有什么新变化<a class="headerlink" href="#what-s-new-in-python-2-7" title="永久链接至标题"></a></h1>
<dl class="field-list simple">
<dt class="field-odd">作者</dt>
<dd class="field-odd"><p>A.M. Kuchling (amk at amk.ca)</p>
</dd>
</dl>
<p>本文介绍了Python 2.7 的新功能。 Python 2.7 于2010年7月3日发布。</p>
<p>数字处理在许多方面得到了改进,包括浮点数和 <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> 类。标准库中有一些有用的补充,例如大大增强的 <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> 模块,用于解析命令行选项的 <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> 模块,在 <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> 模块中方便的 <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a><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> 类,以及许多其他改进。</p>
<p>Python 2.7计划成为2.x版本的最后一个发布版本因此我们努力使其成为长期支持的好版本。为了帮助迁移到Python 3我们在2.7中包含了几个来自Python 3.x系列的新特性。</p>
<p>本文并不试图提供新特性的完整规范说明而是提供一个方便的概览。要了解完整的细节请参阅Python 2.7的文档。如果你想了解有关设计和实现的具体考量请参阅特定新特性的PEP或在https://bugs.python.org上讨论更改的问题。在可能的情况下“What's New in Python”链接到每个更改的错误修正/补丁项。</p>
<section id="the-future-for-python-2-x">
<span id="whatsnew27-python31"></span><h2>Python 2.x的未来<a class="headerlink" href="#the-future-for-python-2-x" title="永久链接至标题"></a></h2>
<p>Python 2.7 是 2.x 系列中的最后一个主版本因为Python 维护人员已将新功能开发工作的重点转移到了 Python 3.x 系列中。这意味着,尽管 Python 2 会继续修复bug并更新以便在新的硬件和支持操作系统版本上正确构建但不会有新的功能发布。</p>
<p>然而,尽管在 Python 2.7 和 Python 3 之间有一个很大的公共子集,并且迁移到该公共子集或直接迁移到 Python 3 所涉及的许多更改可以安全地自动化完成。但是一些其他更改特别是那些与Unicode处理相关的更改可能需要仔细考虑并且最好用自动化回归测试套件进行健壮性测试以便有效地迁移。</p>
<p>这意味着 Python2.7 将长期保留,为尚未移植到 Python 3 的生产系统提供一个稳定且受支持的基础平台。Python 2.7系列的预期完整生命周期在 <span class="target" id="index-39"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0373"><strong>PEP 373</strong></a> 中有详细介绍。</p>
<p>长期保留 2.7 版的的一些关键后果:</p>
<ul class="simple">
<li><p>如上所述与早期的2.x版本相比2.7版本的维护时间更长。目前预计核心开发团队将继续支持Python 2.7接收安全更新和其他错误修复直到至少2020年首次发布后10年相比之下通常的支持期为18--24个月</p></li>
<li><p>随着 Python 2.7 标准库的老化,有效地利用 Python 包索引(直接或通过重新分发者)对 Python 2 用户来说变得更加重要。除了各种任务的第三方包之外,可用的包还包括与 Python 2 兼容的 Python 3 标准库中的新模块和功能的后端移植,以及各种工具和库,这些工具和库可以让用户更容易迁移到 Python 3。 <a class="reference external" href="https://packaging.python.org">Python 包用户指南</a> 提供了从 Python 包索引的下载和安装软件的指导。</p></li>
<li><p>虽然现在增强 Python 2 的首选方法是在Python包索引上发布新包但这种方法不一定适用于所有情况尤其是与网络安全相关的情况。在一些特殊情况下如果在PyPI上发布新的或更新的包无法得到充分的处理则可以使用Python增强建议过程来提出直接在Python 2标准库中添加新功能。任何此类添加及其添加的维护版本将在下面的 <a class="reference internal" href="#py27-maintenance-enhancements"><span class="std std-ref">New Features Added to Python 2.7 Maintenance Releases</span></a> 部分中注明。</p></li>
</ul>
<p>对于希望从 Python2 迁移到 Python3 的项目,或者对于希望同时支持 Python2 和 Python3 用户的库和框架开发人员,可以使用各种工具和指南来帮助决定合适的方法并管理所涉及的一些技术细节。建议从 <a class="reference internal" href="../howto/pyporting.html#pyporting-howto"><span class="std std-ref">将 Python 2 代码迁移到 Python 3</span></a> 操作指南开始。</p>
</section>
<section id="changes-to-the-handling-of-deprecation-warnings">
<h2>对于弃用警告处理方式的改变<a class="headerlink" href="#changes-to-the-handling-of-deprecation-warnings" title="永久链接至标题"></a></h2>
<p>对于 Python 2.7,一个策略决定是默认情况下禁止只对开发人员有兴趣的警告。 现在,除非另有要求,否则将忽略 <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> 及其子类,以防止用户看到应用程序触发的警告。 这个更改也在成为Python 3.2 的分歧点上进行了。 (在 stdlib-sig 上进行了讨论,并在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7319">bpo-7319</a> 中执行。)</p>
<p>在以前的版本中,默认情况下启用了 <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> 消息,为 Python 开发人员提供了一个明确的指示,说明他们的代码可能在未来的 Python 主要版本中出现问题。</p>
<p>然而,越来越多基于 Python 的应用程序的用户并不直接参与这些应用程序的开发。 <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> 消息与这些用户无关,这让他们担心应用能否真正正常工作,并让应用开发人员承担起回应这些担忧的负担。</p>
<p>显示通过使用 <a class="reference internal" href="../using/cmdline.html#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-Wdefault</span></code></a> (简写: <a class="reference internal" href="../using/cmdline.html#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-Wd</span></code></a>) 开关运行 Python或者在运行 Python 之前将 <span class="target" id="index-40"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></a> 环境变量设置为 <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> (或 <code class="docutils literal notranslate"><span class="pre">&quot;d&quot;</span></code>),可以重新启用 <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> 消息。 Python 代码也可以通过调用 <code class="docutils literal notranslate"><span class="pre">warnings.simplefilter('default')</span></code> 重新启用它们。</p>
<p><code class="docutils literal notranslate"><span class="pre">unittest</span></code> 模块还会在运行测试时自动重新启用弃用警告。</p>
</section>
<section id="python-3-1-features">
<h2>Python 3.1 特性<a class="headerlink" href="#python-3-1-features" title="永久链接至标题"></a></h2>
<p>就像 Python2.6 集成了 Python3.0 的特性一样2.7版也集成了 Python3.1 中的一些新特性。2.x 系列继续提供迁移到3.x系列的工具。</p>
<p>3.1 功能的部分列表,这些功能已反向移植到 2.7</p>
<ul class="simple">
<li><p>用于集合字面值的语法 (<code class="docutils literal notranslate"><span class="pre">{1,2,3}</span></code> 是一个可变集合)。</p></li>
<li><p>字典与集合推导式 (<code class="docutils literal notranslate"><span class="pre">{i:</span> <span class="pre">i*2</span> <span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">range(3)}</span></code>)。</p></li>
<li><p>单个 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句中使用多个上下文管理器。</p></li>
<li><p>一个 <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> 库的新版本,用 C 重写以提升性能。</p></li>
<li><p><a class="reference internal" href="#pep-0372"><span class="std std-ref">PEP 372将有序字典 添加到收藏集</span></a> 所描述的有序字典类型。</p></li>
<li><p><a class="reference internal" href="#pep-0378"><span class="std std-ref">PEP 378: 千位分隔符的格式说明符</span></a> 所描述的新的 <code class="docutils literal notranslate"><span class="pre">&quot;,&quot;</span></code> 格式说明符。</p></li>
<li><p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> 对象。</p></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> 模块的一个较小子集,<a class="reference external" href="#importlib-section">described below</a></p></li>
<li><p>在很多情况下,浮点数 <code class="docutils literal notranslate"><span class="pre">x</span></code><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> 更短:现在它基于最短的十进制字符串 ,保证四舍五入到 <code class="docutils literal notranslate"><span class="pre">x</span></code>。 与 Python 以前的版本一样,保证 <code class="docutils literal notranslate"><span class="pre">float(repr(x))</span></code> 能恢复到 <code class="docutils literal notranslate"><span class="pre">x</span></code></p></li>
<li><p>浮点数到字符串和字符串到浮点数的转换已正确舍入。 <a class="reference internal" href="../library/functions.html#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> 函数现在也能正确舍入。</p></li>
<li><p><a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> 类型,用于为扩展模块提供 C API 。</p></li>
<li><p><a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> C API 函数 。</p></li>
</ul>
<p>其他新的 Python3 模式警告包括:</p>
<ul class="simple">
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">operator.isCallable()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.sequenceIncludes()</span></code>,
which are not supported in 3.x, now trigger warnings.</p></li>
<li><p><code class="xref std std-option docutils literal notranslate"><span class="pre">-3</span></code> 开关现在会自动启用:option:<cite>!-Qwarn</cite> 开关,该开关会在使用经典整除法处理整数和长整数时发出警告。</p></li>
</ul>
</section>
<section id="pep-372-adding-an-ordered-dictionary-to-collections">
<span id="pep-0372"></span><h2>PEP 372将有序字典 添加到收藏集<a class="headerlink" href="#pep-372-adding-an-ordered-dictionary-to-collections" title="永久链接至标题"></a></h2>
<p>常规 Python 字典以任意顺序遍历键/值对。 多年来,许多作者编写了替代实现,以记住键最初插入的顺序。 基于这些实现的经验2.7 在 <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> 模块中引入了一个新的 <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> 类。</p>
<p><a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> API 提供与普通字典相同的接口 ,但会根据键首次插入的时间,按一定顺序遍历键和值::</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 2), (&#39;third&#39;, 3)]</span>
</pre></div>
</div>
<p>如果新条目覆盖了现有条目,则原插入位置保持不变</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 4), (&#39;third&#39;, 3)]</span>
</pre></div>
</div>
<p>删除条目并重新插入会将其移至末尾</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;third&#39;, 3), (&#39;second&#39;, 5)]</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/collections.html#collections.OrderedDict.popitem" title="collections.OrderedDict.popitem"><code class="xref py py-meth docutils literal notranslate"><span class="pre">popitem()</span></code></a> 方法有一个可选 <em>last</em> 参数 ,默认为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 。如果 <em>last</em> 为真 ,则返回并删除最近添加的密钥;如果为 false ,则选择最旧的密钥</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">od</span> <span class="o">=</span> <span class="n">OrderedDict</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="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
<span class="go">(19, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
<span class="go">(18, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">(0, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">(1, 0)</span>
</pre></div>
</div>
<p>比较两个有序字典会同时检查键和值,并要求插入顺序相同</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od2</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Move &#39;third&#39; key to the end</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">od2</span><span class="p">[</span><span class="s1">&#39;third&#39;</span><span class="p">];</span> <span class="n">od2</span><span class="p">[</span><span class="s1">&#39;third&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
<span class="go">True</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> 与普通字典进行比较时,会忽略插入顺序,只比较键和值。</p>
<p>How does the <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> work? It maintains a
doubly-linked list of keys, appending new keys to the list as they're inserted.
A secondary dictionary maps keys to their corresponding list node, so
deletion doesn't have to traverse the entire linked list and therefore
remains O(1).</p>
<p>现在,标准库支持在多个模块 中使用有序字典。</p>
<ul class="simple">
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code> module uses them by default, meaning that
configuration files can now be read, modified, and then written back
in their original order.</p></li>
<li><p><a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a><a class="reference internal" href="../library/collections.html#collections.somenamedtuple._asdict" title="collections.somenamedtuple._asdict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_asdict()</span></code></a> 方法现在会返回一个有序字典,其值的出现顺序与底层元组的索引相同。</p></li>
<li><p>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module's <a class="reference internal" href="../library/json.html#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONDecoder</span></code></a> class
constructor was extended with an <em>object_pairs_hook</em> parameter to
allow <code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code> instances to be built by the decoder.
Support was also added for third-party tools like
<a class="reference external" href="http://pyyaml.org/">PyYAML</a>.</p></li>
</ul>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-41"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0372"><strong>PEP 372</strong></a> - 将有序词典添加到集合中</dt><dd><p>PEP 由 Armin Ronacher 和 Raymond Hettinger 撰写,由 Raymond Hettinger 实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-378-format-specifier-for-thousands-separator">
<span id="pep-0378"></span><h2>PEP 378: 千位分隔符的格式说明符<a class="headerlink" href="#pep-378-format-specifier-for-thousands-separator" title="永久链接至标题"></a></h2>
<p>为了使程序输出更易读,可以在大数字上添加分隔符,将其显示为 18,446,744,073,709,551,616 而不是 18446744073709551616。</p>
<p>完全通用的解决方案是 <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> 模块 ,它可以使用不同的分隔符(北美为&quot;,&quot;,欧洲为&quot;.&quot;)和不同的分组大小,但 <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> 使用起来比较复杂,而且不适合多线程应用程序,因为不同的线程会为不同的本地生成输出。</p>
<p>因此,在 <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> 方法使用的迷你语言中添加了一个简单的逗号分组机制。 在格式化浮点数时,只需在宽度和精度之间加上逗号</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:20,.2f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">18446744073709551616.0</span><span class="p">)</span>
<span class="go">&#39;18,446,744,073,709,551,616.00&#39;</span>
</pre></div>
</div>
<p>格式化整数时,在宽度后面加上逗号:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:20,d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">18446744073709551616</span><span class="p">)</span>
<span class="go">&#39;18,446,744,073,709,551,616&#39;</span>
</pre></div>
</div>
<p>这种机制完全没有适应性;逗号总是用作分隔符,分组总是以三位数为一组。 逗号格式机制不如 <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">locale</span></code></a> 模块通用,但使用起来更方便。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-42"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a> - 千位分隔符的格式说明符</dt><dd><p>PEP 由 Raymond Hettinger 撰写,由 Eric Smith 实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-389-the-argparse-module-for-parsing-command-lines">
<h2>PEP 389用于解析命令行的 argparse 模块<a class="headerlink" href="#pep-389-the-argparse-module-for-parsing-command-lines" title="永久链接至标题"></a></h2>
<p>用于解析命令-line参数的 <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> 模块是作为 <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> 模块更强大的替代功能而添加的。</p>
<p>This means Python now supports three different modules for parsing
command-line arguments: <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code></a>, <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>, and
<a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a>. The <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal notranslate"><span class="pre">getopt</span></code></a> module closely resembles the C
library's <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> function, so it remains useful if you're writing a
Python prototype that will eventually be rewritten in C.
<a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> becomes redundant, but there are no plans to remove it
because there are many scripts still using it, and there's no
automated way to update these scripts. (Making the <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a>
API consistent with <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>'s interface was discussed but
rejected as too messy and difficult.)</p>
<p>简而言之,如果你是在编写新脚本并且不需要担心与 Python 较早版本的兼容性,请使用 <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> 而不是 <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a></p>
<p>以下是为示例代码:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Command-line example.&#39;</span><span class="p">)</span>
<span class="c1"># Add optional switches</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-v&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;is_verbose&#39;</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">&#39;produce verbose output&#39;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-o&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;output&#39;</span><span class="p">,</span>
<span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;FILE&#39;</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">&#39;direct output to FILE instead of stdout&#39;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-C&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;context&#39;</span><span class="p">,</span>
<span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;NUM&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">&#39;display NUM lines of added context&#39;</span><span class="p">)</span>
<span class="c1"># Allow any number of additional arguments.</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span>
<span class="n">help</span><span class="o">=</span><span class="s1">&#39;input filenames (default is stdin)&#39;</span><span class="p">)</span>
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="nb">print</span> <span class="n">args</span><span class="o">.</span><span class="vm">__dict__</span>
</pre></div>
</div>
<p>除非你覆盖它,否则会自动添加 <code class="xref std std-option docutils literal notranslate"><span class="pre">-h</span></code><code class="xref std std-option docutils literal notranslate"><span class="pre">--help</span></code> 开关,并产生格式化良好的输出:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">--</span><span class="n">help</span>
<span class="n">usage</span><span class="p">:</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="p">[</span><span class="o">-</span><span class="n">h</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">v</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">o</span> <span class="n">FILE</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">C</span> <span class="n">NUM</span><span class="p">]</span> <span class="p">[</span><span class="n">inputs</span> <span class="p">[</span><span class="n">inputs</span> <span class="o">...</span><span class="p">]]</span>
<span class="n">Command</span><span class="o">-</span><span class="n">line</span> <span class="n">example</span><span class="o">.</span>
<span class="n">positional</span> <span class="n">arguments</span><span class="p">:</span>
<span class="n">inputs</span> <span class="nb">input</span> <span class="n">filenames</span> <span class="p">(</span><span class="n">default</span> <span class="ow">is</span> <span class="n">stdin</span><span class="p">)</span>
<span class="n">optional</span> <span class="n">arguments</span><span class="p">:</span>
<span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="o">--</span><span class="n">help</span> <span class="n">show</span> <span class="n">this</span> <span class="n">help</span> <span class="n">message</span> <span class="ow">and</span> <span class="n">exit</span>
<span class="o">-</span><span class="n">v</span> <span class="n">produce</span> <span class="n">verbose</span> <span class="n">output</span>
<span class="o">-</span><span class="n">o</span> <span class="n">FILE</span> <span class="n">direct</span> <span class="n">output</span> <span class="n">to</span> <span class="n">FILE</span> <span class="n">instead</span> <span class="n">of</span> <span class="n">stdout</span>
<span class="o">-</span><span class="n">C</span> <span class="n">NUM</span> <span class="n">display</span> <span class="n">NUM</span> <span class="n">lines</span> <span class="n">of</span> <span class="n">added</span> <span class="n">context</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> 一样,命令行开关和参数将返回为一个具有通过 <em>dest</em> 形参所指定的属性的对象:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span>
<span class="p">{</span><span class="s1">&#39;output&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
<span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
<span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
<span class="s1">&#39;inputs&#39;</span><span class="p">:</span> <span class="p">[]}</span>
<span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span> <span class="o">-</span><span class="n">o</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">output</span> <span class="o">-</span><span class="n">C</span> <span class="mi">4</span> <span class="n">file1</span> <span class="n">file2</span>
<span class="p">{</span><span class="s1">&#39;output&#39;</span><span class="p">:</span> <span class="s1">&#39;/tmp/output&#39;</span><span class="p">,</span>
<span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
<span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
<span class="s1">&#39;inputs&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;file1&#39;</span><span class="p">,</span> <span class="s1">&#39;file2&#39;</span><span class="p">]}</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> has much fancier validation than <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>; you
can specify an exact number of arguments as an integer, 0 or more
arguments by passing <code class="docutils literal notranslate"><span class="pre">'*'</span></code>, 1 or more by passing <code class="docutils literal notranslate"><span class="pre">'+'</span></code>, or an
optional argument with <code class="docutils literal notranslate"><span class="pre">'?'</span></code>. A top-level parser can contain
sub-parsers to define subcommands that have different sets of
switches, as in <code class="docutils literal notranslate"><span class="pre">svn</span> <span class="pre">commit</span></code>, <code class="docutils literal notranslate"><span class="pre">svn</span> <span class="pre">checkout</span></code>, etc. You can
specify an argument's type as <a class="reference internal" href="../library/argparse.html#argparse.FileType" title="argparse.FileType"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileType</span></code></a>, which will
automatically open files for you and understands that <code class="docutils literal notranslate"><span class="pre">'-'</span></code> means
standard input or output.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">argparse</span></code></a> 文档</dt><dd><p>argparse 模块的文档页面。</p>
</dd>
<dt><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><span class="std std-ref">升级 optparse 代码</span></a></dt><dd><p>Python 文档的一部分,描述如何转换使用了 <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (已移除)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> 的代码。</p>
</dd>
<dt><span class="target" id="index-43"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0389"><strong>PEP 389</strong></a> - argparse - 新的命令行解析模块</dt><dd><p>PEP 由 Steven Bethard 撰写并实现。</p>
</dd>
</dl>
</div>
</section>
<section id="pep-391-dictionary-based-configuration-for-logging">
<h2>PEP 391: 基于字典的日志配置<a class="headerlink" href="#pep-391-dictionary-based-configuration-for-logging" title="永久链接至标题"></a></h2>
<p>The <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> module is very flexible; applications can define
a tree of logging subsystems, and each logger in this tree can filter
out certain messages, format them differently, and direct messages to
a varying number of handlers.</p>
<p>All this flexibility can require a lot of configuration. You can
write Python statements to create objects and set their properties,
but a complex set-up requires verbose but boring code.
<a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> also supports a <code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code>
function that parses a file, but the file format doesn't support
configuring filters, and it's messier to generate programmatically.</p>
<p>Python 2.7 adds a <code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code> function that
uses a dictionary to configure logging. There are many ways to
produce a dictionary from different sources: construct one with code;
parse a file containing JSON; or use a YAML parsing library if one is
installed. For more information see <a class="reference internal" href="../library/logging.config.html#logging-config-api"><span class="std std-ref">配置函数</span></a>.</p>
<p>The following example configures two loggers, the root logger and a
logger named &quot;network&quot;. Messages sent to the root logger will be
sent to the system log using the syslog protocol, and messages
to the &quot;network&quot; logger will be written to a <code class="file docutils literal notranslate"><span class="pre">network.log</span></code> file
that will be rotated once the log reaches 1MB.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">logging.config</span>
<span class="n">configdict</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">&#39;version&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="c1"># Configuration schema in use; must be 1 for now</span>
<span class="s1">&#39;formatters&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;standard&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;format&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(name)-15s</span><span class="s1"> &#39;</span>
<span class="s1">&#39;</span><span class="si">%(levelname)-8s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">&#39;</span><span class="p">)}},</span>
<span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;netlog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;backupCount&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
<span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;logging.handlers.RotatingFileHandler&#39;</span><span class="p">,</span>
<span class="s1">&#39;filename&#39;</span><span class="p">:</span> <span class="s1">&#39;/logs/network.log&#39;</span><span class="p">,</span>
<span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
<span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;INFO&#39;</span><span class="p">,</span>
<span class="s1">&#39;maxBytes&#39;</span><span class="p">:</span> <span class="mi">1000000</span><span class="p">},</span>
<span class="s1">&#39;syslog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;logging.handlers.SysLogHandler&#39;</span><span class="p">,</span>
<span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
<span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;ERROR&#39;</span><span class="p">}},</span>
<span class="c1"># Specify all the subordinate loggers</span>
<span class="s1">&#39;loggers&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;network&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;netlog&#39;</span><span class="p">]</span>
<span class="p">}</span>
<span class="p">},</span>
<span class="c1"># Specify properties of the root logger</span>
<span class="s1">&#39;root&#39;</span><span class="p">:</span> <span class="p">{</span>
<span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;syslog&#39;</span><span class="p">]</span>
<span class="p">},</span>
<span class="p">}</span>
<span class="c1"># Set up configuration</span>
<span class="n">logging</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">dictConfig</span><span class="p">(</span><span class="n">configdict</span><span class="p">)</span>
<span class="c1"># As an example, log two error messages</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Database not found&#39;</span><span class="p">)</span>
<span class="n">netlogger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;network&#39;</span><span class="p">)</span>
<span class="n">netlogger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Connection failed&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Three smaller enhancements to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal notranslate"><span class="pre">logging</span></code></a> module, all
implemented by Vinay Sajip, are:</p>
<ul class="simple">
<li><p>The <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> class now supports
syslogging over TCP. The constructor has a <em>socktype</em> parameter
giving the type of socket to use, either <a class="reference internal" href="../library/socket.html#socket.SOCK_DGRAM" title="socket.SOCK_DGRAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">socket.SOCK_DGRAM</span></code></a>
for UDP or <a class="reference internal" href="../library/socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal notranslate"><span class="pre">socket.SOCK_STREAM</span></code></a> for TCP. The default
protocol remains UDP.</p></li>
<li><p><a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><span class="pre">Logger</span></code></a> instances gained a <a class="reference internal" href="../library/logging.html#logging.Logger.getChild" title="logging.Logger.getChild"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getChild()</span></code></a>
method that retrieves a descendant logger using a relative path.
For example, once you retrieve a logger by doing <code class="docutils literal notranslate"><span class="pre">log</span> <span class="pre">=</span> <span class="pre">getLogger('app')</span></code>,
calling <code class="docutils literal notranslate"><span class="pre">log.getChild('network.listen')</span></code> is equivalent to
<code class="docutils literal notranslate"><span class="pre">getLogger('app.network.listen')</span></code>.</p></li>
<li><p>The <a class="reference internal" href="../library/logging.html#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a> class gained an
<code class="xref py py-meth docutils literal notranslate"><span class="pre">isEnabledFor()</span></code> method that takes a
<em>level</em> and returns whether the underlying logger would
process a message of that level of importance.</p></li>
</ul>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-44"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0391"><strong>PEP 391</strong></a> - 基于字典的日志配置</dt><dd><p>PEP 由 Vinay Sajip 撰写并实现</p>
</dd>
</dl>
</div>
</section>
<section id="pep-3106-dictionary-views">
<h2>PEP 3106: 字典视图<a class="headerlink" href="#pep-3106-dictionary-views" title="永久链接至标题"></a></h2>
<p>字典方法 <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code></a> 在 Python 3.x 有所不同。 它们将返回名为 <em class="dfn">view</em> 的对象而不是完整的列表。</p>
<p>It's not possible to change the return values of <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code></a>,
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code></a> in Python 2.7 because
too much code would break. Instead the 3.x versions were added
under the new names <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewkeys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewvalues()</span></code>,
and <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewitems()</span></code>.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">{0: &#39;A&#39;, 130: &#39;N&#39;, 10: &#39;B&#39;, 140: &#39;O&#39;, 20: ..., 250: &#39;Z&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="go">dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])</span>
</pre></div>
</div>
<p>视图可以被迭代,但键和条目视图的行为也很像是集合。 <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> 运算符执行交集运算,<code class="docutils literal notranslate"><span class="pre">|</span></code> 执行并集运算:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d2</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">**</span><span class="mf">.5</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">&amp;</span> <span class="n">d2</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="go">set([0.0, 10.0, 20.0, 30.0])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">|</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])</span>
</pre></div>
</div>
<p>视图会追踪字典及字典被修改时的内容变化:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
<span class="go">dict_keys([0, 130, 10, ..., 250])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="mi">260</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&amp;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
<span class="go">dict_keys([0, 130, 260, 10, ..., 250])</span>
</pre></div>
</div>
<p>但是,请注意在对视图进行迭代时你是不能添加或移除键的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">vk</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">k</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>: <span class="n">dictionary changed size during iteration</span>
</pre></div>
</div>
<p>You can use the view methods in Python 2.x code, and the 2to3
converter will change them to the standard <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code></a>,
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><span class="pre">values()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code></a> methods.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-45"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3106"><strong>PEP 3106</strong></a> - 改造 dict.keys(), .values() 和 .items()</dt><dd><p>PEP 由 Guido van Rossum 撰写。 由 Alexandre Vassalotti 反向移植到 2.7 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1967">bpo-1967</a></p>
</dd>
</dl>
</div>
</section>
<section id="pep-3137-the-memoryview-object">
<h2>PEP 3137: memoryview 对象<a class="headerlink" href="#pep-3137-the-memoryview-object" title="永久链接至标题"></a></h2>
<p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> 对象提供与 <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> 类型的接口相匹配的另一个对象的内存内容的视图。</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
<span class="go">&lt;memory at 0x37f850&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">m</span><span class="p">)</span> <span class="c1"># Returns length of underlying object</span>
<span class="go">52</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">25</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">26</span><span class="p">]</span> <span class="c1"># Indexing returns one byte</span>
<span class="go">(&#39;a&#39;, &#39;z&#39;, &#39;A&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span> <span class="o">=</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">26</span><span class="p">]</span> <span class="c1"># Slicing returns another memoryview</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span>
<span class="go">&lt;memory at 0x37f080&gt;</span>
</pre></div>
</div>
<p>视图的内容可被转换为一个字节串或整数列表:</p>
<div class="highlight-pycon3 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">tobytes</span><span class="p">()</span>
<span class="go">&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="go">[97, 98, 99, 100, 101, 102, 103, ... 121, 122]</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> 对象允许对属于可变对象的下层对象进行修改。</p>
<div class="highlight-pycon3 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="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">75</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">cannot modify read-only memory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span> <span class="c1"># Creating a mutable object</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">bytearray(b&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span> <span class="o">=</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;*&#39;</span> <span class="c1"># Assign to view, changing the bytearray.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span> <span class="c1"># The bytearray has been changed.</span>
<span class="go">bytearray(b&#39;*bcde&#39;)</span>
<span class="gp">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-46"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3137"><strong>PEP 3137</strong></a> - 不变字节和可变缓冲区</dt><dd><p>PEP 由 Guido van Rossum 撰写。 由 Travis Oliphant, Antoine Pitrou 等人实现。 由 Antoine Pitrou 向下移植到 2.7; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2396">bpo-2396</a></p>
</dd>
</dl>
</div>
</section>
<section id="other-language-changes">
<h2>其他语言特性修改<a class="headerlink" href="#other-language-changes" title="永久链接至标题"></a></h2>
<p>对Python 语言核心进行的小改动:</p>
<ul>
<li><p>已从 Python 3.x 向下移植了集合字面值语法。 使用花括号来标记可变集合的内容;集合与字典的区别在于它不包含冒号及映射的值。 <code class="docutils literal notranslate"><span class="pre">{}</span></code> 仍然表示空字典;请使用 <code class="docutils literal notranslate"><span class="pre">set()</span></code> 来表示空集合。</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span>
<span class="go">set([1, 2, 3, 4, 5])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">set</span><span class="p">()</span> <span class="c1"># empty set</span>
<span class="go">set([])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{}</span> <span class="c1"># empty dict</span>
<span class="go">{}</span>
</pre></div>
</div>
<p>由 Alexandre Vassalotti 向下移植; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2335">bpo-2335</a></p>
</li>
<li><p>字典与集合推导式是另一个从 3.x 向下移植的特性,对列表/生成器推导式进行一般化以针对集合与字典使用字面值语法。</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
<span class="go">{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{(</span><span class="s1">&#39;a&#39;</span><span class="o">*</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="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
<span class="go">set([&#39;&#39;, &#39;a&#39;, &#39;aa&#39;, &#39;aaa&#39;, &#39;aaaa&#39;, &#39;aaaaa&#39;])</span>
</pre></div>
</div>
<p>由 Alexandre Vassalotti 向下移植; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2333">bpo-2333</a></p>
</li>
<li><p>现在 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句可以在一个语句中使用多个上下文管理器。 上下文管理器将按从左到右的顺序处理并且每个都会被视为开始一个新的 <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> 语句。 这意味着:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">,</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
<span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
</pre></div>
</div>
<p>相当于:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">:</span>
<span class="k">with</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
<span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
</pre></div>
</div>
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code> function provides a very similar
function, so it's no longer necessary and has been deprecated.</p>
<p>(Proposed in <a class="reference external" href="https://codereview.appspot.com/53094">https://codereview.appspot.com/53094</a>; implemented by
Georg Brandl.)</p>
</li>
<li><p>Conversions between floating-point numbers and strings are
now correctly rounded on most platforms. These conversions occur
in many different places: <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on
floats and complex numbers; the <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> and <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a>
constructors;
numeric formatting; serializing and
deserializing floats and complex numbers using the
<a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a>, <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a>
and <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> modules;
parsing of float and imaginary literals in Python code;
and <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>-to-float conversion.</p>
<p>Related to this, the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a floating-point number <em>x</em>
now returns a result based on the shortest decimal string that's
guaranteed to round back to <em>x</em> under correct rounding (with
round-half-to-even rounding mode). Previously it gave a string
based on rounding x to 17 decimal digits.</p>
<p>The rounding library responsible for this improvement works on
Windows and on Unix platforms using the gcc, icc, or suncc
compilers. There may be a small number of platforms where correct
operation of this code cannot be guaranteed, so the code is not
used on such systems. You can find out which code is being used
by checking <a class="reference internal" href="../library/sys.html#sys.float_repr_style" title="sys.float_repr_style"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.float_repr_style</span></code></a>, which will be <code class="docutils literal notranslate"><span class="pre">short</span></code>
if the new code is in use and <code class="docutils literal notranslate"><span class="pre">legacy</span></code> if it isn't.</p>
<p>Implemented by Eric Smith and Mark Dickinson, using David Gay's
<code class="file docutils literal notranslate"><span class="pre">dtoa.c</span></code> library; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7117">bpo-7117</a>.</p>
</li>
<li><p>Conversions from long integers and regular integers to floating
point now round differently, returning the floating-point number
closest to the number. This doesn't matter for small integers that
can be converted exactly, but for large numbers that will
unavoidably lose precision, Python 2.7 now approximates more
closely. For example, Python 2.6 computed the following:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">2.9514790517935283e+20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="n">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
<span class="go">65535L</span>
</pre></div>
</div>
<p>Python 2.7's floating-point result is larger, but much closer to the
true value:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">2.9514790517935289e+20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="n">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
<span class="go">-1L</span>
</pre></div>
</div>
<p>(Implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3166">bpo-3166</a>.)</p>
<p>Integer division is also more accurate in its rounding behaviours. (Also
implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1811">bpo-1811</a>.)</p>
</li>
<li><p>Implicit coercion for complex numbers has been removed; the interpreter
will no longer ever attempt to call a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__coerce__()</span></code> method on complex
objects. (Removed by Meador Inge and Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5211">bpo-5211</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method now supports automatic numbering of the replacement
fields. This makes using <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> more closely resemble using
<code class="docutils literal notranslate"><span class="pre">%s</span></code> formatting:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">04</span><span class="p">,</span> <span class="s1">&#39;Sunday&#39;</span><span class="p">)</span>
<span class="go">&#39;2009:4:Sunday&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">:</span><span class="si">{day}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s1">&#39;Sunday&#39;</span><span class="p">)</span>
<span class="go">&#39;2009:4:Sunday&#39;</span>
</pre></div>
</div>
<p>The auto-numbering takes the fields from left to right, so the first <code class="docutils literal notranslate"><span class="pre">{...}</span></code>
specifier will use the first argument to <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>, the next
specifier will use the next argument, and so on. You can't mix auto-numbering
and explicit numbering -- either number all of your specifier fields or none
of them -- but you can mix auto-numbering and named fields, as in the second
example above. (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5237">bpo-5237</a>.)</p>
<p>Complex numbers now correctly support usage with <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a>,
and default to being right-aligned.
Specifying a precision or comma-separation applies to both the real
and imaginary parts of the number, but a specified field width and
alignment is applied to the whole of the resulting <code class="docutils literal notranslate"><span class="pre">1.5+3j</span></code>
output. (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1588">bpo-1588</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7988">bpo-7988</a>.)</p>
<p>The 'F' format code now always formats its output using uppercase characters,
so it will now produce 'INF' and 'NAN'.
(Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3382">bpo-3382</a>.)</p>
<p>A low-level change: the <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__format__()</span></code></a> method now triggers
a <a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a> if it's passed a format string,
because the <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code></a> method for <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> converts
the object to a string representation and formats that. Previously
the method silently applied the format string to the string
representation, but that could hide mistakes in Python code. If
you're supplying formatting information such as an alignment or
precision, presumably you're expecting the formatting to be applied
in some object-specific way. (Fixed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7994">bpo-7994</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> and <code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code> types gained a <code class="docutils literal notranslate"><span class="pre">bit_length</span></code>
method that returns the number of bits necessary to represent
its argument in binary:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">37</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">&#39;0b100101&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">123</span><span class="o">-</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">123</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">124</span>
</pre></div>
</div>
<p>(Contributed by Fredrik Johansson and Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3439">bpo-3439</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement will no longer try an absolute import
if a relative import (e.g. <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">.os</span> <span class="pre">import</span> <span class="pre">sep</span></code>) fails. This
fixes a bug, but could possibly break certain <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code>
statements that were only working by accident. (Fixed by Meador Inge;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7902">bpo-7902</a>.)</p></li>
<li><p>It's now possible for a subclass of the built-in <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code> type
to override the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> method. (Implemented by
Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1583863">bpo-1583863</a>.)</p></li>
<li><p>The <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> type's <a class="reference internal" href="../library/stdtypes.html#bytearray.translate" title="bytearray.translate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">translate()</span></code></a> method now accepts
<code class="docutils literal notranslate"><span class="pre">None</span></code> as its first argument. (Fixed by Georg Brandl;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4759">bpo-4759</a>.)</p>
</li>
<li><p>When using <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> and <code class="docutils literal notranslate"><span class="pre">&#64;staticmethod</span></code> to wrap
methods as class or static methods, the wrapper object now
exposes the wrapped function as their <code class="xref py py-attr docutils literal notranslate"><span class="pre">__func__</span></code> attribute.
(Contributed by Amaury Forgeot d'Arc, after a suggestion by
George Sakkis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5982">bpo-5982</a>.)</p></li>
<li><p>When a restricted set of attributes were set using <code class="docutils literal notranslate"><span class="pre">__slots__</span></code>,
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>
as you would expect. Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7604">bpo-7604</a>.)</p></li>
<li><p>Two new encodings are now supported: &quot;cp720&quot;, used primarily for
Arabic text; and &quot;cp858&quot;, a variant of CP 850 that adds the euro
symbol. (CP720 contributed by Alexander Belchenko and Amaury
Forgeot d'Arc in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1616979">bpo-1616979</a>; CP858 contributed by Tim Hatch in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8016">bpo-8016</a>.)</p></li>
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code> object will now set the <code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code> attribute
on the <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> exception when trying to open a directory
on POSIX platforms (noted by Jan Kaliszewski; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4764">bpo-4764</a>), and
now explicitly checks for and forbids writing to read-only file objects
instead of trusting the C library to catch and report the error
(fixed by Stefan Krah; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5677">bpo-5677</a>).</p></li>
<li><p>The Python tokenizer now translates line endings itself, so the
<a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> built-in function now accepts code using any
line-ending convention. Additionally, it no longer requires that the
code end in a newline.</p></li>
<li><p>Extra parentheses in function definitions are illegal in Python 3.x,
meaning that you get a syntax error from <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">f((x)):</span> <span class="pre">pass</span></code>. In
Python3-warning mode, Python 2.7 will now warn about this odd usage.
(Noted by James Lingard; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7362">bpo-7362</a>.)</p></li>
<li><p>It's now possible to create weak references to old-style class
objects. New-style classes were always weak-referenceable. (Fixed
by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8268">bpo-8268</a>.)</p></li>
<li><p>When a module object is garbage-collected, the module's dictionary is
now only cleared if no one else is holding a reference to the
dictionary (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7140">bpo-7140</a>).</p></li>
</ul>
<section id="interpreter-changes">
<span id="new-27-interpreter"></span><h3>解释器改动<a class="headerlink" href="#interpreter-changes" title="永久链接至标题"></a></h3>
<p>A new environment variable, <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONWARNINGS</span></code></a>,
allows controlling warnings. It should be set to a string
containing warning settings, equivalent to those
used with the <a class="reference internal" href="../using/cmdline.html#cmdoption-w"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> switch, separated by commas.
(Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7301">bpo-7301</a>.)</p>
<p>For example, the following setting will print warnings every time
they occur, but turn warnings from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code> module into an
error. (The exact syntax for setting an environment variable varies
across operating systems and shells.)</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">export</span> <span class="n">PYTHONWARNINGS</span><span class="o">=</span><span class="nb">all</span><span class="p">,</span><span class="n">error</span><span class="p">:::</span><span class="n">Cookie</span><span class="p">:</span><span class="mi">0</span>
</pre></div>
</div>
</section>
<section id="optimizations">
<h3>性能优化<a class="headerlink" href="#optimizations" title="永久链接至标题"></a></h3>
<p>Several performance enhancements have been added:</p>
<ul>
<li><p>A new opcode was added to perform the initial setup for
<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statements, looking up the <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> and
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> methods. (Contributed by Benjamin Peterson.)</p></li>
<li><p>The garbage collector now performs better for one common usage
pattern: when many objects are being allocated without deallocating
any of them. This would previously take quadratic
time for garbage collection, but now the number of full garbage collections
is reduced as the number of objects on the heap grows.
The new logic only performs a full garbage collection pass when
the middle generation has been collected 10 times and when the
number of survivor objects from the middle generation exceeds 10% of
the number of objects in the oldest generation. (Suggested by Martin
von Löwis and implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4074">bpo-4074</a>.)</p></li>
<li><p>The garbage collector tries to avoid tracking simple containers
which can't be part of a cycle. In Python 2.7, this is now true for
tuples and dicts containing atomic types (such as ints, strings,
etc.). Transitively, a dict containing tuples of atomic types won't
be tracked either. This helps reduce the cost of each
garbage collection by decreasing the number of objects to be
considered and traversed by the collector.
(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4688">bpo-4688</a>.)</p></li>
<li><p>Long integers are now stored internally either in base 2**15 or in base
2**30, the base being determined at build time. Previously, they
were always stored in base 2**15. Using base 2**30 gives
significant performance improvements on 64-bit machines, but
benchmark results on 32-bit machines have been mixed. Therefore,
the default is to use base 2**30 on 64-bit machines and base 2**15
on 32-bit machines; on Unix, there's a new configure option
<code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-big-digits</span></code> that can be used to override this default.</p>
<p>Apart from the performance improvements this change should be
invisible to end users, with one exception: for testing and
debugging purposes there's a new structseq <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.long_info</span></code> that
provides information about the internal format, giving the number of
bits per digit and the size in bytes of the C type used to store
each digit:</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">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">long_info</span>
<span class="go">sys.long_info(bits_per_digit=30, sizeof_digit=4)</span>
</pre></div>
</div>
<p>(由 Mark Dickinson在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4258">bpo-4258</a> 贡献)</p>
<p>Another set of changes made long objects a few bytes smaller: 2 bytes
smaller on 32-bit systems and 6 bytes on 64-bit.
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5260">bpo-5260</a>.)</p>
</li>
<li><p>The division algorithm for long integers has been made faster
by tightening the inner loop, doing shifts instead of multiplications,
and fixing an unnecessary extra iteration.
Various benchmarks show speedups of between 50% and 150% for long
integer divisions and modulo operations.
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5512">bpo-5512</a>.)
Bitwise operations are also significantly faster (initial patch by
Gregory Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1087418">bpo-1087418</a>).</p></li>
<li><p>The implementation of <code class="docutils literal notranslate"><span class="pre">%</span></code> checks for the left-side operand being
a Python string and special-cases it; this results in a 1--3%
performance increase for applications that frequently use <code class="docutils literal notranslate"><span class="pre">%</span></code>
with strings, such as templating libraries.
(Implemented by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5176">bpo-5176</a>.)</p></li>
<li><p>List comprehensions with an <code class="docutils literal notranslate"><span class="pre">if</span></code> condition are compiled into
faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
by Jeffrey Yasskin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4715">bpo-4715</a>.)</p></li>
<li><p>Converting an integer or long integer to a decimal string was made
faster by special-casing base 10 instead of using a generalized
conversion function that supports arbitrary bases.
(Patch by Gawain Bolton; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6713">bpo-6713</a>.)</p></li>
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rindex()</span></code>,
<code class="xref py py-meth docutils literal notranslate"><span class="pre">rpartition()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code> methods of string-like types
(strings, Unicode strings, 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 use a
fast reverse-search algorithm instead of a character-by-character
scan. This is sometimes faster by a factor of 10. (Added by
Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7462">bpo-7462</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7622">bpo-7622</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> modules now automatically
intern the strings used for attribute names, reducing memory usage
of the objects resulting from unpickling. (Contributed by Jake
McGuire; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5084">bpo-5084</a>.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> module now special-cases dictionaries,
nearly halving the time required to pickle them.
(Contributed by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5670">bpo-5670</a>.)</p></li>
</ul>
</section>
</section>
<section id="new-and-improved-modules">
<h2>新增和改进的模块<a class="headerlink" href="#new-and-improved-modules" title="永久链接至标题"></a></h2>
<p>As in every release, Python's standard library received a number of
enhancements and bug fixes. Here's a partial list of the most notable
changes, sorted alphabetically by module name. Consult the
<code class="file docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more complete list of
changes, or look through the Subversion logs for all the details.</p>
<ul>
<li><p>The <a class="reference internal" href="../library/bdb.html#module-bdb" title="bdb: Debugger framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bdb</span></code></a> module's base debugging class <a class="reference internal" href="../library/bdb.html#bdb.Bdb" title="bdb.Bdb"><code class="xref py py-class docutils literal notranslate"><span class="pre">Bdb</span></code></a>
gained a feature for skipping modules. The constructor
now takes an iterable containing glob-style patterns such as
<code class="docutils literal notranslate"><span class="pre">django.*</span></code>; the debugger will not step into stack frames
from a module that matches one of these patterns.
(Contributed by Maru Newby after a suggestion by
Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5142">bpo-5142</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a> module now supports the buffer API, so it can be
used with <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> instances and other similar buffer objects.
(Backported from 3.x by Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7703">bpo-7703</a>.)</p></li>
<li><p>Updated module: the <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb</span></code> module has been updated from 4.7.2devel9
to version 4.8.4 of
<a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">the pybsddb package</a>.
The new version features better Python 3.x compatibility, various bug fixes,
and adds several new BerkeleyDB flags and methods.
(Updated by Jesús Cea Avión; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8156">bpo-8156</a>. The pybsddb
changelog can be read at <a class="reference external" href="http://hg.jcea.es/pybsddb/file/tip/ChangeLog">http://hg.jcea.es/pybsddb/file/tip/ChangeLog</a>.)</p></li>
<li><p>The <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> module's <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> now supports the context
management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">bz2.BZ2File(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
(Contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3860">bpo-3860</a>.)</p></li>
<li><p>New class: 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 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 is useful for tallying data. <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> instances
behave mostly like dictionaries but return zero for missing keys instead of
raising a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="s1">&#39;here is a sample of english text&#39;</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">c</span><span class="p">[</span><span class="n">letter</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">Counter({&#39; &#39;: 6, &#39;e&#39;: 5, &#39;s&#39;: 3, &#39;a&#39;: 2, &#39;i&#39;: 2, &#39;h&#39;: 2,</span>
<span class="go">&#39;l&#39;: 2, &#39;t&#39;: 2, &#39;g&#39;: 1, &#39;f&#39;: 1, &#39;m&#39;: 1, &#39;o&#39;: 1, &#39;n&#39;: 1,</span>
<span class="go">&#39;p&#39;: 1, &#39;r&#39;: 1, &#39;x&#39;: 1})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;z&#39;</span><span class="p">]</span>
<span class="go">0</span>
</pre></div>
</div>
<p>There are three additional <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> methods.
<a class="reference internal" href="../library/collections.html#collections.Counter.most_common" title="collections.Counter.most_common"><code class="xref py py-meth docutils literal notranslate"><span class="pre">most_common()</span></code></a> returns the N most common
elements and their counts. <a class="reference internal" href="../library/collections.html#collections.Counter.elements" title="collections.Counter.elements"><code class="xref py py-meth docutils literal notranslate"><span class="pre">elements()</span></code></a>
returns an iterator over the contained elements, repeating each
element as many times as its count.
<a class="reference internal" href="../library/collections.html#collections.Counter.subtract" title="collections.Counter.subtract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subtract()</span></code></a> takes an iterable and
subtracts one for each element instead of adding; if the argument is
a dictionary or another <code class="xref py py-class docutils literal notranslate"><span class="pre">Counter</span></code>, the counts are
subtracted.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="go">[(&#39; &#39;, 6), (&#39;e&#39;, 5), (&#39;s&#39;, 3), (&#39;a&#39;, 2), (&#39;i&#39;, 2)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">()</span> <span class="o">-&gt;</span>
<span class="go"> &#39;a&#39;, &#39;a&#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;,</span>
<span class="go"> &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;g&#39;, &#39;f&#39;, &#39;i&#39;, &#39;i&#39;,</span>
<span class="go"> &#39;h&#39;, &#39;h&#39;, &#39;m&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;n&#39;, &#39;p&#39;, &#39;s&#39;,</span>
<span class="go"> &#39;s&#39;, &#39;s&#39;, &#39;r&#39;, &#39;t&#39;, &#39;t&#39;, &#39;x&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="s1">&#39;very heavy on the letter e&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span> <span class="c1"># Count is now lower</span>
<span class="go">-1</span>
</pre></div>
</div>
<p>Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1696199">bpo-1696199</a>.</p>
<p>New class: <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> is described in the earlier
section <a class="reference internal" href="#pep-0372"><span class="std std-ref">PEP 372将有序字典 添加到收藏集</span></a>.</p>
<p>New method: The <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">deque</span></code></a> data type now has a
<a class="reference internal" href="../library/collections.html#collections.deque.count" title="collections.deque.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> method that returns the number of
contained elements equal to the supplied argument <em>x</em>, and a
<a class="reference internal" href="../library/collections.html#collections.deque.reverse" title="collections.deque.reverse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">reverse()</span></code></a> method that reverses the elements
of the deque in-place. <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal notranslate"><span class="pre">deque</span></code></a> also exposes its maximum
length as the read-only <a class="reference internal" href="../library/collections.html#collections.deque.maxlen" title="collections.deque.maxlen"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxlen</span></code></a> attribute.
(Both features added by Raymond Hettinger.)</p>
<p>The <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">namedtuple</span></code></a> class now has an optional <em>rename</em> parameter.
If <em>rename</em> is true, field names that are invalid because they've
been repeated or aren't legal Python identifiers will be
renamed to legal names that are derived from the field's
position within the list of fields:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;T&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;field1&#39;</span><span class="p">,</span> <span class="s1">&#39;$illegal&#39;</span><span class="p">,</span> <span class="s1">&#39;for&#39;</span><span class="p">,</span> <span class="s1">&#39;field2&#39;</span><span class="p">],</span> <span class="n">rename</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span><span class="o">.</span><span class="n">_fields</span>
<span class="go">(&#39;field1&#39;, &#39;_1&#39;, &#39;_2&#39;, &#39;field2&#39;)</span>
</pre></div>
</div>
<p>(Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1818">bpo-1818</a>.)</p>
<p>Finally, the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code> abstract base class now
returns <a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-const docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> if a mapping is compared to
another type that isn't a <code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code>.
(Fixed by Daniel Stutzbach; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8729">bpo-8729</a>.)</p>
</li>
<li><p>Constructors for the parsing classes in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code> module now
take an <em>allow_no_value</em> parameter, defaulting to false; if true,
options without values will be allowed. 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">ConfigParser</span><span class="o">,</span> <span class="nn">StringIO</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sample_config</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">[mysqld]</span>
<span class="gp">... </span><span class="s2">user = mysql</span>
<span class="gp">... </span><span class="s2">pid-file = /var/run/mysqld/mysqld.pid</span>
<span class="gp">... </span><span class="s2">skip-bdb</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">RawConfigParser</span><span class="p">(</span><span class="n">allow_no_value</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">readfp</span><span class="p">(</span><span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="n">sample_config</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;user&#39;</span><span class="p">)</span>
<span class="go">&#39;mysql&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;skip-bdb&#39;</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;unknown&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="w"> </span><span class="c">...</span>
<span class="gr">NoOptionError</span>: <span class="n">No option &#39;unknown&#39; in section: &#39;mysqld&#39;</span>
</pre></div>
</div>
<p>(Contributed by Mats Kindahl; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7005">bpo-7005</a>.)</p>
</li>
<li><p>Deprecated function: <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code>, which allows
handling more than one context manager with a single <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a>
statement, has been deprecated, because the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement
now supports multiple context managers.</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code> module now ignores cookies that have an invalid
version field, one that doesn't contain an integer value. (Fixed by
John J. Lee; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3924">bpo-3924</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">copy</span></code></a> module's <a class="reference internal" href="../library/copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">deepcopy()</span></code></a> function will now
correctly copy bound instance methods. (Implemented by
Robert Collins; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1515">bpo-1515</a>.)</p></li>
<li><p>The <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> module now always converts <code class="docutils literal notranslate"><span class="pre">None</span></code> to a C <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
pointer for arguments declared as pointers. (Changed by Thomas
Heller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4606">bpo-4606</a>.) The underlying <a class="reference external" href="https://sourceware.org/libffi/">libffi library</a> has been updated to version
3.0.9, containing various fixes for different platforms. (Updated
by Matthias Klose; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8142">bpo-8142</a>.)</p></li>
<li><p>New method: the <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> module's <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> class
gained a <a class="reference internal" href="../library/datetime.html#datetime.timedelta.total_seconds" title="datetime.timedelta.total_seconds"><code class="xref py py-meth docutils literal notranslate"><span class="pre">total_seconds()</span></code></a> method that returns the
number of seconds in the duration. (Contributed by Brian Quinlan; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5788">bpo-5788</a>.)</p></li>
<li><p>New method: 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> class gained a
<a class="reference internal" href="../library/decimal.html#decimal.Decimal.from_float" title="decimal.Decimal.from_float"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_float()</span></code></a> class method that performs an exact
conversion of a floating-point number to a <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>.
This exact conversion strives for the
closest decimal approximation to the floating-point representation's value;
the resulting decimal value will therefore still include the inaccuracy,
if any.
For example, <code class="docutils literal notranslate"><span class="pre">Decimal.from_float(0.1)</span></code> returns
<code class="docutils literal notranslate"><span class="pre">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span></code>.
(Implemented by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4796">bpo-4796</a>.)</p>
<p>Comparing instances of <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> with floating-point
numbers now produces sensible results based on the numeric values
of the operands. Previously such comparisons would fall back to
Python's default rules for comparing objects, which produced arbitrary
results based on their type. Note that you still cannot combine
<code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> and floating-point in other operations such as addition,
since you should be explicitly choosing how to convert between float and
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>. (Fixed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2531">bpo-2531</a>.)</p>
<p>The constructor for <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> now accepts
floating-point numbers (added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8257">bpo-8257</a>)
and non-European Unicode characters such as Arabic-Indic digits
(contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6595">bpo-6595</a>).</p>
<p>Most of the methods of 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> class now accept integers
as well as <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> instances; the only exceptions are the
<a class="reference internal" href="../library/decimal.html#decimal.Context.canonical" title="decimal.Context.canonical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">canonical()</span></code></a> and <a class="reference internal" href="../library/decimal.html#decimal.Context.is_canonical" title="decimal.Context.is_canonical"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_canonical()</span></code></a>
methods. (Patch by Juan José Conti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7633">bpo-7633</a>.)</p>
<p>When using <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> instances with a string's
<a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method, the default alignment was previously
left-alignment. This has been changed to right-alignment, which is
more sensible for numeric types. (Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6857">bpo-6857</a>.)</p>
<p>Comparisons involving a signaling NaN value (or <code class="docutils literal notranslate"><span class="pre">sNAN</span></code>) now signal
<code class="xref py py-const docutils literal notranslate"><span class="pre">InvalidOperation</span></code> instead of silently returning a true or
false value depending on the comparison operator. Quiet NaN values
(or <code class="docutils literal notranslate"><span class="pre">NaN</span></code>) are now hashable. (Fixed by Mark Dickinson;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7279">bpo-7279</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module now produces output that is more
compatible with modern <strong class="command">diff</strong>/<strong class="command">patch</strong> tools
through one small change, using a tab character instead of spaces as
a separator in the header giving the filename. (Fixed by Anatoly
Techtonik; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7585">bpo-7585</a>.)</p></li>
<li><p>The Distutils <code class="docutils literal notranslate"><span class="pre">sdist</span></code> command now always regenerates the
<code class="file docutils literal notranslate"><span class="pre">MANIFEST</span></code> file, since even if the <code class="file docutils literal notranslate"><span class="pre">MANIFEST.in</span></code> or
<code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> files haven't been modified, the user might have
created some new files that should be included.
(Fixed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8688">bpo-8688</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal notranslate"><span class="pre">doctest</span></code></a> module's <code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code> flag
will now ignore the name of the module containing the exception
being tested. (Patch by Lennart Regebro; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7490">bpo-7490</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> module's <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> class will
now accept a Unicode-valued payload, automatically converting the
payload to the encoding specified by <code class="xref py py-attr docutils literal notranslate"><span class="pre">output_charset</span></code>.
(Added by R. David Murray; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1368247">bpo-1368247</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">Fraction</span></code></a> class now accepts a single float or
<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> instance, or two rational numbers, as
arguments to its constructor. (Implemented by Mark Dickinson;
rationals added in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5812">bpo-5812</a>, and float/decimal in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8294">bpo-8294</a>.)</p>
<p>Ordering comparisons (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>) between
fractions and complex numbers now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.
This fixes an oversight, making the <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">Fraction</span></code></a>
match the other numeric types.</p>
</li>
<li><p>New class: <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> in
the <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code></a> module provides secure FTP
connections using TLS encapsulation of authentication as well as
subsequent control and data transfers.
(Contributed by Giampaolo Rodola; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2054">bpo-2054</a>.)</p>
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.storbinary" title="ftplib.FTP.storbinary"><code class="xref py py-meth docutils literal notranslate"><span class="pre">storbinary()</span></code></a> method for binary uploads can now restart
uploads thanks to an added <em>rest</em> parameter (patch by Pablo Mouzo;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6845">bpo-6845</a>.)</p>
</li>
<li><p>New class decorator: <a class="reference internal" href="../library/functools.html#functools.total_ordering" title="functools.total_ordering"><code class="xref py py-func docutils literal notranslate"><span class="pre">total_ordering()</span></code></a> in the <a class="reference internal" href="../library/functools.html#module-functools" title="functools: Higher-order functions and operations on callable objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">functools</span></code></a>
module takes a class that defines an <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__eq__()</span></code></a> method and one of
<a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__le__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__gt__" title="object.__gt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__gt__()</span></code></a>, or <a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__ge__()</span></code></a>,
and generates the missing comparison methods. Since the
<code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method is being deprecated in Python 3.x,
this decorator makes it easier to define ordered classes.
(Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5479">bpo-5479</a>.)</p>
<p>New function: <a class="reference internal" href="../library/functools.html#functools.cmp_to_key" title="functools.cmp_to_key"><code class="xref py py-func docutils literal notranslate"><span class="pre">cmp_to_key()</span></code></a> will take an old-style comparison
function that expects two arguments and return a new callable that
can be used as the <em>key</em> parameter to functions such as
<a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><span class="pre">sorted()</span></code></a>, <a class="reference internal" href="../library/functions.html#min" title="min"><code class="xref py py-func docutils literal notranslate"><span class="pre">min()</span></code></a> and <a class="reference internal" href="../library/functions.html#max" title="max"><code class="xref py py-func docutils literal notranslate"><span class="pre">max()</span></code></a>, etc. The primary
intended use is to help with making code compatible with Python 3.x.
(Added by Raymond Hettinger.)</p>
</li>
<li><p>New function: the <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code></a> module's <a class="reference internal" href="../library/gc.html#gc.is_tracked" title="gc.is_tracked"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_tracked()</span></code></a> returns
true if a given instance is tracked by the garbage collector, false
otherwise. (Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4688">bpo-4688</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> module's <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">GzipFile</span></code></a> now supports the context
management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">gzip.GzipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>
(contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3860">bpo-3860</a>), and it now implements
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> ABC, so you can wrap it with
<a class="reference internal" href="../library/io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedReader</span></code></a> for faster processing
(contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7471">bpo-7471</a>).
It's also now possible to override the modification time
recorded in a gzipped file by providing an optional timestamp to
the constructor. (Contributed by Jacques Frechet; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4272">bpo-4272</a>.)</p>
<p>Files in gzip format can be padded with trailing zero bytes; the
<a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> module will now consume these trailing bytes. (Fixed by
Tadek Pietraszek and Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2846">bpo-2846</a>.)</p>
</li>
<li><p>New attribute: the <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a> module now has an <code class="xref py py-attr docutils literal notranslate"><span class="pre">algorithms</span></code>
attribute containing a tuple naming the supported algorithms.
In Python 2.7, <code class="docutils literal notranslate"><span class="pre">hashlib.algorithms</span></code> contains
<code class="docutils literal notranslate"><span class="pre">('md5',</span> <span class="pre">'sha1',</span> <span class="pre">'sha224',</span> <span class="pre">'sha256',</span> <span class="pre">'sha384',</span> <span class="pre">'sha512')</span></code>.
(Contributed by Carl Chenet; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7418">bpo-7418</a>.)</p></li>
<li><p>The default <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code> class used by the <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> module now
supports buffering, resulting in much faster reading of HTTP responses.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4879">bpo-4879</a>.)</p>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code> classes
now support a <em>source_address</em> parameter, a <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
giving the source address that will be used for the connection.
(Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3972">bpo-3972</a>.)</p>
</li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> module now supports relative imports. Note that
<code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> is an older module for customizing imports,
superseded by the <code class="xref py py-mod docutils literal notranslate"><span class="pre">imputil</span></code> module added in Python 2.0.
(Relative import support added by Neil Schemenauer.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib</span></code></a> module now supports IPv6 addresses.
(Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1655">bpo-1655</a>.)</p></li>
<li><p>New function: the <a class="reference internal" href="../library/inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module's <a class="reference internal" href="../library/inspect.html#inspect.getcallargs" title="inspect.getcallargs"><code class="xref py py-func docutils literal notranslate"><span class="pre">getcallargs()</span></code></a>
takes a callable and its positional and keyword arguments,
and figures out which of the callable's parameters will receive each argument,
returning a dictionary mapping argument names to their values. For example:</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">inspect</span> <span class="kn">import</span> <span class="n">getcallargs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;pos&#39;: (3,), &#39;named&#39;: {}}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: 2, &#39;b&#39;: 1, &#39;pos&#39;: (), &#39;named&#39;: {&#39;x&#39;: 4}}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">f() takes at least 1 argument (0 given)</span>
</pre></div>
</div>
<p>由 Georg Sakkis 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3135">bpo-3135</a> 中贡献。</p>
</li>
<li><p>Updated module: The <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">io</span></code></a> library has been upgraded to the version shipped with
Python 3.1. For 3.1, the I/O library was entirely rewritten in C
and is 2 to 20 times faster depending on the task being performed. The
original Python version was renamed to the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_pyio</span></code> module.</p>
<p>One minor resulting change: the <a class="reference internal" href="../library/io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> class now
has an <code class="xref py py-attr docutils literal notranslate"><span class="pre">errors</span></code> attribute giving the error setting
used for encoding and decoding errors (one of <code class="docutils literal notranslate"><span class="pre">'strict'</span></code>, <code class="docutils literal notranslate"><span class="pre">'replace'</span></code>,
<code class="docutils literal notranslate"><span class="pre">'ignore'</span></code>).</p>
<p>The <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.FileIO</span></code></a> class now raises an <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> when passed
an invalid file descriptor. (Implemented by Benjamin Peterson;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4991">bpo-4991</a>.) The <a class="reference internal" href="../library/io.html#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code></a> method now preserves the
file position; previously it would change the file position to the
end of the new file. (Fixed by Pascal Chambon; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6939">bpo-6939</a>.)</p>
</li>
<li><p>New function: <code class="docutils literal notranslate"><span class="pre">itertools.compress(data,</span> <span class="pre">selectors)</span></code> takes two
iterators. Elements of <em>data</em> are returned if the corresponding
value in <em>selectors</em> is true:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">itertools</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="s1">&#39;ABCDEF&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span> <span class="o">=&gt;</span>
<span class="n">A</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">F</span>
</pre></div>
</div>
<p>New function: <code class="docutils literal notranslate"><span class="pre">itertools.combinations_with_replacement(iter,</span> <span class="pre">r)</span></code>
returns all the possible <em>r</em>-length combinations of elements from the
iterable <em>iter</em>. Unlike <a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><code class="xref py py-func docutils literal notranslate"><span class="pre">combinations()</span></code></a>, individual elements
can be repeated in the generated combinations:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations_with_replacement</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">=&gt;</span>
<span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">),</span>
<span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that elements are treated as unique depending on their position
in the input, not their actual values.</p>
<p>The <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.count()</span></code></a> function now has a <em>step</em> argument that
allows incrementing by values other than 1. <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal notranslate"><span class="pre">count()</span></code></a> also
now allows keyword arguments, and using non-integer values such as
floats or <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> instances. (Implemented by Raymond
Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5032">bpo-5032</a>.)</p>
<p><a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.combinations()</span></code></a> and <a class="reference internal" href="../library/itertools.html#itertools.product" title="itertools.product"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.product()</span></code></a>
previously raised <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> for values of <em>r</em> larger than
the input iterable. This was deemed a specification error, so they
now return an empty iterator. (Fixed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4816">bpo-4816</a>.)</p>
</li>
<li><p>Updated module: The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module was upgraded to version 2.0.9 of the
simplejson package, which includes a C extension that makes
encoding and decoding faster.
(Contributed by Bob Ippolito; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4136">bpo-4136</a>.)</p>
<p>To support the new <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a> type, <a class="reference internal" href="../library/json.html#json.load" title="json.load"><code class="xref py py-func docutils literal notranslate"><span class="pre">json.load()</span></code></a>
now has an optional <em>object_pairs_hook</em> parameter that will be called
with any object literal that decodes to a list of pairs.
(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5381">bpo-5381</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/mailbox.html#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code></a> module's <a class="reference internal" href="../library/mailbox.html#mailbox.Maildir" title="mailbox.Maildir"><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code></a> class now records the
timestamp on the directories it reads, and only re-reads them if the
modification time has subsequently changed. This improves
performance by avoiding unneeded directory scans. (Fixed by
A.M. Kuchling and Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1607951">bpo-1607951</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6896">bpo-6896</a>.)</p></li>
<li><p>New functions: 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 gained
<a class="reference internal" href="../library/math.html#math.erf" title="math.erf"><code class="xref py py-func docutils literal notranslate"><span class="pre">erf()</span></code></a> and <a class="reference internal" href="../library/math.html#math.erfc" title="math.erfc"><code class="xref py py-func docutils literal notranslate"><span class="pre">erfc()</span></code></a> for the error function and the complementary error function,
<a class="reference internal" href="../library/math.html#math.expm1" title="math.expm1"><code class="xref py py-func docutils literal notranslate"><span class="pre">expm1()</span></code></a> which computes <code class="docutils literal notranslate"><span class="pre">e**x</span> <span class="pre">-</span> <span class="pre">1</span></code> with more precision than
using <a class="reference internal" href="../library/math.html#math.exp" title="math.exp"><code class="xref py py-func docutils literal notranslate"><span class="pre">exp()</span></code></a> and subtracting 1,
<a class="reference internal" href="../library/math.html#math.gamma" title="math.gamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">gamma()</span></code></a> for the Gamma function, and
<a class="reference internal" href="../library/math.html#math.lgamma" title="math.lgamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgamma()</span></code></a> for the natural log of the Gamma function.
(Contributed by Mark Dickinson and nirinA raseliarison; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3366">bpo-3366</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module's <code class="xref py py-class docutils literal notranslate"><span class="pre">Manager*</span></code> classes
can now be passed a callable that will be called whenever
a subprocess is started, along with a set of arguments that will be
passed to the callable.
(Contributed by lekma; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5585">bpo-5585</a>.)</p>
<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code> class, which controls a pool of worker processes,
now has an optional <em>maxtasksperchild</em> parameter. Worker processes
will perform the specified number of tasks and then exit, causing the
<code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code> to start a new worker. This is useful if tasks may leak
memory or other resources, or if some tasks will cause the worker to
become very large.
(Contributed by Charles Cazabon; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6963">bpo-6963</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a> module now supports IPv6 addresses.
(Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1664">bpo-1664</a>.)</p></li>
<li><p>New functions: 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 wraps the following POSIX system
calls: <a class="reference internal" href="../library/os.html#os.getresgid" title="os.getresgid"><code class="xref py py-func docutils literal notranslate"><span class="pre">getresgid()</span></code></a> and <a class="reference internal" href="../library/os.html#os.getresuid" title="os.getresuid"><code class="xref py py-func docutils literal notranslate"><span class="pre">getresuid()</span></code></a>, which return the
real, effective, and saved GIDs and UIDs;
<a class="reference internal" href="../library/os.html#os.setresgid" title="os.setresgid"><code class="xref py py-func docutils literal notranslate"><span class="pre">setresgid()</span></code></a> and <a class="reference internal" href="../library/os.html#os.setresuid" title="os.setresuid"><code class="xref py py-func docutils literal notranslate"><span class="pre">setresuid()</span></code></a>, which set
real, effective, and saved GIDs and UIDs to new values;
<a class="reference internal" href="../library/os.html#os.initgroups" title="os.initgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">initgroups()</span></code></a>, which initialize the group access list
for the current process. (GID/UID functions
contributed by Travis H.; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6508">bpo-6508</a>. Support for initgroups added
by Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7333">bpo-7333</a>.)</p>
<p>The <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> function now re-initializes the import lock in
the child process; this fixes problems on Solaris when <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">fork()</span></code></a>
is called from a thread. (Fixed by Zsolt Cserna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7242">bpo-7242</a>.)</p>
</li>
<li><p>In the <a class="reference internal" href="../library/os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal notranslate"><span class="pre">os.path</span></code></a> module, the <a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-func docutils literal notranslate"><span class="pre">normpath()</span></code></a> and
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">abspath()</span></code></a> functions now preserve Unicode; if their input path
is a Unicode string, the return value is also a Unicode string.
(<a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-meth docutils literal notranslate"><span class="pre">normpath()</span></code></a> fixed by Matt Giuca in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5827">bpo-5827</a>;
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-meth docutils literal notranslate"><span class="pre">abspath()</span></code></a> fixed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3426">bpo-3426</a>.)</p></li>
<li><p>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 now has help for the various symbols that Python
uses. You can now do <code class="docutils literal notranslate"><span class="pre">help('&lt;&lt;')</span></code> or <code class="docutils literal notranslate"><span class="pre">help('&#64;')</span></code>, for example.
(Contributed by David Laban; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4739">bpo-4739</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module's <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><span class="pre">split()</span></code></a>, <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><code class="xref py py-func docutils literal notranslate"><span class="pre">sub()</span></code></a>, and <a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><code class="xref py py-func docutils literal notranslate"><span class="pre">subn()</span></code></a>
now accept an optional <em>flags</em> argument, for consistency with the
other functions in the module. (Added by Gregory P. Smith.)</p></li>
<li><p>New function: <a class="reference internal" href="../library/runpy.html#runpy.run_path" title="runpy.run_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">run_path()</span></code></a> in the <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> module
will execute the code at a provided <em>path</em> argument. <em>path</em> can be
the path of a Python source file (<code class="file docutils literal notranslate"><span class="pre">example.py</span></code>), a compiled
bytecode file (<code class="file docutils literal notranslate"><span class="pre">example.pyc</span></code>), a directory
(<code class="file docutils literal notranslate"><span class="pre">./package/</span></code>), or a zip archive (<code class="file docutils literal notranslate"><span class="pre">example.zip</span></code>). If a
directory or zip path is provided, it will be added to the front of
<code class="docutils literal notranslate"><span class="pre">sys.path</span></code> and the module <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> will be imported. It's
expected that the directory or zip contains a <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code>;
if it doesn't, some other <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code> might be imported from
a location later in <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. This makes more of the machinery
of <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal notranslate"><span class="pre">runpy</span></code></a> available to scripts that want to mimic the way
Python's command line processes an explicit path name.
(Added by Nick Coghlan; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6816">bpo-6816</a>.)</p></li>
<li><p>New function: in the <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a> module, <a class="reference internal" href="../library/shutil.html#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">make_archive()</span></code></a>
takes a filename, archive type (zip or tar-format), and a directory
path, and creates an archive containing the directory's contents.
(Added by Tarek Ziadé.)</p>
<p><a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal notranslate"><span class="pre">shutil</span></code></a>'s <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">copyfile()</span></code></a> and <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">copytree()</span></code></a>
functions now raise a <code class="xref py py-exc docutils literal notranslate"><span class="pre">SpecialFileError</span></code> exception when
asked to copy a named pipe. Previously the code would treat
named pipes like a regular file by opening them for reading, and
this would block indefinitely. (Fixed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3002">bpo-3002</a>.)</p>
</li>
<li><p>The <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> module no longer re-installs the signal handler
unless this is truly necessary, which fixes a bug that could make it
impossible to catch the EINTR signal robustly. (Fixed by
Charles-Francois Natali; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8354">bpo-8354</a>.)</p></li>
<li><p>New functions: in the <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> module, three new functions
return various site- and user-specific paths.
<a class="reference internal" href="../library/site.html#site.getsitepackages" title="site.getsitepackages"><code class="xref py py-func docutils literal notranslate"><span class="pre">getsitepackages()</span></code></a> returns a list containing all
global site-packages directories,
<a class="reference internal" href="../library/site.html#site.getusersitepackages" title="site.getusersitepackages"><code class="xref py py-func docutils literal notranslate"><span class="pre">getusersitepackages()</span></code></a> returns the path of the user's
site-packages directory, and
<a class="reference internal" href="../library/site.html#site.getuserbase" title="site.getuserbase"><code class="xref py py-func docutils literal notranslate"><span class="pre">getuserbase()</span></code></a> returns the value of the <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">USER_BASE</span></code>
environment variable, giving the path to a directory that can be used
to store data.
(Contributed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6693">bpo-6693</a>.)</p>
<p>The <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> module now reports exceptions occurring
when the <code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code> module is imported, and will no longer
catch and swallow the <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> exception. (Fixed by
Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3137">bpo-3137</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/socket.html#socket.create_connection" title="socket.create_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_connection()</span></code></a> function
gained a <em>source_address</em> parameter, a <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
giving the source address that will be used for the connection.
(Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3972">bpo-3972</a>.)</p>
<p>The <a class="reference internal" href="../library/socket.html#socket.socket.recv_into" title="socket.socket.recv_into"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv_into()</span></code></a> and <a class="reference internal" href="../library/socket.html#socket.socket.recvfrom_into" title="socket.socket.recvfrom_into"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recvfrom_into()</span></code></a>
methods will now write into objects that support the buffer API, most usefully
the <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> and <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> objects. (Implemented by
Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8104">bpo-8104</a>.)</p>
</li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">SocketServer</span></code> module's <code class="xref py py-class docutils literal notranslate"><span class="pre">TCPServer</span></code> class now
supports socket timeouts and disabling the Nagle algorithm.
The <code class="xref py py-attr docutils literal notranslate"><span class="pre">disable_nagle_algorithm</span></code> class attribute
defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>; if overridden to be true,
new request connections will have the TCP_NODELAY option set to
prevent buffering many small sends into a single TCP packet.
The <code class="xref py py-attr docutils literal notranslate"><span class="pre">timeout</span></code> class attribute can hold
a timeout in seconds that will be applied to the request socket; if
no request is received within that time, <code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_timeout()</span></code>
will be called and <code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_request()</span></code> will return.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6192">bpo-6192</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6267">bpo-6267</a>.)</p></li>
<li><p>Updated module: the <a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> module has been updated to
version 2.6.0 of the <a class="reference external" href="https://github.com/ghaering/pysqlite">pysqlite package</a>. Version 2.6.0 includes a number of bugfixes, and adds
the ability to load SQLite extensions from shared libraries.
Call the <code class="docutils literal notranslate"><span class="pre">enable_load_extension(True)</span></code> method to enable extensions,
and then call <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.load_extension" title="sqlite3.Connection.load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">load_extension()</span></code></a> to load a particular shared library.
(Updated by Gerhard Häring.)</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's <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> objects now support the
buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7133">bpo-7133</a>) and automatically set
OpenSSL's <code class="xref c c-macro docutils literal notranslate"><span class="pre">SSL_MODE_AUTO_RETRY</span></code>, which will prevent an error
code being returned from <code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code> operations that trigger an SSL
renegotiation (fix by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8222">bpo-8222</a>).</p>
<p>The <a class="reference internal" href="../library/ssl.html#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.wrap_socket()</span></code></a> constructor function now takes a
<em>ciphers</em> argument that's a string listing the encryption algorithms
to be allowed; the format of the string is described
<a class="reference external" href="https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-LIST-FORMAT">in the OpenSSL documentation</a>.
(Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8322">bpo-8322</a>.)</p>
<p>Another change makes the extension load all of OpenSSL's ciphers and
digest algorithms so that they're all available. Some SSL
certificates couldn't be verified, reporting an &quot;unknown algorithm&quot;
error. (Reported by Beda Kosata, and fixed by Antoine Pitrou;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8484">bpo-8484</a>.)</p>
<p>The version of OpenSSL being used is now available as the module
attributes <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION" title="ssl.OPENSSL_VERSION"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION</span></code></a> (a string),
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_INFO" title="ssl.OPENSSL_VERSION_INFO"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION_INFO</span></code></a> (a 5-tuple), and
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_NUMBER" title="ssl.OPENSSL_VERSION_NUMBER"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION_NUMBER</span></code></a> (an integer). (Added by Antoine
Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8321">bpo-8321</a>.)</p>
</li>
<li><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 will no longer silently ignore overflow
errors when a value is too large for a particular integer format
code (one of <code class="docutils literal notranslate"><span class="pre">bBhHiIlLqQ</span></code>); it now always raises a
<a class="reference internal" href="../library/struct.html#struct.error" title="struct.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">struct.error</span></code></a> exception. (Changed by Mark Dickinson;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1523">bpo-1523</a>.) The <a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack()</span></code></a> function will also
attempt to use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> to convert and pack non-integers
before trying the <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a> method or reporting an error.
(Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8300">bpo-8300</a>.)</p></li>
<li><p>New function: 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's
<a class="reference internal" href="../library/subprocess.html#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> runs a command with a specified set of arguments
and returns the command's output as a string when the command runs without
error, or raises a <a class="reference internal" href="../library/subprocess.html#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">CalledProcessError</span></code></a> exception otherwise.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;df&#39;</span><span class="p">,</span> <span class="s1">&#39;-h&#39;</span><span class="p">,</span> <span class="s1">&#39;.&#39;</span><span class="p">])</span>
<span class="go">&#39;Filesystem Size Used Avail Capacity Mounted on\n</span>
<span class="go">/dev/disk0s2 52G 49G 3.0G 94% /\n&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s1">&#39;df&#39;</span><span class="p">,</span> <span class="s1">&#39;-h&#39;</span><span class="p">,</span> <span class="s1">&#39;/bogus&#39;</span><span class="p">])</span>
<span class="go"> ...</span>
<span class="go">subprocess.CalledProcessError: Command &#39;[&#39;df&#39;, &#39;-h&#39;, &#39;/bogus&#39;]&#39; returned non-zero exit status 1</span>
</pre></div>
</div>
<p>(由 Gregory P. Smith 贡献)</p>
<p>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 will now retry its internal system calls
on receiving an <code class="xref py py-const docutils literal notranslate"><span class="pre">EINTR</span></code> signal. (Reported by several people; final
patch by Gregory P. Smith in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1068268">bpo-1068268</a>.)</p>
</li>
<li><p>New function: <a class="reference internal" href="../library/symtable.html#symtable.Symbol.is_declared_global" title="symtable.Symbol.is_declared_global"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_declared_global()</span></code></a> in the <a class="reference internal" href="../library/symtable.html#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><code class="xref py py-mod docutils literal notranslate"><span class="pre">symtable</span></code></a> module
returns true for variables that are explicitly declared to be global,
false for ones that are implicitly global.
(Contributed by Jeremy Hylton.)</p></li>
<li><p>The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> as the
identifier instead of the previous default value of <code class="docutils literal notranslate"><span class="pre">'python'</span></code>.
(Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8451">bpo-8451</a>.)</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">sys.version_info</span></code> value is now a named tuple, with attributes
named <code class="xref py py-attr docutils literal notranslate"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">micro</span></code>,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">releaselevel</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">serial</span></code>. (Contributed by Ross
Light; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4285">bpo-4285</a>.)</p>
<p><a class="reference internal" href="../library/sys.html#sys.getwindowsversion" title="sys.getwindowsversion"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getwindowsversion()</span></code></a> also returns a named tuple,
with attributes named <code class="xref py py-attr docutils literal notranslate"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">build</span></code>,
<a class="reference internal" href="../library/platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code class="xref py py-attr docutils literal notranslate"><span class="pre">platform</span></code></a>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack_major</span></code>,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack_minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">suite_mask</span></code>, and
<code class="xref py py-attr docutils literal notranslate"><span class="pre">product_type</span></code>. (Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7766">bpo-7766</a>.)</p>
</li>
<li><p>The <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> module's default error handling has changed, to
no longer suppress fatal errors. The default error level was previously 0,
which meant that errors would only result in a message being written to the
debug log, but because the debug log is not activated by default,
these errors go unnoticed. The default error level is now 1,
which raises an exception if there's an error.
(Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7357">bpo-7357</a>.)</p>
<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 filtering the <a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a>
objects being added to a tar file. When you call <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.add" title="tarfile.TarFile.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a>,
you may supply an optional <em>filter</em> argument
that's a callable. The <em>filter</em> callable will be passed the
<a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> for every file being added, and can modify and return it.
If the callable returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, the file will be excluded from the
resulting archive. This is more powerful than the existing
<em>exclude</em> argument, which has therefore been deprecated.
(Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6856">bpo-6856</a>.)
The <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> class also now supports the context management protocol.
(Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7232">bpo-7232</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> method of the <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> class
now returns the internal flag on exit. This means the method will usually
return true because <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wait()</span></code></a> is supposed to block until the
internal flag becomes true. The return value will only be false if
a timeout was provided and the operation timed out.
(Contributed by Tim Lesher; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1674032">bpo-1674032</a>.)</p></li>
<li><p>The Unicode database provided by the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> module is
now used internally to determine which characters are numeric,
whitespace, or represent line breaks. The database also
includes information from the <code class="file docutils literal notranslate"><span class="pre">Unihan.txt</span></code> data file (patch
by Anders Chrigström and Amaury Forgeot d'Arc; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1571184">bpo-1571184</a>)
and has been updated to version 5.2.0 (updated by
Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8024">bpo-8024</a>).</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code> module's <code class="xref py py-func docutils literal notranslate"><span class="pre">urlsplit()</span></code> now handles
unknown URL schemes in a fashion compliant with <span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
URL is of the form <code class="docutils literal notranslate"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
<code class="docutils literal notranslate"><span class="pre">://</span></code> is treated as the scheme, even if it's a made-up scheme that
the module doesn't know about. This change may break code that
worked around the old behaviour. For example, Python 2.6.4 or 2.5
will return the following:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>Python 2.7 (and Python 2.6.5) will return:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)</p>
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code> module also supports IPv6 literal addresses as defined by
<span class="target" id="index-11"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a> (contributed by Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2987">bpo-2987</a>).</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s1">&#39;http://[1080::8:800:200C:417A]/foo&#39;</span><span class="p">)</span>
<span class="go">ParseResult(scheme=&#39;http&#39;, netloc=&#39;[1080::8:800:200C:417A]&#39;,</span>
<span class="go"> path=&#39;/foo&#39;, params=&#39;&#39;, query=&#39;&#39;, fragment=&#39;&#39;)</span>
</pre></div>
</div>
</li>
<li><p>New class: the <a class="reference internal" href="../library/weakref.html#weakref.WeakSet" title="weakref.WeakSet"><code class="xref py py-class docutils literal notranslate"><span class="pre">WeakSet</span></code></a> class in the <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code></a>
module is a set that only holds weak references to its elements; elements
will be removed once there are no references pointing to them.
(Originally implemented in Python 3.x by Raymond Hettinger, and backported
to 2.7 by Michael Foord.)</p></li>
<li><p>The ElementTree library, <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree</span></code>, no longer escapes
ampersands and angle brackets when outputting an XML processing
instruction (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;?xml-stylesheet</span> <span class="pre">href=&quot;#style1&quot;?&gt;</span></code>)
or comment (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;!--</span> <span class="pre">comment</span> <span class="pre">--&gt;</span></code>).
(Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2746">bpo-2746</a>.)</p></li>
<li><p>The XML-RPC client and server, provided by the <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code> and
<code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleXMLRPCServer</span></code> modules, have improved performance by
supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
to compress the XML being exchanged. The gzip compression is
controlled by the <code class="xref py py-attr docutils literal notranslate"><span class="pre">encode_threshold</span></code> attribute of
<code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleXMLRPCRequestHandler</span></code>, which contains a size in bytes;
responses larger than this will be compressed.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6267">bpo-6267</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> module's <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> now supports the context
management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">zipfile.ZipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
(Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5511">bpo-5511</a>.)</p>
<p><a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code></a> now also supports archiving empty directories and
extracts them correctly. (Fixed by Kuba Wieczorek; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4710">bpo-4710</a>.)
Reading files out of an archive is faster, and interleaving
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code> now works correctly.
(Contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7610">bpo-7610</a>.)</p>
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.is_zipfile" title="zipfile.is_zipfile"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_zipfile()</span></code></a> function now
accepts a file object, in addition to the path names accepted in earlier
versions. (Contributed by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4756">bpo-4756</a>.)</p>
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writestr()</span></code></a> method now has an optional <em>compress_type</em> parameter
that lets you override the default compression method specified in the
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> constructor. (Contributed by Ronald Oussoren;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6003">bpo-6003</a>.)</p>
</li>
</ul>
<section id="new-module-importlib">
<span id="importlib-section"></span><h3>新模块importlib<a class="headerlink" href="#new-module-importlib" title="永久链接至标题"></a></h3>
<p>Python 3.1 includes 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, a re-implementation
of the logic underlying Python's <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.
<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> is useful for implementors of Python interpreters and
to users who wish to write new importers that can participate in the
import process. Python 2.7 doesn't contain the complete
<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, but instead has a tiny subset that contains
a single function, <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">import_module()</span></code></a>.</p>
<p><code class="docutils literal notranslate"><span class="pre">import_module(name,</span> <span class="pre">package=None)</span></code> imports a module. <em>name</em> is
a string containing the module or package's name. It's possible to do
relative imports by providing a string that begins with a <code class="docutils literal notranslate"><span class="pre">.</span></code>
character, such as <code class="docutils literal notranslate"><span class="pre">..utils.errors</span></code>. For relative imports, the
<em>package</em> argument must be provided and is the name of the package that
will be used as the anchor for
the relative import. <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">import_module()</span></code></a> both inserts the imported
module into <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> and returns the module object.</p>
<p>这是一些例子:</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">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s1">&#39;anydbm&#39;</span><span class="p">)</span> <span class="c1"># Standard absolute import</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span>
<span class="go">&lt;module &#39;anydbm&#39; from &#39;/p/python/Lib/anydbm.py&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Relative import</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s1">&#39;..file_util&#39;</span><span class="p">,</span> <span class="s1">&#39;distutils.command&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span>
<span class="go">&lt;module &#39;distutils.file_util&#39; from &#39;/python/Lib/distutils/file_util.pyc&#39;&gt;</span>
</pre></div>
</div>
<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> was implemented by Brett Cannon and introduced in
Python 3.1.</p>
</section>
<section id="new-module-sysconfig">
<h3>新模块sysconfig<a class="headerlink" href="#new-module-sysconfig" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a> module has been pulled out of the Distutils
package, becoming a new top-level module in its own right.
<a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a> provides functions for getting information about
Python's build process: compiler switches, installation paths, the
platform name, and whether Python is running from its source
directory.</p>
<p>该模块中的部分函数:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_var" title="sysconfig.get_config_var"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_config_var()</span></code></a> 返回来自 Python 的 Makefile 和 <code class="file docutils literal notranslate"><span class="pre">pyconfig.h</span></code> 文件的变量。</p></li>
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_vars" title="sysconfig.get_config_vars"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_config_vars()</span></code></a> 返回一个包含所有配置变量的字典。</p></li>
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_path" title="sysconfig.get_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_path()</span></code></a> 返回特定模块类型的配置路径:标准库、站点专属模块、平台专属模块等等。</p></li>
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.is_python_build" title="sysconfig.is_python_build"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_python_build()</span></code></a> 会在你从 Python 源码树运行二进制可执行文件时返回真值,而在其他情况下返回假值。</p></li>
</ul>
<p>Consult the <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a> documentation for more details and for
a complete list of functions.</p>
<p>The Distutils package and <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a> are now maintained by Tarek
Ziadé, who has also started a Distutils2 package (source repository at
<a class="reference external" href="https://hg.python.org/distutils2/">https://hg.python.org/distutils2/</a>) for developing a next-generation
version of Distutils.</p>
</section>
<section id="ttk-themed-widgets-for-tk">
<h3>ttkTk 主题组件<a class="headerlink" href="#ttk-themed-widgets-for-tk" title="永久链接至标题"></a></h3>
<p>Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
widgets but have a more customizable appearance and can therefore more
closely resemble the native platform's widgets. This widget
set was originally called Tile, but was renamed to Ttk (for &quot;themed Tk&quot;)
on being added to Tcl/Tck release 8.5.</p>
<p>To learn more, read the <code class="xref py py-mod docutils literal notranslate"><span class="pre">ttk</span></code> module documentation. You may also
wish to read the Tcl/Tk manual page describing the
Ttk theme engine, available at
<a class="reference external" href="https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm">https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm</a>. Some
screenshots of the Python/Ttk code in use are at
<a class="reference external" href="https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki">https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki</a>.</p>
<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ttk</span></code> module was written by Guilherme Polo and added in
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2983">bpo-2983</a>. An alternate version called <code class="docutils literal notranslate"><span class="pre">Tile.py</span></code>, written by
Martin Franklin and maintained by Kevin Walzer, was proposed for
inclusion in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2618">bpo-2618</a>, but the authors argued that Guilherme
Polo's work was more comprehensive.</p>
</section>
<section id="updated-module-unittest">
<span id="unittest-section"></span><h3>更新的模块unittest<a class="headerlink" href="#updated-module-unittest" title="永久链接至标题"></a></h3>
<p>The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module was greatly enhanced; many
new features were added. Most of these features were implemented
by Michael Foord, unless otherwise noted. The enhanced version of
the module is downloadable separately for use with Python versions 2.4 to 2.6,
packaged as the <code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest2</span></code> package, from
<a class="reference external" href="https://pypi.org/project/unittest2">https://pypi.org/project/unittest2</a>.</p>
<p>When used from the command line, the module can automatically discover
tests. It's not as fancy as <a class="reference external" href="http://pytest.org">py.test</a> or
<a class="reference external" href="https://nose.readthedocs.io/">nose</a>, but provides a
simple way to run tests kept within a set of package directories. For example,
the following command will search the <code class="file docutils literal notranslate"><span class="pre">test/</span></code> subdirectory for
any importable test files named <code class="docutils literal notranslate"><span class="pre">test*.py</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span> <span class="o">-</span><span class="n">s</span> <span class="n">test</span>
</pre></div>
</div>
<p>Consult the <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a> module documentation for more details.
(Developed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6001">bpo-6001</a>.)</p>
<p>The <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> function supports some other new options:</p>
<ul>
<li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-b"><code class="xref std std-option docutils literal notranslate"><span class="pre">-b</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--buffer</span></code> will buffer the standard output
and standard error streams during each test. If the test passes,
any resulting output will be discarded; on failure, the buffered
output will be displayed.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--catch</span></code> will cause the control-C interrupt
to be handled more gracefully. Instead of interrupting the test
process immediately, the currently running test will be completed
and then the partial results up to the interruption will be reported.
If you're impatient, a second press of control-C will cause an immediate
interruption.</p>
<p>This control-C handler tries to avoid causing problems when the code
being tested or the tests being run have defined a signal handler of
their own, by noticing that a signal handler was already set and
calling it. If this doesn't work for you, there's a
<a class="reference internal" href="../library/unittest.html#unittest.removeHandler" title="unittest.removeHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">removeHandler()</span></code></a> decorator that can be used to mark tests that
should have the control-C handling disabled.</p>
</li>
<li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-f"><code class="xref std std-option docutils literal notranslate"><span class="pre">-f</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--failfast</span></code> makes
test execution stop immediately when a test fails instead of
continuing to execute further tests. (Suggested by Cliff Dyer and
implemented by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8074">bpo-8074</a>.)</p></li>
</ul>
<p>The progress messages now show 'x' for expected failures
and 'u' for unexpected successes when run in verbose mode.
(Contributed by Benjamin Peterson.)</p>
<p>Test cases can raise the <a class="reference internal" href="../library/unittest.html#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SkipTest</span></code></a> exception to skip a
test (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1034053">bpo-1034053</a>).</p>
<p>The error messages for <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>,
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTrue()</span></code></a>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertFalse()</span></code></a>
failures now provide more information. If you set the
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.longMessage" title="unittest.TestCase.longMessage"><code class="xref py py-attr docutils literal notranslate"><span class="pre">longMessage</span></code></a> attribute of your <a class="reference internal" href="../library/unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestCase</span></code></a> classes to
true, both the standard error message and any additional message you
provide will be printed for failures. (Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5663">bpo-5663</a>.)</p>
<p>The <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> method now
returns a context handler when called without providing a callable
object to run. For example, you can write this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">KeyError</span><span class="p">):</span>
<span class="p">{}[</span><span class="s1">&#39;foo&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>(Implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4444">bpo-4444</a>.)</p>
<p>Module- and class-level setup and teardown fixtures are now supported.
Modules can contain <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code>
functions. Classes can have <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUpClass()</span></code></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> methods that must be defined as class methods
(using <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> or equivalent). These functions and
methods are invoked when the test runner switches to a test case in a
different module or class.</p>
<p>The methods <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doCleanups()</span></code></a> were added.
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a> lets you add cleanup functions that
will be called unconditionally (after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> if
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><span class="pre">setUp()</span></code></a> fails, otherwise after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDown()</span></code></a>). This allows
for much simpler resource allocation and deallocation during tests
(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5679">bpo-5679</a>).</p>
<p>A number of new methods were added that provide more specialized
tests. Many of these methods were written by Google engineers
for use in their test suites; Gregory P. Smith, Michael Foord, and
GvR worked on merging them into Python's version of <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest</span></code></a>.</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNone" title="unittest.TestCase.assertIsNone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNone()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNotNone" title="unittest.TestCase.assertIsNotNone"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNotNone()</span></code></a> take one
expression and verify that the result is or is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIs()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNot" title="unittest.TestCase.assertIsNot"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsNot()</span></code></a>
take two values and check whether the two values evaluate to the same object or not.
(Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2578">bpo-2578</a>.)</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsInstance" title="unittest.TestCase.assertIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsInstance()</span></code></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIsInstance" title="unittest.TestCase.assertNotIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotIsInstance()</span></code></a> check whether
the resulting object is an instance of a particular class, or of
one of a tuple of classes. (Added by Georg Brandl; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7031">bpo-7031</a>.)</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreater" title="unittest.TestCase.assertGreater"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertGreater()</span></code></a>, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreaterEqual" title="unittest.TestCase.assertGreaterEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertGreaterEqual()</span></code></a>,
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLess" title="unittest.TestCase.assertLess"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLess()</span></code></a>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLessEqual" title="unittest.TestCase.assertLessEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertLessEqual()</span></code></a> compare
two quantities.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertMultiLineEqual()</span></code></a> compares two strings, and if they're
not equal, displays a helpful comparison that highlights the
differences in the two strings. This comparison is now used by
default when Unicode strings are compared with <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.</p></li>
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code> and
<code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotRegexpMatches()</span></code> checks whether the
first argument is a string matching or not matching the regular
expression provided as the second argument (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8038">bpo-8038</a>).</p></li>
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegexp()</span></code> checks whether a particular exception
is raised, and then also checks that the string representation of
the exception matches the provided regular expression.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIn" title="unittest.TestCase.assertIn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIn()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIn" title="unittest.TestCase.assertNotIn"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotIn()</span></code></a>
tests whether <em>first</em> is or is not in <em>second</em>.</p></li>
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertItemsEqual()</span></code> tests whether two provided sequences
contain the same elements.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSetEqual" title="unittest.TestCase.assertSetEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSetEqual()</span></code></a> compares whether two sets are equal, and
only reports the differences between the sets in case of error.</p></li>
<li><p>Similarly, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertListEqual()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTupleEqual" title="unittest.TestCase.assertTupleEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertTupleEqual()</span></code></a>
compare the specified types and explain any differences without necessarily
printing their full values; these methods are now used by default
when comparing lists and tuples using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>.
More generally, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSequenceEqual" title="unittest.TestCase.assertSequenceEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSequenceEqual()</span></code></a> compares two sequences
and can optionally check whether both sequences are of a
particular type.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictEqual()</span></code></a> compares two dictionaries and reports the
differences; it's now used by default when you compare two dictionaries
using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>. <code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictContainsSubset()</span></code> checks whether
all of the key/value pairs in <em>first</em> are found in <em>second</em>.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEqual()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEqual()</span></code></a> test
whether <em>first</em> and <em>second</em> are approximately equal. This method
can either round their difference to an optionally-specified number
of <em>places</em> (the default is 7) and compare it to zero, or require
the difference to be smaller than a supplied <em>delta</em> value.</p></li>
<li><p><a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromName" title="unittest.TestLoader.loadTestsFromName"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loadTestsFromName()</span></code></a> properly honors the
<a class="reference internal" href="../library/unittest.html#unittest.TestLoader.suiteClass" title="unittest.TestLoader.suiteClass"><code class="xref py py-attr docutils literal notranslate"><span class="pre">suiteClass</span></code></a> attribute of
the <a class="reference internal" href="../library/unittest.html#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a>. (Fixed by Mark Roddy; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6866">bpo-6866</a>.)</p></li>
<li><p>A new hook lets you extend the <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a> method to handle
new data types. The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addTypeEqualityFunc" title="unittest.TestCase.addTypeEqualityFunc"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addTypeEqualityFunc()</span></code></a> method takes a type
object and a function. The function will be used when both of the
objects being compared are of the specified type. This function
should compare the two objects and raise an exception if they don't
match; it's a good idea for the function to provide additional
information about why the two objects aren't matching, much as the new
sequence comparison methods do.</p></li>
</ul>
<p><a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a> now takes an optional <code class="docutils literal notranslate"><span class="pre">exit</span></code> argument. If
false, <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> doesn't call <a class="reference internal" href="../library/sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>, allowing
<a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> to be used from the interactive interpreter.
(Contributed by J. Pablo Fernández; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3379">bpo-3379</a>.)</p>
<p><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> has new <a class="reference internal" href="../library/unittest.html#unittest.TestResult.startTestRun" title="unittest.TestResult.startTestRun"><code class="xref py py-meth docutils literal notranslate"><span class="pre">startTestRun()</span></code></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestResult.stopTestRun" title="unittest.TestResult.stopTestRun"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stopTestRun()</span></code></a> methods that are called immediately before
and after a test run. (Contributed by Robert Collins; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5728">bpo-5728</a>.)</p>
<p>With all these changes, the <code class="file docutils literal notranslate"><span class="pre">unittest.py</span></code> was becoming awkwardly
large, so the module was turned into a package and the code split into
several files (by Benjamin Peterson). This doesn't affect how the
module is imported or used.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference external" href="http://www.voidspace.org.uk/python/articles/unittest2.shtml">http://www.voidspace.org.uk/python/articles/unittest2.shtml</a></dt><dd><p>Describes the new features, how to use them, and the
rationale for various design decisions. (By Michael Foord.)</p>
</dd>
</dl>
</div>
</section>
<section id="updated-module-elementtree-1-3">
<span id="elementtree-section"></span><h3>更新的模块ElementTree 1.3<a class="headerlink" href="#updated-module-elementtree-1-3" title="永久链接至标题"></a></h3>
<p>The version of the ElementTree library included with Python was updated to
version 1.3. Some of the new features are:</p>
<ul>
<li><p>The various parsing functions now take a <em>parser</em> keyword argument
giving an <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">XMLParser</span></code></a> instance that will
be used. This makes it possible to override the file's internal encoding:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">p</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;root/&gt;&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">parser</span><span class="o">=</span><span class="n">p</span><span class="p">)</span>
</pre></div>
</div>
<p>Errors in parsing XML now raise a <code class="xref py py-exc docutils literal notranslate"><span class="pre">ParseError</span></code> exception, whose
instances have a <code class="xref py py-attr docutils literal notranslate"><span class="pre">position</span></code> attribute
containing a (<em>line</em>, <em>column</em>) tuple giving the location of the problem.</p>
</li>
<li><p>ElementTree's code for converting trees to a string has been
significantly reworked, making it roughly twice as fast in many
cases. The <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ElementTree.write()</span></code></a>
and <code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.write()</span></code> methods now have a <em>method</em> parameter that can be
&quot;xml&quot; (the default), &quot;html&quot;, or &quot;text&quot;. HTML mode will output empty
elements as <code class="docutils literal notranslate"><span class="pre">&lt;empty&gt;&lt;/empty&gt;</span></code> instead of <code class="docutils literal notranslate"><span class="pre">&lt;empty/&gt;</span></code>, and text
mode will skip over elements and only output the text chunks. If
you set the <code class="xref py py-attr docutils literal notranslate"><span class="pre">tag</span></code> attribute of an element to <code class="docutils literal notranslate"><span class="pre">None</span></code> but
leave its children in place, the element will be omitted when the
tree is written out, so you don't need to do more extensive rearrangement
to remove a single element.</p>
<p>Namespace handling has also been improved. All <code class="docutils literal notranslate"><span class="pre">xmlns:&lt;whatever&gt;</span></code>
declarations are now output on the root element, not scattered throughout
the resulting XML. You can set the default namespace for a tree
by setting the <code class="xref py py-attr docutils literal notranslate"><span class="pre">default_namespace</span></code> attribute and can
register new prefixes with <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.register_namespace" title="xml.etree.ElementTree.register_namespace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">register_namespace()</span></code></a>. In XML mode,
you can use the true/false <em>xml_declaration</em> parameter to suppress the
XML declaration.</p>
</li>
<li><p>New <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> method:
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.extend" title="xml.etree.ElementTree.Element.extend"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extend()</span></code></a> appends the items from a
sequence to the element's children. Elements themselves behave like
sequences, so it's easy to move children from one element to
another:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.etree</span> <span class="kn">import</span> <span class="n">ElementTree</span> <span class="k">as</span> <span class="n">ET</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;list&gt;</span>
<span class="s2"> &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;3&lt;/item&gt;</span>
<span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
<span class="n">new</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s1">&#39;&lt;root/&gt;&#39;</span><span class="p">)</span>
<span class="n">new</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="c1"># Outputs &lt;root&gt;&lt;item&gt;1&lt;/item&gt;...&lt;/root&gt;</span>
<span class="nb">print</span> <span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p>New <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> method:
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal notranslate"><span class="pre">iter()</span></code></a> yields the children of the
element as a generator. It's also possible to write <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">child</span> <span class="pre">in</span>
<span class="pre">elem:</span></code> to loop over an element's children. The existing method
<code class="xref py py-meth docutils literal notranslate"><span class="pre">getiterator()</span></code> is now deprecated, as is <code class="xref py py-meth docutils literal notranslate"><span class="pre">getchildren()</span></code>
which constructs and returns a list of children.</p></li>
<li><p>New <code class="xref py py-class docutils literal notranslate"><span class="pre">Element</span></code> method:
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><code class="xref py py-meth docutils literal notranslate"><span class="pre">itertext()</span></code></a> yields all chunks of
text that are descendants of the element. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;list&gt;</span>
<span class="s2"> &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt; &lt;item&gt;3&lt;/item&gt;</span>
<span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
<span class="c1"># Outputs [&#39;\n &#39;, &#39;1&#39;, &#39; &#39;, &#39;2&#39;, &#39; &#39;, &#39;3&#39;, &#39;\n&#39;]</span>
<span class="nb">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">itertext</span><span class="p">())</span>
</pre></div>
</div>
</li>
<li><p>Deprecated: using an element as a Boolean (i.e., <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">elem:</span></code>) would
return true if the element had any children, or false if there were
no children. This behaviour is confusing -- <code class="docutils literal notranslate"><span class="pre">None</span></code> is false, but
so is a childless element? -- so it will now trigger a
<a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a>. In your code, you should be explicit: write
<code class="docutils literal notranslate"><span class="pre">len(elem)</span> <span class="pre">!=</span> <span class="pre">0</span></code> if you're interested in the number of children,
or <code class="docutils literal notranslate"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></code>.</p></li>
</ul>
<p>Fredrik Lundh develops ElementTree and produced the 1.3 version;
you can read his article describing 1.3 at
<a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">http://effbot.org/zone/elementtree-13-intro.htm</a>.
Florent Xicluna updated the version included with
Python, after discussions on python-dev and in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6472">bpo-6472</a>.)</p>
</section>
</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>
<li><p>The latest release of the GNU Debugger, GDB 7, can be <a class="reference external" href="https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html">scripted
using Python</a>.
When you begin debugging an executable program P, GDB will look for
a file named <code class="docutils literal notranslate"><span class="pre">P-gdb.py</span></code> and automatically read it. Dave Malcolm
contributed a <code class="file docutils literal notranslate"><span class="pre">python-gdb.py</span></code> that adds a number of
commands useful when debugging Python itself. For example,
<code class="docutils literal notranslate"><span class="pre">py-up</span></code> and <code class="docutils literal notranslate"><span class="pre">py-down</span></code> go up or down one Python stack frame,
which usually corresponds to several C stack frames. <code class="docutils literal notranslate"><span class="pre">py-print</span></code>
prints the value of a Python variable, and <code class="docutils literal notranslate"><span class="pre">py-bt</span></code> prints the
Python stack trace. (Added as a result of <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8032">bpo-8032</a>.)</p></li>
<li><p>If you use the <code class="file docutils literal notranslate"><span class="pre">.gdbinit</span></code> file provided with Python,
the &quot;pyo&quot; macro in the 2.7 version now works correctly when the thread being
debugged doesn't hold the GIL; the macro now acquires it before printing.
(Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3632">bpo-3632</a>.)</p></li>
<li><p><a class="reference internal" href="../c-api/init.html#c.Py_AddPendingCall" title="Py_AddPendingCall"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_AddPendingCall()</span></code></a> is now thread-safe, letting any
worker thread submit notifications to the main Python thread. This
is particularly useful for asynchronous IO operations.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4293">bpo-4293</a>.)</p></li>
<li><p>New function: <a class="reference internal" href="../c-api/code.html#c.PyCode_NewEmpty" title="PyCode_NewEmpty"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_NewEmpty()</span></code></a> creates an empty code object;
only the filename, function name, and first line number are required.
This is useful for extension modules that are attempting to
construct a more useful traceback stack. Previously such
extensions needed to call <a class="reference internal" href="../c-api/code.html#c.PyCode_New" title="PyCode_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_New()</span></code></a>, which had many
more arguments. (Added by Jeffrey Yasskin.)</p></li>
<li><p>New function: <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_NewExceptionWithDoc" title="PyErr_NewExceptionWithDoc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_NewExceptionWithDoc()</span></code></a> creates a new
exception class, just as the existing <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_NewException" title="PyErr_NewException"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_NewException()</span></code></a> does,
but takes an extra <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code> argument containing the docstring for the
new exception class. (Added by 'lekma' on the Python bug tracker;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7033">bpo-7033</a>.)</p></li>
<li><p>New function: <a class="reference internal" href="../c-api/reflection.html#c.PyFrame_GetLineNumber" title="PyFrame_GetLineNumber"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFrame_GetLineNumber()</span></code></a> takes a frame object
and returns the line number that the frame is currently executing.
Previously code would need to get the index of the bytecode
instruction currently executing, and then look up the line number
corresponding to that address. (Added by Jeffrey Yasskin.)</p></li>
<li><p>New functions: <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> and
<a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongLongAndOverflow" title="PyLong_AsLongLongAndOverflow"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLongLongAndOverflow()</span></code></a> approximates a Python long
integer as a C <code class="xref c c-type docutils literal notranslate"><span class="pre">long</span></code> or <code class="xref c c-type docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code>.
If the number is too large to fit into
the output type, an <em>overflow</em> flag is set and returned to the caller.
(Contributed by Case Van Horsen; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7528">bpo-7528</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7767">bpo-7767</a>.)</p></li>
<li><p>New function: stemming from the rewrite of string-to-float conversion,
a new <a class="reference internal" href="../c-api/conversion.html#c.PyOS_string_to_double" title="PyOS_string_to_double"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_string_to_double()</span></code></a> function was added. The old
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_atof()</span></code> functions
are now deprecated.</p></li>
<li><p>New function: <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> sets the value of
<code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> and can optionally update <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> to include the
directory containing the script named by <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> depending
on the value of an <em>updatepath</em> parameter.</p>
<p>This function was added to close a security hole for applications
that embed Python. The old function, <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a>, would
always update <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, and sometimes it would add the current
directory. This meant that, if you ran an application embedding
Python in a directory controlled by someone else, attackers could
put a Trojan-horse module in the directory (say, a file named
<code class="file docutils literal notranslate"><span class="pre">os.py</span></code>) that your application would then import and run.</p>
<p>If you maintain a C/C++ application that embeds Python, check
whether you're calling <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider
whether the application should be using <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a>
with <em>updatepath</em> set to false.</p>
<p>Security issue reported as <a class="reference external" href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983">CVE-2008-5983</a>;
discussed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5753">bpo-5753</a>, and fixed by Antoine Pitrou.</p>
</li>
<li><p>New macros: the Python header files now define the following macros:
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISALNUM</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISALPHA</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISDIGIT</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISLOWER</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISSPACE</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISUPPER</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISXDIGIT</span></code>,
<code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOLOWER</span></code>, and <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOUPPER</span></code>.
All of these functions are analogous to the C
standard macros for classifying characters, but ignore the current
locale setting, because in
several places Python needs to analyze characters in a
locale-independent way. (Added by Eric Smith;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5793">bpo-5793</a>.)</p>
</li>
<li><p>Removed function: <code class="xref c c-macro docutils literal notranslate"><span class="pre">PyEval_CallObject</span></code> is now only available
as a macro. A function version was being kept around to preserve
ABI linking compatibility, but that was in 1997; it can certainly be
deleted by now. (Removed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8276">bpo-8276</a>.)</p></li>
<li><p>New format codes: the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyFormat_FromString()</span></code>,
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyFormat_FromStringV()</span></code>, and <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_Format" title="PyErr_Format"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Format()</span></code></a> functions now
accept <code class="docutils literal notranslate"><span class="pre">%lld</span></code> and <code class="docutils literal notranslate"><span class="pre">%llu</span></code> format codes for displaying
C's <code class="xref c c-type docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code> types.
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7228">bpo-7228</a>.)</p></li>
<li><p>The complicated interaction between threads and process forking has
been changed. Previously, the child process created by
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> might fail because the child is created with only a
single thread running, the thread performing the <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>.
If other threads were holding a lock, such as Python's import lock,
when the fork was performed, the lock would still be marked as
&quot;held&quot; in the new process. But in the child process nothing would
ever release the lock, since the other threads weren't replicated,
and the child process would no longer be able to perform imports.</p>
<p>Python 2.7 acquires the import lock before performing an
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>, and will also clean up any locks created using 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. C extension modules that have internal
locks, or that call <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> themselves, will not benefit
from this clean-up.</p>
<p>(Fixed by Thomas Wouters; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1590864">bpo-1590864</a>.)</p>
</li>
<li><p>The <a class="reference internal" href="../c-api/init.html#c.Py_Finalize" title="Py_Finalize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Finalize()</span></code></a> function now calls the internal
<code class="xref py py-func docutils literal notranslate"><span class="pre">threading._shutdown()</span></code> function; this prevents some exceptions from
being raised when an interpreter shuts down.
(Patch by Adam Olsen; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1722344">bpo-1722344</a>.)</p></li>
<li><p>When using the <a class="reference internal" href="../c-api/structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemberDef</span></code></a> structure to define attributes
of a type, Python will no longer let you try to delete or set a
<code class="xref py py-const docutils literal notranslate"><span class="pre">T_STRING_INPLACE</span></code> attribute.</p>
</li>
<li><p>Global symbols defined by the <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> module are now prefixed
with <code class="docutils literal notranslate"><span class="pre">Py</span></code>, or with <code class="docutils literal notranslate"><span class="pre">_ctypes</span></code>. (Implemented by Thomas
Heller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3102">bpo-3102</a>.)</p></li>
<li><p>New configure option: the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-system-expat</span></code> switch allows
building the <code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code> module to use the system Expat library.
(Contributed by Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7609">bpo-7609</a>.)</p></li>
<li><p>New configure option: the
<code class="xref std std-option docutils literal notranslate"><span class="pre">--with-valgrind</span></code> option will now disable the pymalloc
allocator, which is difficult for the Valgrind memory-error detector
to analyze correctly.
Valgrind will therefore be better at detecting memory leaks and
overruns. (Contributed by James Henstridge; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2422">bpo-2422</a>.)</p></li>
<li><p>New configure option: you can now supply an empty string to
<code class="xref std std-option docutils literal notranslate"><span class="pre">--with-dbmliborder=</span></code> in order to disable all of the various
DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6491">bpo-6491</a>.)</p></li>
<li><p>The <strong class="program">configure</strong> script now checks for floating-point rounding bugs
on certain 32-bit Intel chips and defines a <code class="xref c c-macro docutils literal notranslate"><span class="pre">X87_DOUBLE_ROUNDING</span></code>
preprocessor definition. No code currently uses this definition,
but it's available if anyone wishes to use it.
(Added by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2937">bpo-2937</a>.)</p>
<p><strong class="program">configure</strong> also now sets a <span class="target" id="index-12"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LDCXXSHARED</span></code> Makefile
variable for supporting C++ linking. (Contributed by Arfrever
Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1222585">bpo-1222585</a>.)</p>
</li>
<li><p>The build process now creates the necessary files for pkg-config
support. (Contributed by Clinton Roy; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3585">bpo-3585</a>.)</p></li>
<li><p>The build process now supports Subversion 1.7. (Contributed by
Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6094">bpo-6094</a>.)</p></li>
</ul>
<section id="capsules">
<span id="whatsnew27-capsules"></span><h3>Capsule 对象<a class="headerlink" href="#capsules" title="永久链接至标题"></a></h3>
<p>Python 3.1 adds a new C datatype, <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a>, for providing a
C API to an extension module. A capsule is essentially the holder of
a C <code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*</span></code> pointer, and is made available as a module attribute; for
example, the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module's API is exposed as <code class="docutils literal notranslate"><span class="pre">socket.CAPI</span></code>,
and <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> exposes <code class="docutils literal notranslate"><span class="pre">ucnhash_CAPI</span></code>. Other extensions
can import the module, access its dictionary to get the capsule
object, and then get the <code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*</span></code> pointer, which will usually point
to an array of pointers to the module's various API functions.</p>
<p>There is an existing data type already used for this,
<code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code>, but it doesn't provide type safety. Evil code
written in pure Python could cause a segmentation fault by taking a
<code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> from module A and somehow substituting it for the
<code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> in module B. Capsules know their own name,
and getting the pointer requires providing the name:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">vtable</span><span class="p">;</span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PyCapsule_IsValid</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mymodule.CAPI&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_ValueError</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;argument type invalid&quot;</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">vtable</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyCapsule_GetPointer</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mymodule.CAPI&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>You are assured that <code class="docutils literal notranslate"><span class="pre">vtable</span></code> points to whatever you're expecting.
If a different capsule was passed in, <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> would
detect the mismatched name and return false. Refer to
<a class="reference internal" href="../extending/extending.html#using-capsules"><span class="std std-ref">给扩展模块提供C API</span></a> for more information on using these objects.</p>
<p>Python 2.7 now uses capsules internally to provide various
extension-module APIs, but the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyCObject_AsVoidPtr()</span></code> was
modified to handle capsules, preserving compile-time compatibility
with the <code class="xref c c-type docutils literal notranslate"><span class="pre">CObject</span></code> interface. Use of
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyCObject_AsVoidPtr()</span></code> will signal a
<a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a>, which is silent by default.</p>
<p>Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
discussed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5630">bpo-5630</a>.</p>
</section>
<section id="port-specific-changes-windows">
<h3>特定于 Windows 的更改:<a class="headerlink" href="#port-specific-changes-windows" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>The <a class="reference internal" href="../library/msvcrt.html#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">msvcrt</span></code></a> module now contains some constants from
the <code class="file docutils literal notranslate"><span class="pre">crtassem.h</span></code> header file:
<code class="xref py py-data docutils literal notranslate"><span class="pre">CRT_ASSEMBLY_VERSION</span></code>,
<code class="xref py py-data docutils literal notranslate"><span class="pre">VC_ASSEMBLY_PUBLICKEYTOKEN</span></code>,
and <code class="xref py py-data docutils literal notranslate"><span class="pre">LIBRARIES_ASSEMBLY_NAME_PREFIX</span></code>.
(Contributed by David Cournapeau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4365">bpo-4365</a>.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code> module for accessing the registry now implements
the <code class="xref py py-func docutils literal notranslate"><span class="pre">CreateKeyEx()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">DeleteKeyEx()</span></code>
functions, extended versions of previously-supported functions that
take several extra arguments. The <code class="xref py py-func docutils literal notranslate"><span class="pre">DisableReflectionKey()</span></code>,
<code class="xref py py-func docutils literal notranslate"><span class="pre">EnableReflectionKey()</span></code>, and <code class="xref py py-func docutils literal notranslate"><span class="pre">QueryReflectionKey()</span></code>
were also tested and documented.
(Implemented by Brian Curtin: <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7347">bpo-7347</a>.)</p></li>
<li><p>The new <code class="xref c c-func docutils literal notranslate"><span class="pre">_beginthreadex()</span></code> API is used to start threads, and
the native thread-local storage functions are now used.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3582">bpo-3582</a>.)</p></li>
<li><p>The <a class="reference internal" href="../library/os.html#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.kill()</span></code></a> function now works on Windows. The signal value
can be the constants <code class="xref py py-const docutils literal notranslate"><span class="pre">CTRL_C_EVENT</span></code>,
<code class="xref py py-const docutils literal notranslate"><span class="pre">CTRL_BREAK_EVENT</span></code>, or any integer. The first two constants
will send <kbd class="kbd docutils literal notranslate">Control-C</kbd> and <kbd class="kbd docutils literal notranslate">Control-Break</kbd> keystroke events to
subprocesses; any other value will use the <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code>
API. (Contributed by Miki Tebeka; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1220212">bpo-1220212</a>.)</p></li>
<li><p>The <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">os.listdir()</span></code></a> function now correctly fails
for an empty path. (Fixed by Hirokazu Yamamoto; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5913">bpo-5913</a>.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">mimelib</span></code> module will now read the MIME database from
the Windows registry when initializing.
(Patch by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4969">bpo-4969</a>.)</p></li>
</ul>
</section>
<section id="port-specific-changes-mac-os-x">
<h3>特定于 Mac OS X 的更改:<a class="headerlink" href="#port-specific-changes-mac-os-x" title="永久链接至标题"></a></h3>
<ul>
<li><p>The path <code class="docutils literal notranslate"><span class="pre">/Library/Python/2.7/site-packages</span></code> is now appended to
<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, in order to share added packages between the system
installation and a user-installed copy of the same version.
(Changed by Ronald Oussoren; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4865">bpo-4865</a>.)</p>
<blockquote>
<div><div class="versionchanged">
<p><span class="versionmodified changed">在 2.7.13 版更改: </span>As of 2.7.13, this change was removed.
<code class="docutils literal notranslate"><span class="pre">/Library/Python/2.7/site-packages</span></code>, the site-packages directory
used by the Apple-supplied system Python 2.7 is no longer appended to
<code class="docutils literal notranslate"><span class="pre">sys.path</span></code> for user-installed Pythons such as from the python.org
installers. As of macOS 10.12, Apple changed how the system
site-packages directory is configured, which could cause installation
of pip components, like setuptools, to fail. Packages installed for
the system Python will no longer be shared with user-installed
Pythons. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=28440">bpo-28440</a>)</p>
</div>
</div></blockquote>
</li>
</ul>
</section>
<section id="port-specific-changes-freebsd">
<h3>特定于 FreeBSD 的更改:<a class="headerlink" href="#port-specific-changes-freebsd" title="永久链接至标题"></a></h3>
<ul class="simple">
<li><p>FreeBSD 7.1's <code class="xref py py-const docutils literal notranslate"><span class="pre">SO_SETFIB</span></code> constant, used with
<code class="xref py py-func docutils literal notranslate"><span class="pre">getsockopt()</span></code>/<code class="xref py py-func docutils literal notranslate"><span class="pre">setsockopt()</span></code> to select an
alternate routing table, is now available in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a>
module. (Added by Kyle VanderBeek; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8235">bpo-8235</a>.)</p></li>
</ul>
</section>
</section>
<section id="other-changes-and-fixes">
<h2>其他的改变和修正<a class="headerlink" href="#other-changes-and-fixes" title="永久链接至标题"></a></h2>
<ul class="simple">
<li><p>Two benchmark scripts, <code class="file docutils literal notranslate"><span class="pre">iobench</span></code> and <code class="file docutils literal notranslate"><span class="pre">ccbench</span></code>, were
added to the <code class="file docutils literal notranslate"><span class="pre">Tools</span></code> directory. <code class="file docutils literal notranslate"><span class="pre">iobench</span></code> measures the
speed of the built-in file I/O objects returned by <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>
while performing various operations, and <code class="file docutils literal notranslate"><span class="pre">ccbench</span></code> is a
concurrency benchmark that tries to measure computing throughput,
thread switching latency, and IO processing bandwidth when
performing several tasks using a varying number of threads.</p></li>
<li><p>The <code class="file docutils literal notranslate"><span class="pre">Tools/i18n/msgfmt.py</span></code> script now understands plural
forms in <code class="file docutils literal notranslate"><span class="pre">.po</span></code> files. (Fixed by Martin von Löwis;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5464">bpo-5464</a>.)</p></li>
<li><p>When importing a module from a <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> or <code class="file docutils literal notranslate"><span class="pre">.pyo</span></code> file
with an existing <code class="file docutils literal notranslate"><span class="pre">.py</span></code> counterpart, the <code class="xref py py-attr docutils literal notranslate"><span class="pre">co_filename</span></code>
attributes of the resulting code objects are overwritten when the
original filename is obsolete. This can happen if the file has been
renamed, moved, or is accessed through different paths. (Patch by
Ziga Seilnacht and Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1180193">bpo-1180193</a>.)</p></li>
<li><p>The <code class="file docutils literal notranslate"><span class="pre">regrtest.py</span></code> script now takes a <code class="xref std std-option docutils literal notranslate"><span class="pre">--randseed=</span></code>
switch that takes an integer that will be used as the random seed
for the <code class="xref std std-option docutils literal notranslate"><span class="pre">-r</span></code> option that executes tests in random order.
The <code class="xref std std-option docutils literal notranslate"><span class="pre">-r</span></code> option also reports the seed that was used
(Added by Collin Winter.)</p></li>
<li><p>Another <code class="file docutils literal notranslate"><span class="pre">regrtest.py</span></code> switch is <code class="xref std std-option docutils literal notranslate"><span class="pre">-j</span></code>, which
takes an integer specifying how many tests run in parallel. This
allows reducing the total runtime on multi-core machines.
This option is compatible with several other options, including the
<code class="xref std std-option docutils literal notranslate"><span class="pre">-R</span></code> switch which is known to produce long runtimes.
(Added by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6152">bpo-6152</a>.) This can also be used
with a new <code class="xref std std-option docutils literal notranslate"><span class="pre">-F</span></code> switch that runs selected tests in a loop
until they fail. (Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7312">bpo-7312</a>.)</p></li>
<li><p>When executed as a script, the <code class="file docutils literal notranslate"><span class="pre">py_compile.py</span></code> module now
accepts <code class="docutils literal notranslate"><span class="pre">'-'</span></code> as an argument, which will read standard input for
the list of filenames to be compiled. (Contributed by Piotr
Ożarowski; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8233">bpo-8233</a>.)</p></li>
</ul>
</section>
<section id="porting-to-python-2-7">
<h2>移植到 Python 2.7<a class="headerlink" href="#porting-to-python-2-7" title="永久链接至标题"></a></h2>
<p>本节列出了先前描述的改变以及可能需要修改你的代码的其他问题修正:</p>
<ul class="simple">
<li><p>The <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> function processes its arguments more
consistently; it will now call <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a> on non-float,
non-integer arguments that are supplied to it. (Fixed by Alexander
Belopolsky; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1533">bpo-1533</a>.)</p></li>
<li><p>The string <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method changed the default precision used
for floating-point and complex numbers from 6 decimal
places to 12, which matches the precision used by <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>.
(Changed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5920">bpo-5920</a>.)</p></li>
<li><p>Because of an optimization for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, the special
methods <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__enter__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> must belong to the object's
type, and cannot be directly attached to the object's instance. This
affects new-style classes (derived from <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>) and C extension
types. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6101">bpo-6101</a>.)</p></li>
<li><p>Due to a bug in Python 2.6, the <em>exc_value</em> parameter to
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> methods was often the string representation of the
exception, not an instance. This was fixed in 2.7, so <em>exc_value</em>
will be an instance as expected. (Fixed by Florent Xicluna;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7853">bpo-7853</a>.)</p></li>
<li><p>When a restricted set of attributes were set using <code class="docutils literal notranslate"><span class="pre">__slots__</span></code>,
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a>
as you would expect. Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7604">bpo-7604</a>.)</p></li>
</ul>
<p>在标准库中:</p>
<ul>
<li><p>Operations with <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 that resulted in a year
falling outside the supported range didn't always raise
<a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>. Such errors are now checked more carefully
and will now raise the exception. (Reported by Mark Leander, patch
by Anand B. Pillai and Alexander Belopolsky; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7150">bpo-7150</a>.)</p></li>
<li><p>When using <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> instances with a string's
<a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method, the default alignment was previously
left-alignment. This has been changed to right-alignment, which might
change the output of your programs.
(Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6857">bpo-6857</a>.)</p>
<p>Comparisons involving a signaling NaN value (or <code class="docutils literal notranslate"><span class="pre">sNAN</span></code>) now signal
<a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal notranslate"><span class="pre">InvalidOperation</span></code></a> instead of silently returning a true or
false value depending on the comparison operator. Quiet NaN values
(or <code class="docutils literal notranslate"><span class="pre">NaN</span></code>) are now hashable. (Fixed by Mark Dickinson;
<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7279">bpo-7279</a>.)</p>
</li>
<li><p>The ElementTree library, <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree</span></code>, no longer escapes
ampersands and angle brackets when outputting an XML processing
instruction (which looks like <cite>&lt;?xml-stylesheet href=&quot;#style1&quot;?&gt;</cite>)
or comment (which looks like <cite>&lt;!-- comment --&gt;</cite>).
(Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2746">bpo-2746</a>.)</p></li>
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code> method of <code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code> objects now does
nothing when a negative length is requested, as other file-like
objects do. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7348">bpo-7348</a>).</p></li>
<li><p>The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> as the
identifier instead of the previous default value of <code class="docutils literal notranslate"><span class="pre">'python'</span></code>.
(Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8451">bpo-8451</a>.)</p></li>
<li><p>The <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> module's default error handling has changed, to
no longer suppress fatal errors. The default error level was previously 0,
which meant that errors would only result in a message being written to the
debug log, but because the debug log is not activated by default,
these errors go unnoticed. The default error level is now 1,
which raises an exception if there's an error.
(Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7357">bpo-7357</a>.)</p></li>
<li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code> module's <code class="xref py py-func docutils literal notranslate"><span class="pre">urlsplit()</span></code> now handles
unknown URL schemes in a fashion compliant with <span class="target" id="index-13"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
URL is of the form <code class="docutils literal notranslate"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
<code class="docutils literal notranslate"><span class="pre">://</span></code> is treated as the scheme, even if it's a made-up scheme that
the module doesn't know about. This change may break code that
worked around the old behaviour. For example, Python 2.6.4 or 2.5
will return the following:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>Python 2.7 (and Python 2.6.5) will return:</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)</p>
</li>
</ul>
<p>对于C 扩展模块:</p>
<ul class="simple">
<li><p>C extensions that use integer format codes with the <code class="docutils literal notranslate"><span class="pre">PyArg_Parse*</span></code>
family of functions will now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception
instead of triggering a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5080">bpo-5080</a>).</p></li>
<li><p>Use the new <a class="reference internal" href="../c-api/conversion.html#c.PyOS_string_to_double" title="PyOS_string_to_double"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_string_to_double()</span></code></a> function instead of the old
<code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_atof()</span></code> functions,
which are now deprecated.</p></li>
</ul>
<p>对于嵌入Python的应用程序</p>
<ul class="simple">
<li><p>The <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> function was added, letting
applications close a security hole when the existing
<a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a> function was used. Check whether you're
calling <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider whether the
application should be using <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> with
<em>updatepath</em> set to false.</p></li>
</ul>
</section>
<section id="new-features-added-to-python-2-7-maintenance-releases">
<span id="py27-maintenance-enhancements"></span><h2>New Features Added to Python 2.7 Maintenance Releases<a class="headerlink" href="#new-features-added-to-python-2-7-maintenance-releases" title="永久链接至标题"></a></h2>
<p>New features may be added to Python 2.7 maintenance releases when the
situation genuinely calls for it. Any such additions must go through
the Python Enhancement Proposal process, and make a compelling case for why
they can't be adequately addressed by either adding the new feature solely to
Python 3, or else by publishing it on the Python Package Index.</p>
<p>In addition to the specific proposals listed below, there is a general
exemption allowing new <code class="docutils literal notranslate"><span class="pre">-3</span></code> warnings to be added in any Python 2.7
maintenance release.</p>
<section id="two-new-environment-variables-for-debug-mode">
<h3>Two new environment variables for debug mode<a class="headerlink" href="#two-new-environment-variables-for-debug-mode" title="永久链接至标题"></a></h3>
<p>In debug mode, the <code class="docutils literal notranslate"><span class="pre">[xxx</span> <span class="pre">refs]</span></code> statistic is not written by default, the
<span class="target" id="index-14"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSHOWREFCOUNT</span></code> environment variable now must also be set.
(Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=31733">bpo-31733</a>.)</p>
<p>When Python is compiled with <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOC</span></code> defined, allocation counts are no
longer dumped by default anymore: the <span class="target" id="index-15"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSHOWALLOCCOUNT</span></code> environment
variable must now also be set. Moreover, allocation counts are now dumped into
stderr, rather than stdout. (Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=31692">bpo-31692</a>.)</p>
<div class="versionadded">
<p><span class="versionmodified added">2.7.15 新版功能.</span></p>
</div>
</section>
<section id="pep-434-idle-enhancement-exception-for-all-branches">
<h3>PEP 434: IDLE Enhancement Exception for All Branches<a class="headerlink" href="#pep-434-idle-enhancement-exception-for-all-branches" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0434"><strong>PEP 434</strong></a> describes a general exemption for changes made to the IDLE
development environment shipped along with Python. This exemption makes it
possible for the IDLE developers to provide a more consistent user
experience across all supported versions of Python 2 and 3.</p>
<p>For details of any IDLE changes, refer to the NEWS file for the specific
release.</p>
</section>
<section id="pep-466-network-security-enhancements-for-python-2-7">
<h3>PEP 466: 针对 Python 2.7 的网络安全加固<a class="headerlink" href="#pep-466-network-security-enhancements-for-python-2-7" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> describes a number of network security enhancement proposals
that have been approved for inclusion in Python 2.7 maintenance releases,
with the first of those changes appearing in the Python 2.7.7 release.</p>
<p><span class="target" id="index-47"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> Python 2.7.7 中添加的相关功能:</p>
<ul class="simple">
<li><p><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">hmac.compare_digest()</span></code></a> was backported from Python 3 to make a timing
attack resistant comparison operation available to Python 2 applications.
(Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21306">bpo-21306</a>.)</p></li>
<li><p>OpenSSL 1.0.1g was upgraded in the official Windows installers published on
python.org. (Contributed by Zachary Ware; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21462">bpo-21462</a>.)</p></li>
</ul>
<p><span class="target" id="index-48"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> Python 2.7.8 中添加的相关功能:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../library/hashlib.html#hashlib.pbkdf2_hmac" title="hashlib.pbkdf2_hmac"><code class="xref py py-func docutils literal notranslate"><span class="pre">hashlib.pbkdf2_hmac()</span></code></a> was backported from Python 3 to make a hashing
algorithm suitable for secure password storage broadly available to Python
2 applications. (Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21304">bpo-21304</a>.)</p></li>
<li><p>OpenSSL 1.0.1h was upgraded for the official Windows installers published on
python.org. (contributed by Zachary Ware in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21671">bpo-21671</a> for CVE-2014-0224)</p></li>
</ul>
<p><span class="target" id="index-49"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> Python 2.7.9 中添加的相关功能:</p>
<ul>
<li><p>Most of Python 3.4's <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 was backported. This means <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>
now supports Server Name Indication, TLS1.x settings, access to the platform
certificate store, the <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> class, and other
features. (Contributed by Alex Gaynor and David Reid; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21308">bpo-21308</a>.)</p>
<p>Refer to the &quot;Version added: 2.7.9&quot; notes in the module documentation for
specific details.</p>
</li>
<li><p><a class="reference internal" href="../library/os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a> was changed to cache a file descriptor to <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>
instead of reopening <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> on every call. (Contributed by Alex
Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21305">bpo-21305</a>.)</p></li>
<li><p><a class="reference internal" href="../library/hashlib.html#hashlib.algorithms_guaranteed" title="hashlib.algorithms_guaranteed"><code class="xref py py-data docutils literal notranslate"><span class="pre">hashlib.algorithms_guaranteed</span></code></a> and
<a class="reference internal" href="../library/hashlib.html#hashlib.algorithms_available" title="hashlib.algorithms_available"><code class="xref py py-data docutils literal notranslate"><span class="pre">hashlib.algorithms_available</span></code></a> were backported from Python 3 to make
it easier for Python 2 applications to select the strongest available hash
algorithm. (Contributed by Alex Gaynor in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21307">bpo-21307</a>)</p></li>
</ul>
</section>
<section id="pep-477-backport-ensurepip-pep-453-to-python-2-7">
<h3>PEP 477: 将 ensurepip (PEP 453) 向下移植到 Python 2.7<a class="headerlink" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0477"><strong>PEP 477</strong></a> approves the inclusion of the <span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0453"><strong>PEP 453</strong></a> ensurepip module and the
improved documentation that was enabled by it in the Python 2.7 maintenance
releases, appearing first in the Python 2.7.9 release.</p>
<section id="bootstrapping-pip-by-default">
<h4>默认对 pip 进行初始设置<a class="headerlink" href="#bootstrapping-pip-by-default" title="永久链接至标题"></a></h4>
<p>The new <a class="reference internal" href="../library/ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> module (defined in <span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0453"><strong>PEP 453</strong></a>) provides a standard
cross-platform mechanism to bootstrap the pip installer into Python
installations. The version of <code class="docutils literal notranslate"><span class="pre">pip</span></code> included with Python 2.7.9 is <code class="docutils literal notranslate"><span class="pre">pip</span></code>
1.5.6, and future 2.7.x maintenance releases will update the bundled version to
the latest version of <code class="docutils literal notranslate"><span class="pre">pip</span></code> that is available at the time of creating the
release candidate.</p>
<p>By default, the commands <code class="docutils literal notranslate"><span class="pre">pip</span></code>, <code class="docutils literal notranslate"><span class="pre">pipX</span></code> and <code class="docutils literal notranslate"><span class="pre">pipX.Y</span></code> will be installed on
all platforms (where X.Y stands for the version of the Python installation),
along with the <code class="docutils literal notranslate"><span class="pre">pip</span></code> Python package and its dependencies.</p>
<p>For CPython <a class="reference internal" href="../using/unix.html#building-python-on-unix"><span class="std std-ref">source builds on POSIX systems</span></a>,
the <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code> and <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">altinstall</span></code> commands do not bootstrap <code class="docutils literal notranslate"><span class="pre">pip</span></code>
by default. This behaviour can be controlled through configure options, and
overridden through Makefile options.</p>
<p>在 Windows 和 Mac OS X 上,现在 CPython 安装程序默认会将 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 与 CPython 本身一同安装(用户可以在安装过程中选择不安装它)。 Window 用户需要选择执行 <code class="docutils literal notranslate"><span class="pre">PATH</span></code> 修改以使 <code class="docutils literal notranslate"><span class="pre">pip</span></code> 在命令行中默认可用,在其他情况下它仍然可以通过 Windows 版 Python 启动器以 <code class="docutils literal notranslate"><span class="pre">py</span> <span class="pre">-m</span> <span class="pre">pip</span></code> 的方式使用。</p>
<p>正如 <a class="reference external" href="https://www.python.org/dev/peps/pep-0477/#disabling-ensurepip-by-downstream-distributors">在 PEP 中已讨论的</a>,平台打包者可以选择默认不安装这些命令,只需要在它们被发起调用时,能够提供有关如何在该平台上安装它们的简单清晰的指引(通常是使用系统包管理器)。</p>
</section>
<section id="documentation-changes">
<h4>文档更改<a class="headerlink" href="#documentation-changes" title="永久链接至标题"></a></h4>
<p>As part of this change, the <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">安装 Python 模块</span></a> and
<a class="reference internal" href="../distributing/index.html#distributing-index"><span class="std std-ref">分发 Python 模块</span></a> sections of the documentation have been
completely redesigned as short getting started and FAQ documents. Most
packaging documentation has now been moved out to the Python Packaging
Authority maintained <a class="reference external" href="http://packaging.python.org">Python Packaging User Guide</a> and the documentation of the individual
projects.</p>
<p>However, as this migration is currently still incomplete, the legacy
versions of those guides remaining available as <a class="reference internal" href="../install/index.html#install-index"><span class="std std-ref">安装Python模块旧版</span></a>
and <a class="reference internal" href="../distutils/index.html#distutils-index"><span class="std std-ref">分发 Python 模块(遗留版本)</span></a>.</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><span class="target" id="index-50"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0453"><strong>PEP 453</strong></a> -- Python 安装版中对 pip 的显式初始设置</dt><dd><p>PEP 由Donald Stufft 和 Nick Coghlan 撰写,由 Donald StufftNick CoghlanMartin von Löwis 和 Ned Deily 实现。</p>
</dd>
</dl>
</div>
</section>
</section>
<section id="pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">
<h3>PEP 476: 默认为 stdlib http 客户端启用证书验证<a class="headerlink" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-25"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0476"><strong>PEP 476</strong></a> updated <code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code> and modules which use it, such as
<code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code>, to now verify that the server
presents a certificate which is signed by a Certificate Authority in the
platform trust store and whose hostname matches the hostname being requested
by default, significantly improving security for many applications. This
change was made in the Python 2.7.9 release.</p>
<p>对于需要之前版本的旧有行为的应用程序,可以传入一个替代的上下文:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib2</span>
<span class="kn">import</span> <span class="nn">ssl</span>
<span class="c1"># This disables all verification</span>
<span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">_create_unverified_context</span><span class="p">()</span>
<span class="c1"># This allows using a specific certificate for the host, which doesn&#39;t need</span>
<span class="c1"># to be in the trust store</span>
<span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">create_default_context</span><span class="p">(</span><span class="n">cafile</span><span class="o">=</span><span class="s2">&quot;/path/to/file.crt&quot;</span><span class="p">)</span>
<span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s2">&quot;https://invalid-cert&quot;</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="pep-493-https-verification-migration-tools-for-python-2-7">
<h3>PEP 493适用于Python 2.7 的 HTTPS 验证迁移工具<a class="headerlink" href="#pep-493-https-verification-migration-tools-for-python-2-7" title="永久链接至标题"></a></h3>
<p><span class="target" id="index-26"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0493"><strong>PEP 493</strong></a> provides additional migration tools to support a more incremental
infrastructure upgrade process for environments containing applications and
services relying on the historically permissive processing of server
certificates when establishing client HTTPS connections. These additions were
made in the Python 2.7.12 release.</p>
<p>These tools are intended for use in cases where affected applications and
services can't be modified to explicitly pass a more permissive SSL context
when establishing the connection.</p>
<p>For applications and services which can't be modified at all, the new
<code class="docutils literal notranslate"><span class="pre">PYTHONHTTPSVERIFY</span></code> environment variable may be set to <code class="docutils literal notranslate"><span class="pre">0</span></code> to revert an
entire Python process back to the default permissive behaviour of Python 2.7.8
and earlier.</p>
<p>For cases where the connection establishment code can't be modified, but the
overall application can be, the new <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl._https_verify_certificates()</span></code>
function can be used to adjust the default behaviour at runtime.</p>
</section>
<section id="new-make-regen-all-build-target">
<h3>新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标<a class="headerlink" href="#new-make-regen-all-build-target" title="永久链接至标题"></a></h3>
<p>为了简化交叉编译,并确保 CPython 能够可靠地编译而不需要已存在可用的 Python 版本,基于 autotools 的构建系统将不再尝试根据文件修改时间隐式地重新编译已生成的文件。</p>
<p>取而代之的是,新增了一个 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 命令以便在需要时强制重新生成这些文件(例如在基于预生成版本构建了 Python 的初始版本之后)。</p>
<p>还定义了其他一些更具选择性的重生成目标 —— 详情参见 <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Makefile.pre.in">Makefile.pre.in</a></p>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a> 中贡献。)</p>
<div class="versionadded">
<p><span class="versionmodified added">2.7.14 新版功能.</span></p>
</div>
</section>
<section id="removal-of-make-touch-build-target">
<h3>移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标<a class="headerlink" href="#removal-of-make-touch-build-target" title="永久链接至标题"></a></h3>
<p>之前用于通过更新生成文件的修改时间来请求隐式的重新生成这些文件的 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标已被移除。</p>
<p>它已被新的 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 目标所替代。</p>
<p>(由 Victor Stinner 在 <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a> 中贡献。)</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 2.7.14 版更改.</span></p>
</div>
</section>
</section>
<section id="acknowledgements">
<span id="acks27"></span><h2>致谢<a class="headerlink" href="#acknowledgements" title="永久链接至标题"></a></h2>
<p>作者要感谢以下人员为本文的各种草案提供建议,更正和帮助: Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray, Hugh Secker-Walker.</p>
</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 2.7 有什么新变化</a><ul>
<li><a class="reference internal" href="#the-future-for-python-2-x">Python 2.x的未来</a></li>
<li><a class="reference internal" href="#changes-to-the-handling-of-deprecation-warnings">对于弃用警告处理方式的改变</a></li>
<li><a class="reference internal" href="#python-3-1-features">Python 3.1 特性</a></li>
<li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372将有序字典 添加到收藏集</a></li>
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: 千位分隔符的格式说明符</a></li>
<li><a class="reference internal" href="#pep-389-the-argparse-module-for-parsing-command-lines">PEP 389用于解析命令行的 argparse 模块</a></li>
<li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: 基于字典的日志配置</a></li>
<li><a class="reference internal" href="#pep-3106-dictionary-views">PEP 3106: 字典视图</a></li>
<li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: memoryview 对象</a></li>
<li><a class="reference internal" href="#other-language-changes">其他语言特性修改</a><ul>
<li><a class="reference internal" href="#interpreter-changes">解释器改动</a></li>
<li><a class="reference internal" href="#optimizations">性能优化</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-and-improved-modules">新增和改进的模块</a><ul>
<li><a class="reference internal" href="#new-module-importlib">新模块importlib</a></li>
<li><a class="reference internal" href="#new-module-sysconfig">新模块sysconfig</a></li>
<li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttkTk 主题组件</a></li>
<li><a class="reference internal" href="#updated-module-unittest">更新的模块unittest</a></li>
<li><a class="reference internal" href="#updated-module-elementtree-1-3">更新的模块ElementTree 1.3</a></li>
</ul>
</li>
<li><a class="reference internal" href="#build-and-c-api-changes">构建和 C API 的改变</a><ul>
<li><a class="reference internal" href="#capsules">Capsule 对象</a></li>
<li><a class="reference internal" href="#port-specific-changes-windows">特定于 Windows 的更改:</a></li>
<li><a class="reference internal" href="#port-specific-changes-mac-os-x">特定于 Mac OS X 的更改:</a></li>
<li><a class="reference internal" href="#port-specific-changes-freebsd">特定于 FreeBSD 的更改:</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-changes-and-fixes">其他的改变和修正</a></li>
<li><a class="reference internal" href="#porting-to-python-2-7">移植到 Python 2.7</a></li>
<li><a class="reference internal" href="#new-features-added-to-python-2-7-maintenance-releases">New Features Added to Python 2.7 Maintenance Releases</a><ul>
<li><a class="reference internal" href="#two-new-environment-variables-for-debug-mode">Two new environment variables for debug mode</a></li>
<li><a class="reference internal" href="#pep-434-idle-enhancement-exception-for-all-branches">PEP 434: IDLE Enhancement Exception for All Branches</a></li>
<li><a class="reference internal" href="#pep-466-network-security-enhancements-for-python-2-7">PEP 466: 针对 Python 2.7 的网络安全加固</a></li>
<li><a class="reference internal" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7">PEP 477: 将 ensurepip (PEP 453) 向下移植到 Python 2.7</a><ul>
<li><a class="reference internal" href="#bootstrapping-pip-by-default">默认对 pip 进行初始设置</a></li>
<li><a class="reference internal" href="#documentation-changes">文档更改</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">PEP 476: 默认为 stdlib http 客户端启用证书验证</a></li>
<li><a class="reference internal" href="#pep-493-https-verification-migration-tools-for-python-2-7">PEP 493适用于Python 2.7 的 HTTPS 验证迁移工具</a></li>
<li><a class="reference internal" href="#new-make-regen-all-build-target">新增 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> 构建目标</a></li>
<li><a class="reference internal" href="#removal-of-make-touch-build-target">移除了 <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> 构建目标</a></li>
</ul>
</li>
<li><a class="reference internal" href="#acknowledgements">致谢</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="3.0.html"
title="上一章">Python 3.0 有什么新变化</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="2.6.html"
title="下一章">Python 2.6 有什么新变化</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/2.7.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="2.6.html" title="Python 2.6 有什么新变化"
>下一页</a> |</li>
<li class="right" >
<a href="3.0.html" title="Python 3.0 有什么新变化"
>上一页</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>