Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 917

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>What’s New In Python 3.2 &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="What’s New in Python" href="index.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" href="index.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <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="index.html" title="What’s New in Python"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" accesskey="U">What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="what-s-new-in-python-3-2">
<h1>What&#8217;s New In Python 3.2<a class="headerlink" href="#what-s-new-in-python-3-2" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">Raymond Hettinger</td>
</tr>
<tr class="field"><th class="field-name">Release:</th><td class="field-body">3.2.2</td>
</tr>
<tr class="field"><th class="field-name">Date:</th><td class="field-body">September 04, 2011</td>
</tr>
</tbody>
</table>
<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="http://hg.python.org/cpython/file/3.2/Misc/NEWS">Misc/NEWS</a> file.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><span class="target" id="index-0"></span><a class="pep reference external" href="http://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-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0384"><strong>PEP 384</strong></a> - Defining a Stable ABI</dt>
<dd>PEP written by Martin von Löwis.</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."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></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)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></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&#8217;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"><div class="highlight"><pre><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="s">&#39;Manage servers&#39;</span><span class="p">,</span>         <span class="c"># main description for help</span>
            <span class="n">epilog</span> <span class="o">=</span> <span class="s">&#39;Tested on Solaris and Linux&#39;</span><span class="p">)</span> <span class="c"># 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="s">&#39;action&#39;</span><span class="p">,</span>                       <span class="c"># argument name</span>
            <span class="n">choices</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;deploy&#39;</span><span class="p">,</span> <span class="s">&#39;start&#39;</span><span class="p">,</span> <span class="s">&#39;stop&#39;</span><span class="p">],</span>  <span class="c"># three allowed values</span>
            <span class="n">help</span> <span class="o">=</span> <span class="s">&#39;action on each target&#39;</span><span class="p">)</span>         <span class="c"># help msg</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;targets&#39;</span><span class="p">,</span>
            <span class="n">metavar</span> <span class="o">=</span> <span class="s">&#39;HOSTNAME&#39;</span><span class="p">,</span>                   <span class="c"># var name used in help msg</span>
            <span class="n">nargs</span> <span class="o">=</span> <span class="s">&#39;+&#39;</span><span class="p">,</span>                            <span class="c"># require one or more targets</span>
            <span class="n">help</span> <span class="o">=</span> <span class="s">&#39;url for target machines&#39;</span><span class="p">)</span>       <span class="c"># 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="s">&#39;-u&#39;</span><span class="p">,</span> <span class="s">&#39;--user&#39;</span><span class="p">,</span>                 <span class="c"># -u or --user option</span>
            <span class="n">required</span> <span class="o">=</span> <span class="k">True</span><span class="p">,</span>                        <span class="c"># make it a required argument</span>
            <span class="n">help</span> <span class="o">=</span> <span class="s">&#39;login as user&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of calling the parser on a command string:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">cmd</span>  <span class="o">=</span> <span class="s">&#39;deploy sneezy.example.com sleepy.example.com -u skycaptain&#39;</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">action</span>
<span class="go">&#39;deploy&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">targets</span>
<span class="go">[&#39;sneezy.example.com&#39;, &#39;sleepy.example.com&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">user</span>
<span class="go">&#39;skycaptain&#39;</span>
</pre></div>
</div>
<p>Example of the parser&#8217;s automatically generated help:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s">&#39;-h&#39;</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."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a> feature is the ability to define subparsers,
each with their own argument patterns and help displays:</p>
<div class="highlight-python3"><pre>import argparse
parser = argparse.ArgumentParser(prog='HELM')
subparsers = parser.add_subparsers()

parser_l = subparsers.add_parser('launch', help='Launch Control')   # first subgroup
parser_l.add_argument('-m', '--missiles', action='store_true')
parser_l.add_argument('-t', '--torpedos', action='store_true')

parser_m = subparsers.add_parser('move', help='Move Vessel',        # second subgroup
                                 aliases=('steer', 'turn'))         # equivalent names
parser_m.add_argument('-c', '--course', type=int, required=True)
parser_m.add_argument('-s', '--speed', type=int, default=0)

$ ./helm.py --help                         # top level help (launch and move)
$ ./helm.py launch --help                  # help for launch options
$ ./helm.py launch --missiles              # set missiles=True and torpedos=False
$ ./helm.py steer --course 180 --speed 5   # set movement parameters</pre>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="docutils">
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0389"><strong>PEP 389</strong></a> - New Command Line Parsing Module</dt>
<dd>PEP written by Steven Bethard.</dd>
</dl>
<p class="last"><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><em>Upgrading optparse code</em></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)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></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 <tt class="xref py py-mod docutils literal"><span class="pre">ConfigParser</span></tt> 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"><tt class="xref py py-func docutils literal"><span class="pre">logging.config.dictConfig()</span></tt></a> for specifying logging configuration with
plain Python dictionaries.  The configuration options include formatters,
handlers, filters, and loggers.  Here&#8217;s a working example of a configuration
dictionary:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">{</span><span class="s">&quot;version&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
 <span class="s">&quot;formatters&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;brief&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;format&quot;</span><span class="p">:</span> <span class="s">&quot;%(levelname)-8s: %(name)-15s: %(message)s&quot;</span><span class="p">},</span>
                <span class="s">&quot;full&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;format&quot;</span><span class="p">:</span> <span class="s">&quot;%(asctime)s %(name)-15s %(levelname)-8s %(message)s&quot;</span><span class="p">}</span>
                <span class="p">},</span>
 <span class="s">&quot;handlers&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;console&quot;</span><span class="p">:</span> <span class="p">{</span>
                   <span class="s">&quot;class&quot;</span><span class="p">:</span> <span class="s">&quot;logging.StreamHandler&quot;</span><span class="p">,</span>
                   <span class="s">&quot;formatter&quot;</span><span class="p">:</span> <span class="s">&quot;brief&quot;</span><span class="p">,</span>
                   <span class="s">&quot;level&quot;</span><span class="p">:</span> <span class="s">&quot;INFO&quot;</span><span class="p">,</span>
                   <span class="s">&quot;stream&quot;</span><span class="p">:</span> <span class="s">&quot;ext://sys.stdout&quot;</span><span class="p">},</span>
              <span class="s">&quot;console_priority&quot;</span><span class="p">:</span> <span class="p">{</span>
                   <span class="s">&quot;class&quot;</span><span class="p">:</span> <span class="s">&quot;logging.StreamHandler&quot;</span><span class="p">,</span>
                   <span class="s">&quot;formatter&quot;</span><span class="p">:</span> <span class="s">&quot;full&quot;</span><span class="p">,</span>
                   <span class="s">&quot;level&quot;</span><span class="p">:</span> <span class="s">&quot;ERROR&quot;</span><span class="p">,</span>
                   <span class="s">&quot;stream&quot;</span><span class="p">:</span> <span class="s">&quot;ext://sys.stderr&quot;</span><span class="p">}</span>
              <span class="p">},</span>
 <span class="s">&quot;root&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s">&quot;level&quot;</span><span class="p">:</span> <span class="s">&quot;DEBUG&quot;</span><span class="p">,</span> <span class="s">&quot;handlers&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s">&quot;console&quot;</span><span class="p">,</span> <span class="s">&quot;console_priority&quot;</span><span class="p">]}}</span>
</pre></div>
</div>
<p>If that dictionary is stored in a file called <tt class="file docutils literal"><span class="pre">conf.json</span></tt>, it can be
loaded and called with code like this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span><span class="o">,</span> <span class="nn">logging.config</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;conf.json&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="go">        conf = json.load(f)</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&quot;Transaction completed normally&quot;</span><span class="p">)</span>
<span class="go">INFO    : root           : Transaction completed normally</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s">&quot;Abnormal termination&quot;</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-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0391"><strong>PEP 391</strong></a> - Dictionary Based Configuration for Logging</dt>
<dd>PEP written by Vinay Sajip.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3148-the-concurrent-futures-module">
<h2>PEP 3148:  The <tt class="docutils literal"><span class="pre">concurrent.futures</span></tt> 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."><tt class="xref py py-mod docutils literal"><span class="pre">concurrent.futures</span></tt></a> was inspired by
<em>java.util.concurrent.package</em>.  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"><tt class="xref py py-class docutils literal"><span class="pre">Future</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">submit()</span></tt></a> for scheduling a callable and
returning a <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><tt class="xref py py-class docutils literal"><span class="pre">Future</span></tt></a> object;
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><tt class="xref py py-meth docutils literal"><span class="pre">map()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">shutdown()</span></tt></a> for freeing
resources.  The class is a <a class="reference internal" href="../glossary.html#term-context-manager"><em class="xref std std-term">context manager</em></a> and can be used in a
<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">ThreadPoolExecutor</span></tt></a> is a
launch of four parallel threads for copying files:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;src1.txt&#39;</span><span class="p">,</span> <span class="s">&#39;dest1.txt&#39;</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="s">&#39;src2.txt&#39;</span><span class="p">,</span> <span class="s">&#39;dest2.txt&#39;</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="s">&#39;src3.txt&#39;</span><span class="p">,</span> <span class="s">&#39;dest3.txt&#39;</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="s">&#39;src4.txt&#39;</span><span class="p">,</span> <span class="s">&#39;dest4.txt&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="docutils">
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3148"><strong>PEP 3148</strong></a> - Futures &#8211; Execute Computations Asynchronously</dt>
<dd>PEP written by Brian Quinlan.</dd>
</dl>
<p><a class="reference internal" href="../library/concurrent.futures.html#threadpoolexecutor-example"><em>Code for Threaded Parallel URL reads</em></a>, an
example using threads to fetch multiple web pages in parallel.</p>
<p class="last"><a class="reference internal" href="../library/concurrent.futures.html#processpoolexecutor-example"><em>Code for computing prime numbers in
parallel</em></a>, an example demonstrating
<a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><tt class="xref py py-class docutils literal"><span class="pre">ProcessPoolExecutor</span></tt></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&#8217;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 &#8220;pyc fights&#8221; 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&#8217;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 &#8220;mymodule.pyc&#8221;, they will now
look for &#8220;mymodule.cpython-32.pyc&#8221;, &#8220;mymodule.cpython-33.pyc&#8221;, and
&#8220;mymodule.unladen10.pyc&#8221;.  And to prevent all of these new files from
cluttering source directories, the <em>pyc</em> files are now collected in a
&#8220;__pycache__&#8221; 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 class="first">Imported modules now have a <tt class="xref py py-attr docutils literal"><span class="pre">__cached__</span></tt> attribute which stores the name
of the actual file that was imported:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">collections</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">collections</span><span class="o">.</span><span class="n">__cached__</span>
<span class="go">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">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."><tt class="xref py py-mod docutils literal"><span class="pre">imp</span></tt></a>
module:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">imp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">get_tag</span><span class="p">()</span>
<span class="go">&#39;cpython-32&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">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 &#8220;c&#8221; from a &#8221;.pyc&#8221;
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."><tt class="xref py py-mod docutils literal"><span class="pre">imp</span></tt></a> module:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">source_from_cache</span><span class="p">(</span><span class="s">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span><span class="p">)</span>
<span class="go">&#39;c:/py32/lib/collections.py&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">cache_from_source</span><span class="p">(</span><span class="s">&#39;c:/py32/lib/collections.py&#39;</span><span class="p">)</span>
<span class="go">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">The <a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><tt class="xref py py-mod docutils literal"><span class="pre">py_compile</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">compileall</span></tt></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: <tt class="docutils literal"><span class="pre">-i</span></tt> for
specifying a list of files and directories to compile and <tt class="docutils literal"><span class="pre">-b</span></tt> which causes
bytecode files to be written to their legacy location rather than
<em>__pycache__</em>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/importlib.html#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><tt class="xref py py-mod docutils literal"><span class="pre">importlib.abc</span></tt></a> module has been updated with new <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base
classes</em></a> for loading bytecode files.  The obsolete
ABCs, <a class="reference internal" href="../library/importlib.html#importlib.abc.PyLoader" title="importlib.abc.PyLoader"><tt class="xref py py-class docutils literal"><span class="pre">PyLoader</span></tt></a> and
<a class="reference internal" href="../library/importlib.html#importlib.abc.PyPycLoader" title="importlib.abc.PyPycLoader"><tt class="xref py py-class docutils literal"><span class="pre">PyPycLoader</span></tt></a>, have been deprecated (instructions on how
to stay Python 3.1 compatible are included with the documentation).</p>
</li>
</ul>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-5"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> - PYC Repository Directories</dt>
<dd>PEP written by Barry Warsaw.</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 &#8220;pyshared&#8221; 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 &#8220;d&#8221; for
debug, &#8220;m&#8221; for pymalloc, &#8220;u&#8221; for wide-unicode).  For an arbitrary package &#8220;foo&#8221;,
you may see these files when the distribution package is installed:</p>
<div class="highlight-python3"><div class="highlight"><pre><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"><tt class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></tt></a>
module:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sysconfig</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s">&#39;SOABI&#39;</span><span class="p">)</span>    <span class="c"># find the version tag</span>
<span class="go">&#39;cpython-32mu&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s">&#39;SO&#39;</span><span class="p">)</span>       <span class="c"># find the full filename extension</span>
<span class="go">&#39;.cpython-32mu.so&#39;</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-6"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3149"><strong>PEP 3149</strong></a> - ABI Version Tagged .so Files</dt>
<dd>PEP written by Barry Warsaw.</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/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></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/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></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
<tt class="xref py py-func docutils literal"><span class="pre">start_response()</span></tt> function.  They must follow <span class="target" id="index-7"></span><a class="rfc reference external" href="http://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="http://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>If the app already used strings for headers in Python 2, no change is needed.</li>
<li>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 <tt class="docutils literal"><span class="pre">h.encode('utf-8')</span></tt> now needs to convert from
bytes to native strings using <tt class="docutils literal"><span class="pre">h.encode('utf-8').decode('latin-1')</span></tt>.</li>
<li>Values yielded by an application or sent using the <tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt> method
must be byte strings.  The <tt class="xref py py-func docutils literal"><span class="pre">start_response()</span></tt> function and environ
must use native strings.  The two cannot be mixed.</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."><tt class="xref py py-mod docutils literal"><span class="pre">wsgiref</span></tt></a> module has a new function,
<a class="reference internal" href="../library/wsgiref.html#wsgiref.handlers.read_environ" title="wsgiref.handlers.read_environ"><tt class="xref py py-func docutils literal"><span class="pre">wsgiref.handlers.read_environ()</span></tt></a> for transcoding CGI variables from
<a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><tt class="xref py py-attr docutils literal"><span class="pre">os.environ</span></tt></a> into native strings and returning a new dictionary.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3333"><strong>PEP 3333</strong></a> - Python Web Server Gateway Interface v1.0.1</dt>
<dd>PEP written by Phillip Eby.</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 class="first">String formatting for <a class="reference internal" href="../library/functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></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 &#8216;0b&#8217;,
&#8216;0o&#8217;, or &#8216;0x&#8217; 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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="s">&#39;#o&#39;</span><span class="p">)</span>
<span class="go">&#39;0o24&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mf">12.34</span><span class="p">,</span> <span class="s">&#39;#5.0f&#39;</span><span class="p">)</span>
<span class="go">&#39;  12.&#39;</span>
</pre></div>
</div>
<p>(Suggested by Mark Dickinson and implemented by Eric Smith in <a class="reference external" href="http://bugs.python.org/issue7094">issue 7094</a>.)</p>
</li>
<li><p class="first">There is also a new <a class="reference internal" href="../library/stdtypes.html#str.format_map" title="str.format_map"><tt class="xref py py-meth docutils literal"><span class="pre">str.format_map()</span></tt></a> method that extends the
capabilities of the existing <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> method by accepting arbitrary
<a class="reference internal" href="../glossary.html#term-mapping"><em class="xref std std-term">mapping</em></a> objects.  This new method makes it possible to use string
formatting with any of Python&#8217;s many dictionary-like objects such as
<a class="reference internal" href="../library/collections.html#collections.defaultdict" title="collections.defaultdict"><tt class="xref py py-class docutils literal"><span class="pre">defaultdict</span></tt></a>, <a class="reference internal" href="../library/shelve.html#shelve.Shelf" title="shelve.Shelf"><tt class="xref py py-class docutils literal"><span class="pre">Shelf</span></tt></a>,
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><tt class="xref py py-class docutils literal"><span class="pre">ConfigParser</span></tt></a>, or <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><tt class="xref py py-mod docutils literal"><span class="pre">dbm</span></tt></a>.  It is also useful with
custom <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> subclasses that normalize keys before look-up or that
supply a <tt class="xref py py-meth docutils literal"><span class="pre">__missing__()</span></tt> method for unknown keys:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shelve</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;tmp.shl&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;The {project_name} status is {status} as of {date}&#39;</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">&#39;The testing project status is green as of February 15, 2011&#39;</span>

