gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2812 lines
341KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="What’s New in Python 2.7" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/2.7.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, A.M. Kuchling (amk at amk.ca),. This article explains the new features in Python 2.7. Python 2.7 was released on July 3, 2010. Numeric handling has been improved in many ways, for both floa..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Author, A.M. Kuchling (amk at amk.ca),. This article explains the new features in Python 2.7. Python 2.7 was released on July 3, 2010. Numeric handling has been improved in many ways, for both floa..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>What’s New in Python 2.7 &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="What’s New in Python 2.6" href="2.6.html" />
  33. <link rel="prev" title="What’s New In Python 3.0" href="3.0.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/whatsnew/2.7.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">What’s New in Python 2.7</a><ul>
  86. <li><a class="reference internal" href="#the-future-for-python-2-x">The Future for Python 2.x</a></li>
  87. <li><a class="reference internal" href="#changes-to-the-handling-of-deprecation-warnings">Changes to the Handling of Deprecation Warnings</a></li>
  88. <li><a class="reference internal" href="#python-3-1-features">Python 3.1 Features</a></li>
  89. <li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372: Adding an Ordered Dictionary to collections</a></li>
  90. <li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: Format Specifier for Thousands Separator</a></li>
  91. <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>
  92. <li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary-Based Configuration For Logging</a></li>
  93. <li><a class="reference internal" href="#pep-3106-dictionary-views">PEP 3106: Dictionary Views</a></li>
  94. <li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: The memoryview Object</a></li>
  95. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
  96. <li><a class="reference internal" href="#interpreter-changes">Interpreter Changes</a></li>
  97. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  98. </ul>
  99. </li>
  100. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a><ul>
  101. <li><a class="reference internal" href="#new-module-importlib">New module: importlib</a></li>
  102. <li><a class="reference internal" href="#new-module-sysconfig">New module: sysconfig</a></li>
  103. <li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttk: Themed Widgets for Tk</a></li>
  104. <li><a class="reference internal" href="#updated-module-unittest">Updated module: unittest</a></li>
  105. <li><a class="reference internal" href="#updated-module-elementtree-1-3">Updated module: ElementTree 1.3</a></li>
  106. </ul>
  107. </li>
  108. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
  109. <li><a class="reference internal" href="#capsules">Capsules</a></li>
  110. <li><a class="reference internal" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
  111. <li><a class="reference internal" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
  112. <li><a class="reference internal" href="#port-specific-changes-freebsd">Port-Specific Changes: FreeBSD</a></li>
  113. </ul>
  114. </li>
  115. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  116. <li><a class="reference internal" href="#porting-to-python-2-7">Porting to Python 2.7</a></li>
  117. <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>
  118. <li><a class="reference internal" href="#two-new-environment-variables-for-debug-mode">Two new environment variables for debug mode</a></li>
  119. <li><a class="reference internal" href="#pep-434-idle-enhancement-exception-for-all-branches">PEP 434: IDLE Enhancement Exception for All Branches</a></li>
  120. <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>
  121. <li><a class="reference internal" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7">PEP 477: Backport ensurepip (PEP 453) to Python 2.7</a><ul>
  122. <li><a class="reference internal" href="#bootstrapping-pip-by-default">Bootstrapping pip By Default</a></li>
  123. <li><a class="reference internal" href="#documentation-changes">Documentation Changes</a></li>
  124. </ul>
  125. </li>
  126. <li><a class="reference internal" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">PEP 476: Enabling certificate verification by default for stdlib http clients</a></li>
  127. <li><a class="reference internal" href="#pep-493-https-verification-migration-tools-for-python-2-7">PEP 493: HTTPS verification migration tools for Python 2.7</a></li>
  128. <li><a class="reference internal" href="#new-make-regen-all-build-target">New <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> build target</a></li>
  129. <li><a class="reference internal" href="#removal-of-make-touch-build-target">Removal of <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> build target</a></li>
  130. </ul>
  131. </li>
  132. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  133. </ul>
  134. </li>
  135. </ul>
  136. </div>
  137. <div>
  138. <h4>Previous topic</h4>
  139. <p class="topless"><a href="3.0.html"
  140. title="previous chapter">What’s New In Python 3.0</a></p>
  141. </div>
  142. <div>
  143. <h4>Next topic</h4>
  144. <p class="topless"><a href="2.6.html"
  145. title="next chapter">What’s New in Python 2.6</a></p>
  146. </div>
  147. <div role="note" aria-label="source link">
  148. <h3>This Page</h3>
  149. <ul class="this-page-menu">
  150. <li><a href="../bugs.html">Report a Bug</a></li>
  151. <li>
  152. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.7.rst"
  153. rel="nofollow">Show Source
  154. </a>
  155. </li>
  156. </ul>
  157. </div>
  158. </nav>
  159. </div>
  160. </div>
  161. <div class="related" role="navigation" aria-label="related navigation">
  162. <h3>Navigation</h3>
  163. <ul>
  164. <li class="right" style="margin-right: 10px">
  165. <a href="../genindex.html" title="General Index"
  166. accesskey="I">index</a></li>
  167. <li class="right" >
  168. <a href="../py-modindex.html" title="Python Module Index"
  169. >modules</a> |</li>
  170. <li class="right" >
  171. <a href="2.6.html" title="What’s New in Python 2.6"
  172. accesskey="N">next</a> |</li>
  173. <li class="right" >
  174. <a href="3.0.html" title="What’s New In Python 3.0"
  175. accesskey="P">previous</a> |</li>
  176. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  177. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  178. <li class="switchers">
  179. <div class="language_switcher_placeholder"></div>
  180. <div class="version_switcher_placeholder"></div>
  181. </li>
  182. <li>
  183. </li>
  184. <li id="cpython-language-and-version">
  185. <a href="../index.html">3.12.3 Documentation</a> &#187;
  186. </li>
  187. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  188. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.7</a></li>
  189. <li class="right">
  190. <div class="inline-search" role="search">
  191. <form class="inline-search" action="../search.html" method="get">
  192. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  193. <input type="submit" value="Go" />
  194. </form>
  195. </div>
  196. |
  197. </li>
  198. <li class="right">
  199. <label class="theme-selector-label">
  200. Theme
  201. <select class="theme-selector" oninput="activateTheme(this.value)">
  202. <option value="auto" selected>Auto</option>
  203. <option value="light">Light</option>
  204. <option value="dark">Dark</option>
  205. </select>
  206. </label> |</li>
  207. </ul>
  208. </div>
  209. <div class="document">
  210. <div class="documentwrapper">
  211. <div class="bodywrapper">
  212. <div class="body" role="main">
  213. <section id="what-s-new-in-python-2-7">
  214. <h1>What’s New in Python 2.7<a class="headerlink" href="#what-s-new-in-python-2-7" title="Link to this heading">¶</a></h1>
  215. <dl class="field-list simple">
  216. <dt class="field-odd">Author<span class="colon">:</span></dt>
  217. <dd class="field-odd"><p>A.M. Kuchling (amk at amk.ca)</p>
  218. </dd>
  219. </dl>
  220. <p>This article explains the new features in Python 2.7. Python 2.7 was released
  221. on July 3, 2010.</p>
  222. <p>Numeric handling has been improved in many ways, for both
  223. 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 notranslate"><span class="pre">Decimal</span></code></a> class.
  224. There are some useful additions to the standard library, such as a
  225. 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 notranslate"><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 notranslate"><span class="pre">argparse</span></code></a> module
  226. 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 notranslate"><span class="pre">OrderedDict</span></code></a>
  227. and <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">collections</span></code></a> module,
  228. and many other improvements.</p>
  229. <p>Python 2.7 is planned to be the last of the 2.x releases, so we worked
  230. on making it a good release for the long term. To help with porting
  231. to Python 3, several new features from the Python 3.x series have been
  232. included in 2.7.</p>
  233. <p>This article doesn’t attempt to provide a complete specification of
  234. the new features, but instead provides a convenient overview. For
  235. full details, you should refer to the documentation for Python 2.7 at
  236. <a class="reference external" href="https://docs.python.org">https://docs.python.org</a>. If you want to understand the rationale for
  237. the design and implementation, refer to the PEP for a particular new
  238. feature or the issue on <a class="reference external" href="https://bugs.python.org">https://bugs.python.org</a> in which a change was
  239. discussed. Whenever possible, “What’s New in Python” links to the
  240. bug/patch item for each change.</p>
  241. <section id="the-future-for-python-2-x">
  242. <span id="whatsnew27-python31"></span><h2>The Future for Python 2.x<a class="headerlink" href="#the-future-for-python-2-x" title="Link to this heading">¶</a></h2>
  243. <p>Python 2.7 is the last major release in the 2.x series, as the Python
  244. maintainers have shifted the focus of their new feature development efforts
  245. to the Python 3.x series. This means that while Python 2 continues to
  246. receive bug fixes, and to be updated to build correctly on new hardware and
  247. versions of supported operated systems, there will be no new full feature
  248. releases for the language or standard library.</p>
  249. <p>However, while there is a large common subset between Python 2.7 and Python
  250. 3, and many of the changes involved in migrating to that common subset, or
  251. directly to Python 3, can be safely automated, some other changes (notably
  252. those associated with Unicode handling) may require careful consideration,
  253. and preferably robust automated regression test suites, to migrate
  254. effectively.</p>
  255. <p>This means that Python 2.7 will remain in place for a long time, providing a
  256. stable and supported base platform for production systems that have not yet
  257. been ported to Python 3. The full expected lifecycle of the Python 2.7
  258. series is detailed in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0373/"><strong>PEP 373</strong></a>.</p>
  259. <p>Some key consequences of the long-term significance of 2.7 are:</p>
  260. <ul class="simple">
  261. <li><p>As noted above, the 2.7 release has a much longer period of maintenance
  262. when compared to earlier 2.x versions. Python 2.7 is currently expected to
  263. remain supported by the core development team (receiving security updates
  264. and other bug fixes) until at least 2020 (10 years after its initial
  265. release, compared to the more typical support period of 18–24 months).</p></li>
  266. <li><p>As the Python 2.7 standard library ages, making effective use of the
  267. Python Package Index (either directly or via a redistributor) becomes
  268. more important for Python 2 users. In addition to a wide variety of third
  269. party packages for various tasks, the available packages include backports
  270. of new modules and features from the Python 3 standard library that are
  271. compatible with Python 2, as well as various tools and libraries that can
  272. 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
  273. installing software from the Python Package Index.</p></li>
  274. <li><p>While the preferred approach to enhancing Python 2 is now the publication
  275. of new packages on the Python Package Index, this approach doesn’t
  276. necessarily work in all cases, especially those related to network
  277. security. In exceptional cases that cannot be handled adequately by
  278. publishing new or updated packages on PyPI, the Python Enhancement
  279. Proposal process may be used to make the case for adding new features
  280. directly to the Python 2 standard library. Any such additions, and the
  281. maintenance releases where they were added, will be noted in the
  282. <a class="reference internal" href="#py27-maintenance-enhancements"><span class="std std-ref">New Features Added to Python 2.7 Maintenance Releases</span></a> section below.</p></li>
  283. </ul>
  284. <p>For projects wishing to migrate from Python 2 to Python 3, or for library
  285. and framework developers wishing to support users on both Python 2 and
  286. Python 3, there are a variety of tools and guides available to help decide
  287. on a suitable approach and manage some of the technical details involved.
  288. The recommended starting point is the <a class="reference internal" href="../howto/pyporting.html#pyporting-howto"><span class="std std-ref">How to port Python 2 Code to Python 3</span></a> HOWTO guide.</p>
  289. </section>
  290. <section id="changes-to-the-handling-of-deprecation-warnings">
  291. <h2>Changes to the Handling of Deprecation Warnings<a class="headerlink" href="#changes-to-the-handling-of-deprecation-warnings" title="Link to this heading">¶</a></h2>
  292. <p>For Python 2.7, a policy decision was made to silence warnings only of
  293. interest to developers by default. <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> and its
  294. descendants are now ignored unless otherwise requested, preventing
  295. users from seeing warnings triggered by an application. This change
  296. was also made in the branch that became Python 3.2. (Discussed
  297. on stdlib-sig and carried out in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7319">bpo-7319</a>.)</p>
  298. <p>In previous releases, <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> messages were
  299. enabled by default, providing Python developers with a clear
  300. indication of where their code may break in a future major version
  301. of Python.</p>
  302. <p>However, there are increasingly many users of Python-based
  303. applications who are not directly involved in the development of
  304. those applications. <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> messages are
  305. irrelevant to such users, making them worry about an application
  306. that’s actually working correctly and burdening application developers
  307. with responding to these concerns.</p>
  308. <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 notranslate"><span class="pre">DeprecationWarning</span></code></a> messages by
  309. running Python with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal notranslate"><span class="pre">-Wdefault</span></code></a> (short form:
  310. <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal notranslate"><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 notranslate"><span class="pre">PYTHONWARNINGS</span></code></a>
  311. environment variable to <code class="docutils literal notranslate"><span class="pre">&quot;default&quot;</span></code> (or <code class="docutils literal notranslate"><span class="pre">&quot;d&quot;</span></code>) before running
  312. Python. Python code can also re-enable them
  313. by calling <code class="docutils literal notranslate"><span class="pre">warnings.simplefilter('default')</span></code>.</p>
  314. <p>The <code class="docutils literal notranslate"><span class="pre">unittest</span></code> module also automatically reenables deprecation warnings
  315. when running tests.</p>
  316. </section>
  317. <section id="python-3-1-features">
  318. <h2>Python 3.1 Features<a class="headerlink" href="#python-3-1-features" title="Link to this heading">¶</a></h2>
  319. <p>Much as Python 2.6 incorporated features from Python 3.0,
  320. version 2.7 incorporates some of the new features
  321. in Python 3.1. The 2.x series continues to provide tools
  322. for migrating to the 3.x series.</p>
  323. <p>A partial list of 3.1 features that were backported to 2.7:</p>
  324. <ul class="simple">
  325. <li><p>The syntax for set literals (<code class="docutils literal notranslate"><span class="pre">{1,2,3}</span></code> is a mutable set).</p></li>
  326. <li><p>Dictionary and set comprehensions (<code class="docutils literal notranslate"><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>).</p></li>
  327. <li><p>Multiple context managers in a single <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement.</p></li>
  328. <li><p>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 notranslate"><span class="pre">io</span></code></a> library, rewritten in C for performance.</p></li>
  329. <li><p>The ordered-dictionary type described in <a class="reference internal" href="#pep-0372"><span class="std std-ref">PEP 372: Adding an Ordered Dictionary to collections</span></a>.</p></li>
  330. <li><p>The new <code class="docutils literal notranslate"><span class="pre">&quot;,&quot;</span></code> format specifier described in <a class="reference internal" href="#pep-0378"><span class="std std-ref">PEP 378: Format Specifier for Thousands Separator</span></a>.</p></li>
  331. <li><p>The <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> object.</p></li>
  332. <li><p>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 notranslate"><span class="pre">importlib</span></code></a> module,
  333. <a class="reference external" href="#importlib-section">described below</a>.</p></li>
  334. <li><p>The <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a float <code class="docutils literal notranslate"><span class="pre">x</span></code> is shorter in many cases: it’s now
  335. based on the shortest decimal string that’s guaranteed to round back
  336. to <code class="docutils literal notranslate"><span class="pre">x</span></code>. As in previous versions of Python, it’s guaranteed that
  337. <code class="docutils literal notranslate"><span class="pre">float(repr(x))</span></code> recovers <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p></li>
  338. <li><p>Float-to-string and string-to-float conversions are correctly rounded.
  339. The <a class="reference internal" href="../library/functions.html#round" title="round"><code class="xref py py-func docutils literal notranslate"><span class="pre">round()</span></code></a> function is also now correctly rounded.</p></li>
  340. <li><p>The <a class="reference internal" href="../c-api/capsule.html#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> type, used to provide a C API for extension modules.</p></li>
  341. <li><p>The <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongAndOverflow" title="PyLong_AsLongAndOverflow"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> C API function.</p></li>
  342. </ul>
  343. <p>Other new Python3-mode warnings include:</p>
  344. <ul class="simple">
  345. <li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">operator.isCallable()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">operator.sequenceIncludes()</span></code>,
  346. which are not supported in 3.x, now trigger warnings.</p></li>
  347. <li><p>The <code class="xref std std-option docutils literal notranslate"><span class="pre">-3</span></code> switch now automatically
  348. enables the <code class="xref std std-option docutils literal notranslate"><span class="pre">-Qwarn</span></code> switch that causes warnings
  349. about using classic division with integers and long integers.</p></li>
  350. </ul>
  351. </section>
  352. <section id="pep-372-adding-an-ordered-dictionary-to-collections">
  353. <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="Link to this heading">¶</a></h2>
  354. <p>Regular Python dictionaries iterate over key/value pairs in arbitrary order.
  355. Over the years, a number of authors have written alternative implementations
  356. that remember the order that the keys were originally inserted. Based on
  357. the experiences from those implementations, 2.7 introduces a new
  358. <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">collections</span></code></a> module.</p>
  359. <p>The <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> API provides the same interface as regular
  360. dictionaries but iterates over keys and values in a guaranteed order
  361. depending on when a key was first inserted:</p>
  362. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
  363. <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>
  364. <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>
  365. <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>
  366. <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>
  367. <span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 2), (&#39;third&#39;, 3)]</span>
  368. </pre></div>
  369. </div>
  370. <p>If a new entry overwrites an existing entry, the original insertion
  371. position is left unchanged:</p>
  372. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  373. <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>
  374. <span class="go">[(&#39;first&#39;, 1), (&#39;second&#39;, 4), (&#39;third&#39;, 3)]</span>
  375. </pre></div>
  376. </div>
  377. <p>Deleting an entry and reinserting it will move it to the end:</p>
  378. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  379. <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>
  380. <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>
  381. <span class="go">[(&#39;first&#39;, 1), (&#39;third&#39;, 3), (&#39;second&#39;, 5)]</span>
  382. </pre></div>
  383. </div>
  384. <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 notranslate"><span class="pre">popitem()</span></code></a> method has an optional <em>last</em>
  385. argument that defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code>. If <em>last</em> is true, the most recently
  386. added key is returned and removed; if it’s false, the
  387. oldest key is selected:</p>
  388. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  389. <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>
  390. <span class="go">(19, 0)</span>
  391. <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>
  392. <span class="go">(18, 0)</span>
  393. <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>
  394. <span class="go">(0, 0)</span>
  395. <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>
  396. <span class="go">(1, 0)</span>
  397. </pre></div>
  398. </div>
  399. <p>Comparing two ordered dictionaries checks both the keys and values,
  400. and requires that the insertion order was the same:</p>
  401. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  402. <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>
  403. <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>
  404. <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>
  405. <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>
  406. <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>
  407. <span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
  408. <span class="go">False</span>
  409. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Move &#39;third&#39; key to the end</span>
  410. <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>
  411. <span class="gp">&gt;&gt;&gt; </span><span class="n">od1</span> <span class="o">==</span> <span class="n">od2</span>
  412. <span class="go">True</span>
  413. </pre></div>
  414. </div>
  415. <p>Comparing an <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> with a regular dictionary
  416. ignores the insertion order and just compares the keys and values.</p>
  417. <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 notranslate"><span class="pre">OrderedDict</span></code></a> work? It maintains a
  418. doubly linked list of keys, appending new keys to the list as they’re inserted.
  419. A secondary dictionary maps keys to their corresponding list node, so
  420. deletion doesn’t have to traverse the entire linked list and therefore
  421. remains <em>O</em>(1).</p>
  422. <p>The standard library now supports use of ordered dictionaries in several
  423. modules.</p>
  424. <ul class="simple">
  425. <li><p>The <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code></a> module uses them by default, meaning that
  426. configuration files can now be read, modified, and then written back
  427. in their original order.</p></li>
  428. <li><p>The <a class="reference internal" href="../library/collections.html#collections.somenamedtuple._asdict" title="collections.somenamedtuple._asdict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">_asdict()</span></code></a> method for
  429. <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-func docutils literal notranslate"><span class="pre">collections.namedtuple()</span></code></a> now returns an ordered dictionary with the
  430. values appearing in the same order as the underlying tuple indices.</p></li>
  431. <li><p>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 notranslate"><span class="pre">json</span></code></a> module’s <a class="reference internal" href="../library/json.html#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONDecoder</span></code></a> class
  432. constructor was extended with an <em>object_pairs_hook</em> parameter to
  433. allow <code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code> instances to be built by the decoder.
  434. Support was also added for third-party tools like
  435. <a class="reference external" href="https://pyyaml.org/">PyYAML</a>.</p></li>
  436. </ul>
  437. <div class="admonition seealso">
  438. <p class="admonition-title">See also</p>
  439. <dl class="simple">
  440. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0372/"><strong>PEP 372</strong></a> - Adding an ordered dictionary to collections</dt><dd><p>PEP written by Armin Ronacher and Raymond Hettinger;
  441. implemented by Raymond Hettinger.</p>
  442. </dd>
  443. </dl>
  444. </div>
  445. </section>
  446. <section id="pep-378-format-specifier-for-thousands-separator">
  447. <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="Link to this heading">¶</a></h2>
  448. <p>To make program output more readable, it can be useful to add
  449. separators to large numbers, rendering them as
  450. 18,446,744,073,709,551,616 instead of 18446744073709551616.</p>
  451. <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 notranslate"><span class="pre">locale</span></code></a> module,
  452. which can use different separators (“,” in North America, “.” in
  453. 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 notranslate"><span class="pre">locale</span></code></a> is complicated
  454. to use and unsuitable for multi-threaded applications where different
  455. threads are producing output for different locales.</p>
  456. <p>Therefore, a simple comma-grouping mechanism has been added to the
  457. 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 notranslate"><span class="pre">str.format()</span></code></a> method. When
  458. formatting a floating-point number, simply include a comma between the
  459. width and the precision:</p>
  460. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:20,.2f}</span><span class="s1">&#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>
  461. <span class="go">&#39;18,446,744,073,709,551,616.00&#39;</span>
  462. </pre></div>
  463. </div>
  464. <p>When formatting an integer, include the comma after the width:</p>
  465. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:20,d}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">18446744073709551616</span><span class="p">)</span>
  466. <span class="go">&#39;18,446,744,073,709,551,616&#39;</span>
  467. </pre></div>
  468. </div>
  469. <p>This mechanism is not adaptable at all; commas are always used as the
  470. separator and the grouping is always into three-digit groups. The
  471. comma-formatting mechanism isn’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 notranslate"><span class="pre">locale</span></code></a>
  472. module, but it’s easier to use.</p>
  473. <div class="admonition seealso">
  474. <p class="admonition-title">See also</p>
  475. <dl class="simple">
  476. <dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://peps.python.org/pep-0378/"><strong>PEP 378</strong></a> - Format Specifier for Thousands Separator</dt><dd><p>PEP written by Raymond Hettinger; implemented by Eric Smith.</p>
  477. </dd>
  478. </dl>
  479. </div>
  480. </section>
  481. <section id="pep-389-the-argparse-module-for-parsing-command-lines">
  482. <h2>PEP 389: The argparse Module for Parsing Command Lines<a class="headerlink" href="#pep-389-the-argparse-module-for-parsing-command-lines" title="Link to this heading">¶</a></h2>
  483. <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 notranslate"><span class="pre">argparse</span></code></a> module for parsing command-line arguments was
  484. added as a more powerful replacement for the
  485. <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 notranslate"><span class="pre">optparse</span></code></a> module.</p>
  486. <p>This means Python now supports three different modules for parsing
  487. 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 notranslate"><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 notranslate"><span class="pre">optparse</span></code></a>, and
  488. <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 notranslate"><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 notranslate"><span class="pre">getopt</span></code></a> module closely resembles the C
  489. library’s <code class="xref c c-func docutils literal notranslate"><span class="pre">getopt()</span></code> function, so it remains useful if you’re writing a
  490. Python prototype that will eventually be rewritten in C.
  491. <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 notranslate"><span class="pre">optparse</span></code></a> becomes redundant, but there are no plans to remove it
  492. because there are many scripts still using it, and there’s no
  493. 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 notranslate"><span class="pre">argparse</span></code></a>
  494. 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 notranslate"><span class="pre">optparse</span></code></a>’s interface was discussed but
  495. rejected as too messy and difficult.)</p>
  496. <p>In short, if you’re writing a new script and don’t need to worry
  497. about compatibility with earlier versions of Python, use
  498. <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 notranslate"><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 notranslate"><span class="pre">optparse</span></code></a>.</p>
  499. <p>Here’s an example:</p>
  500. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
  501. <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>
  502. <span class="c1"># Add optional switches</span>
  503. <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>
  504. <span class="n">help</span><span class="o">=</span><span class="s1">&#39;produce verbose output&#39;</span><span class="p">)</span>
  505. <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>
  506. <span class="n">metavar</span><span class="o">=</span><span class="s1">&#39;FILE&#39;</span><span class="p">,</span>
  507. <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>
  508. <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>
  509. <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>
  510. <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>
  511. <span class="c1"># Allow any number of additional arguments.</span>
  512. <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>
  513. <span class="n">help</span><span class="o">=</span><span class="s1">&#39;input filenames (default is stdin)&#39;</span><span class="p">)</span>
  514. <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>
  515. <span class="nb">print</span> <span class="n">args</span><span class="o">.</span><span class="vm">__dict__</span>
  516. </pre></div>
  517. </div>
  518. <p>Unless you override it, <code class="xref std std-option docutils literal notranslate"><span class="pre">-h</span></code> and <code class="xref std std-option docutils literal notranslate"><span class="pre">--help</span></code> switches
  519. are automatically added, and produce neatly formatted output:</p>
  520. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  521. <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>
  522. <span class="n">Command</span><span class="o">-</span><span class="n">line</span> <span class="n">example</span><span class="o">.</span>
  523. <span class="n">positional</span> <span class="n">arguments</span><span class="p">:</span>
  524. <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>
  525. <span class="n">optional</span> <span class="n">arguments</span><span class="p">:</span>
  526. <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>
  527. <span class="o">-</span><span class="n">v</span> <span class="n">produce</span> <span class="n">verbose</span> <span class="n">output</span>
  528. <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>
  529. <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>
  530. </pre></div>
  531. </div>
  532. <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 notranslate"><span class="pre">optparse</span></code></a>, the command-line switches and arguments
  533. are returned as an object with attributes named by the <em>dest</em> parameters:</p>
  534. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  535. <span class="p">{</span><span class="s1">&#39;output&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
  536. <span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
  537. <span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
  538. <span class="s1">&#39;inputs&#39;</span><span class="p">:</span> <span class="p">[]}</span>
  539. <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>
  540. <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>
  541. <span class="s1">&#39;is_verbose&#39;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
  542. <span class="s1">&#39;context&#39;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
  543. <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>
  544. </pre></div>
  545. </div>
  546. <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 notranslate"><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 notranslate"><span class="pre">optparse</span></code></a>; you
  547. can specify an exact number of arguments as an integer, 0 or more
  548. arguments by passing <code class="docutils literal notranslate"><span class="pre">'*'</span></code>, 1 or more by passing <code class="docutils literal notranslate"><span class="pre">'+'</span></code>, or an
  549. optional argument with <code class="docutils literal notranslate"><span class="pre">'?'</span></code>. A top-level parser can contain
  550. sub-parsers to define subcommands that have different sets of
  551. switches, as in <code class="docutils literal notranslate"><span class="pre">svn</span> <span class="pre">commit</span></code>, <code class="docutils literal notranslate"><span class="pre">svn</span> <span class="pre">checkout</span></code>, etc. You can
  552. specify an argument’s type as <a class="reference internal" href="../library/argparse.html#argparse.FileType" title="argparse.FileType"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileType</span></code></a>, which will
  553. automatically open files for you and understands that <code class="docutils literal notranslate"><span class="pre">'-'</span></code> means
  554. standard input or output.</p>
  555. <div class="admonition seealso">
  556. <p class="admonition-title">See also</p>
  557. <dl class="simple">
  558. <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 notranslate"><span class="pre">argparse</span></code></a> documentation</dt><dd><p>The documentation page of the argparse module.</p>
  559. </dd>
  560. <dt><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><span class="std std-ref">Upgrading optparse code</span></a></dt><dd><p>Part of the Python documentation, describing how to convert
  561. 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 notranslate"><span class="pre">optparse</span></code></a>.</p>
  562. </dd>
  563. <dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-0389/"><strong>PEP 389</strong></a> - argparse - New Command Line Parsing Module</dt><dd><p>PEP written and implemented by Steven Bethard.</p>
  564. </dd>
  565. </dl>
  566. </div>
  567. </section>
  568. <section id="pep-391-dictionary-based-configuration-for-logging">
  569. <h2>PEP 391: Dictionary-Based Configuration For Logging<a class="headerlink" href="#pep-391-dictionary-based-configuration-for-logging" title="Link to this heading">¶</a></h2>
  570. <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 notranslate"><span class="pre">logging</span></code></a> module is very flexible; applications can define
  571. a tree of logging subsystems, and each logger in this tree can filter
  572. out certain messages, format them differently, and direct messages to
  573. a varying number of handlers.</p>
  574. <p>All this flexibility can require a lot of configuration. You can
  575. write Python statements to create objects and set their properties,
  576. but a complex set-up requires verbose but boring code.
  577. <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 notranslate"><span class="pre">logging</span></code></a> also supports a <a class="reference internal" href="../library/logging.config.html#logging.config.fileConfig" title="logging.config.fileConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileConfig()</span></code></a>
  578. function that parses a file, but the file format doesn’t support
  579. configuring filters, and it’s messier to generate programmatically.</p>
  580. <p>Python 2.7 adds a <a class="reference internal" href="../library/logging.config.html#logging.config.dictConfig" title="logging.config.dictConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">dictConfig()</span></code></a> function that
  581. uses a dictionary to configure logging. There are many ways to
  582. produce a dictionary from different sources: construct one with code;
  583. parse a file containing JSON; or use a YAML parsing library if one is
  584. installed. For more information see <a class="reference internal" href="../library/logging.config.html#logging-config-api"><span class="std std-ref">Configuration functions</span></a>.</p>
  585. <p>The following example configures two loggers, the root logger and a
  586. logger named “network”. Messages sent to the root logger will be
  587. sent to the system log using the syslog protocol, and messages
  588. to the “network” logger will be written to a <code class="file docutils literal notranslate"><span class="pre">network.log</span></code> file
  589. that will be rotated once the log reaches 1MB.</p>
  590. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">logging</span>
  591. <span class="kn">import</span> <span class="nn">logging.config</span>
  592. <span class="n">configdict</span> <span class="o">=</span> <span class="p">{</span>
  593. <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>
  594. <span class="s1">&#39;formatters&#39;</span><span class="p">:</span> <span class="p">{</span>
  595. <span class="s1">&#39;standard&#39;</span><span class="p">:</span> <span class="p">{</span>
  596. <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>
  597. <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>
  598. <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>
  599. <span class="s1">&#39;class&#39;</span><span class="p">:</span> <span class="s1">&#39;logging.handlers.RotatingFileHandler&#39;</span><span class="p">,</span>
  600. <span class="s1">&#39;filename&#39;</span><span class="p">:</span> <span class="s1">&#39;/logs/network.log&#39;</span><span class="p">,</span>
  601. <span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
  602. <span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;INFO&#39;</span><span class="p">,</span>
  603. <span class="s1">&#39;maxBytes&#39;</span><span class="p">:</span> <span class="mi">1000000</span><span class="p">},</span>
  604. <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>
  605. <span class="s1">&#39;formatter&#39;</span><span class="p">:</span> <span class="s1">&#39;standard&#39;</span><span class="p">,</span>
  606. <span class="s1">&#39;level&#39;</span><span class="p">:</span> <span class="s1">&#39;ERROR&#39;</span><span class="p">}},</span>
  607. <span class="c1"># Specify all the subordinate loggers</span>
  608. <span class="s1">&#39;loggers&#39;</span><span class="p">:</span> <span class="p">{</span>
  609. <span class="s1">&#39;network&#39;</span><span class="p">:</span> <span class="p">{</span>
  610. <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>
  611. <span class="p">}</span>
  612. <span class="p">},</span>
  613. <span class="c1"># Specify properties of the root logger</span>
  614. <span class="s1">&#39;root&#39;</span><span class="p">:</span> <span class="p">{</span>
  615. <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>
  616. <span class="p">},</span>
  617. <span class="p">}</span>
  618. <span class="c1"># Set up configuration</span>
  619. <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>
  620. <span class="c1"># As an example, log two error messages</span>
  621. <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>
  622. <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>
  623. <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>
  624. <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>
  625. </pre></div>
  626. </div>
  627. <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 notranslate"><span class="pre">logging</span></code></a> module, all
  628. implemented by Vinay Sajip, are:</p>
  629. <ul class="simple">
  630. <li><p>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 notranslate"><span class="pre">SysLogHandler</span></code></a> class now supports
  631. syslogging over TCP. The constructor has a <em>socktype</em> parameter
  632. 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 notranslate"><span class="pre">socket.SOCK_DGRAM</span></code></a>
  633. 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 notranslate"><span class="pre">socket.SOCK_STREAM</span></code></a> for TCP. The default
  634. protocol remains UDP.</p></li>
  635. <li><p><a class="reference internal" href="../library/logging.html#logging.Logger" title="logging.Logger"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">getChild()</span></code></a>
  636. method that retrieves a descendant logger using a relative path.
  637. For example, once you retrieve a logger by doing <code class="docutils literal notranslate"><span class="pre">log</span> <span class="pre">=</span> <span class="pre">getLogger('app')</span></code>,
  638. calling <code class="docutils literal notranslate"><span class="pre">log.getChild('network.listen')</span></code> is equivalent to
  639. <code class="docutils literal notranslate"><span class="pre">getLogger('app.network.listen')</span></code>.</p></li>
  640. <li><p>The <a class="reference internal" href="../library/logging.html#logging.LoggerAdapter" title="logging.LoggerAdapter"><code class="xref py py-class docutils literal notranslate"><span class="pre">LoggerAdapter</span></code></a> class gained an
  641. <a class="reference internal" href="../library/logging.html#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><code class="xref py py-meth docutils literal notranslate"><span class="pre">isEnabledFor()</span></code></a> method that takes a
  642. <em>level</em> and returns whether the underlying logger would
  643. process a message of that level of importance.</p></li>
  644. </ul>
  645. <div class="admonition seealso">
  646. <p class="admonition-title">See also</p>
  647. <dl class="simple">
  648. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-0391/"><strong>PEP 391</strong></a> - Dictionary-Based Configuration For Logging</dt><dd><p>PEP written and implemented by Vinay Sajip.</p>
  649. </dd>
  650. </dl>
  651. </div>
  652. </section>
  653. <section id="pep-3106-dictionary-views">
  654. <h2>PEP 3106: Dictionary Views<a class="headerlink" href="#pep-3106-dictionary-views" title="Link to this heading">¶</a></h2>
  655. <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 notranslate"><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 notranslate"><span class="pre">values()</span></code></a>, and
  656. <a class="reference internal" href="../library/stdtypes.html#dict.items" title="dict.items"><code class="xref py py-meth docutils literal notranslate"><span class="pre">items()</span></code></a> are different in Python 3.x. They return an object
  657. called a <em class="dfn">view</em> instead of a fully materialized list.</p>
  658. <p>It’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 notranslate"><span class="pre">keys()</span></code></a>,
  659. <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">items()</span></code></a> in Python 2.7 because
  660. too much code would break. Instead the 3.x versions were added
  661. under the new names <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewkeys()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewvalues()</span></code>,
  662. and <code class="xref py py-meth docutils literal notranslate"><span class="pre">viewitems()</span></code>.</p>
  663. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  664. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
  665. <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>
  666. <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>
  667. <span class="go">dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])</span>
  668. </pre></div>
  669. </div>
  670. <p>Views can be iterated over, but the key and item views also behave
  671. like sets. The <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> operator performs intersection, and <code class="docutils literal notranslate"><span class="pre">|</span></code>
  672. performs a union:</p>
  673. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  674. <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="mf">.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>
  675. <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>
  676. <span class="go">set([0.0, 10.0, 20.0, 30.0])</span>
  677. <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>
  678. <span class="go">set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])</span>
  679. </pre></div>
  680. </div>
  681. <p>The view keeps track of the dictionary and its contents change as the
  682. dictionary is modified:</p>
  683. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  684. <span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
  685. <span class="go">dict_keys([0, 130, 10, ..., 250])</span>
  686. <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>
  687. <span class="gp">&gt;&gt;&gt; </span><span class="n">vk</span>
  688. <span class="go">dict_keys([0, 130, 260, 10, ..., 250])</span>
  689. </pre></div>
  690. </div>
  691. <p>However, note that you can’t add or remove keys while you’re iterating
  692. over the view:</p>
  693. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  694. <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>
  695. <span class="gp">...</span>
  696. <span class="gt">Traceback (most recent call last):</span>
  697. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  698. <span class="gr">RuntimeError</span>: <span class="n">dictionary changed size during iteration</span>
  699. </pre></div>
  700. </div>
  701. <p>You can use the view methods in Python 2.x code, and the 2to3
  702. 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 notranslate"><span class="pre">keys()</span></code></a>,
  703. <a class="reference internal" href="../library/stdtypes.html#dict.values" title="dict.values"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">items()</span></code></a> methods.</p>
  704. <div class="admonition seealso">
  705. <p class="admonition-title">See also</p>
  706. <dl class="simple">
  707. <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-3106/"><strong>PEP 3106</strong></a> - Revamping dict.keys(), .values() and .items()</dt><dd><p>PEP written by Guido van Rossum.
  708. Backported to 2.7 by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1967">bpo-1967</a>.</p>
  709. </dd>
  710. </dl>
  711. </div>
  712. </section>
  713. <section id="pep-3137-the-memoryview-object">
  714. <h2>PEP 3137: The memoryview Object<a class="headerlink" href="#pep-3137-the-memoryview-object" title="Link to this heading">¶</a></h2>
  715. <p>The <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> object provides a view of another object’s
  716. memory content that matches the <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type’s interface.</p>
  717. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">string</span>
  718. <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>
  719. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span>
  720. <span class="go">&lt;memory at 0x37f850&gt;</span>
  721. <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>
  722. <span class="go">52</span>
  723. <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>
  724. <span class="go">(&#39;a&#39;, &#39;z&#39;, &#39;A&#39;)</span>
  725. <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>
  726. <span class="gp">&gt;&gt;&gt; </span><span class="n">m2</span>
  727. <span class="go">&lt;memory at 0x37f080&gt;</span>
  728. </pre></div>
  729. </div>
  730. <p>The content of the view can be converted to a string of bytes or
  731. a list of integers:</p>
  732. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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>
  733. <span class="go">&#39;abcdefghijklmnopqrstuvwxyz&#39;</span>
  734. <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>
  735. <span class="go">[97, 98, 99, 100, 101, 102, 103, ... 121, 122]</span>
  736. <span class="gp">&gt;&gt;&gt;</span>
  737. </pre></div>
  738. </div>
  739. <p><a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> objects allow modifying the underlying object if
  740. it’s a mutable object.</p>
  741. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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>
  742. <span class="gt">Traceback (most recent call last):</span>
  743. File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
  744. <span class="gr">TypeError</span>: <span class="n">cannot modify read-only memory</span>
  745. <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>
  746. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
  747. <span class="go">bytearray(b&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&#39;)</span>
  748. <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>
  749. <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>
  750. <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>
  751. <span class="go">bytearray(b&#39;*bcde&#39;)</span>
  752. <span class="gp">&gt;&gt;&gt;</span>
  753. </pre></div>
  754. </div>
  755. <div class="admonition seealso">
  756. <p class="admonition-title">See also</p>
  757. <dl class="simple">
  758. <dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://peps.python.org/pep-3137/"><strong>PEP 3137</strong></a> - Immutable Bytes and Mutable Buffer</dt><dd><p>PEP written by Guido van Rossum.
  759. Implemented by Travis Oliphant, Antoine Pitrou and others.
  760. Backported to 2.7 by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2396">bpo-2396</a>.</p>
  761. </dd>
  762. </dl>
  763. </div>
  764. </section>
  765. <section id="other-language-changes">
  766. <h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Link to this heading">¶</a></h2>
  767. <p>Some smaller changes made to the core Python language are:</p>
  768. <ul>
  769. <li><p>The syntax for set literals has been backported from Python 3.x.
  770. Curly brackets are used to surround the contents of the resulting
  771. mutable set; set literals are
  772. distinguished from dictionaries by not containing colons and values.
  773. <code class="docutils literal notranslate"><span class="pre">{}</span></code> continues to represent an empty dictionary; use
  774. <code class="docutils literal notranslate"><span class="pre">set()</span></code> for an empty set.</p>
  775. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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>
  776. <span class="go">set([1, 2, 3, 4, 5])</span>
  777. <span class="gp">&gt;&gt;&gt; </span><span class="nb">set</span><span class="p">()</span> <span class="c1"># empty set</span>
  778. <span class="go">set([])</span>
  779. <span class="gp">&gt;&gt;&gt; </span><span class="p">{}</span> <span class="c1"># empty dict</span>
  780. <span class="go">{}</span>
  781. </pre></div>
  782. </div>
  783. <p>Backported by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2335">bpo-2335</a>.</p>
  784. </li>
  785. <li><p>Dictionary and set comprehensions are another feature backported from
  786. 3.x, generalizing list/generator comprehensions to use
  787. the literal syntax for sets and dictionaries.</p>
  788. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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>
  789. <span class="go">{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}</span>
  790. <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>
  791. <span class="go">set([&#39;&#39;, &#39;a&#39;, &#39;aa&#39;, &#39;aaa&#39;, &#39;aaaa&#39;, &#39;aaaaa&#39;])</span>
  792. </pre></div>
  793. </div>
  794. <p>Backported by Alexandre Vassalotti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2333">bpo-2333</a>.</p>
  795. </li>
  796. <li><p>The <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement can now use multiple context managers
  797. in one statement. Context managers are processed from left to right
  798. and each one is treated as beginning a new <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement.
  799. This means that:</p>
  800. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  801. <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
  802. </pre></div>
  803. </div>
  804. <p>is equivalent to:</p>
  805. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  806. <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>
  807. <span class="o">...</span> <span class="n">suite</span> <span class="n">of</span> <span class="n">statements</span> <span class="o">...</span>
  808. </pre></div>
  809. </div>
  810. <p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code> function provides a very similar
  811. function, so it’s no longer necessary and has been deprecated.</p>
  812. <p>(Proposed in <a class="reference external" href="https://codereview.appspot.com/53094">https://codereview.appspot.com/53094</a>; implemented by
  813. Georg Brandl.)</p>
  814. </li>
  815. <li><p>Conversions between floating-point numbers and strings are
  816. now correctly rounded on most platforms. These conversions occur
  817. in many different places: <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on
  818. floats and complex numbers; the <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">complex</span></code></a>
  819. constructors;
  820. numeric formatting; serializing and
  821. deserializing floats and complex numbers using the
  822. <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 notranslate"><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 notranslate"><span class="pre">pickle</span></code></a>
  823. 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 notranslate"><span class="pre">json</span></code></a> modules;
  824. parsing of float and imaginary literals in Python code;
  825. and <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>-to-float conversion.</p>
  826. <p>Related to this, the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of a floating-point number <em>x</em>
  827. now returns a result based on the shortest decimal string that’s
  828. guaranteed to round back to <em>x</em> under correct rounding (with
  829. round-half-to-even rounding mode). Previously it gave a string
  830. based on rounding x to 17 decimal digits.</p>
  831. <p>The rounding library responsible for this improvement works on
  832. Windows and on Unix platforms using the gcc, icc, or suncc
  833. compilers. There may be a small number of platforms where correct
  834. operation of this code cannot be guaranteed, so the code is not
  835. used on such systems. You can find out which code is being used
  836. 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 notranslate"><span class="pre">sys.float_repr_style</span></code></a>, which will be <code class="docutils literal notranslate"><span class="pre">short</span></code>
  837. if the new code is in use and <code class="docutils literal notranslate"><span class="pre">legacy</span></code> if it isn’t.</p>
  838. <p>Implemented by Eric Smith and Mark Dickinson, using David Gay’s
  839. <code class="file docutils literal notranslate"><span class="pre">dtoa.c</span></code> library; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7117">bpo-7117</a>.</p>
  840. </li>
  841. <li><p>Conversions from long integers and regular integers to floating
  842. point now round differently, returning the floating-point number
  843. closest to the number. This doesn’t matter for small integers that
  844. can be converted exactly, but for large numbers that will
  845. unavoidably lose precision, Python 2.7 now approximates more
  846. closely. For example, Python 2.6 computed the following:</p>
  847. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
  848. <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>
  849. <span class="go">2.9514790517935283e+20</span>
  850. <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>
  851. <span class="go">65535L</span>
  852. </pre></div>
  853. </div>
  854. <p>Python 2.7’s floating-point result is larger, but much closer to the
  855. true value:</p>
  856. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">295147905179352891391</span>
  857. <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>
  858. <span class="go">2.9514790517935289e+20</span>
  859. <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>
  860. <span class="go">-1L</span>
  861. </pre></div>
  862. </div>
  863. <p>(Implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3166">bpo-3166</a>.)</p>
  864. <p>Integer division is also more accurate in its rounding behaviours. (Also
  865. implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1811">bpo-1811</a>.)</p>
  866. </li>
  867. <li><p>Implicit coercion for complex numbers has been removed; the interpreter
  868. will no longer ever attempt to call a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__coerce__()</span></code> method on complex
  869. objects. (Removed by Meador Inge and Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5211">bpo-5211</a>.)</p></li>
  870. <li><p>The <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method now supports automatic numbering of the replacement
  871. 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 notranslate"><span class="pre">str.format()</span></code></a> more closely resemble using
  872. <code class="docutils literal notranslate"><span class="pre">%s</span></code> formatting:</p>
  873. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">&#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>
  874. <span class="go">&#39;2009:4:Sunday&#39;</span>
  875. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">:</span><span class="si">{}</span><span class="s1">:</span><span class="si">{day}</span><span class="s1">&#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>
  876. <span class="go">&#39;2009:4:Sunday&#39;</span>
  877. </pre></div>
  878. </div>
  879. <p>The auto-numbering takes the fields from left to right, so the first <code class="docutils literal notranslate"><span class="pre">{...}</span></code>
  880. 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 notranslate"><span class="pre">str.format()</span></code></a>, the next
  881. specifier will use the next argument, and so on. You can’t mix auto-numbering
  882. and explicit numbering – either number all of your specifier fields or none
  883. of them – but you can mix auto-numbering and named fields, as in the second
  884. example above. (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5237">bpo-5237</a>.)</p>
  885. <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 notranslate"><span class="pre">format()</span></code></a>,
  886. and default to being right-aligned.
  887. Specifying a precision or comma-separation applies to both the real
  888. and imaginary parts of the number, but a specified field width and
  889. alignment is applied to the whole of the resulting <code class="docutils literal notranslate"><span class="pre">1.5+3j</span></code>
  890. output. (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1588">bpo-1588</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7988">bpo-7988</a>.)</p>
  891. <p>The ‘F’ format code now always formats its output using uppercase characters,
  892. so it will now produce ‘INF’ and ‘NAN’.
  893. (Contributed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3382">bpo-3382</a>.)</p>
  894. <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 notranslate"><span class="pre">object.__format__()</span></code></a> method now triggers
  895. a <a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a> if it’s passed a format string,
  896. because the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code> method for <a class="reference internal" href="../library/functions.html#object" title="object"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a> converts
  897. the object to a string representation and formats that. Previously
  898. the method silently applied the format string to the string
  899. representation, but that could hide mistakes in Python code. If
  900. you’re supplying formatting information such as an alignment or
  901. precision, presumably you’re expecting the formatting to be applied
  902. in some object-specific way. (Fixed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7994">bpo-7994</a>.)</p>
  903. </li>
  904. <li><p>The <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> and <code class="xref py py-func docutils literal notranslate"><span class="pre">long()</span></code> types gained a <code class="docutils literal notranslate"><span class="pre">bit_length</span></code>
  905. method that returns the number of bits necessary to represent
  906. its argument in binary:</p>
  907. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">37</span>
  908. <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>
  909. <span class="go">&#39;0b100101&#39;</span>
  910. <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>
  911. <span class="go">6</span>
  912. <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>
  913. <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>
  914. <span class="go">123</span>
  915. <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>
  916. <span class="go">124</span>
  917. </pre></div>
  918. </div>
  919. <p>(Contributed by Fredrik Johansson and Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3439">bpo-3439</a>.)</p>
  920. </li>
  921. <li><p>The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement will no longer try an absolute import
  922. if a relative import (e.g. <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">.os</span> <span class="pre">import</span> <span class="pre">sep</span></code>) fails. This
  923. fixes a bug, but could possibly break certain <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code>
  924. statements that were only working by accident. (Fixed by Meador Inge;
  925. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7902">bpo-7902</a>.)</p></li>
  926. <li><p>It’s now possible for a subclass of the built-in <code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code> type
  927. to override the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__unicode__()</span></code> method. (Implemented by
  928. Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1583863">bpo-1583863</a>.)</p></li>
  929. <li><p>The <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> type’s <a class="reference internal" href="../library/stdtypes.html#bytearray.translate" title="bytearray.translate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">translate()</span></code></a> method now accepts
  930. <code class="docutils literal notranslate"><span class="pre">None</span></code> as its first argument. (Fixed by Georg Brandl;
  931. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4759">bpo-4759</a>.)</p>
  932. </li>
  933. <li><p>When using <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">&#64;classmethod</span></code></a> and
  934. <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-class docutils literal notranslate"><span class="pre">&#64;staticmethod</span></code></a> to wrap
  935. methods as class or static methods, the wrapper object now
  936. exposes the wrapped function as their <a class="reference internal" href="../reference/datamodel.html#method.__func__" title="method.__func__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__func__</span></code></a>
  937. attribute.
  938. (Contributed by Amaury Forgeot d’Arc, after a suggestion by
  939. George Sakkis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5982">bpo-5982</a>.)</p></li>
  940. <li><p>When a restricted set of attributes were set using <code class="docutils literal notranslate"><span class="pre">__slots__</span></code>,
  941. 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 notranslate"><span class="pre">AttributeError</span></code></a>
  942. as you would expect. Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7604">bpo-7604</a>.)</p></li>
  943. <li><p>Two new encodings are now supported: “cp720”, used primarily for
  944. Arabic text; and “cp858”, a variant of CP 850 that adds the euro
  945. symbol. (CP720 contributed by Alexander Belchenko and Amaury
  946. Forgeot d’Arc in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1616979">bpo-1616979</a>; CP858 contributed by Tim Hatch in
  947. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8016">bpo-8016</a>.)</p></li>
  948. <li><p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">file</span></code> object will now set the <code class="xref py py-attr docutils literal notranslate"><span class="pre">filename</span></code> attribute
  949. on the <a class="reference internal" href="../library/exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> exception when trying to open a directory
  950. on POSIX platforms (noted by Jan Kaliszewski; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4764">bpo-4764</a>), and
  951. now explicitly checks for and forbids writing to read-only file objects
  952. instead of trusting the C library to catch and report the error
  953. (fixed by Stefan Krah; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5677">bpo-5677</a>).</p></li>
  954. <li><p>The Python tokenizer now translates line endings itself, so the
  955. <a class="reference internal" href="../library/functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> built-in function now accepts code using any
  956. line-ending convention. Additionally, it no longer requires that the
  957. code end in a newline.</p></li>
  958. <li><p>Extra parentheses in function definitions are illegal in Python 3.x,
  959. meaning that you get a syntax error from <code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">f((x)):</span> <span class="pre">pass</span></code>. In
  960. Python3-warning mode, Python 2.7 will now warn about this odd usage.
  961. (Noted by James Lingard; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7362">bpo-7362</a>.)</p></li>
  962. <li><p>It’s now possible to create weak references to old-style class
  963. objects. New-style classes were always weak-referenceable. (Fixed
  964. by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8268">bpo-8268</a>.)</p></li>
  965. <li><p>When a module object is garbage-collected, the module’s dictionary is
  966. now only cleared if no one else is holding a reference to the
  967. dictionary (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7140">bpo-7140</a>).</p></li>
  968. </ul>
  969. <section id="interpreter-changes">
  970. <span id="new-27-interpreter"></span><h3>Interpreter Changes<a class="headerlink" href="#interpreter-changes" title="Link to this heading">¶</a></h3>
  971. <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 notranslate"><span class="pre">PYTHONWARNINGS</span></code></a>,
  972. allows controlling warnings. It should be set to a string
  973. containing warning settings, equivalent to those
  974. used with the <a class="reference internal" href="../using/cmdline.html#cmdoption-W"><code class="xref std std-option docutils literal notranslate"><span class="pre">-W</span></code></a> switch, separated by commas.
  975. (Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7301">bpo-7301</a>.)</p>
  976. <p>For example, the following setting will print warnings every time
  977. they occur, but turn warnings from the <a class="reference internal" href="../library/http.cookies.html#module-http.cookies" title="http.cookies: Support for HTTP state management (cookies)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">Cookie</span></code></a> module into an
  978. error. (The exact syntax for setting an environment variable varies
  979. across operating systems and shells.)</p>
  980. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  981. </pre></div>
  982. </div>
  983. </section>
  984. <section id="optimizations">
  985. <h3>Optimizations<a class="headerlink" href="#optimizations" title="Link to this heading">¶</a></h3>
  986. <p>Several performance enhancements have been added:</p>
  987. <ul>
  988. <li><p>A new opcode was added to perform the initial setup for
  989. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><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 notranslate"><span class="pre">__enter__()</span></code></a> and
  990. <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> methods. (Contributed by Benjamin Peterson.)</p></li>
  991. <li><p>The garbage collector now performs better for one common usage
  992. pattern: when many objects are being allocated without deallocating
  993. any of them. This would previously take quadratic
  994. time for garbage collection, but now the number of full garbage collections
  995. is reduced as the number of objects on the heap grows.
  996. The new logic only performs a full garbage collection pass when
  997. the middle generation has been collected 10 times and when the
  998. number of survivor objects from the middle generation exceeds 10% of
  999. the number of objects in the oldest generation. (Suggested by Martin
  1000. von Löwis and implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4074">bpo-4074</a>.)</p></li>
  1001. <li><p>The garbage collector tries to avoid tracking simple containers
  1002. which can’t be part of a cycle. In Python 2.7, this is now true for
  1003. tuples and dicts containing atomic types (such as ints, strings,
  1004. etc.). Transitively, a dict containing tuples of atomic types won’t
  1005. be tracked either. This helps reduce the cost of each
  1006. garbage collection by decreasing the number of objects to be
  1007. considered and traversed by the collector.
  1008. (Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4688">bpo-4688</a>.)</p></li>
  1009. <li><p>Long integers are now stored internally either in base <code class="docutils literal notranslate"><span class="pre">2**15</span></code> or in base
  1010. <code class="docutils literal notranslate"><span class="pre">2**30</span></code>, the base being determined at build time. Previously, they
  1011. were always stored in base <code class="docutils literal notranslate"><span class="pre">2**15</span></code>. Using base <code class="docutils literal notranslate"><span class="pre">2**30</span></code> gives
  1012. significant performance improvements on 64-bit machines, but
  1013. benchmark results on 32-bit machines have been mixed. Therefore,
  1014. the default is to use base <code class="docutils literal notranslate"><span class="pre">2**30</span></code> on 64-bit machines and base <code class="docutils literal notranslate"><span class="pre">2**15</span></code>
  1015. on 32-bit machines; on Unix, there’s a new configure option
  1016. <code class="xref std std-option docutils literal notranslate"><span class="pre">--enable-big-digits</span></code> that can be used to override this default.</p>
  1017. <p>Apart from the performance improvements this change should be
  1018. invisible to end users, with one exception: for testing and
  1019. debugging purposes there’s a new structseq <code class="xref py py-data docutils literal notranslate"><span class="pre">sys.long_info</span></code> that
  1020. provides information about the internal format, giving the number of
  1021. bits per digit and the size in bytes of the C type used to store
  1022. each digit:</p>
  1023. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
  1024. <span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">long_info</span>
  1025. <span class="go">sys.long_info(bits_per_digit=30, sizeof_digit=4)</span>
  1026. </pre></div>
  1027. </div>
  1028. <p>(Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4258">bpo-4258</a>.)</p>
  1029. <p>Another set of changes made long objects a few bytes smaller: 2 bytes
  1030. smaller on 32-bit systems and 6 bytes on 64-bit.
  1031. (Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5260">bpo-5260</a>.)</p>
  1032. </li>
  1033. <li><p>The division algorithm for long integers has been made faster
  1034. by tightening the inner loop, doing shifts instead of multiplications,
  1035. and fixing an unnecessary extra iteration.
  1036. Various benchmarks show speedups of between 50% and 150% for long
  1037. integer divisions and modulo operations.
  1038. (Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5512">bpo-5512</a>.)
  1039. Bitwise operations are also significantly faster (initial patch by
  1040. Gregory Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1087418">bpo-1087418</a>).</p></li>
  1041. <li><p>The implementation of <code class="docutils literal notranslate"><span class="pre">%</span></code> checks for the left-side operand being
  1042. a Python string and special-cases it; this results in a 1–3%
  1043. performance increase for applications that frequently use <code class="docutils literal notranslate"><span class="pre">%</span></code>
  1044. with strings, such as templating libraries.
  1045. (Implemented by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5176">bpo-5176</a>.)</p></li>
  1046. <li><p>List comprehensions with an <code class="docutils literal notranslate"><span class="pre">if</span></code> condition are compiled into
  1047. faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
  1048. by Jeffrey Yasskin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4715">bpo-4715</a>.)</p></li>
  1049. <li><p>Converting an integer or long integer to a decimal string was made
  1050. faster by special-casing base 10 instead of using a generalized
  1051. conversion function that supports arbitrary bases.
  1052. (Patch by Gawain Bolton; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6713">bpo-6713</a>.)</p></li>
  1053. <li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rindex()</span></code>,
  1054. <code class="xref py py-meth docutils literal notranslate"><span class="pre">rpartition()</span></code>, and <code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code> methods of string-like types
  1055. (strings, Unicode strings, and <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> objects) now use a
  1056. fast reverse-search algorithm instead of a character-by-character
  1057. scan. This is sometimes faster by a factor of 10. (Added by
  1058. Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7462">bpo-7462</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7622">bpo-7622</a>.)</p></li>
  1059. <li><p>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 notranslate"><span class="pre">pickle</span></code></a> and <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> modules now automatically
  1060. intern the strings used for attribute names, reducing memory usage
  1061. of the objects resulting from unpickling. (Contributed by Jake
  1062. McGuire; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5084">bpo-5084</a>.)</p></li>
  1063. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">cPickle</span></code> module now special-cases dictionaries,
  1064. nearly halving the time required to pickle them.
  1065. (Contributed by Collin Winter; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5670">bpo-5670</a>.)</p></li>
  1066. </ul>
  1067. </section>
  1068. </section>
  1069. <section id="new-and-improved-modules">
  1070. <h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Link to this heading">¶</a></h2>
  1071. <p>As in every release, Python’s standard library received a number of
  1072. enhancements and bug fixes. Here’s a partial list of the most notable
  1073. changes, sorted alphabetically by module name. Consult the
  1074. <code class="file docutils literal notranslate"><span class="pre">Misc/NEWS</span></code> file in the source tree for a more complete list of
  1075. changes, or look through the Subversion logs for all the details.</p>
  1076. <ul>
  1077. <li><p>The <a class="reference internal" href="../library/bdb.html#module-bdb" title="bdb: Debugger framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bdb</span></code></a> module’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 notranslate"><span class="pre">Bdb</span></code></a>
  1078. gained a feature for skipping modules. The constructor
  1079. now takes an iterable containing glob-style patterns such as
  1080. <code class="docutils literal notranslate"><span class="pre">django.*</span></code>; the debugger will not step into stack frames
  1081. from a module that matches one of these patterns.
  1082. (Contributed by Maru Newby after a suggestion by
  1083. Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5142">bpo-5142</a>.)</p></li>
  1084. <li><p>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 notranslate"><span class="pre">binascii</span></code></a> module now supports the buffer API, so it can be
  1085. used with <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-class docutils literal notranslate"><span class="pre">memoryview</span></code></a> instances and other similar buffer objects.
  1086. (Backported from 3.x by Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7703">bpo-7703</a>.)</p></li>
  1087. <li><p>Updated module: the <code class="xref py py-mod docutils literal notranslate"><span class="pre">bsddb</span></code> module has been updated from 4.7.2devel9
  1088. to version 4.8.4 of
  1089. <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">the pybsddb package</a>.
  1090. The new version features better Python 3.x compatibility, various bug fixes,
  1091. and adds several new BerkeleyDB flags and methods.
  1092. (Updated by Jesús Cea Avión; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8156">bpo-8156</a>. The pybsddb
  1093. changelog can be read at <a class="reference external" href="https://hg.jcea.es/pybsddb/file/tip/ChangeLog">https://hg.jcea.es/pybsddb/file/tip/ChangeLog</a>.)</p></li>
  1094. <li><p>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 notranslate"><span class="pre">bz2</span></code></a> module’s <a class="reference internal" href="../library/bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> now supports the context
  1095. management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">bz2.BZ2File(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
  1096. (Contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3860">bpo-3860</a>.)</p></li>
  1097. <li><p>New class: the <a class="reference internal" href="../library/collections.html#collections.Counter" title="collections.Counter"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">collections</span></code></a>
  1098. 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 notranslate"><span class="pre">Counter</span></code></a> instances
  1099. behave mostly like dictionaries but return zero for missing keys instead of
  1100. raising a <a class="reference internal" href="../library/exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a>:</p>
  1101. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
  1102. <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>
  1103. <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>
  1104. <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>
  1105. <span class="gp">...</span>
  1106. <span class="gp">&gt;&gt;&gt; </span><span class="n">c</span>
  1107. <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>
  1108. <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>
  1109. <span class="go">&#39;p&#39;: 1, &#39;r&#39;: 1, &#39;x&#39;: 1})</span>
  1110. <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>
  1111. <span class="go">5</span>
  1112. <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>
  1113. <span class="go">0</span>
  1114. </pre></div>
  1115. </div>
  1116. <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 notranslate"><span class="pre">Counter</span></code></a> methods.
  1117. <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 notranslate"><span class="pre">most_common()</span></code></a> returns the N most common
  1118. 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 notranslate"><span class="pre">elements()</span></code></a>
  1119. returns an iterator over the contained elements, repeating each
  1120. element as many times as its count.
  1121. <a class="reference internal" href="../library/collections.html#collections.Counter.subtract" title="collections.Counter.subtract"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subtract()</span></code></a> takes an iterable and
  1122. subtracts one for each element instead of adding; if the argument is
  1123. a dictionary or another <code class="xref py py-class docutils literal notranslate"><span class="pre">Counter</span></code>, the counts are
  1124. subtracted.</p>
  1125. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1126. <span class="go">[(&#39; &#39;, 6), (&#39;e&#39;, 5), (&#39;s&#39;, 3), (&#39;a&#39;, 2), (&#39;i&#39;, 2)]</span>
  1127. <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>
  1128. <span class="go"> &#39;a&#39;, &#39;a&#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;, &#39; &#39;,</span>
  1129. <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>
  1130. <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>
  1131. <span class="go"> &#39;s&#39;, &#39;s&#39;, &#39;r&#39;, &#39;t&#39;, &#39;t&#39;, &#39;x&#39;</span>
  1132. <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>
  1133. <span class="go">5</span>
  1134. <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>
  1135. <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>
  1136. <span class="go">-1</span>
  1137. </pre></div>
  1138. </div>
  1139. <p>Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1696199">bpo-1696199</a>.</p>
  1140. <p>New class: <a class="reference internal" href="../library/collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderedDict</span></code></a> is described in the earlier
  1141. section <a class="reference internal" href="#pep-0372"><span class="std std-ref">PEP 372: Adding an Ordered Dictionary to collections</span></a>.</p>
  1142. <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 notranslate"><span class="pre">deque</span></code></a> data type now has a
  1143. <a class="reference internal" href="../library/collections.html#collections.deque.count" title="collections.deque.count"><code class="xref py py-meth docutils literal notranslate"><span class="pre">count()</span></code></a> method that returns the number of
  1144. contained elements equal to the supplied argument <em>x</em>, and a
  1145. <a class="reference internal" href="../library/collections.html#collections.deque.reverse" title="collections.deque.reverse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">reverse()</span></code></a> method that reverses the elements
  1146. 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 notranslate"><span class="pre">deque</span></code></a> also exposes its maximum
  1147. 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 notranslate"><span class="pre">maxlen</span></code></a> attribute.
  1148. (Both features added by Raymond Hettinger.)</p>
  1149. <p>The <a class="reference internal" href="../library/collections.html#collections.namedtuple" title="collections.namedtuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">namedtuple</span></code></a> class now has an optional <em>rename</em> parameter.
  1150. If <em>rename</em> is true, field names that are invalid because they’ve
  1151. been repeated or aren’t legal Python identifiers will be
  1152. renamed to legal names that are derived from the field’s
  1153. position within the list of fields:</p>
  1154. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">namedtuple</span>
  1155. <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>
  1156. <span class="gp">&gt;&gt;&gt; </span><span class="n">T</span><span class="o">.</span><span class="n">_fields</span>
  1157. <span class="go">(&#39;field1&#39;, &#39;_1&#39;, &#39;_2&#39;, &#39;field2&#39;)</span>
  1158. </pre></div>
  1159. </div>
  1160. <p>(Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1818">bpo-1818</a>.)</p>
  1161. <p>Finally, the <a class="reference internal" href="../library/collections.abc.html#collections.abc.Mapping" title="collections.abc.Mapping"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code></a> abstract base class now
  1162. returns <a class="reference internal" href="../library/constants.html#NotImplemented" title="NotImplemented"><code class="xref py py-data docutils literal notranslate"><span class="pre">NotImplemented</span></code></a> if a mapping is compared to
  1163. another type that isn’t a <code class="xref py py-class docutils literal notranslate"><span class="pre">Mapping</span></code>.
  1164. (Fixed by Daniel Stutzbach; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8729">bpo-8729</a>.)</p>
  1165. </li>
  1166. <li><p>Constructors for the parsing classes in the <a class="reference internal" href="../library/configparser.html#module-configparser" title="configparser: Configuration file parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ConfigParser</span></code></a> module now
  1167. take an <em>allow_no_value</em> parameter, defaulting to false; if true,
  1168. options without values will be allowed. For example:</p>
  1169. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1170. <span class="gp">&gt;&gt;&gt; </span><span class="n">sample_config</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
  1171. <span class="gp">... </span><span class="s2">[mysqld]</span>
  1172. <span class="gp">... </span><span class="s2">user = mysql</span>
  1173. <span class="gp">... </span><span class="s2">pid-file = /var/run/mysqld/mysqld.pid</span>
  1174. <span class="gp">... </span><span class="s2">skip-bdb</span>
  1175. <span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span>
  1176. <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>
  1177. <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>
  1178. <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>
  1179. <span class="go">&#39;mysql&#39;</span>
  1180. <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>
  1181. <span class="go">None</span>
  1182. <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>
  1183. <span class="gt">Traceback (most recent call last):</span>
  1184. <span class="w"> </span><span class="c">...</span>
  1185. <span class="gr">NoOptionError</span>: <span class="n">No option &#39;unknown&#39; in section: &#39;mysqld&#39;</span>
  1186. </pre></div>
  1187. </div>
  1188. <p>(Contributed by Mats Kindahl; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7005">bpo-7005</a>.)</p>
  1189. </li>
  1190. <li><p>Deprecated function: <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code>, which allows
  1191. 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 notranslate"><span class="pre">with</span></code></a>
  1192. statement, has been deprecated, because the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code> statement
  1193. now supports multiple context managers.</p></li>
  1194. <li><p>The <a class="reference internal" href="../library/http.cookiejar.html#module-http.cookiejar" title="http.cookiejar: Classes for automatic handling of HTTP cookies."><code class="xref py py-mod docutils literal notranslate"><span class="pre">cookielib</span></code></a> module now ignores cookies that have an invalid
  1195. version field, one that doesn’t contain an integer value. (Fixed by
  1196. John J. Lee; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3924">bpo-3924</a>.)</p></li>
  1197. <li><p>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 notranslate"><span class="pre">copy</span></code></a> module’s <a class="reference internal" href="../library/copy.html#copy.deepcopy" title="copy.deepcopy"><code class="xref py py-func docutils literal notranslate"><span class="pre">deepcopy()</span></code></a> function will now
  1198. correctly copy bound instance methods. (Implemented by
  1199. Robert Collins; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1515">bpo-1515</a>.)</p></li>
  1200. <li><p>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 notranslate"><span class="pre">ctypes</span></code></a> module now always converts <code class="docutils literal notranslate"><span class="pre">None</span></code> to a C <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  1201. pointer for arguments declared as pointers. (Changed by Thomas
  1202. Heller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4606">bpo-4606</a>.) The underlying <a class="reference external" href="https://sourceware.org/libffi/">libffi library</a> has been updated to version
  1203. 3.0.9, containing various fixes for different platforms. (Updated
  1204. by Matthias Klose; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8142">bpo-8142</a>.)</p></li>
  1205. <li><p>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 notranslate"><span class="pre">datetime</span></code></a> module’s <a class="reference internal" href="../library/datetime.html#datetime.timedelta" title="datetime.timedelta"><code class="xref py py-class docutils literal notranslate"><span class="pre">timedelta</span></code></a> class
  1206. 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 notranslate"><span class="pre">total_seconds()</span></code></a> method that returns the
  1207. number of seconds in the duration. (Contributed by Brian Quinlan; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5788">bpo-5788</a>.)</p></li>
  1208. <li><p>New method: the <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> class gained a
  1209. <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 notranslate"><span class="pre">from_float()</span></code></a> class method that performs an exact
  1210. conversion of a floating-point number to a <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>.
  1211. This exact conversion strives for the
  1212. closest decimal approximation to the floating-point representation’s value;
  1213. the resulting decimal value will therefore still include the inaccuracy,
  1214. if any.
  1215. For example, <code class="docutils literal notranslate"><span class="pre">Decimal.from_float(0.1)</span></code> returns
  1216. <code class="docutils literal notranslate"><span class="pre">Decimal('0.1000000000000000055511151231257827021181583404541015625')</span></code>.
  1217. (Implemented by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4796">bpo-4796</a>.)</p>
  1218. <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 notranslate"><span class="pre">Decimal</span></code></a> with floating-point
  1219. numbers now produces sensible results based on the numeric values
  1220. of the operands. Previously such comparisons would fall back to
  1221. Python’s default rules for comparing objects, which produced arbitrary
  1222. results based on their type. Note that you still cannot combine
  1223. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code> and floating-point in other operations such as addition,
  1224. since you should be explicitly choosing how to convert between float and
  1225. <code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code>. (Fixed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2531">bpo-2531</a>.)</p>
  1226. <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 notranslate"><span class="pre">Decimal</span></code></a> now accepts
  1227. floating-point numbers (added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8257">bpo-8257</a>)
  1228. and non-European Unicode characters such as Arabic-Indic digits
  1229. (contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6595">bpo-6595</a>).</p>
  1230. <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 notranslate"><span class="pre">Context</span></code></a> class now accept integers
  1231. as well as <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> instances; the only exceptions are the
  1232. <a class="reference internal" href="../library/decimal.html#decimal.Context.canonical" title="decimal.Context.canonical"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">is_canonical()</span></code></a>
  1233. methods. (Patch by Juan José Conti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7633">bpo-7633</a>.)</p>
  1234. <p>When using <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> instances with a string’s
  1235. <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method, the default alignment was previously
  1236. left-alignment. This has been changed to right-alignment, which is
  1237. more sensible for numeric types. (Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6857">bpo-6857</a>.)</p>
  1238. <p>Comparisons involving a signaling NaN value (or <code class="docutils literal notranslate"><span class="pre">sNAN</span></code>) now signal
  1239. <a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal notranslate"><span class="pre">InvalidOperation</span></code></a> instead of silently returning a true or
  1240. false value depending on the comparison operator. Quiet NaN values
  1241. (or <code class="docutils literal notranslate"><span class="pre">NaN</span></code>) are now hashable. (Fixed by Mark Dickinson;
  1242. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7279">bpo-7279</a>.)</p>
  1243. </li>
  1244. <li><p>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 notranslate"><span class="pre">difflib</span></code></a> module now produces output that is more
  1245. compatible with modern <strong class="command">diff</strong>/<strong class="command">patch</strong> tools
  1246. through one small change, using a tab character instead of spaces as
  1247. a separator in the header giving the filename. (Fixed by Anatoly
  1248. Techtonik; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7585">bpo-7585</a>.)</p></li>
  1249. <li><p>The Distutils <code class="docutils literal notranslate"><span class="pre">sdist</span></code> command now always regenerates the
  1250. <code class="file docutils literal notranslate"><span class="pre">MANIFEST</span></code> file, since even if the <code class="file docutils literal notranslate"><span class="pre">MANIFEST.in</span></code> or
  1251. <code class="file docutils literal notranslate"><span class="pre">setup.py</span></code> files haven’t been modified, the user might have
  1252. created some new files that should be included.
  1253. (Fixed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8688">bpo-8688</a>.)</p></li>
  1254. <li><p>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 notranslate"><span class="pre">doctest</span></code></a> module’s <a class="reference internal" href="../library/doctest.html#doctest.IGNORE_EXCEPTION_DETAIL" title="doctest.IGNORE_EXCEPTION_DETAIL"><code class="xref py py-const docutils literal notranslate"><span class="pre">IGNORE_EXCEPTION_DETAIL</span></code></a> flag
  1255. will now ignore the name of the module containing the exception
  1256. being tested. (Patch by Lennart Regebro; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7490">bpo-7490</a>.)</p></li>
  1257. <li><p>The <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> module’s <a class="reference internal" href="../library/email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> class will
  1258. now accept a Unicode-valued payload, automatically converting the
  1259. payload to the encoding specified by <code class="xref py py-attr docutils literal notranslate"><span class="pre">output_charset</span></code>.
  1260. (Added by R. David Murray; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1368247">bpo-1368247</a>.)</p></li>
  1261. <li><p>The <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">Fraction</span></code></a> class now accepts a single float or
  1262. <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> instance, or two rational numbers, as
  1263. arguments to its constructor. (Implemented by Mark Dickinson;
  1264. rationals added in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5812">bpo-5812</a>, and float/decimal in
  1265. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8294">bpo-8294</a>.)</p>
  1266. <p>Ordering comparisons (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>) between
  1267. 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 notranslate"><span class="pre">TypeError</span></code></a>.
  1268. 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 notranslate"><span class="pre">Fraction</span></code></a>
  1269. match the other numeric types.</p>
  1270. </li>
  1271. <li><p>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 notranslate"><span class="pre">FTP_TLS</span></code></a> in
  1272. 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 notranslate"><span class="pre">ftplib</span></code></a> module provides secure FTP
  1273. connections using TLS encapsulation of authentication as well as
  1274. subsequent control and data transfers.
  1275. (Contributed by Giampaolo Rodola; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2054">bpo-2054</a>.)</p>
  1276. <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 notranslate"><span class="pre">storbinary()</span></code></a> method for binary uploads can now restart
  1277. uploads thanks to an added <em>rest</em> parameter (patch by Pablo Mouzo;
  1278. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6845">bpo-6845</a>.)</p>
  1279. </li>
  1280. <li><p>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 notranslate"><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 notranslate"><span class="pre">functools</span></code></a>
  1281. 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 notranslate"><span class="pre">__eq__()</span></code></a> method and one of
  1282. <a class="reference internal" href="../reference/datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><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 notranslate"><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 notranslate"><span class="pre">__ge__()</span></code></a>,
  1283. and generates the missing comparison methods. Since the
  1284. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code> method is being deprecated in Python 3.x,
  1285. this decorator makes it easier to define ordered classes.
  1286. (Added by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5479">bpo-5479</a>.)</p>
  1287. <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 notranslate"><span class="pre">cmp_to_key()</span></code></a> will take an old-style comparison
  1288. function that expects two arguments and return a new callable that
  1289. can be used as the <em>key</em> parameter to functions such as
  1290. <a class="reference internal" href="../library/functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">max()</span></code></a>, etc. The primary
  1291. intended use is to help with making code compatible with Python 3.x.
  1292. (Added by Raymond Hettinger.)</p>
  1293. </li>
  1294. <li><p>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 notranslate"><span class="pre">gc</span></code></a> module’s <a class="reference internal" href="../library/gc.html#gc.is_tracked" title="gc.is_tracked"><code class="xref py py-func docutils literal notranslate"><span class="pre">is_tracked()</span></code></a> returns
  1295. true if a given instance is tracked by the garbage collector, false
  1296. otherwise. (Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4688">bpo-4688</a>.)</p></li>
  1297. <li><p>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 notranslate"><span class="pre">gzip</span></code></a> module’s <a class="reference internal" href="../library/gzip.html#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">GzipFile</span></code></a> now supports the context
  1298. management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">gzip.GzipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>
  1299. (contributed by Hagen Fürstenau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3860">bpo-3860</a>), and it now implements
  1300. the <a class="reference internal" href="../library/io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> ABC, so you can wrap it with
  1301. <a class="reference internal" href="../library/io.html#io.BufferedReader" title="io.BufferedReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedReader</span></code></a> for faster processing
  1302. (contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7471">bpo-7471</a>).
  1303. It’s also now possible to override the modification time
  1304. recorded in a gzipped file by providing an optional timestamp to
  1305. the constructor. (Contributed by Jacques Frechet; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4272">bpo-4272</a>.)</p>
  1306. <p>Files in gzip format can be padded with trailing zero bytes; the
  1307. <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 notranslate"><span class="pre">gzip</span></code></a> module will now consume these trailing bytes. (Fixed by
  1308. Tadek Pietraszek and Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2846">bpo-2846</a>.)</p>
  1309. </li>
  1310. <li><p>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 notranslate"><span class="pre">hashlib</span></code></a> module now has an <code class="xref py py-attr docutils literal notranslate"><span class="pre">algorithms</span></code>
  1311. attribute containing a tuple naming the supported algorithms.
  1312. In Python 2.7, <code class="docutils literal notranslate"><span class="pre">hashlib.algorithms</span></code> contains
  1313. <code class="docutils literal notranslate"><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>.
  1314. (Contributed by Carl Chenet; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7418">bpo-7418</a>.)</p></li>
  1315. <li><p>The default <a class="reference internal" href="../library/http.client.html#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> class used by the <a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code></a> module now
  1316. supports buffering, resulting in much faster reading of HTTP responses.
  1317. (Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4879">bpo-4879</a>.)</p>
  1318. <p>The <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> and <a class="reference internal" href="../library/http.client.html#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a> classes
  1319. now support a <em>source_address</em> parameter, a <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
  1320. giving the source address that will be used for the connection.
  1321. (Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3972">bpo-3972</a>.)</p>
  1322. </li>
  1323. <li><p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> module now supports relative imports. Note that
  1324. <code class="xref py py-mod docutils literal notranslate"><span class="pre">ihooks</span></code> is an older module for customizing imports,
  1325. superseded by the <code class="xref py py-mod docutils literal notranslate"><span class="pre">imputil</span></code> module added in Python 2.0.
  1326. (Relative import support added by Neil Schemenauer.)</p>
  1327. </li>
  1328. <li><p>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 notranslate"><span class="pre">imaplib</span></code></a> module now supports IPv6 addresses.
  1329. (Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1655">bpo-1655</a>.)</p></li>
  1330. <li><p>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 notranslate"><span class="pre">inspect</span></code></a> module’s <a class="reference internal" href="../library/inspect.html#inspect.getcallargs" title="inspect.getcallargs"><code class="xref py py-func docutils literal notranslate"><span class="pre">getcallargs()</span></code></a>
  1331. takes a callable and its positional and keyword arguments,
  1332. and figures out which of the callable’s parameters will receive each argument,
  1333. returning a dictionary mapping argument names to their values. For example:</p>
  1334. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">getcallargs</span>
  1335. <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>
  1336. <span class="gp">... </span> <span class="k">pass</span>
  1337. <span class="gp">...</span>
  1338. <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>
  1339. <span class="go">{&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;pos&#39;: (3,), &#39;named&#39;: {}}</span>
  1340. <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>
  1341. <span class="go">{&#39;a&#39;: 2, &#39;b&#39;: 1, &#39;pos&#39;: (), &#39;named&#39;: {&#39;x&#39;: 4}}</span>
  1342. <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>
  1343. <span class="gt">Traceback (most recent call last):</span>
  1344. <span class="c">...</span>
  1345. <span class="gr">TypeError</span>: <span class="n">f() takes at least 1 argument (0 given)</span>
  1346. </pre></div>
  1347. </div>
  1348. <p>Contributed by George Sakkis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3135">bpo-3135</a>.</p>
  1349. </li>
  1350. <li><p>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 notranslate"><span class="pre">io</span></code></a> library has been upgraded to the version shipped with
  1351. Python 3.1. For 3.1, the I/O library was entirely rewritten in C
  1352. and is 2 to 20 times faster depending on the task being performed. The
  1353. original Python version was renamed to the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_pyio</span></code> module.</p>
  1354. <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 notranslate"><span class="pre">io.TextIOBase</span></code></a> class now
  1355. has an <a class="reference internal" href="../library/io.html#io.TextIOBase.errors" title="io.TextIOBase.errors"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errors</span></code></a> attribute giving the error setting
  1356. used for encoding and decoding errors (one of <code class="docutils literal notranslate"><span class="pre">'strict'</span></code>, <code class="docutils literal notranslate"><span class="pre">'replace'</span></code>,
  1357. <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code>).</p>
  1358. <p>The <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">OSError</span></code></a> when passed
  1359. an invalid file descriptor. (Implemented by Benjamin Peterson;
  1360. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4991">bpo-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 notranslate"><span class="pre">truncate()</span></code></a> method now preserves the
  1361. file position; previously it would change the file position to the
  1362. end of the new file. (Fixed by Pascal Chambon; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6939">bpo-6939</a>.)</p>
  1363. </li>
  1364. <li><p>New function: <code class="docutils literal notranslate"><span class="pre">itertools.compress(data,</span> <span class="pre">selectors)</span></code> takes two
  1365. iterators. Elements of <em>data</em> are returned if the corresponding
  1366. value in <em>selectors</em> is true:</p>
  1367. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1368. <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>
  1369. </pre></div>
  1370. </div>
  1371. <p>New function: <code class="docutils literal notranslate"><span class="pre">itertools.combinations_with_replacement(iter,</span> <span class="pre">r)</span></code>
  1372. returns all the possible <em>r</em>-length combinations of elements from the
  1373. 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 notranslate"><span class="pre">combinations()</span></code></a>, individual elements
  1374. can be repeated in the generated combinations:</p>
  1375. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1376. <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>
  1377. <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>
  1378. </pre></div>
  1379. </div>
  1380. <p>Note that elements are treated as unique depending on their position
  1381. in the input, not their actual values.</p>
  1382. <p>The <a class="reference internal" href="../library/itertools.html#itertools.count" title="itertools.count"><code class="xref py py-func docutils literal notranslate"><span class="pre">itertools.count()</span></code></a> function now has a <em>step</em> argument that
  1383. 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 notranslate"><span class="pre">count()</span></code></a> also
  1384. now allows keyword arguments, and using non-integer values such as
  1385. floats or <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> instances. (Implemented by Raymond
  1386. Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5032">bpo-5032</a>.)</p>
  1387. <p><a class="reference internal" href="../library/itertools.html#itertools.combinations" title="itertools.combinations"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">itertools.product()</span></code></a>
  1388. previously raised <a class="reference internal" href="../library/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> for values of <em>r</em> larger than
  1389. the input iterable. This was deemed a specification error, so they
  1390. now return an empty iterator. (Fixed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4816">bpo-4816</a>.)</p>
  1391. </li>
  1392. <li><p>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 notranslate"><span class="pre">json</span></code></a> module was upgraded to version 2.0.9 of the
  1393. simplejson package, which includes a C extension that makes
  1394. encoding and decoding faster.
  1395. (Contributed by Bob Ippolito; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4136">bpo-4136</a>.)</p>
  1396. <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 notranslate"><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 notranslate"><span class="pre">json.load()</span></code></a>
  1397. now has an optional <em>object_pairs_hook</em> parameter that will be called
  1398. with any object literal that decodes to a list of pairs.
  1399. (Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5381">bpo-5381</a>.)</p>
  1400. </li>
  1401. <li><p>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 notranslate"><span class="pre">mailbox</span></code></a> module’s <a class="reference internal" href="../library/mailbox.html#mailbox.Maildir" title="mailbox.Maildir"><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code></a> class now records the
  1402. timestamp on the directories it reads, and only re-reads them if the
  1403. modification time has subsequently changed. This improves
  1404. performance by avoiding unneeded directory scans. (Fixed by
  1405. A.M. Kuchling and Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1607951">bpo-1607951</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6896">bpo-6896</a>.)</p></li>
  1406. <li><p>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 notranslate"><span class="pre">math</span></code></a> module gained
  1407. <a class="reference internal" href="../library/math.html#math.erf" title="math.erf"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">erfc()</span></code></a> for the error function and the complementary error function,
  1408. <a class="reference internal" href="../library/math.html#math.expm1" title="math.expm1"><code class="xref py py-func docutils literal notranslate"><span class="pre">expm1()</span></code></a> which computes <code class="docutils literal notranslate"><span class="pre">e**x</span> <span class="pre">-</span> <span class="pre">1</span></code> with more precision than
  1409. using <a class="reference internal" href="../library/math.html#math.exp" title="math.exp"><code class="xref py py-func docutils literal notranslate"><span class="pre">exp()</span></code></a> and subtracting 1,
  1410. <a class="reference internal" href="../library/math.html#math.gamma" title="math.gamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">gamma()</span></code></a> for the Gamma function, and
  1411. <a class="reference internal" href="../library/math.html#math.lgamma" title="math.lgamma"><code class="xref py py-func docutils literal notranslate"><span class="pre">lgamma()</span></code></a> for the natural log of the Gamma function.
  1412. (Contributed by Mark Dickinson and nirinA raseliarison; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3366">bpo-3366</a>.)</p></li>
  1413. <li><p>The <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">multiprocessing</span></code></a> module’s <code class="xref py py-class docutils literal notranslate"><span class="pre">Manager*</span></code> classes
  1414. can now be passed a callable that will be called whenever
  1415. a subprocess is started, along with a set of arguments that will be
  1416. passed to the callable.
  1417. (Contributed by lekma; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5585">bpo-5585</a>.)</p>
  1418. <p>The <a class="reference internal" href="../library/multiprocessing.html#multiprocessing.pool.Pool" title="multiprocessing.pool.Pool"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code></a> class, which controls a pool of worker processes,
  1419. now has an optional <em>maxtasksperchild</em> parameter. Worker processes
  1420. will perform the specified number of tasks and then exit, causing the
  1421. <code class="xref py py-class docutils literal notranslate"><span class="pre">Pool</span></code> to start a new worker. This is useful if tasks may leak
  1422. memory or other resources, or if some tasks will cause the worker to
  1423. become very large.
  1424. (Contributed by Charles Cazabon; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6963">bpo-6963</a>.)</p>
  1425. </li>
  1426. <li><p>The <a class="reference internal" href="../library/nntplib.html#module-nntplib" title="nntplib: NNTP protocol client (requires sockets). (deprecated)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">nntplib</span></code></a> module now supports IPv6 addresses.
  1427. (Contributed by Derek Morr; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1664">bpo-1664</a>.)</p></li>
  1428. <li><p>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 notranslate"><span class="pre">os</span></code></a> module wraps the following POSIX system
  1429. calls: <a class="reference internal" href="../library/os.html#os.getresgid" title="os.getresgid"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">getresuid()</span></code></a>, which return the
  1430. real, effective, and saved GIDs and UIDs;
  1431. <a class="reference internal" href="../library/os.html#os.setresgid" title="os.setresgid"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">setresuid()</span></code></a>, which set
  1432. real, effective, and saved GIDs and UIDs to new values;
  1433. <a class="reference internal" href="../library/os.html#os.initgroups" title="os.initgroups"><code class="xref py py-func docutils literal notranslate"><span class="pre">initgroups()</span></code></a>, which initialize the group access list
  1434. for the current process. (GID/UID functions
  1435. contributed by Travis H.; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6508">bpo-6508</a>. Support for initgroups added
  1436. by Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7333">bpo-7333</a>.)</p>
  1437. <p>The <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> function now re-initializes the import lock in
  1438. 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 notranslate"><span class="pre">fork()</span></code></a>
  1439. is called from a thread. (Fixed by Zsolt Cserna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7242">bpo-7242</a>.)</p>
  1440. </li>
  1441. <li><p>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 notranslate"><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 notranslate"><span class="pre">normpath()</span></code></a> and
  1442. <a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-func docutils literal notranslate"><span class="pre">abspath()</span></code></a> functions now preserve Unicode; if their input path
  1443. is a Unicode string, the return value is also a Unicode string.
  1444. (<a class="reference internal" href="../library/os.path.html#os.path.normpath" title="os.path.normpath"><code class="xref py py-meth docutils literal notranslate"><span class="pre">normpath()</span></code></a> fixed by Matt Giuca in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5827">bpo-5827</a>;
  1445. <a class="reference internal" href="../library/os.path.html#os.path.abspath" title="os.path.abspath"><code class="xref py py-meth docutils literal notranslate"><span class="pre">abspath()</span></code></a> fixed by Ezio Melotti in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3426">bpo-3426</a>.)</p></li>
  1446. <li><p>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 notranslate"><span class="pre">pydoc</span></code></a> module now has help for the various symbols that Python
  1447. uses. You can now do <code class="docutils literal notranslate"><span class="pre">help('&lt;&lt;')</span></code> or <code class="docutils literal notranslate"><span class="pre">help('&#64;')</span></code>, for example.
  1448. (Contributed by David Laban; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4739">bpo-4739</a>.)</p></li>
  1449. <li><p>The <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code></a> module’s <a class="reference internal" href="../library/re.html#re.split" title="re.split"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><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 notranslate"><span class="pre">subn()</span></code></a>
  1450. now accept an optional <em>flags</em> argument, for consistency with the
  1451. other functions in the module. (Added by Gregory P. Smith.)</p></li>
  1452. <li><p>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 notranslate"><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 notranslate"><span class="pre">runpy</span></code></a> module
  1453. will execute the code at a provided <em>path</em> argument. <em>path</em> can be
  1454. the path of a Python source file (<code class="file docutils literal notranslate"><span class="pre">example.py</span></code>), a compiled
  1455. bytecode file (<code class="file docutils literal notranslate"><span class="pre">example.pyc</span></code>), a directory
  1456. (<code class="file docutils literal notranslate"><span class="pre">./package/</span></code>), or a zip archive (<code class="file docutils literal notranslate"><span class="pre">example.zip</span></code>). If a
  1457. directory or zip path is provided, it will be added to the front of
  1458. <code class="docutils literal notranslate"><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 top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a> will be imported. It’s
  1459. expected that the directory or zip contains a <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code>;
  1460. if it doesn’t, some other <code class="file docutils literal notranslate"><span class="pre">__main__.py</span></code> might be imported from
  1461. a location later in <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. This makes more of the machinery
  1462. 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 notranslate"><span class="pre">runpy</span></code></a> available to scripts that want to mimic the way
  1463. Python’s command line processes an explicit path name.
  1464. (Added by Nick Coghlan; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6816">bpo-6816</a>.)</p></li>
  1465. <li><p>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 notranslate"><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 notranslate"><span class="pre">make_archive()</span></code></a>
  1466. takes a filename, archive type (zip or tar-format), and a directory
  1467. path, and creates an archive containing the directory’s contents.
  1468. (Added by Tarek Ziadé.)</p>
  1469. <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 notranslate"><span class="pre">shutil</span></code></a>’s <a class="reference internal" href="../library/shutil.html#shutil.copyfile" title="shutil.copyfile"><code class="xref py py-func docutils literal notranslate"><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 notranslate"><span class="pre">copytree()</span></code></a>
  1470. functions now raise a <code class="xref py py-exc docutils literal notranslate"><span class="pre">SpecialFileError</span></code> exception when
  1471. asked to copy a named pipe. Previously the code would treat
  1472. named pipes like a regular file by opening them for reading, and
  1473. this would block indefinitely. (Fixed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3002">bpo-3002</a>.)</p>
  1474. </li>
  1475. <li><p>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 notranslate"><span class="pre">signal</span></code></a> module no longer re-installs the signal handler
  1476. unless this is truly necessary, which fixes a bug that could make it
  1477. impossible to catch the EINTR signal robustly. (Fixed by
  1478. Charles-Francois Natali; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8354">bpo-8354</a>.)</p></li>
  1479. <li><p>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 notranslate"><span class="pre">site</span></code></a> module, three new functions
  1480. return various site- and user-specific paths.
  1481. <a class="reference internal" href="../library/site.html#site.getsitepackages" title="site.getsitepackages"><code class="xref py py-func docutils literal notranslate"><span class="pre">getsitepackages()</span></code></a> returns a list containing all
  1482. global site-packages directories,
  1483. <a class="reference internal" href="../library/site.html#site.getusersitepackages" title="site.getusersitepackages"><code class="xref py py-func docutils literal notranslate"><span class="pre">getusersitepackages()</span></code></a> returns the path of the user’s
  1484. site-packages directory, and
  1485. <a class="reference internal" href="../library/site.html#site.getuserbase" title="site.getuserbase"><code class="xref py py-func docutils literal notranslate"><span class="pre">getuserbase()</span></code></a> returns the value of the <a class="reference internal" href="../library/site.html#site.USER_BASE" title="site.USER_BASE"><code class="xref py py-data docutils literal notranslate"><span class="pre">USER_BASE</span></code></a>
  1486. environment variable, giving the path to a directory that can be used
  1487. to store data.
  1488. (Contributed by Tarek Ziadé; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6693">bpo-6693</a>.)</p>
  1489. <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 notranslate"><span class="pre">site</span></code></a> module now reports exceptions occurring
  1490. when the <a class="reference internal" href="../library/site.html#module-sitecustomize" title="sitecustomize"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sitecustomize</span></code></a> module is imported, and will no longer
  1491. catch and swallow the <a class="reference internal" href="../library/exceptions.html#KeyboardInterrupt" title="KeyboardInterrupt"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyboardInterrupt</span></code></a> exception. (Fixed by
  1492. Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3137">bpo-3137</a>.)</p>
  1493. </li>
  1494. <li><p>The <a class="reference internal" href="../library/socket.html#socket.create_connection" title="socket.create_connection"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_connection()</span></code></a> function
  1495. gained a <em>source_address</em> parameter, a <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> 2-tuple
  1496. giving the source address that will be used for the connection.
  1497. (Contributed by Eldon Ziegler; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3972">bpo-3972</a>.)</p>
  1498. <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 notranslate"><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 notranslate"><span class="pre">recvfrom_into()</span></code></a>
  1499. methods will now write into objects that support the buffer API, most usefully
  1500. the <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">memoryview</span></code></a> objects. (Implemented by
  1501. Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8104">bpo-8104</a>.)</p>
  1502. </li>
  1503. <li><p>The <a class="reference internal" href="../library/socketserver.html#module-socketserver" title="socketserver: A framework for network servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SocketServer</span></code></a> module’s <a class="reference internal" href="../library/socketserver.html#socketserver.TCPServer" title="socketserver.TCPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">TCPServer</span></code></a> class now
  1504. supports socket timeouts and disabling the Nagle algorithm.
  1505. The <code class="xref py py-attr docutils literal notranslate"><span class="pre">disable_nagle_algorithm</span></code> class attribute
  1506. defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>; if overridden to be true,
  1507. new request connections will have the TCP_NODELAY option set to
  1508. prevent buffering many small sends into a single TCP packet.
  1509. The <a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer.timeout" title="socketserver.BaseServer.timeout"><code class="xref py py-attr docutils literal notranslate"><span class="pre">timeout</span></code></a> class attribute can hold
  1510. a timeout in seconds that will be applied to the request socket; if
  1511. no request is received within that time, <a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer.handle_timeout" title="socketserver.BaseServer.handle_timeout"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_timeout()</span></code></a>
  1512. will be called and <a class="reference internal" href="../library/socketserver.html#socketserver.BaseServer.handle_request" title="socketserver.BaseServer.handle_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_request()</span></code></a> will return.
  1513. (Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6192">bpo-6192</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6267">bpo-6267</a>.)</p></li>
  1514. <li><p>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 notranslate"><span class="pre">sqlite3</span></code></a> module has been updated to
  1515. version 2.6.0 of the <a class="reference external" href="https://github.com/ghaering/pysqlite">pysqlite package</a>. Version 2.6.0 includes a number of bugfixes, and adds
  1516. the ability to load SQLite extensions from shared libraries.
  1517. Call the <code class="docutils literal notranslate"><span class="pre">enable_load_extension(True)</span></code> method to enable extensions,
  1518. 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 notranslate"><span class="pre">load_extension()</span></code></a> to load a particular shared library.
  1519. (Updated by Gerhard Häring.)</p></li>
  1520. <li><p>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 notranslate"><span class="pre">ssl</span></code></a> module’s <a class="reference internal" href="../library/ssl.html#ssl.SSLSocket" title="ssl.SSLSocket"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLSocket</span></code></a> objects now support the
  1521. buffer API, which fixed a test suite failure (fix by Antoine Pitrou;
  1522. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7133">bpo-7133</a>) and automatically set
  1523. OpenSSL’s <code class="xref c c-macro docutils literal notranslate"><span class="pre">SSL_MODE_AUTO_RETRY</span></code>, which will prevent an error
  1524. code being returned from <code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code> operations that trigger an SSL
  1525. renegotiation (fix by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8222">bpo-8222</a>).</p>
  1526. <p>The <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-func docutils literal notranslate"><span class="pre">wrap_socket()</span></code></a> constructor function now takes a
  1527. <em>ciphers</em> argument that’s a string listing the encryption algorithms
  1528. to be allowed; the format of the string is described
  1529. <a class="reference external" href="https://www.openssl.org/docs/man1.0.2/man1/ciphers.html">in the OpenSSL documentation</a>.
  1530. (Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8322">bpo-8322</a>.)</p>
  1531. <p>Another change makes the extension load all of OpenSSL’s ciphers and
  1532. digest algorithms so that they’re all available. Some SSL
  1533. certificates couldn’t be verified, reporting an “unknown algorithm”
  1534. error. (Reported by Beda Kosata, and fixed by Antoine Pitrou;
  1535. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8484">bpo-8484</a>.)</p>
  1536. <p>The version of OpenSSL being used is now available as the module
  1537. attributes <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION" title="ssl.OPENSSL_VERSION"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION</span></code></a> (a string),
  1538. <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_INFO" title="ssl.OPENSSL_VERSION_INFO"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION_INFO</span></code></a> (a 5-tuple), and
  1539. <a class="reference internal" href="../library/ssl.html#ssl.OPENSSL_VERSION_NUMBER" title="ssl.OPENSSL_VERSION_NUMBER"><code class="xref py py-const docutils literal notranslate"><span class="pre">ssl.OPENSSL_VERSION_NUMBER</span></code></a> (an integer). (Added by Antoine
  1540. Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8321">bpo-8321</a>.)</p>
  1541. </li>
  1542. <li><p>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 notranslate"><span class="pre">struct</span></code></a> module will no longer silently ignore overflow
  1543. errors when a value is too large for a particular integer format
  1544. code (one of <code class="docutils literal notranslate"><span class="pre">bBhHiIlLqQ</span></code>); it now always raises a
  1545. <a class="reference internal" href="../library/struct.html#struct.error" title="struct.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">struct.error</span></code></a> exception. (Changed by Mark Dickinson;
  1546. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1523">bpo-1523</a>.) The <a class="reference internal" href="../library/struct.html#struct.pack" title="struct.pack"><code class="xref py py-func docutils literal notranslate"><span class="pre">pack()</span></code></a> function will also
  1547. attempt to use <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__index__()</span></code></a> to convert and pack non-integers
  1548. before trying the <a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__int__()</span></code></a> method or reporting an error.
  1549. (Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8300">bpo-8300</a>.)</p></li>
  1550. <li><p>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 notranslate"><span class="pre">subprocess</span></code></a> module’s
  1551. <a class="reference internal" href="../library/subprocess.html#subprocess.check_output" title="subprocess.check_output"><code class="xref py py-func docutils literal notranslate"><span class="pre">check_output()</span></code></a> runs a command with a specified set of arguments
  1552. and returns the command’s output as a string when the command runs without
  1553. 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 notranslate"><span class="pre">CalledProcessError</span></code></a> exception otherwise.</p>
  1554. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">subprocess</span><span class="o">.</span><span class="n">check_output</span><span class="p">([</span><span class="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>
  1555. <span class="go">&#39;Filesystem Size Used Avail Capacity Mounted on\n</span>
  1556. <span class="go">/dev/disk0s2 52G 49G 3.0G 94% /\n&#39;</span>
  1557. <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>
  1558. <span class="go"> ...</span>
  1559. <span class="go">subprocess.CalledProcessError: Command &#39;[&#39;df&#39;, &#39;-h&#39;, &#39;/bogus&#39;]&#39; returned non-zero exit status 1</span>
  1560. </pre></div>
  1561. </div>
  1562. <p>(Contributed by Gregory P. Smith.)</p>
  1563. <p>The <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module will now retry its internal system calls
  1564. on receiving an <a class="reference internal" href="../library/errno.html#errno.EINTR" title="errno.EINTR"><code class="xref py py-const docutils literal notranslate"><span class="pre">EINTR</span></code></a> signal. (Reported by several people; final
  1565. patch by Gregory P. Smith in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1068268">bpo-1068268</a>.)</p>
  1566. </li>
  1567. <li><p>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 notranslate"><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 notranslate"><span class="pre">symtable</span></code></a> module
  1568. returns true for variables that are explicitly declared to be global,
  1569. false for ones that are implicitly global.
  1570. (Contributed by Jeremy Hylton.)</p></li>
  1571. <li><p>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 notranslate"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> as the
  1572. identifier instead of the previous default value of <code class="docutils literal notranslate"><span class="pre">'python'</span></code>.
  1573. (Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8451">bpo-8451</a>.)</p></li>
  1574. <li><p>The <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> value is now a named tuple, with attributes
  1575. named <code class="xref py py-attr docutils literal notranslate"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">micro</span></code>,
  1576. <code class="xref py py-attr docutils literal notranslate"><span class="pre">releaselevel</span></code>, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">serial</span></code>. (Contributed by Ross
  1577. Light; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4285">bpo-4285</a>.)</p>
  1578. <p><a class="reference internal" href="../library/sys.html#sys.getwindowsversion" title="sys.getwindowsversion"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.getwindowsversion()</span></code></a> also returns a named tuple,
  1579. with attributes named <code class="xref py py-attr docutils literal notranslate"><span class="pre">major</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">build</span></code>,
  1580. <code class="xref py py-attr docutils literal notranslate"><span class="pre">platform</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack_major</span></code>,
  1581. <code class="xref py py-attr docutils literal notranslate"><span class="pre">service_pack_minor</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">suite_mask</span></code>, and
  1582. <code class="xref py py-attr docutils literal notranslate"><span class="pre">product_type</span></code>. (Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7766">bpo-7766</a>.)</p>
  1583. </li>
  1584. <li><p>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 notranslate"><span class="pre">tarfile</span></code></a> module’s default error handling has changed, to
  1585. no longer suppress fatal errors. The default error level was previously 0,
  1586. which meant that errors would only result in a message being written to the
  1587. debug log, but because the debug log is not activated by default,
  1588. these errors go unnoticed. The default error level is now 1,
  1589. which raises an exception if there’s an error.
  1590. (Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7357">bpo-7357</a>.)</p>
  1591. <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 notranslate"><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 notranslate"><span class="pre">TarInfo</span></code></a>
  1592. 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 notranslate"><span class="pre">add()</span></code></a>,
  1593. you may supply an optional <em>filter</em> argument
  1594. that’s a callable. The <em>filter</em> callable will be passed the
  1595. <a class="reference internal" href="../library/tarfile.html#tarfile.TarInfo" title="tarfile.TarInfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarInfo</span></code></a> for every file being added, and can modify and return it.
  1596. If the callable returns <code class="docutils literal notranslate"><span class="pre">None</span></code>, the file will be excluded from the
  1597. resulting archive. This is more powerful than the existing
  1598. <em>exclude</em> argument, which has therefore been deprecated.
  1599. (Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6856">bpo-6856</a>.)
  1600. The <a class="reference internal" href="../library/tarfile.html#tarfile.TarFile" title="tarfile.TarFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TarFile</span></code></a> class also now supports the context management protocol.
  1601. (Added by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7232">bpo-7232</a>.)</p>
  1602. </li>
  1603. <li><p>The <a class="reference internal" href="../library/threading.html#threading.Event.wait" title="threading.Event.wait"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">threading.Event</span></code></a> class
  1604. now returns the internal flag on exit. This means the method will usually
  1605. 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 notranslate"><span class="pre">wait()</span></code></a> is supposed to block until the
  1606. internal flag becomes true. The return value will only be false if
  1607. a timeout was provided and the operation timed out.
  1608. (Contributed by Tim Lesher; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1674032">bpo-1674032</a>.)</p></li>
  1609. <li><p>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 notranslate"><span class="pre">unicodedata</span></code></a> module is
  1610. now used internally to determine which characters are numeric,
  1611. whitespace, or represent line breaks. The database also
  1612. includes information from the <code class="file docutils literal notranslate"><span class="pre">Unihan.txt</span></code> data file (patch
  1613. by Anders Chrigström and Amaury Forgeot d’Arc; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1571184">bpo-1571184</a>)
  1614. and has been updated to version 5.2.0 (updated by
  1615. Florent Xicluna; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8024">bpo-8024</a>).</p></li>
  1616. <li><p>The <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code></a> module’s <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlsplit" title="urllib.parse.urlsplit"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlsplit()</span></code></a> now handles
  1617. unknown URL schemes in a fashion compliant with <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
  1618. URL is of the form <code class="docutils literal notranslate"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
  1619. <code class="docutils literal notranslate"><span class="pre">://</span></code> is treated as the scheme, even if it’s a made-up scheme that
  1620. the module doesn’t know about. This change may break code that
  1621. worked around the old behaviour. For example, Python 2.6.4 or 2.5
  1622. will return the following:</p>
  1623. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
  1624. <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>
  1625. <span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
  1626. </pre></div>
  1627. </div>
  1628. <p>Python 2.7 (and Python 2.6.5) will return:</p>
  1629. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
  1630. <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>
  1631. <span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
  1632. </pre></div>
  1633. </div>
  1634. <p>(Python 2.7 actually produces slightly different output, since it
  1635. returns a named tuple instead of a standard tuple.)</p>
  1636. <p>The <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code></a> module also supports IPv6 literal addresses as defined by
  1637. <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2732.html"><strong>RFC 2732</strong></a> (contributed by Senthil Kumaran; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2987">bpo-2987</a>).</p>
  1638. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><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>
  1639. <span class="go">ParseResult(scheme=&#39;http&#39;, netloc=&#39;[1080::8:800:200C:417A]&#39;,</span>
  1640. <span class="go"> path=&#39;/foo&#39;, params=&#39;&#39;, query=&#39;&#39;, fragment=&#39;&#39;)</span>
  1641. </pre></div>
  1642. </div>
  1643. </li>
  1644. <li><p>New class: the <a class="reference internal" href="../library/weakref.html#weakref.WeakSet" title="weakref.WeakSet"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">weakref</span></code></a>
  1645. module is a set that only holds weak references to its elements; elements
  1646. will be removed once there are no references pointing to them.
  1647. (Originally implemented in Python 3.x by Raymond Hettinger, and backported
  1648. to 2.7 by Michael Foord.)</p></li>
  1649. <li><p>The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> library, no longer escapes
  1650. ampersands and angle brackets when outputting an XML processing
  1651. instruction (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;?xml-stylesheet</span> <span class="pre">href=&quot;#style1&quot;?&gt;</span></code>)
  1652. or comment (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;!--</span> <span class="pre">comment</span> <span class="pre">--&gt;</span></code>).
  1653. (Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2746">bpo-2746</a>.)</p></li>
  1654. <li><p>The XML-RPC client and server, provided by the <a class="reference internal" href="../library/xmlrpc.client.html#module-xmlrpc.client" title="xmlrpc.client: XML-RPC client access."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code></a> and
  1655. <a class="reference internal" href="../library/xmlrpc.server.html#module-xmlrpc.server" title="xmlrpc.server: Basic XML-RPC server implementations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleXMLRPCServer</span></code></a> modules, have improved performance by
  1656. supporting HTTP/1.1 keep-alive and by optionally using gzip encoding
  1657. to compress the XML being exchanged. The gzip compression is
  1658. controlled by the <code class="xref py py-attr docutils literal notranslate"><span class="pre">encode_threshold</span></code> attribute of
  1659. <a class="reference internal" href="../library/xmlrpc.server.html#xmlrpc.server.SimpleXMLRPCRequestHandler" title="xmlrpc.server.SimpleXMLRPCRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleXMLRPCRequestHandler</span></code></a>, which contains a size in bytes;
  1660. responses larger than this will be compressed.
  1661. (Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6267">bpo-6267</a>.)</p></li>
  1662. <li><p>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 notranslate"><span class="pre">zipfile</span></code></a> module’s <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> now supports the context
  1663. management protocol, so you can write <code class="docutils literal notranslate"><span class="pre">with</span> <span class="pre">zipfile.ZipFile(...)</span> <span class="pre">as</span> <span class="pre">f:</span></code>.
  1664. (Contributed by Brian Curtin; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5511">bpo-5511</a>.)</p>
  1665. <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 notranslate"><span class="pre">zipfile</span></code></a> now also supports archiving empty directories and
  1666. extracts them correctly. (Fixed by Kuba Wieczorek; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4710">bpo-4710</a>.)
  1667. Reading files out of an archive is faster, and interleaving
  1668. <a class="reference internal" href="../library/io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> and
  1669. <a class="reference internal" href="../library/io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> now works correctly.
  1670. (Contributed by Nir Aides; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7610">bpo-7610</a>.)</p>
  1671. <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 notranslate"><span class="pre">is_zipfile()</span></code></a> function now
  1672. accepts a file object, in addition to the path names accepted in earlier
  1673. versions. (Contributed by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4756">bpo-4756</a>.)</p>
  1674. <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 notranslate"><span class="pre">writestr()</span></code></a> method now has an optional <em>compress_type</em> parameter
  1675. that lets you override the default compression method specified in the
  1676. <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile" title="zipfile.ZipFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">ZipFile</span></code></a> constructor. (Contributed by Ronald Oussoren;
  1677. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6003">bpo-6003</a>.)</p>
  1678. </li>
  1679. </ul>
  1680. <section id="new-module-importlib">
  1681. <span id="importlib-section"></span><h3>New module: importlib<a class="headerlink" href="#new-module-importlib" title="Link to this heading">¶</a></h3>
  1682. <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 notranslate"><span class="pre">importlib</span></code></a> package, a re-implementation
  1683. of the logic underlying Python’s <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.
  1684. <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 notranslate"><span class="pre">importlib</span></code></a> is useful for implementors of Python interpreters and
  1685. to users who wish to write new importers that can participate in the
  1686. import process. Python 2.7 doesn’t contain the complete
  1687. <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 notranslate"><span class="pre">importlib</span></code></a> package, but instead has a tiny subset that contains
  1688. 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 notranslate"><span class="pre">import_module()</span></code></a>.</p>
  1689. <p><code class="docutils literal notranslate"><span class="pre">import_module(name,</span> <span class="pre">package=None)</span></code> imports a module. <em>name</em> is
  1690. a string containing the module or package’s name. It’s possible to do
  1691. relative imports by providing a string that begins with a <code class="docutils literal notranslate"><span class="pre">.</span></code>
  1692. character, such as <code class="docutils literal notranslate"><span class="pre">..utils.errors</span></code>. For relative imports, the
  1693. <em>package</em> argument must be provided and is the name of the package that
  1694. will be used as the anchor for
  1695. 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 notranslate"><span class="pre">import_module()</span></code></a> both inserts the imported
  1696. module into <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> and returns the module object.</p>
  1697. <p>Here are some examples:</p>
  1698. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
  1699. <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>
  1700. <span class="gp">&gt;&gt;&gt; </span><span class="n">anydbm</span>
  1701. <span class="go">&lt;module &#39;anydbm&#39; from &#39;/p/python/Lib/anydbm.py&#39;&gt;</span>
  1702. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Relative import</span>
  1703. <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>
  1704. <span class="gp">&gt;&gt;&gt; </span><span class="n">file_util</span>
  1705. <span class="go">&lt;module &#39;distutils.file_util&#39; from &#39;/python/Lib/distutils/file_util.pyc&#39;&gt;</span>
  1706. </pre></div>
  1707. </div>
  1708. <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 notranslate"><span class="pre">importlib</span></code></a> was implemented by Brett Cannon and introduced in
  1709. Python 3.1.</p>
  1710. </section>
  1711. <section id="new-module-sysconfig">
  1712. <h3>New module: sysconfig<a class="headerlink" href="#new-module-sysconfig" title="Link to this heading">¶</a></h3>
  1713. <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 notranslate"><span class="pre">sysconfig</span></code></a> module has been pulled out of the Distutils
  1714. package, becoming a new top-level module in its own right.
  1715. <a class="reference internal" href="../library/sysconfig.html#module-sysconfig" title="sysconfig: Python's configuration information"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sysconfig</span></code></a> provides functions for getting information about
  1716. Python’s build process: compiler switches, installation paths, the
  1717. platform name, and whether Python is running from its source
  1718. directory.</p>
  1719. <p>Some of the functions in the module are:</p>
  1720. <ul class="simple">
  1721. <li><p><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 notranslate"><span class="pre">get_config_var()</span></code></a> returns variables from Python’s
  1722. Makefile and the <code class="file docutils literal notranslate"><span class="pre">pyconfig.h</span></code> file.</p></li>
  1723. <li><p><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 notranslate"><span class="pre">get_config_vars()</span></code></a> returns a dictionary containing
  1724. all of the configuration variables.</p></li>
  1725. <li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_path" title="sysconfig.get_path"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_path()</span></code></a> returns the configured path for
  1726. a particular type of module: the standard library,
  1727. site-specific modules, platform-specific modules, etc.</p></li>
  1728. <li><p><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 notranslate"><span class="pre">is_python_build()</span></code></a> returns true if you’re running a
  1729. binary from a Python source tree, and false otherwise.</p></li>
  1730. </ul>
  1731. <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 notranslate"><span class="pre">sysconfig</span></code></a> documentation for more details and for
  1732. a complete list of functions.</p>
  1733. <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 notranslate"><span class="pre">sysconfig</span></code></a> are now maintained by Tarek
  1734. Ziadé, who has also started a Distutils2 package (source repository at
  1735. <a class="reference external" href="https://hg.python.org/distutils2/">https://hg.python.org/distutils2/</a>) for developing a next-generation
  1736. version of Distutils.</p>
  1737. </section>
  1738. <section id="ttk-themed-widgets-for-tk">
  1739. <h3>ttk: Themed Widgets for Tk<a class="headerlink" href="#ttk-themed-widgets-for-tk" title="Link to this heading">¶</a></h3>
  1740. <p>Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
  1741. widgets but have a more customizable appearance and can therefore more
  1742. closely resemble the native platform’s widgets. This widget
  1743. set was originally called Tile, but was renamed to Ttk (for “themed Tk”)
  1744. on being added to Tcl/Tck release 8.5.</p>
  1745. <p>To learn more, read the <a class="reference internal" href="../library/tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ttk</span></code></a> module documentation. You may also
  1746. wish to read the Tcl/Tk manual page describing the
  1747. Ttk theme engine, available at
  1748. <a class="reference external" href="https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.html">https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.html</a>. Some
  1749. screenshots of the Python/Ttk code in use are at
  1750. <a class="reference external" href="https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki">https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki</a>.</p>
  1751. <p>The <a class="reference internal" href="../library/tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code></a> module was written by Guilherme Polo and added in
  1752. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2983">bpo-2983</a>. An alternate version called <code class="docutils literal notranslate"><span class="pre">Tile.py</span></code>, written by
  1753. Martin Franklin and maintained by Kevin Walzer, was proposed for
  1754. inclusion in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2618">bpo-2618</a>, but the authors argued that Guilherme
  1755. Polo’s work was more comprehensive.</p>
  1756. </section>
  1757. <section id="updated-module-unittest">
  1758. <span id="unittest-section"></span><h3>Updated module: unittest<a class="headerlink" href="#updated-module-unittest" title="Link to this heading">¶</a></h3>
  1759. <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 notranslate"><span class="pre">unittest</span></code></a> module was greatly enhanced; many
  1760. new features were added. Most of these features were implemented
  1761. by Michael Foord, unless otherwise noted. The enhanced version of
  1762. the module is downloadable separately for use with Python versions 2.4 to 2.6,
  1763. packaged as the <code class="xref py py-mod docutils literal notranslate"><span class="pre">unittest2</span></code> package, from
  1764. <a class="reference external" href="https://pypi.org/project/unittest2">https://pypi.org/project/unittest2</a>.</p>
  1765. <p>When used from the command line, the module can automatically discover
  1766. tests. It’s not as fancy as <a class="reference external" href="https://pytest.org">py.test</a> or
  1767. <a class="reference external" href="https://nose.readthedocs.io/">nose</a>, but provides a
  1768. simple way to run tests kept within a set of package directories. For example,
  1769. the following command will search the <code class="file docutils literal notranslate"><span class="pre">test/</span></code> subdirectory for
  1770. any importable test files named <code class="docutils literal notranslate"><span class="pre">test*.py</span></code>:</p>
  1771. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1772. </pre></div>
  1773. </div>
  1774. <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 notranslate"><span class="pre">unittest</span></code></a> module documentation for more details.
  1775. (Developed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6001">bpo-6001</a>.)</p>
  1776. <p>The <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> function supports some other new options:</p>
  1777. <ul>
  1778. <li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-b"><code class="xref std std-option docutils literal notranslate"><span class="pre">-b</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--buffer</span></code> will buffer the standard output
  1779. and standard error streams during each test. If the test passes,
  1780. any resulting output will be discarded; on failure, the buffered
  1781. output will be displayed.</p></li>
  1782. <li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--catch</span></code> will cause the control-C interrupt
  1783. to be handled more gracefully. Instead of interrupting the test
  1784. process immediately, the currently running test will be completed
  1785. and then the partial results up to the interruption will be reported.
  1786. If you’re impatient, a second press of control-C will cause an immediate
  1787. interruption.</p>
  1788. <p>This control-C handler tries to avoid causing problems when the code
  1789. being tested or the tests being run have defined a signal handler of
  1790. their own, by noticing that a signal handler was already set and
  1791. calling it. If this doesn’t work for you, there’s a
  1792. <a class="reference internal" href="../library/unittest.html#unittest.removeHandler" title="unittest.removeHandler"><code class="xref py py-func docutils literal notranslate"><span class="pre">removeHandler()</span></code></a> decorator that can be used to mark tests that
  1793. should have the control-C handling disabled.</p>
  1794. </li>
  1795. <li><p><a class="reference internal" href="../library/unittest.html#cmdoption-unittest-f"><code class="xref std std-option docutils literal notranslate"><span class="pre">-f</span></code></a> or <code class="xref std std-option docutils literal notranslate"><span class="pre">--failfast</span></code> makes
  1796. test execution stop immediately when a test fails instead of
  1797. continuing to execute further tests. (Suggested by Cliff Dyer and
  1798. implemented by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8074">bpo-8074</a>.)</p></li>
  1799. </ul>
  1800. <p>The progress messages now show ‘x’ for expected failures
  1801. and ‘u’ for unexpected successes when run in verbose mode.
  1802. (Contributed by Benjamin Peterson.)</p>
  1803. <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 notranslate"><span class="pre">SkipTest</span></code></a> exception to skip a
  1804. test (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1034053">bpo-1034053</a>).</p>
  1805. <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 notranslate"><span class="pre">assertEqual()</span></code></a>,
  1806. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertTrue" title="unittest.TestCase.assertTrue"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertFalse()</span></code></a>
  1807. failures now provide more information. If you set the
  1808. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.longMessage" title="unittest.TestCase.longMessage"><code class="xref py py-attr docutils literal notranslate"><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 notranslate"><span class="pre">TestCase</span></code></a> classes to
  1809. true, both the standard error message and any additional message you
  1810. provide will be printed for failures. (Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5663">bpo-5663</a>.)</p>
  1811. <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 notranslate"><span class="pre">assertRaises()</span></code></a> method now
  1812. returns a context handler when called without providing a callable
  1813. object to run. For example, you can write this:</p>
  1814. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">assertRaises</span><span class="p">(</span><span class="ne">KeyError</span><span class="p">):</span>
  1815. <span class="p">{}[</span><span class="s1">&#39;foo&#39;</span><span class="p">]</span>
  1816. </pre></div>
  1817. </div>
  1818. <p>(Implemented by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4444">bpo-4444</a>.)</p>
  1819. <p>Module- and class-level setup and teardown fixtures are now supported.
  1820. Modules can contain <code class="xref py py-func docutils literal notranslate"><span class="pre">setUpModule()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">tearDownModule()</span></code>
  1821. 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 notranslate"><span class="pre">setUpClass()</span></code></a> and
  1822. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.tearDownClass" title="unittest.TestCase.tearDownClass"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tearDownClass()</span></code></a> methods that must be defined as class methods
  1823. (using <code class="docutils literal notranslate"><span class="pre">&#64;classmethod</span></code> or equivalent). These functions and
  1824. methods are invoked when the test runner switches to a test case in a
  1825. different module or class.</p>
  1826. <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 notranslate"><span class="pre">addCleanup()</span></code></a> and
  1827. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.doCleanups" title="unittest.TestCase.doCleanups"><code class="xref py py-meth docutils literal notranslate"><span class="pre">doCleanups()</span></code></a> were added.
  1828. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.addCleanup" title="unittest.TestCase.addCleanup"><code class="xref py py-meth docutils literal notranslate"><span class="pre">addCleanup()</span></code></a> lets you add cleanup functions that
  1829. 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 notranslate"><span class="pre">setUp()</span></code></a> if
  1830. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.setUp" title="unittest.TestCase.setUp"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">tearDown()</span></code></a>). This allows
  1831. for much simpler resource allocation and deallocation during tests
  1832. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5679">bpo-5679</a>).</p>
  1833. <p>A number of new methods were added that provide more specialized
  1834. tests. Many of these methods were written by Google engineers
  1835. for use in their test suites; Gregory P. Smith, Michael Foord, and
  1836. GvR worked on merging them into Python’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 notranslate"><span class="pre">unittest</span></code></a>.</p>
  1837. <ul class="simple">
  1838. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsNone" title="unittest.TestCase.assertIsNone"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertIsNotNone()</span></code></a> take one
  1839. expression and verify that the result is or is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p></li>
  1840. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIs" title="unittest.TestCase.assertIs"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertIsNot()</span></code></a>
  1841. take two values and check whether the two values evaluate to the same object or not.
  1842. (Added by Michael Foord; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2578">bpo-2578</a>.)</p></li>
  1843. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIsInstance" title="unittest.TestCase.assertIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertIsInstance()</span></code></a> and
  1844. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotIsInstance" title="unittest.TestCase.assertNotIsInstance"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotIsInstance()</span></code></a> check whether
  1845. the resulting object is an instance of a particular class, or of
  1846. one of a tuple of classes. (Added by Georg Brandl; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7031">bpo-7031</a>.)</p></li>
  1847. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertGreater" title="unittest.TestCase.assertGreater"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertGreaterEqual()</span></code></a>,
  1848. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertLess" title="unittest.TestCase.assertLess"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertLessEqual()</span></code></a> compare
  1849. two quantities.</p></li>
  1850. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertMultiLineEqual" title="unittest.TestCase.assertMultiLineEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertMultiLineEqual()</span></code></a> compares two strings, and if they’re
  1851. not equal, displays a helpful comparison that highlights the
  1852. differences in the two strings. This comparison is now used by
  1853. 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 notranslate"><span class="pre">assertEqual()</span></code></a>.</p></li>
  1854. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRegex" title="unittest.TestCase.assertRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code></a> and
  1855. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotRegex" title="unittest.TestCase.assertNotRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotRegexpMatches()</span></code></a> checks whether the
  1856. first argument is a string matching or not matching the regular
  1857. expression provided as the second argument (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8038">bpo-8038</a>).</p></li>
  1858. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertRaisesRegex" title="unittest.TestCase.assertRaisesRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRaisesRegexp()</span></code></a> checks
  1859. whether a particular exception
  1860. is raised, and then also checks that the string representation of
  1861. the exception matches the provided regular expression.</p></li>
  1862. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertIn" title="unittest.TestCase.assertIn"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertNotIn()</span></code></a>
  1863. tests whether <em>first</em> is or is not in <em>second</em>.</p></li>
  1864. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertCountEqual" title="unittest.TestCase.assertCountEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertItemsEqual()</span></code></a> tests whether two provided sequences
  1865. contain the same elements.</p></li>
  1866. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertSetEqual" title="unittest.TestCase.assertSetEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertSetEqual()</span></code></a> compares whether two sets are equal, and
  1867. only reports the differences between the sets in case of error.</p></li>
  1868. <li><p>Similarly, <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertListEqual" title="unittest.TestCase.assertListEqual"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertTupleEqual()</span></code></a>
  1869. compare the specified types and explain any differences without necessarily
  1870. printing their full values; these methods are now used by default
  1871. 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 notranslate"><span class="pre">assertEqual()</span></code></a>.
  1872. 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 notranslate"><span class="pre">assertSequenceEqual()</span></code></a> compares two sequences
  1873. and can optionally check whether both sequences are of a
  1874. particular type.</p></li>
  1875. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertDictEqual" title="unittest.TestCase.assertDictEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictEqual()</span></code></a> compares two dictionaries and reports the
  1876. differences; it’s now used by default when you compare two dictionaries
  1877. using <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertEqual" title="unittest.TestCase.assertEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEqual()</span></code></a>. <code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictContainsSubset()</span></code> checks whether
  1878. all of the key/value pairs in <em>first</em> are found in <em>second</em>.</p></li>
  1879. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertAlmostEqual" title="unittest.TestCase.assertAlmostEqual"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">assertNotAlmostEqual()</span></code></a> test
  1880. whether <em>first</em> and <em>second</em> are approximately equal. This method
  1881. can either round their difference to an optionally specified number
  1882. of <em>places</em> (the default is 7) and compare it to zero, or require
  1883. the difference to be smaller than a supplied <em>delta</em> value.</p></li>
  1884. <li><p><a class="reference internal" href="../library/unittest.html#unittest.TestLoader.loadTestsFromName" title="unittest.TestLoader.loadTestsFromName"><code class="xref py py-meth docutils literal notranslate"><span class="pre">loadTestsFromName()</span></code></a> properly honors the
  1885. <a class="reference internal" href="../library/unittest.html#unittest.TestLoader.suiteClass" title="unittest.TestLoader.suiteClass"><code class="xref py py-attr docutils literal notranslate"><span class="pre">suiteClass</span></code></a> attribute of
  1886. the <a class="reference internal" href="../library/unittest.html#unittest.TestLoader" title="unittest.TestLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">TestLoader</span></code></a>. (Fixed by Mark Roddy; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6866">bpo-6866</a>.)</p></li>
  1887. <li><p>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 notranslate"><span class="pre">assertEqual()</span></code></a> method to handle
  1888. 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 notranslate"><span class="pre">addTypeEqualityFunc()</span></code></a> method takes a type
  1889. object and a function. The function will be used when both of the
  1890. objects being compared are of the specified type. This function
  1891. should compare the two objects and raise an exception if they don’t
  1892. match; it’s a good idea for the function to provide additional
  1893. information about why the two objects aren’t matching, much as the new
  1894. sequence comparison methods do.</p></li>
  1895. </ul>
  1896. <p><a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">unittest.main()</span></code></a> now takes an optional <code class="docutils literal notranslate"><span class="pre">exit</span></code> argument. If
  1897. false, <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> doesn’t call <a class="reference internal" href="../library/sys.html#sys.exit" title="sys.exit"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exit()</span></code></a>, allowing
  1898. <a class="reference internal" href="../library/unittest.html#unittest.main" title="unittest.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">main()</span></code></a> to be used from the interactive interpreter.
  1899. (Contributed by J. Pablo Fernández; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3379">bpo-3379</a>.)</p>
  1900. <p><a class="reference internal" href="../library/unittest.html#unittest.TestResult" title="unittest.TestResult"><code class="xref py py-class docutils literal notranslate"><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 notranslate"><span class="pre">startTestRun()</span></code></a> and
  1901. <a class="reference internal" href="../library/unittest.html#unittest.TestResult.stopTestRun" title="unittest.TestResult.stopTestRun"><code class="xref py py-meth docutils literal notranslate"><span class="pre">stopTestRun()</span></code></a> methods that are called immediately before
  1902. and after a test run. (Contributed by Robert Collins; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5728">bpo-5728</a>.)</p>
  1903. <p>With all these changes, the <code class="file docutils literal notranslate"><span class="pre">unittest.py</span></code> was becoming awkwardly
  1904. large, so the module was turned into a package and the code split into
  1905. several files (by Benjamin Peterson). This doesn’t affect how the
  1906. module is imported or used.</p>
  1907. <div class="admonition seealso">
  1908. <p class="admonition-title">See also</p>
  1909. <dl class="simple">
  1910. <dt><a class="reference external" href="https://web.archive.org/web/20210619163128/http://www.voidspace.org.uk/python/articles/unittest2.shtml">https://web.archive.org/web/20210619163128/http://www.voidspace.org.uk/python/articles/unittest2.shtml</a></dt><dd><p>Describes the new features, how to use them, and the
  1911. rationale for various design decisions. (By Michael Foord.)</p>
  1912. </dd>
  1913. </dl>
  1914. </div>
  1915. </section>
  1916. <section id="updated-module-elementtree-1-3">
  1917. <span id="elementtree-section"></span><h3>Updated module: ElementTree 1.3<a class="headerlink" href="#updated-module-elementtree-1-3" title="Link to this heading">¶</a></h3>
  1918. <p>The version of the ElementTree library included with Python was updated to
  1919. version 1.3. Some of the new features are:</p>
  1920. <ul>
  1921. <li><p>The various parsing functions now take a <em>parser</em> keyword argument
  1922. 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 notranslate"><span class="pre">XMLParser</span></code></a> instance that will
  1923. be used. This makes it possible to override the file’s internal encoding:</p>
  1924. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><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>
  1925. <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>
  1926. </pre></div>
  1927. </div>
  1928. <p>Errors in parsing XML now raise a <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.ParseError" title="xml.etree.ElementTree.ParseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ParseError</span></code></a> exception, whose
  1929. instances have a <code class="xref py py-attr docutils literal notranslate"><span class="pre">position</span></code> attribute
  1930. containing a (<em>line</em>, <em>column</em>) tuple giving the location of the problem.</p>
  1931. </li>
  1932. <li><p>ElementTree’s code for converting trees to a string has been
  1933. significantly reworked, making it roughly twice as fast in many
  1934. 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 notranslate"><span class="pre">ElementTree.write()</span></code></a>
  1935. and <code class="xref py py-meth docutils literal notranslate"><span class="pre">Element.write()</span></code> methods now have a <em>method</em> parameter that can be
  1936. “xml” (the default), “html”, or “text”. HTML mode will output empty
  1937. elements as <code class="docutils literal notranslate"><span class="pre">&lt;empty&gt;&lt;/empty&gt;</span></code> instead of <code class="docutils literal notranslate"><span class="pre">&lt;empty/&gt;</span></code>, and text
  1938. mode will skip over elements and only output the text chunks. If
  1939. you set the <a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.tag" title="xml.etree.ElementTree.Element.tag"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tag</span></code></a> attribute of an
  1940. element to <code class="docutils literal notranslate"><span class="pre">None</span></code> but
  1941. leave its children in place, the element will be omitted when the
  1942. tree is written out, so you don’t need to do more extensive rearrangement
  1943. to remove a single element.</p>
  1944. <p>Namespace handling has also been improved. All <code class="docutils literal notranslate"><span class="pre">xmlns:&lt;whatever&gt;</span></code>
  1945. declarations are now output on the root element, not scattered throughout
  1946. the resulting XML. You can set the default namespace for a tree
  1947. by setting the <code class="xref py py-attr docutils literal notranslate"><span class="pre">default_namespace</span></code> attribute and can
  1948. 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 notranslate"><span class="pre">register_namespace()</span></code></a>. In XML mode,
  1949. you can use the true/false <em>xml_declaration</em> parameter to suppress the
  1950. XML declaration.</p>
  1951. </li>
  1952. <li><p>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 notranslate"><span class="pre">Element</span></code></a> method:
  1953. <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 notranslate"><span class="pre">extend()</span></code></a> appends the items from a
  1954. sequence to the element’s children. Elements themselves behave like
  1955. sequences, so it’s easy to move children from one element to
  1956. another:</p>
  1957. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">xml.etree</span> <span class="kn">import</span> <span class="n">ElementTree</span> <span class="k">as</span> <span class="n">ET</span>
  1958. <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>
  1959. <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>
  1960. <span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
  1961. <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>
  1962. <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>
  1963. <span class="c1"># Outputs &lt;root&gt;&lt;item&gt;1&lt;/item&gt;...&lt;/root&gt;</span>
  1964. <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>
  1965. </pre></div>
  1966. </div>
  1967. </li>
  1968. <li><p>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 notranslate"><span class="pre">Element</span></code></a> method:
  1969. <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 notranslate"><span class="pre">iter()</span></code></a> yields the children of the
  1970. element as a generator. It’s also possible to write <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">child</span> <span class="pre">in</span>
  1971. <span class="pre">elem:</span></code> to loop over an element’s children. The existing method
  1972. <code class="xref py py-meth docutils literal notranslate"><span class="pre">getiterator()</span></code> is now deprecated, as is <code class="xref py py-meth docutils literal notranslate"><span class="pre">getchildren()</span></code>
  1973. which constructs and returns a list of children.</p></li>
  1974. <li><p>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 notranslate"><span class="pre">Element</span></code></a> method:
  1975. <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 notranslate"><span class="pre">itertext()</span></code></a> yields all chunks of
  1976. text that are descendants of the element. For example:</p>
  1977. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">ET</span><span class="o">.</span><span class="n">XML</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;&lt;list&gt;</span>
  1978. <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>
  1979. <span class="s2">&lt;/list&gt;&quot;&quot;&quot;</span><span class="p">)</span>
  1980. <span class="c1"># Outputs [&#39;\n &#39;, &#39;1&#39;, &#39; &#39;, &#39;2&#39;, &#39; &#39;, &#39;3&#39;, &#39;\n&#39;]</span>
  1981. <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>
  1982. </pre></div>
  1983. </div>
  1984. </li>
  1985. <li><p>Deprecated: using an element as a Boolean (i.e., <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">elem:</span></code>) would
  1986. return true if the element had any children, or false if there were
  1987. no children. This behaviour is confusing – <code class="docutils literal notranslate"><span class="pre">None</span></code> is false, but
  1988. so is a childless element? – so it will now trigger a
  1989. <a class="reference internal" href="../library/exceptions.html#FutureWarning" title="FutureWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">FutureWarning</span></code></a>. In your code, you should be explicit: write
  1990. <code class="docutils literal notranslate"><span class="pre">len(elem)</span> <span class="pre">!=</span> <span class="pre">0</span></code> if you’re interested in the number of children,
  1991. or <code class="docutils literal notranslate"><span class="pre">elem</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">None</span></code>.</p></li>
  1992. </ul>
  1993. <p>Fredrik Lundh develops ElementTree and produced the 1.3 version;
  1994. you can read his article describing 1.3 at
  1995. <a class="reference external" href="https://web.archive.org/web/20200703234532/http://effbot.org/zone/elementtree-13-intro.htm">https://web.archive.org/web/20200703234532/http://effbot.org/zone/elementtree-13-intro.htm</a>.
  1996. Florent Xicluna updated the version included with
  1997. Python, after discussions on python-dev and in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6472">bpo-6472</a>.)</p>
  1998. </section>
  1999. </section>
  2000. <section id="build-and-c-api-changes">
  2001. <h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Link to this heading">¶</a></h2>
  2002. <p>Changes to Python’s build process and to the C API include:</p>
  2003. <ul>
  2004. <li><p>The latest release of the GNU Debugger, GDB 7, can be <a class="reference external" href="https://web.archive.org/web/20110715084810/http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html">scripted
  2005. using Python</a>.
  2006. When you begin debugging an executable program P, GDB will look for
  2007. a file named <code class="docutils literal notranslate"><span class="pre">P-gdb.py</span></code> and automatically read it. Dave Malcolm
  2008. contributed a <code class="file docutils literal notranslate"><span class="pre">python-gdb.py</span></code> that adds a number of
  2009. commands useful when debugging Python itself. For example,
  2010. <code class="docutils literal notranslate"><span class="pre">py-up</span></code> and <code class="docutils literal notranslate"><span class="pre">py-down</span></code> go up or down one Python stack frame,
  2011. which usually corresponds to several C stack frames. <code class="docutils literal notranslate"><span class="pre">py-print</span></code>
  2012. prints the value of a Python variable, and <code class="docutils literal notranslate"><span class="pre">py-bt</span></code> prints the
  2013. Python stack trace. (Added as a result of <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8032">bpo-8032</a>.)</p></li>
  2014. <li><p>If you use the <code class="file docutils literal notranslate"><span class="pre">.gdbinit</span></code> file provided with Python,
  2015. the “pyo” macro in the 2.7 version now works correctly when the thread being
  2016. debugged doesn’t hold the GIL; the macro now acquires it before printing.
  2017. (Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3632">bpo-3632</a>.)</p></li>
  2018. <li><p><a class="reference internal" href="../c-api/init.html#c.Py_AddPendingCall" title="Py_AddPendingCall"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_AddPendingCall()</span></code></a> is now thread-safe, letting any
  2019. worker thread submit notifications to the main Python thread. This
  2020. is particularly useful for asynchronous IO operations.
  2021. (Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4293">bpo-4293</a>.)</p></li>
  2022. <li><p>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 notranslate"><span class="pre">PyCode_NewEmpty()</span></code></a> creates an empty code object;
  2023. only the filename, function name, and first line number are required.
  2024. This is useful for extension modules that are attempting to
  2025. construct a more useful traceback stack. Previously such
  2026. extensions needed to call <code class="xref c c-func docutils literal notranslate"><span class="pre">PyCode_New()</span></code>, which had many
  2027. more arguments. (Added by Jeffrey Yasskin.)</p></li>
  2028. <li><p>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 notranslate"><span class="pre">PyErr_NewExceptionWithDoc()</span></code></a> creates a new
  2029. 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 notranslate"><span class="pre">PyErr_NewException()</span></code></a> does,
  2030. but takes an extra <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code> argument containing the docstring for the
  2031. new exception class. (Added by ‘lekma’ on the Python bug tracker;
  2032. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7033">bpo-7033</a>.)</p></li>
  2033. <li><p>New function: <a class="reference internal" href="../c-api/frame.html#c.PyFrame_GetLineNumber" title="PyFrame_GetLineNumber"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyFrame_GetLineNumber()</span></code></a> takes a frame object
  2034. and returns the line number that the frame is currently executing.
  2035. Previously code would need to get the index of the bytecode
  2036. instruction currently executing, and then look up the line number
  2037. corresponding to that address. (Added by Jeffrey Yasskin.)</p></li>
  2038. <li><p>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 notranslate"><span class="pre">PyLong_AsLongAndOverflow()</span></code></a> and
  2039. <a class="reference internal" href="../c-api/long.html#c.PyLong_AsLongLongAndOverflow" title="PyLong_AsLongLongAndOverflow"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_AsLongLongAndOverflow()</span></code></a> approximates a Python long
  2040. integer as a C <span class="c-expr sig sig-inline c"><span class="kt">long</span></span> or <span class="c-expr sig sig-inline c"><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span>.
  2041. If the number is too large to fit into
  2042. the output type, an <em>overflow</em> flag is set and returned to the caller.
  2043. (Contributed by Case Van Horsen; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7528">bpo-7528</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7767">bpo-7767</a>.)</p></li>
  2044. <li><p>New function: stemming from the rewrite of string-to-float conversion,
  2045. 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 notranslate"><span class="pre">PyOS_string_to_double()</span></code></a> function was added. The old
  2046. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_atof()</span></code> functions
  2047. are now deprecated.</p></li>
  2048. <li><p>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 notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> sets the value of
  2049. <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> and can optionally update <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> to include the
  2050. directory containing the script named by <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> depending
  2051. on the value of an <em>updatepath</em> parameter.</p>
  2052. <p>This function was added to close a security hole for applications
  2053. 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 notranslate"><span class="pre">PySys_SetArgv()</span></code></a>, would
  2054. always update <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, and sometimes it would add the current
  2055. directory. This meant that, if you ran an application embedding
  2056. Python in a directory controlled by someone else, attackers could
  2057. put a Trojan-horse module in the directory (say, a file named
  2058. <code class="file docutils literal notranslate"><span class="pre">os.py</span></code>) that your application would then import and run.</p>
  2059. <p>If you maintain a C/C++ application that embeds Python, check
  2060. whether you’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 notranslate"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider
  2061. 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 notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a>
  2062. with <em>updatepath</em> set to false.</p>
  2063. <p>Security issue reported as <a class="reference external" href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983">CVE-2008-5983</a>;
  2064. discussed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5753">bpo-5753</a>, and fixed by Antoine Pitrou.</p>
  2065. </li>
  2066. <li><p>New macros: the Python header files now define the following macros:
  2067. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISALNUM</span></code>,
  2068. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISALPHA</span></code>,
  2069. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISDIGIT</span></code>,
  2070. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISLOWER</span></code>,
  2071. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISSPACE</span></code>,
  2072. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISUPPER</span></code>,
  2073. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ISXDIGIT</span></code>,
  2074. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOLOWER</span></code>, and <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TOUPPER</span></code>.
  2075. All of these functions are analogous to the C
  2076. standard macros for classifying characters, but ignore the current
  2077. locale setting, because in
  2078. several places Python needs to analyze characters in a
  2079. locale-independent way. (Added by Eric Smith;
  2080. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5793">bpo-5793</a>.)</p>
  2081. </li>
  2082. <li><p>Removed function: <code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_CallObject()</span></code> is now only available
  2083. as a macro. A function version was being kept around to preserve
  2084. ABI linking compatibility, but that was in 1997; it can certainly be
  2085. deleted by now. (Removed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8276">bpo-8276</a>.)</p></li>
  2086. <li><p>New format codes: the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyString_FromFormat()</span></code>,
  2087. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyString_FromFormatV()</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 notranslate"><span class="pre">PyErr_Format()</span></code></a> functions now
  2088. accept <code class="docutils literal notranslate"><span class="pre">%lld</span></code> and <code class="docutils literal notranslate"><span class="pre">%llu</span></code> format codes for displaying
  2089. C’s <span class="c-expr sig sig-inline c"><span class="kt">long</span><span class="w"> </span><span class="kt">long</span></span> types.
  2090. (Contributed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7228">bpo-7228</a>.)</p></li>
  2091. <li><p>The complicated interaction between threads and process forking has
  2092. been changed. Previously, the child process created by
  2093. <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> might fail because the child is created with only a
  2094. 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 notranslate"><span class="pre">os.fork()</span></code></a>.
  2095. If other threads were holding a lock, such as Python’s import lock,
  2096. when the fork was performed, the lock would still be marked as
  2097. “held” in the new process. But in the child process nothing would
  2098. ever release the lock, since the other threads weren’t replicated,
  2099. and the child process would no longer be able to perform imports.</p>
  2100. <p>Python 2.7 acquires the import lock before performing an
  2101. <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>, and will also clean up any locks created using the
  2102. <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module. C extension modules that have internal
  2103. locks, or that call <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> themselves, will not benefit
  2104. from this clean-up.</p>
  2105. <p>(Fixed by Thomas Wouters; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1590864">bpo-1590864</a>.)</p>
  2106. </li>
  2107. <li><p>The <a class="reference internal" href="../c-api/init.html#c.Py_Finalize" title="Py_Finalize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Finalize()</span></code></a> function now calls the internal
  2108. <code class="xref py py-func docutils literal notranslate"><span class="pre">threading._shutdown()</span></code> function; this prevents some exceptions from
  2109. being raised when an interpreter shuts down.
  2110. (Patch by Adam Olsen; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1722344">bpo-1722344</a>.)</p></li>
  2111. <li><p>When using the <a class="reference internal" href="../c-api/structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemberDef</span></code></a> structure to define attributes
  2112. of a type, Python will no longer let you try to delete or set a
  2113. <code class="xref c c-macro docutils literal notranslate"><span class="pre">T_STRING_INPLACE</span></code> attribute.</p>
  2114. </li>
  2115. <li><p>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 notranslate"><span class="pre">ctypes</span></code></a> module are now prefixed
  2116. with <code class="docutils literal notranslate"><span class="pre">Py</span></code>, or with <code class="docutils literal notranslate"><span class="pre">_ctypes</span></code>. (Implemented by Thomas
  2117. Heller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3102">bpo-3102</a>.)</p></li>
  2118. <li><p>New configure option: the <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-system-expat</span></code> switch allows
  2119. building the <a class="reference internal" href="../library/pyexpat.html#module-xml.parsers.expat" title="xml.parsers.expat: An interface to the Expat non-validating XML parser."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pyexpat</span></code></a> module to use the system Expat library.
  2120. (Contributed by Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7609">bpo-7609</a>.)</p></li>
  2121. <li><p>New configure option: the
  2122. <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-valgrind</span></code> option will now disable the pymalloc
  2123. allocator, which is difficult for the Valgrind memory-error detector
  2124. to analyze correctly.
  2125. Valgrind will therefore be better at detecting memory leaks and
  2126. overruns. (Contributed by James Henstridge; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2422">bpo-2422</a>.)</p></li>
  2127. <li><p>New configure option: you can now supply an empty string to
  2128. <code class="xref std std-option docutils literal notranslate"><span class="pre">--with-dbmliborder=</span></code> in order to disable all of the various
  2129. DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
  2130. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6491">bpo-6491</a>.)</p></li>
  2131. <li><p>The <strong class="program">configure</strong> script now checks for floating-point rounding bugs
  2132. on certain 32-bit Intel chips and defines a <code class="xref c c-macro docutils literal notranslate"><span class="pre">X87_DOUBLE_ROUNDING</span></code>
  2133. preprocessor definition. No code currently uses this definition,
  2134. but it’s available if anyone wishes to use it.
  2135. (Added by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2937">bpo-2937</a>.)</p>
  2136. <p><strong class="program">configure</strong> also now sets a <span class="target" id="index-11"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">LDCXXSHARED</span></code> Makefile
  2137. variable for supporting C++ linking. (Contributed by Arfrever
  2138. Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1222585">bpo-1222585</a>.)</p>
  2139. </li>
  2140. <li><p>The build process now creates the necessary files for pkg-config
  2141. support. (Contributed by Clinton Roy; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3585">bpo-3585</a>.)</p></li>
  2142. <li><p>The build process now supports Subversion 1.7. (Contributed by
  2143. Arfrever Frehtes Taifersar Arahesis; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6094">bpo-6094</a>.)</p></li>
  2144. </ul>
  2145. <section id="capsules">
  2146. <span id="whatsnew27-capsules"></span><h3>Capsules<a class="headerlink" href="#capsules" title="Link to this heading">¶</a></h3>
  2147. <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 notranslate"><span class="pre">PyCapsule</span></code></a>, for providing a
  2148. C API to an extension module. A capsule is essentially the holder of
  2149. a C <code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*</span></code> pointer, and is made available as a module attribute; for
  2150. 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 notranslate"><span class="pre">socket</span></code></a> module’s API is exposed as <code class="docutils literal notranslate"><span class="pre">socket.CAPI</span></code>,
  2151. 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 notranslate"><span class="pre">unicodedata</span></code></a> exposes <code class="docutils literal notranslate"><span class="pre">ucnhash_CAPI</span></code>. Other extensions
  2152. can import the module, access its dictionary to get the capsule
  2153. object, and then get the <code class="docutils literal notranslate"><span class="pre">void</span> <span class="pre">*</span></code> pointer, which will usually point
  2154. to an array of pointers to the module’s various API functions.</p>
  2155. <p>There is an existing data type already used for this,
  2156. <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code>, but it doesn’t provide type safety. Evil code
  2157. written in pure Python could cause a segmentation fault by taking a
  2158. <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> from module A and somehow substituting it for the
  2159. <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> in module B. Capsules know their own name,
  2160. and getting the pointer requires providing the name:</p>
  2161. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">vtable</span><span class="p">;</span>
  2162. <span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PyCapsule_IsValid</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mymodule.CAPI&quot;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  2163. <span class="w"> </span><span class="n">PyErr_SetString</span><span class="p">(</span><span class="n">PyExc_ValueError</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;argument type invalid&quot;</span><span class="p">);</span>
  2164. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
  2165. <span class="p">}</span>
  2166. <span class="n">vtable</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyCapsule_GetPointer</span><span class="p">(</span><span class="n">capsule</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mymodule.CAPI&quot;</span><span class="p">);</span>
  2167. </pre></div>
  2168. </div>
  2169. <p>You are assured that <code class="docutils literal notranslate"><span class="pre">vtable</span></code> points to whatever you’re expecting.
  2170. 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 notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> would
  2171. detect the mismatched name and return false. Refer to
  2172. <a class="reference internal" href="../extending/extending.html#using-capsules"><span class="std std-ref">Providing a C API for an Extension Module</span></a> for more information on using these objects.</p>
  2173. <p>Python 2.7 now uses capsules internally to provide various
  2174. extension-module APIs, but the <code class="xref c c-func docutils literal notranslate"><span class="pre">PyCObject_AsVoidPtr()</span></code> was
  2175. modified to handle capsules, preserving compile-time compatibility
  2176. with the <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> interface. Use of
  2177. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyCObject_AsVoidPtr()</span></code> will signal a
  2178. <a class="reference internal" href="../library/exceptions.html#PendingDeprecationWarning" title="PendingDeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PendingDeprecationWarning</span></code></a>, which is silent by default.</p>
  2179. <p>Implemented in Python 3.1 and backported to 2.7 by Larry Hastings;
  2180. discussed in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5630">bpo-5630</a>.</p>
  2181. </section>
  2182. <section id="port-specific-changes-windows">
  2183. <h3>Port-Specific Changes: Windows<a class="headerlink" href="#port-specific-changes-windows" title="Link to this heading">¶</a></h3>
  2184. <ul class="simple">
  2185. <li><p>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 notranslate"><span class="pre">msvcrt</span></code></a> module now contains some constants from
  2186. the <code class="file docutils literal notranslate"><span class="pre">crtassem.h</span></code> header file:
  2187. <a class="reference internal" href="../library/msvcrt.html#msvcrt.CRT_ASSEMBLY_VERSION" title="msvcrt.CRT_ASSEMBLY_VERSION"><code class="xref py py-data docutils literal notranslate"><span class="pre">CRT_ASSEMBLY_VERSION</span></code></a>,
  2188. <a class="reference internal" href="../library/msvcrt.html#msvcrt.VC_ASSEMBLY_PUBLICKEYTOKEN" title="msvcrt.VC_ASSEMBLY_PUBLICKEYTOKEN"><code class="xref py py-data docutils literal notranslate"><span class="pre">VC_ASSEMBLY_PUBLICKEYTOKEN</span></code></a>,
  2189. and <a class="reference internal" href="../library/msvcrt.html#msvcrt.LIBRARIES_ASSEMBLY_NAME_PREFIX" title="msvcrt.LIBRARIES_ASSEMBLY_NAME_PREFIX"><code class="xref py py-data docutils literal notranslate"><span class="pre">LIBRARIES_ASSEMBLY_NAME_PREFIX</span></code></a>.
  2190. (Contributed by David Cournapeau; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4365">bpo-4365</a>.)</p></li>
  2191. <li><p>The <a class="reference internal" href="../library/winreg.html#module-winreg" title="winreg: Routines and objects for manipulating the Windows registry. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">_winreg</span></code></a> module for accessing the registry now implements
  2192. the <a class="reference internal" href="../library/winreg.html#winreg.CreateKeyEx" title="winreg.CreateKeyEx"><code class="xref py py-func docutils literal notranslate"><span class="pre">CreateKeyEx()</span></code></a> and <a class="reference internal" href="../library/winreg.html#winreg.DeleteKeyEx" title="winreg.DeleteKeyEx"><code class="xref py py-func docutils literal notranslate"><span class="pre">DeleteKeyEx()</span></code></a>
  2193. functions, extended versions of previously supported functions that
  2194. take several extra arguments. The <a class="reference internal" href="../library/winreg.html#winreg.DisableReflectionKey" title="winreg.DisableReflectionKey"><code class="xref py py-func docutils literal notranslate"><span class="pre">DisableReflectionKey()</span></code></a>,
  2195. <a class="reference internal" href="../library/winreg.html#winreg.EnableReflectionKey" title="winreg.EnableReflectionKey"><code class="xref py py-func docutils literal notranslate"><span class="pre">EnableReflectionKey()</span></code></a>, and <a class="reference internal" href="../library/winreg.html#winreg.QueryReflectionKey" title="winreg.QueryReflectionKey"><code class="xref py py-func docutils literal notranslate"><span class="pre">QueryReflectionKey()</span></code></a>
  2196. were also tested and documented.
  2197. (Implemented by Brian Curtin: <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7347">bpo-7347</a>.)</p></li>
  2198. <li><p>The new <code class="xref c c-func docutils literal notranslate"><span class="pre">_beginthreadex()</span></code> API is used to start threads, and
  2199. the native thread-local storage functions are now used.
  2200. (Contributed by Kristján Valur Jónsson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3582">bpo-3582</a>.)</p></li>
  2201. <li><p>The <a class="reference internal" href="../library/os.html#os.kill" title="os.kill"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.kill()</span></code></a> function now works on Windows. The signal value
  2202. can be the constants <a class="reference internal" href="../library/signal.html#signal.CTRL_C_EVENT" title="signal.CTRL_C_EVENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">CTRL_C_EVENT</span></code></a>,
  2203. <a class="reference internal" href="../library/signal.html#signal.CTRL_BREAK_EVENT" title="signal.CTRL_BREAK_EVENT"><code class="xref py py-const docutils literal notranslate"><span class="pre">CTRL_BREAK_EVENT</span></code></a>, or any integer. The first two constants
  2204. will send <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">C</kbd></kbd> and <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">Break</kbd></kbd> keystroke events to
  2205. subprocesses; any other value will use the <code class="xref c c-func docutils literal notranslate"><span class="pre">TerminateProcess()</span></code>
  2206. API. (Contributed by Miki Tebeka; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1220212">bpo-1220212</a>.)</p></li>
  2207. <li><p>The <a class="reference internal" href="../library/os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> function now correctly fails
  2208. for an empty path. (Fixed by Hirokazu Yamamoto; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5913">bpo-5913</a>.)</p></li>
  2209. <li><p>The <a class="reference internal" href="../library/mimetypes.html#module-mimetypes" title="mimetypes: Mapping of filename extensions to MIME types."><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code></a> module will now read the MIME database from
  2210. the Windows registry when initializing.
  2211. (Patch by Gabriel Genellina; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4969">bpo-4969</a>.)</p></li>
  2212. </ul>
  2213. </section>
  2214. <section id="port-specific-changes-mac-os-x">
  2215. <h3>Port-Specific Changes: Mac OS X<a class="headerlink" href="#port-specific-changes-mac-os-x" title="Link to this heading">¶</a></h3>
  2216. <ul>
  2217. <li><p>The path <code class="docutils literal notranslate"><span class="pre">/Library/Python/2.7/site-packages</span></code> is now appended to
  2218. <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>, in order to share added packages between the system
  2219. installation and a user-installed copy of the same version.
  2220. (Changed by Ronald Oussoren; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4865">bpo-4865</a>.)</p>
  2221. <div class="versionchanged">
  2222. <p><span class="versionmodified changed">Changed in version 2.7.13: </span>As of 2.7.13, this change was removed.
  2223. <code class="docutils literal notranslate"><span class="pre">/Library/Python/2.7/site-packages</span></code>, the site-packages directory
  2224. used by the Apple-supplied system Python 2.7 is no longer appended to
  2225. <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> for user-installed Pythons such as from the python.org
  2226. installers. As of macOS 10.12, Apple changed how the system
  2227. site-packages directory is configured, which could cause installation
  2228. of pip components, like setuptools, to fail. Packages installed for
  2229. the system Python will no longer be shared with user-installed
  2230. Pythons. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=28440">bpo-28440</a>)</p>
  2231. </div>
  2232. </li>
  2233. </ul>
  2234. </section>
  2235. <section id="port-specific-changes-freebsd">
  2236. <h3>Port-Specific Changes: FreeBSD<a class="headerlink" href="#port-specific-changes-freebsd" title="Link to this heading">¶</a></h3>
  2237. <ul class="simple">
  2238. <li><p>FreeBSD 7.1’s <code class="xref py py-const docutils literal notranslate"><span class="pre">SO_SETFIB</span></code> constant, used with the <a class="reference internal" href="../library/socket.html#socket.socket" title="socket.socket"><code class="xref py py-func docutils literal notranslate"><span class="pre">socket()</span></code></a> methods
  2239. <a class="reference internal" href="../library/socket.html#socket.socket.getsockopt" title="socket.socket.getsockopt"><code class="xref py py-func docutils literal notranslate"><span class="pre">getsockopt()</span></code></a>/<a class="reference internal" href="../library/socket.html#socket.socket.setsockopt" title="socket.socket.setsockopt"><code class="xref py py-func docutils literal notranslate"><span class="pre">setsockopt()</span></code></a> to select an
  2240. 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 notranslate"><span class="pre">socket</span></code></a>
  2241. module. (Added by Kyle VanderBeek; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8235">bpo-8235</a>.)</p></li>
  2242. </ul>
  2243. </section>
  2244. </section>
  2245. <section id="other-changes-and-fixes">
  2246. <h2>Other Changes and Fixes<a class="headerlink" href="#other-changes-and-fixes" title="Link to this heading">¶</a></h2>
  2247. <ul class="simple">
  2248. <li><p>Two benchmark scripts, <code class="file docutils literal notranslate"><span class="pre">iobench</span></code> and <code class="file docutils literal notranslate"><span class="pre">ccbench</span></code>, were
  2249. added to the <code class="file docutils literal notranslate"><span class="pre">Tools</span></code> directory. <code class="file docutils literal notranslate"><span class="pre">iobench</span></code> measures the
  2250. 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 notranslate"><span class="pre">open()</span></code></a>
  2251. while performing various operations, and <code class="file docutils literal notranslate"><span class="pre">ccbench</span></code> is a
  2252. concurrency benchmark that tries to measure computing throughput,
  2253. thread switching latency, and IO processing bandwidth when
  2254. performing several tasks using a varying number of threads.</p></li>
  2255. <li><p>The <code class="file docutils literal notranslate"><span class="pre">Tools/i18n/msgfmt.py</span></code> script now understands plural
  2256. forms in <code class="file docutils literal notranslate"><span class="pre">.po</span></code> files. (Fixed by Martin von Löwis;
  2257. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5464">bpo-5464</a>.)</p></li>
  2258. <li><p>When importing a module from a <code class="file docutils literal notranslate"><span class="pre">.pyc</span></code> or <code class="file docutils literal notranslate"><span class="pre">.pyo</span></code> file
  2259. with an existing <code class="file docutils literal notranslate"><span class="pre">.py</span></code> counterpart, the <a class="reference internal" href="../reference/datamodel.html#codeobject.co_filename" title="codeobject.co_filename"><code class="xref py py-attr docutils literal notranslate"><span class="pre">co_filename</span></code></a>
  2260. attributes of the resulting code objects are overwritten when the
  2261. original filename is obsolete. This can happen if the file has been
  2262. renamed, moved, or is accessed through different paths. (Patch by
  2263. Ziga Seilnacht and Jean-Paul Calderone; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1180193">bpo-1180193</a>.)</p></li>
  2264. <li><p>The <code class="file docutils literal notranslate"><span class="pre">regrtest.py</span></code> script now takes a <code class="xref std std-option docutils literal notranslate"><span class="pre">--randseed=</span></code>
  2265. switch that takes an integer that will be used as the random seed
  2266. for the <code class="xref std std-option docutils literal notranslate"><span class="pre">-r</span></code> option that executes tests in random order.
  2267. The <code class="xref std std-option docutils literal notranslate"><span class="pre">-r</span></code> option also reports the seed that was used
  2268. (Added by Collin Winter.)</p></li>
  2269. <li><p>Another <code class="file docutils literal notranslate"><span class="pre">regrtest.py</span></code> switch is <code class="xref std std-option docutils literal notranslate"><span class="pre">-j</span></code>, which
  2270. takes an integer specifying how many tests run in parallel. This
  2271. allows reducing the total runtime on multi-core machines.
  2272. This option is compatible with several other options, including the
  2273. <code class="xref std std-option docutils literal notranslate"><span class="pre">-R</span></code> switch which is known to produce long runtimes.
  2274. (Added by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6152">bpo-6152</a>.) This can also be used
  2275. with a new <code class="xref std std-option docutils literal notranslate"><span class="pre">-F</span></code> switch that runs selected tests in a loop
  2276. until they fail. (Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7312">bpo-7312</a>.)</p></li>
  2277. <li><p>When executed as a script, the <code class="file docutils literal notranslate"><span class="pre">py_compile.py</span></code> module now
  2278. accepts <code class="docutils literal notranslate"><span class="pre">'-'</span></code> as an argument, which will read standard input for
  2279. the list of filenames to be compiled. (Contributed by Piotr
  2280. Ożarowski; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8233">bpo-8233</a>.)</p></li>
  2281. </ul>
  2282. </section>
  2283. <section id="porting-to-python-2-7">
  2284. <h2>Porting to Python 2.7<a class="headerlink" href="#porting-to-python-2-7" title="Link to this heading">¶</a></h2>
  2285. <p>This section lists previously described changes and other bugfixes
  2286. that may require changes to your code:</p>
  2287. <ul class="simple">
  2288. <li><p>The <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-func docutils literal notranslate"><span class="pre">range()</span></code></a> function processes its arguments more
  2289. 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 notranslate"><span class="pre">__int__()</span></code></a> on non-float,
  2290. non-integer arguments that are supplied to it. (Fixed by Alexander
  2291. Belopolsky; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1533">bpo-1533</a>.)</p></li>
  2292. <li><p>The string <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method changed the default precision used
  2293. for floating-point and complex numbers from 6 decimal
  2294. 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 notranslate"><span class="pre">str()</span></code></a>.
  2295. (Changed by Eric Smith; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5920">bpo-5920</a>.)</p></li>
  2296. <li><p>Because of an optimization for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement, the special
  2297. methods <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><code class="xref py py-meth docutils literal notranslate"><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 notranslate"><span class="pre">__exit__()</span></code></a> must belong to the object’s
  2298. type, and cannot be directly attached to the object’s instance. This
  2299. 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 notranslate"><span class="pre">object</span></code></a>) and C extension
  2300. types. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6101">bpo-6101</a>.)</p></li>
  2301. <li><p>Due to a bug in Python 2.6, the <em>exc_value</em> parameter to
  2302. <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__exit__()</span></code></a> methods was often the string representation of the
  2303. exception, not an instance. This was fixed in 2.7, so <em>exc_value</em>
  2304. will be an instance as expected. (Fixed by Florent Xicluna;
  2305. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7853">bpo-7853</a>.)</p></li>
  2306. <li><p>When a restricted set of attributes were set using <code class="docutils literal notranslate"><span class="pre">__slots__</span></code>,
  2307. 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 notranslate"><span class="pre">AttributeError</span></code></a>
  2308. as you would expect. Fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7604">bpo-7604</a>.)</p></li>
  2309. </ul>
  2310. <p>In the standard library:</p>
  2311. <ul>
  2312. <li><p>Operations with <a class="reference internal" href="../library/datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instances that resulted in a year
  2313. falling outside the supported range didn’t always raise
  2314. <a class="reference internal" href="../library/exceptions.html#OverflowError" title="OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a>. Such errors are now checked more carefully
  2315. and will now raise the exception. (Reported by Mark Leander, patch
  2316. by Anand B. Pillai and Alexander Belopolsky; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7150">bpo-7150</a>.)</p></li>
  2317. <li><p>When using <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> instances with a string’s
  2318. <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method, the default alignment was previously
  2319. left-alignment. This has been changed to right-alignment, which might
  2320. change the output of your programs.
  2321. (Changed by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6857">bpo-6857</a>.)</p>
  2322. <p>Comparisons involving a signaling NaN value (or <code class="docutils literal notranslate"><span class="pre">sNAN</span></code>) now signal
  2323. <a class="reference internal" href="../library/decimal.html#decimal.InvalidOperation" title="decimal.InvalidOperation"><code class="xref py py-const docutils literal notranslate"><span class="pre">InvalidOperation</span></code></a> instead of silently returning a true or
  2324. false value depending on the comparison operator. Quiet NaN values
  2325. (or <code class="docutils literal notranslate"><span class="pre">NaN</span></code>) are now hashable. (Fixed by Mark Dickinson;
  2326. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7279">bpo-7279</a>.)</p>
  2327. </li>
  2328. <li><p>The <a class="reference internal" href="../library/xml.etree.elementtree.html#module-xml.etree.ElementTree" title="xml.etree.ElementTree: Implementation of the ElementTree API."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> library no longer escapes
  2329. ampersands and angle brackets when outputting an XML processing
  2330. instruction (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;?xml-stylesheet</span> <span class="pre">href=&quot;#style1&quot;?&gt;</span></code>)
  2331. or comment (which looks like <code class="docutils literal notranslate"><span class="pre">&lt;!--</span> <span class="pre">comment</span> <span class="pre">--&gt;</span></code>).
  2332. (Patch by Neil Muller; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2746">bpo-2746</a>.)</p></li>
  2333. <li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code> method of <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">StringIO</span></code></a> objects now does
  2334. nothing when a negative length is requested, as other file-like
  2335. objects do. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7348">bpo-7348</a>).</p></li>
  2336. <li><p>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 notranslate"><span class="pre">syslog</span></code></a> module will now use the value of <code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> as the
  2337. identifier instead of the previous default value of <code class="docutils literal notranslate"><span class="pre">'python'</span></code>.
  2338. (Changed by Sean Reifschneider; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8451">bpo-8451</a>.)</p></li>
  2339. <li><p>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 notranslate"><span class="pre">tarfile</span></code></a> module’s default error handling has changed, to
  2340. no longer suppress fatal errors. The default error level was previously 0,
  2341. which meant that errors would only result in a message being written to the
  2342. debug log, but because the debug log is not activated by default,
  2343. these errors go unnoticed. The default error level is now 1,
  2344. which raises an exception if there’s an error.
  2345. (Changed by Lars Gustäbel; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7357">bpo-7357</a>.)</p></li>
  2346. <li><p>The <a class="reference internal" href="../library/urllib.parse.html#module-urllib.parse" title="urllib.parse: Parse URLs into or assemble them from components."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urlparse</span></code></a> module’s <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlsplit" title="urllib.parse.urlsplit"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlsplit()</span></code></a> now handles
  2347. unknown URL schemes in a fashion compliant with <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3986.html"><strong>RFC 3986</strong></a>: if the
  2348. URL is of the form <code class="docutils literal notranslate"><span class="pre">&quot;&lt;something&gt;://...&quot;</span></code>, the text before the
  2349. <code class="docutils literal notranslate"><span class="pre">://</span></code> is treated as the scheme, even if it’s a made-up scheme that
  2350. the module doesn’t know about. This change may break code that
  2351. worked around the old behaviour. For example, Python 2.6.4 or 2.5
  2352. will return the following:</p>
  2353. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
  2354. <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>
  2355. <span class="go">(&#39;invented&#39;, &#39;&#39;, &#39;//host/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
  2356. </pre></div>
  2357. </div>
  2358. <p>Python 2.7 (and Python 2.6.5) will return:</p>
  2359. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urlparse</span>
  2360. <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>
  2361. <span class="go">(&#39;invented&#39;, &#39;host&#39;, &#39;/filename?query&#39;, &#39;&#39;, &#39;&#39;)</span>
  2362. </pre></div>
  2363. </div>
  2364. <p>(Python 2.7 actually produces slightly different output, since it
  2365. returns a named tuple instead of a standard tuple.)</p>
  2366. </li>
  2367. </ul>
  2368. <p>For C extensions:</p>
  2369. <ul class="simple">
  2370. <li><p>C extensions that use integer format codes with the <code class="docutils literal notranslate"><span class="pre">PyArg_Parse*</span></code>
  2371. 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 notranslate"><span class="pre">TypeError</span></code></a> exception
  2372. instead of triggering a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5080">bpo-5080</a>).</p></li>
  2373. <li><p>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 notranslate"><span class="pre">PyOS_string_to_double()</span></code></a> function instead of the old
  2374. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_strtod()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_ascii_atof()</span></code> functions,
  2375. which are now deprecated.</p></li>
  2376. </ul>
  2377. <p>For applications that embed Python:</p>
  2378. <ul class="simple">
  2379. <li><p>The <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> function was added, letting
  2380. applications close a security hole when the existing
  2381. <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a> function was used. Check whether you’re
  2382. calling <a class="reference internal" href="../c-api/init.html#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a> and carefully consider whether the
  2383. 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 notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> with
  2384. <em>updatepath</em> set to false.</p></li>
  2385. </ul>
  2386. </section>
  2387. <section id="new-features-added-to-python-2-7-maintenance-releases">
  2388. <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="Link to this heading">¶</a></h2>
  2389. <p>New features may be added to Python 2.7 maintenance releases when the
  2390. situation genuinely calls for it. Any such additions must go through
  2391. the Python Enhancement Proposal process, and make a compelling case for why
  2392. they can’t be adequately addressed by either adding the new feature solely to
  2393. Python 3, or else by publishing it on the Python Package Index.</p>
  2394. <p>In addition to the specific proposals listed below, there is a general
  2395. exemption allowing new <code class="docutils literal notranslate"><span class="pre">-3</span></code> warnings to be added in any Python 2.7
  2396. maintenance release.</p>
  2397. <section id="two-new-environment-variables-for-debug-mode">
  2398. <h3>Two new environment variables for debug mode<a class="headerlink" href="#two-new-environment-variables-for-debug-mode" title="Link to this heading">¶</a></h3>
  2399. <p>In debug mode, the <code class="docutils literal notranslate"><span class="pre">[xxx</span> <span class="pre">refs]</span></code> statistic is not written by default, the
  2400. <code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSHOWREFCOUNT</span></code> environment variable now must also be set.
  2401. (Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=31733">bpo-31733</a>.)</p>
  2402. <p>When Python is compiled with <code class="docutils literal notranslate"><span class="pre">COUNT_ALLOC</span></code> defined, allocation counts are no
  2403. longer dumped by default anymore: the <code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSHOWALLOCCOUNT</span></code> environment
  2404. variable must now also be set. Moreover, allocation counts are now dumped into
  2405. stderr, rather than stdout. (Contributed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=31692">bpo-31692</a>.)</p>
  2406. <div class="versionadded">
  2407. <p><span class="versionmodified added">New in version 2.7.15.</span></p>
  2408. </div>
  2409. </section>
  2410. <section id="pep-434-idle-enhancement-exception-for-all-branches">
  2411. <h3>PEP 434: IDLE Enhancement Exception for All Branches<a class="headerlink" href="#pep-434-idle-enhancement-exception-for-all-branches" title="Link to this heading">¶</a></h3>
  2412. <p><span class="target" id="index-13"></span><a class="pep reference external" href="https://peps.python.org/pep-0434/"><strong>PEP 434</strong></a> describes a general exemption for changes made to the IDLE
  2413. development environment shipped along with Python. This exemption makes it
  2414. possible for the IDLE developers to provide a more consistent user
  2415. experience across all supported versions of Python 2 and 3.</p>
  2416. <p>For details of any IDLE changes, refer to the NEWS file for the specific
  2417. release.</p>
  2418. </section>
  2419. <section id="pep-466-network-security-enhancements-for-python-2-7">
  2420. <h3>PEP 466: Network Security Enhancements for Python 2.7<a class="headerlink" href="#pep-466-network-security-enhancements-for-python-2-7" title="Link to this heading">¶</a></h3>
  2421. <p><span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0466/"><strong>PEP 466</strong></a> describes a number of network security enhancement proposals
  2422. that have been approved for inclusion in Python 2.7 maintenance releases,
  2423. with the first of those changes appearing in the Python 2.7.7 release.</p>
  2424. <p><span class="target" id="index-15"></span><a class="pep reference external" href="https://peps.python.org/pep-0466/"><strong>PEP 466</strong></a> related features added in Python 2.7.7:</p>
  2425. <ul class="simple">
  2426. <li><p><a class="reference internal" href="../library/hmac.html#hmac.compare_digest" title="hmac.compare_digest"><code class="xref py py-func docutils literal notranslate"><span class="pre">hmac.compare_digest()</span></code></a> was backported from Python 3 to make a timing
  2427. attack resistant comparison operation available to Python 2 applications.
  2428. (Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21306">bpo-21306</a>.)</p></li>
  2429. <li><p>OpenSSL 1.0.1g was upgraded in the official Windows installers published on
  2430. python.org. (Contributed by Zachary Ware; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21462">bpo-21462</a>.)</p></li>
  2431. </ul>
  2432. <p><span class="target" id="index-16"></span><a class="pep reference external" href="https://peps.python.org/pep-0466/"><strong>PEP 466</strong></a> related features added in Python 2.7.8:</p>
  2433. <ul class="simple">
  2434. <li><p><a class="reference internal" href="../library/hashlib.html#hashlib.pbkdf2_hmac" title="hashlib.pbkdf2_hmac"><code class="xref py py-func docutils literal notranslate"><span class="pre">hashlib.pbkdf2_hmac()</span></code></a> was backported from Python 3 to make a hashing
  2435. algorithm suitable for secure password storage broadly available to Python
  2436. 2 applications. (Contributed by Alex Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21304">bpo-21304</a>.)</p></li>
  2437. <li><p>OpenSSL 1.0.1h was upgraded for the official Windows installers published on
  2438. python.org. (contributed by Zachary Ware in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21671">bpo-21671</a> for CVE-2014-0224)</p></li>
  2439. </ul>
  2440. <p><span class="target" id="index-17"></span><a class="pep reference external" href="https://peps.python.org/pep-0466/"><strong>PEP 466</strong></a> related features added in Python 2.7.9:</p>
  2441. <ul>
  2442. <li><p>Most of Python 3.4’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 notranslate"><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 notranslate"><span class="pre">ssl</span></code></a>
  2443. now supports Server Name Indication, TLS1.x settings, access to the platform
  2444. certificate store, the <a class="reference internal" href="../library/ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">SSLContext</span></code></a> class, and other
  2445. features. (Contributed by Alex Gaynor and David Reid; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21308">bpo-21308</a>.)</p>
  2446. <p>Refer to the “Version added: 2.7.9” notes in the module documentation for
  2447. specific details.</p>
  2448. </li>
  2449. <li><p><a class="reference internal" href="../library/os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a> was changed to cache a file descriptor to <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code>
  2450. instead of reopening <code class="docutils literal notranslate"><span class="pre">/dev/urandom</span></code> on every call. (Contributed by Alex
  2451. Gaynor; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21305">bpo-21305</a>.)</p></li>
  2452. <li><p><a class="reference internal" href="../library/hashlib.html#hashlib.algorithms_guaranteed" title="hashlib.algorithms_guaranteed"><code class="xref py py-data docutils literal notranslate"><span class="pre">hashlib.algorithms_guaranteed</span></code></a> and
  2453. <a class="reference internal" href="../library/hashlib.html#hashlib.algorithms_available" title="hashlib.algorithms_available"><code class="xref py py-data docutils literal notranslate"><span class="pre">hashlib.algorithms_available</span></code></a> were backported from Python 3 to make
  2454. it easier for Python 2 applications to select the strongest available hash
  2455. algorithm. (Contributed by Alex Gaynor in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=21307">bpo-21307</a>)</p></li>
  2456. </ul>
  2457. </section>
  2458. <section id="pep-477-backport-ensurepip-pep-453-to-python-2-7">
  2459. <h3>PEP 477: Backport ensurepip (PEP 453) to Python 2.7<a class="headerlink" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7" title="Link to this heading">¶</a></h3>
  2460. <p><span class="target" id="index-18"></span><a class="pep reference external" href="https://peps.python.org/pep-0477/"><strong>PEP 477</strong></a> approves the inclusion of the <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0453/"><strong>PEP 453</strong></a> ensurepip module and the
  2461. improved documentation that was enabled by it in the Python 2.7 maintenance
  2462. releases, appearing first in the Python 2.7.9 release.</p>
  2463. <section id="bootstrapping-pip-by-default">
  2464. <h4>Bootstrapping pip By Default<a class="headerlink" href="#bootstrapping-pip-by-default" title="Link to this heading">¶</a></h4>
  2465. <p>The new <a class="reference internal" href="../library/ensurepip.html#module-ensurepip" title="ensurepip: Bootstrapping the &quot;pip&quot; installer into an existing Python installation or virtual environment."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ensurepip</span></code></a> module (defined in <span class="target" id="index-20"></span><a class="pep reference external" href="https://peps.python.org/pep-0453/"><strong>PEP 453</strong></a>) provides a standard
  2466. cross-platform mechanism to bootstrap the pip installer into Python
  2467. installations. The version of <code class="docutils literal notranslate"><span class="pre">pip</span></code> included with Python 2.7.9 is <code class="docutils literal notranslate"><span class="pre">pip</span></code>
  2468. 1.5.6, and future 2.7.x maintenance releases will update the bundled version to
  2469. the latest version of <code class="docutils literal notranslate"><span class="pre">pip</span></code> that is available at the time of creating the
  2470. release candidate.</p>
  2471. <p>By default, the commands <code class="docutils literal notranslate"><span class="pre">pip</span></code>, <code class="docutils literal notranslate"><span class="pre">pipX</span></code> and <code class="docutils literal notranslate"><span class="pre">pipX.Y</span></code> will be installed on
  2472. all platforms (where X.Y stands for the version of the Python installation),
  2473. along with the <code class="docutils literal notranslate"><span class="pre">pip</span></code> Python package and its dependencies.</p>
  2474. <p>For CPython <a class="reference internal" href="../using/unix.html#building-python-on-unix"><span class="std std-ref">source builds on POSIX systems</span></a>,
  2475. the <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">install</span></code> and <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">altinstall</span></code> commands do not bootstrap <code class="docutils literal notranslate"><span class="pre">pip</span></code>
  2476. by default. This behaviour can be controlled through configure options, and
  2477. overridden through Makefile options.</p>
  2478. <p>On Windows and Mac OS X, the CPython installers now default to installing
  2479. <code class="docutils literal notranslate"><span class="pre">pip</span></code> along with CPython itself (users may opt out of installing it
  2480. during the installation process). Window users will need to opt in to the
  2481. automatic <code class="docutils literal notranslate"><span class="pre">PATH</span></code> modifications to have <code class="docutils literal notranslate"><span class="pre">pip</span></code> available from the command
  2482. line by default, otherwise it can still be accessed through the Python
  2483. launcher for Windows as <code class="docutils literal notranslate"><span class="pre">py</span> <span class="pre">-m</span> <span class="pre">pip</span></code>.</p>
  2484. <p>As <a class="reference external" href="https://peps.python.org/pep-0477/#disabling-ensurepip-by-downstream-distributors">discussed in the PEP</a>, platform packagers may choose not to install
  2485. these commands by default, as long as, when invoked, they provide clear and
  2486. simple directions on how to install them on that platform (usually using
  2487. the system package manager).</p>
  2488. </section>
  2489. <section id="documentation-changes">
  2490. <h4>Documentation Changes<a class="headerlink" href="#documentation-changes" title="Link to this heading">¶</a></h4>
  2491. <p>As part of this change, the <a class="reference internal" href="../installing/index.html#installing-index"><span class="std std-ref">Installing Python Modules</span></a> and
  2492. <a class="reference internal" href="../distributing/index.html#distributing-index"><span class="std std-ref">Distributing Python Modules</span></a> sections of the documentation have been
  2493. completely redesigned as short getting started and FAQ documents. Most
  2494. packaging documentation has now been moved out to the Python Packaging
  2495. Authority maintained <a class="reference external" href="https://packaging.python.org">Python Packaging User Guide</a> and the documentation of the individual
  2496. projects.</p>
  2497. <p>However, as this migration is currently still incomplete, the legacy
  2498. versions of those guides remaining available as <a class="reference internal" href="../extending/building.html#install-index"><span class="std std-ref">Building C and C++ Extensions with setuptools</span></a>
  2499. and <a class="reference internal" href="../extending/building.html#setuptools-index"><span class="std std-ref">Building C and C++ Extensions with setuptools</span></a>.</p>
  2500. <div class="admonition seealso">
  2501. <p class="admonition-title">See also</p>
  2502. <dl class="simple">
  2503. <dt><span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0453/"><strong>PEP 453</strong></a> – Explicit bootstrapping of pip in Python installations</dt><dd><p>PEP written by Donald Stufft and Nick Coghlan, implemented by
  2504. Donald Stufft, Nick Coghlan, Martin von Löwis and Ned Deily.</p>
  2505. </dd>
  2506. </dl>
  2507. </div>
  2508. </section>
  2509. </section>
  2510. <section id="pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">
  2511. <h3>PEP 476: Enabling certificate verification by default for stdlib http clients<a class="headerlink" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients" title="Link to this heading">¶</a></h3>
  2512. <p><span class="target" id="index-22"></span><a class="pep reference external" href="https://peps.python.org/pep-0476/"><strong>PEP 476</strong></a> updated <a class="reference internal" href="../library/http.html#module-http" title="http: HTTP status codes and messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">httplib</span></code></a> and modules which use it, such as
  2513. <a class="reference internal" href="../library/urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib2</span></code></a> and <a class="reference internal" href="../library/xmlrpc.client.html#module-xmlrpc.client" title="xmlrpc.client: XML-RPC client access."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xmlrpclib</span></code></a>, to now
  2514. verify that the server
  2515. presents a certificate which is signed by a Certificate Authority in the
  2516. platform trust store and whose hostname matches the hostname being requested
  2517. by default, significantly improving security for many applications. This
  2518. change was made in the Python 2.7.9 release.</p>
  2519. <p>For applications which require the old previous behavior, they can pass an
  2520. alternate context:</p>
  2521. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">urllib2</span>
  2522. <span class="kn">import</span> <span class="nn">ssl</span>
  2523. <span class="c1"># This disables all verification</span>
  2524. <span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">_create_unverified_context</span><span class="p">()</span>
  2525. <span class="c1"># This allows using a specific certificate for the host, which doesn&#39;t need</span>
  2526. <span class="c1"># to be in the trust store</span>
  2527. <span class="n">context</span> <span class="o">=</span> <span class="n">ssl</span><span class="o">.</span><span class="n">create_default_context</span><span class="p">(</span><span class="n">cafile</span><span class="o">=</span><span class="s2">&quot;/path/to/file.crt&quot;</span><span class="p">)</span>
  2528. <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s2">&quot;https://invalid-cert&quot;</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">context</span><span class="p">)</span>
  2529. </pre></div>
  2530. </div>
  2531. </section>
  2532. <section id="pep-493-https-verification-migration-tools-for-python-2-7">
  2533. <h3>PEP 493: HTTPS verification migration tools for Python 2.7<a class="headerlink" href="#pep-493-https-verification-migration-tools-for-python-2-7" title="Link to this heading">¶</a></h3>
  2534. <p><span class="target" id="index-23"></span><a class="pep reference external" href="https://peps.python.org/pep-0493/"><strong>PEP 493</strong></a> provides additional migration tools to support a more incremental
  2535. infrastructure upgrade process for environments containing applications and
  2536. services relying on the historically permissive processing of server
  2537. certificates when establishing client HTTPS connections. These additions were
  2538. made in the Python 2.7.12 release.</p>
  2539. <p>These tools are intended for use in cases where affected applications and
  2540. services can’t be modified to explicitly pass a more permissive SSL context
  2541. when establishing the connection.</p>
  2542. <p>For applications and services which can’t be modified at all, the new
  2543. <code class="docutils literal notranslate"><span class="pre">PYTHONHTTPSVERIFY</span></code> environment variable may be set to <code class="docutils literal notranslate"><span class="pre">0</span></code> to revert an
  2544. entire Python process back to the default permissive behaviour of Python 2.7.8
  2545. and earlier.</p>
  2546. <p>For cases where the connection establishment code can’t be modified, but the
  2547. overall application can be, the new <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl._https_verify_certificates()</span></code>
  2548. function can be used to adjust the default behaviour at runtime.</p>
  2549. </section>
  2550. <section id="new-make-regen-all-build-target">
  2551. <h3>New <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> build target<a class="headerlink" href="#new-make-regen-all-build-target" title="Link to this heading">¶</a></h3>
  2552. <p>To simplify cross-compilation, and to ensure that CPython can reliably be
  2553. compiled without requiring an existing version of Python to already be
  2554. available, the autotools-based build system no longer attempts to implicitly
  2555. recompile generated files based on file modification times.</p>
  2556. <p>Instead, a new <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> command has been added to force regeneration
  2557. of these files when desired (e.g. after an initial version of Python has
  2558. already been built based on the pregenerated versions).</p>
  2559. <p>More selective regeneration targets are also defined - see
  2560. <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Makefile.pre.in">Makefile.pre.in</a> for details.</p>
  2561. <p>(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a>.)</p>
  2562. <div class="versionadded">
  2563. <p><span class="versionmodified added">New in version 2.7.14.</span></p>
  2564. </div>
  2565. </section>
  2566. <section id="removal-of-make-touch-build-target">
  2567. <h3>Removal of <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> build target<a class="headerlink" href="#removal-of-make-touch-build-target" title="Link to this heading">¶</a></h3>
  2568. <p>The <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> build target previously used to request implicit regeneration
  2569. of generated files by updating their modification times has been removed.</p>
  2570. <p>It has been replaced by the new <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> target.</p>
  2571. <p>(Contributed by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=23404">bpo-23404</a>.)</p>
  2572. <div class="versionchanged">
  2573. <p><span class="versionmodified changed">Changed in version 2.7.14.</span></p>
  2574. </div>
  2575. </section>
  2576. </section>
  2577. <section id="acknowledgements">
  2578. <span id="acks27"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Link to this heading">¶</a></h2>
  2579. <p>The author would like to thank the following people for offering
  2580. suggestions, corrections and assistance with various drafts of this
  2581. article: Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray,
  2582. Hugh Secker-Walker.</p>
  2583. </section>
  2584. </section>
  2585. <div class="clearer"></div>
  2586. </div>
  2587. </div>
  2588. </div>
  2589. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2590. <div class="sphinxsidebarwrapper">
  2591. <div>
  2592. <h3><a href="../contents.html">Table of Contents</a></h3>
  2593. <ul>
  2594. <li><a class="reference internal" href="#">What’s New in Python 2.7</a><ul>
  2595. <li><a class="reference internal" href="#the-future-for-python-2-x">The Future for Python 2.x</a></li>
  2596. <li><a class="reference internal" href="#changes-to-the-handling-of-deprecation-warnings">Changes to the Handling of Deprecation Warnings</a></li>
  2597. <li><a class="reference internal" href="#python-3-1-features">Python 3.1 Features</a></li>
  2598. <li><a class="reference internal" href="#pep-372-adding-an-ordered-dictionary-to-collections">PEP 372: Adding an Ordered Dictionary to collections</a></li>
  2599. <li><a class="reference internal" href="#pep-378-format-specifier-for-thousands-separator">PEP 378: Format Specifier for Thousands Separator</a></li>
  2600. <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>
  2601. <li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary-Based Configuration For Logging</a></li>
  2602. <li><a class="reference internal" href="#pep-3106-dictionary-views">PEP 3106: Dictionary Views</a></li>
  2603. <li><a class="reference internal" href="#pep-3137-the-memoryview-object">PEP 3137: The memoryview Object</a></li>
  2604. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
  2605. <li><a class="reference internal" href="#interpreter-changes">Interpreter Changes</a></li>
  2606. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  2607. </ul>
  2608. </li>
  2609. <li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a><ul>
  2610. <li><a class="reference internal" href="#new-module-importlib">New module: importlib</a></li>
  2611. <li><a class="reference internal" href="#new-module-sysconfig">New module: sysconfig</a></li>
  2612. <li><a class="reference internal" href="#ttk-themed-widgets-for-tk">ttk: Themed Widgets for Tk</a></li>
  2613. <li><a class="reference internal" href="#updated-module-unittest">Updated module: unittest</a></li>
  2614. <li><a class="reference internal" href="#updated-module-elementtree-1-3">Updated module: ElementTree 1.3</a></li>
  2615. </ul>
  2616. </li>
  2617. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
  2618. <li><a class="reference internal" href="#capsules">Capsules</a></li>
  2619. <li><a class="reference internal" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
  2620. <li><a class="reference internal" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
  2621. <li><a class="reference internal" href="#port-specific-changes-freebsd">Port-Specific Changes: FreeBSD</a></li>
  2622. </ul>
  2623. </li>
  2624. <li><a class="reference internal" href="#other-changes-and-fixes">Other Changes and Fixes</a></li>
  2625. <li><a class="reference internal" href="#porting-to-python-2-7">Porting to Python 2.7</a></li>
  2626. <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>
  2627. <li><a class="reference internal" href="#two-new-environment-variables-for-debug-mode">Two new environment variables for debug mode</a></li>
  2628. <li><a class="reference internal" href="#pep-434-idle-enhancement-exception-for-all-branches">PEP 434: IDLE Enhancement Exception for All Branches</a></li>
  2629. <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>
  2630. <li><a class="reference internal" href="#pep-477-backport-ensurepip-pep-453-to-python-2-7">PEP 477: Backport ensurepip (PEP 453) to Python 2.7</a><ul>
  2631. <li><a class="reference internal" href="#bootstrapping-pip-by-default">Bootstrapping pip By Default</a></li>
  2632. <li><a class="reference internal" href="#documentation-changes">Documentation Changes</a></li>
  2633. </ul>
  2634. </li>
  2635. <li><a class="reference internal" href="#pep-476-enabling-certificate-verification-by-default-for-stdlib-http-clients">PEP 476: Enabling certificate verification by default for stdlib http clients</a></li>
  2636. <li><a class="reference internal" href="#pep-493-https-verification-migration-tools-for-python-2-7">PEP 493: HTTPS verification migration tools for Python 2.7</a></li>
  2637. <li><a class="reference internal" href="#new-make-regen-all-build-target">New <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">regen-all</span></code> build target</a></li>
  2638. <li><a class="reference internal" href="#removal-of-make-touch-build-target">Removal of <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">touch</span></code> build target</a></li>
  2639. </ul>
  2640. </li>
  2641. <li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
  2642. </ul>
  2643. </li>
  2644. </ul>
  2645. </div>
  2646. <div>
  2647. <h4>Previous topic</h4>
  2648. <p class="topless"><a href="3.0.html"
  2649. title="previous chapter">What’s New In Python 3.0</a></p>
  2650. </div>
  2651. <div>
  2652. <h4>Next topic</h4>
  2653. <p class="topless"><a href="2.6.html"
  2654. title="next chapter">What’s New in Python 2.6</a></p>
  2655. </div>
  2656. <div role="note" aria-label="source link">
  2657. <h3>This Page</h3>
  2658. <ul class="this-page-menu">
  2659. <li><a href="../bugs.html">Report a Bug</a></li>
  2660. <li>
  2661. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/2.7.rst"
  2662. rel="nofollow">Show Source
  2663. </a>
  2664. </li>
  2665. </ul>
  2666. </div>
  2667. </div>
  2668. <div id="sidebarbutton" title="Collapse sidebar">
  2669. <span>«</span>
  2670. </div>
  2671. </div>
  2672. <div class="clearer"></div>
  2673. </div>
  2674. <div class="related" role="navigation" aria-label="related navigation">
  2675. <h3>Navigation</h3>
  2676. <ul>
  2677. <li class="right" style="margin-right: 10px">
  2678. <a href="../genindex.html" title="General Index"
  2679. >index</a></li>
  2680. <li class="right" >
  2681. <a href="../py-modindex.html" title="Python Module Index"
  2682. >modules</a> |</li>
  2683. <li class="right" >
  2684. <a href="2.6.html" title="What’s New in Python 2.6"
  2685. >next</a> |</li>
  2686. <li class="right" >
  2687. <a href="3.0.html" title="What’s New In Python 3.0"
  2688. >previous</a> |</li>
  2689. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2690. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2691. <li class="switchers">
  2692. <div class="language_switcher_placeholder"></div>
  2693. <div class="version_switcher_placeholder"></div>
  2694. </li>
  2695. <li>
  2696. </li>
  2697. <li id="cpython-language-and-version">
  2698. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2699. </li>
  2700. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  2701. <li class="nav-item nav-item-this"><a href="">What’s New in Python 2.7</a></li>
  2702. <li class="right">
  2703. <div class="inline-search" role="search">
  2704. <form class="inline-search" action="../search.html" method="get">
  2705. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2706. <input type="submit" value="Go" />
  2707. </form>
  2708. </div>
  2709. |
  2710. </li>
  2711. <li class="right">
  2712. <label class="theme-selector-label">
  2713. Theme
  2714. <select class="theme-selector" oninput="activateTheme(this.value)">
  2715. <option value="auto" selected>Auto</option>
  2716. <option value="light">Light</option>
  2717. <option value="dark">Dark</option>
  2718. </select>
  2719. </label> |</li>
  2720. </ul>
  2721. </div>
  2722. <div class="footer">
  2723. &copy;
  2724. <a href="../copyright.html">
  2725. Copyright
  2726. </a>
  2727. 2001-2024, Python Software Foundation.
  2728. <br />
  2729. This page is licensed under the Python Software Foundation License Version 2.
  2730. <br />
  2731. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2732. <br />
  2733. See <a href="/license.html">History and License</a> for more information.<br />
  2734. <br />
  2735. The Python Software Foundation is a non-profit corporation.
  2736. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2737. <br />
  2738. <br />
  2739. Last updated on Apr 09, 2024 (13:47 UTC).
  2740. <a href="/bugs.html">Found a bug</a>?
  2741. <br />
  2742. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2743. </div>
  2744. </body>
  2745. </html>
上海开阖软件有限公司 沪ICP备12045867号-1