2724 lines
314 KiB
HTML
2724 lines
314 KiB
HTML
|
|
|||
|
<!DOCTYPE html>
|
|||
|
|
|||
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|||
|
<head>
|
|||
|
<meta charset="utf-8" />
|
|||
|
<title>What’s New In Python 3.2 — Python 3.7.4 documentation</title>
|
|||
|
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
|||
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|||
|
|
|||
|
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/language_data.js"></script>
|
|||
|
|
|||
|
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
|||
|
|
|||
|
<link rel="search" type="application/opensearchdescription+xml"
|
|||
|
title="Search within Python 3.7.4 documentation"
|
|||
|
href="../_static/opensearch.xml"/>
|
|||
|
<link rel="author" title="About these documents" href="../about.html" />
|
|||
|
<link rel="index" title="Index" href="../genindex.html" />
|
|||
|
<link rel="search" title="Search" href="../search.html" />
|
|||
|
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
|||
|
<link rel="next" title="What’s New In Python 3.1" href="3.1.html" />
|
|||
|
<link rel="prev" title="What’s New In Python 3.3" href="3.3.html" />
|
|||
|
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
|||
|
<link rel="canonical" href="https://docs.python.org/3/whatsnew/3.2.html" />
|
|||
|
|
|||
|
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
|||
|
<script type="text/javascript" src="../_static/switchers.js"></script>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<style>
|
|||
|
@media only screen {
|
|||
|
table.full-width-table {
|
|||
|
width: 100%;
|
|||
|
}
|
|||
|
}
|
|||
|
</style>
|
|||
|
|
|||
|
|
|||
|
</head><body>
|
|||
|
|
|||
|
<div class="related" role="navigation" aria-label="related navigation">
|
|||
|
<h3>Navigation</h3>
|
|||
|
<ul>
|
|||
|
<li class="right" style="margin-right: 10px">
|
|||
|
<a href="../genindex.html" title="General Index"
|
|||
|
accesskey="I">index</a></li>
|
|||
|
<li class="right" >
|
|||
|
<a href="../py-modindex.html" title="Python Module Index"
|
|||
|
>modules</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="3.1.html" title="What’s New In Python 3.1"
|
|||
|
accesskey="N">next</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="3.3.html" title="What’s New In Python 3.3"
|
|||
|
accesskey="P">previous</a> |</li>
|
|||
|
<li><img src="../_static/py.png" alt=""
|
|||
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|||
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|||
|
<li>
|
|||
|
<span class="language_switcher_placeholder">en</span>
|
|||
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|||
|
<a href="../index.html">Documentation </a> »
|
|||
|
</li>
|
|||
|
|
|||
|
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> »</li>
|
|||
|
<li class="right">
|
|||
|
|
|||
|
|
|||
|
<div class="inline-search" style="display: none" role="search">
|
|||
|
<form class="inline-search" action="../search.html" method="get">
|
|||
|
<input placeholder="Quick search" type="text" name="q" />
|
|||
|
<input type="submit" value="Go" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|||
|
|
|
|||
|
</li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
|
|||
|
<div class="document">
|
|||
|
<div class="documentwrapper">
|
|||
|
<div class="bodywrapper">
|
|||
|
<div class="body" role="main">
|
|||
|
|
|||
|
<div class="section" id="what-s-new-in-python-3-2">
|
|||
|
<h1>What’s New In Python 3.2<a class="headerlink" href="#what-s-new-in-python-3-2" title="Permalink to this headline">¶</a></h1>
|
|||
|
<dl class="field-list simple">
|
|||
|
<dt class="field-odd">Author</dt>
|
|||
|
<dd class="field-odd"><p>Raymond Hettinger</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
<p>This article explains the new features in Python 3.2 as compared to 3.1. It
|
|||
|
focuses on a few highlights and gives a few examples. For full details, see the
|
|||
|
<a class="reference external" href="https://github.com/python/cpython/blob/076ca6c3c8df3030307e548d9be792ce3c1c6eea/Misc/NEWS">Misc/NEWS</a>
|
|||
|
file.</p>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<p><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0392"><strong>PEP 392</strong></a> - Python 3.2 Release Schedule</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-384-defining-a-stable-abi">
|
|||
|
<h2>PEP 384: Defining a Stable ABI<a class="headerlink" href="#pep-384-defining-a-stable-abi" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>In the past, extension modules built for one Python version were often
|
|||
|
not usable with other Python versions. Particularly on Windows, every
|
|||
|
feature release of Python required rebuilding all extension modules that
|
|||
|
one wanted to use. This requirement was the result of the free access to
|
|||
|
Python interpreter internals that extension modules could use.</p>
|
|||
|
<p>With Python 3.2, an alternative approach becomes available: extension
|
|||
|
modules which restrict themselves to a limited API (by defining
|
|||
|
Py_LIMITED_API) cannot use many of the internals, but are constrained
|
|||
|
to a set of API functions that are promised to be stable for several
|
|||
|
releases. As a consequence, extension modules built for 3.2 in that
|
|||
|
mode will also work with 3.3, 3.4, and so on. Extension modules that
|
|||
|
make use of details of memory structures can still be built, but will
|
|||
|
need to be recompiled for every feature release.</p>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0384"><strong>PEP 384</strong></a> - Defining a Stable ABI</dt><dd><p>PEP written by Martin von Löwis.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-389-argparse-command-line-parsing-module">
|
|||
|
<h2>PEP 389: Argparse Command Line Parsing Module<a class="headerlink" href="#pep-389-argparse-command-line-parsing-module" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>A new module for command line parsing, <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>, was introduced to
|
|||
|
overcome the limitations of <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a> which did not provide support for
|
|||
|
positional arguments (not just options), subcommands, required options and other
|
|||
|
common patterns of specifying and validating options.</p>
|
|||
|
<p>This module has already had widespread success in the community as a
|
|||
|
third-party module. Being more fully featured than its predecessor, 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> module is now the preferred module for command-line processing.
|
|||
|
The older module is still being kept available because of the substantial amount
|
|||
|
of legacy code that depends on it.</p>
|
|||
|
<p>Here’s an annotated example parser showing features like limiting results to a
|
|||
|
set of choices, specifying a <em>metavar</em> in the help screen, validating that one
|
|||
|
or more positional arguments is present, and making a required option:</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">'Manage servers'</span><span class="p">,</span> <span class="c1"># main description for help</span>
|
|||
|
<span class="n">epilog</span> <span class="o">=</span> <span class="s1">'Tested on Solaris and Linux'</span><span class="p">)</span> <span class="c1"># displayed after help</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'action'</span><span class="p">,</span> <span class="c1"># argument name</span>
|
|||
|
<span class="n">choices</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'deploy'</span><span class="p">,</span> <span class="s1">'start'</span><span class="p">,</span> <span class="s1">'stop'</span><span class="p">],</span> <span class="c1"># three allowed values</span>
|
|||
|
<span class="n">help</span> <span class="o">=</span> <span class="s1">'action on each target'</span><span class="p">)</span> <span class="c1"># help msg</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'targets'</span><span class="p">,</span>
|
|||
|
<span class="n">metavar</span> <span class="o">=</span> <span class="s1">'HOSTNAME'</span><span class="p">,</span> <span class="c1"># var name used in help msg</span>
|
|||
|
<span class="n">nargs</span> <span class="o">=</span> <span class="s1">'+'</span><span class="p">,</span> <span class="c1"># require one or more targets</span>
|
|||
|
<span class="n">help</span> <span class="o">=</span> <span class="s1">'url for target machines'</span><span class="p">)</span> <span class="c1"># help msg explanation</span>
|
|||
|
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-u'</span><span class="p">,</span> <span class="s1">'--user'</span><span class="p">,</span> <span class="c1"># -u or --user option</span>
|
|||
|
<span class="n">required</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="c1"># make it a required argument</span>
|
|||
|
<span class="n">help</span> <span class="o">=</span> <span class="s1">'login as user'</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Example of calling the parser on a command string:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">cmd</span> <span class="o">=</span> <span class="s1">'deploy sneezy.example.com sleepy.example.com -u skycaptain'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">result</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="n">cmd</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
|
|||
|
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">action</span>
|
|||
|
<span class="go">'deploy'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">targets</span>
|
|||
|
<span class="go">['sneezy.example.com', 'sleepy.example.com']</span>
|
|||
|
<span class="gp">>>> </span><span class="n">result</span><span class="o">.</span><span class="n">user</span>
|
|||
|
<span class="go">'skycaptain'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Example of the parser’s automatically generated help:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s1">'-h'</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
|
|||
|
|
|||
|
<span class="go">usage: manage_cloud.py [-h] -u USER</span>
|
|||
|
<span class="go"> {deploy,start,stop} HOSTNAME [HOSTNAME ...]</span>
|
|||
|
|
|||
|
<span class="go">Manage servers</span>
|
|||
|
|
|||
|
<span class="go">positional arguments:</span>
|
|||
|
<span class="go"> {deploy,start,stop} action on each target</span>
|
|||
|
<span class="go"> HOSTNAME url for target machines</span>
|
|||
|
|
|||
|
<span class="go">optional arguments:</span>
|
|||
|
<span class="go"> -h, --help show this help message and exit</span>
|
|||
|
<span class="go"> -u USER, --user USER login as user</span>
|
|||
|
|
|||
|
<span class="go">Tested on Solaris and Linux</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>An especially nice <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> feature is the ability to define subparsers,
|
|||
|
each with their own argument patterns and help displays:</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">prog</span><span class="o">=</span><span class="s1">'HELM'</span><span class="p">)</span>
|
|||
|
<span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">()</span>
|
|||
|
|
|||
|
<span class="n">parser_l</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s1">'launch'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">'Launch Control'</span><span class="p">)</span> <span class="c1"># first subgroup</span>
|
|||
|
<span class="n">parser_l</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-m'</span><span class="p">,</span> <span class="s1">'--missiles'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">'store_true'</span><span class="p">)</span>
|
|||
|
<span class="n">parser_l</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-t'</span><span class="p">,</span> <span class="s1">'--torpedos'</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">'store_true'</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="n">parser_m</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s1">'move'</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">'Move Vessel'</span><span class="p">,</span> <span class="c1"># second subgroup</span>
|
|||
|
<span class="n">aliases</span><span class="o">=</span><span class="p">(</span><span class="s1">'steer'</span><span class="p">,</span> <span class="s1">'turn'</span><span class="p">))</span> <span class="c1"># equivalent names</span>
|
|||
|
<span class="n">parser_m</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-c'</span><span class="p">,</span> <span class="s1">'--course'</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">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
|||
|
<span class="n">parser_m</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">'-s'</span><span class="p">,</span> <span class="s1">'--speed'</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">default</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> ./helm.py --help <span class="c1"># top level help (launch and move)</span>
|
|||
|
<span class="gp">$</span> ./helm.py launch --help <span class="c1"># help for launch options</span>
|
|||
|
<span class="gp">$</span> ./helm.py launch --missiles <span class="c1"># set missiles=True and torpedos=False</span>
|
|||
|
<span class="gp">$</span> ./helm.py steer --course <span class="m">180</span> --speed <span class="m">5</span> <span class="c1"># set movement parameters</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0389"><strong>PEP 389</strong></a> - New Command Line Parsing Module</dt><dd><p>PEP written by Steven Bethard.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
<p><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><span class="std std-ref">Upgrading optparse code</span></a> for details on the differences from <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">optparse</span></code></a>.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-391-dictionary-based-configuration-for-logging">
|
|||
|
<h2>PEP 391: Dictionary Based Configuration for Logging<a class="headerlink" href="#pep-391-dictionary-based-configuration-for-logging" title="Permalink to this headline">¶</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 provided two kinds of configuration, one style with
|
|||
|
function calls for each option or another style driven by an external file saved
|
|||
|
in a <code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code> format. Those options did not provide the flexibility
|
|||
|
to create configurations from JSON or YAML files, nor did they support
|
|||
|
incremental configuration, which is needed for specifying logger options from a
|
|||
|
command line.</p>
|
|||
|
<p>To support a more flexible style, the module now offers
|
|||
|
<a class="reference internal" href="../library/logging.config.html#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">logging.config.dictConfig()</span></code></a> for specifying logging configuration with
|
|||
|
plain Python dictionaries. The configuration options include formatters,
|
|||
|
handlers, filters, and loggers. Here’s a working example of a configuration
|
|||
|
dictionary:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">"version"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
|||
|
<span class="s2">"formatters"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"brief"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"format"</span><span class="p">:</span> <span class="s2">"</span><span class="si">%(levelname)-8s</span><span class="s2">: </span><span class="si">%(name)-15s</span><span class="s2">: </span><span class="si">%(message)s</span><span class="s2">"</span><span class="p">},</span>
|
|||
|
<span class="s2">"full"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"format"</span><span class="p">:</span> <span class="s2">"</span><span class="si">%(asctime)s</span><span class="s2"> </span><span class="si">%(name)-15s</span><span class="s2"> </span><span class="si">%(levelname)-8s</span><span class="s2"> </span><span class="si">%(message)s</span><span class="s2">"</span><span class="p">}</span>
|
|||
|
<span class="p">},</span>
|
|||
|
<span class="s2">"handlers"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"console"</span><span class="p">:</span> <span class="p">{</span>
|
|||
|
<span class="s2">"class"</span><span class="p">:</span> <span class="s2">"logging.StreamHandler"</span><span class="p">,</span>
|
|||
|
<span class="s2">"formatter"</span><span class="p">:</span> <span class="s2">"brief"</span><span class="p">,</span>
|
|||
|
<span class="s2">"level"</span><span class="p">:</span> <span class="s2">"INFO"</span><span class="p">,</span>
|
|||
|
<span class="s2">"stream"</span><span class="p">:</span> <span class="s2">"ext://sys.stdout"</span><span class="p">},</span>
|
|||
|
<span class="s2">"console_priority"</span><span class="p">:</span> <span class="p">{</span>
|
|||
|
<span class="s2">"class"</span><span class="p">:</span> <span class="s2">"logging.StreamHandler"</span><span class="p">,</span>
|
|||
|
<span class="s2">"formatter"</span><span class="p">:</span> <span class="s2">"full"</span><span class="p">,</span>
|
|||
|
<span class="s2">"level"</span><span class="p">:</span> <span class="s2">"ERROR"</span><span class="p">,</span>
|
|||
|
<span class="s2">"stream"</span><span class="p">:</span> <span class="s2">"ext://sys.stderr"</span><span class="p">}</span>
|
|||
|
<span class="p">},</span>
|
|||
|
<span class="s2">"root"</span><span class="p">:</span> <span class="p">{</span><span class="s2">"level"</span><span class="p">:</span> <span class="s2">"DEBUG"</span><span class="p">,</span> <span class="s2">"handlers"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"console"</span><span class="p">,</span> <span class="s2">"console_priority"</span><span class="p">]}}</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>If that dictionary is stored in a file called <code class="file docutils literal notranslate"><span class="pre">conf.json</span></code>, it can be
|
|||
|
loaded and called with code like this:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">json</span><span class="o">,</span> <span class="nn">logging.config</span>
|
|||
|
<span class="gp">>>> </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'conf.json'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="gp">... </span> <span class="n">conf</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </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">conf</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">"Transaction completed normally"</span><span class="p">)</span>
|
|||
|
<span class="go">INFO : root : Transaction completed normally</span>
|
|||
|
<span class="gp">>>> </span><span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s2">"Abnormal termination"</span><span class="p">)</span>
|
|||
|
<span class="go">2011-02-17 11:14:36,694 root CRITICAL Abnormal termination</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0391"><strong>PEP 391</strong></a> - Dictionary Based Configuration for Logging</dt><dd><p>PEP written by Vinay Sajip.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-3148-the-concurrent-futures-module">
|
|||
|
<h2>PEP 3148: The <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> module<a class="headerlink" href="#pep-3148-the-concurrent-futures-module" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Code for creating and managing concurrency is being collected in a new top-level
|
|||
|
namespace, <em>concurrent</em>. Its first member is a <em>futures</em> package which provides
|
|||
|
a uniform high-level interface for managing threads and processes.</p>
|
|||
|
<p>The design for <a class="reference internal" href="../library/concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> was inspired by the
|
|||
|
<em>java.util.concurrent</em> package. In that model, a running call and its result
|
|||
|
are represented by a <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object that abstracts
|
|||
|
features common to threads, processes, and remote procedure calls. That object
|
|||
|
supports status checks (running or done), timeouts, cancellations, adding
|
|||
|
callbacks, and access to results or exceptions.</p>
|
|||
|
<p>The primary offering of the new module is a pair of executor classes for
|
|||
|
launching and managing calls. The goal of the executors is to make it easier to
|
|||
|
use existing tools for making parallel calls. They save the effort needed to
|
|||
|
setup a pool of resources, launch the calls, create a results queue, add
|
|||
|
time-out handling, and limit the total number of threads, processes, or remote
|
|||
|
procedure calls.</p>
|
|||
|
<p>Ideally, each application should share a single executor across multiple
|
|||
|
components so that process and thread limits can be centrally managed. This
|
|||
|
solves the design challenge that arises when each component has its own
|
|||
|
competing strategy for resource management.</p>
|
|||
|
<p>Both classes share a common interface with three methods:
|
|||
|
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a> for scheduling a callable and
|
|||
|
returning a <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object;
|
|||
|
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> for scheduling many asynchronous calls
|
|||
|
at a time, and <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.shutdown" title="concurrent.futures.Executor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a> for freeing
|
|||
|
resources. The class is a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> and can be used in a
|
|||
|
<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement to assure that resources are automatically released
|
|||
|
when currently pending futures are done executing.</p>
|
|||
|
<p>A simple of example of <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> is a
|
|||
|
launch of four parallel threads for copying files:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span><span class="o">,</span> <span class="nn">shutil</span>
|
|||
|
<span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
|||
|
<span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">'src1.txt'</span><span class="p">,</span> <span class="s1">'dest1.txt'</span><span class="p">)</span>
|
|||
|
<span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">'src2.txt'</span><span class="p">,</span> <span class="s1">'dest2.txt'</span><span class="p">)</span>
|
|||
|
<span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">'src3.txt'</span><span class="p">,</span> <span class="s1">'dest3.txt'</span><span class="p">)</span>
|
|||
|
<span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">'src3.txt'</span><span class="p">,</span> <span class="s1">'dest4.txt'</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3148"><strong>PEP 3148</strong></a> - Futures – Execute Computations Asynchronously</dt><dd><p>PEP written by Brian Quinlan.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
<p><a class="reference internal" href="../library/concurrent.futures.html#threadpoolexecutor-example"><span class="std std-ref">Code for Threaded Parallel URL reads</span></a>, an
|
|||
|
example using threads to fetch multiple web pages in parallel.</p>
|
|||
|
<p><a class="reference internal" href="../library/concurrent.futures.html#processpoolexecutor-example"><span class="std std-ref">Code for computing prime numbers in
|
|||
|
parallel</span></a>, an example demonstrating
|
|||
|
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>.</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-3147-pyc-repository-directories">
|
|||
|
<h2>PEP 3147: PYC Repository Directories<a class="headerlink" href="#pep-3147-pyc-repository-directories" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Python’s scheme for caching bytecode in <em>.pyc</em> files did not work well in
|
|||
|
environments with multiple Python interpreters. If one interpreter encountered
|
|||
|
a cached file created by another interpreter, it would recompile the source and
|
|||
|
overwrite the cached file, thus losing the benefits of caching.</p>
|
|||
|
<p>The issue of “pyc fights” has become more pronounced as it has become
|
|||
|
commonplace for Linux distributions to ship with multiple versions of Python.
|
|||
|
These conflicts also arise with CPython alternatives such as Unladen Swallow.</p>
|
|||
|
<p>To solve this problem, Python’s import machinery has been extended to use
|
|||
|
distinct filenames for each interpreter. Instead of Python 3.2 and Python 3.3 and
|
|||
|
Unladen Swallow each competing for a file called “mymodule.pyc”, they will now
|
|||
|
look for “mymodule.cpython-32.pyc”, “mymodule.cpython-33.pyc”, and
|
|||
|
“mymodule.unladen10.pyc”. And to prevent all of these new files from
|
|||
|
cluttering source directories, the <em>pyc</em> files are now collected in a
|
|||
|
“__pycache__” directory stored under the package directory.</p>
|
|||
|
<p>Aside from the filenames and target directories, the new scheme has a few
|
|||
|
aspects that are visible to the programmer:</p>
|
|||
|
<ul>
|
|||
|
<li><p>Imported modules now have a <a class="reference internal" href="../reference/import.html#__cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> attribute which stores the name
|
|||
|
of the actual file that was imported:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">collections</span>
|
|||
|
<span class="gp">>>> </span><span class="n">collections</span><span class="o">.</span><span class="n">__cached__</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">'c:/py32/lib/__pycache__/collections.cpython-32.pyc'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li><p>The tag that is unique to each interpreter is accessible from the <a class="reference internal" href="../library/imp.html#module-imp" title="imp: Access the implementation of the import statement. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code></a>
|
|||
|
module:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">imp</span>
|
|||
|
<span class="gp">>>> </span><span class="n">imp</span><span class="o">.</span><span class="n">get_tag</span><span class="p">()</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">'cpython-32'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li><p>Scripts that try to deduce source filename from the imported file now need to
|
|||
|
be smarter. It is no longer sufficient to simply strip the “c” from a “.pyc”
|
|||
|
filename. Instead, use the new functions in the <a class="reference internal" href="../library/imp.html#module-imp" title="imp: Access the implementation of the import statement. (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code></a> module:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">imp</span><span class="o">.</span><span class="n">source_from_cache</span><span class="p">(</span><span class="s1">'c:/py32/lib/__pycache__/collections.cpython-32.pyc'</span><span class="p">)</span>
|
|||
|
<span class="go">'c:/py32/lib/collections.py'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">imp</span><span class="o">.</span><span class="n">cache_from_source</span><span class="p">(</span><span class="s1">'c:/py32/lib/collections.py'</span><span class="p">)</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">'c:/py32/lib/__pycache__/collections.cpython-32.pyc'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> and <a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> modules have been updated to
|
|||
|
reflect the new naming convention and target directory. The command-line
|
|||
|
invocation of <em>compileall</em> has new options: <code class="docutils literal notranslate"><span class="pre">-i</span></code> for
|
|||
|
specifying a list of files and directories to compile and <code class="docutils literal notranslate"><span class="pre">-b</span></code> which causes
|
|||
|
bytecode files to be written to their legacy location rather than
|
|||
|
<em>__pycache__</em>.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/importlib.html#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> module has been updated with new <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base
|
|||
|
classes</span></a> for loading bytecode files. The obsolete
|
|||
|
ABCs, <code class="xref py py-class docutils literal notranslate"><span class="pre">PyLoader</span></code> and
|
|||
|
<code class="xref py py-class docutils literal notranslate"><span class="pre">PyPycLoader</span></code>, have been deprecated (instructions on how
|
|||
|
to stay Python 3.1 compatible are included with the documentation).</p></li>
|
|||
|
</ul>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> - PYC Repository Directories</dt><dd><p>PEP written by Barry Warsaw.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-3149-abi-version-tagged-so-files">
|
|||
|
<h2>PEP 3149: ABI Version Tagged .so Files<a class="headerlink" href="#pep-3149-abi-version-tagged-so-files" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>The PYC repository directory allows multiple bytecode cache files to be
|
|||
|
co-located. This PEP implements a similar mechanism for shared object files by
|
|||
|
giving them a common directory and distinct names for each version.</p>
|
|||
|
<p>The common directory is “pyshared” and the file names are made distinct by
|
|||
|
identifying the Python implementation (such as CPython, PyPy, Jython, etc.), the
|
|||
|
major and minor version numbers, and optional build flags (such as “d” for
|
|||
|
debug, “m” for pymalloc, “u” for wide-unicode). For an arbitrary package “foo”,
|
|||
|
you may see these files when the distribution package is installed:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">share</span><span class="o">/</span><span class="n">pyshared</span><span class="o">/</span><span class="n">foo</span><span class="o">.</span><span class="n">cpython</span><span class="o">-</span><span class="mi">32</span><span class="n">m</span><span class="o">.</span><span class="n">so</span>
|
|||
|
<span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">share</span><span class="o">/</span><span class="n">pyshared</span><span class="o">/</span><span class="n">foo</span><span class="o">.</span><span class="n">cpython</span><span class="o">-</span><span class="mi">33</span><span class="n">md</span><span class="o">.</span><span class="n">so</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>In Python itself, the tags are accessible from functions in 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:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sysconfig</span>
|
|||
|
<span class="gp">>>> </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s1">'SOABI'</span><span class="p">)</span> <span class="c1"># find the version tag</span>
|
|||
|
<span class="go">'cpython-32mu'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s1">'EXT_SUFFIX'</span><span class="p">)</span> <span class="c1"># find the full filename extension</span>
|
|||
|
<span class="go">'.cpython-32mu.so'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3149"><strong>PEP 3149</strong></a> - ABI Version Tagged .so Files</dt><dd><p>PEP written by Barry Warsaw.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="pep-3333-python-web-server-gateway-interface-v1-0-1">
|
|||
|
<h2>PEP 3333: Python Web Server Gateway Interface v1.0.1<a class="headerlink" href="#pep-3333-python-web-server-gateway-interface-v1-0-1" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>This informational PEP clarifies how bytes/text issues are to be handled by the
|
|||
|
WSGI protocol. The challenge is that string handling in Python 3 is most
|
|||
|
conveniently handled with the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type even though the HTTP protocol
|
|||
|
is itself bytes oriented.</p>
|
|||
|
<p>The PEP differentiates so-called <em>native strings</em> that are used for
|
|||
|
request/response headers and metadata versus <em>byte strings</em> which are used for
|
|||
|
the bodies of requests and responses.</p>
|
|||
|
<p>The <em>native strings</em> are always of type <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> but are restricted to code
|
|||
|
points between <em>U+0000</em> through <em>U+00FF</em> which are translatable to bytes using
|
|||
|
<em>Latin-1</em> encoding. These strings are used for the keys and values in the
|
|||
|
environment dictionary and for response headers and statuses in the
|
|||
|
<code class="xref py py-func docutils literal notranslate"><span class="pre">start_response()</span></code> function. They must follow <span class="target" id="index-7"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2616.html"><strong>RFC 2616</strong></a> with respect to
|
|||
|
encoding. That is, they must either be <em>ISO-8859-1</em> characters or use
|
|||
|
<span class="target" id="index-8"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2047.html"><strong>RFC 2047</strong></a> MIME encoding.</p>
|
|||
|
<p>For developers porting WSGI applications from Python 2, here are the salient
|
|||
|
points:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>If the app already used strings for headers in Python 2, no change is needed.</p></li>
|
|||
|
<li><p>If instead, the app encoded output headers or decoded input headers, then the
|
|||
|
headers will need to be re-encoded to Latin-1. For example, an output header
|
|||
|
encoded in utf-8 was using <code class="docutils literal notranslate"><span class="pre">h.encode('utf-8')</span></code> now needs to convert from
|
|||
|
bytes to native strings using <code class="docutils literal notranslate"><span class="pre">h.encode('utf-8').decode('latin-1')</span></code>.</p></li>
|
|||
|
<li><p>Values yielded by an application or sent using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method
|
|||
|
must be byte strings. The <code class="xref py py-func docutils literal notranslate"><span class="pre">start_response()</span></code> function and environ
|
|||
|
must use native strings. The two cannot be mixed.</p></li>
|
|||
|
</ul>
|
|||
|
<p>For server implementers writing CGI-to-WSGI pathways or other CGI-style
|
|||
|
protocols, the users must to be able access the environment using native strings
|
|||
|
even though the underlying platform may have a different convention. To bridge
|
|||
|
this gap, the <a class="reference internal" href="../library/wsgiref.html#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code></a> module has a new function,
|
|||
|
<a class="reference internal" href="../library/wsgiref.html#wsgiref.handlers.read_environ" title="wsgiref.handlers.read_environ"><code class="xref py py-func docutils literal notranslate"><span class="pre">wsgiref.handlers.read_environ()</span></code></a> for transcoding CGI variables from
|
|||
|
<a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-attr docutils literal notranslate"><span class="pre">os.environ</span></code></a> into native strings and returning a new dictionary.</p>
|
|||
|
<div class="admonition seealso">
|
|||
|
<p class="admonition-title">See also</p>
|
|||
|
<dl class="simple">
|
|||
|
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> - Python Web Server Gateway Interface v1.0.1</dt><dd><p>PEP written by Phillip Eby.</p>
|
|||
|
</dd>
|
|||
|
</dl>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="other-language-changes">
|
|||
|
<h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Some smaller changes made to the core Python language are:</p>
|
|||
|
<ul>
|
|||
|
<li><p>String formatting for <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 <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> gained new
|
|||
|
capabilities for the format character <strong>#</strong>. Previously, for integers in
|
|||
|
binary, octal, or hexadecimal, it caused the output to be prefixed with ‘0b’,
|
|||
|
‘0o’, or ‘0x’ respectively. Now it can also handle floats, complex, and
|
|||
|
Decimal, causing the output to always have a decimal point even when no digits
|
|||
|
follow it.</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="s1">'#o'</span><span class="p">)</span>
|
|||
|
<span class="go">'0o24'</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">format</span><span class="p">(</span><span class="mf">12.34</span><span class="p">,</span> <span class="s1">'#5.0f'</span><span class="p">)</span>
|
|||
|
<span class="go">' 12.'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Suggested by Mark Dickinson and implemented by Eric Smith in <a class="reference external" href="https://bugs.python.org/issue7094">bpo-7094</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>There is also a new <a class="reference internal" href="../library/stdtypes.html#str.format_map" title="str.format_map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format_map()</span></code></a> method that extends the
|
|||
|
capabilities of the existing <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 by accepting arbitrary
|
|||
|
<a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> objects. This new method makes it possible to use string
|
|||
|
formatting with any of Python’s many dictionary-like objects such as
|
|||
|
<a class="reference internal" href="../library/collections.html#collections.defaultdict" title="collections.defaultdict"><code class="xref py py-class docutils literal notranslate"><span class="pre">defaultdict</span></code></a>, <a class="reference internal" href="../library/shelve.html#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a>,
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a>, or <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix "database" formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>. It is also useful with
|
|||
|
custom <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> subclasses that normalize keys before look-up or that
|
|||
|
supply a <a class="reference internal" href="../reference/datamodel.html#object.__missing__" title="object.__missing__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__missing__()</span></code></a> method for unknown keys:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">shelve</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'tmp.shl'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="s1">'The </span><span class="si">{project_name}</span><span class="s1"> status is </span><span class="si">{status}</span><span class="s1"> as of </span><span class="si">{date}</span><span class="s1">'</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
|
|||
|
<span class="go">'The testing project status is green as of February 15, 2011'</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">LowerCasedDict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="nb">dict</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
|
|||
|
<span class="gp">>>> </span><span class="n">lcd</span> <span class="o">=</span> <span class="n">LowerCasedDict</span><span class="p">(</span><span class="n">part</span><span class="o">=</span><span class="s1">'widgets'</span><span class="p">,</span> <span class="n">quantity</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="s1">'There are </span><span class="si">{QUANTITY}</span><span class="s1"> </span><span class="si">{Part}</span><span class="s1"> in stock'</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">lcd</span><span class="p">)</span>
|
|||
|
<span class="go">'There are 10 widgets in stock'</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">PlaceholderDict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__missing__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="s1">'<</span><span class="si">{}</span><span class="s1">>'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="s1">'Hello </span><span class="si">{name}</span><span class="s1">, welcome to </span><span class="si">{location}</span><span class="s1">'</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">PlaceholderDict</span><span class="p">())</span>
|
|||
|
<span class="go">'Hello <name>, welcome to <location>'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<blockquote>
|
|||
|
<div><p>(Suggested by Raymond Hettinger and implemented by Eric Smith in
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue6081">bpo-6081</a>.)</p>
|
|||
|
</div></blockquote>
|
|||
|
<ul>
|
|||
|
<li><p>The interpreter can now be started with a quiet option, <code class="docutils literal notranslate"><span class="pre">-q</span></code>, to prevent
|
|||
|
the copyright and version information from being displayed in the interactive
|
|||
|
mode. The option can be introspected using the <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.flags</span></code></a> attribute:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -q
|
|||
|
<span class="gp">></span>>> sys.flags
|
|||
|
<span class="go">sys.flags(debug=0, division_warning=0, inspect=0, interactive=0,</span>
|
|||
|
<span class="go">optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,</span>
|
|||
|
<span class="go">ignore_environment=0, verbose=0, bytes_warning=0, quiet=1)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Marcin Wojdyr in <a class="reference external" href="https://bugs.python.org/issue1772833">bpo-1772833</a>).</p>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> function works by calling <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> and detecting
|
|||
|
whether an exception is raised. This technique allows it to detect methods
|
|||
|
created dynamically by <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> which
|
|||
|
would otherwise be absent from the class dictionary. Formerly, <em>hasattr</em>
|
|||
|
would catch any exception, possibly masking genuine errors. Now, <em>hasattr</em>
|
|||
|
has been tightened to only catch <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> and let other
|
|||
|
exceptions pass through:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
|||
|
<span class="gp">... </span> <span class="nd">@property</span>
|
|||
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="mi">1</span> <span class="o">//</span> <span class="mi">0</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'f'</span><span class="p">)</span>
|
|||
|
<span class="gt">Traceback (most recent call last):</span>
|
|||
|
<span class="c">...</span>
|
|||
|
<span class="gr">ZeroDivisionError</span>: <span class="n">integer division or modulo by zero</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Discovered by Yury Selivanov and fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue9666">bpo-9666</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <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> of a float or complex number is now the same as its
|
|||
|
<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>. Previously, the <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> form was shorter but that just
|
|||
|
caused confusion and is no longer needed now that the shortest possible
|
|||
|
<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> is displayed by default:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">math</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">repr</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
|
|||
|
<span class="go">'3.141592653589793'</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
|
|||
|
<span class="go">'3.141592653589793'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Proposed and implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue9337">bpo-9337</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> objects now have a <a class="reference internal" href="../library/stdtypes.html#memoryview.release" title="memoryview.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> method
|
|||
|
and they also now support the context management protocol. This allows timely
|
|||
|
release of any resources that were acquired when requesting a buffer from the
|
|||
|
original object.</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="nb">memoryview</span><span class="p">(</span><span class="sa">b</span><span class="s1">'abcdefgh'</span><span class="p">)</span> <span class="k">as</span> <span class="n">v</span><span class="p">:</span>
|
|||
|
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
|
|||
|
<span class="go">[97, 98, 99, 100, 101, 102, 103, 104]</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue9757">bpo-9757</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Previously it was illegal to delete a name from the local namespace if it
|
|||
|
occurs as a free variable in a nested block:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">outer</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
|||
|
<span class="k">def</span> <span class="nf">inner</span><span class="p">():</span>
|
|||
|
<span class="k">return</span> <span class="n">x</span>
|
|||
|
<span class="n">inner</span><span class="p">()</span>
|
|||
|
<span class="k">del</span> <span class="n">x</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>This is now allowed. Remember that the target of an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> clause
|
|||
|
is cleared, so this code which used to work with Python 2.6, raised a
|
|||
|
<a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> with Python 3.1 and now works again:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
|
|||
|
<span class="k">def</span> <span class="nf">print_error</span><span class="p">():</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
|
|||
|
<span class="k">try</span><span class="p">:</span>
|
|||
|
<span class="n">something</span>
|
|||
|
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
|
|||
|
<span class="n">print_error</span><span class="p">()</span>
|
|||
|
<span class="c1"># implicit "del e" here</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(See <a class="reference external" href="https://bugs.python.org/issue4617">bpo-4617</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The internal <code class="xref c c-type docutils literal notranslate"><span class="pre">structsequence</span></code> tool now creates subclasses of tuple.
|
|||
|
This means that C structures like those returned by <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>,
|
|||
|
<a class="reference internal" href="../library/time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a>, and <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> now work like a
|
|||
|
<a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> and now work with functions and methods that
|
|||
|
expect a tuple as an argument. This is a big step forward in making the C
|
|||
|
structures as flexible as their pure Python counterparts:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
|
|||
|
<span class="go">True</span>
|
|||
|
<span class="gp">>>> </span><span class="s1">'Version </span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1"> </span><span class="si">%s</span><span class="s1">(</span><span class="si">%d</span><span class="s1">)'</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">'Version 3.2.0 final(0)'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Suggested by Arfrever Frehtes Taifersar Arahesis and implemented
|
|||
|
by Benjamin Peterson in <a class="reference external" href="https://bugs.python.org/issue8413">bpo-8413</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Warnings are now easier to control using the <span class="target" id="index-10"></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>
|
|||
|
environment variable as an alternative to using <code class="docutils literal notranslate"><span class="pre">-W</span></code> at the command line:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> <span class="nb">export</span> <span class="nv">PYTHONWARNINGS</span><span class="o">=</span><span class="s1">'ignore::RuntimeWarning::,once::UnicodeWarning::'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Suggested by Barry Warsaw and implemented by Philip Jenvey in <a class="reference external" href="https://bugs.python.org/issue7301">bpo-7301</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>A new warning category, <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a>, has been added. It is
|
|||
|
emitted when potential issues with resource consumption or cleanup
|
|||
|
are detected. It is silenced by default in normal release builds but
|
|||
|
can be enabled through the means provided by the <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a>
|
|||
|
module, or on the command line.</p>
|
|||
|
<p>A <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> is issued at interpreter shutdown if the
|
|||
|
<a class="reference internal" href="../library/gc.html#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">gc.garbage</span></code></a> list isn’t empty, and if <a class="reference internal" href="../library/gc.html#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><code class="xref py py-attr docutils literal notranslate"><span class="pre">gc.DEBUG_UNCOLLECTABLE</span></code></a> is
|
|||
|
set, all uncollectable objects are printed. This is meant to make the
|
|||
|
programmer aware that their code contains object finalization issues.</p>
|
|||
|
<p>A <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> is also issued when a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> is destroyed
|
|||
|
without having been explicitly closed. While the deallocator for such
|
|||
|
object ensures it closes the underlying operating system resource
|
|||
|
(usually, a file descriptor), the delay in deallocating the object could
|
|||
|
produce various issues, especially under Windows. Here is an example
|
|||
|
of enabling the warning from the command line:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -q -Wdefault
|
|||
|
<span class="gp">></span>>> <span class="nv">f</span> <span class="o">=</span> open<span class="o">(</span><span class="s2">"foo"</span>, <span class="s2">"wb"</span><span class="o">)</span>
|
|||
|
<span class="gp">></span>>> del f
|
|||
|
<span class="go">__main__:1: ResourceWarning: unclosed file <_io.BufferedWriter name='foo'></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Added by Antoine Pitrou and Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue10093">bpo-10093</a> and <a class="reference external" href="https://bugs.python.org/issue477863">bpo-477863</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p><a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> objects now support <em>index</em> and <em>count</em> methods. This is part
|
|||
|
of an effort to make more objects fully implement the
|
|||
|
<code class="xref py py-class docutils literal notranslate"><span class="pre">collections.Sequence</span></code> <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a>. As a result, the
|
|||
|
language will have a more uniform API. In addition, <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> objects
|
|||
|
now support slicing and negative indices, even with values larger than
|
|||
|
<a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>. This makes <em>range</em> more interoperable with lists:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
|||
|
<span class="go">1</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
|
|||
|
<span class="go">5</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">5</span><span class="p">]</span>
|
|||
|
<span class="go">10</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</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="go">range(0, 10, 2)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Daniel Stutzbach in <a class="reference external" href="https://bugs.python.org/issue9213">bpo-9213</a>, by Alexander Belopolsky
|
|||
|
in <a class="reference external" href="https://bugs.python.org/issue2690">bpo-2690</a>, and by Nick Coghlan in <a class="reference external" href="https://bugs.python.org/issue10889">bpo-10889</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/functions.html#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a> builtin function from Py2.x was resurrected. It provides
|
|||
|
a concise, readable alternative to using an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> in an
|
|||
|
expression like <code class="docutils literal notranslate"><span class="pre">isinstance(x,</span> <span class="pre">collections.Callable)</span></code>:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">callable</span><span class="p">(</span><span class="nb">max</span><span class="p">)</span>
|
|||
|
<span class="go">True</span>
|
|||
|
<span class="gp">>>> </span><span class="n">callable</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
|
|||
|
<span class="go">False</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(See <a class="reference external" href="https://bugs.python.org/issue10518">bpo-10518</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Python’s import mechanism can now load modules installed in directories with
|
|||
|
non-ASCII characters in the path name. This solved an aggravating problem
|
|||
|
with home directories for users with non-ASCII characters in their usernames.</p></li>
|
|||
|
</ul>
|
|||
|
<blockquote>
|
|||
|
<div><p>(Required extensive work by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue9425">bpo-9425</a>.)</p>
|
|||
|
</div></blockquote>
|
|||
|
</div>
|
|||
|
<div class="section" id="new-improved-and-deprecated-modules">
|
|||
|
<h2>New, Improved, and Deprecated Modules<a class="headerlink" href="#new-improved-and-deprecated-modules" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Python’s standard library has undergone significant maintenance efforts and
|
|||
|
quality improvements.</p>
|
|||
|
<p>The biggest news for Python 3.2 is that 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> package, <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, and <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> modules now work correctly with the bytes/text model
|
|||
|
in Python 3. For the first time, there is correct handling of messages with
|
|||
|
mixed encodings.</p>
|
|||
|
<p>Throughout the standard library, there has been more careful attention to
|
|||
|
encodings and text versus bytes issues. In particular, interactions with the
|
|||
|
operating system are now better able to exchange non-ASCII data using the
|
|||
|
Windows MBCS encoding, locale-aware encodings, or UTF-8.</p>
|
|||
|
<p>Another significant win is the addition of substantially better support for
|
|||
|
<em>SSL</em> connections and security certificates.</p>
|
|||
|
<p>In addition, more classes now implement a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> to support
|
|||
|
convenient and reliable resource clean-up using a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement.</p>
|
|||
|
<div class="section" id="email">
|
|||
|
<h3>email<a class="headerlink" href="#email" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The usability of 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> package in Python 3 has been mostly fixed by
|
|||
|
the extensive efforts of R. David Murray. The problem was that emails are
|
|||
|
typically read and stored in the form of <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> rather than <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>
|
|||
|
text, and they may contain multiple encodings within a single email. So, the
|
|||
|
email package had to be extended to parse and generate email messages in bytes
|
|||
|
format.</p>
|
|||
|
<ul>
|
|||
|
<li><p>New functions <a class="reference internal" href="../library/email.parser.html#email.message_from_bytes" title="email.message_from_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">message_from_bytes()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/email.parser.html#email.message_from_binary_file" title="email.message_from_binary_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">message_from_binary_file()</span></code></a>, and new classes
|
|||
|
<a class="reference internal" href="../library/email.parser.html#email.parser.BytesFeedParser" title="email.parser.BytesFeedParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesFeedParser</span></code></a> and <a class="reference internal" href="../library/email.parser.html#email.parser.BytesParser" title="email.parser.BytesParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesParser</span></code></a>
|
|||
|
allow binary message data to be parsed into model objects.</p></li>
|
|||
|
<li><p>Given bytes input to the model, <a class="reference internal" href="../library/email.compat32-message.html#email.message.Message.get_payload" title="email.message.Message.get_payload"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_payload()</span></code></a>
|
|||
|
will by default decode a message body that has a
|
|||
|
<em class="mailheader">Content-Transfer-Encoding</em> of <em>8bit</em> using the charset
|
|||
|
specified in the MIME headers and return the resulting string.</p></li>
|
|||
|
<li><p>Given bytes input to the model, <a class="reference internal" href="../library/email.generator.html#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> will
|
|||
|
convert message bodies that have a <em class="mailheader">Content-Transfer-Encoding</em> of
|
|||
|
<em>8bit</em> to instead have a <em>7bit</em> <em class="mailheader">Content-Transfer-Encoding</em>.</p>
|
|||
|
<p>Headers with unencoded non-ASCII bytes are deemed to be <span class="target" id="index-11"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2047.html"><strong>RFC 2047</strong></a>-encoded
|
|||
|
using the <em>unknown-8bit</em> character set.</p>
|
|||
|
</li>
|
|||
|
<li><p>A new class <a class="reference internal" href="../library/email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> produces bytes as output,
|
|||
|
preserving any unchanged non-ASCII data that was present in the input used to
|
|||
|
build the model, including message bodies with a
|
|||
|
<em class="mailheader">Content-Transfer-Encoding</em> of <em>8bit</em>.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> class now accepts a byte string
|
|||
|
for the <em>msg</em> argument to the <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> method,
|
|||
|
and a new method, <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_message()</span></code></a> accepts a
|
|||
|
<a class="reference internal" href="../library/email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object and can optionally obtain the
|
|||
|
<em>from_addr</em> and <em>to_addrs</em> addresses directly from the object.</p></li>
|
|||
|
</ul>
|
|||
|
<p>(Proposed and implemented by R. David Murray, <a class="reference external" href="https://bugs.python.org/issue4661">bpo-4661</a> and <a class="reference external" href="https://bugs.python.org/issue10321">bpo-10321</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="elementtree">
|
|||
|
<h3>elementtree<a class="headerlink" href="#elementtree" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> package and its <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code>
|
|||
|
counterpart have been updated to version 1.3.</p>
|
|||
|
<p>Several new and useful functions and methods have been added:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.fromstringlist" title="xml.etree.ElementTree.fromstringlist"><code class="xref py py-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.fromstringlist()</span></code></a> which builds an XML document
|
|||
|
from a sequence of fragments</p></li>
|
|||
|
<li><p><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-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.register_namespace()</span></code></a> for registering a global
|
|||
|
namespace prefix</p></li>
|
|||
|
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.tostringlist" title="xml.etree.ElementTree.tostringlist"><code class="xref py py-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.tostringlist()</span></code></a> for string representation
|
|||
|
including all sublists</p></li>
|
|||
|
<li><p><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">xml.etree.ElementTree.Element.extend()</span></code></a> for appending a sequence of zero
|
|||
|
or more elements</p></li>
|
|||
|
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.Element.iterfind()</span></code></a> searches an element and
|
|||
|
subelements</p></li>
|
|||
|
<li><p><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">xml.etree.ElementTree.Element.itertext()</span></code></a> creates a text iterator over
|
|||
|
an element and its subelements</p></li>
|
|||
|
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.end" title="xml.etree.ElementTree.TreeBuilder.end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.TreeBuilder.end()</span></code></a> closes the current element</p></li>
|
|||
|
<li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.TreeBuilder.doctype()</span></code></a> handles a doctype
|
|||
|
declaration</p></li>
|
|||
|
</ul>
|
|||
|
<p>Two methods have been deprecated:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.getchildren()</span></code> use <code class="docutils literal notranslate"><span class="pre">list(elem)</span></code> instead.</p></li>
|
|||
|
<li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.getiterator()</span></code> use <code class="docutils literal notranslate"><span class="pre">Element.iter</span></code> instead.</p></li>
|
|||
|
</ul>
|
|||
|
<p>For details of the update, see <a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">Introducing ElementTree</a> on Fredrik Lundh’s website.</p>
|
|||
|
<p>(Contributed by Florent Xicluna and Fredrik Lundh, <a class="reference external" href="https://bugs.python.org/issue6472">bpo-6472</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="functools">
|
|||
|
<h3>functools<a class="headerlink" href="#functools" title="Permalink to this headline">¶</a></h3>
|
|||
|
<ul>
|
|||
|
<li><p>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 includes a new decorator for caching function
|
|||
|
calls. <a class="reference internal" href="../library/functools.html#functools.lru_cache" title="functools.lru_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.lru_cache()</span></code></a> can save repeated queries to an external
|
|||
|
resource whenever the results are expected to be the same.</p>
|
|||
|
<p>For example, adding a caching decorator to a database query function can save
|
|||
|
database accesses for popular searches:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">functools</span>
|
|||
|
<span class="gp">>>> </span><span class="nd">@functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>
|
|||
|
<span class="gp">... </span><span class="k">def</span> <span class="nf">get_phone_number</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
|
|||
|
<span class="gp">... </span> <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">'SELECT phonenumber FROM phonelist WHERE name=?'</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,))</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">c</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">user_requests</span><span class="p">:</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="gp">... </span> <span class="n">get_phone_number</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="c1"># cached lookup</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>To help with choosing an effective cache size, the wrapped function is
|
|||
|
instrumented for tracking cache statistics:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">get_phone_number</span><span class="o">.</span><span class="n">cache_info</span><span class="p">()</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>If the phonelist table gets updated, the outdated contents of the cache can be
|
|||
|
cleared with:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">get_phone_number</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger and incorporating design ideas from Jim
|
|||
|
Baker, Miki Tebeka, and Nick Coghlan; see <a class="reference external" href="https://code.activestate.com/recipes/498245">recipe 498245</a>, <a class="reference external" href="https://code.activestate.com/recipes/577479">recipe 577479</a>, <a class="reference external" href="https://bugs.python.org/issue10586">bpo-10586</a>, and
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue10593">bpo-10593</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/functools.html#functools.wraps" title="functools.wraps"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.wraps()</span></code></a> decorator now adds a <code class="xref py py-attr docutils literal notranslate"><span class="pre">__wrapped__</span></code> attribute
|
|||
|
pointing to the original callable function. This allows wrapped functions to
|
|||
|
be introspected. It also copies <code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code> if defined. And now
|
|||
|
it also gracefully skips over missing attributes such as <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> which
|
|||
|
might not be defined for the wrapped callable.</p>
|
|||
|
<p>In the above example, the cache can be removed by recovering the original
|
|||
|
function:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">get_phone_number</span> <span class="o">=</span> <span class="n">get_phone_number</span><span class="o">.</span><span class="n">__wrapped__</span> <span class="c1"># uncached function</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(By Nick Coghlan and Terrence Cole; <a class="reference external" href="https://bugs.python.org/issue9567">bpo-9567</a>, <a class="reference external" href="https://bugs.python.org/issue3445">bpo-3445</a>, and
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue8814">bpo-8814</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>To help write classes with rich comparison methods, a new 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">functools.total_ordering()</span></code></a> will use existing equality and inequality
|
|||
|
methods to fill in the remaining methods.</p>
|
|||
|
<p>For example, supplying <em>__eq__</em> and <em>__lt__</em> will enable
|
|||
|
<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> to fill-in <em>__le__</em>, <em>__gt__</em> and <em>__ge__</em>:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@total_ordering</span>
|
|||
|
<span class="k">class</span> <span class="nc">Student</span><span class="p">:</span>
|
|||
|
<span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o">==</span>
|
|||
|
<span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
|
|||
|
<span class="k">return</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o"><</span>
|
|||
|
<span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>With the <em>total_ordering</em> decorator, the remaining comparison methods
|
|||
|
are filled in automatically.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
<li><p>To aid in porting programs from Python 2, the <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">functools.cmp_to_key()</span></code></a>
|
|||
|
function converts an old-style comparison function to
|
|||
|
modern <a class="reference internal" href="../glossary.html#term-key-function"><span class="xref std std-term">key function</span></a>:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="c1"># locale-aware sort order</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">sorted</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">cmp_to_key</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">strcoll</span><span class="p">))</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>For sorting examples and a brief sorting tutorial, see the <a class="reference external" href="https://wiki.python.org/moin/HowTo/Sorting/">Sorting HowTo</a> tutorial.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="itertools">
|
|||
|
<h3>itertools<a class="headerlink" href="#itertools" title="Permalink to this headline">¶</a></h3>
|
|||
|
<ul>
|
|||
|
<li><p>The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module has a new <a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">accumulate()</span></code></a> function
|
|||
|
modeled on APL’s <em>scan</em> operator and Numpy’s <em>accumulate</em> function:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">accumulate</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">accumulate</span><span class="p">([</span><span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">50</span><span class="p">]))</span>
|
|||
|
<span class="go">[8, 10, 60]</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">prob_dist</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">]</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">accumulate</span><span class="p">(</span><span class="n">prob_dist</span><span class="p">))</span> <span class="c1"># cumulative probability distribution</span>
|
|||
|
<span class="go">[0.1, 0.5, 0.7, 1.0]</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>For an example using <a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">accumulate()</span></code></a>, see the <a class="reference internal" href="../library/random.html#random-examples"><span class="std std-ref">examples for
|
|||
|
the random module</span></a>.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger and incorporating design suggestions
|
|||
|
from Mark Dickinson.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="collections">
|
|||
|
<h3>collections<a class="headerlink" href="#collections" title="Permalink to this headline">¶</a></h3>
|
|||
|
<ul>
|
|||
|
<li><p>The <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.Counter</span></code></a> class now has two forms of in-place
|
|||
|
subtraction, the existing <em>-=</em> operator for <a class="reference external" href="https://en.wikipedia.org/wiki/Saturation_arithmetic">saturating subtraction</a> and the new
|
|||
|
<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> method for regular subtraction. The
|
|||
|
former is suitable for <a class="reference external" href="https://en.wikipedia.org/wiki/Multiset">multisets</a>
|
|||
|
which only have positive counts, and the latter is more suitable for use cases
|
|||
|
that allow negative counts:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">Counter</span>
|
|||
|
<span class="gp">>>> </span><span class="n">tally</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">tally</span> <span class="o">-=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span> <span class="c1"># saturating subtraction</span>
|
|||
|
<span class="gp">>>> </span><span class="n">tally</span>
|
|||
|
<span class="go">Counter({'dogs': 3})</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">tally</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">tally</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span> <span class="c1"># regular subtraction</span>
|
|||
|
<span class="gp">>>> </span><span class="n">tally</span>
|
|||
|
<span class="go">Counter({'dogs': 3, 'cats': -5})</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
<li><p>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">collections.OrderedDict</span></code></a> class has a new method
|
|||
|
<a class="reference internal" href="../library/collections.html#collections.OrderedDict.move_to_end" title="collections.OrderedDict.move_to_end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">move_to_end()</span></code></a> which takes an existing key and
|
|||
|
moves it to either the first or last position in the ordered sequence.</p>
|
|||
|
<p>The default is to move an item to the last position. This is equivalent of
|
|||
|
renewing an entry with <code class="docutils literal notranslate"><span class="pre">od[k]</span> <span class="pre">=</span> <span class="pre">od.pop(k)</span></code>.</p>
|
|||
|
<p>A fast move-to-end operation is useful for resequencing entries. For example,
|
|||
|
an ordered dictionary can be used to track order of access by aging entries
|
|||
|
from the oldest to the most recently accessed.</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="o">.</span><span class="n">fromkeys</span><span class="p">([</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'X'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">])</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
|
|||
|
<span class="go">['a', 'b', 'X', 'd', 'e']</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">move_to_end</span><span class="p">(</span><span class="s1">'X'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
|
|||
|
<span class="go">['a', 'b', 'd', 'e', 'X']</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
<li><p>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">collections.deque</span></code></a> class grew two new methods
|
|||
|
<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> and <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> that
|
|||
|
make them more substitutable for <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> objects:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">deque</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s1">'simsalabim'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">'s'</span><span class="p">)</span>
|
|||
|
<span class="go">2</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">d</span>
|
|||
|
<span class="go">deque(['m', 'i', 'b', 'a', 'l', 'a', 's', 'm', 'i', 's'])</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="threading">
|
|||
|
<h3>threading<a class="headerlink" href="#threading" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>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 has a new <a class="reference internal" href="../library/threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a>
|
|||
|
synchronization class for making multiple threads wait until all of them have
|
|||
|
reached a common barrier point. Barriers are useful for making sure that a task
|
|||
|
with multiple preconditions does not run until all of the predecessor tasks are
|
|||
|
complete.</p>
|
|||
|
<p>Barriers can work with an arbitrary number of threads. This is a generalization
|
|||
|
of a <a class="reference external" href="https://en.wikipedia.org/wiki/Synchronous_rendezvous">Rendezvous</a> which
|
|||
|
is defined for only two threads.</p>
|
|||
|
<p>Implemented as a two-phase cyclic barrier, <a class="reference internal" href="../library/threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a> objects
|
|||
|
are suitable for use in loops. The separate <em>filling</em> and <em>draining</em> phases
|
|||
|
assure that all threads get released (drained) before any one of them can loop
|
|||
|
back and re-enter the barrier. The barrier fully resets after each cycle.</p>
|
|||
|
<p>Example of using barriers:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">threading</span> <span class="k">import</span> <span class="n">Barrier</span><span class="p">,</span> <span class="n">Thread</span>
|
|||
|
|
|||
|
<span class="k">def</span> <span class="nf">get_votes</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
|
|||
|
<span class="n">ballots</span> <span class="o">=</span> <span class="n">conduct_election</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
|
|||
|
<span class="n">all_polls_closed</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="c1"># do not count until all polls are closed</span>
|
|||
|
<span class="n">totals</span> <span class="o">=</span> <span class="n">summarize</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
|
|||
|
<span class="n">publish</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">totals</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="n">all_polls_closed</span> <span class="o">=</span> <span class="n">Barrier</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sites</span><span class="p">))</span>
|
|||
|
<span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">:</span>
|
|||
|
<span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">get_votes</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">site</span><span class="p">,))</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>In this example, the barrier enforces a rule that votes cannot be counted at any
|
|||
|
polling site until all polls are closed. Notice how a solution with a barrier
|
|||
|
is similar to one with <a class="reference internal" href="../library/threading.html#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Thread.join()</span></code></a>, but the threads stay alive
|
|||
|
and continue to do work (summarizing ballots) after the barrier point is
|
|||
|
crossed.</p>
|
|||
|
<p>If any of the predecessor tasks can hang or be delayed, a barrier can be created
|
|||
|
with an optional <em>timeout</em> parameter. Then if the timeout period elapses before
|
|||
|
all the predecessor tasks reach the barrier point, all waiting threads are
|
|||
|
released and a <a class="reference internal" href="../library/threading.html#threading.BrokenBarrierError" title="threading.BrokenBarrierError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception is raised:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_votes</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
|
|||
|
<span class="n">ballots</span> <span class="o">=</span> <span class="n">conduct_election</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
|
|||
|
<span class="k">try</span><span class="p">:</span>
|
|||
|
<span class="n">all_polls_closed</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="n">midnight</span> <span class="o">-</span> <span class="n">time</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
|
|||
|
<span class="k">except</span> <span class="n">BrokenBarrierError</span><span class="p">:</span>
|
|||
|
<span class="n">lockbox</span> <span class="o">=</span> <span class="n">seal_ballots</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
|
|||
|
<span class="n">queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">lockbox</span><span class="p">)</span>
|
|||
|
<span class="k">else</span><span class="p">:</span>
|
|||
|
<span class="n">totals</span> <span class="o">=</span> <span class="n">summarize</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
|
|||
|
<span class="n">publish</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">totals</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>In this example, the barrier enforces a more robust rule. If some election
|
|||
|
sites do not finish before midnight, the barrier times-out and the ballots are
|
|||
|
sealed and deposited in a queue for later handling.</p>
|
|||
|
<p>See <a class="reference external" href="http://osl.cs.illinois.edu/media/papers/karmani-2009-barrier_synchronization_pattern.pdf">Barrier Synchronization Patterns</a>
|
|||
|
for more examples of how barriers can be used in parallel computing. Also, there is
|
|||
|
a simple but thorough explanation of barriers in <a class="reference external" href="https://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf">The Little Book of Semaphores</a>, <em>section 3.6</em>.</p>
|
|||
|
<p>(Contributed by Kristján Valur Jónsson with an API review by Jeffrey Yasskin in
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue8777">bpo-8777</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="datetime-and-time">
|
|||
|
<h3>datetime and time<a class="headerlink" href="#datetime-and-time" title="Permalink to this headline">¶</a></h3>
|
|||
|
<ul>
|
|||
|
<li><p>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 has a new type <a class="reference internal" href="../library/datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> that
|
|||
|
implements the <a class="reference internal" href="../library/datetime.html#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> interface by returning a fixed UTC
|
|||
|
offset and timezone name. This makes it easier to create timezone-aware
|
|||
|
datetime objects:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">datetime</span> <span class="k">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timezone</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
|
|||
|
<span class="go">datetime.datetime(2010, 12, 8, 21, 4, 2, 923754, tzinfo=datetime.timezone.utc)</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">"01/01/2000 12:00 +0000"</span><span class="p">,</span> <span class="s2">"%m/</span><span class="si">%d</span><span class="s2">/%Y %H:%M %z"</span><span class="p">)</span>
|
|||
|
<span class="go">datetime.datetime(2000, 1, 1, 12, 0, tzinfo=datetime.timezone.utc)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
<li><p>Also, <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> objects can now be multiplied by
|
|||
|
<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 divided by <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#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> objects.
|
|||
|
And <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> objects can now divide one another.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/datetime.html#datetime.date.strftime" title="datetime.date.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.date.strftime()</span></code></a> method is no longer restricted to years
|
|||
|
after 1900. The new supported year range is from 1000 to 9999 inclusive.</p></li>
|
|||
|
<li><p>Whenever a two-digit year is used in a time tuple, the interpretation has been
|
|||
|
governed by <code class="xref py py-attr docutils literal notranslate"><span class="pre">time.accept2dyear</span></code>. The default is <code class="docutils literal notranslate"><span class="pre">True</span></code> which means that
|
|||
|
for a two-digit year, the century is guessed according to the POSIX rules
|
|||
|
governing the <code class="docutils literal notranslate"><span class="pre">%y</span></code> strptime format.</p>
|
|||
|
<p>Starting with Py3.2, use of the century guessing heuristic will emit 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>. Instead, it is recommended that
|
|||
|
<code class="xref py py-attr docutils literal notranslate"><span class="pre">time.accept2dyear</span></code> be set to <code class="docutils literal notranslate"><span class="pre">False</span></code> so that large date ranges
|
|||
|
can be used without guesswork:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">warnings</span>
|
|||
|
<span class="gp">>>> </span><span class="n">warnings</span><span class="o">.</span><span class="n">resetwarnings</span><span class="p">()</span> <span class="c1"># remove the default warning filters</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="kc">True</span> <span class="c1"># guess whether 11 means 11 or 2011</span>
|
|||
|
<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">((</span><span class="mi">11</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="mi">12</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">56</span><span class="p">,</span> <span class="mi">4</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="go">Warning (from warnings module):</span>
|
|||
|
<span class="go"> ...</span>
|
|||
|
<span class="go">DeprecationWarning: Century info guessed for a 2-digit year.</span>
|
|||
|
<span class="go">'Fri Jan 1 12:34:56 2011'</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># use the full range of allowable dates</span>
|
|||
|
<span class="gp">>>> </span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">((</span><span class="mi">11</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="mi">12</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">56</span><span class="p">,</span> <span class="mi">4</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="go">'Fri Jan 1 12:34:56 11'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Several functions now have significantly expanded date ranges. When
|
|||
|
<code class="xref py py-attr docutils literal notranslate"><span class="pre">time.accept2dyear</span></code> is false, the <a class="reference internal" href="../library/time.html#time.asctime" title="time.asctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.asctime()</span></code></a> function will
|
|||
|
accept any year that fits in a C int, while the <a class="reference internal" href="../library/time.html#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.mktime()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> functions will accept the full range supported by the
|
|||
|
corresponding operating system functions.</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>(Contributed by Alexander Belopolsky and Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue1289118">bpo-1289118</a>,
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue5094">bpo-5094</a>, <a class="reference external" href="https://bugs.python.org/issue6641">bpo-6641</a>, <a class="reference external" href="https://bugs.python.org/issue2706">bpo-2706</a>, <a class="reference external" href="https://bugs.python.org/issue1777412">bpo-1777412</a>, <a class="reference external" href="https://bugs.python.org/issue8013">bpo-8013</a>,
|
|||
|
and <a class="reference external" href="https://bugs.python.org/issue10827">bpo-10827</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="math">
|
|||
|
<h3>math<a class="headerlink" href="#math" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">math</span></code></a> module has been updated with six new functions inspired by the
|
|||
|
C99 standard.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/math.html#math.isfinite" title="math.isfinite"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfinite()</span></code></a> function provides a reliable and fast way to detect
|
|||
|
special values. It returns <code class="docutils literal notranslate"><span class="pre">True</span></code> for regular numbers and <code class="docutils literal notranslate"><span class="pre">False</span></code> for <em>Nan</em> or
|
|||
|
<em>Infinity</em>:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">isfinite</span>
|
|||
|
<span class="gp">>>> </span><span class="p">[</span><span class="n">isfinite</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="mf">4.56</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s1">'Nan'</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="s1">'Inf'</span><span class="p">))]</span>
|
|||
|
<span class="go">[True, True, False, False]</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>The <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> function computes <code class="docutils literal notranslate"><span class="pre">e**x-1</span></code> for small values of <em>x</em>
|
|||
|
without incurring the loss of precision that usually accompanies the subtraction
|
|||
|
of nearly equal quantities:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">expm1</span>
|
|||
|
<span class="gp">>>> </span><span class="n">expm1</span><span class="p">(</span><span class="mf">0.013671875</span><span class="p">)</span> <span class="c1"># more accurate way to compute e**x-1 for a small x</span>
|
|||
|
<span class="go">0.013765762467652909</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>The <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> function computes a probability integral or <a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">Gaussian
|
|||
|
error function</a>. The
|
|||
|
complementary error function, <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>, is <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">-</span> <span class="pre">erf(x)</span></code>:</p>
|
|||
|
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">erf</span><span class="p">,</span> <span class="n">erfc</span><span class="p">,</span> <span class="n">sqrt</span>
|
|||
|
<span class="gp">>>> </span><span class="n">erf</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="c1"># portion of normal distribution within 1 standard deviation</span>
|
|||
|
<span class="go">0.682689492137086</span>
|
|||
|
<span class="gp">>>> </span><span class="n">erfc</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="c1"># portion of normal distribution outside 1 standard deviation</span>
|
|||
|
<span class="go">0.31731050786291404</span>
|
|||
|
<span class="gp">>>> </span><span class="n">erf</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="o">+</span> <span class="n">erfc</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span>
|
|||
|
<span class="go">1.0</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>The <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> function is a continuous extension of the factorial
|
|||
|
function. See <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">https://en.wikipedia.org/wiki/Gamma_function</a> for details. Because
|
|||
|
the function is related to factorials, it grows large even for small values of
|
|||
|
<em>x</em>, so there is also a <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> function for computing the natural
|
|||
|
logarithm of the gamma function:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">lgamma</span>
|
|||
|
<span class="gp">>>> </span><span class="n">gamma</span><span class="p">(</span><span class="mf">7.0</span><span class="p">)</span> <span class="c1"># six factorial</span>
|
|||
|
<span class="go">720.0</span>
|
|||
|
<span class="gp">>>> </span><span class="n">lgamma</span><span class="p">(</span><span class="mf">801.0</span><span class="p">)</span> <span class="c1"># log(800 factorial)</span>
|
|||
|
<span class="go">4551.950730698041</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Mark Dickinson.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="abc">
|
|||
|
<h3>abc<a class="headerlink" href="#abc" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module now supports <a class="reference internal" href="../library/abc.html#abc.abstractclassmethod" title="abc.abstractclassmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abstractclassmethod()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/abc.html#abc.abstractstaticmethod" title="abc.abstractstaticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abstractstaticmethod()</span></code></a>.</p>
|
|||
|
<p>These tools make it possible to define an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> that
|
|||
|
requires a particular <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> or <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> to be
|
|||
|
implemented:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Temperature</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
|
|||
|
<span class="nd">@abc</span><span class="o">.</span><span class="n">abstractclassmethod</span>
|
|||
|
<span class="k">def</span> <span class="nf">from_fahrenheit</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
|
|||
|
<span class="o">...</span>
|
|||
|
<span class="nd">@abc</span><span class="o">.</span><span class="n">abstractclassmethod</span>
|
|||
|
<span class="k">def</span> <span class="nf">from_celsius</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
|
|||
|
<span class="o">...</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Patch submitted by Daniel Urban; <a class="reference external" href="https://bugs.python.org/issue5867">bpo-5867</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="io">
|
|||
|
<h3>io<a class="headerlink" href="#io" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> has a new method, <a class="reference internal" href="../library/io.html#io.BytesIO.getbuffer" title="io.BytesIO.getbuffer"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getbuffer()</span></code></a>, which
|
|||
|
provides functionality similar to <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a>. It creates an editable
|
|||
|
view of the data without making a copy. The buffer’s random access and support
|
|||
|
for slice notation are well-suited to in-place editing:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">REC_LEN</span><span class="p">,</span> <span class="n">LOC_START</span><span class="p">,</span> <span class="n">LOC_LEN</span> <span class="o">=</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">11</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">record_number</span><span class="p">,</span> <span class="n">location</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="n">start</span> <span class="o">=</span> <span class="n">record_number</span> <span class="o">*</span> <span class="n">REC_LEN</span> <span class="o">+</span> <span class="n">LOC_START</span>
|
|||
|
<span class="gp">... </span> <span class="n">buffer</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="o">+</span><span class="n">LOC_LEN</span><span class="p">]</span> <span class="o">=</span> <span class="n">location</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="kn">import</span> <span class="nn">io</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">byte_stream</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span>
|
|||
|
<span class="gp">... </span> <span class="sa">b</span><span class="s1">'G3805 storeroom Main chassis '</span>
|
|||
|
<span class="gp">... </span> <span class="sa">b</span><span class="s1">'X7899 shipping Reserve cog '</span>
|
|||
|
<span class="gp">... </span> <span class="sa">b</span><span class="s1">'L6988 receiving Primary sprocket'</span>
|
|||
|
<span class="gp">... </span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">buffer</span> <span class="o">=</span> <span class="n">byte_stream</span><span class="o">.</span><span class="n">getbuffer</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'warehouse '</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'showroom '</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">byte_stream</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span>
|
|||
|
<span class="go">b'G3805 showroom Main chassis '</span>
|
|||
|
<span class="go">b'X7899 warehouse Reserve cog '</span>
|
|||
|
<span class="go">b'L6988 receiving Primary sprocket'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue5506">bpo-5506</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="reprlib">
|
|||
|
<h3>reprlib<a class="headerlink" href="#reprlib" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>When writing a <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> method for a custom container, it is easy to
|
|||
|
forget to handle the case where a member refers back to the container itself.
|
|||
|
Python’s builtin objects such as <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> handle
|
|||
|
self-reference by displaying “…” in the recursive part of the representation
|
|||
|
string.</p>
|
|||
|
<p>To help write such <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> methods, the <a class="reference internal" href="../library/reprlib.html#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code></a> module has a new
|
|||
|
decorator, <a class="reference internal" href="../library/reprlib.html#reprlib.recursive_repr" title="reprlib.recursive_repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">recursive_repr()</span></code></a>, for detecting recursive calls to
|
|||
|
<a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> and substituting a placeholder string instead:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="nd">@recursive_repr</span><span class="p">()</span>
|
|||
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="s1">'<'</span> <span class="o">+</span> <span class="s1">'|'</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span> <span class="o">+</span> <span class="s1">'>'</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">MyList</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
|
|||
|
<span class="go"><'a'|'b'|'c'|...|'x'></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue9826">bpo-9826</a> and <a class="reference external" href="https://bugs.python.org/issue9840">bpo-9840</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="logging">
|
|||
|
<h3>logging<a class="headerlink" href="#logging" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>In addition to dictionary-based configuration described above, 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> package has many other improvements.</p>
|
|||
|
<p>The logging documentation has been augmented by a <a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><span class="std std-ref">basic tutorial</span></a>, an <a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><span class="std std-ref">advanced tutorial</span></a>, and a <a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><span class="std std-ref">cookbook</span></a> of
|
|||
|
logging recipes. These documents are the fastest way to learn about logging.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">logging.basicConfig()</span></code></a> set-up function gained a <em>style</em> argument to
|
|||
|
support three different types of string formatting. It defaults to “%” for
|
|||
|
traditional %-formatting, can be set to “{” for the new <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> style, or
|
|||
|
can be set to “$” for the shell-style formatting provided by
|
|||
|
<a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a>. The following three configurations are equivalent:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">logging</span> <span class="k">import</span> <span class="n">basicConfig</span>
|
|||
|
<span class="gp">>>> </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'%'</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">"</span><span class="si">%(name)s</span><span class="s2"> -> </span><span class="si">%(levelname)s</span><span class="s2">: </span><span class="si">%(message)s</span><span class="s2">"</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'{'</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">"</span><span class="si">{name}</span><span class="s2"> -> </span><span class="si">{levelname}</span><span class="s2"> </span><span class="si">{message}</span><span class="s2">"</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">'$'</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">"$name -> $levelname: $message"</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>If no configuration is set-up before a logging event occurs, there is now a
|
|||
|
default configuration using a <a class="reference internal" href="../library/logging.handlers.html#logging.StreamHandler" title="logging.StreamHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamHandler</span></code></a> directed to
|
|||
|
<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> for events of <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> level or higher. Formerly, an
|
|||
|
event occurring before a configuration was set-up would either raise an
|
|||
|
exception or silently drop the event depending on the value of
|
|||
|
<code class="xref py py-attr docutils literal notranslate"><span class="pre">logging.raiseExceptions</span></code>. The new default handler is stored in
|
|||
|
<a class="reference internal" href="../library/logging.html#logging.lastResort" title="logging.lastResort"><code class="xref py py-attr docutils literal notranslate"><span class="pre">logging.lastResort</span></code></a>.</p>
|
|||
|
<p>The use of filters has been simplified. Instead of creating a
|
|||
|
<a class="reference internal" href="../library/logging.html#logging.Filter" title="logging.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code></a> object, the predicate can be any Python callable that
|
|||
|
returns <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
|
|||
|
<p>There were a number of other improvements that add flexibility and simplify
|
|||
|
configuration. See the module documentation for a full listing of changes in
|
|||
|
Python 3.2.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="csv">
|
|||
|
<h3>csv<a class="headerlink" href="#csv" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/csv.html#module-csv" title="csv: Write and read tabular data to and from delimited files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">csv</span></code></a> module now supports a new dialect, <a class="reference internal" href="../library/csv.html#csv.unix_dialect" title="csv.unix_dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">unix_dialect</span></code></a>,
|
|||
|
which applies quoting for all fields and a traditional Unix style with <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> as
|
|||
|
the line terminator. The registered dialect name is <code class="docutils literal notranslate"><span class="pre">unix</span></code>.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/csv.html#csv.DictWriter" title="csv.DictWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">csv.DictWriter</span></code></a> has a new method,
|
|||
|
<a class="reference internal" href="../library/csv.html#csv.DictWriter.writeheader" title="csv.DictWriter.writeheader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeheader()</span></code></a> for writing-out an initial row to document
|
|||
|
the field names:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">csv</span><span class="o">,</span> <span class="nn">sys</span>
|
|||
|
<span class="gp">>>> </span><span class="n">w</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">DictWriter</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="p">[</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'dept'</span><span class="p">],</span> <span class="n">dialect</span><span class="o">=</span><span class="s1">'unix'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">w</span><span class="o">.</span><span class="n">writeheader</span><span class="p">()</span>
|
|||
|
<span class="go">"name","dept"</span>
|
|||
|
<span class="gp">>>> </span><span class="n">w</span><span class="o">.</span><span class="n">writerows</span><span class="p">([</span>
|
|||
|
<span class="gp">... </span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'tom'</span><span class="p">,</span> <span class="s1">'dept'</span><span class="p">:</span> <span class="s1">'accounting'</span><span class="p">},</span>
|
|||
|
<span class="gp">... </span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'susan'</span><span class="p">,</span> <span class="s1">'dept'</span><span class="p">:</span> <span class="s1">'Salesl'</span><span class="p">}])</span>
|
|||
|
<span class="go">"tom","accounting"</span>
|
|||
|
<span class="go">"susan","sales"</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(New dialect suggested by Jay Talbot in <a class="reference external" href="https://bugs.python.org/issue5975">bpo-5975</a>, and the new method
|
|||
|
suggested by Ed Abraham in <a class="reference external" href="https://bugs.python.org/issue1537721">bpo-1537721</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="contextlib">
|
|||
|
<h3>contextlib<a class="headerlink" href="#contextlib" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>There is a new and slightly mind-blowing tool
|
|||
|
<a class="reference internal" href="../library/contextlib.html#contextlib.ContextDecorator" title="contextlib.ContextDecorator"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContextDecorator</span></code></a> that is helpful for creating a
|
|||
|
<a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> that does double duty as a function decorator.</p>
|
|||
|
<p>As a convenience, this new functionality is used by
|
|||
|
<a class="reference internal" href="../library/contextlib.html#contextlib.contextmanager" title="contextlib.contextmanager"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextmanager()</span></code></a> so that no extra effort is needed to support
|
|||
|
both roles.</p>
|
|||
|
<p>The basic idea is that both context managers and function decorators can be used
|
|||
|
for pre-action and post-action wrappers. Context managers wrap a group of
|
|||
|
statements using a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, and function decorators wrap a
|
|||
|
group of statements enclosed in a function. So, occasionally there is a need to
|
|||
|
write a pre-action or post-action wrapper that can be used in either role.</p>
|
|||
|
<p>For example, it is sometimes useful to wrap functions or groups of statements
|
|||
|
with a logger that can track the time of entry and time of exit. Rather than
|
|||
|
writing both a function decorator and a context manager for the task, the
|
|||
|
<a class="reference internal" href="../library/contextlib.html#contextlib.contextmanager" title="contextlib.contextmanager"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextmanager()</span></code></a> provides both capabilities in a single
|
|||
|
definition:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">contextlib</span> <span class="k">import</span> <span class="n">contextmanager</span>
|
|||
|
<span class="kn">import</span> <span class="nn">logging</span>
|
|||
|
|
|||
|
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
|
|||
|
|
|||
|
<span class="nd">@contextmanager</span>
|
|||
|
<span class="k">def</span> <span class="nf">track_entry_and_exit</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
|
|||
|
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Entering: </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
|||
|
<span class="k">yield</span>
|
|||
|
<span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">'Exiting: </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Formerly, this would have only been usable as a context manager:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">track_entry_and_exit</span><span class="p">(</span><span class="s1">'widget loader'</span><span class="p">):</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Some time consuming activity goes here'</span><span class="p">)</span>
|
|||
|
<span class="n">load_widget</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Now, it can be used as a decorator as well:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@track_entry_and_exit</span><span class="p">(</span><span class="s1">'widget loader'</span><span class="p">)</span>
|
|||
|
<span class="k">def</span> <span class="nf">activity</span><span class="p">():</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'Some time consuming activity goes here'</span><span class="p">)</span>
|
|||
|
<span class="n">load_widget</span><span class="p">()</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Trying to fulfill two roles at once places some limitations on the technique.
|
|||
|
Context managers normally have the flexibility to return an argument usable by
|
|||
|
a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, but there is no parallel for function decorators.</p>
|
|||
|
<p>In the above example, there is not a clean way for the <em>track_entry_and_exit</em>
|
|||
|
context manager to return a logging instance for use in the body of enclosed
|
|||
|
statements.</p>
|
|||
|
<p>(Contributed by Michael Foord in <a class="reference external" href="https://bugs.python.org/issue9110">bpo-9110</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="decimal-and-fractions">
|
|||
|
<h3>decimal and fractions<a class="headerlink" href="#decimal-and-fractions" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>Mark Dickinson crafted an elegant and efficient scheme for assuring that
|
|||
|
different numeric datatypes will have the same hash value whenever their actual
|
|||
|
values are equal (<a class="reference external" href="https://bugs.python.org/issue8188">bpo-8188</a>):</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="nb">hash</span><span class="p">(</span><span class="n">Fraction</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="o">==</span> <span class="nb">hash</span><span class="p">(</span><span class="mf">1.5</span><span class="p">)</span> <span class="o">==</span> \
|
|||
|
<span class="nb">hash</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">"1.5"</span><span class="p">))</span> <span class="o">==</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="mf">1.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Some of the hashing details are exposed through a new attribute,
|
|||
|
<a class="reference internal" href="../library/sys.html#sys.hash_info" title="sys.hash_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.hash_info</span></code></a>, which describes the bit width of the hash value, the
|
|||
|
prime modulus, the hash values for <em>infinity</em> and <em>nan</em>, and the multiplier
|
|||
|
used for the imaginary part of a number:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">hash_info</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>An early decision to limit the inter-operability of various numeric types has
|
|||
|
been relaxed. It is still unsupported (and ill-advised) to have implicit
|
|||
|
mixing in arithmetic expressions such as <code class="docutils literal notranslate"><span class="pre">Decimal('1.1')</span> <span class="pre">+</span> <span class="pre">float('1.1')</span></code>
|
|||
|
because the latter loses information in the process of constructing the binary
|
|||
|
float. However, since existing floating point value can be converted losslessly
|
|||
|
to either a decimal or rational representation, it makes sense to add them to
|
|||
|
the constructor and to support mixed-type comparisons.</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>The <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a> constructor now accepts <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> objects
|
|||
|
directly so there in no longer a need to use the <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>
|
|||
|
method (<a class="reference external" href="https://bugs.python.org/issue8257">bpo-8257</a>).</p></li>
|
|||
|
<li><p>Mixed type comparisons are now fully supported so that
|
|||
|
<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> objects can be directly compared with <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/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">fractions.Fraction</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue2531">bpo-2531</a> and <a class="reference external" href="https://bugs.python.org/issue8188">bpo-8188</a>).</p></li>
|
|||
|
</ul>
|
|||
|
<p>Similar changes were made to <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">fractions.Fraction</span></code></a> so that the
|
|||
|
<a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_float" title="fractions.Fraction.from_float"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_float()</span></code></a> and <a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_decimal" title="fractions.Fraction.from_decimal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_decimal()</span></code></a>
|
|||
|
methods are no longer needed (<a class="reference external" href="https://bugs.python.org/issue8294">bpo-8294</a>):</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">decimal</span> <span class="k">import</span> <span class="n">Decimal</span>
|
|||
|
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">fractions</span> <span class="k">import</span> <span class="n">Fraction</span>
|
|||
|
<span class="gp">>>> </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
|
|||
|
<span class="go">Decimal('1.100000000000000088817841970012523233890533447265625')</span>
|
|||
|
<span class="gp">>>> </span><span class="n">Fraction</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
|
|||
|
<span class="go">Fraction(2476979795053773, 2251799813685248)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Another useful change for the <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module is that the
|
|||
|
<code class="xref py py-attr docutils literal notranslate"><span class="pre">Context.clamp</span></code> attribute is now public. This is useful in creating
|
|||
|
contexts that correspond to the decimal interchange formats specified in IEEE
|
|||
|
754 (see <a class="reference external" href="https://bugs.python.org/issue8540">bpo-8540</a>).</p>
|
|||
|
<p>(Contributed by Mark Dickinson and Raymond Hettinger.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="ftp">
|
|||
|
<h3>ftp<a class="headerlink" href="#ftp" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">ftplib.FTP</span></code></a> class now supports the context management protocol to
|
|||
|
unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a> exceptions and to close the FTP
|
|||
|
connection when done:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="k">import</span> <span class="n">FTP</span>
|
|||
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">FTP</span><span class="p">(</span><span class="s2">"ftp1.at.proftpd.org"</span><span class="p">)</span> <span class="k">as</span> <span class="n">ftp</span><span class="p">:</span>
|
|||
|
<span class="go"> ftp.login()</span>
|
|||
|
<span class="go"> ftp.dir()</span>
|
|||
|
|
|||
|
<span class="go">'230 Anonymous login ok, restrictions apply.'</span>
|
|||
|
<span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .</span>
|
|||
|
<span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 ..</span>
|
|||
|
<span class="go">dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS</span>
|
|||
|
<span class="go">dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Other file-like objects such as <a class="reference internal" href="../library/mmap.html#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap.mmap</span></code></a> and <a class="reference internal" href="../library/fileinput.html#fileinput.input" title="fileinput.input"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileinput.input()</span></code></a>
|
|||
|
also grew auto-closing context managers:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">fileinput</span><span class="o">.</span><span class="n">input</span><span class="p">(</span><span class="n">files</span><span class="o">=</span><span class="p">(</span><span class="s1">'log1.txt'</span><span class="p">,</span> <span class="s1">'log2.txt'</span><span class="p">))</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
|
|||
|
<span class="n">process</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Tarek Ziadé and Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue4972">bpo-4972</a>, and
|
|||
|
by Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue8046">bpo-8046</a> and <a class="reference external" href="https://bugs.python.org/issue1286">bpo-1286</a>.)</p>
|
|||
|
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal notranslate"><span class="pre">FTP_TLS</span></code></a> class now accepts a <em>context</em> parameter, which is a
|
|||
|
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object allowing bundling SSL configuration options,
|
|||
|
certificates and private keys into a single (potentially long-lived) structure.</p>
|
|||
|
<p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue8806">bpo-8806</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="popen">
|
|||
|
<h3>popen<a class="headerlink" href="#popen" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a> and <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.Popen()</span></code></a> functions now support
|
|||
|
<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 for auto-closing of the file descriptors.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou and Brian Curtin in <a class="reference external" href="https://bugs.python.org/issue7461">bpo-7461</a> and
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue10554">bpo-10554</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="select">
|
|||
|
<h3>select<a class="headerlink" href="#select" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> module now exposes a new, constant attribute,
|
|||
|
<a class="reference internal" href="../library/select.html#select.PIPE_BUF" title="select.PIPE_BUF"><code class="xref py py-attr docutils literal notranslate"><span class="pre">PIPE_BUF</span></code></a>, which gives the minimum number of bytes which are
|
|||
|
guaranteed not to block when <a class="reference internal" href="../library/select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a> says a pipe is ready
|
|||
|
for writing.</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">select</span>
|
|||
|
<span class="gp">>>> </span><span class="n">select</span><span class="o">.</span><span class="n">PIPE_BUF</span> <span class="c1"># doctest: +SKIP</span>
|
|||
|
<span class="go">512</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Available on Unix systems. Patch by Sébastien Sablé in <a class="reference external" href="https://bugs.python.org/issue9862">bpo-9862</a>)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="gzip-and-zipfile">
|
|||
|
<h3>gzip and zipfile<a class="headerlink" href="#gzip-and-zipfile" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p><a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">gzip.GzipFile</span></code></a> 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>
|
|||
|
<a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> (except for <code class="docutils literal notranslate"><span class="pre">truncate()</span></code>). It also has a
|
|||
|
<a class="reference internal" href="../library/gzip.html#gzip.GzipFile.peek" title="gzip.GzipFile.peek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code></a> method and supports unseekable as well as
|
|||
|
zero-padded file objects.</p>
|
|||
|
<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 also gains the <a class="reference internal" href="../library/gzip.html#gzip.compress" title="gzip.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/gzip.html#gzip.decompress" title="gzip.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> functions for easier in-memory compression and
|
|||
|
decompression. Keep in mind that text needs to be encoded as <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>
|
|||
|
before compressing and decompressing:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">gzip</span>
|
|||
|
<span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s1">'Three shall be the number thou shalt count, '</span>
|
|||
|
<span class="gp">>>> </span><span class="n">s</span> <span class="o">+=</span> <span class="s1">'and the number of the counting shall be three'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span> <span class="c1"># convert to utf-8</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
|||
|
<span class="go">89</span>
|
|||
|
<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">gzip</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
|
|||
|
<span class="go">77</span>
|
|||
|
<span class="gp">>>> </span><span class="n">gzip</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">()[:</span><span class="mi">42</span><span class="p">]</span> <span class="c1"># decompress and convert to text</span>
|
|||
|
<span class="go">'Three shall be the number thou shalt count'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Anand B. Pillai in <a class="reference external" href="https://bugs.python.org/issue3488">bpo-3488</a>; and by Antoine Pitrou, Nir
|
|||
|
Aides and Brian Curtin in <a class="reference external" href="https://bugs.python.org/issue9962">bpo-9962</a>, <a class="reference external" href="https://bugs.python.org/issue1675951">bpo-1675951</a>, <a class="reference external" href="https://bugs.python.org/issue7471">bpo-7471</a> and
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue2846">bpo-2846</a>.)</p>
|
|||
|
<p>Also, the <code class="xref py py-class docutils literal notranslate"><span class="pre">zipfile.ZipExtFile</span></code> class was reworked internally to represent
|
|||
|
files stored inside an archive. The new implementation is significantly faster
|
|||
|
and can be wrapped in an <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> object for more speedups. It
|
|||
|
also solves an issue where interleaved calls to <em>read</em> and <em>readline</em> gave the
|
|||
|
wrong results.</p>
|
|||
|
<p>(Patch submitted by Nir Aides in <a class="reference external" href="https://bugs.python.org/issue7610">bpo-7610</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="tarfile">
|
|||
|
<h3>tarfile<a class="headerlink" href="#tarfile" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>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 can now be used as a context manager. In
|
|||
|
addition, its <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> method has a new option, <em>filter</em>,
|
|||
|
that controls which files are added to the archive and allows the file metadata
|
|||
|
to be edited.</p>
|
|||
|
<p>The new <em>filter</em> option replaces the older, less flexible <em>exclude</em> parameter
|
|||
|
which is now deprecated. If specified, the optional <em>filter</em> parameter needs to
|
|||
|
be a <a class="reference internal" href="../glossary.html#term-keyword-argument"><span class="xref std std-term">keyword argument</span></a>. The user-supplied filter function accepts a
|
|||
|
<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> object and returns an updated
|
|||
|
<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> object, or if it wants the file to be excluded, the
|
|||
|
function can return <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">tarfile</span><span class="o">,</span> <span class="nn">glob</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">myfilter</span><span class="p">(</span><span class="n">tarinfo</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="k">if</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isfile</span><span class="p">():</span> <span class="c1"># only save real files</span>
|
|||
|
<span class="gp">... </span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">uname</span> <span class="o">=</span> <span class="s1">'monty'</span> <span class="c1"># redact the user name</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="n">tarinfo</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="k">with</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'myarchive.tar.gz'</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">'w:gz'</span><span class="p">)</span> <span class="k">as</span> <span class="n">tf</span><span class="p">:</span>
|
|||
|
<span class="gp">... </span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">'*.txt'</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="n">tf</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">myfilter</span><span class="p">)</span>
|
|||
|
<span class="gp">... </span> <span class="n">tf</span><span class="o">.</span><span class="n">list</span><span class="p">()</span>
|
|||
|
<span class="go">-rw-r--r-- monty/501 902 2011-01-26 17:59:11 annotations.txt</span>
|
|||
|
<span class="go">-rw-r--r-- monty/501 123 2011-01-26 17:59:11 general_questions.txt</span>
|
|||
|
<span class="go">-rw-r--r-- monty/501 3514 2011-01-26 17:59:11 prion.txt</span>
|
|||
|
<span class="go">-rw-r--r-- monty/501 124 2011-01-26 17:59:11 py_todo.txt</span>
|
|||
|
<span class="go">-rw-r--r-- monty/501 1399 2011-01-26 17:59:11 semaphore_notes.txt</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Proposed by Tarek Ziadé and implemented by Lars Gustäbel in <a class="reference external" href="https://bugs.python.org/issue6856">bpo-6856</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="hashlib">
|
|||
|
<h3>hashlib<a class="headerlink" href="#hashlib" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>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 has two new constant attributes listing the hashing
|
|||
|
algorithms guaranteed to be present in all implementations and those available
|
|||
|
on the current implementation:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">hashlib</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_guaranteed</span>
|
|||
|
<span class="go">{'sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5'}</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_available</span>
|
|||
|
<span class="go">{'md2', 'SHA256', 'SHA512', 'dsaWithSHA', 'mdc2', 'SHA224', 'MD4', 'sha256',</span>
|
|||
|
<span class="go">'sha512', 'ripemd160', 'SHA1', 'MDC2', 'SHA', 'SHA384', 'MD2',</span>
|
|||
|
<span class="go">'ecdsa-with-SHA1','md4', 'md5', 'sha1', 'DSA-SHA', 'sha224',</span>
|
|||
|
<span class="go">'dsaEncryption', 'DSA', 'RIPEMD160', 'sha', 'MD5', 'sha384'}</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Suggested by Carl Chenet in <a class="reference external" href="https://bugs.python.org/issue7418">bpo-7418</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="ast">
|
|||
|
<h3>ast<a class="headerlink" href="#ast" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module has a wonderful a general-purpose tool for safely
|
|||
|
evaluating expression strings using the Python literal
|
|||
|
syntax. The <a class="reference internal" href="../library/ast.html#ast.literal_eval" title="ast.literal_eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.literal_eval()</span></code></a> function serves as a secure alternative to
|
|||
|
the builtin <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> function which is easily abused. Python 3.2 adds
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> literals to the list of supported types:
|
|||
|
strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">ast</span> <span class="k">import</span> <span class="n">literal_eval</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">request</span> <span class="o">=</span> <span class="s2">"{'req': 3, 'func': 'pow', 'args': (2, 0.5)}"</span>
|
|||
|
<span class="gp">>>> </span><span class="n">literal_eval</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
|||
|
<span class="go">{'args': (2, 0.5), 'req': 3, 'func': 'pow'}</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">request</span> <span class="o">=</span> <span class="s2">"os.system('do something harmful')"</span>
|
|||
|
<span class="gp">>>> </span><span class="n">literal_eval</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
|||
|
<span class="gt">Traceback (most recent call last):</span>
|
|||
|
<span class="c">...</span>
|
|||
|
<span class="gr">ValueError</span>: <span class="n">malformed node or string: <_ast.Call object at 0x101739a10></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Implemented by Benjamin Peterson and Georg Brandl.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="os">
|
|||
|
<h3>os<a class="headerlink" href="#os" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>Different operating systems use various encodings for filenames and environment
|
|||
|
variables. 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 provides two new functions,
|
|||
|
<a class="reference internal" href="../library/os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsencode()</span></code></a> and <a class="reference internal" href="../library/os.html#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsdecode()</span></code></a>, for encoding and decoding
|
|||
|
filenames:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span>
|
|||
|
<span class="gp">>>> </span><span class="n">filename</span> <span class="o">=</span> <span class="s1">'Sehenswürdigkeiten'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">fsencode</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
|
|||
|
<span class="go">b'Sehensw\xc3\xbcrdigkeiten'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Some operating systems allow direct access to encoded bytes in the
|
|||
|
environment. If so, the <a class="reference internal" href="../library/os.html#os.supports_bytes_environ" title="os.supports_bytes_environ"><code class="xref py py-attr docutils literal notranslate"><span class="pre">os.supports_bytes_environ</span></code></a> constant will be
|
|||
|
true.</p>
|
|||
|
<p>For direct access to encoded environment variables (if available),
|
|||
|
use the new <a class="reference internal" href="../library/os.html#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getenvb()</span></code></a> function or use <a class="reference internal" href="../library/os.html#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a>
|
|||
|
which is a bytes version of <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>.</p>
|
|||
|
<p>(Contributed by Victor Stinner.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="shutil">
|
|||
|
<h3>shutil<a class="headerlink" href="#shutil" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copytree()</span></code></a> function has two new options:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><em>ignore_dangling_symlinks</em>: when <code class="docutils literal notranslate"><span class="pre">symlinks=False</span></code> so that the function
|
|||
|
copies a file pointed to by a symlink, not the symlink itself. This option
|
|||
|
will silence the error raised if the file doesn’t exist.</p></li>
|
|||
|
<li><p><em>copy_function</em>: is a callable that will be used to copy files.
|
|||
|
<a class="reference internal" href="../library/shutil.html#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a> is used by default.</p></li>
|
|||
|
</ul>
|
|||
|
<p>(Contributed by Tarek Ziadé.)</p>
|
|||
|
<p>In addition, 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 now supports <a class="reference internal" href="../library/shutil.html#archiving-operations"><span class="std std-ref">archiving operations</span></a> for zipfiles, uncompressed tarfiles, gzipped tarfiles,
|
|||
|
and bzipped tarfiles. And there are functions for registering additional
|
|||
|
archiving file formats (such as xz compressed tarfiles or custom formats).</p>
|
|||
|
<p>The principal functions are <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> and
|
|||
|
<a class="reference internal" href="../library/shutil.html#shutil.unpack_archive" title="shutil.unpack_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_archive()</span></code></a>. By default, both operate on the current
|
|||
|
directory (which can be set by <a class="reference internal" href="../library/os.html#os.chdir" title="os.chdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chdir()</span></code></a>) and on any sub-directories.
|
|||
|
The archive filename needs to be specified with a full pathname. The archiving
|
|||
|
step is non-destructive (the original files are left unchanged).</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">pprint</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">'mydata'</span><span class="p">)</span> <span class="c1"># change to the source directory</span>
|
|||
|
<span class="gp">>>> </span><span class="n">f</span> <span class="o">=</span> <span class="n">shutil</span><span class="o">.</span><span class="n">make_archive</span><span class="p">(</span><span class="s1">'/var/backup/mydata'</span><span class="p">,</span>
|
|||
|
<span class="gp">... </span> <span class="s1">'zip'</span><span class="p">)</span> <span class="c1"># archive the current directory</span>
|
|||
|
<span class="gp">>>> </span><span class="n">f</span> <span class="c1"># show the name of archive</span>
|
|||
|
<span class="go">'/var/backup/mydata.zip'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">'tmp'</span><span class="p">)</span> <span class="c1"># change to an unpacking</span>
|
|||
|
<span class="gp">>>> </span><span class="n">shutil</span><span class="o">.</span><span class="n">unpack_archive</span><span class="p">(</span><span class="s1">'/var/backup/mydata.zip'</span><span class="p">)</span> <span class="c1"># recover the data</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">get_archive_formats</span><span class="p">())</span> <span class="c1"># display known formats</span>
|
|||
|
<span class="go">[('bztar', "bzip2'ed tar-file"),</span>
|
|||
|
<span class="go"> ('gztar', "gzip'ed tar-file"),</span>
|
|||
|
<span class="go"> ('tar', 'uncompressed tar file'),</span>
|
|||
|
<span class="go"> ('zip', 'ZIP file')]</span>
|
|||
|
|
|||
|
<span class="gp">>>> </span><span class="n">shutil</span><span class="o">.</span><span class="n">register_archive_format</span><span class="p">(</span> <span class="c1"># register a new archive format</span>
|
|||
|
<span class="gp">... </span> <span class="n">name</span><span class="o">=</span><span class="s1">'xz'</span><span class="p">,</span>
|
|||
|
<span class="gp">... </span> <span class="n">function</span><span class="o">=</span><span class="n">xz</span><span class="o">.</span><span class="n">compress</span><span class="p">,</span> <span class="c1"># callable archiving function</span>
|
|||
|
<span class="gp">... </span> <span class="n">extra_args</span><span class="o">=</span><span class="p">[(</span><span class="s1">'level'</span><span class="p">,</span> <span class="mi">8</span><span class="p">)],</span> <span class="c1"># arguments to the function</span>
|
|||
|
<span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="s1">'xz compression'</span>
|
|||
|
<span class="gp">... </span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Tarek Ziadé.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="sqlite3">
|
|||
|
<h3>sqlite3<a class="headerlink" href="#sqlite3" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>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 was updated to pysqlite version 2.6.0. It has two new capabilities.</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">sqlite3.Connection.in_transit</span></code> attribute is true if there is an
|
|||
|
active transaction for uncommitted changes.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.enable_load_extension" title="sqlite3.Connection.enable_load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sqlite3.Connection.enable_load_extension()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.load_extension" title="sqlite3.Connection.load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sqlite3.Connection.load_extension()</span></code></a> methods allows you to load SQLite
|
|||
|
extensions from “.so” files. One well-known extension is the fulltext-search
|
|||
|
extension distributed with SQLite.</p></li>
|
|||
|
</ul>
|
|||
|
<p>(Contributed by R. David Murray and Shashwat Anand; <a class="reference external" href="https://bugs.python.org/issue8845">bpo-8845</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="html">
|
|||
|
<h3>html<a class="headerlink" href="#html" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>A new <a class="reference internal" href="../library/html.html#module-html" title="html: Helpers for manipulating HTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html</span></code></a> module was introduced with only a single function,
|
|||
|
<a class="reference internal" href="../library/html.html#html.escape" title="html.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">escape()</span></code></a>, which is used for escaping reserved characters from HTML
|
|||
|
markup:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">html</span>
|
|||
|
<span class="gp">>>> </span><span class="n">html</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="s1">'x > 2 && x < 7'</span><span class="p">)</span>
|
|||
|
<span class="go">'x &gt; 2 &amp;&amp; x &lt; 7'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="socket">
|
|||
|
<h3>socket<a class="headerlink" href="#socket" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> module has two new improvements.</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>Socket objects now have a <a class="reference internal" href="../library/socket.html#socket.socket.detach" title="socket.socket.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a> method which puts
|
|||
|
the socket into closed state without actually closing the underlying file
|
|||
|
descriptor. The latter can then be reused for other purposes.
|
|||
|
(Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8524">bpo-8524</a>.)</p></li>
|
|||
|
<li><p><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">socket.create_connection()</span></code></a> now supports the context management protocol
|
|||
|
to unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a> exceptions and to close the
|
|||
|
socket when done.
|
|||
|
(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue9794">bpo-9794</a>.)</p></li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="ssl">
|
|||
|
<h3>ssl<a class="headerlink" href="#ssl" title="Permalink to this headline">¶</a></h3>
|
|||
|
<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 added a number of features to satisfy common requirements
|
|||
|
for secure (encrypted, authenticated) internet connections:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>A new class, <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>, serves as a container for persistent
|
|||
|
SSL data, such as protocol settings, certificates, private keys, and various
|
|||
|
other options. It includes a <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_socket()</span></code></a> for creating
|
|||
|
an SSL socket from an SSL context.</p></li>
|
|||
|
<li><p>A new function, <a class="reference internal" href="../library/ssl.html#ssl.match_hostname" title="ssl.match_hostname"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.match_hostname()</span></code></a>, supports server identity
|
|||
|
verification for higher-level protocols by implementing the rules of HTTPS
|
|||
|
(from <span class="target" id="index-12"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2818.html"><strong>RFC 2818</strong></a>) which are also suitable for other protocols.</p></li>
|
|||
|
<li><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. The <em>ciphers</em> string lists the allowed encryption algorithms using
|
|||
|
the format described in the <a class="reference external" href="https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL documentation</a>.</p></li>
|
|||
|
<li><p>When linked against recent versions of OpenSSL, the <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> module now
|
|||
|
supports the Server Name Indication extension to the TLS protocol, allowing
|
|||
|
multiple “virtual hosts” using different certificates on a single IP port.
|
|||
|
This extension is only supported in client mode, and is activated by passing
|
|||
|
the <em>server_hostname</em> argument to <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.wrap_socket()</span></code></a>.</p></li>
|
|||
|
<li><p>Various options have been added to 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, such as
|
|||
|
<a class="reference internal" href="../library/ssl.html#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a> which disables the insecure and obsolete SSLv2
|
|||
|
protocol.</p></li>
|
|||
|
<li><p>The extension now loads all the OpenSSL ciphers and digest algorithms. If
|
|||
|
some SSL certificates cannot be verified, they are reported as an “unknown
|
|||
|
algorithm” error.</p></li>
|
|||
|
<li><p>The version of OpenSSL being used is now accessible using 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).</p></li>
|
|||
|
</ul>
|
|||
|
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue8850">bpo-8850</a>, <a class="reference external" href="https://bugs.python.org/issue1589">bpo-1589</a>, <a class="reference external" href="https://bugs.python.org/issue8322">bpo-8322</a>,
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue5639">bpo-5639</a>, <a class="reference external" href="https://bugs.python.org/issue4870">bpo-4870</a>, <a class="reference external" href="https://bugs.python.org/issue8484">bpo-8484</a>, and <a class="reference external" href="https://bugs.python.org/issue8321">bpo-8321</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="nntp">
|
|||
|
<h3>nntp<a class="headerlink" href="#nntp" title="Permalink to this headline">¶</a></h3>
|
|||
|
<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 has a revamped implementation with better bytes and
|
|||
|
text semantics as well as more practical APIs. These improvements break
|
|||
|
compatibility with the nntplib version in Python 3.1, which was partly
|
|||
|
dysfunctional in itself.</p>
|
|||
|
<p>Support for secure connections through both implicit (using
|
|||
|
<a class="reference internal" href="../library/nntplib.html#nntplib.NNTP_SSL" title="nntplib.NNTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">nntplib.NNTP_SSL</span></code></a>) and explicit (using <a class="reference internal" href="../library/nntplib.html#nntplib.NNTP.starttls" title="nntplib.NNTP.starttls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">nntplib.NNTP.starttls()</span></code></a>)
|
|||
|
TLS has also been added.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue9360">bpo-9360</a> and Andrew Vant in <a class="reference external" href="https://bugs.python.org/issue1926">bpo-1926</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="certificates">
|
|||
|
<h3>certificates<a class="headerlink" href="#certificates" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p><a class="reference internal" href="../library/http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPSConnection</span></code></a>, <a class="reference internal" href="../library/urllib.request.html#urllib.request.HTTPSHandler" title="urllib.request.HTTPSHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.request.HTTPSHandler</span></code></a>
|
|||
|
and <a class="reference internal" href="../library/urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.request.urlopen()</span></code></a> now take optional arguments to allow for
|
|||
|
server certificate checking against a set of Certificate Authorities,
|
|||
|
as recommended in public uses of HTTPS.</p>
|
|||
|
<p>(Added by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue9003">bpo-9003</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="imaplib">
|
|||
|
<h3>imaplib<a class="headerlink" href="#imaplib" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>Support for explicit TLS on standard IMAP4 connections has been added through
|
|||
|
the new <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4.starttls" title="imaplib.IMAP4.starttls"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib.IMAP4.starttls</span></code></a> method.</p>
|
|||
|
<p>(Contributed by Lorenzo M. Catucci and Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue4471">bpo-4471</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="http-client">
|
|||
|
<h3>http.client<a class="headerlink" href="#http-client" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>There were a number of small API improvements in the <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> module.
|
|||
|
The old-style HTTP 0.9 simple responses are no longer supported and the <em>strict</em>
|
|||
|
parameter is deprecated in all classes.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a> classes now have a <em>source_address</em>
|
|||
|
parameter for a (host, port) tuple indicating where the HTTP connection is made
|
|||
|
from.</p>
|
|||
|
<p>Support for certificate checking and HTTPS virtual hosts were added to
|
|||
|
<a class="reference internal" href="../library/http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a>.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.request" title="http.client.HTTPConnection.request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">request()</span></code></a> method on connection objects
|
|||
|
allowed an optional <em>body</em> argument so that a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> could be used
|
|||
|
to supply the content of the request. Conveniently, the <em>body</em> argument now
|
|||
|
also accepts an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> object so long as it includes an explicit
|
|||
|
<code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> header. This extended interface is much more flexible than
|
|||
|
before.</p>
|
|||
|
<p>To establish an HTTPS connection through a proxy server, there is a new
|
|||
|
<a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.set_tunnel" title="http.client.HTTPConnection.set_tunnel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_tunnel()</span></code></a> method that sets the host and
|
|||
|
port for HTTP Connect tunneling.</p>
|
|||
|
<p>To match the behavior of <a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a>, the HTTP client library now also
|
|||
|
encodes headers with ISO-8859-1 (Latin-1) encoding. It was already doing that
|
|||
|
for incoming headers, so now the behavior is consistent for both incoming and
|
|||
|
outgoing traffic. (See work by Armin Ronacher in <a class="reference external" href="https://bugs.python.org/issue10980">bpo-10980</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="unittest">
|
|||
|
<h3>unittest<a class="headerlink" href="#unittest" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The unittest module has a number of improvements supporting test discovery for
|
|||
|
packages, easier experimentation at the interactive prompt, new testcase
|
|||
|
methods, improved diagnostic messages for test failures, and better method
|
|||
|
names.</p>
|
|||
|
<ul>
|
|||
|
<li><p>The command-line call <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span></code> can now accept file paths
|
|||
|
instead of module names for running specific tests (<a class="reference external" href="https://bugs.python.org/issue10620">bpo-10620</a>). The new
|
|||
|
test discovery can find tests within packages, locating any test importable
|
|||
|
from the top-level directory. The top-level directory can be specified with
|
|||
|
the <cite>-t</cite> option, a pattern for matching files with <code class="docutils literal notranslate"><span class="pre">-p</span></code>, and a directory to
|
|||
|
start discovery with <code class="docutils literal notranslate"><span class="pre">-s</span></code>:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m unittest discover -s my_proj_dir -p _test.py
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Michael Foord.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Experimentation at the interactive prompt is now easier because the
|
|||
|
<code class="xref py py-class docutils literal notranslate"><span class="pre">unittest.case.TestCase</span></code> class can now be instantiated without
|
|||
|
arguments:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">unittest</span> <span class="k">import</span> <span class="n">TestCase</span>
|
|||
|
<span class="gp">>>> </span><span class="n">TestCase</span><span class="p">()</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="nb">pow</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">8</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Michael Foord.)</p>
|
|||
|
</li>
|
|||
|
<li><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 has two new methods,
|
|||
|
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarnsRegex()</span></code></a> to verify that a given warning type
|
|||
|
is triggered by the code under test:</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">assertWarns</span><span class="p">(</span><span class="ne">DeprecationWarning</span><span class="p">):</span>
|
|||
|
<span class="n">legacy_function</span><span class="p">(</span><span class="s1">'XYZ'</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue9754">bpo-9754</a>.)</p>
|
|||
|
<p>Another new method, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertCountEqual" title="unittest.TestCase.assertCountEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertCountEqual()</span></code></a> is used to
|
|||
|
compare two iterables to determine if their element counts are equal (whether
|
|||
|
the same elements are present with the same number of occurrences regardless
|
|||
|
of order):</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_anagram</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="bp">self</span><span class="o">.</span><span class="n">assertCountEqual</span><span class="p">(</span><span class="s1">'algorithm'</span><span class="p">,</span> <span class="s1">'logarithm'</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
<li><p>A principal feature of the unittest module is an effort to produce meaningful
|
|||
|
diagnostics when a test fails. When possible, the failure is recorded along
|
|||
|
with a diff of the output. This is especially helpful for analyzing log files
|
|||
|
of failed test runs. However, since diffs can sometime be voluminous, there is
|
|||
|
a new <a class="reference internal" href="../library/unittest.html#unittest.TestCase.maxDiff" title="unittest.TestCase.maxDiff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxDiff</span></code></a> attribute that sets maximum length of
|
|||
|
diffs displayed.</p></li>
|
|||
|
<li><p>In addition, the method names in the module have undergone a number of clean-ups.</p>
|
|||
|
<p>For example, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRegex" title="unittest.TestCase.assertRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegex()</span></code></a> is the new name for
|
|||
|
<code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code> which was misnamed because the
|
|||
|
test uses <a class="reference internal" href="../library/re.html#re.search" title="re.search"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.search()</span></code></a>, not <a class="reference internal" href="../library/re.html#re.match" title="re.match"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.match()</span></code></a>. Other methods using
|
|||
|
regular expressions are now named using short form “Regex” in preference to
|
|||
|
“Regexp” – this matches the names used in other unittest implementations,
|
|||
|
matches Python’s old name for 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, and it has unambiguous
|
|||
|
camel-casing.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger and implemented by Ezio Melotti.)</p>
|
|||
|
</li>
|
|||
|
<li><p>To improve consistency, some long-standing method aliases are being
|
|||
|
deprecated in favor of the preferred names:</p>
|
|||
|
<blockquote>
|
|||
|
<div><table class="docutils align-center">
|
|||
|
<colgroup>
|
|||
|
<col style="width: 51%" />
|
|||
|
<col style="width: 49%" />
|
|||
|
</colgroup>
|
|||
|
<thead>
|
|||
|
<tr class="row-odd"><th class="head"><p>Old Name</p></th>
|
|||
|
<th class="head"><p>Preferred Name</p></th>
|
|||
|
</tr>
|
|||
|
</thead>
|
|||
|
<tbody>
|
|||
|
<tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assert_()</span></code></p></td>
|
|||
|
<td><p><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></p></td>
|
|||
|
</tr>
|
|||
|
<tr class="row-odd"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEquals()</span></code></p></td>
|
|||
|
<td><p><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></td>
|
|||
|
</tr>
|
|||
|
<tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEquals()</span></code></p></td>
|
|||
|
<td><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEqual()</span></code></a></p></td>
|
|||
|
</tr>
|
|||
|
<tr class="row-odd"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEquals()</span></code></p></td>
|
|||
|
<td><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></p></td>
|
|||
|
</tr>
|
|||
|
<tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEquals()</span></code></p></td>
|
|||
|
<td><p><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></p></td>
|
|||
|
</tr>
|
|||
|
</tbody>
|
|||
|
</table>
|
|||
|
</div></blockquote>
|
|||
|
<p>Likewise, the <code class="docutils literal notranslate"><span class="pre">TestCase.fail*</span></code> methods deprecated in Python 3.1 are expected
|
|||
|
to be removed in Python 3.3. Also see the <a class="reference internal" href="../library/unittest.html#deprecated-aliases"><span class="std std-ref">Deprecated aliases</span></a> section in
|
|||
|
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> documentation.</p>
|
|||
|
<p>(Contributed by Ezio Melotti; <a class="reference external" href="https://bugs.python.org/issue9424">bpo-9424</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictContainsSubset()</span></code> method was deprecated
|
|||
|
because it was misimplemented with the arguments in the wrong order. This
|
|||
|
created hard-to-debug optical illusions where tests like
|
|||
|
<code class="docutils literal notranslate"><span class="pre">TestCase().assertDictContainsSubset({'a':1,</span> <span class="pre">'b':2},</span> <span class="pre">{'a':1})</span></code> would fail.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="random">
|
|||
|
<h3>random<a class="headerlink" href="#random" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The integer methods in the <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> module now do a better job of producing
|
|||
|
uniform distributions. Previously, they computed selections with
|
|||
|
<code class="docutils literal notranslate"><span class="pre">int(n*random())</span></code> which had a slight bias whenever <em>n</em> was not a power of two.
|
|||
|
Now, multiple selections are made from a range up to the next power of two and a
|
|||
|
selection is kept only when it falls within the range <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">x</span> <span class="pre"><</span> <span class="pre">n</span></code>. The
|
|||
|
functions and methods affected are <a class="reference internal" href="../library/random.html#random.randrange" title="random.randrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">randrange()</span></code></a>,
|
|||
|
<a class="reference internal" href="../library/random.html#random.randint" title="random.randint"><code class="xref py py-func docutils literal notranslate"><span class="pre">randint()</span></code></a>, <a class="reference internal" href="../library/random.html#random.choice" title="random.choice"><code class="xref py py-func docutils literal notranslate"><span class="pre">choice()</span></code></a>, <a class="reference internal" href="../library/random.html#random.shuffle" title="random.shuffle"><code class="xref py py-func docutils literal notranslate"><span class="pre">shuffle()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/random.html#random.sample" title="random.sample"><code class="xref py py-func docutils literal notranslate"><span class="pre">sample()</span></code></a>.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue9025">bpo-9025</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="poplib">
|
|||
|
<h3>poplib<a class="headerlink" href="#poplib" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p><a class="reference internal" href="../library/poplib.html#poplib.POP3_SSL" title="poplib.POP3_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">POP3_SSL</span></code></a> class now accepts a <em>context</em> parameter, which is a
|
|||
|
<a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> object allowing bundling SSL configuration options,
|
|||
|
certificates and private keys into a single (potentially long-lived)
|
|||
|
structure.</p>
|
|||
|
<p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue8807">bpo-8807</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="asyncore">
|
|||
|
<h3>asyncore<a class="headerlink" href="#asyncore" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p><a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncore.dispatcher</span></code></a> now provides a
|
|||
|
<a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher.handle_accepted" title="asyncore.dispatcher.handle_accepted"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_accepted()</span></code></a> method
|
|||
|
returning a <cite>(sock, addr)</cite> pair which is called when a connection has actually
|
|||
|
been established with a new remote endpoint. This is supposed to be used as a
|
|||
|
replacement for old <a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher.handle_accept" title="asyncore.dispatcher.handle_accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_accept()</span></code></a> and avoids
|
|||
|
the user to call <a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher.accept" title="asyncore.dispatcher.accept"><code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code></a> directly.</p>
|
|||
|
<p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue6706">bpo-6706</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="tempfile">
|
|||
|
<h3>tempfile<a class="headerlink" href="#tempfile" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a> module has a new context manager,
|
|||
|
<a class="reference internal" href="../library/tempfile.html#tempfile.TemporaryDirectory" title="tempfile.TemporaryDirectory"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemporaryDirectory</span></code></a> which provides easy deterministic
|
|||
|
cleanup of temporary directories:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryDirectory</span><span class="p">()</span> <span class="k">as</span> <span class="n">tmpdirname</span><span class="p">:</span>
|
|||
|
<span class="nb">print</span><span class="p">(</span><span class="s1">'created temporary dir:'</span><span class="p">,</span> <span class="n">tmpdirname</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Neil Schemenauer and Nick Coghlan; <a class="reference external" href="https://bugs.python.org/issue5178">bpo-5178</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="inspect">
|
|||
|
<h3>inspect<a class="headerlink" href="#inspect" title="Permalink to this headline">¶</a></h3>
|
|||
|
<ul>
|
|||
|
<li><p>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 has a new function
|
|||
|
<a class="reference internal" href="../library/inspect.html#inspect.getgeneratorstate" title="inspect.getgeneratorstate"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorstate()</span></code></a> to easily identify the current state of a
|
|||
|
generator-iterator:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getgeneratorstate</span>
|
|||
|
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
|
|||
|
<span class="gp">... </span> <span class="k">yield</span> <span class="s1">'demo'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">g</span> <span class="o">=</span> <span class="n">gen</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
|
|||
|
<span class="go">'GEN_CREATED'</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
|
|||
|
<span class="go">'demo'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
|
|||
|
<span class="go">'GEN_SUSPENDED'</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
|
|||
|
<span class="go">'GEN_CLOSED'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Rodolpho Eckhardt and Nick Coghlan, <a class="reference external" href="https://bugs.python.org/issue10220">bpo-10220</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>To support lookups without the possibility of activating a dynamic attribute,
|
|||
|
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 has a new function, <a class="reference internal" href="../library/inspect.html#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a>.
|
|||
|
Unlike <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a>, this is a true read-only search, guaranteed not to
|
|||
|
change state while it is searching:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
|
|||
|
<span class="gp">... </span> <span class="nd">@property</span>
|
|||
|
<span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
|||
|
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">'Running'</span><span class="p">)</span>
|
|||
|
<span class="gp">... </span> <span class="k">return</span> <span class="mi">10</span>
|
|||
|
<span class="gp">...</span>
|
|||
|
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="nb">getattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'f'</span><span class="p">)</span>
|
|||
|
<span class="go">Running</span>
|
|||
|
<span class="go">10</span>
|
|||
|
<span class="gp">>>> </span><span class="n">inspect</span><span class="o">.</span><span class="n">getattr_static</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'f'</span><span class="p">)</span>
|
|||
|
<span class="go"><property object at 0x1022bd788></span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<blockquote>
|
|||
|
<div><p>(Contributed by Michael Foord.)</p>
|
|||
|
</div></blockquote>
|
|||
|
</div>
|
|||
|
<div class="section" id="pydoc">
|
|||
|
<h3>pydoc<a class="headerlink" href="#pydoc" title="Permalink to this headline">¶</a></h3>
|
|||
|
<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 provides a much-improved Web server interface, as
|
|||
|
well as a new command-line option <code class="docutils literal notranslate"><span class="pre">-b</span></code> to automatically open a browser window
|
|||
|
to display that server:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> pydoc3.2 -b
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Ron Adam; <a class="reference external" href="https://bugs.python.org/issue2001">bpo-2001</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="dis">
|
|||
|
<h3>dis<a class="headerlink" href="#dis" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/dis.html#module-dis" title="dis: Disassembler for Python bytecode."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dis</span></code></a> module gained two new functions for inspecting code,
|
|||
|
<a class="reference internal" href="../library/dis.html#dis.code_info" title="dis.code_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">code_info()</span></code></a> and <a class="reference internal" href="../library/dis.html#dis.show_code" title="dis.show_code"><code class="xref py py-func docutils literal notranslate"><span class="pre">show_code()</span></code></a>. Both provide detailed code
|
|||
|
object information for the supplied function, method, source code string or code
|
|||
|
object. The former returns a string and the latter prints it:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">dis</span><span class="o">,</span> <span class="nn">random</span>
|
|||
|
<span class="gp">>>> </span><span class="n">dis</span><span class="o">.</span><span class="n">show_code</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">)</span>
|
|||
|
<span class="go">Name: choice</span>
|
|||
|
<span class="go">Filename: /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/random.py</span>
|
|||
|
<span class="go">Argument count: 2</span>
|
|||
|
<span class="go">Kw-only arguments: 0</span>
|
|||
|
<span class="go">Number of locals: 3</span>
|
|||
|
<span class="go">Stack size: 11</span>
|
|||
|
<span class="go">Flags: OPTIMIZED, NEWLOCALS, NOFREE</span>
|
|||
|
<span class="go">Constants:</span>
|
|||
|
<span class="go"> 0: 'Choose a random element from a non-empty sequence.'</span>
|
|||
|
<span class="go"> 1: 'Cannot choose from an empty sequence'</span>
|
|||
|
<span class="go">Names:</span>
|
|||
|
<span class="go"> 0: _randbelow</span>
|
|||
|
<span class="go"> 1: len</span>
|
|||
|
<span class="go"> 2: ValueError</span>
|
|||
|
<span class="go"> 3: IndexError</span>
|
|||
|
<span class="go">Variable names:</span>
|
|||
|
<span class="go"> 0: self</span>
|
|||
|
<span class="go"> 1: seq</span>
|
|||
|
<span class="go"> 2: i</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>In addition, the <a class="reference internal" href="../library/dis.html#dis.dis" title="dis.dis"><code class="xref py py-func docutils literal notranslate"><span class="pre">dis()</span></code></a> function now accepts string arguments
|
|||
|
so that the common idiom <code class="docutils literal notranslate"><span class="pre">dis(compile(s,</span> <span class="pre">'',</span> <span class="pre">'eval'))</span></code> can be shortened
|
|||
|
to <code class="docutils literal notranslate"><span class="pre">dis(s)</span></code>:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">dis</span><span class="p">(</span><span class="s1">'3*x+1 if x%2==1 else x//2'</span><span class="p">)</span>
|
|||
|
<span class="go"> 1 0 LOAD_NAME 0 (x)</span>
|
|||
|
<span class="go"> 3 LOAD_CONST 0 (2)</span>
|
|||
|
<span class="go"> 6 BINARY_MODULO</span>
|
|||
|
<span class="go"> 7 LOAD_CONST 1 (1)</span>
|
|||
|
<span class="go"> 10 COMPARE_OP 2 (==)</span>
|
|||
|
<span class="go"> 13 POP_JUMP_IF_FALSE 28</span>
|
|||
|
<span class="go"> 16 LOAD_CONST 2 (3)</span>
|
|||
|
<span class="go"> 19 LOAD_NAME 0 (x)</span>
|
|||
|
<span class="go"> 22 BINARY_MULTIPLY</span>
|
|||
|
<span class="go"> 23 LOAD_CONST 1 (1)</span>
|
|||
|
<span class="go"> 26 BINARY_ADD</span>
|
|||
|
<span class="go"> 27 RETURN_VALUE</span>
|
|||
|
<span class="go"> >> 28 LOAD_NAME 0 (x)</span>
|
|||
|
<span class="go"> 31 LOAD_CONST 0 (2)</span>
|
|||
|
<span class="go"> 34 BINARY_FLOOR_DIVIDE</span>
|
|||
|
<span class="go"> 35 RETURN_VALUE</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Taken together, these improvements make it easier to explore how CPython is
|
|||
|
implemented and to see for yourself what the language syntax does
|
|||
|
under-the-hood.</p>
|
|||
|
<p>(Contributed by Nick Coghlan in <a class="reference external" href="https://bugs.python.org/issue9147">bpo-9147</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="dbm">
|
|||
|
<h3>dbm<a class="headerlink" href="#dbm" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>All database modules now support the <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> methods.</p>
|
|||
|
<p>(Suggested by Ray Allen in <a class="reference external" href="https://bugs.python.org/issue9523">bpo-9523</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="ctypes">
|
|||
|
<h3>ctypes<a class="headerlink" href="#ctypes" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>A new type, <a class="reference internal" href="../library/ctypes.html#ctypes.c_ssize_t" title="ctypes.c_ssize_t"><code class="xref py py-class docutils literal notranslate"><span class="pre">ctypes.c_ssize_t</span></code></a> represents the C <code class="xref c c-type docutils literal notranslate"><span class="pre">ssize_t</span></code> datatype.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="site">
|
|||
|
<h3>site<a class="headerlink" href="#site" title="Permalink to this headline">¶</a></h3>
|
|||
|
<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 has three new functions useful for reporting on the
|
|||
|
details of a given Python installation.</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><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> lists all global site-packages directories.</p></li>
|
|||
|
<li><p><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> reports on the user’s base directory where data can
|
|||
|
be stored.</p></li>
|
|||
|
<li><p><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> reveals the user-specific site-packages
|
|||
|
directory path.</p></li>
|
|||
|
</ul>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">site</span>
|
|||
|
<span class="gp">>>> </span><span class="n">site</span><span class="o">.</span><span class="n">getsitepackages</span><span class="p">()</span>
|
|||
|
<span class="go">['/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages',</span>
|
|||
|
<span class="go"> '/Library/Frameworks/Python.framework/Versions/3.2/lib/site-python',</span>
|
|||
|
<span class="go"> '/Library/Python/3.2/site-packages']</span>
|
|||
|
<span class="gp">>>> </span><span class="n">site</span><span class="o">.</span><span class="n">getuserbase</span><span class="p">()</span>
|
|||
|
<span class="go">'/Users/raymondhettinger/Library/Python/3.2'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
|
|||
|
<span class="go">'/Users/raymondhettinger/Library/Python/3.2/lib/python/site-packages'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>Conveniently, some of site’s functionality is accessible directly from the
|
|||
|
command-line:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m site --user-base
|
|||
|
<span class="go">/Users/raymondhettinger/.local</span>
|
|||
|
<span class="gp">$</span> python -m site --user-site
|
|||
|
<span class="go">/Users/raymondhettinger/.local/lib/python3.2/site-packages</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Tarek Ziadé in <a class="reference external" href="https://bugs.python.org/issue6693">bpo-6693</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="sysconfig">
|
|||
|
<h3>sysconfig<a class="headerlink" href="#sysconfig" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The new <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 makes it straightforward to discover
|
|||
|
installation paths and configuration variables that vary across platforms and
|
|||
|
installations.</p>
|
|||
|
<p>The module offers access simple access functions for platform and version
|
|||
|
information:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_platform" title="sysconfig.get_platform"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_platform()</span></code></a> returning values like <em>linux-i586</em> or
|
|||
|
<em>macosx-10.6-ppc</em>.</p></li>
|
|||
|
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_python_version" title="sysconfig.get_python_version"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_python_version()</span></code></a> returns a Python version string
|
|||
|
such as “3.2”.</p></li>
|
|||
|
</ul>
|
|||
|
<p>It also provides access to the paths and variables corresponding to one of
|
|||
|
seven named schemes used by <a class="reference internal" href="../library/distutils.html#module-distutils" title="distutils: Support for building and installing Python modules into an existing Python installation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">distutils</span></code></a>. Those include <em>posix_prefix</em>,
|
|||
|
<em>posix_home</em>, <em>posix_user</em>, <em>nt</em>, <em>nt_user</em>, <em>os2</em>, <em>os2_home</em>:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_paths" title="sysconfig.get_paths"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_paths()</span></code></a> makes a dictionary containing installation paths
|
|||
|
for the current installation scheme.</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> returns a dictionary of platform specific
|
|||
|
variables.</p></li>
|
|||
|
</ul>
|
|||
|
<p>There is also a convenient command-line interface:</p>
|
|||
|
<div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\Python32></span>python -m sysconfig
|
|||
|
<span class="go">Platform: "win32"</span>
|
|||
|
<span class="go">Python version: "3.2"</span>
|
|||
|
<span class="go">Current installation scheme: "nt"</span>
|
|||
|
|
|||
|
<span class="go">Paths:</span>
|
|||
|
<span class="go"> data = "C:\Python32"</span>
|
|||
|
<span class="go"> include = "C:\Python32\Include"</span>
|
|||
|
<span class="go"> platinclude = "C:\Python32\Include"</span>
|
|||
|
<span class="go"> platlib = "C:\Python32\Lib\site-packages"</span>
|
|||
|
<span class="go"> platstdlib = "C:\Python32\Lib"</span>
|
|||
|
<span class="go"> purelib = "C:\Python32\Lib\site-packages"</span>
|
|||
|
<span class="go"> scripts = "C:\Python32\Scripts"</span>
|
|||
|
<span class="go"> stdlib = "C:\Python32\Lib"</span>
|
|||
|
|
|||
|
<span class="go">Variables:</span>
|
|||
|
<span class="go"> BINDIR = "C:\Python32"</span>
|
|||
|
<span class="go"> BINLIBDEST = "C:\Python32\Lib"</span>
|
|||
|
<span class="go"> EXE = ".exe"</span>
|
|||
|
<span class="go"> INCLUDEPY = "C:\Python32\Include"</span>
|
|||
|
<span class="go"> LIBDEST = "C:\Python32\Lib"</span>
|
|||
|
<span class="go"> SO = ".pyd"</span>
|
|||
|
<span class="go"> VERSION = "32"</span>
|
|||
|
<span class="go"> abiflags = ""</span>
|
|||
|
<span class="go"> base = "C:\Python32"</span>
|
|||
|
<span class="go"> exec_prefix = "C:\Python32"</span>
|
|||
|
<span class="go"> platbase = "C:\Python32"</span>
|
|||
|
<span class="go"> prefix = "C:\Python32"</span>
|
|||
|
<span class="go"> projectbase = "C:\Python32"</span>
|
|||
|
<span class="go"> py_version = "3.2"</span>
|
|||
|
<span class="go"> py_version_nodot = "32"</span>
|
|||
|
<span class="go"> py_version_short = "3.2"</span>
|
|||
|
<span class="go"> srcdir = "C:\Python32"</span>
|
|||
|
<span class="go"> userbase = "C:\Documents and Settings\Raymond\Application Data\Python"</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Moved out of Distutils by Tarek Ziadé.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="pdb">
|
|||
|
<h3>pdb<a class="headerlink" href="#pdb" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> debugger module gained a number of usability improvements:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p><code class="file docutils literal notranslate"><span class="pre">pdb.py</span></code> now has a <code class="docutils literal notranslate"><span class="pre">-c</span></code> option that executes commands as given in a
|
|||
|
<code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> script file.</p></li>
|
|||
|
<li><p>A <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> script file can contain <code class="docutils literal notranslate"><span class="pre">continue</span></code> and <code class="docutils literal notranslate"><span class="pre">next</span></code> commands
|
|||
|
that continue debugging.</p></li>
|
|||
|
<li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Pdb</span></code> class constructor now accepts a <em>nosigint</em> argument.</p></li>
|
|||
|
<li><p>New commands: <code class="docutils literal notranslate"><span class="pre">l(list)</span></code>, <code class="docutils literal notranslate"><span class="pre">ll(long</span> <span class="pre">list)</span></code> and <code class="docutils literal notranslate"><span class="pre">source</span></code> for
|
|||
|
listing source code.</p></li>
|
|||
|
<li><p>New commands: <code class="docutils literal notranslate"><span class="pre">display</span></code> and <code class="docutils literal notranslate"><span class="pre">undisplay</span></code> for showing or hiding
|
|||
|
the value of an expression if it has changed.</p></li>
|
|||
|
<li><p>New command: <code class="docutils literal notranslate"><span class="pre">interact</span></code> for starting an interactive interpreter containing
|
|||
|
the global and local names found in the current scope.</p></li>
|
|||
|
<li><p>Breakpoints can be cleared by breakpoint number.</p></li>
|
|||
|
</ul>
|
|||
|
<p>(Contributed by Georg Brandl, Antonio Cuni and Ilya Sandler.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="configparser">
|
|||
|
<h3>configparser<a class="headerlink" href="#configparser" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a> module was modified to improve usability and
|
|||
|
predictability of the default parser and its supported INI syntax. The old
|
|||
|
<code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> class was removed in favor of <code class="xref py py-class docutils literal notranslate"><span class="pre">SafeConfigParser</span></code>
|
|||
|
which has in turn been renamed to <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a>. Support
|
|||
|
for inline comments is now turned off by default and section or option
|
|||
|
duplicates are not allowed in a single configuration source.</p>
|
|||
|
<p>Config parsers gained a new API based on the mapping protocol:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s2">"""</span>
|
|||
|
<span class="gp">... </span><span class="s2">[DEFAULT]</span>
|
|||
|
<span class="gp">... </span><span class="s2">location = upper left</span>
|
|||
|
<span class="gp">... </span><span class="s2">visible = yes</span>
|
|||
|
<span class="gp">... </span><span class="s2">editable = no</span>
|
|||
|
<span class="gp">... </span><span class="s2">color = blue</span>
|
|||
|
<span class="gp">...</span><span class="s2"></span>
|
|||
|
<span class="gp">... </span><span class="s2">[main]</span>
|
|||
|
<span class="gp">... </span><span class="s2">title = Main Menu</span>
|
|||
|
<span class="gp">... </span><span class="s2">color = green</span>
|
|||
|
<span class="gp">...</span><span class="s2"></span>
|
|||
|
<span class="gp">... </span><span class="s2">[options]</span>
|
|||
|
<span class="gp">... </span><span class="s2">title = Options</span>
|
|||
|
<span class="gp">... </span><span class="s2">"""</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="p">[</span><span class="s1">'main'</span><span class="p">][</span><span class="s1">'color'</span><span class="p">]</span>
|
|||
|
<span class="go">'green'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="p">[</span><span class="s1">'main'</span><span class="p">][</span><span class="s1">'editable'</span><span class="p">]</span>
|
|||
|
<span class="go">'no'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">section</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s1">'options'</span><span class="p">]</span>
|
|||
|
<span class="gp">>>> </span><span class="n">section</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span>
|
|||
|
<span class="go">'Options'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">section</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Options (editable: </span><span class="si">%(editable)s</span><span class="s1">)'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">section</span><span class="p">[</span><span class="s1">'title'</span><span class="p">]</span>
|
|||
|
<span class="go">'Options (editable: no)'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>The new API is implemented on top of the classical API, so custom parser
|
|||
|
subclasses should be able to use it without modifications.</p>
|
|||
|
<p>The INI file structure accepted by config parsers can now be customized. Users
|
|||
|
can specify alternative option/value delimiters and comment prefixes, change the
|
|||
|
name of the <em>DEFAULT</em> section or switch the interpolation syntax.</p>
|
|||
|
<p>There is support for pluggable interpolation including an additional interpolation
|
|||
|
handler <a class="reference internal" href="../library/configparser.html#configparser.ExtendedInterpolation" title="configparser.ExtendedInterpolation"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExtendedInterpolation</span></code></a>:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">(</span><span class="n">interpolation</span><span class="o">=</span><span class="n">ExtendedInterpolation</span><span class="p">())</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">read_dict</span><span class="p">({</span><span class="s1">'buildout'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'directory'</span><span class="p">:</span> <span class="s1">'/home/ambv/zope9'</span><span class="p">},</span>
|
|||
|
<span class="gp">... </span> <span class="s1">'custom'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'prefix'</span><span class="p">:</span> <span class="s1">'/usr/local'</span><span class="p">}})</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s2">"""</span>
|
|||
|
<span class="gp">... </span><span class="s2">[buildout]</span>
|
|||
|
<span class="gp">... </span><span class="s2">parts =</span>
|
|||
|
<span class="gp">... </span><span class="s2"> zope9</span>
|
|||
|
<span class="gp">... </span><span class="s2"> instance</span>
|
|||
|
<span class="gp">... </span><span class="s2">find-links =</span>
|
|||
|
<span class="gp">... </span><span class="s2"> ${buildout:directory}/downloads/dist</span>
|
|||
|
<span class="gp">...</span><span class="s2"></span>
|
|||
|
<span class="gp">... </span><span class="s2">[zope9]</span>
|
|||
|
<span class="gp">... </span><span class="s2">recipe = plone.recipe.zope9install</span>
|
|||
|
<span class="gp">... </span><span class="s2">location = /opt/zope</span>
|
|||
|
<span class="gp">...</span><span class="s2"></span>
|
|||
|
<span class="gp">... </span><span class="s2">[instance]</span>
|
|||
|
<span class="gp">... </span><span class="s2">recipe = plone.recipe.zope9instance</span>
|
|||
|
<span class="gp">... </span><span class="s2">zope9-location = ${zope9:location}</span>
|
|||
|
<span class="gp">... </span><span class="s2">zope-conf = ${custom:prefix}/etc/zope.conf</span>
|
|||
|
<span class="gp">... </span><span class="s2">"""</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="p">[</span><span class="s1">'buildout'</span><span class="p">][</span><span class="s1">'find-links'</span><span class="p">]</span>
|
|||
|
<span class="go">'\n/home/ambv/zope9/downloads/dist'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">parser</span><span class="p">[</span><span class="s1">'instance'</span><span class="p">][</span><span class="s1">'zope-conf'</span><span class="p">]</span>
|
|||
|
<span class="go">'/usr/local/etc/zope.conf'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">instance</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s1">'instance'</span><span class="p">]</span>
|
|||
|
<span class="gp">>>> </span><span class="n">instance</span><span class="p">[</span><span class="s1">'zope-conf'</span><span class="p">]</span>
|
|||
|
<span class="go">'/usr/local/etc/zope.conf'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">instance</span><span class="p">[</span><span class="s1">'zope9-location'</span><span class="p">]</span>
|
|||
|
<span class="go">'/opt/zope'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>A number of smaller features were also introduced, like support for specifying
|
|||
|
encoding in read operations, specifying fallback values for get-functions, or
|
|||
|
reading directly from dictionaries and strings.</p>
|
|||
|
<p>(All changes contributed by Łukasz Langa.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="urllib-parse">
|
|||
|
<h3>urllib.parse<a class="headerlink" href="#urllib-parse" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>A number of usability improvements were made for the <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code></a> module.</p>
|
|||
|
<p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlparse" title="urllib.parse.urlparse"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlparse()</span></code></a> function now supports <a class="reference external" href="https://en.wikipedia.org/wiki/IPv6">IPv6</a> addresses as described in <span class="target" id="index-13"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a>:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
|
|||
|
<span class="gp">>>> </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s1">'http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/'</span><span class="p">)</span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
|
|||
|
<span class="go">ParseResult(scheme='http',</span>
|
|||
|
<span class="go"> netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',</span>
|
|||
|
<span class="go"> path='/foo/',</span>
|
|||
|
<span class="go"> params='',</span>
|
|||
|
<span class="go"> query='',</span>
|
|||
|
<span class="go"> fragment='')</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urldefrag" title="urllib.parse.urldefrag"><code class="xref py py-func docutils literal notranslate"><span class="pre">urldefrag()</span></code></a> function now returns a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">r</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urldefrag</span><span class="p">(</span><span class="s1">'http://python.org/about/#target'</span><span class="p">)</span>
|
|||
|
<span class="gp">>>> </span><span class="n">r</span>
|
|||
|
<span class="go">DefragResult(url='http://python.org/about/', fragment='target')</span>
|
|||
|
<span class="gp">>>> </span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|||
|
<span class="go">'http://python.org/about/'</span>
|
|||
|
<span class="gp">>>> </span><span class="n">r</span><span class="o">.</span><span class="n">fragment</span>
|
|||
|
<span class="go">'target'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>And, the <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlencode()</span></code></a> function is now much more flexible,
|
|||
|
accepting either a string or bytes type for the <em>query</em> argument. If it is a
|
|||
|
string, then the <em>safe</em>, <em>encoding</em>, and <em>error</em> parameters are sent to
|
|||
|
<a class="reference internal" href="../library/urllib.parse.html#urllib.parse.quote_plus" title="urllib.parse.quote_plus"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote_plus()</span></code></a> for encoding:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">([</span>
|
|||
|
<span class="gp">... </span> <span class="p">(</span><span class="s1">'type'</span><span class="p">,</span> <span class="s1">'telenovela'</span><span class="p">),</span>
|
|||
|
<span class="gp">... </span> <span class="p">(</span><span class="s1">'name'</span><span class="p">,</span> <span class="s1">'¿Dónde Está Elisa?'</span><span class="p">)],</span>
|
|||
|
<span class="gp">... </span> <span class="n">encoding</span><span class="o">=</span><span class="s1">'latin-1'</span><span class="p">)</span>
|
|||
|
<span class="go">'type=telenovela&name=%BFD%F3nde+Est%E1+Elisa%3F'</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>As detailed in <a class="reference internal" href="../library/urllib.parse.html#parsing-ascii-encoded-bytes"><span class="std std-ref">Parsing ASCII Encoded Bytes</span></a>, all the <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.parse</span></code></a>
|
|||
|
functions now accept ASCII-encoded byte strings as input, so long as they are
|
|||
|
not mixed with regular strings. If ASCII-encoded byte strings are given as
|
|||
|
parameters, the return types will also be an ASCII-encoded byte strings:</p>
|
|||
|
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="sa">b</span><span class="s1">'http://www.python.org:80/about/'</span><span class="p">)</span> <span class="c1"># doctest: +NORMALIZE_WHITESPACE</span>
|
|||
|
<span class="go">ParseResultBytes(scheme=b'http', netloc=b'www.python.org:80',</span>
|
|||
|
<span class="go"> path=b'/about/', params=b'', query=b'', fragment=b'')</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in <a class="reference external" href="https://bugs.python.org/issue2987">bpo-2987</a>,
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue5468">bpo-5468</a>, and <a class="reference external" href="https://bugs.python.org/issue9873">bpo-9873</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="mailbox">
|
|||
|
<h3>mailbox<a class="headerlink" href="#mailbox" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>Thanks to a concerted effort by R. David Murray, 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 has
|
|||
|
been fixed for Python 3.2. The challenge was that mailbox had been originally
|
|||
|
designed with a text interface, but email messages are best represented with
|
|||
|
<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> because various parts of a message may have different encodings.</p>
|
|||
|
<p>The solution harnessed 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> package’s binary support for parsing
|
|||
|
arbitrary email messages. In addition, the solution required a number of API
|
|||
|
changes.</p>
|
|||
|
<p>As expected, the <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a> method for
|
|||
|
<a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">mailbox.Mailbox</span></code></a> objects now accepts binary input.</p>
|
|||
|
<p><a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> and text file input are deprecated. Also, string input
|
|||
|
will fail early if non-ASCII characters are used. Previously it would fail when
|
|||
|
the email was processed in a later step.</p>
|
|||
|
<p>There is also support for binary output. The <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_file" title="mailbox.Mailbox.get_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_file()</span></code></a>
|
|||
|
method now returns a file in the binary mode (where it used to incorrectly set
|
|||
|
the file to text-mode). There is also a new <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_bytes" title="mailbox.Mailbox.get_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_bytes()</span></code></a>
|
|||
|
method that returns 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> representation of a message corresponding
|
|||
|
to a given <em>key</em>.</p>
|
|||
|
<p>It is still possible to get non-binary output using the old API’s
|
|||
|
<a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_string" title="mailbox.Mailbox.get_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_string()</span></code></a> method, but that approach
|
|||
|
is not very useful. Instead, it is best to extract messages from
|
|||
|
a <a class="reference internal" href="../library/mailbox.html#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object or to load them from binary input.</p>
|
|||
|
<p>(Contributed by R. David Murray, with efforts from Steffen Daode Nurpmeso and an
|
|||
|
initial patch by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue9124">bpo-9124</a>.)</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="turtledemo">
|
|||
|
<h3>turtledemo<a class="headerlink" href="#turtledemo" title="Permalink to this headline">¶</a></h3>
|
|||
|
<p>The demonstration code for the <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> module was moved from the <em>Demo</em>
|
|||
|
directory to main library. It includes over a dozen sample scripts with
|
|||
|
lively displays. Being on <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path</span></code></a>, it can now be run directly
|
|||
|
from the command-line:</p>
|
|||
|
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m turtledemo
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Moved from the Demo directory by Alexander Belopolsky in <a class="reference external" href="https://bugs.python.org/issue10199">bpo-10199</a>.)</p>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="section" id="multi-threading">
|
|||
|
<h2>Multi-threading<a class="headerlink" href="#multi-threading" title="Permalink to this headline">¶</a></h2>
|
|||
|
<ul>
|
|||
|
<li><p>The mechanism for serializing execution of concurrently running Python threads
|
|||
|
(generally known as the <a class="reference internal" href="../glossary.html#term-gil"><span class="xref std std-term">GIL</span></a> or <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">Global Interpreter Lock</span></a>) has
|
|||
|
been rewritten. Among the objectives were more predictable switching
|
|||
|
intervals and reduced overhead due to lock contention and the number of
|
|||
|
ensuing system calls. The notion of a “check interval” to allow thread
|
|||
|
switches has been abandoned and replaced by an absolute duration expressed in
|
|||
|
seconds. This parameter is tunable through <a class="reference internal" href="../library/sys.html#sys.setswitchinterval" title="sys.setswitchinterval"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setswitchinterval()</span></code></a>.
|
|||
|
It currently defaults to 5 milliseconds.</p>
|
|||
|
<p>Additional details about the implementation can be read from a <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2009-October/093321.html">python-dev
|
|||
|
mailing-list message</a>
|
|||
|
(however, “priority requests” as exposed in this message have not been kept
|
|||
|
for inclusion).</p>
|
|||
|
<p>(Contributed by Antoine Pitrou.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Regular and recursive locks now accept an optional <em>timeout</em> argument to their
|
|||
|
<a class="reference internal" href="../library/threading.html#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> method. (Contributed by Antoine Pitrou;
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue7316">bpo-7316</a>.)</p></li>
|
|||
|
<li><p>Similarly, <a class="reference internal" href="../library/threading.html#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Semaphore.acquire()</span></code></a> also gained a <em>timeout</em>
|
|||
|
argument. (Contributed by Torsten Landschoff; <a class="reference external" href="https://bugs.python.org/issue850728">bpo-850728</a>.)</p></li>
|
|||
|
<li><p>Regular and recursive lock acquisitions can now be interrupted by signals on
|
|||
|
platforms using Pthreads. This means that Python programs that deadlock while
|
|||
|
acquiring locks can be successfully killed by repeatedly sending SIGINT to the
|
|||
|
process (by pressing <kbd class="kbd docutils literal notranslate">Ctrl+C</kbd> in most shells).
|
|||
|
(Contributed by Reid Kleckner; <a class="reference external" href="https://bugs.python.org/issue8844">bpo-8844</a>.)</p></li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="optimizations">
|
|||
|
<h2>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>A number of small performance enhancements have been added:</p>
|
|||
|
<ul>
|
|||
|
<li><p>Python’s peephole optimizer now recognizes patterns such <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">{1,</span> <span class="pre">2,</span> <span class="pre">3}</span></code> as
|
|||
|
being a test for membership in a set of constants. The optimizer recasts the
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> as a <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> and stores the pre-built constant.</p>
|
|||
|
<p>Now that the speed penalty is gone, it is practical to start writing
|
|||
|
membership tests using set-notation. This style is both semantically clear
|
|||
|
and operationally fast:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">extension</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">rpartition</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
|
|||
|
<span class="k">if</span> <span class="n">extension</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">'xml'</span><span class="p">,</span> <span class="s1">'html'</span><span class="p">,</span> <span class="s1">'xhtml'</span><span class="p">,</span> <span class="s1">'css'</span><span class="p">}:</span>
|
|||
|
<span class="n">handle</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Patch and additional tests contributed by Dave Malcolm; <a class="reference external" href="https://bugs.python.org/issue6690">bpo-6690</a>).</p>
|
|||
|
</li>
|
|||
|
<li><p>Serializing and unserializing data using 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> module is now
|
|||
|
several times faster.</p>
|
|||
|
<p>(Contributed by Alexandre Vassalotti, Antoine Pitrou
|
|||
|
and the Unladen Swallow team in <a class="reference external" href="https://bugs.python.org/issue9410">bpo-9410</a> and <a class="reference external" href="https://bugs.python.org/issue3873">bpo-3873</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference external" href="https://en.wikipedia.org/wiki/Timsort">Timsort algorithm</a> used in
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> and <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> now runs faster and uses less memory
|
|||
|
when called with a <a class="reference internal" href="../glossary.html#term-key-function"><span class="xref std std-term">key function</span></a>. Previously, every element of
|
|||
|
a list was wrapped with a temporary object that remembered the key value
|
|||
|
associated with each element. Now, two arrays of keys and values are
|
|||
|
sorted in parallel. This saves the memory consumed by the sort wrappers,
|
|||
|
and it saves time lost to delegating comparisons.</p>
|
|||
|
<p>(Patch by Daniel Stutzbach in <a class="reference external" href="https://bugs.python.org/issue9915">bpo-9915</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>JSON decoding performance is improved and memory consumption is reduced
|
|||
|
whenever the same string is repeated for multiple keys. Also, JSON encoding
|
|||
|
now uses the C speedups when the <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> argument is true.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue7451">bpo-7451</a> and by Raymond Hettinger and
|
|||
|
Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue10314">bpo-10314</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Recursive locks (created with the <a class="reference internal" href="../library/threading.html#threading.RLock" title="threading.RLock"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.RLock()</span></code></a> API) now benefit
|
|||
|
from a C implementation which makes them as fast as regular locks, and between
|
|||
|
10x and 15x faster than their previous pure Python implementation.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue3001">bpo-3001</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The fast-search algorithm in stringlib is now used by 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">rsplit()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">splitlines()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code> methods on
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects. Likewise, the
|
|||
|
algorithm is also used by <code class="xref py py-meth docutils literal notranslate"><span class="pre">rfind()</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">rsplit()</span></code> and
|
|||
|
<code class="xref py py-meth docutils literal notranslate"><span class="pre">rpartition()</span></code>.</p>
|
|||
|
<p>(Patch by Florent Xicluna in <a class="reference external" href="https://bugs.python.org/issue7622">bpo-7622</a> and <a class="reference external" href="https://bugs.python.org/issue7462">bpo-7462</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Integer to string conversions now work two “digits” at a time, reducing the
|
|||
|
number of division and modulo operations.</p>
|
|||
|
<p>(<a class="reference external" href="https://bugs.python.org/issue6713">bpo-6713</a> by Gawain Bolton, Mark Dickinson, and Victor Stinner.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
<p>There were several other minor optimizations. Set differencing now runs faster
|
|||
|
when one operand is much larger than the other (patch by Andress Bennetts in
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue8685">bpo-8685</a>). The <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.repeat()</span></code> method has a faster implementation
|
|||
|
(<a class="reference external" href="https://bugs.python.org/issue1569291">bpo-1569291</a> by Alexander Belopolsky). The <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code>
|
|||
|
has more efficient buffering (<a class="reference external" href="https://bugs.python.org/issue3709">bpo-3709</a> by Andrew Schaaf). The
|
|||
|
<a class="reference internal" href="../library/operator.html#operator.attrgetter" title="operator.attrgetter"><code class="xref py py-func docutils literal notranslate"><span class="pre">operator.attrgetter()</span></code></a> function has been sped-up (<a class="reference external" href="https://bugs.python.org/issue10160">bpo-10160</a> by
|
|||
|
Christos Georgiou). And <code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> loads multi-line arguments a bit
|
|||
|
faster (<a class="reference external" href="https://bugs.python.org/issue7113">bpo-7113</a> by Łukasz Langa).</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="unicode">
|
|||
|
<h2>Unicode<a class="headerlink" href="#unicode" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Python has been updated to <a class="reference external" href="http://unicode.org/versions/Unicode6.0.0/">Unicode 6.0.0</a>. The update to the standard adds
|
|||
|
over 2,000 new characters including <a class="reference external" href="https://en.wikipedia.org/wiki/Emoji">emoji</a>
|
|||
|
symbols which are important for mobile phones.</p>
|
|||
|
<p>In addition, the updated standard has altered the character properties for two
|
|||
|
Kannada characters (U+0CF1, U+0CF2) and one New Tai Lue numeric character
|
|||
|
(U+19DA), making the former eligible for use in identifiers while disqualifying
|
|||
|
the latter. For more information, see <a class="reference external" href="http://www.unicode.org/versions/Unicode6.0.0/#Database_Changes">Unicode Character Database Changes</a>.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="codecs">
|
|||
|
<h2>Codecs<a class="headerlink" href="#codecs" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Support was added for <em>cp720</em> Arabic DOS encoding (<a class="reference external" href="https://bugs.python.org/issue1616979">bpo-1616979</a>).</p>
|
|||
|
<p>MBCS encoding no longer ignores the error handler argument. In the default
|
|||
|
strict mode, it raises an <a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a> when it encounters an
|
|||
|
undecodable byte sequence and an <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a> for an unencodable
|
|||
|
character.</p>
|
|||
|
<p>The MBCS codec supports <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> and <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> error handlers for
|
|||
|
decoding, and <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> and <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> for encoding.</p>
|
|||
|
<p>To emulate Python3.1 MBCS encoding, select the <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> handler for decoding
|
|||
|
and the <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> handler for encoding.</p>
|
|||
|
<p>On Mac OS X, Python decodes command line arguments with <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> rather than
|
|||
|
the locale encoding.</p>
|
|||
|
<p>By default, <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> uses <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> encoding on Windows (instead of
|
|||
|
<code class="docutils literal notranslate"><span class="pre">'mbcs'</span></code>) and the <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> error handler on all operating
|
|||
|
systems.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="documentation">
|
|||
|
<h2>Documentation<a class="headerlink" href="#documentation" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>The documentation continues to be improved.</p>
|
|||
|
<ul>
|
|||
|
<li><p>A table of quick links has been added to the top of lengthy sections such as
|
|||
|
<a class="reference internal" href="../library/functions.html#built-in-funcs"><span class="std std-ref">Built-in Functions</span></a>. In the case of <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a>, the links are
|
|||
|
accompanied by tables of cheatsheet-style summaries to provide an overview and
|
|||
|
memory jog without having to read all of the docs.</p></li>
|
|||
|
<li><p>In some cases, the pure Python source code can be a helpful adjunct to the
|
|||
|
documentation, so now many modules now feature quick links to the latest
|
|||
|
version of the source code. For example, 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
|
|||
|
documentation has a quick link at the top labeled:</p>
|
|||
|
<blockquote>
|
|||
|
<div><p><strong>Source code</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/functools.py">Lib/functools.py</a>.</p>
|
|||
|
</div></blockquote>
|
|||
|
<p>(Contributed by Raymond Hettinger; see
|
|||
|
<a class="reference external" href="https://rhettinger.wordpress.com/2011/01/28/open-your-source-more/">rationale</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The docs now contain more examples and recipes. In particular, <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 has an extensive section, <a class="reference internal" href="../library/re.html#re-examples"><span class="std std-ref">Regular Expression Examples</span></a>. Likewise, the
|
|||
|
<a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module continues to be updated with new
|
|||
|
<a class="reference internal" href="../library/itertools.html#itertools-recipes"><span class="std std-ref">Itertools Recipes</span></a>.</p></li>
|
|||
|
<li><p>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 now has an auxiliary implementation in pure Python.
|
|||
|
No functionality was changed. This just provides an easier-to-read alternate
|
|||
|
implementation.</p>
|
|||
|
<p>(Contributed by Alexander Belopolsky in <a class="reference external" href="https://bugs.python.org/issue9528">bpo-9528</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The unmaintained <code class="file docutils literal notranslate"><span class="pre">Demo</span></code> directory has been removed. Some demos were
|
|||
|
integrated into the documentation, some were moved to the <code class="file docutils literal notranslate"><span class="pre">Tools/demo</span></code>
|
|||
|
directory, and others were removed altogether.</p>
|
|||
|
<p>(Contributed by Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue7962">bpo-7962</a>.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="idle">
|
|||
|
<h2>IDLE<a class="headerlink" href="#idle" title="Permalink to this headline">¶</a></h2>
|
|||
|
<ul>
|
|||
|
<li><p>The format menu now has an option to clean source files by stripping
|
|||
|
trailing whitespace.</p>
|
|||
|
<p>(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue5150">bpo-5150</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>IDLE on Mac OS X now works with both Carbon AquaTk and Cocoa AquaTk.</p>
|
|||
|
<p>(Contributed by Kevin Walzer, Ned Deily, and Ronald Oussoren; <a class="reference external" href="https://bugs.python.org/issue6075">bpo-6075</a>.)</p>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="section" id="code-repository">
|
|||
|
<h2>Code Repository<a class="headerlink" href="#code-repository" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>In addition to the existing Subversion code repository at <a class="reference external" href="http://svn.python.org">http://svn.python.org</a>
|
|||
|
there is now a <a class="reference external" href="https://www.mercurial-scm.org/">Mercurial</a> repository at
|
|||
|
<a class="reference external" href="https://hg.python.org/">https://hg.python.org/</a>.</p>
|
|||
|
<p>After the 3.2 release, there are plans to switch to Mercurial as the primary
|
|||
|
repository. This distributed version control system should make it easier for
|
|||
|
members of the community to create and share external changesets. See
|
|||
|
<span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0385"><strong>PEP 385</strong></a> for details.</p>
|
|||
|
<p>To learn to use the new version control system, see the <a class="reference external" href="https://www.mercurial-scm.org/wiki/QuickStart">Quick Start</a> or the <a class="reference external" href="https://www.mercurial-scm.org/guide">Guide to
|
|||
|
Mercurial Workflows</a>.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="build-and-c-api-changes">
|
|||
|
<h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>Changes to Python’s build process and to the C API include:</p>
|
|||
|
<ul>
|
|||
|
<li><p>The <em>idle</em>, <em>pydoc</em> and <em>2to3</em> scripts are now installed with a
|
|||
|
version-specific suffix on <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">altinstall</span></code> (<a class="reference external" href="https://bugs.python.org/issue10679">bpo-10679</a>).</p></li>
|
|||
|
<li><p>The C functions that access the Unicode Database now accept and return
|
|||
|
characters from the full Unicode range, even on narrow unicode builds
|
|||
|
(Py_UNICODE_TOLOWER, Py_UNICODE_ISDECIMAL, and others). A visible difference
|
|||
|
in Python is that <a class="reference internal" href="../library/unicodedata.html#unicodedata.numeric" title="unicodedata.numeric"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicodedata.numeric()</span></code></a> now returns the correct value
|
|||
|
for large code points, and <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> may consider more characters as
|
|||
|
printable.</p>
|
|||
|
<p>(Reported by Bupjoe Lee and fixed by Amaury Forgeot D’Arc; <a class="reference external" href="https://bugs.python.org/issue5127">bpo-5127</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Computed gotos are now enabled by default on supported compilers (which are
|
|||
|
detected by the configure script). They can still be disabled selectively by
|
|||
|
specifying <code class="docutils literal notranslate"><span class="pre">--without-computed-gotos</span></code>.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue9203">bpo-9203</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The option <code class="docutils literal notranslate"><span class="pre">--with-wctype-functions</span></code> was removed. The built-in unicode
|
|||
|
database is now used for all functions.</p>
|
|||
|
<p>(Contributed by Amaury Forgeot D’Arc; <a class="reference external" href="https://bugs.python.org/issue9210">bpo-9210</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Hash values are now values of a new type, <code class="xref c c-type docutils literal notranslate"><span class="pre">Py_hash_t</span></code>, which is
|
|||
|
defined to be the same size as a pointer. Previously they were of type long,
|
|||
|
which on some 64-bit operating systems is still only 32 bits long. As a
|
|||
|
result of this fix, <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> can now hold more than
|
|||
|
<code class="docutils literal notranslate"><span class="pre">2**32</span></code> entries on builds with 64-bit pointers (previously, they could grow
|
|||
|
to that size but their performance degraded catastrophically).</p>
|
|||
|
<p>(Suggested by Raymond Hettinger and implemented by Benjamin Peterson;
|
|||
|
<a class="reference external" href="https://bugs.python.org/issue9778">bpo-9778</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>A new macro <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_VA_COPY</span></code> copies the state of the variable argument
|
|||
|
list. It is equivalent to C99 <em>va_copy</em> but available on all Python platforms
|
|||
|
(<a class="reference external" href="https://bugs.python.org/issue2443">bpo-2443</a>).</p></li>
|
|||
|
<li><p>A new C API 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> allows an embedded interpreter
|
|||
|
to set <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.argv</span></code></a> without also modifying <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path</span></code></a>
|
|||
|
(<a class="reference external" href="https://bugs.python.org/issue5753">bpo-5753</a>).</p></li>
|
|||
|
<li><p><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyEval_CallObject</span></code> is now only available in macro form. The
|
|||
|
function declaration, which was kept for backwards compatibility reasons, is
|
|||
|
now removed – the macro was introduced in 1997 (<a class="reference external" href="https://bugs.python.org/issue8276">bpo-8276</a>).</p></li>
|
|||
|
<li><p>There is a new function <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> which
|
|||
|
is analogous to <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>. They both serve to
|
|||
|
convert Python <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> into a native fixed-width type while providing
|
|||
|
detection of cases where the conversion won’t fit (<a class="reference external" href="https://bugs.python.org/issue7767">bpo-7767</a>).</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CompareWithASCIIString" title="PyUnicode_CompareWithASCIIString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CompareWithASCIIString()</span></code></a> function now returns <em>not
|
|||
|
equal</em> if the Python string is <em>NUL</em> terminated.</p></li>
|
|||
|
<li><p>There is a 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> that is
|
|||
|
like <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> but allows a docstring to be specified.
|
|||
|
This lets C exceptions have the same self-documenting capabilities as
|
|||
|
their pure Python counterparts (<a class="reference external" href="https://bugs.python.org/issue7033">bpo-7033</a>).</p></li>
|
|||
|
<li><p>When compiled with the <code class="docutils literal notranslate"><span class="pre">--with-valgrind</span></code> option, the pymalloc
|
|||
|
allocator will be automatically disabled when running under Valgrind. This
|
|||
|
gives improved memory leak detection when running under Valgrind, while taking
|
|||
|
advantage of pymalloc at other times (<a class="reference external" href="https://bugs.python.org/issue2422">bpo-2422</a>).</p></li>
|
|||
|
<li><p>Removed the <code class="docutils literal notranslate"><span class="pre">O?</span></code> format from the <em>PyArg_Parse</em> functions. The format is no
|
|||
|
longer used and it had never been documented (<a class="reference external" href="https://bugs.python.org/issue8837">bpo-8837</a>).</p></li>
|
|||
|
</ul>
|
|||
|
<p>There were a number of other small changes to the C-API. See the
|
|||
|
<a class="reference external" href="https://github.com/python/cpython/tree/3.7/Misc/NEWS">Misc/NEWS</a> file for a complete list.</p>
|
|||
|
<p>Also, there were a number of updates to the Mac OS X build, see
|
|||
|
<a class="reference external" href="https://github.com/python/cpython/tree/3.7/Mac/BuildScript/README.txt">Mac/BuildScript/README.txt</a> for details. For users running a 32/64-bit
|
|||
|
build, there is a known problem with the default Tcl/Tk on Mac OS X 10.6.
|
|||
|
Accordingly, we recommend installing an updated alternative such as
|
|||
|
<a class="reference external" href="https://www.activestate.com/activetcl/downloads">ActiveState Tcl/Tk 8.5.9</a>.
|
|||
|
See <a class="reference external" href="https://www.python.org/download/mac/tcltk/">https://www.python.org/download/mac/tcltk/</a> for additional details.</p>
|
|||
|
</div>
|
|||
|
<div class="section" id="porting-to-python-3-2">
|
|||
|
<h2>Porting to Python 3.2<a class="headerlink" href="#porting-to-python-3-2" title="Permalink to this headline">¶</a></h2>
|
|||
|
<p>This section lists previously described changes and other bugfixes that may
|
|||
|
require changes to your code:</p>
|
|||
|
<ul>
|
|||
|
<li><p>The <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">configparser</span></code></a> module has a number of clean-ups. The major change is
|
|||
|
to replace the old <code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> class with long-standing preferred
|
|||
|
alternative <code class="xref py py-class docutils literal notranslate"><span class="pre">SafeConfigParser</span></code>. In addition there are a number of
|
|||
|
smaller incompatibilities:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>The interpolation syntax is now validated on
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.get" title="configparser.ConfigParser.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> operations. In the default
|
|||
|
interpolation scheme, only two tokens with percent signs are valid: <code class="docutils literal notranslate"><span class="pre">%(name)s</span></code>
|
|||
|
and <code class="docutils literal notranslate"><span class="pre">%%</span></code>, the latter being an escaped percent sign.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.add_section" title="configparser.ConfigParser.add_section"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_section()</span></code></a> methods now verify that
|
|||
|
values are actual strings. Formerly, unsupported types could be introduced
|
|||
|
unintentionally.</p></li>
|
|||
|
<li><p>Duplicate sections or options from a single source now raise either
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.DuplicateSectionError" title="configparser.DuplicateSectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DuplicateSectionError</span></code></a> or
|
|||
|
<a class="reference internal" href="../library/configparser.html#configparser.DuplicateOptionError" title="configparser.DuplicateOptionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DuplicateOptionError</span></code></a>. Formerly, duplicates would
|
|||
|
silently overwrite a previous entry.</p></li>
|
|||
|
<li><p>Inline comments are now disabled by default so now the <strong>;</strong> character
|
|||
|
can be safely used in values.</p></li>
|
|||
|
<li><p>Comments now can be indented. Consequently, for <strong>;</strong> or <strong>#</strong> to appear at
|
|||
|
the start of a line in multiline values, it has to be interpolated. This
|
|||
|
keeps comment prefix characters in values from being mistaken as comments.</p></li>
|
|||
|
<li><p><code class="docutils literal notranslate"><span class="pre">""</span></code> is now a valid value and is no longer automatically converted to an
|
|||
|
empty string. For empty strings, use <code class="docutils literal notranslate"><span class="pre">"option</span> <span class="pre">="</span></code> in a line.</p></li>
|
|||
|
</ul>
|
|||
|
</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 was reworked extensively, meaning that its APIs
|
|||
|
are often incompatible with the 3.1 APIs.</p></li>
|
|||
|
<li><p><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 can no longer be used as filenames; instead,
|
|||
|
they should be converted to <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></li>
|
|||
|
<li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.tostring()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.fromstring()</span></code> have been renamed to
|
|||
|
<code class="xref py py-meth docutils literal notranslate"><span class="pre">array.tobytes()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.frombytes()</span></code> for clarity. The old names
|
|||
|
have been deprecated. (See <a class="reference external" href="https://bugs.python.org/issue8990">bpo-8990</a>.)</p></li>
|
|||
|
<li><p><code class="docutils literal notranslate"><span class="pre">PyArg_Parse*()</span></code> functions:</p>
|
|||
|
<ul class="simple">
|
|||
|
<li><p>“t#” format has been removed: use “s#” or “s*” instead</p></li>
|
|||
|
<li><p>“w” and “w#” formats has been removed: use “w*” instead</p></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li><p>The <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> type, deprecated in 3.1, has been removed. To wrap
|
|||
|
opaque C pointers in Python objects, the <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> API should be used
|
|||
|
instead; the new type has a well-defined interface for passing typing safety
|
|||
|
information and a less complicated signature for calling a destructor.</p></li>
|
|||
|
<li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setfilesystemencoding()</span></code> function was removed because
|
|||
|
it had a flawed design.</p></li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/random.html#random.seed" title="random.seed"><code class="xref py py-func docutils literal notranslate"><span class="pre">random.seed()</span></code></a> function and method now salt string seeds with an
|
|||
|
sha512 hash function. To access the previous version of <em>seed</em> in order to
|
|||
|
reproduce Python 3.1 sequences, set the <em>version</em> argument to <em>1</em>,
|
|||
|
<code class="docutils literal notranslate"><span class="pre">random.seed(s,</span> <span class="pre">version=1)</span></code>.</p></li>
|
|||
|
<li><p>The previously deprecated <code class="xref py py-func docutils literal notranslate"><span class="pre">string.maketrans()</span></code> function has been removed
|
|||
|
in favor of the static methods <a class="reference internal" href="../library/stdtypes.html#bytes.maketrans" title="bytes.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.maketrans()</span></code></a> and
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#bytearray.maketrans" title="bytearray.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytearray.maketrans()</span></code></a>. This change solves the confusion around which
|
|||
|
types were supported by the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module. Now, <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>,
|
|||
|
<a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> each have their own <strong>maketrans</strong> and
|
|||
|
<strong>translate</strong> methods with intermediate translation tables of the appropriate
|
|||
|
type.</p>
|
|||
|
<p>(Contributed by Georg Brandl; <a class="reference external" href="https://bugs.python.org/issue5675">bpo-5675</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The previously deprecated <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code> function has been removed
|
|||
|
in favor of a plain <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 which can accept multiple
|
|||
|
context managers. The latter technique is faster (because it is built-in),
|
|||
|
and it does a better job finalizing multiple context managers when one of them
|
|||
|
raises an exception:</p>
|
|||
|
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'mylog.txt'</span><span class="p">)</span> <span class="k">as</span> <span class="n">infile</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'a.out'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
|
|||
|
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">infile</span><span class="p">:</span>
|
|||
|
<span class="k">if</span> <span class="s1">'<critical>'</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
|
|||
|
<span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
|||
|
</pre></div>
|
|||
|
</div>
|
|||
|
<p>(Contributed by Georg Brandl and Mattias Brändström;
|
|||
|
<a class="reference external" href="https://codereview.appspot.com/53094">appspot issue 53094</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p><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">struct.pack()</span></code></a> now only allows bytes for the <code class="docutils literal notranslate"><span class="pre">s</span></code> string pack code.
|
|||
|
Formerly, it would accept text arguments and implicitly encode them to bytes
|
|||
|
using UTF-8. This was problematic because it made assumptions about the
|
|||
|
correct encoding and because a variable-length encoding can fail when writing
|
|||
|
to fixed length segment of a structure.</p>
|
|||
|
<p>Code such as <code class="docutils literal notranslate"><span class="pre">struct.pack('<6sHHBBB',</span> <span class="pre">'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></code> should be rewritten
|
|||
|
with to use bytes instead of text, <code class="docutils literal notranslate"><span class="pre">struct.pack('<6sHHBBB',</span> <span class="pre">b'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></code>.</p>
|
|||
|
<p>(Discovered by David Beazley and fixed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue10783">bpo-10783</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-class docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> class now raises an
|
|||
|
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.ParseError" title="xml.etree.ElementTree.ParseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">xml.etree.ElementTree.ParseError</span></code></a> when a parse fails. Previously it
|
|||
|
raised an <a class="reference internal" href="../library/pyexpat.html#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">xml.parsers.expat.ExpatError</span></code></a>.</p></li>
|
|||
|
<li><p>The new, longer <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> value on floats may break doctests which rely on
|
|||
|
the old output format.</p></li>
|
|||
|
<li><p>In <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a>, the default value for <em>close_fds</em> is now
|
|||
|
<code class="docutils literal notranslate"><span class="pre">True</span></code> under Unix; under Windows, it is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the three standard
|
|||
|
streams are set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. Previously, <em>close_fds</em>
|
|||
|
was always <code class="docutils literal notranslate"><span class="pre">False</span></code> by default, which produced difficult to solve bugs
|
|||
|
or race conditions when open file descriptors would leak into the child
|
|||
|
process.</p></li>
|
|||
|
<li><p>Support for legacy HTTP 0.9 has been removed from <a class="reference internal" href="../library/urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a>
|
|||
|
and <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a>. Such support is still present on the server side
|
|||
|
(in <a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a>).</p>
|
|||
|
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue10711">bpo-10711</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>SSL sockets in timeout mode now raise <a class="reference internal" href="../library/socket.html#socket.timeout" title="socket.timeout"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.timeout</span></code></a> when a timeout
|
|||
|
occurs, rather than a generic <a class="reference internal" href="../library/ssl.html#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a>.</p>
|
|||
|
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue10272">bpo-10272</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>The misleading functions <a class="reference internal" href="../c-api/init.html#c.PyEval_AcquireLock" title="PyEval_AcquireLock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_AcquireLock()</span></code></a> and
|
|||
|
<a class="reference internal" href="../c-api/init.html#c.PyEval_ReleaseLock" title="PyEval_ReleaseLock"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ReleaseLock()</span></code></a> have been officially deprecated. The
|
|||
|
thread-state aware APIs (such as <a class="reference internal" href="../c-api/init.html#c.PyEval_SaveThread" title="PyEval_SaveThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SaveThread()</span></code></a>
|
|||
|
and <a class="reference internal" href="../c-api/init.html#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a>) should be used instead.</p></li>
|
|||
|
<li><p>Due to security risks, <code class="xref py py-func docutils literal notranslate"><span class="pre">asyncore.handle_accept()</span></code> has been deprecated, and
|
|||
|
a new function, <code class="xref py py-func docutils literal notranslate"><span class="pre">asyncore.handle_accepted()</span></code>, was added to replace it.</p>
|
|||
|
<p>(Contributed by Giampaolo Rodola in <a class="reference external" href="https://bugs.python.org/issue6706">bpo-6706</a>.)</p>
|
|||
|
</li>
|
|||
|
<li><p>Due to the new <a class="reference internal" href="../glossary.html#term-gil"><span class="xref std std-term">GIL</span></a> implementation, <a class="reference internal" href="../c-api/init.html#c.PyEval_InitThreads" title="PyEval_InitThreads"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_InitThreads()</span></code></a>
|
|||
|
cannot be called before <a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> anymore.</p></li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
|
|||
|
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
|||
|
<div class="sphinxsidebarwrapper">
|
|||
|
<h3><a href="../contents.html">Table of Contents</a></h3>
|
|||
|
<ul>
|
|||
|
<li><a class="reference internal" href="#">What’s New In Python 3.2</a><ul>
|
|||
|
<li><a class="reference internal" href="#pep-384-defining-a-stable-abi">PEP 384: Defining a Stable ABI</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-389-argparse-command-line-parsing-module">PEP 389: Argparse Command Line Parsing Module</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary Based Configuration for Logging</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-3148-the-concurrent-futures-module">PEP 3148: The <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> module</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-3147-pyc-repository-directories">PEP 3147: PYC Repository Directories</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-3149-abi-version-tagged-so-files">PEP 3149: ABI Version Tagged .so Files</a></li>
|
|||
|
<li><a class="reference internal" href="#pep-3333-python-web-server-gateway-interface-v1-0-1">PEP 3333: Python Web Server Gateway Interface v1.0.1</a></li>
|
|||
|
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
|
|||
|
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
|
|||
|
<li><a class="reference internal" href="#email">email</a></li>
|
|||
|
<li><a class="reference internal" href="#elementtree">elementtree</a></li>
|
|||
|
<li><a class="reference internal" href="#functools">functools</a></li>
|
|||
|
<li><a class="reference internal" href="#itertools">itertools</a></li>
|
|||
|
<li><a class="reference internal" href="#collections">collections</a></li>
|
|||
|
<li><a class="reference internal" href="#threading">threading</a></li>
|
|||
|
<li><a class="reference internal" href="#datetime-and-time">datetime and time</a></li>
|
|||
|
<li><a class="reference internal" href="#math">math</a></li>
|
|||
|
<li><a class="reference internal" href="#abc">abc</a></li>
|
|||
|
<li><a class="reference internal" href="#io">io</a></li>
|
|||
|
<li><a class="reference internal" href="#reprlib">reprlib</a></li>
|
|||
|
<li><a class="reference internal" href="#logging">logging</a></li>
|
|||
|
<li><a class="reference internal" href="#csv">csv</a></li>
|
|||
|
<li><a class="reference internal" href="#contextlib">contextlib</a></li>
|
|||
|
<li><a class="reference internal" href="#decimal-and-fractions">decimal and fractions</a></li>
|
|||
|
<li><a class="reference internal" href="#ftp">ftp</a></li>
|
|||
|
<li><a class="reference internal" href="#popen">popen</a></li>
|
|||
|
<li><a class="reference internal" href="#select">select</a></li>
|
|||
|
<li><a class="reference internal" href="#gzip-and-zipfile">gzip and zipfile</a></li>
|
|||
|
<li><a class="reference internal" href="#tarfile">tarfile</a></li>
|
|||
|
<li><a class="reference internal" href="#hashlib">hashlib</a></li>
|
|||
|
<li><a class="reference internal" href="#ast">ast</a></li>
|
|||
|
<li><a class="reference internal" href="#os">os</a></li>
|
|||
|
<li><a class="reference internal" href="#shutil">shutil</a></li>
|
|||
|
<li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
|
|||
|
<li><a class="reference internal" href="#html">html</a></li>
|
|||
|
<li><a class="reference internal" href="#socket">socket</a></li>
|
|||
|
<li><a class="reference internal" href="#ssl">ssl</a></li>
|
|||
|
<li><a class="reference internal" href="#nntp">nntp</a></li>
|
|||
|
<li><a class="reference internal" href="#certificates">certificates</a></li>
|
|||
|
<li><a class="reference internal" href="#imaplib">imaplib</a></li>
|
|||
|
<li><a class="reference internal" href="#http-client">http.client</a></li>
|
|||
|
<li><a class="reference internal" href="#unittest">unittest</a></li>
|
|||
|
<li><a class="reference internal" href="#random">random</a></li>
|
|||
|
<li><a class="reference internal" href="#poplib">poplib</a></li>
|
|||
|
<li><a class="reference internal" href="#asyncore">asyncore</a></li>
|
|||
|
<li><a class="reference internal" href="#tempfile">tempfile</a></li>
|
|||
|
<li><a class="reference internal" href="#inspect">inspect</a></li>
|
|||
|
<li><a class="reference internal" href="#pydoc">pydoc</a></li>
|
|||
|
<li><a class="reference internal" href="#dis">dis</a></li>
|
|||
|
<li><a class="reference internal" href="#dbm">dbm</a></li>
|
|||
|
<li><a class="reference internal" href="#ctypes">ctypes</a></li>
|
|||
|
<li><a class="reference internal" href="#site">site</a></li>
|
|||
|
<li><a class="reference internal" href="#sysconfig">sysconfig</a></li>
|
|||
|
<li><a class="reference internal" href="#pdb">pdb</a></li>
|
|||
|
<li><a class="reference internal" href="#configparser">configparser</a></li>
|
|||
|
<li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
|
|||
|
<li><a class="reference internal" href="#mailbox">mailbox</a></li>
|
|||
|
<li><a class="reference internal" href="#turtledemo">turtledemo</a></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
<li><a class="reference internal" href="#multi-threading">Multi-threading</a></li>
|
|||
|
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
|
|||
|
<li><a class="reference internal" href="#unicode">Unicode</a></li>
|
|||
|
<li><a class="reference internal" href="#codecs">Codecs</a></li>
|
|||
|
<li><a class="reference internal" href="#documentation">Documentation</a></li>
|
|||
|
<li><a class="reference internal" href="#idle">IDLE</a></li>
|
|||
|
<li><a class="reference internal" href="#code-repository">Code Repository</a></li>
|
|||
|
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a></li>
|
|||
|
<li><a class="reference internal" href="#porting-to-python-3-2">Porting to Python 3.2</a></li>
|
|||
|
</ul>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<h4>Previous topic</h4>
|
|||
|
<p class="topless"><a href="3.3.html"
|
|||
|
title="previous chapter">What’s New In Python 3.3</a></p>
|
|||
|
<h4>Next topic</h4>
|
|||
|
<p class="topless"><a href="3.1.html"
|
|||
|
title="next chapter">What’s New In Python 3.1</a></p>
|
|||
|
<div role="note" aria-label="source link">
|
|||
|
<h3>This Page</h3>
|
|||
|
<ul class="this-page-menu">
|
|||
|
<li><a href="../bugs.html">Report a Bug</a></li>
|
|||
|
<li>
|
|||
|
<a href="https://github.com/python/cpython/blob/3.7/Doc/whatsnew/3.2.rst"
|
|||
|
rel="nofollow">Show Source
|
|||
|
</a>
|
|||
|
</li>
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
</div>
|
|||
|
<div class="clearer"></div>
|
|||
|
</div>
|
|||
|
<div class="related" role="navigation" aria-label="related navigation">
|
|||
|
<h3>Navigation</h3>
|
|||
|
<ul>
|
|||
|
<li class="right" style="margin-right: 10px">
|
|||
|
<a href="../genindex.html" title="General Index"
|
|||
|
>index</a></li>
|
|||
|
<li class="right" >
|
|||
|
<a href="../py-modindex.html" title="Python Module Index"
|
|||
|
>modules</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="3.1.html" title="What’s New In Python 3.1"
|
|||
|
>next</a> |</li>
|
|||
|
<li class="right" >
|
|||
|
<a href="3.3.html" title="What’s New In Python 3.3"
|
|||
|
>previous</a> |</li>
|
|||
|
<li><img src="../_static/py.png" alt=""
|
|||
|
style="vertical-align: middle; margin-top: -1px"/></li>
|
|||
|
<li><a href="https://www.python.org/">Python</a> »</li>
|
|||
|
<li>
|
|||
|
<span class="language_switcher_placeholder">en</span>
|
|||
|
<span class="version_switcher_placeholder">3.7.4</span>
|
|||
|
<a href="../index.html">Documentation </a> »
|
|||
|
</li>
|
|||
|
|
|||
|
<li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> »</li>
|
|||
|
<li class="right">
|
|||
|
|
|||
|
|
|||
|
<div class="inline-search" style="display: none" role="search">
|
|||
|
<form class="inline-search" action="../search.html" method="get">
|
|||
|
<input placeholder="Quick search" type="text" name="q" />
|
|||
|
<input type="submit" value="Go" />
|
|||
|
<input type="hidden" name="check_keywords" value="yes" />
|
|||
|
<input type="hidden" name="area" value="default" />
|
|||
|
</form>
|
|||
|
</div>
|
|||
|
<script type="text/javascript">$('.inline-search').show(0);</script>
|
|||
|
|
|
|||
|
</li>
|
|||
|
|
|||
|
</ul>
|
|||
|
</div>
|
|||
|
<div class="footer">
|
|||
|
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
|||
|
<br />
|
|||
|
The Python Software Foundation is a non-profit corporation.
|
|||
|
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
|||
|
<br />
|
|||
|
Last updated on Jul 13, 2019.
|
|||
|
<a href="../bugs.html">Found a bug</a>?
|
|||
|
<br />
|
|||
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
|||
|
</div>
|
|||
|
|
|||
|
</body>
|
|||
|
</html>
|