Sh3ll
OdayForums


Server : Apache
System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64
User : nobody ( 99)
PHP Version : 8.1.23
Disable Function : NONE
Directory :  /usr/share/doc/python-docs-2.7.5/html/whatsnew/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/doc/python-docs-2.7.5/html/whatsnew/2.7.html

<!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 2.7 &mdash; Python 2.7.5 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:     '2.7.5',
        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 2.7.5 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 2.7.5 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 2.6" href="2.6.html" />
    <link rel="prev" title="What’s New in Python" href="index.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 

  </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="2.6.html" title="What’s New in Python 2.6"
             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="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 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-2-7">
<h1>What&#8217;s New in Python 2.7<a class="headerlink" href="#what-s-new-in-python-2-7" 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-odd field"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling (amk at amk.ca)</td>
</tr>
</tbody>
</table>
<p>This article explains the new features in Python 2.7.  Python 2.7 was released
on July 3, 2010.</p>
<p>Numeric handling has been improved in many ways, for both
floating-point numbers and for 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</span></tt></a> class.
There are some useful additions to the standard library, such as a
greatly enhanced <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, 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
for parsing command-line options, convenient <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a>
and <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> classes in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module,
and many other improvements.</p>
<p>Python 2.7 is planned to be the last of the 2.x releases, so we worked
on making it a good release for the long term.  To help with porting
to Python 3, several new features from the Python 3.x series have been
included in 2.7.</p>
<p>This article doesn&#8217;t attempt to provide a complete specification of
the new features, but instead provides a convenient overview.  For
full details, you should refer to the documentation for Python 2.7 at
<a class="reference external" href="http://docs.python.org">http://docs.python.org</a>. If you want to understand the rationale for
the design and implementation, refer to the PEP for a particular new
feature or the issue on <a class="reference external" href="http://bugs.python.org">http://bugs.python.org</a> in which a change was
discussed.  Whenever possible, &#8220;What&#8217;s New in Python&#8221; links to the
bug/patch item for each change.</p>
<div class="section" id="the-future-for-python-2-x">
<span id="whatsnew27-python31"></span><h2>The Future for Python 2.x<a class="headerlink" href="#the-future-for-python-2-x" title="Permalink to this headline">¶</a></h2>
<p>Python 2.7 is intended to be the last major release in the 2.x series.
The Python maintainers are planning to focus their future efforts on
the Python 3.x series.</p>
<p>This means that 2.7 will remain in place for a long time, running
production systems that have not been ported to Python 3.x.
Two consequences of the long-term significance of 2.7 are:</p>
<ul>
<li><p class="first">It&#8217;s very likely the 2.7 release will have a longer period of
maintenance compared to earlier 2.x versions.  Python 2.7 will
continue to be maintained while the transition to 3.x continues, and
the developers are planning to support Python 2.7 with bug-fix
releases beyond the typical two years.</p>
</li>
<li><p class="first">A policy decision was made to silence warnings only of interest to
developers.  <a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a> and its
descendants are now ignored unless otherwise requested, preventing
users from seeing warnings triggered by an application.  This change
was also made in the branch that will become Python 3.2. (Discussed
on stdlib-sig and carried out in <a class="reference external" href="http://bugs.python.org/issue7319">issue 7319</a>.)</p>
<p>In previous releases, <a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a> messages were
enabled by default, providing Python developers with a clear
indication of where their code may break in a future major version
of Python.</p>
<p>However, there are increasingly many users of Python-based
applications who are not directly involved in the development of
those applications.  <a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a> messages are
irrelevant to such users, making them worry about an application
that&#8217;s actually working correctly and burdening application developers
with responding to these concerns.</p>
<p>You can re-enable display of <a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a> messages by
running Python with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><em class="xref std std-option">-Wdefault</em></a> (short form:
<a class="reference internal" href="../using/cmdline.html#cmdoption-W"><em class="xref std std-option">-Wd</em></a>) switch, or by setting the <span class="target" id="index-0"></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 to <tt class="docutils literal"><span class="pre">&quot;default&quot;</span></tt> (or <tt class="docutils literal"><span class="pre">&quot;d&quot;</span></tt>) before running
Python.  Python code can also re-enable them
by calling <tt class="docutils literal"><span class="pre">warnings.simplefilter('default')</span></tt>.</p>
</li>
</ul>
</div>
<div class="section" id="python-3-1-features">
<h2>Python 3.1 Features<a class="headerlink" href="#python-3-1-features" title="Permalink to this headline">¶</a></h2>
<p>Much as Python 2.6 incorporated features from Python 3.0,
version 2.7 incorporates some of the new features
in Python 3.1.  The 2.x series continues to provide tools
for migrating to the 3.x series.</p>
<p>A partial list of 3.1 features that were backported to 2.7:</p>
<ul class="simple">
<li>The syntax for set literals (<tt class="docutils literal"><span class="pre">{1,2,3}</span></tt> is a mutable set).</li>
<li>Dictionary and set comprehensions (<tt class="docutils literal"><span class="pre">{i:</span> <span class="pre">i*2</span> <span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">range(3)}</span></tt>).</li>
<li>Multiple context managers in a single <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.</li>
<li>A new version of the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> library, rewritten in C for performance.</li>
<li>The ordered-dictionary type described in <a class="reference internal" href="#pep-0372"><em>PEP 372: Adding an Ordered Dictionary to collections</em></a>.</li>
<li>The new <tt class="docutils literal"><span class="pre">&quot;,&quot;</span></tt> format specifier described in <a class="reference internal" href="#pep-0378"><em>PEP 378: Format Specifier for Thousands Separator</em></a>.</li>
<li>The <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> object.</li>
<li>A small subset of the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: Convenience wrappers for __import__"><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> module,
<a class="reference external" href="#importlib-section">described below</a>.</li>
<li>The <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> of a float <tt class="docutils literal"><span class="pre">x</span></tt> is shorter in many cases: it&#8217;s now
based on the shortest decimal string that&#8217;s guaranteed to round back
to <tt class="docutils literal"><span class="pre">x</span></tt>.  As in previous versions of Python, it&#8217;s guaranteed that
<tt class="docutils literal"><span class="pre">float(repr(x))</span></tt> recovers <tt class="docutils literal"><span class="pre">x</span></tt>.</li>
<li>Float-to-string and string-to-float conversions are correctly rounded.
The <a class="reference internal" href="../library/functions.html#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a> function is also now correctly rounded.</li>
<li>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> type, used to provide a C API for extension modules.</li>
<li>The <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> C API function.</li>
</ul>
<p>Other new Python3-mode warnings include:</p>
<ul class="simple">
<li><a class="reference internal" href="../library/operator.html#operator.isCallable" title="operator.isCallable"><tt class="xref py py-func docutils literal"><span class="pre">operator.isCallable()</span></tt></a> and <a class="reference internal" href="../library/operator.html#operator.sequenceIncludes" title="operator.sequenceIncludes"><tt class="xref py py-func docutils literal"><span class="pre">operator.sequenceIncludes()</span></tt></a>,
which are not supported in 3.x, now trigger warnings.</li>
<li>The <a class="reference internal" href="../using/cmdline.html#cmdoption-3"><em class="xref std std-option">-3</em></a> switch now automatically
enables the <a class="reference internal" href="../using/cmdline.html#cmdoption-Q"><em class="xref std std-option">-Qwarn</em></a> switch that causes warnings
about using classic division with integers and long integers.</li>
</ul>
</div>
<div class="section" id="pep-372-adding-an-ordered-dictionary-to-collections">
<span id="pep-0372"></span><h2>PEP 372: Adding an Ordered Dictionary to collections<a class="headerlink" href="#pep-372-adding-an-ordered-dictionary-to-collections" title="Permalink to this headline">¶</a></h2>
<p>Regular Python dictionaries iterate over key/value pairs in arbitrary order.
Over the years, a number of authors have written alternative implementations
that remember the order that the keys were originally inserted.  Based on
the experiences from those implementations, 2.7 introduces a new
<a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> class in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module.</p>
<p>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">OrderedDict</span></tt></a> API provides the same interface as regular
dictionaries but iterates over keys and values in a guaranteed order
depending on when a key was first inserted:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>                 <span class="p">(</span><span class="s">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span>                 <span class="p">(</span><span class="s">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 2), (&#39;third&#39;, 3)]</span>
</pre></div>
</div>
<p>If a new entry overwrites an existing entry, the original insertion
position is left unchanged:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 4), (&#39;third&#39;, 3)]</span>
</pre></div>
</div>
<p>Deleting an entry and reinserting it will move it to the end:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="s">&#39;second&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
<span class="go">[(&#39;first&#39;, 1), (&#39;third&#39;, 3), (&#39;second&#39;, 5)]</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/collections.html#collections.OrderedDict.popitem" title="collections.OrderedDict.popitem"><tt class="xref py py-meth docutils literal"><span class="pre">popitem()</span></tt></a> method has an optional <em>last</em>
argument that defaults to True.  If <em>last</em> is True, the most recently
added key is returned and removed; if it&#8217;s False, the
oldest key is selected:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">od</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
<span class="go">(19, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
<span class="go">(18, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">(0, 0)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="go">(1, 0)</span>
</pre></div>
</div>
<p>Comparing two ordered dictionaries checks both the keys and values,
and requires that the insertion order was the same:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>                   <span class="p">(</span><span class="s">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span>                   <span class="p">(</span><span class="s">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od2</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
<span class="gp">... </span>                   <span class="p">(</span><span class="s">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="gp">... </span>                   <span class="p">(</span><span class="s">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Move &#39;third&#39; key to the end</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">od2</span><span class="p">[</span><span class="s">&#39;third&#39;</span><span class="p">];</span> <span class="n">od2</span><span class="p">[</span><span class="s">&#39;third&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Comparing an <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> with a regular dictionary
ignores the insertion order and just compares the keys and values.</p>
<p>How does 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">OrderedDict</span></tt></a> work?  It maintains a
doubly-linked list of keys, appending new keys to the list as they&#8217;re inserted.
A secondary dictionary maps keys to their corresponding list node, so
deletion doesn&#8217;t have to traverse the entire linked list and therefore
remains O(1).</p>
<p>The standard library now supports use of ordered dictionaries in several
modules.</p>
<ul class="simple">
<li>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 uses them by default, meaning that
configuration files can now be read, modified, and then written back
in their original order.</li>
<li>The <a class="reference internal" href="../library/collections.html#collections.somenamedtuple._asdict" title="collections.somenamedtuple._asdict"><tt class="xref py py-meth docutils literal"><span class="pre">_asdict()</span></tt></a> method for
<a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></tt></a> now returns an ordered dictionary with the
values appearing in the same order as the underlying tuple indices.</li>
<li>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> module&#8217;s <a class="reference internal" href="../library/json.html#json.JSONDecoder" title="json.JSONDecoder"><tt class="xref py py-class docutils literal"><span class="pre">JSONDecoder</span></tt></a> class
constructor was extended with an <em>object_pairs_hook</em> parameter to
allow <tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt> instances to be built by the decoder.
Support was also added for third-party tools like
<a class="reference external" href="http://pyyaml.org/">PyYAML</a>.</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-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0372"><strong>PEP 372</strong></a> - Adding an ordered dictionary to collections</dt>
<dd>PEP written by Armin Ronacher and Raymond Hettinger;
implemented by Raymond Hettinger.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-378-format-specifier-for-thousands-separator">
<span id="pep-0378"></span><h2>PEP 378: Format Specifier for Thousands Separator<a class="headerlink" href="#pep-378-format-specifier-for-thousands-separator" title="Permalink to this headline">¶</a></h2>
<p>To make program output more readable, it can be useful to add
separators to large numbers, rendering them as
18,446,744,073,709,551,616 instead of 18446744073709551616.</p>
<p>The fully general solution for doing this is the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><tt class="xref py py-mod docutils literal"><span class="pre">locale</span></tt></a> module,
which can use different separators (&#8221;,&#8221; in North America, &#8221;.&#8221; in
Europe) and different grouping sizes, but <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><tt class="xref py py-mod docutils literal"><span class="pre">locale</span></tt></a> is complicated
to use and unsuitable for multi-threaded applications where different
threads are producing output for different locales.</p>
<p>Therefore, a simple comma-grouping mechanism has been added to the
mini-language used by the <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.  When
formatting a floating-point number, simply include a comma between the
width and the precision:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{:20,.2f}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">18446744073709551616.0</span><span class="p">)</span>
<span class="go">&#39;18,446,744,073,709,551,616.00&#39;</span>
</pre></div>
</div>
<p>When formatting an integer, include the comma after the width:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{:20,d}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">18446744073709551616</span><span class="p">)</span>
<span class="go">&#39;18,446,744,073,709,551,616&#39;</span>
</pre></div>
</div>
<p>This mechanism is not adaptable at all; commas are always used as the
separator and the grouping is always into three-digit groups.  The
comma-formatting mechanism isn&#8217;t as general as the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><tt class="xref py py-mod docutils literal"><span class="pre">locale</span></tt></a>
module, but it&#8217;s easier to use.</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-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a> - Format Specifier for Thousands Separator</dt>
<dd>PEP written by Raymond Hettinger; implemented by Eric Smith.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-389-the-argparse-module-for-parsing-command-lines">
<h2>PEP 389: The argparse Module for Parsing Command Lines<a class="headerlink" href="#pep-389-the-argparse-module-for-parsing-command-lines" title="Permalink to this headline">¶</a></h2>
<p>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 for parsing command-line arguments was
added as a more powerful replacement for the
<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> module.</p>
<p>This means Python now supports three different modules for parsing
command-line arguments: <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><tt class="xref py py-mod docutils literal"><span class="pre">getopt</span></tt></a>, <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>, and
<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>.  The <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><tt class="xref py py-mod docutils literal"><span class="pre">getopt</span></tt></a> module closely resembles the C
library&#8217;s <tt class="xref c c-func docutils literal"><span class="pre">getopt()</span></tt> function, so it remains useful if you&#8217;re writing a
Python prototype that will eventually be rewritten in C.
<a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">optparse</span></tt></a> becomes redundant, but there are no plans to remove it
because there are many scripts still using it, and there&#8217;s no
automated way to update these scripts.  (Making the <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><tt class="xref py py-mod docutils literal"><span class="pre">argparse</span></tt></a>
API consistent with <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>&#8216;s interface was discussed but
rejected as too messy and difficult.)</p>
<p>In short, if you&#8217;re writing a new script and don&#8217;t need to worry
about compatibility with earlier versions of Python, use
<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> instead 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>.</p>
<p>Here&#8217;s an example:</p>
<div class="highlight-python"><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;Command-line example.&#39;</span><span class="p">)</span>

<span class="c"># Add optional switches</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-v&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;is_verbose&#39;</span><span class="p">,</span>
                    <span class="n">help</span><span class="o">=</span><span class="s">&#39;produce verbose output&#39;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-o&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;output&#39;</span><span class="p">,</span>
                    <span class="n">metavar</span><span class="o">=</span><span class="s">&#39;FILE&#39;</span><span class="p">,</span>
                    <span class="n">help</span><span class="o">=</span><span class="s">&#39;direct output to FILE instead of stdout&#39;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s">&#39;-C&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;context&#39;</span><span class="p">,</span>
                    <span class="n">metavar</span><span class="o">=</span><span class="s">&#39;NUM&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
                    <span class="n">help</span><span class="o">=</span><span class="s">&#39;display NUM lines of added context&#39;</span><span class="p">)</span>

<span class="c"># Allow any number of additional arguments.</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="n">nargs</span><span class="o">=</span><span class="s">&#39;*&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;inputs&#39;</span><span class="p">,</span>
                    <span class="n">help</span><span class="o">=</span><span class="s">&#39;input filenames (default is stdin)&#39;</span><span class="p">)</span>

<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="k">print</span> <span class="n">args</span><span class="o">.</span><span class="n">__dict__</span>
</pre></div>
</div>
<p>Unless you override it, <a class="reference internal" href="../using/cmdline.html#cmdoption-h"><em class="xref std std-option">-h</em></a> and <a class="reference internal" href="../using/cmdline.html#cmdoption--help"><em class="xref std std-option">--help</em></a> switches
are automatically added, and produce neatly formatted output:</p>
<div class="highlight-python"><pre>-&gt; ./python.exe argparse-example.py --help
usage: argparse-example.py [-h] [-v] [-o FILE] [-C NUM] [inputs [inputs ...]]

Command-line example.

positional arguments:
  inputs      input filenames (default is stdin)

optional arguments:
  -h, --help  show this help message and exit
  -v          produce verbose output
  -o FILE     direct output to FILE instead of stdout
  -C NUM      display NUM lines of added context</pre>
</div>
<p>As with <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>, the command-line switches and arguments
are returned as an object with attributes named by the <em>dest</em> parameters:</p>
<div class="highlight-python"><pre>-&gt; ./python.exe argparse-example.py -v
{'output': None,
 'is_verbose': True,
 'context': 0,
 'inputs': []}

-&gt; ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2
{'output': '/tmp/output',
 'is_verbose': True,
 'context': 4,
 'inputs': ['file1', 'file2']}</pre>
</div>
<p><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> has much fancier validation than <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>; you
can specify an exact number of arguments as an integer, 0 or more
arguments by passing <tt class="docutils literal"><span class="pre">'*'</span></tt>, 1 or more by passing <tt class="docutils literal"><span class="pre">'+'</span></tt>, or an
optional argument with <tt class="docutils literal"><span class="pre">'?'</span></tt>.  A top-level parser can contain
sub-parsers to define subcommands that have different sets of
switches, as in <tt class="docutils literal"><span class="pre">svn</span> <span class="pre">commit</span></tt>, <tt class="docutils literal"><span class="pre">svn</span> <span class="pre">checkout</span></tt>, etc.  You can
specify an argument&#8217;s type as <a class="reference internal" href="../library/argparse.html#argparse.FileType" title="argparse.FileType"><tt class="xref py py-class docutils literal"><span class="pre">FileType</span></tt></a>, which will
automatically open files for you and understands that <tt class="docutils literal"><span class="pre">'-'</span></tt> means
standard input or output.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><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> documentation</dt>
<dd>The documentation page of the argparse module.</dd>
<dt><a class="reference internal" href="../library/argparse.html#argparse-from-optparse"><em>Upgrading optparse code</em></a></dt>
<dd>Part of the Python documentation, describing how to convert
code that uses <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>.</dd>
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0389"><strong>PEP 389</strong></a> - argparse - New Command Line Parsing Module</dt>
<dd>PEP written and implemented by Steven Bethard.</dd>
</dl>
</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 is very flexible; applications can define
a tree of logging subsystems, and each logger in this tree can filter
out certain messages, format them differently, and direct messages to
a varying number of handlers.</p>
<p>All this flexibility can require a lot of configuration.  You can
write Python statements to create objects and set their properties,
but a complex set-up requires verbose but boring code.
<a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> also supports a <tt class="xref py py-func docutils literal"><span class="pre">fileConfig()</span></tt>
function that parses a file, but the file format doesn&#8217;t support
configuring filters, and it&#8217;s messier to generate programmatically.</p>
<p>Python 2.7 adds a <tt class="xref py py-func docutils literal"><span class="pre">dictConfig()</span></tt> function that
uses a dictionary to configure logging.  There are many ways to
produce a dictionary from different sources: construct one with code;
parse a file containing JSON; or use a YAML parsing library if one is
installed.  For more information see <a class="reference internal" href="../library/logging.config.html#logging-config-api"><em>Configuration functions</em></a>.</p>
<p>The following example configures two loggers, the root logger and a
logger named &#8220;network&#8221;.  Messages sent to the root logger will be
sent to the system log using the syslog protocol, and messages
to the &#8220;network&#8221; logger will be written to a <tt class="file docutils literal"><span class="pre">network.log</span></tt> file
that will be rotated once the log reaches 1MB.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">logging.config</span>

<span class="n">configdict</span> <span class="o">=</span> <span class="p">{</span>
 <span class="s">&#39;version&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>    <span class="c"># Configuration schema in use; must be 1 for now</span>
 <span class="s">&#39;formatters&#39;</span><span class="p">:</span> <span class="p">{</span>
     <span class="s">&#39;standard&#39;</span><span class="p">:</span> <span class="p">{</span>
         <span class="s">&#39;format&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s">&#39;</span><span class="si">%(asctime)s</span><span class="s"> </span><span class="si">%(name)-15s</span><span class="s"> &#39;</span>
                    <span class="s">&#39;</span><span class="si">%(levelname)-8s</span><span class="s"> </span><span class="si">%(message)s</span><span class="s">&#39;</span><span class="p">)}},</span>

 <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;netlog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;backupCount&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
                     <span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&#39;logging.handlers.RotatingFileHandler&#39;</span><span class="p">,</span>
                     <span class="s">&#39;filename&#39;</span><span class="p">:</span> <span class="s">&#39;/logs/network.log&#39;</span><span class="p">,</span>
                     <span class="s">&#39;formatter&#39;</span><span class="p">:</span> <span class="s">&#39;standard&#39;</span><span class="p">,</span>
                     <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;INFO&#39;</span><span class="p">,</span>
                     <span class="s">&#39;maxBytes&#39;</span><span class="p">:</span> <span class="mi">1000000</span><span class="p">},</span>
              <span class="s">&#39;syslog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s">&#39;class&#39;</span><span class="p">:</span> <span class="s">&#39;logging.handlers.SysLogHandler&#39;</span><span class="p">,</span>
                         <span class="s">&#39;formatter&#39;</span><span class="p">:</span> <span class="s">&#39;standard&#39;</span><span class="p">,</span>
                         <span class="s">&#39;level&#39;</span><span class="p">:</span> <span class="s">&#39;ERROR&#39;</span><span class="p">}},</span>

 <span class="c"># Specify all the subordinate loggers</span>
 <span class="s">&#39;loggers&#39;</span><span class="p">:</span> <span class="p">{</span>
             <span class="s">&#39;network&#39;</span><span class="p">:</span> <span class="p">{</span>
                         <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;netlog&#39;</span><span class="p">]</span>
             <span class="p">}</span>
 <span class="p">},</span>
 <span class="c"># Specify properties of the root logger</span>
 <span class="s">&#39;root&#39;</span><span class="p">:</span> <span class="p">{</span>
          <span class="s">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;syslog&#39;</span><span class="p">]</span>
 <span class="p">},</span>
<span class="p">}</span>

<span class="c"># Set up configuration</span>
<span class="n">logging</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">dictConfig</span><span class="p">(</span><span class="n">configdict</span><span class="p">)</span>

<span class="c"># As an example, log two error messages</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&#39;Database not found&#39;</span><span class="p">)</span>

<span class="n">netlogger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;network&#39;</span><span class="p">)</span>
<span class="n">netlogger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&#39;Connection failed&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Three smaller enhancements to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> module, all
implemented by Vinay Sajip, are:</p>
<ul class="simple">
<li>The <a class="reference internal" href="../library/logging.handlers.html#logging.handlers.SysLogHandler" title="logging.handlers.SysLogHandler"><tt class="xref py py-class docutils literal"><span class="pre">SysLogHandler</span></tt></a> class now supports
syslogging over TCP.  The constructor has a <em>socktype</em> parameter
giving the type of socket to use, either <a class="reference internal" href="../library/socket.html#socket.SOCK_DGRAM" title="socket.SOCK_DGRAM"><tt class="xref py py-const docutils literal"><span class="pre">socket.SOCK_DGRAM</span></tt></a>
for UDP or <a class="reference internal" href="../library/socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><tt class="xref py py-const docutils literal"><span class="pre">socket.SOCK_STREAM</span></tt></a> for TCP.  The default
protocol remains UDP.</li>
<li><a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a> instances gained a <a class="reference internal" href="../library/logging.html#logging.Logger.getChild" title="logging.Logger.getChild"><tt class="xref py py-meth docutils literal"><span class="pre">getChild()</span></tt></a>
method that retrieves a descendant logger using a relative path.
For example, once you retrieve a logger by doing <tt class="docutils literal"><span class="pre">log</span> <span class="pre">=</span> <span class="pre">getLogger('app')</span></tt>,
calling <tt class="docutils literal"><span class="pre">log.getChild('network.listen')</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">getLogger('app.network.listen')</span></tt>.</li>
<li>The <a class="reference internal" href="../library/logging.html#logging.LoggerAdapter" title="logging.LoggerAdapter"><tt class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></tt></a> class gained a
<tt class="xref py py-meth docutils literal"><span class="pre">isEnabledFor()</span></tt> method that takes a
<em>level</em> and returns whether the underlying logger would
process a message of that level of importance.</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-4"></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 and implemented by Vinay Sajip.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3106-dictionary-views">
<h2>PEP 3106: Dictionary Views<a class="headerlink" href="#pep-3106-dictionary-views" title="Permalink to this headline">¶</a></h2>
<p>The dictionary methods <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt></a>, <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt></a>, and
<a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt></a> are different in Python 3.x.  They return an object
called a <em class="dfn">view</em> instead of a fully materialized list.</p>
<p>It&#8217;s not possible to change the return values of <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt></a>,
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt></a> in Python 2.7 because
too much code would break.  Instead the 3.x versions were added
under the new names <a class="reference internal" href="../library/stdtypes.html#dict.viewkeys" title="dict.viewkeys"><tt class="xref py py-meth docutils literal"><span class="pre">viewkeys()</span></tt></a>, <a class="reference internal" href="../library/stdtypes.html#dict.viewvalues" title="dict.viewvalues"><tt class="xref py py-meth docutils literal"><span class="pre">viewvalues()</span></tt></a>,
and <a class="reference internal" href="../library/stdtypes.html#dict.viewitems" title="dict.viewitems"><tt class="xref py py-meth docutils literal"><span class="pre">viewitems()</span></tt></a>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
<span class="go">{0: &#39;A&#39;, 130: &#39;N&#39;, 10: &#39;B&#39;, 140: &#39;O&#39;, 20: ..., 250: &#39;Z&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="go">dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])</span>
</pre></div>
</div>
<p>Views can be iterated over, but the key and item views also behave
like sets.  The <tt class="docutils literal"><span class="pre">&amp;</span></tt> operator performs intersection, and <tt class="docutils literal"><span class="pre">|</span></tt>
performs a union:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d2</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">**.</span><span class="mi">5</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">&amp;</span> <span class="n">d2</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="go">set([0.0, 10.0, 20.0, 30.0])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">|</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
<span class="go">set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])</span>
</pre></div>
</div>
<p>The view keeps track of the dictionary and its contents change as the
dictionary is modified:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
<span class="go">dict_keys([0, 130, 10, ..., 250])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="mi">260</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;&amp;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
<span class="go">dict_keys([0, 130, 260, 10, ..., 250])</span>
</pre></div>
</div>
<p>However, note that you can&#8217;t add or remove keys while you&#8217;re iterating
over the view:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">vk</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">k</span>
<span class="gp">...</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>: <span class="n">dictionary changed size during iteration</span>
</pre></div>
</div>
<p>You can use the view methods in Python 2.x code, and the 2to3
converter will change them to the standard <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt></a>,
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt></a> methods.</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-5"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3106"><strong>PEP 3106</strong></a> - Revamping dict.keys(), .values() and .items()</dt>
<dd>PEP written by Guido van Rossum.
Backported to 2.7 by Alexandre Vassalotti; <a class="reference external" href="http://bugs.python.org/issue1967">issue 1967</a>.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3137-the-memoryview-object">
<h2>PEP 3137: The memoryview Object<a class="headerlink" href="#pep-3137-the-memoryview-object" title="Permalink to this headline">¶</a></h2>
<p>The <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> object provides a view of another object&#8217;s
memory content that matches the <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> type&#8217;s interface.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">memoryview</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
<span class="go">&lt;memory at 0x37f850&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>           <span class="c"># Returns length of underlying object</span>
<span class="go">52</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">25</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">26</span><span class="p">]</span>   <span class="c"># Indexing returns one byte</span>
<span class="go">(&#39;a&#39;, &#39;z&#39;, &#39;A&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span> <span class="o">=</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">26</span><span class="p">]</span>         <span class="c"># Slicing returns another memoryview</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span>
<span class="go">&lt;memory at 0x37f080&gt;</span>
</pre></div>
</div>
<p>The content of the view can be converted to a string of bytes or
a list of integers:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
<span class="go">&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="go">[97, 98, 99, 100, 101, 102, 103, ... 121, 122]</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p><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 allow modifying the underlying object if
it&#8217;s a mutable object.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">75</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">cannot modify read-only memory</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span>  <span class="c"># Creating a mutable object</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">bytearray(b&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span> <span class="o">=</span> <span class="n">memoryview</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;*&#39;</span>         <span class="c"># Assign to view, changing the bytearray.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>              <span class="c"># The bytearray has been changed.</span>
<span class="go">bytearray(b&#39;*bcde&#39;)</span>
<span class="go">&gt;&gt;&gt;</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-3137"><strong>PEP 3137</strong></a> - Immutable Bytes and Mutable Buffer</dt>
<dd>PEP written by Guido van Rossum.
Implemented by Travis Oliphant, Antoine Pitrou and others.
Backported to 2.7 by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue2396">issue 2396</a>.</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">The syntax for set literals has been backported from Python 3.x.
Curly brackets are used to surround the contents of the resulting
mutable set; set literals are
distinguished from dictionaries by not containing colons and values.
<tt class="docutils literal"><span class="pre">{}</span></tt> continues to represent an empty dictionary; use
<tt class="docutils literal"><span class="pre">set()</span></tt> for an empty set.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span>
<span class="go">set([1, 2, 3, 4, 5])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">set</span><span class="p">()</span> <span class="c"># empty set</span>
<span class="go">set([])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{}</span>    <span class="c"># empty dict</span>
<span class="go">{}</span>
</pre></div>
</div>
<p>Backported by Alexandre Vassalotti; <a class="reference external" href="http://bugs.python.org/issue2335">issue 2335</a>.</p>
</li>
<li><p class="first">Dictionary and set comprehensions are another feature backported from
3.x, generalizing list/generator comprehensions to use
the literal syntax for sets and dictionaries.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
<span class="go">{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">{(</span><span class="s">&#39;a&#39;</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
<span class="go">set([&#39;&#39;, &#39;a&#39;, &#39;aa&#39;, &#39;aaa&#39;, &#39;aaaa&#39;, &#39;aaaaa&#39;])</span>
</pre></div>
</div>
<p>Backported by Alexandre Vassalotti; <a class="reference external" href="http://bugs.python.org/issue2333">issue 2333</a>.</p>
</li>
<li><p class="first">The <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 can now use multiple context managers
in one statement.  Context managers are processed from left to right
and each one is treated as beginning a new <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.
This means that:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">,</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
    <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
</pre></div>
</div>
<p>is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">:</span>
    <span class="k">with</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
        <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/contextlib.html#contextlib.nested" title="contextlib.nested"><tt class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></tt></a> function provides a very similar
function, so it&#8217;s no longer necessary and has been deprecated.</p>
<p>(Proposed in <a class="reference external" href="http://codereview.appspot.com/53094">http://codereview.appspot.com/53094</a>; implemented by
Georg Brandl.)</p>
</li>
<li><p class="first">Conversions between floating-point numbers and strings are
now correctly rounded on most platforms.  These conversions occur
in many different places: <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> on
floats and complex numbers; the <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#complex" title="complex"><tt class="xref py py-class docutils literal"><span class="pre">complex</span></tt></a>
constructors;
numeric formatting; serializing and
deserializing floats and complex numbers using the
<a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><tt class="xref py py-mod docutils literal"><span class="pre">marshal</span></tt></a>, <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>
and <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> modules;
parsing of float and imaginary literals in Python code;
and <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>-to-float conversion.</p>
<p>Related to this, the <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> of a floating-point number <em>x</em>
now returns a result based on the shortest decimal string that&#8217;s
guaranteed to round back to <em>x</em> under correct rounding (with
round-half-to-even rounding mode).  Previously it gave a string
based on rounding x to 17 decimal digits.</p>
<p>The rounding library responsible for this improvement works on
Windows and on Unix platforms using the gcc, icc, or suncc
compilers.  There may be a small number of platforms where correct
operation of this code cannot be guaranteed, so the code is not
used on such systems.  You can find out which code is being used
by checking <a class="reference internal" href="../library/sys.html#sys.float_repr_style" title="sys.float_repr_style"><tt class="xref py py-data docutils literal"><span class="pre">sys.float_repr_style</span></tt></a>,  which will be <tt class="docutils literal"><span class="pre">short</span></tt>
if the new code is in use and <tt class="docutils literal"><span class="pre">legacy</span></tt> if it isn&#8217;t.</p>
<p>Implemented by Eric Smith and Mark Dickinson, using David Gay&#8217;s
<tt class="file docutils literal"><span class="pre">dtoa.c</span></tt> library; <a class="reference external" href="http://bugs.python.org/issue7117">issue 7117</a>.</p>
</li>
<li><p class="first">Conversions from long integers and regular integers to floating
point now round differently, returning the floating-point number
closest to the number.  This doesn&#8217;t matter for small integers that
can be converted exactly, but for large numbers that will
unavoidably lose precision, Python 2.7 now approximates more
closely.  For example, Python 2.6 computed the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">2.9514790517935283e+20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="nb">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
<span class="go">65535L</span>
</pre></div>
</div>
<p>Python 2.7&#8217;s floating-point result is larger, but much closer to the
true value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">2.9514790517935289e+20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="nb">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
<span class="go">-1L</span>
</pre></div>
</div>
<p>(Implemented by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue3166">issue 3166</a>.)</p>
<p>Integer division is also more accurate in its rounding behaviours.  (Also
implemented by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue1811">issue 1811</a>.)</p>
</li>
<li><p class="first">Implicit coercion for complex numbers has been removed; the interpreter
will no longer ever attempt to call a <a class="reference internal" href="../reference/datamodel.html#object.__coerce__" title="object.__coerce__"><tt class="xref py py-meth docutils literal"><span class="pre">__coerce__()</span></tt></a> method on complex
objects.  (Removed by Meador Inge and Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue5211">issue 5211</a>.)</p>
</li>
<li><p class="first">The <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 now supports automatic numbering of the replacement
fields.  This makes using <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> more closely resemble using
<tt class="docutils literal"><span class="pre">%s</span></tt> formatting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{}:{}:{}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mo">04</span><span class="p">,</span> <span class="s">&#39;Sunday&#39;</span><span class="p">)</span>
<span class="go">&#39;2009:4:Sunday&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{}:{}:{day}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s">&#39;Sunday&#39;</span><span class="p">)</span>
<span class="go">&#39;2009:4:Sunday&#39;</span>
</pre></div>
</div>
<p>The auto-numbering takes the fields from left to right, so the first <tt class="docutils literal"><span class="pre">{...}</span></tt>
specifier will use the first argument to <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>, the next
specifier will use the next argument, and so on.  You can&#8217;t mix auto-numbering
and explicit numbering &#8211; either number all of your specifier fields or none
of them &#8211; but you can mix auto-numbering and named fields, as in the second
example above.  (Contributed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue5237">issue 5237</a>.)</p>
<p>Complex numbers now correctly support usage with <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 default to being right-aligned.
Specifying a precision or comma-separation applies to both the real
and imaginary parts of the number, but a specified field width and
alignment is applied to the whole of the resulting <tt class="docutils literal"><span class="pre">1.5+3j</span></tt>
output.  (Contributed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue1588">issue 1588</a> and <a class="reference external" href="http://bugs.python.org/issue7988">issue 7988</a>.)</p>
<p>The &#8216;F&#8217; format code now always formats its output using uppercase characters,
so it will now produce &#8216;INF&#8217; and &#8216;NAN&#8217;.
(Contributed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue3382">issue 3382</a>.)</p>
<p>A low-level change: the <tt class="xref py py-meth docutils literal"><span class="pre">object.__format__()</span></tt> method now triggers
a <a class="reference internal" href="../library/exceptions.html#exceptions.PendingDeprecationWarning" title="exceptions.PendingDeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></tt></a> if it&#8217;s passed a format string,
because the <tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt> method for <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> converts
the object to a string representation and formats that.  Previously
the method silently applied the format string to the string
representation, but that could hide mistakes in Python code.  If
you&#8217;re supplying formatting information such as an alignment or
precision, presumably you&#8217;re expecting the formatting to be applied
in some object-specific way.  (Fixed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue7994">issue 7994</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> and <a class="reference internal" href="../library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> types gained a <tt class="docutils literal"><span class="pre">bit_length</span></tt>
method that returns the number of bits necessary to represent
its argument in binary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">37</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">&#39;0b100101&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">123</span><span class="o">-</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">123</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">bit_length</span><span class="p">()</span>
<span class="go">124</span>
</pre></div>
</div>
<p>(Contributed by Fredrik Johansson and Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue3439">issue 3439</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement will no longer try an absolute import
if a relative import (e.g. <tt class="docutils literal"><span class="pre">from</span> <span class="pre">.os</span> <span class="pre">import</span> <span class="pre">sep</span></tt>) fails.  This
fixes a bug, but could possibly break certain <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a>
statements that were only working by accident.  (Fixed by Meador Inge;
<a class="reference external" href="http://bugs.python.org/issue7902">issue 7902</a>.)</p>
</li>
<li><p class="first">It&#8217;s now possible for a subclass of the built-in <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><tt class="xref py py-class docutils literal"><span class="pre">unicode</span></tt></a> type
to override the <a class="reference internal" href="../reference/datamodel.html#object.__unicode__" title="object.__unicode__"><tt class="xref py py-meth docutils literal"><span class="pre">__unicode__()</span></tt></a> method.  (Implemented by
Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue1583863">issue 1583863</a>.)</p>
</li>
<li><p class="first">The <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> type&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">translate()</span></tt> method now accepts
<tt class="docutils literal"><span class="pre">None</span></tt> as its first argument.  (Fixed by Georg Brandl;
<a class="reference external" href="http://bugs.python.org/issue4759">issue 4759</a>.)</p>
</li>
<li><p class="first">When using <tt class="docutils literal"><span class="pre">&#64;classmethod</span></tt> and <tt class="docutils literal"><span class="pre">&#64;staticmethod</span></tt> to wrap
methods as class or static methods, the wrapper object now
exposes the wrapped function as their <tt class="xref py py-attr docutils literal"><span class="pre">__func__</span></tt> attribute.
(Contributed by Amaury Forgeot d&#8217;Arc, after a suggestion by
George Sakkis; <a class="reference external" href="http://bugs.python.org/issue5982">issue 5982</a>.)</p>
</li>
<li><p class="first">When a restricted set of attributes were set using <tt class="docutils literal"><span class="pre">__slots__</span></tt>,
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a>
as you would expect.  Fixed by Benjamin Peterson; <a class="reference external" href="http://bugs.python.org/issue7604">issue 7604</a>.)</p>
</li>
<li><p class="first">Two new encodings are now supported: &#8220;cp720&#8221;, used primarily for
Arabic text; and &#8220;cp858&#8221;, a variant of CP 850 that adds the euro
symbol.  (CP720 contributed by Alexander Belchenko and Amaury
Forgeot d&#8217;Arc in <a class="reference external" href="http://bugs.python.org/issue1616979">issue 1616979</a>; CP858 contributed by Tim Hatch in
<a class="reference external" href="http://bugs.python.org/issue8016">issue 8016</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#file" title="file"><tt class="xref py py-class docutils literal"><span class="pre">file</span></tt></a> object will now set the <tt class="xref py py-attr docutils literal"><span class="pre">filename</span></tt> attribute
on the <a class="reference internal" href="../library/exceptions.html#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> exception when trying to open a directory
on POSIX platforms (noted by Jan Kaliszewski; <a class="reference external" href="http://bugs.python.org/issue4764">issue 4764</a>), and
now explicitly checks for and forbids writing to read-only file objects
instead of trusting the C library to catch and report the error
(fixed by Stefan Krah; <a class="reference external" href="http://bugs.python.org/issue5677">issue 5677</a>).</p>
</li>
<li><p class="first">The Python tokenizer now translates line endings itself, so the
<a class="reference internal" href="../library/functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a> built-in function now accepts code using any
line-ending convention.  Additionally, it no longer requires that the
code end in a newline.</p>
</li>
<li><p class="first">Extra parentheses in function definitions are illegal in Python 3.x,
meaning that you get a syntax error from <tt class="docutils literal"><span class="pre">def</span> <span class="pre">f((x)):</span> <span class="pre">pass</span></tt>.  In
Python3-warning mode, Python 2.7 will now warn about this odd usage.
(Noted by James Lingard; <a class="reference external" href="http://bugs.python.org/issue7362">issue 7362</a>.)</p>
</li>
<li><p class="first">It&#8217;s now possible to create weak references to old-style class
objects.  New-style classes were always weak-referenceable.  (Fixed
by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue8268">issue 8268</a>.)</p>
</li>
<li><p class="first">When a module object is garbage-collected, the module&#8217;s dictionary is
now only cleared if no one else is holding a reference to the
dictionary (<a class="reference external" href="http://bugs.python.org/issue7140">issue 7140</a>).</p>
</li>
</ul>
<div class="section" id="interpreter-changes">
<span id="new-27-interpreter"></span><h3>Interpreter Changes<a class="headerlink" href="#interpreter-changes" title="Permalink to this headline">¶</a></h3>
<p>A new environment variable, <span class="target" id="index-7"></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>,
allows controlling warnings.  It should be set to a string
containing warning settings, equivalent to those
used with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><em class="xref std std-option">-W</em></a> switch, separated by commas.
(Contributed by Brian Curtin; <a class="reference external" href="http://bugs.python.org/issue7301">issue 7301</a>.)</p>
<p>For example, the following setting will print warnings every time
they occur, but turn warnings from the <a class="reference internal" href="../library/cookie.html#module-Cookie" title="Cookie: Support for HTTP state management (cookies)."><tt class="xref py py-mod docutils literal"><span class="pre">Cookie</span></tt></a> module into an
error.  (The exact syntax for setting an environment variable varies
across operating systems and shells.)</p>
<div class="highlight-python"><pre>export PYTHONWARNINGS=all,error:::Cookie:0</pre>
</div>
</div>
<div class="section" id="optimizations">
<h3>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h3>
<p>Several performance enhancements have been added:</p>
<ul>
<li><p class="first">A new opcode was added to perform the initial setup for
<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, looking up the <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> and
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> methods.  (Contributed by Benjamin Peterson.)</p>
</li>
<li><p class="first">The garbage collector now performs better for one common usage
pattern: when many objects are being allocated without deallocating
any of them.  This would previously take quadratic
time for garbage collection, but now the number of full garbage collections
is reduced as the number of objects on the heap grows.
The new logic only performs a full garbage collection pass when
the middle generation has been collected 10 times and when the
number of survivor objects from the middle generation exceeds 10% of
the number of objects in the oldest generation.  (Suggested by Martin
von Löwis and implemented by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue4074">issue 4074</a>.)</p>
</li>
<li><p class="first">The garbage collector tries to avoid tracking simple containers
which can&#8217;t be part of a cycle. In Python 2.7, this is now true for
tuples and dicts containing atomic types (such as ints, strings,
etc.). Transitively, a dict containing tuples of atomic types won&#8217;t
be tracked either. This helps reduce the cost of each
garbage collection by decreasing the number of objects to be
considered and traversed by the collector.
(Contributed by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue4688">issue 4688</a>.)</p>
</li>
<li><p class="first">Long integers are now stored internally either in base 2**15 or in base
2**30, the base being determined at build time.  Previously, they
were always stored in base 2**15.  Using base 2**30 gives
significant performance improvements on 64-bit machines, but
benchmark results on 32-bit machines have been mixed.  Therefore,
the default is to use base 2**30 on 64-bit machines and base 2**15
on 32-bit machines; on Unix, there&#8217;s a new configure option
<em class="xref std std-option">--enable-big-digits</em> that can be used to override this default.</p>
<p>Apart from the performance improvements this change should be
invisible to end users, with one exception: for testing and
debugging purposes there&#8217;s a new structseq <a class="reference internal" href="../library/sys.html#sys.long_info" title="sys.long_info"><tt class="xref py py-data docutils literal"><span class="pre">sys.long_info</span></tt></a> that
provides information about the internal format, giving the number of
bits per digit and the size in bytes of the C type used to store
each digit:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">long_info</span>
<span class="go">sys.long_info(bits_per_digit=30, sizeof_digit=4)</span>
</pre></div>
</div>
<p>(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue4258">issue 4258</a>.)</p>
<p>Another set of changes made long objects a few bytes smaller: 2 bytes
smaller on 32-bit systems and 6 bytes on 64-bit.
(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue5260">issue 5260</a>.)</p>
</li>
<li><p class="first">The division algorithm for long integers has been made faster
by tightening the inner loop, doing shifts instead of multiplications,
and fixing an unnecessary extra iteration.
Various benchmarks show speedups of between 50% and 150% for long
integer divisions and modulo operations.
(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue5512">issue 5512</a>.)
Bitwise operations are also significantly faster (initial patch by
Gregory Smith; <a class="reference external" href="http://bugs.python.org/issue1087418">issue 1087418</a>).</p>
</li>
<li><p class="first">The implementation of <tt class="docutils literal"><span class="pre">%</span></tt> checks for the left-side operand being
a Python string and special-cases it; this results in a 1-3%
performance increase for applications that frequently use <tt class="docutils literal"><span class="pre">%</span></tt>
with strings, such as templating libraries.
(Implemented by Collin Winter; <a class="reference external" href="http://bugs.python.org/issue5176">issue 5176</a>.)</p>
</li>
<li><p class="first">List comprehensions with an <tt class="docutils literal"><span class="pre">if</span></tt> condition are compiled into
faster bytecode.  (Patch by Antoine Pitrou, back-ported to 2.7
by Jeffrey Yasskin; <a class="reference external" href="http://bugs.python.org/issue4715">issue 4715</a>.)</p>
</li>
<li><p class="first">Converting an integer or long integer to a decimal string was made
faster by special-casing base 10 instead of using a generalized
conversion function that supports arbitrary bases.
(Patch by Gawain Bolton; <a class="reference external" href="http://bugs.python.org/issue6713">issue 6713</a>.)</p>
</li>
<li><p class="first">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">replace()</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">rpartition()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">rsplit()</span></tt> methods of string-like types
(strings, Unicode strings, 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> objects) now use a
fast reverse-search algorithm instead of a character-by-character
scan.  This is sometimes faster by a factor of 10.  (Added by
Florent Xicluna; <a class="reference external" href="http://bugs.python.org/issue7462">issue 7462</a> and <a class="reference external" href="http://bugs.python.org/issue7622">issue 7622</a>.)</p>
</li>
<li><p class="first">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> and <a class="reference internal" href="../library/pickle.html#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> modules now automatically
intern the strings used for attribute names, reducing memory usage
of the objects resulting from unpickling.  (Contributed by Jake
McGuire; <a class="reference external" href="http://bugs.python.org/issue5084">issue 5084</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/pickle.html#module-cPickle" title="cPickle: Faster version of pickle, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cPickle</span></tt></a> module now special-cases dictionaries,
nearly halving the time required to pickle them.
(Contributed by Collin Winter; <a class="reference external" href="http://bugs.python.org/issue5670">issue 5670</a>.)</p>
</li>
</ul>
</div>
</div>
<div class="section" id="new-and-improved-modules">
<h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Permalink to this headline">¶</a></h2>
<p>As in every release, Python&#8217;s standard library received a number of
enhancements and bug fixes.  Here&#8217;s a partial list of the most notable
changes, sorted alphabetically by module name. Consult the
<tt class="file docutils literal"><span class="pre">Misc/NEWS</span></tt> file in the source tree for a more complete list of
changes, or look through the Subversion logs for all the details.</p>
<ul>
<li><p class="first">The <a class="reference internal" href="../library/bdb.html#module-bdb" title="bdb: Debugger framework."><tt class="xref py py-mod docutils literal"><span class="pre">bdb</span></tt></a> module&#8217;s base debugging class <a class="reference internal" href="../library/bdb.html#bdb.Bdb" title="bdb.Bdb"><tt class="xref py py-class docutils literal"><span class="pre">Bdb</span></tt></a>
gained a feature for skipping modules.  The constructor
now takes an iterable containing glob-style patterns such as
<tt class="docutils literal"><span class="pre">django.*</span></tt>; the debugger will not step into stack frames
from a module that matches one of these patterns.
(Contributed by Maru Newby after a suggestion by
Senthil Kumaran; <a class="reference external" href="http://bugs.python.org/issue5142">issue 5142</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><tt class="xref py py-mod docutils literal"><span class="pre">binascii</span></tt></a> module now supports the buffer API, so it can be
used with <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> instances and other similar buffer objects.
(Backported from 3.x by Florent Xicluna; <a class="reference external" href="http://bugs.python.org/issue7703">issue 7703</a>.)</p>
</li>
<li><p class="first">Updated module: the <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><tt class="xref py py-mod docutils literal"><span class="pre">bsddb</span></tt></a> module has been updated from 4.7.2devel9
to version 4.8.4 of
<a class="reference external" href="http://www.jcea.es/programacion/pybsddb.htm">the pybsddb package</a>.
The new version features better Python 3.x compatibility, various bug fixes,
and adds several new BerkeleyDB flags and methods.
(Updated by Jesús Cea Avión; <a class="reference external" href="http://bugs.python.org/issue8156">issue 8156</a>.  The pybsddb
changelog can be read at <a class="reference external" href="http://hg.jcea.es/pybsddb/file/tip/ChangeLog">http://hg.jcea.es/pybsddb/file/tip/ChangeLog</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interface to compression and decompression routines compatible with bzip2."><tt class="xref py py-mod docutils literal"><span class="pre">bz2</span></tt></a> module&#8217;s <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> now supports the context
management protocol, so you can write <tt class="docutils literal"><span class="pre">with</span> <span class="pre">bz2.BZ2File(...)</span> <span class="pre">as</span> <span class="pre">f:</span></tt>.
(Contributed by Hagen Fürstenau; <a class="reference external" href="http://bugs.python.org/issue3860">issue 3860</a>.)</p>
</li>
<li><p class="first">New class: 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">Counter</span></tt></a> class in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a>
module is useful for tallying data.  <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> instances
behave mostly like dictionaries but return zero for missing keys instead of
raising a <a class="reference internal" href="../library/exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="s">&#39;here is a sample of english text&#39;</span><span class="p">:</span>
<span class="gp">... </span>  <span class="n">c</span><span class="p">[</span><span class="n">letter</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
<span class="go">Counter({&#39; &#39;: 6, &#39;e&#39;: 5, &#39;s&#39;: 3, &#39;a&#39;: 2, &#39;i&#39;: 2, &#39;h&#39;: 2,</span>
<span class="go">&#39;l&#39;: 2, &#39;t&#39;: 2, &#39;g&#39;: 1, &#39;f&#39;: 1, &#39;m&#39;: 1, &#39;o&#39;: 1, &#39;n&#39;: 1,</span>
<span class="go">&#39;p&#39;: 1, &#39;r&#39;: 1, &#39;x&#39;: 1})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;e&#39;</span><span class="p">]</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;z&#39;</span><span class="p">]</span>
<span class="go">0</span>
</pre></div>
</div>
<p>There are three additional <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt></a> methods.
<a class="reference internal" href="../library/collections.html#collections.Counter.most_common" title="collections.Counter.most_common"><tt class="xref py py-meth docutils literal"><span class="pre">most_common()</span></tt></a> returns the N most common
elements and their counts.  <a class="reference internal" href="../library/collections.html#collections.Counter.elements" title="collections.Counter.elements"><tt class="xref py py-meth docutils literal"><span class="pre">elements()</span></tt></a>
returns an iterator over the contained elements, repeating each
element as many times as its count.
<a class="reference internal" href="../library/collections.html#collections.Counter.subtract" title="collections.Counter.subtract"><tt class="xref py py-meth docutils literal"><span class="pre">subtract()</span></tt></a> takes an iterable and
subtracts one for each element instead of adding; if the argument is
a dictionary or another <tt class="xref py py-class docutils literal"><span class="pre">Counter</span></tt>, the counts are
subtracted.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="go">[(&#39; &#39;, 6), (&#39;e&#39;, 5), (&#39;s&#39;, 3), (&#39;a&#39;, 2), (&#39;i&#39;, 2)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">elements</span><span class="p">()</span> <span class="o">-&gt;</span>
<span class="go">   &#39;a&#39;, &#39;a&#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;,</span>
<span class="go">   &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;e&#39;, &#39;g&#39;, &#39;f&#39;, &#39;i&#39;, &#39;i&#39;,</span>
<span class="go">   &#39;h&#39;, &#39;h&#39;, &#39;m&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;n&#39;, &#39;p&#39;, &#39;s&#39;,</span>
<span class="go">   &#39;s&#39;, &#39;s&#39;, &#39;r&#39;, &#39;t&#39;, &#39;t&#39;, &#39;x&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;e&#39;</span><span class="p">]</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="s">&#39;very heavy on the letter e&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s">&#39;e&#39;</span><span class="p">]</span>    <span class="c"># Count is now lower</span>
<span class="go">-1</span>
</pre></div>
</div>
<p>Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1696199">issue 1696199</a>.</p>
<p>New class: <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><tt class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></tt></a> is described in the earlier
section <a class="reference internal" href="#pep-0372"><em>PEP 372: Adding an Ordered Dictionary to collections</em></a>.</p>
<p>New method: 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">deque</span></tt></a> data type now has a
<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> method that returns the number of
contained elements equal to the supplied argument <em>x</em>, and a
<a class="reference internal" href="../library/collections.html#collections.deque.reverse" title="collections.deque.reverse"><tt class="xref py py-meth docutils literal"><span class="pre">reverse()</span></tt></a> method that reverses the elements
of the deque in-place.  <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt></a> also exposes its maximum
length as the read-only <a class="reference internal" href="../library/collections.html#collections.deque.maxlen" title="collections.deque.maxlen"><tt class="xref py py-attr docutils literal"><span class="pre">maxlen</span></tt></a> attribute.
(Both features added by Raymond Hettinger.)</p>
<p>The <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><tt class="xref py py-class docutils literal"><span class="pre">namedtuple</span></tt></a> class now has an optional <em>rename</em> parameter.
If <em>rename</em> is true, field names that are invalid because they&#8217;ve
been repeated or aren&#8217;t legal Python identifiers will be
renamed to legal names that are derived from the field&#8217;s
position within the list of fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;T&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;field1&#39;</span><span class="p">,</span> <span class="s">&#39;$illegal&#39;</span><span class="p">,</span> <span class="s">&#39;for&#39;</span><span class="p">,</span> <span class="s">&#39;field2&#39;</span><span class="p">],</span> <span class="n">rename</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span><span class="o">.</span><span class="n">_fields</span>
<span class="go">(&#39;field1&#39;, &#39;_1&#39;, &#39;_2&#39;, &#39;field2&#39;)</span>
</pre></div>
</div>
<p>(Added by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1818">issue 1818</a>.)</p>
<p>Finally, the <a class="reference internal" href="../library/collections.html#collections.Mapping" title="collections.Mapping"><tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt></a> abstract base class now
returns <a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><tt class="xref py py-const docutils literal"><span class="pre">NotImplemented</span></tt></a> if a mapping is compared to
another type that isn&#8217;t a <tt class="xref py py-class docutils literal"><span class="pre">Mapping</span></tt>.
(Fixed by Daniel Stutzbach; <a class="reference external" href="http://bugs.python.org/issue8729">issue 8729</a>.)</p>
</li>
<li><p class="first">Constructors for the parsing classes in 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 now
take a <em>allow_no_value</em> parameter, defaulting to false; if true,
options without values will be allowed.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ConfigParser</span><span class="o">,</span> <span class="nn">StringIO</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sample_config</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s">[mysqld]</span>
<span class="gp">... </span><span class="s">user = mysql</span>
<span class="gp">... </span><span class="s">pid-file = /var/run/mysqld/mysqld.pid</span>
<span class="gp">... </span><span class="s">skip-bdb</span>
<span class="gp">... </span><span class="s">&quot;&quot;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">RawConfigParser</span><span class="p">(</span><span class="n">allow_no_value</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">readfp</span><span class="p">(</span><span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="n">sample_config</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s">&#39;user&#39;</span><span class="p">)</span>
<span class="go">&#39;mysql&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s">&#39;skip-bdb&#39;</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s">&#39;unknown&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">NoOptionError: No option &#39;unknown&#39; in section</span>: <span class="n">&#39;mysqld&#39;</span>
</pre></div>
</div>
<p>(Contributed by Mats Kindahl; <a class="reference external" href="http://bugs.python.org/issue7005">issue 7005</a>.)</p>
</li>
<li><p class="first">Deprecated function: <a class="reference internal" href="../library/contextlib.html#contextlib.nested" title="contextlib.nested"><tt class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></tt></a>, which allows
handling more than one context manager with a single <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, has been deprecated, because the <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
now supports multiple context managers.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/cookielib.html#module-cookielib" title="cookielib: Classes for automatic handling of HTTP cookies."><tt class="xref py py-mod docutils literal"><span class="pre">cookielib</span></tt></a> module now ignores cookies that have an invalid
version field, one that doesn&#8217;t contain an integer value.  (Fixed by
John J. Lee; <a class="reference external" href="http://bugs.python.org/issue3924">issue 3924</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-mod docutils literal"><span class="pre">copy</span></tt></a> module&#8217;s <a class="reference internal" href="../library/copy.html#copy.deepcopy" title="copy.deepcopy"><tt class="xref py py-func docutils literal"><span class="pre">deepcopy()</span></tt></a> function will now
correctly copy bound instance methods.  (Implemented by
Robert Collins; <a class="reference external" href="http://bugs.python.org/issue1515">issue 1515</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> module now always converts <tt class="docutils literal"><span class="pre">None</span></tt> to a C NULL
pointer for arguments declared as pointers.  (Changed by Thomas
Heller; <a class="reference external" href="http://bugs.python.org/issue4606">issue 4606</a>.)  The underlying <a class="reference external" href="http://sourceware.org/libffi/">libffi library</a> has been updated to version
3.0.9, containing various fixes for different platforms.  (Updated
by Matthias Klose; <a class="reference external" href="http://bugs.python.org/issue8142">issue 8142</a>.)</p>
</li>
<li><p class="first">New method: 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&#8217;s <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> class
gained a <a class="reference internal" href="../library/datetime.html#datetime.timedelta.total_seconds" title="datetime.timedelta.total_seconds"><tt class="xref py py-meth docutils literal"><span class="pre">total_seconds()</span></tt></a> method that returns the
number of seconds in the duration.  (Contributed by Brian Quinlan; <a class="reference external" href="http://bugs.python.org/issue5788">issue 5788</a>.)</p>
</li>
<li><p class="first">New method: 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</span></tt></a> class gained a
<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> class method that performs an exact
conversion of a floating-point number to a <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>.
This exact conversion strives for the
closest decimal approximation to the floating-point representation&#8217;s value;
the resulting decimal value will therefore still include the inaccuracy,
if any.
For example, <tt class="docutils literal"><span class="pre">Decimal.from_float(0.1)</span></tt> returns
<tt class="docutils literal"><span class="pre">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span></tt>.
(Implemented by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue4796">issue 4796</a>.)</p>
<p>Comparing instances of <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> with floating-point
numbers now produces sensible results based on the numeric values
of the operands.  Previously such comparisons would fall back to
Python&#8217;s default rules for comparing objects, which produced arbitrary
results based on their type.  Note that you still cannot combine
<tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> and floating-point in other operations such as addition,
since you should be explicitly choosing how to convert between float and
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a>.  (Fixed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue2531">issue 2531</a>.)</p>
<p>The constructor for <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> now accepts
floating-point numbers (added by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue8257">issue 8257</a>)
and non-European Unicode characters such as Arabic-Indic digits
(contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue6595">issue 6595</a>).</p>
<p>Most of the methods of the <a class="reference internal" href="../library/decimal.html#decimal.Context" title="decimal.Context"><tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt></a> class now accept integers
as well as <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> instances; the only exceptions are the
<a class="reference internal" href="../library/decimal.html#decimal.Context.canonical" title="decimal.Context.canonical"><tt class="xref py py-meth docutils literal"><span class="pre">canonical()</span></tt></a> and <a class="reference internal" href="../library/decimal.html#decimal.Context.is_canonical" title="decimal.Context.is_canonical"><tt class="xref py py-meth docutils literal"><span class="pre">is_canonical()</span></tt></a>
methods.  (Patch by Juan José Conti; <a class="reference external" href="http://bugs.python.org/issue7633">issue 7633</a>.)</p>
<p>When using <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> instances with a string&#8217;s
<a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> method, the default alignment was previously
left-alignment.  This has been changed to right-alignment, which is
more sensible for numeric types.  (Changed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue6857">issue 6857</a>.)</p>
<p>Comparisons involving a signaling NaN value (or <tt class="docutils literal"><span class="pre">sNAN</span></tt>) now signal
<tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt> instead of silently returning a true or
false value depending on the comparison operator.  Quiet NaN values
(or <tt class="docutils literal"><span class="pre">NaN</span></tt>) are now hashable.  (Fixed by Mark Dickinson;
<a class="reference external" href="http://bugs.python.org/issue7279">issue 7279</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> module now produces output that is more
compatible with modern <strong class="command">diff</strong>/<strong class="command">patch</strong> tools
through one small change, using a tab character instead of spaces as
a separator in the header giving the filename.  (Fixed by Anatoly
Techtonik; <a class="reference external" href="http://bugs.python.org/issue7585">issue 7585</a>.)</p>
</li>
<li><p class="first">The Distutils <tt class="docutils literal"><span class="pre">sdist</span></tt> command now always regenerates the
<tt class="file docutils literal"><span class="pre">MANIFEST</span></tt> file, since even if the <tt class="file docutils literal"><span class="pre">MANIFEST.in</span></tt> or
<tt class="file docutils literal"><span class="pre">setup.py</span></tt> files haven&#8217;t been modified, the user might have
created some new files that should be included.
(Fixed by Tarek Ziadé; <a class="reference external" href="http://bugs.python.org/issue8688">issue 8688</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><tt class="xref py py-mod docutils literal"><span class="pre">doctest</span></tt></a> module&#8217;s <tt class="xref py py-const docutils literal"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></tt> flag
will now ignore the name of the module containing the exception
being tested.  (Patch by Lennart Regebro; <a class="reference external" href="http://bugs.python.org/issue7490">issue 7490</a>.)</p>
</li>
<li><p class="first">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> module&#8217;s <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> class will
now accept a Unicode-valued payload, automatically converting the
payload to the encoding specified by <tt class="xref py py-attr docutils literal"><span class="pre">output_charset</span></tt>.
(Added by R. David Murray; <a class="reference external" href="http://bugs.python.org/issue1368247">issue 1368247</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">Fraction</span></tt></a> class now accepts a single float or
<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> instance, or two rational numbers, as
arguments to its constructor.  (Implemented by Mark Dickinson;
rationals added in <a class="reference external" href="http://bugs.python.org/issue5812">issue 5812</a>, and float/decimal in
<a class="reference external" href="http://bugs.python.org/issue8294">issue 8294</a>.)</p>
<p>Ordering comparisons (<tt class="docutils literal"><span class="pre">&lt;</span></tt>, <tt class="docutils literal"><span class="pre">&lt;=</span></tt>, <tt class="docutils literal"><span class="pre">&gt;</span></tt>, <tt class="docutils literal"><span class="pre">&gt;=</span></tt>) between
fractions and complex numbers now raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.
This fixes an oversight, making the <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">Fraction</span></tt></a>
match the other numeric types.</p>
</li>
<li><p class="first">New class: <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> in
the <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">ftplib</span></tt></a> module provides secure FTP
connections using TLS encapsulation of authentication as well as
subsequent control and data transfers.
(Contributed by Giampaolo Rodola; <a class="reference external" href="http://bugs.python.org/issue2054">issue 2054</a>.)</p>
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.storbinary" title="ftplib.FTP.storbinary"><tt class="xref py py-meth docutils literal"><span class="pre">storbinary()</span></tt></a> method for binary uploads can now restart
uploads thanks to an added <em>rest</em> parameter (patch by Pablo Mouzo;
<a class="reference external" href="http://bugs.python.org/issue6845">issue 6845</a>.)</p>
</li>
<li><p class="first">New class 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">total_ordering()</span></tt></a> in 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 takes a class that defines an <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><tt class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></tt></a> method and one of
<a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><tt class="xref py py-meth docutils literal"><span class="pre">__lt__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><tt class="xref py py-meth docutils literal"><span class="pre">__le__()</span></tt></a>, <a class="reference internal" href="../reference/datamodel.html#object.__gt__" title="object.__gt__"><tt class="xref py py-meth docutils literal"><span class="pre">__gt__()</span></tt></a>, or <a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><tt class="xref py py-meth docutils literal"><span class="pre">__ge__()</span></tt></a>,
and generates the missing comparison methods.  Since the
<a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><tt class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></tt></a> method is being deprecated in Python 3.x,
this decorator makes it easier to define ordered classes.
(Added by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue5479">issue 5479</a>.)</p>
<p>New function: <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">cmp_to_key()</span></tt></a> will take an old-style comparison
function that expects two arguments and return a new callable that
can be used as the <em>key</em> parameter to functions such as
<a class="reference internal" href="../library/functions.html#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a>, <a class="reference internal" href="../library/functions.html#min" title="min"><tt class="xref py py-func docutils literal"><span class="pre">min()</span></tt></a> and <a class="reference internal" href="../library/functions.html#max" title="max"><tt class="xref py py-func docutils literal"><span class="pre">max()</span></tt></a>, etc.  The primary
intended use is to help with making code compatible with Python 3.x.
(Added by Raymond Hettinger.)</p>
</li>
<li><p class="first">New function: the <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><tt class="xref py py-mod docutils literal"><span class="pre">gc</span></tt></a> module&#8217;s <a class="reference internal" href="../library/gc.html#gc.is_tracked" title="gc.is_tracked"><tt class="xref py py-func docutils literal"><span class="pre">is_tracked()</span></tt></a> returns
true if a given instance is tracked by the garbage collector, false
otherwise. (Contributed by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue4688">issue 4688</a>.)</p>
</li>
<li><p class="first">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&#8217;s <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><tt class="xref py py-class docutils literal"><span class="pre">GzipFile</span></tt></a> now supports the context
management protocol, so you can write <tt class="docutils literal"><span class="pre">with</span> <span class="pre">gzip.GzipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></tt>
(contributed by Hagen Fürstenau; <a class="reference external" href="http://bugs.python.org/issue3860">issue 3860</a>), and it 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> ABC, so you can wrap it with
<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> for faster processing
(contributed by Nir Aides; <a class="reference external" href="http://bugs.python.org/issue7471">issue 7471</a>).
It&#8217;s also now possible to override the modification time
recorded in a gzipped file by providing an optional timestamp to
the constructor.  (Contributed by Jacques Frechet; <a class="reference external" href="http://bugs.python.org/issue4272">issue 4272</a>.)</p>
<p>Files in gzip format can be padded with trailing zero bytes; the
<a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><tt class="xref py py-mod docutils literal"><span class="pre">gzip</span></tt></a> module will now consume these trailing bytes.  (Fixed by
Tadek Pietraszek and Brian Curtin; <a class="reference external" href="http://bugs.python.org/issue2846">issue 2846</a>.)</p>
</li>
<li><p class="first">New attribute: 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 now has an <a class="reference internal" href="../library/hashlib.html#hashlib.hashlib.algorithms" title="hashlib.hashlib.algorithms"><tt class="xref py py-attr docutils literal"><span class="pre">algorithms</span></tt></a>
attribute containing a tuple naming the supported algorithms.
In Python 2.7, <tt class="docutils literal"><span class="pre">hashlib.algorithms</span></tt> contains
<tt class="docutils literal"><span class="pre">('md5',</span> <span class="pre">'sha1',</span> <span class="pre">'sha224',</span> <span class="pre">'sha256',</span> <span class="pre">'sha384',</span> <span class="pre">'sha512')</span></tt>.
(Contributed by Carl Chenet; <a class="reference external" href="http://bugs.python.org/issue7418">issue 7418</a>.)</p>
</li>
<li><p class="first">The default <a class="reference internal" href="../library/httplib.html#httplib.HTTPResponse" title="httplib.HTTPResponse"><tt class="xref py py-class docutils literal"><span class="pre">HTTPResponse</span></tt></a> class used by the <a class="reference internal" href="../library/httplib.html#module-httplib" title="httplib: HTTP and HTTPS protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">httplib</span></tt></a> module now
supports buffering, resulting in much faster reading of HTTP responses.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="http://bugs.python.org/issue4879">issue 4879</a>.)</p>
<p>The <a class="reference internal" href="../library/httplib.html#httplib.HTTPConnection" title="httplib.HTTPConnection"><tt class="xref py py-class docutils literal"><span class="pre">HTTPConnection</span></tt></a> and <a class="reference internal" href="../library/httplib.html#httplib.HTTPSConnection" title="httplib.HTTPSConnection"><tt class="xref py py-class docutils literal"><span class="pre">HTTPSConnection</span></tt></a> classes
now support a <em>source_address</em> parameter, a <tt class="docutils literal"><span class="pre">(host,</span> <span class="pre">port)</span></tt> 2-tuple
giving the source address that will be used for the connection.
(Contributed by Eldon Ziegler; <a class="reference external" href="http://bugs.python.org/issue3972">issue 3972</a>.)</p>
</li>
<li><p class="first">The <tt class="xref py py-mod docutils literal"><span class="pre">ihooks</span></tt> module now supports relative imports.  Note that
<tt class="xref py py-mod docutils literal"><span class="pre">ihooks</span></tt> is an older module for customizing imports,
superseded by the <a class="reference internal" href="../library/imputil.html#module-imputil" title="imputil: Manage and augment the import process. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">imputil</span></tt></a> module added in Python 2.0.
(Relative import support added by Neil Schemenauer.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">imaplib</span></tt></a> module now supports IPv6 addresses.
(Contributed by Derek Morr; <a class="reference external" href="http://bugs.python.org/issue1655">issue 1655</a>.)</p>
</li>
<li><p class="first">New function: 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&#8217;s <a class="reference internal" href="../library/inspect.html#inspect.getcallargs" title="inspect.getcallargs"><tt class="xref py py-func docutils literal"><span class="pre">getcallargs()</span></tt></a>
takes a callable and its positional and keyword arguments,
and figures out which of the callable&#8217;s parameters will receive each argument,
returning a dictionary mapping argument names to their values.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">getcallargs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;pos&#39;: (3,), &#39;named&#39;: {}}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">{&#39;a&#39;: 2, &#39;b&#39;: 1, &#39;pos&#39;: (), &#39;named&#39;: {&#39;x&#39;: 4}}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">f() takes at least 1 argument (0 given)</span>
</pre></div>
</div>
<p>Contributed by George Sakkis; <a class="reference external" href="http://bugs.python.org/issue3135">issue 3135</a>.</p>
</li>
<li><p class="first">Updated module: The <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> library has been upgraded to the version shipped with
Python 3.1.  For 3.1, the I/O library was entirely rewritten in C
and is 2 to 20 times faster depending on the task being performed.  The
original Python version was renamed to the <tt class="xref py py-mod docutils literal"><span class="pre">_pyio</span></tt> module.</p>
<p>One minor resulting change: the <a class="reference internal" href="../library/io.html#io.TextIOBase" title="io.TextIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.TextIOBase</span></tt></a> class now
has an <tt class="xref py py-attr docutils literal"><span class="pre">errors</span></tt> attribute giving the error setting
used for encoding and decoding errors (one of <tt class="docutils literal"><span class="pre">'strict'</span></tt>, <tt class="docutils literal"><span class="pre">'replace'</span></tt>,
<tt class="docutils literal"><span class="pre">'ignore'</span></tt>).</p>
<p>The <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><tt class="xref py py-class docutils literal"><span class="pre">io.FileIO</span></tt></a> class now raises an <a class="reference internal" href="../library/exceptions.html#exceptions.OSError" title="exceptions.OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a> when passed
an invalid file descriptor.  (Implemented by Benjamin Peterson;
<a class="reference external" href="http://bugs.python.org/issue4991">issue 4991</a>.)  The <a class="reference internal" href="../library/io.html#io.IOBase.truncate" title="io.IOBase.truncate"><tt class="xref py py-meth docutils literal"><span class="pre">truncate()</span></tt></a> method now preserves the
file position; previously it would change the file position to the
end of the new file.  (Fixed by Pascal Chambon; <a class="reference external" href="http://bugs.python.org/issue6939">issue 6939</a>.)</p>
</li>
<li><p class="first">New function: <tt class="docutils literal"><span class="pre">itertools.compress(data,</span> <span class="pre">selectors)</span></tt> takes two
iterators.  Elements of <em>data</em> are returned if the corresponding
value in <em>selectors</em> is true:</p>
<div class="highlight-python"><pre>itertools.compress('ABCDEF', [1,0,1,0,1,1]) =&gt;
  A, C, E, F</pre>
</div>
<p>New function: <tt class="docutils literal"><span class="pre">itertools.combinations_with_replacement(iter,</span> <span class="pre">r)</span></tt>
returns all the possible <em>r</em>-length combinations of elements from the
iterable <em>iter</em>.  Unlike <a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><tt class="xref py py-func docutils literal"><span class="pre">combinations()</span></tt></a>, individual elements
can be repeated in the generated combinations:</p>
<div class="highlight-python"><pre>itertools.combinations_with_replacement('abc', 2) =&gt;
  ('a', 'a'), ('a', 'b'), ('a', 'c'),
  ('b', 'b'), ('b', 'c'), ('c', 'c')</pre>
</div>
<p>Note that elements are treated as unique depending on their position
in the input, not their actual values.</p>
<p>The <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><tt class="xref py py-func docutils literal"><span class="pre">itertools.count()</span></tt></a> function now has a <em>step</em> argument that
allows incrementing by values other than 1.  <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><tt class="xref py py-func docutils literal"><span class="pre">count()</span></tt></a> also
now allows keyword arguments, and using non-integer values such as
floats or <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt></a> instances.  (Implemented by Raymond
Hettinger; <a class="reference external" href="http://bugs.python.org/issue5032">issue 5032</a>.)</p>
<p><a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><tt class="xref py py-func docutils literal"><span class="pre">itertools.combinations()</span></tt></a> and <a class="reference internal" href="../library/itertools.html#itertools.product" title="itertools.product"><tt class="xref py py-func docutils literal"><span class="pre">itertools.product()</span></tt></a>
previously raised <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> for values of <em>r</em> larger than
the input iterable.  This was deemed a specification error, so they
now return an empty iterator.  (Fixed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue4816">issue 4816</a>.)</p>
</li>
<li><p class="first">Updated module: The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> module was upgraded to version 2.0.9 of the
simplejson package, which includes a C extension that makes
encoding and decoding faster.
(Contributed by Bob Ippolito; <a class="reference external" href="http://bugs.python.org/issue4136">issue 4136</a>.)</p>
<p>To support the new <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> type, <a class="reference internal" href="../library/json.html#json.load" title="json.load"><tt class="xref py py-func docutils literal"><span class="pre">json.load()</span></tt></a>
now has an optional <em>object_pairs_hook</em> parameter that will be called
with any object literal that decodes to a list of pairs.
(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue5381">issue 5381</a>.)</p>
</li>
<li><p class="first">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&#8217;s <a class="reference internal" href="../library/mailbox.html#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> class now records the
timestamp on the directories it reads, and only re-reads them if the
modification time has subsequently changed.  This improves
performance by avoiding unneeded directory scans.  (Fixed by
A.M. Kuchling and Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue1607951">issue 1607951</a>, <a class="reference external" href="http://bugs.python.org/issue6896">issue 6896</a>.)</p>
</li>
<li><p class="first">New functions: 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 gained
<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> and <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> for the error function and the complementary error function,
<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> which computes <tt class="docutils literal"><span class="pre">e**x</span> <span class="pre">-</span> <span class="pre">1</span></tt> with more precision than
using <a class="reference internal" href="../library/math.html#math.exp" title="math.exp"><tt class="xref py py-func docutils literal"><span class="pre">exp()</span></tt></a> and subtracting 1,
<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> for the Gamma function, and
<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> for the natural log of the Gamma function.
(Contributed by Mark Dickinson and nirinA raseliarison; <a class="reference external" href="http://bugs.python.org/issue3366">issue 3366</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Manager*</span></tt> classes
can now be passed a callable that will be called whenever
a subprocess is started, along with a set of arguments that will be
passed to the callable.
(Contributed by lekma; <a class="reference external" href="http://bugs.python.org/issue5585">issue 5585</a>.)</p>
<p>The <tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt> class, which controls a pool of worker processes,
now has an optional <em>maxtasksperchild</em> parameter.  Worker processes
will perform the specified number of tasks and then exit, causing the
<tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt> to start a new worker.  This is useful if tasks may leak
memory or other resources, or if some tasks will cause the worker to
become very large.
(Contributed by Charles Cazabon; <a class="reference external" href="http://bugs.python.org/issue6963">issue 6963</a>.)</p>
</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 now supports IPv6 addresses.
(Contributed by Derek Morr; <a class="reference external" href="http://bugs.python.org/issue1664">issue 1664</a>.)</p>
</li>
<li><p class="first">New functions: 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 wraps the following POSIX system
calls: <a class="reference internal" href="../library/os.html#os.getresgid" title="os.getresgid"><tt class="xref py py-func docutils literal"><span class="pre">getresgid()</span></tt></a> and <a class="reference internal" href="../library/os.html#os.getresuid" title="os.getresuid"><tt class="xref py py-func docutils literal"><span class="pre">getresuid()</span></tt></a>, which return the
real, effective, and saved GIDs and UIDs;
<a class="reference internal" href="../library/os.html#os.setresgid" title="os.setresgid"><tt class="xref py py-func docutils literal"><span class="pre">setresgid()</span></tt></a> and <a class="reference internal" href="../library/os.html#os.setresuid" title="os.setresuid"><tt class="xref py py-func docutils literal"><span class="pre">setresuid()</span></tt></a>, which set
real, effective, and saved GIDs and UIDs to new values;
<a class="reference internal" href="../library/os.html#os.initgroups" title="os.initgroups"><tt class="xref py py-func docutils literal"><span class="pre">initgroups()</span></tt></a>, which initialize the group access list
for the current process.  (GID/UID functions
contributed by Travis H.; <a class="reference external" href="http://bugs.python.org/issue6508">issue 6508</a>.  Support for initgroups added
by Jean-Paul Calderone; <a class="reference external" href="http://bugs.python.org/issue7333">issue 7333</a>.)</p>
<p>The <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><tt class="xref py py-func docutils literal"><span class="pre">os.fork()</span></tt></a> function now re-initializes the import lock in
the child process; this fixes problems on Solaris when <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><tt class="xref py py-func docutils literal"><span class="pre">fork()</span></tt></a>
is called from a thread.  (Fixed by Zsolt Cserna; <a class="reference external" href="http://bugs.python.org/issue7242">issue 7242</a>.)</p>
</li>
<li><p class="first">In the <a class="reference internal" href="../library/os.path.html#module-os.path" title="os.path: Operations on pathnames."><tt class="xref py py-mod docutils literal"><span class="pre">os.path</span></tt></a> module, the <a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><tt class="xref py py-func docutils literal"><span class="pre">normpath()</span></tt></a> and
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><tt class="xref py py-func docutils literal"><span class="pre">abspath()</span></tt></a> functions now preserve Unicode; if their input path
is a Unicode string, the return value is also a Unicode string.
(<a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><tt class="xref py py-meth docutils literal"><span class="pre">normpath()</span></tt></a> fixed by Matt Giuca in <a class="reference external" href="http://bugs.python.org/issue5827">issue 5827</a>;
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><tt class="xref py py-meth docutils literal"><span class="pre">abspath()</span></tt></a> fixed by Ezio Melotti in <a class="reference external" href="http://bugs.python.org/issue3426">issue 3426</a>.)</p>
</li>
<li><p class="first">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 has help for the various symbols that Python
uses.  You can now do <tt class="docutils literal"><span class="pre">help('&lt;&lt;')</span></tt> or <tt class="docutils literal"><span class="pre">help('&#64;')</span></tt>, for example.
(Contributed by David Laban; <a class="reference external" href="http://bugs.python.org/issue4739">issue 4739</a>.)</p>
</li>
<li><p class="first">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&#8217;s <a class="reference internal" href="../library/re.html#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">split()</span></tt></a>, <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">sub()</span></tt></a>, and <a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><tt class="xref py py-func docutils literal"><span class="pre">subn()</span></tt></a>
now accept an optional <em>flags</em> argument, for consistency with the
other functions in the module.  (Added by Gregory P. Smith.)</p>
</li>
<li><p class="first">New function: <a class="reference internal" href="../library/runpy.html#runpy.run_path" title="runpy.run_path"><tt class="xref py py-func docutils literal"><span class="pre">run_path()</span></tt></a> in the <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> module
will execute the code at a provided <em>path</em> argument.  <em>path</em> can be
the path of a Python source file (<tt class="file docutils literal"><span class="pre">example.py</span></tt>), a compiled
bytecode file (<tt class="file docutils literal"><span class="pre">example.pyc</span></tt>), a directory
(<tt class="file docutils literal"><span class="pre">./package/</span></tt>), or a zip archive (<tt class="file docutils literal"><span class="pre">example.zip</span></tt>).  If a
directory or zip path is provided, it will be added to the front of
<tt class="docutils literal"><span class="pre">sys.path</span></tt> and the module <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><tt class="xref py py-mod docutils literal"><span class="pre">__main__</span></tt></a> will be imported.  It&#8217;s
expected that the directory or zip contains a <tt class="file docutils literal"><span class="pre">__main__.py</span></tt>;
if it doesn&#8217;t, some other <tt class="file docutils literal"><span class="pre">__main__.py</span></tt> might be imported from
a location later in <tt class="docutils literal"><span class="pre">sys.path</span></tt>.  This makes more of the machinery
of <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> available to scripts that want to mimic the way
Python&#8217;s command line processes an explicit path name.
(Added by Nick Coghlan; <a class="reference external" href="http://bugs.python.org/issue6816">issue 6816</a>.)</p>
</li>
<li><p class="first">New function: in 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, <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>
takes a filename, archive type (zip or tar-format), and a directory
path, and creates an archive containing the directory&#8217;s contents.
(Added by Tarek Ziadé.)</p>
<p><a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><tt class="xref py py-mod docutils literal"><span class="pre">shutil</span></tt></a>&#8216;s <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><tt class="xref py py-func docutils literal"><span class="pre">copyfile()</span></tt></a> and <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><tt class="xref py py-func docutils literal"><span class="pre">copytree()</span></tt></a>
functions now raise a <tt class="xref py py-exc docutils literal"><span class="pre">SpecialFileError</span></tt> exception when
asked to copy a named pipe.  Previously the code would treat
named pipes like a regular file by opening them for reading, and
this would block indefinitely.  (Fixed by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue3002">issue 3002</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><tt class="xref py py-mod docutils literal"><span class="pre">signal</span></tt></a> module no longer re-installs the signal handler
unless this is truly necessary, which fixes a bug that could make it
impossible to catch the EINTR signal robustly.  (Fixed by
Charles-Francois Natali; <a class="reference external" href="http://bugs.python.org/issue8354">issue 8354</a>.)</p>
</li>
<li><p class="first">New functions: in 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, three new functions
return various site- and user-specific paths.
<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> returns a list containing all
global site-packages directories,
<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> returns the path of the user&#8217;s
site-packages directory, and
<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> returns the value of the <span class="target" id="index-8"></span><tt class="xref std std-envvar docutils literal"><span class="pre">USER_BASE</span></tt>
environment variable, giving the path to a directory that can be used
to store data.
(Contributed by Tarek Ziadé; <a class="reference external" href="http://bugs.python.org/issue6693">issue 6693</a>.)</p>
<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 now reports exceptions occurring
when the <tt class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></tt> module is imported, and will no longer
catch and swallow the <a class="reference internal" href="../library/exceptions.html#exceptions.KeyboardInterrupt" title="exceptions.KeyboardInterrupt"><tt class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> exception.  (Fixed by
Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue3137">issue 3137</a>.)</p>
</li>
<li><p class="first">The <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">create_connection()</span></tt></a> function
gained a <em>source_address</em> parameter, a <tt class="docutils literal"><span class="pre">(host,</span> <span class="pre">port)</span></tt> 2-tuple
giving the source address that will be used for the connection.
(Contributed by Eldon Ziegler; <a class="reference external" href="http://bugs.python.org/issue3972">issue 3972</a>.)</p>
<p>The <a class="reference internal" href="../library/socket.html#socket.socket.recv_into" title="socket.socket.recv_into"><tt class="xref py py-meth docutils literal"><span class="pre">recv_into()</span></tt></a> and <a class="reference internal" href="../library/socket.html#socket.socket.recvfrom_into" title="socket.socket.recvfrom_into"><tt class="xref py py-meth docutils literal"><span class="pre">recvfrom_into()</span></tt></a>
methods will now write into objects that support the buffer API, most usefully
the <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/stdtypes.html#memoryview" title="memoryview"><tt class="xref py py-class docutils literal"><span class="pre">memoryview</span></tt></a> objects.  (Implemented by
Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue8104">issue 8104</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/socketserver.html#module-SocketServer" title="SocketServer: A framework for network servers."><tt class="xref py py-mod docutils literal"><span class="pre">SocketServer</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">TCPServer</span></tt> class now
supports socket timeouts and disabling the Nagle algorithm.
The <tt class="xref py py-attr docutils literal"><span class="pre">disable_nagle_algorithm</span></tt> class attribute
defaults to False; if overridden to be True,
new request connections will have the TCP_NODELAY option set to
prevent buffering many small sends into a single TCP packet.
The <a class="reference internal" href="../library/socketserver.html#SocketServer.BaseServer.timeout" title="SocketServer.BaseServer.timeout"><tt class="xref py py-attr docutils literal"><span class="pre">timeout</span></tt></a> class attribute can hold
a timeout in seconds that will be applied to the request socket; if
no request is received within that time, <a class="reference internal" href="../library/socketserver.html#SocketServer.BaseServer.handle_timeout" title="SocketServer.BaseServer.handle_timeout"><tt class="xref py py-meth docutils literal"><span class="pre">handle_timeout()</span></tt></a>
will be called and <a class="reference internal" href="../library/socketserver.html#SocketServer.BaseServer.handle_request" title="SocketServer.BaseServer.handle_request"><tt class="xref py py-meth docutils literal"><span class="pre">handle_request()</span></tt></a> will return.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="http://bugs.python.org/issue6192">issue 6192</a> and <a class="reference external" href="http://bugs.python.org/issue6267">issue 6267</a>.)</p>
</li>
<li><p class="first">Updated module: the <a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module has been updated to
version 2.6.0 of the <a class="reference external" href="http://code.google.com/p/pysqlite/">pysqlite package</a>. Version 2.6.0 includes a number of bugfixes, and adds
the ability to load SQLite extensions from shared libraries.
Call the <tt class="docutils literal"><span class="pre">enable_load_extension(True)</span></tt> method to enable extensions,
and then call <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">load_extension()</span></tt></a> to load a particular shared library.
(Updated by Gerhard Häring.)</p>
</li>
<li><p class="first">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&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">SSLSocket</span></tt> objects now support the
buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
<a class="reference external" href="http://bugs.python.org/issue7133">issue 7133</a>) and automatically set
OpenSSL&#8217;s <tt class="xref c c-macro docutils literal"><span class="pre">SSL_MODE_AUTO_RETRY</span></tt>, which will prevent an error
code being returned from <tt class="xref py py-meth docutils literal"><span class="pre">recv()</span></tt> operations that trigger an SSL
renegotiation (fix by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue8222">issue 8222</a>).</p>
<p>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 that&#8217;s a string listing the encryption algorithms
to be allowed; the format of the string is described
<a class="reference external" href="http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT">in the OpenSSL documentation</a>.
(Added by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue8322">issue 8322</a>.)</p>
<p>Another change makes the extension load all of OpenSSL&#8217;s ciphers and
digest algorithms so that they&#8217;re all available.  Some SSL
certificates couldn&#8217;t be verified, reporting an &#8220;unknown algorithm&#8221;
error.  (Reported by Beda Kosata, and fixed by Antoine Pitrou;
<a class="reference external" href="http://bugs.python.org/issue8484">issue 8484</a>.)</p>
<p>The version of OpenSSL being used is now available as the module
attributes <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION" title="ssl.OPENSSL_VERSION"><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).  (Added by Antoine
Pitrou; <a class="reference external" href="http://bugs.python.org/issue8321">issue 8321</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module will no longer silently ignore overflow
errors when a value is too large for a particular integer format
code (one of <tt class="docutils literal"><span class="pre">bBhHiIlLqQ</span></tt>); it now always raises a
<a class="reference internal" href="../library/struct.html#struct.error" title="struct.error"><tt class="xref py py-exc docutils literal"><span class="pre">struct.error</span></tt></a> exception.  (Changed by Mark Dickinson;
<a class="reference external" href="http://bugs.python.org/issue1523">issue 1523</a>.)  The <a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><tt class="xref py py-func docutils literal"><span class="pre">pack()</span></tt></a> function will also
attempt to use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">__index__()</span></tt></a> to convert and pack non-integers
before trying the <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><tt class="xref py py-meth docutils literal"><span class="pre">__int__()</span></tt></a> method or reporting an error.
(Changed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue8300">issue 8300</a>.)</p>
</li>
<li><p class="first">New function: the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module&#8217;s
<a class="reference internal" href="../library/subprocess.html#subprocess.check_output" title="subprocess.check_output"><tt class="xref py py-func docutils literal"><span class="pre">check_output()</span></tt></a> runs a command with a specified set of arguments
and returns the command&#8217;s output as a string when the command runs without
error, or raises a <a class="reference internal" href="../library/subprocess.html#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><tt class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></tt></a> exception otherwise.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s">&#39;df&#39;</span><span class="p">,</span> <span class="s">&#39;-h&#39;</span><span class="p">,</span> <span class="s">&#39;.&#39;</span><span class="p">])</span>
<span class="go">&#39;Filesystem     Size   Used  Avail Capacity  Mounted on\n</span>
<span class="go">/dev/disk0s2    52G    49G   3.0G    94%    /\n&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="s">&#39;df&#39;</span><span class="p">,</span> <span class="s">&#39;-h&#39;</span><span class="p">,</span> <span class="s">&#39;/bogus&#39;</span><span class="p">])</span>
<span class="go">  ...</span>
<span class="go">subprocess.CalledProcessError: Command &#39;[&#39;df&#39;, &#39;-h&#39;, &#39;/bogus&#39;]&#39; returned non-zero exit status 1</span>
</pre></div>
</div>
<p>(Contributed by Gregory P. Smith.)</p>
<p>The <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module will now retry its internal system calls
on receiving an <tt class="xref py py-const docutils literal"><span class="pre">EINTR</span></tt> signal.  (Reported by several people; final
patch by Gregory P. Smith in <a class="reference external" href="http://bugs.python.org/issue1068268">issue 1068268</a>.)</p>
</li>
<li><p class="first">New function: <a class="reference internal" href="../library/symtable.html#symtable.Symbol.is_declared_global" title="symtable.Symbol.is_declared_global"><tt class="xref py py-func docutils literal"><span class="pre">is_declared_global()</span></tt></a> in the <a class="reference internal" href="../library/symtable.html#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><tt class="xref py py-mod docutils literal"><span class="pre">symtable</span></tt></a> module
returns true for variables that are explicitly declared to be global,
false for ones that are implicitly global.
(Contributed by Jeremy Hylton.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">syslog</span></tt></a> module will now use the value of <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> as the
identifier instead of the previous default value of <tt class="docutils literal"><span class="pre">'python'</span></tt>.
(Changed by Sean Reifschneider; <a class="reference external" href="http://bugs.python.org/issue8451">issue 8451</a>.)</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">sys.version_info</span></tt> value is now a named tuple, with attributes
named <tt class="xref py py-attr docutils literal"><span class="pre">major</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minor</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">micro</span></tt>,
<tt class="xref py py-attr docutils literal"><span class="pre">releaselevel</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">serial</span></tt>.  (Contributed by Ross
Light; <a class="reference external" href="http://bugs.python.org/issue4285">issue 4285</a>.)</p>
<p><a class="reference internal" href="../library/sys.html#sys.getwindowsversion" title="sys.getwindowsversion"><tt class="xref py py-func docutils literal"><span class="pre">sys.getwindowsversion()</span></tt></a> also returns a named tuple,
with attributes named <tt class="xref py py-attr docutils literal"><span class="pre">major</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">minor</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">build</span></tt>,
<a class="reference internal" href="../library/platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><tt class="xref py py-attr docutils literal"><span class="pre">platform</span></tt></a>, <tt class="xref py py-attr docutils literal"><span class="pre">service_pack</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">service_pack_major</span></tt>,
<tt class="xref py py-attr docutils literal"><span class="pre">service_pack_minor</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">suite_mask</span></tt>, and
<tt class="xref py py-attr docutils literal"><span class="pre">product_type</span></tt>.  (Contributed by Brian Curtin; <a class="reference external" href="http://bugs.python.org/issue7766">issue 7766</a>.)</p>
</li>
<li><p class="first">The <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> module&#8217;s default error handling has changed, to
no longer suppress fatal errors.  The default error level was previously 0,
which meant that errors would only result in a message being written to the
debug log, but because the debug log is not activated by default,
these errors go unnoticed.  The default error level is now 1,
which raises an exception if there&#8217;s an error.
(Changed by Lars Gustäbel; <a class="reference external" href="http://bugs.python.org/issue7357">issue 7357</a>.)</p>
<p><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> now supports filtering the <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>
objects being added to a tar file.  When you call <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.add" title="tarfile.TarFile.add"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a>,
you may supply an optional <em>filter</em> argument
that&#8217;s a callable.  The <em>filter</em> callable will be passed the
<a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><tt class="xref py py-class docutils literal"><span class="pre">TarInfo</span></tt></a> for every file being added, and can modify and return it.
If the callable returns <tt class="docutils literal"><span class="pre">None</span></tt>, the file will be excluded from the
resulting archive.  This is more powerful than the existing
<em>exclude</em> argument, which has therefore been deprecated.
(Added by Lars Gustäbel; <a class="reference external" href="http://bugs.python.org/issue6856">issue 6856</a>.)
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 also now supports the context manager protocol.
(Added by Lars Gustäbel; <a class="reference external" href="http://bugs.python.org/issue7232">issue 7232</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> method of the <a class="reference internal" href="../library/threading.html#threading.Event" title="threading.Event"><tt class="xref py py-class docutils literal"><span class="pre">threading.Event</span></tt></a> class
now returns the internal flag on exit.  This means the method will usually
return true because <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> is supposed to block until the
internal flag becomes true.  The return value will only be false if
a timeout was provided and the operation timed out.
(Contributed by Tim Lesher; <a class="reference external" href="http://bugs.python.org/issue1674032">issue 1674032</a>.)</p>
</li>
<li><p class="first">The Unicode database provided by the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><tt class="xref py py-mod docutils literal"><span class="pre">unicodedata</span></tt></a> module is
now used internally to determine which characters are numeric,
whitespace, or represent line breaks.  The database also
includes information from the <tt class="file docutils literal"><span class="pre">Unihan.txt</span></tt> data file (patch
by Anders Chrigström and Amaury Forgeot d&#8217;Arc; <a class="reference external" href="http://bugs.python.org/issue1571184">issue 1571184</a>)
and has been updated to version 5.2.0 (updated by
Florent Xicluna; <a class="reference external" href="http://bugs.python.org/issue8024">issue 8024</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/urlparse.html#module-urlparse" title="urlparse: Parse URLs into or assemble them from components."><tt class="xref py py-mod docutils literal"><span class="pre">urlparse</span></tt></a> module&#8217;s <a class="reference internal" href="../library/urlparse.html#urlparse.urlsplit" title="urlparse.urlsplit"><tt class="xref py py-func docutils literal"><span class="pre">urlsplit()</span></tt></a> now handles
unknown URL schemes in a fashion compliant with <span class="target" id="index-9"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
URL is of the form <tt class="docutils literal"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></tt>, the text before the
<tt class="docutils literal"><span class="pre">://</span></tt> is treated as the scheme, even if it&#8217;s a made-up scheme that
the module doesn&#8217;t know about.  This change may break code that
worked around the old behaviour.  For example, Python 2.6.4 or 2.5
will return the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>Python 2.7 (and Python 2.6.5) will return:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)</p>
<p>The <a class="reference internal" href="../library/urlparse.html#module-urlparse" title="urlparse: Parse URLs into or assemble them from components."><tt class="xref py py-mod docutils literal"><span class="pre">urlparse</span></tt></a> module also supports IPv6 literal addresses as defined by
<span class="target" id="index-10"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a> (contributed by Senthil Kumaran; <a class="reference external" href="http://bugs.python.org/issue2987">issue 2987</a>).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s">&#39;http://[1080::8:800:200C:417A]/foo&#39;</span><span class="p">)</span>
<span class="go">ParseResult(scheme=&#39;http&#39;, netloc=&#39;[1080::8:800:200C:417A]&#39;,</span>
<span class="go">            path=&#39;/foo&#39;, params=&#39;&#39;, query=&#39;&#39;, fragment=&#39;&#39;)</span>
</pre></div>
</div>
</li>
<li><p class="first">New class: the <a class="reference internal" href="../library/weakref.html#weakref.WeakSet" title="weakref.WeakSet"><tt class="xref py py-class docutils literal"><span class="pre">WeakSet</span></tt></a> class in the <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><tt class="xref py py-mod docutils literal"><span class="pre">weakref</span></tt></a>
module is a set that only holds weak references to its elements; elements
will be removed once there are no references pointing to them.
(Originally implemented in Python 3.x by Raymond Hettinger, and backported
to 2.7 by Michael Foord.)</p>
</li>
<li><p class="first">The ElementTree library, <tt class="xref py py-mod docutils literal"><span class="pre">xml.etree</span></tt>, no longer escapes
ampersands and angle brackets when outputting an XML processing
instruction (which looks like <tt class="docutils literal"><span class="pre">&lt;?xml-stylesheet</span> <span class="pre">href=&quot;#style1&quot;?&gt;</span></tt>)
or comment (which looks like <tt class="docutils literal"><span class="pre">&lt;!--</span> <span class="pre">comment</span> <span class="pre">--&gt;</span></tt>).
(Patch by Neil Muller; <a class="reference external" href="http://bugs.python.org/issue2746">issue 2746</a>.)</p>
</li>
<li><p class="first">The XML-RPC client and server, provided by the <a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> and
<a class="reference internal" href="../library/simplexmlrpcserver.html#module-SimpleXMLRPCServer" title="SimpleXMLRPCServer: Basic XML-RPC server implementation."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleXMLRPCServer</span></tt></a> modules, have improved performance by
supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
to compress the XML being exchanged.  The gzip compression is
controlled by the <tt class="xref py py-attr docutils literal"><span class="pre">encode_threshold</span></tt> attribute of
<tt class="xref py py-class docutils literal"><span class="pre">SimpleXMLRPCRequestHandler</span></tt>, which contains a size in bytes;
responses larger than this will be compressed.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="http://bugs.python.org/issue6267">issue 6267</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a> module&#8217;s <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><tt class="xref py py-class docutils literal"><span class="pre">ZipFile</span></tt></a> now supports the context
management protocol, so you can write <tt class="docutils literal"><span class="pre">with</span> <span class="pre">zipfile.ZipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></tt>.
(Contributed by Brian Curtin; <a class="reference external" href="http://bugs.python.org/issue5511">issue 5511</a>.)</p>
<p><a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a> now also supports archiving empty directories and
extracts them correctly.  (Fixed by Kuba Wieczorek; <a class="reference external" href="http://bugs.python.org/issue4710">issue 4710</a>.)
Reading files out of an archive is faster, and interleaving
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt></a> and <tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt> now works correctly.
(Contributed by Nir Aides; <a class="reference external" href="http://bugs.python.org/issue7610">issue 7610</a>.)</p>
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.is_zipfile" title="zipfile.is_zipfile"><tt class="xref py py-func docutils literal"><span class="pre">is_zipfile()</span></tt></a> function now
accepts a file object, in addition to the path names accepted in earlier
versions.  (Contributed by Gabriel Genellina; <a class="reference external" href="http://bugs.python.org/issue4756">issue 4756</a>.)</p>
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><tt class="xref py py-meth docutils literal"><span class="pre">writestr()</span></tt></a> method now has an optional <em>compress_type</em> parameter
that lets you override the default compression method specified in the
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><tt class="xref py py-class docutils literal"><span class="pre">ZipFile</span></tt></a> constructor.  (Contributed by Ronald Oussoren;
<a class="reference external" href="http://bugs.python.org/issue6003">issue 6003</a>.)</p>
</li>
</ul>
<div class="section" id="new-module-importlib">
<span id="importlib-section"></span><h3>New module: importlib<a class="headerlink" href="#new-module-importlib" title="Permalink to this headline">¶</a></h3>
<p>Python 3.1 includes the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: Convenience wrappers for __import__"><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> package, a re-implementation
of the logic underlying Python&#8217;s <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement.
<a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: Convenience wrappers for __import__"><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> is useful for implementors of Python interpreters and
to users who wish to write new importers that can participate in the
import process.  Python 2.7 doesn&#8217;t contain the complete
<a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: Convenience wrappers for __import__"><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> package, but instead has a tiny subset that contains
a single function, <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><tt class="xref py py-func docutils literal"><span class="pre">import_module()</span></tt></a>.</p>
<p><tt class="docutils literal"><span class="pre">import_module(name,</span> <span class="pre">package=None)</span></tt> imports a module.  <em>name</em> is
a string containing the module or package&#8217;s name.  It&#8217;s possible to do
relative imports by providing a string that begins with a <tt class="docutils literal"><span class="pre">.</span></tt>
character, such as <tt class="docutils literal"><span class="pre">..utils.errors</span></tt>.  For relative imports, the
<em>package</em> argument must be provided and is the name of the package that
will be used as the anchor for
the relative import.  <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><tt class="xref py py-func docutils literal"><span class="pre">import_module()</span></tt></a> both inserts the imported
module into <tt class="docutils literal"><span class="pre">sys.modules</span></tt> and returns the module object.</p>
<p>Here are some examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s">&#39;anydbm&#39;</span><span class="p">)</span>  <span class="c"># Standard absolute import</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span>
<span class="go">&lt;module &#39;anydbm&#39; from &#39;/p/python/Lib/anydbm.py&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Relative import</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s">&#39;..file_util&#39;</span><span class="p">,</span> <span class="s">&#39;distutils.command&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span>
<span class="go">&lt;module &#39;distutils.file_util&#39; from &#39;/python/Lib/distutils/file_util.pyc&#39;&gt;</span>
</pre></div>
</div>
<p><a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: Convenience wrappers for __import__"><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> was implemented by Brett Cannon and introduced in
Python 3.1.</p>
</div>
<div class="section" id="new-module-sysconfig">
<h3>New module: sysconfig<a class="headerlink" href="#new-module-sysconfig" title="Permalink to this headline">¶</a></h3>
<p>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 has been pulled out of the Distutils
package, becoming a new top-level module in its own right.
<a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><tt class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></tt></a> provides functions for getting information about
Python&#8217;s build process: compiler switches, installation paths, the
platform name, and whether Python is running from its source
directory.</p>
<p>Some of the functions in the module are:</p>
<ul class="simple">
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_var" title="sysconfig.get_config_var"><tt class="xref py py-func docutils literal"><span class="pre">get_config_var()</span></tt></a> returns variables from Python&#8217;s
Makefile and the <tt class="file docutils literal"><span class="pre">pyconfig.h</span></tt> file.</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 containing
all of the configuration variables.</li>
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_path" title="sysconfig.get_path"><tt class="xref py py-func docutils literal"><span class="pre">get_path()</span></tt></a> returns the configured path for
a particular type of module: the standard library,
site-specific modules, platform-specific modules, etc.</li>
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.is_python_build" title="sysconfig.is_python_build"><tt class="xref py py-func docutils literal"><span class="pre">is_python_build()</span></tt></a> returns true if you&#8217;re running a
binary from a Python source tree, and false otherwise.</li>
</ul>
<p>Consult 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> documentation for more details and for
a complete list of functions.</p>
<p>The Distutils package and <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><tt class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></tt></a> are now maintained by Tarek
Ziadé, who has also started a Distutils2 package (source repository at
<a class="reference external" href="http://hg.python.org/distutils2/">http://hg.python.org/distutils2/</a>) for developing a next-generation
version of Distutils.</p>
</div>
<div class="section" id="ttk-themed-widgets-for-tk">
<h3>ttk: Themed Widgets for Tk<a class="headerlink" href="#ttk-themed-widgets-for-tk" title="Permalink to this headline">¶</a></h3>
<p>Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
widgets but have a more customizable appearance and can therefore more
closely resemble the native platform&#8217;s widgets.  This widget
set was originally called Tile, but was renamed to Ttk (for &#8220;themed Tk&#8221;)
on being added to Tcl/Tck release 8.5.</p>
<p>To learn more, read the <a class="reference internal" href="../library/ttk.html#module-ttk" title="ttk: Tk themed widget set"><tt class="xref py py-mod docutils literal"><span class="pre">ttk</span></tt></a> module documentation.  You may also
wish to read the Tcl/Tk manual page describing the
Ttk theme engine, available at
<a class="reference external" href="http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm">http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm</a>. Some
screenshots of the Python/Ttk code in use are at
<a class="reference external" href="http://code.google.com/p/python-ttk/wiki/Screenshots">http://code.google.com/p/python-ttk/wiki/Screenshots</a>.</p>
<p>The <a class="reference internal" href="../library/ttk.html#module-ttk" title="ttk: Tk themed widget set"><tt class="xref py py-mod docutils literal"><span class="pre">ttk</span></tt></a> module was written by Guilherme Polo and added in
<a class="reference external" href="http://bugs.python.org/issue2983">issue 2983</a>.  An alternate version called <tt class="docutils literal"><span class="pre">Tile.py</span></tt>, written by
Martin Franklin and maintained by Kevin Walzer, was proposed for
inclusion in <a class="reference external" href="http://bugs.python.org/issue2618">issue 2618</a>, but the authors argued that Guilherme
Polo&#8217;s work was more comprehensive.</p>
</div>
<div class="section" id="updated-module-unittest">
<span id="unittest-section"></span><h3>Updated module: unittest<a class="headerlink" href="#updated-module-unittest" title="Permalink to this headline">¶</a></h3>
<p>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 was greatly enhanced; many
new features were added.  Most of these features were implemented
by Michael Foord, unless otherwise noted.  The enhanced version of
the module is downloadable separately for use with Python versions 2.4 to 2.6,
packaged as the <tt class="xref py py-mod docutils literal"><span class="pre">unittest2</span></tt> package, from
<a class="reference external" href="http://pypi.python.org/pypi/unittest2">http://pypi.python.org/pypi/unittest2</a>.</p>
<p>When used from the command line, the module can automatically discover
tests.  It&#8217;s not as fancy as <a class="reference external" href="http://pytest.org">py.test</a> or
<a class="reference external" href="http://code.google.com/p/python-nose/">nose</a>, but provides a simple way
to run tests kept within a set of package directories.  For example,
the following command will search the <tt class="file docutils literal"><span class="pre">test/</span></tt> subdirectory for
any importable test files named <tt class="docutils literal"><span class="pre">test*.py</span></tt>:</p>
<div class="highlight-python"><pre>python -m unittest discover -s test</pre>
</div>
<p>Consult 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 documentation for more details.
(Developed in <a class="reference external" href="http://bugs.python.org/issue6001">issue 6001</a>.)</p>
<p>The <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><tt class="xref py py-func docutils literal"><span class="pre">main()</span></tt></a> function supports some other new options:</p>
<ul>
<li><p class="first"><em class="xref std std-option">-b</em> or <em class="xref std std-option">--buffer</em> will buffer the standard output
and standard error streams during each test.  If the test passes,
any resulting output will be discarded; on failure, the buffered
output will be displayed.</p>
</li>
<li><p class="first"><a class="reference internal" href="../using/cmdline.html#cmdoption-c"><em class="xref std std-option">-c</em></a> or <em class="xref std std-option">--catch</em> will cause the control-C interrupt
to be handled more gracefully.  Instead of interrupting the test
process immediately, the currently running test will be completed
and then the partial results up to the interruption will be reported.
If you&#8217;re impatient, a second press of control-C will cause an immediate
interruption.</p>
<p>This control-C handler tries to avoid causing problems when the code
being tested or the tests being run have defined a signal handler of
their own, by noticing that a signal handler was already set and
calling it.  If this doesn&#8217;t work for you, there&#8217;s a
<a class="reference internal" href="../library/unittest.html#unittest.removeHandler" title="unittest.removeHandler"><tt class="xref py py-func docutils literal"><span class="pre">removeHandler()</span></tt></a> decorator that can be used to mark tests that
should have the control-C handling disabled.</p>
</li>
<li><p class="first"><em class="xref std std-option">-f</em> or <em class="xref std std-option">--failfast</em> makes
test execution stop immediately when a test fails instead of
continuing to execute further tests.  (Suggested by Cliff Dyer and
implemented by Michael Foord; <a class="reference external" href="http://bugs.python.org/issue8074">issue 8074</a>.)</p>
</li>
</ul>
<p>The progress messages now show &#8216;x&#8217; for expected failures
and &#8216;u&#8217; for unexpected successes when run in verbose mode.
(Contributed by Benjamin Peterson.)</p>
<p>Test cases can raise the <a class="reference internal" href="../library/unittest.html#unittest.SkipTest" title="unittest.SkipTest"><tt class="xref py py-exc docutils literal"><span class="pre">SkipTest</span></tt></a> exception to skip a
test (<a class="reference external" href="http://bugs.python.org/issue1034053">issue 1034053</a>).</p>
<p>The error messages for <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>,
<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>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><tt class="xref py py-meth docutils literal"><span class="pre">assertFalse()</span></tt></a>
failures now provide more information.  If you set the
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.longMessage" title="unittest.TestCase.longMessage"><tt class="xref py py-attr docutils literal"><span class="pre">longMessage</span></tt></a> attribute of your <a class="reference internal" href="../library/unittest.html#unittest.TestCase" title="unittest.TestCase"><tt class="xref py py-class docutils literal"><span class="pre">TestCase</span></tt></a> classes to
True, both the standard error message and any additional message you
provide will be printed for failures.  (Added by Michael Foord; <a class="reference external" href="http://bugs.python.org/issue5663">issue 5663</a>.)</p>
<p>The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><tt class="xref py py-meth docutils literal"><span class="pre">assertRaises()</span></tt></a> method now
returns a context handler when called without providing a callable
object to run.  For example, you can write this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">KeyError</span><span class="p">):</span>
    <span class="p">{}[</span><span class="s">&#39;foo&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>(Implemented by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue4444">issue 4444</a>.)</p>
<p>Module- and class-level setup and teardown fixtures are now supported.
Modules can contain <tt class="xref py py-func docutils literal"><span class="pre">setUpModule()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">tearDownModule()</span></tt>
functions.  Classes can have <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><tt class="xref py py-meth docutils literal"><span class="pre">setUpClass()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><tt class="xref py py-meth docutils literal"><span class="pre">tearDownClass()</span></tt></a> methods that must be defined as class methods
(using <tt class="docutils literal"><span class="pre">&#64;classmethod</span></tt> or equivalent).  These functions and
methods are invoked when the test runner switches to a test case in a
different module or class.</p>
<p>The methods <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><tt class="xref py py-meth docutils literal"><span class="pre">addCleanup()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><tt class="xref py py-meth docutils literal"><span class="pre">doCleanups()</span></tt></a> were added.
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><tt class="xref py py-meth docutils literal"><span class="pre">addCleanup()</span></tt></a> lets you add cleanup functions that
will be called unconditionally (after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><tt class="xref py py-meth docutils literal"><span class="pre">setUp()</span></tt></a> if
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><tt class="xref py py-meth docutils literal"><span class="pre">setUp()</span></tt></a> fails, otherwise after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><tt class="xref py py-meth docutils literal"><span class="pre">tearDown()</span></tt></a>). This allows
for much simpler resource allocation and deallocation during tests
(<a class="reference external" href="http://bugs.python.org/issue5679">issue 5679</a>).</p>
<p>A number of new methods were added that provide more specialized
tests.  Many of these methods were written by Google engineers
for use in their test suites; Gregory P. Smith, Michael Foord, and
GvR worked on merging them into Python&#8217;s version of <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>.</p>
<ul class="simple">
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNone" title="unittest.TestCase.assertIsNone"><tt class="xref py py-meth docutils literal"><span class="pre">assertIsNone()</span></tt></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNotNone" title="unittest.TestCase.assertIsNotNone"><tt class="xref py py-meth docutils literal"><span class="pre">assertIsNotNone()</span></tt></a> take one
expression and verify that the result is or is not <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><tt class="xref py py-meth docutils literal"><span class="pre">assertIs()</span></tt></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNot" title="unittest.TestCase.assertIsNot"><tt class="xref py py-meth docutils literal"><span class="pre">assertIsNot()</span></tt></a>
take two values and check whether the two values evaluate to the same object or not.
(Added by Michael Foord; <a class="reference external" href="http://bugs.python.org/issue2578">issue 2578</a>.)</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsInstance" title="unittest.TestCase.assertIsInstance"><tt class="xref py py-meth docutils literal"><span class="pre">assertIsInstance()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIsInstance" title="unittest.TestCase.assertNotIsInstance"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotIsInstance()</span></tt></a> check whether
the resulting object is an instance of a particular class, or of
one of a tuple of classes.  (Added by Georg Brandl; <a class="reference external" href="http://bugs.python.org/issue7031">issue 7031</a>.)</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreater" title="unittest.TestCase.assertGreater"><tt class="xref py py-meth docutils literal"><span class="pre">assertGreater()</span></tt></a>, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreaterEqual" title="unittest.TestCase.assertGreaterEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertGreaterEqual()</span></tt></a>,
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLess" title="unittest.TestCase.assertLess"><tt class="xref py py-meth docutils literal"><span class="pre">assertLess()</span></tt></a>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLessEqual" title="unittest.TestCase.assertLessEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertLessEqual()</span></tt></a> compare
two quantities.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertMultiLineEqual()</span></tt></a> compares two strings, and if they&#8217;re
not equal, displays a helpful comparison that highlights the
differences in the two strings.  This comparison is now used by
default when Unicode strings are compared with <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></tt></a>.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRegexpMatches" title="unittest.TestCase.assertRegexpMatches"><tt class="xref py py-meth docutils literal"><span class="pre">assertRegexpMatches()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotRegexpMatches" title="unittest.TestCase.assertNotRegexpMatches"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotRegexpMatches()</span></tt></a> checks whether the
first argument is a string matching or not matching the regular
expression provided as the second argument (<a class="reference external" href="http://bugs.python.org/issue8038">issue 8038</a>).</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRaisesRegexp" title="unittest.TestCase.assertRaisesRegexp"><tt class="xref py py-meth docutils literal"><span class="pre">assertRaisesRegexp()</span></tt></a> checks whether a particular exception
is raised, and then also checks that the string representation of
the exception matches the provided regular expression.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIn" title="unittest.TestCase.assertIn"><tt class="xref py py-meth docutils literal"><span class="pre">assertIn()</span></tt></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIn" title="unittest.TestCase.assertNotIn"><tt class="xref py py-meth docutils literal"><span class="pre">assertNotIn()</span></tt></a>
tests whether <em>first</em> is or is not in  <em>second</em>.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertItemsEqual" title="unittest.TestCase.assertItemsEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertItemsEqual()</span></tt></a> tests whether two provided sequences
contain the same elements.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSetEqual" title="unittest.TestCase.assertSetEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertSetEqual()</span></tt></a> compares whether two sets are equal, and
only reports the differences between the sets in case of error.</li>
<li>Similarly, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertListEqual()</span></tt></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTupleEqual" title="unittest.TestCase.assertTupleEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertTupleEqual()</span></tt></a>
compare the specified types and explain any differences without necessarily
printing their full values; these methods are now used by default
when comparing lists and tuples using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></tt></a>.
More generally, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSequenceEqual" title="unittest.TestCase.assertSequenceEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertSequenceEqual()</span></tt></a> compares two sequences
and can optionally check whether both sequences are of a
particular type.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertDictEqual()</span></tt></a> compares two dictionaries and reports the
differences; it&#8217;s now used by default when you compare two dictionaries
using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><tt class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></tt></a>.  <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> checks whether
all of the key/value pairs in <em>first</em> are found in <em>second</em>.</li>
<li><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> and <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> test
whether <em>first</em> and <em>second</em> are approximately equal.  This method
can either round their difference to an optionally-specified number
of <em>places</em> (the default is 7) and compare it to zero, or require
the difference to be smaller than a supplied <em>delta</em> value.</li>
<li><a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromName" title="unittest.TestLoader.loadTestsFromName"><tt class="xref py py-meth docutils literal"><span class="pre">loadTestsFromName()</span></tt></a> properly honors the
<a class="reference internal" href="../library/unittest.html#unittest.TestLoader.suiteClass" title="unittest.TestLoader.suiteClass"><tt class="xref py py-attr docutils literal"><span class="pre">suiteClass</span></tt></a> attribute of
the <a class="reference internal" href="../library/unittest.html#unittest.TestLoader" title="unittest.TestLoader"><tt class="xref py py-class docutils literal"><span class="pre">TestLoader</span></tt></a>. (Fixed by Mark Roddy; <a class="reference external" href="http://bugs.python.org/issue6866">issue 6866</a>.)</li>
<li>A new hook lets you extend the <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> method to handle
new data types.  The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addTypeEqualityFunc" title="unittest.TestCase.addTypeEqualityFunc"><tt class="xref py py-meth docutils literal"><span class="pre">addTypeEqualityFunc()</span></tt></a> method takes a type
object and a function. The function will be used when both of the
objects being compared are of the specified type.  This function
should compare the two objects and raise an exception if they don&#8217;t
match; it&#8217;s a good idea for the function to provide additional
information about why the two objects aren&#8217;t matching, much as the new
sequence comparison methods do.</li>
</ul>
<p><a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><tt class="xref py py-func docutils literal"><span class="pre">unittest.main()</span></tt></a> now takes an optional <tt class="docutils literal"><span class="pre">exit</span></tt> argument.  If
False, <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><tt class="xref py py-func docutils literal"><span class="pre">main()</span></tt></a> doesn&#8217;t call <a class="reference internal" href="../library/sys.html#sys.exit" title="sys.exit"><tt class="xref py py-func docutils literal"><span class="pre">sys.exit()</span></tt></a>, allowing
<a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><tt class="xref py py-func docutils literal"><span class="pre">main()</span></tt></a> to be used from the interactive interpreter.
(Contributed by J. Pablo Fernández; <a class="reference external" href="http://bugs.python.org/issue3379">issue 3379</a>.)</p>
<p><a class="reference internal" href="../library/unittest.html#unittest.TestResult" title="unittest.TestResult"><tt class="xref py py-class docutils literal"><span class="pre">TestResult</span></tt></a> has new <a class="reference internal" href="../library/unittest.html#unittest.TestResult.startTestRun" title="unittest.TestResult.startTestRun"><tt class="xref py py-meth docutils literal"><span class="pre">startTestRun()</span></tt></a> and
<a class="reference internal" href="../library/unittest.html#unittest.TestResult.stopTestRun" title="unittest.TestResult.stopTestRun"><tt class="xref py py-meth docutils literal"><span class="pre">stopTestRun()</span></tt></a> methods that are called immediately before
and after a test run.  (Contributed by Robert Collins; <a class="reference external" href="http://bugs.python.org/issue5728">issue 5728</a>.)</p>
<p>With all these changes, the <tt class="file docutils literal"><span class="pre">unittest.py</span></tt> was becoming awkwardly
large, so the module was turned into a package and the code split into
several files (by Benjamin Peterson).  This doesn&#8217;t affect how the
module is imported or used.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://www.voidspace.org.uk/python/articles/unittest2.shtml">http://www.voidspace.org.uk/python/articles/unittest2.shtml</a></dt>
<dd>Describes the new features, how to use them, and the
rationale for various design decisions.  (By Michael Foord.)</dd>
</dl>
</div>
</div>
<div class="section" id="updated-module-elementtree-1-3">
<span id="elementtree-section"></span><h3>Updated module: ElementTree 1.3<a class="headerlink" href="#updated-module-elementtree-1-3" title="Permalink to this headline">¶</a></h3>
<p>The version of the ElementTree library included with Python was updated to
version 1.3.  Some of the new features are:</p>
<ul>
<li><p class="first">The various parsing functions now take a <em>parser</em> keyword argument
giving an <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><tt class="xref py py-class docutils literal"><span class="pre">XMLParser</span></tt></a> instance that will
be used.  This makes it possible to override the file&#8217;s internal encoding:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">p</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">&quot;&quot;&quot;&lt;root/&gt;&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">parser</span><span class="o">=</span><span class="n">p</span><span class="p">)</span>
</pre></div>
</div>
<p>Errors in parsing XML now raise a <tt class="xref py py-exc docutils literal"><span class="pre">ParseError</span></tt> exception, whose
instances have a <tt class="xref py py-attr docutils literal"><span class="pre">position</span></tt> attribute
containing a (<em>line</em>, <em>column</em>) tuple giving the location of the problem.</p>
</li>
<li><p class="first">ElementTree&#8217;s code for converting trees to a string has been
significantly reworked, making it roughly twice as fast in many
cases.  The <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><tt class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></tt></a>
and <tt class="xref py py-meth docutils literal"><span class="pre">Element.write()</span></tt> methods now have a <em>method</em> parameter that can be
&#8220;xml&#8221; (the default), &#8220;html&#8221;, or &#8220;text&#8221;.  HTML mode will output empty
elements as <tt class="docutils literal"><span class="pre">&lt;empty&gt;&lt;/empty&gt;</span></tt> instead of <tt class="docutils literal"><span class="pre">&lt;empty/&gt;</span></tt>, and text
mode will skip over elements and only output the text chunks.  If
you set the <tt class="xref py py-attr docutils literal"><span class="pre">tag</span></tt> attribute of an element to <tt class="docutils literal"><span class="pre">None</span></tt> but
leave its children in place, the element will be omitted when the
tree is written out, so you don&#8217;t need to do more extensive rearrangement
to remove a single element.</p>
<p>Namespace handling has also been improved.  All <tt class="docutils literal"><span class="pre">xmlns:&lt;whatever&gt;</span></tt>
declarations are now output on the root element, not scattered throughout
the resulting XML.  You can set the default namespace for a tree
by setting the <tt class="xref py py-attr docutils literal"><span class="pre">default_namespace</span></tt> attribute and can
register new prefixes with <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.register_namespace" title="xml.etree.ElementTree.register_namespace"><tt class="xref py py-meth docutils literal"><span class="pre">register_namespace()</span></tt></a>.  In XML mode,
you can use the true/false <em>xml_declaration</em> parameter to suppress the
XML declaration.</p>
</li>
<li><p class="first">New <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt></a> method:
<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">extend()</span></tt></a> appends the items from a
sequence to the element&#8217;s children.  Elements themselves behave like
sequences, so it&#8217;s easy to move children from one element to
another:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">xml.etree</span> <span class="kn">import</span> <span class="n">ElementTree</span> <span class="k">as</span> <span class="n">ET</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">&quot;&quot;&quot;&lt;list&gt;</span>
<span class="s">  &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt;  &lt;item&gt;3&lt;/item&gt;</span>
<span class="s">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
<span class="n">new</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">&#39;&lt;root/&gt;&#39;</span><span class="p">)</span>
<span class="n">new</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="c"># Outputs &lt;root&gt;&lt;item&gt;1&lt;/item&gt;...&lt;/root&gt;</span>
<span class="k">print</span> <span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">New <tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt> method:
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><tt class="xref py py-meth docutils literal"><span class="pre">iter()</span></tt></a> yields the children of the
element as a generator.  It&#8217;s also possible to write <tt class="docutils literal"><span class="pre">for</span> <span class="pre">child</span> <span class="pre">in</span>
<span class="pre">elem:</span></tt> to loop over an element&#8217;s children.  The existing method
<tt class="xref py py-meth docutils literal"><span class="pre">getiterator()</span></tt> is now deprecated, as is <tt class="xref py py-meth docutils literal"><span class="pre">getchildren()</span></tt>
which constructs and returns a list of children.</p>
</li>
<li><p class="first">New <tt class="xref py py-class docutils literal"><span class="pre">Element</span></tt> method:
<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">itertext()</span></tt></a> yields all chunks of
text that are descendants of the element.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s">&quot;&quot;&quot;&lt;list&gt;</span>
<span class="s">  &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt;  &lt;item&gt;3&lt;/item&gt;</span>
<span class="s">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>

<span class="c"># Outputs [&#39;\n  &#39;, &#39;1&#39;, &#39; &#39;, &#39;2&#39;, &#39;  &#39;, &#39;3&#39;, &#39;\n&#39;]</span>
<span class="k">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">itertext</span><span class="p">())</span>
</pre></div>
</div>
</li>
<li><p class="first">Deprecated: using an element as a Boolean (i.e., <tt class="docutils literal"><span class="pre">if</span> <span class="pre">elem:</span></tt>) would
return true if the element had any children, or false if there were
no children.  This behaviour is confusing &#8211; <tt class="docutils literal"><span class="pre">None</span></tt> is false, but
so is a childless element? &#8211; so it will now trigger a
<a class="reference internal" href="../library/exceptions.html#exceptions.FutureWarning" title="exceptions.FutureWarning"><tt class="xref py py-exc docutils literal"><span class="pre">FutureWarning</span></tt></a>.  In your code, you should be explicit: write
<tt class="docutils literal"><span class="pre">len(elem)</span> <span class="pre">!=</span> <span class="pre">0</span></tt> if you&#8217;re interested in the number of children,
or <tt class="docutils literal"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></tt>.</p>
</li>
</ul>
<p>Fredrik Lundh develops ElementTree and produced the 1.3 version;
you can read his article describing 1.3 at
<a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">http://effbot.org/zone/elementtree-13-intro.htm</a>.
Florent Xicluna updated the version included with
Python, after discussions on python-dev and in <a class="reference external" href="http://bugs.python.org/issue6472">issue 6472</a>.)</p>
</div>
</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 latest release of the GNU Debugger, GDB 7, can be <a class="reference external" href="http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html">scripted
using Python</a>.
When you begin debugging an executable program P, GDB will look for
a file named <tt class="docutils literal"><span class="pre">P-gdb.py</span></tt> and automatically read it.  Dave Malcolm
contributed a <tt class="file docutils literal"><span class="pre">python-gdb.py</span></tt> that adds a number of
commands useful when debugging Python itself.  For example,
<tt class="docutils literal"><span class="pre">py-up</span></tt> and <tt class="docutils literal"><span class="pre">py-down</span></tt> go up or down one Python stack frame,
which usually corresponds to several C stack frames.  <tt class="docutils literal"><span class="pre">py-print</span></tt>
prints the value of a Python variable, and <tt class="docutils literal"><span class="pre">py-bt</span></tt> prints the
Python stack trace.  (Added as a result of <a class="reference external" href="http://bugs.python.org/issue8032">issue 8032</a>.)</p>
</li>
<li><p class="first">If you use the <tt class="file docutils literal"><span class="pre">.gdbinit</span></tt> file provided with Python,
the &#8220;pyo&#8221; macro in the 2.7 version now works correctly when the thread being
debugged doesn&#8217;t hold the GIL; the macro now acquires it before printing.
(Contributed by Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue3632">issue 3632</a>.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../c-api/init.html#Py_AddPendingCall" title="Py_AddPendingCall"><tt class="xref c c-func docutils literal"><span class="pre">Py_AddPendingCall()</span></tt></a> is now thread-safe, letting any
worker thread submit notifications to the main Python thread.  This
is particularly useful for asynchronous IO operations.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="http://bugs.python.org/issue4293">issue 4293</a>.)</p>
</li>
<li><p class="first">New function: <a class="reference internal" href="../c-api/code.html#PyCode_NewEmpty" title="PyCode_NewEmpty"><tt class="xref c c-func docutils literal"><span class="pre">PyCode_NewEmpty()</span></tt></a> creates an empty code object;
only the filename, function name, and first line number are required.
This is useful for extension modules that are attempting to
construct a more useful traceback stack.  Previously such
extensions needed to call <a class="reference internal" href="../c-api/code.html#PyCode_New" title="PyCode_New"><tt class="xref c c-func docutils literal"><span class="pre">PyCode_New()</span></tt></a>, which had many
more arguments.  (Added by Jeffrey Yasskin.)</p>
</li>
<li><p class="first">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> creates a new
exception class, just as the existing <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> does,
but takes an extra <tt class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></tt> argument containing the docstring for the
new exception class.  (Added by &#8216;lekma&#8217; on the Python bug tracker;
<a class="reference external" href="http://bugs.python.org/issue7033">issue 7033</a>.)</p>
</li>
<li><p class="first">New function: <a class="reference internal" href="../c-api/reflection.html#PyFrame_GetLineNumber" title="PyFrame_GetLineNumber"><tt class="xref c c-func docutils literal"><span class="pre">PyFrame_GetLineNumber()</span></tt></a> takes a frame object
and returns the line number that the frame is currently executing.
Previously code would need to get the index of the bytecode
instruction currently executing, and then look up the line number
corresponding to that address.  (Added by Jeffrey Yasskin.)</p>
</li>
<li><p class="first">New functions: <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> and
<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>  approximates a Python long
integer as a C <tt class="xref c c-type docutils literal"><span class="pre">long</span></tt> or <tt class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt>.
If the number is too large to fit into
the output type, an <em>overflow</em> flag is set and returned to the caller.
(Contributed by Case Van Horsen; <a class="reference external" href="http://bugs.python.org/issue7528">issue 7528</a> and <a class="reference external" href="http://bugs.python.org/issue7767">issue 7767</a>.)</p>
</li>
<li><p class="first">New function: stemming from the rewrite of string-to-float conversion,
a new <a class="reference internal" href="../c-api/conversion.html#PyOS_string_to_double" title="PyOS_string_to_double"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_string_to_double()</span></tt></a> function was added.  The old
<a class="reference internal" href="../c-api/conversion.html#PyOS_ascii_strtod" title="PyOS_ascii_strtod"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_strtod()</span></tt></a> and <a class="reference internal" href="../c-api/conversion.html#PyOS_ascii_atof" title="PyOS_ascii_atof"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_atof()</span></tt></a> functions
are now deprecated.</p>
</li>
<li><p class="first">New 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> sets the value of
<tt class="docutils literal"><span class="pre">sys.argv</span></tt> and can optionally update <tt class="docutils literal"><span class="pre">sys.path</span></tt> to include the
directory containing the script named by <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> depending
on the value of an <em>updatepath</em> parameter.</p>
<p>This function was added to close a security hole for applications
that embed Python.  The old function, <a class="reference internal" href="../c-api/init.html#PySys_SetArgv" title="PySys_SetArgv"><tt class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></tt></a>, would
always update <tt class="docutils literal"><span class="pre">sys.path</span></tt>, and sometimes it would add the current
directory.  This meant that, if you ran an application embedding
Python in a directory controlled by someone else, attackers could
put a Trojan-horse module in the directory (say, a file named
<tt class="file docutils literal"><span class="pre">os.py</span></tt>) that your application would then import and run.</p>
<p>If you maintain a C/C++ application that embeds Python, check
whether you&#8217;re calling <a class="reference internal" href="../c-api/init.html#PySys_SetArgv" title="PySys_SetArgv"><tt class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></tt></a> and carefully consider
whether the application should be using <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>
with <em>updatepath</em> set to false.</p>
<p>Security issue reported as <a class="reference external" href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983">CVE-2008-5983</a>;
discussed in <a class="reference external" href="http://bugs.python.org/issue5753">issue 5753</a>, and fixed by Antoine Pitrou.</p>
</li>
<li><p class="first">New macros: the Python header files now define the following macros:
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISALNUM</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISALPHA</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISDIGIT</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISLOWER</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISSPACE</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISUPPER</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_ISXDIGIT</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_TOLOWER</span></tt>, and <tt class="xref c c-macro docutils literal"><span class="pre">Py_TOUPPER</span></tt>.
All of these functions are analogous to the C
standard macros for classifying characters, but ignore the current
locale setting, because in
several places Python needs to analyze characters in a
locale-independent way.  (Added by Eric Smith;
<a class="reference external" href="http://bugs.python.org/issue5793">issue 5793</a>.)</p>
</li>
<li><p class="first">Removed function: <tt class="xref c c-macro docutils literal"><span class="pre">PyEval_CallObject</span></tt> is now only available
as a macro.  A function version was being kept around to preserve
ABI linking compatibility, but that was in 1997; it can certainly be
deleted by now.  (Removed by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue8276">issue 8276</a>.)</p>
</li>
<li><p class="first">New format codes: the <tt class="xref c c-func docutils literal"><span class="pre">PyFormat_FromString()</span></tt>,
<tt class="xref c c-func docutils literal"><span class="pre">PyFormat_FromStringV()</span></tt>, and <a class="reference internal" href="../c-api/exceptions.html#PyErr_Format" title="PyErr_Format"><tt class="xref c c-func docutils literal"><span class="pre">PyErr_Format()</span></tt></a> functions now
accept <tt class="docutils literal"><span class="pre">%lld</span></tt> and <tt class="docutils literal"><span class="pre">%llu</span></tt> format codes for displaying
C&#8217;s <tt class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></tt> types.
(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue7228">issue 7228</a>.)</p>
</li>
<li><p class="first">The complicated interaction between threads and process forking has
been changed.  Previously, the child process created by
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><tt class="xref py py-func docutils literal"><span class="pre">os.fork()</span></tt></a> might fail because the child is created with only a
single thread running, the thread performing the <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><tt class="xref py py-func docutils literal"><span class="pre">os.fork()</span></tt></a>.
If other threads were holding a lock, such as Python&#8217;s import lock,
when the fork was performed, the lock would still be marked as
&#8220;held&#8221; in the new process.  But in the child process nothing would
ever release the lock, since the other threads weren&#8217;t replicated,
and the child process would no longer be able to perform imports.</p>
<p>Python 2.7 acquires the import lock before performing an
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><tt class="xref py py-func docutils literal"><span class="pre">os.fork()</span></tt></a>, and will also clean up any locks created using the
<a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module.  C extension modules that have internal
locks, or that call <tt class="xref c c-func docutils literal"><span class="pre">fork()</span></tt> themselves, will not benefit
from this clean-up.</p>
<p>(Fixed by Thomas Wouters; <a class="reference external" href="http://bugs.python.org/issue1590864">issue 1590864</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../c-api/init.html#Py_Finalize" title="Py_Finalize"><tt class="xref c c-func docutils literal"><span class="pre">Py_Finalize()</span></tt></a> function now calls the internal
<tt class="xref py py-func docutils literal"><span class="pre">threading._shutdown()</span></tt> function; this prevents some exceptions from
being raised when an interpreter shuts down.
(Patch by Adam Olsen; <a class="reference external" href="http://bugs.python.org/issue1722344">issue 1722344</a>.)</p>
</li>
<li><p class="first">When using the <a class="reference internal" href="../c-api/structures.html#PyMemberDef" title="PyMemberDef"><tt class="xref c c-type docutils literal"><span class="pre">PyMemberDef</span></tt></a> structure to define attributes
of a type, Python will no longer let you try to delete or set a
<tt class="xref py py-const docutils literal"><span class="pre">T_STRING_INPLACE</span></tt> attribute.</p>
</li>
<li><p class="first">Global symbols defined by the <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> module are now prefixed
with <tt class="docutils literal"><span class="pre">Py</span></tt>, or with <tt class="docutils literal"><span class="pre">_ctypes</span></tt>.  (Implemented by Thomas
Heller; <a class="reference external" href="http://bugs.python.org/issue3102">issue 3102</a>.)</p>
</li>
<li><p class="first">New configure option: the <em class="xref std std-option">--with-system-expat</em> switch allows
building the <tt class="xref py py-mod docutils literal"><span class="pre">pyexpat</span></tt> module to use the system Expat library.
(Contributed by Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="http://bugs.python.org/issue7609">issue 7609</a>.)</p>
</li>
<li><p class="first">New configure option: the
<em class="xref std std-option">--with-valgrind</em> option will now disable the pymalloc
allocator, which is difficult for the Valgrind memory-error detector
to analyze correctly.
Valgrind will therefore be better at detecting memory leaks and
overruns. (Contributed by James Henstridge; <a class="reference external" href="http://bugs.python.org/issue2422">issue 2422</a>.)</p>
</li>
<li><p class="first">New configure option: you can now supply an empty string to
<em class="xref std std-option">--with-dbmliborder=</em> in order to disable all of the various
DBM modules.  (Added by Arfrever Frehtes Taifersar Arahesis;
<a class="reference external" href="http://bugs.python.org/issue6491">issue 6491</a>.)</p>
</li>
<li><p class="first">The <strong class="program">configure</strong> script now checks for floating-point rounding bugs
on certain 32-bit Intel chips and defines a <tt class="xref c c-macro docutils literal"><span class="pre">X87_DOUBLE_ROUNDING</span></tt>
preprocessor definition.  No code currently uses this definition,
but it&#8217;s available if anyone wishes to use it.
(Added by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue2937">issue 2937</a>.)</p>
<p><strong class="program">configure</strong> also now sets a <span class="target" id="index-11"></span><tt class="xref std std-envvar docutils literal"><span class="pre">LDCXXSHARED</span></tt> Makefile
variable for supporting C++ linking.  (Contributed by Arfrever
Frehtes Taifersar Arahesis; <a class="reference external" href="http://bugs.python.org/issue1222585">issue 1222585</a>.)</p>
</li>
<li><p class="first">The build process now creates the necessary files for pkg-config
support.  (Contributed by Clinton Roy; <a class="reference external" href="http://bugs.python.org/issue3585">issue 3585</a>.)</p>
</li>
<li><p class="first">The build process now supports Subversion 1.7.  (Contributed by
Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="http://bugs.python.org/issue6094">issue 6094</a>.)</p>
</li>
</ul>
<div class="section" id="capsules">
<span id="whatsnew27-capsules"></span><h3>Capsules<a class="headerlink" href="#capsules" title="Permalink to this headline">¶</a></h3>
<p>Python 3.1 adds a new C datatype, <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>, for providing a
C API to an extension module.  A capsule is essentially the holder of
a C <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> pointer, and is made available as a module attribute; for
example, the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module&#8217;s API is exposed as <tt class="docutils literal"><span class="pre">socket.CAPI</span></tt>,
and <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><tt class="xref py py-mod docutils literal"><span class="pre">unicodedata</span></tt></a> exposes <tt class="docutils literal"><span class="pre">ucnhash_CAPI</span></tt>.  Other extensions
can import the module, access its dictionary to get the capsule
object, and then get the <tt class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></tt> pointer, which will usually point
to an array of pointers to the module&#8217;s various API functions.</p>
<p>There is an existing data type already used for this,
<a class="reference internal" href="../c-api/cobject.html#PyCObject" title="PyCObject"><tt class="xref c c-type docutils literal"><span class="pre">PyCObject</span></tt></a>, but it doesn&#8217;t provide type safety.  Evil code
written in pure Python could cause a segmentation fault by taking a
<a class="reference internal" href="../c-api/cobject.html#PyCObject" title="PyCObject"><tt class="xref c c-type docutils literal"><span class="pre">PyCObject</span></tt></a> from module A and somehow substituting it for the
<a class="reference internal" href="../c-api/cobject.html#PyCObject" title="PyCObject"><tt class="xref c c-type docutils literal"><span class="pre">PyCObject</span></tt></a> in module B.   Capsules know their own name,
and getting the pointer requires providing the name:</p>
<div class="highlight-python"><pre>void *vtable;

if (!PyCapsule_IsValid(capsule, "mymodule.CAPI") {
        PyErr_SetString(PyExc_ValueError, "argument type invalid");
        return NULL;
}

vtable = PyCapsule_GetPointer(capsule, "mymodule.CAPI");</pre>
</div>
<p>You are assured that <tt class="docutils literal"><span class="pre">vtable</span></tt> points to whatever you&#8217;re expecting.
If a different capsule was passed in, <a class="reference internal" href="../c-api/capsule.html#PyCapsule_IsValid" title="PyCapsule_IsValid"><tt class="xref c c-func docutils literal"><span class="pre">PyCapsule_IsValid()</span></tt></a> would
detect the mismatched name and return false.  Refer to
<a class="reference internal" href="../extending/extending.html#using-capsules"><em>Providing a C API for an Extension Module</em></a> for more information on using these objects.</p>
<p>Python 2.7 now uses capsules internally to provide various
extension-module APIs, but the <a class="reference internal" href="../c-api/cobject.html#PyCObject_AsVoidPtr" title="PyCObject_AsVoidPtr"><tt class="xref c c-func docutils literal"><span class="pre">PyCObject_AsVoidPtr()</span></tt></a> was
modified to handle capsules, preserving compile-time compatibility
with the <tt class="xref c c-type docutils literal"><span class="pre">CObject</span></tt> interface.  Use of
<a class="reference internal" href="../c-api/cobject.html#PyCObject_AsVoidPtr" title="PyCObject_AsVoidPtr"><tt class="xref c c-func docutils literal"><span class="pre">PyCObject_AsVoidPtr()</span></tt></a> will signal a
<a class="reference internal" href="../library/exceptions.html#exceptions.PendingDeprecationWarning" title="exceptions.PendingDeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></tt></a>, which is silent by default.</p>
<p>Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
discussed in <a class="reference external" href="http://bugs.python.org/issue5630">issue 5630</a>.</p>
</div>
<div class="section" id="port-specific-changes-windows">
<h3>Port-Specific Changes: Windows<a class="headerlink" href="#port-specific-changes-windows" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>The <a class="reference internal" href="../library/msvcrt.html#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">msvcrt</span></tt></a> module now contains some constants from
the <tt class="file docutils literal"><span class="pre">crtassem.h</span></tt> header file:
<tt class="xref py py-data docutils literal"><span class="pre">CRT_ASSEMBLY_VERSION</span></tt>,
<tt class="xref py py-data docutils literal"><span class="pre">VC_ASSEMBLY_PUBLICKEYTOKEN</span></tt>,
and <tt class="xref py py-data docutils literal"><span class="pre">LIBRARIES_ASSEMBLY_NAME_PREFIX</span></tt>.
(Contributed by David Cournapeau; <a class="reference external" href="http://bugs.python.org/issue4365">issue 4365</a>.)</li>
<li>The <a class="reference internal" href="../library/_winreg.html#module-_winreg" title="_winreg: Routines and objects for manipulating the Windows registry. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">_winreg</span></tt></a> module for accessing the registry now implements
the <a class="reference internal" href="../library/_winreg.html#_winreg.CreateKeyEx" title="_winreg.CreateKeyEx"><tt class="xref py py-func docutils literal"><span class="pre">CreateKeyEx()</span></tt></a> and <a class="reference internal" href="../library/_winreg.html#_winreg.DeleteKeyEx" title="_winreg.DeleteKeyEx"><tt class="xref py py-func docutils literal"><span class="pre">DeleteKeyEx()</span></tt></a>
functions, extended versions of previously-supported functions that
take several extra arguments.  The <a class="reference internal" href="../library/_winreg.html#_winreg.DisableReflectionKey" title="_winreg.DisableReflectionKey"><tt class="xref py py-func docutils literal"><span class="pre">DisableReflectionKey()</span></tt></a>,
<a class="reference internal" href="../library/_winreg.html#_winreg.EnableReflectionKey" title="_winreg.EnableReflectionKey"><tt class="xref py py-func docutils literal"><span class="pre">EnableReflectionKey()</span></tt></a>, and <a class="reference internal" href="../library/_winreg.html#_winreg.QueryReflectionKey" title="_winreg.QueryReflectionKey"><tt class="xref py py-func docutils literal"><span class="pre">QueryReflectionKey()</span></tt></a>
were also tested and documented.
(Implemented by Brian Curtin: <a class="reference external" href="http://bugs.python.org/issue7347">issue 7347</a>.)</li>
<li>The new <tt class="xref c c-func docutils literal"><span class="pre">_beginthreadex()</span></tt> API is used to start threads, and
the native thread-local storage functions are now used.
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="http://bugs.python.org/issue3582">issue 3582</a>.)</li>
<li>The <a class="reference internal" href="../library/os.html#os.kill" title="os.kill"><tt class="xref py py-func docutils literal"><span class="pre">os.kill()</span></tt></a> function now works on Windows.  The signal value
can be the constants <tt class="xref py py-const docutils literal"><span class="pre">CTRL_C_EVENT</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">CTRL_BREAK_EVENT</span></tt>, or any integer.  The first two constants
will send Control-C and Control-Break keystroke events to
subprocesses; any other value will use the <tt class="xref c c-func docutils literal"><span class="pre">TerminateProcess()</span></tt>
API.  (Contributed by Miki Tebeka; <a class="reference external" href="http://bugs.python.org/issue1220212">issue 1220212</a>.)</li>
<li>The <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><tt class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></tt></a> function now correctly fails
for an empty path.  (Fixed by Hirokazu Yamamoto; <a class="reference external" href="http://bugs.python.org/issue5913">issue 5913</a>.)</li>
<li>The <tt class="xref py py-mod docutils literal"><span class="pre">mimelib</span></tt> module will now read the MIME database from
the Windows registry when initializing.
(Patch by Gabriel Genellina; <a class="reference external" href="http://bugs.python.org/issue4969">issue 4969</a>.)</li>
</ul>
</div>
<div class="section" id="port-specific-changes-mac-os-x">
<h3>Port-Specific Changes: Mac OS X<a class="headerlink" href="#port-specific-changes-mac-os-x" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>The path <tt class="docutils literal"><span class="pre">/Library/Python/2.7/site-packages</span></tt> is now appended to
<tt class="docutils literal"><span class="pre">sys.path</span></tt>, in order to share added packages between the system
installation and a user-installed copy of the same version.
(Changed by Ronald Oussoren; <a class="reference external" href="http://bugs.python.org/issue4865">issue 4865</a>.)</li>
</ul>
</div>
<div class="section" id="port-specific-changes-freebsd">
<h3>Port-Specific Changes: FreeBSD<a class="headerlink" href="#port-specific-changes-freebsd" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>FreeBSD 7.1&#8217;s <tt class="xref py py-const docutils literal"><span class="pre">SO_SETFIB</span></tt> constant, used with
<tt class="xref py py-func docutils literal"><span class="pre">getsockopt()</span></tt>/<tt class="xref py py-func docutils literal"><span class="pre">setsockopt()</span></tt> to select an
alternate routing table, is now available in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a>
module.  (Added by Kyle VanderBeek; <a class="reference external" href="http://bugs.python.org/issue8235">issue 8235</a>.)</li>
</ul>
</div>
</div>
<div class="section" id="other-changes-and-fixes">
<h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>Two benchmark scripts, <tt class="file docutils literal"><span class="pre">iobench</span></tt> and <tt class="file docutils literal"><span class="pre">ccbench</span></tt>, were
added to the <tt class="file docutils literal"><span class="pre">Tools</span></tt> directory.  <tt class="file docutils literal"><span class="pre">iobench</span></tt> measures the
speed of the built-in file I/O objects returned by <a class="reference internal" href="../library/functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a>
while performing various operations, and <tt class="file docutils literal"><span class="pre">ccbench</span></tt> is a
concurrency benchmark that tries to measure computing throughput,
thread switching latency, and IO processing bandwidth when
performing several tasks using a varying number of threads.</li>
<li>The <tt class="file docutils literal"><span class="pre">Tools/i18n/msgfmt.py</span></tt> script now understands plural
forms in <tt class="file docutils literal"><span class="pre">.po</span></tt> files.  (Fixed by Martin von Löwis;
<a class="reference external" href="http://bugs.python.org/issue5464">issue 5464</a>.)</li>
<li>When importing a module from a <tt class="file docutils literal"><span class="pre">.pyc</span></tt> or <tt class="file docutils literal"><span class="pre">.pyo</span></tt> file
with an existing <tt class="file docutils literal"><span class="pre">.py</span></tt> counterpart, the <tt class="xref py py-attr docutils literal"><span class="pre">co_filename</span></tt>
attributes of the resulting code objects are overwritten when the
original filename is obsolete.  This can happen if the file has been
renamed, moved, or is accessed through different paths.  (Patch by
Ziga Seilnacht and Jean-Paul Calderone; <a class="reference external" href="http://bugs.python.org/issue1180193">issue 1180193</a>.)</li>
<li>The <tt class="file docutils literal"><span class="pre">regrtest.py</span></tt> script now takes a <em class="xref std std-option">--randseed=</em>
switch that takes an integer that will be used as the random seed
for the <em class="xref std std-option">-r</em> option that executes tests in random order.
The <em class="xref std std-option">-r</em> option also reports the seed that was used
(Added by Collin Winter.)</li>
<li>Another <tt class="file docutils literal"><span class="pre">regrtest.py</span></tt> switch is <em class="xref std std-option">-j</em>, which
takes an integer specifying how many tests run in parallel. This
allows reducing the total runtime on multi-core machines.
This option is compatible with several other options, including the
<a class="reference internal" href="../using/cmdline.html#cmdoption-R"><em class="xref std std-option">-R</em></a> switch which is known to produce long runtimes.
(Added by Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue6152">issue 6152</a>.)  This can also be used
with a new <em class="xref std std-option">-F</em> switch that runs selected tests in a loop
until they fail.  (Added by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue7312">issue 7312</a>.)</li>
<li>When executed as a script, the <tt class="file docutils literal"><span class="pre">py_compile.py</span></tt> module now
accepts <tt class="docutils literal"><span class="pre">'-'</span></tt> as an argument, which will read standard input for
the list of filenames to be compiled.  (Contributed by Piotr
Ożarowski; <a class="reference external" href="http://bugs.python.org/issue8233">issue 8233</a>.)</li>
</ul>
</div>
<div class="section" id="porting-to-python-2-7">
<h2>Porting to Python 2.7<a class="headerlink" href="#porting-to-python-2-7" 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 class="simple">
<li>The <a class="reference internal" href="../library/functions.html#range" title="range"><tt class="xref py py-func docutils literal"><span class="pre">range()</span></tt></a> function processes its arguments more
consistently; it will now call <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><tt class="xref py py-meth docutils literal"><span class="pre">__int__()</span></tt></a> on non-float,
non-integer arguments that are supplied to it.  (Fixed by Alexander
Belopolsky; <a class="reference external" href="http://bugs.python.org/issue1533">issue 1533</a>.)</li>
<li>The string <a class="reference internal" href="../library/functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> method changed the default precision used
for floating-point and complex numbers from 6 decimal
places to 12, which matches the precision used by <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a>.
(Changed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue5920">issue 5920</a>.)</li>
<li>Because of an optimization for the <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, the special
methods <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> must belong to the object&#8217;s
type, and cannot be directly attached to the object&#8217;s instance.  This
affects new-style classes (derived from <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a>) and C extension
types.  (<a class="reference external" href="http://bugs.python.org/issue6101">issue 6101</a>.)</li>
<li>Due to a bug in Python 2.6, the <em>exc_value</em> parameter to
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> methods was often the string representation of the
exception, not an instance.  This was fixed in 2.7, so <em>exc_value</em>
will be an instance as expected.  (Fixed by Florent Xicluna;
<a class="reference external" href="http://bugs.python.org/issue7853">issue 7853</a>.)</li>
<li>When a restricted set of attributes were set using <tt class="docutils literal"><span class="pre">__slots__</span></tt>,
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a>
as you would expect.  Fixed by Benjamin Peterson; <a class="reference external" href="http://bugs.python.org/issue7604">issue 7604</a>.)</li>
</ul>
<p>In the standard library:</p>
<ul>
<li><p class="first">Operations with <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><tt class="xref py py-class docutils literal"><span class="pre">datetime</span></tt></a> instances that resulted in a year
falling outside the supported range didn&#8217;t always raise
<a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a>.  Such errors are now checked more carefully
and will now raise the exception. (Reported by Mark Leander, patch
by Anand B. Pillai and Alexander Belopolsky; <a class="reference external" href="http://bugs.python.org/issue7150">issue 7150</a>.)</p>
</li>
<li><p class="first">When using <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> instances with a string&#8217;s
<a class="reference internal" href="../library/functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> method, the default alignment was previously
left-alignment.  This has been changed to right-alignment, which might
change the output of your programs.
(Changed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue6857">issue 6857</a>.)</p>
<p>Comparisons involving a signaling NaN value (or <tt class="docutils literal"><span class="pre">sNAN</span></tt>) now signal
<a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><tt class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></tt></a> instead of silently returning a true or
false value depending on the comparison operator.  Quiet NaN values
(or <tt class="docutils literal"><span class="pre">NaN</span></tt>) are now hashable.  (Fixed by Mark Dickinson;
<a class="reference external" href="http://bugs.python.org/issue7279">issue 7279</a>.)</p>
</li>
<li><p class="first">The ElementTree library, <tt class="xref py py-mod docutils literal"><span class="pre">xml.etree</span></tt>, no longer escapes
ampersands and angle brackets when outputting an XML processing
instruction (which looks like <cite>&lt;?xml-stylesheet href=&#8221;#style1&#8221;?&gt;</cite>)
or comment (which looks like <cite>&lt;!&#8211; comment &#8211;&gt;</cite>).
(Patch by Neil Muller; <a class="reference external" href="http://bugs.python.org/issue2746">issue 2746</a>.)</p>
</li>
<li><p class="first">The <tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt> method of <a class="reference internal" href="../library/stringio.html#StringIO.StringIO" title="StringIO.StringIO"><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> objects now does
nothing when a negative length is requested, as other file-like
objects do.  (<a class="reference external" href="http://bugs.python.org/issue7348">issue 7348</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">syslog</span></tt></a> module will now use the value of <tt class="docutils literal"><span class="pre">sys.argv[0]</span></tt> as the
identifier instead of the previous default value of <tt class="docutils literal"><span class="pre">'python'</span></tt>.
(Changed by Sean Reifschneider; <a class="reference external" href="http://bugs.python.org/issue8451">issue 8451</a>.)</p>
</li>
<li><p class="first">The <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> module&#8217;s default error handling has changed, to
no longer suppress fatal errors.  The default error level was previously 0,
which meant that errors would only result in a message being written to the
debug log, but because the debug log is not activated by default,
these errors go unnoticed.  The default error level is now 1,
which raises an exception if there&#8217;s an error.
(Changed by Lars Gustäbel; <a class="reference external" href="http://bugs.python.org/issue7357">issue 7357</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/urlparse.html#module-urlparse" title="urlparse: Parse URLs into or assemble them from components."><tt class="xref py py-mod docutils literal"><span class="pre">urlparse</span></tt></a> module&#8217;s <a class="reference internal" href="../library/urlparse.html#urlparse.urlsplit" title="urlparse.urlsplit"><tt class="xref py py-func docutils literal"><span class="pre">urlsplit()</span></tt></a> now handles
unknown URL schemes in a fashion compliant with <span class="target" id="index-12"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
URL is of the form <tt class="docutils literal"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></tt>, the text before the
<tt class="docutils literal"><span class="pre">://</span></tt> is treated as the scheme, even if it&#8217;s a made-up scheme that
the module doesn&#8217;t know about.  This change may break code that
worked around the old behaviour.  For example, Python 2.6.4 or 2.5
will return the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>Python 2.7 (and Python 2.6.5) will return:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
</pre></div>
</div>
<p>(Python 2.7 actually produces slightly different output, since it
returns a named tuple instead of a standard tuple.)</p>
</li>
</ul>
<p>For C extensions:</p>
<ul class="simple">
<li>C extensions that use integer format codes with the <tt class="docutils literal"><span class="pre">PyArg_Parse*</span></tt>
family of functions will now raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception
instead of triggering a <a class="reference internal" href="../library/exceptions.html#exceptions.DeprecationWarning" title="exceptions.DeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></tt></a> (<a class="reference external" href="http://bugs.python.org/issue5080">issue 5080</a>).</li>
<li>Use the new <a class="reference internal" href="../c-api/conversion.html#PyOS_string_to_double" title="PyOS_string_to_double"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_string_to_double()</span></tt></a> function instead of the old
<a class="reference internal" href="../c-api/conversion.html#PyOS_ascii_strtod" title="PyOS_ascii_strtod"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_strtod()</span></tt></a> and <a class="reference internal" href="../c-api/conversion.html#PyOS_ascii_atof" title="PyOS_ascii_atof"><tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_atof()</span></tt></a> functions,
which are now deprecated.</li>
</ul>
<p>For applications that embed Python:</p>
<ul class="simple">
<li>The <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> function was added, letting
applications close a security hole when the existing
<a class="reference internal" href="../c-api/init.html#PySys_SetArgv" title="PySys_SetArgv"><tt class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></tt></a> function was used.  Check whether you&#8217;re
calling <a class="reference internal" href="../c-api/init.html#PySys_SetArgv" title="PySys_SetArgv"><tt class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></tt></a> and carefully consider whether the
application should be using <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> with
<em>updatepath</em> set to false.</li>
</ul>
</div>
<div class="section" id="acknowledgements">
<span id="acks27"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
<p>The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray,
Hugh Secker-Walker.</p>
</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 2.7</a><ul>
<li><a class="reference internal" href="#the-future-for-python-2-x">The Future for Python 2.x</a></li>
<li><a class="reference internal" href="#python-3-1-features">Python 3.1 Features</a></li>
<li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372: Adding an Ordered Dictionary to collections</a></li>
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: Format Specifier for Thousands Separator</a></li>
<li><a class="reference internal" href="#pep-389-the-argparse-module-for-parsing-command-lines">PEP 389: The argparse Module for Parsing Command Lines</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-3106-dictionary-views">PEP 3106: Dictionary Views</a></li>
<li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: The memoryview Object</a></li>
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
<li><a class="reference internal" href="#interpreter-changes">Interpreter Changes</a></li>
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a><ul>
<li><a class="reference internal" href="#new-module-importlib">New module: importlib</a></li>
<li><a class="reference internal" href="#new-module-sysconfig">New module: sysconfig</a></li>
<li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttk: Themed Widgets for Tk</a></li>
<li><a class="reference internal" href="#updated-module-unittest">Updated module: unittest</a></li>
<li><a class="reference internal" href="#updated-module-elementtree-1-3">Updated module: ElementTree 1.3</a></li>
</ul>
</li>
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
<li><a class="reference internal" href="#capsules">Capsules</a></li>
<li><a class="reference internal" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
<li><a class="reference internal" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
<li><a class="reference internal" href="#port-specific-changes-freebsd">Port-Specific Changes: FreeBSD</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
<li><a class="reference internal" href="#porting-to-python-2-7">Porting to Python 2.7</a></li>
<li><a class="reference internal" href="#acknowledgements">Acknowledgements</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="2.6.html"
                        title="next chapter">What&#8217;s New in Python 2.6</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/2.7.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" />
      <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="2.6.html" title="What’s New in Python 2.6"
             >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="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 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-2020, 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 Oct 13, 2020.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

  </body>
</html>

ZeroDay Forums Mini