<span class="gp">&gt;&gt;&gt; </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="go">        def __getitem__(self, key):</span>
<span class="go">            return dict.__getitem__(self, key.lower())</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;widgets&#39;</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">&gt;&gt;&gt; </span><span class="s">&#39;There are {QUANTITY} {Part} in stock&#39;</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">&#39;There are 10 widgets in stock&#39;</span>

<span class="gp">&gt;&gt;&gt; </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="go">        def __missing__(self, key):</span>
<span class="go">            return &#39;&lt;{}&gt;&#39;.format(key)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Hello {name}, welcome to {location}&#39;</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">&#39;Hello &lt;name&gt;, welcome to &lt;location&gt;&#39;</span>
</pre></div>
</div>
</li>
</ul>
<blockquote>
<div>(Suggested by Raymond Hettinger and implemented by Eric Smith in
<a class="reference external" href="http://bugs.python.org/issue6081">issue 6081</a>.)</div></blockquote>
<ul>
<li><p class="first">The interpreter can now be started with a quiet option, <tt class="docutils literal"><span class="pre">-q</span></tt>, 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"><tt class="xref py py-attr docutils literal"><span class="pre">sys.flags</span></tt></a> attribute:</p>
<div class="highlight-python3"><pre>$ python -q
&gt;&gt;&gt; sys.flags
sys.flags(debug=0, division_warning=0, inspect=0, interactive=0,
optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,
ignore_environment=0, verbose=0, bytes_warning=0, quiet=1)</pre>
</div>
<p>(Contributed by Marcin Wojdyr in <a class="reference external" href="http://bugs.python.org/issue1772833">issue 1772833</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a> function works by calling <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></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__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__()</span></tt></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"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a> and let other
exceptions pass through:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="go">        @property</span>
<span class="go">        def f(self):</span>
<span class="go">            return 1 // 0</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s">&#39;f&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="nc">ZeroDivisionError</span>: <span class="n-Identifier">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="http://bugs.python.org/issue9666">issue 9666</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> of a float or complex number is now the same as its
<a class="reference internal" href="../library/functions.html#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>. Previously, the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> is displayed by default:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
<span class="gp">&gt;&gt;&gt; </span><span class="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">&#39;3.141592653589793&#39;</span>
<span class="gp">&gt;&gt;&gt; </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">&#39;3.141592653589793&#39;</span>
</pre></div>
</div>
<p>(Proposed and implemented by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue9337">issue 9337</a>.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><tt class="xref py py-class docutils literal"><span class="pre">memoryview</span></tt></a> objects now have a <a class="reference internal" href="../library/stdtypes.html#memoryview.release" title="memoryview.release"><tt class="xref py py-meth docutils literal"><span class="pre">release()</span></tt></a> method
and they also now support the context manager protocol.  This allows timely
release of any resources that were acquired when requesting a buffer from the
original object.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">b</span><span class="s">&#39;abcdefgh&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">v</span><span class="p">:</span>
<span class="go">        print(v.tolist())</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="http://bugs.python.org/issue9757">issue 9757</a>.)</p>
</li>
<li><p class="first">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"><div class="highlight"><pre><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"><tt class="xref std std-keyword docutils literal"><span class="pre">except</span></tt></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"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a> with Python 3.1 and now works again:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="c"># implicit &quot;del e&quot; here</span>
</pre></div>
</div>
<p>(See <a class="reference external" href="http://bugs.python.org/issue4617">issue 4617</a>.)</p>
</li>
<li><p class="first">The internal <tt class="xref c c-type docutils literal"><span class="pre">structsequence</span></tt> 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"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a>,
<a class="reference internal" href="../library/time.html#time.gmtime" title="time.gmtime"><tt class="xref py py-func docutils literal"><span class="pre">time.gmtime()</span></tt></a>, and <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><tt class="xref py py-attr docutils literal"><span class="pre">sys.version_info</span></tt></a> now work like a
<a class="reference internal" href="../glossary.html#term-named-tuple"><em class="xref std std-term">named tuple</em></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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="s">&#39;Version %d.%d.%d %s(%d)&#39;</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span>
<span class="go">&#39;Version 3.2.0 final(0)&#39;</span>
</pre></div>
</div>
<p>(Suggested by Arfrever Frehtes Taifersar Arahesis and implemented
by Benjamin Peterson in <a class="reference external" href="http://bugs.python.org/issue8413">issue 8413</a>.)</p>
</li>
<li><p class="first">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"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONWARNINGS</span></tt></a>
environment variable as an alternative to using <tt class="docutils literal"><span class="pre">-W</span></tt> at the command line:</p>
<div class="highlight-python3"><pre>$ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'</pre>
</div>
<p>(Suggested by Barry Warsaw and implemented by Philip Jenvey in <a class="reference external" href="http://bugs.python.org/issue7301">issue 7301</a>.)</p>
</li>
<li><p class="first">A new warning category, <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><tt class="xref py py-exc docutils literal"><span class="pre">ResourceWarning</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a>
module, or on the command line.</p>
<p>A <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><tt class="xref py py-exc docutils literal"><span class="pre">ResourceWarning</span></tt></a> is issued at interpreter shutdown if the
<a class="reference internal" href="../library/gc.html#gc.garbage" title="gc.garbage"><tt class="xref py py-data docutils literal"><span class="pre">gc.garbage</span></tt></a> list isn&#8217;t empty, and if <a class="reference internal" href="../library/gc.html#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><tt class="xref py py-attr docutils literal"><span class="pre">gc.DEBUG_UNCOLLECTABLE</span></tt></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"><tt class="xref py py-exc docutils literal"><span class="pre">ResourceWarning</span></tt></a> is also issued when a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></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-python3"><pre>$ python -q -Wdefault
&gt;&gt;&gt; f = open("foo", "wb")
&gt;&gt;&gt; del f
__main__:1: ResourceWarning: unclosed file &lt;_io.BufferedWriter name='foo'&gt;</pre>
</div>
<p>(Added by Antoine Pitrou and Georg Brandl in <a class="reference external" href="http://bugs.python.org/issue10093">issue 10093</a> and <a class="reference external" href="http://bugs.python.org/issue477863">issue 477863</a>.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/functions.html#range" title="range"><tt class="xref py py-class docutils literal"><span class="pre">range</span></tt></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
<a class="reference internal" href="../library/collections.html#collections.Sequence" title="collections.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">collections.Sequence</span></tt></a> <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base class</em></a>.  As a result, the
language will have a more uniform API.  In addition, <a class="reference internal" href="../library/functions.html#range" title="range"><tt class="xref py py-class docutils literal"><span class="pre">range</span></tt></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"><tt class="xref py py-attr docutils literal"><span class="pre">sys.maxsize</span></tt></a>.  This makes <em>range</em> more interoperable with lists:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="http://bugs.python.org/issue9213">issue 9213</a>, by Alexander Belopolsky
in <a class="reference external" href="http://bugs.python.org/issue2690">issue 2690</a>, and by Nick Coghlan in <a class="reference external" href="http://bugs.python.org/issue10889">issue 10889</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#callable" title="callable"><tt class="xref py py-func docutils literal"><span class="pre">callable()</span></tt></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"><em class="xref std std-term">abstract base class</em></a> in an
expression like <tt class="docutils literal"><span class="pre">isinstance(x,</span> <span class="pre">collections.Callable)</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="http://bugs.python.org/issue10518">issue 10518</a>.)</p>
</li>
<li><p class="first">Python&#8217;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>(Required extensive work by Victor Stinner in <a class="reference external" href="http://bugs.python.org/issue9425">issue 9425</a>.)</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&#8217;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, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a> package, <a class="reference internal" href="../library/mailbox.html#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><tt class="xref py py-mod docutils literal"><span class="pre">mailbox</span></tt></a>
module, and <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">nntplib</span></tt></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"><em class="xref std std-term">context manager</em></a> to support
convenient and reliable resource clean-up using a <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></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, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></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/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> rather than <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></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 class="first">New functions <a class="reference internal" href="../library/email.parser.html#email.message_from_bytes" title="email.message_from_bytes"><tt class="xref py py-func docutils literal"><span class="pre">message_from_bytes()</span></tt></a> and
<a class="reference internal" href="../library/email.parser.html#email.message_from_binary_file" title="email.message_from_binary_file"><tt class="xref py py-func docutils literal"><span class="pre">message_from_binary_file()</span></tt></a>, and new classes
<a class="reference internal" href="../library/email.parser.html#email.parser.BytesFeedParser" title="email.parser.BytesFeedParser"><tt class="xref py py-class docutils literal"><span class="pre">BytesFeedParser</span></tt></a> and <a class="reference internal" href="../library/email.parser.html#email.parser.BytesParser" title="email.parser.BytesParser"><tt class="xref py py-class docutils literal"><span class="pre">BytesParser</span></tt></a>
allow binary message data to be parsed into model objects.</p>
</li>
<li><p class="first">Given bytes input to the model, <a class="reference internal" href="../library/email.message.html#email.message.Message.get_payload" title="email.message.Message.get_payload"><tt class="xref py py-meth docutils literal"><span class="pre">get_payload()</span></tt></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 class="first">Given bytes input to the model, <a class="reference internal" href="../library/email.generator.html#email.generator.Generator" title="email.generator.Generator"><tt class="xref py py-class docutils literal"><span class="pre">Generator</span></tt></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="http://tools.ietf.org/html/rfc2047.html"><strong>RFC 2047</strong></a>-encoded
using the <em>unknown-8bit</em> character set.</p>
</li>
<li><p class="first">A new class <a class="reference internal" href="../library/email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><tt class="xref py py-class docutils literal"><span class="pre">BytesGenerator</span></tt></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 class="first">The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">smtplib</span></tt></a> <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><tt class="xref py py-class docutils literal"><span class="pre">SMTP</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">sendmail()</span></tt></a> method,
and a new method, <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><tt class="xref py py-meth docutils literal"><span class="pre">send_message()</span></tt></a> accepts a
<a class="reference internal" href="../library/email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></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="http://bugs.python.org/issue4661">issue 4661</a> and <a class="reference external" href="http://bugs.python.org/issue10321">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> package and its <tt class="xref py py-mod docutils literal"><span class="pre">xml.etree.cElementTree</span></tt>
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><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.fromstringlist" title="xml.etree.ElementTree.fromstringlist"><tt class="xref py py-func docutils literal"><span class="pre">xml.etree.ElementTree.fromstringlist()</span></tt></a> which builds an XML document
from a sequence of fragments</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.register_namespace" title="xml.etree.ElementTree.register_namespace"><tt class="xref py py-func docutils literal"><span class="pre">xml.etree.ElementTree.register_namespace()</span></tt></a> for registering a global
namespace prefix</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.tostringlist" title="xml.etree.ElementTree.tostringlist"><tt class="xref py py-func docutils literal"><span class="pre">xml.etree.ElementTree.tostringlist()</span></tt></a> for string representation
including all sublists</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.extend" title="xml.etree.ElementTree.Element.extend"><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.Element.extend()</span></tt></a> for appending a sequence of zero
or more elements</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.Element.iterfind()</span></tt></a> searches an element and
subelements</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.Element.itertext()</span></tt></a> creates a text iterator over
an element and its subelements</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.end" title="xml.etree.ElementTree.TreeBuilder.end"><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.TreeBuilder.end()</span></tt></a> closes the current element</li>
<li><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.TreeBuilder.doctype()</span></tt></a> handles a doctype
declaration</li>
</ul>
<p>Two methods have been deprecated:</p>
<ul class="simple">
<li><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.getchildren()</span></tt> use <tt class="docutils literal"><span class="pre">list(elem)</span></tt> instead.</li>
<li><tt class="xref py py-meth docutils literal"><span class="pre">xml.etree.ElementTree.getiterator()</span></tt> use <tt class="docutils literal"><span class="pre">Element.iter</span></tt> instead.</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&#8217;s website.</p>
<p>(Contributed by Florent Xicluna and Fredrik Lundh, <a class="reference external" href="http://bugs.python.org/issue6472">issue 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 class="first">The <a class="reference internal" href="../library/functools.html#module-functools" title="functools: Higher order functions and operations on callable objects."><tt class="xref py py-mod docutils literal"><span class="pre">functools</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">functools.lru_cache()</span></tt></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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">functools</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="go">        c = conn.cursor()</span>
<span class="go">        c.execute(&#39;SELECT phonenumber FROM phonelist WHERE name=?&#39;, (name,))</span>
<span class="go">        return c.fetchone()[0]</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="go">        get_phone_number(name)        # 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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">get_phone_number</span><span class="o">.</span><span class="n">cache_info</span><span class="p">()</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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="http://code.activestate.com/recipes/498245">recipe 498245</a>, <a class="reference external" href="http://code.activestate.com/recipes/577479">recipe 577479</a>, <a class="reference external" href="http://bugs.python.org/issue10586">issue 10586</a>, and
<a class="reference external" href="http://bugs.python.org/issue10593">issue 10593</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functools.html#functools.wraps" title="functools.wraps"><tt class="xref py py-func docutils literal"><span class="pre">functools.wraps()</span></tt></a> decorator now adds a <tt class="xref py py-attr docutils literal"><span class="pre">__wrapped__</span></tt> attribute
pointing to the original callable function.  This allows wrapped functions to
be introspected.  It also copies <tt class="xref py py-attr docutils literal"><span class="pre">__annotations__</span></tt> if defined.  And now
it also gracefully skips over missing attributes such as <tt class="xref py py-attr docutils literal"><span class="pre">__doc__</span></tt> 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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="c"># uncached function</span>
</pre></div>
</div>
<p>(By Nick Coghlan and Terrence Cole; <a class="reference external" href="http://bugs.python.org/issue9567">issue 9567</a>, <a class="reference external" href="http://bugs.python.org/issue3445">issue 3445</a>, and
<a class="reference external" href="http://bugs.python.org/issue8814">issue 8814</a>.)</p>
</li>
<li><p class="first">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"><tt class="xref py py-func docutils literal"><span class="pre">functools.total_ordering()</span></tt></a> will use a 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"><tt class="xref py py-func docutils literal"><span class="pre">total_ordering()</span></tt></a> to fill-in <em>__le__</em>, <em>__gt__</em> and <em>__ge__</em>:</p>
<div class="highlight-python3"><div class="highlight"><pre><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">&lt;</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 class="first">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"><tt class="xref py py-func docutils literal"><span class="pre">functools.cmp_to_key()</span></tt></a>
function converts an old-style comparison function to
modern <a class="reference internal" href="../glossary.html#term-key-function"><em class="xref std std-term">key function</em></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># locale-aware sort order</span>
<span class="gp">&gt;&gt;&gt; </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>
</pre></div>
</div>
<p>For sorting examples and a brief sorting tutorial, see the <a class="reference external" href="http://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 class="first">The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> module has a new <a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><tt class="xref py py-func docutils literal"><span class="pre">accumulate()</span></tt></a> function
modeled on APL&#8217;s <em>scan</em> operator and Numpy&#8217;s <em>accumulate</em> function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">accumulate</span>
<span class="gp">&gt;&gt;&gt; </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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="c"># 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"><tt class="xref py py-func docutils literal"><span class="pre">accumulate()</span></tt></a>, see the <a class="reference internal" href="../library/random.html#random-examples"><em>examples for
the random module</em></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 class="first">The <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">collections.Counter</span></tt></a> class now has two forms of in-place
subtraction, the existing <em>-=</em> operator for <a class="reference external" href="http://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"><tt class="xref py py-meth docutils literal"><span class="pre">subtract()</span></tt></a> method for regular subtraction.  The
former is suitable for <a class="reference external" href="http://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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">cat</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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="c"># saturating subtraction</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span>
<span class="go">Counter({&#39;dogs&#39;: 3})</span>
</pre></div>
</div>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="c"># regular subtraction</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span>
<span class="go">Counter({&#39;dogs&#39;: 3, &#39;cats&#39;: -5})</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">collections.OrderedDict</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">move_to_end()</span></tt></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 <tt class="docutils literal"><span class="pre">od[k]</span> <span class="pre">=</span> <span class="pre">od.pop(k)</span></tt>.</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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;X&#39;</span><span class="p">,</span> <span class="s">&#39;d&#39;</span><span class="p">,</span> <span class="s">&#39;e&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;X&#39;, &#39;d&#39;, &#39;e&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">move_to_end</span><span class="p">(</span><span class="s">&#39;X&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;X&#39;]</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">collections.deque</span></tt></a> class grew two new methods
<a class="reference internal" href="../library/collections.html#collections.deque.count" title="collections.deque.count"><tt class="xref py py-meth docutils literal"><span class="pre">count()</span></tt></a> and <a class="reference internal" href="../library/collections.html#collections.deque.reverse" title="collections.deque.reverse"><tt class="xref py py-meth docutils literal"><span class="pre">reverse()</span></tt></a> that
make them more substitutable for <a class="reference internal" href="../library/functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> objects:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s">&#39;simsalabim&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s">&#39;s&#39;</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">deque([&#39;m&#39;, &#39;i&#39;, &#39;b&#39;, &#39;a&#39;, &#39;l&#39;, &#39;a&#39;, &#39;s&#39;, &#39;m&#39;, &#39;i&#39;, &#39;s&#39;])</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."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module has a new <a class="reference internal" href="../library/threading.html#threading.Barrier" title="threading.Barrier"><tt class="xref py py-class docutils literal"><span class="pre">Barrier</span></tt></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="http://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"><tt class="xref py py-class docutils literal"><span class="pre">Barrier</span></tt></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"><div class="highlight"><pre><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="c"># 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"><tt class="xref py py-meth docutils literal"><span class="pre">threading.Thread.join()</span></tt></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"><tt class="xref py py-exc docutils literal"><span class="pre">BrokenBarrierError</span></tt></a> exception is raised:</p>
<div class="highlight-python3"><div class="highlight"><pre><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://parlab.eecs.berkeley.edu/wiki/_media/patterns/paraplop_g1_3.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="http://greenteapress.com/semaphores/downey08semaphores.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="http://bugs.python.org/issue8777">issue 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 class="first">The <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module has a new type <a class="reference internal" href="../library/datetime.html#datetime.timezone" title="datetime.timezone"><tt class="xref py py-class docutils literal"><span class="pre">timezone</span></tt></a> that
implements the <a class="reference internal" href="../library/datetime.html#datetime.tzinfo" title="datetime.tzinfo"><tt class="xref py py-class docutils literal"><span class="pre">tzinfo</span></tt></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"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s">&quot;01/01/2000 12:00 +0000&quot;</span><span class="p">,</span> <span class="s">&quot;%m/%d/%Y %H:%M %z&quot;</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 class="first">Also, <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects can now be multiplied by
<a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> and divided by <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a> and <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> objects.
And <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><tt class="xref py py-class docutils literal"><span class="pre">timedelta</span></tt></a> objects can now divide one another.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/datetime.html#datetime.date.strftime" title="datetime.date.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">datetime.date.strftime()</span></tt></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 class="first">Whenever a two-digit year is used in a time tuple, the interpretation has been
governed by <a class="reference internal" href="../library/time.html#time.accept2dyear" title="time.accept2dyear"><tt class="xref py py-attr docutils literal"><span class="pre">time.accept2dyear</span></tt></a>.  The default is <em>True</em> which means that
for a two-digit year, the century is guessed according to the POSIX rules
governing the <tt class="docutils literal"><span class="pre">%y</span></tt> 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"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a>.  Instead, it is recommended that
<a class="reference internal" href="../library/time.html#time.accept2dyear" title="time.accept2dyear"><tt class="xref py py-attr docutils literal"><span class="pre">time.accept2dyear</span></tt></a> be set to <em>False</em> so that large date ranges
can be used without guesswork:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">warnings</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">warnings</span><span class="o">.</span><span class="n">resetwarnings</span><span class="p">()</span>      <span class="c"># remove the default warning filters</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="k">True</span>      <span class="c"># guess whether 11 means 11 or 2011</span>
<span class="gp">&gt;&gt;&gt; </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">&#39;Fri Jan  1 12:34:56 2011&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="k">False</span>     <span class="c"># use the full range of allowable dates</span>
<span class="gp">&gt;&gt;&gt; </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">&#39;Fri Jan  1 12:34:56 11&#39;</span>
</pre></div>
</div>
<p>Several functions now have significantly expanded date ranges.  When
<a class="reference internal" href="../library/time.html#time.accept2dyear" title="time.accept2dyear"><tt class="xref py py-attr docutils literal"><span class="pre">time.accept2dyear</span></tt></a> is false, the <a class="reference internal" href="../library/time.html#time.asctime" title="time.asctime"><tt class="xref py py-func docutils literal"><span class="pre">time.asctime()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">time.mktime()</span></tt></a> and
<a class="reference internal" href="../library/time.html#time.strftime" title="time.strftime"><tt class="xref py py-func docutils literal"><span class="pre">time.strftime()</span></tt></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="http://bugs.python.org/issue1289118">issue 1289118</a>,
<a class="reference external" href="http://bugs.python.org/issue5094">issue 5094</a>, <a class="reference external" href="http://bugs.python.org/issue6641">issue 6641</a>, <a class="reference external" href="http://bugs.python.org/issue2706">issue 2706</a>, <a class="reference external" href="http://bugs.python.org/issue1777412">issue 1777412</a>, <a class="reference external" href="http://bugs.python.org/issue8013">issue 8013</a>,
and <a class="reference external" href="http://bugs.python.org/issue10827">issue 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.)."><tt class="xref py py-mod docutils literal"><span class="pre">math</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">isfinite()</span></tt></a> function provides a reliable and fast way to detect
special values.  It returns <em>True</em> for regular numbers and <em>False</em> for <em>Nan</em> or
<em>Infinity</em>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="s">&#39;Nan&#39;</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="s">&#39;Inf&#39;</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"><tt class="xref py py-func docutils literal"><span class="pre">expm1()</span></tt></a> function computes <tt class="docutils literal"><span class="pre">e**x-1</span></tt> 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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">expm1</span><span class="p">(</span><span class="mf">0.013671875</span><span class="p">)</span>   <span class="c"># 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"><tt class="xref py py-func docutils literal"><span class="pre">erf()</span></tt></a> function computes a probability integral or <a class="reference external" href="http://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"><tt class="xref py py-func docutils literal"><span class="pre">erfc()</span></tt></a>, is <tt class="docutils literal"><span class="pre">1</span> <span class="pre">-</span> <span class="pre">erf(x)</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="c"># portion of normal distribution within 1 standard deviation</span>
<span class="go">0.682689492137086</span>
<span class="gp">&gt;&gt;&gt; </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="c"># portion of normal distribution outside 1 standard deviation</span>
<span class="go">0.31731050786291404</span>
<span class="gp">&gt;&gt;&gt; </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"><tt class="xref py py-func docutils literal"><span class="pre">gamma()</span></tt></a> function is a continuous extension of the factorial
function.  See <a class="reference external" href="http://en.wikipedia.org/wiki/Gamma_function">http://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"><tt class="xref py py-func docutils literal"><span class="pre">lgamma()</span></tt></a> function for computing the natural
logarithm of the gamma function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">gamma</span><span class="p">(</span><span class="mf">7.0</span><span class="p">)</span>           <span class="c"># six factorial</span>
<span class="go">720.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">lgamma</span><span class="p">(</span><span class="mf">801.0</span><span class="p">)</span>        <span class="c"># 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."><tt class="xref py py-mod docutils literal"><span class="pre">abc</span></tt></a> module now supports <a class="reference internal" href="../library/abc.html#abc.abstractclassmethod" title="abc.abstractclassmethod"><tt class="xref py py-func docutils literal"><span class="pre">abstractclassmethod()</span></tt></a> and
<a class="reference internal" href="../library/abc.html#abc.abstractstaticmethod" title="abc.abstractstaticmethod"><tt class="xref py py-func docutils literal"><span class="pre">abstractstaticmethod()</span></tt></a>.</p>
<p>These tools make it possible to define an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base class</em></a> that
requires a particular <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a> or <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a> to be
implemented:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="n">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="n">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="http://bugs.python.org/issue5867">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></tt></a> has a new method, <a class="reference internal" href="../library/io.html#io.BytesIO.getbuffer" title="io.BytesIO.getbuffer"><tt class="xref py py-meth docutils literal"><span class="pre">getbuffer()</span></tt></a>, which
provides functionality similar to <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><tt class="xref py py-func docutils literal"><span class="pre">memoryview()</span></tt></a>.  It creates an editable
view of the data without making a copy.  The buffer&#8217;s random access and support
for slice notation are well-suited to in-place editing:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="go">        start = record_number * REC_LEN + LOC_START</span>
<span class="go">        buffer[start: start+LOC_LEN] = location</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">io</span>

<span class="gp">&gt;&gt;&gt; </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="go">    b&#39;G3805  storeroom  Main chassis    &#39;</span>
<span class="go">    b&#39;X7899  shipping   Reserve cog     &#39;</span>
<span class="go">    b&#39;L6988  receiving  Primary sprocket&#39;</span>
<span class="go">)</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="n">b</span><span class="s">&#39;warehouse  &#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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="n">b</span><span class="s">&#39;showroom   &#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">byte_stream</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span>
<span class="go">b&#39;G3805  showroom   Main chassis    &#39;</span>
<span class="go">b&#39;X7899  warehouse  Reserve cog     &#39;</span>
<span class="go">b&#39;L6988  receiving  Primary sprocket&#39;</span>
</pre></div>
</div>
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="http://bugs.python.org/issue5506">issue 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__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></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&#8217;s builtin objects such as <a class="reference internal" href="../library/functions.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> handle
self-reference by displaying &#8221;...&#8221; 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__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> methods, the <a class="reference internal" href="../library/reprlib.html#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><tt class="xref py py-mod docutils literal"><span class="pre">reprlib</span></tt></a> module has a new
decorator, <a class="reference internal" href="../library/reprlib.html#reprlib.recursive_repr" title="reprlib.recursive_repr"><tt class="xref py py-func docutils literal"><span class="pre">recursive_repr()</span></tt></a>, for detecting recursive calls to
<a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> and substituting a placeholder string instead:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="go">        @recursive_repr()</span>
<span class="go">        def __repr__(self):</span>
<span class="go">            return &#39;&lt;&#39; + &#39;|&#39;.join(map(repr, self)) + &#39;&gt;&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">MyList</span><span class="p">(</span><span class="s">&#39;abc&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
<span class="go">&lt;&#39;a&#39;|&#39;b&#39;|&#39;c&#39;|...|&#39;x&#39;&gt;</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger in <a class="reference external" href="http://bugs.python.org/issue9826">issue 9826</a> and <a class="reference external" href="http://bugs.python.org/issue9840">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></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"><em>basic tutorial</em></a>, an <a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><em>advanced tutorial</em></a>, and a <a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><em>cookbook</em></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"><tt class="xref py py-func docutils literal"><span class="pre">logging.basicConfig()</span></tt></a> set-up function gained a <em>style</em> argument to
support three different types of string formatting.  It defaults to &#8220;%&#8221; for
traditional %-formatting, can be set to &#8220;{&#8221; for the new <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> style, or
can be set to &#8220;$&#8221; for the shell-style formatting provided by
<a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><tt class="xref py py-class docutils literal"><span class="pre">string.Template</span></tt></a>.  The following three configurations are equivalent:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">logging</span> <span class="k">import</span> <span class="n">basicConfig</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s">&#39;%&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s">&quot;%(name)s -&gt; %(levelname)s: %(message)s&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s">&#39;{&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s">&quot;{name} -&gt; {levelname} {message}&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s">&#39;$&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s">&quot;$name -&gt; $levelname: $message&quot;</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"><tt class="xref py py-class docutils literal"><span class="pre">StreamHandler</span></tt></a> directed to
<a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><tt class="xref py py-attr docutils literal"><span class="pre">sys.stderr</span></tt></a> for events of <tt class="docutils literal"><span class="pre">WARNING</span></tt> 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
<tt class="xref py py-attr docutils literal"><span class="pre">logging.raiseExceptions</span></tt>.  The new default handler is stored in
<tt class="xref py py-attr docutils literal"><span class="pre">logging.lastResort</span></tt>.</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"><tt class="xref py py-class docutils literal"><span class="pre">Filter</span></tt></a> object, the predicate can be any Python callable that
returns <em>True</em> or <em>False</em>.</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."><tt class="xref py py-mod docutils literal"><span class="pre">csv</span></tt></a> module now supports a new dialect, <a class="reference internal" href="../library/csv.html#csv.unix_dialect" title="csv.unix_dialect"><tt class="xref py py-class docutils literal"><span class="pre">unix_dialect</span></tt></a>,
which applies quoting for all fields and a traditional Unix style with <tt class="docutils literal"><span class="pre">'\n'</span></tt> as
the line terminator.  The registered dialect name is <tt class="docutils literal"><span class="pre">unix</span></tt>.</p>
<p>The <a class="reference internal" href="../library/csv.html#csv.DictWriter" title="csv.DictWriter"><tt class="xref py py-class docutils literal"><span class="pre">csv.DictWriter</span></tt></a> has a new method,
<a class="reference internal" href="../library/csv.html#csv.DictWriter.writeheader" title="csv.DictWriter.writeheader"><tt class="xref py py-meth docutils literal"><span class="pre">writeheader()</span></tt></a> for writing-out an initial row to document
the field names:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">csv</span><span class="o">,</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;name&#39;</span><span class="p">,</span> <span class="s">&#39;dept&#39;</span><span class="p">],</span> <span class="n">dialect</span><span class="o">=</span><span class="s">&#39;unix&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">writeheader</span><span class="p">()</span>
<span class="go">&quot;name&quot;,&quot;dept&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">writerows</span><span class="p">([</span>
<span class="go">        {&#39;name&#39;: &#39;tom&#39;, &#39;dept&#39;: &#39;accounting&#39;},</span>
<span class="go">        {&#39;name&#39;: &#39;susan&#39;, &#39;dept&#39;: &#39;Salesl&#39;}])</span>
<span class="go">&quot;tom&quot;,&quot;accounting&quot;</span>
<span class="go">&quot;susan&quot;,&quot;sales&quot;</span>
</pre></div>
</div>
<p>(New dialect suggested by Jay Talbot in <a class="reference external" href="http://bugs.python.org/issue5975">issue 5975</a>, and the new method
suggested by Ed Abraham in <a class="reference external" href="http://bugs.python.org/issue1537721">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">ContextDecorator</span></tt></a> that is helpful for creating a
<a class="reference internal" href="../glossary.html#term-context-manager"><em class="xref std std-term">context manager</em></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"><tt class="xref py py-func docutils literal"><span class="pre">contextmanager()</span></tt></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"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">contextmanager()</span></tt></a> provides both capabilities in a single
definition:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;Entering: {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
    <span class="k">yield</span>
    <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s">&#39;Exiting: {}&#39;</span><span class="o">.</span><span class="n">format</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"><div class="highlight"><pre><span class="k">with</span> <span class="n">track_entry_and_exit</span><span class="p">(</span><span class="s">&#39;widget loader&#39;</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s">&#39;Some time consuming activity goes here&#39;</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"><div class="highlight"><pre><span class="nd">@track_entry_and_exit</span><span class="p">(</span><span class="s">&#39;widget loader&#39;</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="s">&#39;Some time consuming activity goes here&#39;</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"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></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="http://bugs.python.org/issue9110">issue 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="http://bugs.python.org/issue8188">issue 8188</a>):</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&quot;1.5&quot;</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"><tt class="xref py py-attr docutils literal"><span class="pre">sys.hash_info</span></tt></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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">hash_info</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 <tt class="docutils literal"><span class="pre">Decimal('1.1')</span> <span class="pre">+</span> <span class="pre">float('1.1')</span></tt>
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>The <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">decimal.Decimal</span></tt></a> constructor now accepts <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">from_float()</span></tt></a>
method (<a class="reference external" href="http://bugs.python.org/issue8257">issue 8257</a>).</li>
<li>Mixed type comparisons are now fully supported so that
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> objects can be directly compared with <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>
and <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">fractions.Fraction</span></tt></a> (<a class="reference external" href="http://bugs.python.org/issue2531">issue 2531</a> and <a class="reference external" href="http://bugs.python.org/issue8188">issue 8188</a>).</li>
</ul>
<p>Similar changes were made to <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">fractions.Fraction</span></tt></a> so that the
<a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_float" title="fractions.Fraction.from_float"><tt class="xref py py-meth docutils literal"><span class="pre">from_float()</span></tt></a> and <a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_decimal" title="fractions.Fraction.from_decimal"><tt class="xref py py-meth docutils literal"><span class="pre">from_decimal()</span></tt></a>
methods are no longer needed (<a class="reference external" href="http://bugs.python.org/issue8294">issue 8294</a>):</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
<span class="go">Decimal(&#39;1.100000000000000088817841970012523233890533447265625&#39;)</span>
<span class="gp">&gt;&gt;&gt; </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."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module is that the
<tt class="xref py py-attr docutils literal"><span class="pre">Context.clamp</span></tt> 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="http://bugs.python.org/issue8540">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">ftplib.FTP</span></tt></a> class now supports the context manager protocol to
unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><tt class="xref py py-exc docutils literal"><span class="pre">socket.error</span></tt></a> exceptions and to close the FTP
connection when done:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">ftplib</span> <span class="k">import</span> <span class="n">FTP</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">FTP</span><span class="p">(</span><span class="s">&quot;ftp1.at.proftpd.org&quot;</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">&#39;230 Anonymous login ok, restrictions apply.&#39;</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"><tt class="xref py py-class docutils literal"><span class="pre">mmap.mmap</span></tt></a> and <a class="reference internal" href="../library/fileinput.html#fileinput.input" title="fileinput.input"><tt class="xref py py-func docutils literal"><span class="pre">fileinput.input()</span></tt></a>
also grew auto-closing context managers:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;log1.txt&#39;</span><span class="p">,</span> <span class="s">&#39;log2.txt&#39;</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="http://bugs.python.org/issue4972">issue 4972</a>, and
by Georg Brandl in <a class="reference external" href="http://bugs.python.org/issue8046">issue 8046</a> and <a class="reference external" href="http://bugs.python.org/issue1286">issue 1286</a>.)</p>
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><tt class="xref py py-class docutils literal"><span class="pre">FTP_TLS</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">ssl.SSLContext</span></tt></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="http://bugs.python.org/issue8806">issue 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 <tt class="xref py py-func docutils literal"><span class="pre">os.popen()</span></tt> and <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-func docutils literal"><span class="pre">subprocess.Popen()</span></tt></a> functions now support
<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statements for auto-closing of the file descriptors.</p>
<p>(Contributed by Antoine Pitrou and Brian Curtin in <a class="reference external" href="http://bugs.python.org/issue7461">issue 7461</a> and
<a class="reference external" href="http://bugs.python.org/issue10554">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">select</span></tt></a> module now exposes a new, constant attribute,
<a class="reference internal" href="../library/select.html#select.PIPE_BUF" title="select.PIPE_BUF"><tt class="xref py py-attr docutils literal"><span class="pre">PIPE_BUF</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">select.select()</span></tt></a> says a pipe is ready
for writing.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">select</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">select</span><span class="o">.</span><span class="n">PIPE_BUF</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="http://bugs.python.org/issue9862">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">gzip.GzipFile</span></tt></a> now implements the <a class="reference internal" href="../library/io.html#io.BufferedIOBase" title="io.BufferedIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></tt></a>
<a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">abstract base class</em></a> (except for <tt class="docutils literal"><span class="pre">truncate()</span></tt>).  It also has a
<a class="reference internal" href="../library/gzip.html#gzip.GzipFile.peek" title="gzip.GzipFile.peek"><tt class="xref py py-meth docutils literal"><span class="pre">peek()</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">gzip</span></tt></a> module also gains the <a class="reference internal" href="../library/gzip.html#gzip.compress" title="gzip.compress"><tt class="xref py py-func docutils literal"><span class="pre">compress()</span></tt></a> and
<a class="reference internal" href="../library/gzip.html#gzip.decompress" title="gzip.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></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/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>
before compressing and decompressing:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;Three shall be the number thou shalt count, &#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">+=</span> <span class="s">&#39;and the number of the counting shall be three&#39;</span>
<span class="gp">&gt;&gt;&gt; </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="c"># convert to utf-8</span>
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </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="c"># decompress and convert to text</span>
<span class="go">&#39;Three shall be the number thou shalt count,&#39;</span>
</pre></div>
</div>
<p>(Contributed by Anand B. Pillai in <a class="reference external" href="http://bugs.python.org/issue3488">issue 3488</a>; and by Antoine Pitrou, Nir
Aides and Brian Curtin in <a class="reference external" href="http://bugs.python.org/issue9962">issue 9962</a>, <a class="reference external" href="http://bugs.python.org/issue1675951">issue 1675951</a>, <a class="reference external" href="http://bugs.python.org/issue7471">issue 7471</a> and
<a class="reference external" href="http://bugs.python.org/issue2846">issue 2846</a>.)</p>
<p>Also, the <tt class="xref py py-class docutils literal"><span class="pre">zipfile.ZipExtFile</span></tt> class was reworked internally to represent
files stored inside an archive.  The new implementation is significantly faster
and can be wrapped in a <a class="reference internal" href="../library/io.html#io.BufferedReader" title="io.BufferedReader"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedReader</span></tt></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="http://bugs.python.org/issue7610">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">TarFile</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></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"><em class="xref std std-term">keyword argument</em></a>.  The user-supplied filter function accepts a
<a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><tt class="xref py py-class docutils literal"><span class="pre">TarInfo</span></tt></a> object and returns an updated
<a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><tt class="xref py py-class docutils literal"><span class="pre">TarInfo</span></tt></a> object, or if it wants the file to be excluded, the
function can return <em>None</em>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">tarfile</span><span class="o">,</span> <span class="nn">glob</span>

<span class="gp">&gt;&gt;&gt; </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="go">       if tarinfo.isfile():             # only save real files</span>
<span class="go">            tarinfo.uname = &#39;monty&#39;     # redact the user name</span>
<span class="go">            return tarinfo</span>

<span class="gp">&gt;&gt;&gt; </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="s">&#39;myarchive.tar.gz&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s">&#39;w:gz&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">tf</span><span class="p">:</span>
<span class="go">        for filename in glob.glob(&#39;*.txt&#39;):</span>
<span class="go">            tf.add(filename, filter=myfilter)</span>
<span class="go">        tf.list()</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="http://bugs.python.org/issue6856">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">hashlib</span></tt></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"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hashlib</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_guaranteed</span>
<span class="go">{&#39;sha1&#39;, &#39;sha224&#39;, &#39;sha384&#39;, &#39;sha256&#39;, &#39;sha512&#39;, &#39;md5&#39;}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_available</span>
<span class="go">{&#39;md2&#39;, &#39;SHA256&#39;, &#39;SHA512&#39;, &#39;dsaWithSHA&#39;, &#39;mdc2&#39;, &#39;SHA224&#39;, &#39;MD4&#39;, &#39;sha256&#39;,</span>
<span class="go">&#39;sha512&#39;, &#39;ripemd160&#39;, &#39;SHA1&#39;, &#39;MDC2&#39;, &#39;SHA&#39;, &#39;SHA384&#39;, &#39;MD2&#39;,</span>
<span class="go">&#39;ecdsa-with-SHA1&#39;,&#39;md4&#39;, &#39;md5&#39;, &#39;sha1&#39;, &#39;DSA-SHA&#39;, &#39;sha224&#39;,</span>
<span class="go">&#39;dsaEncryption&#39;, &#39;DSA&#39;, &#39;RIPEMD160&#39;, &#39;sha&#39;, &#39;MD5&#39;, &#39;sha384&#39;}</span>
</pre></div>
</div>
<p>(Suggested by Carl Chenet in <a class="reference external" href="http://bugs.python.org/issue7418">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">ast</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">ast.literal_eval()</span></tt></a> function serves as a secure alternative to
the builtin <a class="reference internal" href="../library/functions.html#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a> function which is easily abused.  Python 3.2 adds
<a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> literals to the list of supported types:
strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="s">&quot;{&#39;req&#39;: 3, &#39;func&#39;: &#39;pow&#39;, &#39;args&#39;: (2, 0.5)}&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">literal_eval</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
<span class="go">{&#39;args&#39;: (2, 0.5), &#39;req&#39;: 3, &#39;func&#39;: &#39;pow&#39;}</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="s">&quot;os.system(&#39;do something harmful&#39;)&quot;</span>
<span class="gp">&gt;&gt;&gt; </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="nc">ValueError: malformed node or string</span>: <span class="n-Identifier">&lt;_ast.Call object at 0x101739a10&gt;</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."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a> module provides two new functions,
<a class="reference internal" href="../library/os.html#os.fsencode" title="os.fsencode"><tt class="xref py py-func docutils literal"><span class="pre">fsencode()</span></tt></a> and <a class="reference internal" href="../library/os.html#os.fsdecode" title="os.fsdecode"><tt class="xref py py-func docutils literal"><span class="pre">fsdecode()</span></tt></a>, for encoding and decoding
filenames:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">filename</span> <span class="o">=</span> <span class="s">&#39;Sehenswürdigkeiten&#39;</span>
<span class="gp">&gt;&gt;&gt; </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&#39;Sehensw\xc3\xbcrdigkeiten&#39;</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"><tt class="xref py py-attr docutils literal"><span class="pre">os.supports_bytes_environ</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">os.getenvb()</span></tt></a> function or use <a class="reference internal" href="../library/os.html#os.environb" title="os.environb"><tt class="xref py py-data docutils literal"><span class="pre">os.environb</span></tt></a>
which is a bytes version of <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><tt class="xref py py-data docutils literal"><span class="pre">os.environ</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">shutil.copytree()</span></tt></a> function has two new options:</p>
<ul class="simple">
<li><em>ignore_dangling_symlinks</em>: when <tt class="docutils literal"><span class="pre">symlinks=False</span></tt> 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&#8217;t exist.</li>
<li><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"><tt class="xref py py-func docutils literal"><span class="pre">shutil.copy2()</span></tt></a> is used by default.</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."><tt class="xref py py-mod docutils literal"><span class="pre">shutil</span></tt></a> module now supports <a class="reference internal" href="../library/shutil.html#archiving-operations"><em>archiving operations</em></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"><tt class="xref py py-func docutils literal"><span class="pre">make_archive()</span></tt></a> and
<a class="reference internal" href="../library/shutil.html#shutil.unpack_archive" title="shutil.unpack_archive"><tt class="xref py py-func docutils literal"><span class="pre">unpack_archive()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">os.chdir()</span></tt></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"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">shutil</span><span class="o">,</span> <span class="nn">pprint</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s">&#39;mydata&#39;</span><span class="p">)</span>                               <span class="c"># change to the source directory</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;/var/backup/mydata&#39;</span><span class="p">,</span>
<span class="go">                            &#39;zip&#39;)                   # archive the current directory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span>                                                <span class="c"># show the name of archive</span>
<span class="go">&#39;/var/backup/mydata.zip&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s">&#39;tmp&#39;</span><span class="p">)</span>                                  <span class="c"># change to an unpacking</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">unpack_archive</span><span class="p">(</span><span class="s">&#39;/var/backup/mydata.zip&#39;</span><span class="p">)</span>  <span class="c"># recover the data</span>

<span class="gp">&gt;&gt;&gt; </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="c"># display known formats</span>
<span class="go">[(&#39;bztar&#39;, &quot;bzip2&#39;ed tar-file&quot;),</span>
<span class="go"> (&#39;gztar&#39;, &quot;gzip&#39;ed tar-file&quot;),</span>
<span class="go"> (&#39;tar&#39;, &#39;uncompressed tar file&#39;),</span>
<span class="go"> (&#39;zip&#39;, &#39;ZIP file&#39;)]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">register_archive_format</span><span class="p">(</span>                  <span class="c"># register a new archive format</span>
<span class="go">        name = &#39;xz&#39;,</span>
<span class="go">        function = xz.compress,                      # callable archiving function</span>
<span class="go">        extra_args = [(&#39;level&#39;, 8)],                 # arguments to the function</span>
<span class="go">        description = &#39;xz compression&#39;</span>
<span class="go">)</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."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module was updated to pysqlite version 2.6.0.  It has two new capabilities.</p>
<ul class="simple">
<li>The <tt class="xref py py-attr docutils literal"><span class="pre">sqlite3.Connection.in_transit</span></tt> attribute is true if there is an
active transaction for uncommitted changes.</li>
<li>The <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.enable_load_extension" title="sqlite3.Connection.enable_load_extension"><tt class="xref py py-meth docutils literal"><span class="pre">sqlite3.Connection.enable_load_extension()</span></tt></a> and
<a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.load_extension" title="sqlite3.Connection.load_extension"><tt class="xref py py-meth docutils literal"><span class="pre">sqlite3.Connection.load_extension()</span></tt></a> methods allows you to load SQLite
extensions from &#8221;.so&#8221; files.  One well-known extension is the fulltext-search
extension distributed with SQLite.</li>
</ul>
<p>(Contributed by R. David Murray and Shashwat Anand; <a class="reference external" href="http://bugs.python.org/issue8845">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">html</span></tt></a> module was introduced with only a single function,
<a class="reference internal" href="../library/html.html#html.escape" title="html.escape"><tt class="xref py py-func docutils literal"><span class="pre">escape()</span></tt></a>, which is used for escaping reserved characters from HTML
markup:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">html</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">html</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="s">&#39;x &gt; 2 &amp;&amp; x &lt; 7&#39;</span><span class="p">)</span>
<span class="go">&#39;x &amp;gt; 2 &amp;amp;&amp;amp; x &amp;lt; 7&#39;</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."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module has two new improvements.</p>
<ul class="simple">
<li>Socket objects now have a <a class="reference internal" href="../library/socket.html#socket.socket.detach" title="socket.socket.detach"><tt class="xref py py-meth docutils literal"><span class="pre">detach()</span></tt></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="http://bugs.python.org/issue8524">issue 8524</a>.)</li>
<li><a class="reference internal" href="../library/socket.html#socket.create_connection" title="socket.create_connection"><tt class="xref py py-func docutils literal"><span class="pre">socket.create_connection()</span></tt></a> now supports the context manager protocol
to unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><tt class="xref py py-exc docutils literal"><span class="pre">socket.error</span></tt></a> exceptions and to close the
socket when done.
(Contributed by Giampaolo Rodolà; <a class="reference external" href="http://bugs.python.org/issue9794">issue 9794</a>.)</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"><tt class="xref py py-mod docutils literal"><span class="pre">ssl</span></tt></a> module added a number of features to satisfy common requirements
for secure (encrypted, authenticated) internet connections:</p>
<ul class="simple">
<li>A new class, <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><tt class="xref py py-class docutils literal"><span class="pre">SSLContext</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">wrap_socket()</span></tt></a> for creating
an SSL socket from an SSL context.</li>
<li>A new function, <a class="reference internal" href="../library/ssl.html#ssl.match_hostname" title="ssl.match_hostname"><tt class="xref py py-func docutils literal"><span class="pre">ssl.match_hostname()</span></tt></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="http://tools.ietf.org/html/rfc2818.html"><strong>RFC 2818</strong></a>) which are also suitable for other protocols.</li>
<li>The <a class="reference internal" href="../library/ssl.html#ssl.wrap_socket" title="ssl.wrap_socket"><tt class="xref py py-func docutils literal"><span class="pre">ssl.wrap_socket()</span></tt></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="http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT">OpenSSL documentation</a>.</li>
<li>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"><tt class="xref py py-mod docutils literal"><span class="pre">ssl</span></tt></a> module now
supports the Server Name Indication extension to the TLS protocol, allowing
multiple &#8220;virtual hosts&#8221; 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"><tt class="xref py py-meth docutils literal"><span class="pre">ssl.SSLContext.wrap_socket()</span></tt></a>.</li>
<li>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"><tt class="xref py py-mod docutils literal"><span class="pre">ssl</span></tt></a> module, such as
<a class="reference internal" href="../library/ssl.html#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><tt class="xref py py-data docutils literal"><span class="pre">OP_NO_SSLv2</span></tt></a> which disables the insecure and obsolete SSLv2
protocol.</li>
<li>The extension now loads all the OpenSSL ciphers and digest algorithms.  If
some SSL certificates cannot be verified, they are reported as an &#8220;unknown
algorithm&#8221; error.</li>
<li>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"><tt class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION</span></tt></a> (a string),
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_INFO" title="ssl.OPENSSL_VERSION_INFO"><tt class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION_INFO</span></tt></a> (a 5-tuple), and
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_NUMBER" title="ssl.OPENSSL_VERSION_NUMBER"><tt class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION_NUMBER</span></tt></a> (an integer).</li>
</ul>
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="http://bugs.python.org/issue8850">issue 8850</a>, <a class="reference external" href="http://bugs.python.org/issue1589">issue 1589</a>, <a class="reference external" href="http://bugs.python.org/issue8322">issue 8322</a>,
<a class="reference external" href="http://bugs.python.org/issue5639">issue 5639</a>, <a class="reference external" href="http://bugs.python.org/issue4870">issue 4870</a>, <a class="reference external" href="http://bugs.python.org/issue8484">issue 8484</a>, and <a class="reference external" href="http://bugs.python.org/issue8321">issue 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)."><tt class="xref py py-mod docutils literal"><span class="pre">nntplib</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">nntplib.NNTP_SSL</span></tt></a>) and explicit (using <a class="reference internal" href="../library/nntplib.html#nntplib.NNTP.starttls" title="nntplib.NNTP.starttls"><tt class="xref py py-meth docutils literal"><span class="pre">nntplib.NNTP.starttls()</span></tt></a>)
TLS has also been added.</p>
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="http://bugs.python.org/issue9360">issue 9360</a> and Andrew Vant in <a class="reference external" href="http://bugs.python.org/issue1926">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">http.client.HTTPSConnection</span></tt></a>, <a class="reference internal" href="../library/urllib.request.html#urllib.request.HTTPSHandler" title="urllib.request.HTTPSHandler"><tt class="xref py py-class docutils literal"><span class="pre">urllib.request.HTTPSHandler</span></tt></a>
and <a class="reference internal" href="../library/urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><tt class="xref py py-func docutils literal"><span class="pre">urllib.request.urlopen()</span></tt></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="http://bugs.python.org/issue9003">issue 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"><tt class="xref py py-mod docutils literal"><span class="pre">imaplib.IMAP4.starttls</span></tt></a> method.</p>
<p>(Contributed by Lorenzo M. Catucci and Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue4471">issue 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)."><tt class="xref py py-mod docutils literal"><span class="pre">http.client</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">HTTPConnection</span></tt></a> and
<a class="reference internal" href="../library/http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><tt class="xref py py-class docutils literal"><span class="pre">HTTPSConnection</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">HTTPSConnection</span></tt></a>.</p>
<p>The <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.request" title="http.client.HTTPConnection.request"><tt class="xref py py-meth docutils literal"><span class="pre">request()</span></tt></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"><em class="xref std std-term">file object</em></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"><em class="xref std std-term">iterable</em></a> object so long as it includes an explicit
<tt class="docutils literal"><span class="pre">Content-Length</span></tt> 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"><tt class="xref py py-meth docutils literal"><span class="pre">set_tunnel()</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">http.server</span></tt></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="http://bugs.python.org/issue10980">issue 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 class="first">The command-line call <tt class="docutils literal"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span></tt> can now accept file paths
instead of module names for running specific tests (<a class="reference external" href="http://bugs.python.org/issue10620">issue 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 <tt class="docutils literal"><span class="pre">-p</span></tt>, and a directory to
start discovery with <tt class="docutils literal"><span class="pre">-s</span></tt>:</p>
<div class="highlight-python3"><pre>$ python -m unittest discover -s my_proj_dir -p _test.py</pre>
</div>
<p>(Contributed by Michael Foord.)</p>
</li>
<li><p class="first">Experimentation at the interactive prompt is now easier because the
<tt class="xref py py-class docutils literal"><span class="pre">unittest.case.TestCase</span></tt> class can now be instantiated without
arguments:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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 class="first">The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><tt class="xref py py-mod docutils literal"><span class="pre">unittest</span></tt></a> module has two new methods,
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><tt class="xref py py-meth docutils literal"><span class="pre">assertWarns()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><tt class="xref py py-meth docutils literal"><span class="pre">assertWarnsRegex()</span></tt></a> to verify that a given warning type
is triggered by the code under test:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;XYZ&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue9754">issue 9754</a>.)</p>
<p>Another new method, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertCountEqual" title="unittest.TestCase.assertCountEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertCountEqual()</span></tt></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"><div class="highlight"><pre><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="s">&#39;algorithm&#39;</span><span class="p">,</span> <span class="s">&#39;logarithm&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">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"><tt class="xref py py-attr docutils literal"><span class="pre">maxDiff</span></tt></a> attribute that sets maximum length of
diffs displayed.</p>
</li>
<li><p class="first">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"><tt class="xref py py-meth docutils literal"><span class="pre">assertRegex()</span></tt></a> is the new name for
<tt class="xref py py-meth docutils literal"><span class="pre">assertRegexpMatches()</span></tt> which was misnamed because the
test uses <a class="reference internal" href="../library/re.html#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">re.search()</span></tt></a>, not <a class="reference internal" href="../library/re.html#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">re.match()</span></tt></a>.  Other methods using
regular expressions are now named using short form &#8220;Regex&#8221; in preference to
&#8220;Regexp&#8221; &#8211; this matches the names used in other unittest implementations,
matches Python&#8217;s old name for the <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a> module, and it has unambiguous
camel-casing.</p>
<p>(Contributed by Raymond Hettinger and implemented by Ezio Melotti.)</p>
</li>
<li><p class="first">To improve consistency, some long-standing method aliases are being
deprecated in favor of the preferred names:</p>
<blockquote>
<div><table border="1" class="docutils">
<colgroup>
<col width="51%" />
<col width="49%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head"><p class="first last">Old Name</p>
</th>
<th class="head"><p class="first last">Preferred Name</p>
</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><p class="first last"><tt class="xref py py-meth docutils literal"><span class="pre">assert_()</span></tt></p>
</td>
<td><p class="first last"><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><tt class="xref py py-meth docutils literal"><span class="pre">assertTrue()</span></tt></a></p>
</td>
</tr>
<tr><td><p class="first last"><tt class="xref py py-meth docutils literal"><span class="pre">assertEquals()</span></tt></p>
</td>
<td><p class="first last"><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></tt></a></p>
</td>
</tr>
<tr><td><p class="first last"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotEquals()</span></tt></p>
</td>
<td><p class="first last"><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotEqual()</span></tt></a></p>
</td>
</tr>
<tr><td><p class="first last"><tt class="xref py py-meth docutils literal"><span class="pre">assertAlmostEquals()</span></tt></p>
</td>
<td><p class="first last"><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertAlmostEqual()</span></tt></a></p>
</td>
</tr>
<tr><td><p class="first last"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotAlmostEquals()</span></tt></p>
</td>
<td><p class="first last"><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotAlmostEqual()</span></tt></a></p>
</td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>Likewise, the <tt class="docutils literal"><span class="pre">TestCase.fail*</span></tt> 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"><em>Deprecated aliases</em></a> section in
the <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><tt class="xref py py-mod docutils literal"><span class="pre">unittest</span></tt></a> documentation.</p>
<p>(Contributed by Ezio Melotti; <a class="reference external" href="http://bugs.python.org/issue9424">issue 9424</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertDictContainsSubset" title="unittest.TestCase.assertDictContainsSubset"><tt class="xref py py-meth docutils literal"><span class="pre">assertDictContainsSubset()</span></tt></a> method was deprecated
because it was misimplemented with the arguments in the wrong order.  This
created hard-to-debug optical illusions where tests like
<tt class="docutils literal"><span class="pre">TestCase().assertDictContainsSubset({'a':1,</span> <span class="pre">'b':2},</span> <span class="pre">{'a':1})</span></tt> 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."><tt class="xref py py-mod docutils literal"><span class="pre">random</span></tt></a> module now do a better job of producing
uniform distributions.  Previously, they computed selections with
<tt class="docutils literal"><span class="pre">int(n*random())</span></tt> 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 <tt class="docutils literal"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">n</span></tt>.  The
functions and methods affected are <a class="reference internal" href="../library/random.html#random.randrange" title="random.randrange"><tt class="xref py py-func docutils literal"><span class="pre">randrange()</span></tt></a>,
<a class="reference internal" href="../library/random.html#random.randint" title="random.randint"><tt class="xref py py-func docutils literal"><span class="pre">randint()</span></tt></a>, <a class="reference internal" href="../library/random.html#random.choice" title="random.choice"><tt class="xref py py-func docutils literal"><span class="pre">choice()</span></tt></a>, <a class="reference internal" href="../library/random.html#random.shuffle" title="random.shuffle"><tt class="xref py py-func docutils literal"><span class="pre">shuffle()</span></tt></a> and
<a class="reference internal" href="../library/random.html#random.sample" title="random.sample"><tt class="xref py py-func docutils literal"><span class="pre">sample()</span></tt></a>.</p>
<p>(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue9025">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">POP3_SSL</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">ssl.SSLContext</span></tt></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="http://bugs.python.org/issue8807">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a> now provides a
<a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher.handle_accepted" title="asyncore.dispatcher.handle_accepted"><tt class="xref py py-meth docutils literal"><span class="pre">handle_accepted()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">handle_accept()</span></tt></a> and avoids
the user  to call <a class="reference internal" href="../library/asyncore.html#asyncore.dispatcher.accept" title="asyncore.dispatcher.accept"><tt class="xref py py-meth docutils literal"><span class="pre">accept()</span></tt></a> directly.</p>
<p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="http://bugs.python.org/issue6706">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">tempfile</span></tt></a> module has a new context manager,
<a class="reference internal" href="../library/tempfile.html#tempfile.TemporaryDirectory" title="tempfile.TemporaryDirectory"><tt class="xref py py-class docutils literal"><span class="pre">TemporaryDirectory</span></tt></a> which provides easy deterministic
cleanup of temporary directories:</p>
<div class="highlight-python3"><div class="highlight"><pre><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="s">&#39;created temporary dir:&#39;</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="http://bugs.python.org/issue5178">issue 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 class="first">The <a class="reference internal" href="../library/inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><tt class="xref py py-mod docutils literal"><span class="pre">inspect</span></tt></a> module has a new function
<a class="reference internal" href="../library/inspect.html#inspect.getgeneratorstate" title="inspect.getgeneratorstate"><tt class="xref py py-func docutils literal"><span class="pre">getgeneratorstate()</span></tt></a> to easily identify the current state of a
generator-iterator:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getgeneratorstate</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
<span class="go">        yield &#39;demo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">gen</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">&#39;GEN_CREATED&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">&#39;demo&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">&#39;GEN_SUSPENDED&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="k">None</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
<span class="go">&#39;GEN_CLOSED&#39;</span>
</pre></div>
</div>
<p>(Contributed by Rodolpho Eckhardt and Nick Coghlan, <a class="reference external" href="http://bugs.python.org/issue10220">issue 10220</a>.)</p>
</li>
<li><p class="first">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."><tt class="xref py py-mod docutils literal"><span class="pre">inspect</span></tt></a> module has a new function, <a class="reference internal" href="../library/inspect.html#inspect.getattr_static" title="inspect.getattr_static"><tt class="xref py py-func docutils literal"><span class="pre">getattr_static()</span></tt></a>.
Unlike <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a>, this is a true read-only search, guaranteed not to
change state while it is searching:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
<span class="go">        @property</span>
<span class="go">        def f(self):</span>
<span class="go">            print(&#39;Running&#39;)</span>
<span class="go">            return 10</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">getattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s">&#39;f&#39;</span><span class="p">)</span>
<span class="go">Running</span>
<span class="go">10</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;f&#39;</span><span class="p">)</span>
<span class="go">&lt;property object at 0x1022bd788&gt;</span>
</pre></div>
</div>
</li>
</ul>
<blockquote>
<div>(Contributed by Michael Foord.)</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."><tt class="xref py py-mod docutils literal"><span class="pre">pydoc</span></tt></a> module now provides a much-improved Web server interface, as
well as a new command-line option <tt class="docutils literal"><span class="pre">-b</span></tt> to automatically open a browser window
to display that server:</p>
<div class="highlight-python3"><pre>$ pydoc3.2 -b</pre>
</div>
<p>(Contributed by Ron Adam; <a class="reference external" href="http://bugs.python.org/issue2001">issue 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."><tt class="xref py py-mod docutils literal"><span class="pre">dis</span></tt></a> module gained two new functions for inspecting code,
<a class="reference internal" href="../library/dis.html#dis.code_info" title="dis.code_info"><tt class="xref py py-func docutils literal"><span class="pre">code_info()</span></tt></a> and <a class="reference internal" href="../library/dis.html#dis.show_code" title="dis.show_code"><tt class="xref py py-func docutils literal"><span class="pre">show_code()</span></tt></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"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">dis</span><span class="o">,</span> <span class="nn">random</span>
<span class="gp">&gt;&gt;&gt; </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: &#39;Choose a random element from a non-empty sequence.&#39;</span>
<span class="go">   1: &#39;Cannot choose from an empty sequence&#39;</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"><tt class="xref py py-func docutils literal"><span class="pre">dis()</span></tt></a> function now accepts string arguments
so that the common idiom <tt class="docutils literal"><span class="pre">dis(compile(s,</span> <span class="pre">'',</span> <span class="pre">'eval'))</span></tt> can be shortened
to <tt class="docutils literal"><span class="pre">dis(s)</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">dis</span><span class="p">(</span><span class="s">&#39;3*x+1 if x%2==1 else x//2&#39;</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">        &gt;&gt;   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="http://bugs.python.org/issue9147">issue 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 <tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">setdefault()</span></tt> methods.</p>
<p>(Suggested by Ray Allen in <a class="reference external" href="http://bugs.python.org/issue9523">issue 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"><tt class="xref py py-class docutils literal"><span class="pre">ctypes.c_ssize_t</span></tt></a> represents the C <tt class="xref c c-type docutils literal"><span class="pre">ssize_t</span></tt> 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."><tt class="xref py py-mod docutils literal"><span class="pre">site</span></tt></a> module has three new functions useful for reporting on the
details of a given Python installation.</p>
<ul class="simple">
<li><a class="reference internal" href="../library/site.html#site.getsitepackages" title="site.getsitepackages"><tt class="xref py py-func docutils literal"><span class="pre">getsitepackages()</span></tt></a> lists all global site-packages directories.</li>
<li><a class="reference internal" href="../library/site.html#site.getuserbase" title="site.getuserbase"><tt class="xref py py-func docutils literal"><span class="pre">getuserbase()</span></tt></a> reports on the user&#8217;s base directory where data can
be stored.</li>
<li><a class="reference internal" href="../library/site.html#site.getusersitepackages" title="site.getusersitepackages"><tt class="xref py py-func docutils literal"><span class="pre">getusersitepackages()</span></tt></a> reveals the user-specific site-packages
directory path.</li>
</ul>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">site</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getsitepackages</span><span class="p">()</span>
<span class="go">[&#39;/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages&#39;,</span>
<span class="go"> &#39;/Library/Frameworks/Python.framework/Versions/3.2/lib/site-python&#39;,</span>
<span class="go"> &#39;/Library/Python/3.2/site-packages&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getuserbase</span><span class="p">()</span>
<span class="go">&#39;/Users/raymondhettinger/Library/Python/3.2&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
<span class="go">&#39;/Users/raymondhettinger/Library/Python/3.2/lib/python/site-packages&#39;</span>
</pre></div>
</div>
<p>Conveniently, some of site&#8217;s functionality is accessible directly from the
command-line:</p>
<div class="highlight-python3"><pre>$ python -m site --user-base
/Users/raymondhettinger/.local
$ python -m site --user-site
/Users/raymondhettinger/.local/lib/python3.2/site-packages</pre>
</div>
<p>(Contributed by Tarek Ziadé in <a class="reference external" href="http://bugs.python.org/issue6693">issue 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"><tt class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></tt></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><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_platform" title="sysconfig.get_platform"><tt class="xref py py-func docutils literal"><span class="pre">get_platform()</span></tt></a> returning values like <em>linux-i586</em> or
<em>macosx-10.6-ppc</em>.</li>
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_python_version" title="sysconfig.get_python_version"><tt class="xref py py-func docutils literal"><span class="pre">get_python_version()</span></tt></a> returns a Python version string
such as &#8220;3.2&#8221;.</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."><tt class="xref py py-mod docutils literal"><span class="pre">distutils</span></tt></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><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_paths" title="sysconfig.get_paths"><tt class="xref py py-func docutils literal"><span class="pre">get_paths()</span></tt></a> makes a dictionary containing installation paths
for the current installation scheme.</li>
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_vars" title="sysconfig.get_config_vars"><tt class="xref py py-func docutils literal"><span class="pre">get_config_vars()</span></tt></a> returns a dictionary of platform specific
variables.</li>
</ul>
<p>There is also a convenient command-line interface:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">C</span><span class="p">:</span>\<span class="n">Python32</span><span class="o">&gt;</span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">sysconfig</span>
<span class="n">Platform</span><span class="p">:</span> <span class="s">&quot;win32&quot;</span>
<span class="n">Python</span> <span class="n">version</span><span class="p">:</span> <span class="s">&quot;3.2&quot;</span>
<span class="n">Current</span> <span class="n">installation</span> <span class="n">scheme</span><span class="p">:</span> <span class="s">&quot;nt&quot;</span>

<span class="n">Paths</span><span class="p">:</span>
        <span class="n">data</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">include</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Include&quot;</span>
        <span class="n">platinclude</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Include&quot;</span>
        <span class="n">platlib</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib\site-packages&quot;</span>
        <span class="n">platstdlib</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib&quot;</span>
        <span class="n">purelib</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib\site-packages&quot;</span>
        <span class="n">scripts</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Scripts&quot;</span>
        <span class="n">stdlib</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib&quot;</span>

<span class="n">Variables</span><span class="p">:</span>
        <span class="n">BINDIR</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">BINLIBDEST</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib&quot;</span>
        <span class="n">EXE</span> <span class="o">=</span> <span class="s">&quot;.exe&quot;</span>
        <span class="n">INCLUDEPY</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Include&quot;</span>
        <span class="n">LIBDEST</span> <span class="o">=</span> <span class="s">&quot;C:\Python32\Lib&quot;</span>
        <span class="n">SO</span> <span class="o">=</span> <span class="s">&quot;.pyd&quot;</span>
        <span class="n">VERSION</span> <span class="o">=</span> <span class="s">&quot;32&quot;</span>
        <span class="n">abiflags</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
        <span class="n">base</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">exec_prefix</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">platbase</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">prefix</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">projectbase</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">py_version</span> <span class="o">=</span> <span class="s">&quot;3.2&quot;</span>
        <span class="n">py_version_nodot</span> <span class="o">=</span> <span class="s">&quot;32&quot;</span>
        <span class="n">py_version_short</span> <span class="o">=</span> <span class="s">&quot;3.2&quot;</span>
        <span class="n">srcdir</span> <span class="o">=</span> <span class="s">&quot;C:\Python32&quot;</span>
        <span class="n">userbase</span> <span class="o">=</span> <span class="s">&quot;C:\Documents and Settings\Raymond\Application Data\Python&quot;</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."><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> debugger module gained a number of usability improvements:</p>
<ul class="simple">
<li><tt class="file docutils literal"><span class="pre">pdb.py</span></tt> now has a <tt class="docutils literal"><span class="pre">-c</span></tt> option that executes commands as given in a
<tt class="file docutils literal"><span class="pre">.pdbrc</span></tt> script file.</li>
<li>A <tt class="file docutils literal"><span class="pre">.pdbrc</span></tt> script file can contain <tt class="docutils literal"><span class="pre">continue</span></tt> and <tt class="docutils literal"><span class="pre">next</span></tt> commands
that continue debugging.</li>
<li>The <tt class="xref py py-class docutils literal"><span class="pre">Pdb</span></tt> class constructor now accepts a <em>nosigint</em> argument.</li>
<li>New commands: <tt class="docutils literal"><span class="pre">l(list)</span></tt>, <tt class="docutils literal"><span class="pre">ll(long</span> <span class="pre">list)</span></tt> and <tt class="docutils literal"><span class="pre">source</span></tt> for
listing source code.</li>
<li>New commands: <tt class="docutils literal"><span class="pre">display</span></tt> and <tt class="docutils literal"><span class="pre">undisplay</span></tt> for showing or hiding
the value of an expression if it has changed.</li>
<li>New command: <tt class="docutils literal"><span class="pre">interact</span></tt> for starting an interactive interpreter containing
the global and local  names found in the current scope.</li>
<li>Breakpoints can be cleared by breakpoint number.</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."><tt class="xref py py-mod docutils literal"><span class="pre">configparser</span></tt></a> module was modified to improve usability and
predictability of the default parser and its supported INI syntax.  The old
<tt class="xref py py-class docutils literal"><span class="pre">ConfigParser</span></tt> class was removed in favor of <tt class="xref py py-class docutils literal"><span class="pre">SafeConfigParser</span></tt>
which has in turn been renamed to <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><tt class="xref py py-class docutils literal"><span class="pre">ConfigParser</span></tt></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"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="go">[DEFAULT]</span>
<span class="go">location = upper left</span>
<span class="go">visible = yes</span>
<span class="go">editable = no</span>
<span class="go">color = blue</span>

<span class="go">[main]</span>
<span class="go">title = Main Menu</span>
<span class="go">color = green</span>

<span class="go">[options]</span>
<span class="go">title = Options</span>
<span class="go">&quot;&quot;&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s">&#39;main&#39;</span><span class="p">][</span><span class="s">&#39;color&#39;</span><span class="p">]</span>
<span class="go">&#39;green&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s">&#39;main&#39;</span><span class="p">][</span><span class="s">&#39;editable&#39;</span><span class="p">]</span>
<span class="go">&#39;no&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s">&#39;options&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span>
<span class="go">&#39;Options&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;Options (editable: %(editable)s)&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s">&#39;title&#39;</span><span class="p">]</span>
<span class="go">&#39;Options (editable: no)&#39;</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"><tt class="xref py py-class docutils literal"><span class="pre">ExtendedInterpolation</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_dict</span><span class="p">({</span><span class="s">&#39;buildout&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;directory&#39;</span><span class="p">:</span> <span class="s">&#39;/home/ambv/zope9&#39;</span><span class="p">},</span>
<span class="go">                      &#39;custom&#39;: {&#39;prefix&#39;: &#39;/usr/local&#39;}})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="go">    [buildout]</span>
<span class="go">    parts =</span>
<span class="go">      zope9</span>
<span class="go">      instance</span>
<span class="go">    find-links =</span>
<span class="go">      ${buildout:directory}/downloads/dist</span>

<span class="go">    [zope9]</span>
<span class="go">    recipe = plone.recipe.zope9install</span>
<span class="go">    location = /opt/zope</span>

<span class="go">    [instance]</span>
<span class="go">    recipe = plone.recipe.zope9instance</span>
<span class="go">    zope9-location = ${zope9:location}</span>
<span class="go">    zope-conf = ${custom:prefix}/etc/zope.conf</span>
<span class="go">    &quot;&quot;&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s">&#39;buildout&#39;</span><span class="p">][</span><span class="s">&#39;find-links&#39;</span><span class="p">]</span>
<span class="go">&#39;\n/home/ambv/zope9/downloads/dist&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s">&#39;instance&#39;</span><span class="p">][</span><span class="s">&#39;zope-conf&#39;</span><span class="p">]</span>
<span class="go">&#39;/usr/local/etc/zope.conf&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s">&#39;instance&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span><span class="p">[</span><span class="s">&#39;zope-conf&#39;</span><span class="p">]</span>
<span class="go">&#39;/usr/local/etc/zope.conf&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span><span class="p">[</span><span class="s">&#39;zope9-location&#39;</span><span class="p">]</span>
<span class="go">&#39;/opt/zope&#39;</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."><tt class="xref py py-mod docutils literal"><span class="pre">urllib.parse</span></tt></a> module.</p>
<p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlparse" title="urllib.parse.urlparse"><tt class="xref py py-func docutils literal"><span class="pre">urlparse()</span></tt></a> function now supports <a class="reference external" href="http://en.wikipedia.org/wiki/IPv6">IPv6</a> addresses as described in <span class="target" id="index-13"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
<span class="gp">&gt;&gt;&gt; </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="s">&#39;http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/&#39;</span><span class="p">)</span>
<span class="go">ParseResult(scheme=&#39;http&#39;,</span>
<span class="go">            netloc=&#39;[dead:beef:cafe:5417:affe:8FA3:deaf:feed]&#39;,</span>
<span class="go">            path=&#39;/foo/&#39;,</span>
<span class="go">            params=&#39;&#39;,</span>
<span class="go">            query=&#39;&#39;,</span>
<span class="go">            fragment=&#39;&#39;)</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urldefrag" title="urllib.parse.urldefrag"><tt class="xref py py-func docutils literal"><span class="pre">urldefrag()</span></tt></a> function now returns a <a class="reference internal" href="../glossary.html#term-named-tuple"><em class="xref std std-term">named tuple</em></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="s">&#39;http://python.org/about/#target&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span>
<span class="go">DefragResult(url=&#39;http://python.org/about/&#39;, fragment=&#39;target&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="go">&#39;http://python.org/about/&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">fragment</span>
<span class="go">&#39;target&#39;</span>
</pre></div>
</div>
<p>And, the <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><tt class="xref py py-func docutils literal"><span class="pre">urlencode()</span></tt></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"><tt class="xref py py-func docutils literal"><span class="pre">quote_plus()</span></tt></a> for encoding:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="go">         (&#39;type&#39;, &#39;telenovela&#39;),</span>
<span class="go">         (&#39;name&#39;, &#39;¿Dónde Está Elisa?&#39;)],</span>
<span class="go">         encoding=&#39;latin-1&#39;)</span>
<span class="go">&#39;type=telenovela&amp;name=%BFD%F3nde+Est%E1+Elisa%3F&#39;</span>
</pre></div>
</div>
<p>As detailed in <a class="reference internal" href="../library/urllib.parse.html#parsing-ascii-encoded-bytes"><em>Parsing ASCII Encoded Bytes</em></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."><tt class="xref py py-mod docutils literal"><span class="pre">urllib.parse</span></tt></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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="n">b</span><span class="s">&#39;http://www.python.org:80/about/&#39;</span><span class="p">)</span>
<span class="go">ParseResultBytes(scheme=b&#39;http&#39;, netloc=b&#39;www.python.org:80&#39;,</span>
<span class="go">                 path=b&#39;/about/&#39;, params=b&#39;&#39;, query=b&#39;&#39;, fragment=b&#39;&#39;)</span>
</pre></div>
</div>
<p>(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in <a class="reference external" href="http://bugs.python.org/issue2987">issue 2987</a>,
<a class="reference external" href="http://bugs.python.org/issue5468">issue 5468</a>, and <a class="reference external" href="http://bugs.python.org/issue9873">issue 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"><tt class="xref py py-mod docutils literal"><span class="pre">mailbox</span></tt></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/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></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, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a> package&#8217;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"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a> method for
<a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">mailbox.Mailbox</span></tt></a> objects now accepts binary input.</p>
<p><a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">get_file()</span></tt></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"><tt class="xref py py-meth docutils literal"><span class="pre">get_bytes()</span></tt></a>
method that returns a <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></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&#8217;s
<a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_string" title="mailbox.Mailbox.get_string"><tt class="xref py py-meth docutils literal"><span class="pre">get_string()</span></tt></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"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></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="http://bugs.python.org/issue9124">issue 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"><tt class="xref py py-mod docutils literal"><span class="pre">turtle</span></tt></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"><tt class="xref py py-attr docutils literal"><span class="pre">sys.path</span></tt></a>, it can now be run directly
from the command-line:</p>
<div class="highlight-python3"><pre>$ python -m turtledemo</pre>
</div>
<p>(Moved from the Demo directory by Alexander Belopolsky in <a class="reference external" href="http://bugs.python.org/issue10199">issue 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 class="first">The mechanism for serializing execution of concurrently running Python threads
(generally known as the <a class="reference internal" href="../glossary.html#term-gil"><em class="xref std std-term">GIL</em></a> or <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><em class="xref std std-term">Global Interpreter Lock</em></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 &#8220;check interval&#8221; 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"><tt class="xref py py-func docutils literal"><span class="pre">sys.setswitchinterval()</span></tt></a>.
It currently defaults to 5 milliseconds.</p>
<p>Additional details about the implementation can be read from a <a class="reference external" href="http://mail.python.org/pipermail/python-dev/2009-October/093321.html">python-dev
mailing-list message</a>
(however, &#8220;priority requests&#8221; as exposed in this message have not been kept
for inclusion).</p>
<p>(Contributed by Antoine Pitrou.)</p>
</li>
<li><p class="first">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"><tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt></a> method.  (Contributed by Antoine Pitrou;
<a class="reference external" href="http://bugs.python.org/issue7316">issue 7316</a>.)</p>
</li>
<li><p class="first">Similarly, <a class="reference internal" href="../library/threading.html#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><tt class="xref py py-meth docutils literal"><span class="pre">threading.Semaphore.acquire()</span></tt></a> also gained a <em>timeout</em>
argument.  (Contributed by Torsten Landschoff; <a class="reference external" href="http://bugs.python.org/issue850728">issue 850728</a>.)</p>
</li>
<li><p class="first">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 <tt class="kbd docutils literal"><span class="pre">Ctrl+C</span></tt> in most shells).
(Contributed by Reid Kleckner; <a class="reference external" href="http://bugs.python.org/issue8844">issue 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 class="first">Python&#8217;s peephole optimizer now recognizes patterns such <tt class="docutils literal"><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></tt> 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"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> as a <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><tt class="xref py py-class docutils literal"><span class="pre">frozenset</span></tt></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"><div class="highlight"><pre><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="s">&#39;.&#39;</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="s">&#39;xml&#39;</span><span class="p">,</span> <span class="s">&#39;html&#39;</span><span class="p">,</span> <span class="s">&#39;xhtml&#39;</span><span class="p">,</span> <span class="s">&#39;css&#39;</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="http://bugs.python.org/issue6690">issue 6690</a>).</p>
</li>
<li><p class="first">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."><tt class="xref py py-mod docutils literal"><span class="pre">pickle</span></tt></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="http://bugs.python.org/issue9410">issue 9410</a> and <a class="reference external" href="http://bugs.python.org/issue3873">issue 3873</a>.)</p>
</li>
<li><p class="first">The <a class="reference external" href="http://en.wikipedia.org/wiki/Timsort">Timsort algorithm</a> used in
<tt class="xref py py-meth docutils literal"><span class="pre">list.sort()</span></tt> and <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a> now runs faster and uses less memory
when called with a <a class="reference internal" href="../glossary.html#term-key-function"><em class="xref std std-term">key function</em></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="http://bugs.python.org/issue9915">issue 9915</a>.)</p>
</li>
<li><p class="first">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 <tt class="docutils literal"><span class="pre">sort_keys</span></tt> argument is true.</p>
<p>(Contributed by Antoine Pitrou in <a class="reference external" href="http://bugs.python.org/issue7451">issue 7451</a> and by Raymond Hettinger and
Antoine Pitrou in <a class="reference external" href="http://bugs.python.org/issue10314">issue 10314</a>.)</p>
</li>
<li><p class="first">Recursive locks (created with the <a class="reference internal" href="../library/threading.html#threading.RLock" title="threading.RLock"><tt class="xref py py-func docutils literal"><span class="pre">threading.RLock()</span></tt></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="http://bugs.python.org/issue3001">issue 3001</a>.)</p>
</li>
<li><p class="first">The fast-search algorithm in stringlib is now used by the <tt class="xref py py-meth docutils literal"><span class="pre">split()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">splitlines()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">replace()</span></tt> methods on
<a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>, <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> and <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> objects. Likewise, the
algorithm is also used by <tt class="xref py py-meth docutils literal"><span class="pre">rfind()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">rindex()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">rsplit()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">rpartition()</span></tt>.</p>
<p>(Patch by Florent Xicluna in <a class="reference external" href="http://bugs.python.org/issue7622">issue 7622</a> and <a class="reference external" href="http://bugs.python.org/issue7462">issue 7462</a>.)</p>
</li>
<li><p class="first">String to integer conversions now work two &#8220;digits&#8221; at a time, reducing the
number of division and modulo operations.</p>
<p>(<a class="reference external" href="http://bugs.python.org/issue6713">issue 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="http://bugs.python.org/issue8685">issue 8685</a>).  The <tt class="xref py py-meth docutils literal"><span class="pre">array.repeat()</span></tt> method has a faster implementation
(<a class="reference external" href="http://bugs.python.org/issue1569291">issue 1569291</a> by Alexander Belopolsky). The <tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPRequestHandler</span></tt>
has more efficient buffering (<a class="reference external" href="http://bugs.python.org/issue3709">issue 3709</a> by Andrew Schaaf).  The
<a class="reference internal" href="../library/operator.html#operator.attrgetter" title="operator.attrgetter"><tt class="xref py py-func docutils literal"><span class="pre">operator.attrgetter()</span></tt></a> function has been sped-up (<a class="reference external" href="http://bugs.python.org/issue10160">issue 10160</a> by
Christos Georgiou).  And <tt class="xref py py-class docutils literal"><span class="pre">ConfigParser</span></tt> loads multi-line arguments a bit
faster (<a class="reference external" href="http://bugs.python.org/issue7113">issue 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="http://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="http://bugs.python.org/issue1616979">issue 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"><tt class="xref py py-exc docutils literal"><span class="pre">UnicodeDecodeError</span></tt></a> when it encounters an
undecodable byte sequence and an <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><tt class="xref py py-exc docutils literal"><span class="pre">UnicodeEncodeError</span></tt></a> for an unencodable
character.</p>
<p>The MBCS codec supports <tt class="docutils literal"><span class="pre">'strict'</span></tt> and <tt class="docutils literal"><span class="pre">'ignore'</span></tt> error handlers for
decoding, and <tt class="docutils literal"><span class="pre">'strict'</span></tt> and <tt class="docutils literal"><span class="pre">'replace'</span></tt> for encoding.</p>
<p>To emulate Python3.1 MBCS encoding, select the <tt class="docutils literal"><span class="pre">'ignore'</span></tt> handler for decoding
and the <tt class="docutils literal"><span class="pre">'replace'</span></tt> handler for encoding.</p>
<p>On Mac OS X, Python decodes command line arguments with <tt class="docutils literal"><span class="pre">'utf-8'</span></tt> 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."><tt class="xref py py-mod docutils literal"><span class="pre">tarfile</span></tt></a> uses <tt class="docutils literal"><span class="pre">'utf-8'</span></tt> encoding on Windows (instead of
<tt class="docutils literal"><span class="pre">'mbcs'</span></tt>) and the <tt class="docutils literal"><span class="pre">'surrogateescape'</span></tt> 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 class="first">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"><em>Built-in Functions</em></a>.  In the case of <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></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 class="first">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."><tt class="xref py py-mod docutils literal"><span class="pre">functools</span></tt></a> module
documentation has a quick link at the top labeled:</p>
<blockquote>
<div><p><strong>Source code</strong> <a class="reference external" href="http://hg.python.org/cpython/file/3.2/Lib/functools.py">Lib/functools.py</a>.</p>
</div></blockquote>
<p>(Contributed by Raymond Hettinger; see
<a class="reference external" href="http://rhettinger.wordpress.com/2011/01/28/open-your-source-more/">rationale</a>.)</p>
</li>
<li><p class="first">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."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a>
module has an extensive section, <a class="reference internal" href="../library/re.html#re-examples"><em>Regular Expression Examples</em></a>.  Likewise, the
<a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> module continues to be updated with new
<a class="reference internal" href="../library/itertools.html#itertools-recipes"><em>Itertools Recipes</em></a>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></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="http://bugs.python.org/issue9528">issue 9528</a>.)</p>
</li>
<li><p class="first">The unmaintained <tt class="file docutils literal"><span class="pre">Demo</span></tt> directory has been removed.  Some demos were
integrated into the documentation, some were moved to the <tt class="file docutils literal"><span class="pre">Tools/demo</span></tt>
directory, and others were removed altogether.</p>
<p>(Contributed by Georg Brandl in <a class="reference external" href="http://bugs.python.org/issue7962">issue 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 class="first">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="http://bugs.python.org/issue5150">issue 5150</a>.)</p>
</li>
<li><p class="first">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="http://bugs.python.org/issue6075">issue 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="http://mercurial.selenic.com/">Mercurial</a> repository at
<a class="reference external" href="http://hg.python.org/">http://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="http://www.python.org/dev/peps/pep-0385"><strong>PEP 385</strong></a> for details.</p>
<p>To learn the new version control system, see the <a class="reference external" href="http://hginit.com">tutorial by Joel
Spolsky</a> or the <a class="reference external" href="http://mercurial.selenic.com/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&#8217;s build process and to the C API include:</p>
<ul>
<li><p class="first">The <em>idle</em>, <em>pydoc</em> and <em>2to3</em> scripts are now installed with a
version-specific suffix on <tt class="docutils literal"><span class="pre">make</span> <span class="pre">altinstall</span></tt> (<a class="reference external" href="http://bugs.python.org/issue10679">issue 10679</a>).</p>
</li>
<li><p class="first">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"><tt class="xref py py-func docutils literal"><span class="pre">unicodedata.numeric()</span></tt></a> now returns the correct value
for large code points, and <a class="reference internal" href="../library/functions.html#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> may consider more characters as
printable.</p>
<p>(Reported by Bupjoe Lee and fixed by Amaury Forgeot D&#8217;Arc; <a class="reference external" href="http://bugs.python.org/issue5127">issue 5127</a>.)</p>
</li>
<li><p class="first">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 <tt class="docutils literal"><span class="pre">--without-computed-gotos</span></tt>.</p>
<p>(Contributed by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue9203">issue 9203</a>.)</p>
</li>
<li><p class="first">The option <tt class="docutils literal"><span class="pre">--with-wctype-functions</span></tt> was removed.  The built-in unicode
database is now used for all functions.</p>
<p>(Contributed by Amaury Forgeot D&#8217;Arc; <a class="reference external" href="http://bugs.python.org/issue9210">issue 9210</a>.)</p>
</li>
<li><p class="first">Hash values are now values of a new type, <tt class="xref c c-type docutils literal"><span class="pre">Py_hash_t</span></tt>, 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"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> can now hold more than
<tt class="docutils literal"><span class="pre">2**32</span></tt> 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="http://bugs.python.org/issue9778">issue 9778</a>.)</p>
</li>
<li><p class="first">A new macro <tt class="xref c c-macro docutils literal"><span class="pre">Py_VA_COPY</span></tt> 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="http://bugs.python.org/issue2443">issue 2443</a>).</p>
</li>
<li><p class="first">A new C API function <a class="reference internal" href="../c-api/init.html#PySys_SetArgvEx" title="PySys_SetArgvEx"><tt class="xref c c-func docutils literal"><span class="pre">PySys_SetArgvEx()</span></tt></a> allows an embedded interpreter
to set <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><tt class="xref py py-attr docutils literal"><span class="pre">sys.argv</span></tt></a> without also modifying <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><tt class="xref py py-attr docutils literal"><span class="pre">sys.path</span></tt></a>
(<a class="reference external" href="http://bugs.python.org/issue5753">issue 5753</a>).</p>
</li>
<li><p class="first"><tt class="xref c c-macro docutils literal"><span class="pre">PyEval_CallObject</span></tt> is now only available in macro form.  The
function declaration, which was kept for backwards compatibility reasons, is
now removed &#8211; the macro was introduced in 1997 (<a class="reference external" href="http://bugs.python.org/issue8276">issue 8276</a>).</p>
</li>
<li><p class="first">There is a new function <a class="reference internal" href="../c-api/long.html#PyLong_AsLongLongAndOverflow" title="PyLong_AsLongLongAndOverflow"><tt class="xref c c-func docutils literal"><span class="pre">PyLong_AsLongLongAndOverflow()</span></tt></a> which
is analogous to <a class="reference internal" href="../c-api/long.html#PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><tt class="xref c c-func docutils literal"><span class="pre">PyLong_AsLongAndOverflow()</span></tt></a>.  They both serve to
convert Python <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> into a native fixed-width type while providing
detection of cases where the conversion won&#8217;t fit (<a class="reference external" href="http://bugs.python.org/issue7767">issue 7767</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../c-api/unicode.html#PyUnicode_CompareWithASCIIString" title="PyUnicode_CompareWithASCIIString"><tt class="xref c c-func docutils literal"><span class="pre">PyUnicode_CompareWithASCIIString()</span></tt></a> function now returns <em>not
equal</em> if the Python string is <em>NUL</em> terminated.</p>
</li>
<li><p class="first">There is a new function <a class="reference internal" href="../c-api/exceptions.html#PyErr_NewExceptionWithDoc" title="PyErr_NewExceptionWithDoc"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_NewExceptionWithDoc()</span></tt></a> that is
like <a class="reference internal" href="../c-api/exceptions.html#PyErr_NewException" title="PyErr_NewException"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_NewException()</span></tt></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="http://bugs.python.org/issue7033">issue 7033</a>).</p>
</li>
<li><p class="first">When compiled with the <tt class="docutils literal"><span class="pre">--with-valgrind</span></tt> 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="http://bugs.python.org/issue2422">issue 2422</a>).</p>
</li>
<li><p class="first">Removed the <tt class="docutils literal"><span class="pre">O?</span></tt> 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="http://bugs.python.org/issue8837">issue 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="http://hg.python.org/cpython/file/3.2/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="http://hg.python.org/cpython/file/3.2/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="http://www.activestate.com/activetcl/downloads">ActiveState Tcl/Tk 8.5.9</a>.
See <a class="reference external" href="http://www.python.org/download/mac/tcltk/">http://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 class="first">The <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><tt class="xref py py-mod docutils literal"><span class="pre">configparser</span></tt></a> module has a number of clean-ups.  The major change is
to replace the old <tt class="xref py py-class docutils literal"><span class="pre">ConfigParser</span></tt> class with long-standing preferred
alternative <tt class="xref py py-class docutils literal"><span class="pre">SafeConfigParser</span></tt>.  In addition there are a number of
smaller incompatibilities:</p>
<ul class="simple">
<li>The interpolation syntax is now validated on
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.get" title="configparser.ConfigParser.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> and
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> operations. In the default
interpolation scheme, only two tokens with percent signs are valid: <tt class="docutils literal"><span class="pre">%(name)s</span></tt>
and <tt class="docutils literal"><span class="pre">%%</span></tt>, the latter being an escaped percent sign.</li>
<li>The <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><tt class="xref py py-meth docutils literal"><span class="pre">set()</span></tt></a> and
<a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.add_section" title="configparser.ConfigParser.add_section"><tt class="xref py py-meth docutils literal"><span class="pre">add_section()</span></tt></a> methods now verify that
values are actual strings.  Formerly, unsupported types could be introduced
unintentionally.</li>
<li>Duplicate sections or options from a single source now raise either
<a class="reference internal" href="../library/configparser.html#configparser.DuplicateSectionError" title="configparser.DuplicateSectionError"><tt class="xref py py-exc docutils literal"><span class="pre">DuplicateSectionError</span></tt></a> or
<a class="reference internal" href="../library/configparser.html#configparser.DuplicateOptionError" title="configparser.DuplicateOptionError"><tt class="xref py py-exc docutils literal"><span class="pre">DuplicateOptionError</span></tt></a>.  Formerly, duplicates would
silently overwrite a previous entry.</li>
<li>Inline comments are now disabled by default so now the <strong>;</strong> character
can be safely used in values.</li>
<li>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.</li>
<li><tt class="docutils literal"><span class="pre">&quot;&quot;</span></tt> is now a valid value and is no longer automatically converted to an
empty string. For empty strings, use <tt class="docutils literal"><span class="pre">&quot;option</span> <span class="pre">=&quot;</span></tt> in a line.</li>
</ul>
</li>
<li><p class="first">The <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">nntplib</span></tt></a> module was reworked extensively, meaning that its APIs
are often incompatible with the 3.1 APIs.</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> objects can no longer be used as filenames; instead,
they should be converted to <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>.</p>
</li>
<li><p class="first">The <tt class="xref py py-meth docutils literal"><span class="pre">array.tostring()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">array.fromstring()</span></tt> have been renamed to
<tt class="xref py py-meth docutils literal"><span class="pre">array.tobytes()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">array.frombytes()</span></tt> for clarity.  The old names
have been deprecated. (See <a class="reference external" href="http://bugs.python.org/issue8990">issue 8990</a>.)</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">PyArg_Parse*()</span></tt> functions:</p>
<ul class="simple">
<li>&#8220;t#&#8221; format has been removed: use &#8220;s#&#8221; or &#8220;s*&#8221; instead</li>
<li>&#8220;w&#8221; and &#8220;w#&#8221; formats has been removed: use &#8220;w*&#8221; instead</li>
</ul>
</li>
<li><p class="first">The <tt class="xref c c-type docutils literal"><span class="pre">PyCObject</span></tt> 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#PyCapsule" title="PyCapsule"><tt class="xref c c-type docutils literal"><span class="pre">PyCapsule</span></tt></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 class="first">The <tt class="xref py py-func docutils literal"><span class="pre">sys.setfilesystemencoding()</span></tt> function was removed because
it had a flawed design.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/random.html#random.seed" title="random.seed"><tt class="xref py py-func docutils literal"><span class="pre">random.seed()</span></tt></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>,
<tt class="docutils literal"><span class="pre">random.seed(s,</span> <span class="pre">version=1)</span></tt>.</p>
</li>
<li><p class="first">The previously deprecated <tt class="xref py py-func docutils literal"><span class="pre">string.maketrans()</span></tt> function has been removed
in favor of the static methods <a class="reference internal" href="../library/stdtypes.html#bytes.maketrans" title="bytes.maketrans"><tt class="xref py py-meth docutils literal"><span class="pre">bytes.maketrans()</span></tt></a> and
<a class="reference internal" href="../library/stdtypes.html#bytearray.maketrans" title="bytearray.maketrans"><tt class="xref py py-meth docutils literal"><span class="pre">bytearray.maketrans()</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module.  Now, <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>,
<a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>, and <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></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="http://bugs.python.org/issue5675">issue 5675</a>.)</p>
</li>
<li><p class="first">The previously deprecated <tt class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></tt> function has been removed
in favor of a plain <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></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"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;mylog.txt&#39;</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="s">&#39;a.out&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</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="s">&#39;&lt;critical&gt;&#39;</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="http://codereview.appspot.com/53094">appspot issue 53094</a>.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><tt class="xref py py-func docutils literal"><span class="pre">struct.pack()</span></tt></a> now only allows bytes for the <tt class="docutils literal"><span class="pre">s</span></tt> 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 <tt class="docutils literal"><span class="pre">struct.pack('&lt;6sHHBBB',</span> <span class="pre">'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></tt> should be rewritten
with to use bytes instead of text, <tt class="docutils literal"><span class="pre">struct.pack('&lt;6sHHBBB',</span> <span class="pre">b'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></tt>.</p>
<p>(Discovered by David Beazley and fixed by Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue10783">issue 10783</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><tt class="xref py py-class docutils literal"><span class="pre">xml.etree.ElementTree</span></tt></a> class now raises an
<tt class="xref py py-exc docutils literal"><span class="pre">xml.etree.ElementTree.ParseError</span></tt> when a parse fails. Previously it
raised a <a class="reference internal" href="../library/pyexpat.html#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><tt class="xref py py-exc docutils literal"><span class="pre">xml.parsers.expat.ExpatError</span></tt></a>.</p>
</li>
<li><p class="first">The new, longer <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> value on floats may break doctests which rely on
the old output format.</p>
</li>
<li><p class="first">In <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><tt class="xref py py-class docutils literal"><span class="pre">subprocess.Popen</span></tt></a>, the default value for <em>close_fds</em> is now
<tt class="xref docutils literal"><span class="pre">True</span></tt> under Unix; under Windows, it is <tt class="xref docutils literal"><span class="pre">True</span></tt> if the three standard
streams are set to <tt class="xref docutils literal"><span class="pre">None</span></tt>, <tt class="xref docutils literal"><span class="pre">False</span></tt> otherwise.  Previously, <em>close_fds</em>
was always <tt class="xref docutils literal"><span class="pre">False</span></tt> 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 class="first">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: Next generation URL opening library."><tt class="xref py py-mod docutils literal"><span class="pre">urllib.request</span></tt></a>
and <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">http.client</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">http.server</span></tt></a>).</p>
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue10711">issue 10711</a>.)</p>
</li>
<li><p class="first">SSL sockets in timeout mode now raise <a class="reference internal" href="../library/socket.html#socket.timeout" title="socket.timeout"><tt class="xref py py-exc docutils literal"><span class="pre">socket.timeout</span></tt></a> when a timeout
occurs, rather than a generic <a class="reference internal" href="../library/ssl.html#ssl.SSLError" title="ssl.SSLError"><tt class="xref py py-exc docutils literal"><span class="pre">SSLError</span></tt></a>.</p>
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue10272">issue 10272</a>.)</p>
</li>
<li><p class="first">The misleading functions <a class="reference internal" href="../c-api/init.html#PyEval_AcquireLock" title="PyEval_AcquireLock"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_AcquireLock()</span></tt></a> and
<a class="reference internal" href="../c-api/init.html#PyEval_ReleaseLock" title="PyEval_ReleaseLock"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_ReleaseLock()</span></tt></a> have been officially deprecated.  The
thread-state aware APIs (such as <a class="reference internal" href="../c-api/init.html#PyEval_SaveThread" title="PyEval_SaveThread"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_SaveThread()</span></tt></a>
and <a class="reference internal" href="../c-api/init.html#PyEval_RestoreThread" title="PyEval_RestoreThread"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_RestoreThread()</span></tt></a>) should be used instead.</p>
</li>
<li><p class="first">Due to security risks, <tt class="xref py py-func docutils literal"><span class="pre">asyncore.handle_accept()</span></tt> has been deprecated, and
a new function, <tt class="xref py py-func docutils literal"><span class="pre">asyncore.handle_accepted()</span></tt>, was added to replace it.</p>
<p>(Contributed by Giampaolo Rodola in <a class="reference external" href="http://bugs.python.org/issue6706">issue 6706</a>.)</p>
</li>
<li><p class="first">Due to the new <a class="reference internal" href="../glossary.html#term-gil"><em class="xref std std-term">GIL</em></a> implementation, <a class="reference internal" href="../c-api/init.html#PyEval_InitThreads" title="PyEval_InitThreads"><tt class="xref c c-func docutils literal"><span class="pre">PyEval_InitThreads()</span></tt></a>
cannot be called before <a class="reference internal" href="../c-api/init.html#Py_Initialize" title="Py_Initialize"><tt class="xref c c-func docutils literal"><span class="pre">Py_Initialize()</span></tt></a> anymore.</p>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">What&#8217;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 <tt class="docutils literal"><span class="pre">concurrent.futures</span></tt> 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="index.html"
                        title="previous chapter">What&#8217;s New in Python</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="3.1.html"
                        title="next chapter">What&#8217;s New In Python 3.1</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/whatsnew/3.2.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <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="index.html" title="What’s New in Python"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>