~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-3.5.1-docs-html/whatsnew/2.7.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>What’s New in Python 2.7 &mdash; Python 3.5.1 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '3.5.1',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
23
 
    <script type="text/javascript" src="../_static/jquery.js"></script>
24
 
    <script type="text/javascript" src="../_static/underscore.js"></script>
25
 
    <script type="text/javascript" src="../_static/doctools.js"></script>
26
 
    <script type="text/javascript" src="../_static/sidebar.js"></script>
27
 
    <link rel="search" type="application/opensearchdescription+xml"
28
 
          title="Search within Python 3.5.1 documentation"
29
 
          href="../_static/opensearch.xml"/>
30
 
    <link rel="author" title="About these documents" href="../about.html" />
31
 
    <link rel="copyright" title="Copyright" href="../copyright.html" />
32
 
    <link rel="top" title="Python 3.5.1 documentation" href="../contents.html" />
33
 
    <link rel="up" title="What’s New in Python" href="index.html" />
34
 
    <link rel="next" title="What’s New in Python 2.6" href="2.6.html" />
35
 
    <link rel="prev" title="What’s New In Python 3.0" href="3.0.html" />
36
 
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
 
    <script type="text/javascript" src="../_static/copybutton.js"></script>
38
 
    <script type="text/javascript" src="../_static/version_switch.js"></script>
39
 
    
40
 
 
41
 
 
42
 
  </head>
43
 
  <body role="document">  
44
 
    <div class="related" role="navigation" aria-label="related navigation">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="2.6.html" title="What’s New in Python 2.6"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
57
 
          <a href="3.0.html" title="What’s New In Python 3.0"
58
 
             accesskey="P">previous</a> |</li>
59
 
        <li><img src="../_static/py.png" alt=""
60
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
61
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">3.5.1</span>
64
 
          <a href="../index.html">Documentation </a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What&#8217;s New in Python</a> &raquo;</li> 
68
 
      </ul>
69
 
    </div>    
70
 
 
71
 
    <div class="document">
72
 
      <div class="documentwrapper">
73
 
        <div class="bodywrapper">
74
 
          <div class="body" role="main">
75
 
            
76
 
  <div class="section" id="what-s-new-in-python-2-7">
77
 
<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>
78
 
<table class="docutils field-list" frame="void" rules="none">
79
 
<col class="field-name" />
80
 
<col class="field-body" />
81
 
<tbody valign="top">
82
 
<tr class="field-odd field"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling (amk at amk.ca)</td>
83
 
</tr>
84
 
</tbody>
85
 
</table>
86
 
<p>This article explains the new features in Python 2.7.  Python 2.7 was released
87
 
on July 3, 2010.</p>
88
 
<p>Numeric handling has been improved in many ways, for both
89
 
floating-point numbers and for the <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> class.
90
 
There are some useful additions to the standard library, such as a
91
 
greatly enhanced <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> module, the <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a> module
92
 
for parsing command-line options, convenient <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a>
93
 
and <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal"><span class="pre">Counter</span></code></a> classes in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a> module,
94
 
and many other improvements.</p>
95
 
<p>Python 2.7 is planned to be the last of the 2.x releases, so we worked
96
 
on making it a good release for the long term.  To help with porting
97
 
to Python 3, several new features from the Python 3.x series have been
98
 
included in 2.7.</p>
99
 
<p>This article doesn&#8217;t attempt to provide a complete specification of
100
 
the new features, but instead provides a convenient overview.  For
101
 
full details, you should refer to the documentation for Python 2.7 at
102
 
<a class="reference external" href="https://docs.python.org">https://docs.python.org</a>. If you want to understand the rationale for
103
 
the design and implementation, refer to the PEP for a particular new
104
 
feature or the issue on <a class="reference external" href="https://bugs.python.org">https://bugs.python.org</a> in which a change was
105
 
discussed.  Whenever possible, &#8220;What&#8217;s New in Python&#8221; links to the
106
 
bug/patch item for each change.</p>
107
 
<div class="section" id="the-future-for-python-2-x">
108
 
<span id="whatsnew27-python31"></span><h2>The Future for Python 2.x<a class="headerlink" href="#the-future-for-python-2-x" title="Permalink to this headline">¶</a></h2>
109
 
<p>Python 2.7 is the last major release in the 2.x series, as the Python
110
 
maintainers have shifted the focus of their new feature development efforts
111
 
to the Python 3.x series. This means that while Python 2 continues to
112
 
receive bug fixes, and to be updated to build correctly on new hardware and
113
 
versions of supported operated systems, there will be no new full feature
114
 
releases for the language or standard library.</p>
115
 
<p>However, while there is a large common subset between Python 2.7 and Python
116
 
3, and many of the changes involved in migrating to that common subset, or
117
 
directly to Python 3, can be safely automated, some other changes (notably
118
 
those associated with Unicode handling) may require careful consideration,
119
 
and preferably robust automated regression test suites, to migrate
120
 
effectively.</p>
121
 
<p>This means that Python 2.7 will remain in place for a long time, providing a
122
 
stable and supported base platform for production systems that have not yet
123
 
been ported to Python 3. The full expected lifecycle of the Python 2.7
124
 
series is detailed in <span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0373"><strong>PEP 373</strong></a>.</p>
125
 
<p>Some key consequences of the long-term significance of 2.7 are:</p>
126
 
<ul class="simple">
127
 
<li>As noted above, the 2.7 release has a much longer period of maintenance
128
 
when compared to earlier 2.x versions. Python 2.7 is currently expected to
129
 
remain supported by the core development team (receiving security updates
130
 
and other bug fixes) until at least 2020 (10 years after its initial
131
 
release, compared to the more typical support period of 18-24 months).</li>
132
 
<li>As the Python 2.7 standard library ages, making effective use of the
133
 
Python Package Index (either directly or via a redistributor) becomes
134
 
more important for Python 2 users. In addition to a wide variety of third
135
 
party packages for various tasks, the available packages include backports
136
 
of new modules and features from the Python 3 standard library that are
137
 
compatible with Python 2, as well as various tools and libraries that can
138
 
make it easier to migrate to Python 3. The <a class="reference external" href="https://packaging.python.org">Python Packaging User Guide</a> provides guidance on downloading and
139
 
installing software from the Python Package Index.</li>
140
 
<li>While the preferred approach to enhancing Python 2 is now the publication
141
 
of new packages on the Python Package Index, this approach doesn&#8217;t
142
 
necessarily work in all cases, especially those related to network
143
 
security. In exceptional cases that cannot be handled adequately by
144
 
publishing new or updated packages on PyPI, the Python Enhancement
145
 
Proposal process may be used to make the case for adding new features
146
 
directly to the Python 2 standard library. Any such additions, and the
147
 
maintenance releases where they were added, will be noted in the
148
 
<a class="reference internal" href="#py27-maintenance-enhancements"><span>New Features Added to Python 2.7 Maintenance Releases</span></a> section below.</li>
149
 
</ul>
150
 
<p>For projects wishing to migrate from Python 2 to Python 3, or for library
151
 
and framework developers wishing to support users on both Python 2 and
152
 
Python 3, there are a variety of tools and guides available to help decide
153
 
on a suitable approach and manage some of the technical details involved.
154
 
The recommended starting point is the <a class="reference internal" href="../howto/pyporting.html#pyporting-howto"><span>Porting Python 2 Code to Python 3</span></a> HOWTO guide.</p>
155
 
</div>
156
 
<div class="section" id="changes-to-the-handling-of-deprecation-warnings">
157
 
<h2>Changes to the Handling of Deprecation Warnings<a class="headerlink" href="#changes-to-the-handling-of-deprecation-warnings" title="Permalink to this headline">¶</a></h2>
158
 
<p>For Python 2.7, a policy decision was made to silence warnings only of
159
 
interest to developers by default.  <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> and its
160
 
descendants are now ignored unless otherwise requested, preventing
161
 
users from seeing warnings triggered by an application.  This change
162
 
was also made in the branch that became Python 3.2. (Discussed
163
 
on stdlib-sig and carried out in <a class="reference external" href="https://bugs.python.org/issue7319">issue 7319</a>.)</p>
164
 
<p>In previous releases, <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> messages were
165
 
enabled by default, providing Python developers with a clear
166
 
indication of where their code may break in a future major version
167
 
of Python.</p>
168
 
<p>However, there are increasingly many users of Python-based
169
 
applications who are not directly involved in the development of
170
 
those applications.  <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> messages are
171
 
irrelevant to such users, making them worry about an application
172
 
that&#8217;s actually working correctly and burdening application developers
173
 
with responding to these concerns.</p>
174
 
<p>You can re-enable display of <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> messages by
175
 
running Python with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal"><span class="pre">-Wdefault</span></code></a> (short form:
176
 
<a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal"><span class="pre">-Wd</span></code></a>) switch, or by setting the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONWARNINGS</span></code></a>
177
 
environment variable to <code class="docutils literal"><span class="pre">&quot;default&quot;</span></code> (or <code class="docutils literal"><span class="pre">&quot;d&quot;</span></code>) before running
178
 
Python.  Python code can also re-enable them
179
 
by calling <code class="docutils literal"><span class="pre">warnings.simplefilter('default')</span></code>.</p>
180
 
<p>The <code class="docutils literal"><span class="pre">unittest</span></code> module also automatically reenables deprecation warnings
181
 
when running tests.</p>
182
 
</div>
183
 
<div class="section" id="python-3-1-features">
184
 
<h2>Python 3.1 Features<a class="headerlink" href="#python-3-1-features" title="Permalink to this headline">¶</a></h2>
185
 
<p>Much as Python 2.6 incorporated features from Python 3.0,
186
 
version 2.7 incorporates some of the new features
187
 
in Python 3.1.  The 2.x series continues to provide tools
188
 
for migrating to the 3.x series.</p>
189
 
<p>A partial list of 3.1 features that were backported to 2.7:</p>
190
 
<ul class="simple">
191
 
<li>The syntax for set literals (<code class="docutils literal"><span class="pre">{1,2,3}</span></code> is a mutable set).</li>
192
 
