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