Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1 | **************************** |
| 2 | What's New in Python 2.7 |
| 3 | **************************** |
| 4 | |
| 5 | :Author: A.M. Kuchling (amk at amk.ca) |
| 6 | :Release: |release| |
| 7 | :Date: |today| |
| 8 | |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 9 | .. Fix accents on Kristjan Valur Jonsson, Fuerstenau |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 10 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 11 | .. Big jobs: argparse, ElementTree 1.3, pep 391, 3106, sysconfig |
| 12 | .. unittest test discovery |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 13 | .. hyperlink all the methods & functions. |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 14 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 15 | .. $Id$ |
| 16 | Rules for maintenance: |
| 17 | |
| 18 | * Anyone can add text to this document. Do not spend very much time |
| 19 | on the wording of your changes, because your text will probably |
| 20 | get rewritten to some degree. |
| 21 | |
| 22 | * The maintainer will go through Misc/NEWS periodically and add |
| 23 | changes; it's therefore more important to add your changes to |
| 24 | Misc/NEWS than to this file. |
| 25 | |
| 26 | * This is not a complete list of every single change; completeness |
| 27 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 28 | or esoteric to include. If such a change is added to the text, |
| 29 | I'll just remove it. (This is another reason you shouldn't spend |
| 30 | too much time on writing your addition.) |
| 31 | |
| 32 | * If you want to draw your new text to the attention of the |
| 33 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 34 | section. |
| 35 | |
| 36 | * It's OK to just add a fragmentary note about a change. For |
| 37 | example: "XXX Describe the transmogrify() function added to the |
| 38 | socket module." The maintainer will research the change and |
| 39 | write the necessary text. |
| 40 | |
| 41 | * You can comment out your additions if you like, but it's not |
| 42 | necessary (especially when a final release is some months away). |
| 43 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 44 | * Credit the author of a patch or bugfix. Just the name is |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 45 | sufficient; the e-mail address isn't necessary. |
| 46 | |
| 47 | * It's helpful to add the bug/patch number in a parenthetical comment. |
| 48 | |
| 49 | XXX Describe the transmogrify() function added to the socket |
| 50 | module. |
| 51 | (Contributed by P.Y. Developer; :issue:`12345`.) |
| 52 | |
| 53 | This saves the maintainer some effort going through the SVN logs |
| 54 | when researching a change. |
| 55 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 56 | This article explains the new features in Python 2.7. The final |
| 57 | release of 2.7 is currently scheduled for June 2010; the detailed |
| 58 | schedule is described in :pep:`373`. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 59 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 60 | Python 2.7 is planned to be the last major release in the 2.x series. |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 61 | Though more major releases have not been absolutely ruled out, the |
| 62 | Python maintainers are planning to focus more on Python 3.x. Despite |
| 63 | that, it's likely that the 2.7 release will have a longer period of |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 64 | maintenance compared to earlier 2.x versions. |
| 65 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 66 | .. Compare with previous release in 2 - 3 sentences here. |
| 67 | add hyperlink when the documentation becomes available online. |
| 68 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 69 | .. _whatsnew27-python31: |
| 70 | |
| 71 | Python 3.1 Features |
| 72 | ======================= |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 73 | |
| 74 | Much as Python 2.6 incorporated features from Python 3.0, |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 75 | version 2.7 incorporates some of the new features |
| 76 | in Python 3.1. The 2.x series continues to provide tools |
| 77 | for migrating to the 3.x series. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 78 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 79 | A partial list of 3.1 features that were backported to 2.7: |
| 80 | |
| 81 | * A version of the :mod:`io` library, rewritten in C for performance. |
| 82 | * The ordered-dictionary type described in :ref:`pep-0372`. |
Andrew M. Kuchling | 8f254e7 | 2009-12-08 02:37:05 +0000 | [diff] [blame] | 83 | * The new format specifier described in :ref:`pep-0378`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 84 | * The :class:`memoryview` object. |
| 85 | * A small subset of the :mod:`importlib` module `described below <#importlib-section>`__. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 86 | * Float-to-string and string-to-float conversions now round their |
| 87 | results more correctly. And :func:`repr` of a floating-point |
| 88 | number *x* returns a result that's guaranteed to round back to the |
| 89 | same number when converted back to a string. |
| 90 | * The :cfunc:`PyLong_AsLongAndOverflow` C API function. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 91 | |
| 92 | One porting change: the :option:`-3` switch now automatically |
| 93 | enables the :option:`-Qwarn` switch that causes warnings |
| 94 | about using classic division with integers and long integers. |
| 95 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 96 | Other new Python3-mode warnings include: |
| 97 | |
| 98 | * :func:`operator.isCallable` and :func:`operator.sequenceIncludes`, |
| 99 | which are not supported in 3.x. |
| 100 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 101 | .. ======================================================================== |
| 102 | .. Large, PEP-level features and changes should be described here. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 103 | .. ======================================================================== |
| 104 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 105 | .. _pep-0372: |
| 106 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 107 | PEP 372: Adding an ordered dictionary to collections |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 108 | ==================================================== |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 109 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 110 | Regular Python dictionaries iterate over key/value pairs in arbitrary order. |
| 111 | Over the years, a number of authors have written alternative implementations |
| 112 | that remember the order that the keys were originally inserted. Based on |
| 113 | the experiences from those implementations, a new |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 114 | :class:`~collections.OrderedDict` class has been introduced in the |
| 115 | :mod:`collections` module. |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 116 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 117 | The :class:`~collections.OrderedDict` API is substantially the same as regular |
| 118 | dictionaries but will iterate over keys and values in a guaranteed order |
| 119 | depending on when a key was first inserted:: |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 120 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 121 | >>> from collections import OrderedDict |
| 122 | >>> d = OrderedDict([('first', 1), ('second', 2), |
| 123 | ... ('third', 3)]) |
| 124 | >>> d.items() |
| 125 | [('first', 1), ('second', 2), ('third', 3)] |
| 126 | |
| 127 | If a new entry overwrites an existing entry, the original insertion |
| 128 | position is left unchanged:: |
| 129 | |
| 130 | >>> d['second'] = 4 |
| 131 | >>> d.items() |
| 132 | [('first', 1), ('second', 4), ('third', 3)] |
| 133 | |
| 134 | Deleting an entry and reinserting it will move it to the end:: |
| 135 | |
| 136 | >>> del d['second'] |
| 137 | >>> d['second'] = 5 |
| 138 | >>> d.items() |
| 139 | [('first', 1), ('third', 3), ('second', 5)] |
| 140 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 141 | The :meth:`~collections.OrderedDict.popitem` method has an optional *last* |
| 142 | argument that defaults to True. If *last* is True, the most recently |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 143 | added key is returned and removed; if it's False, the |
| 144 | oldest key is selected:: |
| 145 | |
| 146 | >>> od = OrderedDict([(x,0) for x in range(20)]) |
| 147 | >>> od.popitem() |
| 148 | (19, 0) |
| 149 | >>> od.popitem() |
| 150 | (18, 0) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 151 | >>> od.popitem(last=False) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 152 | (0, 0) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 153 | >>> od.popitem(last=False) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 154 | (1, 0) |
| 155 | |
| 156 | Comparing two ordered dictionaries checks both the keys and values, |
| 157 | and requires that the insertion order was the same:: |
| 158 | |
| 159 | >>> od1 = OrderedDict([('first', 1), ('second', 2), |
| 160 | ... ('third', 3)]) |
| 161 | >>> od2 = OrderedDict([('third', 3), ('first', 1), |
| 162 | ... ('second', 2)]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 163 | >>> od1 == od2 |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 164 | False |
| 165 | >>> # Move 'third' key to the end |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 166 | >>> del od2['third']; od2['third'] = 3 |
| 167 | >>> od1 == od2 |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 168 | True |
| 169 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 170 | Comparing an :class:`~collections.OrderedDict` with a regular dictionary |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 171 | ignores the insertion order and just compares the keys and values. |
| 172 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 173 | How does the :class:`~collections.OrderedDict` work? It maintains a |
| 174 | doubly-linked list of keys, appending new keys to the list as they're inserted. |
| 175 | A secondary dictionary maps keys to their corresponding list node, so |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 176 | deletion doesn't have to traverse the entire linked list and therefore |
| 177 | remains O(1). |
| 178 | |
| 179 | .. XXX check O(1)-ness with Raymond |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 180 | .. Also check if the 'somenamedtuple' in the collection module should |
| 181 | .. be replaced/removed in order to use |
| 182 | .. :meth:`~collections.namedtuple._asdict()` (see below) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 183 | |
| 184 | The standard library now supports use of ordered dictionaries in several |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 185 | modules. The :mod:`ConfigParser` module uses them by default. This lets |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 186 | configuration files be read, modified, and then written back in their original |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 187 | order. The :meth:`~collections.somenamedtuple._asdict()` method for |
| 188 | :func:`collections.namedtuple` now returns an ordered dictionary with the |
| 189 | values appearing in the same order as the underlying tuple indices. |
| 190 | The :mod:`json` module is being built-out with an *object_pairs_hook* to allow |
| 191 | OrderedDicts to be built by the decoder. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 192 | Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_. |
| 193 | |
Andrew M. Kuchling | 7fe65a0 | 2009-10-13 15:49:33 +0000 | [diff] [blame] | 194 | .. seealso:: |
| 195 | |
| 196 | :pep:`372` - Adding an ordered dictionary to collections |
| 197 | PEP written by Armin Ronacher and Raymond Hettinger; |
| 198 | implemented by Raymond Hettinger. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 199 | |
| 200 | .. _pep-0378: |
| 201 | |
| 202 | PEP 378: Format Specifier for Thousands Separator |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 203 | ================================================= |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 204 | |
| 205 | To make program output more readable, it can be useful to add |
| 206 | separators to large numbers and render them as |
| 207 | 18,446,744,073,709,551,616 instead of 18446744073709551616. |
| 208 | |
| 209 | The fully general solution for doing this is the :mod:`locale` module, |
| 210 | which can use different separators ("," in North America, "." in |
| 211 | Europe) and different grouping sizes, but :mod:`locale` is complicated |
| 212 | to use and unsuitable for multi-threaded applications where different |
| 213 | threads are producing output for different locales. |
| 214 | |
| 215 | Therefore, a simple comma-grouping mechanism has been added to the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 216 | mini-language used by the :meth:`str.format` method. When |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 217 | formatting a floating-point number, simply include a comma between the |
| 218 | width and the precision:: |
| 219 | |
Eric Smith | c466385 | 2010-04-06 14:30:15 +0000 | [diff] [blame] | 220 | >>> '{:20,.2f}'.format(18446744073709551616.0) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 221 | '18,446,744,073,709,551,616.00' |
| 222 | |
Eric Smith | 6a92860 | 2010-04-06 15:17:33 +0000 | [diff] [blame] | 223 | When formatting an integer, include the comma after the width: |
| 224 | |
| 225 | >>> '{:20,d}'.format(18446744073709551616) |
| 226 | '18,446,744,073,709,551,616' |
| 227 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 228 | This mechanism is not adaptable at all; commas are always used as the |
| 229 | separator and the grouping is always into three-digit groups. The |
| 230 | comma-formatting mechanism isn't as general as the :mod:`locale` |
| 231 | module, but it's easier to use. |
| 232 | |
Andrew M. Kuchling | 85ea4bf | 2009-10-05 22:45:39 +0000 | [diff] [blame] | 233 | .. XXX "Format String Syntax" in string.rst could use many more examples. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 234 | |
| 235 | .. seealso:: |
| 236 | |
| 237 | :pep:`378` - Format Specifier for Thousands Separator |
| 238 | PEP written by Raymond Hettinger; implemented by Eric Smith. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 239 | |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 240 | PEP 389: The argparse Module for Parsing Command Lines |
| 241 | ====================================================== |
| 242 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 243 | The :mod:`argparse` module for parsing command-line arguments was |
| 244 | added, intended as a more powerful replacement for the |
| 245 | :mod:`optparse` module. |
| 246 | |
| 247 | This means Python now supports three different modules for parsing |
| 248 | command-line arguments: :mod:`getopt`, :mod:`optparse`, and |
| 249 | :mod:`argparse`. The :mod:`getopt` module closely resembles the C |
| 250 | :cfunc:`getopt` function, so it remains useful if you're writing a |
| 251 | Python prototype that will eventually be rewritten in C. |
| 252 | :mod:`optparse` becomes redundant, but there are no plans to remove it |
| 253 | because there are many scripts still using it, and there's no |
| 254 | automated way to update these scripts. (Making the :mod:`argparse` |
| 255 | API consistent with :mod:`optparse`'s interface was discussed but |
| 256 | rejected as too messy and difficult.) |
| 257 | |
Andrew M. Kuchling | f03641a | 2010-04-14 01:14:59 +0000 | [diff] [blame^] | 258 | In short, if you're writing a new script and don't need to worry |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 259 | about compatibility with earlier versions of Python, use |
| 260 | :mod:`argparse` instead of :mod:`optparse`. |
| 261 | |
Andrew M. Kuchling | f03641a | 2010-04-14 01:14:59 +0000 | [diff] [blame^] | 262 | Here's an example:: |
| 263 | |
| 264 | import argparse |
| 265 | |
| 266 | parser = argparse.ArgumentParser(description='Command-line example.') |
| 267 | |
| 268 | # Add optional switches |
| 269 | parser.add_argument('-v', action='store_true', dest='is_verbose', |
| 270 | help='produce verbose output') |
| 271 | parser.add_argument('-o', action='store', dest='output', |
| 272 | metavar='FILE', |
| 273 | help='direct output to FILE instead of stdout') |
| 274 | parser.add_argument('-C', action='store', type=int, dest='context', |
| 275 | metavar='NUM', default=0, |
| 276 | help='display NUM lines of added context') |
| 277 | |
| 278 | # Allow any number of additional arguments. |
| 279 | parser.add_argument(nargs='*', action='store', dest='inputs', |
| 280 | help='input filenames (default is stdin)') |
| 281 | |
| 282 | args = parser.parse_args() |
| 283 | print args.__dict__ |
| 284 | |
| 285 | Unless you override it, :option:`-h` and :option:`--help` switches |
| 286 | are automatically added, and produce neatly formatted output:: |
| 287 | |
| 288 | -> ./python.exe argparse-example.py --help |
| 289 | usage: parse.py [-h] [-v] [-o FILE] [-C NUM] |
| 290 | |
| 291 | Command-line example. |
| 292 | |
| 293 | positional arguments: |
| 294 | inputs input filenames (default is stdin) |
| 295 | |
| 296 | optional arguments: |
| 297 | -h, --help show this help message and exit |
| 298 | -v produce verbose output |
| 299 | -o FILE direct output to FILE instead of stdout |
| 300 | -C NUM display NUM lines of added context |
| 301 | |
| 302 | Similarly to :mod:`optparse`, the command-line switches and arguments |
| 303 | are returned as an object with attributes named by the *dest* parameters:: |
| 304 | |
| 305 | -> ./python.exe argparse-example.py -v |
| 306 | {'output': None, 'is_verbose': True, 'context': 0, 'inputs': []} |
| 307 | |
| 308 | -> ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2 |
| 309 | {'output': '/tmp/output', 'is_verbose': True, 'context': 4, |
| 310 | 'inputs': ['file1', 'file2']} |
| 311 | |
| 312 | :mod:`argparse` has much fancier validation than :mod:`optparse`; you |
| 313 | can specify an exact number of arguments as an integer, 0 or more |
| 314 | arguments by passing ``'*'``, 1 or more by passing ``'+'``, or an |
| 315 | optional argument with ``'?'``. A top-level parser can contain |
| 316 | sub-parsers, so you can define subcommands that have different sets of |
| 317 | switches, as in ``svn commit``, ``svn checkout``, etc. You can |
| 318 | specify an argument type as :class:`~argparse.FileType`, which will |
| 319 | automatically open files for you and understands that ``'-'`` means |
| 320 | standard input or output. |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 321 | |
| 322 | .. seealso:: |
| 323 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 324 | `argparse module documentation <http://docs.python.org/dev/library/argparse.html>`__ |
| 325 | |
| 326 | `Upgrading optparse code to use argparse <http://docs.python.org/dev/library/argparse.html#upgrading-optparse-code>`__ |
| 327 | |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 328 | :pep:`389` - argparse - New Command Line Parsing Module |
| 329 | PEP written and implemented by Steven Bethard. |
| 330 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 331 | PEP 391: Dictionary-Based Configuration For Logging |
| 332 | ==================================================== |
| 333 | |
| 334 | XXX write this section. |
| 335 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 336 | Two smaller enhancements to the logging module are: |
| 337 | |
| 338 | .. rev79293 |
| 339 | |
| 340 | * :class:`Logger` instances gained a :meth:`getChild` that retrieves a |
| 341 | descendant logger using a relative path. For example, |
| 342 | once you retrieve a logger by doing ``log = getLogger('app')``, |
| 343 | calling ``log.getChild('network.listen')`` is equivalent to |
| 344 | ``getLogger('app.network.listen')``. |
| 345 | |
| 346 | * The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method |
| 347 | that takes a *level* and returns whether the underlying logger would |
| 348 | process a message of that level of importance. |
| 349 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 350 | .. seealso:: |
| 351 | |
| 352 | :pep:`391` - Dictionary-Based Configuration For Logging |
| 353 | PEP written and implemented by Vinay Sajip. |
| 354 | |
| 355 | PEP 3106: Dictionary Views |
| 356 | ==================================================== |
| 357 | |
| 358 | XXX write this section. |
| 359 | |
| 360 | .. seealso:: |
| 361 | |
| 362 | :pep:`3106` - Revamping dict.keys(), .values() and .items() |
| 363 | PEP written by Guido van Rossum. |
| 364 | Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`. |
| 365 | |
| 366 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 367 | Other Language Changes |
| 368 | ====================== |
| 369 | |
| 370 | Some smaller changes made to the core Python language are: |
| 371 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 372 | * The syntax for set literals has been backported from Python 3.x. |
| 373 | Curly brackets are used to surround the contents of the resulting |
| 374 | mutable set; set literals are |
| 375 | distinguished from dictionaries by not containing colons and values. |
| 376 | ``{}`` continues to represent an empty dictionary; use |
| 377 | ``set()`` for an empty set. |
| 378 | |
| 379 | >>> {1,2,3,4,5} |
| 380 | set([1, 2, 3, 4, 5]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 381 | >>> set() # empty set |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 382 | set([]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 383 | >>> {} # empty dict |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 384 | {} |
| 385 | |
| 386 | Backported by Alexandre Vassalotti; :issue:`2335`. |
| 387 | |
| 388 | * Dictionary and set comprehensions are another feature backported from |
| 389 | 3.x, generalizing list/generator comprehensions to use |
| 390 | the literal syntax for sets and dictionaries. |
| 391 | |
| 392 | >>> {x:x*x for x in range(6)} |
| 393 | {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} |
| 394 | >>> {'a'*x for x in range(6)} |
| 395 | set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa']) |
| 396 | |
| 397 | Backported by Alexandre Vassalotti; :issue:`2333`. |
| 398 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 399 | * The :keyword:`with` statement can now use multiple context managers |
| 400 | in one statement. Context managers are processed from left to right |
| 401 | and each one is treated as beginning a new :keyword:`with` statement. |
| 402 | This means that:: |
| 403 | |
| 404 | with A() as a, B() as b: |
| 405 | ... suite of statements ... |
| 406 | |
| 407 | is equivalent to:: |
| 408 | |
| 409 | with A() as a: |
| 410 | with B() as b: |
| 411 | ... suite of statements ... |
| 412 | |
| 413 | The :func:`contextlib.nested` function provides a very similar |
| 414 | function, so it's no longer necessary and has been deprecated. |
| 415 | |
| 416 | (Proposed in http://codereview.appspot.com/53094; implemented by |
| 417 | Georg Brandl.) |
| 418 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 419 | * Conversions between floating-point numbers and strings are |
| 420 | now correctly rounded on most platforms. These conversions occur |
| 421 | in many different places: :func:`str` on |
| 422 | floats and complex numbers; the :class:`float` and :class:`complex` |
| 423 | constructors; |
| 424 | numeric formatting; serialization and |
| 425 | deserialization of floats and complex numbers using the |
| 426 | :mod:`marshal`, :mod:`pickle` |
| 427 | and :mod:`json` modules; |
| 428 | parsing of float and imaginary literals in Python code; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 429 | and :class:`~decimal.Decimal`-to-float conversion. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 430 | |
| 431 | Related to this, the :func:`repr` of a floating-point number *x* |
| 432 | now returns a result based on the shortest decimal string that's |
| 433 | guaranteed to round back to *x* under correct rounding (with |
| 434 | round-half-to-even rounding mode). Previously it gave a string |
| 435 | based on rounding x to 17 decimal digits. |
| 436 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 437 | .. maybe add an example? |
| 438 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 439 | The rounding library responsible for this improvement works on |
| 440 | Windows, and on Unix platforms using the gcc, icc, or suncc |
| 441 | compilers. There may be a small number of platforms where correct |
| 442 | operation of this code cannot be guaranteed, so the code is not |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 443 | used on such systems. You can find out which code is being used |
| 444 | by checking :data:`sys.float_repr_style`, which will be ``short`` |
| 445 | if the new code is in use and ``legacy`` if it isn't. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 446 | |
Mark Dickinson | bdd863d | 2010-01-07 09:28:29 +0000 | [diff] [blame] | 447 | Implemented by Eric Smith and Mark Dickinson, using David Gay's |
| 448 | :file:`dtoa.c` library; :issue:`7117`. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 449 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 450 | * The :meth:`str.format` method now supports automatic numbering of the replacement |
Benjamin Peterson | aa0a0b9 | 2009-04-11 20:27:15 +0000 | [diff] [blame] | 451 | fields. This makes using :meth:`str.format` more closely resemble using |
| 452 | ``%s`` formatting:: |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 453 | |
| 454 | >>> '{}:{}:{}'.format(2009, 04, 'Sunday') |
| 455 | '2009:4:Sunday' |
| 456 | >>> '{}:{}:{day}'.format(2009, 4, day='Sunday') |
| 457 | '2009:4:Sunday' |
| 458 | |
Benjamin Peterson | aa0a0b9 | 2009-04-11 20:27:15 +0000 | [diff] [blame] | 459 | The auto-numbering takes the fields from left to right, so the first ``{...}`` |
| 460 | specifier will use the first argument to :meth:`str.format`, the next |
| 461 | specifier will use the next argument, and so on. You can't mix auto-numbering |
| 462 | and explicit numbering -- either number all of your specifier fields or none |
| 463 | of them -- but you can mix auto-numbering and named fields, as in the second |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 464 | example above. (Contributed by Eric Smith; :issue:`5237`.) |
| 465 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 466 | Complex numbers now correctly support usage with :func:`format`, |
| 467 | and default to being right-aligned. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 468 | Specifying a precision or comma-separation applies to both the real |
| 469 | and imaginary parts of the number, but a specified field width and |
| 470 | alignment is applied to the whole of the resulting ``1.5+3j`` |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 471 | output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 472 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 473 | The 'F' format code now always formats its output using uppercase characters, |
| 474 | so it will now produce 'INF' and 'NAN'. |
| 475 | (Contributed by Eric Smith; :issue:`3382`.) |
| 476 | |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 477 | * The :func:`int` and :func:`long` types gained a ``bit_length`` |
Georg Brandl | 64e1c75 | 2009-04-11 18:19:27 +0000 | [diff] [blame] | 478 | method that returns the number of bits necessary to represent |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 479 | its argument in binary:: |
| 480 | |
| 481 | >>> n = 37 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 482 | >>> bin(n) |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 483 | '0b100101' |
| 484 | >>> n.bit_length() |
| 485 | 6 |
| 486 | >>> n = 2**123-1 |
| 487 | >>> n.bit_length() |
| 488 | 123 |
| 489 | >>> (n+1).bit_length() |
| 490 | 124 |
| 491 | |
| 492 | (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.) |
| 493 | |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 494 | * Conversions from long integers and regular integers to floating |
| 495 | point now round differently, returning the floating-point number |
| 496 | closest to the number. This doesn't matter for small integers that |
| 497 | can be converted exactly, but for large numbers that will |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 498 | unavoidably lose precision, Python 2.7 now approximates more |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 499 | closely. For example, Python 2.6 computed the following:: |
| 500 | |
| 501 | >>> n = 295147905179352891391 |
| 502 | >>> float(n) |
| 503 | 2.9514790517935283e+20 |
| 504 | >>> n - long(float(n)) |
| 505 | 65535L |
| 506 | |
| 507 | Python 2.7's floating-point result is larger, but much closer to the |
| 508 | true value:: |
| 509 | |
| 510 | >>> n = 295147905179352891391 |
| 511 | >>> float(n) |
| 512 | 2.9514790517935289e+20 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 513 | >>> n - long(float(n)) |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 514 | -1L |
| 515 | |
| 516 | (Implemented by Mark Dickinson; :issue:`3166`.) |
| 517 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 518 | Integer division is also more accurate in its rounding behaviours. (Also |
| 519 | implemented by Mark Dickinson; :issue:`1811`.) |
| 520 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 521 | * It's now possible for a subclass of the built-in :class:`unicode` type |
| 522 | to override the :meth:`__unicode__` method. (Implemented by |
| 523 | Victor Stinner; :issue:`1583863`.) |
| 524 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 525 | * The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 526 | ``None`` as its first argument. (Fixed by Georg Brandl; |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 527 | :issue:`4759`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 528 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 529 | .. bytearray doesn't seem to be documented |
| 530 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 531 | * When using ``@classmethod`` and ``@staticmethod`` to wrap |
| 532 | methods as class or static methods, the wrapper object now |
| 533 | exposes the wrapped function as their :attr:`__func__` attribute. |
| 534 | (Contributed by Amaury Forgeot d'Arc, after a suggestion by |
| 535 | George Sakkis; :issue:`5982`.) |
| 536 | |
| 537 | * A new encoding named "cp720", used primarily for Arabic text, is now |
| 538 | supported. (Contributed by Alexander Belchenko and Amaury Forgeot |
| 539 | d'Arc; :issue:`1616979`.) |
| 540 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 541 | * The :class:`file` object will now set the :attr:`filename` attribute |
| 542 | on the :exc:`IOError` exception when trying to open a directory |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 543 | on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and |
| 544 | now explicitly checks for and forbids writing to read-only file objects |
| 545 | instead of trusting the C library to catch and report the error |
| 546 | (fixed by Stefan Krah; :issue:`5677`). |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 547 | |
Benjamin Peterson | ae9a0a0 | 2009-12-31 16:49:37 +0000 | [diff] [blame] | 548 | * The Python tokenizer now translates line endings itself, so the |
| 549 | :func:`compile` built-in function can now accept code using any |
| 550 | line-ending convention. Additionally, it no longer requires that the |
| 551 | code end in a newline. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 552 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 553 | * Extra parentheses in function definitions are illegal in Python 3.x, |
| 554 | meaning that you get a syntax error from ``def f((x)): pass``. In |
| 555 | Python3-warning mode, Python 2.7 will now warn about this odd usage. |
| 556 | (Noted by James Lingard; :issue:`7362`.) |
| 557 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 558 | * It's now possible to create weak references to old-style class |
| 559 | objects. New-style classes were always weak-referenceable. (Fixed |
| 560 | by Antoine Pitrou; :issue:`8268`.) |
| 561 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 562 | * When a module object is garbage-collected, the module's dictionary is |
| 563 | now only cleared if no one else is holding a reference to the |
| 564 | dictionary (:issue:`7140`). |
| 565 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 566 | .. ====================================================================== |
| 567 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 568 | .. _new-27-interpreter: |
| 569 | |
| 570 | Interpreter Changes |
| 571 | ------------------------------- |
| 572 | |
| 573 | A new environment variable, :envvar:`PYTHONWARNINGS`, |
| 574 | allows controlling warnings. It should be set to a string |
| 575 | containing warning settings, equivalent to those |
| 576 | used with the :option:`-W` switch, separated by commas. |
| 577 | (Contributed by Brian Curtin; :issue:`7301`.) |
| 578 | |
| 579 | For example, the following setting will print warnings every time |
| 580 | they occur, but turn warnings from the :mod:`Cookie` module into an |
| 581 | error. (The exact syntax for setting an environment variable varies |
| 582 | across operating systems and shells, so it may be different for you.) |
| 583 | |
| 584 | :: |
| 585 | |
| 586 | export PYTHONWARNINGS=all,error:::Cookie:0 |
| 587 | |
| 588 | |
| 589 | .. ====================================================================== |
| 590 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 591 | |
| 592 | Optimizations |
| 593 | ------------- |
| 594 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 595 | Several performance enhancements have been added: |
| 596 | |
| 597 | .. * A new :program:`configure` option, :option:`--with-computed-gotos`, |
| 598 | compiles the main bytecode interpreter loop using a new dispatch |
| 599 | mechanism that gives speedups of up to 20%, depending on the system |
| 600 | and benchmark. The new mechanism is only supported on certain |
| 601 | compilers, such as gcc, SunPro, and icc. |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 602 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 603 | * A new opcode was added to perform the initial setup for |
| 604 | :keyword:`with` statements, looking up the :meth:`__enter__` and |
| 605 | :meth:`__exit__` methods. (Contributed by Benjamin Peterson.) |
| 606 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 607 | * The garbage collector now performs better for one common usage |
| 608 | pattern: when many objects are being allocated without deallocating |
| 609 | any of them. This would previously take quadratic |
| 610 | time for garbage collection, but now the number of full garbage collections |
| 611 | is reduced as the number of objects on the heap grows. |
| 612 | The new logic is to only perform a full garbage collection pass when |
| 613 | the middle generation has been collected 10 times and when the |
| 614 | number of survivor objects from the middle generation exceeds 10% of |
| 615 | the number of objects in the oldest generation. (Suggested by Martin |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 616 | von Löwis and implemented by Antoine Pitrou; :issue:`4074`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 617 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 618 | * The garbage collector tries to avoid tracking simple containers |
| 619 | which can't be part of a cycle. In Python 2.7, this is now true for |
| 620 | tuples and dicts containing atomic types (such as ints, strings, |
| 621 | etc.). Transitively, a dict containing tuples of atomic types won't |
| 622 | be tracked either. This helps reduce the cost of each |
| 623 | garbage collection by decreasing the number of objects to be |
| 624 | considered and traversed by the collector. |
Antoine Pitrou | c18f6b0 | 2009-03-28 19:10:13 +0000 | [diff] [blame] | 625 | (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 626 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 627 | * Long integers are now stored internally either in base 2**15 or in base |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 628 | 2**30, the base being determined at build time. Previously, they |
| 629 | were always stored in base 2**15. Using base 2**30 gives |
| 630 | significant performance improvements on 64-bit machines, but |
| 631 | benchmark results on 32-bit machines have been mixed. Therefore, |
| 632 | the default is to use base 2**30 on 64-bit machines and base 2**15 |
| 633 | on 32-bit machines; on Unix, there's a new configure option |
| 634 | :option:`--enable-big-digits` that can be used to override this default. |
| 635 | |
| 636 | Apart from the performance improvements this change should be |
| 637 | invisible to end users, with one exception: for testing and |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 638 | debugging purposes there's a new structseq :data:`sys.long_info` that |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 639 | provides information about the internal format, giving the number of |
| 640 | bits per digit and the size in bytes of the C type used to store |
| 641 | each digit:: |
| 642 | |
| 643 | >>> import sys |
| 644 | >>> sys.long_info |
| 645 | sys.long_info(bits_per_digit=30, sizeof_digit=4) |
| 646 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 647 | (Contributed by Mark Dickinson; :issue:`4258`.) |
| 648 | |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 649 | Another set of changes made long objects a few bytes smaller: 2 bytes |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 650 | smaller on 32-bit systems and 6 bytes on 64-bit. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 651 | (Contributed by Mark Dickinson; :issue:`5260`.) |
| 652 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 653 | * The division algorithm for long integers has been made faster |
| 654 | by tightening the inner loop, doing shifts instead of multiplications, |
| 655 | and fixing an unnecessary extra iteration. |
| 656 | Various benchmarks show speedups of between 50% and 150% for long |
| 657 | integer divisions and modulo operations. |
| 658 | (Contributed by Mark Dickinson; :issue:`5512`.) |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 659 | Bitwise operations are also significantly faster (initial patch by |
| 660 | Gregory Smith; :issue:`1087418`). |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 661 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 662 | * The implementation of ``%`` checks for the left-side operand being |
| 663 | a Python string and special-cases it; this results in a 1-3% |
| 664 | performance increase for applications that frequently use ``%`` |
| 665 | with strings, such as templating libraries. |
| 666 | (Implemented by Collin Winter; :issue:`5176`.) |
| 667 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 668 | * List comprehensions with an ``if`` condition are compiled into |
| 669 | faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7 |
| 670 | by Jeffrey Yasskin; :issue:`4715`.) |
| 671 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 672 | * Converting an integer or long integer to a decimal string was made |
| 673 | faster by special-casing base 10 instead of using a generalized |
| 674 | conversion function that supports arbitrary bases. |
| 675 | (Patch by Gawain Bolton; :issue:`6713`.) |
| 676 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 677 | * The :meth:`split`, :meth:`replace`, :meth:`rindex`, |
| 678 | :meth:`rpartition`, and :meth:`rsplit` methods of string-like types |
| 679 | (strings, Unicode strings, and :class:`bytearray` objects) now use a |
| 680 | fast reverse-search algorithm instead of a character-by-character |
| 681 | scan. This is sometimes faster by a factor of 10. (Added by |
| 682 | Florent Xicluna; :issue:`7462` and :issue:`7622`.) |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 683 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 684 | * The :mod:`pickle` and :mod:`cPickle` modules now automatically |
| 685 | intern the strings used for attribute names, reducing memory usage |
| 686 | of the objects resulting from unpickling. (Contributed by Jake |
| 687 | McGuire; :issue:`5084`.) |
| 688 | |
| 689 | * The :mod:`cPickle` module now special-cases dictionaries, |
| 690 | nearly halving the time required to pickle them. |
| 691 | (Contributed by Collin Winter; :issue:`5670`.) |
| 692 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 693 | .. ====================================================================== |
| 694 | |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 695 | New and Improved Modules |
| 696 | ======================== |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 697 | |
| 698 | As in every release, Python's standard library received a number of |
| 699 | enhancements and bug fixes. Here's a partial list of the most notable |
| 700 | changes, sorted alphabetically by module name. Consult the |
| 701 | :file:`Misc/NEWS` file in the source tree for a more complete list of |
| 702 | changes, or look through the Subversion logs for all the details. |
| 703 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 704 | * The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 705 | gained a feature for skipping modules. The constructor |
| 706 | now takes an iterable containing glob-style patterns such as |
| 707 | ``django.*``; the debugger will not step into stack frames |
| 708 | from a module that matches one of these patterns. |
| 709 | (Contributed by Maru Newby after a suggestion by |
| 710 | Senthil Kumaran; :issue:`5142`.) |
| 711 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 712 | * The :mod:`binascii` module now supports the buffer API, so it can be |
| 713 | used with :class:`memoryview` instances and other similar buffer objects. |
| 714 | (Backported from 3.x by Florent Xicluna; :issue:`7703`.) |
| 715 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 716 | * Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9 |
| 717 | to version 4.8.4 of |
| 718 | `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__. |
| 719 | The new version features better Python 3.x compatibility, various bug fixes, |
| 720 | and adds several new BerkeleyDB flags and methods. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 721 | (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 722 | changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.) |
| 723 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 724 | * The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 725 | management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``. |
| 726 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
| 727 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 728 | * New class: the :class:`~collections.Counter` class in the :mod:`collections` |
| 729 | module is useful for tallying data. :class:`~collections.Counter` instances |
| 730 | behave mostly like dictionaries but return zero for missing keys instead of |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 731 | raising a :exc:`KeyError`: |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 732 | |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 733 | .. doctest:: |
| 734 | :options: +NORMALIZE_WHITESPACE |
| 735 | |
| 736 | >>> from collections import Counter |
| 737 | >>> c = Counter() |
| 738 | >>> for letter in 'here is a sample of english text': |
| 739 | ... c[letter] += 1 |
| 740 | ... |
| 741 | >>> c |
| 742 | Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2, |
| 743 | 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1, |
| 744 | 'p': 1, 'r': 1, 'x': 1}) |
| 745 | >>> c['e'] |
| 746 | 5 |
| 747 | >>> c['z'] |
| 748 | 0 |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 749 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 750 | There are three additional :class:`~collections.Counter` methods: |
| 751 | :meth:`~collections.Counter.most_common` returns the N most common |
| 752 | elements and their counts. :meth:`~collections.Counter.elements` |
| 753 | returns an iterator over the contained elements, repeating each |
| 754 | element as many times as its count. |
| 755 | :meth:`~collections.Counter.subtract` takes an iterable and |
| 756 | subtracts one for each element instead of adding; if the argument is |
| 757 | a dictionary or another :class:`Counter`, the counts are |
| 758 | subtracted. :: |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 759 | |
| 760 | >>> c.most_common(5) |
| 761 | [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)] |
| 762 | >>> c.elements() -> |
| 763 | 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ', |
| 764 | 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i', |
| 765 | 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's', |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 766 | 's', 's', 'r', 't', 't', 'x' |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 767 | >>> c['e'] |
| 768 | 5 |
| 769 | >>> c.subtract('very heavy on the letter e') |
| 770 | >>> c['e'] # Count is now lower |
| 771 | -1 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 772 | |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 773 | Contributed by Raymond Hettinger; :issue:`1696199`. |
| 774 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 775 | .. revision 79660 |
| 776 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 777 | The new :class:`~collections.OrderedDict` class is described in the earlier |
| 778 | section :ref:`pep-0372`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 779 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 780 | The :class:`~collections.namedtuple` class now has an optional *rename* parameter. |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 781 | If *rename* is true, field names that are invalid because they've |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 782 | been repeated or that aren't legal Python identifiers will be |
| 783 | renamed to legal names that are derived from the field's |
| 784 | position within the list of fields: |
| 785 | |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 786 | >>> from collections import namedtuple |
| 787 | >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True) |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 788 | >>> T._fields |
| 789 | ('field1', '_1', '_2', 'field2') |
| 790 | |
| 791 | (Added by Raymond Hettinger; :issue:`1818`.) |
| 792 | |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 793 | The :class:`~collections.deque` data type now has a |
| 794 | :meth:`~collections.deque.count` method that returns the number of |
| 795 | contained elements equal to the supplied argument *x*, and a |
| 796 | :meth:`~collections.deque.reverse` method that reverses the elements |
| 797 | of the deque in-place. :class:`deque` also exposes its maximum |
| 798 | length as the read-only :attr:`~collections.deque.maxlen` attribute. |
| 799 | (Both features added by Raymond Hettinger.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 800 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 801 | * The :mod:`copy` module's :func:`~copy.deepcopy` function will now |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 802 | correctly copy bound instance methods. (Implemented by |
| 803 | Robert Collins; :issue:`1515`.) |
| 804 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 805 | * The :mod:`ctypes` module now always converts ``None`` to a C NULL |
| 806 | pointer for arguments declared as pointers. (Changed by Thomas |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 807 | Heller; :issue:`4606`.) The underlying `libffi library |
| 808 | <http://sourceware.org/libffi/>`__ has been updated to version |
| 809 | 3.0.9, containing various fixes for different platforms. (Updated |
| 810 | by Matthias Klose; :issue:`8142`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 811 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 812 | * New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class |
| 813 | gained a :meth:`~datetime.timedelta.total_seconds` method that returns the |
| 814 | number of seconds in the duration. (Contributed by Brian Quinlan; :issue:`5788`.) |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 815 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 816 | * New method: the :class:`~decimal.Decimal` class gained a |
| 817 | :meth:`~decimal.Decimal.from_float` class method that performs an exact |
| 818 | conversion of a floating-point number to a :class:`~decimal.Decimal`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 819 | Note that this is an **exact** conversion that strives for the |
| 820 | closest decimal approximation to the floating-point representation's value; |
| 821 | the resulting decimal value will therefore still include the inaccuracy, |
| 822 | if any. |
| 823 | For example, ``Decimal.from_float(0.1)`` returns |
| 824 | ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``. |
| 825 | (Implemented by Raymond Hettinger; :issue:`4796`.) |
| 826 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 827 | Most of the methods of the :class:`~decimal.Context` class now accept integers |
| 828 | as well as :class:`~decimal.Decimal` instances; the only exceptions are the |
| 829 | :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical` |
| 830 | methods. (Patch by Juan José Conti; :issue:`7633`.) |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 831 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 832 | The constructor for :class:`~decimal.Decimal` now accepts |
| 833 | floating-point numbers (added by Raymond Hettinger; :issue:`8257`) |
| 834 | and non-European Unicode characters such as Arabic-Indic digits |
| 835 | (contributed by Mark Dickinson; :issue:`6595`). |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 836 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 837 | When using :class:`~decimal.Decimal` instances with a string's |
| 838 | :meth:`~str.format` method, the default alignment was previously |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 839 | left-alignment. This has been changed to right-alignment, which seems |
| 840 | more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.) |
| 841 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 842 | * The :class:`~fractions.Fraction` class now accepts a single float or |
| 843 | :class:`~decimal.Decimal` instance, or two rational numbers, as |
| 844 | arguments to its constructor. (Implemented by Mark Dickinson; |
| 845 | rationals added in :issue:`5812`, and float/decimal in |
| 846 | :issue:`8294`.) |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 847 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 848 | An oversight was fixed, making the :class:`Fraction` match the other |
| 849 | numeric types; ordering comparisons (``<``, ``<=``, ``>``, ``>=``) between |
| 850 | fractions and complex numbers now raise a :exc:`TypeError`. |
| 851 | |
| 852 | .. revision 79455 |
| 853 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 854 | * New class: a new :class:`~ftplib.FTP_TLS` class in |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 855 | the :mod:`ftplib` module provides secure FTP |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 856 | connections using TLS encapsulation of authentication as well as |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 857 | subsequent control and data transfers. |
| 858 | (Contributed by Giampaolo Rodola', :issue:`2054`.) |
| 859 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 860 | The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 861 | uploads thanks to an added *rest* parameter (patch by Pablo Mouzo; |
| 862 | :issue:`6845`.) |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 863 | |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 864 | * New class decorator: :func:`total_ordering` in the :mod:`functools` |
| 865 | module takes a class that defines an :meth:`__eq__` method and one of |
| 866 | :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`, |
| 867 | and generates the missing comparison methods. Since the |
| 868 | :meth:`__cmp__` method is being deprecated in Python 3.x, |
| 869 | this decorator makes it easier to define ordered classes. |
| 870 | (Added by Raymond Hettinger; :issue:`5479`.) |
| 871 | |
| 872 | New function: :func:`cmp_to_key` will take an old-style comparison |
| 873 | function that expects two arguments and return a new callable that |
| 874 | can be used as the *key* parameter to functions such as |
| 875 | :func:`sorted`, :func:`min` and :func:`max`, etc. The primary |
| 876 | intended use is to help with making code compatible with Python 3.x. |
| 877 | (Added by Raymond Hettinger.) |
| 878 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 879 | * New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 880 | true if a given instance is tracked by the garbage collector, false |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 881 | otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 882 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 883 | * The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 884 | management protocol, so you can write ``with gzip.GzipFile(...) as f: ...`` |
| 885 | (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements |
| 886 | the :class:`io.BufferedIOBase` ABC, so you can wrap it with |
| 887 | :class:`io.BufferedReader` for faster processing |
| 888 | (contributed by Nir Aides; :issue:`7471`). |
| 889 | It's also now possible to override the modification time |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 890 | recorded in a gzipped file by providing an optional timestamp to |
| 891 | the constructor. (Contributed by Jacques Frechet; :issue:`4272`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 892 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 893 | Files in gzip format can be padded with trailing zero bytes; the |
| 894 | :mod:`gzip` module will now consume these trailing bytes. (Fixed by |
| 895 | Tadek Pietraszek and Brian Curtin; :issue:`2846`.) |
| 896 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 897 | * New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms` |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 898 | attribute containing a tuple naming the supported algorithms. |
| 899 | In Python 2.7, ``hashlib.algorithms`` contains |
| 900 | ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')`` |
| 901 | (Contributed by Carl Chenet; :issue:`7418`.) |
| 902 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 903 | * The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 904 | supports buffering, resulting in much faster reading of HTTP responses. |
| 905 | (Contributed by Kristjan Valur Jonsson; :issue:`4879`.) |
| 906 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 907 | The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 908 | now support a *source_address* parameter, a ``(host, port)`` 2-tuple |
| 909 | giving the source address that will be used for the connection. |
| 910 | (Contributed by Eldon Ziegler; :issue:`3972`.) |
| 911 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 912 | * The :mod:`imaplib` module now supports IPv6 addresses. |
| 913 | (Contributed by Derek Morr; :issue:`1655`.) |
| 914 | |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 915 | * New function: the :mod:`inspect` module's :func:`~inspect.getcallargs` |
| 916 | takes a callable and its positional and keyword arguments, |
| 917 | and figures out which of the callable's parameters will receive each argument, |
| 918 | returning a dictionary mapping argument names to their values. For example:: |
| 919 | |
| 920 | >>> from inspect import getcallargs |
| 921 | >>> def f(a, b=1, *pos, **named): |
| 922 | ... pass |
| 923 | >>> getcallargs(f, 1, 2, 3) |
| 924 | {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} |
| 925 | >>> getcallargs(f, a=2, x=4) |
| 926 | {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} |
| 927 | >>> getcallargs(f) |
| 928 | Traceback (most recent call last): |
| 929 | ... |
| 930 | TypeError: f() takes at least 1 argument (0 given) |
| 931 | |
| 932 | Contributed by George Sakkis; :issue:`3135`. |
| 933 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 934 | * Updated module: The :mod:`io` library has been upgraded to the version shipped with |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 935 | Python 3.1. For 3.1, the I/O library was entirely rewritten in C |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 936 | and is 2 to 20 times faster depending on the task being performed. The |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 937 | original Python version was renamed to the :mod:`_pyio` module. |
| 938 | |
| 939 | One minor resulting change: the :class:`io.TextIOBase` class now |
| 940 | has an :attr:`errors` attribute giving the error setting |
| 941 | used for encoding and decoding errors (one of ``'strict'``, ``'replace'``, |
| 942 | ``'ignore'``). |
| 943 | |
| 944 | The :class:`io.FileIO` class now raises an :exc:`OSError` when passed |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 945 | an invalid file descriptor. (Implemented by Benjamin Peterson; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 946 | :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 947 | file position; previously it would change the file position to the |
| 948 | end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 949 | |
Andrew M. Kuchling | 5a73ff8 | 2009-12-02 14:27:11 +0000 | [diff] [blame] | 950 | * New function: ``itertools.compress(data, selectors)`` takes two |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 951 | iterators. Elements of *data* are returned if the corresponding |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 952 | value in *selectors* is true:: |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 953 | |
| 954 | itertools.compress('ABCDEF', [1,0,1,0,1,1]) => |
| 955 | A, C, E, F |
| 956 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 957 | .. maybe here is better to use >>> list(itertools.compress(...)) instead |
| 958 | |
Andrew M. Kuchling | 5a73ff8 | 2009-12-02 14:27:11 +0000 | [diff] [blame] | 959 | New function: ``itertools.combinations_with_replacement(iter, r)`` |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 960 | returns all the possible *r*-length combinations of elements from the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 961 | iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 962 | can be repeated in the generated combinations:: |
| 963 | |
| 964 | itertools.combinations_with_replacement('abc', 2) => |
| 965 | ('a', 'a'), ('a', 'b'), ('a', 'c'), |
| 966 | ('b', 'b'), ('b', 'c'), ('c', 'c') |
| 967 | |
| 968 | Note that elements are treated as unique depending on their position |
| 969 | in the input, not their actual values. |
| 970 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 971 | The :func:`itertools.count` function now has a *step* argument that |
| 972 | allows incrementing by values other than 1. :func:`~itertools.count` also |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 973 | now allows keyword arguments, and using non-integer values such as |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 974 | floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 975 | Hettinger; :issue:`5032`.) |
| 976 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 977 | :func:`itertools.combinations` and :func:`itertools.product` were |
| 978 | previously raising :exc:`ValueError` for values of *r* larger than |
| 979 | the input iterable. This was deemed a specification error, so they |
| 980 | now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.) |
| 981 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 982 | * Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 983 | simplejson package, which includes a C extension that makes |
| 984 | encoding and decoding faster. |
| 985 | (Contributed by Bob Ippolito; :issue:`4136`.) |
| 986 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 987 | To support the new :class:`collections.OrderedDict` type, :func:`json.load` |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 988 | now has an optional *object_pairs_hook* parameter that will be called |
| 989 | with any object literal that decodes to a list of pairs. |
| 990 | (Contributed by Raymond Hettinger; :issue:`5381`.) |
| 991 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 992 | * New functions: the :mod:`math` module gained |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 993 | :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function, |
| 994 | :func:`~math.expm1` which computes ``e**x - 1`` with more precision than |
| 995 | using :func:`~math.exp` and subtracting 1, |
| 996 | :func:`~math.gamma` for the Gamma function, and |
| 997 | :func:`~math.lgamma` for the natural log of the Gamma function. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 998 | (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.) |
| 999 | |
Andrew M. Kuchling | 24520b4 | 2009-04-09 11:22:47 +0000 | [diff] [blame] | 1000 | * The :mod:`multiprocessing` module's :class:`Manager*` classes |
| 1001 | can now be passed a callable that will be called whenever |
| 1002 | a subprocess is started, along with a set of arguments that will be |
| 1003 | passed to the callable. |
| 1004 | (Contributed by lekma; :issue:`5585`.) |
| 1005 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1006 | The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes, |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1007 | now has an optional *maxtasksperchild* parameter. Worker processes |
| 1008 | will perform the specified number of tasks and then exit, causing the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1009 | :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1010 | memory or other resources, or if some tasks will cause the worker to |
| 1011 | become very large. |
| 1012 | (Contributed by Charles Cazabon; :issue:`6963`.) |
| 1013 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1014 | * The :mod:`nntplib` module now supports IPv6 addresses. |
| 1015 | (Contributed by Derek Morr; :issue:`1664`.) |
| 1016 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1017 | * New functions: the :mod:`os` module wraps the following POSIX system |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1018 | calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1019 | real, effective, and saved GIDs and UIDs; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1020 | :func:`~os.setresgid` and :func:`~os.setresuid`, which set |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1021 | real, effective, and saved GIDs and UIDs to new values; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1022 | :func:`~os.initgroups`. (GID/UID functions |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1023 | contributed by Travis H.; :issue:`6508`. Support for initgroups added |
| 1024 | by Jean-Paul Calderone; :issue:`7333`.) |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1025 | |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1026 | The :func:`os.fork` function now re-initializes the import lock in |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1027 | the child process; this fixes problems on Solaris when :func:`~os.fork` |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1028 | is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.) |
| 1029 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1030 | * In the :mod:`os.path` module, the :func:`~os.path.normpath` and |
| 1031 | :func:`~os.path.abspath` functions now preserve Unicode; if their input path |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1032 | is a Unicode string, the return value is also a Unicode string. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1033 | (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`; |
| 1034 | :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.) |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1035 | |
Andrew M. Kuchling | 9cb4277 | 2009-01-21 02:15:43 +0000 | [diff] [blame] | 1036 | * The :mod:`pydoc` module now has help for the various symbols that Python |
| 1037 | uses. You can now do ``help('<<')`` or ``help('@')``, for example. |
| 1038 | (Contributed by David Laban; :issue:`4739`.) |
| 1039 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1040 | * The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn` |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1041 | now accept an optional *flags* argument, for consistency with the |
| 1042 | other functions in the module. (Added by Gregory P. Smith.) |
| 1043 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1044 | * New function: in the :mod:`shutil` module, :func:`~shutil.make_archive` |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1045 | takes a filename, archive type (zip or tar-format), and a directory |
| 1046 | path, and creates an archive containing the directory's contents. |
| 1047 | (Added by Tarek Ziadé.) |
| 1048 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1049 | :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree` |
| 1050 | functions now raise a :exc:`~shutil.SpecialFileError` exception when |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1051 | asked to copy a named pipe. Previously the code would treat |
| 1052 | named pipes like a regular file by opening them for reading, and |
| 1053 | this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.) |
| 1054 | |
| 1055 | * New functions: in the :mod:`site` module, three new functions |
| 1056 | return various site- and user-specific paths. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1057 | :func:`~site.getsitepackages` returns a list containing all |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1058 | global site-packages directories, and |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1059 | :func:`~site.getusersitepackages` returns the path of the user's |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1060 | site-packages directory. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1061 | :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1062 | environment variable, giving the path to a directory that can be used |
| 1063 | to store data. |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1064 | (Contributed by Tarek Ziadé; :issue:`6693`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1065 | |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1066 | The :mod:`site` module now reports exceptions occurring |
| 1067 | when the :mod:`sitecustomize` module is imported, and will no longer |
Florent Xicluna | ad59833 | 2010-03-31 21:40:32 +0000 | [diff] [blame] | 1068 | catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1069 | Victor Stinner; :issue:`3137`.) |
| 1070 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1071 | * The :mod:`socket` module's :class:`~ssl.SSL` objects now support the |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1072 | buffer API, which fixed a test suite failure (fix by Antoine Pitrou; |
| 1073 | :issue:`7133`). :class:`SSL` objects also now automatically set |
| 1074 | OpenSSL's :cmacro:`SSL_MODE_AUTO_RETRY`, which will prevent an error |
| 1075 | code being returned from :meth:`recv` operations that trigger an SSL |
| 1076 | renegotiation (fix by Antoine Pitrou; :issue:`8222`). |
| 1077 | |
| 1078 | The version of OpenSSL being used is now available as the module |
| 1079 | attributes :attr:`OPENSSL_VERSION` (a string), |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 1080 | :attr:`OPENSSL_VERSION_INFO` (a 5-tuple), and |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1081 | :attr:`OPENSSL_VERSION_NUMBER` (an integer). (Added by Antoine |
| 1082 | Pitrou; :issue:`8321`.) |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1083 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1084 | The :func:`~socket.create_connection` function |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 1085 | gained a *source_address* parameter, a ``(host, port)`` 2-tuple |
| 1086 | giving the source address that will be used for the connection. |
| 1087 | (Contributed by Eldon Ziegler; :issue:`3972`.) |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1088 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1089 | The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into` |
| 1090 | methods will now write into objects that support the buffer API, most usefully |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1091 | the :class:`bytearray` and :class:`memoryview` objects. (Implemented by |
| 1092 | Antoine Pitrou; :issue:`8104`.) |
| 1093 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1094 | * The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now |
| 1095 | has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1096 | The default value is False; if overridden to be True, |
| 1097 | new request connections will have the TCP_NODELAY option set to |
| 1098 | prevent buffering many small sends into a single TCP packet. |
| 1099 | (Contributed by Kristjan Valur Jonsson; :issue:`6192`.) |
| 1100 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1101 | * Updated module: the :mod:`sqlite3` module has been updated to |
Andrew M. Kuchling | fed1576 | 2010-03-08 12:00:39 +0000 | [diff] [blame] | 1102 | version 2.6.0 of the `pysqlite package <http://code.google.com/p/pysqlite/>`__. Version 2.6.0 includes a number of bugfixes, and adds |
| 1103 | the ability to load SQLite extensions from shared libraries. |
| 1104 | Call the ``enable_load_extension(True)`` method to enable extensions, |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1105 | and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library. |
Andrew M. Kuchling | fed1576 | 2010-03-08 12:00:39 +0000 | [diff] [blame] | 1106 | (Updated by Gerhard Häring.) |
| 1107 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1108 | * The :mod:`struct` module will no longer silently ignore overflow |
| 1109 | errors when a value is too large for a particular integer format |
| 1110 | code (one of ``bBhHiIlLqQ``); it now always raises a |
| 1111 | :exc:`struct.error` exception. (Changed by Mark Dickinson; |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1112 | :issue:`1523`.) The :func:`~struct.pack` function will also |
| 1113 | attempt to use :meth:`__index__` to convert and pack non-integers |
| 1114 | before trying the :meth:`__int__` method or reporting an error. |
| 1115 | (Changed by Mark Dickinson; :issue:`8300`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1116 | |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1117 | * New function: the :mod:`subprocess` module's |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1118 | :func:`~subprocess.check_output` runs a command with a specified set of arguments |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1119 | and returns the command's output as a string when the command runs without |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1120 | error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise. |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 1121 | |
| 1122 | :: |
| 1123 | |
| 1124 | >>> subprocess.check_output(['df', '-h', '.']) |
| 1125 | 'Filesystem Size Used Avail Capacity Mounted on\n |
| 1126 | /dev/disk0s2 52G 49G 3.0G 94% /\n' |
| 1127 | |
| 1128 | >>> subprocess.check_output(['df', '-h', '/bogus']) |
| 1129 | ... |
| 1130 | subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1 |
| 1131 | |
| 1132 | (Contributed by Gregory P. Smith.) |
| 1133 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1134 | The :mod:`subprocess` module will now retry its internal system calls |
| 1135 | on receiving an :const:`EINTR` signal. (Reported by several people; final |
| 1136 | patch by Gregory P. Smith in :issue:`1068268`.) |
| 1137 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1138 | * New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1139 | returns true for variables that are explicitly declared to be global, |
| 1140 | false for ones that are implicitly global. |
| 1141 | (Contributed by Jeremy Hylton.) |
| 1142 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1143 | * The ``sys.version_info`` value is now a named tuple, with attributes |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1144 | named :attr:`major`, :attr:`minor`, :attr:`micro`, |
| 1145 | :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross |
| 1146 | Light; :issue:`4285`.) |
| 1147 | |
| 1148 | :func:`sys.getwindowsversion` also returns a named tuple, |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 1149 | with attributes named :attr:`major`, :attr:`minor`, :attr:`build`, |
Ezio Melotti | 1247775 | 2010-02-08 22:22:41 +0000 | [diff] [blame] | 1150 | :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`, |
Eric Smith | b3c5488 | 2010-02-03 14:17:50 +0000 | [diff] [blame] | 1151 | :attr:`service_pack_minor`, :attr:`suite_mask`, and |
| 1152 | :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.) |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1153 | |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1154 | * The :mod:`tarfile` module's default error handling has changed, to |
| 1155 | no longer suppress fatal errors. The default error level was previously 0, |
| 1156 | which meant that errors would only result in a message being written to the |
| 1157 | debug log, but because the debug log is not activated by default, |
| 1158 | these errors go unnoticed. The default error level is now 1, |
| 1159 | which raises an exception if there's an error. |
| 1160 | (Changed by Lars Gustäbel; :issue:`7357`.) |
| 1161 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1162 | :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo` |
| 1163 | objects being added to a tar file. When you call :meth:`~tarfile.TarFile.add`, |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1164 | instance, you may supply an optional *filter* argument |
| 1165 | that's a callable. The *filter* callable will be passed the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1166 | :class:`~tarfile.TarInfo` for every file being added, and can modify and return it. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1167 | If the callable returns ``None``, the file will be excluded from the |
| 1168 | resulting archive. This is more powerful than the existing |
| 1169 | *exclude* argument, which has therefore been deprecated. |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1170 | (Added by Lars Gustäbel; :issue:`6856`.) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1171 | The :class:`~tarfile.TarFile` class also now supports the context manager protocol. |
Andrew M. Kuchling | fed1576 | 2010-03-08 12:00:39 +0000 | [diff] [blame] | 1172 | (Added by Lars Gustäbel; :issue:`7232`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1173 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1174 | * The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class |
| 1175 | now returns the internal flag on exit. This means the method will usually |
| 1176 | return true because :meth:`~threading.Event.wait` is supposed to block until the |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1177 | internal flag becomes true. The return value will only be false if |
| 1178 | a timeout was provided and the operation timed out. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1179 | (Contributed by Tim Lesher; :issue:`1674032`.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1180 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1181 | * The Unicode database provided by the :mod:`unicodedata` module is |
| 1182 | now used internally to determine which characters are numeric, |
| 1183 | whitespace, or represent line breaks. The database also |
| 1184 | includes information from the :file:`Unihan.txt` data file (patch |
| 1185 | by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`) |
| 1186 | and has been updated to version 5.2.0 (updated by |
| 1187 | Florent Xicluna; :issue:`8024`). |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1188 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1189 | * The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1190 | Benjamin Peterson.) |
| 1191 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1192 | * The ElementTree library, :mod:`xml.etree`, no longer escapes |
| 1193 | ampersands and angle brackets when outputting an XML processing |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1194 | instruction (which looks like ``<?xml-stylesheet href="#style1"?>``) |
| 1195 | or comment (which looks like ``<!-- comment -->``). |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1196 | (Patch by Neil Muller; :issue:`2746`.) |
| 1197 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1198 | * The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1199 | management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``. |
| 1200 | (Contributed by Brian Curtin; :issue:`5511`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1201 | |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1202 | :mod:`zipfile` now supports archiving empty directories and |
| 1203 | extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.) |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1204 | Reading files out of an archive is now faster, and interleaving |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1205 | :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly. |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1206 | (Contributed by Nir Aides; :issue:`7610`.) |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1207 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1208 | The :func:`~zipfile.is_zipfile` function now |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1209 | accepts a file object, in addition to the path names accepted in earlier |
| 1210 | versions. (Contributed by Gabriel Genellina; :issue:`4756`.) |
| 1211 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1212 | The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1213 | that lets you override the default compression method specified in the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1214 | :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren; |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1215 | :issue:`6003`.) |
| 1216 | |
| 1217 | |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1218 | New module: sysconfig |
| 1219 | --------------------------------- |
| 1220 | |
| 1221 | XXX A new :mod:`sysconfig` module has been extracted from |
| 1222 | :mod:`distutils` and put in the standard library. |
| 1223 | |
| 1224 | The :mod:`sysconfig` module provides access to Python's configuration |
| 1225 | information like the list of installation paths and the configuration |
Tarek Ziadé | 1e069ee | 2010-02-23 05:20:22 +0000 | [diff] [blame] | 1226 | variables relevant for the current platform. (contributed by Tarek) |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1227 | |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1228 | Updated module: ElementTree 1.3 |
| 1229 | --------------------------------- |
| 1230 | |
| 1231 | XXX write this. |
| 1232 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1233 | .. ====================================================================== |
| 1234 | .. whole new modules get described in subsections here |
| 1235 | |
Tarek Ziadé | 2b21069 | 2010-02-02 23:39:40 +0000 | [diff] [blame] | 1236 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1237 | Unit Testing Enhancements |
| 1238 | --------------------------------- |
| 1239 | |
| 1240 | The :mod:`unittest` module was enhanced in several ways. |
| 1241 | The progress messages now shows 'x' for expected failures |
| 1242 | and 'u' for unexpected successes when run in verbose mode. |
| 1243 | (Contributed by Benjamin Peterson.) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1244 | Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a test. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1245 | (:issue:`1034053`.) |
| 1246 | |
| 1247 | .. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.) |
| 1248 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1249 | The error messages for :meth:`~unittest.TestCase.assertEqual`, |
| 1250 | :meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1251 | failures now provide more information. If you set the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1252 | :attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to |
| 1253 | True, both the standard error message and any additional message you |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1254 | provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.) |
| 1255 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1256 | The :meth:`~unittest.TestCase.assertRaises` method now |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1257 | return a context handler when called without providing a callable |
| 1258 | object to run. For example, you can write this:: |
| 1259 | |
| 1260 | with self.assertRaises(KeyError): |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1261 | {}['foo'] |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1262 | |
| 1263 | (Implemented by Antoine Pitrou; :issue:`4444`.) |
| 1264 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1265 | .. rev 78774 |
| 1266 | |
| 1267 | Module- and class-level setup and teardown fixtures are now supported. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1268 | Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule` |
| 1269 | functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and |
| 1270 | :meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods |
| 1271 | (using ``@classmethod`` or equivalent). These functions and |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1272 | methods are invoked when the test runner switches to a test case in a |
| 1273 | different module or class. |
| 1274 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1275 | The methods :meth:`~unittest.TestCase.addCleanup` and |
| 1276 | :meth:`~unittest.TestCase.doCleanups` were added. |
| 1277 | :meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that |
| 1278 | will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if |
| 1279 | :meth:`~unittest.TestCase.setUp` fails, otherwise after :meth:`~unittest.TestCase.tearDown`). This allows |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1280 | for much simpler resource allocation and deallocation during tests |
| 1281 | (:issue:`5679`). |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1282 | |
| 1283 | A number of new methods were added that provide more specialized |
| 1284 | tests. Many of these methods were written by Google engineers |
| 1285 | for use in their test suites; Gregory P. Smith, Michael Foord, and |
| 1286 | GvR worked on merging them into Python's version of :mod:`unittest`. |
| 1287 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1288 | * :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1289 | expression and verify that the result is or is not ``None``. |
| 1290 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1291 | * :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot` |
| 1292 | take two values and check whether the two values evaluate to the same object or not. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1293 | (Added by Michael Foord; :issue:`2578`.) |
| 1294 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1295 | * :meth:`~unittest.TestCase.assertIsInstance` and |
| 1296 | :meth:`~unittest.TestCase.assertNotIsInstance` check whether |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1297 | the resulting object is an instance of a particular class, or of |
| 1298 | one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.) |
| 1299 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1300 | * :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`, |
| 1301 | :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1302 | two quantities. |
| 1303 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1304 | * :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1305 | not equal, displays a helpful comparison that highlights the |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1306 | differences in the two strings. This comparison is now used by |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1307 | default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1308 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1309 | * :meth:`~unittest.TestCase.assertRegexpMatches` checks whether its first argument is a |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1310 | string matching a regular expression provided as its second argument. |
| 1311 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1312 | .. XXX add assertNotRegexpMatches see issue 8038 |
| 1313 | |
| 1314 | * :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1315 | is raised, and then also checks that the string representation of |
| 1316 | the exception matches the provided regular expression. |
| 1317 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1318 | * :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn` |
| 1319 | tests whether *first* is or is not in *second*. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1320 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1321 | * :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1322 | contain the same elements. |
| 1323 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1324 | * :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1325 | only reports the differences between the sets in case of error. |
| 1326 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1327 | * Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual` |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1328 | compare the specified types and explain any differences without necessarily |
| 1329 | printing their full values; these methods are now used by default |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1330 | when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`. |
| 1331 | More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1332 | and can optionally check whether both sequences are of a |
| 1333 | particular type. |
| 1334 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1335 | * :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1336 | differences; it's now used by default when you compare two dictionaries |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1337 | using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1338 | all of the key/value pairs in *first* are found in *second*. |
| 1339 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1340 | * :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1341 | whether *first* and *second* are approximately equal by computing |
| 1342 | their difference, rounding the result to an optionally-specified number |
| 1343 | of *places* (the default is 7), and comparing to zero. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1344 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1345 | * :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the |
| 1346 | :attr:`~unittest.TestLoader.suiteClass` attribute of |
| 1347 | the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1348 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1349 | * A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle |
| 1350 | new data types. The :meth:`~unittest.TestCase.addTypeEqualityFunc` method takes a type |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1351 | object and a function. The function will be used when both of the |
| 1352 | objects being compared are of the specified type. This function |
| 1353 | should compare the two objects and raise an exception if they don't |
| 1354 | match; it's a good idea for the function to provide additional |
| 1355 | information about why the two objects are matching, much as the new |
| 1356 | sequence comparison methods do. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1357 | |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1358 | :func:`unittest.main` now takes an optional ``exit`` argument. If |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1359 | False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1360 | used from the interactive interpreter. (Contributed by J. Pablo |
| 1361 | Fernández; :issue:`3379`.) |
| 1362 | |
| 1363 | A new command-line switch, :option:`-f` or :option:`--failfast`, makes |
| 1364 | test execution stop immediately when a test fails instead of |
| 1365 | continuing to execute further tests. (Suggested by Cliff Dyer and |
| 1366 | implemented by Michael Foord; :issue:`8074`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1367 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1368 | .. XXX document the other new switches |
| 1369 | |
| 1370 | :class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and |
| 1371 | :meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1372 | and after a test run. (Contributed by Robert Collins; :issue:`5728`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1373 | |
| 1374 | With all these changes, the :file:`unittest.py` was becoming awkwardly |
| 1375 | large, so the module was turned into a package and the code split into |
| 1376 | several files (by Benjamin Peterson). This doesn't affect how the |
| 1377 | module is imported. |
| 1378 | |
| 1379 | |
| 1380 | .. _importlib-section: |
| 1381 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1382 | importlib: Importing Modules |
| 1383 | ------------------------------ |
| 1384 | |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1385 | Python 3.1 includes the :mod:`importlib` package, a re-implementation |
| 1386 | of the logic underlying Python's :keyword:`import` statement. |
| 1387 | :mod:`importlib` is useful for implementors of Python interpreters and |
Brett Cannon | ca2dc47 | 2009-12-22 02:37:37 +0000 | [diff] [blame] | 1388 | to users who wish to write new importers that can participate in the |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1389 | import process. Python 2.7 doesn't contain the complete |
| 1390 | :mod:`importlib` package, but instead has a tiny subset that contains |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1391 | a single function, :func:`~importlib.import_module`. |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1392 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1393 | ``import_module(name, package=None)`` imports a module. *name* is |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1394 | a string containing the module or package's name. It's possible to do |
| 1395 | relative imports by providing a string that begins with a ``.`` |
| 1396 | character, such as ``..utils.errors``. For relative imports, the |
| 1397 | *package* argument must be provided and is the name of the package that |
| 1398 | will be used as the anchor for |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1399 | the relative import. :func:`~importlib.import_module` both inserts the imported |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1400 | module into ``sys.modules`` and returns the module object. |
| 1401 | |
| 1402 | Here are some examples:: |
| 1403 | |
| 1404 | >>> from importlib import import_module |
| 1405 | >>> anydbm = import_module('anydbm') # Standard absolute import |
| 1406 | >>> anydbm |
| 1407 | <module 'anydbm' from '/p/python/Lib/anydbm.py'> |
| 1408 | >>> # Relative import |
| 1409 | >>> sysconfig = import_module('..sysconfig', 'distutils.command') |
| 1410 | >>> sysconfig |
| 1411 | <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'> |
| 1412 | |
| 1413 | :mod:`importlib` was implemented by Brett Cannon and introduced in |
| 1414 | Python 3.1. |
| 1415 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1416 | |
Andrew M. Kuchling | a17cd4a | 2009-01-31 02:50:09 +0000 | [diff] [blame] | 1417 | ttk: Themed Widgets for Tk |
| 1418 | -------------------------- |
| 1419 | |
| 1420 | Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk |
| 1421 | widgets but have a more customizable appearance and can therefore more |
| 1422 | closely resemble the native platform's widgets. This widget |
| 1423 | set was originally called Tile, but was renamed to Ttk (for "themed Tk") |
| 1424 | on being added to Tcl/Tck release 8.5. |
| 1425 | |
| 1426 | XXX write a brief discussion and an example here. |
| 1427 | |
| 1428 | The :mod:`ttk` module was written by Guilherme Polo and added in |
| 1429 | :issue:`2983`. An alternate version called ``Tile.py``, written by |
| 1430 | Martin Franklin and maintained by Kevin Walzer, was proposed for |
| 1431 | inclusion in :issue:`2618`, but the authors argued that Guilherme |
| 1432 | Polo's work was more comprehensive. |
| 1433 | |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 1434 | |
| 1435 | Deprecations and Removals |
| 1436 | ========================= |
| 1437 | |
| 1438 | * :func:`contextlib.nested`, which allows handling more than one context manager |
| 1439 | with one :keyword:`with` statement, has been deprecated; :keyword:`with` |
| 1440 | supports multiple context managers syntactically now. |
| 1441 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1442 | .. ====================================================================== |
| 1443 | |
| 1444 | |
| 1445 | Build and C API Changes |
| 1446 | ======================= |
| 1447 | |
| 1448 | Changes to Python's build process and to the C API include: |
| 1449 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1450 | * The latest release of the GNU Debugger, GDB 7, can be `scripted |
| 1451 | using Python |
| 1452 | <http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html>`__. |
| 1453 | When you begin debugging an executable program P, GDB will look for |
| 1454 | a file named ``P-gdb.py`` and automatically read it. Dave Malcolm |
| 1455 | contributed a :file:`python-gdb.py` that adds a number of useful |
| 1456 | commands when debugging Python itself. For example, there are |
| 1457 | ``py-up`` and ``py-down`` that go up or down one Python stack frame, |
| 1458 | which usually corresponds to several C stack frames. ``py-print`` |
| 1459 | prints the value of a Python variable, and ``py-bt`` prints the |
| 1460 | Python stack trace. (Added as a result of :issue:`8032`.) |
| 1461 | |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 1462 | * If you use the :file:`.gdbinit` file provided with Python, |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1463 | the "pyo" macro in the 2.7 version now works correctly when the thread being |
| 1464 | debugged doesn't hold the GIL; the macro now acquires it before printing. |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1465 | (Contributed by Victor Stinner; :issue:`3632`.) |
| 1466 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1467 | * :cfunc:`Py_AddPendingCall` is now thread-safe, letting any |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1468 | worker thread submit notifications to the main Python thread. This |
| 1469 | is particularly useful for asynchronous IO operations. |
| 1470 | (Contributed by Kristjan Valur Jonsson; :issue:`4293`.) |
| 1471 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1472 | * New function: :cfunc:`PyCode_NewEmpty` creates an empty code object; |
| 1473 | only the filename, function name, and first line number are required. |
| 1474 | This is useful to extension modules that are attempting to |
| 1475 | construct a more useful traceback stack. Previously such |
| 1476 | extensions needed to call :cfunc:`PyCode_New`, which had many |
| 1477 | more arguments. (Added by Jeffrey Yasskin.) |
| 1478 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1479 | * New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new |
| 1480 | exception class, just as the existing :cfunc:`PyErr_NewException` does, |
| 1481 | but takes an extra ``char *`` argument containing the docstring for the |
| 1482 | new exception class. (Added by the 'lekma' user on the Python bug tracker; |
| 1483 | :issue:`7033`.) |
| 1484 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1485 | * New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object |
| 1486 | and returns the line number that the frame is currently executing. |
| 1487 | Previously code would need to get the index of the bytecode |
| 1488 | instruction currently executing, and then look up the line number |
| 1489 | corresponding to that address. (Added by Jeffrey Yasskin.) |
| 1490 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1491 | * New functions: :cfunc:`PyLong_AsLongAndOverflow` and |
| 1492 | :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long |
| 1493 | integer as a C :ctype:`long` or :ctype:`long long`. |
| 1494 | If the number is too large to fit into |
| 1495 | the output type, an *overflow* flag is set and returned to the caller. |
| 1496 | (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.) |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1497 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1498 | * New function: stemming from the rewrite of string-to-float conversion, |
| 1499 | a new :cfunc:`PyOS_string_to_double` function was added. The old |
| 1500 | :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions |
| 1501 | are now deprecated. |
| 1502 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1503 | * New macros: the Python header files now define the following macros: |
| 1504 | :cmacro:`Py_ISALNUM`, |
| 1505 | :cmacro:`Py_ISALPHA`, |
| 1506 | :cmacro:`Py_ISDIGIT`, |
| 1507 | :cmacro:`Py_ISLOWER`, |
| 1508 | :cmacro:`Py_ISSPACE`, |
| 1509 | :cmacro:`Py_ISUPPER`, |
| 1510 | :cmacro:`Py_ISXDIGIT`, |
| 1511 | and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`. |
| 1512 | All of these functions are analogous to the C |
| 1513 | standard macros for classifying characters, but ignore the current |
| 1514 | locale setting, because in |
| 1515 | several places Python needs to analyze characters in a |
| 1516 | locale-independent way. (Added by Eric Smith; |
| 1517 | :issue:`5793`.) |
| 1518 | |
| 1519 | .. XXX these macros don't seem to be described in the c-api docs. |
| 1520 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1521 | * New format codes: the :cfunc:`PyFormat_FromString`, |
| 1522 | :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now |
| 1523 | accepts ``%lld`` and ``%llu`` format codes for displaying values of |
| 1524 | C's :ctype:`long long` types. |
| 1525 | (Contributed by Mark Dickinson; :issue:`7228`.) |
| 1526 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1527 | * The complicated interaction between threads and process forking has |
| 1528 | been changed. Previously, the child process created by |
| 1529 | :func:`os.fork` might fail because the child is created with only a |
| 1530 | single thread running, the thread performing the :func:`os.fork`. |
| 1531 | If other threads were holding a lock, such as Python's import lock, |
| 1532 | when the fork was performed, the lock would still be marked as |
| 1533 | "held" in the new process. But in the child process nothing would |
| 1534 | ever release the lock, since the other threads weren't replicated, |
| 1535 | and the child process would no longer be able to perform imports. |
| 1536 | |
| 1537 | Python 2.7 now acquires the import lock before performing an |
| 1538 | :func:`os.fork`, and will also clean up any locks created using the |
| 1539 | :mod:`threading` module. C extension modules that have internal |
| 1540 | locks, or that call :cfunc:`fork()` themselves, will not benefit |
| 1541 | from this clean-up. |
| 1542 | |
| 1543 | (Fixed by Thomas Wouters; :issue:`1590864`.) |
| 1544 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1545 | * The :cfunc:`Py_Finalize` function now calls the internal |
| 1546 | :func:`threading._shutdown` function; this prevents some exceptions from |
| 1547 | being raised when an interpreter shuts down. |
| 1548 | (Patch by Adam Olsen; :issue:`1722344`.) |
| 1549 | |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 1550 | * Global symbols defined by the :mod:`ctypes` module are now prefixed |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1551 | with ``Py``, or with ``_ctypes``. (Implemented by Thomas |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 1552 | Heller; :issue:`3102`.) |
| 1553 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1554 | * New configure option: the :option:`--with-system-expat` switch allows |
| 1555 | building the :mod:`pyexpat` module to use the system Expat library. |
| 1556 | (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.) |
| 1557 | |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 1558 | * New configure option: compiling Python with the |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1559 | :option:`--with-valgrind` option will now disable the pymalloc |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 1560 | allocator, which is difficult for the Valgrind memory-error detector |
| 1561 | to analyze correctly. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1562 | Valgrind will therefore be better at detecting memory leaks and |
| 1563 | overruns. (Contributed by James Henstridge; :issue:`2422`.) |
| 1564 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 1565 | * New configure option: you can now supply no arguments to |
| 1566 | :option:`--with-dbmliborder=` in order to build none of the various |
| 1567 | DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis; |
| 1568 | :issue:`6491`.) |
| 1569 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1570 | * The :program:`configure` script now checks for floating-point rounding bugs |
| 1571 | on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING` |
| 1572 | preprocessor definition. No code currently uses this definition, |
| 1573 | but it's available if anyone wishes to use it. |
| 1574 | (Added by Mark Dickinson; :issue:`2937`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1575 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1576 | :program:`configure` also now sets a :envvar:`LDCXXSHARED` Makefile |
| 1577 | variable for supporting C++ linking. (Contributed by Arfrever |
| 1578 | Frehtes Taifersar Arahesis; :issue:`1222585`.) |
| 1579 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1580 | * The build process now creates the necessary files for pkg-config |
| 1581 | support. (Contributed by Clinton Roy; :issue:`3585`.) |
| 1582 | |
| 1583 | * The build process now supports Subversion 1.7. (Contributed by |
| 1584 | Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.) |
| 1585 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1586 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1587 | .. ====================================================================== |
| 1588 | |
| 1589 | Port-Specific Changes: Windows |
| 1590 | ----------------------------------- |
| 1591 | |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 1592 | * The :mod:`msvcrt` module now contains some constants from |
| 1593 | the :file:`crtassem.h` header file: |
| 1594 | :data:`CRT_ASSEMBLY_VERSION`, |
| 1595 | :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`, |
| 1596 | and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`. |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1597 | (Contributed by David Cournapeau; :issue:`4365`.) |
| 1598 | |
| 1599 | * The new :cfunc:`_beginthreadex` API is used to start threads, and |
| 1600 | the native thread-local storage functions are now used. |
| 1601 | (Contributed by Kristjan Valur Jonsson; :issue:`3582`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1602 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1603 | * The :func:`os.listdir` function now correctly fails |
| 1604 | for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.) |
| 1605 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1606 | * The :mod:`mimelib` module will now read the MIME database from |
| 1607 | the Windows registry when initializing. |
| 1608 | (Patch by Gabriel Genellina; :issue:`4969`.) |
| 1609 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1610 | .. ====================================================================== |
| 1611 | |
| 1612 | Port-Specific Changes: Mac OS X |
| 1613 | ----------------------------------- |
| 1614 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1615 | * The path ``/Library/Python/2.7/site-packages`` is now appended to |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1616 | ``sys.path``, in order to share added packages between the system |
| 1617 | installation and a user-installed copy of the same version. |
| 1618 | (Changed by Ronald Oussoren; :issue:`4865`.) |
| 1619 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1620 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1621 | Other Changes and Fixes |
| 1622 | ======================= |
| 1623 | |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1624 | * Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were |
| 1625 | added to the :file:`Tools` directory. :file:`iobench` measures the |
Antoine Pitrou | dde96e6 | 2010-02-08 20:25:47 +0000 | [diff] [blame] | 1626 | speed of built-in file I/O objects (as returned by :func:`open`) |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1627 | while performing various operations, and :file:`ccbench` is a |
| 1628 | concurrency benchmark that tries to measure computing throughput, |
| 1629 | thread switching latency, and IO processing bandwidth when |
| 1630 | performing several tasks using a varying number of threads. |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1631 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1632 | * When importing a module from a :file:`.pyc` or :file:`.pyo` file |
| 1633 | with an existing :file:`.py` counterpart, the :attr:`co_filename` |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 1634 | attributes of the resulting code objects are overwritten when the |
| 1635 | original filename is obsolete. This can happen if the file has been |
| 1636 | renamed, moved, or is accessed through different paths. (Patch by |
| 1637 | Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.) |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1638 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1639 | * The :file:`regrtest.py` script now takes a :option:`--randseed=` |
| 1640 | switch that takes an integer that will be used as the random seed |
| 1641 | for the :option:`-r` option that executes tests in random order. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1642 | The :option:`-r` option also reports the seed that was used |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1643 | (Added by Collin Winter.) |
| 1644 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1645 | * Another :file:`regrtest.py` switch is :option:`-j`, which |
| 1646 | takes an integer specifying how many tests run in parallel. This |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1647 | allows reducing the total runtime on multi-core machines. |
Antoine Pitrou | 4698d99 | 2009-05-31 14:20:14 +0000 | [diff] [blame] | 1648 | This option is compatible with several other options, including the |
| 1649 | :option:`-R` switch which is known to produce long runtimes. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1650 | (Added by Antoine Pitrou, :issue:`6152`.) This can also be used |
| 1651 | with a new :option:`-F` switch that runs selected tests in a loop |
| 1652 | until they fail. (Added by Antoine Pitrou; :issue:`7312`.) |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1653 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1654 | .. ====================================================================== |
| 1655 | |
| 1656 | Porting to Python 2.7 |
| 1657 | ===================== |
| 1658 | |
| 1659 | This section lists previously described changes and other bugfixes |
| 1660 | that may require changes to your code: |
| 1661 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1662 | * When using :class:`Decimal` instances with a string's |
| 1663 | :meth:`format` method, the default alignment was previously |
| 1664 | left-alignment. This has been changed to right-alignment, which might |
| 1665 | change the output of your programs. |
| 1666 | (Changed by Mark Dickinson; :issue:`6857`.) |
| 1667 | |
| 1668 | Another :meth:`format`-related change: the default precision used |
| 1669 | for floating-point and complex numbers was changed from 6 decimal |
| 1670 | places to 12, which matches the precision used by :func:`str`. |
| 1671 | (Changed by Eric Smith; :issue:`5920`.) |
| 1672 | |
Amaury Forgeot d'Arc | 901f200 | 2009-06-09 23:08:13 +0000 | [diff] [blame] | 1673 | * Because of an optimization for the :keyword:`with` statement, the special |
| 1674 | methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's |
| 1675 | type, and cannot be directly attached to the object's instance. This |
Amaury Forgeot d'Arc | d81333c | 2009-06-10 20:30:19 +0000 | [diff] [blame] | 1676 | affects new-style classes (derived from :class:`object`) and C extension |
Amaury Forgeot d'Arc | 901f200 | 2009-06-09 23:08:13 +0000 | [diff] [blame] | 1677 | types. (:issue:`6101`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1678 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1679 | * The :meth:`readline` method of :class:`StringIO` objects now does |
| 1680 | nothing when a negative length is requested, as other file-like |
| 1681 | objects do. (:issue:`7348`). |
| 1682 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1683 | In the standard library: |
| 1684 | |
| 1685 | * The ElementTree library, :mod:`xml.etree`, no longer escapes |
| 1686 | ampersands and angle brackets when outputting an XML processing |
| 1687 | instruction (which looks like `<?xml-stylesheet href="#style1"?>`) |
| 1688 | or comment (which looks like `<!-- comment -->`). |
| 1689 | (Patch by Neil Muller; :issue:`2746`.) |
| 1690 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1691 | For C extensions: |
| 1692 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 1693 | * C extensions that use integer format codes with the ``PyArg_Parse*`` |
| 1694 | family of functions will now raise a :exc:`TypeError` exception |
| 1695 | instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`). |
| 1696 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1697 | * Use the new :cfunc:`PyOS_string_to_double` function instead of the old |
| 1698 | :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions, |
| 1699 | which are now deprecated. |
| 1700 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 1701 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1702 | .. ====================================================================== |
| 1703 | |
| 1704 | |
| 1705 | .. _acks27: |
| 1706 | |
| 1707 | Acknowledgements |
| 1708 | ================ |
| 1709 | |
| 1710 | The author would like to thank the following people for offering |
| 1711 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | 8f254e7 | 2009-12-08 02:37:05 +0000 | [diff] [blame] | 1712 | article: Ryan Lovett, Hugh Secker-Walker. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1713 | |