gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2935 lines
329KB

  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 3.2" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/whatsnew/3.2.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Author, Raymond Hettinger,. This article explains the new features in Python 3.2 as compared to 3.1. Python 3.2 was released on February 20, 2011. It focuses on a few highlights and gives a few exa..." />
  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, Raymond Hettinger,. This article explains the new features in Python 3.2 as compared to 3.1. Python 3.2 was released on February 20, 2011. It focuses on a few highlights and gives a few exa..." />
  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 3.2 &#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 3.1" href="3.1.html" />
  33. <link rel="prev" title="What’s New In Python 3.3" href="3.3.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/whatsnew/3.2.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 3.2</a><ul>
  86. <li><a class="reference internal" href="#pep-384-defining-a-stable-abi">PEP 384: Defining a Stable ABI</a></li>
  87. <li><a class="reference internal" href="#pep-389-argparse-command-line-parsing-module">PEP 389: Argparse Command Line Parsing Module</a></li>
  88. <li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary Based Configuration for Logging</a></li>
  89. <li><a class="reference internal" href="#pep-3148-the-concurrent-futures-module">PEP 3148: The <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> module</a></li>
  90. <li><a class="reference internal" href="#pep-3147-pyc-repository-directories">PEP 3147: PYC Repository Directories</a></li>
  91. <li><a class="reference internal" href="#pep-3149-abi-version-tagged-so-files">PEP 3149: ABI Version Tagged .so Files</a></li>
  92. <li><a class="reference internal" href="#pep-3333-python-web-server-gateway-interface-v1-0-1">PEP 3333: Python Web Server Gateway Interface v1.0.1</a></li>
  93. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
  94. <li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
  95. <li><a class="reference internal" href="#email">email</a></li>
  96. <li><a class="reference internal" href="#elementtree">elementtree</a></li>
  97. <li><a class="reference internal" href="#functools">functools</a></li>
  98. <li><a class="reference internal" href="#itertools">itertools</a></li>
  99. <li><a class="reference internal" href="#collections">collections</a></li>
  100. <li><a class="reference internal" href="#threading">threading</a></li>
  101. <li><a class="reference internal" href="#datetime-and-time">datetime and time</a></li>
  102. <li><a class="reference internal" href="#math">math</a></li>
  103. <li><a class="reference internal" href="#abc">abc</a></li>
  104. <li><a class="reference internal" href="#io">io</a></li>
  105. <li><a class="reference internal" href="#reprlib">reprlib</a></li>
  106. <li><a class="reference internal" href="#logging">logging</a></li>
  107. <li><a class="reference internal" href="#csv">csv</a></li>
  108. <li><a class="reference internal" href="#contextlib">contextlib</a></li>
  109. <li><a class="reference internal" href="#decimal-and-fractions">decimal and fractions</a></li>
  110. <li><a class="reference internal" href="#ftp">ftp</a></li>
  111. <li><a class="reference internal" href="#popen">popen</a></li>
  112. <li><a class="reference internal" href="#select">select</a></li>
  113. <li><a class="reference internal" href="#gzip-and-zipfile">gzip and zipfile</a></li>
  114. <li><a class="reference internal" href="#tarfile">tarfile</a></li>
  115. <li><a class="reference internal" href="#hashlib">hashlib</a></li>
  116. <li><a class="reference internal" href="#ast">ast</a></li>
  117. <li><a class="reference internal" href="#os">os</a></li>
  118. <li><a class="reference internal" href="#shutil">shutil</a></li>
  119. <li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
  120. <li><a class="reference internal" href="#html">html</a></li>
  121. <li><a class="reference internal" href="#socket">socket</a></li>
  122. <li><a class="reference internal" href="#ssl">ssl</a></li>
  123. <li><a class="reference internal" href="#nntp">nntp</a></li>
  124. <li><a class="reference internal" href="#certificates">certificates</a></li>
  125. <li><a class="reference internal" href="#imaplib">imaplib</a></li>
  126. <li><a class="reference internal" href="#http-client">http.client</a></li>
  127. <li><a class="reference internal" href="#unittest">unittest</a></li>
  128. <li><a class="reference internal" href="#random">random</a></li>
  129. <li><a class="reference internal" href="#poplib">poplib</a></li>
  130. <li><a class="reference internal" href="#asyncore">asyncore</a></li>
  131. <li><a class="reference internal" href="#tempfile">tempfile</a></li>
  132. <li><a class="reference internal" href="#inspect">inspect</a></li>
  133. <li><a class="reference internal" href="#pydoc">pydoc</a></li>
  134. <li><a class="reference internal" href="#dis">dis</a></li>
  135. <li><a class="reference internal" href="#dbm">dbm</a></li>
  136. <li><a class="reference internal" href="#ctypes">ctypes</a></li>
  137. <li><a class="reference internal" href="#site">site</a></li>
  138. <li><a class="reference internal" href="#sysconfig">sysconfig</a></li>
  139. <li><a class="reference internal" href="#pdb">pdb</a></li>
  140. <li><a class="reference internal" href="#configparser">configparser</a></li>
  141. <li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
  142. <li><a class="reference internal" href="#mailbox">mailbox</a></li>
  143. <li><a class="reference internal" href="#turtledemo">turtledemo</a></li>
  144. </ul>
  145. </li>
  146. <li><a class="reference internal" href="#multi-threading">Multi-threading</a></li>
  147. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  148. <li><a class="reference internal" href="#unicode">Unicode</a></li>
  149. <li><a class="reference internal" href="#codecs">Codecs</a></li>
  150. <li><a class="reference internal" href="#documentation">Documentation</a></li>
  151. <li><a class="reference internal" href="#idle">IDLE</a></li>
  152. <li><a class="reference internal" href="#code-repository">Code Repository</a></li>
  153. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a></li>
  154. <li><a class="reference internal" href="#porting-to-python-3-2">Porting to Python 3.2</a></li>
  155. </ul>
  156. </li>
  157. </ul>
  158. </div>
  159. <div>
  160. <h4>Previous topic</h4>
  161. <p class="topless"><a href="3.3.html"
  162. title="previous chapter">What’s New In Python 3.3</a></p>
  163. </div>
  164. <div>
  165. <h4>Next topic</h4>
  166. <p class="topless"><a href="3.1.html"
  167. title="next chapter">What’s New In Python 3.1</a></p>
  168. </div>
  169. <div role="note" aria-label="source link">
  170. <h3>This Page</h3>
  171. <ul class="this-page-menu">
  172. <li><a href="../bugs.html">Report a Bug</a></li>
  173. <li>
  174. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/3.2.rst"
  175. rel="nofollow">Show Source
  176. </a>
  177. </li>
  178. </ul>
  179. </div>
  180. </nav>
  181. </div>
  182. </div>
  183. <div class="related" role="navigation" aria-label="related navigation">
  184. <h3>Navigation</h3>
  185. <ul>
  186. <li class="right" style="margin-right: 10px">
  187. <a href="../genindex.html" title="General Index"
  188. accesskey="I">index</a></li>
  189. <li class="right" >
  190. <a href="../py-modindex.html" title="Python Module Index"
  191. >modules</a> |</li>
  192. <li class="right" >
  193. <a href="3.1.html" title="What’s New In Python 3.1"
  194. accesskey="N">next</a> |</li>
  195. <li class="right" >
  196. <a href="3.3.html" title="What’s New In Python 3.3"
  197. accesskey="P">previous</a> |</li>
  198. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  199. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  200. <li class="switchers">
  201. <div class="language_switcher_placeholder"></div>
  202. <div class="version_switcher_placeholder"></div>
  203. </li>
  204. <li>
  205. </li>
  206. <li id="cpython-language-and-version">
  207. <a href="../index.html">3.12.3 Documentation</a> &#187;
  208. </li>
  209. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">What’s New in Python</a> &#187;</li>
  210. <li class="nav-item nav-item-this"><a href="">What’s New In Python 3.2</a></li>
  211. <li class="right">
  212. <div class="inline-search" role="search">
  213. <form class="inline-search" action="../search.html" method="get">
  214. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  215. <input type="submit" value="Go" />
  216. </form>
  217. </div>
  218. |
  219. </li>
  220. <li class="right">
  221. <label class="theme-selector-label">
  222. Theme
  223. <select class="theme-selector" oninput="activateTheme(this.value)">
  224. <option value="auto" selected>Auto</option>
  225. <option value="light">Light</option>
  226. <option value="dark">Dark</option>
  227. </select>
  228. </label> |</li>
  229. </ul>
  230. </div>
  231. <div class="document">
  232. <div class="documentwrapper">
  233. <div class="bodywrapper">
  234. <div class="body" role="main">
  235. <section id="what-s-new-in-python-3-2">
  236. <h1>What’s New In Python 3.2<a class="headerlink" href="#what-s-new-in-python-3-2" title="Link to this heading">¶</a></h1>
  237. <dl class="field-list simple">
  238. <dt class="field-odd">Author<span class="colon">:</span></dt>
  239. <dd class="field-odd"><p>Raymond Hettinger</p>
  240. </dd>
  241. </dl>
  242. <p>This article explains the new features in Python 3.2 as compared to 3.1.
  243. Python 3.2 was released on February 20, 2011. It
  244. focuses on a few highlights and gives a few examples. For full details, see the
  245. <a class="reference external" href="https://github.com/python/cpython/blob/076ca6c3c8df3030307e548d9be792ce3c1c6eea/Misc/NEWS">Misc/NEWS</a>
  246. file.</p>
  247. <div class="admonition seealso">
  248. <p class="admonition-title">See also</p>
  249. <p><span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0392/"><strong>PEP 392</strong></a> - Python 3.2 Release Schedule</p>
  250. </div>
  251. <section id="pep-384-defining-a-stable-abi">
  252. <h2>PEP 384: Defining a Stable ABI<a class="headerlink" href="#pep-384-defining-a-stable-abi" title="Link to this heading">¶</a></h2>
  253. <p>In the past, extension modules built for one Python version were often
  254. not usable with other Python versions. Particularly on Windows, every
  255. feature release of Python required rebuilding all extension modules that
  256. one wanted to use. This requirement was the result of the free access to
  257. Python interpreter internals that extension modules could use.</p>
  258. <p>With Python 3.2, an alternative approach becomes available: extension
  259. modules which restrict themselves to a limited API (by defining
  260. Py_LIMITED_API) cannot use many of the internals, but are constrained
  261. to a set of API functions that are promised to be stable for several
  262. releases. As a consequence, extension modules built for 3.2 in that
  263. mode will also work with 3.3, 3.4, and so on. Extension modules that
  264. make use of details of memory structures can still be built, but will
  265. need to be recompiled for every feature release.</p>
  266. <div class="admonition seealso">
  267. <p class="admonition-title">See also</p>
  268. <dl class="simple">
  269. <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0384/"><strong>PEP 384</strong></a> - Defining a Stable ABI</dt><dd><p>PEP written by Martin von Löwis.</p>
  270. </dd>
  271. </dl>
  272. </div>
  273. </section>
  274. <section id="pep-389-argparse-command-line-parsing-module">
  275. <h2>PEP 389: Argparse Command Line Parsing Module<a class="headerlink" href="#pep-389-argparse-command-line-parsing-module" title="Link to this heading">¶</a></h2>
  276. <p>A new module for command line parsing, <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>, was introduced to
  277. overcome the limitations 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> which did not provide support for
  278. positional arguments (not just options), subcommands, required options and other
  279. common patterns of specifying and validating options.</p>
  280. <p>This module has already had widespread success in the community as a
  281. third-party module. Being more fully featured than its predecessor, the
  282. <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 is now the preferred module for command-line processing.
  283. The older module is still being kept available because of the substantial amount
  284. of legacy code that depends on it.</p>
  285. <p>Here’s an annotated example parser showing features like limiting results to a
  286. set of choices, specifying a <em>metavar</em> in the help screen, validating that one
  287. or more positional arguments is present, and making a required option:</p>
  288. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
  289. <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>
  290. <span class="n">description</span> <span class="o">=</span> <span class="s1">&#39;Manage servers&#39;</span><span class="p">,</span> <span class="c1"># main description for help</span>
  291. <span class="n">epilog</span> <span class="o">=</span> <span class="s1">&#39;Tested on Solaris and Linux&#39;</span><span class="p">)</span> <span class="c1"># displayed after help</span>
  292. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;action&#39;</span><span class="p">,</span> <span class="c1"># argument name</span>
  293. <span class="n">choices</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;deploy&#39;</span><span class="p">,</span> <span class="s1">&#39;start&#39;</span><span class="p">,</span> <span class="s1">&#39;stop&#39;</span><span class="p">],</span> <span class="c1"># three allowed values</span>
  294. <span class="n">help</span> <span class="o">=</span> <span class="s1">&#39;action on each target&#39;</span><span class="p">)</span> <span class="c1"># help msg</span>
  295. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;targets&#39;</span><span class="p">,</span>
  296. <span class="n">metavar</span> <span class="o">=</span> <span class="s1">&#39;HOSTNAME&#39;</span><span class="p">,</span> <span class="c1"># var name used in help msg</span>
  297. <span class="n">nargs</span> <span class="o">=</span> <span class="s1">&#39;+&#39;</span><span class="p">,</span> <span class="c1"># require one or more targets</span>
  298. <span class="n">help</span> <span class="o">=</span> <span class="s1">&#39;url for target machines&#39;</span><span class="p">)</span> <span class="c1"># help msg explanation</span>
  299. <span class="n">parser</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-u&#39;</span><span class="p">,</span> <span class="s1">&#39;--user&#39;</span><span class="p">,</span> <span class="c1"># -u or --user option</span>
  300. <span class="n">required</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="c1"># make it a required argument</span>
  301. <span class="n">help</span> <span class="o">=</span> <span class="s1">&#39;login as user&#39;</span><span class="p">)</span>
  302. </pre></div>
  303. </div>
  304. <p>Example of calling the parser on a command string:</p>
  305. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">cmd</span> <span class="o">=</span> <span class="s1">&#39;deploy sneezy.example.com sleepy.example.com -u skycaptain&#39;</span>
  306. <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">cmd</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
  307. <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">action</span>
  308. <span class="go">&#39;deploy&#39;</span>
  309. <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">targets</span>
  310. <span class="go">[&#39;sneezy.example.com&#39;, &#39;sleepy.example.com&#39;]</span>
  311. <span class="gp">&gt;&gt;&gt; </span><span class="n">result</span><span class="o">.</span><span class="n">user</span>
  312. <span class="go">&#39;skycaptain&#39;</span>
  313. </pre></div>
  314. </div>
  315. <p>Example of the parser’s automatically generated help:</p>
  316. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="s1">&#39;-h&#39;</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
  317. <span class="go">usage: manage_cloud.py [-h] -u USER</span>
  318. <span class="go"> {deploy,start,stop} HOSTNAME [HOSTNAME ...]</span>
  319. <span class="go">Manage servers</span>
  320. <span class="go">positional arguments:</span>
  321. <span class="go"> {deploy,start,stop} action on each target</span>
  322. <span class="go"> HOSTNAME url for target machines</span>
  323. <span class="go">optional arguments:</span>
  324. <span class="go"> -h, --help show this help message and exit</span>
  325. <span class="go"> -u USER, --user USER login as user</span>
  326. <span class="go">Tested on Solaris and Linux</span>
  327. </pre></div>
  328. </div>
  329. <p>An especially nice <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> feature is the ability to define subparsers,
  330. each with their own argument patterns and help displays:</p>
  331. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">argparse</span>
  332. <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">prog</span><span class="o">=</span><span class="s1">&#39;HELM&#39;</span><span class="p">)</span>
  333. <span class="n">subparsers</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_subparsers</span><span class="p">()</span>
  334. <span class="n">parser_l</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s1">&#39;launch&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Launch Control&#39;</span><span class="p">)</span> <span class="c1"># first subgroup</span>
  335. <span class="n">parser_l</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-m&#39;</span><span class="p">,</span> <span class="s1">&#39;--missiles&#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>
  336. <span class="n">parser_l</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-t&#39;</span><span class="p">,</span> <span class="s1">&#39;--torpedos&#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>
  337. <span class="n">parser_m</span> <span class="o">=</span> <span class="n">subparsers</span><span class="o">.</span><span class="n">add_parser</span><span class="p">(</span><span class="s1">&#39;move&#39;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s1">&#39;Move Vessel&#39;</span><span class="p">,</span> <span class="c1"># second subgroup</span>
  338. <span class="n">aliases</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;steer&#39;</span><span class="p">,</span> <span class="s1">&#39;turn&#39;</span><span class="p">))</span> <span class="c1"># equivalent names</span>
  339. <span class="n">parser_m</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="s1">&#39;--course&#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">required</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  340. <span class="n">parser_m</span><span class="o">.</span><span class="n">add_argument</span><span class="p">(</span><span class="s1">&#39;-s&#39;</span><span class="p">,</span> <span class="s1">&#39;--speed&#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">default</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  341. </pre></div>
  342. </div>
  343. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>./helm.py<span class="w"> </span>--help<span class="w"> </span><span class="c1"># top level help (launch and move)</span>
  344. <span class="gp">$ </span>./helm.py<span class="w"> </span>launch<span class="w"> </span>--help<span class="w"> </span><span class="c1"># help for launch options</span>
  345. <span class="gp">$ </span>./helm.py<span class="w"> </span>launch<span class="w"> </span>--missiles<span class="w"> </span><span class="c1"># set missiles=True and torpedos=False</span>
  346. <span class="gp">$ </span>./helm.py<span class="w"> </span>steer<span class="w"> </span>--course<span class="w"> </span><span class="m">180</span><span class="w"> </span>--speed<span class="w"> </span><span class="m">5</span><span class="w"> </span><span class="c1"># set movement parameters</span>
  347. </pre></div>
  348. </div>
  349. <div class="admonition seealso">
  350. <p class="admonition-title">See also</p>
  351. <dl class="simple">
  352. <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0389/"><strong>PEP 389</strong></a> - New Command Line Parsing Module</dt><dd><p>PEP written by Steven Bethard.</p>
  353. </dd>
  354. </dl>
  355. <p><a class="reference internal" href="../library/argparse.html#upgrading-optparse-code"><span class="std std-ref">Upgrading optparse code</span></a> for details on the differences from <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>
  356. </div>
  357. </section>
  358. <section id="pep-391-dictionary-based-configuration-for-logging">
  359. <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>
  360. <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 provided two kinds of configuration, one style with
  361. function calls for each option or another style driven by an external file saved
  362. in a <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> format. Those options did not provide the flexibility
  363. to create configurations from JSON or YAML files, nor did they support
  364. incremental configuration, which is needed for specifying logger options from a
  365. command line.</p>
  366. <p>To support a more flexible style, the module now offers
  367. <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">logging.config.dictConfig()</span></code></a> for specifying logging configuration with
  368. plain Python dictionaries. The configuration options include formatters,
  369. handlers, filters, and loggers. Here’s a working example of a configuration
  370. dictionary:</p>
  371. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">{</span><span class="s2">&quot;version&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
  372. <span class="s2">&quot;formatters&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;brief&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;format&quot;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%(levelname)-8s</span><span class="s2">: </span><span class="si">%(name)-15s</span><span class="s2">: </span><span class="si">%(message)s</span><span class="s2">&quot;</span><span class="p">},</span>
  373. <span class="s2">&quot;full&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;format&quot;</span><span class="p">:</span> <span class="s2">&quot;</span><span class="si">%(asctime)s</span><span class="s2"> </span><span class="si">%(name)-15s</span><span class="s2"> </span><span class="si">%(levelname)-8s</span><span class="s2"> </span><span class="si">%(message)s</span><span class="s2">&quot;</span><span class="p">}</span>
  374. <span class="p">},</span>
  375. <span class="s2">&quot;handlers&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;console&quot;</span><span class="p">:</span> <span class="p">{</span>
  376. <span class="s2">&quot;class&quot;</span><span class="p">:</span> <span class="s2">&quot;logging.StreamHandler&quot;</span><span class="p">,</span>
  377. <span class="s2">&quot;formatter&quot;</span><span class="p">:</span> <span class="s2">&quot;brief&quot;</span><span class="p">,</span>
  378. <span class="s2">&quot;level&quot;</span><span class="p">:</span> <span class="s2">&quot;INFO&quot;</span><span class="p">,</span>
  379. <span class="s2">&quot;stream&quot;</span><span class="p">:</span> <span class="s2">&quot;ext://sys.stdout&quot;</span><span class="p">},</span>
  380. <span class="s2">&quot;console_priority&quot;</span><span class="p">:</span> <span class="p">{</span>
  381. <span class="s2">&quot;class&quot;</span><span class="p">:</span> <span class="s2">&quot;logging.StreamHandler&quot;</span><span class="p">,</span>
  382. <span class="s2">&quot;formatter&quot;</span><span class="p">:</span> <span class="s2">&quot;full&quot;</span><span class="p">,</span>
  383. <span class="s2">&quot;level&quot;</span><span class="p">:</span> <span class="s2">&quot;ERROR&quot;</span><span class="p">,</span>
  384. <span class="s2">&quot;stream&quot;</span><span class="p">:</span> <span class="s2">&quot;ext://sys.stderr&quot;</span><span class="p">}</span>
  385. <span class="p">},</span>
  386. <span class="s2">&quot;root&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;level&quot;</span><span class="p">:</span> <span class="s2">&quot;DEBUG&quot;</span><span class="p">,</span> <span class="s2">&quot;handlers&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;console&quot;</span><span class="p">,</span> <span class="s2">&quot;console_priority&quot;</span><span class="p">]}}</span>
  387. </pre></div>
  388. </div>
  389. <p>If that dictionary is stored in a file called <code class="file docutils literal notranslate"><span class="pre">conf.json</span></code>, it can be
  390. loaded and called with code like this:</p>
  391. <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">json</span><span class="o">,</span> <span class="nn">logging.config</span>
  392. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;conf.json&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  393. <span class="gp">... </span> <span class="n">conf</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
  394. <span class="gp">...</span>
  395. <span class="gp">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">dictConfig</span><span class="p">(</span><span class="n">conf</span><span class="p">)</span>
  396. <span class="gp">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Transaction completed normally&quot;</span><span class="p">)</span>
  397. <span class="go">INFO : root : Transaction completed normally</span>
  398. <span class="gp">&gt;&gt;&gt; </span><span class="n">logging</span><span class="o">.</span><span class="n">critical</span><span class="p">(</span><span class="s2">&quot;Abnormal termination&quot;</span><span class="p">)</span>
  399. <span class="go">2011-02-17 11:14:36,694 root CRITICAL Abnormal termination</span>
  400. </pre></div>
  401. </div>
  402. <div class="admonition seealso">
  403. <p class="admonition-title">See also</p>
  404. <dl class="simple">
  405. <dt><span class="target" id="index-3"></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 by Vinay Sajip.</p>
  406. </dd>
  407. </dl>
  408. </div>
  409. </section>
  410. <section id="pep-3148-the-concurrent-futures-module">
  411. <h2>PEP 3148: The <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> module<a class="headerlink" href="#pep-3148-the-concurrent-futures-module" title="Link to this heading">¶</a></h2>
  412. <p>Code for creating and managing concurrency is being collected in a new top-level
  413. namespace, <em>concurrent</em>. Its first member is a <em>futures</em> package which provides
  414. a uniform high-level interface for managing threads and processes.</p>
  415. <p>The design for <a class="reference internal" href="../library/concurrent.futures.html#module-concurrent.futures" title="concurrent.futures: Execute computations concurrently using threads or processes."><code class="xref py py-mod docutils literal notranslate"><span class="pre">concurrent.futures</span></code></a> was inspired by the
  416. <em>java.util.concurrent</em> package. In that model, a running call and its result
  417. are represented by a <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object that abstracts
  418. features common to threads, processes, and remote procedure calls. That object
  419. supports status checks (running or done), timeouts, cancellations, adding
  420. callbacks, and access to results or exceptions.</p>
  421. <p>The primary offering of the new module is a pair of executor classes for
  422. launching and managing calls. The goal of the executors is to make it easier to
  423. use existing tools for making parallel calls. They save the effort needed to
  424. setup a pool of resources, launch the calls, create a results queue, add
  425. time-out handling, and limit the total number of threads, processes, or remote
  426. procedure calls.</p>
  427. <p>Ideally, each application should share a single executor across multiple
  428. components so that process and thread limits can be centrally managed. This
  429. solves the design challenge that arises when each component has its own
  430. competing strategy for resource management.</p>
  431. <p>Both classes share a common interface with three methods:
  432. <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.submit" title="concurrent.futures.Executor.submit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">submit()</span></code></a> for scheduling a callable and
  433. returning a <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Future" title="concurrent.futures.Future"><code class="xref py py-class docutils literal notranslate"><span class="pre">Future</span></code></a> object;
  434. <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.map" title="concurrent.futures.Executor.map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">map()</span></code></a> for scheduling many asynchronous calls
  435. at a time, and <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.Executor.shutdown" title="concurrent.futures.Executor.shutdown"><code class="xref py py-meth docutils literal notranslate"><span class="pre">shutdown()</span></code></a> for freeing
  436. resources. The class is a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> and can be used in a
  437. <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 to assure that resources are automatically released
  438. when currently pending futures are done executing.</p>
  439. <p>A simple of example of <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor" title="concurrent.futures.ThreadPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadPoolExecutor</span></code></a> is a
  440. launch of four parallel threads for copying files:</p>
  441. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">concurrent.futures</span><span class="o">,</span> <span class="nn">shutil</span>
  442. <span class="k">with</span> <span class="n">concurrent</span><span class="o">.</span><span class="n">futures</span><span class="o">.</span><span class="n">ThreadPoolExecutor</span><span class="p">(</span><span class="n">max_workers</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  443. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src1.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest1.txt&#39;</span><span class="p">)</span>
  444. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src2.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest2.txt&#39;</span><span class="p">)</span>
  445. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src3.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest3.txt&#39;</span><span class="p">)</span>
  446. <span class="n">e</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">,</span> <span class="s1">&#39;src3.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;dest4.txt&#39;</span><span class="p">)</span>
  447. </pre></div>
  448. </div>
  449. <div class="admonition seealso">
  450. <p class="admonition-title">See also</p>
  451. <dl class="simple">
  452. <dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://peps.python.org/pep-3148/"><strong>PEP 3148</strong></a> - Futures – Execute Computations Asynchronously</dt><dd><p>PEP written by Brian Quinlan.</p>
  453. </dd>
  454. </dl>
  455. <p><a class="reference internal" href="../library/concurrent.futures.html#threadpoolexecutor-example"><span class="std std-ref">Code for Threaded Parallel URL reads</span></a>, an
  456. example using threads to fetch multiple web pages in parallel.</p>
  457. <p><a class="reference internal" href="../library/concurrent.futures.html#processpoolexecutor-example"><span class="std std-ref">Code for computing prime numbers in
  458. parallel</span></a>, an example demonstrating
  459. <a class="reference internal" href="../library/concurrent.futures.html#concurrent.futures.ProcessPoolExecutor" title="concurrent.futures.ProcessPoolExecutor"><code class="xref py py-class docutils literal notranslate"><span class="pre">ProcessPoolExecutor</span></code></a>.</p>
  460. </div>
  461. </section>
  462. <section id="pep-3147-pyc-repository-directories">
  463. <h2>PEP 3147: PYC Repository Directories<a class="headerlink" href="#pep-3147-pyc-repository-directories" title="Link to this heading">¶</a></h2>
  464. <p>Python’s scheme for caching bytecode in <em>.pyc</em> files did not work well in
  465. environments with multiple Python interpreters. If one interpreter encountered
  466. a cached file created by another interpreter, it would recompile the source and
  467. overwrite the cached file, thus losing the benefits of caching.</p>
  468. <p>The issue of “pyc fights” has become more pronounced as it has become
  469. commonplace for Linux distributions to ship with multiple versions of Python.
  470. These conflicts also arise with CPython alternatives such as Unladen Swallow.</p>
  471. <p>To solve this problem, Python’s import machinery has been extended to use
  472. distinct filenames for each interpreter. Instead of Python 3.2 and Python 3.3 and
  473. Unladen Swallow each competing for a file called “mymodule.pyc”, they will now
  474. look for “mymodule.cpython-32.pyc”, “mymodule.cpython-33.pyc”, and
  475. “mymodule.unladen10.pyc”. And to prevent all of these new files from
  476. cluttering source directories, the <em>pyc</em> files are now collected in a
  477. “__pycache__” directory stored under the package directory.</p>
  478. <p>Aside from the filenames and target directories, the new scheme has a few
  479. aspects that are visible to the programmer:</p>
  480. <ul>
  481. <li><p>Imported modules now have a <a class="reference internal" href="../reference/import.html#cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a> attribute which stores the name
  482. of the actual file that was imported:</p>
  483. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">collections</span>
  484. <span class="gp">&gt;&gt;&gt; </span><span class="n">collections</span><span class="o">.</span><span class="n">__cached__</span>
  485. <span class="go">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span>
  486. </pre></div>
  487. </div>
  488. </li>
  489. <li><p>The tag that is unique to each interpreter is accessible from the <code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code>
  490. module:</p>
  491. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">imp</span>
  492. <span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">get_tag</span><span class="p">()</span>
  493. <span class="go">&#39;cpython-32&#39;</span>
  494. </pre></div>
  495. </div>
  496. </li>
  497. <li><p>Scripts that try to deduce source filename from the imported file now need to
  498. be smarter. It is no longer sufficient to simply strip the “c” from a “.pyc”
  499. filename. Instead, use the new functions in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">imp</span></code> module:</p>
  500. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">source_from_cache</span><span class="p">(</span><span class="s1">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span><span class="p">)</span>
  501. <span class="go">&#39;c:/py32/lib/collections.py&#39;</span>
  502. <span class="gp">&gt;&gt;&gt; </span><span class="n">imp</span><span class="o">.</span><span class="n">cache_from_source</span><span class="p">(</span><span class="s1">&#39;c:/py32/lib/collections.py&#39;</span><span class="p">)</span>
  503. <span class="go">&#39;c:/py32/lib/__pycache__/collections.cpython-32.pyc&#39;</span>
  504. </pre></div>
  505. </div>
  506. </li>
  507. <li><p>The <a class="reference internal" href="../library/py_compile.html#module-py_compile" title="py_compile: Generate byte-code files from Python source files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">py_compile</span></code></a> and <a class="reference internal" href="../library/compileall.html#module-compileall" title="compileall: Tools for byte-compiling all Python source files in a directory tree."><code class="xref py py-mod docutils literal notranslate"><span class="pre">compileall</span></code></a> modules have been updated to
  508. reflect the new naming convention and target directory. The command-line
  509. invocation of <em>compileall</em> has new options: <code class="docutils literal notranslate"><span class="pre">-i</span></code> for
  510. specifying a list of files and directories to compile and <code class="docutils literal notranslate"><span class="pre">-b</span></code> which causes
  511. bytecode files to be written to their legacy location rather than
  512. <em>__pycache__</em>.</p></li>
  513. <li><p>The <a class="reference internal" href="../library/importlib.html#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> module has been updated with new <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base
  514. classes</span></a> for loading bytecode files. The obsolete
  515. ABCs, <code class="xref py py-class docutils literal notranslate"><span class="pre">PyLoader</span></code> and
  516. <code class="xref py py-class docutils literal notranslate"><span class="pre">PyPycLoader</span></code>, have been deprecated (instructions on how
  517. to stay Python 3.1 compatible are included with the documentation).</p></li>
  518. </ul>
  519. <div class="admonition seealso">
  520. <p class="admonition-title">See also</p>
  521. <dl class="simple">
  522. <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://peps.python.org/pep-3147/"><strong>PEP 3147</strong></a> - PYC Repository Directories</dt><dd><p>PEP written by Barry Warsaw.</p>
  523. </dd>
  524. </dl>
  525. </div>
  526. </section>
  527. <section id="pep-3149-abi-version-tagged-so-files">
  528. <h2>PEP 3149: ABI Version Tagged .so Files<a class="headerlink" href="#pep-3149-abi-version-tagged-so-files" title="Link to this heading">¶</a></h2>
  529. <p>The PYC repository directory allows multiple bytecode cache files to be
  530. co-located. This PEP implements a similar mechanism for shared object files by
  531. giving them a common directory and distinct names for each version.</p>
  532. <p>The common directory is “pyshared” and the file names are made distinct by
  533. identifying the Python implementation (such as CPython, PyPy, Jython, etc.), the
  534. major and minor version numbers, and optional build flags (such as “d” for
  535. debug, “m” for pymalloc, “u” for wide-unicode). For an arbitrary package “foo”,
  536. you may see these files when the distribution package is installed:</p>
  537. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">share</span><span class="o">/</span><span class="n">pyshared</span><span class="o">/</span><span class="n">foo</span><span class="o">.</span><span class="n">cpython</span><span class="o">-</span><span class="mi">32</span><span class="n">m</span><span class="o">.</span><span class="n">so</span>
  538. <span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">share</span><span class="o">/</span><span class="n">pyshared</span><span class="o">/</span><span class="n">foo</span><span class="o">.</span><span class="n">cpython</span><span class="o">-</span><span class="mi">33</span><span class="n">md</span><span class="o">.</span><span class="n">so</span>
  539. </pre></div>
  540. </div>
  541. <p>In Python itself, the tags are accessible from functions in 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>
  542. module:</p>
  543. <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">sysconfig</span>
  544. <span class="gp">&gt;&gt;&gt; </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s1">&#39;SOABI&#39;</span><span class="p">)</span> <span class="c1"># find the version tag</span>
  545. <span class="go">&#39;cpython-32mu&#39;</span>
  546. <span class="gp">&gt;&gt;&gt; </span><span class="n">sysconfig</span><span class="o">.</span><span class="n">get_config_var</span><span class="p">(</span><span class="s1">&#39;EXT_SUFFIX&#39;</span><span class="p">)</span> <span class="c1"># find the full filename extension</span>
  547. <span class="go">&#39;.cpython-32mu.so&#39;</span>
  548. </pre></div>
  549. </div>
  550. <div class="admonition seealso">
  551. <p class="admonition-title">See also</p>
  552. <dl class="simple">
  553. <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-3149/"><strong>PEP 3149</strong></a> - ABI Version Tagged .so Files</dt><dd><p>PEP written by Barry Warsaw.</p>
  554. </dd>
  555. </dl>
  556. </div>
  557. </section>
  558. <section id="pep-3333-python-web-server-gateway-interface-v1-0-1">
  559. <h2>PEP 3333: Python Web Server Gateway Interface v1.0.1<a class="headerlink" href="#pep-3333-python-web-server-gateway-interface-v1-0-1" title="Link to this heading">¶</a></h2>
  560. <p>This informational PEP clarifies how bytes/text issues are to be handled by the
  561. WSGI protocol. The challenge is that string handling in Python 3 is most
  562. conveniently handled with the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type even though the HTTP protocol
  563. is itself bytes oriented.</p>
  564. <p>The PEP differentiates so-called <em>native strings</em> that are used for
  565. request/response headers and metadata versus <em>byte strings</em> which are used for
  566. the bodies of requests and responses.</p>
  567. <p>The <em>native strings</em> are always of type <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> but are restricted to code
  568. points between <em>U+0000</em> through <em>U+00FF</em> which are translatable to bytes using
  569. <em>Latin-1</em> encoding. These strings are used for the keys and values in the
  570. environment dictionary and for response headers and statuses in the
  571. <code class="xref py py-func docutils literal notranslate"><span class="pre">start_response()</span></code> function. They must follow <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2616.html"><strong>RFC 2616</strong></a> with respect to
  572. encoding. That is, they must either be <em>ISO-8859-1</em> characters or use
  573. <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a> MIME encoding.</p>
  574. <p>For developers porting WSGI applications from Python 2, here are the salient
  575. points:</p>
  576. <ul class="simple">
  577. <li><p>If the app already used strings for headers in Python 2, no change is needed.</p></li>
  578. <li><p>If instead, the app encoded output headers or decoded input headers, then the
  579. headers will need to be re-encoded to Latin-1. For example, an output header
  580. encoded in utf-8 was using <code class="docutils literal notranslate"><span class="pre">h.encode('utf-8')</span></code> now needs to convert from
  581. bytes to native strings using <code class="docutils literal notranslate"><span class="pre">h.encode('utf-8').decode('latin-1')</span></code>.</p></li>
  582. <li><p>Values yielded by an application or sent using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">write()</span></code> method
  583. must be byte strings. The <code class="xref py py-func docutils literal notranslate"><span class="pre">start_response()</span></code> function and environ
  584. must use native strings. The two cannot be mixed.</p></li>
  585. </ul>
  586. <p>For server implementers writing CGI-to-WSGI pathways or other CGI-style
  587. protocols, the users must to be able access the environment using native strings
  588. even though the underlying platform may have a different convention. To bridge
  589. this gap, the <a class="reference internal" href="../library/wsgiref.html#module-wsgiref" title="wsgiref: WSGI Utilities and Reference Implementation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">wsgiref</span></code></a> module has a new function,
  590. <a class="reference internal" href="../library/wsgiref.html#wsgiref.handlers.read_environ" title="wsgiref.handlers.read_environ"><code class="xref py py-func docutils literal notranslate"><span class="pre">wsgiref.handlers.read_environ()</span></code></a> for transcoding CGI variables from
  591. <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a> into native strings and returning a new dictionary.</p>
  592. <div class="admonition seealso">
  593. <p class="admonition-title">See also</p>
  594. <dl class="simple">
  595. <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-3333/"><strong>PEP 3333</strong></a> - Python Web Server Gateway Interface v1.0.1</dt><dd><p>PEP written by Phillip Eby.</p>
  596. </dd>
  597. </dl>
  598. </div>
  599. </section>
  600. <section id="other-language-changes">
  601. <h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Link to this heading">¶</a></h2>
  602. <p>Some smaller changes made to the core Python language are:</p>
  603. <ul>
  604. <li><p>String formatting for <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> and <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> gained new
  605. capabilities for the format character <strong>#</strong>. Previously, for integers in
  606. binary, octal, or hexadecimal, it caused the output to be prefixed with ‘0b’,
  607. ‘0o’, or ‘0x’ respectively. Now it can also handle floats, complex, and
  608. Decimal, causing the output to always have a decimal point even when no digits
  609. follow it.</p>
  610. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="s1">&#39;#o&#39;</span><span class="p">)</span>
  611. <span class="go">&#39;0o24&#39;</span>
  612. <span class="gp">&gt;&gt;&gt; </span><span class="nb">format</span><span class="p">(</span><span class="mf">12.34</span><span class="p">,</span> <span class="s1">&#39;#5.0f&#39;</span><span class="p">)</span>
  613. <span class="go">&#39; 12.&#39;</span>
  614. </pre></div>
  615. </div>
  616. <p>(Suggested by Mark Dickinson and implemented by Eric Smith in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7094">bpo-7094</a>.)</p>
  617. </li>
  618. <li><p>There is also a new <a class="reference internal" href="../library/stdtypes.html#str.format_map" title="str.format_map"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format_map()</span></code></a> method that extends the
  619. capabilities of the existing <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 by accepting arbitrary
  620. <a class="reference internal" href="../glossary.html#term-mapping"><span class="xref std std-term">mapping</span></a> objects. This new method makes it possible to use string
  621. formatting with any of Python’s many dictionary-like objects such as
  622. <a class="reference internal" href="../library/collections.html#collections.defaultdict" title="collections.defaultdict"><code class="xref py py-class docutils literal notranslate"><span class="pre">defaultdict</span></code></a>, <a class="reference internal" href="../library/shelve.html#shelve.Shelf" title="shelve.Shelf"><code class="xref py py-class docutils literal notranslate"><span class="pre">Shelf</span></code></a>,
  623. <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a>, or <a class="reference internal" href="../library/dbm.html#module-dbm" title="dbm: Interfaces to various Unix &quot;database&quot; formats."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dbm</span></code></a>. It is also useful with
  624. custom <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> subclasses that normalize keys before look-up or that
  625. supply a <code class="xref py py-meth docutils literal notranslate"><span class="pre">__missing__()</span></code> method for unknown keys:</p>
  626. <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">shelve</span>
  627. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">shelve</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;tmp.shl&#39;</span><span class="p">)</span>
  628. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;The </span><span class="si">{project_name}</span><span class="s1"> status is </span><span class="si">{status}</span><span class="s1"> as of </span><span class="si">{date}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  629. <span class="go">&#39;The testing project status is green as of February 15, 2011&#39;</span>
  630. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">LowerCasedDict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
  631. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
  632. <span class="gp">... </span> <span class="k">return</span> <span class="nb">dict</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
  633. <span class="gp">...</span>
  634. <span class="gp">&gt;&gt;&gt; </span><span class="n">lcd</span> <span class="o">=</span> <span class="n">LowerCasedDict</span><span class="p">(</span><span class="n">part</span><span class="o">=</span><span class="s1">&#39;widgets&#39;</span><span class="p">,</span> <span class="n">quantity</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
  635. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;There are </span><span class="si">{QUANTITY}</span><span class="s1"> </span><span class="si">{Part}</span><span class="s1"> in stock&#39;</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">lcd</span><span class="p">)</span>
  636. <span class="go">&#39;There are 10 widgets in stock&#39;</span>
  637. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">PlaceholderDict</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
  638. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__missing__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
  639. <span class="gp">... </span> <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">{}</span><span class="s1">&gt;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  640. <span class="gp">...</span>
  641. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;Hello </span><span class="si">{name}</span><span class="s1">, welcome to </span><span class="si">{location}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format_map</span><span class="p">(</span><span class="n">PlaceholderDict</span><span class="p">())</span>
  642. <span class="go">&#39;Hello &lt;name&gt;, welcome to &lt;location&gt;&#39;</span>
  643. </pre></div>
  644. </div>
  645. </li>
  646. </ul>
  647. <blockquote>
  648. <div><p>(Suggested by Raymond Hettinger and implemented by Eric Smith in
  649. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6081">bpo-6081</a>.)</p>
  650. </div></blockquote>
  651. <ul>
  652. <li><p>The interpreter can now be started with a quiet option, <code class="docutils literal notranslate"><span class="pre">-q</span></code>, to prevent
  653. the copyright and version information from being displayed in the interactive
  654. mode. The option can be introspected using the <a class="reference internal" href="../library/sys.html#sys.flags" title="sys.flags"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.flags</span></code></a> attribute:</p>
  655. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-q
  656. <span class="go">&gt;&gt;&gt; sys.flags</span>
  657. <span class="go">sys.flags(debug=0, division_warning=0, inspect=0, interactive=0,</span>
  658. <span class="go">optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,</span>
  659. <span class="go">ignore_environment=0, verbose=0, bytes_warning=0, quiet=1)</span>
  660. </pre></div>
  661. </div>
  662. <p>(Contributed by Marcin Wojdyr in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1772833">bpo-1772833</a>).</p>
  663. </li>
  664. <li><p>The <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> function works by calling <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> and detecting
  665. whether an exception is raised. This technique allows it to detect methods
  666. created dynamically by <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code></a> which
  667. would otherwise be absent from the class dictionary. Formerly, <em>hasattr</em>
  668. would catch any exception, possibly masking genuine errors. Now, <em>hasattr</em>
  669. has been tightened to only catch <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> and let other
  670. exceptions pass through:</p>
  671. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  672. <span class="gp">... </span> <span class="nd">@property</span>
  673. <span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  674. <span class="gp">... </span> <span class="k">return</span> <span class="mi">1</span> <span class="o">//</span> <span class="mi">0</span>
  675. <span class="gp">...</span>
  676. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
  677. <span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">&#39;f&#39;</span><span class="p">)</span>
  678. <span class="gt">Traceback (most recent call last):</span>
  679. <span class="w"> </span><span class="c">...</span>
  680. <span class="gr">ZeroDivisionError</span>: <span class="n">integer division or modulo by zero</span>
  681. </pre></div>
  682. </div>
  683. <p>(Discovered by Yury Selivanov and fixed by Benjamin Peterson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9666">bpo-9666</a>.)</p>
  684. </li>
  685. <li><p>The <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> of a float or complex number is now the same as its
  686. <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>. Previously, the <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> form was shorter but that just
  687. caused confusion and is no longer needed now that the shortest possible
  688. <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> is displayed by default:</p>
  689. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">math</span>
  690. <span class="gp">&gt;&gt;&gt; </span><span class="nb">repr</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
  691. <span class="go">&#39;3.141592653589793&#39;</span>
  692. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
  693. <span class="go">&#39;3.141592653589793&#39;</span>
  694. </pre></div>
  695. </div>
  696. <p>(Proposed and implemented by Mark Dickinson; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9337">bpo-9337</a>.)</p>
  697. </li>
  698. <li><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 now have a <a class="reference internal" href="../library/stdtypes.html#memoryview.release" title="memoryview.release"><code class="xref py py-meth docutils literal notranslate"><span class="pre">release()</span></code></a> method
  699. and they also now support the context management protocol. This allows timely
  700. release of any resources that were acquired when requesting a buffer from the
  701. original object.</p>
  702. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">memoryview</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;abcdefgh&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">v</span><span class="p">:</span>
  703. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">tolist</span><span class="p">())</span>
  704. <span class="go">[97, 98, 99, 100, 101, 102, 103, 104]</span>
  705. </pre></div>
  706. </div>
  707. <p>(Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9757">bpo-9757</a>.)</p>
  708. </li>
  709. <li><p>Previously it was illegal to delete a name from the local namespace if it
  710. occurs as a free variable in a nested block:</p>
  711. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">outer</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
  712. <span class="k">def</span> <span class="nf">inner</span><span class="p">():</span>
  713. <span class="k">return</span> <span class="n">x</span>
  714. <span class="n">inner</span><span class="p">()</span>
  715. <span class="k">del</span> <span class="n">x</span>
  716. </pre></div>
  717. </div>
  718. <p>This is now allowed. Remember that the target of an <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> clause
  719. is cleared, so this code which used to work with Python 2.6, raised a
  720. <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> with Python 3.1 and now works again:</p>
  721. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span>
  722. <span class="k">def</span> <span class="nf">print_error</span><span class="p">():</span>
  723. <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
  724. <span class="k">try</span><span class="p">:</span>
  725. <span class="n">something</span>
  726. <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  727. <span class="n">print_error</span><span class="p">()</span>
  728. <span class="c1"># implicit &quot;del e&quot; here</span>
  729. </pre></div>
  730. </div>
  731. <p>(See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4617">bpo-4617</a>.)</p>
  732. </li>
  733. <li><p><a class="reference internal" href="../c-api/tuple.html#struct-sequence-objects"><span class="std std-ref">Struct sequence types</span></a> are now subclasses of tuple.
  734. This means that C structures like those returned by <a class="reference internal" href="../library/os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a>,
  735. <a class="reference internal" href="../library/time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a>, and <a class="reference internal" href="../library/sys.html#sys.version_info" title="sys.version_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version_info</span></code></a> now work like a
  736. <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> and now work with functions and methods that
  737. expect a tuple as an argument. This is a big step forward in making the C
  738. structures as flexible as their pure Python counterparts:</p>
  739. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
  740. <span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)</span>
  741. <span class="go">True</span>
  742. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;Version </span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1">.</span><span class="si">%d</span><span class="s1"> </span><span class="si">%s</span><span class="s1">(</span><span class="si">%d</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span>
  743. <span class="go">&#39;Version 3.2.0 final(0)&#39;</span>
  744. </pre></div>
  745. </div>
  746. <p>(Suggested by Arfrever Frehtes Taifersar Arahesis and implemented
  747. by Benjamin Peterson in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8413">bpo-8413</a>.)</p>
  748. </li>
  749. <li><p>Warnings are now easier to control using the <span class="target" id="index-10"></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>
  750. environment variable as an alternative to using <code class="docutils literal notranslate"><span class="pre">-W</span></code> at the command line:</p>
  751. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nb">export</span><span class="w"> </span><span class="nv">PYTHONWARNINGS</span><span class="o">=</span><span class="s1">&#39;ignore::RuntimeWarning::,once::UnicodeWarning::&#39;</span>
  752. </pre></div>
  753. </div>
  754. <p>(Suggested by Barry Warsaw and implemented by Philip Jenvey in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7301">bpo-7301</a>.)</p>
  755. </li>
  756. <li><p>A new warning category, <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a>, has been added. It is
  757. emitted when potential issues with resource consumption or cleanup
  758. are detected. It is silenced by default in normal release builds but
  759. can be enabled through the means provided by the <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><code class="xref py py-mod docutils literal notranslate"><span class="pre">warnings</span></code></a>
  760. module, or on the command line.</p>
  761. <p>A <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> is issued at interpreter shutdown if the
  762. <a class="reference internal" href="../library/gc.html#gc.garbage" title="gc.garbage"><code class="xref py py-data docutils literal notranslate"><span class="pre">gc.garbage</span></code></a> list isn’t empty, and if <a class="reference internal" href="../library/gc.html#gc.DEBUG_UNCOLLECTABLE" title="gc.DEBUG_UNCOLLECTABLE"><code class="xref py py-const docutils literal notranslate"><span class="pre">gc.DEBUG_UNCOLLECTABLE</span></code></a> is
  763. set, all uncollectable objects are printed. This is meant to make the
  764. programmer aware that their code contains object finalization issues.</p>
  765. <p>A <a class="reference internal" href="../library/exceptions.html#ResourceWarning" title="ResourceWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ResourceWarning</span></code></a> is also issued when a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> is destroyed
  766. without having been explicitly closed. While the deallocator for such
  767. object ensures it closes the underlying operating system resource
  768. (usually, a file descriptor), the delay in deallocating the object could
  769. produce various issues, especially under Windows. Here is an example
  770. of enabling the warning from the command line:</p>
  771. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-q<span class="w"> </span>-Wdefault
  772. <span class="go">&gt;&gt;&gt; f = open(&quot;foo&quot;, &quot;wb&quot;)</span>
  773. <span class="go">&gt;&gt;&gt; del f</span>
  774. <span class="go">__main__:1: ResourceWarning: unclosed file &lt;_io.BufferedWriter name=&#39;foo&#39;&gt;</span>
  775. </pre></div>
  776. </div>
  777. <p>(Added by Antoine Pitrou and Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10093">bpo-10093</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=477863">bpo-477863</a>.)</p>
  778. </li>
  779. <li><p><a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> objects now support <em>index</em> and <em>count</em> methods. This is part
  780. of an effort to make more objects fully implement the
  781. <a class="reference internal" href="../library/collections.abc.html#collections.abc.Sequence" title="collections.abc.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.Sequence</span></code></a> <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a>. As a result, the
  782. language will have a more uniform API. In addition, <a class="reference internal" href="../library/stdtypes.html#range" title="range"><code class="xref py py-class docutils literal notranslate"><span class="pre">range</span></code></a> objects
  783. now support slicing and negative indices, even with values larger than
  784. <a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>. This makes <em>range</em> more interoperable with lists:</p>
  785. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
  786. <span class="go">1</span>
  787. <span class="gp">&gt;&gt;&gt; </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
  788. <span class="go">5</span>
  789. <span class="gp">&gt;&gt;&gt; </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">5</span><span class="p">]</span>
  790. <span class="go">10</span>
  791. <span class="gp">&gt;&gt;&gt; </span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">2</span><span class="p">)[</span><span class="mi">0</span><span class="p">:</span><span class="mi">5</span><span class="p">]</span>
  792. <span class="go">range(0, 10, 2)</span>
  793. </pre></div>
  794. </div>
  795. <p>(Contributed by Daniel Stutzbach in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9213">bpo-9213</a>, by Alexander Belopolsky
  796. in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2690">bpo-2690</a>, and by Nick Coghlan in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10889">bpo-10889</a>.)</p>
  797. </li>
  798. <li><p>The <a class="reference internal" href="../library/functions.html#callable" title="callable"><code class="xref py py-func docutils literal notranslate"><span class="pre">callable()</span></code></a> builtin function from Py2.x was resurrected. It provides
  799. a concise, readable alternative to using an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> in an
  800. expression like <code class="docutils literal notranslate"><span class="pre">isinstance(x,</span> <span class="pre">collections.Callable)</span></code>:</p>
  801. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">callable</span><span class="p">(</span><span class="nb">max</span><span class="p">)</span>
  802. <span class="go">True</span>
  803. <span class="gp">&gt;&gt;&gt; </span><span class="nb">callable</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
  804. <span class="go">False</span>
  805. </pre></div>
  806. </div>
  807. <p>(See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10518">bpo-10518</a>.)</p>
  808. </li>
  809. <li><p>Python’s import mechanism can now load modules installed in directories with
  810. non-ASCII characters in the path name. This solved an aggravating problem
  811. with home directories for users with non-ASCII characters in their usernames.</p></li>
  812. </ul>
  813. <blockquote>
  814. <div><p>(Required extensive work by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9425">bpo-9425</a>.)</p>
  815. </div></blockquote>
  816. </section>
  817. <section id="new-improved-and-deprecated-modules">
  818. <h2>New, Improved, and Deprecated Modules<a class="headerlink" href="#new-improved-and-deprecated-modules" title="Link to this heading">¶</a></h2>
  819. <p>Python’s standard library has undergone significant maintenance efforts and
  820. quality improvements.</p>
  821. <p>The biggest news for Python 3.2 is that 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> package, <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>
  822. module, and <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> modules now work correctly with the bytes/text model
  823. in Python 3. For the first time, there is correct handling of messages with
  824. mixed encodings.</p>
  825. <p>Throughout the standard library, there has been more careful attention to
  826. encodings and text versus bytes issues. In particular, interactions with the
  827. operating system are now better able to exchange non-ASCII data using the
  828. Windows MBCS encoding, locale-aware encodings, or UTF-8.</p>
  829. <p>Another significant win is the addition of substantially better support for
  830. <em>SSL</em> connections and security certificates.</p>
  831. <p>In addition, more classes now implement a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> to support
  832. convenient and reliable resource clean-up using a <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>
  833. <section id="email">
  834. <h3>email<a class="headerlink" href="#email" title="Link to this heading">¶</a></h3>
  835. <p>The usability of 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> package in Python 3 has been mostly fixed by
  836. the extensive efforts of R. David Murray. The problem was that emails are
  837. typically read and stored in the form of <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> rather than <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>
  838. text, and they may contain multiple encodings within a single email. So, the
  839. email package had to be extended to parse and generate email messages in bytes
  840. format.</p>
  841. <ul>
  842. <li><p>New functions <a class="reference internal" href="../library/email.parser.html#email.message_from_bytes" title="email.message_from_bytes"><code class="xref py py-func docutils literal notranslate"><span class="pre">message_from_bytes()</span></code></a> and
  843. <a class="reference internal" href="../library/email.parser.html#email.message_from_binary_file" title="email.message_from_binary_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">message_from_binary_file()</span></code></a>, and new classes
  844. <a class="reference internal" href="../library/email.parser.html#email.parser.BytesFeedParser" title="email.parser.BytesFeedParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesFeedParser</span></code></a> and <a class="reference internal" href="../library/email.parser.html#email.parser.BytesParser" title="email.parser.BytesParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesParser</span></code></a>
  845. allow binary message data to be parsed into model objects.</p></li>
  846. <li><p>Given bytes input to the model, <a class="reference internal" href="../library/email.compat32-message.html#email.message.Message.get_payload" title="email.message.Message.get_payload"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_payload()</span></code></a>
  847. will by default decode a message body that has a
  848. <em class="mailheader">Content-Transfer-Encoding</em> of <em>8bit</em> using the charset
  849. specified in the MIME headers and return the resulting string.</p></li>
  850. <li><p>Given bytes input to the model, <a class="reference internal" href="../library/email.generator.html#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> will
  851. convert message bodies that have a <em class="mailheader">Content-Transfer-Encoding</em> of
  852. <em>8bit</em> to instead have a <em>7bit</em> <em class="mailheader">Content-Transfer-Encoding</em>.</p>
  853. <p>Headers with unencoded non-ASCII bytes are deemed to be <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a>-encoded
  854. using the <em>unknown-8bit</em> character set.</p>
  855. </li>
  856. <li><p>A new class <a class="reference internal" href="../library/email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> produces bytes as output,
  857. preserving any unchanged non-ASCII data that was present in the input used to
  858. build the model, including message bodies with a
  859. <em class="mailheader">Content-Transfer-Encoding</em> of <em>8bit</em>.</p></li>
  860. <li><p>The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">smtplib</span></code></a> <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP" title="smtplib.SMTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">SMTP</span></code></a> class now accepts a byte string
  861. for the <em>msg</em> argument to the <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.sendmail" title="smtplib.SMTP.sendmail"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sendmail()</span></code></a> method,
  862. and a new method, <a class="reference internal" href="../library/smtplib.html#smtplib.SMTP.send_message" title="smtplib.SMTP.send_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_message()</span></code></a> accepts a
  863. <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> object and can optionally obtain the
  864. <em>from_addr</em> and <em>to_addrs</em> addresses directly from the object.</p></li>
  865. </ul>
  866. <p>(Proposed and implemented by R. David Murray, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4661">bpo-4661</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10321">bpo-10321</a>.)</p>
  867. </section>
  868. <section id="elementtree">
  869. <h3>elementtree<a class="headerlink" href="#elementtree" title="Link to this heading">¶</a></h3>
  870. <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> package and its <code class="xref py py-mod docutils literal notranslate"><span class="pre">xml.etree.cElementTree</span></code>
  871. counterpart have been updated to version 1.3.</p>
  872. <p>Several new and useful functions and methods have been added:</p>
  873. <ul class="simple">
  874. <li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.fromstringlist" title="xml.etree.ElementTree.fromstringlist"><code class="xref py py-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.fromstringlist()</span></code></a> which builds an XML document
  875. from a sequence of fragments</p></li>
  876. <li><p><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-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.register_namespace()</span></code></a> for registering a global
  877. namespace prefix</p></li>
  878. <li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.tostringlist" title="xml.etree.ElementTree.tostringlist"><code class="xref py py-func docutils literal notranslate"><span class="pre">xml.etree.ElementTree.tostringlist()</span></code></a> for string representation
  879. including all sublists</p></li>
  880. <li><p><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">xml.etree.ElementTree.Element.extend()</span></code></a> for appending a sequence of zero
  881. or more elements</p></li>
  882. <li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.iterfind" title="xml.etree.ElementTree.Element.iterfind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.Element.iterfind()</span></code></a> searches an element and
  883. subelements</p></li>
  884. <li><p><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">xml.etree.ElementTree.Element.itertext()</span></code></a> creates a text iterator over
  885. an element and its subelements</p></li>
  886. <li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.end" title="xml.etree.ElementTree.TreeBuilder.end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.TreeBuilder.end()</span></code></a> closes the current element</p></li>
  887. <li><p><a class="reference internal" href="../library/xml.etree.elementtree.html#xml.etree.ElementTree.TreeBuilder.doctype" title="xml.etree.ElementTree.TreeBuilder.doctype"><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.TreeBuilder.doctype()</span></code></a> handles a doctype
  888. declaration</p></li>
  889. </ul>
  890. <p>Two methods have been deprecated:</p>
  891. <ul class="simple">
  892. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.getchildren()</span></code> use <code class="docutils literal notranslate"><span class="pre">list(elem)</span></code> instead.</p></li>
  893. <li><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">xml.etree.ElementTree.getiterator()</span></code> use <code class="docutils literal notranslate"><span class="pre">Element.iter</span></code> instead.</p></li>
  894. </ul>
  895. <p>For details of the update, see <a class="reference external" href="https://web.archive.org/web/20200703234532/http://effbot.org/zone/elementtree-13-intro.htm">Introducing ElementTree</a>
  896. on Fredrik Lundh’s website.</p>
  897. <p>(Contributed by Florent Xicluna and Fredrik Lundh, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6472">bpo-6472</a>.)</p>
  898. </section>
  899. <section id="functools">
  900. <h3>functools<a class="headerlink" href="#functools" title="Link to this heading">¶</a></h3>
  901. <ul>
  902. <li><p>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> module includes a new decorator for caching function
  903. calls. <a class="reference internal" href="../library/functools.html#functools.lru_cache" title="functools.lru_cache"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.lru_cache()</span></code></a> can save repeated queries to an external
  904. resource whenever the results are expected to be the same.</p>
  905. <p>For example, adding a caching decorator to a database query function can save
  906. database accesses for popular searches:</p>
  907. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">functools</span>
  908. <span class="gp">&gt;&gt;&gt; </span><span class="nd">@functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>
  909. <span class="gp">... </span><span class="k">def</span> <span class="nf">get_phone_number</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
  910. <span class="gp">... </span> <span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
  911. <span class="gp">... </span> <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s1">&#39;SELECT phonenumber FROM phonelist WHERE name=?&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,))</span>
  912. <span class="gp">... </span> <span class="k">return</span> <span class="n">c</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
  913. </pre></div>
  914. </div>
  915. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">user_requests</span><span class="p">:</span>
  916. <span class="gp">... </span> <span class="n">get_phone_number</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="c1"># cached lookup</span>
  917. </pre></div>
  918. </div>
  919. <p>To help with choosing an effective cache size, the wrapped function is
  920. instrumented for tracking cache statistics:</p>
  921. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">get_phone_number</span><span class="o">.</span><span class="n">cache_info</span><span class="p">()</span>
  922. <span class="go">CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)</span>
  923. </pre></div>
  924. </div>
  925. <p>If the phonelist table gets updated, the outdated contents of the cache can be
  926. cleared with:</p>
  927. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">get_phone_number</span><span class="o">.</span><span class="n">cache_clear</span><span class="p">()</span>
  928. </pre></div>
  929. </div>
  930. <p>(Contributed by Raymond Hettinger and incorporating design ideas from Jim
  931. Baker, Miki Tebeka, and Nick Coghlan; see <a class="reference external" href="https://code.activestate.com/recipes/498245/">recipe 498245</a>, <a class="reference external" href="https://code.activestate.com/recipes/577479/">recipe 577479</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10586">bpo-10586</a>, and
  932. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10593">bpo-10593</a>.)</p>
  933. </li>
  934. <li><p>The <a class="reference internal" href="../library/functools.html#functools.wraps" title="functools.wraps"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.wraps()</span></code></a> decorator now adds a <code class="xref py py-attr docutils literal notranslate"><span class="pre">__wrapped__</span></code> attribute
  935. pointing to the original callable function. This allows wrapped functions to
  936. be introspected. It also copies <a class="reference internal" href="../reference/datamodel.html#function.__annotations__" title="function.__annotations__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__annotations__</span></code></a> if
  937. defined. And now it also gracefully skips over missing attributes such as
  938. <a class="reference internal" href="../reference/datamodel.html#function.__doc__" title="function.__doc__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code></a> which
  939. might not be defined for the wrapped callable.</p>
  940. <p>In the above example, the cache can be removed by recovering the original
  941. function:</p>
  942. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">get_phone_number</span> <span class="o">=</span> <span class="n">get_phone_number</span><span class="o">.</span><span class="n">__wrapped__</span> <span class="c1"># uncached function</span>
  943. </pre></div>
  944. </div>
  945. <p>(By Nick Coghlan and Terrence Cole; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9567">bpo-9567</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3445">bpo-3445</a>, and
  946. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8814">bpo-8814</a>.)</p>
  947. </li>
  948. <li><p>To help write classes with rich comparison methods, a new decorator
  949. <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">functools.total_ordering()</span></code></a> will use existing equality and inequality
  950. methods to fill in the remaining methods.</p>
  951. <p>For example, supplying <em>__eq__</em> and <em>__lt__</em> will enable
  952. <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> to fill-in <em>__le__</em>, <em>__gt__</em> and <em>__ge__</em>:</p>
  953. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@total_ordering</span>
  954. <span class="k">class</span> <span class="nc">Student</span><span class="p">:</span>
  955. <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
  956. <span class="k">return</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o">==</span>
  957. <span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
  958. <span class="k">def</span> <span class="fm">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
  959. <span class="k">return</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span> <span class="o">&lt;</span>
  960. <span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">lastname</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">other</span><span class="o">.</span><span class="n">firstname</span><span class="o">.</span><span class="n">lower</span><span class="p">()))</span>
  961. </pre></div>
  962. </div>
  963. <p>With the <em>total_ordering</em> decorator, the remaining comparison methods
  964. are filled in automatically.</p>
  965. <p>(Contributed by Raymond Hettinger.)</p>
  966. </li>
  967. <li><p>To aid in porting programs from Python 2, the <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">functools.cmp_to_key()</span></code></a>
  968. function converts an old-style comparison function to
  969. modern <a class="reference internal" href="../glossary.html#term-key-function"><span class="xref std std-term">key function</span></a>:</p>
  970. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># locale-aware sort order</span>
  971. <span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">cmp_to_key</span><span class="p">(</span><span class="n">locale</span><span class="o">.</span><span class="n">strcoll</span><span class="p">))</span>
  972. </pre></div>
  973. </div>
  974. <p>For sorting examples and a brief sorting tutorial, see the <a class="reference external" href="https://wiki.python.org/moin/HowTo/Sorting/">Sorting HowTo</a> tutorial.</p>
  975. <p>(Contributed by Raymond Hettinger.)</p>
  976. </li>
  977. </ul>
  978. </section>
  979. <section id="itertools">
  980. <h3>itertools<a class="headerlink" href="#itertools" title="Link to this heading">¶</a></h3>
  981. <ul>
  982. <li><p>The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module has a new <a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">accumulate()</span></code></a> function
  983. modeled on APL’s <em>scan</em> operator and Numpy’s <em>accumulate</em> function:</p>
  984. <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">itertools</span> <span class="kn">import</span> <span class="n">accumulate</span>
  985. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">accumulate</span><span class="p">([</span><span class="mi">8</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">50</span><span class="p">]))</span>
  986. <span class="go">[8, 10, 60]</span>
  987. </pre></div>
  988. </div>
  989. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">prob_dist</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">]</span>
  990. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">accumulate</span><span class="p">(</span><span class="n">prob_dist</span><span class="p">))</span> <span class="c1"># cumulative probability distribution</span>
  991. <span class="go">[0.1, 0.5, 0.7, 1.0]</span>
  992. </pre></div>
  993. </div>
  994. <p>For an example using <a class="reference internal" href="../library/itertools.html#itertools.accumulate" title="itertools.accumulate"><code class="xref py py-func docutils literal notranslate"><span class="pre">accumulate()</span></code></a>, see the <a class="reference internal" href="../library/random.html#random-examples"><span class="std std-ref">examples for
  995. the random module</span></a>.</p>
  996. <p>(Contributed by Raymond Hettinger and incorporating design suggestions
  997. from Mark Dickinson.)</p>
  998. </li>
  999. </ul>
  1000. </section>
  1001. <section id="collections">
  1002. <h3>collections<a class="headerlink" href="#collections" title="Link to this heading">¶</a></h3>
  1003. <ul>
  1004. <li><p>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">collections.Counter</span></code></a> class now has two forms of in-place
  1005. subtraction, the existing <em>-=</em> operator for <a class="reference external" href="https://en.wikipedia.org/wiki/Saturation_arithmetic">saturating subtraction</a> and the new
  1006. <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> method for regular subtraction. The
  1007. former is suitable for <a class="reference external" href="https://en.wikipedia.org/wiki/Multiset">multisets</a>
  1008. which only have positive counts, and the latter is more suitable for use cases
  1009. that allow negative counts:</p>
  1010. <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">Counter</span>
  1011. <span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  1012. <span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span> <span class="o">-=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span> <span class="c1"># saturating subtraction</span>
  1013. <span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span>
  1014. <span class="go">Counter({&#39;dogs&#39;: 3})</span>
  1015. </pre></div>
  1016. </div>
  1017. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
  1018. <span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">dogs</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">cats</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span> <span class="c1"># regular subtraction</span>
  1019. <span class="gp">&gt;&gt;&gt; </span><span class="n">tally</span>
  1020. <span class="go">Counter({&#39;dogs&#39;: 3, &#39;cats&#39;: -5})</span>
  1021. </pre></div>
  1022. </div>
  1023. <p>(Contributed by Raymond Hettinger.)</p>
  1024. </li>
  1025. <li><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">collections.OrderedDict</span></code></a> class has a new method
  1026. <a class="reference internal" href="../library/collections.html#collections.OrderedDict.move_to_end" title="collections.OrderedDict.move_to_end"><code class="xref py py-meth docutils literal notranslate"><span class="pre">move_to_end()</span></code></a> which takes an existing key and
  1027. moves it to either the first or last position in the ordered sequence.</p>
  1028. <p>The default is to move an item to the last position. This is equivalent of
  1029. renewing an entry with <code class="docutils literal notranslate"><span class="pre">od[k]</span> <span class="pre">=</span> <span class="pre">od.pop(k)</span></code>.</p>
  1030. <p>A fast move-to-end operation is useful for resequencing entries. For example,
  1031. an ordered dictionary can be used to track order of access by aging entries
  1032. from the oldest to the most recently accessed.</p>
  1033. <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">OrderedDict</span>
  1034. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="o">.</span><span class="n">fromkeys</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="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="s1">&#39;e&#39;</span><span class="p">])</span>
  1035. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  1036. <span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;X&#39;, &#39;d&#39;, &#39;e&#39;]</span>
  1037. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">move_to_end</span><span class="p">(</span><span class="s1">&#39;X&#39;</span><span class="p">)</span>
  1038. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  1039. <span class="go">[&#39;a&#39;, &#39;b&#39;, &#39;d&#39;, &#39;e&#39;, &#39;X&#39;]</span>
  1040. </pre></div>
  1041. </div>
  1042. <p>(Contributed by Raymond Hettinger.)</p>
  1043. </li>
  1044. <li><p>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">collections.deque</span></code></a> class grew two new methods
  1045. <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> and <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> that
  1046. make them more substitutable for <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> objects:</p>
  1047. <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">deque</span>
  1048. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">deque</span><span class="p">(</span><span class="s1">&#39;simsalabim&#39;</span><span class="p">)</span>
  1049. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="s1">&#39;s&#39;</span><span class="p">)</span>
  1050. <span class="go">2</span>
  1051. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
  1052. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span>
  1053. <span class="go">deque([&#39;m&#39;, &#39;i&#39;, &#39;b&#39;, &#39;a&#39;, &#39;l&#39;, &#39;a&#39;, &#39;s&#39;, &#39;m&#39;, &#39;i&#39;, &#39;s&#39;])</span>
  1054. </pre></div>
  1055. </div>
  1056. <p>(Contributed by Raymond Hettinger.)</p>
  1057. </li>
  1058. </ul>
  1059. </section>
  1060. <section id="threading">
  1061. <h3>threading<a class="headerlink" href="#threading" title="Link to this heading">¶</a></h3>
  1062. <p>The <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 has a new <a class="reference internal" href="../library/threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a>
  1063. synchronization class for making multiple threads wait until all of them have
  1064. reached a common barrier point. Barriers are useful for making sure that a task
  1065. with multiple preconditions does not run until all of the predecessor tasks are
  1066. complete.</p>
  1067. <p>Barriers can work with an arbitrary number of threads. This is a generalization
  1068. of a <a class="reference external" href="https://en.wikipedia.org/wiki/Synchronous_rendezvous">Rendezvous</a> which
  1069. is defined for only two threads.</p>
  1070. <p>Implemented as a two-phase cyclic barrier, <a class="reference internal" href="../library/threading.html#threading.Barrier" title="threading.Barrier"><code class="xref py py-class docutils literal notranslate"><span class="pre">Barrier</span></code></a> objects
  1071. are suitable for use in loops. The separate <em>filling</em> and <em>draining</em> phases
  1072. assure that all threads get released (drained) before any one of them can loop
  1073. back and re-enter the barrier. The barrier fully resets after each cycle.</p>
  1074. <p>Example of using barriers:</p>
  1075. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">threading</span> <span class="kn">import</span> <span class="n">Barrier</span><span class="p">,</span> <span class="n">Thread</span>
  1076. <span class="k">def</span> <span class="nf">get_votes</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
  1077. <span class="n">ballots</span> <span class="o">=</span> <span class="n">conduct_election</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
  1078. <span class="n">all_polls_closed</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span> <span class="c1"># do not count until all polls are closed</span>
  1079. <span class="n">totals</span> <span class="o">=</span> <span class="n">summarize</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
  1080. <span class="n">publish</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">totals</span><span class="p">)</span>
  1081. <span class="n">all_polls_closed</span> <span class="o">=</span> <span class="n">Barrier</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sites</span><span class="p">))</span>
  1082. <span class="k">for</span> <span class="n">site</span> <span class="ow">in</span> <span class="n">sites</span><span class="p">:</span>
  1083. <span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">get_votes</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">site</span><span class="p">,))</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
  1084. </pre></div>
  1085. </div>
  1086. <p>In this example, the barrier enforces a rule that votes cannot be counted at any
  1087. polling site until all polls are closed. Notice how a solution with a barrier
  1088. is similar to one with <a class="reference internal" href="../library/threading.html#threading.Thread.join" title="threading.Thread.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Thread.join()</span></code></a>, but the threads stay alive
  1089. and continue to do work (summarizing ballots) after the barrier point is
  1090. crossed.</p>
  1091. <p>If any of the predecessor tasks can hang or be delayed, a barrier can be created
  1092. with an optional <em>timeout</em> parameter. Then if the timeout period elapses before
  1093. all the predecessor tasks reach the barrier point, all waiting threads are
  1094. released and a <a class="reference internal" href="../library/threading.html#threading.BrokenBarrierError" title="threading.BrokenBarrierError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BrokenBarrierError</span></code></a> exception is raised:</p>
  1095. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_votes</span><span class="p">(</span><span class="n">site</span><span class="p">):</span>
  1096. <span class="n">ballots</span> <span class="o">=</span> <span class="n">conduct_election</span><span class="p">(</span><span class="n">site</span><span class="p">)</span>
  1097. <span class="k">try</span><span class="p">:</span>
  1098. <span class="n">all_polls_closed</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="n">midnight</span> <span class="o">-</span> <span class="n">time</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>
  1099. <span class="k">except</span> <span class="n">BrokenBarrierError</span><span class="p">:</span>
  1100. <span class="n">lockbox</span> <span class="o">=</span> <span class="n">seal_ballots</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
  1101. <span class="n">queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">lockbox</span><span class="p">)</span>
  1102. <span class="k">else</span><span class="p">:</span>
  1103. <span class="n">totals</span> <span class="o">=</span> <span class="n">summarize</span><span class="p">(</span><span class="n">ballots</span><span class="p">)</span>
  1104. <span class="n">publish</span><span class="p">(</span><span class="n">site</span><span class="p">,</span> <span class="n">totals</span><span class="p">)</span>
  1105. </pre></div>
  1106. </div>
  1107. <p>In this example, the barrier enforces a more robust rule. If some election
  1108. sites do not finish before midnight, the barrier times-out and the ballots are
  1109. sealed and deposited in a queue for later handling.</p>
  1110. <p>See <a class="reference external" href="https://osl.cs.illinois.edu/media/papers/karmani-2009-barrier_synchronization_pattern.pdf">Barrier Synchronization Patterns</a>
  1111. for more examples of how barriers can be used in parallel computing. Also, there is
  1112. a simple but thorough explanation of barriers in <a class="reference external" href="https://greenteapress.com/semaphores/LittleBookOfSemaphores.pdf">The Little Book of Semaphores</a>, <em>section 3.6</em>.</p>
  1113. <p>(Contributed by Kristján Valur Jónsson with an API review by Jeffrey Yasskin in
  1114. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8777">bpo-8777</a>.)</p>
  1115. </section>
  1116. <section id="datetime-and-time">
  1117. <h3>datetime and time<a class="headerlink" href="#datetime-and-time" title="Link to this heading">¶</a></h3>
  1118. <ul>
  1119. <li><p>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 has a new type <a class="reference internal" href="../library/datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">timezone</span></code></a> that
  1120. implements the <a class="reference internal" href="../library/datetime.html#datetime.tzinfo" title="datetime.tzinfo"><code class="xref py py-class docutils literal notranslate"><span class="pre">tzinfo</span></code></a> interface by returning a fixed UTC
  1121. offset and timezone name. This makes it easier to create timezone-aware
  1122. datetime objects:</p>
  1123. <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">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timezone</span>
  1124. <span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
  1125. <span class="go">datetime.datetime(2010, 12, 8, 21, 4, 2, 923754, tzinfo=datetime.timezone.utc)</span>
  1126. <span class="gp">&gt;&gt;&gt; </span><span class="n">datetime</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s2">&quot;01/01/2000 12:00 +0000&quot;</span><span class="p">,</span> <span class="s2">&quot;%m/</span><span class="si">%d</span><span class="s2">/%Y %H:%M %z&quot;</span><span class="p">)</span>
  1127. <span class="go">datetime.datetime(2000, 1, 1, 12, 0, tzinfo=datetime.timezone.utc)</span>
  1128. </pre></div>
  1129. </div>
  1130. </li>
  1131. <li><p>Also, <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> objects can now be multiplied by
  1132. <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 divided by <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#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> objects.
  1133. And <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> objects can now divide one another.</p></li>
  1134. <li><p>The <a class="reference internal" href="../library/datetime.html#datetime.date.strftime" title="datetime.date.strftime"><code class="xref py py-meth docutils literal notranslate"><span class="pre">datetime.date.strftime()</span></code></a> method is no longer restricted to years
  1135. after 1900. The new supported year range is from 1000 to 9999 inclusive.</p></li>
  1136. <li><p>Whenever a two-digit year is used in a time tuple, the interpretation has been
  1137. governed by <code class="xref py py-data docutils literal notranslate"><span class="pre">time.accept2dyear</span></code>. The default is <code class="docutils literal notranslate"><span class="pre">True</span></code> which means that
  1138. for a two-digit year, the century is guessed according to the POSIX rules
  1139. governing the <code class="docutils literal notranslate"><span class="pre">%y</span></code> strptime format.</p>
  1140. <p>Starting with Py3.2, use of the century guessing heuristic will emit a
  1141. <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>. Instead, it is recommended that
  1142. <code class="xref py py-data docutils literal notranslate"><span class="pre">time.accept2dyear</span></code> be set to <code class="docutils literal notranslate"><span class="pre">False</span></code> so that large date ranges
  1143. can be used without guesswork:</p>
  1144. <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">time</span><span class="o">,</span> <span class="nn">warnings</span>
  1145. <span class="gp">&gt;&gt;&gt; </span><span class="n">warnings</span><span class="o">.</span><span class="n">resetwarnings</span><span class="p">()</span> <span class="c1"># remove the default warning filters</span>
  1146. <span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="kc">True</span> <span class="c1"># guess whether 11 means 11 or 2011</span>
  1147. <span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">((</span><span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">56</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
  1148. <span class="go">Warning (from warnings module):</span>
  1149. <span class="go"> ...</span>
  1150. <span class="go">DeprecationWarning: Century info guessed for a 2-digit year.</span>
  1151. <span class="go">&#39;Fri Jan 1 12:34:56 2011&#39;</span>
  1152. <span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">accept2dyear</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># use the full range of allowable dates</span>
  1153. <span class="gp">&gt;&gt;&gt; </span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">((</span><span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">56</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
  1154. <span class="go">&#39;Fri Jan 1 12:34:56 11&#39;</span>
  1155. </pre></div>
  1156. </div>
  1157. <p>Several functions now have significantly expanded date ranges. When
  1158. <code class="xref py py-data docutils literal notranslate"><span class="pre">time.accept2dyear</span></code> is false, the <a class="reference internal" href="../library/time.html#time.asctime" title="time.asctime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.asctime()</span></code></a> function will
  1159. accept any year that fits in a C int, while the <a class="reference internal" href="../library/time.html#time.mktime" title="time.mktime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.mktime()</span></code></a> and
  1160. <a class="reference internal" href="../library/time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a> functions will accept the full range supported by the
  1161. corresponding operating system functions.</p>
  1162. </li>
  1163. </ul>
  1164. <p>(Contributed by Alexander Belopolsky and Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1289118">bpo-1289118</a>,
  1165. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5094">bpo-5094</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6641">bpo-6641</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2706">bpo-2706</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1777412">bpo-1777412</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8013">bpo-8013</a>,
  1166. and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10827">bpo-10827</a>.)</p>
  1167. </section>
  1168. <section id="math">
  1169. <h3>math<a class="headerlink" href="#math" title="Link to this heading">¶</a></h3>
  1170. <p>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 has been updated with six new functions inspired by the
  1171. C99 standard.</p>
  1172. <p>The <a class="reference internal" href="../library/math.html#math.isfinite" title="math.isfinite"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfinite()</span></code></a> function provides a reliable and fast way to detect
  1173. special values. It returns <code class="docutils literal notranslate"><span class="pre">True</span></code> for regular numbers and <code class="docutils literal notranslate"><span class="pre">False</span></code> for <em>Nan</em> or
  1174. <em>Infinity</em>:</p>
  1175. <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">math</span> <span class="kn">import</span> <span class="n">isfinite</span>
  1176. <span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">isfinite</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">123</span><span class="p">,</span> <span class="mf">4.56</span><span class="p">,</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;Nan&#39;</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="s1">&#39;Inf&#39;</span><span class="p">))]</span>
  1177. <span class="go">[True, True, False, False]</span>
  1178. </pre></div>
  1179. </div>
  1180. <p>The <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> function computes <code class="docutils literal notranslate"><span class="pre">e**x-1</span></code> for small values of <em>x</em>
  1181. without incurring the loss of precision that usually accompanies the subtraction
  1182. of nearly equal quantities:</p>
  1183. <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">math</span> <span class="kn">import</span> <span class="n">expm1</span>
  1184. <span class="gp">&gt;&gt;&gt; </span><span class="n">expm1</span><span class="p">(</span><span class="mf">0.013671875</span><span class="p">)</span> <span class="c1"># more accurate way to compute e**x-1 for a small x</span>
  1185. <span class="go">0.013765762467652909</span>
  1186. </pre></div>
  1187. </div>
  1188. <p>The <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> function computes a probability integral or <a class="reference external" href="https://en.wikipedia.org/wiki/Error_function">Gaussian
  1189. error function</a>. The
  1190. complementary error function, <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>, is <code class="docutils literal notranslate"><span class="pre">1</span> <span class="pre">-</span> <span class="pre">erf(x)</span></code>:</p>
  1191. <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">math</span> <span class="kn">import</span> <span class="n">erf</span><span class="p">,</span> <span class="n">erfc</span><span class="p">,</span> <span class="n">sqrt</span>
  1192. <span class="gp">&gt;&gt;&gt; </span><span class="n">erf</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="c1"># portion of normal distribution within 1 standard deviation</span>
  1193. <span class="go">0.682689492137086</span>
  1194. <span class="gp">&gt;&gt;&gt; </span><span class="n">erfc</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="c1"># portion of normal distribution outside 1 standard deviation</span>
  1195. <span class="go">0.31731050786291404</span>
  1196. <span class="gp">&gt;&gt;&gt; </span><span class="n">erf</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span> <span class="o">+</span> <span class="n">erfc</span><span class="p">(</span><span class="mf">1.0</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">2.0</span><span class="p">))</span>
  1197. <span class="go">1.0</span>
  1198. </pre></div>
  1199. </div>
  1200. <p>The <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> function is a continuous extension of the factorial
  1201. function. See <a class="reference external" href="https://en.wikipedia.org/wiki/Gamma_function">https://en.wikipedia.org/wiki/Gamma_function</a> for details. Because
  1202. the function is related to factorials, it grows large even for small values of
  1203. <em>x</em>, so there is also a <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> function for computing the natural
  1204. logarithm of the gamma function:</p>
  1205. <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">math</span> <span class="kn">import</span> <span class="n">gamma</span><span class="p">,</span> <span class="n">lgamma</span>
  1206. <span class="gp">&gt;&gt;&gt; </span><span class="n">gamma</span><span class="p">(</span><span class="mf">7.0</span><span class="p">)</span> <span class="c1"># six factorial</span>
  1207. <span class="go">720.0</span>
  1208. <span class="gp">&gt;&gt;&gt; </span><span class="n">lgamma</span><span class="p">(</span><span class="mf">801.0</span><span class="p">)</span> <span class="c1"># log(800 factorial)</span>
  1209. <span class="go">4551.950730698041</span>
  1210. </pre></div>
  1211. </div>
  1212. <p>(Contributed by Mark Dickinson.)</p>
  1213. </section>
  1214. <section id="abc">
  1215. <h3>abc<a class="headerlink" href="#abc" title="Link to this heading">¶</a></h3>
  1216. <p>The <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to :pep:`3119`."><code class="xref py py-mod docutils literal notranslate"><span class="pre">abc</span></code></a> module now supports <a class="reference internal" href="../library/abc.html#abc.abstractclassmethod" title="abc.abstractclassmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abstractclassmethod()</span></code></a> and
  1217. <a class="reference internal" href="../library/abc.html#abc.abstractstaticmethod" title="abc.abstractstaticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">abstractstaticmethod()</span></code></a>.</p>
  1218. <p>These tools make it possible to define an <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> that
  1219. requires a particular <a class="reference internal" href="../library/functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> or <a class="reference internal" href="../library/functions.html#staticmethod" title="staticmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">staticmethod()</span></code></a> to be
  1220. implemented:</p>
  1221. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Temperature</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">):</span>
  1222. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractclassmethod</span>
  1223. <span class="k">def</span> <span class="nf">from_fahrenheit</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
  1224. <span class="o">...</span>
  1225. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractclassmethod</span>
  1226. <span class="k">def</span> <span class="nf">from_celsius</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
  1227. <span class="o">...</span>
  1228. </pre></div>
  1229. </div>
  1230. <p>(Patch submitted by Daniel Urban; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5867">bpo-5867</a>.)</p>
  1231. </section>
  1232. <section id="io">
  1233. <h3>io<a class="headerlink" href="#io" title="Link to this heading">¶</a></h3>
  1234. <p>The <a class="reference internal" href="../library/io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> has a new method, <a class="reference internal" href="../library/io.html#io.BytesIO.getbuffer" title="io.BytesIO.getbuffer"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getbuffer()</span></code></a>, which
  1235. provides functionality similar to <a class="reference internal" href="../library/stdtypes.html#memoryview" title="memoryview"><code class="xref py py-func docutils literal notranslate"><span class="pre">memoryview()</span></code></a>. It creates an editable
  1236. view of the data without making a copy. The buffer’s random access and support
  1237. for slice notation are well-suited to in-place editing:</p>
  1238. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">REC_LEN</span><span class="p">,</span> <span class="n">LOC_START</span><span class="p">,</span> <span class="n">LOC_LEN</span> <span class="o">=</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">11</span>
  1239. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">record_number</span><span class="p">,</span> <span class="n">location</span><span class="p">):</span>
  1240. <span class="gp">... </span> <span class="n">start</span> <span class="o">=</span> <span class="n">record_number</span> <span class="o">*</span> <span class="n">REC_LEN</span> <span class="o">+</span> <span class="n">LOC_START</span>
  1241. <span class="gp">... </span> <span class="n">buffer</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">start</span><span class="o">+</span><span class="n">LOC_LEN</span><span class="p">]</span> <span class="o">=</span> <span class="n">location</span>
  1242. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">io</span>
  1243. <span class="gp">&gt;&gt;&gt; </span><span class="n">byte_stream</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span>
  1244. <span class="gp">... </span> <span class="sa">b</span><span class="s1">&#39;G3805 storeroom Main chassis &#39;</span>
  1245. <span class="gp">... </span> <span class="sa">b</span><span class="s1">&#39;X7899 shipping Reserve cog &#39;</span>
  1246. <span class="gp">... </span> <span class="sa">b</span><span class="s1">&#39;L6988 receiving Primary sprocket&#39;</span>
  1247. <span class="gp">... </span><span class="p">)</span>
  1248. <span class="gp">&gt;&gt;&gt; </span><span class="n">buffer</span> <span class="o">=</span> <span class="n">byte_stream</span><span class="o">.</span><span class="n">getbuffer</span><span class="p">()</span>
  1249. <span class="gp">&gt;&gt;&gt; </span><span class="n">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;warehouse &#39;</span><span class="p">)</span>
  1250. <span class="gp">&gt;&gt;&gt; </span><span class="n">change_location</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;showroom &#39;</span><span class="p">)</span>
  1251. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">byte_stream</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span>
  1252. <span class="go">b&#39;G3805 showroom Main chassis &#39;</span>
  1253. <span class="go">b&#39;X7899 warehouse Reserve cog &#39;</span>
  1254. <span class="go">b&#39;L6988 receiving Primary sprocket&#39;</span>
  1255. </pre></div>
  1256. </div>
  1257. <p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5506">bpo-5506</a>.)</p>
  1258. </section>
  1259. <section id="reprlib">
  1260. <h3>reprlib<a class="headerlink" href="#reprlib" title="Link to this heading">¶</a></h3>
  1261. <p>When writing a <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> method for a custom container, it is easy to
  1262. forget to handle the case where a member refers back to the container itself.
  1263. Python’s builtin objects such as <a class="reference internal" href="../library/stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> handle
  1264. self-reference by displaying “…” in the recursive part of the representation
  1265. string.</p>
  1266. <p>To help write such <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code></a> methods, the <a class="reference internal" href="../library/reprlib.html#module-reprlib" title="reprlib: Alternate repr() implementation with size limits."><code class="xref py py-mod docutils literal notranslate"><span class="pre">reprlib</span></code></a> module has a new
  1267. decorator, <a class="reference internal" href="../library/reprlib.html#reprlib.recursive_repr" title="reprlib.recursive_repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">recursive_repr()</span></code></a>, for detecting recursive calls to
  1268. <code class="xref py py-meth docutils literal notranslate"><span class="pre">__repr__()</span></code> and substituting a placeholder string instead:</p>
  1269. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">MyList</span><span class="p">(</span><span class="nb">list</span><span class="p">):</span>
  1270. <span class="gp">... </span> <span class="nd">@recursive_repr</span><span class="p">()</span>
  1271. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1272. <span class="gp">... </span> <span class="k">return</span> <span class="s1">&#39;&lt;&#39;</span> <span class="o">+</span> <span class="s1">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">repr</span><span class="p">,</span> <span class="bp">self</span><span class="p">))</span> <span class="o">+</span> <span class="s1">&#39;&gt;&#39;</span>
  1273. <span class="gp">...</span>
  1274. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">MyList</span><span class="p">(</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span>
  1275. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
  1276. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
  1277. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span>
  1278. <span class="go">&lt;&#39;a&#39;|&#39;b&#39;|&#39;c&#39;|...|&#39;x&#39;&gt;</span>
  1279. </pre></div>
  1280. </div>
  1281. <p>(Contributed by Raymond Hettinger in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9826">bpo-9826</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9840">bpo-9840</a>.)</p>
  1282. </section>
  1283. <section id="logging">
  1284. <h3>logging<a class="headerlink" href="#logging" title="Link to this heading">¶</a></h3>
  1285. <p>In addition to dictionary-based configuration described above, the
  1286. <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> package has many other improvements.</p>
  1287. <p>The logging documentation has been augmented by a <a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><span class="std std-ref">basic tutorial</span></a>, an <a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><span class="std std-ref">advanced tutorial</span></a>, and a <a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><span class="std std-ref">cookbook</span></a> of
  1288. logging recipes. These documents are the fastest way to learn about logging.</p>
  1289. <p>The <a class="reference internal" href="../library/logging.html#logging.basicConfig" title="logging.basicConfig"><code class="xref py py-func docutils literal notranslate"><span class="pre">logging.basicConfig()</span></code></a> set-up function gained a <em>style</em> argument to
  1290. support three different types of string formatting. It defaults to “%” for
  1291. traditional %-formatting, can be set to “{” for the new <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> style, or
  1292. can be set to “$” for the shell-style formatting provided by
  1293. <a class="reference internal" href="../library/string.html#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.Template</span></code></a>. The following three configurations are equivalent:</p>
  1294. <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">logging</span> <span class="kn">import</span> <span class="n">basicConfig</span>
  1295. <span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">&#39;%&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">%(name)s</span><span class="s2"> -&gt; </span><span class="si">%(levelname)s</span><span class="s2">: </span><span class="si">%(message)s</span><span class="s2">&quot;</span><span class="p">)</span>
  1296. <span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">&#39;{&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">{name}</span><span class="s2"> -&gt; </span><span class="si">{levelname}</span><span class="s2"> </span><span class="si">{message}</span><span class="s2">&quot;</span><span class="p">)</span>
  1297. <span class="gp">&gt;&gt;&gt; </span><span class="n">basicConfig</span><span class="p">(</span><span class="n">style</span><span class="o">=</span><span class="s1">&#39;$&#39;</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s2">&quot;$name -&gt; $levelname: $message&quot;</span><span class="p">)</span>
  1298. </pre></div>
  1299. </div>
  1300. <p>If no configuration is set-up before a logging event occurs, there is now a
  1301. default configuration using a <a class="reference internal" href="../library/logging.handlers.html#logging.StreamHandler" title="logging.StreamHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">StreamHandler</span></code></a> directed to
  1302. <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> for events of <code class="docutils literal notranslate"><span class="pre">WARNING</span></code> level or higher. Formerly, an
  1303. event occurring before a configuration was set-up would either raise an
  1304. exception or silently drop the event depending on the value of
  1305. <a class="reference internal" href="../library/logging.html#logging.raiseExceptions" title="logging.raiseExceptions"><code class="xref py py-data docutils literal notranslate"><span class="pre">logging.raiseExceptions</span></code></a>. The new default handler is stored in
  1306. <a class="reference internal" href="../library/logging.html#logging.lastResort" title="logging.lastResort"><code class="xref py py-data docutils literal notranslate"><span class="pre">logging.lastResort</span></code></a>.</p>
  1307. <p>The use of filters has been simplified. Instead of creating a
  1308. <a class="reference internal" href="../library/logging.html#logging.Filter" title="logging.Filter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Filter</span></code></a> object, the predicate can be any Python callable that
  1309. returns <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  1310. <p>There were a number of other improvements that add flexibility and simplify
  1311. configuration. See the module documentation for a full listing of changes in
  1312. Python 3.2.</p>
  1313. </section>
  1314. <section id="csv">
  1315. <h3>csv<a class="headerlink" href="#csv" title="Link to this heading">¶</a></h3>
  1316. <p>The <a class="reference internal" href="../library/csv.html#module-csv" title="csv: Write and read tabular data to and from delimited files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">csv</span></code></a> module now supports a new dialect, <a class="reference internal" href="../library/csv.html#csv.unix_dialect" title="csv.unix_dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">unix_dialect</span></code></a>,
  1317. which applies quoting for all fields and a traditional Unix style with <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> as
  1318. the line terminator. The registered dialect name is <code class="docutils literal notranslate"><span class="pre">unix</span></code>.</p>
  1319. <p>The <a class="reference internal" href="../library/csv.html#csv.DictWriter" title="csv.DictWriter"><code class="xref py py-class docutils literal notranslate"><span class="pre">csv.DictWriter</span></code></a> has a new method,
  1320. <a class="reference internal" href="../library/csv.html#csv.DictWriter.writeheader" title="csv.DictWriter.writeheader"><code class="xref py py-meth docutils literal notranslate"><span class="pre">writeheader()</span></code></a> for writing-out an initial row to document
  1321. the field names:</p>
  1322. <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">csv</span><span class="o">,</span> <span class="nn">sys</span>
  1323. <span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">DictWriter</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;dept&#39;</span><span class="p">],</span> <span class="n">dialect</span><span class="o">=</span><span class="s1">&#39;unix&#39;</span><span class="p">)</span>
  1324. <span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">writeheader</span><span class="p">()</span>
  1325. <span class="go">&quot;name&quot;,&quot;dept&quot;</span>
  1326. <span class="gp">&gt;&gt;&gt; </span><span class="n">w</span><span class="o">.</span><span class="n">writerows</span><span class="p">([</span>
  1327. <span class="gp">... </span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;tom&#39;</span><span class="p">,</span> <span class="s1">&#39;dept&#39;</span><span class="p">:</span> <span class="s1">&#39;accounting&#39;</span><span class="p">},</span>
  1328. <span class="gp">... </span> <span class="p">{</span><span class="s1">&#39;name&#39;</span><span class="p">:</span> <span class="s1">&#39;susan&#39;</span><span class="p">,</span> <span class="s1">&#39;dept&#39;</span><span class="p">:</span> <span class="s1">&#39;Salesl&#39;</span><span class="p">}])</span>
  1329. <span class="go">&quot;tom&quot;,&quot;accounting&quot;</span>
  1330. <span class="go">&quot;susan&quot;,&quot;sales&quot;</span>
  1331. </pre></div>
  1332. </div>
  1333. <p>(New dialect suggested by Jay Talbot in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5975">bpo-5975</a>, and the new method
  1334. suggested by Ed Abraham in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1537721">bpo-1537721</a>.)</p>
  1335. </section>
  1336. <section id="contextlib">
  1337. <h3>contextlib<a class="headerlink" href="#contextlib" title="Link to this heading">¶</a></h3>
  1338. <p>There is a new and slightly mind-blowing tool
  1339. <a class="reference internal" href="../library/contextlib.html#contextlib.ContextDecorator" title="contextlib.ContextDecorator"><code class="xref py py-class docutils literal notranslate"><span class="pre">ContextDecorator</span></code></a> that is helpful for creating a
  1340. <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> that does double duty as a function decorator.</p>
  1341. <p>As a convenience, this new functionality is used by
  1342. <a class="reference internal" href="../library/contextlib.html#contextlib.contextmanager" title="contextlib.contextmanager"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextmanager()</span></code></a> so that no extra effort is needed to support
  1343. both roles.</p>
  1344. <p>The basic idea is that both context managers and function decorators can be used
  1345. for pre-action and post-action wrappers. Context managers wrap a group of
  1346. statements using a <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, and function decorators wrap a
  1347. group of statements enclosed in a function. So, occasionally there is a need to
  1348. write a pre-action or post-action wrapper that can be used in either role.</p>
  1349. <p>For example, it is sometimes useful to wrap functions or groups of statements
  1350. with a logger that can track the time of entry and time of exit. Rather than
  1351. writing both a function decorator and a context manager for the task, the
  1352. <a class="reference internal" href="../library/contextlib.html#contextlib.contextmanager" title="contextlib.contextmanager"><code class="xref py py-func docutils literal notranslate"><span class="pre">contextmanager()</span></code></a> provides both capabilities in a single
  1353. definition:</p>
  1354. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">contextmanager</span>
  1355. <span class="kn">import</span> <span class="nn">logging</span>
  1356. <span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="n">level</span><span class="o">=</span><span class="n">logging</span><span class="o">.</span><span class="n">INFO</span><span class="p">)</span>
  1357. <span class="nd">@contextmanager</span>
  1358. <span class="k">def</span> <span class="nf">track_entry_and_exit</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
  1359. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Entering: </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
  1360. <span class="k">yield</span>
  1361. <span class="n">logging</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Exiting: </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
  1362. </pre></div>
  1363. </div>
  1364. <p>Formerly, this would have only been usable as a context manager:</p>
  1365. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">track_entry_and_exit</span><span class="p">(</span><span class="s1">&#39;widget loader&#39;</span><span class="p">):</span>
  1366. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Some time consuming activity goes here&#39;</span><span class="p">)</span>
  1367. <span class="n">load_widget</span><span class="p">()</span>
  1368. </pre></div>
  1369. </div>
  1370. <p>Now, it can be used as a decorator as well:</p>
  1371. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nd">@track_entry_and_exit</span><span class="p">(</span><span class="s1">&#39;widget loader&#39;</span><span class="p">)</span>
  1372. <span class="k">def</span> <span class="nf">activity</span><span class="p">():</span>
  1373. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Some time consuming activity goes here&#39;</span><span class="p">)</span>
  1374. <span class="n">load_widget</span><span class="p">()</span>
  1375. </pre></div>
  1376. </div>
  1377. <p>Trying to fulfill two roles at once places some limitations on the technique.
  1378. Context managers normally have the flexibility to return an argument usable by
  1379. a <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, but there is no parallel for function decorators.</p>
  1380. <p>In the above example, there is not a clean way for the <em>track_entry_and_exit</em>
  1381. context manager to return a logging instance for use in the body of enclosed
  1382. statements.</p>
  1383. <p>(Contributed by Michael Foord in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9110">bpo-9110</a>.)</p>
  1384. </section>
  1385. <section id="decimal-and-fractions">
  1386. <h3>decimal and fractions<a class="headerlink" href="#decimal-and-fractions" title="Link to this heading">¶</a></h3>
  1387. <p>Mark Dickinson crafted an elegant and efficient scheme for assuring that
  1388. different numeric datatypes will have the same hash value whenever their actual
  1389. values are equal (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8188">bpo-8188</a>):</p>
  1390. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="nb">hash</span><span class="p">(</span><span class="n">Fraction</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span> <span class="o">==</span> <span class="nb">hash</span><span class="p">(</span><span class="mf">1.5</span><span class="p">)</span> <span class="o">==</span> \
  1391. <span class="nb">hash</span><span class="p">(</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">&quot;1.5&quot;</span><span class="p">))</span> <span class="o">==</span> <span class="nb">hash</span><span class="p">(</span><span class="nb">complex</span><span class="p">(</span><span class="mf">1.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
  1392. </pre></div>
  1393. </div>
  1394. <p>Some of the hashing details are exposed through a new attribute,
  1395. <a class="reference internal" href="../library/sys.html#sys.hash_info" title="sys.hash_info"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.hash_info</span></code></a>, which describes the bit width of the hash value, the
  1396. prime modulus, the hash values for <em>infinity</em> and <em>nan</em>, and the multiplier
  1397. used for the imaginary part of a number:</p>
  1398. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">hash_info</span>
  1399. <span class="go">sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003)</span>
  1400. </pre></div>
  1401. </div>
  1402. <p>An early decision to limit the interoperability of various numeric types has
  1403. been relaxed. It is still unsupported (and ill-advised) to have implicit
  1404. mixing in arithmetic expressions such as <code class="docutils literal notranslate"><span class="pre">Decimal('1.1')</span> <span class="pre">+</span> <span class="pre">float('1.1')</span></code>
  1405. because the latter loses information in the process of constructing the binary
  1406. float. However, since existing floating point value can be converted losslessly
  1407. to either a decimal or rational representation, it makes sense to add them to
  1408. the constructor and to support mixed-type comparisons.</p>
  1409. <ul class="simple">
  1410. <li><p>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.Decimal</span></code></a> constructor now accepts <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> objects
  1411. directly so there in no longer a need to use the <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>
  1412. method (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8257">bpo-8257</a>).</p></li>
  1413. <li><p>Mixed type comparisons are now fully supported so that
  1414. <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> objects can be directly compared with <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>
  1415. and <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">fractions.Fraction</span></code></a> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2531">bpo-2531</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8188">bpo-8188</a>).</p></li>
  1416. </ul>
  1417. <p>Similar changes were made to <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">fractions.Fraction</span></code></a> so that the
  1418. <a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_float" title="fractions.Fraction.from_float"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_float()</span></code></a> and <a class="reference internal" href="../library/fractions.html#fractions.Fraction.from_decimal" title="fractions.Fraction.from_decimal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">from_decimal()</span></code></a>
  1419. methods are no longer needed (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8294">bpo-8294</a>):</p>
  1420. <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">decimal</span> <span class="kn">import</span> <span class="n">Decimal</span>
  1421. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">fractions</span> <span class="kn">import</span> <span class="n">Fraction</span>
  1422. <span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
  1423. <span class="go">Decimal(&#39;1.100000000000000088817841970012523233890533447265625&#39;)</span>
  1424. <span class="gp">&gt;&gt;&gt; </span><span class="n">Fraction</span><span class="p">(</span><span class="mf">1.1</span><span class="p">)</span>
  1425. <span class="go">Fraction(2476979795053773, 2251799813685248)</span>
  1426. </pre></div>
  1427. </div>
  1428. <p>Another useful change for the <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic Specification."><code class="xref py py-mod docutils literal notranslate"><span class="pre">decimal</span></code></a> module is that the
  1429. <code class="xref py py-attr docutils literal notranslate"><span class="pre">Context.clamp</span></code> attribute is now public. This is useful in creating
  1430. contexts that correspond to the decimal interchange formats specified in IEEE
  1431. 754 (see <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8540">bpo-8540</a>).</p>
  1432. <p>(Contributed by Mark Dickinson and Raymond Hettinger.)</p>
  1433. </section>
  1434. <section id="ftp">
  1435. <h3>ftp<a class="headerlink" href="#ftp" title="Link to this heading">¶</a></h3>
  1436. <p>The <a class="reference internal" href="../library/ftplib.html#ftplib.FTP" title="ftplib.FTP"><code class="xref py py-class docutils literal notranslate"><span class="pre">ftplib.FTP</span></code></a> class now supports the context management protocol to
  1437. unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a> exceptions and to close the FTP
  1438. connection when done:</p>
  1439. <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">ftplib</span> <span class="kn">import</span> <span class="n">FTP</span>
  1440. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">FTP</span><span class="p">(</span><span class="s2">&quot;ftp1.at.proftpd.org&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">ftp</span><span class="p">:</span>
  1441. <span class="go"> ftp.login()</span>
  1442. <span class="go"> ftp.dir()</span>
  1443. <span class="go">&#39;230 Anonymous login ok, restrictions apply.&#39;</span>
  1444. <span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 .</span>
  1445. <span class="go">dr-xr-xr-x 9 ftp ftp 154 May 6 10:43 ..</span>
  1446. <span class="go">dr-xr-xr-x 5 ftp ftp 4096 May 6 10:43 CentOS</span>
  1447. <span class="go">dr-xr-xr-x 3 ftp ftp 18 Jul 10 2008 Fedora</span>
  1448. </pre></div>
  1449. </div>
  1450. <p>Other file-like objects such as <a class="reference internal" href="../library/mmap.html#mmap.mmap" title="mmap.mmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">mmap.mmap</span></code></a> and <a class="reference internal" href="../library/fileinput.html#fileinput.input" title="fileinput.input"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileinput.input()</span></code></a>
  1451. also grew auto-closing context managers:</p>
  1452. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">fileinput</span><span class="o">.</span><span class="n">input</span><span class="p">(</span><span class="n">files</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;log1.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;log2.txt&#39;</span><span class="p">))</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  1453. <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
  1454. <span class="n">process</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
  1455. </pre></div>
  1456. </div>
  1457. <p>(Contributed by Tarek Ziadé and Giampaolo Rodolà in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4972">bpo-4972</a>, and
  1458. by Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8046">bpo-8046</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1286">bpo-1286</a>.)</p>
  1459. <p>The <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> class now accepts a <em>context</em> parameter, which is a
  1460. <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">ssl.SSLContext</span></code></a> object allowing bundling SSL configuration options,
  1461. certificates and private keys into a single (potentially long-lived) structure.</p>
  1462. <p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8806">bpo-8806</a>.)</p>
  1463. </section>
  1464. <section id="popen">
  1465. <h3>popen<a class="headerlink" href="#popen" title="Link to this heading">¶</a></h3>
  1466. <p>The <a class="reference internal" href="../library/os.html#os.popen" title="os.popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.popen()</span></code></a> and <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-func docutils literal notranslate"><span class="pre">subprocess.Popen()</span></code></a> functions now support
  1467. <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 for auto-closing of the file descriptors.</p>
  1468. <p>(Contributed by Antoine Pitrou and Brian Curtin in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7461">bpo-7461</a> and
  1469. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10554">bpo-10554</a>.)</p>
  1470. </section>
  1471. <section id="select">
  1472. <h3>select<a class="headerlink" href="#select" title="Link to this heading">¶</a></h3>
  1473. <p>The <a class="reference internal" href="../library/select.html#module-select" title="select: Wait for I/O completion on multiple streams."><code class="xref py py-mod docutils literal notranslate"><span class="pre">select</span></code></a> module now exposes a new, constant attribute,
  1474. <a class="reference internal" href="../library/select.html#select.PIPE_BUF" title="select.PIPE_BUF"><code class="xref py py-const docutils literal notranslate"><span class="pre">PIPE_BUF</span></code></a>, which gives the minimum number of bytes which are
  1475. guaranteed not to block when <a class="reference internal" href="../library/select.html#select.select" title="select.select"><code class="xref py py-func docutils literal notranslate"><span class="pre">select.select()</span></code></a> says a pipe is ready
  1476. for writing.</p>
  1477. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">select</span>
  1478. <span class="gp">&gt;&gt;&gt; </span><span class="n">select</span><span class="o">.</span><span class="n">PIPE_BUF</span>
  1479. <span class="go">512</span>
  1480. </pre></div>
  1481. </div>
  1482. <p>(Available on Unix systems. Patch by Sébastien Sablé in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9862">bpo-9862</a>)</p>
  1483. </section>
  1484. <section id="gzip-and-zipfile">
  1485. <h3>gzip and zipfile<a class="headerlink" href="#gzip-and-zipfile" title="Link to this heading">¶</a></h3>
  1486. <p><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">gzip.GzipFile</span></code></a> now implements 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>
  1487. <a class="reference internal" href="../glossary.html#term-abstract-base-class"><span class="xref std std-term">abstract base class</span></a> (except for <code class="docutils literal notranslate"><span class="pre">truncate()</span></code>). It also has a
  1488. <a class="reference internal" href="../library/gzip.html#gzip.GzipFile.peek" title="gzip.GzipFile.peek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code></a> method and supports unseekable as well as
  1489. zero-padded file objects.</p>
  1490. <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 also gains the <a class="reference internal" href="../library/gzip.html#gzip.compress" title="gzip.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a> and
  1491. <a class="reference internal" href="../library/gzip.html#gzip.decompress" title="gzip.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> functions for easier in-memory compression and
  1492. decompression. Keep in mind that text needs to be encoded as <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>
  1493. before compressing and decompressing:</p>
  1494. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">gzip</span>
  1495. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;Three shall be the number thou shalt count, &#39;</span>
  1496. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">+=</span> <span class="s1">&#39;and the number of the counting shall be three&#39;</span>
  1497. <span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">encode</span><span class="p">()</span> <span class="c1"># convert to utf-8</span>
  1498. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  1499. <span class="go">89</span>
  1500. <span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">gzip</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
  1501. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
  1502. <span class="go">77</span>
  1503. <span class="gp">&gt;&gt;&gt; </span><span class="n">gzip</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">()[:</span><span class="mi">42</span><span class="p">]</span> <span class="c1"># decompress and convert to text</span>
  1504. <span class="go">&#39;Three shall be the number thou shalt count&#39;</span>
  1505. </pre></div>
  1506. </div>
  1507. <p>(Contributed by Anand B. Pillai in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3488">bpo-3488</a>; and by Antoine Pitrou, Nir
  1508. Aides and Brian Curtin in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9962">bpo-9962</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1675951">bpo-1675951</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7471">bpo-7471</a> and
  1509. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2846">bpo-2846</a>.)</p>
  1510. <p>Also, the <a class="reference internal" href="../library/zipfile.html#zipfile.ZipFile.open" title="zipfile.ZipFile.open"><code class="xref py py-class docutils literal notranslate"><span class="pre">zipfile.ZipExtFile</span></code></a> class was reworked internally to represent
  1511. files stored inside an archive. The new implementation is significantly faster
  1512. and can be wrapped in an <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> object for more speedups. It
  1513. also solves an issue where interleaved calls to <em>read</em> and <em>readline</em> gave the
  1514. wrong results.</p>
  1515. <p>(Patch submitted by Nir Aides in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7610">bpo-7610</a>.)</p>
  1516. </section>
  1517. <section id="tarfile">
  1518. <h3>tarfile<a class="headerlink" href="#tarfile" title="Link to this heading">¶</a></h3>
  1519. <p>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 can now be used as a context manager. In
  1520. addition, its <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> method has a new option, <em>filter</em>,
  1521. that controls which files are added to the archive and allows the file metadata
  1522. to be edited.</p>
  1523. <p>The new <em>filter</em> option replaces the older, less flexible <em>exclude</em> parameter
  1524. which is now deprecated. If specified, the optional <em>filter</em> parameter needs to
  1525. be a <a class="reference internal" href="../glossary.html#term-keyword-argument"><span class="xref std std-term">keyword argument</span></a>. The user-supplied filter function accepts a
  1526. <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> object and returns an updated
  1527. <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> object, or if it wants the file to be excluded, the
  1528. function can return <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
  1529. <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">tarfile</span><span class="o">,</span> <span class="nn">glob</span>
  1530. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">myfilter</span><span class="p">(</span><span class="n">tarinfo</span><span class="p">):</span>
  1531. <span class="gp">... </span> <span class="k">if</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isfile</span><span class="p">():</span> <span class="c1"># only save real files</span>
  1532. <span class="gp">... </span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">uname</span> <span class="o">=</span> <span class="s1">&#39;monty&#39;</span> <span class="c1"># redact the user name</span>
  1533. <span class="gp">... </span> <span class="k">return</span> <span class="n">tarinfo</span>
  1534. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;myarchive.tar.gz&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;w:gz&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">tf</span><span class="p">:</span>
  1535. <span class="gp">... </span> <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;*.txt&#39;</span><span class="p">):</span>
  1536. <span class="gp">... </span> <span class="n">tf</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">myfilter</span><span class="p">)</span>
  1537. <span class="gp">... </span> <span class="n">tf</span><span class="o">.</span><span class="n">list</span><span class="p">()</span>
  1538. <span class="go">-rw-r--r-- monty/501 902 2011-01-26 17:59:11 annotations.txt</span>
  1539. <span class="go">-rw-r--r-- monty/501 123 2011-01-26 17:59:11 general_questions.txt</span>
  1540. <span class="go">-rw-r--r-- monty/501 3514 2011-01-26 17:59:11 prion.txt</span>
  1541. <span class="go">-rw-r--r-- monty/501 124 2011-01-26 17:59:11 py_todo.txt</span>
  1542. <span class="go">-rw-r--r-- monty/501 1399 2011-01-26 17:59:11 semaphore_notes.txt</span>
  1543. </pre></div>
  1544. </div>
  1545. <p>(Proposed by Tarek Ziadé and implemented by Lars Gustäbel in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6856">bpo-6856</a>.)</p>
  1546. </section>
  1547. <section id="hashlib">
  1548. <h3>hashlib<a class="headerlink" href="#hashlib" title="Link to this heading">¶</a></h3>
  1549. <p>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 has two new constant attributes listing the hashing
  1550. algorithms guaranteed to be present in all implementations and those available
  1551. on the current implementation:</p>
  1552. <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">hashlib</span>
  1553. <span class="gp">&gt;&gt;&gt; </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_guaranteed</span>
  1554. <span class="go">{&#39;sha1&#39;, &#39;sha224&#39;, &#39;sha384&#39;, &#39;sha256&#39;, &#39;sha512&#39;, &#39;md5&#39;}</span>
  1555. <span class="gp">&gt;&gt;&gt; </span><span class="n">hashlib</span><span class="o">.</span><span class="n">algorithms_available</span>
  1556. <span class="go">{&#39;md2&#39;, &#39;SHA256&#39;, &#39;SHA512&#39;, &#39;dsaWithSHA&#39;, &#39;mdc2&#39;, &#39;SHA224&#39;, &#39;MD4&#39;, &#39;sha256&#39;,</span>
  1557. <span class="go">&#39;sha512&#39;, &#39;ripemd160&#39;, &#39;SHA1&#39;, &#39;MDC2&#39;, &#39;SHA&#39;, &#39;SHA384&#39;, &#39;MD2&#39;,</span>
  1558. <span class="go">&#39;ecdsa-with-SHA1&#39;,&#39;md4&#39;, &#39;md5&#39;, &#39;sha1&#39;, &#39;DSA-SHA&#39;, &#39;sha224&#39;,</span>
  1559. <span class="go">&#39;dsaEncryption&#39;, &#39;DSA&#39;, &#39;RIPEMD160&#39;, &#39;sha&#39;, &#39;MD5&#39;, &#39;sha384&#39;}</span>
  1560. </pre></div>
  1561. </div>
  1562. <p>(Suggested by Carl Chenet in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7418">bpo-7418</a>.)</p>
  1563. </section>
  1564. <section id="ast">
  1565. <h3>ast<a class="headerlink" href="#ast" title="Link to this heading">¶</a></h3>
  1566. <p>The <a class="reference internal" href="../library/ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module has a wonderful a general-purpose tool for safely
  1567. evaluating expression strings using the Python literal
  1568. syntax. The <a class="reference internal" href="../library/ast.html#ast.literal_eval" title="ast.literal_eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.literal_eval()</span></code></a> function serves as a secure alternative to
  1569. the builtin <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> function which is easily abused. Python 3.2 adds
  1570. <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> and <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> literals to the list of supported types:
  1571. strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  1572. <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">ast</span> <span class="kn">import</span> <span class="n">literal_eval</span>
  1573. <span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="s2">&quot;{&#39;req&#39;: 3, &#39;func&#39;: &#39;pow&#39;, &#39;args&#39;: (2, 0.5)}&quot;</span>
  1574. <span class="gp">&gt;&gt;&gt; </span><span class="n">literal_eval</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
  1575. <span class="go">{&#39;args&#39;: (2, 0.5), &#39;req&#39;: 3, &#39;func&#39;: &#39;pow&#39;}</span>
  1576. <span class="gp">&gt;&gt;&gt; </span><span class="n">request</span> <span class="o">=</span> <span class="s2">&quot;os.system(&#39;do something harmful&#39;)&quot;</span>
  1577. <span class="gp">&gt;&gt;&gt; </span><span class="n">literal_eval</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
  1578. <span class="gt">Traceback (most recent call last):</span>
  1579. <span class="w"> </span><span class="c">...</span>
  1580. <span class="gr">ValueError</span>: <span class="n">malformed node or string: &lt;_ast.Call object at 0x101739a10&gt;</span>
  1581. </pre></div>
  1582. </div>
  1583. <p>(Implemented by Benjamin Peterson and Georg Brandl.)</p>
  1584. </section>
  1585. <section id="os">
  1586. <h3>os<a class="headerlink" href="#os" title="Link to this heading">¶</a></h3>
  1587. <p>Different operating systems use various encodings for filenames and environment
  1588. variables. 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 provides two new functions,
  1589. <a class="reference internal" href="../library/os.html#os.fsencode" title="os.fsencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsencode()</span></code></a> and <a class="reference internal" href="../library/os.html#os.fsdecode" title="os.fsdecode"><code class="xref py py-func docutils literal notranslate"><span class="pre">fsdecode()</span></code></a>, for encoding and decoding
  1590. filenames:</p>
  1591. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">os</span>
  1592. <span class="gp">&gt;&gt;&gt; </span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;Sehenswürdigkeiten&#39;</span>
  1593. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">fsencode</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
  1594. <span class="go">b&#39;Sehensw\xc3\xbcrdigkeiten&#39;</span>
  1595. </pre></div>
  1596. </div>
  1597. <p>Some operating systems allow direct access to encoded bytes in the
  1598. environment. If so, the <a class="reference internal" href="../library/os.html#os.supports_bytes_environ" title="os.supports_bytes_environ"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.supports_bytes_environ</span></code></a> constant will be
  1599. true.</p>
  1600. <p>For direct access to encoded environment variables (if available),
  1601. use the new <a class="reference internal" href="../library/os.html#os.getenvb" title="os.getenvb"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.getenvb()</span></code></a> function or use <a class="reference internal" href="../library/os.html#os.environb" title="os.environb"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environb</span></code></a>
  1602. which is a bytes version of <a class="reference internal" href="../library/os.html#os.environ" title="os.environ"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.environ</span></code></a>.</p>
  1603. <p>(Contributed by Victor Stinner.)</p>
  1604. </section>
  1605. <section id="shutil">
  1606. <h3>shutil<a class="headerlink" href="#shutil" title="Link to this heading">¶</a></h3>
  1607. <p>The <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">shutil.copytree()</span></code></a> function has two new options:</p>
  1608. <ul class="simple">
  1609. <li><p><em>ignore_dangling_symlinks</em>: when <code class="docutils literal notranslate"><span class="pre">symlinks=False</span></code> so that the function
  1610. copies a file pointed to by a symlink, not the symlink itself. This option
  1611. will silence the error raised if the file doesn’t exist.</p></li>
  1612. <li><p><em>copy_function</em>: is a callable that will be used to copy files.
  1613. <a class="reference internal" href="../library/shutil.html#shutil.copy2" title="shutil.copy2"><code class="xref py py-func docutils literal notranslate"><span class="pre">shutil.copy2()</span></code></a> is used by default.</p></li>
  1614. </ul>
  1615. <p>(Contributed by Tarek Ziadé.)</p>
  1616. <p>In addition, 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 now supports <a class="reference internal" href="../library/shutil.html#archiving-operations"><span class="std std-ref">archiving operations</span></a> for zipfiles, uncompressed tarfiles, gzipped tarfiles,
  1617. and bzipped tarfiles. And there are functions for registering additional
  1618. archiving file formats (such as xz compressed tarfiles or custom formats).</p>
  1619. <p>The principal functions are <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> and
  1620. <a class="reference internal" href="../library/shutil.html#shutil.unpack_archive" title="shutil.unpack_archive"><code class="xref py py-func docutils literal notranslate"><span class="pre">unpack_archive()</span></code></a>. By default, both operate on the current
  1621. directory (which can be set by <a class="reference internal" href="../library/os.html#os.chdir" title="os.chdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.chdir()</span></code></a>) and on any sub-directories.
  1622. The archive filename needs to be specified with a full pathname. The archiving
  1623. step is non-destructive (the original files are left unchanged).</p>
  1624. <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">shutil</span><span class="o">,</span> <span class="nn">pprint</span>
  1625. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;mydata&#39;</span><span class="p">)</span> <span class="c1"># change to the source directory</span>
  1626. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">shutil</span><span class="o">.</span><span class="n">make_archive</span><span class="p">(</span><span class="s1">&#39;/var/backup/mydata&#39;</span><span class="p">,</span>
  1627. <span class="gp">... </span> <span class="s1">&#39;zip&#39;</span><span class="p">)</span> <span class="c1"># archive the current directory</span>
  1628. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="c1"># show the name of archive</span>
  1629. <span class="go">&#39;/var/backup/mydata.zip&#39;</span>
  1630. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;tmp&#39;</span><span class="p">)</span> <span class="c1"># change to an unpacking</span>
  1631. <span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">unpack_archive</span><span class="p">(</span><span class="s1">&#39;/var/backup/mydata.zip&#39;</span><span class="p">)</span> <span class="c1"># recover the data</span>
  1632. <span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">shutil</span><span class="o">.</span><span class="n">get_archive_formats</span><span class="p">())</span> <span class="c1"># display known formats</span>
  1633. <span class="go">[(&#39;bztar&#39;, &quot;bzip2&#39;ed tar-file&quot;),</span>
  1634. <span class="go"> (&#39;gztar&#39;, &quot;gzip&#39;ed tar-file&quot;),</span>
  1635. <span class="go"> (&#39;tar&#39;, &#39;uncompressed tar file&#39;),</span>
  1636. <span class="go"> (&#39;zip&#39;, &#39;ZIP file&#39;)]</span>
  1637. <span class="gp">&gt;&gt;&gt; </span><span class="n">shutil</span><span class="o">.</span><span class="n">register_archive_format</span><span class="p">(</span> <span class="c1"># register a new archive format</span>
  1638. <span class="gp">... </span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;xz&#39;</span><span class="p">,</span>
  1639. <span class="gp">... </span> <span class="n">function</span><span class="o">=</span><span class="n">xz</span><span class="o">.</span><span class="n">compress</span><span class="p">,</span> <span class="c1"># callable archiving function</span>
  1640. <span class="gp">... </span> <span class="n">extra_args</span><span class="o">=</span><span class="p">[(</span><span class="s1">&#39;level&#39;</span><span class="p">,</span> <span class="mi">8</span><span class="p">)],</span> <span class="c1"># arguments to the function</span>
  1641. <span class="gp">... </span> <span class="n">description</span><span class="o">=</span><span class="s1">&#39;xz compression&#39;</span>
  1642. <span class="gp">... </span><span class="p">)</span>
  1643. </pre></div>
  1644. </div>
  1645. <p>(Contributed by Tarek Ziadé.)</p>
  1646. </section>
  1647. <section id="sqlite3">
  1648. <h3>sqlite3<a class="headerlink" href="#sqlite3" title="Link to this heading">¶</a></h3>
  1649. <p>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 was updated to pysqlite version 2.6.0. It has two new capabilities.</p>
  1650. <ul class="simple">
  1651. <li><p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">sqlite3.Connection.in_transit</span></code> attribute is true if there is an
  1652. active transaction for uncommitted changes.</p></li>
  1653. <li><p>The <a class="reference internal" href="../library/sqlite3.html#sqlite3.Connection.enable_load_extension" title="sqlite3.Connection.enable_load_extension"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sqlite3.Connection.enable_load_extension()</span></code></a> and
  1654. <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">sqlite3.Connection.load_extension()</span></code></a> methods allows you to load SQLite
  1655. extensions from “.so” files. One well-known extension is the fulltext-search
  1656. extension distributed with SQLite.</p></li>
  1657. </ul>
  1658. <p>(Contributed by R. David Murray and Shashwat Anand; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8845">bpo-8845</a>.)</p>
  1659. </section>
  1660. <section id="html">
  1661. <h3>html<a class="headerlink" href="#html" title="Link to this heading">¶</a></h3>
  1662. <p>A new <a class="reference internal" href="../library/html.html#module-html" title="html: Helpers for manipulating HTML."><code class="xref py py-mod docutils literal notranslate"><span class="pre">html</span></code></a> module was introduced with only a single function,
  1663. <a class="reference internal" href="../library/html.html#html.escape" title="html.escape"><code class="xref py py-func docutils literal notranslate"><span class="pre">escape()</span></code></a>, which is used for escaping reserved characters from HTML
  1664. markup:</p>
  1665. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">html</span>
  1666. <span class="gp">&gt;&gt;&gt; </span><span class="n">html</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="s1">&#39;x &gt; 2 &amp;&amp; x &lt; 7&#39;</span><span class="p">)</span>
  1667. <span class="go">&#39;x &amp;gt; 2 &amp;amp;&amp;amp; x &amp;lt; 7&#39;</span>
  1668. </pre></div>
  1669. </div>
  1670. </section>
  1671. <section id="socket">
  1672. <h3>socket<a class="headerlink" href="#socket" title="Link to this heading">¶</a></h3>
  1673. <p>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 has two new improvements.</p>
  1674. <ul class="simple">
  1675. <li><p>Socket objects now have a <a class="reference internal" href="../library/socket.html#socket.socket.detach" title="socket.socket.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a> method which puts
  1676. the socket into closed state without actually closing the underlying file
  1677. descriptor. The latter can then be reused for other purposes.
  1678. (Added by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8524">bpo-8524</a>.)</p></li>
  1679. <li><p><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">socket.create_connection()</span></code></a> now supports the context management protocol
  1680. to unconditionally consume <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.error</span></code></a> exceptions and to close the
  1681. socket when done.
  1682. (Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9794">bpo-9794</a>.)</p></li>
  1683. </ul>
  1684. </section>
  1685. <section id="ssl">
  1686. <h3>ssl<a class="headerlink" href="#ssl" title="Link to this heading">¶</a></h3>
  1687. <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 added a number of features to satisfy common requirements
  1688. for secure (encrypted, authenticated) internet connections:</p>
  1689. <ul class="simple">
  1690. <li><p>A new class, <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>, serves as a container for persistent
  1691. SSL data, such as protocol settings, certificates, private keys, and various
  1692. other options. It includes a <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">wrap_socket()</span></code></a> for creating
  1693. an SSL socket from an SSL context.</p></li>
  1694. <li><p>A new function, <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.match_hostname()</span></code>, supports server identity
  1695. verification for higher-level protocols by implementing the rules of HTTPS
  1696. (from <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2818.html"><strong>RFC 2818</strong></a>) which are also suitable for other protocols.</p></li>
  1697. <li><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">ssl.wrap_socket()</span></code></a> constructor function now takes a <em>ciphers</em>
  1698. argument. The <em>ciphers</em> string lists the allowed encryption algorithms using
  1699. the format described in the <a class="reference external" href="https://www.openssl.org/docs/man1.0.2/man1/ciphers.html#CIPHER-LIST-FORMAT">OpenSSL documentation</a>.</p></li>
  1700. <li><p>When linked against recent versions of OpenSSL, 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 now
  1701. supports the Server Name Indication extension to the TLS protocol, allowing
  1702. multiple “virtual hosts” using different certificates on a single IP port.
  1703. This extension is only supported in client mode, and is activated by passing
  1704. the <em>server_hostname</em> argument to <a class="reference internal" href="../library/ssl.html#ssl.SSLContext.wrap_socket" title="ssl.SSLContext.wrap_socket"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.wrap_socket()</span></code></a>.</p></li>
  1705. <li><p>Various options have been added to 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, such as
  1706. <a class="reference internal" href="../library/ssl.html#ssl.OP_NO_SSLv2" title="ssl.OP_NO_SSLv2"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP_NO_SSLv2</span></code></a> which disables the insecure and obsolete SSLv2
  1707. protocol.</p></li>
  1708. <li><p>The extension now loads all the OpenSSL ciphers and digest algorithms. If
  1709. some SSL certificates cannot be verified, they are reported as an “unknown
  1710. algorithm” error.</p></li>
  1711. <li><p>The version of OpenSSL being used is now accessible using the module
  1712. 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),
  1713. <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
  1714. <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).</p></li>
  1715. </ul>
  1716. <p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8850">bpo-8850</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1589">bpo-1589</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8322">bpo-8322</a>,
  1717. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5639">bpo-5639</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4870">bpo-4870</a>, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8484">bpo-8484</a>, and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8321">bpo-8321</a>.)</p>
  1718. </section>
  1719. <section id="nntp">
  1720. <h3>nntp<a class="headerlink" href="#nntp" title="Link to this heading">¶</a></h3>
  1721. <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 has a revamped implementation with better bytes and
  1722. text semantics as well as more practical APIs. These improvements break
  1723. compatibility with the nntplib version in Python 3.1, which was partly
  1724. dysfunctional in itself.</p>
  1725. <p>Support for secure connections through both implicit (using
  1726. <a class="reference internal" href="../library/nntplib.html#nntplib.NNTP_SSL" title="nntplib.NNTP_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">nntplib.NNTP_SSL</span></code></a>) and explicit (using <a class="reference internal" href="../library/nntplib.html#nntplib.NNTP.starttls" title="nntplib.NNTP.starttls"><code class="xref py py-meth docutils literal notranslate"><span class="pre">nntplib.NNTP.starttls()</span></code></a>)
  1727. TLS has also been added.</p>
  1728. <p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9360">bpo-9360</a> and Andrew Vant in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1926">bpo-1926</a>.)</p>
  1729. </section>
  1730. <section id="certificates">
  1731. <h3>certificates<a class="headerlink" href="#certificates" title="Link to this heading">¶</a></h3>
  1732. <p><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">http.client.HTTPSConnection</span></code></a>, <a class="reference internal" href="../library/urllib.request.html#urllib.request.HTTPSHandler" title="urllib.request.HTTPSHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.request.HTTPSHandler</span></code></a>
  1733. and <a class="reference internal" href="../library/urllib.request.html#urllib.request.urlopen" title="urllib.request.urlopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">urllib.request.urlopen()</span></code></a> now take optional arguments to allow for
  1734. server certificate checking against a set of Certificate Authorities,
  1735. as recommended in public uses of HTTPS.</p>
  1736. <p>(Added by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9003">bpo-9003</a>.)</p>
  1737. </section>
  1738. <section id="imaplib">
  1739. <h3>imaplib<a class="headerlink" href="#imaplib" title="Link to this heading">¶</a></h3>
  1740. <p>Support for explicit TLS on standard IMAP4 connections has been added through
  1741. the new <a class="reference internal" href="../library/imaplib.html#imaplib.IMAP4.starttls" title="imaplib.IMAP4.starttls"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imaplib.IMAP4.starttls</span></code></a> method.</p>
  1742. <p>(Contributed by Lorenzo M. Catucci and Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=4471">bpo-4471</a>.)</p>
  1743. </section>
  1744. <section id="http-client">
  1745. <h3>http.client<a class="headerlink" href="#http-client" title="Link to this heading">¶</a></h3>
  1746. <p>There were a number of small API improvements in the <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> module.
  1747. The old-style HTTP 0.9 simple responses are no longer supported and the <em>strict</em>
  1748. parameter is deprecated in all classes.</p>
  1749. <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
  1750. <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 now have a <em>source_address</em>
  1751. parameter for a (host, port) tuple indicating where the HTTP connection is made
  1752. from.</p>
  1753. <p>Support for certificate checking and HTTPS virtual hosts were added to
  1754. <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>.</p>
  1755. <p>The <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.request" title="http.client.HTTPConnection.request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">request()</span></code></a> method on connection objects
  1756. allowed an optional <em>body</em> argument so that a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> could be used
  1757. to supply the content of the request. Conveniently, the <em>body</em> argument now
  1758. also accepts an <a class="reference internal" href="../glossary.html#term-iterable"><span class="xref std std-term">iterable</span></a> object so long as it includes an explicit
  1759. <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code> header. This extended interface is much more flexible than
  1760. before.</p>
  1761. <p>To establish an HTTPS connection through a proxy server, there is a new
  1762. <a class="reference internal" href="../library/http.client.html#http.client.HTTPConnection.set_tunnel" title="http.client.HTTPConnection.set_tunnel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_tunnel()</span></code></a> method that sets the host and
  1763. port for HTTP Connect tunneling.</p>
  1764. <p>To match the behavior of <a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a>, the HTTP client library now also
  1765. encodes headers with ISO-8859-1 (Latin-1) encoding. It was already doing that
  1766. for incoming headers, so now the behavior is consistent for both incoming and
  1767. outgoing traffic. (See work by Armin Ronacher in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10980">bpo-10980</a>.)</p>
  1768. </section>
  1769. <section id="unittest">
  1770. <h3>unittest<a class="headerlink" href="#unittest" title="Link to this heading">¶</a></h3>
  1771. <p>The unittest module has a number of improvements supporting test discovery for
  1772. packages, easier experimentation at the interactive prompt, new testcase
  1773. methods, improved diagnostic messages for test failures, and better method
  1774. names.</p>
  1775. <ul>
  1776. <li><p>The command-line call <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">unittest</span></code> can now accept file paths
  1777. instead of module names for running specific tests (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10620">bpo-10620</a>). The new
  1778. test discovery can find tests within packages, locating any test importable
  1779. from the top-level directory. The top-level directory can be specified with
  1780. the <code class="docutils literal notranslate"><span class="pre">-t</span></code> option, a pattern for matching files with <code class="docutils literal notranslate"><span class="pre">-p</span></code>, and a directory to
  1781. start discovery with <code class="docutils literal notranslate"><span class="pre">-s</span></code>:</p>
  1782. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>unittest<span class="w"> </span>discover<span class="w"> </span>-s<span class="w"> </span>my_proj_dir<span class="w"> </span>-p<span class="w"> </span>_test.py
  1783. </pre></div>
  1784. </div>
  1785. <p>(Contributed by Michael Foord.)</p>
  1786. </li>
  1787. <li><p>Experimentation at the interactive prompt is now easier because the
  1788. <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">unittest.TestCase</span></code></a> class can now be instantiated without
  1789. arguments:</p>
  1790. <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">unittest</span> <span class="kn">import</span> <span class="n">TestCase</span>
  1791. <span class="gp">&gt;&gt;&gt; </span><span class="n">TestCase</span><span class="p">()</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="nb">pow</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">8</span><span class="p">)</span>
  1792. </pre></div>
  1793. </div>
  1794. <p>(Contributed by Michael Foord.)</p>
  1795. </li>
  1796. <li><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 has two new methods,
  1797. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarns" title="unittest.TestCase.assertWarns"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarns()</span></code></a> and
  1798. <a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertWarnsRegex" title="unittest.TestCase.assertWarnsRegex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertWarnsRegex()</span></code></a> to verify that a given warning type
  1799. is triggered by the code under test:</p>
  1800. <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">assertWarns</span><span class="p">(</span><span class="ne">DeprecationWarning</span><span class="p">):</span>
  1801. <span class="n">legacy_function</span><span class="p">(</span><span class="s1">&#39;XYZ&#39;</span><span class="p">)</span>
  1802. </pre></div>
  1803. </div>
  1804. <p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9754">bpo-9754</a>.)</p>
  1805. <p>Another new method, <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">assertCountEqual()</span></code></a> is used to
  1806. compare two iterables to determine if their element counts are equal (whether
  1807. the same elements are present with the same number of occurrences regardless
  1808. of order):</p>
  1809. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_anagram</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1810. <span class="bp">self</span><span class="o">.</span><span class="n">assertCountEqual</span><span class="p">(</span><span class="s1">&#39;algorithm&#39;</span><span class="p">,</span> <span class="s1">&#39;logarithm&#39;</span><span class="p">)</span>
  1811. </pre></div>
  1812. </div>
  1813. <p>(Contributed by Raymond Hettinger.)</p>
  1814. </li>
  1815. <li><p>A principal feature of the unittest module is an effort to produce meaningful
  1816. diagnostics when a test fails. When possible, the failure is recorded along
  1817. with a diff of the output. This is especially helpful for analyzing log files
  1818. of failed test runs. However, since diffs can sometime be voluminous, there is
  1819. a new <a class="reference internal" href="../library/unittest.html#unittest.TestCase.maxDiff" title="unittest.TestCase.maxDiff"><code class="xref py py-attr docutils literal notranslate"><span class="pre">maxDiff</span></code></a> attribute that sets maximum length of
  1820. diffs displayed.</p></li>
  1821. <li><p>In addition, the method names in the module have undergone a number of clean-ups.</p>
  1822. <p>For example, <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">assertRegex()</span></code></a> is the new name for
  1823. <code class="xref py py-meth docutils literal notranslate"><span class="pre">assertRegexpMatches()</span></code> which was misnamed because the
  1824. test uses <a class="reference internal" href="../library/re.html#re.search" title="re.search"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.search()</span></code></a>, not <a class="reference internal" href="../library/re.html#re.match" title="re.match"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.match()</span></code></a>. Other methods using
  1825. regular expressions are now named using short form “Regex” in preference to
  1826. “Regexp” – this matches the names used in other unittest implementations,
  1827. matches Python’s old name for 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, and it has unambiguous
  1828. camel-casing.</p>
  1829. <p>(Contributed by Raymond Hettinger and implemented by Ezio Melotti.)</p>
  1830. </li>
  1831. <li><p>To improve consistency, some long-standing method aliases are being
  1832. deprecated in favor of the preferred names:</p>
  1833. <blockquote>
  1834. <div><table class="docutils align-default">
  1835. <thead>
  1836. <tr class="row-odd"><th class="head"><p>Old Name</p></th>
  1837. <th class="head"><p>Preferred Name</p></th>
  1838. </tr>
  1839. </thead>
  1840. <tbody>
  1841. <tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assert_()</span></code></p></td>
  1842. <td><p><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></p></td>
  1843. </tr>
  1844. <tr class="row-odd"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertEquals()</span></code></p></td>
  1845. <td><p><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></td>
  1846. </tr>
  1847. <tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEquals()</span></code></p></td>
  1848. <td><p><a class="reference internal" href="../library/unittest.html#unittest.TestCase.assertNotEqual" title="unittest.TestCase.assertNotEqual"><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotEqual()</span></code></a></p></td>
  1849. </tr>
  1850. <tr class="row-odd"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertAlmostEquals()</span></code></p></td>
  1851. <td><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></p></td>
  1852. </tr>
  1853. <tr class="row-even"><td><p><code class="xref py py-meth docutils literal notranslate"><span class="pre">assertNotAlmostEquals()</span></code></p></td>
  1854. <td><p><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></p></td>
  1855. </tr>
  1856. </tbody>
  1857. </table>
  1858. </div></blockquote>
  1859. <p>Likewise, the <code class="docutils literal notranslate"><span class="pre">TestCase.fail*</span></code> methods deprecated in Python 3.1 are expected
  1860. to be removed in Python 3.3.</p>
  1861. <p>(Contributed by Ezio Melotti; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9424">bpo-9424</a>.)</p>
  1862. </li>
  1863. <li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">assertDictContainsSubset()</span></code> method was deprecated
  1864. because it was misimplemented with the arguments in the wrong order. This
  1865. created hard-to-debug optical illusions where tests like
  1866. <code class="docutils literal notranslate"><span class="pre">TestCase().assertDictContainsSubset({'a':1,</span> <span class="pre">'b':2},</span> <span class="pre">{'a':1})</span></code> would fail.</p>
  1867. <p>(Contributed by Raymond Hettinger.)</p>
  1868. </li>
  1869. </ul>
  1870. </section>
  1871. <section id="random">
  1872. <h3>random<a class="headerlink" href="#random" title="Link to this heading">¶</a></h3>
  1873. <p>The integer methods in the <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> module now do a better job of producing
  1874. uniform distributions. Previously, they computed selections with
  1875. <code class="docutils literal notranslate"><span class="pre">int(n*random())</span></code> which had a slight bias whenever <em>n</em> was not a power of two.
  1876. Now, multiple selections are made from a range up to the next power of two and a
  1877. selection is kept only when it falls within the range <code class="docutils literal notranslate"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">n</span></code>. The
  1878. functions and methods affected are <a class="reference internal" href="../library/random.html#random.randrange" title="random.randrange"><code class="xref py py-func docutils literal notranslate"><span class="pre">randrange()</span></code></a>,
  1879. <a class="reference internal" href="../library/random.html#random.randint" title="random.randint"><code class="xref py py-func docutils literal notranslate"><span class="pre">randint()</span></code></a>, <a class="reference internal" href="../library/random.html#random.choice" title="random.choice"><code class="xref py py-func docutils literal notranslate"><span class="pre">choice()</span></code></a>, <a class="reference internal" href="../library/random.html#random.shuffle" title="random.shuffle"><code class="xref py py-func docutils literal notranslate"><span class="pre">shuffle()</span></code></a> and
  1880. <a class="reference internal" href="../library/random.html#random.sample" title="random.sample"><code class="xref py py-func docutils literal notranslate"><span class="pre">sample()</span></code></a>.</p>
  1881. <p>(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9025">bpo-9025</a>.)</p>
  1882. </section>
  1883. <section id="poplib">
  1884. <h3>poplib<a class="headerlink" href="#poplib" title="Link to this heading">¶</a></h3>
  1885. <p><a class="reference internal" href="../library/poplib.html#poplib.POP3_SSL" title="poplib.POP3_SSL"><code class="xref py py-class docutils literal notranslate"><span class="pre">POP3_SSL</span></code></a> class now accepts a <em>context</em> parameter, which is a
  1886. <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">ssl.SSLContext</span></code></a> object allowing bundling SSL configuration options,
  1887. certificates and private keys into a single (potentially long-lived)
  1888. structure.</p>
  1889. <p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8807">bpo-8807</a>.)</p>
  1890. </section>
  1891. <section id="asyncore">
  1892. <h3>asyncore<a class="headerlink" href="#asyncore" title="Link to this heading">¶</a></h3>
  1893. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">asyncore.dispatcher</span></code> now provides a
  1894. <code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_accepted()</span></code> method
  1895. returning a <code class="docutils literal notranslate"><span class="pre">(sock,</span> <span class="pre">addr)</span></code> pair which is called when a connection has actually
  1896. been established with a new remote endpoint. This is supposed to be used as a
  1897. replacement for old <code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_accept()</span></code> and avoids
  1898. the user to call <code class="xref py py-meth docutils literal notranslate"><span class="pre">accept()</span></code> directly.</p>
  1899. <p>(Contributed by Giampaolo Rodolà; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6706">bpo-6706</a>.)</p>
  1900. </section>
  1901. <section id="tempfile">
  1902. <h3>tempfile<a class="headerlink" href="#tempfile" title="Link to this heading">¶</a></h3>
  1903. <p>The <a class="reference internal" href="../library/tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a> module has a new context manager,
  1904. <a class="reference internal" href="../library/tempfile.html#tempfile.TemporaryDirectory" title="tempfile.TemporaryDirectory"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemporaryDirectory</span></code></a> which provides easy deterministic
  1905. cleanup of temporary directories:</p>
  1906. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryDirectory</span><span class="p">()</span> <span class="k">as</span> <span class="n">tmpdirname</span><span class="p">:</span>
  1907. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;created temporary dir:&#39;</span><span class="p">,</span> <span class="n">tmpdirname</span><span class="p">)</span>
  1908. </pre></div>
  1909. </div>
  1910. <p>(Contributed by Neil Schemenauer and Nick Coghlan; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5178">bpo-5178</a>.)</p>
  1911. </section>
  1912. <section id="inspect">
  1913. <h3>inspect<a class="headerlink" href="#inspect" title="Link to this heading">¶</a></h3>
  1914. <ul>
  1915. <li><p>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 has a new function
  1916. <a class="reference internal" href="../library/inspect.html#inspect.getgeneratorstate" title="inspect.getgeneratorstate"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorstate()</span></code></a> to easily identify the current state of a
  1917. generator-iterator:</p>
  1918. <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">getgeneratorstate</span>
  1919. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span>
  1920. <span class="gp">... </span> <span class="k">yield</span> <span class="s1">&#39;demo&#39;</span>
  1921. <span class="gp">...</span>
  1922. <span class="gp">&gt;&gt;&gt; </span><span class="n">g</span> <span class="o">=</span> <span class="n">gen</span><span class="p">()</span>
  1923. <span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  1924. <span class="go">&#39;GEN_CREATED&#39;</span>
  1925. <span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  1926. <span class="go">&#39;demo&#39;</span>
  1927. <span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  1928. <span class="go">&#39;GEN_SUSPENDED&#39;</span>
  1929. <span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">g</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
  1930. <span class="gp">&gt;&gt;&gt; </span><span class="n">getgeneratorstate</span><span class="p">(</span><span class="n">g</span><span class="p">)</span>
  1931. <span class="go">&#39;GEN_CLOSED&#39;</span>
  1932. </pre></div>
  1933. </div>
  1934. <p>(Contributed by Rodolpho Eckhardt and Nick Coghlan, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10220">bpo-10220</a>.)</p>
  1935. </li>
  1936. <li><p>To support lookups without the possibility of activating a dynamic attribute,
  1937. 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 has a new function, <a class="reference internal" href="../library/inspect.html#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a>.
  1938. Unlike <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a>, this is a true read-only search, guaranteed not to
  1939. change state while it is searching:</p>
  1940. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  1941. <span class="gp">... </span> <span class="nd">@property</span>
  1942. <span class="gp">... </span> <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  1943. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Running&#39;</span><span class="p">)</span>
  1944. <span class="gp">... </span> <span class="k">return</span> <span class="mi">10</span>
  1945. <span class="gp">...</span>
  1946. <span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
  1947. <span class="gp">&gt;&gt;&gt; </span><span class="nb">getattr</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">&#39;f&#39;</span><span class="p">)</span>
  1948. <span class="go">Running</span>
  1949. <span class="go">10</span>
  1950. <span class="gp">&gt;&gt;&gt; </span><span class="n">inspect</span><span class="o">.</span><span class="n">getattr_static</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">&#39;f&#39;</span><span class="p">)</span>
  1951. <span class="go">&lt;property object at 0x1022bd788&gt;</span>
  1952. </pre></div>
  1953. </div>
  1954. </li>
  1955. </ul>
  1956. <blockquote>
  1957. <div><p>(Contributed by Michael Foord.)</p>
  1958. </div></blockquote>
  1959. </section>
  1960. <section id="pydoc">
  1961. <h3>pydoc<a class="headerlink" href="#pydoc" title="Link to this heading">¶</a></h3>
  1962. <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 provides a much-improved web server interface, as
  1963. well as a new command-line option <code class="docutils literal notranslate"><span class="pre">-b</span></code> to automatically open a browser window
  1964. to display that server:</p>
  1965. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>pydoc3.2<span class="w"> </span>-b
  1966. </pre></div>
  1967. </div>
  1968. <p>(Contributed by Ron Adam; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2001">bpo-2001</a>.)</p>
  1969. </section>
  1970. <section id="dis">
  1971. <h3>dis<a class="headerlink" href="#dis" title="Link to this heading">¶</a></h3>
  1972. <p>The <a class="reference internal" href="../library/dis.html#module-dis" title="dis: Disassembler for Python bytecode."><code class="xref py py-mod docutils literal notranslate"><span class="pre">dis</span></code></a> module gained two new functions for inspecting code,
  1973. <a class="reference internal" href="../library/dis.html#dis.code_info" title="dis.code_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">code_info()</span></code></a> and <a class="reference internal" href="../library/dis.html#dis.show_code" title="dis.show_code"><code class="xref py py-func docutils literal notranslate"><span class="pre">show_code()</span></code></a>. Both provide detailed code
  1974. object information for the supplied function, method, source code string or code
  1975. object. The former returns a string and the latter prints it:</p>
  1976. <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">dis</span><span class="o">,</span> <span class="nn">random</span>
  1977. <span class="gp">&gt;&gt;&gt; </span><span class="n">dis</span><span class="o">.</span><span class="n">show_code</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">)</span>
  1978. <span class="go">Name: choice</span>
  1979. <span class="go">Filename: /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/random.py</span>
  1980. <span class="go">Argument count: 2</span>
  1981. <span class="go">Kw-only arguments: 0</span>
  1982. <span class="go">Number of locals: 3</span>
  1983. <span class="go">Stack size: 11</span>
  1984. <span class="go">Flags: OPTIMIZED, NEWLOCALS, NOFREE</span>
  1985. <span class="go">Constants:</span>
  1986. <span class="go"> 0: &#39;Choose a random element from a non-empty sequence.&#39;</span>
  1987. <span class="go"> 1: &#39;Cannot choose from an empty sequence&#39;</span>
  1988. <span class="go">Names:</span>
  1989. <span class="go"> 0: _randbelow</span>
  1990. <span class="go"> 1: len</span>
  1991. <span class="go"> 2: ValueError</span>
  1992. <span class="go"> 3: IndexError</span>
  1993. <span class="go">Variable names:</span>
  1994. <span class="go"> 0: self</span>
  1995. <span class="go"> 1: seq</span>
  1996. <span class="go"> 2: i</span>
  1997. </pre></div>
  1998. </div>
  1999. <p>In addition, the <a class="reference internal" href="../library/dis.html#dis.dis" title="dis.dis"><code class="xref py py-func docutils literal notranslate"><span class="pre">dis()</span></code></a> function now accepts string arguments
  2000. so that the common idiom <code class="docutils literal notranslate"><span class="pre">dis(compile(s,</span> <span class="pre">'',</span> <span class="pre">'eval'))</span></code> can be shortened
  2001. to <code class="docutils literal notranslate"><span class="pre">dis(s)</span></code>:</p>
  2002. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dis</span><span class="p">(</span><span class="s1">&#39;3*x+1 if x%2==1 else x//2&#39;</span><span class="p">)</span>
  2003. <span class="go"> 1 0 LOAD_NAME 0 (x)</span>
  2004. <span class="go"> 3 LOAD_CONST 0 (2)</span>
  2005. <span class="go"> 6 BINARY_MODULO</span>
  2006. <span class="go"> 7 LOAD_CONST 1 (1)</span>
  2007. <span class="go"> 10 COMPARE_OP 2 (==)</span>
  2008. <span class="go"> 13 POP_JUMP_IF_FALSE 28</span>
  2009. <span class="go"> 16 LOAD_CONST 2 (3)</span>
  2010. <span class="go"> 19 LOAD_NAME 0 (x)</span>
  2011. <span class="go"> 22 BINARY_MULTIPLY</span>
  2012. <span class="go"> 23 LOAD_CONST 1 (1)</span>
  2013. <span class="go"> 26 BINARY_ADD</span>
  2014. <span class="go"> 27 RETURN_VALUE</span>
  2015. <span class="go"> &gt;&gt; 28 LOAD_NAME 0 (x)</span>
  2016. <span class="go"> 31 LOAD_CONST 0 (2)</span>
  2017. <span class="go"> 34 BINARY_FLOOR_DIVIDE</span>
  2018. <span class="go"> 35 RETURN_VALUE</span>
  2019. </pre></div>
  2020. </div>
  2021. <p>Taken together, these improvements make it easier to explore how CPython is
  2022. implemented and to see for yourself what the language syntax does
  2023. under-the-hood.</p>
  2024. <p>(Contributed by Nick Coghlan in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9147">bpo-9147</a>.)</p>
  2025. </section>
  2026. <section id="dbm">
  2027. <h3>dbm<a class="headerlink" href="#dbm" title="Link to this heading">¶</a></h3>
  2028. <p>All database modules now support the <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">setdefault()</span></code> methods.</p>
  2029. <p>(Suggested by Ray Allen in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9523">bpo-9523</a>.)</p>
  2030. </section>
  2031. <section id="ctypes">
  2032. <h3>ctypes<a class="headerlink" href="#ctypes" title="Link to this heading">¶</a></h3>
  2033. <p>A new type, <a class="reference internal" href="../library/ctypes.html#ctypes.c_ssize_t" title="ctypes.c_ssize_t"><code class="xref py py-class docutils literal notranslate"><span class="pre">ctypes.c_ssize_t</span></code></a> represents the C <code class="xref c c-type docutils literal notranslate"><span class="pre">ssize_t</span></code> datatype.</p>
  2034. </section>
  2035. <section id="site">
  2036. <h3>site<a class="headerlink" href="#site" title="Link to this heading">¶</a></h3>
  2037. <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 has three new functions useful for reporting on the
  2038. details of a given Python installation.</p>
  2039. <ul class="simple">
  2040. <li><p><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> lists all global site-packages directories.</p></li>
  2041. <li><p><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> reports on the user’s base directory where data can
  2042. be stored.</p></li>
  2043. <li><p><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> reveals the user-specific site-packages
  2044. directory path.</p></li>
  2045. </ul>
  2046. <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">site</span>
  2047. <span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getsitepackages</span><span class="p">()</span>
  2048. <span class="go">[&#39;/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages&#39;,</span>
  2049. <span class="go"> &#39;/Library/Frameworks/Python.framework/Versions/3.2/lib/site-python&#39;,</span>
  2050. <span class="go"> &#39;/Library/Python/3.2/site-packages&#39;]</span>
  2051. <span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getuserbase</span><span class="p">()</span>
  2052. <span class="go">&#39;/Users/raymondhettinger/Library/Python/3.2&#39;</span>
  2053. <span class="gp">&gt;&gt;&gt; </span><span class="n">site</span><span class="o">.</span><span class="n">getusersitepackages</span><span class="p">()</span>
  2054. <span class="go">&#39;/Users/raymondhettinger/Library/Python/3.2/lib/python/site-packages&#39;</span>
  2055. </pre></div>
  2056. </div>
  2057. <p>Conveniently, some of site’s functionality is accessible directly from the
  2058. command-line:</p>
  2059. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>site<span class="w"> </span>--user-base
  2060. <span class="go">/Users/raymondhettinger/.local</span>
  2061. <span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>site<span class="w"> </span>--user-site
  2062. <span class="go">/Users/raymondhettinger/.local/lib/python3.2/site-packages</span>
  2063. </pre></div>
  2064. </div>
  2065. <p>(Contributed by Tarek Ziadé in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6693">bpo-6693</a>.)</p>
  2066. </section>
  2067. <section id="sysconfig">
  2068. <h3>sysconfig<a class="headerlink" href="#sysconfig" title="Link to this heading">¶</a></h3>
  2069. <p>The new <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 makes it straightforward to discover
  2070. installation paths and configuration variables that vary across platforms and
  2071. installations.</p>
  2072. <p>The module offers access simple access functions for platform and version
  2073. information:</p>
  2074. <ul class="simple">
  2075. <li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_platform" title="sysconfig.get_platform"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_platform()</span></code></a> returning values like <em>linux-i586</em> or
  2076. <em>macosx-10.6-ppc</em>.</p></li>
  2077. <li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_python_version" title="sysconfig.get_python_version"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_python_version()</span></code></a> returns a Python version string
  2078. such as “3.2”.</p></li>
  2079. </ul>
  2080. <p>It also provides access to the paths and variables corresponding to one of
  2081. seven named schemes used by <code class="docutils literal notranslate"><span class="pre">distutils</span></code>. Those include <em>posix_prefix</em>,
  2082. <em>posix_home</em>, <em>posix_user</em>, <em>nt</em>, <em>nt_user</em>, <em>os2</em>, <em>os2_home</em>:</p>
  2083. <ul class="simple">
  2084. <li><p><a class="reference internal" href="../library/sysconfig.html#sysconfig.get_paths" title="sysconfig.get_paths"><code class="xref py py-func docutils literal notranslate"><span class="pre">get_paths()</span></code></a> makes a dictionary containing installation paths
  2085. for the current installation scheme.</p></li>
  2086. <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 of platform specific
  2087. variables.</p></li>
  2088. </ul>
  2089. <p>There is also a convenient command-line interface:</p>
  2090. <div class="highlight-doscon notranslate"><div class="highlight"><pre><span></span><span class="gp">C:\Python32&gt;</span>python -m sysconfig
  2091. <span class="go">Platform: &quot;win32&quot;</span>
  2092. <span class="go">Python version: &quot;3.2&quot;</span>
  2093. <span class="go">Current installation scheme: &quot;nt&quot;</span>
  2094. <span class="go">Paths:</span>
  2095. <span class="go"> data = &quot;C:\Python32&quot;</span>
  2096. <span class="go"> include = &quot;C:\Python32\Include&quot;</span>
  2097. <span class="go"> platinclude = &quot;C:\Python32\Include&quot;</span>
  2098. <span class="go"> platlib = &quot;C:\Python32\Lib\site-packages&quot;</span>
  2099. <span class="go"> platstdlib = &quot;C:\Python32\Lib&quot;</span>
  2100. <span class="go"> purelib = &quot;C:\Python32\Lib\site-packages&quot;</span>
  2101. <span class="go"> scripts = &quot;C:\Python32\Scripts&quot;</span>
  2102. <span class="go"> stdlib = &quot;C:\Python32\Lib&quot;</span>
  2103. <span class="go">Variables:</span>
  2104. <span class="go"> BINDIR = &quot;C:\Python32&quot;</span>
  2105. <span class="go"> BINLIBDEST = &quot;C:\Python32\Lib&quot;</span>
  2106. <span class="go"> EXE = &quot;.exe&quot;</span>
  2107. <span class="go"> INCLUDEPY = &quot;C:\Python32\Include&quot;</span>
  2108. <span class="go"> LIBDEST = &quot;C:\Python32\Lib&quot;</span>
  2109. <span class="go"> SO = &quot;.pyd&quot;</span>
  2110. <span class="go"> VERSION = &quot;32&quot;</span>
  2111. <span class="go"> abiflags = &quot;&quot;</span>
  2112. <span class="go"> base = &quot;C:\Python32&quot;</span>
  2113. <span class="go"> exec_prefix = &quot;C:\Python32&quot;</span>
  2114. <span class="go"> platbase = &quot;C:\Python32&quot;</span>
  2115. <span class="go"> prefix = &quot;C:\Python32&quot;</span>
  2116. <span class="go"> projectbase = &quot;C:\Python32&quot;</span>
  2117. <span class="go"> py_version = &quot;3.2&quot;</span>
  2118. <span class="go"> py_version_nodot = &quot;32&quot;</span>
  2119. <span class="go"> py_version_short = &quot;3.2&quot;</span>
  2120. <span class="go"> srcdir = &quot;C:\Python32&quot;</span>
  2121. <span class="go"> userbase = &quot;C:\Documents and Settings\Raymond\Application Data\Python&quot;</span>
  2122. </pre></div>
  2123. </div>
  2124. <p>(Moved out of Distutils by Tarek Ziadé.)</p>
  2125. </section>
  2126. <section id="pdb">
  2127. <h3>pdb<a class="headerlink" href="#pdb" title="Link to this heading">¶</a></h3>
  2128. <p>The <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pdb</span></code></a> debugger module gained a number of usability improvements:</p>
  2129. <ul class="simple">
  2130. <li><p><code class="file docutils literal notranslate"><span class="pre">pdb.py</span></code> now has a <code class="docutils literal notranslate"><span class="pre">-c</span></code> option that executes commands as given in a
  2131. <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> script file.</p></li>
  2132. <li><p>A <code class="file docutils literal notranslate"><span class="pre">.pdbrc</span></code> script file can contain <code class="docutils literal notranslate"><span class="pre">continue</span></code> and <code class="docutils literal notranslate"><span class="pre">next</span></code> commands
  2133. that continue debugging.</p></li>
  2134. <li><p>The <a class="reference internal" href="../library/pdb.html#pdb.Pdb" title="pdb.Pdb"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pdb</span></code></a> class constructor now accepts a <em>nosigint</em> argument.</p></li>
  2135. <li><p>New commands: <code class="docutils literal notranslate"><span class="pre">l(list)</span></code>, <code class="docutils literal notranslate"><span class="pre">ll(long</span> <span class="pre">list)</span></code> and <code class="docutils literal notranslate"><span class="pre">source</span></code> for
  2136. listing source code.</p></li>
  2137. <li><p>New commands: <code class="docutils literal notranslate"><span class="pre">display</span></code> and <code class="docutils literal notranslate"><span class="pre">undisplay</span></code> for showing or hiding
  2138. the value of an expression if it has changed.</p></li>
  2139. <li><p>New command: <code class="docutils literal notranslate"><span class="pre">interact</span></code> for starting an interactive interpreter containing
  2140. the global and local names found in the current scope.</p></li>
  2141. <li><p>Breakpoints can be cleared by breakpoint number.</p></li>
  2142. </ul>
  2143. <p>(Contributed by Georg Brandl, Antonio Cuni and Ilya Sandler.)</p>
  2144. </section>
  2145. <section id="configparser">
  2146. <h3>configparser<a class="headerlink" href="#configparser" title="Link to this heading">¶</a></h3>
  2147. <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 was modified to improve usability and
  2148. predictability of the default parser and its supported INI syntax. The old
  2149. <code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> class was removed in favor of <code class="xref py py-class docutils literal notranslate"><span class="pre">SafeConfigParser</span></code>
  2150. which has in turn been renamed to <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a>. Support
  2151. for inline comments is now turned off by default and section or option
  2152. duplicates are not allowed in a single configuration source.</p>
  2153. <p>Config parsers gained a new API based on the mapping protocol:</p>
  2154. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">()</span>
  2155. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  2156. <span class="gp">... </span><span class="s2">[DEFAULT]</span>
  2157. <span class="gp">... </span><span class="s2">location = upper left</span>
  2158. <span class="gp">... </span><span class="s2">visible = yes</span>
  2159. <span class="gp">... </span><span class="s2">editable = no</span>
  2160. <span class="gp">... </span><span class="s2">color = blue</span>
  2161. <span class="gp">...</span>
  2162. <span class="gp">... </span><span class="s2">[main]</span>
  2163. <span class="gp">... </span><span class="s2">title = Main Menu</span>
  2164. <span class="gp">... </span><span class="s2">color = green</span>
  2165. <span class="gp">...</span>
  2166. <span class="gp">... </span><span class="s2">[options]</span>
  2167. <span class="gp">... </span><span class="s2">title = Options</span>
  2168. <span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
  2169. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s1">&#39;main&#39;</span><span class="p">][</span><span class="s1">&#39;color&#39;</span><span class="p">]</span>
  2170. <span class="go">&#39;green&#39;</span>
  2171. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s1">&#39;main&#39;</span><span class="p">][</span><span class="s1">&#39;editable&#39;</span><span class="p">]</span>
  2172. <span class="go">&#39;no&#39;</span>
  2173. <span class="gp">&gt;&gt;&gt; </span><span class="n">section</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s1">&#39;options&#39;</span><span class="p">]</span>
  2174. <span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">]</span>
  2175. <span class="go">&#39;Options&#39;</span>
  2176. <span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Options (editable: </span><span class="si">%(editable)s</span><span class="s1">)&#39;</span>
  2177. <span class="gp">&gt;&gt;&gt; </span><span class="n">section</span><span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">]</span>
  2178. <span class="go">&#39;Options (editable: no)&#39;</span>
  2179. </pre></div>
  2180. </div>
  2181. <p>The new API is implemented on top of the classical API, so custom parser
  2182. subclasses should be able to use it without modifications.</p>
  2183. <p>The INI file structure accepted by config parsers can now be customized. Users
  2184. can specify alternative option/value delimiters and comment prefixes, change the
  2185. name of the <em>DEFAULT</em> section or switch the interpolation syntax.</p>
  2186. <p>There is support for pluggable interpolation including an additional interpolation
  2187. handler <a class="reference internal" href="../library/configparser.html#configparser.ExtendedInterpolation" title="configparser.ExtendedInterpolation"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExtendedInterpolation</span></code></a>:</p>
  2188. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span> <span class="o">=</span> <span class="n">ConfigParser</span><span class="p">(</span><span class="n">interpolation</span><span class="o">=</span><span class="n">ExtendedInterpolation</span><span class="p">())</span>
  2189. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_dict</span><span class="p">({</span><span class="s1">&#39;buildout&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;directory&#39;</span><span class="p">:</span> <span class="s1">&#39;/home/ambv/zope9&#39;</span><span class="p">},</span>
  2190. <span class="gp">... </span> <span class="s1">&#39;custom&#39;</span><span class="p">:</span> <span class="p">{</span><span class="s1">&#39;prefix&#39;</span><span class="p">:</span> <span class="s1">&#39;/usr/local&#39;</span><span class="p">}})</span>
  2191. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="o">.</span><span class="n">read_string</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
  2192. <span class="gp">... </span><span class="s2">[buildout]</span>
  2193. <span class="gp">... </span><span class="s2">parts =</span>
  2194. <span class="gp">... </span><span class="s2"> zope9</span>
  2195. <span class="gp">... </span><span class="s2"> instance</span>
  2196. <span class="gp">... </span><span class="s2">find-links =</span>
  2197. <span class="gp">... </span><span class="s2"> ${buildout:directory}/downloads/dist</span>
  2198. <span class="gp">...</span>
  2199. <span class="gp">... </span><span class="s2">[zope9]</span>
  2200. <span class="gp">... </span><span class="s2">recipe = plone.recipe.zope9install</span>
  2201. <span class="gp">... </span><span class="s2">location = /opt/zope</span>
  2202. <span class="gp">...</span>
  2203. <span class="gp">... </span><span class="s2">[instance]</span>
  2204. <span class="gp">... </span><span class="s2">recipe = plone.recipe.zope9instance</span>
  2205. <span class="gp">... </span><span class="s2">zope9-location = ${zope9:location}</span>
  2206. <span class="gp">... </span><span class="s2">zope-conf = ${custom:prefix}/etc/zope.conf</span>
  2207. <span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">)</span>
  2208. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s1">&#39;buildout&#39;</span><span class="p">][</span><span class="s1">&#39;find-links&#39;</span><span class="p">]</span>
  2209. <span class="go">&#39;\n/home/ambv/zope9/downloads/dist&#39;</span>
  2210. <span class="gp">&gt;&gt;&gt; </span><span class="n">parser</span><span class="p">[</span><span class="s1">&#39;instance&#39;</span><span class="p">][</span><span class="s1">&#39;zope-conf&#39;</span><span class="p">]</span>
  2211. <span class="go">&#39;/usr/local/etc/zope.conf&#39;</span>
  2212. <span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span> <span class="o">=</span> <span class="n">parser</span><span class="p">[</span><span class="s1">&#39;instance&#39;</span><span class="p">]</span>
  2213. <span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span><span class="p">[</span><span class="s1">&#39;zope-conf&#39;</span><span class="p">]</span>
  2214. <span class="go">&#39;/usr/local/etc/zope.conf&#39;</span>
  2215. <span class="gp">&gt;&gt;&gt; </span><span class="n">instance</span><span class="p">[</span><span class="s1">&#39;zope9-location&#39;</span><span class="p">]</span>
  2216. <span class="go">&#39;/opt/zope&#39;</span>
  2217. </pre></div>
  2218. </div>
  2219. <p>A number of smaller features were also introduced, like support for specifying
  2220. encoding in read operations, specifying fallback values for get-functions, or
  2221. reading directly from dictionaries and strings.</p>
  2222. <p>(All changes contributed by Łukasz Langa.)</p>
  2223. </section>
  2224. <section id="urllib-parse">
  2225. <h3>urllib.parse<a class="headerlink" href="#urllib-parse" title="Link to this heading">¶</a></h3>
  2226. <p>A number of usability improvements were made for 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">urllib.parse</span></code></a> module.</p>
  2227. <p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlparse" title="urllib.parse.urlparse"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlparse()</span></code></a> function now supports <a class="reference external" href="https://en.wikipedia.org/wiki/IPv6">IPv6</a> addresses as described in <span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2732.html"><strong>RFC 2732</strong></a>:</p>
  2228. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">urllib.parse</span>
  2229. <span class="gp">&gt;&gt;&gt; </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s1">&#39;http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/&#39;</span><span class="p">)</span>
  2230. <span class="go">ParseResult(scheme=&#39;http&#39;,</span>
  2231. <span class="go"> netloc=&#39;[dead:beef:cafe:5417:affe:8FA3:deaf:feed]&#39;,</span>
  2232. <span class="go"> path=&#39;/foo/&#39;,</span>
  2233. <span class="go"> params=&#39;&#39;,</span>
  2234. <span class="go"> query=&#39;&#39;,</span>
  2235. <span class="go"> fragment=&#39;&#39;)</span>
  2236. </pre></div>
  2237. </div>
  2238. <p>The <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urldefrag" title="urllib.parse.urldefrag"><code class="xref py py-func docutils literal notranslate"><span class="pre">urldefrag()</span></code></a> function now returns a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a>:</p>
  2239. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urldefrag</span><span class="p">(</span><span class="s1">&#39;http://python.org/about/#target&#39;</span><span class="p">)</span>
  2240. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span>
  2241. <span class="go">DefragResult(url=&#39;http://python.org/about/&#39;, fragment=&#39;target&#39;)</span>
  2242. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
  2243. <span class="go">&#39;http://python.org/about/&#39;</span>
  2244. <span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">fragment</span>
  2245. <span class="go">&#39;target&#39;</span>
  2246. </pre></div>
  2247. </div>
  2248. <p>And, the <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.urlencode" title="urllib.parse.urlencode"><code class="xref py py-func docutils literal notranslate"><span class="pre">urlencode()</span></code></a> function is now much more flexible,
  2249. accepting either a string or bytes type for the <em>query</em> argument. If it is a
  2250. string, then the <em>safe</em>, <em>encoding</em>, and <em>error</em> parameters are sent to
  2251. <a class="reference internal" href="../library/urllib.parse.html#urllib.parse.quote_plus" title="urllib.parse.quote_plus"><code class="xref py py-func docutils literal notranslate"><span class="pre">quote_plus()</span></code></a> for encoding:</p>
  2252. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">([</span>
  2253. <span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39;telenovela&#39;</span><span class="p">),</span>
  2254. <span class="gp">... </span> <span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;¿Dónde Está Elisa?&#39;</span><span class="p">)],</span>
  2255. <span class="gp">... </span> <span class="n">encoding</span><span class="o">=</span><span class="s1">&#39;latin-1&#39;</span><span class="p">)</span>
  2256. <span class="go">&#39;type=telenovela&amp;name=%BFD%F3nde+Est%E1+Elisa%3F&#39;</span>
  2257. </pre></div>
  2258. </div>
  2259. <p>As detailed in <a class="reference internal" href="../library/urllib.parse.html#parsing-ascii-encoded-bytes"><span class="std std-ref">Parsing ASCII Encoded Bytes</span></a>, all 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">urllib.parse</span></code></a>
  2260. functions now accept ASCII-encoded byte strings as input, so long as they are
  2261. not mixed with regular strings. If ASCII-encoded byte strings are given as
  2262. parameters, the return types will also be an ASCII-encoded byte strings:</p>
  2263. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;http://www.python.org:80/about/&#39;</span><span class="p">)</span>
  2264. <span class="go">ParseResultBytes(scheme=b&#39;http&#39;, netloc=b&#39;www.python.org:80&#39;,</span>
  2265. <span class="go"> path=b&#39;/about/&#39;, params=b&#39;&#39;, query=b&#39;&#39;, fragment=b&#39;&#39;)</span>
  2266. </pre></div>
  2267. </div>
  2268. <p>(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2987">bpo-2987</a>,
  2269. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5468">bpo-5468</a>, and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9873">bpo-9873</a>.)</p>
  2270. </section>
  2271. <section id="mailbox">
  2272. <h3>mailbox<a class="headerlink" href="#mailbox" title="Link to this heading">¶</a></h3>
  2273. <p>Thanks to a concerted effort by R. David Murray, 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 has
  2274. been fixed for Python 3.2. The challenge was that mailbox had been originally
  2275. designed with a text interface, but email messages are best represented with
  2276. <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> because various parts of a message may have different encodings.</p>
  2277. <p>The solution harnessed 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> package’s binary support for parsing
  2278. arbitrary email messages. In addition, the solution required a number of API
  2279. changes.</p>
  2280. <p>As expected, the <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a> method for
  2281. <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">mailbox.Mailbox</span></code></a> objects now accepts binary input.</p>
  2282. <p><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> and text file input are deprecated. Also, string input
  2283. will fail early if non-ASCII characters are used. Previously it would fail when
  2284. the email was processed in a later step.</p>
  2285. <p>There is also support for binary output. The <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_file" title="mailbox.Mailbox.get_file"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_file()</span></code></a>
  2286. method now returns a file in the binary mode (where it used to incorrectly set
  2287. the file to text-mode). There is also a new <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_bytes" title="mailbox.Mailbox.get_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_bytes()</span></code></a>
  2288. method that returns a <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> representation of a message corresponding
  2289. to a given <em>key</em>.</p>
  2290. <p>It is still possible to get non-binary output using the old API’s
  2291. <a class="reference internal" href="../library/mailbox.html#mailbox.Mailbox.get_string" title="mailbox.Mailbox.get_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_string()</span></code></a> method, but that approach
  2292. is not very useful. Instead, it is best to extract messages from
  2293. a <a class="reference internal" href="../library/mailbox.html#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object or to load them from binary input.</p>
  2294. <p>(Contributed by R. David Murray, with efforts from Steffen Daode Nurpmeso and an
  2295. initial patch by Victor Stinner in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9124">bpo-9124</a>.)</p>
  2296. </section>
  2297. <section id="turtledemo">
  2298. <h3>turtledemo<a class="headerlink" href="#turtledemo" title="Link to this heading">¶</a></h3>
  2299. <p>The demonstration code for the <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a> module was moved from the <em>Demo</em>
  2300. directory to main library. It includes over a dozen sample scripts with
  2301. lively displays. Being on <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>, it can now be run directly
  2302. from the command-line:</p>
  2303. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>turtledemo
  2304. </pre></div>
  2305. </div>
  2306. <p>(Moved from the Demo directory by Alexander Belopolsky in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10199">bpo-10199</a>.)</p>
  2307. </section>
  2308. </section>
  2309. <section id="multi-threading">
  2310. <h2>Multi-threading<a class="headerlink" href="#multi-threading" title="Link to this heading">¶</a></h2>
  2311. <ul>
  2312. <li><p>The mechanism for serializing execution of concurrently running Python threads
  2313. (generally known as the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> or Global Interpreter Lock) has
  2314. been rewritten. Among the objectives were more predictable switching
  2315. intervals and reduced overhead due to lock contention and the number of
  2316. ensuing system calls. The notion of a “check interval” to allow thread
  2317. switches has been abandoned and replaced by an absolute duration expressed in
  2318. seconds. This parameter is tunable through <a class="reference internal" href="../library/sys.html#sys.setswitchinterval" title="sys.setswitchinterval"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setswitchinterval()</span></code></a>.
  2319. It currently defaults to 5 milliseconds.</p>
  2320. <p>Additional details about the implementation can be read from a <a class="reference external" href="https://mail.python.org/pipermail/python-dev/2009-October/093321.html">python-dev
  2321. mailing-list message</a>
  2322. (however, “priority requests” as exposed in this message have not been kept
  2323. for inclusion).</p>
  2324. <p>(Contributed by Antoine Pitrou.)</p>
  2325. </li>
  2326. <li><p>Regular and recursive locks now accept an optional <em>timeout</em> argument to their
  2327. <a class="reference internal" href="../library/threading.html#threading.Lock.acquire" title="threading.Lock.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">acquire()</span></code></a> method. (Contributed by Antoine Pitrou;
  2328. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7316">bpo-7316</a>.)</p></li>
  2329. <li><p>Similarly, <a class="reference internal" href="../library/threading.html#threading.Semaphore.acquire" title="threading.Semaphore.acquire"><code class="xref py py-meth docutils literal notranslate"><span class="pre">threading.Semaphore.acquire()</span></code></a> also gained a <em>timeout</em>
  2330. argument. (Contributed by Torsten Landschoff; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=850728">bpo-850728</a>.)</p></li>
  2331. <li><p>Regular and recursive lock acquisitions can now be interrupted by signals on
  2332. platforms using Pthreads. This means that Python programs that deadlock while
  2333. acquiring locks can be successfully killed by repeatedly sending SIGINT to the
  2334. process (by pressing <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Ctrl</kbd>+<kbd class="kbd docutils literal notranslate">C</kbd></kbd> in most shells).
  2335. (Contributed by Reid Kleckner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8844">bpo-8844</a>.)</p></li>
  2336. </ul>
  2337. </section>
  2338. <section id="optimizations">
  2339. <h2>Optimizations<a class="headerlink" href="#optimizations" title="Link to this heading">¶</a></h2>
  2340. <p>A number of small performance enhancements have been added:</p>
  2341. <ul>
  2342. <li><p>Python’s peephole optimizer now recognizes patterns such <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">{1,</span> <span class="pre">2,</span> <span class="pre">3}</span></code> as
  2343. being a test for membership in a set of constants. The optimizer recasts the
  2344. <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> as a <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal notranslate"><span class="pre">frozenset</span></code></a> and stores the pre-built constant.</p>
  2345. <p>Now that the speed penalty is gone, it is practical to start writing
  2346. membership tests using set-notation. This style is both semantically clear
  2347. and operationally fast:</p>
  2348. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">extension</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">rpartition</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)[</span><span class="mi">2</span><span class="p">]</span>
  2349. <span class="k">if</span> <span class="n">extension</span> <span class="ow">in</span> <span class="p">{</span><span class="s1">&#39;xml&#39;</span><span class="p">,</span> <span class="s1">&#39;html&#39;</span><span class="p">,</span> <span class="s1">&#39;xhtml&#39;</span><span class="p">,</span> <span class="s1">&#39;css&#39;</span><span class="p">}:</span>
  2350. <span class="n">handle</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
  2351. </pre></div>
  2352. </div>
  2353. <p>(Patch and additional tests contributed by Dave Malcolm; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6690">bpo-6690</a>).</p>
  2354. </li>
  2355. <li><p>Serializing and unserializing data using 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> module is now
  2356. several times faster.</p>
  2357. <p>(Contributed by Alexandre Vassalotti, Antoine Pitrou
  2358. and the Unladen Swallow team in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9410">bpo-9410</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3873">bpo-3873</a>.)</p>
  2359. </li>
  2360. <li><p>The <a class="reference external" href="https://en.wikipedia.org/wiki/Timsort">Timsort algorithm</a> used in
  2361. <a class="reference internal" href="../library/stdtypes.html#list.sort" title="list.sort"><code class="xref py py-meth docutils literal notranslate"><span class="pre">list.sort()</span></code></a> and <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> now runs faster and uses less memory
  2362. when called with a <a class="reference internal" href="../glossary.html#term-key-function"><span class="xref std std-term">key function</span></a>. Previously, every element of
  2363. a list was wrapped with a temporary object that remembered the key value
  2364. associated with each element. Now, two arrays of keys and values are
  2365. sorted in parallel. This saves the memory consumed by the sort wrappers,
  2366. and it saves time lost to delegating comparisons.</p>
  2367. <p>(Patch by Daniel Stutzbach in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9915">bpo-9915</a>.)</p>
  2368. </li>
  2369. <li><p>JSON decoding performance is improved and memory consumption is reduced
  2370. whenever the same string is repeated for multiple keys. Also, JSON encoding
  2371. now uses the C speedups when the <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> argument is true.</p>
  2372. <p>(Contributed by Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7451">bpo-7451</a> and by Raymond Hettinger and
  2373. Antoine Pitrou in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10314">bpo-10314</a>.)</p>
  2374. </li>
  2375. <li><p>Recursive locks (created with the <a class="reference internal" href="../library/threading.html#threading.RLock" title="threading.RLock"><code class="xref py py-func docutils literal notranslate"><span class="pre">threading.RLock()</span></code></a> API) now benefit
  2376. from a C implementation which makes them as fast as regular locks, and between
  2377. 10x and 15x faster than their previous pure Python implementation.</p>
  2378. <p>(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3001">bpo-3001</a>.)</p>
  2379. </li>
  2380. <li><p>The fast-search algorithm in stringlib is now used by the <a class="reference internal" href="../library/stdtypes.html#str.split" title="str.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">split()</span></code></a>,
  2381. <a class="reference internal" href="../library/stdtypes.html#str.rsplit" title="str.rsplit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.splitlines" title="str.splitlines"><code class="xref py py-meth docutils literal notranslate"><span class="pre">splitlines()</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#str.replace" title="str.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">replace()</span></code></a> methods on
  2382. <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>, <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#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects. Likewise, the
  2383. algorithm is also used by <a class="reference internal" href="../library/stdtypes.html#str.rfind" title="str.rfind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rfind()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.rindex" title="str.rindex"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rindex()</span></code></a>, <a class="reference internal" href="../library/stdtypes.html#str.rsplit" title="str.rsplit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rsplit()</span></code></a> and
  2384. <a class="reference internal" href="../library/stdtypes.html#str.rpartition" title="str.rpartition"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rpartition()</span></code></a>.</p>
  2385. <p>(Patch by Florent Xicluna in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7622">bpo-7622</a> and <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7462">bpo-7462</a>.)</p>
  2386. </li>
  2387. <li><p>Integer to string conversions now work two “digits” at a time, reducing the
  2388. number of division and modulo operations.</p>
  2389. <p>(<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6713">bpo-6713</a> by Gawain Bolton, Mark Dickinson, and Victor Stinner.)</p>
  2390. </li>
  2391. </ul>
  2392. <p>There were several other minor optimizations. Set differencing now runs faster
  2393. when one operand is much larger than the other (patch by Andress Bennetts in
  2394. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8685">bpo-8685</a>). The <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.repeat()</span></code> method has a faster implementation
  2395. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1569291">bpo-1569291</a> by Alexander Belopolsky). The <a class="reference internal" href="../library/http.server.html#http.server.BaseHTTPRequestHandler" title="http.server.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code></a>
  2396. has more efficient buffering (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=3709">bpo-3709</a> by Andrew Schaaf). The
  2397. <a class="reference internal" href="../library/operator.html#operator.attrgetter" title="operator.attrgetter"><code class="xref py py-func docutils literal notranslate"><span class="pre">operator.attrgetter()</span></code></a> function has been sped-up (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10160">bpo-10160</a> by
  2398. Christos Georgiou). And <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser" title="configparser.ConfigParser"><code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code></a> loads multi-line arguments a bit
  2399. faster (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7113">bpo-7113</a> by Łukasz Langa).</p>
  2400. </section>
  2401. <section id="unicode">
  2402. <h2>Unicode<a class="headerlink" href="#unicode" title="Link to this heading">¶</a></h2>
  2403. <p>Python has been updated to <a class="reference external" href="https://unicode.org/versions/Unicode6.0.0/">Unicode 6.0.0</a>. The update to the standard adds
  2404. over 2,000 new characters including <a class="reference external" href="https://en.wikipedia.org/wiki/Emoji">emoji</a>
  2405. symbols which are important for mobile phones.</p>
  2406. <p>In addition, the updated standard has altered the character properties for two
  2407. Kannada characters (U+0CF1, U+0CF2) and one New Tai Lue numeric character
  2408. (U+19DA), making the former eligible for use in identifiers while disqualifying
  2409. the latter. For more information, see <a class="reference external" href="https://www.unicode.org/versions/Unicode6.0.0/#Database_Changes">Unicode Character Database Changes</a>.</p>
  2410. </section>
  2411. <section id="codecs">
  2412. <h2>Codecs<a class="headerlink" href="#codecs" title="Link to this heading">¶</a></h2>
  2413. <p>Support was added for <em>cp720</em> Arabic DOS encoding (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=1616979">bpo-1616979</a>).</p>
  2414. <p>MBCS encoding no longer ignores the error handler argument. In the default
  2415. strict mode, it raises an <a class="reference internal" href="../library/exceptions.html#UnicodeDecodeError" title="UnicodeDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeDecodeError</span></code></a> when it encounters an
  2416. undecodable byte sequence and an <a class="reference internal" href="../library/exceptions.html#UnicodeEncodeError" title="UnicodeEncodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">UnicodeEncodeError</span></code></a> for an unencodable
  2417. character.</p>
  2418. <p>The MBCS codec supports <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> and <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> error handlers for
  2419. decoding, and <code class="docutils literal notranslate"><span class="pre">'strict'</span></code> and <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> for encoding.</p>
  2420. <p>To emulate Python3.1 MBCS encoding, select the <code class="docutils literal notranslate"><span class="pre">'ignore'</span></code> handler for decoding
  2421. and the <code class="docutils literal notranslate"><span class="pre">'replace'</span></code> handler for encoding.</p>
  2422. <p>On Mac OS X, Python decodes command line arguments with <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> rather than
  2423. the locale encoding.</p>
  2424. <p>By default, <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> uses <code class="docutils literal notranslate"><span class="pre">'utf-8'</span></code> encoding on Windows (instead of
  2425. <code class="docutils literal notranslate"><span class="pre">'mbcs'</span></code>) and the <code class="docutils literal notranslate"><span class="pre">'surrogateescape'</span></code> error handler on all operating
  2426. systems.</p>
  2427. </section>
  2428. <section id="documentation">
  2429. <h2>Documentation<a class="headerlink" href="#documentation" title="Link to this heading">¶</a></h2>
  2430. <p>The documentation continues to be improved.</p>
  2431. <ul>
  2432. <li><p>A table of quick links has been added to the top of lengthy sections such as
  2433. <a class="reference internal" href="../library/functions.html#built-in-funcs"><span class="std std-ref">Built-in Functions</span></a>. In the case of <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a>, the links are
  2434. accompanied by tables of cheatsheet-style summaries to provide an overview and
  2435. memory jog without having to read all of the docs.</p></li>
  2436. <li><p>In some cases, the pure Python source code can be a helpful adjunct to the
  2437. documentation, so now many modules now feature quick links to the latest
  2438. version of the source code. For example, 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> module
  2439. documentation has a quick link at the top labeled:</p>
  2440. <blockquote>
  2441. <div><p><strong>Source code</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/functools.py">Lib/functools.py</a>.</p>
  2442. </div></blockquote>
  2443. <p>(Contributed by Raymond Hettinger; see
  2444. <a class="reference external" href="https://rhettinger.wordpress.com/2011/01/28/open-your-source-more/">rationale</a>.)</p>
  2445. </li>
  2446. <li><p>The docs now contain more examples and recipes. In particular, <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>
  2447. module has an extensive section, <a class="reference internal" href="../library/re.html#re-examples"><span class="std std-ref">Regular Expression Examples</span></a>. Likewise, the
  2448. <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><code class="xref py py-mod docutils literal notranslate"><span class="pre">itertools</span></code></a> module continues to be updated with new
  2449. <a class="reference internal" href="../library/itertools.html#itertools-recipes"><span class="std std-ref">Itertools Recipes</span></a>.</p></li>
  2450. <li><p>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 now has an auxiliary implementation in pure Python.
  2451. No functionality was changed. This just provides an easier-to-read alternate
  2452. implementation.</p>
  2453. <p>(Contributed by Alexander Belopolsky in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9528">bpo-9528</a>.)</p>
  2454. </li>
  2455. <li><p>The unmaintained <code class="file docutils literal notranslate"><span class="pre">Demo</span></code> directory has been removed. Some demos were
  2456. integrated into the documentation, some were moved to the <code class="file docutils literal notranslate"><span class="pre">Tools/demo</span></code>
  2457. directory, and others were removed altogether.</p>
  2458. <p>(Contributed by Georg Brandl in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7962">bpo-7962</a>.)</p>
  2459. </li>
  2460. </ul>
  2461. </section>
  2462. <section id="idle">
  2463. <h2>IDLE<a class="headerlink" href="#idle" title="Link to this heading">¶</a></h2>
  2464. <ul>
  2465. <li><p>The format menu now has an option to clean source files by stripping
  2466. trailing whitespace.</p>
  2467. <p>(Contributed by Raymond Hettinger; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5150">bpo-5150</a>.)</p>
  2468. </li>
  2469. <li><p>IDLE on Mac OS X now works with both Carbon AquaTk and Cocoa AquaTk.</p>
  2470. <p>(Contributed by Kevin Walzer, Ned Deily, and Ronald Oussoren; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6075">bpo-6075</a>.)</p>
  2471. </li>
  2472. </ul>
  2473. </section>
  2474. <section id="code-repository">
  2475. <h2>Code Repository<a class="headerlink" href="#code-repository" title="Link to this heading">¶</a></h2>
  2476. <p>In addition to the existing Subversion code repository at <a class="reference external" href="https://svn.python.org">https://svn.python.org</a>
  2477. there is now a <a class="reference external" href="https://www.mercurial-scm.org/">Mercurial</a> repository at
  2478. <a class="reference external" href="https://hg.python.org/">https://hg.python.org/</a>.</p>
  2479. <p>After the 3.2 release, there are plans to switch to Mercurial as the primary
  2480. repository. This distributed version control system should make it easier for
  2481. members of the community to create and share external changesets. See
  2482. <span class="target" id="index-14"></span><a class="pep reference external" href="https://peps.python.org/pep-0385/"><strong>PEP 385</strong></a> for details.</p>
  2483. <p>To learn to use the new version control system, see the <a class="reference external" href="https://www.mercurial-scm.org/wiki/QuickStart">Quick Start</a> or the <a class="reference external" href="https://www.mercurial-scm.org/guide">Guide to
  2484. Mercurial Workflows</a>.</p>
  2485. </section>
  2486. <section id="build-and-c-api-changes">
  2487. <h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Link to this heading">¶</a></h2>
  2488. <p>Changes to Python’s build process and to the C API include:</p>
  2489. <ul>
  2490. <li><p>The <em>idle</em>, <em>pydoc</em> and <em>2to3</em> scripts are now installed with a
  2491. version-specific suffix on <code class="docutils literal notranslate"><span class="pre">make</span> <span class="pre">altinstall</span></code> (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10679">bpo-10679</a>).</p></li>
  2492. <li><p>The C functions that access the Unicode Database now accept and return
  2493. characters from the full Unicode range, even on narrow unicode builds
  2494. (Py_UNICODE_TOLOWER, Py_UNICODE_ISDECIMAL, and others). A visible difference
  2495. in Python is that <a class="reference internal" href="../library/unicodedata.html#unicodedata.numeric" title="unicodedata.numeric"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicodedata.numeric()</span></code></a> now returns the correct value
  2496. for large code points, and <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> may consider more characters as
  2497. printable.</p>
  2498. <p>(Reported by Bupjoe Lee and fixed by Amaury Forgeot D’Arc; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5127">bpo-5127</a>.)</p>
  2499. </li>
  2500. <li><p>Computed gotos are now enabled by default on supported compilers (which are
  2501. detected by the configure script). They can still be disabled selectively by
  2502. specifying <code class="docutils literal notranslate"><span class="pre">--without-computed-gotos</span></code>.</p>
  2503. <p>(Contributed by Antoine Pitrou; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9203">bpo-9203</a>.)</p>
  2504. </li>
  2505. <li><p>The option <code class="docutils literal notranslate"><span class="pre">--with-wctype-functions</span></code> was removed. The built-in unicode
  2506. database is now used for all functions.</p>
  2507. <p>(Contributed by Amaury Forgeot D’Arc; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9210">bpo-9210</a>.)</p>
  2508. </li>
  2509. <li><p>Hash values are now values of a new type, <a class="reference internal" href="../c-api/hash.html#c.Py_hash_t" title="Py_hash_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_hash_t</span></code></a>, which is
  2510. defined to be the same size as a pointer. Previously they were of type long,
  2511. which on some 64-bit operating systems is still only 32 bits long. As a
  2512. result of this fix, <a class="reference internal" href="../library/stdtypes.html#set" title="set"><code class="xref py py-class docutils literal notranslate"><span class="pre">set</span></code></a> and <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> can now hold more than
  2513. <code class="docutils literal notranslate"><span class="pre">2**32</span></code> entries on builds with 64-bit pointers (previously, they could grow
  2514. to that size but their performance degraded catastrophically).</p>
  2515. <p>(Suggested by Raymond Hettinger and implemented by Benjamin Peterson;
  2516. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=9778">bpo-9778</a>.)</p>
  2517. </li>
  2518. <li><p>A new macro <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_VA_COPY</span></code> copies the state of the variable argument
  2519. list. It is equivalent to C99 <em>va_copy</em> but available on all Python platforms
  2520. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2443">bpo-2443</a>).</p></li>
  2521. <li><p>A new C API function <code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code> allows an embedded interpreter
  2522. to set <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> without also modifying <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>
  2523. (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5753">bpo-5753</a>).</p></li>
  2524. <li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_CallObject()</span></code> is now only available in macro form. The
  2525. function declaration, which was kept for backwards compatibility reasons, is
  2526. now removed – the macro was introduced in 1997 (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8276">bpo-8276</a>).</p></li>
  2527. <li><p>There is a new function <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> which
  2528. is analogous to <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>. They both serve to
  2529. convert Python <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> into a native fixed-width type while providing
  2530. detection of cases where the conversion won’t fit (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7767">bpo-7767</a>).</p></li>
  2531. <li><p>The <a class="reference internal" href="../c-api/unicode.html#c.PyUnicode_CompareWithASCIIString" title="PyUnicode_CompareWithASCIIString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnicode_CompareWithASCIIString()</span></code></a> function now returns <em>not
  2532. equal</em> if the Python string is <em>NUL</em> terminated.</p></li>
  2533. <li><p>There is a 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> that is
  2534. like <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> but allows a docstring to be specified.
  2535. This lets C exceptions have the same self-documenting capabilities as
  2536. their pure Python counterparts (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=7033">bpo-7033</a>).</p></li>
  2537. <li><p>When compiled with the <code class="docutils literal notranslate"><span class="pre">--with-valgrind</span></code> option, the pymalloc
  2538. allocator will be automatically disabled when running under Valgrind. This
  2539. gives improved memory leak detection when running under Valgrind, while taking
  2540. advantage of pymalloc at other times (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=2422">bpo-2422</a>).</p></li>
  2541. <li><p>Removed the <code class="docutils literal notranslate"><span class="pre">O?</span></code> format from the <em>PyArg_Parse</em> functions. The format is no
  2542. longer used and it had never been documented (<a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8837">bpo-8837</a>).</p></li>
  2543. </ul>
  2544. <p>There were a number of other small changes to the C-API. See the
  2545. <a class="reference external" href="https://github.com/python/cpython/blob/v3.2.6/Misc/NEWS">Misc/NEWS</a>
  2546. file for a complete list.</p>
  2547. <p>Also, there were a number of updates to the Mac OS X build, see
  2548. <a class="reference external" href="https://github.com/python/cpython/blob/v3.2.6/Mac/BuildScript/README.txt">Mac/BuildScript/README.txt</a>
  2549. for details. For users running a 32/64-bit
  2550. build, there is a known problem with the default Tcl/Tk on Mac OS X 10.6.
  2551. Accordingly, we recommend installing an updated alternative such as
  2552. <a class="reference external" href="https://web.archive.org/web/20101208191259/https://www.activestate.com/activetcl/downloads">ActiveState Tcl/Tk 8.5.9</a>.
  2553. See <a class="reference external" href="https://www.python.org/download/mac/tcltk/">https://www.python.org/download/mac/tcltk/</a> for additional details.</p>
  2554. </section>
  2555. <section id="porting-to-python-3-2">
  2556. <h2>Porting to Python 3.2<a class="headerlink" href="#porting-to-python-3-2" title="Link to this heading">¶</a></h2>
  2557. <p>This section lists previously described changes and other bugfixes that may
  2558. require changes to your code:</p>
  2559. <ul>
  2560. <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 has a number of clean-ups. The major change is
  2561. to replace the old <code class="xref py py-class docutils literal notranslate"><span class="pre">ConfigParser</span></code> class with long-standing preferred
  2562. alternative <code class="xref py py-class docutils literal notranslate"><span class="pre">SafeConfigParser</span></code>. In addition there are a number of
  2563. smaller incompatibilities:</p>
  2564. <ul class="simple">
  2565. <li><p>The interpolation syntax is now validated on
  2566. <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.get" title="configparser.ConfigParser.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> and
  2567. <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> operations. In the default
  2568. interpolation scheme, only two tokens with percent signs are valid: <code class="docutils literal notranslate"><span class="pre">%(name)s</span></code>
  2569. and <code class="docutils literal notranslate"><span class="pre">%%</span></code>, the latter being an escaped percent sign.</p></li>
  2570. <li><p>The <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.set" title="configparser.ConfigParser.set"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code></a> and
  2571. <a class="reference internal" href="../library/configparser.html#configparser.ConfigParser.add_section" title="configparser.ConfigParser.add_section"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_section()</span></code></a> methods now verify that
  2572. values are actual strings. Formerly, unsupported types could be introduced
  2573. unintentionally.</p></li>
  2574. <li><p>Duplicate sections or options from a single source now raise either
  2575. <a class="reference internal" href="../library/configparser.html#configparser.DuplicateSectionError" title="configparser.DuplicateSectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DuplicateSectionError</span></code></a> or
  2576. <a class="reference internal" href="../library/configparser.html#configparser.DuplicateOptionError" title="configparser.DuplicateOptionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DuplicateOptionError</span></code></a>. Formerly, duplicates would
  2577. silently overwrite a previous entry.</p></li>
  2578. <li><p>Inline comments are now disabled by default so now the <strong>;</strong> character
  2579. can be safely used in values.</p></li>
  2580. <li><p>Comments now can be indented. Consequently, for <strong>;</strong> or <strong>#</strong> to appear at
  2581. the start of a line in multiline values, it has to be interpolated. This
  2582. keeps comment prefix characters in values from being mistaken as comments.</p></li>
  2583. <li><p><code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> is now a valid value and is no longer automatically converted to an
  2584. empty string. For empty strings, use <code class="docutils literal notranslate"><span class="pre">&quot;option</span> <span class="pre">=&quot;</span></code> in a line.</p></li>
  2585. </ul>
  2586. </li>
  2587. <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 was reworked extensively, meaning that its APIs
  2588. are often incompatible with the 3.1 APIs.</p></li>
  2589. <li><p><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 can no longer be used as filenames; instead,
  2590. they should be converted to <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>.</p></li>
  2591. <li><p>The <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.tostring()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">array.fromstring()</span></code> have been renamed to
  2592. <a class="reference internal" href="../library/array.html#array.array.tobytes" title="array.array.tobytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">array.tobytes()</span></code></a> and
  2593. <a class="reference internal" href="../library/array.html#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">array.frombytes()</span></code></a> for clarity. The old names
  2594. have been deprecated. (See <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=8990">bpo-8990</a>.)</p></li>
  2595. <li><p><code class="docutils literal notranslate"><span class="pre">PyArg_Parse*()</span></code> functions:</p>
  2596. <ul class="simple">
  2597. <li><p>“t#” format has been removed: use “s#” or “s*” instead</p></li>
  2598. <li><p>“w” and “w#” formats has been removed: use “w*” instead</p></li>
  2599. </ul>
  2600. </li>
  2601. <li><p>The <code class="xref c c-type docutils literal notranslate"><span class="pre">PyCObject</span></code> type, deprecated in 3.1, has been removed. To wrap
  2602. opaque C pointers in Python objects, 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> API should be used
  2603. instead; the new type has a well-defined interface for passing typing safety
  2604. information and a less complicated signature for calling a destructor.</p></li>
  2605. <li><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setfilesystemencoding()</span></code> function was removed because
  2606. it had a flawed design.</p></li>
  2607. <li><p>The <a class="reference internal" href="../library/random.html#random.seed" title="random.seed"><code class="xref py py-func docutils literal notranslate"><span class="pre">random.seed()</span></code></a> function and method now salt string seeds with an
  2608. sha512 hash function. To access the previous version of <em>seed</em> in order to
  2609. reproduce Python 3.1 sequences, set the <em>version</em> argument to <em>1</em>,
  2610. <code class="docutils literal notranslate"><span class="pre">random.seed(s,</span> <span class="pre">version=1)</span></code>.</p></li>
  2611. <li><p>The previously deprecated <code class="xref py py-func docutils literal notranslate"><span class="pre">string.maketrans()</span></code> function has been removed
  2612. in favor of the static methods <a class="reference internal" href="../library/stdtypes.html#bytes.maketrans" title="bytes.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytes.maketrans()</span></code></a> and
  2613. <a class="reference internal" href="../library/stdtypes.html#bytearray.maketrans" title="bytearray.maketrans"><code class="xref py py-meth docutils literal notranslate"><span class="pre">bytearray.maketrans()</span></code></a>. This change solves the confusion around which
  2614. types were supported by the <a class="reference internal" href="../library/string.html#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module. Now, <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>,
  2615. <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>, 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> each have their own <strong>maketrans</strong> and
  2616. <strong>translate</strong> methods with intermediate translation tables of the appropriate
  2617. type.</p>
  2618. <p>(Contributed by Georg Brandl; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=5675">bpo-5675</a>.)</p>
  2619. </li>
  2620. <li><p>The previously deprecated <code class="xref py py-func docutils literal notranslate"><span class="pre">contextlib.nested()</span></code> function has been removed
  2621. in favor of a plain <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 which can accept multiple
  2622. context managers. The latter technique is faster (because it is built-in),
  2623. and it does a better job finalizing multiple context managers when one of them
  2624. raises an exception:</p>
  2625. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;mylog.txt&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">infile</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;a.out&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">outfile</span><span class="p">:</span>
  2626. <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">infile</span><span class="p">:</span>
  2627. <span class="k">if</span> <span class="s1">&#39;&lt;critical&gt;&#39;</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
  2628. <span class="n">outfile</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
  2629. </pre></div>
  2630. </div>
  2631. <p>(Contributed by Georg Brandl and Mattias Brändström;
  2632. <a class="reference external" href="https://codereview.appspot.com/53094">appspot issue 53094</a>.)</p>
  2633. </li>
  2634. <li><p><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">struct.pack()</span></code></a> now only allows bytes for the <code class="docutils literal notranslate"><span class="pre">s</span></code> string pack code.
  2635. Formerly, it would accept text arguments and implicitly encode them to bytes
  2636. using UTF-8. This was problematic because it made assumptions about the
  2637. correct encoding and because a variable-length encoding can fail when writing
  2638. to fixed length segment of a structure.</p>
  2639. <p>Code such as <code class="docutils literal notranslate"><span class="pre">struct.pack('&lt;6sHHBBB',</span> <span class="pre">'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></code> should be rewritten
  2640. with to use bytes instead of text, <code class="docutils literal notranslate"><span class="pre">struct.pack('&lt;6sHHBBB',</span> <span class="pre">b'GIF87a',</span> <span class="pre">x,</span> <span class="pre">y)</span></code>.</p>
  2641. <p>(Discovered by David Beazley and fixed by Victor Stinner; <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10783">bpo-10783</a>.)</p>
  2642. </li>
  2643. <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-class docutils literal notranslate"><span class="pre">xml.etree.ElementTree</span></code></a> class now raises an
  2644. <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">xml.etree.ElementTree.ParseError</span></code></a> when a parse fails. Previously it
  2645. raised an <a class="reference internal" href="../library/pyexpat.html#xml.parsers.expat.ExpatError" title="xml.parsers.expat.ExpatError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">xml.parsers.expat.ExpatError</span></code></a>.</p></li>
  2646. <li><p>The new, longer <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> value on floats may break doctests which rely on
  2647. the old output format.</p></li>
  2648. <li><p>In <a class="reference internal" href="../library/subprocess.html#subprocess.Popen" title="subprocess.Popen"><code class="xref py py-class docutils literal notranslate"><span class="pre">subprocess.Popen</span></code></a>, the default value for <em>close_fds</em> is now
  2649. <code class="docutils literal notranslate"><span class="pre">True</span></code> under Unix; under Windows, it is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the three standard
  2650. streams are set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise. Previously, <em>close_fds</em>
  2651. was always <code class="docutils literal notranslate"><span class="pre">False</span></code> by default, which produced difficult to solve bugs
  2652. or race conditions when open file descriptors would leak into the child
  2653. process.</p></li>
  2654. <li><p>Support for legacy HTTP 0.9 has been removed from <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">urllib.request</span></code></a>
  2655. and <a class="reference internal" href="../library/http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a>. Such support is still present on the server side
  2656. (in <a class="reference internal" href="../library/http.server.html#module-http.server" title="http.server: HTTP server and request handlers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code></a>).</p>
  2657. <p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10711">bpo-10711</a>.)</p>
  2658. </li>
  2659. <li><p>SSL sockets in timeout mode now raise <a class="reference internal" href="../library/socket.html#socket.timeout" title="socket.timeout"><code class="xref py py-exc docutils literal notranslate"><span class="pre">socket.timeout</span></code></a> when a timeout
  2660. occurs, rather than a generic <a class="reference internal" href="../library/ssl.html#ssl.SSLError" title="ssl.SSLError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SSLError</span></code></a>.</p>
  2661. <p>(Contributed by Antoine Pitrou, <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=10272">bpo-10272</a>.)</p>
  2662. </li>
  2663. <li><p>The misleading functions <code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_AcquireLock()</span></code> and
  2664. <code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ReleaseLock()</span></code> have been officially deprecated. The
  2665. thread-state aware APIs (such as <a class="reference internal" href="../c-api/init.html#c.PyEval_SaveThread" title="PyEval_SaveThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SaveThread()</span></code></a>
  2666. and <a class="reference internal" href="../c-api/init.html#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a>) should be used instead.</p></li>
  2667. <li><p>Due to security risks, <code class="xref py py-func docutils literal notranslate"><span class="pre">asyncore.handle_accept()</span></code> has been deprecated, and
  2668. a new function, <code class="xref py py-func docutils literal notranslate"><span class="pre">asyncore.handle_accepted()</span></code>, was added to replace it.</p>
  2669. <p>(Contributed by Giampaolo Rodola in <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=6706">bpo-6706</a>.)</p>
  2670. </li>
  2671. <li><p>Due to the new <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> implementation, <code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_InitThreads()</span></code>
  2672. cannot be called before <a class="reference internal" href="../c-api/init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> anymore.</p></li>
  2673. </ul>
  2674. </section>
  2675. </section>
  2676. <div class="clearer"></div>
  2677. </div>
  2678. </div>
  2679. </div>
  2680. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2681. <div class="sphinxsidebarwrapper">
  2682. <div>
  2683. <h3><a href="../contents.html">Table of Contents</a></h3>
  2684. <ul>
  2685. <li><a class="reference internal" href="#">What’s New In Python 3.2</a><ul>
  2686. <li><a class="reference internal" href="#pep-384-defining-a-stable-abi">PEP 384: Defining a Stable ABI</a></li>
  2687. <li><a class="reference internal" href="#pep-389-argparse-command-line-parsing-module">PEP 389: Argparse Command Line Parsing Module</a></li>
  2688. <li><a class="reference internal" href="#pep-391-dictionary-based-configuration-for-logging">PEP 391: Dictionary Based Configuration for Logging</a></li>
  2689. <li><a class="reference internal" href="#pep-3148-the-concurrent-futures-module">PEP 3148: The <code class="docutils literal notranslate"><span class="pre">concurrent.futures</span></code> module</a></li>
  2690. <li><a class="reference internal" href="#pep-3147-pyc-repository-directories">PEP 3147: PYC Repository Directories</a></li>
  2691. <li><a class="reference internal" href="#pep-3149-abi-version-tagged-so-files">PEP 3149: ABI Version Tagged .so Files</a></li>
  2692. <li><a class="reference internal" href="#pep-3333-python-web-server-gateway-interface-v1-0-1">PEP 3333: Python Web Server Gateway Interface v1.0.1</a></li>
  2693. <li><a class="reference internal" href="#other-language-changes">Other Language Changes</a></li>
  2694. <li><a class="reference internal" href="#new-improved-and-deprecated-modules">New, Improved, and Deprecated Modules</a><ul>
  2695. <li><a class="reference internal" href="#email">email</a></li>
  2696. <li><a class="reference internal" href="#elementtree">elementtree</a></li>
  2697. <li><a class="reference internal" href="#functools">functools</a></li>
  2698. <li><a class="reference internal" href="#itertools">itertools</a></li>
  2699. <li><a class="reference internal" href="#collections">collections</a></li>
  2700. <li><a class="reference internal" href="#threading">threading</a></li>
  2701. <li><a class="reference internal" href="#datetime-and-time">datetime and time</a></li>
  2702. <li><a class="reference internal" href="#math">math</a></li>
  2703. <li><a class="reference internal" href="#abc">abc</a></li>
  2704. <li><a class="reference internal" href="#io">io</a></li>
  2705. <li><a class="reference internal" href="#reprlib">reprlib</a></li>
  2706. <li><a class="reference internal" href="#logging">logging</a></li>
  2707. <li><a class="reference internal" href="#csv">csv</a></li>
  2708. <li><a class="reference internal" href="#contextlib">contextlib</a></li>
  2709. <li><a class="reference internal" href="#decimal-and-fractions">decimal and fractions</a></li>
  2710. <li><a class="reference internal" href="#ftp">ftp</a></li>
  2711. <li><a class="reference internal" href="#popen">popen</a></li>
  2712. <li><a class="reference internal" href="#select">select</a></li>
  2713. <li><a class="reference internal" href="#gzip-and-zipfile">gzip and zipfile</a></li>
  2714. <li><a class="reference internal" href="#tarfile">tarfile</a></li>
  2715. <li><a class="reference internal" href="#hashlib">hashlib</a></li>
  2716. <li><a class="reference internal" href="#ast">ast</a></li>
  2717. <li><a class="reference internal" href="#os">os</a></li>
  2718. <li><a class="reference internal" href="#shutil">shutil</a></li>
  2719. <li><a class="reference internal" href="#sqlite3">sqlite3</a></li>
  2720. <li><a class="reference internal" href="#html">html</a></li>
  2721. <li><a class="reference internal" href="#socket">socket</a></li>
  2722. <li><a class="reference internal" href="#ssl">ssl</a></li>
  2723. <li><a class="reference internal" href="#nntp">nntp</a></li>
  2724. <li><a class="reference internal" href="#certificates">certificates</a></li>
  2725. <li><a class="reference internal" href="#imaplib">imaplib</a></li>
  2726. <li><a class="reference internal" href="#http-client">http.client</a></li>
  2727. <li><a class="reference internal" href="#unittest">unittest</a></li>
  2728. <li><a class="reference internal" href="#random">random</a></li>
  2729. <li><a class="reference internal" href="#poplib">poplib</a></li>
  2730. <li><a class="reference internal" href="#asyncore">asyncore</a></li>
  2731. <li><a class="reference internal" href="#tempfile">tempfile</a></li>
  2732. <li><a class="reference internal" href="#inspect">inspect</a></li>
  2733. <li><a class="reference internal" href="#pydoc">pydoc</a></li>
  2734. <li><a class="reference internal" href="#dis">dis</a></li>
  2735. <li><a class="reference internal" href="#dbm">dbm</a></li>
  2736. <li><a class="reference internal" href="#ctypes">ctypes</a></li>
  2737. <li><a class="reference internal" href="#site">site</a></li>
  2738. <li><a class="reference internal" href="#sysconfig">sysconfig</a></li>
  2739. <li><a class="reference internal" href="#pdb">pdb</a></li>
  2740. <li><a class="reference internal" href="#configparser">configparser</a></li>
  2741. <li><a class="reference internal" href="#urllib-parse">urllib.parse</a></li>
  2742. <li><a class="reference internal" href="#mailbox">mailbox</a></li>
  2743. <li><a class="reference internal" href="#turtledemo">turtledemo</a></li>
  2744. </ul>
  2745. </li>
  2746. <li><a class="reference internal" href="#multi-threading">Multi-threading</a></li>
  2747. <li><a class="reference internal" href="#optimizations">Optimizations</a></li>
  2748. <li><a class="reference internal" href="#unicode">Unicode</a></li>
  2749. <li><a class="reference internal" href="#codecs">Codecs</a></li>
  2750. <li><a class="reference internal" href="#documentation">Documentation</a></li>
  2751. <li><a class="reference internal" href="#idle">IDLE</a></li>
  2752. <li><a class="reference internal" href="#code-repository">Code Repository</a></li>
  2753. <li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a></li>
  2754. <li><a class="reference internal" href="#porting-to-python-3-2">Porting to Python 3.2</a></li>
  2755. </ul>
  2756. </li>
  2757. </ul>
  2758. </div>
  2759. <div>
  2760. <h4>Previous topic</h4>
  2761. <p class="topless"><a href="3.3.html"
  2762. title="previous chapter">What’s New In Python 3.3</a></p>
  2763. </div>
  2764. <div>
  2765. <h4>Next topic</h4>
  2766. <p class="topless"><a href="3.1.html"
  2767. title="next chapter">What’s New In Python 3.1</a></p>
  2768. </div>
  2769. <div role="note" aria-label="source link">
  2770. <h3>This Page</h3>
  2771. <ul class="this-page-menu">
  2772. <li><a href="../bugs.html">Report a Bug</a></li>
  2773. <li>
  2774. <a href="https://github.com/python/cpython/blob/main/Doc/whatsnew/3.2.rst"
  2775. rel="nofollow">Show Source
  2776. </a>
  2777. </li>
  2778. </ul>
  2779. </div>
  2780. </div>
  2781. <div id="sidebarbutton" title="Collapse sidebar">
  2782. <span>«</span>
  2783. </div>
  2784. </div>
  2785. <div class="clearer"></div>
  2786. </div>
  2787. <div class="related" role="navigation" aria-label="related navigation">
  2788. <h3>Navigation</h3>
  2789. <ul>
  2790. <li class="right" style="margin-right: 10px">
  2791. <a href="../genindex.html" title="General Index"
  2792. >index</a></li>
  2793. <li class="right" >
  2794. <a href="../py-modindex.html" title="Python Module Index"
  2795. >modules</a> |</li>
  2796. <li class="right" >
  2797. <a href="3.1.html" title="What’s New In Python 3.1"
  2798. >next</a> |</li>
  2799. <li class="right" >
  2800. <a href="3.3.html" title="What’s New In Python 3.3"
  2801. >previous</a> |</li>
  2802. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2803. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2804. <li class="switchers">
  2805. <div class="language_switcher_placeholder"></div>
  2806. <div class="version_switcher_placeholder"></div>
  2807. </li>
  2808. <li>
  2809. </li>
  2810. <li id="cpython-language-and-version">
  2811. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2812. </li>
  2813. <li class="nav-item nav-item-1"><a href="index.html" >What’s New in Python</a> &#187;</li>
  2814. <li class="nav-item nav-item-this"><a href="">What’s New In Python 3.2</a></li>
  2815. <li class="right">
  2816. <div class="inline-search" role="search">
  2817. <form class="inline-search" action="../search.html" method="get">
  2818. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2819. <input type="submit" value="Go" />
  2820. </form>
  2821. </div>
  2822. |
  2823. </li>
  2824. <li class="right">
  2825. <label class="theme-selector-label">
  2826. Theme
  2827. <select class="theme-selector" oninput="activateTheme(this.value)">
  2828. <option value="auto" selected>Auto</option>
  2829. <option value="light">Light</option>
  2830. <option value="dark">Dark</option>
  2831. </select>
  2832. </label> |</li>
  2833. </ul>
  2834. </div>
  2835. <div class="footer">
  2836. &copy;
  2837. <a href="../copyright.html">
  2838. Copyright
  2839. </a>
  2840. 2001-2024, Python Software Foundation.
  2841. <br />
  2842. This page is licensed under the Python Software Foundation License Version 2.
  2843. <br />
  2844. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2845. <br />
  2846. See <a href="/license.html">History and License</a> for more information.<br />
  2847. <br />
  2848. The Python Software Foundation is a non-profit corporation.
  2849. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2850. <br />
  2851. <br />
  2852. Last updated on Apr 09, 2024 (13:47 UTC).
  2853. <a href="/bugs.html">Found a bug</a>?
  2854. <br />
  2855. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2856. </div>
  2857. </body>
  2858. </html>
上海开阖软件有限公司 沪ICP备12045867号-1