Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > e870e6598e1c7e3918555a3d0ba5f3d4 > files > 862

python3-docs-3.1.1-2mdv2010.0.i586.rpm

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

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

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">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="3.0.html" title="What’s New In Python 3.0"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 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"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling (amk at amk.ca)</td>
</tr>
<tr class="field"><th class="field-name">Release:</th><td class="field-body">3.1.1</td>
</tr>
<tr class="field"><th class="field-name">Date:</th><td class="field-body">August 16, 2009</td>
</tr>
</tbody>
</table>
<p>This article explains the new features in Python 2.7.
No release schedule has been decided yet for 2.7.</p>
<div class="section" id="python-3-1">
<h2>Python 3.1<a class="headerlink" href="#python-3-1" title="Permalink to this headline">¶</a></h2>
<p>Much as Python 2.6 incorporated features from Python 3.0,
version 2.7 is influenced by features from 3.1.</p>
<p>XXX mention importlib; anything else?</p>
<p>One porting change: the <em class="xref">-3</em> switch now automatically
enables the <em class="xref">-Qwarn</em> switch that causes warnings
about using classic division with integers and long integers.</p>
</div>
<div class="section" id="pep-372-adding-an-ordered-dictionary-to-collections">
<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>XXX write this</p>
<p>Several modules will now use <tt class="xref docutils literal"><span class="pre">OrderedDict</span></tt> by default.  The
<tt class="xref docutils literal"><span class="pre">ConfigParser</span></tt> module uses <tt class="xref docutils literal"><span class="pre">OrderedDict</span></tt> for the list
of sections and the options within a section.
The <tt class="xref docutils literal"><span class="pre">namedtuple._asdict()</span></tt> method returns an <tt class="xref docutils literal"><span class="pre">OrderedDict</span></tt>
as well.</p>
</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"><a title="str.format" class="reference external" href="../library/stdtypes.html#str.format"><tt class="xref docutils literal"><span class="pre">str.format()</span></tt></a> method now supports automatic numbering of the replacement
fields.  This makes using <a title="str.format" class="reference external" href="../library/stdtypes.html#str.format"><tt class="xref 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-python3"><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="mf">2009</span><span class="p">,</span> <span class="mf">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="mf">2009</span><span class="p">,</span> <span class="mf">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 title="str.format" class="reference external" href="../library/stdtypes.html#str.format"><tt class="xref 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; :issue`5237`.)</p>
</li>
<li><p class="first">The <a title="int" class="reference external" href="../library/functions.html#int"><tt class="xref docutils literal"><span class="pre">int()</span></tt></a> and <tt class="xref docutils literal"><span class="pre">long()</span></tt> 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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mf">37</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bin</span><span class="p">(</span><span class="mf">37</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="mf">2</span><span class="o">**</span><span class="mf">123</span><span class="o">-</span><span class="mf">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="mf">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">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 will now approximate more
closely.  For example, Python 2.6 computed the following:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mf">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-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mf">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="gp">... </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>
</li>
<li><p class="first">The <a title="bytearray" class="reference external" href="../library/functions.html#bytearray"><tt class="xref docutils literal"><span class="pre">bytearray</span></tt></a> type&#8217;s <tt class="xref docutils literal"><span class="pre">translate()</span></tt> method will
now accept <tt class="xref 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>
</ul>
<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">The garbage collector now performs better when many objects are
being allocated without deallocating any.  A full garbage collection
pass is only performed 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.  The second condition was added to reduce the number
of full garbage collections as the number of objects on the heap grows,
avoiding quadratic performance when allocating very many objects.
(Suggested by Martin von Loewis 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">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">--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 <tt class="docutils literal"><span class="pre">sys.long_info</span></tt> that
provides information about the internal format, giving the number of
bits per digit and the size in bytes of the C type used to store
each digit:</p>
<div class="highlight-python3"><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>.)</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>
</ul>
</div>
</div>
<div class="section" id="new-improved-and-deprecated-modules">
<h2>New, Improved, and Deprecated Modules<a class="headerlink" href="#new-improved-and-deprecated-modules" title="Permalink to this headline">¶</a></h2>
<p>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="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 title="Interface to compression and decompression routines compatible with bzip2." class="reference external" href="../library/bz2.html#module-bz2"><tt class="xref docutils literal"><span class="pre">bz2</span></tt></a> module&#8217;s <tt class="xref docutils literal"><span class="pre">BZ2File</span></tt> 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> <span class="pre">...</span></tt>.
(Contributed by Hagen Fuerstenau; <a class="reference external" href="http://bugs.python.org/issue3860">issue 3860</a>.)</p>
</li>
<li><p class="first">New class: the <tt class="xref docutils literal"><span class="pre">Counter</span></tt> class in the <a title="Container datatypes" class="reference external" href="../library/collections.html#module-collections"><tt class="xref docutils literal"><span class="pre">collections</span></tt></a> module is
useful for tallying data.  <tt class="xref docutils literal"><span class="pre">Counter</span></tt> instances behave mostly
like dictionaries but return zero for missing keys instead of
raising a <a title="exceptions.KeyError" class="reference external" href="../library/exceptions.html#exceptions.KeyError"><tt class="xref docutils literal"><span class="pre">KeyError</span></tt></a>:</p>
<div class="highlight-python3"><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="mf">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 two additional <tt class="xref docutils literal"><span class="pre">Counter</span></tt> methods: <tt class="xref docutils literal"><span class="pre">most_common()</span></tt>
returns the N most common elements and their counts, and <tt class="xref docutils literal"><span class="pre">elements()</span></tt>
returns an iterator over the contained element, repeating each element
as many times as its count:</p>
<div class="highlight-python3"><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="mf">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>
</pre></div>
</div>
<p>Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1696199">issue 1696199</a>.</p>
<p>The <tt class="xref docutils literal"><span class="pre">namedtuple</span></tt> 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 that 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-python3"><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>The <tt class="xref docutils literal"><span class="pre">deque</span></tt> data type now exposes its maximum length as the
read-only <tt class="xref docutils literal"><span class="pre">maxlen</span></tt> attribute.  (Added by Raymond Hettinger.)</p>
</li>
<li><p class="first">In Distutils, <tt class="xref docutils literal"><span class="pre">distutils.sdist.add_defaults()</span></tt> now uses
<em>package_dir</em> and <em>data_files</em> to create the MANIFEST file.
<a title="Low-level access to configuration information of the Python interpreter." class="reference external" href="../distutils/apiref.html#module-distutils.sysconfig"><tt class="xref docutils literal"><span class="pre">distutils.sysconfig</span></tt></a> will now read the <span class="target" id="index-1143"></span><strong class="xref">AR</strong>
environment variable.</p>
<p>It is no longer mandatory to store clear-text passwords in the
<tt class="docutils literal"><span class="pre">.pypirc</span></tt> file when registering and uploading packages to PyPI. As long
as the username is present in that file, the <a title="Support for building and installing Python modules into an existing Python installation." class="reference external" href="../library/distutils.html#module-distutils"><tt class="xref docutils literal"><span class="pre">distutils</span></tt></a> package will
prompt for the password if not present.  (Added by Tarek Ziade,
based on an initial contribution by Nathan Van Gheem; <a class="reference external" href="http://bugs.python.org/issue4394">issue 4394</a>.)</p>
<p>A Distutils setup can now specify that a C extension is optional by
setting the <em>optional</em> option setting to true.  If this optional is
supplied, failure to build the extension will not abort the build
process, but instead simply not install the failing extension.
(Contributed by Georg Brandl; <a class="reference external" href="http://bugs.python.org/issue5583">issue 5583</a>.)</p>
</li>
<li><p class="first">New method: the <tt class="xref docutils literal"><span class="pre">Decimal</span></tt> class gained a
<tt class="xref docutils literal"><span class="pre">from_float()</span></tt> class method that performs an exact conversion
of a floating-point number to a <tt class="xref docutils literal"><span class="pre">Decimal</span></tt>.
Note that this is an <strong>exact</strong> conversion that 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>
</li>
<li><p class="first">The <tt class="xref docutils literal"><span class="pre">Fraction</span></tt> class will now accept two rational numbers
as arguments to its constructor.
(Implemented by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue5812">issue 5812</a>.)</p>
</li>
<li><p class="first">New function: the <a title="Interface to the cycle-detecting garbage collector." class="reference external" href="../library/gc.html#module-gc"><tt class="xref docutils literal"><span class="pre">gc</span></tt></a> module&#8217;s <tt class="xref docutils literal"><span class="pre">is_tracked()</span></tt> 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 title="Interfaces for gzip compression and decompression using file objects." class="reference external" href="../library/gzip.html#module-gzip"><tt class="xref docutils literal"><span class="pre">gzip</span></tt></a> module&#8217;s <tt class="xref docutils literal"><span class="pre">GzipFile</span></tt> 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> <span class="pre">...</span></tt>.
(Contributed by Hagen Fuerstenau; <a class="reference external" href="http://bugs.python.org/issue3860">issue 3860</a>.)
It&#8217;s 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>
</li>
<li><p class="first">The <a title="io.FileIO" class="reference external" href="../library/io.html#io.FileIO"><tt class="xref docutils literal"><span class="pre">io.FileIO</span></tt></a> class now raises an <a title="exceptions.OSError" class="reference external" href="../library/exceptions.html#exceptions.OSError"><tt class="xref 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>.)</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-python3"><div class="highlight"><pre><span class="n">itertools</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="s">&#39;ABCDEF&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">])</span> <span class="o">=&gt;</span>
  <span class="n">A</span><span class="p">,</span> <span class="n">C</span><span class="p">,</span> <span class="n">E</span><span class="p">,</span> <span class="n">F</span>
</pre></div>
</div>
<p>New function: <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 <tt class="xref docutils literal"><span class="pre">combinations()</span></tt>, individual elements
can be repeated in the generated combinations:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">itertools</span><span class="o">.</span><span class="n">combinations_with_replacement</span><span class="p">(</span><span class="s">&#39;abc&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">=&gt;</span>
  <span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;a&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">),</span>
  <span class="p">(</span><span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;b&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;c&#39;</span><span class="p">,</span> <span class="s">&#39;c&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that elements are treated as unique depending on their position
in the input, not their actual values.</p>
<p>The <a title="itertools.count" class="reference external" href="../library/itertools.html#itertools.count"><tt class="xref 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.  <tt class="xref docutils literal"><span class="pre">count()</span></tt> also
now allows keyword arguments, and using non-integer values such as
floats or <tt class="xref docutils literal"><span class="pre">Decimal</span></tt> instances.  (Implemented by Raymond
Hettinger; <a class="reference external" href="http://bugs.python.org/issue5032">issue 5032</a>.)</p>
<p><a title="itertools.combinations" class="reference external" href="../library/itertools.html#itertools.combinations"><tt class="xref docutils literal"><span class="pre">itertools.combinations()</span></tt></a> and <a title="itertools.product" class="reference external" href="../library/itertools.html#itertools.product"><tt class="xref docutils literal"><span class="pre">itertools.product()</span></tt></a> were
previously raising <a title="exceptions.ValueError" class="reference external" href="../library/exceptions.html#exceptions.ValueError"><tt class="xref 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">The <a title="Encode and decode the JSON format." class="reference external" href="../library/json.html#module-json"><tt class="xref 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 <tt class="xref docutils literal"><span class="pre">OrderedDict</span></tt> type, <a title="json.load" class="reference external" href="../library/json.html#json.load"><tt class="xref 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 title="Process-based &quot;threading&quot; interface." class="reference external" href="../library/multiprocessing.html#module-multiprocessing"><tt class="xref docutils literal"><span class="pre">multiprocessing</span></tt></a> module&#8217;s <tt class="xref 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>
</li>
<li><p class="first">The <a title="Documentation generator and online help system." class="reference external" href="../library/pydoc.html#module-pydoc"><tt class="xref 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 title="Regular expression operations." class="reference external" href="../library/re.html#module-re"><tt class="xref docutils literal"><span class="pre">re</span></tt></a> module&#8217;s <tt class="xref docutils literal"><span class="pre">split()</span></tt>, <tt class="xref docutils literal"><span class="pre">sub()</span></tt>, and <tt class="xref docutils literal"><span class="pre">subn()</span></tt>
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: the <a title="Subprocess management." class="reference external" href="../library/subprocess.html#module-subprocess"><tt class="xref docutils literal"><span class="pre">subprocess</span></tt></a> module&#8217;s
<tt class="xref docutils literal"><span class="pre">check_output()</span></tt> 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 <tt class="xref docutils literal"><span class="pre">CalledProcessError</span></tt> exception otherwise.</p>
<div class="highlight-python3"><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>
</li>
<li><p class="first">New function: <tt class="xref docutils literal"><span class="pre">is_declared_global()</span></tt> in the <a title="Interface to the compiler's internal symbol tables." class="reference external" href="../library/symtable.html#module-symtable"><tt class="xref 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 <tt class="docutils literal"><span class="pre">sys.version_info</span></tt> value is now a named tuple, with attributes
named <tt class="docutils literal"><span class="pre">major</span></tt>, <tt class="docutils literal"><span class="pre">minor</span></tt>, <tt class="docutils literal"><span class="pre">micro</span></tt>, <tt class="docutils literal"><span class="pre">releaselevel</span></tt>, and <tt class="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>
</li>
<li><p class="first">The <a title="Higher-level threading interface." class="reference external" href="../library/threading.html#module-threading"><tt class="xref docutils literal"><span class="pre">threading</span></tt></a> module&#8217;s <tt class="xref docutils literal"><span class="pre">Event.wait()</span></tt> method now returns
the internal flag on exit.  This means the method will usually
return true because <tt class="xref docutils literal"><span class="pre">wait()</span></tt> 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 XXX; <a class="reference external" href="http://bugs.python.org/issue1674032">issue 1674032</a>.)</p>
</li>
<li><p class="first">The <a title="Unit testing framework for Python." class="reference external" href="../library/unittest.html#module-unittest"><tt class="xref docutils literal"><span class="pre">unittest</span></tt></a> module was enhanced in several ways.
The progress messages will now show &#8216;x&#8217; for expected failures
and &#8216;u&#8217; for unexpected successes when run in verbose mode.
(Contributed by Benjamin Peterson.)
Test cases can raise the <tt class="xref docutils literal"><span class="pre">SkipTest</span></tt> exception to skip a test.
(<a class="reference external" href="http://bugs.python.org/issue1034053">issue 1034053</a>.)</p>
<p>The error messages for <tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt>,
<tt class="xref docutils literal"><span class="pre">assertTrue()</span></tt>, and <tt class="xref docutils literal"><span class="pre">assertFalse()</span></tt>
failures now provide more information.  If you set the
<tt class="xref docutils literal"><span class="pre">longMessage</span></tt> attribute of your <tt class="xref docutils literal"><span class="pre">TestCase</span></tt> 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 <tt class="xref docutils literal"><span class="pre">assertRaises()</span></tt> and <tt class="xref docutils literal"><span class="pre">failUnlessRaises()</span></tt> methods now
return a context handler when called without providing a callable
object to run.  For example, you can write this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">KeyError</span><span class="p">):</span>
    <span class="k">raise</span> <span class="ne">ValueError</span>
</pre></div>
</div>
<p>(Implemented by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue4444">issue 4444</a>.)</p>
<p>The methods <tt class="xref docutils literal"><span class="pre">addCleanup()</span></tt> and <tt class="xref docutils literal"><span class="pre">doCleanups()</span></tt> were added.
<tt class="xref docutils literal"><span class="pre">addCleanup()</span></tt> allows you to add cleanup functions that
will be called unconditionally (after <tt class="xref docutils literal"><span class="pre">setUp()</span></tt> if
<tt class="xref docutils literal"><span class="pre">setUp()</span></tt> fails, otherwise after <tt class="xref docutils literal"><span class="pre">tearDown()</span></tt>). 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 title="Unit testing framework for Python." class="reference external" href="../library/unittest.html#module-unittest"><tt class="xref docutils literal"><span class="pre">unittest</span></tt></a>.</p>
<ul class="simple">
<li><tt class="xref docutils literal"><span class="pre">assertIsNone()</span></tt> and <tt class="xref docutils literal"><span class="pre">assertIsNotNone()</span></tt> take one
expression and verify that the result is or is not <tt class="xref docutils literal"><span class="pre">None</span></tt>.</li>
<li><tt class="xref docutils literal"><span class="pre">assertIs()</span></tt> and <tt class="xref docutils literal"><span class="pre">assertIsNot()</span></tt> 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><tt class="xref docutils literal"><span class="pre">assertGreater()</span></tt>, <tt class="xref docutils literal"><span class="pre">assertGreaterEqual()</span></tt>,
<tt class="xref docutils literal"><span class="pre">assertLess()</span></tt>, and <tt class="xref docutils literal"><span class="pre">assertLessEqual()</span></tt> compare
two quantities.</li>
<li><tt class="xref docutils literal"><span class="pre">assertMultiLineEqual()</span></tt> compares two strings, and if they&#8217;re
not equal, displays a helpful comparison that highlights the
differences in the two strings.</li>
<li><tt class="xref docutils literal"><span class="pre">assertRegexpMatches()</span></tt> checks whether its first argument is a
string matching a regular expression provided as its second argument.</li>
<li><tt class="xref docutils literal"><span class="pre">assertRaisesRegexp()</span></tt> 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><tt class="xref docutils literal"><span class="pre">assertIn()</span></tt> and <tt class="xref docutils literal"><span class="pre">assertNotIn()</span></tt> tests whether
<em>first</em> is or is not in  <em>second</em>.</li>
<li><tt class="xref docutils literal"><span class="pre">assertSameElements()</span></tt> tests whether two provided sequences
contain the same elements.</li>
<li><tt class="xref docutils literal"><span class="pre">assertSetEqual()</span></tt> compares whether two sets are equal, and
only reports the differences between the sets in case of error.</li>
<li>Similarly, <tt class="xref docutils literal"><span class="pre">assertListEqual()</span></tt> and <tt class="xref docutils literal"><span class="pre">assertTupleEqual()</span></tt>
compare the specified types and explain the differences.
More generally, <tt class="xref docutils literal"><span class="pre">assertSequenceEqual()</span></tt> compares two sequences
and can optionally check whether both sequences are of a
particular type.</li>
<li><tt class="xref docutils literal"><span class="pre">assertDictEqual()</span></tt> compares two dictionaries and reports the
differences.  <tt class="xref docutils literal"><span class="pre">assertDictContainsSubset()</span></tt> checks whether
all of the key/value pairs in <em>first</em> are found in <em>second</em>.</li>
<li>A new hook, <tt class="xref docutils literal"><span class="pre">addTypeEqualityFunc()</span></tt> takes a type object and a
function.  The <tt class="xref docutils literal"><span class="pre">assertEqual()</span></tt> method will use the function
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 are
matching, much as the new sequence comparison methods do.</li>
</ul>
<p><a title="unittest.main" class="reference external" href="../library/unittest.html#unittest.main"><tt class="xref 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 <tt class="docutils literal"><span class="pre">main</span></tt> doesn&#8217;t call <a title="sys.exit" class="reference external" href="../library/sys.html#sys.exit"><tt class="xref docutils literal"><span class="pre">sys.exit()</span></tt></a> allowing it to
be used from the interactive interpreter. <a class="reference external" href="http://bugs.python.org/issue3379">issue 3379</a>.</p>
<p><tt class="xref docutils literal"><span class="pre">TestResult</span></tt> has new <tt class="xref docutils literal"><span class="pre">startTestRun()</span></tt> and
<tt class="xref docutils literal"><span class="pre">stopTestRun()</span></tt> methods; called immediately before
and after a test run. <a class="reference external" href="http://bugs.python.org/issue5728">issue 5728</a> by Robert Collins.</p>
</li>
<li><p class="first">The <tt class="xref docutils literal"><span class="pre">is_zipfile()</span></tt> function in the <a title="Read and write ZIP-format archive files." class="reference external" href="../library/zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a> module will now
accept 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><a title="Read and write ZIP-format archive files." class="reference external" href="../library/zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a> now 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>.)</p>
</li>
</ul>
<div class="section" id="importlib-importing-modules">
<h3>importlib: Importing Modules<a class="headerlink" href="#importlib-importing-modules" title="Permalink to this headline">¶</a></h3>
<p>Python 3.1 includes the <a title="An implementation of the import machinery." class="reference external" href="../library/importlib.html#module-importlib"><tt class="xref docutils literal"><span class="pre">importlib</span></tt></a> package, a re-implementation
of the logic underlying Python&#8217;s <a class="reference external" href="../reference/simple_stmts.html#import"><tt class="xref docutils literal"><span class="pre">import</span></tt></a> statement.
<a title="An implementation of the import machinery." class="reference external" href="../library/importlib.html#module-importlib"><tt class="xref docutils literal"><span class="pre">importlib</span></tt></a> is useful for implementors of Python interpreters and
to user who wish to write new importers that can participate in the
import process.  Python 2.7 doesn&#8217;t contain the complete
<a title="An implementation of the import machinery." class="reference external" href="../library/importlib.html#module-importlib"><tt class="xref docutils literal"><span class="pre">importlib</span></tt></a> package, but instead has a tiny subset that contains
a single function, <tt class="xref docutils literal"><span class="pre">import_module()</span></tt>.</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.  <tt class="xref docutils literal"><span class="pre">import_module()</span></tt> 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-python3"><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">sysconfig</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s">&#39;..sysconfig&#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">sysconfig</span>
<span class="go">&lt;module &#39;distutils.sysconfig&#39; from &#39;/p/python/Lib/distutils/sysconfig.pyc&#39;&gt;</span>
</pre></div>
</div>
<p><a title="An implementation of the import machinery." class="reference external" href="../library/importlib.html#module-importlib"><tt class="xref 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="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>XXX write a brief discussion and an example here.</p>
<p>The <tt class="xref docutils literal"><span class="pre">ttk</span></tt> 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>
<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 class="simple">
<li>If you use the <tt class="docutils literal"><span class="pre">.gdbinit</span></tt> file provided with Python,
the &#8220;pyo&#8221; macro in the 2.7 version will now work when the thread being
debugged doesn&#8217;t hold the GIL; the macro will now acquire it before printing.
(Contributed by Victor Stinner; <a class="reference external" href="http://bugs.python.org/issue3632">issue 3632</a>.)</li>
<li><a title="Py_AddPendingCall" class="reference external" href="../c-api/init.html#Py_AddPendingCall"><tt class="xref 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 Kristjan Valur Jonsson; <a class="reference external" href="http://bugs.python.org/issue4293">issue 4293</a>.)</li>
<li>Global symbols defined by the <a title="A foreign function library for Python." class="reference external" href="../library/ctypes.html#module-ctypes"><tt class="xref docutils literal"><span class="pre">ctypes</span></tt></a> module are now prefixed
with <tt class="docutils literal"><span class="pre">Py`,</span> <span class="pre">or</span> <span class="pre">with</span> <span class="pre">``_ctypes</span></tt>.  (Implemented by Thomas
Heller; <a class="reference external" href="http://bugs.python.org/issue3102">issue 3102</a>.)</li>
<li>The <strong>configure</strong> script now checks for floating-point rounding bugs
on certain 32-bit Intel chips and defines a <tt class="xref 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>.)</li>
</ul>
<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 title="(Windows) Miscellaneous useful routines from the MS VC++ runtime." class="reference external" href="../library/msvcrt.html#module-msvcrt"><tt class="xref docutils literal"><span class="pre">msvcrt</span></tt></a> module now contains some constants from
the <tt class="docutils literal"><span class="pre">crtassem.h</span></tt> header file:
<tt class="xref docutils literal"><span class="pre">CRT_ASSEMBLY_VERSION</span></tt>,
<tt class="xref docutils literal"><span class="pre">VC_ASSEMBLY_PUBLICKEYTOKEN</span></tt>,
and <tt class="xref 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 new <tt class="xref 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 Kristjan Valur Jonsson; <a class="reference external" href="http://bugs.python.org/issue3582">issue 3582</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 <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>
<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>When importing a module from a <tt class="docutils literal"><span class="pre">.pyc</span></tt> or <tt class="docutils literal"><span class="pre">.pyo</span></tt> file
with an existing <tt class="docutils literal"><span class="pre">.py</span></tt> counterpart, the <tt class="xref 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="docutils literal"><span class="pre">regrtest.py</span></tt> script now takes a <em class="xref">--randseed=</em>
switch that takes an integer that will be used as the random seed
for the <em class="xref">-r</em> option that executes tests in random order.
The <em class="xref">-r</em> option also now reports the seed that was used
(Added by Collin Winter.)</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>Because of an optimization for the <a class="reference external" href="../reference/compound_stmts.html#with"><tt class="xref docutils literal"><span class="pre">with</span></tt></a> statement, the special
methods <a title="object.__enter__" class="reference external" href="../reference/datamodel.html#object.__enter__"><tt class="xref docutils literal"><span class="pre">__enter__()</span></tt></a> and <a title="object.__exit__" class="reference external" href="../reference/datamodel.html#object.__exit__"><tt class="xref 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 title="object" class="reference external" href="../library/functions.html#object"><tt class="xref 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>
</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: no one yet.</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 external" href="">What&#8217;s New in Python 2.7</a><ul>
<li><a class="reference external" href="#python-3-1">Python 3.1</a></li>
<li><a class="reference external" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372: Adding an ordered dictionary to collections</a></li>
<li><a class="reference external" href="#other-language-changes">Other Language Changes</a><ul>
<li><a class="reference external" href="#optimizations">Optimizations</a></li>
</ul>
</li>
<li><a class="reference external" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
<li><a class="reference external" href="#importlib-importing-modules">importlib: Importing Modules</a></li>
<li><a class="reference external" href="#ttk-themed-widgets-for-tk">ttk: Themed Widgets for Tk</a></li>
</ul>
</li>
<li><a class="reference external" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
<li><a class="reference external" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
<li><a class="reference external" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
</ul>
</li>
<li><a class="reference external" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
<li><a class="reference external" href="#porting-to-python-2-7">Porting to Python 2.7</a></li>
<li><a class="reference external" href="#acknowledgements">Acknowledgements</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="3.0.html"
                                  title="previous chapter">What&#8217;s New In Python 3.0</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="../_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" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global 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="3.0.html" title="What’s New In Python 3.0"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.1.1 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-2009, 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 Aug 16, 2009.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>

  </body>
</html>