Sophie

Sophie

distrib > Mandriva > 2010.1 > i586 > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 916

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



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

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

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="3.0.html" title="What’s New In Python 3.0"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="3.2.html" title="What’s New In Python 3.2"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

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

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="what-s-new-in-python-3-1">
<h1>What&#8217;s New In Python 3.1<a class="headerlink" href="#what-s-new-in-python-3-1" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">Raymond Hettinger</td>
</tr>
<tr class="field"><th class="field-name">Release:</th><td class="field-body">3.2.2</td>
</tr>
<tr class="field"><th class="field-name">Date:</th><td class="field-body">September 04, 2011</td>
</tr>
</tbody>
</table>
<p>This article explains the new features in Python 3.1, compared to 3.0.</p>
<div class="section" id="pep-372-ordered-dictionaries">
<h2>PEP 372: Ordered Dictionaries<a class="headerlink" href="#pep-372-ordered-dictionaries" 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, 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">collections.OrderedDict</span></tt></a> class has been introduced.</p>
<p>The OrderedDict API is substantially the same as regular dictionaries
but will iterate over keys and values in a guaranteed order depending on
when a key was first inserted.  If a new entry overwrites an existing entry,
the original insertion position is left unchanged.  Deleting an entry and
reinserting it will move it to the end.</p>
<p>The standard library now supports use of ordered dictionaries in several
modules.  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.  This lets
configuration files be read, modified, and then written back in their original
order.  The <em>_asdict()</em> 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 indicies.  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 is being built-out with
an <em>object_pairs_hook</em> to allow OrderedDicts to be built by the decoder.
Support was also added for third-party tools like <a class="reference external" href="http://pyyaml.org/">PyYAML</a>.</p>
<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-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0372"><strong>PEP 372</strong></a> - Ordered Dictionaries</dt>
<dd>PEP written by Armin Ronacher and Raymond Hettinger.  Implementation
written by Raymond Hettinger.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-378-format-specifier-for-thousands-separator">
<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>The built-in <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> function and 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 use
a mini-language that now includes a simple, non-locale aware way to format
a number with a thousands separator.  That provides a way to humanize a
program&#8217;s output, improving its professional appearance and readability:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">1234567</span><span class="p">,</span> <span class="s">&#39;,d&#39;</span><span class="p">)</span>
<span class="go">&#39;1,234,567&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mf">1234567.89</span><span class="p">,</span> <span class="s">&#39;,.2f&#39;</span><span class="p">)</span>
<span class="go">&#39;1,234,567.89&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mf">12345.6</span> <span class="o">+</span> <span class="mf">8901234.12</span><span class="n">j</span><span class="p">,</span> <span class="s">&#39;,f&#39;</span><span class="p">)</span>
<span class="go">&#39;12,345.600000+8,901,234.120000j&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;1234567.89&#39;</span><span class="p">),</span> <span class="s">&#39;,f&#39;</span><span class="p">)</span>
<span class="go">&#39;1,234,567.89&#39;</span>
</pre></div>
</div>
<p>The supported types are <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>, <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>, <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>
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.Decimal</span></tt></a>.</p>
<p>Discussions are underway about how to specify alternative separators
like dots, spaces, apostrophes, or underscores.  Locale-aware applications
should use the existing <em>n</em> format specifier which already has some support
for thousands separators.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a> - Format Specifier for Thousands Separator</dt>
<dd>PEP written by Raymond Hettinger and implemented by Eric Smith and
Mark Dickinson.</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">Directories and zip archives containing a <tt class="file docutils literal"><span class="pre">__main__.py</span></tt>
file can now be executed directly by passing their name to the
interpreter. The directory/zipfile is automatically inserted as the
first entry in sys.path.  (Suggestion and initial patch by Andy Chu;
revised patch by Phillip J. Eby and Nick Coghlan; <a class="reference external" href="http://bugs.python.org/issue1739468">issue 1739468</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> type 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="mi">37</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="mi">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="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, Victor Stinner, Raymond Hettinger,
and Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue3439">issue 3439</a>.)</p>
</li>
<li><p class="first">The fields in <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> strings can now be automatically
numbered:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Sir {} of {}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Gallahad&#39;</span><span class="p">,</span> <span class="s">&#39;Camelot&#39;</span><span class="p">)</span>
<span class="go">&#39;Sir Gallahad of Camelot&#39;</span>
</pre></div>
</div>
<p>Formerly, the string would have required numbered fields such as:
<tt class="docutils literal"><span class="pre">'Sir</span> <span class="pre">{0}</span> <span class="pre">of</span> <span class="pre">{1}'</span></tt>.</p>
<p>(Contributed by Eric Smith; <a class="reference external" href="http://bugs.python.org/issue5237">issue 5237</a>.)</p>
</li>
<li><p class="first">The <tt class="xref py py-func docutils literal"><span class="pre">string.maketrans()</span></tt> function is deprecated and is replaced by new
static methods, <a class="reference internal" href="../library/stdtypes.html#bytes.maketrans" title="bytes.maketrans"><tt class="xref py py-meth docutils literal"><span class="pre">bytes.maketrans()</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#bytearray.maketrans" title="bytearray.maketrans"><tt class="xref py py-meth docutils literal"><span class="pre">bytearray.maketrans()</span></tt></a>.
This change solves the confusion around which types were supported by the
<a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module. Now, <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>, <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>, and
<a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> each have their own <strong>maketrans</strong> and <strong>translate</strong>
methods with intermediate translation tables of the appropriate type.</p>
<p>(Contributed by Georg Brandl; <a class="reference external" href="http://bugs.python.org/issue5675">issue 5675</a>.)</p>
</li>
<li><p class="first">The syntax of 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 allows multiple context
managers in a single statement:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;mylog.txt&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">infile</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;a.out&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">infile</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="s">&#39;&lt;critical&gt;&#39;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
<span class="gp">... </span>            <span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<p>With the new syntax, the <tt class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></tt> function is no longer
needed and is now deprecated.</p>
<p>(Contributed by Georg Brandl and Mattias Brändström;
<a class="reference external" href="http://codereview.appspot.com/53094">appspot issue 53094</a>.)</p>
</li>
<li><p class="first"><tt class="docutils literal"><span class="pre">round(x,</span> <span class="pre">n)</span></tt> now returns an integer if <em>x</em> is an integer.
Previously it returned a float:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">round</span><span class="p">(</span><span class="mi">1123</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1100</span>
</pre></div>
</div>
<p>(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue4707">issue 4707</a>.)</p>
</li>
<li><p class="first">Python now uses David Gay&#8217;s algorithm for finding the shortest floating
point representation that doesn&#8217;t change its value.  This should help
mitigate some of the confusion surrounding binary floating point
numbers.</p>
<p>The significance is easily seen with a number like <tt class="docutils literal"><span class="pre">1.1</span></tt> which does not
have an exact equivalent in binary floating point.  Since there is no exact
equivalent, an expression like <tt class="docutils literal"><span class="pre">float('1.1')</span></tt> evaluates to the nearest
representable value which is <tt class="docutils literal"><span class="pre">0x1.199999999999ap+0</span></tt> in hex or
<tt class="docutils literal"><span class="pre">1.100000000000000088817841970012523233890533447265625</span></tt> in decimal. That
nearest value was and still is used in subsequent floating point
calculations.</p>
<p>What is new is how the number gets displayed.  Formerly, Python used a
simple approach.  The value of <tt class="docutils literal"><span class="pre">repr(1.1)</span></tt> was computed as <tt class="docutils literal"><span class="pre">format(1.1,</span>
<span class="pre">'.17g')</span></tt> which evaluated to <tt class="docutils literal"><span class="pre">'1.1000000000000001'</span></tt>. The advantage of
using 17 digits was that it relied on IEEE-754 guarantees to assure that
<tt class="docutils literal"><span class="pre">eval(repr(1.1))</span></tt> would round-trip exactly to its original value.  The
disadvantage is that many people found the output to be confusing (mistaking
intrinsic limitations of binary floating point representation as being a
problem with Python itself).</p>
<p>The new algorithm for <tt class="docutils literal"><span class="pre">repr(1.1)</span></tt> is smarter and returns <tt class="docutils literal"><span class="pre">'1.1'</span></tt>.
Effectively, it searches all equivalent string representations (ones that
get stored with the same underlying float value) and returns the shortest
representation.</p>
<p>The new algorithm tends to emit cleaner representations when possible, but
it does not change the underlying values.  So, it is still the case that
<tt class="docutils literal"><span class="pre">1.1</span> <span class="pre">+</span> <span class="pre">2.2</span> <span class="pre">!=</span> <span class="pre">3.3</span></tt> even though the representations may suggest otherwise.</p>
<p>The new algorithm depends on certain features in the underlying floating
point implementation.  If the required features are not found, the old
algorithm will continue to be used.  Also, the text pickle protocols
assure cross-platform portability by using the old algorithm.</p>
<p>(Contributed by Eric Smith and Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue1580">issue 1580</a>)</p>
</li>
</ul>
</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>
<ul>
<li><p class="first">Added a <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><tt class="xref py py-class docutils literal"><span class="pre">collections.Counter</span></tt></a> class to support convenient
counting of unique items in a sequence or iterable:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Counter</span><span class="p">([</span><span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="s">&#39;red&#39;</span><span class="p">,</span> <span class="s">&#39;green&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">,</span> <span class="s">&#39;blue&#39;</span><span class="p">])</span>
<span class="go">Counter({&#39;blue&#39;: 3, &#39;red&#39;: 2, &#39;green&#39;: 1})</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1696199">issue 1696199</a>.)</p>
</li>
<li><p class="first">Added a new module, <a class="reference internal" href="../library/tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><tt class="xref py py-mod docutils literal"><span class="pre">tkinter.ttk</span></tt></a> for access to the Tk themed widget set.
The basic idea of ttk is to separate, to the extent possible, the code
implementing a widget&#8217;s behavior from the code implementing its appearance.</p>
<p>(Contributed by Guilherme Polo; <a class="reference external" href="http://bugs.python.org/issue2983">issue 2983</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><tt class="xref py py-class docutils literal"><span class="pre">gzip.GzipFile</span></tt></a> and <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">bz2.BZ2File</span></tt></a> classes now support
the context manager protocol:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Automatically close file after writing</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">GzipFile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b</span><span class="s">&quot;xxx&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>(Contributed by Antoine Pitrou.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module now supports methods for creating a
decimal object from a binary <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>.  The conversion is
exact but can sometimes be surprising:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="o">.</span><span class="n">from_float</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
<span class="go">Decimal(&#39;1.100000000000000088817841970012523233890533447265625&#39;)</span>
</pre></div>
</div>
<p>The long decimal result shows the actual binary fraction being
stored for <em>1.1</em>.  The fraction has many digits because <em>1.1</em> cannot
be exactly represented in binary.</p>
<p>(Contributed by Raymond Hettinger and Mark Dickinson.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> module grew two new functions.  The
<a class="reference internal" href="../library/itertools.html#itertools.combinations_with_replacement" title="itertools.combinations_with_replacement"><tt class="xref py py-func docutils literal"><span class="pre">itertools.combinations_with_replacement()</span></tt></a> function is one of
four for generating combinatorics including permutations and Cartesian
products.  The <a class="reference internal" href="../library/itertools.html#itertools.compress" title="itertools.compress"><tt class="xref py py-func docutils literal"><span class="pre">itertools.compress()</span></tt></a> function mimics its namesake
from APL.  Also, the existing <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
an optional <em>step</em> argument and can accept any type of counting
sequence including <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><tt class="xref py py-class docutils literal"><span class="pre">fractions.Fraction</span></tt></a> 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.Decimal</span></tt></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">p</span><span class="o">+</span><span class="n">q</span> <span class="k">for</span> <span class="n">p</span><span class="p">,</span><span class="n">q</span> <span class="ow">in</span> <span class="n">combinations_with_replacement</span><span class="p">(</span><span class="s">&#39;LOVE&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)]</span>
<span class="go">[&#39;LL&#39;, &#39;LO&#39;, &#39;LV&#39;, &#39;LE&#39;, &#39;OO&#39;, &#39;OV&#39;, &#39;OE&#39;, &#39;VV&#39;, &#39;VE&#39;, &#39;EE&#39;]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">compress</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">selectors</span><span class="o">=</span><span class="p">[</span><span class="mi">0</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="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">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]))</span>
<span class="go">[2, 3, 5, 7]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">count</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="n">Fraction</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="n">step</span><span class="o">=</span><span class="n">Fraction</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">6</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">),</span> <span class="nb">next</span><span class="p">(</span><span class="n">c</span><span class="p">)]</span>
<span class="go">[Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first"><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 supports a keyword argument
<em>rename</em> which lets invalid fieldnames be automatically converted to
positional names in the form _0, _1, etc.  This is useful when
the field names are being created by an external source such as a
CSV header, SQL field list, or user input:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">query</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
<span class="go">SELECT region, dept, count(*) FROM main GROUPBY region, dept</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">query_fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">desc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">desc</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">description</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">UserQuery</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;UserQuery&#39;</span><span class="p">,</span> <span class="n">query_fields</span><span class="p">,</span> <span class="n">rename</span><span class="o">=</span><span class="k">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">([</span><span class="n">UserQuery</span><span class="p">(</span><span class="o">*</span><span class="n">row</span><span class="p">)</span> <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="p">])</span>
<span class="go">[UserQuery(region=&#39;South&#39;, dept=&#39;Shipping&#39;, _2=185),</span>
<span class="go"> UserQuery(region=&#39;North&#39;, dept=&#39;Accounting&#39;, _2=37),</span>
<span class="go"> UserQuery(region=&#39;West&#39;, dept=&#39;Sales&#39;, _2=419)]</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1818">issue 1818</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">re.sub()</span></tt></a>, <a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><tt class="xref py py-func docutils literal"><span class="pre">re.subn()</span></tt></a> and <a class="reference internal" href="../library/re.html#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">re.split()</span></tt></a> functions now
accept a flags parameter.</p>
<p>(Contributed by Gregory Smith.)</p>
</li>
<li><p class="first">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 now implements a simple <a class="reference internal" href="../library/logging.handlers.html#logging.NullHandler" title="logging.NullHandler"><tt class="xref py py-class docutils literal"><span class="pre">logging.NullHandler</span></tt></a>
class for applications that are not using logging but are calling
library code that does.  Setting-up a null handler will suppress
spurious warnings such as &#8220;No handlers could be found for logger foo&#8221;:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">NullHandler</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&quot;foo&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">h</span><span class="p">)</span>
</pre></div>
</div>
<p>(Contributed by Vinay Sajip; <a class="reference external" href="http://bugs.python.org/issue4384">issue 4384</a>).</p>
</li>
<li><p class="first">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 which supports the <tt class="docutils literal"><span class="pre">-m</span></tt> command line switch
now supports the execution of packages by looking for and executing
a <tt class="docutils literal"><span class="pre">__main__</span></tt> submodule when a package name is supplied.</p>
<p>(Contributed by Andi Vajda; <a class="reference external" href="http://bugs.python.org/issue4195">issue 4195</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> module can now access and display source code loaded via
<a class="reference internal" href="../library/zipimport.html#module-zipimport" title="zipimport: support for importing Python modules from ZIP archives."><tt class="xref py py-mod docutils literal"><span class="pre">zipimport</span></tt></a> (or any other conformant <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> loader).</p>
<p>(Contributed by Alexander Belopolsky; <a class="reference external" href="http://bugs.python.org/issue4201">issue 4201</a>.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/functools.html#functools.partial" title="functools.partial"><tt class="xref py py-class docutils literal"><span class="pre">functools.partial</span></tt></a> objects can now be pickled.</p>
</li>
</ul>
<blockquote>
<div>(Suggested by Antoine Pitrou and Jesse Noller.  Implemented by
Jack Diederich; <a class="reference external" href="http://bugs.python.org/issue5228">issue 5228</a>.)</div></blockquote>
<ul>
<li><p class="first">Add <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> help topics for symbols so that <tt class="docutils literal"><span class="pre">help('&#64;')</span></tt>
works as expected in the interactive environment.</p>
<p>(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/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 now supports skipping individual tests or classes
of tests. And it supports marking a test as a expected failure, a test that
is known to be broken, but shouldn&#8217;t be counted as a failure on a
TestResult:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">TestGizmo</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>

    <span class="nd">@unittest</span><span class="o">.</span><span class="n">skipUnless</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;win&quot;</span><span class="p">),</span> <span class="s">&quot;requires Windows&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">test_gizmo_on_windows</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="o">...</span>

    <span class="nd">@unittest</span><span class="o">.</span><span class="n">expectedFailure</span>
    <span class="k">def</span> <span class="nf">test_gimzo_without_required_library</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>Also, tests for exceptions have been builtout to work with context managers
using 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:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test_division_by_zero</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">/</span> <span class="mi">0</span>
</pre></div>
</div>
<p>In addition, several new assertion methods were added including
<tt class="xref py py-func docutils literal"><span class="pre">assertSetEqual()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">assertDictEqual()</span></tt>,
<tt class="xref py py-func docutils literal"><span class="pre">assertDictContainsSubset()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">assertListEqual()</span></tt>,
<tt class="xref py py-func docutils literal"><span class="pre">assertTupleEqual()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">assertSequenceEqual()</span></tt>,
<tt class="xref py py-func docutils literal"><span class="pre">assertRaisesRegexp()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">assertIsNone()</span></tt>,
and <tt class="xref py py-func docutils literal"><span class="pre">assertIsNotNone()</span></tt>.</p>
<p>(Contributed by Benjamin Peterson and Antoine Pitrou.)</p>
</li>
<li><p class="first">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> module has three new constants for the <tt class="xref py py-meth docutils literal"><span class="pre">seek()</span></tt>
method <tt class="xref py py-data docutils literal"><span class="pre">SEEK_SET</span></tt>, <tt class="xref py py-data docutils literal"><span class="pre">SEEK_CUR</span></tt>, and <tt class="xref py py-data docutils literal"><span class="pre">SEEK_END</span></tt>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><tt class="xref py py-attr docutils literal"><span class="pre">sys.version_info</span></tt></a> tuple is now a named tuple:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span>
<span class="go">sys.version_info(major=3, minor=1, micro=0, releaselevel=&#39;alpha&#39;, serial=2)</span>
</pre></div>
</div>
<p>(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 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> and <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> modules now support IPv6.</p>
<p>(Contributed by Derek Morr; <a class="reference external" href="http://bugs.python.org/issue1655">issue 1655</a> and <a class="reference external" href="http://bugs.python.org/issue1664">issue 1664</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> module has been adapted for better interoperability with
Python 2.x when used with protocol 2 or lower.  The reorganization of the
standard library changed the formal reference for many objects.  For
example, <tt class="docutils literal"><span class="pre">__builtin__.set</span></tt> in Python 2 is called <tt class="docutils literal"><span class="pre">builtins.set</span></tt> in Python
3. This change confounded efforts to share data between different versions of
Python.  But now when protocol 2 or lower is selected, the pickler will
automatically use the old Python 2 names for both loading and dumping. This
remapping is turned-on by default but can be disabled with the <em>fix_imports</em>
option:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="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="gp">&gt;&gt;&gt; </span><span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go">b&#39;c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pickle</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">protocol</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">fix_imports</span><span class="o">=</span><span class="k">False</span><span class="p">)</span>
<span class="go">b&#39;cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.&#39;</span>
</pre></div>
</div>
<p>An unfortunate but unavoidable side-effect of this change is that protocol 2
pickles produced by Python 3.1 won&#8217;t be readable with Python 3.0. The latest
pickle protocol, protocol 3, should be used when migrating data between
Python 3.x implementations, as it doesn&#8217;t attempt to remain compatible with
Python 2.x.</p>
<p>(Contributed by Alexandre Vassalotti and Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue6137">issue 6137</a>.)</p>
</li>
<li><p class="first">A new module, <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: An implementation of the import machinery."><tt class="xref py py-mod docutils literal"><span class="pre">importlib</span></tt></a> was added.  It provides a complete, portable,
pure Python reference implementation of 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 and its
counterpart, the <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a> function.  It represents a substantial
step forward in documenting and defining the actions that take place during
imports.</p>
<p>(Contributed by Brett Cannon.)</p>
</li>
</ul>
</div>
<div class="section" id="optimizations">
<h2>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h2>
<p>Major performance enhancements have been added:</p>
<ul>
<li><p class="first">The new I/O library (as defined in <span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3116"><strong>PEP 3116</strong></a>) was mostly written in
Python and quickly proved to be a problematic bottleneck in Python 3.0.
In Python 3.1, the I/O library has been entirely rewritten in C and is
2 to 20 times faster depending on the task at hand. The pure Python
version is still available for experimentation purposes through
the <tt class="docutils literal"><span class="pre">_pyio</span></tt> module.</p>
<p>(Contributed by Amaury Forgeot d&#8217;Arc and Antoine Pitrou.)</p>
</li>
<li><p class="first">Added a heuristic so that tuples and dicts containing only untrackable objects
are not tracked by the garbage collector. This can reduce the size of
collections and therefore the garbage collection overhead on long-running
programs, depending on their particular use of datatypes.</p>
<p>(Contributed by Antoine Pitrou, <a class="reference external" href="http://bugs.python.org/issue4688">issue 4688</a>.)</p>
</li>
<li><p class="first">Enabling a configure option named <tt class="docutils literal"><span class="pre">--with-computed-gotos</span></tt>
on compilers that support it (notably: gcc, SunPro, icc), the bytecode
evaluation loop is compiled with a new dispatch mechanism which gives
speedups of up to 20%, depending on the system, the compiler, and
the benchmark.</p>
<p>(Contributed by Antoine Pitrou along with a number of other participants,
<a class="reference external" href="http://bugs.python.org/issue4753">issue 4753</a>).</p>
</li>
<li><p class="first">The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times
faster.</p>
<p>(Contributed by Antoine Pitrou and Amaury Forgeot d&#8217;Arc, <a class="reference external" href="http://bugs.python.org/issue4868">issue 4868</a>.)</p>
</li>
<li><p class="first">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 now has a C extension to substantially improve
its performance.  In addition, the API was modified so that json works
only with <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>, not with <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>.  That change makes the
module closely match the <a class="reference external" href="http://json.org/">JSON specification</a>
which is defined in terms of Unicode.</p>
<p>(Contributed by Bob Ippolito and converted to Py3.1 by Antoine Pitrou
and Benjamin Peterson; <a class="reference external" href="http://bugs.python.org/issue4136">issue 4136</a>.)</p>
</li>
<li><p class="first">Unpickling now interns the attribute names of pickled objects.  This saves
memory and allows pickles to be smaller.</p>
<p>(Contributed by Jake McGuire and Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue5084">issue 5084</a>.)</p>
</li>
</ul>
</div>
<div class="section" id="idle">
<h2>IDLE<a class="headerlink" href="#idle" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">IDLE&#8217;s format menu now provides an option to strip trailing whitespace
from a source file.</p>
<p>(Contributed by Roger D. Serwy; <a class="reference external" href="http://bugs.python.org/issue5150">issue 5150</a>.)</p>
</li>
</ul>
</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">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
<tt class="docutils literal"><span class="pre">--enable-big-digits</span></tt> 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 <a class="reference internal" href="../library/sys.html#sys.int_info" title="sys.int_info"><tt class="xref py py-attr docutils literal"><span class="pre">sys.int_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-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">int_info</span>
<span class="go">sys.int_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>
</li>
<li><p class="first">The <a class="reference internal" href="../c-api/long.html#PyLong_AsUnsignedLongLong" title="PyLong_AsUnsignedLongLong"><tt class="xref c c-func docutils literal"><span class="pre">PyLong_AsUnsignedLongLong()</span></tt></a> function now handles a negative
<em>pylong</em> by raising <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> instead of <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.</p>
<p>(Contributed by Mark Dickinson and Lisandro Dalcrin; <a class="reference external" href="http://bugs.python.org/issue5175">issue 5175</a>.)</p>
</li>
<li><p class="first">Deprecated <tt class="xref c c-func docutils literal"><span class="pre">PyNumber_Int()</span></tt>.  Use <a class="reference internal" href="../c-api/number.html#PyNumber_Long" title="PyNumber_Long"><tt class="xref c c-func docutils literal"><span class="pre">PyNumber_Long()</span></tt></a> instead.</p>
<p>(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue4910">issue 4910</a>.)</p>
</li>
<li><p class="first">Added 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 to replace the
deprecated functions <tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_strtod()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_atof()</span></tt>.</p>
<p>(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue5914">issue 5914</a>.)</p>
</li>
<li><p class="first">Added <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> as a replacement for the <tt class="xref c c-type docutils literal"><span class="pre">PyCObject</span></tt> API.
The principal difference is that the new type has a well defined interface
for passing typing safety information and a less complicated signature
for calling a destructor.  The old type had a problematic API and is now
deprecated.</p>
<p>(Contributed by Larry Hastings; <a class="reference external" href="http://bugs.python.org/issue5630">issue 5630</a>.)</p>
</li>
</ul>
</div>
<div class="section" id="porting-to-python-3-1">
<h2>Porting to Python 3.1<a class="headerlink" href="#porting-to-python-3-1" title="Permalink to this headline">¶</a></h2>
<p>This section lists previously described changes and other bugfixes
that may require changes to your code:</p>
<ul>
<li><p class="first">The new floating point string representations can break existing doctests.
For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">e</span><span class="p">():</span>
    <span class="sd">&#39;&#39;&#39;Compute the base of natural logarithms.</span>

<span class="sd">    &gt;&gt;&gt; e()</span>
<span class="sd">    2.7182818284590451</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="mi">1</span><span class="o">/</span><span class="n">math</span><span class="o">.</span><span class="n">factorial</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">30</span><span class="p">)))</span>

<span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>

<span class="o">**********************************************************************</span>
<span class="n">Failed</span> <span class="n">example</span><span class="p">:</span>
    <span class="n">e</span><span class="p">()</span>
<span class="n">Expected</span><span class="p">:</span>
    <span class="mf">2.7182818284590451</span>
<span class="n">Got</span><span class="p">:</span>
    <span class="mf">2.718281828459045</span>
<span class="o">**********************************************************************</span>
</pre></div>
</div>
</li>
<li><p class="first">The automatic name remapping in the pickle module for protocol 2 or lower can
make Python 3.1 pickles unreadable in Python 3.0.  One solution is to use
protocol 3.  Another solution is to set the <em>fix_imports</em> option to <strong>False</strong>.
See the discussion above for more details.</p>
</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">What&#8217;s New In Python 3.1</a><ul>
<li><a class="reference internal" href="#pep-372-ordered-dictionaries">PEP 372: Ordered Dictionaries</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="#other-language-changes">Other Language Changes</a></li>
<li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a></li>
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
<li><a class="reference internal" href="#idle">IDLE</a></li>
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a></li>
<li><a class="reference internal" href="#porting-to-python-3-1">Porting to Python 3.1</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="3.2.html"
                        title="previous chapter">What&#8217;s New In Python 3.2</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="3.0.html"
                        title="next chapter">What&#8217;s New In Python 3.0</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/whatsnew/3.1.txt"
         rel="nofollow">Show Source</a></li>
</ul>

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

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

  </body>
</html>