<li>Dictionary and set comprehensions (<code class="docutils literal"><span class="pre">{i:</span> <span class="pre">i*2</span> <span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">range(3)}</span></code>).</li>
193
 
<li>Multiple context managers in a single <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement.</li>
194
 
<li>A new version of the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal"><span class="pre">io</span></code></a> library, rewritten in C for performance.</li>
195
 
<li>The ordered-dictionary type described in <a class="reference internal" href="#pep-0372"><span>PEP 372: Adding an Ordered Dictionary to collections</span></a>.</li>
196
 
<li>The new <code class="docutils literal"><span class="pre">&quot;,&quot;</span></code> format specifier described in <a class="reference internal" href="#pep-0378"><span>PEP 378: Format Specifier for Thousands Separator</span></a>.</li>
197
 
<li>The <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal"><span class="pre">memoryview</span></code></a> object.</li>
198
 
<li>A small subset of the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> module,
199
 
<a class="reference external" href="#importlib-section">described below</a>.</li>
200
 
<li>The <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> of a float <code class="docutils literal"><span class="pre">x</span></code> is shorter in many cases: it&#8217;s now
201
 
based on the shortest decimal string that&#8217;s guaranteed to round back
202
 
to <code class="docutils literal"><span class="pre">x</span></code>.  As in previous versions of Python, it&#8217;s guaranteed that
203
 
<code class="docutils literal"><span class="pre">float(repr(x))</span></code> recovers <code class="docutils literal"><span class="pre">x</span></code>.</li>
204
 
<li>Float-to-string and string-to-float conversions are correctly rounded.
205
 
The <a class="reference internal" href="../library/functions.html#round" title="round"><code class="xref py py-func docutils literal"><span class="pre">round()</span></code></a> function is also now correctly rounded.</li>
206
 
<li>The <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal"><span class="pre">PyCapsule</span></code></a> type, used to provide a C API for extension modules.</li>
207
 
<li>The <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><code class="xref c c-func docutils literal"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> C API function.</li>
208
 
</ul>
209
 
<p>Other new Python3-mode warnings include:</p>
210
 
<ul class="simple">
211
 
<li><code class="xref py py-func docutils literal"><span class="pre">operator.isCallable()</span></code> and <code class="xref py py-func docutils literal"><span class="pre">operator.sequenceIncludes()</span></code>,
212
 
which are not supported in 3.x, now trigger warnings.</li>
213
 
<li>The <code class="xref std std-option docutils literal"><span class="pre">-3</span></code> switch now automatically
214
 
enables the <code class="xref std std-option docutils literal"><span class="pre">-Qwarn</span></code> switch that causes warnings
215
 
about using classic division with integers and long integers.</li>
216
 
</ul>
217
 
</div>
218
 
<div class="section" id="pep-372-adding-an-ordered-dictionary-to-collections">
219
 
<span id="pep-0372"></span><h2>PEP 372: Adding an Ordered Dictionary to collections<a class="headerlink" href="#pep-372-adding-an-ordered-dictionary-to-collections" title="Permalink to this headline">¶</a></h2>
220
 
<p>Regular Python dictionaries iterate over key/value pairs in arbitrary order.
221
 
Over the years, a number of authors have written alternative implementations
222
 
that remember the order that the keys were originally inserted.  Based on
223
 
the experiences from those implementations, 2.7 introduces a new
224
 
<a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a> class in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a> module.</p>
225
 
<p>The <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a> API provides the same interface as regular
226
 
dictionaries but iterates over keys and values in a guaranteed order
227
 
depending on when a key was first inserted:</p>
228
 
<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="k">import</span> <span class="n">OrderedDict</span>
229
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
230
 
<span class="gp">... </span>                 <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
231
 
<span class="gp">... </span>                 <span class="p">(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
232
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
233
 
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 2), (&#39;third&#39;, 3)]</span>
234
 
</pre></div>
235
 
</div>
236
 
<p>If a new entry overwrites an existing entry, the original insertion
237
 
position is left unchanged:</p>
238
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4</span>
239
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
240
 
<span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 4), (&#39;third&#39;, 3)]</span>
241
 
</pre></div>
242
 
</div>
243
 
<p>Deleting an entry and reinserting it will move it to the end:</p>
244
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span>
245
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="s1">&#39;second&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">5</span>
246
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
247
 
<span class="go">[(&#39;first&#39;, 1), (&#39;third&#39;, 3), (&#39;second&#39;, 5)]</span>
248
 
</pre></div>
249
 
</div>
250
 
<p>The <a class="reference internal" href="../library/collections.html#collections.OrderedDict.popitem" title="collections.OrderedDict.popitem"><code class="xref py py-meth docutils literal"><span class="pre">popitem()</span></code></a> method has an optional <em>last</em>
251
 
argument that defaults to True.  If <em>last</em> is True, the most recently
252
 
added key is returned and removed; if it&#8217;s False, the
253
 
oldest key is selected:</p>
254
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">od</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="n">x</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20</span><span class="p">)])</span>
255
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
256
 
<span class="go">(19, 0)</span>
257
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">()</span>
258
 
<span class="go">(18, 0)</span>
259
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
260
 
<span class="go">(0, 0)</span>
261
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od</span><span class="o">.</span><span class="n">popitem</span><span class="p">(</span><span class="n">last</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
262
 
<span class="go">(1, 0)</span>
263
 
</pre></div>
264
 
</div>
265
 
<p>Comparing two ordered dictionaries checks both the keys and values,
266
 
and requires that the insertion order was the same:</p>
267
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
268
 
<span class="gp">... </span>                   <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
269
 
<span class="gp">... </span>                   <span class="p">(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">)])</span>
270
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od2</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">([(</span><span class="s1">&#39;third&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
271
 
<span class="gp">... </span>                   <span class="p">(</span><span class="s1">&#39;first&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
272
 
<span class="gp">... </span>                   <span class="p">(</span><span class="s1">&#39;second&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
273
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
274
 
<span class="go">False</span>
275
 
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Move &#39;third&#39; key to the end</span>
276
 
<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">od2</span><span class="p">[</span><span class="s1">&#39;third&#39;</span><span class="p">];</span> <span class="n">od2</span><span class="p">[</span><span class="s1">&#39;third&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">3</span>
277
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
278
 
<span class="go">True</span>
279
 
</pre></div>
280
 
</div>
281
 
<p>Comparing an <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a> with a regular dictionary
282
 
ignores the insertion order and just compares the keys and values.</p>
283
 
<p>How does the <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a> work?  It maintains a
284
 
doubly-linked list of keys, appending new keys to the list as they&#8217;re inserted.
285
 
A secondary dictionary maps keys to their corresponding list node, so
286
 
deletion doesn&#8217;t have to traverse the entire linked list and therefore
287
 
remains O(1).</p>
288
 
<p>The standard library now supports use of ordered dictionaries in several
289
 
modules.</p>
290
 
<ul class="simple">
291
 
<li>The <code class="xref py py-mod docutils literal"><span class="pre">ConfigParser</span></code> module uses them by default, meaning that
292
 
configuration files can now be read, modified, and then written back
293
 
in their original order.</li>
294
 
<li>The <a class="reference internal" href="../library/collections.html#collections.somenamedtuple._asdict" title="collections.somenamedtuple._asdict"><code class="xref py py-meth docutils literal"><span class="pre">_asdict()</span></code></a> method for
295
 
<a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal"><span class="pre">collections.namedtuple()</span></code></a> now returns an ordered dictionary with the
296
 
values appearing in the same order as the underlying tuple indices.</li>
297
 
<li>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal"><span class="pre">json</span></code></a> module&#8217;s <a class="reference internal" href="../library/json.html#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal"><span class="pre">JSONDecoder</span></code></a> class
298
 
constructor was extended with an <em>object_pairs_hook</em> parameter to
299
 
allow <code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code> instances to be built by the decoder.
300
 
Support was also added for third-party tools like
301
 
<a class="reference external" href="http://pyyaml.org/">PyYAML</a>.</li>
302
 
</ul>
303
 
<div class="admonition seealso">
304
 
<p class="first admonition-title">See also</p>
305
 
<dl class="last docutils">
306
 
<dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0372"><strong>PEP 372</strong></a> - Adding an ordered dictionary to collections</dt>
307
 
<dd>PEP written by Armin Ronacher and Raymond Hettinger;
308
 
implemented by Raymond Hettinger.</dd>
309
 
</dl>
310
 
</div>
311
 
</div>
312
 
<div class="section" id="pep-378-format-specifier-for-thousands-separator">
313
 
<span id="pep-0378"></span><h2>PEP 378: Format Specifier for Thousands Separator<a class="headerlink" href="#pep-378-format-specifier-for-thousands-separator" title="Permalink to this headline">¶</a></h2>
314
 
<p>To make program output more readable, it can be useful to add
315
 
separators to large numbers, rendering them as
316
 
18,446,744,073,709,551,616 instead of 18446744073709551616.</p>
317
 
<p>The fully general solution for doing this is the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal"><span class="pre">locale</span></code></a> module,
318
 
which can use different separators (&#8221;,&#8221; in North America, &#8221;.&#8221; in
319
 
Europe) and different grouping sizes, but <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal"><span class="pre">locale</span></code></a> is complicated
320
 
to use and unsuitable for multi-threaded applications where different
321
 
threads are producing output for different locales.</p>
322
 
<p>Therefore, a simple comma-grouping mechanism has been added to the
323
 
mini-language used by the <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal"><span class="pre">str.format()</span></code></a> method.  When
324
 
formatting a floating-point number, simply include a comma between the
325
 
width and the precision:</p>
326
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{:20,.2f}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">18446744073709551616.0</span><span class="p">)</span>
327
 
<span class="go">&#39;18,446,744,073,709,551,616.00&#39;</span>
328
 
</pre></div>
329
 
</div>
330
 
<p>When formatting an integer, include the comma after the width:</p>
331
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{:20,d}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">18446744073709551616</span><span class="p">)</span>
332
 
<span class="go">&#39;18,446,744,073,709,551,616&#39;</span>
333
 
</pre></div>
334
 
</div>
335
 
<p>This mechanism is not adaptable at all; commas are always used as the
336
 
separator and the grouping is always into three-digit groups.  The
337
 
comma-formatting mechanism isn&#8217;t as general as the <a class="reference internal" href="../library/locale.html#module-locale" title="locale: Internationalization services."><code class="xref py py-mod docutils literal"><span class="pre">locale</span></code></a>
338
 
module, but it&#8217;s easier to use.</p>
339
 
<div class="admonition seealso">
340
 
<p class="first admonition-title">See also</p>
341
 
<dl class="last docutils">
342
 
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a> - Format Specifier for Thousands Separator</dt>
343
 
<dd>PEP written by Raymond Hettinger; implemented by Eric Smith.</dd>
344
 
</dl>
345
 
</div>
346
 
</div>
347
 
<div class="section" id="pep-389-the-argparse-module-for-parsing-command-lines">
348
 
<h2>PEP 389: The argparse Module for Parsing Command Lines<a class="headerlink" href="#pep-389-the-argparse-module-for-parsing-command-lines" title="Permalink to this headline">¶</a></h2>
349
 
<p>The <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a> module for parsing command-line arguments was
350
 
added as a more powerful replacement for the
351
 
<a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a> module.</p>
352
 
<p>This means Python now supports three different modules for parsing
353
 
command-line arguments: <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal"><span class="pre">getopt</span></code></a>, <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>, and
354
 
<a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a>.  The <a class="reference internal" href="../library/getopt.html#module-getopt" title="getopt: Portable parser for command line options; support both short and long option names."><code class="xref py py-mod docutils literal"><span class="pre">getopt</span></code></a> module closely resembles the C
355
 
library&#8217;s <code class="xref c c-func docutils literal"><span class="pre">getopt()</span></code> function, so it remains useful if you&#8217;re writing a
356
 
Python prototype that will eventually be rewritten in C.
357
 
<a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a> becomes redundant, but there are no plans to remove it
358
 
because there are many scripts still using it, and there&#8217;s no
359
 
automated way to update these scripts.  (Making the <a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a>
360
 
API consistent with <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>&#8216;s interface was discussed but
361
 
rejected as too messy and difficult.)</p>
362
 
<p>In short, if you&#8217;re writing a new script and don&#8217;t need to worry
363
 
about compatibility with earlier versions of Python, use
364
 
<a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a> instead of <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>.</p>
365
 
<p>Here&#8217;s an example:</p>
366
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">argparse</span>
367
 
 
368
 
<span class="n">parser</span> <span class="o">=</span> <span class="n">argparse</span><span class="o">.</span><span class="n">ArgumentParser</span><span class="p">(</span><span class="n">description</span><span class="o">=</span><span class="s1">&#39;Command-line example.&#39;</span><span class="p">)</span>
369
 
 
370
 
<span class="c1"># Add optional switches</span>
371
 
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-v&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;is_verbose&#39;</span><span class="p">,</span>
372
 
                    <span class="n">help</span><span class="o">=</span><span class="s1">&#39;produce verbose output&#39;</span><span class="p">)</span>
373
 
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-o&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;output&#39;</span><span class="p">,</span>
374
 
                    <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;FILE&#39;</span><span class="p">,</span>
375
 
                    <span class="n">help</span><span class="o">=</span><span class="s1">&#39;direct output to FILE instead of stdout&#39;</span><span class="p">)</span>
376
 
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-C&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="nb">int</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;context&#39;</span><span class="p">,</span>
377
 
                    <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;NUM&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
378
 
                    <span class="n">help</span><span class="o">=</span><span class="s1">&#39;display NUM lines of added context&#39;</span><span class="p">)</span>
379
 
 
380
 
<span class="c1"># Allow any number of additional arguments.</span>
381
 
<span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="n">nargs</span><span class="o">=</span><span class="s1">&#39;*&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s1">&#39;store&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s1">&#39;inputs&#39;</span><span class="p">,</span>
382
 
                    <span class="n">help</span><span class="o">=</span><span class="s1">&#39;input filenames (default is stdin)&#39;</span><span class="p">)</span>
383
 
 
384
 
<span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
385
 
<span class="nb">print</span> <span class="n">args</span><span class="o">.</span><span class="n">__dict__</span>
386
 
</pre></div>
387
 
</div>
388
 
<p>Unless you override it, <a class="reference internal" href="../using/cmdline.html#cmdoption-h"><code class="xref std std-option docutils literal"><span class="pre">-h</span></code></a> and <a class="reference internal" href="../using/cmdline.html#cmdoption--help"><code class="xref std std-option docutils literal"><span class="pre">--help</span></code></a> switches
389
 
are automatically added, and produce neatly formatted output:</p>
390
 
<div class="highlight-python3"><div class="highlight"><pre><span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">--</span><span class="n">help</span>
391
 
<span class="n">usage</span><span class="p">:</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="p">[</span><span class="o">-</span><span class="n">h</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">v</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">o</span> <span class="n">FILE</span><span class="p">]</span> <span class="p">[</span><span class="o">-</span><span class="n">C</span> <span class="n">NUM</span><span class="p">]</span> <span class="p">[</span><span class="n">inputs</span> <span class="p">[</span><span class="n">inputs</span> <span class="o">...</span><span class="p">]]</span>
392
 
 
393
 
<span class="n">Command</span><span class="o">-</span><span class="n">line</span> <span class="n">example</span><span class="o">.</span>
394
 
 
395
 
<span class="n">positional</span> <span class="n">arguments</span><span class="p">:</span>
396
 
  <span class="n">inputs</span>      <span class="nb">input</span> <span class="n">filenames</span> <span class="p">(</span><span class="n">default</span> <span class="ow">is</span> <span class="n">stdin</span><span class="p">)</span>
397
 
 
398
 
<span class="n">optional</span> <span class="n">arguments</span><span class="p">:</span>
399
 
  <span class="o">-</span><span class="n">h</span><span class="p">,</span> <span class="o">--</span><span class="n">help</span>  <span class="n">show</span> <span class="n">this</span> <span class="n">help</span> <span class="n">message</span> <span class="ow">and</span> <span class="n">exit</span>
400
 
  <span class="o">-</span><span class="n">v</span>          <span class="n">produce</span> <span class="n">verbose</span> <span class="n">output</span>
401
 
  <span class="o">-</span><span class="n">o</span> <span class="n">FILE</span>     <span class="n">direct</span> <span class="n">output</span> <span class="n">to</span> <span class="n">FILE</span> <span class="n">instead</span> <span class="n">of</span> <span class="n">stdout</span>
402
 
  <span class="o">-</span><span class="n">C</span> <span class="n">NUM</span>      <span class="n">display</span> <span class="n">NUM</span> <span class="n">lines</span> <span class="n">of</span> <span class="n">added</span> <span class="n">context</span>
403
 
</pre></div>
404
 
</div>
405
 
<p>As with <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>, the command-line switches and arguments
406
 
are returned as an object with attributes named by the <em>dest</em> parameters:</p>
407
 
<div class="highlight-python3"><div class="highlight"><pre><span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span>
408
 
<span class="p">{</span><span class="s1">&#39;output&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
409
 
 <span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
410
 
 <span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
411
 
 <span class="s1">&#39;inputs&#39;</span><span class="p">:</span> <span class="p">[]}</span>
412
 
 
413
 
<span class="o">-&gt;</span> <span class="o">./</span><span class="n">python</span><span class="o">.</span><span class="n">exe</span> <span class="n">argparse</span><span class="o">-</span><span class="n">example</span><span class="o">.</span><span class="n">py</span> <span class="o">-</span><span class="n">v</span> <span class="o">-</span><span class="n">o</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span><span class="n">output</span> <span class="o">-</span><span class="n">C</span> <span class="mi">4</span> <span class="n">file1</span> <span class="n">file2</span>
414
 
<span class="p">{</span><span class="s1">&#39;output&#39;</span><span class="p">:</span> <span class="s1">&#39;/tmp/output&#39;</span><span class="p">,</span>
415
 
 <span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
416
 
 <span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
417
 
 <span class="s1">&#39;inputs&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;file1&#39;</span><span class="p">,</span> <span class="s1">&#39;file2&#39;</span><span class="p">]}</span>
418
 
</pre></div>
419
 
</div>
420
 
<p><a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a> has much fancier validation than <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>; you
421
 
can specify an exact number of arguments as an integer, 0 or more
422
 
arguments by passing <code class="docutils literal"><span class="pre">'*'</span></code>, 1 or more by passing <code class="docutils literal"><span class="pre">'+'</span></code>, or an
423
 
optional argument with <code class="docutils literal"><span class="pre">'?'</span></code>.  A top-level parser can contain
424
 
sub-parsers to define subcommands that have different sets of
425
 
switches, as in <code class="docutils literal"><span class="pre">svn</span> <span class="pre">commit</span></code>, <code class="docutils literal"><span class="pre">svn</span> <span class="pre">checkout</span></code>, etc.  You can
426
 
specify an argument&#8217;s type as <a class="reference internal" href="../library/argparse.html#argparse.FileType" title="argparse.FileType"><code class="xref py py-class docutils literal"><span class="pre">FileType</span></code></a>, which will
427
 
automatically open files for you and understands that <code class="docutils literal"><span class="pre">'-'</span></code> means
428
 
standard input or output.</p>
429
 
<div class="admonition seealso">
430
 
<p class="first admonition-title">See also</p>
431
 
<dl class="last docutils">
432
 
<dt><a class="reference internal" href="../library/argparse.html#module-argparse" title="argparse: Command-line option and argument parsing library."><code class="xref py py-mod docutils literal"><span class="pre">argparse</span></code></a> documentation</dt>
433
 
<dd>The documentation page of the argparse module.</dd>
434
 
<dt><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><span>Upgrading optparse code</span></a></dt>
435
 
<dd>Part of the Python documentation, describing how to convert
436
 
code that uses <a class="reference internal" href="../library/optparse.html#module-optparse" title="optparse: Command-line option parsing library. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">optparse</span></code></a>.</dd>
437
 
<dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0389"><strong>PEP 389</strong></a> - argparse - New Command Line Parsing Module</dt>
438
 
<dd>PEP written and implemented by Steven Bethard.</dd>
439
 
</dl>
440
 
</div>
441
 
</div>
442
 
<div class="section" id="pep-391-dictionary-based-configuration-for-logging">
443
 
<h2>PEP 391: Dictionary-Based Configuration For Logging<a class="headerlink" href="#pep-391-dictionary-based-configuration-for-logging" title="Permalink to this headline">¶</a></h2>
444
 
<p>The <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> module is very flexible; applications can define
445
 
a tree of logging subsystems, and each logger in this tree can filter
446
 
out certain messages, format them differently, and direct messages to
447
 
a varying number of handlers.</p>
448
 
<p>All this flexibility can require a lot of configuration.  You can
449
 
write Python statements to create objects and set their properties,
450
 
but a complex set-up requires verbose but boring code.
451
 
<a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> also supports a <code class="xref py py-func docutils literal"><span class="pre">fileConfig()</span></code>
452
 
function that parses a file, but the file format doesn&#8217;t support
453
 
configuring filters, and it&#8217;s messier to generate programmatically.</p>
454
 
<p>Python 2.7 adds a <code class="xref py py-func docutils literal"><span class="pre">dictConfig()</span></code> function that
455
 
uses a dictionary to configure logging.  There are many ways to
456
 
produce a dictionary from different sources: construct one with code;
457
 
parse a file containing JSON; or use a YAML parsing library if one is
458
 
installed.  For more information see <a class="reference internal" href="../library/logging.config.html#logging-config-api"><span>Configuration functions</span></a>.</p>
459
 
<p>The following example configures two loggers, the root logger and a
460
 
logger named &#8220;network&#8221;.  Messages sent to the root logger will be
461
 
sent to the system log using the syslog protocol, and messages
462
 
to the &#8220;network&#8221; logger will be written to a <code class="file docutils literal"><span class="pre">network.log</span></code> file
463
 
that will be rotated once the log reaches 1MB.</p>
464
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">logging</span>
465
 
<span class="kn">import</span> <span class="nn">logging.config</span>
466
 
 
467
 
<span class="n">configdict</span> <span class="o">=</span> <span class="p">{</span>
468
 
 <span class="s1">&#39;version&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>    <span class="c1"># Configuration schema in use; must be 1 for now</span>
469
 
 <span class="s1">&#39;formatters&#39;</span><span class="p">:</span> <span class="p">{</span>
470
 
     <span class="s1">&#39;standard&#39;</span><span class="p">:</span> <span class="p">{</span>
471
 
         <span class="s1">&#39;format&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;</span><span class="si">%(asctime)s</span><span class="s1"> </span><span class="si">%(name)-15s</span><span class="s1"> &#39;</span>
472
 
                    <span class="s1">&#39;</span><span class="si">%(levelname)-8s</span><span class="s1"> </span><span class="si">%(message)s</span><span class="s1">&#39;</span><span class="p">)}},</span>
473
 
 
474
 
 <span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;netlog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;backupCount&#39;</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span>
475
 
                     <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;logging.handlers.RotatingFileHandler&#39;</span><span class="p">,</span>
476
 
                     <span class="s1">&#39;filename&#39;</span><span class="p">:</span> <span class="s1">&#39;/logs/network.log&#39;</span><span class="p">,</span>
477
 
                     <span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
478
 
                     <span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;INFO&#39;</span><span class="p">,</span>
479
 
                     <span class="s1">&#39;maxBytes&#39;</span><span class="p">:</span> <span class="mi">1000000</span><span class="p">},</span>
480
 
              <span class="s1">&#39;syslog&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;logging.handlers.SysLogHandler&#39;</span><span class="p">,</span>
481
 
                         <span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
482
 
                         <span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;ERROR&#39;</span><span class="p">}},</span>
483
 
 
484
 
 <span class="c1"># Specify all the subordinate loggers</span>
485
 
 <span class="s1">&#39;loggers&#39;</span><span class="p">:</span> <span class="p">{</span>
486
 
             <span class="s1">&#39;network&#39;</span><span class="p">:</span> <span class="p">{</span>
487
 
                         <span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;netlog&#39;</span><span class="p">]</span>
488
 
             <span class="p">}</span>
489
 
 <span class="p">},</span>
490
 
 <span class="c1"># Specify properties of the root logger</span>
491
 
 <span class="s1">&#39;root&#39;</span><span class="p">:</span> <span class="p">{</span>
492
 
          <span class="s1">&#39;handlers&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;syslog&#39;</span><span class="p">]</span>
493
 
 <span class="p">},</span>
494
 
<span class="p">}</span>
495
 
 
496
 
<span class="c1"># Set up configuration</span>
497
 
<span class="n">logging</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">dictConfig</span><span class="p">(</span><span class="n">configdict</span><span class="p">)</span>
498
 
 
499
 
<span class="c1"># As an example, log two error messages</span>
500
 
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
501
 
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Database not found&#39;</span><span class="p">)</span>
502
 
 
503
 
<span class="n">netlogger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;network&#39;</span><span class="p">)</span>
504
 
<span class="n">netlogger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s1">&#39;Connection failed&#39;</span><span class="p">)</span>
505
 
</pre></div>
506
 
</div>
507
 
<p>Three smaller enhancements to the <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><code class="xref py py-mod docutils literal"><span class="pre">logging</span></code></a> module, all
508
 
implemented by Vinay Sajip, are:</p>
509
 
<ul class="simple">
510
 
<li>The <a class="reference internal" href="../library/logging.handlers.html#logging.handlers.SysLogHandler" title="logging.handlers.SysLogHandler"><code class="xref py py-class docutils literal"><span class="pre">SysLogHandler</span></code></a> class now supports
511
 
syslogging over TCP.  The constructor has a <em>socktype</em> parameter
512
 
giving the type of socket to use, either <a class="reference internal" href="../library/socket.html#socket.SOCK_DGRAM" title="socket.SOCK_DGRAM"><code class="xref py py-const docutils literal"><span class="pre">socket.SOCK_DGRAM</span></code></a>
513
 
for UDP or <a class="reference internal" href="../library/socket.html#socket.SOCK_STREAM" title="socket.SOCK_STREAM"><code class="xref py py-const docutils literal"><span class="pre">socket.SOCK_STREAM</span></code></a> for TCP.  The default
514
 
protocol remains UDP.</li>
515
 
<li><a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal"><span class="pre">Logger</span></code></a> instances gained a <a class="reference internal" href="../library/logging.html#logging.Logger.getChild" title="logging.Logger.getChild"><code class="xref py py-meth docutils literal"><span class="pre">getChild()</span></code></a>
516
 
method that retrieves a descendant logger using a relative path.
517
 
For example, once you retrieve a logger by doing <code class="docutils literal"><span class="pre">log</span> <span class="pre">=</span> <span class="pre">getLogger('app')</span></code>,
518
 
calling <code class="docutils literal"><span class="pre">log.getChild('network.listen')</span></code> is equivalent to
519
 
<code class="docutils literal"><span class="pre">getLogger('app.network.listen')</span></code>.</li>
520
 
<li>The <a class="reference internal" href="../library/logging.html#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></code></a> class gained a
521
 
<code class="xref py py-meth docutils literal"><span class="pre">isEnabledFor()</span></code> method that takes a
522
 
<em>level</em> and returns whether the underlying logger would
523
 
process a message of that level of importance.</li>
524
 
</ul>
525
 
<div class="admonition seealso">
526
 
<p class="first admonition-title">See also</p>
527
 
<dl class="last docutils">
528
 
<dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0391"><strong>PEP 391</strong></a> - Dictionary-Based Configuration For Logging</dt>
529
 
<dd>PEP written and implemented by Vinay Sajip.</dd>
530
 
</dl>
531
 
</div>
532
 
</div>
533
 
<div class="section" id="pep-3106-dictionary-views">
534
 
<h2>PEP 3106: Dictionary Views<a class="headerlink" href="#pep-3106-dictionary-views" title="Permalink to this headline">¶</a></h2>
535
 
<p>The dictionary methods <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal"><span class="pre">keys()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal"><span class="pre">values()</span></code></a>, and
536
 
<a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal"><span class="pre">items()</span></code></a> are different in Python 3.x.  They return an object
537
 
called a <em class="dfn">view</em> instead of a fully materialized list.</p>
538
 
<p>It&#8217;s not possible to change the return values of <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal"><span class="pre">keys()</span></code></a>,
539
 
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal"><span class="pre">values()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal"><span class="pre">items()</span></code></a> in Python 2.7 because
540
 
too much code would break.  Instead the 3.x versions were added
541
 
under the new names <code class="xref py py-meth docutils literal"><span class="pre">viewkeys()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">viewvalues()</span></code>,
542
 
and <code class="xref py py-meth docutils literal"><span class="pre">viewitems()</span></code>.</p>
543
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
544
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
545
 
<span class="go">{0: &#39;A&#39;, 130: &#39;N&#39;, 10: &#39;B&#39;, 140: &#39;O&#39;, 20: ..., 250: &#39;Z&#39;}</span>
546
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
547
 
<span class="go">dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])</span>
548
 
</pre></div>
549
 
</div>
550
 
<p>Views can be iterated over, but the key and item views also behave
551
 
like sets.  The <code class="docutils literal"><span class="pre">&amp;</span></code> operator performs intersection, and <code class="docutils literal"><span class="pre">|</span></code>
552
 
performs a union:</p>
553
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">*</span><span class="mi">10</span><span class="p">,</span> <span class="nb">chr</span><span class="p">(</span><span class="mi">65</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">26</span><span class="p">))</span>
554
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d2</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">i</span><span class="o">**.</span><span class="mi">5</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>
555
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">&amp;</span> <span class="n">d2</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
556
 
<span class="go">set([0.0, 10.0, 20.0, 30.0])</span>
557
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d1</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span> <span class="o">|</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span>
558
 
<span class="go">set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])</span>
559
 
</pre></div>
560
 
</div>
561
 
<p>The view keeps track of the dictionary and its contents change as the
562
 
dictionary is modified:</p>
563
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">viewkeys</span><span class="p">()</span>
564
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
565
 
<span class="go">dict_keys([0, 130, 10, ..., 250])</span>
566
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="p">[</span><span class="mi">260</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;&amp;&#39;</span>
567
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
568
 
<span class="go">dict_keys([0, 130, 260, 10, ..., 250])</span>
569
 
</pre></div>
570
 
</div>
571
 
<p>However, note that you can&#8217;t add or remove keys while you&#8217;re iterating
572
 
over the view:</p>
573
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">vk</span><span class="p">:</span>
574
 
<span class="gp">... </span>    <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">k</span>
575
 
<span class="gp">...</span>
576
 
<span class="gt">Traceback (most recent call last):</span>
577
 
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
578
 
<span class="gr">RuntimeError</span>: <span class="n">dictionary changed size during iteration</span>
579
 
</pre></div>
580
 
</div>
581
 
<p>You can use the view methods in Python 2.x code, and the 2to3
582
 
converter will change them to the standard <a class="reference internal" href="../library/stdtypes.html#dict.keys" title="dict.keys"><code class="xref py py-meth docutils literal"><span class="pre">keys()</span></code></a>,
583
 
<a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal"><span class="pre">values()</span></code></a>, and <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal"><span class="pre">items()</span></code></a> methods.</p>
584
 
<div class="admonition seealso">
585
 
<p class="first admonition-title">See also</p>
586
 
<dl class="last docutils">
587
 
<dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3106"><strong>PEP 3106</strong></a> - Revamping dict.keys(), .values() and .items()</dt>
588
 
<dd>PEP written by Guido van Rossum.
589
 
Backported to 2.7 by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue1967">issue 1967</a>.</dd>
590
 
</dl>
591
 
</div>
592
 
</div>
593
 
<div class="section" id="pep-3137-the-memoryview-object">
594
 
<h2>PEP 3137: The memoryview Object<a class="headerlink" href="#pep-3137-the-memoryview-object" title="Permalink to this headline">¶</a></h2>
595
 
<p>The <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal"><span class="pre">memoryview</span></code></a> object provides a view of another object&#8217;s
596
 
memory content that matches the <a class="reference internal" href="../library/functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> type&#8217;s interface.</p>
597
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
598
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span>
599
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
600
 
<span class="go">&lt;memory at 0x37f850&gt;</span>
601
 
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>           <span class="c1"># Returns length of underlying object</span>
602
 
<span class="go">52</span>
603
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">25</span><span class="p">],</span> <span class="n">m</span><span class="p">[</span><span class="mi">26</span><span class="p">]</span>   <span class="c1"># Indexing returns one byte</span>
604
 
<span class="go">(&#39;a&#39;, &#39;z&#39;, &#39;A&#39;)</span>
605
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span> <span class="o">=</span> <span class="n">m</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">26</span><span class="p">]</span>         <span class="c1"># Slicing returns another memoryview</span>
606
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span>
607
 
<span class="go">&lt;memory at 0x37f080&gt;</span>
608
 
</pre></div>
609
 
</div>
610
 
<p>The content of the view can be converted to a string of bytes or
611
 
a list of integers:</p>
612
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="o">.</span><span class="n">tobytes</span><span class="p">()</span>
613
 
<span class="go">&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
614
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
615
 
<span class="go">[97, 98, 99, 100, 101, 102, 103, ... 121, 122]</span>
616
 
<span class="go">&gt;&gt;&gt;</span>
617
 
</pre></div>
618
 
</div>
619
 
<p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal"><span class="pre">memoryview</span></code></a> objects allow modifying the underlying object if
620
 
it&#8217;s a mutable object.</p>
621
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">75</span>
622
 
<span class="gt">Traceback (most recent call last):</span>
623
 
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
624
 
<span class="gr">TypeError</span>: <span class="n">cannot modify read-only memory</span>
625
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">)</span>  <span class="c1"># Creating a mutable object</span>
626
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
627
 
<span class="go">bytearray(b&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;)</span>
628
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span> <span class="o">=</span> <span class="nb">memoryview</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
629
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">mb</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;*&#39;</span>         <span class="c1"># Assign to view, changing the bytearray.</span>
630
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>              <span class="c1"># The bytearray has been changed.</span>
631
 
<span class="go">bytearray(b&#39;*bcde&#39;)</span>
632
 
<span class="go">&gt;&gt;&gt;</span>
633
 
</pre></div>
634
 
</div>
635
 
<div class="admonition seealso">
636
 
<p class="first admonition-title">See also</p>
637
 
<dl class="last docutils">
638
 
<dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3137"><strong>PEP 3137</strong></a> - Immutable Bytes and Mutable Buffer</dt>
639
 
<dd>PEP written by Guido van Rossum.
640
 
Implemented by Travis Oliphant, Antoine Pitrou and others.
641
 
Backported to 2.7 by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue2396">issue 2396</a>.</dd>
642
 
</dl>
643
 
</div>
644
 
</div>
645
 
<div class="section" id="other-language-changes">
646
 
<h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Permalink to this headline">¶</a></h2>
647
 
<p>Some smaller changes made to the core Python language are:</p>
648
 
<ul>
649
 
<li><p class="first">The syntax for set literals has been backported from Python 3.x.
650
 
Curly brackets are used to surround the contents of the resulting
651
 
mutable set; set literals are
652
 
distinguished from dictionaries by not containing colons and values.
653
 
<code class="docutils literal"><span class="pre">{}</span></code> continues to represent an empty dictionary; use
654
 
<code class="docutils literal"><span class="pre">set()</span></code> for an empty set.</p>
655
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">}</span>
656
 
<span class="go">set([1, 2, 3, 4, 5])</span>
657
 
<span class="gp">&gt;&gt;&gt; </span><span class="nb">set</span><span class="p">()</span> <span class="c1"># empty set</span>
658
 
<span class="go">set([])</span>
659
 
<span class="gp">&gt;&gt;&gt; </span><span class="p">{}</span>    <span class="c1"># empty dict</span>
660
 
<span class="go">{}</span>
661
 
</pre></div>
662
 
</div>
663
 
<p>Backported by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue2335">issue 2335</a>.</p>
664
 
</li>
665
 
<li><p class="first">Dictionary and set comprehensions are another feature backported from
666
 
3.x, generalizing list/generator comprehensions to use
667
 
the literal syntax for sets and dictionaries.</p>
668
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">*</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
669
 
<span class="go">{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</span>
670
 
<span class="gp">&gt;&gt;&gt; </span><span class="p">{(</span><span class="s1">&#39;a&#39;</span><span class="o">*</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">6</span><span class="p">)}</span>
671
 
<span class="go">set([&#39;&#39;, &#39;a&#39;, &#39;aa&#39;, &#39;aaa&#39;, &#39;aaaa&#39;, &#39;aaaaa&#39;])</span>
672
 
</pre></div>
673
 
</div>
674
 
<p>Backported by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue2333">issue 2333</a>.</p>
675
 
</li>
676
 
<li><p class="first">The <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement can now use multiple context managers
677
 
in one statement.  Context managers are processed from left to right
678
 
and each one is treated as beginning a new <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement.
679
 
This means that:</p>
680
 
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">,</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
681
 
    <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
682
 
</pre></div>
683
 
</div>
684
 
<p>is equivalent to:</p>
685
 
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="n">A</span><span class="p">()</span> <span class="k">as</span> <span class="n">a</span><span class="p">:</span>
686
 
    <span class="k">with</span> <span class="n">B</span><span class="p">()</span> <span class="k">as</span> <span class="n">b</span><span class="p">:</span>
687
 
        <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
688
 
</pre></div>
689
 
</div>
690
 
<p>The <code class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></code> function provides a very similar
691
 
function, so it&#8217;s no longer necessary and has been deprecated.</p>
692
 
<p>(Proposed in <a class="reference external" href="https://codereview.appspot.com/53094">https://codereview.appspot.com/53094</a>; implemented by
693
 
Georg Brandl.)</p>
694
 
</li>
695
 
<li><p class="first">Conversions between floating-point numbers and strings are
696
 
now correctly rounded on most platforms.  These conversions occur
697
 
in many different places: <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a> on
698
 
floats and complex numbers; the <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal"><span class="pre">float</span></code></a> and <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal"><span class="pre">complex</span></code></a>
699
 
constructors;
700
 
numeric formatting; serializing and
701
 
deserializing floats and complex numbers using the
702
 
<a class="reference internal" href="../library/marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal"><span class="pre">marshal</span></code></a>, <a class="reference internal" href="../library/pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a>
703
 
and <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal"><span class="pre">json</span></code></a> modules;
704
 
parsing of float and imaginary literals in Python code;
705
 
and <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>-to-float conversion.</p>
706
 
<p>Related to this, the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> of a floating-point number <em>x</em>
707
 
now returns a result based on the shortest decimal string that&#8217;s
708
 
guaranteed to round back to <em>x</em> under correct rounding (with
709
 
round-half-to-even rounding mode).  Previously it gave a string
710
 
based on rounding x to 17 decimal digits.</p>
711
 
<p>The rounding library responsible for this improvement works on
712
 
Windows and on Unix platforms using the gcc, icc, or suncc
713
 
compilers.  There may be a small number of platforms where correct
714
 
operation of this code cannot be guaranteed, so the code is not
715
 
used on such systems.  You can find out which code is being used
716
 
by checking <a class="reference internal" href="../library/sys.html#sys.float_repr_style" title="sys.float_repr_style"><code class="xref py py-data docutils literal"><span class="pre">sys.float_repr_style</span></code></a>,  which will be <code class="docutils literal"><span class="pre">short</span></code>
717
 
if the new code is in use and <code class="docutils literal"><span class="pre">legacy</span></code> if it isn&#8217;t.</p>
718
 
<p>Implemented by Eric Smith and Mark Dickinson, using David Gay&#8217;s
719
 
<code class="file docutils literal"><span class="pre">dtoa.c</span></code> library; <a class="reference external" href="https://bugs.python.org/issue7117">issue 7117</a>.</p>
720
 
</li>
721
 
<li><p class="first">Conversions from long integers and regular integers to floating
722
 
point now round differently, returning the floating-point number
723
 
closest to the number.  This doesn&#8217;t matter for small integers that
724
 
can be converted exactly, but for large numbers that will
725
 
unavoidably lose precision, Python 2.7 now approximates more
726
 
closely.  For example, Python 2.6 computed the following:</p>
727
 
<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">295147905179352891391</span>
728
 
<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>
729
 
<span class="go">2.9514790517935283e+20</span>
730
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="n">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
731
 
<span class="go">65535L</span>
732
 
</pre></div>
733
 
</div>
734
 
<p>Python 2.7&#8217;s floating-point result is larger, but much closer to the
735
 
true value:</p>
736
 
<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">295147905179352891391</span>
737
 
<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>
738
 
<span class="go">2.9514790517935289e+20</span>
739
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">-</span> <span class="n">long</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
740
 
<span class="go">-1L</span>
741
 
</pre></div>
742
 
</div>
743
 
<p>(Implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue3166">issue 3166</a>.)</p>
744
 
<p>Integer division is also more accurate in its rounding behaviours.  (Also
745
 
implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue1811">issue 1811</a>.)</p>
746
 
</li>
747
 
<li><p class="first">Implicit coercion for complex numbers has been removed; the interpreter
748
 
will no longer ever attempt to call a <code class="xref py py-meth docutils literal"><span class="pre">__coerce__()</span></code> method on complex
749
 
objects.  (Removed by Meador Inge and Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue5211">issue 5211</a>.)</p>
750
 
</li>
751
 
<li><p class="first">The <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal"><span class="pre">str.format()</span></code></a> method now supports automatic numbering of the replacement
752
 
fields.  This makes using <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal"><span class="pre">str.format()</span></code></a> more closely resemble using
753
 
<code class="docutils literal"><span class="pre">%s</span></code> formatting:</p>
754
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{}:{}:{}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">04</span><span class="p">,</span> <span class="s1">&#39;Sunday&#39;</span><span class="p">)</span>
755
 
<span class="go">&#39;2009:4:Sunday&#39;</span>
756
 
<span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{}:{}:{day}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">2009</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="s1">&#39;Sunday&#39;</span><span class="p">)</span>
757
 
<span class="go">&#39;2009:4:Sunday&#39;</span>
758
 
</pre></div>
759
 
</div>
760
 
<p>The auto-numbering takes the fields from left to right, so the first <code class="docutils literal"><span class="pre">{...}</span></code>
761
 
specifier will use the first argument to <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal"><span class="pre">str.format()</span></code></a>, the next
762
 
specifier will use the next argument, and so on.  You can&#8217;t mix auto-numbering
763
 
and explicit numbering &#8211; either number all of your specifier fields or none
764
 
of them &#8211; but you can mix auto-numbering and named fields, as in the second
765
 
example above.  (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue5237">issue 5237</a>.)</p>
766
 
<p>Complex numbers now correctly support usage with <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-func docutils literal"><span class="pre">format()</span></code></a>,
767
 
and default to being right-aligned.
768
 
Specifying a precision or comma-separation applies to both the real
769
 
and imaginary parts of the number, but a specified field width and
770
 
alignment is applied to the whole of the resulting <code class="docutils literal"><span class="pre">1.5+3j</span></code>
771
 
output.  (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue1588">issue 1588</a> and <a class="reference external" href="https://bugs.python.org/issue7988">issue 7988</a>.)</p>
772
 
<p>The &#8216;F&#8217; format code now always formats its output using uppercase characters,
773
 
so it will now produce &#8216;INF&#8217; and &#8216;NAN&#8217;.
774
 
(Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue3382">issue 3382</a>.)</p>
775
 
<p>A low-level change: the <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal"><span class="pre">object.__format__()</span></code></a> method now triggers
776
 
a <a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></code></a> if it&#8217;s passed a format string,
777
 
because the <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal"><span class="pre">__format__()</span></code></a> method for <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal"><span class="pre">object</span></code></a> converts
778
 
the object to a string representation and formats that.  Previously
779
 
the method silently applied the format string to the string
780
 
representation, but that could hide mistakes in Python code.  If
781
 
you&#8217;re supplying formatting information such as an alignment or
782
 
precision, presumably you&#8217;re expecting the formatting to be applied
783
 
in some object-specific way.  (Fixed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue7994">issue 7994</a>.)</p>
784
 
</li>
785
 
<li><p class="first">The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal"><span class="pre">int()</span></code></a> and <code class="xref py py-func docutils literal"><span class="pre">long()</span></code> types gained a <code class="docutils literal"><span class="pre">bit_length</span></code>
786
 
method that returns the number of bits necessary to represent
787
 
its argument in binary:</p>
788
 
<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>
789
 
<span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
790
 
<span class="go">&#39;0b100101&#39;</span>
791
 
<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>
792
 
<span class="go">6</span>
793
 
<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>
794
 
<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>
795
 
<span class="go">123</span>
796
 
<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>
797
 
<span class="go">124</span>
798
 
</pre></div>
799
 
</div>
800
 
<p>(Contributed by Fredrik Johansson and Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue3439">issue 3439</a>.)</p>
801
 
</li>
802
 
<li><p class="first">The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> statement will no longer try an absolute import
803
 
if a relative import (e.g. <code class="docutils literal"><span class="pre">from</span> <span class="pre">.os</span> <span class="pre">import</span> <span class="pre">sep</span></code>) fails.  This
804
 
fixes a bug, but could possibly break certain <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a>
805
 
statements that were only working by accident.  (Fixed by Meador Inge;
806
 
<a class="reference external" href="https://bugs.python.org/issue7902">issue 7902</a>.)</p>
807
 
</li>
808
 
<li><p class="first">It&#8217;s now possible for a subclass of the built-in <code class="xref py py-class docutils literal"><span class="pre">unicode</span></code> type
809
 
to override the <code class="xref py py-meth docutils literal"><span class="pre">__unicode__()</span></code> method.  (Implemented by
810
 
Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue1583863">issue 1583863</a>.)</p>
811
 
</li>
812
 
<li><p class="first">The <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal"><span class="pre">bytearray</span></code></a> type&#8217;s <a class="reference internal" href="../library/stdtypes.html#bytearray.translate" title="bytearray.translate"><code class="xref py py-meth docutils literal"><span class="pre">translate()</span></code></a> method now accepts
813
 
<code class="docutils literal"><span class="pre">None</span></code> as its first argument.  (Fixed by Georg Brandl;
814
 
<a class="reference external" href="https://bugs.python.org/issue4759">issue 4759</a>.)</p>
815
 
</li>
816
 
<li><p class="first">When using <code class="docutils literal"><span class="pre">&#64;classmethod</span></code> and <code class="docutils literal"><span class="pre">&#64;staticmethod</span></code> to wrap
817
 
methods as class or static methods, the wrapper object now
818
 
exposes the wrapped function as their <code class="xref py py-attr docutils literal"><span class="pre">__func__</span></code> attribute.
819
 
(Contributed by Amaury Forgeot d&#8217;Arc, after a suggestion by
820
 
George Sakkis; <a class="reference external" href="https://bugs.python.org/issue5982">issue 5982</a>.)</p>
821
 
</li>
822
 
<li><p class="first">When a restricted set of attributes were set using <code class="docutils literal"><span class="pre">__slots__</span></code>,
823
 
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></code></a>
824
 
as you would expect.  Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue7604">issue 7604</a>.)</p>
825
 
</li>
826
 
<li><p class="first">Two new encodings are now supported: &#8220;cp720&#8221;, used primarily for
827
 
Arabic text; and &#8220;cp858&#8221;, a variant of CP 850 that adds the euro
828
 
symbol.  (CP720 contributed by Alexander Belchenko and Amaury
829
 
Forgeot d&#8217;Arc in <a class="reference external" href="https://bugs.python.org/issue1616979">issue 1616979</a>; CP858 contributed by Tim Hatch in
830
 
<a class="reference external" href="https://bugs.python.org/issue8016">issue 8016</a>.)</p>
831
 
</li>
832
 
<li><p class="first">The <code class="xref py py-class docutils literal"><span class="pre">file</span></code> object will now set the <code class="xref py py-attr docutils literal"><span class="pre">filename</span></code> attribute
833
 
on the <a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal"><span class="pre">IOError</span></code></a> exception when trying to open a directory
834
 
on POSIX platforms (noted by Jan Kaliszewski; <a class="reference external" href="https://bugs.python.org/issue4764">issue 4764</a>), and
835
 
now explicitly checks for and forbids writing to read-only file objects
836
 
instead of trusting the C library to catch and report the error
837
 
(fixed by Stefan Krah; <a class="reference external" href="https://bugs.python.org/issue5677">issue 5677</a>).</p>
838
 
</li>
839
 
<li><p class="first">The Python tokenizer now translates line endings itself, so the
840
 
<a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> built-in function now accepts code using any
841
 
line-ending convention.  Additionally, it no longer requires that the
842
 
code end in a newline.</p>
843
 
</li>
844
 
<li><p class="first">Extra parentheses in function definitions are illegal in Python 3.x,
845
 
meaning that you get a syntax error from <code class="docutils literal"><span class="pre">def</span> <span class="pre">f((x)):</span> <span class="pre">pass</span></code>.  In
846
 
Python3-warning mode, Python 2.7 will now warn about this odd usage.
847
 
(Noted by James Lingard; <a class="reference external" href="https://bugs.python.org/issue7362">issue 7362</a>.)</p>
848
 
</li>
849
 
<li><p class="first">It&#8217;s now possible to create weak references to old-style class
850
 
objects.  New-style classes were always weak-referenceable.  (Fixed
851
 
by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8268">issue 8268</a>.)</p>
852
 
</li>
853
 
<li><p class="first">When a module object is garbage-collected, the module&#8217;s dictionary is
854
 
now only cleared if no one else is holding a reference to the
855
 
dictionary (<a class="reference external" href="https://bugs.python.org/issue7140">issue 7140</a>).</p>
856
 
</li>
857
 
</ul>
858
 
<div class="section" id="interpreter-changes">
859
 
<span id="new-27-interpreter"></span><h3>Interpreter Changes<a class="headerlink" href="#interpreter-changes" title="Permalink to this headline">¶</a></h3>
860
 
<p>A new environment variable, <span class="target" id="index-8"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONWARNINGS"><code class="xref std std-envvar docutils literal"><span class="pre">PYTHONWARNINGS</span></code></a>,
861
 
allows controlling warnings.  It should be set to a string
862
 
containing warning settings, equivalent to those
863
 
used with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal"><span class="pre">-W</span></code></a> switch, separated by commas.
864
 
(Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue7301">issue 7301</a>.)</p>
865
 
<p>For example, the following setting will print warnings every time
866
 
they occur, but turn warnings from the <code class="xref py py-mod docutils literal"><span class="pre">Cookie</span></code> module into an
867
 
error.  (The exact syntax for setting an environment variable varies
868
 
across operating systems and shells.)</p>
869
 
<div class="highlight-python3"><div class="highlight"><pre><span class="n">export</span> <span class="n">PYTHONWARNINGS</span><span class="o">=</span><span class="nb">all</span><span class="p">,</span><span class="n">error</span><span class="p">:::</span><span class="n">Cookie</span><span class="p">:</span><span class="mi">0</span>
870
 
</pre></div>
871
 
</div>
872
 
</div>
873
 
<div class="section" id="optimizations">
874
 
<h3>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h3>
875
 
<p>Several performance enhancements have been added:</p>
876
 
<ul>
877
 
<li><p class="first">A new opcode was added to perform the initial setup for
878
 
<a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statements, looking up the <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></code></a> and
879
 
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></code></a> methods.  (Contributed by Benjamin Peterson.)</p>
880
 
</li>
881
 
<li><p class="first">The garbage collector now performs better for one common usage
882
 
pattern: when many objects are being allocated without deallocating
883
 
any of them.  This would previously take quadratic
884
 
time for garbage collection, but now the number of full garbage collections
885
 
is reduced as the number of objects on the heap grows.
886
 
The new logic only performs a full garbage collection pass when
887
 
the middle generation has been collected 10 times and when the
888
 
number of survivor objects from the middle generation exceeds 10% of
889
 
the number of objects in the oldest generation.  (Suggested by Martin
890
 
von Löwis and implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue4074">issue 4074</a>.)</p>
891
 
</li>
892
 
<li><p class="first">The garbage collector tries to avoid tracking simple containers
893
 
which can&#8217;t be part of a cycle. In Python 2.7, this is now true for
894
 
tuples and dicts containing atomic types (such as ints, strings,
895
 
etc.). Transitively, a dict containing tuples of atomic types won&#8217;t
896
 
be tracked either. This helps reduce the cost of each
897
 
garbage collection by decreasing the number of objects to be
898
 
considered and traversed by the collector.
899
 
(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue4688">issue 4688</a>.)</p>
900
 
</li>
901
 
<li><p class="first">Long integers are now stored internally either in base 2**15 or in base
902
 
2**30, the base being determined at build time.  Previously, they
903
 
were always stored in base 2**15.  Using base 2**30 gives
904
 
significant performance improvements on 64-bit machines, but
905
 
benchmark results on 32-bit machines have been mixed.  Therefore,
906
 
the default is to use base 2**30 on 64-bit machines and base 2**15
907
 
on 32-bit machines; on Unix, there&#8217;s a new configure option
908
 
<code class="xref std std-option docutils literal"><span class="pre">--enable-big-digits</span></code> that can be used to override this default.</p>
909
 
<p>Apart from the performance improvements this change should be
910
 
invisible to end users, with one exception: for testing and
911
 
debugging purposes there&#8217;s a new structseq <code class="xref py py-data docutils literal"><span class="pre">sys.long_info</span></code> that
912
 
provides information about the internal format, giving the number of
913
 
bits per digit and the size in bytes of the C type used to store
914
 
each digit:</p>
915
 
<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>
916
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">long_info</span>
917
 
<span class="go">sys.long_info(bits_per_digit=30, sizeof_digit=4)</span>
918
 
</pre></div>
919
 
</div>
920
 
<p>(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue4258">issue 4258</a>.)</p>
921
 
<p>Another set of changes made long objects a few bytes smaller: 2 bytes
922
 
smaller on 32-bit systems and 6 bytes on 64-bit.
923
 
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue5260">issue 5260</a>.)</p>
924
 
</li>
925
 
<li><p class="first">The division algorithm for long integers has been made faster
926
 
by tightening the inner loop, doing shifts instead of multiplications,
927
 
and fixing an unnecessary extra iteration.
928
 
Various benchmarks show speedups of between 50% and 150% for long
929
 
integer divisions and modulo operations.
930
 
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue5512">issue 5512</a>.)
931
 
Bitwise operations are also significantly faster (initial patch by
932
 
Gregory Smith; <a class="reference external" href="https://bugs.python.org/issue1087418">issue 1087418</a>).</p>
933
 
</li>
934
 
<li><p class="first">The implementation of <code class="docutils literal"><span class="pre">%</span></code> checks for the left-side operand being
935
 
a Python string and special-cases it; this results in a 1-3%
936
 
performance increase for applications that frequently use <code class="docutils literal"><span class="pre">%</span></code>
937
 
with strings, such as templating libraries.
938
 
(Implemented by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue5176">issue 5176</a>.)</p>
939
 
</li>
940
 
<li><p class="first">List comprehensions with an <code class="docutils literal"><span class="pre">if</span></code> condition are compiled into
941
 
faster bytecode.  (Patch by Antoine Pitrou, back-ported to 2.7
942
 
by Jeffrey Yasskin; <a class="reference external" href="https://bugs.python.org/issue4715">issue 4715</a>.)</p>
943
 
</li>
944
 
<li><p class="first">Converting an integer or long integer to a decimal string was made
945
 
faster by special-casing base 10 instead of using a generalized
946
 
conversion function that supports arbitrary bases.
947
 
(Patch by Gawain Bolton; <a class="reference external" href="https://bugs.python.org/issue6713">issue 6713</a>.)</p>
948
 
</li>
949
 
<li><p class="first">The <code class="xref py py-meth docutils literal"><span class="pre">split()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">replace()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">rindex()</span></code>,
950
 
<code class="xref py py-meth docutils literal"><span class="pre">rpartition()</span></code>, and <code class="xref py py-meth docutils literal"><span class="pre">rsplit()</span></code> methods of string-like types
951
 
(strings, Unicode strings, and <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal"><span class="pre">bytearray</span></code></a> objects) now use a
952
 
fast reverse-search algorithm instead of a character-by-character
953
 
scan.  This is sometimes faster by a factor of 10.  (Added by
954
 
Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue7462">issue 7462</a> and <a class="reference external" href="https://bugs.python.org/issue7622">issue 7622</a>.)</p>
955
 
</li>
956
 
<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."><code class="xref py py-mod docutils literal"><span class="pre">pickle</span></code></a> and <code class="xref py py-mod docutils literal"><span class="pre">cPickle</span></code> modules now automatically
957
 
intern the strings used for attribute names, reducing memory usage
958
 
of the objects resulting from unpickling.  (Contributed by Jake
959
 
McGuire; <a class="reference external" href="https://bugs.python.org/issue5084">issue 5084</a>.)</p>
960
 
</li>
961
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">cPickle</span></code> module now special-cases dictionaries,
962
 
nearly halving the time required to pickle them.
963
 
(Contributed by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue5670">issue 5670</a>.)</p>
964
 
</li>
965
 
</ul>
966
 
</div>
967
 
</div>
968
 
<div class="section" id="new-and-improved-modules">
969
 
<h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Permalink to this headline">¶</a></h2>
970
 
<p>As in every release, Python&#8217;s standard library received a number of
971
 
enhancements and bug fixes.  Here&#8217;s a partial list of the most notable
972
 
changes, sorted alphabetically by module name. Consult the
973
 
<code class="file docutils literal"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more complete list of
974
 
changes, or look through the Subversion logs for all the details.</p>
975
 
<ul>
976
 
<li><p class="first">The <a class="reference internal" href="../library/bdb.html#module-bdb" title="bdb: Debugger framework."><code class="xref py py-mod docutils literal"><span class="pre">bdb</span></code></a> module&#8217;s base debugging class <a class="reference internal" href="../library/bdb.html#bdb.Bdb" title="bdb.Bdb"><code class="xref py py-class docutils literal"><span class="pre">Bdb</span></code></a>
977
 
gained a feature for skipping modules.  The constructor
978
 
now takes an iterable containing glob-style patterns such as
979
 
<code class="docutils literal"><span class="pre">django.*</span></code>; the debugger will not step into stack frames
980
 
from a module that matches one of these patterns.
981
 
(Contributed by Maru Newby after a suggestion by
982
 
Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue5142">issue 5142</a>.)</p>
983
 
</li>
984
 
<li><p class="first">The <a class="reference internal" href="../library/binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal"><span class="pre">binascii</span></code></a> module now supports the buffer API, so it can be
985
 
used with <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal"><span class="pre">memoryview</span></code></a> instances and other similar buffer objects.
986
 
(Backported from 3.x by Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue7703">issue 7703</a>.)</p>
987
 
</li>
988
 
<li><p class="first">Updated module: the <code class="xref py py-mod docutils literal"><span class="pre">bsddb</span></code> module has been updated from 4.7.2devel9
989
 
to version 4.8.4 of
990
 
<a class="reference external" href="http://www.jcea.es/programacion/pybsddb.htm">the pybsddb package</a>.
991
 
The new version features better Python 3.x compatibility, various bug fixes,
992
 
and adds several new BerkeleyDB flags and methods.
993
 
(Updated by Jesús Cea Avión; <a class="reference external" href="https://bugs.python.org/issue8156">issue 8156</a>.  The pybsddb
994
 
changelog can be read at <a class="reference external" href="http://hg.jcea.es/pybsddb/file/tip/ChangeLog">http://hg.jcea.es/pybsddb/file/tip/ChangeLog</a>.)</p>
995
 
</li>
996
 
<li><p class="first">The <a class="reference internal" href="../library/bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal"><span class="pre">bz2</span></code></a> module&#8217;s <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal"><span class="pre">BZ2File</span></code></a> now supports the context
997
 
management protocol, so you can write <code class="docutils literal"><span class="pre">with</span> <span class="pre">bz2.BZ2File(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
998
 
(Contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue3860">issue 3860</a>.)</p>
999
 
</li>
1000
 
<li><p class="first">New class: the <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal"><span class="pre">Counter</span></code></a> class in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: Container datatypes"><code class="xref py py-mod docutils literal"><span class="pre">collections</span></code></a>
1001
 
module is useful for tallying data.  <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal"><span class="pre">Counter</span></code></a> instances
1002
 
behave mostly like dictionaries but return zero for missing keys instead of
1003
 
raising a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></a>:</p>
1004
 
<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="k">import</span> <span class="n">Counter</span>
1005
 
<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>
1006
 
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="s1">&#39;here is a sample of english text&#39;</span><span class="p">:</span>
1007
 
<span class="gp">... </span>  <span class="n">c</span><span class="p">[</span><span class="n">letter</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span>
1008
 
<span class="gp">...</span>
1009
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
1010
 
<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>
1011
 
<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>
1012
 
<span class="go">&#39;p&#39;: 1, &#39;r&#39;: 1, &#39;x&#39;: 1})</span>
1013
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span>
1014
 
<span class="go">5</span>
1015
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;z&#39;</span><span class="p">]</span>
1016
 
<span class="go">0</span>
1017
 
</pre></div>
1018
 
</div>
1019
 
<p>There are three additional <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal"><span class="pre">Counter</span></code></a> methods.
1020
 
<a class="reference internal" href="../library/collections.html#collections.Counter.most_common" title="collections.Counter.most_common"><code class="xref py py-meth docutils literal"><span class="pre">most_common()</span></code></a> returns the N most common
1021
 
elements and their counts.  <a class="reference internal" href="../library/collections.html#collections.Counter.elements" title="collections.Counter.elements"><code class="xref py py-meth docutils literal"><span class="pre">elements()</span></code></a>
1022
 
returns an iterator over the contained elements, repeating each
1023
 
element as many times as its count.
1024
 
<a class="reference internal" href="../library/collections.html#collections.Counter.subtract" title="collections.Counter.subtract"><code class="xref py py-meth docutils literal"><span class="pre">subtract()</span></code></a> takes an iterable and
1025
 
subtracts one for each element instead of adding; if the argument is
1026
 
a dictionary or another <code class="xref py py-class docutils literal"><span class="pre">Counter</span></code>, the counts are
1027
 
subtracted.</p>
1028
 
<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="mi">5</span><span class="p">)</span>
1029
 
<span class="go">[(&#39; &#39;, 6), (&#39;e&#39;, 5), (&#39;s&#39;, 3), (&#39;a&#39;, 2), (&#39;i&#39;, 2)]</span>
1030
 
<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>
1031
 
<span class="go">   &#39;a&#39;, &#39;a&#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;,</span>
1032
 
<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>
1033
 
<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>
1034
 
<span class="go">   &#39;s&#39;, &#39;s&#39;, &#39;r&#39;, &#39;t&#39;, &#39;t&#39;, &#39;x&#39;</span>
1035
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span>
1036
 
<span class="go">5</span>
1037
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="s1">&#39;very heavy on the letter e&#39;</span><span class="p">)</span>
1038
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="p">[</span><span class="s1">&#39;e&#39;</span><span class="p">]</span>    <span class="c1"># Count is now lower</span>
1039
 
<span class="go">-1</span>
1040
 
</pre></div>
1041
 
</div>
1042
 
<p>Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue1696199">issue 1696199</a>.</p>
1043
 
<p>New class: <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">OrderedDict</span></code></a> is described in the earlier
1044
 
section <a class="reference internal" href="#pep-0372"><span>PEP 372: Adding an Ordered Dictionary to collections</span></a>.</p>
1045
 
<p>New method: The <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal"><span class="pre">deque</span></code></a> data type now has a
1046
 
<a class="reference internal" href="../library/collections.html#collections.deque.count" title="collections.deque.count"><code class="xref py py-meth docutils literal"><span class="pre">count()</span></code></a> method that returns the number of
1047
 
contained elements equal to the supplied argument <em>x</em>, and a
1048
 
<a class="reference internal" href="../library/collections.html#collections.deque.reverse" title="collections.deque.reverse"><code class="xref py py-meth docutils literal"><span class="pre">reverse()</span></code></a> method that reverses the elements
1049
 
of the deque in-place.  <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><code class="xref py py-class docutils literal"><span class="pre">deque</span></code></a> also exposes its maximum
1050
 
length as the read-only <a class="reference internal" href="../library/collections.html#collections.deque.maxlen" title="collections.deque.maxlen"><code class="xref py py-attr docutils literal"><span class="pre">maxlen</span></code></a> attribute.
1051
 
(Both features added by Raymond Hettinger.)</p>
1052
 
<p>The <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal"><span class="pre">namedtuple</span></code></a> class now has an optional <em>rename</em> parameter.
1053
 
If <em>rename</em> is true, field names that are invalid because they&#8217;ve
1054
 
been repeated or aren&#8217;t legal Python identifiers will be
1055
 
renamed to legal names that are derived from the field&#8217;s
1056
 
position within the list of fields:</p>
1057
 
<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="k">import</span> <span class="n">namedtuple</span>
1058
 
<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="s1">&#39;T&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;field1&#39;</span><span class="p">,</span> <span class="s1">&#39;$illegal&#39;</span><span class="p">,</span> <span class="s1">&#39;for&#39;</span><span class="p">,</span> <span class="s1">&#39;field2&#39;</span><span class="p">],</span> <span class="n">rename</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
1059
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">T</span><span class="o">.</span><span class="n">_fields</span>
1060
 
<span class="go">(&#39;field1&#39;, &#39;_1&#39;, &#39;_2&#39;, &#39;field2&#39;)</span>
1061
 
</pre></div>
1062
 
</div>
1063
 
<p>(Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue1818">issue 1818</a>.)</p>
1064
 
<p>Finally, the <code class="xref py py-class docutils literal"><span class="pre">Mapping</span></code> abstract base class now
1065
 
returns <a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-const docutils literal"><span class="pre">NotImplemented</span></code></a> if a mapping is compared to
1066
 
another type that isn&#8217;t a <code class="xref py py-class docutils literal"><span class="pre">Mapping</span></code>.
1067
 
(Fixed by Daniel Stutzbach; <a class="reference external" href="https://bugs.python.org/issue8729">issue 8729</a>.)</p>
1068
 
</li>
1069
 
<li><p class="first">Constructors for the parsing classes in the <code class="xref py py-mod docutils literal"><span class="pre">ConfigParser</span></code> module now
1070
 
take an <em>allow_no_value</em> parameter, defaulting to false; if true,
1071
 
options without values will be allowed.  For example:</p>
1072
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">ConfigParser</span><span class="o">,</span> <span class="nn">StringIO</span>
1073
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">sample_config</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
1074
 
<span class="gp">... </span><span class="s2">[mysqld]</span>
1075
 
<span class="gp">... </span><span class="s2">user = mysql</span>
1076
 
<span class="gp">... </span><span class="s2">pid-file = /var/run/mysqld/mysqld.pid</span>
1077
 
<span class="gp">... </span><span class="s2">skip-bdb</span>
1078
 
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span>
1079
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="o">.</span><span class="n">RawConfigParser</span><span class="p">(</span><span class="n">allow_no_value</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
1080
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">readfp</span><span class="p">(</span><span class="n">StringIO</span><span class="o">.</span><span class="n">StringIO</span><span class="p">(</span><span class="n">sample_config</span><span class="p">))</span>
1081
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;user&#39;</span><span class="p">)</span>
1082
 
<span class="go">&#39;mysql&#39;</span>
1083
 
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;skip-bdb&#39;</span><span class="p">)</span>
1084
 
<span class="go">None</span>
1085
 
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">config</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mysqld&#39;</span><span class="p">,</span> <span class="s1">&#39;unknown&#39;</span><span class="p">)</span>
1086
 
<span class="gt">Traceback (most recent call last):</span>
1087
 
  <span class="c">...</span>
1088
 
<span class="gr">NoOptionError</span>: <span class="n">No option &#39;unknown&#39; in section: &#39;mysqld&#39;</span>
1089
 
</pre></div>
1090
 
</div>
1091
 
<p>(Contributed by Mats Kindahl; <a class="reference external" href="https://bugs.python.org/issue7005">issue 7005</a>.)</p>
1092
 
</li>
1093
 
<li><p class="first">Deprecated function: <code class="xref py py-func docutils literal"><span class="pre">contextlib.nested()</span></code>, which allows
1094
 
handling more than one context manager with a single <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a>
1095
 
statement, has been deprecated, because the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement
1096
 
now supports multiple context managers.</p>
1097
 
</li>
1098
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">cookielib</span></code> module now ignores cookies that have an invalid
1099
 
version field, one that doesn&#8217;t contain an integer value.  (Fixed by
1100
 
John J. Lee; <a class="reference external" href="https://bugs.python.org/issue3924">issue 3924</a>.)</p>
1101
 
</li>
1102
 
<li><p class="first">The <a class="reference internal" href="../library/copy.html#module-copy" title="copy: Shallow and deep copy operations."><code class="xref py py-mod docutils literal"><span class="pre">copy</span></code></a> module&#8217;s <a class="reference internal" href="../library/copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal"><span class="pre">deepcopy()</span></code></a> function will now
1103
 
correctly copy bound instance methods.  (Implemented by
1104
 
Robert Collins; <a class="reference external" href="https://bugs.python.org/issue1515">issue 1515</a>.)</p>
1105
 
</li>
1106
 
<li><p class="first">The <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal"><span class="pre">ctypes</span></code></a> module now always converts <code class="docutils literal"><span class="pre">None</span></code> to a C NULL
1107
 
pointer for arguments declared as pointers.  (Changed by Thomas
1108
 
Heller; <a class="reference external" href="https://bugs.python.org/issue4606">issue 4606</a>.)  The underlying <a class="reference external" href="http://sourceware.org/libffi/">libffi library</a> has been updated to version
1109
 
3.0.9, containing various fixes for different platforms.  (Updated
1110
 
by Matthias Klose; <a class="reference external" href="https://bugs.python.org/issue8142">issue 8142</a>.)</p>
1111
 
</li>
1112
 
<li><p class="first">New method: the <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><code class="xref py py-mod docutils literal"><span class="pre">datetime</span></code></a> module&#8217;s <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal"><span class="pre">timedelta</span></code></a> class
1113
 
gained a <a class="reference internal" href="../library/datetime.html#datetime.timedelta.total_seconds" title="datetime.timedelta.total_seconds"><code class="xref py py-meth docutils literal"><span class="pre">total_seconds()</span></code></a> method that returns the
1114
 
number of seconds in the duration.  (Contributed by Brian Quinlan; <a class="reference external" href="https://bugs.python.org/issue5788">issue 5788</a>.)</p>
1115
 
</li>
1116
 
<li><p class="first">New method: the <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> class gained a
1117
 
<a class="reference internal" href="../library/decimal.html#decimal.Decimal.from_float" title="decimal.Decimal.from_float"><code class="xref py py-meth docutils literal"><span class="pre">from_float()</span></code></a> class method that performs an exact
1118
 
conversion of a floating-point number to a <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>.
1119
 
This exact conversion strives for the
1120
 
closest decimal approximation to the floating-point representation&#8217;s value;
1121
 
the resulting decimal value will therefore still include the inaccuracy,
1122
 
if any.
1123
 
For example, <code class="docutils literal"><span class="pre">Decimal.from_float(0.1)</span></code> returns
1124
 
<code class="docutils literal"><span class="pre">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span></code>.
1125
 
(Implemented by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue4796">issue 4796</a>.)</p>
1126
 
<p>Comparing instances of <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> with floating-point
1127
 
numbers now produces sensible results based on the numeric values
1128
 
of the operands.  Previously such comparisons would fall back to
1129
 
Python&#8217;s default rules for comparing objects, which produced arbitrary
1130
 
results based on their type.  Note that you still cannot combine
1131
 
<code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code> and floating-point in other operations such as addition,
1132
 
since you should be explicitly choosing how to convert between float and
1133
 
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a>.  (Fixed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue2531">issue 2531</a>.)</p>
1134
 
<p>The constructor for <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> now accepts
1135
 
floating-point numbers (added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue8257">issue 8257</a>)
1136
 
and non-European Unicode characters such as Arabic-Indic digits
1137
 
(contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue6595">issue 6595</a>).</p>
1138
 
<p>Most of the methods of the <a class="reference internal" href="../library/decimal.html#decimal.Context" title="decimal.Context"><code class="xref py py-class docutils literal"><span class="pre">Context</span></code></a> class now accept integers
1139
 
as well as <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances; the only exceptions are the
1140
 
<a class="reference internal" href="../library/decimal.html#decimal.Context.canonical" title="decimal.Context.canonical"><code class="xref py py-meth docutils literal"><span class="pre">canonical()</span></code></a> and <a class="reference internal" href="../library/decimal.html#decimal.Context.is_canonical" title="decimal.Context.is_canonical"><code class="xref py py-meth docutils literal"><span class="pre">is_canonical()</span></code></a>
1141
 
methods.  (Patch by Juan José Conti; <a class="reference external" href="https://bugs.python.org/issue7633">issue 7633</a>.)</p>
1142
 
<p>When using <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances with a string&#8217;s
1143
 
<a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal"><span class="pre">format()</span></code></a> method, the default alignment was previously
1144
 
left-alignment.  This has been changed to right-alignment, which is
1145
 
more sensible for numeric types.  (Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue6857">issue 6857</a>.)</p>
1146
 
<p>Comparisons involving a signaling NaN value (or <code class="docutils literal"><span class="pre">sNAN</span></code>) now signal
1147
 
<code class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></code> instead of silently returning a true or
1148
 
false value depending on the comparison operator.  Quiet NaN values
1149
 
(or <code class="docutils literal"><span class="pre">NaN</span></code>) are now hashable.  (Fixed by Mark Dickinson;
1150
 
<a class="reference external" href="https://bugs.python.org/issue7279">issue 7279</a>.)</p>
1151
 
</li>
1152
 
<li><p class="first">The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal"><span class="pre">difflib</span></code></a> module now produces output that is more
1153
 
compatible with modern <strong class="command">diff</strong>/<strong class="command">patch</strong> tools
1154
 
through one small change, using a tab character instead of spaces as
1155
 
a separator in the header giving the filename.  (Fixed by Anatoly
1156
 
Techtonik; <a class="reference external" href="https://bugs.python.org/issue7585">issue 7585</a>.)</p>
1157
 
</li>
1158
 
<li><p class="first">The Distutils <code class="docutils literal"><span class="pre">sdist</span></code> command now always regenerates the
1159
 
<code class="file docutils literal"><span class="pre">MANIFEST</span></code> file, since even if the <code class="file docutils literal"><span class="pre">MANIFEST.in</span></code> or
1160
 
<code class="file docutils literal"><span class="pre">setup.py</span></code> files haven&#8217;t been modified, the user might have
1161
 
created some new files that should be included.
1162
 
(Fixed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue8688">issue 8688</a>.)</p>
1163
 
</li>
1164
 
<li><p class="first">The <a class="reference internal" href="../library/doctest.html#module-doctest" title="doctest: Test pieces of code within docstrings."><code class="xref py py-mod docutils literal"><span class="pre">doctest</span></code></a> module&#8217;s <code class="xref py py-const docutils literal"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code> flag
1165
 
will now ignore the name of the module containing the exception
1166
 
being tested.  (Patch by Lennart Regebro; <a class="reference external" href="https://bugs.python.org/issue7490">issue 7490</a>.)</p>
1167
 
</li>
1168
 
<li><p class="first">The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><code class="xref py py-mod docutils literal"><span class="pre">email</span></code></a> module&#8217;s <a class="reference internal" href="../library/email.message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal"><span class="pre">Message</span></code></a> class will
1169
 
now accept a Unicode-valued payload, automatically converting the
1170
 
payload to the encoding specified by <code class="xref py py-attr docutils literal"><span class="pre">output_charset</span></code>.
1171
 
(Added by R. David Murray; <a class="reference external" href="https://bugs.python.org/issue1368247">issue 1368247</a>.)</p>
1172
 
</li>
1173
 
<li><p class="first">The <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal"><span class="pre">Fraction</span></code></a> class now accepts a single float or
1174
 
<a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instance, or two rational numbers, as
1175
 
arguments to its constructor.  (Implemented by Mark Dickinson;
1176
 
rationals added in <a class="reference external" href="https://bugs.python.org/issue5812">issue 5812</a>, and float/decimal in
1177
 
<a class="reference external" href="https://bugs.python.org/issue8294">issue 8294</a>.)</p>
1178
 
<p>Ordering comparisons (<code class="docutils literal"><span class="pre">&lt;</span></code>, <code class="docutils literal"><span class="pre">&lt;=</span></code>, <code class="docutils literal"><span class="pre">&gt;</span></code>, <code class="docutils literal"><span class="pre">&gt;=</span></code>) between
1179
 
fractions and complex numbers now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a>.
1180
 
This fixes an oversight, making the <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal"><span class="pre">Fraction</span></code></a>
1181
 
match the other numeric types.</p>
1182
 
</li>
1183
 
<li><p class="first">New class: <a class="reference internal" href="../library/ftplib.html#ftplib.FTP_TLS" title="ftplib.FTP_TLS"><code class="xref py py-class docutils literal"><span class="pre">FTP_TLS</span></code></a> in
1184
 
the <a class="reference internal" href="../library/ftplib.html#module-ftplib" title="ftplib: FTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">ftplib</span></code></a> module provides secure FTP
1185
 
connections using TLS encapsulation of authentication as well as
1186
 
subsequent control and data transfers.
1187
 
(Contributed by Giampaolo Rodola; <a class="reference external" href="https://bugs.python.org/issue2054">issue 2054</a>.)</p>
1188
 
<p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP.storbinary" title="ftplib.FTP.storbinary"><code class="xref py py-meth docutils literal"><span class="pre">storbinary()</span></code></a> method for binary uploads can now restart
1189
 
uploads thanks to an added <em>rest</em> parameter (patch by Pablo Mouzo;
1190
 
<a class="reference external" href="https://bugs.python.org/issue6845">issue 6845</a>.)</p>
1191
 
</li>
1192
 
<li><p class="first">New class decorator: <a class="reference internal" href="../library/functools.html#functools.total_ordering" title="functools.total_ordering"><code class="xref py py-func docutils literal"><span class="pre">total_ordering()</span></code></a> in the <a class="reference internal" href="../library/functools.html#module-functools" title="functools: Higher-order functions and operations on callable objects."><code class="xref py py-mod docutils literal"><span class="pre">functools</span></code></a>
1193
 
module takes a class that defines an <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></code></a> method and one of
1194
 
<a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal"><span class="pre">__lt__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__le__" title="object.__le__"><code class="xref py py-meth docutils literal"><span class="pre">__le__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__gt__" title="object.__gt__"><code class="xref py py-meth docutils literal"><span class="pre">__gt__()</span></code></a>, or <a class="reference internal" href="../reference/datamodel.html#object.__ge__" title="object.__ge__"><code class="xref py py-meth docutils literal"><span class="pre">__ge__()</span></code></a>,
1195
 
and generates the missing comparison methods.  Since the
1196
 
<code class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></code> method is being deprecated in Python 3.x,
1197
 
this decorator makes it easier to define ordered classes.
1198
 
(Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue5479">issue 5479</a>.)</p>
1199
 
<p>New function: <a class="reference internal" href="../library/functools.html#functools.cmp_to_key" title="functools.cmp_to_key"><code class="xref py py-func docutils literal"><span class="pre">cmp_to_key()</span></code></a> will take an old-style comparison
1200
 
function that expects two arguments and return a new callable that
1201
 
can be used as the <em>key</em> parameter to functions such as
1202
 
<a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal"><span class="pre">sorted()</span></code></a>, <a class="reference internal" href="../library/functions.html#min" title="min"><code class="xref py py-func docutils literal"><span class="pre">min()</span></code></a> and <a class="reference internal" href="../library/functions.html#max" title="max"><code class="xref py py-func docutils literal"><span class="pre">max()</span></code></a>, etc.  The primary
1203
 
intended use is to help with making code compatible with Python 3.x.
1204
 
(Added by Raymond Hettinger.)</p>
1205
 
</li>
1206
 
<li><p class="first">New function: the <a class="reference internal" href="../library/gc.html#module-gc" title="gc: Interface to the cycle-detecting garbage collector."><code class="xref py py-mod docutils literal"><span class="pre">gc</span></code></a> module&#8217;s <a class="reference internal" href="../library/gc.html#gc.is_tracked" title="gc.is_tracked"><code class="xref py py-func docutils literal"><span class="pre">is_tracked()</span></code></a> returns
1207
 
true if a given instance is tracked by the garbage collector, false
1208
 
otherwise. (Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue4688">issue 4688</a>.)</p>
1209
 
</li>
1210
 
<li><p class="first">The <a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a> module&#8217;s <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> now supports the context
1211
 
management protocol, so you can write <code class="docutils literal"><span class="pre">with</span> <span class="pre">gzip.GzipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>
1212
 
(contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue3860">issue 3860</a>), and it now implements
1213
 
the <a class="reference internal" href="../library/io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></code></a> ABC, so you can wrap it with
1214
 
<a class="reference internal" href="../library/io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedReader</span></code></a> for faster processing
1215
 
(contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue7471">issue 7471</a>).
1216
 
It&#8217;s also now possible to override the modification time
1217
 
recorded in a gzipped file by providing an optional timestamp to
1218
 
the constructor.  (Contributed by Jacques Frechet; <a class="reference external" href="https://bugs.python.org/issue4272">issue 4272</a>.)</p>
1219
 
<p>Files in gzip format can be padded with trailing zero bytes; the
1220
 
<a class="reference internal" href="../library/gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a> module will now consume these trailing bytes.  (Fixed by
1221
 
Tadek Pietraszek and Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue2846">issue 2846</a>.)</p>
1222
 
</li>
1223
 
<li><p class="first">New attribute: the <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal"><span class="pre">hashlib</span></code></a> module now has an <code class="xref py py-attr docutils literal"><span class="pre">algorithms</span></code>
1224
 
attribute containing a tuple naming the supported algorithms.
1225
 
In Python 2.7, <code class="docutils literal"><span class="pre">hashlib.algorithms</span></code> contains
1226
 
<code class="docutils literal"><span class="pre">('md5',</span> <span class="pre">'sha1',</span> <span class="pre">'sha224',</span> <span class="pre">'sha256',</span> <span class="pre">'sha384',</span> <span class="pre">'sha512')</span></code>.
1227
 
(Contributed by Carl Chenet; <a class="reference external" href="https://bugs.python.org/issue7418">issue 7418</a>.)</p>
1228
 
</li>
1229
 
<li><p class="first">The default <code class="xref py py-class docutils literal"><span class="pre">HTTPResponse</span></code> class used by the <code class="xref py py-mod docutils literal"><span class="pre">httplib</span></code> module now
1230
 
supports buffering, resulting in much faster reading of HTTP responses.
1231
 
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue4879">issue 4879</a>.)</p>
1232
 
<p>The <code class="xref py py-class docutils literal"><span class="pre">HTTPConnection</span></code> and <code class="xref py py-class docutils literal"><span class="pre">HTTPSConnection</span></code> classes
1233
 
now support a <em>source_address</em> parameter, a <code class="docutils literal"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
1234
 
giving the source address that will be used for the connection.
1235
 
(Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue3972">issue 3972</a>.)</p>
1236
 
</li>
1237
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">ihooks</span></code> module now supports relative imports.  Note that
1238
 
<code class="xref py py-mod docutils literal"><span class="pre">ihooks</span></code> is an older module for customizing imports,
1239
 
superseded by the <code class="xref py py-mod docutils literal"><span class="pre">imputil</span></code> module added in Python 2.0.
1240
 
(Relative import support added by Neil Schemenauer.)</p>
1241
 
</li>
1242
 
<li><p class="first">The <a class="reference internal" href="../library/imaplib.html#module-imaplib" title="imaplib: IMAP4 protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">imaplib</span></code></a> module now supports IPv6 addresses.
1243
 
(Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue1655">issue 1655</a>.)</p>
1244
 
</li>
1245
 
<li><p class="first">New function: the <a class="reference internal" href="../library/inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal"><span class="pre">inspect</span></code></a> module&#8217;s <a class="reference internal" href="../library/inspect.html#inspect.getcallargs" title="inspect.getcallargs"><code class="xref py py-func docutils literal"><span class="pre">getcallargs()</span></code></a>
1246
 
takes a callable and its positional and keyword arguments,
1247
 
and figures out which of the callable&#8217;s parameters will receive each argument,
1248
 
returning a dictionary mapping argument names to their values.  For example:</p>
1249
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getcallargs</span>
1250
 
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span>
1251
 
<span class="gp">... </span>    <span class="k">pass</span>
1252
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
1253
 
<span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;pos&#39;: (3,), &#39;named&#39;: {}}</span>
1254
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
1255
 
<span class="go">{&#39;a&#39;: 2, &#39;b&#39;: 1, &#39;pos&#39;: (), &#39;named&#39;: {&#39;x&#39;: 4}}</span>
1256
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
1257
 
<span class="gt">Traceback (most recent call last):</span>
1258
 
<span class="c">...</span>
1259
 
<span class="gr">TypeError</span>: <span class="n">f() takes at least 1 argument (0 given)</span>
1260
 
</pre></div>
1261
 
</div>
1262
 
<p>Contributed by George Sakkis; <a class="reference external" href="https://bugs.python.org/issue3135">issue 3135</a>.</p>
1263
 
</li>
1264
 
<li><p class="first">Updated module: The <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><code class="xref py py-mod docutils literal"><span class="pre">io</span></code></a> library has been upgraded to the version shipped with
1265
 
Python 3.1.  For 3.1, the I/O library was entirely rewritten in C
1266
 
and is 2 to 20 times faster depending on the task being performed.  The
1267
 
original Python version was renamed to the <code class="xref py py-mod docutils literal"><span class="pre">_pyio</span></code> module.</p>
1268
 
<p>One minor resulting change: the <a class="reference internal" href="../library/io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal"><span class="pre">io.TextIOBase</span></code></a> class now
1269
 
has an <code class="xref py py-attr docutils literal"><span class="pre">errors</span></code> attribute giving the error setting
1270
 
used for encoding and decoding errors (one of <code class="docutils literal"><span class="pre">'strict'</span></code>, <code class="docutils literal"><span class="pre">'replace'</span></code>,
1271
 
<code class="docutils literal"><span class="pre">'ignore'</span></code>).</p>
1272
 
<p>The <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal"><span class="pre">io.FileIO</span></code></a> class now raises an <a class="reference internal" href="../library/exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal"><span class="pre">OSError</span></code></a> when passed
1273
 
an invalid file descriptor.  (Implemented by Benjamin Peterson;
1274
 
<a class="reference external" href="https://bugs.python.org/issue4991">issue 4991</a>.)  The <a class="reference internal" href="../library/io.html#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code></a> method now preserves the
1275
 
file position; previously it would change the file position to the
1276
 
end of the new file.  (Fixed by Pascal Chambon; <a class="reference external" href="https://bugs.python.org/issue6939">issue 6939</a>.)</p>
1277
 
</li>
1278
 
<li><p class="first">New function: <code class="docutils literal"><span class="pre">itertools.compress(data,</span> <span class="pre">selectors)</span></code> takes two
1279
 
iterators.  Elements of <em>data</em> are returned if the corresponding
1280
 
value in <em>selectors</em> is true:</p>
1281
 
<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="s1">&#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>
1282
 
  <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>
1283
 
</pre></div>
1284
 
</div>
1285
 
<p>New function: <code class="docutils literal"><span class="pre">itertools.combinations_with_replacement(iter,</span> <span class="pre">r)</span></code>
1286
 
returns all the possible <em>r</em>-length combinations of elements from the
1287
 
iterable <em>iter</em>.  Unlike <a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><code class="xref py py-func docutils literal"><span class="pre">combinations()</span></code></a>, individual elements
1288
 
can be repeated in the generated combinations:</p>
1289
 
<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="s1">&#39;abc&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="o">=&gt;</span>
1290
 
  <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">),</span>
1291
 
  <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
1292
 
</pre></div>
1293
 
</div>
1294
 
<p>Note that elements are treated as unique depending on their position
1295
 
in the input, not their actual values.</p>
1296
 
<p>The <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal"><span class="pre">itertools.count()</span></code></a> function now has a <em>step</em> argument that
1297
 
allows incrementing by values other than 1.  <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal"><span class="pre">count()</span></code></a> also
1298
 
now allows keyword arguments, and using non-integer values such as
1299
 
floats or <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances.  (Implemented by Raymond
1300
 
Hettinger; <a class="reference external" href="https://bugs.python.org/issue5032">issue 5032</a>.)</p>
1301
 
<p><a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><code class="xref py py-func docutils literal"><span class="pre">itertools.combinations()</span></code></a> and <a class="reference internal" href="../library/itertools.html#itertools.product" title="itertools.product"><code class="xref py py-func docutils literal"><span class="pre">itertools.product()</span></code></a>
1302
 
previously raised <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> for values of <em>r</em> larger than
1303
 
the input iterable.  This was deemed a specification error, so they
1304
 
now return an empty iterator.  (Fixed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue4816">issue 4816</a>.)</p>
1305
 
</li>
1306
 
<li><p class="first">Updated module: The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal"><span class="pre">json</span></code></a> module was upgraded to version 2.0.9 of the
1307
 
simplejson package, which includes a C extension that makes
1308
 
encoding and decoding faster.
1309
 
(Contributed by Bob Ippolito; <a class="reference external" href="https://bugs.python.org/issue4136">issue 4136</a>.)</p>
1310
 
<p>To support the new <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal"><span class="pre">collections.OrderedDict</span></code></a> type, <a class="reference internal" href="../library/json.html#json.load" title="json.load"><code class="xref py py-func docutils literal"><span class="pre">json.load()</span></code></a>
1311
 
now has an optional <em>object_pairs_hook</em> parameter that will be called
1312
 
with any object literal that decodes to a list of pairs.
1313
 
(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue5381">issue 5381</a>.)</p>
1314
 
</li>
1315
 
<li><p class="first">The <a class="reference internal" href="../library/mailbox.html#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><code class="xref py py-mod docutils literal"><span class="pre">mailbox</span></code></a> module&#8217;s <a class="reference internal" href="../library/mailbox.html#mailbox.Maildir" title="mailbox.Maildir"><code class="xref py py-class docutils literal"><span class="pre">Maildir</span></code></a> class now records the
1316
 
timestamp on the directories it reads, and only re-reads them if the
1317
 
modification time has subsequently changed.  This improves
1318
 
performance by avoiding unneeded directory scans.  (Fixed by
1319
 
A.M. Kuchling and Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue1607951">issue 1607951</a>, <a class="reference external" href="https://bugs.python.org/issue6896">issue 6896</a>.)</p>
1320
 
</li>
1321
 
<li><p class="first">New functions: the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><code class="xref py py-mod docutils literal"><span class="pre">math</span></code></a> module gained
1322
 
<a class="reference internal" href="../library/math.html#math.erf" title="math.erf"><code class="xref py py-func docutils literal"><span class="pre">erf()</span></code></a> and <a class="reference internal" href="../library/math.html#math.erfc" title="math.erfc"><code class="xref py py-func docutils literal"><span class="pre">erfc()</span></code></a> for the error function and the complementary error function,
1323
 
<a class="reference internal" href="../library/math.html#math.expm1" title="math.expm1"><code class="xref py py-func docutils literal"><span class="pre">expm1()</span></code></a> which computes <code class="docutils literal"><span class="pre">e**x</span> <span class="pre">-</span> <span class="pre">1</span></code> with more precision than
1324
 
using <a class="reference internal" href="../library/math.html#math.exp" title="math.exp"><code class="xref py py-func docutils literal"><span class="pre">exp()</span></code></a> and subtracting 1,
1325
 
<a class="reference internal" href="../library/math.html#math.gamma" title="math.gamma"><code class="xref py py-func docutils literal"><span class="pre">gamma()</span></code></a> for the Gamma function, and
1326
 
<a class="reference internal" href="../library/math.html#math.lgamma" title="math.lgamma"><code class="xref py py-func docutils literal"><span class="pre">lgamma()</span></code></a> for the natural log of the Gamma function.
1327
 
(Contributed by Mark Dickinson and nirinA raseliarison; <a class="reference external" href="https://bugs.python.org/issue3366">issue 3366</a>.)</p>
1328
 
</li>
1329
 
<li><p class="first">The <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></code></a> module&#8217;s <code class="xref py py-class docutils literal"><span class="pre">Manager*</span></code> classes
1330
 
can now be passed a callable that will be called whenever
1331
 
a subprocess is started, along with a set of arguments that will be
1332
 
passed to the callable.
1333
 
(Contributed by lekma; <a class="reference external" href="https://bugs.python.org/issue5585">issue 5585</a>.)</p>
1334
 
<p>The <code class="xref py py-class docutils literal"><span class="pre">Pool</span></code> class, which controls a pool of worker processes,
1335
 
now has an optional <em>maxtasksperchild</em> parameter.  Worker processes
1336
 
will perform the specified number of tasks and then exit, causing the
1337
 
<code class="xref py py-class docutils literal"><span class="pre">Pool</span></code> to start a new worker.  This is useful if tasks may leak
1338
 
memory or other resources, or if some tasks will cause the worker to
1339
 
become very large.
1340
 
(Contributed by Charles Cazabon; <a class="reference external" href="https://bugs.python.org/issue6963">issue 6963</a>.)</p>
1341
 
</li>
1342
 
<li><p class="first">The <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal"><span class="pre">nntplib</span></code></a> module now supports IPv6 addresses.
1343
 
(Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue1664">issue 1664</a>.)</p>
1344
 
</li>
1345
 
<li><p class="first">New functions: the <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><code class="xref py py-mod docutils literal"><span class="pre">os</span></code></a> module wraps the following POSIX system
1346
 
calls: <a class="reference internal" href="../library/os.html#os.getresgid" title="os.getresgid"><code class="xref py py-func docutils literal"><span class="pre">getresgid()</span></code></a> and <a class="reference internal" href="../library/os.html#os.getresuid" title="os.getresuid"><code class="xref py py-func docutils literal"><span class="pre">getresuid()</span></code></a>, which return the
1347
 
real, effective, and saved GIDs and UIDs;
1348
 
<a class="reference internal" href="../library/os.html#os.setresgid" title="os.setresgid"><code class="xref py py-func docutils literal"><span class="pre">setresgid()</span></code></a> and <a class="reference internal" href="../library/os.html#os.setresuid" title="os.setresuid"><code class="xref py py-func docutils literal"><span class="pre">setresuid()</span></code></a>, which set
1349
 
real, effective, and saved GIDs and UIDs to new values;
1350
 
<a class="reference internal" href="../library/os.html#os.initgroups" title="os.initgroups"><code class="xref py py-func docutils literal"><span class="pre">initgroups()</span></code></a>, which initialize the group access list
1351
 
for the current process.  (GID/UID functions
1352
 
contributed by Travis H.; <a class="reference external" href="https://bugs.python.org/issue6508">issue 6508</a>.  Support for initgroups added
1353
 
by Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue7333">issue 7333</a>.)</p>
1354
 
<p>The <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">os.fork()</span></code></a> function now re-initializes the import lock in
1355
 
the child process; this fixes problems on Solaris when <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">fork()</span></code></a>
1356
 
is called from a thread.  (Fixed by Zsolt Cserna; <a class="reference external" href="https://bugs.python.org/issue7242">issue 7242</a>.)</p>
1357
 
</li>
1358
 
<li><p class="first">In the <a class="reference internal" href="../library/os.path.html#module-os.path" title="os.path: Operations on pathnames."><code class="xref py py-mod docutils literal"><span class="pre">os.path</span></code></a> module, the <a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-func docutils literal"><span class="pre">normpath()</span></code></a> and
1359
 
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal"><span class="pre">abspath()</span></code></a> functions now preserve Unicode; if their input path
1360
 
is a Unicode string, the return value is also a Unicode string.
1361
 
(<a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-meth docutils literal"><span class="pre">normpath()</span></code></a> fixed by Matt Giuca in <a class="reference external" href="https://bugs.python.org/issue5827">issue 5827</a>;
1362
 
<a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-meth docutils literal"><span class="pre">abspath()</span></code></a> fixed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue3426">issue 3426</a>.)</p>
1363
 
</li>
1364
 
<li><p class="first">The <a class="reference internal" href="../library/pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><code class="xref py py-mod docutils literal"><span class="pre">pydoc</span></code></a> module now has help for the various symbols that Python
1365
 
uses.  You can now do <code class="docutils literal"><span class="pre">help('&lt;&lt;')</span></code> or <code class="docutils literal"><span class="pre">help('&#64;')</span></code>, for example.
1366
 
(Contributed by David Laban; <a class="reference external" href="https://bugs.python.org/issue4739">issue 4739</a>.)</p>
1367
 
</li>
1368
 
<li><p class="first">The <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal"><span class="pre">re</span></code></a> module&#8217;s <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal"><span class="pre">split()</span></code></a>, <a class="reference internal" href="../library/re.html#re.sub" title="re.sub"><code class="xref py py-func docutils literal"><span class="pre">sub()</span></code></a>, and <a class="reference internal" href="../library/re.html#re.subn" title="re.subn"><code class="xref py py-func docutils literal"><span class="pre">subn()</span></code></a>
1369
 
now accept an optional <em>flags</em> argument, for consistency with the
1370
 
other functions in the module.  (Added by Gregory P. Smith.)</p>
1371
 
</li>
1372
 
<li><p class="first">New function: <a class="reference internal" href="../library/runpy.html#runpy.run_path" title="runpy.run_path"><code class="xref py py-func docutils literal"><span class="pre">run_path()</span></code></a> in the <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal"><span class="pre">runpy</span></code></a> module
1373
 
will execute the code at a provided <em>path</em> argument.  <em>path</em> can be
1374
 
the path of a Python source file (<code class="file docutils literal"><span class="pre">example.py</span></code>), a compiled
1375
 
bytecode file (<code class="file docutils literal"><span class="pre">example.pyc</span></code>), a directory
1376
 
(<code class="file docutils literal"><span class="pre">./package/</span></code>), or a zip archive (<code class="file docutils literal"><span class="pre">example.zip</span></code>).  If a
1377
 
directory or zip path is provided, it will be added to the front of
1378
 
<code class="docutils literal"><span class="pre">sys.path</span></code> and the module <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal"><span class="pre">__main__</span></code></a> will be imported.  It&#8217;s
1379
 
expected that the directory or zip contains a <code class="file docutils literal"><span class="pre">__main__.py</span></code>;
1380
 
if it doesn&#8217;t, some other <code class="file docutils literal"><span class="pre">__main__.py</span></code> might be imported from
1381
 
a location later in <code class="docutils literal"><span class="pre">sys.path</span></code>.  This makes more of the machinery
1382
 
of <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><code class="xref py py-mod docutils literal"><span class="pre">runpy</span></code></a> available to scripts that want to mimic the way
1383
 
Python&#8217;s command line processes an explicit path name.
1384
 
(Added by Nick Coghlan; <a class="reference external" href="https://bugs.python.org/issue6816">issue 6816</a>.)</p>
1385
 
</li>
1386
 
<li><p class="first">New function: in the <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a> module, <a class="reference internal" href="../library/shutil.html#shutil.make_archive" title="shutil.make_archive"><code class="xref py py-func docutils literal"><span class="pre">make_archive()</span></code></a>
1387
 
takes a filename, archive type (zip or tar-format), and a directory
1388
 
path, and creates an archive containing the directory&#8217;s contents.
1389
 
(Added by Tarek Ziadé.)</p>
1390
 
<p><a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><code class="xref py py-mod docutils literal"><span class="pre">shutil</span></code></a>&#8216;s <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal"><span class="pre">copyfile()</span></code></a> and <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><code class="xref py py-func docutils literal"><span class="pre">copytree()</span></code></a>
1391
 
functions now raise a <code class="xref py py-exc docutils literal"><span class="pre">SpecialFileError</span></code> exception when
1392
 
asked to copy a named pipe.  Previously the code would treat
1393
 
named pipes like a regular file by opening them for reading, and
1394
 
this would block indefinitely.  (Fixed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue3002">issue 3002</a>.)</p>
1395
 
</li>
1396
 
<li><p class="first">The <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><code class="xref py py-mod docutils literal"><span class="pre">signal</span></code></a> module no longer re-installs the signal handler
1397
 
unless this is truly necessary, which fixes a bug that could make it
1398
 
impossible to catch the EINTR signal robustly.  (Fixed by
1399
 
Charles-Francois Natali; <a class="reference external" href="https://bugs.python.org/issue8354">issue 8354</a>.)</p>
1400
 
</li>
1401
 
<li><p class="first">New functions: in the <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> module, three new functions
1402
 
return various site- and user-specific paths.
1403
 
<a class="reference internal" href="../library/site.html#site.getsitepackages" title="site.getsitepackages"><code class="xref py py-func docutils literal"><span class="pre">getsitepackages()</span></code></a> returns a list containing all
1404
 
global site-packages directories,
1405
 
<a class="reference internal" href="../library/site.html#site.getusersitepackages" title="site.getusersitepackages"><code class="xref py py-func docutils literal"><span class="pre">getusersitepackages()</span></code></a> returns the path of the user&#8217;s
1406
 
site-packages directory, and
1407
 
<a class="reference internal" href="../library/site.html#site.getuserbase" title="site.getuserbase"><code class="xref py py-func docutils literal"><span class="pre">getuserbase()</span></code></a> returns the value of the <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal"><span class="pre">USER_BASE</span></code>
1408
 
environment variable, giving the path to a directory that can be used
1409
 
to store data.
1410
 
(Contributed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue6693">issue 6693</a>.)</p>
1411
 
<p>The <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal"><span class="pre">site</span></code></a> module now reports exceptions occurring
1412
 
when the <code class="xref py py-mod docutils literal"><span class="pre">sitecustomize</span></code> module is imported, and will no longer
1413
 
catch and swallow the <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></code></a> exception.  (Fixed by
1414
 
Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue3137">issue 3137</a>.)</p>
1415
 
</li>
1416
 
<li><p class="first">The <a class="reference internal" href="../library/socket.html#socket.create_connection" title="socket.create_connection"><code class="xref py py-func docutils literal"><span class="pre">create_connection()</span></code></a> function
1417
 
gained a <em>source_address</em> parameter, a <code class="docutils literal"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
1418
 
giving the source address that will be used for the connection.
1419
 
(Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue3972">issue 3972</a>.)</p>
1420
 
<p>The <a class="reference internal" href="../library/socket.html#socket.socket.recv_into" title="socket.socket.recv_into"><code class="xref py py-meth docutils literal"><span class="pre">recv_into()</span></code></a> and <a class="reference internal" href="../library/socket.html#socket.socket.recvfrom_into" title="socket.socket.recvfrom_into"><code class="xref py py-meth docutils literal"><span class="pre">recvfrom_into()</span></code></a>
1421
 
methods will now write into objects that support the buffer API, most usefully
1422
 
the <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal"><span class="pre">bytearray</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal"><span class="pre">memoryview</span></code></a> objects.  (Implemented by
1423
 
Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8104">issue 8104</a>.)</p>
1424
 
</li>
1425
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">SocketServer</span></code> module&#8217;s <code class="xref py py-class docutils literal"><span class="pre">TCPServer</span></code> class now
1426
 
supports socket timeouts and disabling the Nagle algorithm.
1427
 
The <code class="xref py py-attr docutils literal"><span class="pre">disable_nagle_algorithm</span></code> class attribute
1428
 
defaults to False; if overridden to be True,
1429
 
new request connections will have the TCP_NODELAY option set to
1430
 
prevent buffering many small sends into a single TCP packet.
1431
 
The <code class="xref py py-attr docutils literal"><span class="pre">timeout</span></code> class attribute can hold
1432
 
a timeout in seconds that will be applied to the request socket; if
1433
 
no request is received within that time, <code class="xref py py-meth docutils literal"><span class="pre">handle_timeout()</span></code>
1434
 
will be called and <code class="xref py py-meth docutils literal"><span class="pre">handle_request()</span></code> will return.
1435
 
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue6192">issue 6192</a> and <a class="reference external" href="https://bugs.python.org/issue6267">issue 6267</a>.)</p>
1436
 
</li>
1437
 
<li><p class="first">Updated module: the <a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><code class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></code></a> module has been updated to
1438
 
version 2.6.0 of the <a class="reference external" href="http://code.google.com/p/pysqlite/">pysqlite package</a>. Version 2.6.0 includes a number of bugfixes, and adds
1439
 
the ability to load SQLite extensions from shared libraries.
1440
 
Call the <code class="docutils literal"><span class="pre">enable_load_extension(True)</span></code> method to enable extensions,
1441
 
and then call <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.load_extension" title="sqlite3.Connection.load_extension"><code class="xref py py-meth docutils literal"><span class="pre">load_extension()</span></code></a> to load a particular shared library.
1442
 
(Updated by Gerhard Häring.)</p>
1443
 
</li>
1444
 
<li><p class="first">The <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a> module&#8217;s <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal"><span class="pre">SSLSocket</span></code></a> objects now support the
1445
 
buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
1446
 
<a class="reference external" href="https://bugs.python.org/issue7133">issue 7133</a>) and automatically set
1447
 
OpenSSL&#8217;s <code class="xref c c-macro docutils literal"><span class="pre">SSL_MODE_AUTO_RETRY</span></code>, which will prevent an error
1448
 
code being returned from <code class="xref py py-meth docutils literal"><span class="pre">recv()</span></code> operations that trigger an SSL
1449
 
renegotiation (fix by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8222">issue 8222</a>).</p>
1450
 
<p>The <a class="reference internal" href="../library/ssl.html#ssl.wrap_socket" title="ssl.wrap_socket"><code class="xref py py-func docutils literal"><span class="pre">ssl.wrap_socket()</span></code></a> constructor function now takes a
1451
 
<em>ciphers</em> argument that&#8217;s a string listing the encryption algorithms
1452
 
to be allowed; the format of the string is described
1453
 
<a class="reference external" href="http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT">in the OpenSSL documentation</a>.
1454
 
(Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8322">issue 8322</a>.)</p>
1455
 
<p>Another change makes the extension load all of OpenSSL&#8217;s ciphers and
1456
 
digest algorithms so that they&#8217;re all available.  Some SSL
1457
 
certificates couldn&#8217;t be verified, reporting an &#8220;unknown algorithm&#8221;
1458
 
error.  (Reported by Beda Kosata, and fixed by Antoine Pitrou;
1459
 
<a class="reference external" href="https://bugs.python.org/issue8484">issue 8484</a>.)</p>
1460
 
<p>The version of OpenSSL being used is now available as the module
1461
 
attributes <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION" title="ssl.OPENSSL_VERSION"><code class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION</span></code></a> (a string),
1462
 
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_INFO" title="ssl.OPENSSL_VERSION_INFO"><code class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION_INFO</span></code></a> (a 5-tuple), and
1463
 
<a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_NUMBER" title="ssl.OPENSSL_VERSION_NUMBER"><code class="xref py py-data docutils literal"><span class="pre">ssl.OPENSSL_VERSION_NUMBER</span></code></a> (an integer).  (Added by Antoine
1464
 
Pitrou; <a class="reference external" href="https://bugs.python.org/issue8321">issue 8321</a>.)</p>
1465
 
</li>
1466
 
<li><p class="first">The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal"><span class="pre">struct</span></code></a> module will no longer silently ignore overflow
1467
 
errors when a value is too large for a particular integer format
1468
 
code (one of <code class="docutils literal"><span class="pre">bBhHiIlLqQ</span></code>); it now always raises a
1469
 
<a class="reference internal" href="../library/struct.html#struct.error" title="struct.error"><code class="xref py py-exc docutils literal"><span class="pre">struct.error</span></code></a> exception.  (Changed by Mark Dickinson;
1470
 
<a class="reference external" href="https://bugs.python.org/issue1523">issue 1523</a>.)  The <a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal"><span class="pre">pack()</span></code></a> function will also
1471
 
attempt to use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal"><span class="pre">__index__()</span></code></a> to convert and pack non-integers
1472
 
before trying the <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal"><span class="pre">__int__()</span></code></a> method or reporting an error.
1473
 
(Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue8300">issue 8300</a>.)</p>
1474
 
</li>
1475
 
<li><p class="first">New function: the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal"><span class="pre">subprocess</span></code></a> module&#8217;s
1476
 
<a class="reference internal" href="../library/subprocess.html#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal"><span class="pre">check_output()</span></code></a> runs a command with a specified set of arguments
1477
 
and returns the command&#8217;s output as a string when the command runs without
1478
 
error, or raises a <a class="reference internal" href="../library/subprocess.html#subprocess.CalledProcessError" title="subprocess.CalledProcessError"><code class="xref py py-exc docutils literal"><span class="pre">CalledProcessError</span></code></a> exception otherwise.</p>
1479
 
<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="s1">&#39;df&#39;</span><span class="p">,</span> <span class="s1">&#39;-h&#39;</span><span class="p">,</span> <span class="s1">&#39;.&#39;</span><span class="p">])</span>
1480
 
<span class="go">&#39;Filesystem     Size   Used  Avail Capacity  Mounted on\n</span>
1481
 
<span class="go">/dev/disk0s2    52G    49G   3.0G    94%    /\n&#39;</span>
1482
 
 
1483
 
<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="s1">&#39;df&#39;</span><span class="p">,</span> <span class="s1">&#39;-h&#39;</span><span class="p">,</span> <span class="s1">&#39;/bogus&#39;</span><span class="p">])</span>
1484
 
<span class="go">  ...</span>
1485
 
<span class="go">subprocess.CalledProcessError: Command &#39;[&#39;df&#39;, &#39;-h&#39;, &#39;/bogus&#39;]&#39; returned non-zero exit status 1</span>
1486
 
</pre></div>
1487
 
</div>
1488
 
<p>(Contributed by Gregory P. Smith.)</p>
1489
 
<p>The <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal"><span class="pre">subprocess</span></code></a> module will now retry its internal system calls
1490
 
on receiving an <code class="xref py py-const docutils literal"><span class="pre">EINTR</span></code> signal.  (Reported by several people; final
1491
 
patch by Gregory P. Smith in <a class="reference external" href="https://bugs.python.org/issue1068268">issue 1068268</a>.)</p>
1492
 
</li>
1493
 
<li><p class="first">New function: <a class="reference internal" href="../library/symtable.html#symtable.Symbol.is_declared_global" title="symtable.Symbol.is_declared_global"><code class="xref py py-func docutils literal"><span class="pre">is_declared_global()</span></code></a> in the <a class="reference internal" href="../library/symtable.html#module-symtable" title="symtable: Interface to the compiler's internal symbol tables."><code class="xref py py-mod docutils literal"><span class="pre">symtable</span></code></a> module
1494
 
returns true for variables that are explicitly declared to be global,
1495
 
false for ones that are implicitly global.
1496
 
(Contributed by Jeremy Hylton.)</p>
1497
 
</li>
1498
 
<li><p class="first">The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal"><span class="pre">sys.argv[0]</span></code> as the
1499
 
identifier instead of the previous default value of <code class="docutils literal"><span class="pre">'python'</span></code>.
1500
 
(Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue8451">issue 8451</a>.)</p>
1501
 
</li>
1502
 
<li><p class="first">The <code class="docutils literal"><span class="pre">sys.version_info</span></code> value is now a named tuple, with attributes
1503
 
named <code class="xref py py-attr docutils literal"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">micro</span></code>,
1504
 
<code class="xref py py-attr docutils literal"><span class="pre">releaselevel</span></code>, and <code class="xref py py-attr docutils literal"><span class="pre">serial</span></code>.  (Contributed by Ross
1505
 
Light; <a class="reference external" href="https://bugs.python.org/issue4285">issue 4285</a>.)</p>
1506
 
<p><a class="reference internal" href="../library/sys.html#sys.getwindowsversion" title="sys.getwindowsversion"><code class="xref py py-func docutils literal"><span class="pre">sys.getwindowsversion()</span></code></a> also returns a named tuple,
1507
 
with attributes named <code class="xref py py-attr docutils literal"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">build</span></code>,
1508
 
<a class="reference internal" href="../library/platform.html#module-platform" title="platform: Retrieves as much platform identifying data as possible."><code class="xref py py-attr docutils literal"><span class="pre">platform</span></code></a>, <code class="xref py py-attr docutils literal"><span class="pre">service_pack</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">service_pack_major</span></code>,
1509
 
<code class="xref py py-attr docutils literal"><span class="pre">service_pack_minor</span></code>, <code class="xref py py-attr docutils literal"><span class="pre">suite_mask</span></code>, and
1510
 
<code class="xref py py-attr docutils literal"><span class="pre">product_type</span></code>.  (Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue7766">issue 7766</a>.)</p>
1511
 
</li>
1512
 
<li><p class="first">The <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> module&#8217;s default error handling has changed, to
1513
 
no longer suppress fatal errors.  The default error level was previously 0,
1514
 
which meant that errors would only result in a message being written to the
1515
 
debug log, but because the debug log is not activated by default,
1516
 
these errors go unnoticed.  The default error level is now 1,
1517
 
which raises an exception if there&#8217;s an error.
1518
 
(Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue7357">issue 7357</a>.)</p>
1519
 
<p><a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> now supports filtering the <a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a>
1520
 
objects being added to a tar file.  When you call <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile.add" title="tarfile.TarFile.add"><code class="xref py py-meth docutils literal"><span class="pre">add()</span></code></a>,
1521
 
you may supply an optional <em>filter</em> argument
1522
 
that&#8217;s a callable.  The <em>filter</em> callable will be passed the
1523
 
<a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal"><span class="pre">TarInfo</span></code></a> for every file being added, and can modify and return it.
1524
 
If the callable returns <code class="docutils literal"><span class="pre">None</span></code>, the file will be excluded from the
1525
 
resulting archive.  This is more powerful than the existing
1526
 
<em>exclude</em> argument, which has therefore been deprecated.
1527
 
(Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue6856">issue 6856</a>.)
1528
 
The <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal"><span class="pre">TarFile</span></code></a> class also now supports the context management protocol.
1529
 
(Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue7232">issue 7232</a>.)</p>
1530
 
</li>
1531
 
<li><p class="first">The <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> method of the <a class="reference internal" href="../library/threading.html#threading.Event" title="threading.Event"><code class="xref py py-class docutils literal"><span class="pre">threading.Event</span></code></a> class
1532
 
now returns the internal flag on exit.  This means the method will usually
1533
 
return true because <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> is supposed to block until the
1534
 
internal flag becomes true.  The return value will only be false if
1535
 
a timeout was provided and the operation timed out.
1536
 
(Contributed by Tim Lesher; <a class="reference external" href="https://bugs.python.org/issue1674032">issue 1674032</a>.)</p>
1537
 
</li>
1538
 
<li><p class="first">The Unicode database provided by the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal"><span class="pre">unicodedata</span></code></a> module is
1539
 
now used internally to determine which characters are numeric,
1540
 
whitespace, or represent line breaks.  The database also
1541
 
includes information from the <code class="file docutils literal"><span class="pre">Unihan.txt</span></code> data file (patch
1542
 
by Anders Chrigström and Amaury Forgeot d&#8217;Arc; <a class="reference external" href="https://bugs.python.org/issue1571184">issue 1571184</a>)
1543
 
and has been updated to version 5.2.0 (updated by
1544
 
Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue8024">issue 8024</a>).</p>
1545
 
</li>
1546
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">urlparse</span></code> module&#8217;s <code class="xref py py-func docutils literal"><span class="pre">urlsplit()</span></code> now handles
1547
 
unknown URL schemes in a fashion compliant with <span class="target" id="index-10"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
1548
 
URL is of the form <code class="docutils literal"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
1549
 
<code class="docutils literal"><span class="pre">://</span></code> is treated as the scheme, even if it&#8217;s a made-up scheme that
1550
 
the module doesn&#8217;t know about.  This change may break code that
1551
 
worked around the old behaviour.  For example, Python 2.6.4 or 2.5
1552
 
will return the following:</p>
1553
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
1554
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
1555
 
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
1556
 
</pre></div>
1557
 
</div>
1558
 
<p>Python 2.7 (and Python 2.6.5) will return:</p>
1559
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
1560
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
1561
 
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
1562
 
</pre></div>
1563
 
</div>
1564
 
<p>(Python 2.7 actually produces slightly different output, since it
1565
 
returns a named tuple instead of a standard tuple.)</p>
1566
 
<p>The <code class="xref py py-mod docutils literal"><span class="pre">urlparse</span></code> module also supports IPv6 literal addresses as defined by
1567
 
<span class="target" id="index-11"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc2732.html"><strong>RFC 2732</strong></a> (contributed by Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue2987">issue 2987</a>).</p>
1568
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s1">&#39;http://[1080::8:800:200C:417A]/foo&#39;</span><span class="p">)</span>
1569
 
<span class="go">ParseResult(scheme=&#39;http&#39;, netloc=&#39;[1080::8:800:200C:417A]&#39;,</span>
1570
 
<span class="go">            path=&#39;/foo&#39;, params=&#39;&#39;, query=&#39;&#39;, fragment=&#39;&#39;)</span>
1571
 
</pre></div>
1572
 
</div>
1573
 
</li>
1574
 
<li><p class="first">New class: the <a class="reference internal" href="../library/weakref.html#weakref.WeakSet" title="weakref.WeakSet"><code class="xref py py-class docutils literal"><span class="pre">WeakSet</span></code></a> class in the <a class="reference internal" href="../library/weakref.html#module-weakref" title="weakref: Support for weak references and weak dictionaries."><code class="xref py py-mod docutils literal"><span class="pre">weakref</span></code></a>
1575
 
module is a set that only holds weak references to its elements; elements
1576
 
will be removed once there are no references pointing to them.
1577
 
(Originally implemented in Python 3.x by Raymond Hettinger, and backported
1578
 
to 2.7 by Michael Foord.)</p>
1579
 
</li>
1580
 
<li><p class="first">The ElementTree library, <code class="xref py py-mod docutils literal"><span class="pre">xml.etree</span></code>, no longer escapes
1581
 
ampersands and angle brackets when outputting an XML processing
1582
 
instruction (which looks like <code class="docutils literal"><span class="pre">&lt;?xml-stylesheet</span> <span class="pre">href=&quot;#style1&quot;?&gt;</span></code>)
1583
 
or comment (which looks like <code class="docutils literal"><span class="pre">&lt;!--</span> <span class="pre">comment</span> <span class="pre">--&gt;</span></code>).
1584
 
(Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue2746">issue 2746</a>.)</p>
1585
 
</li>
1586
 
<li><p class="first">The XML-RPC client and server, provided by the <code class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></code> and
1587
 
<code class="xref py py-mod docutils literal"><span class="pre">SimpleXMLRPCServer</span></code> modules, have improved performance by
1588
 
supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
1589
 
to compress the XML being exchanged.  The gzip compression is
1590
 
controlled by the <code class="xref py py-attr docutils literal"><span class="pre">encode_threshold</span></code> attribute of
1591
 
<code class="xref py py-class docutils literal"><span class="pre">SimpleXMLRPCRequestHandler</span></code>, which contains a size in bytes;
1592
 
responses larger than this will be compressed.
1593
 
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue6267">issue 6267</a>.)</p>
1594
 
</li>
1595
 
<li><p class="first">The <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">zipfile</span></code></a> module&#8217;s <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal"><span class="pre">ZipFile</span></code></a> now supports the context
1596
 
management protocol, so you can write <code class="docutils literal"><span class="pre">with</span> <span class="pre">zipfile.ZipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
1597
 
(Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue5511">issue 5511</a>.)</p>
1598
 
<p><a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">zipfile</span></code></a> now also supports archiving empty directories and
1599
 
extracts them correctly.  (Fixed by Kuba Wieczorek; <a class="reference external" href="https://bugs.python.org/issue4710">issue 4710</a>.)
1600
 
Reading files out of an archive is faster, and interleaving
1601
 
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.read" title="zipfile.ZipFile.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a> and <code class="xref py py-meth docutils literal"><span class="pre">readline()</span></code> now works correctly.
1602
 
(Contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue7610">issue 7610</a>.)</p>
1603
 
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.is_zipfile" title="zipfile.is_zipfile"><code class="xref py py-func docutils literal"><span class="pre">is_zipfile()</span></code></a> function now
1604
 
accepts a file object, in addition to the path names accepted in earlier
1605
 
versions.  (Contributed by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue4756">issue 4756</a>.)</p>
1606
 
<p>The <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.writestr" title="zipfile.ZipFile.writestr"><code class="xref py py-meth docutils literal"><span class="pre">writestr()</span></code></a> method now has an optional <em>compress_type</em> parameter
1607
 
that lets you override the default compression method specified in the
1608
 
<a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal"><span class="pre">ZipFile</span></code></a> constructor.  (Contributed by Ronald Oussoren;
1609
 
<a class="reference external" href="https://bugs.python.org/issue6003">issue 6003</a>.)</p>
1610
 
</li>
1611
 
</ul>
1612
 
<div class="section" id="new-module-importlib">
1613
 
<span id="importlib-section"></span><h3>New module: importlib<a class="headerlink" href="#new-module-importlib" title="Permalink to this headline">¶</a></h3>
1614
 
<p>Python 3.1 includes the <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> package, a re-implementation
1615
 
of the logic underlying Python&#8217;s <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal"><span class="pre">import</span></code></a> statement.
1616
 
<a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> is useful for implementors of Python interpreters and
1617
 
to users who wish to write new importers that can participate in the
1618
 
import process.  Python 2.7 doesn&#8217;t contain the complete
1619
 
<a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> package, but instead has a tiny subset that contains
1620
 
a single function, <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code></a>.</p>
1621
 
<p><code class="docutils literal"><span class="pre">import_module(name,</span> <span class="pre">package=None)</span></code> imports a module.  <em>name</em> is
1622
 
a string containing the module or package&#8217;s name.  It&#8217;s possible to do
1623
 
relative imports by providing a string that begins with a <code class="docutils literal"><span class="pre">.</span></code>
1624
 
character, such as <code class="docutils literal"><span class="pre">..utils.errors</span></code>.  For relative imports, the
1625
 
<em>package</em> argument must be provided and is the name of the package that
1626
 
will be used as the anchor for
1627
 
the relative import.  <a class="reference internal" href="../library/importlib.html#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal"><span class="pre">import_module()</span></code></a> both inserts the imported
1628
 
module into <code class="docutils literal"><span class="pre">sys.modules</span></code> and returns the module object.</p>
1629
 
<p>Here are some examples:</p>
1630
 
<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="k">import</span> <span class="n">import_module</span>
1631
 
<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="s1">&#39;anydbm&#39;</span><span class="p">)</span>  <span class="c1"># Standard absolute import</span>
1632
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span>
1633
 
<span class="go">&lt;module &#39;anydbm&#39; from &#39;/p/python/Lib/anydbm.py&#39;&gt;</span>
1634
 
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Relative import</span>
1635
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="s1">&#39;..file_util&#39;</span><span class="p">,</span> <span class="s1">&#39;distutils.command&#39;</span><span class="p">)</span>
1636
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span>
1637
 
<span class="go">&lt;module &#39;distutils.file_util&#39; from &#39;/python/Lib/distutils/file_util.pyc&#39;&gt;</span>
1638
 
</pre></div>
1639
 
</div>
1640
 
<p><a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal"><span class="pre">importlib</span></code></a> was implemented by Brett Cannon and introduced in
1641
 
Python 3.1.</p>
1642
 
</div>
1643
 
<div class="section" id="new-module-sysconfig">
1644
 
<h3>New module: sysconfig<a class="headerlink" href="#new-module-sysconfig" title="Permalink to this headline">¶</a></h3>
1645
 
<p>The <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></code></a> module has been pulled out of the Distutils
1646
 
package, becoming a new top-level module in its own right.
1647
 
<a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></code></a> provides functions for getting information about
1648
 
Python&#8217;s build process: compiler switches, installation paths, the
1649
 
platform name, and whether Python is running from its source
1650
 
directory.</p>
1651
 
<p>Some of the functions in the module are:</p>
1652
 
<ul class="simple">
1653
 
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_var" title="sysconfig.get_config_var"><code class="xref py py-func docutils literal"><span class="pre">get_config_var()</span></code></a> returns variables from Python&#8217;s
1654
 
Makefile and the <code class="file docutils literal"><span class="pre">pyconfig.h</span></code> file.</li>
1655
 
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_config_vars" title="sysconfig.get_config_vars"><code class="xref py py-func docutils literal"><span class="pre">get_config_vars()</span></code></a> returns a dictionary containing
1656
 
all of the configuration variables.</li>
1657
 
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_path" title="sysconfig.get_path"><code class="xref py py-func docutils literal"><span class="pre">get_path()</span></code></a> returns the configured path for
1658
 
a particular type of module: the standard library,
1659
 
site-specific modules, platform-specific modules, etc.</li>
1660
 
<li><a class="reference internal" href="../library/sysconfig.html#sysconfig.is_python_build" title="sysconfig.is_python_build"><code class="xref py py-func docutils literal"><span class="pre">is_python_build()</span></code></a> returns true if you&#8217;re running a
1661
 
binary from a Python source tree, and false otherwise.</li>
1662
 
</ul>
1663
 
<p>Consult the <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></code></a> documentation for more details and for
1664
 
a complete list of functions.</p>
1665
 
<p>The Distutils package and <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal"><span class="pre">sysconfig</span></code></a> are now maintained by Tarek
1666
 
Ziadé, who has also started a Distutils2 package (source repository at
1667
 
<a class="reference external" href="https://hg.python.org/distutils2/">https://hg.python.org/distutils2/</a>) for developing a next-generation
1668
 
version of Distutils.</p>
1669
 
</div>
1670
 
<div class="section" id="ttk-themed-widgets-for-tk">
1671
 
<h3>ttk: Themed Widgets for Tk<a class="headerlink" href="#ttk-themed-widgets-for-tk" title="Permalink to this headline">¶</a></h3>
1672
 
<p>Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1673
 
widgets but have a more customizable appearance and can therefore more
1674
 
closely resemble the native platform&#8217;s widgets.  This widget
1675
 
set was originally called Tile, but was renamed to Ttk (for &#8220;themed Tk&#8221;)
1676
 
on being added to Tcl/Tck release 8.5.</p>
1677
 
<p>To learn more, read the <code class="xref py py-mod docutils literal"><span class="pre">ttk</span></code> module documentation.  You may also
1678
 
wish to read the Tcl/Tk manual page describing the
1679
 
Ttk theme engine, available at
1680
 
<a class="reference external" href="http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm">http://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm</a>. Some
1681
 
screenshots of the Python/Ttk code in use are at
1682
 
<a class="reference external" href="http://code.google.com/p/python-ttk/wiki/Screenshots">http://code.google.com/p/python-ttk/wiki/Screenshots</a>.</p>
1683
 
<p>The <code class="xref py py-mod docutils literal"><span class="pre">ttk</span></code> module was written by Guilherme Polo and added in
1684
 
<a class="reference external" href="https://bugs.python.org/issue2983">issue 2983</a>.  An alternate version called <code class="docutils literal"><span class="pre">Tile.py</span></code>, written by
1685
 
Martin Franklin and maintained by Kevin Walzer, was proposed for
1686
 
inclusion in <a class="reference external" href="https://bugs.python.org/issue2618">issue 2618</a>, but the authors argued that Guilherme
1687
 
Polo&#8217;s work was more comprehensive.</p>
1688
 
</div>
1689
 
<div class="section" id="updated-module-unittest">
1690
 
<span id="unittest-section"></span><h3>Updated module: unittest<a class="headerlink" href="#updated-module-unittest" title="Permalink to this headline">¶</a></h3>
1691
 
<p>The <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> module was greatly enhanced; many
1692
 
new features were added.  Most of these features were implemented
1693
 
by Michael Foord, unless otherwise noted.  The enhanced version of
1694
 
the module is downloadable separately for use with Python versions 2.4 to 2.6,
1695
 
packaged as the <code class="xref py py-mod docutils literal"><span class="pre">unittest2</span></code> package, from
1696
 
<a class="reference external" href="https://pypi.python.org/pypi/unittest2">https://pypi.python.org/pypi/unittest2</a>.</p>
1697
 
<p>When used from the command line, the module can automatically discover
1698
 
tests.  It&#8217;s not as fancy as <a class="reference external" href="http://pytest.org">py.test</a> or
1699
 
<a class="reference external" href="http://code.google.com/p/python-nose/">nose</a>, but provides a simple way
1700
 
to run tests kept within a set of package directories.  For example,
1701
 
the following command will search the <code class="file docutils literal"><span class="pre">test/</span></code> subdirectory for
1702
 
any importable test files named <code class="docutils literal"><span class="pre">test*.py</span></code>:</p>
1703
 
<div class="highlight-python3"><div class="highlight"><pre><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">unittest</span> <span class="n">discover</span> <span class="o">-</span><span class="n">s</span> <span class="n">test</span>
1704
 
</pre></div>
1705
 
</div>
1706
 
<p>Consult the <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a> module documentation for more details.
1707
 
(Developed in <a class="reference external" href="https://bugs.python.org/issue6001">issue 6001</a>.)</p>
1708
 
<p>The <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal"><span class="pre">main()</span></code></a> function supports some other new options:</p>
1709
 
<ul>
1710
 
<li><p class="first"><a class="reference internal" href="../using/cmdline.html#cmdoption-b"><code class="xref std std-option docutils literal"><span class="pre">-b</span></code></a> or <code class="xref std std-option docutils literal"><span class="pre">--buffer</span></code> will buffer the standard output
1711
 
and standard error streams during each test.  If the test passes,
1712
 
any resulting output will be discarded; on failure, the buffered
1713
 
output will be displayed.</p>
1714
 
</li>
1715
 
<li><p class="first"><a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal"><span class="pre">-c</span></code></a> or <code class="xref std std-option docutils literal"><span class="pre">--catch</span></code> will cause the control-C interrupt
1716
 
to be handled more gracefully.  Instead of interrupting the test
1717
 
process immediately, the currently running test will be completed
1718
 
and then the partial results up to the interruption will be reported.
1719
 
If you&#8217;re impatient, a second press of control-C will cause an immediate
1720
 
interruption.</p>
1721
 
<p>This control-C handler tries to avoid causing problems when the code
1722
 
being tested or the tests being run have defined a signal handler of
1723
 
their own, by noticing that a signal handler was already set and
1724
 
calling it.  If this doesn&#8217;t work for you, there&#8217;s a
1725
 
<a class="reference internal" href="../library/unittest.html#unittest.removeHandler" title="unittest.removeHandler"><code class="xref py py-func docutils literal"><span class="pre">removeHandler()</span></code></a> decorator that can be used to mark tests that
1726
 
should have the control-C handling disabled.</p>
1727
 
</li>
1728
 
<li><p class="first"><code class="xref std std-option docutils literal"><span class="pre">-f</span></code> or <code class="xref std std-option docutils literal"><span class="pre">--failfast</span></code> makes
1729
 
test execution stop immediately when a test fails instead of
1730
 
continuing to execute further tests.  (Suggested by Cliff Dyer and
1731
 
implemented by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue8074">issue 8074</a>.)</p>
1732
 
</li>
1733
 
</ul>
1734
 
<p>The progress messages now show &#8216;x&#8217; for expected failures
1735
 
and &#8216;u&#8217; for unexpected successes when run in verbose mode.
1736
 
(Contributed by Benjamin Peterson.)</p>
1737
 
<p>Test cases can raise the <a class="reference internal" href="../library/unittest.html#unittest.SkipTest" title="unittest.SkipTest"><code class="xref py py-exc docutils literal"><span class="pre">SkipTest</span></code></a> exception to skip a
1738
 
test (<a class="reference external" href="https://bugs.python.org/issue1034053">issue 1034053</a>).</p>
1739
 
<p>The error messages for <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a>,
1740
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal"><span class="pre">assertTrue()</span></code></a>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertFalse" title="unittest.TestCase.assertFalse"><code class="xref py py-meth docutils literal"><span class="pre">assertFalse()</span></code></a>
1741
 
failures now provide more information.  If you set the
1742
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.longMessage" title="unittest.TestCase.longMessage"><code class="xref py py-attr docutils literal"><span class="pre">longMessage</span></code></a> attribute of your <a class="reference internal" href="../library/unittest.html#unittest.TestCase" title="unittest.TestCase"><code class="xref py py-class docutils literal"><span class="pre">TestCase</span></code></a> classes to
1743
 
True, both the standard error message and any additional message you
1744
 
provide will be printed for failures.  (Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue5663">issue 5663</a>.)</p>
1745
 
<p>The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRaises" title="unittest.TestCase.assertRaises"><code class="xref py py-meth docutils literal"><span class="pre">assertRaises()</span></code></a> method now
1746
 
returns a context handler when called without providing a callable
1747
 
object to run.  For example, you can write this:</p>
1748
 
<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>
1749
 
    <span class="p">{}[</span><span class="s1">&#39;foo&#39;</span><span class="p">]</span>
1750
 
</pre></div>
1751
 
</div>
1752
 
<p>(Implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue4444">issue 4444</a>.)</p>
1753
 
<p>Module- and class-level setup and teardown fixtures are now supported.
1754
 
Modules can contain <code class="xref py py-func docutils literal"><span class="pre">setUpModule()</span></code> and <code class="xref py py-func docutils literal"><span class="pre">tearDownModule()</span></code>
1755
 
functions.  Classes can have <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUpClass" title="unittest.TestCase.setUpClass"><code class="xref py py-meth docutils literal"><span class="pre">setUpClass()</span></code></a> and
1756
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal"><span class="pre">tearDownClass()</span></code></a> methods that must be defined as class methods
1757
 
(using <code class="docutils literal"><span class="pre">&#64;classmethod</span></code> or equivalent).  These functions and
1758
 
methods are invoked when the test runner switches to a test case in a
1759
 
different module or class.</p>
1760
 
<p>The methods <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal"><span class="pre">addCleanup()</span></code></a> and
1761
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><code class="xref py py-meth docutils literal"><span class="pre">doCleanups()</span></code></a> were added.
1762
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal"><span class="pre">addCleanup()</span></code></a> lets you add cleanup functions that
1763
 
will be called unconditionally (after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal"><span class="pre">setUp()</span></code></a> if
1764
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal"><span class="pre">setUp()</span></code></a> fails, otherwise after <a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDown" title="unittest.TestCase.tearDown"><code class="xref py py-meth docutils literal"><span class="pre">tearDown()</span></code></a>). This allows
1765
 
for much simpler resource allocation and deallocation during tests
1766
 
(<a class="reference external" href="https://bugs.python.org/issue5679">issue 5679</a>).</p>
1767
 
<p>A number of new methods were added that provide more specialized
1768
 
tests.  Many of these methods were written by Google engineers
1769
 
for use in their test suites; Gregory P. Smith, Michael Foord, and
1770
 
GvR worked on merging them into Python&#8217;s version of <a class="reference internal" href="../library/unittest.html#module-unittest" title="unittest: Unit testing framework for Python."><code class="xref py py-mod docutils literal"><span class="pre">unittest</span></code></a>.</p>
1771
 
<ul class="simple">
1772
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNone" title="unittest.TestCase.assertIsNone"><code class="xref py py-meth docutils literal"><span class="pre">assertIsNone()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNotNone" title="unittest.TestCase.assertIsNotNone"><code class="xref py py-meth docutils literal"><span class="pre">assertIsNotNone()</span></code></a> take one
1773
 
expression and verify that the result is or is not <code class="docutils literal"><span class="pre">None</span></code>.</li>
1774
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><code class="xref py py-meth docutils literal"><span class="pre">assertIs()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNot" title="unittest.TestCase.assertIsNot"><code class="xref py py-meth docutils literal"><span class="pre">assertIsNot()</span></code></a>
1775
 
take two values and check whether the two values evaluate to the same object or not.
1776
 
(Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue2578">issue 2578</a>.)</li>
1777
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsInstance" title="unittest.TestCase.assertIsInstance"><code class="xref py py-meth docutils literal"><span class="pre">assertIsInstance()</span></code></a> and
1778
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIsInstance" title="unittest.TestCase.assertNotIsInstance"><code class="xref py py-meth docutils literal"><span class="pre">assertNotIsInstance()</span></code></a> check whether
1779
 
the resulting object is an instance of a particular class, or of
1780
 
one of a tuple of classes.  (Added by Georg Brandl; <a class="reference external" href="https://bugs.python.org/issue7031">issue 7031</a>.)</li>
1781
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreater" title="unittest.TestCase.assertGreater"><code class="xref py py-meth docutils literal"><span class="pre">assertGreater()</span></code></a>, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreaterEqual" title="unittest.TestCase.assertGreaterEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertGreaterEqual()</span></code></a>,
1782
 
<a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLess" title="unittest.TestCase.assertLess"><code class="xref py py-meth docutils literal"><span class="pre">assertLess()</span></code></a>, and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLessEqual" title="unittest.TestCase.assertLessEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertLessEqual()</span></code></a> compare
1783
 
two quantities.</li>
1784
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertMultiLineEqual()</span></code></a> compares two strings, and if they&#8217;re
1785
 
not equal, displays a helpful comparison that highlights the
1786
 
differences in the two strings.  This comparison is now used by
1787
 
default when Unicode strings are compared with <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a>.</li>
1788
 
<li><code class="xref py py-meth docutils literal"><span class="pre">assertRegexpMatches()</span></code> and
1789
 
<code class="xref py py-meth docutils literal"><span class="pre">assertNotRegexpMatches()</span></code> checks whether the
1790
 
first argument is a string matching or not matching the regular
1791
 
expression provided as the second argument (<a class="reference external" href="https://bugs.python.org/issue8038">issue 8038</a>).</li>
1792
 
<li><code class="xref py py-meth docutils literal"><span class="pre">assertRaisesRegexp()</span></code> checks whether a particular exception
1793
 
is raised, and then also checks that the string representation of
1794
 
the exception matches the provided regular expression.</li>
1795
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIn" title="unittest.TestCase.assertIn"><code class="xref py py-meth docutils literal"><span class="pre">assertIn()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIn" title="unittest.TestCase.assertNotIn"><code class="xref py py-meth docutils literal"><span class="pre">assertNotIn()</span></code></a>
1796
 
tests whether <em>first</em> is or is not in  <em>second</em>.</li>
1797
 
<li><code class="xref py py-meth docutils literal"><span class="pre">assertItemsEqual()</span></code> tests whether two provided sequences
1798
 
contain the same elements.</li>
1799
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSetEqual" title="unittest.TestCase.assertSetEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertSetEqual()</span></code></a> compares whether two sets are equal, and
1800
 
only reports the differences between the sets in case of error.</li>
1801
 
<li>Similarly, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertListEqual()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTupleEqual" title="unittest.TestCase.assertTupleEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertTupleEqual()</span></code></a>
1802
 
compare the specified types and explain any differences without necessarily
1803
 
printing their full values; these methods are now used by default
1804
 
when comparing lists and tuples using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a>.
1805
 
More generally, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSequenceEqual" title="unittest.TestCase.assertSequenceEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertSequenceEqual()</span></code></a> compares two sequences
1806
 
and can optionally check whether both sequences are of a
1807
 
particular type.</li>
1808
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertDictEqual()</span></code></a> compares two dictionaries and reports the
1809
 
differences; it&#8217;s now used by default when you compare two dictionaries
1810
 
using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a>.  <code class="xref py py-meth docutils literal"><span class="pre">assertDictContainsSubset()</span></code> checks whether
1811
 
all of the key/value pairs in <em>first</em> are found in <em>second</em>.</li>
1812
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertAlmostEqual()</span></code></a> and <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotAlmostEqual" title="unittest.TestCase.assertNotAlmostEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertNotAlmostEqual()</span></code></a> test
1813
 
whether <em>first</em> and <em>second</em> are approximately equal.  This method
1814
 
can either round their difference to an optionally-specified number
1815
 
of <em>places</em> (the default is 7) and compare it to zero, or require
1816
 
the difference to be smaller than a supplied <em>delta</em> value.</li>
1817
 
<li><a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromName" title="unittest.TestLoader.loadTestsFromName"><code class="xref py py-meth docutils literal"><span class="pre">loadTestsFromName()</span></code></a> properly honors the
1818
 
<a class="reference internal" href="../library/unittest.html#unittest.TestLoader.suiteClass" title="unittest.TestLoader.suiteClass"><code class="xref py py-attr docutils literal"><span class="pre">suiteClass</span></code></a> attribute of
1819
 
the <a class="reference internal" href="../library/unittest.html#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal"><span class="pre">TestLoader</span></code></a>. (Fixed by Mark Roddy; <a class="reference external" href="https://bugs.python.org/issue6866">issue 6866</a>.)</li>
1820
 
<li>A new hook lets you extend the <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal"><span class="pre">assertEqual()</span></code></a> method to handle
1821
 
new data types.  The <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addTypeEqualityFunc" title="unittest.TestCase.addTypeEqualityFunc"><code class="xref py py-meth docutils literal"><span class="pre">addTypeEqualityFunc()</span></code></a> method takes a type
1822
 
object and a function. The function will be used when both of the
1823
 
objects being compared are of the specified type.  This function
1824
 
should compare the two objects and raise an exception if they don&#8217;t
1825
 
match; it&#8217;s a good idea for the function to provide additional
1826
 
information about why the two objects aren&#8217;t matching, much as the new
1827
 
sequence comparison methods do.</li>
1828
 
</ul>
1829
 
<p><a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal"><span class="pre">unittest.main()</span></code></a> now takes an optional <code class="docutils literal"><span class="pre">exit</span></code> argument.  If
1830
 
False, <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal"><span class="pre">main()</span></code></a> doesn&#8217;t call <a class="reference internal" href="../library/sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal"><span class="pre">sys.exit()</span></code></a>, allowing
1831
 
<a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal"><span class="pre">main()</span></code></a> to be used from the interactive interpreter.
1832
 
(Contributed by J. Pablo Fernández; <a class="reference external" href="https://bugs.python.org/issue3379">issue 3379</a>.)</p>
1833
 
<p><a class="reference internal" href="../library/unittest.html#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal"><span class="pre">TestResult</span></code></a> has new <a class="reference internal" href="../library/unittest.html#unittest.TestResult.startTestRun" title="unittest.TestResult.startTestRun"><code class="xref py py-meth docutils literal"><span class="pre">startTestRun()</span></code></a> and
1834
 
<a class="reference internal" href="../library/unittest.html#unittest.TestResult.stopTestRun" title="unittest.TestResult.stopTestRun"><code class="xref py py-meth docutils literal"><span class="pre">stopTestRun()</span></code></a> methods that are called immediately before
1835
 
and after a test run.  (Contributed by Robert Collins; <a class="reference external" href="https://bugs.python.org/issue5728">issue 5728</a>.)</p>
1836
 
<p>With all these changes, the <code class="file docutils literal"><span class="pre">unittest.py</span></code> was becoming awkwardly
1837
 
large, so the module was turned into a package and the code split into
1838
 
several files (by Benjamin Peterson).  This doesn&#8217;t affect how the
1839
 
module is imported or used.</p>
1840
 
<div class="admonition seealso">
1841
 
<p class="first admonition-title">See also</p>
1842
 
<dl class="last docutils">
1843
 
<dt><a class="reference external" href="http://www.voidspace.org.uk/python/articles/unittest2.shtml">http://www.voidspace.org.uk/python/articles/unittest2.shtml</a></dt>
1844
 
<dd>Describes the new features, how to use them, and the
1845
 
rationale for various design decisions.  (By Michael Foord.)</dd>
1846
 
</dl>
1847
 
</div>
1848
 
</div>
1849
 
<div class="section" id="updated-module-elementtree-1-3">
1850
 
<span id="elementtree-section"></span><h3>Updated module: ElementTree 1.3<a class="headerlink" href="#updated-module-elementtree-1-3" title="Permalink to this headline">¶</a></h3>
1851
 
<p>The version of the ElementTree library included with Python was updated to
1852
 
version 1.3.  Some of the new features are:</p>
1853
 
<ul>
1854
 
<li><p class="first">The various parsing functions now take a <em>parser</em> keyword argument
1855
 
giving an <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.XMLParser" title="xml.etree.ElementTree.XMLParser"><code class="xref py py-class docutils literal"><span class="pre">XMLParser</span></code></a> instance that will
1856
 
be used.  This makes it possible to override the file&#8217;s internal encoding:</p>
1857
 
<div class="highlight-python3"><div class="highlight"><pre><span class="n">p</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XMLParser</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
1858
 
<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;root/&gt;&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">parser</span><span class="o">=</span><span class="n">p</span><span class="p">)</span>
1859
 
</pre></div>
1860
 
</div>
1861
 
<p>Errors in parsing XML now raise a <code class="xref py py-exc docutils literal"><span class="pre">ParseError</span></code> exception, whose
1862
 
instances have a <code class="xref py py-attr docutils literal"><span class="pre">position</span></code> attribute
1863
 
containing a (<em>line</em>, <em>column</em>) tuple giving the location of the problem.</p>
1864
 
</li>
1865
 
<li><p class="first">ElementTree&#8217;s code for converting trees to a string has been
1866
 
significantly reworked, making it roughly twice as fast in many
1867
 
cases.  The <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write" title="xml.etree.ElementTree.ElementTree.write"><code class="xref py py-meth docutils literal"><span class="pre">ElementTree.write()</span></code></a>
1868
 
and <code class="xref py py-meth docutils literal"><span class="pre">Element.write()</span></code> methods now have a <em>method</em> parameter that can be
1869
 
&#8220;xml&#8221; (the default), &#8220;html&#8221;, or &#8220;text&#8221;.  HTML mode will output empty
1870
 
elements as <code class="docutils literal"><span class="pre">&lt;empty&gt;&lt;/empty&gt;</span></code> instead of <code class="docutils literal"><span class="pre">&lt;empty/&gt;</span></code>, and text
1871
 
mode will skip over elements and only output the text chunks.  If
1872
 
you set the <code class="xref py py-attr docutils literal"><span class="pre">tag</span></code> attribute of an element to <code class="docutils literal"><span class="pre">None</span></code> but
1873
 
leave its children in place, the element will be omitted when the
1874
 
tree is written out, so you don&#8217;t need to do more extensive rearrangement
1875
 
to remove a single element.</p>
1876
 
<p>Namespace handling has also been improved.  All <code class="docutils literal"><span class="pre">xmlns:&lt;whatever&gt;</span></code>
1877
 
declarations are now output on the root element, not scattered throughout
1878
 
the resulting XML.  You can set the default namespace for a tree
1879
 
by setting the <code class="xref py py-attr docutils literal"><span class="pre">default_namespace</span></code> attribute and can
1880
 
register new prefixes with <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.register_namespace" title="xml.etree.ElementTree.register_namespace"><code class="xref py py-meth docutils literal"><span class="pre">register_namespace()</span></code></a>.  In XML mode,
1881
 
you can use the true/false <em>xml_declaration</em> parameter to suppress the
1882
 
XML declaration.</p>
1883
 
</li>
1884
 
<li><p class="first">New <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element" title="xml.etree.ElementTree.Element"><code class="xref py py-class docutils literal"><span class="pre">Element</span></code></a> method:
1885
 
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.extend" title="xml.etree.ElementTree.Element.extend"><code class="xref py py-meth docutils literal"><span class="pre">extend()</span></code></a> appends the items from a
1886
 
sequence to the element&#8217;s children.  Elements themselves behave like
1887
 
sequences, so it&#8217;s easy to move children from one element to
1888
 
another:</p>
1889
 
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">xml.etree</span> <span class="k">import</span> <span class="n">ElementTree</span> <span class="k">as</span> <span class="n">ET</span>
1890
 
 
1891
 
<span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;list&gt;</span>
1892
 
<span class="s2">  &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt;  &lt;item&gt;3&lt;/item&gt;</span>
1893
 
<span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
1894
 
<span class="n">new</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s1">&#39;&lt;root/&gt;&#39;</span><span class="p">)</span>
1895
 
<span class="n">new</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
1896
 
 
1897
 
<span class="c1"># Outputs &lt;root&gt;&lt;item&gt;1&lt;/item&gt;...&lt;/root&gt;</span>
1898
 
<span class="nb">print</span> <span class="n">ET</span><span class="o">.</span><span class="n">tostring</span><span class="p">(</span><span class="n">new</span><span class="p">)</span>
1899
 
</pre></div>
1900
 
</div>
1901
 
</li>
1902
 
<li><p class="first">New <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> method:
1903
 
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iter" title="xml.etree.ElementTree.Element.iter"><code class="xref py py-meth docutils literal"><span class="pre">iter()</span></code></a> yields the children of the
1904
 
element as a generator.  It&#8217;s also possible to write <code class="docutils literal"><span class="pre">for</span> <span class="pre">child</span> <span class="pre">in</span>
1905
 
<span class="pre">elem:</span></code> to loop over an element&#8217;s children.  The existing method
1906
 
<code class="xref py py-meth docutils literal"><span class="pre">getiterator()</span></code> is now deprecated, as is <code class="xref py py-meth docutils literal"><span class="pre">getchildren()</span></code>
1907
 
which constructs and returns a list of children.</p>
1908
 
</li>
1909
 
<li><p class="first">New <code class="xref py py-class docutils literal"><span class="pre">Element</span></code> method:
1910
 
<a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.itertext" title="xml.etree.ElementTree.Element.itertext"><code class="xref py py-meth docutils literal"><span class="pre">itertext()</span></code></a> yields all chunks of
1911
 
text that are descendants of the element.  For example:</p>
1912
 
<div class="highlight-python3"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;list&gt;</span>
1913
 
<span class="s2">  &lt;item&gt;1&lt;/item&gt; &lt;item&gt;2&lt;/item&gt;  &lt;item&gt;3&lt;/item&gt;</span>
1914
 
<span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
1915
 
 
1916
 
<span class="c1"># Outputs [&#39;\n  &#39;, &#39;1&#39;, &#39; &#39;, &#39;2&#39;, &#39;  &#39;, &#39;3&#39;, &#39;\n&#39;]</span>
1917
 
<span class="nb">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">itertext</span><span class="p">())</span>
1918
 
</pre></div>
1919
 
</div>
1920
 
</li>
1921
 
<li><p class="first">Deprecated: using an element as a Boolean (i.e., <code class="docutils literal"><span class="pre">if</span> <span class="pre">elem:</span></code>) would
1922
 
return true if the element had any children, or false if there were
1923
 
no children.  This behaviour is confusing &#8211; <code class="docutils literal"><span class="pre">None</span></code> is false, but
1924
 
so is a childless element? &#8211; so it will now trigger a
1925
 
<a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal"><span class="pre">FutureWarning</span></code></a>.  In your code, you should be explicit: write
1926
 
<code class="docutils literal"><span class="pre">len(elem)</span> <span class="pre">!=</span> <span class="pre">0</span></code> if you&#8217;re interested in the number of children,
1927
 
or <code class="docutils literal"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></code>.</p>
1928
 
</li>
1929
 
</ul>
1930
 
<p>Fredrik Lundh develops ElementTree and produced the 1.3 version;
1931
 
you can read his article describing 1.3 at
1932
 
<a class="reference external" href="http://effbot.org/zone/elementtree-13-intro.htm">http://effbot.org/zone/elementtree-13-intro.htm</a>.
1933
 
Florent Xicluna updated the version included with
1934
 
Python, after discussions on python-dev and in <a class="reference external" href="https://bugs.python.org/issue6472">issue 6472</a>.)</p>
1935
 
</div>
1936
 
</div>
1937
 
<div class="section" id="build-and-c-api-changes">
1938
 
<h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
1939
 
<p>Changes to Python&#8217;s build process and to the C API include:</p>
1940
 
<ul>
1941
 
<li><p class="first">The latest release of the GNU Debugger, GDB 7, can be <a class="reference external" href="http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html">scripted
1942
 
using Python</a>.
1943
 
When you begin debugging an executable program P, GDB will look for
1944
 
a file named <code class="docutils literal"><span class="pre">P-gdb.py</span></code> and automatically read it.  Dave Malcolm
1945
 
contributed a <code class="file docutils literal"><span class="pre">python-gdb.py</span></code> that adds a number of
1946
 
commands useful when debugging Python itself.  For example,
1947
 
<code class="docutils literal"><span class="pre">py-up</span></code> and <code class="docutils literal"><span class="pre">py-down</span></code> go up or down one Python stack frame,
1948
 
which usually corresponds to several C stack frames.  <code class="docutils literal"><span class="pre">py-print</span></code>
1949
 
prints the value of a Python variable, and <code class="docutils literal"><span class="pre">py-bt</span></code> prints the
1950
 
Python stack trace.  (Added as a result of <a class="reference external" href="https://bugs.python.org/issue8032">issue 8032</a>.)</p>
1951
 
</li>
1952
 
<li><p class="first">If you use the <code class="file docutils literal"><span class="pre">.gdbinit</span></code> file provided with Python,
1953
 
the &#8220;pyo&#8221; macro in the 2.7 version now works correctly when the thread being
1954
 
debugged doesn&#8217;t hold the GIL; the macro now acquires it before printing.
1955
 
(Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue3632">issue 3632</a>.)</p>
1956
 
</li>
1957
 
<li><p class="first"><a class="reference internal" href="../c-api/init.html#c.Py_AddPendingCall" title="Py_AddPendingCall"><code class="xref c c-func docutils literal"><span class="pre">Py_AddPendingCall()</span></code></a> is now thread-safe, letting any
1958
 
worker thread submit notifications to the main Python thread.  This
1959
 
is particularly useful for asynchronous IO operations.
1960
 
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue4293">issue 4293</a>.)</p>
1961
 
</li>
1962
 
<li><p class="first">New function: <a class="reference internal" href="../c-api/code.html#c.PyCode_NewEmpty" title="PyCode_NewEmpty"><code class="xref c c-func docutils literal"><span class="pre">PyCode_NewEmpty()</span></code></a> creates an empty code object;
1963
 
only the filename, function name, and first line number are required.
1964
 
This is useful for extension modules that are attempting to
1965
 
construct a more useful traceback stack.  Previously such
1966
 
extensions needed to call <a class="reference internal" href="../c-api/code.html#c.PyCode_New" title="PyCode_New"><code class="xref c c-func docutils literal"><span class="pre">PyCode_New()</span></code></a>, which had many
1967
 
more arguments.  (Added by Jeffrey Yasskin.)</p>
1968
 
</li>
1969
 
<li><p class="first">New function: <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_NewExceptionWithDoc" title="PyErr_NewExceptionWithDoc"><code class="xref c c-func docutils literal"><span class="pre">PyErr_NewExceptionWithDoc()</span></code></a> creates a new
1970
 
exception class, just as the existing <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_NewException" title="PyErr_NewException"><code class="xref c c-func docutils literal"><span class="pre">PyErr_NewException()</span></code></a> does,
1971
 
but takes an extra <code class="docutils literal"><span class="pre">char</span> <span class="pre">*</span></code> argument containing the docstring for the
1972
 
new exception class.  (Added by &#8216;lekma&#8217; on the Python bug tracker;
1973
 
<a class="reference external" href="https://bugs.python.org/issue7033">issue 7033</a>.)</p>
1974
 
</li>
1975
 
<li><p class="first">New function: <a class="reference internal" href="../c-api/reflection.html#c.PyFrame_GetLineNumber" title="PyFrame_GetLineNumber"><code class="xref c c-func docutils literal"><span class="pre">PyFrame_GetLineNumber()</span></code></a> takes a frame object
1976
 
and returns the line number that the frame is currently executing.
1977
 
Previously code would need to get the index of the bytecode
1978
 
instruction currently executing, and then look up the line number
1979
 
corresponding to that address.  (Added by Jeffrey Yasskin.)</p>
1980
 
</li>
1981
 
<li><p class="first">New functions: <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><code class="xref c c-func docutils literal"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> and
1982
 
<a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongLongAndOverflow" title="PyLong_AsLongLongAndOverflow"><code class="xref c c-func docutils literal"><span class="pre">PyLong_AsLongLongAndOverflow()</span></code></a>  approximates a Python long
1983
 
integer as a C <code class="xref c c-type docutils literal"><span class="pre">long</span></code> or <code class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></code>.
1984
 
If the number is too large to fit into
1985
 
the output type, an <em>overflow</em> flag is set and returned to the caller.
1986
 
(Contributed by Case Van Horsen; <a class="reference external" href="https://bugs.python.org/issue7528">issue 7528</a> and <a class="reference external" href="https://bugs.python.org/issue7767">issue 7767</a>.)</p>
1987
 
</li>
1988
 
<li><p class="first">New function: stemming from the rewrite of string-to-float conversion,
1989
 
a new <a class="reference internal" href="../c-api/conversion.html#c.PyOS_string_to_double" title="PyOS_string_to_double"><code class="xref c c-func docutils literal"><span class="pre">PyOS_string_to_double()</span></code></a> function was added.  The old
1990
 
<code class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_atof()</span></code> functions
1991
 
are now deprecated.</p>
1992
 
</li>
1993
 
<li><p class="first">New function: <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgvEx()</span></code></a> sets the value of
1994
 
<code class="docutils literal"><span class="pre">sys.argv</span></code> and can optionally update <code class="docutils literal"><span class="pre">sys.path</span></code> to include the
1995
 
directory containing the script named by <code class="docutils literal"><span class="pre">sys.argv[0]</span></code> depending
1996
 
on the value of an <em>updatepath</em> parameter.</p>
1997
 
<p>This function was added to close a security hole for applications
1998
 
that embed Python.  The old function, <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></code></a>, would
1999
 
always update <code class="docutils literal"><span class="pre">sys.path</span></code>, and sometimes it would add the current
2000
 
directory.  This meant that, if you ran an application embedding
2001
 
Python in a directory controlled by someone else, attackers could
2002
 
put a Trojan-horse module in the directory (say, a file named
2003
 
<code class="file docutils literal"><span class="pre">os.py</span></code>) that your application would then import and run.</p>
2004
 
<p>If you maintain a C/C++ application that embeds Python, check
2005
 
whether you&#8217;re calling <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider
2006
 
whether the application should be using <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgvEx()</span></code></a>
2007
 
with <em>updatepath</em> set to false.</p>
2008
 
<p>Security issue reported as <a class="reference external" href="http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983">CVE-2008-5983</a>;
2009
 
discussed in <a class="reference external" href="https://bugs.python.org/issue5753">issue 5753</a>, and fixed by Antoine Pitrou.</p>
2010
 
</li>
2011
 
<li><p class="first">New macros: the Python header files now define the following macros:
2012
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISALNUM</span></code>,
2013
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISALPHA</span></code>,
2014
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISDIGIT</span></code>,
2015
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISLOWER</span></code>,
2016
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISSPACE</span></code>,
2017
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISUPPER</span></code>,
2018
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_ISXDIGIT</span></code>,
2019
 
<code class="xref c c-macro docutils literal"><span class="pre">Py_TOLOWER</span></code>, and <code class="xref c c-macro docutils literal"><span class="pre">Py_TOUPPER</span></code>.
2020
 
All of these functions are analogous to the C
2021
 
standard macros for classifying characters, but ignore the current
2022
 
locale setting, because in
2023
 
several places Python needs to analyze characters in a
2024
 
locale-independent way.  (Added by Eric Smith;
2025
 
<a class="reference external" href="https://bugs.python.org/issue5793">issue 5793</a>.)</p>
2026
 
</li>
2027
 
<li><p class="first">Removed function: <code class="xref c c-macro docutils literal"><span class="pre">PyEval_CallObject</span></code> is now only available
2028
 
as a macro.  A function version was being kept around to preserve
2029
 
ABI linking compatibility, but that was in 1997; it can certainly be
2030
 
deleted by now.  (Removed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue8276">issue 8276</a>.)</p>
2031
 
</li>
2032
 
<li><p class="first">New format codes: the <code class="xref c c-func docutils literal"><span class="pre">PyFormat_FromString()</span></code>,
2033
 
<code class="xref c c-func docutils literal"><span class="pre">PyFormat_FromStringV()</span></code>, and <a class="reference internal" href="../c-api/exceptions.html#c.PyErr_Format" title="PyErr_Format"><code class="xref c c-func docutils literal"><span class="pre">PyErr_Format()</span></code></a> functions now
2034
 
accept <code class="docutils literal"><span class="pre">%lld</span></code> and <code class="docutils literal"><span class="pre">%llu</span></code> format codes for displaying
2035
 
C&#8217;s <code class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></code> types.
2036
 
(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue7228">issue 7228</a>.)</p>
2037
 
</li>
2038
 
<li><p class="first">The complicated interaction between threads and process forking has
2039
 
been changed.  Previously, the child process created by
2040
 
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">os.fork()</span></code></a> might fail because the child is created with only a
2041
 
single thread running, the thread performing the <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">os.fork()</span></code></a>.
2042
 
If other threads were holding a lock, such as Python&#8217;s import lock,
2043
 
when the fork was performed, the lock would still be marked as
2044
 
&#8220;held&#8221; in the new process.  But in the child process nothing would
2045
 
ever release the lock, since the other threads weren&#8217;t replicated,
2046
 
and the child process would no longer be able to perform imports.</p>
2047
 
<p>Python 2.7 acquires the import lock before performing an
2048
 
<a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal"><span class="pre">os.fork()</span></code></a>, and will also clean up any locks created using the
2049
 
<a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal"><span class="pre">threading</span></code></a> module.  C extension modules that have internal
2050
 
locks, or that call <code class="xref c c-func docutils literal"><span class="pre">fork()</span></code> themselves, will not benefit
2051
 
from this clean-up.</p>
2052
 
<p>(Fixed by Thomas Wouters; <a class="reference external" href="https://bugs.python.org/issue1590864">issue 1590864</a>.)</p>
2053
 
</li>
2054
 
<li><p class="first">The <a class="reference internal" href="../c-api/init.html#c.Py_Finalize" title="Py_Finalize"><code class="xref c c-func docutils literal"><span class="pre">Py_Finalize()</span></code></a> function now calls the internal
2055
 
<code class="xref py py-func docutils literal"><span class="pre">threading._shutdown()</span></code> function; this prevents some exceptions from
2056
 
being raised when an interpreter shuts down.
2057
 
(Patch by Adam Olsen; <a class="reference external" href="https://bugs.python.org/issue1722344">issue 1722344</a>.)</p>
2058
 
</li>
2059
 
<li><p class="first">When using the <a class="reference internal" href="../c-api/structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal"><span class="pre">PyMemberDef</span></code></a> structure to define attributes
2060
 
of a type, Python will no longer let you try to delete or set a
2061
 
<code class="xref py py-const docutils literal"><span class="pre">T_STRING_INPLACE</span></code> attribute.</p>
2062
 
</li>
2063
 
<li><p class="first">Global symbols defined by the <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal"><span class="pre">ctypes</span></code></a> module are now prefixed
2064
 
with <code class="docutils literal"><span class="pre">Py</span></code>, or with <code class="docutils literal"><span class="pre">_ctypes</span></code>.  (Implemented by Thomas
2065
 
Heller; <a class="reference external" href="https://bugs.python.org/issue3102">issue 3102</a>.)</p>
2066
 
</li>
2067
 
<li><p class="first">New configure option: the <code class="xref std std-option docutils literal"><span class="pre">--with-system-expat</span></code> switch allows
2068
 
building the <code class="xref py py-mod docutils literal"><span class="pre">pyexpat</span></code> module to use the system Expat library.
2069
 
(Contributed by Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue7609">issue 7609</a>.)</p>
2070
 
</li>
2071
 
<li><p class="first">New configure option: the
2072
 
<code class="xref std std-option docutils literal"><span class="pre">--with-valgrind</span></code> option will now disable the pymalloc
2073
 
allocator, which is difficult for the Valgrind memory-error detector
2074
 
to analyze correctly.
2075
 
Valgrind will therefore be better at detecting memory leaks and
2076
 
overruns. (Contributed by James Henstridge; <a class="reference external" href="https://bugs.python.org/issue2422">issue 2422</a>.)</p>
2077
 
</li>
2078
 
<li><p class="first">New configure option: you can now supply an empty string to
2079
 
<code class="xref std std-option docutils literal"><span class="pre">--with-dbmliborder=</span></code> in order to disable all of the various
2080
 
DBM modules.  (Added by Arfrever Frehtes Taifersar Arahesis;
2081
 
<a class="reference external" href="https://bugs.python.org/issue6491">issue 6491</a>.)</p>
2082
 
</li>
2083
 
<li><p class="first">The <strong class="program">configure</strong> script now checks for floating-point rounding bugs
2084
 
on certain 32-bit Intel chips and defines a <code class="xref c c-macro docutils literal"><span class="pre">X87_DOUBLE_ROUNDING</span></code>
2085
 
preprocessor definition.  No code currently uses this definition,
2086
 
but it&#8217;s available if anyone wishes to use it.
2087
 
(Added by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue2937">issue 2937</a>.)</p>
2088
 
<p><strong class="program">configure</strong> also now sets a <span class="target" id="index-12"></span><code class="xref std std-envvar docutils literal"><span class="pre">LDCXXSHARED</span></code> Makefile
2089
 
variable for supporting C++ linking.  (Contributed by Arfrever
2090
 
Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue1222585">issue 1222585</a>.)</p>
2091
 
</li>
2092
 
<li><p class="first">The build process now creates the necessary files for pkg-config
2093
 
support.  (Contributed by Clinton Roy; <a class="reference external" href="https://bugs.python.org/issue3585">issue 3585</a>.)</p>
2094
 
</li>
2095
 
<li><p class="first">The build process now supports Subversion 1.7.  (Contributed by
2096
 
Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue6094">issue 6094</a>.)</p>
2097
 
</li>
2098
 
</ul>
2099
 
<div class="section" id="capsules">
2100
 
<span id="whatsnew27-capsules"></span><h3>Capsules<a class="headerlink" href="#capsules" title="Permalink to this headline">¶</a></h3>
2101
 
<p>Python 3.1 adds a new C datatype, <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal"><span class="pre">PyCapsule</span></code></a>, for providing a
2102
 
C API to an extension module.  A capsule is essentially the holder of
2103
 
a C <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code> pointer, and is made available as a module attribute; for
2104
 
example, the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a> module&#8217;s API is exposed as <code class="docutils literal"><span class="pre">socket.CAPI</span></code>,
2105
 
and <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal"><span class="pre">unicodedata</span></code></a> exposes <code class="docutils literal"><span class="pre">ucnhash_CAPI</span></code>.  Other extensions
2106
 
can import the module, access its dictionary to get the capsule
2107
 
object, and then get the <code class="docutils literal"><span class="pre">void</span> <span class="pre">*</span></code> pointer, which will usually point
2108
 
to an array of pointers to the module&#8217;s various API functions.</p>
2109
 
<p>There is an existing data type already used for this,
2110
 
<code class="xref c c-type docutils literal"><span class="pre">PyCObject</span></code>, but it doesn&#8217;t provide type safety.  Evil code
2111
 
written in pure Python could cause a segmentation fault by taking a
2112
 
<code class="xref c c-type docutils literal"><span class="pre">PyCObject</span></code> from module A and somehow substituting it for the
2113
 
<code class="xref c c-type docutils literal"><span class="pre">PyCObject</span></code> in module B.   Capsules know their own name,
2114
 
and getting the pointer requires providing the name:</p>
2115
 
<div class="highlight-python3"><div class="highlight"><pre>void *vtable;
2116
 
 
2117
 
if (!PyCapsule_IsValid(capsule, &quot;mymodule.CAPI&quot;) {
2118
 
        PyErr_SetString(PyExc_ValueError, &quot;argument type invalid&quot;);
2119
 
        return NULL;
2120
 
}
2121
 
 
2122
 
vtable = PyCapsule_GetPointer(capsule, &quot;mymodule.CAPI&quot;);
2123
 
</pre></div>
2124
 
</div>
2125
 
<p>You are assured that <code class="docutils literal"><span class="pre">vtable</span></code> points to whatever you&#8217;re expecting.
2126
 
If a different capsule was passed in, <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal"><span class="pre">PyCapsule_IsValid()</span></code></a> would
2127
 
detect the mismatched name and return false.  Refer to
2128
 
<a class="reference internal" href="../extending/extending.html#using-capsules"><span>Providing a C API for an Extension Module</span></a> for more information on using these objects.</p>
2129
 
<p>Python 2.7 now uses capsules internally to provide various
2130
 
extension-module APIs, but the <code class="xref c c-func docutils literal"><span class="pre">PyCObject_AsVoidPtr()</span></code> was
2131
 
modified to handle capsules, preserving compile-time compatibility
2132
 
with the <code class="xref c c-type docutils literal"><span class="pre">CObject</span></code> interface.  Use of
2133
 
<code class="xref c c-func docutils literal"><span class="pre">PyCObject_AsVoidPtr()</span></code> will signal a
2134
 
<a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></code></a>, which is silent by default.</p>
2135
 
<p>Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
2136
 
discussed in <a class="reference external" href="https://bugs.python.org/issue5630">issue 5630</a>.</p>
2137
 
</div>
2138
 
<div class="section" id="port-specific-changes-windows">
2139
 
<h3>Port-Specific Changes: Windows<a class="headerlink" href="#port-specific-changes-windows" title="Permalink to this headline">¶</a></h3>
2140
 
<ul class="simple">
2141
 
<li>The <a class="reference internal" href="../library/msvcrt.html#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><code class="xref py py-mod docutils literal"><span class="pre">msvcrt</span></code></a> module now contains some constants from
2142
 
the <code class="file docutils literal"><span class="pre">crtassem.h</span></code> header file:
2143
 
<code class="xref py py-data docutils literal"><span class="pre">CRT_ASSEMBLY_VERSION</span></code>,
2144
 
<code class="xref py py-data docutils literal"><span class="pre">VC_ASSEMBLY_PUBLICKEYTOKEN</span></code>,
2145
 
and <code class="xref py py-data docutils literal"><span class="pre">LIBRARIES_ASSEMBLY_NAME_PREFIX</span></code>.
2146
 
(Contributed by David Cournapeau; <a class="reference external" href="https://bugs.python.org/issue4365">issue 4365</a>.)</li>
2147
 
<li>The <code class="xref py py-mod docutils literal"><span class="pre">_winreg</span></code> module for accessing the registry now implements
2148
 
the <code class="xref py py-func docutils literal"><span class="pre">CreateKeyEx()</span></code> and <code class="xref py py-func docutils literal"><span class="pre">DeleteKeyEx()</span></code>
2149
 
functions, extended versions of previously-supported functions that
2150
 
take several extra arguments.  The <code class="xref py py-func docutils literal"><span class="pre">DisableReflectionKey()</span></code>,
2151
 
<code class="xref py py-func docutils literal"><span class="pre">EnableReflectionKey()</span></code>, and <code class="xref py py-func docutils literal"><span class="pre">QueryReflectionKey()</span></code>
2152
 
were also tested and documented.
2153
 
(Implemented by Brian Curtin: <a class="reference external" href="https://bugs.python.org/issue7347">issue 7347</a>.)</li>
2154
 
<li>The new <code class="xref c c-func docutils literal"><span class="pre">_beginthreadex()</span></code> API is used to start threads, and
2155
 
the native thread-local storage functions are now used.
2156
 
(Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue3582">issue 3582</a>.)</li>
2157
 
<li>The <a class="reference internal" href="../library/os.html#os.kill" title="os.kill"><code class="xref py py-func docutils literal"><span class="pre">os.kill()</span></code></a> function now works on Windows.  The signal value
2158
 
can be the constants <code class="xref py py-const docutils literal"><span class="pre">CTRL_C_EVENT</span></code>,
2159
 
<code class="xref py py-const docutils literal"><span class="pre">CTRL_BREAK_EVENT</span></code>, or any integer.  The first two constants
2160
 
will send <code class="kbd docutils literal"><span class="pre">Control-C</span></code> and <code class="kbd docutils literal"><span class="pre">Control-Break</span></code> keystroke events to
2161
 
subprocesses; any other value will use the <code class="xref c c-func docutils literal"><span class="pre">TerminateProcess()</span></code>
2162
 
API.  (Contributed by Miki Tebeka; <a class="reference external" href="https://bugs.python.org/issue1220212">issue 1220212</a>.)</li>
2163
 
<li>The <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></code></a> function now correctly fails
2164
 
for an empty path.  (Fixed by Hirokazu Yamamoto; <a class="reference external" href="https://bugs.python.org/issue5913">issue 5913</a>.)</li>
2165
 
<li>The <code class="xref py py-mod docutils literal"><span class="pre">mimelib</span></code> module will now read the MIME database from
2166
 
the Windows registry when initializing.
2167
 
(Patch by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue4969">issue 4969</a>.)</li>
2168
 
</ul>
2169
 
</div>
2170
 
<div class="section" id="port-specific-changes-mac-os-x">
2171
 
<h3>Port-Specific Changes: Mac OS X<a class="headerlink" href="#port-specific-changes-mac-os-x" title="Permalink to this headline">¶</a></h3>
2172
 
<ul class="simple">
2173
 
<li>The path <code class="docutils literal"><span class="pre">/Library/Python/2.7/site-packages</span></code> is now appended to
2174
 
<code class="docutils literal"><span class="pre">sys.path</span></code>, in order to share added packages between the system
2175
 
installation and a user-installed copy of the same version.
2176
 
(Changed by Ronald Oussoren; <a class="reference external" href="https://bugs.python.org/issue4865">issue 4865</a>.)</li>
2177
 
</ul>
2178
 
</div>
2179
 
<div class="section" id="port-specific-changes-freebsd">
2180
 
<h3>Port-Specific Changes: FreeBSD<a class="headerlink" href="#port-specific-changes-freebsd" title="Permalink to this headline">¶</a></h3>
2181
 
<ul class="simple">
2182
 
<li>FreeBSD 7.1&#8217;s <code class="xref py py-const docutils literal"><span class="pre">SO_SETFIB</span></code> constant, used with
2183
 
<code class="xref py py-func docutils literal"><span class="pre">getsockopt()</span></code>/<code class="xref py py-func docutils literal"><span class="pre">setsockopt()</span></code> to select an
2184
 
alternate routing table, is now available in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><code class="xref py py-mod docutils literal"><span class="pre">socket</span></code></a>
2185
 
module.  (Added by Kyle VanderBeek; <a class="reference external" href="https://bugs.python.org/issue8235">issue 8235</a>.)</li>
2186
 
</ul>
2187
 
</div>
2188
 
</div>
2189
 
<div class="section" id="other-changes-and-fixes">
2190
 
<h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Permalink to this headline">¶</a></h2>
2191
 
<ul class="simple">
2192
 
<li>Two benchmark scripts, <code class="file docutils literal"><span class="pre">iobench</span></code> and <code class="file docutils literal"><span class="pre">ccbench</span></code>, were
2193
 
added to the <code class="file docutils literal"><span class="pre">Tools</span></code> directory.  <code class="file docutils literal"><span class="pre">iobench</span></code> measures the
2194
 
speed of the built-in file I/O objects returned by <a class="reference internal" href="../library/functions.html#open" title="open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a>
2195
 
while performing various operations, and <code class="file docutils literal"><span class="pre">ccbench</span></code> is a
2196
 
concurrency benchmark that tries to measure computing throughput,
2197
 
thread switching latency, and IO processing bandwidth when
2198
 
performing several tasks using a varying number of threads.</li>
2199
 
<li>The <code class="file docutils literal"><span class="pre">Tools/i18n/msgfmt.py</span></code> script now understands plural
2200
 
forms in <code class="file docutils literal"><span class="pre">.po</span></code> files.  (Fixed by Martin von Löwis;
2201
 
<a class="reference external" href="https://bugs.python.org/issue5464">issue 5464</a>.)</li>
2202
 
<li>When importing a module from a <code class="file docutils literal"><span class="pre">.pyc</span></code> or <code class="file docutils literal"><span class="pre">.pyo</span></code> file
2203
 
with an existing <code class="file docutils literal"><span class="pre">.py</span></code> counterpart, the <code class="xref py py-attr docutils literal"><span class="pre">co_filename</span></code>
2204
 
attributes of the resulting code objects are overwritten when the
2205
 
original filename is obsolete.  This can happen if the file has been
2206
 
renamed, moved, or is accessed through different paths.  (Patch by
2207
 
Ziga Seilnacht and Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue1180193">issue 1180193</a>.)</li>
2208
 
<li>The <code class="file docutils literal"><span class="pre">regrtest.py</span></code> script now takes a <code class="xref std std-option docutils literal"><span class="pre">--randseed=</span></code>
2209
 
switch that takes an integer that will be used as the random seed
2210
 
for the <code class="xref std std-option docutils literal"><span class="pre">-r</span></code> option that executes tests in random order.
2211
 
The <code class="xref std std-option docutils literal"><span class="pre">-r</span></code> option also reports the seed that was used
2212
 
(Added by Collin Winter.)</li>
2213
 
<li>Another <code class="file docutils literal"><span class="pre">regrtest.py</span></code> switch is <code class="xref std std-option docutils literal"><span class="pre">-j</span></code>, which
2214
 
takes an integer specifying how many tests run in parallel. This
2215
 
allows reducing the total runtime on multi-core machines.
2216
 
This option is compatible with several other options, including the
2217
 
<a class="reference internal" href="../using/cmdline.html#cmdoption-R"><code class="xref std std-option docutils literal"><span class="pre">-R</span></code></a> switch which is known to produce long runtimes.
2218
 
(Added by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue6152">issue 6152</a>.)  This can also be used
2219
 
with a new <code class="xref std std-option docutils literal"><span class="pre">-F</span></code> switch that runs selected tests in a loop
2220
 
until they fail.  (Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue7312">issue 7312</a>.)</li>
2221
 
<li>When executed as a script, the <code class="file docutils literal"><span class="pre">py_compile.py</span></code> module now
2222
 
accepts <code class="docutils literal"><span class="pre">'-'</span></code> as an argument, which will read standard input for
2223
 
the list of filenames to be compiled.  (Contributed by Piotr
2224
 
Ożarowski; <a class="reference external" href="https://bugs.python.org/issue8233">issue 8233</a>.)</li>
2225
 
</ul>
2226
 
</div>
2227
 
<div class="section" id="porting-to-python-2-7">
2228
 
<h2>Porting to Python 2.7<a class="headerlink" href="#porting-to-python-2-7" title="Permalink to this headline">¶</a></h2>
2229
 
<p>This section lists previously described changes and other bugfixes
2230
 
that may require changes to your code:</p>
2231
 
<ul class="simple">
2232
 
<li>The <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal"><span class="pre">range()</span></code></a> function processes its arguments more
2233
 
consistently; it will now call <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal"><span class="pre">__int__()</span></code></a> on non-float,
2234
 
non-integer arguments that are supplied to it.  (Fixed by Alexander
2235
 
Belopolsky; <a class="reference external" href="https://bugs.python.org/issue1533">issue 1533</a>.)</li>
2236
 
<li>The string <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal"><span class="pre">format()</span></code></a> method changed the default precision used
2237
 
for floating-point and complex numbers from 6 decimal
2238
 
places to 12, which matches the precision used by <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal"><span class="pre">str()</span></code></a>.
2239
 
(Changed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue5920">issue 5920</a>.)</li>
2240
 
<li>Because of an optimization for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement, the special
2241
 
methods <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></code></a> must belong to the object&#8217;s
2242
 
type, and cannot be directly attached to the object&#8217;s instance.  This
2243
 
affects new-style classes (derived from <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal"><span class="pre">object</span></code></a>) and C extension
2244
 
types.  (<a class="reference external" href="https://bugs.python.org/issue6101">issue 6101</a>.)</li>
2245
 
<li>Due to a bug in Python 2.6, the <em>exc_value</em> parameter to
2246
 
<a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></code></a> methods was often the string representation of the
2247
 
exception, not an instance.  This was fixed in 2.7, so <em>exc_value</em>
2248
 
will be an instance as expected.  (Fixed by Florent Xicluna;
2249
 
<a class="reference external" href="https://bugs.python.org/issue7853">issue 7853</a>.)</li>
2250
 
<li>When a restricted set of attributes were set using <code class="docutils literal"><span class="pre">__slots__</span></code>,
2251
 
deleting an unset attribute would not raise <a class="reference internal" href="../library/exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></code></a>
2252
 
as you would expect.  Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue7604">issue 7604</a>.)</li>
2253
 
</ul>
2254
 
<p>In the standard library:</p>
2255
 
<ul>
2256
 
<li><p class="first">Operations with <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal"><span class="pre">datetime</span></code></a> instances that resulted in a year
2257
 
falling outside the supported range didn&#8217;t always raise
2258
 
<a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></code></a>.  Such errors are now checked more carefully
2259
 
and will now raise the exception. (Reported by Mark Leander, patch
2260
 
by Anand B. Pillai and Alexander Belopolsky; <a class="reference external" href="https://bugs.python.org/issue7150">issue 7150</a>.)</p>
2261
 
</li>
2262
 
<li><p class="first">When using <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal"><span class="pre">Decimal</span></code></a> instances with a string&#8217;s
2263
 
<a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal"><span class="pre">format()</span></code></a> method, the default alignment was previously
2264
 
left-alignment.  This has been changed to right-alignment, which might
2265
 
change the output of your programs.
2266
 
(Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue6857">issue 6857</a>.)</p>
2267
 
<p>Comparisons involving a signaling NaN value (or <code class="docutils literal"><span class="pre">sNAN</span></code>) now signal
2268
 
<a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal"><span class="pre">InvalidOperation</span></code></a> instead of silently returning a true or
2269
 
false value depending on the comparison operator.  Quiet NaN values
2270
 
(or <code class="docutils literal"><span class="pre">NaN</span></code>) are now hashable.  (Fixed by Mark Dickinson;
2271
 
<a class="reference external" href="https://bugs.python.org/issue7279">issue 7279</a>.)</p>
2272
 
</li>
2273
 
<li><p class="first">The ElementTree library, <code class="xref py py-mod docutils literal"><span class="pre">xml.etree</span></code>, no longer escapes
2274
 
ampersands and angle brackets when outputting an XML processing
2275
 
instruction (which looks like <cite>&lt;?xml-stylesheet href=&#8221;#style1&#8221;?&gt;</cite>)
2276
 
or comment (which looks like <cite>&lt;!&#8211; comment &#8211;&gt;</cite>).
2277
 
(Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue2746">issue 2746</a>.)</p>
2278
 
</li>
2279
 
<li><p class="first">The <code class="xref py py-meth docutils literal"><span class="pre">readline()</span></code> method of <code class="xref py py-class docutils literal"><span class="pre">StringIO</span></code> objects now does
2280
 
nothing when a negative length is requested, as other file-like
2281
 
objects do.  (<a class="reference external" href="https://bugs.python.org/issue7348">issue 7348</a>).</p>
2282
 
</li>
2283
 
<li><p class="first">The <a class="reference internal" href="../library/syslog.html#module-syslog" title="syslog: An interface to the Unix syslog library routines. (Unix)"><code class="xref py py-mod docutils literal"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal"><span class="pre">sys.argv[0]</span></code> as the
2284
 
identifier instead of the previous default value of <code class="docutils literal"><span class="pre">'python'</span></code>.
2285
 
(Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue8451">issue 8451</a>.)</p>
2286
 
</li>
2287
 
<li><p class="first">The <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><code class="xref py py-mod docutils literal"><span class="pre">tarfile</span></code></a> module&#8217;s default error handling has changed, to
2288
 
no longer suppress fatal errors.  The default error level was previously 0,
2289
 
which meant that errors would only result in a message being written to the
2290
 
debug log, but because the debug log is not activated by default,
2291
 
these errors go unnoticed.  The default error level is now 1,
2292
 
which raises an exception if there&#8217;s an error.
2293
 
(Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue7357">issue 7357</a>.)</p>
2294
 
</li>
2295
 
<li><p class="first">The <code class="xref py py-mod docutils literal"><span class="pre">urlparse</span></code> module&#8217;s <code class="xref py py-func docutils literal"><span class="pre">urlsplit()</span></code> now handles
2296
 
unknown URL schemes in a fashion compliant with <span class="target" id="index-13"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
2297
 
URL is of the form <code class="docutils literal"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
2298
 
<code class="docutils literal"><span class="pre">://</span></code> is treated as the scheme, even if it&#8217;s a made-up scheme that
2299
 
the module doesn&#8217;t know about.  This change may break code that
2300
 
worked around the old behaviour.  For example, Python 2.6.4 or 2.5
2301
 
will return the following:</p>
2302
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
2303
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
2304
 
<span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
2305
 
</pre></div>
2306
 
</div>
2307
 
<p>Python 2.7 (and Python 2.6.5) will return:</p>
2308
 
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
2309
 
<span class="gp">&gt;&gt;&gt; </span><span class="n">urlparse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s1">&#39;invented://host/filename?query&#39;</span><span class="p">)</span>
2310
 
<span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
2311
 
</pre></div>
2312
 
</div>
2313
 
<p>(Python 2.7 actually produces slightly different output, since it
2314
 
returns a named tuple instead of a standard tuple.)</p>
2315
 
</li>
2316
 
</ul>
2317
 
<p>For C extensions:</p>
2318
 
<ul class="simple">
2319
 
<li>C extensions that use integer format codes with the <code class="docutils literal"><span class="pre">PyArg_Parse*</span></code>
2320
 
family of functions will now raise a <a class="reference internal" href="../library/exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> exception
2321
 
instead of triggering a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal"><span class="pre">DeprecationWarning</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue5080">issue 5080</a>).</li>
2322
 
<li>Use the new <a class="reference internal" href="../c-api/conversion.html#c.PyOS_string_to_double" title="PyOS_string_to_double"><code class="xref c c-func docutils literal"><span class="pre">PyOS_string_to_double()</span></code></a> function instead of the old
2323
 
<code class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal"><span class="pre">PyOS_ascii_atof()</span></code> functions,
2324
 
which are now deprecated.</li>
2325
 
</ul>
2326
 
<p>For applications that embed Python:</p>
2327
 
<ul class="simple">
2328
 
<li>The <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgvEx()</span></code></a> function was added, letting
2329
 
applications close a security hole when the existing
2330
 
<a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></code></a> function was used.  Check whether you&#8217;re
2331
 
calling <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider whether the
2332
 
application should be using <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal"><span class="pre">PySys_SetArgvEx()</span></code></a> with
2333
 
<em>updatepath</em> set to false.</li>
2334
 
</ul>
2335
 
</div>
2336
 
<div class="section" id="new-features-added-to-python-2-7-maintenance-releases">
2337
 
<span id="py27-maintenance-enhancements"></span><h2>New Features Added to Python 2.7 Maintenance Releases<a class="headerlink" href="#new-features-added-to-python-2-7-maintenance-releases" title="Permalink to this headline">¶</a></h2>
2338
 
<p>New features may be added to Python 2.7 maintenance releases when the
2339
 
situation genuinely calls for it. Any such additions must go through
2340
 
the Python Enhancement Proposal process, and make a compelling case for why
2341
 
they can&#8217;t be adequately addressed by either adding the new feature solely to
2342
 
Python 3, or else by publishing it on the Python Package Index.</p>
2343
 
<p>In addition to the specific proposals listed below, there is a general
2344
 
exemption allowing new <code class="docutils literal"><span class="pre">-3</span></code> warnings to be added in any Python 2.7
2345
 
maintenance release.</p>
2346
 
<div class="section" id="pep-434-idle-enhancement-exception-for-all-branches">
2347
 
<h3>PEP 434: IDLE Enhancement Exception for All Branches<a class="headerlink" href="#pep-434-idle-enhancement-exception-for-all-branches" title="Permalink to this headline">¶</a></h3>
2348
 
<p><span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0434"><strong>PEP 434</strong></a> describes a general exemption for changes made to the IDLE
2349
 
development environment shipped along with Python. This exemption makes it
2350
 
possible for the IDLE developers to provide a more consistent user
2351
 
experience across all supported versions of Python 2 and 3.</p>
2352
 
<p>For details of any IDLE changes, refer to the NEWS file for the specific
2353
 
release.</p>
2354
 
</div>
2355
 
<div class="section" id="pep-466-network-security-enhancements-for-python-2-7">
2356
 
<h3>PEP 466: Network Security Enhancements for Python 2.7<a class="headerlink" href="#pep-466-network-security-enhancements-for-python-2-7" title="Permalink to this headline">¶</a></h3>
2357
 
<p><span class="target" id="index-15"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> describes a number of network security enhancement proposals
2358
 
that have been approved for inclusion in Python 2.7 maintenance releases,
2359
 
with the first of those changes appearing in the Python 2.7.7 release.</p>
2360
 
<p><span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> related features added in Python 2.7.7:</p>
2361
 
<ul class="simple">
2362
 
<li><a class="reference internal" href="../library/hmac.html#hmac.compare_digest" title="hmac.compare_digest"><code class="xref py py-func docutils literal"><span class="pre">hmac.compare_digest()</span></code></a> was backported from Python 3 to make a timing
2363
 
attack resistant comparison operation available to Python 2 applications.
2364
 
(Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue21306">issue 21306</a>.)</li>
2365
 
<li>OpenSSL 1.0.1g was upgraded in the official Windows installers published on
2366
 
python.org. (Contributed by Zachary Ware; <a class="reference external" href="https://bugs.python.org/issue21462">issue 21462</a>.)</li>
2367
 
</ul>
2368
 
<p><span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> related features added in Python 2.7.8:</p>
2369
 
<ul class="simple">
2370
 
<li><a class="reference internal" href="../library/hashlib.html#hashlib.pbkdf2_hmac" title="hashlib.pbkdf2_hmac"><code class="xref py py-func docutils literal"><span class="pre">hashlib.pbkdf2_hmac()</span></code></a> was backported from Python 3 to make a hashing
2371
 
algorithm suitable for secure password storage broadly available to Python
2372
 
2 applications. (Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue21304">issue 21304</a>.)</li>
2373
 
<li>OpenSSL 1.0.1h was upgraded for the official Windows installers published on
2374
 
python.org. (contributed by Zachary Ware in <a class="reference external" href="https://bugs.python.org/issue21671">issue 21671</a> for CVE-2014-0224)</li>
2375
 
</ul>
2376
 
<p><span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0466"><strong>PEP 466</strong></a> related features added in Python 2.7.9:</p>
2377
 
<ul class="simple">
2378
 
<li>Most of Python 3.4&#8217;s <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a> module was backported. This means <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal"><span class="pre">ssl</span></code></a>
2379
 
now supports Server Name Indication, TLS1.x settings, access to the platform
2380
 
certificate store, the <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal"><span class="pre">SSLContext</span></code></a> class, and other
2381
 
features. (Contributed by Alex Gaynor and David Reid; <a class="reference external" href="https://bugs.python.org/issue21308">issue 21308</a>.)</li>
2382
 
<li><a class="reference internal" href="../library/os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal"><span class="pre">os.urandom()</span></code></a> was changed to cache a file descriptor to <code class="docutils literal"><span class="pre">/dev/urandom</span></code>
2383
 
instead of reopening <code class="docutils literal"><span class="pre">/dev/urandom</span></code> on every call. (Contributed by Alex
2384
 
Gaynor; <a class="reference external" href="https://bugs.python.org/issue21305">issue 21305</a>.)</li>
2385
 
</ul>
2386
 
</div>
2387
 
</div>
2388
 
<div class="section" id="acknowledgements">
2389
 
<span id="acks27"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
2390
 
<p>The author would like to thank the following people for offering
2391
 
suggestions, corrections and assistance with various drafts of this
2392
 
article: Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray,
2393
 
Hugh Secker-Walker.</p>
2394
 
</div>
2395
 
</div>
2396
 
 
2397
 
 
2398
 
          </div>
2399
 
        </div>
2400
 
      </div>
2401
 
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
2402
 
        <div class="sphinxsidebarwrapper">
2403
 
  <h3><a href="../contents.html">Table Of Contents</a></h3>
2404
 
  <ul>
2405
 
<li><a class="reference internal" href="#">What&#8217;s New in Python 2.7</a><ul>
2406
 
<li><a class="reference internal" href="#the-future-for-python-2-x">The Future for Python 2.x</a></li>
2407
 
<li><a class="reference internal" href="#changes-to-the-handling-of-deprecation-warnings">Changes to the Handling of Deprecation Warnings</a></li>
2408
 
<li><a class="reference internal" href="#python-3-1-features">Python 3.1 Features</a></li>
2409
 
<li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372: Adding an Ordered Dictionary to collections</a></li>
2410
 
<li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: Format Specifier for Thousands Separator</a></li>
2411
 
<li><a class="reference internal" href="#pep-389-the-argparse-module-for-parsing-command-lines">PEP 389: The argparse Module for Parsing Command Lines</a></li>
2412
 
<li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary-Based Configuration For Logging</a></li>
2413
 
<li><a class="reference internal" href="#pep-3106-dictionary-views">PEP 3106: Dictionary Views</a></li>
2414
 
<li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: The memoryview Object</a></li>
2415
 
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
2416
 
<li><a class="reference internal" href="#interpreter-changes">Interpreter Changes</a></li>
2417
 
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
2418
 
</ul>
2419
 
</li>
2420
 
<li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a><ul>
2421
 
<li><a class="reference internal" href="#new-module-importlib">New module: importlib</a></li>
2422
 
<li><a class="reference internal" href="#new-module-sysconfig">New module: sysconfig</a></li>
2423
 
<li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttk: Themed Widgets for Tk</a></li>
2424
 
<li><a class="reference internal" href="#updated-module-unittest">Updated module: unittest</a></li>
2425
 
<li><a class="reference internal" href="#updated-module-elementtree-1-3">Updated module: ElementTree 1.3</a></li>
2426
 
</ul>
2427
 
</li>
2428
 
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
2429
 
<li><a class="reference internal" href="#capsules">Capsules</a></li>
2430
 
<li><a class="reference internal" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
2431
 
<li><a class="reference internal" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
2432
 
<li><a class="reference internal" href="#port-specific-changes-freebsd">Port-Specific Changes: FreeBSD</a></li>
2433
 
</ul>
2434
 
</li>
2435
 
<li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
2436
 
<li><a class="reference internal" href="#porting-to-python-2-7">Porting to Python 2.7</a></li>
2437
 
<li><a class="reference internal" href="#new-features-added-to-python-2-7-maintenance-releases">New Features Added to Python 2.7 Maintenance Releases</a><ul>
2438
 
<li><a class="reference internal" href="#pep-434-idle-enhancement-exception-for-all-branches">PEP 434: IDLE Enhancement Exception for All Branches</a></li>
2439
 
<li><a class="reference internal" href="#pep-466-network-security-enhancements-for-python-2-7">PEP 466: Network Security Enhancements for Python 2.7</a></li>
2440
 
</ul>
2441
 
</li>
2442
 
<li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
2443
 
</ul>
2444
 
</li>
2445
 
</ul>
2446
 
 
2447
 
  <h4>Previous topic</h4>
2448
 
  <p class="topless"><a href="3.0.html"
2449
 
                        title="previous chapter">What&#8217;s New In Python 3.0</a></p>
2450
 
  <h4>Next topic</h4>
2451
 
  <p class="topless"><a href="2.6.html"
2452
 
                        title="next chapter">What&#8217;s New in Python 2.6</a></p>
2453
 
<h3>This Page</h3>
2454
 
<ul class="this-page-menu">
2455
 
  <li><a href="../bugs.html">Report a Bug</a></li>
2456
 
  <li><a href="../_sources/whatsnew/2.7.txt"
2457
 
         rel="nofollow">Show Source</a></li>
2458
 
</ul>
2459
 
 
2460
 
<div id="searchbox" style="display: none" role="search">
2461
 
  <h3>Quick search</h3>
2462
 
    <form class="search" action="../search.html" method="get">
2463
 
      <input type="text" name="q" />
2464
 
      <input type="submit" value="Go" />
2465
 
      <input type="hidden" name="check_keywords" value="yes" />
2466
 
      <input type="hidden" name="area" value="default" />
2467
 
    </form>
2468
 
    <p class="searchtip" style="font-size: 90%">
2469
 
    Enter search terms or a module, class or function name.
2470
 
    </p>
2471
 
</div>
2472
 
<script type="text/javascript">$('#searchbox').show(0);</script>
2473
 
        </div>
2474
 
      </div>
2475
 
      <div class="clearer"></div>
2476
 
    </div>  
2477
 
    <div class="related" role="navigation" aria-label="related navigation">
2478
 
      <h3>Navigation</h3>
2479
 
      <ul>
2480
 
        <li class="right" style="margin-right: 10px">
2481
 
          <a href="../genindex.html" title="General Index"
2482
 
             >index</a></li>
2483
 
        <li class="right" >
2484
 
          <a href="../py-modindex.html" title="Python Module Index"
2485
 
             >modules</a> |</li>
2486
 
        <li class="right" >
2487
 
          <a href="2.6.html" title="What’s New in Python 2.6"
2488
 
             >next</a> |</li>
2489
 
        <li class="right" >
2490
 
          <a href="3.0.html" title="What’s New In Python 3.0"
2491
 
             >previous</a> |</li>
2492
 
        <li><img src="../_static/py.png" alt=""
2493
 
                 style="vertical-align: middle; margin-top: -1px"/></li>
2494
 
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
2495
 
        <li>
2496
 
          <span class="version_switcher_placeholder">3.5.1</span>
2497
 
          <a href="../index.html">Documentation </a> &raquo;
2498
 
        </li>
2499
 
 
2500
 
          <li class="nav-item nav-item-1"><a href="index.html" >What&#8217;s New in Python</a> &raquo;</li> 
2501
 
      </ul>
2502
 
    </div>  
2503
 
    <div class="footer">
2504
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
2505
 
    <br />
2506
 
    The Python Software Foundation is a non-profit corporation.
2507
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
2508
 
    <br />
2509
 
    Last updated on Jan 22, 2016.
2510
 
    <a href="../bugs.html">Found a bug</a>?
2511
 
    <br />
2512
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
2513
 
    </div>
2514
 
 
2515
 
  </body>
2516
 
</html>
 
 
b'\\ No newline at end of file'