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 | 02f7b99 | 2010-05-07 01:45:14 +0000 | [diff] [blame] | 11 | .. Big jobs: pep 391, PyCapsule |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 12 | |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 13 | .. hyperlink all the methods & functions. |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 14 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 15 | .. T_STRING_INPLACE not described in main docs |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 16 | .. XXX "Format String Syntax" in string.rst could use many more examples. |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 17 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 18 | .. $Id$ |
| 19 | Rules for maintenance: |
| 20 | |
| 21 | * Anyone can add text to this document. Do not spend very much time |
| 22 | on the wording of your changes, because your text will probably |
| 23 | get rewritten to some degree. |
| 24 | |
| 25 | * The maintainer will go through Misc/NEWS periodically and add |
| 26 | changes; it's therefore more important to add your changes to |
| 27 | Misc/NEWS than to this file. |
| 28 | |
| 29 | * This is not a complete list of every single change; completeness |
| 30 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 31 | or esoteric to include. If such a change is added to the text, |
| 32 | I'll just remove it. (This is another reason you shouldn't spend |
| 33 | too much time on writing your addition.) |
| 34 | |
| 35 | * If you want to draw your new text to the attention of the |
| 36 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 37 | section. |
| 38 | |
| 39 | * It's OK to just add a fragmentary note about a change. For |
| 40 | example: "XXX Describe the transmogrify() function added to the |
| 41 | socket module." The maintainer will research the change and |
| 42 | write the necessary text. |
| 43 | |
| 44 | * You can comment out your additions if you like, but it's not |
| 45 | necessary (especially when a final release is some months away). |
| 46 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 47 | * 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] | 48 | sufficient; the e-mail address isn't necessary. |
| 49 | |
| 50 | * It's helpful to add the bug/patch number in a parenthetical comment. |
| 51 | |
| 52 | XXX Describe the transmogrify() function added to the socket |
| 53 | module. |
| 54 | (Contributed by P.Y. Developer; :issue:`12345`.) |
| 55 | |
| 56 | This saves the maintainer some effort going through the SVN logs |
| 57 | when researching a change. |
| 58 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 59 | This article explains the new features in Python 2.7. The final |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 60 | release of 2.7 is currently scheduled for July 2010; the detailed |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 61 | schedule is described in :pep:`373`. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 62 | |
| 63 | .. Compare with previous release in 2 - 3 sentences here. |
| 64 | add hyperlink when the documentation becomes available online. |
| 65 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 66 | .. _whatsnew27-python31: |
| 67 | |
Andrew M. Kuchling | 02f7b99 | 2010-05-07 01:45:14 +0000 | [diff] [blame] | 68 | The Future for Python 2.x |
| 69 | ========================= |
| 70 | |
| 71 | Python 2.7 is intended to be the last major release in the 2.x series. |
| 72 | Though more major releases have not been absolutely ruled out, the |
| 73 | Python maintainers are planning to focus their efforts on Python 3.x. |
| 74 | |
| 75 | This means that 2.7 will remain in place for a long time, running |
| 76 | production systems that have not been ported to Python 3.x. |
| 77 | Two consequences of the long-term significance of 2.7 are: |
| 78 | |
| 79 | * It's very likely the 2.7 release will have a longer period of |
| 80 | maintenance compared to earlier 2.x versions. Python 2.7 will |
| 81 | continue to be maintained while the transition to 3.x is in |
| 82 | progress, and that transition will itself be lengthy. Most 2.x |
| 83 | versions are maintained for about 4 years, from the first to the |
| 84 | last bugfix release; patchlevel releases for Python 2.7 will |
| 85 | probably be made for at least 6 years. |
| 86 | |
| 87 | * Because 2.7 will be running production applications, a policy |
| 88 | decision was made to silence warnings only of interest to developers |
| 89 | by default. Silencing :exc:`DeprecationWarning` and its descendants |
| 90 | prevents users from seeing warnings triggered by an application. |
| 91 | (Carried out in :issue:`7319`.) |
| 92 | |
| 93 | You can re-enable display of :exc:`DeprecationWarning` messages by |
| 94 | running Python with the :option:`-Wdefault` (short form: |
| 95 | :option:`-Wd`) switch, or you can add |
| 96 | ``warnings.simplefilter('default')`` to your code. |
| 97 | |
| 98 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 99 | Python 3.1 Features |
| 100 | ======================= |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 101 | |
| 102 | Much as Python 2.6 incorporated features from Python 3.0, |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 103 | version 2.7 incorporates some of the new features |
| 104 | in Python 3.1. The 2.x series continues to provide tools |
| 105 | for migrating to the 3.x series. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 106 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 107 | A partial list of 3.1 features that were backported to 2.7: |
| 108 | |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 109 | * A new version of the :mod:`io` library, rewritten in C for performance. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 110 | * The ordered-dictionary type described in :ref:`pep-0372`. |
Andrew M. Kuchling | 8f254e7 | 2009-12-08 02:37:05 +0000 | [diff] [blame] | 111 | * The new format specifier described in :ref:`pep-0378`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 112 | * The :class:`memoryview` object. |
| 113 | * 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] | 114 | * Float-to-string and string-to-float conversions now round their |
| 115 | results more correctly. And :func:`repr` of a floating-point |
| 116 | number *x* returns a result that's guaranteed to round back to the |
| 117 | same number when converted back to a string. |
| 118 | * The :cfunc:`PyLong_AsLongAndOverflow` C API function. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 119 | |
| 120 | One porting change: the :option:`-3` switch now automatically |
| 121 | enables the :option:`-Qwarn` switch that causes warnings |
| 122 | about using classic division with integers and long integers. |
| 123 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 124 | Other new Python3-mode warnings include: |
| 125 | |
| 126 | * :func:`operator.isCallable` and :func:`operator.sequenceIncludes`, |
| 127 | which are not supported in 3.x. |
| 128 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 129 | .. ======================================================================== |
| 130 | .. Large, PEP-level features and changes should be described here. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 131 | .. ======================================================================== |
| 132 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 133 | .. _pep-0372: |
| 134 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 135 | PEP 372: Adding an ordered dictionary to collections |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 136 | ==================================================== |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 137 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 138 | Regular Python dictionaries iterate over key/value pairs in arbitrary order. |
| 139 | Over the years, a number of authors have written alternative implementations |
| 140 | that remember the order that the keys were originally inserted. Based on |
| 141 | the experiences from those implementations, a new |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 142 | :class:`~collections.OrderedDict` class has been introduced in the |
| 143 | :mod:`collections` module. |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 144 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 145 | The :class:`~collections.OrderedDict` API is substantially the same as regular |
| 146 | dictionaries but will iterate over keys and values in a guaranteed order |
| 147 | depending on when a key was first inserted:: |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 148 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 149 | >>> from collections import OrderedDict |
| 150 | >>> d = OrderedDict([('first', 1), ('second', 2), |
| 151 | ... ('third', 3)]) |
| 152 | >>> d.items() |
| 153 | [('first', 1), ('second', 2), ('third', 3)] |
| 154 | |
| 155 | If a new entry overwrites an existing entry, the original insertion |
| 156 | position is left unchanged:: |
| 157 | |
| 158 | >>> d['second'] = 4 |
| 159 | >>> d.items() |
| 160 | [('first', 1), ('second', 4), ('third', 3)] |
| 161 | |
| 162 | Deleting an entry and reinserting it will move it to the end:: |
| 163 | |
| 164 | >>> del d['second'] |
| 165 | >>> d['second'] = 5 |
| 166 | >>> d.items() |
| 167 | [('first', 1), ('third', 3), ('second', 5)] |
| 168 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 169 | The :meth:`~collections.OrderedDict.popitem` method has an optional *last* |
| 170 | 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] | 171 | added key is returned and removed; if it's False, the |
| 172 | oldest key is selected:: |
| 173 | |
| 174 | >>> od = OrderedDict([(x,0) for x in range(20)]) |
| 175 | >>> od.popitem() |
| 176 | (19, 0) |
| 177 | >>> od.popitem() |
| 178 | (18, 0) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 179 | >>> od.popitem(last=False) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 180 | (0, 0) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 181 | >>> od.popitem(last=False) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 182 | (1, 0) |
| 183 | |
| 184 | Comparing two ordered dictionaries checks both the keys and values, |
| 185 | and requires that the insertion order was the same:: |
| 186 | |
| 187 | >>> od1 = OrderedDict([('first', 1), ('second', 2), |
| 188 | ... ('third', 3)]) |
| 189 | >>> od2 = OrderedDict([('third', 3), ('first', 1), |
| 190 | ... ('second', 2)]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 191 | >>> od1 == od2 |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 192 | False |
| 193 | >>> # Move 'third' key to the end |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 194 | >>> del od2['third']; od2['third'] = 3 |
| 195 | >>> od1 == od2 |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 196 | True |
| 197 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 198 | Comparing an :class:`~collections.OrderedDict` with a regular dictionary |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 199 | ignores the insertion order and just compares the keys and values. |
| 200 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 201 | How does the :class:`~collections.OrderedDict` work? It maintains a |
| 202 | doubly-linked list of keys, appending new keys to the list as they're inserted. |
| 203 | A secondary dictionary maps keys to their corresponding list node, so |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 204 | deletion doesn't have to traverse the entire linked list and therefore |
| 205 | remains O(1). |
| 206 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 207 | The standard library now supports use of ordered dictionaries in several |
Andrew M. Kuchling | 363dbcc | 2010-04-14 23:55:17 +0000 | [diff] [blame] | 208 | modules. |
| 209 | |
| 210 | * The :mod:`ConfigParser` module uses them by default, letting |
| 211 | configuration files be read, modified, and then written back in their original |
| 212 | order. |
| 213 | |
| 214 | * The :meth:`~collections.somenamedtuple._asdict()` method for |
| 215 | :func:`collections.namedtuple` now returns an ordered dictionary with the |
| 216 | values appearing in the same order as the underlying tuple indices. |
| 217 | |
| 218 | * The :mod:`json` module's :class:`~json.JSONDecoder` class |
| 219 | constructor was extended with an *object_pairs_hook* parameter to |
| 220 | allow :class:`OrderedDict` instances to be built by the decoder. |
| 221 | Support was also added for third-party tools like |
| 222 | `PyYAML <http://pyyaml.org/>`_. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 223 | |
Andrew M. Kuchling | 7fe65a0 | 2009-10-13 15:49:33 +0000 | [diff] [blame] | 224 | .. seealso:: |
| 225 | |
| 226 | :pep:`372` - Adding an ordered dictionary to collections |
| 227 | PEP written by Armin Ronacher and Raymond Hettinger; |
| 228 | implemented by Raymond Hettinger. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 229 | |
| 230 | .. _pep-0378: |
| 231 | |
| 232 | PEP 378: Format Specifier for Thousands Separator |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 233 | ================================================= |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 234 | |
| 235 | To make program output more readable, it can be useful to add |
| 236 | separators to large numbers and render them as |
| 237 | 18,446,744,073,709,551,616 instead of 18446744073709551616. |
| 238 | |
| 239 | The fully general solution for doing this is the :mod:`locale` module, |
| 240 | which can use different separators ("," in North America, "." in |
| 241 | Europe) and different grouping sizes, but :mod:`locale` is complicated |
| 242 | to use and unsuitable for multi-threaded applications where different |
| 243 | threads are producing output for different locales. |
| 244 | |
| 245 | Therefore, a simple comma-grouping mechanism has been added to the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 246 | mini-language used by the :meth:`str.format` method. When |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 247 | formatting a floating-point number, simply include a comma between the |
| 248 | width and the precision:: |
| 249 | |
Eric Smith | c466385 | 2010-04-06 14:30:15 +0000 | [diff] [blame] | 250 | >>> '{:20,.2f}'.format(18446744073709551616.0) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 251 | '18,446,744,073,709,551,616.00' |
| 252 | |
Eric Smith | 6a92860 | 2010-04-06 15:17:33 +0000 | [diff] [blame] | 253 | When formatting an integer, include the comma after the width: |
| 254 | |
| 255 | >>> '{:20,d}'.format(18446744073709551616) |
| 256 | '18,446,744,073,709,551,616' |
| 257 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 258 | This mechanism is not adaptable at all; commas are always used as the |
| 259 | separator and the grouping is always into three-digit groups. The |
| 260 | comma-formatting mechanism isn't as general as the :mod:`locale` |
| 261 | module, but it's easier to use. |
| 262 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 263 | .. seealso:: |
| 264 | |
| 265 | :pep:`378` - Format Specifier for Thousands Separator |
| 266 | PEP written by Raymond Hettinger; implemented by Eric Smith. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 267 | |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 268 | PEP 389: The argparse Module for Parsing Command Lines |
| 269 | ====================================================== |
| 270 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 271 | The :mod:`argparse` module for parsing command-line arguments was |
| 272 | added, intended as a more powerful replacement for the |
| 273 | :mod:`optparse` module. |
| 274 | |
| 275 | This means Python now supports three different modules for parsing |
| 276 | command-line arguments: :mod:`getopt`, :mod:`optparse`, and |
| 277 | :mod:`argparse`. The :mod:`getopt` module closely resembles the C |
| 278 | :cfunc:`getopt` function, so it remains useful if you're writing a |
| 279 | Python prototype that will eventually be rewritten in C. |
| 280 | :mod:`optparse` becomes redundant, but there are no plans to remove it |
| 281 | because there are many scripts still using it, and there's no |
| 282 | automated way to update these scripts. (Making the :mod:`argparse` |
| 283 | API consistent with :mod:`optparse`'s interface was discussed but |
| 284 | rejected as too messy and difficult.) |
| 285 | |
Andrew M. Kuchling | f03641a | 2010-04-14 01:14:59 +0000 | [diff] [blame] | 286 | In short, if you're writing a new script and don't need to worry |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 287 | about compatibility with earlier versions of Python, use |
| 288 | :mod:`argparse` instead of :mod:`optparse`. |
| 289 | |
Andrew M. Kuchling | f03641a | 2010-04-14 01:14:59 +0000 | [diff] [blame] | 290 | Here's an example:: |
| 291 | |
| 292 | import argparse |
| 293 | |
| 294 | parser = argparse.ArgumentParser(description='Command-line example.') |
| 295 | |
| 296 | # Add optional switches |
| 297 | parser.add_argument('-v', action='store_true', dest='is_verbose', |
| 298 | help='produce verbose output') |
| 299 | parser.add_argument('-o', action='store', dest='output', |
| 300 | metavar='FILE', |
| 301 | help='direct output to FILE instead of stdout') |
| 302 | parser.add_argument('-C', action='store', type=int, dest='context', |
| 303 | metavar='NUM', default=0, |
| 304 | help='display NUM lines of added context') |
| 305 | |
| 306 | # Allow any number of additional arguments. |
| 307 | parser.add_argument(nargs='*', action='store', dest='inputs', |
| 308 | help='input filenames (default is stdin)') |
| 309 | |
| 310 | args = parser.parse_args() |
| 311 | print args.__dict__ |
| 312 | |
| 313 | Unless you override it, :option:`-h` and :option:`--help` switches |
| 314 | are automatically added, and produce neatly formatted output:: |
| 315 | |
| 316 | -> ./python.exe argparse-example.py --help |
Andrew M. Kuchling | 363dbcc | 2010-04-14 23:55:17 +0000 | [diff] [blame] | 317 | usage: argparse-example.py [-h] [-v] [-o FILE] [-C NUM] [inputs [inputs ...]] |
Andrew M. Kuchling | f03641a | 2010-04-14 01:14:59 +0000 | [diff] [blame] | 318 | |
| 319 | Command-line example. |
| 320 | |
| 321 | positional arguments: |
| 322 | inputs input filenames (default is stdin) |
| 323 | |
| 324 | optional arguments: |
| 325 | -h, --help show this help message and exit |
| 326 | -v produce verbose output |
| 327 | -o FILE direct output to FILE instead of stdout |
| 328 | -C NUM display NUM lines of added context |
| 329 | |
| 330 | Similarly to :mod:`optparse`, the command-line switches and arguments |
| 331 | are returned as an object with attributes named by the *dest* parameters:: |
| 332 | |
| 333 | -> ./python.exe argparse-example.py -v |
| 334 | {'output': None, 'is_verbose': True, 'context': 0, 'inputs': []} |
| 335 | |
| 336 | -> ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2 |
| 337 | {'output': '/tmp/output', 'is_verbose': True, 'context': 4, |
| 338 | 'inputs': ['file1', 'file2']} |
| 339 | |
| 340 | :mod:`argparse` has much fancier validation than :mod:`optparse`; you |
| 341 | can specify an exact number of arguments as an integer, 0 or more |
| 342 | arguments by passing ``'*'``, 1 or more by passing ``'+'``, or an |
| 343 | optional argument with ``'?'``. A top-level parser can contain |
| 344 | sub-parsers, so you can define subcommands that have different sets of |
| 345 | switches, as in ``svn commit``, ``svn checkout``, etc. You can |
| 346 | specify an argument type as :class:`~argparse.FileType`, which will |
| 347 | automatically open files for you and understands that ``'-'`` means |
| 348 | standard input or output. |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 349 | |
| 350 | .. seealso:: |
| 351 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 352 | `argparse module documentation <http://docs.python.org/dev/library/argparse.html>`__ |
| 353 | |
| 354 | `Upgrading optparse code to use argparse <http://docs.python.org/dev/library/argparse.html#upgrading-optparse-code>`__ |
| 355 | |
Andrew M. Kuchling | ab21f75 | 2010-03-02 13:55:33 +0000 | [diff] [blame] | 356 | :pep:`389` - argparse - New Command Line Parsing Module |
| 357 | PEP written and implemented by Steven Bethard. |
| 358 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 359 | PEP 391: Dictionary-Based Configuration For Logging |
| 360 | ==================================================== |
| 361 | |
Andrew M. Kuchling | b6c1aeb | 2010-04-14 14:28:31 +0000 | [diff] [blame] | 362 | .. not documented in library reference yet. |
| 363 | |
| 364 | The :mod:`logging` module is very flexible; an application can define |
| 365 | a tree of logging subsystems, and each logger in this tree can filter |
| 366 | out certain messages, format them differently, and direct messages to |
| 367 | a varying number of handlers. |
| 368 | |
| 369 | All this flexibility can require a lot of configuration. You can |
| 370 | write Python statements to create objects and set their properties, |
| 371 | but a complex set-up would require verbose but boring code. |
| 372 | :mod:`logging` also supports a :func:`~logging.config.fileConfig` |
| 373 | function that parses a file, but the file format doesn't support |
| 374 | configuring filters, and it's messier to generate programmatically. |
| 375 | |
| 376 | Python 2.7 adds a :func:`~logging.config.dictConfig` function that |
| 377 | uses a dictionary, and there are many ways to produce a dictionary |
| 378 | from different sources. You can construct one with code, of course. |
| 379 | Python's standard library now includes a JSON parser, so you could |
| 380 | parse a file containing JSON, or you could use a YAML parsing library |
| 381 | if one is installed. |
| 382 | |
| 383 | XXX describe an example. |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 384 | |
Andrew M. Kuchling | 02f7b99 | 2010-05-07 01:45:14 +0000 | [diff] [blame] | 385 | Three smaller enhancements to the :mod:`logging` module, all |
| 386 | implemented by Vinay Sajip, are: |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 387 | |
| 388 | .. rev79293 |
| 389 | |
Andrew M. Kuchling | 02f7b99 | 2010-05-07 01:45:14 +0000 | [diff] [blame] | 390 | * The :class:`~logging.handlers.SysLogHandler` class now supports |
| 391 | syslogging over TCP. The constructor has a *socktype* parameter |
| 392 | giving the type of socket to use, either :const:`socket.SOCK_DGRAM` |
| 393 | for UDP or :const:`socket.SOCK_STREAM` for TCP. The default |
| 394 | protocol remains UDP. |
| 395 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 396 | * :class:`Logger` instances gained a :meth:`getChild` method that retrieves a |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 397 | descendant logger using a relative path. For example, |
| 398 | once you retrieve a logger by doing ``log = getLogger('app')``, |
| 399 | calling ``log.getChild('network.listen')`` is equivalent to |
| 400 | ``getLogger('app.network.listen')``. |
| 401 | |
| 402 | * The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method |
| 403 | that takes a *level* and returns whether the underlying logger would |
| 404 | process a message of that level of importance. |
| 405 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 406 | .. seealso:: |
| 407 | |
| 408 | :pep:`391` - Dictionary-Based Configuration For Logging |
| 409 | PEP written and implemented by Vinay Sajip. |
| 410 | |
| 411 | PEP 3106: Dictionary Views |
| 412 | ==================================================== |
| 413 | |
Andrew M. Kuchling | 85f928a | 2010-04-15 01:42:27 +0000 | [diff] [blame] | 414 | The dictionary methods :meth:`keys`, :meth:`values`, and :meth:`items` |
| 415 | are different in Python 3.x. They return an object called a :dfn:`view` |
| 416 | instead of a fully materialized list. |
| 417 | |
Andrew M. Kuchling | 85f928a | 2010-04-15 01:42:27 +0000 | [diff] [blame] | 418 | It's not possible to change the return values of :meth:`keys`, |
| 419 | :meth:`values`, and :meth:`items` in Python 2.7 because too much code |
| 420 | would break. Instead the 3.x versions were added under the new names |
| 421 | of :meth:`viewkeys`, :meth:`viewvalues`, and :meth:`viewitems`. |
| 422 | |
| 423 | :: |
| 424 | |
| 425 | >>> d = dict((i*10, chr(65+i)) for i in range(26)) |
| 426 | >>> d |
| 427 | {0: 'A', 130: 'N', 10: 'B', 140: 'O', 20: ..., 250: 'Z'} |
| 428 | >>> d.viewkeys() |
| 429 | dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250]) |
| 430 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 431 | Views can be iterated over, but they also behave like sets. The ``&`` |
| 432 | operator performs intersection, and ``|`` performs a union:: |
| 433 | |
| 434 | >>> d1 = dict((i*10, chr(65+i)) for i in range(26)) |
| 435 | >>> d2 = dict((i**.5, i) for i in range(1000)) |
| 436 | >>> d1.viewkeys() & d2.viewkeys() |
| 437 | set([0.0, 10.0, 20.0, 30.0]) |
| 438 | >>> d1.viewkeys() | range(0, 30) |
| 439 | set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250]) |
| 440 | |
Andrew M. Kuchling | 85f928a | 2010-04-15 01:42:27 +0000 | [diff] [blame] | 441 | The view keeps track of the dictionary and its contents change as the |
| 442 | dictionary is modified:: |
| 443 | |
| 444 | >>> vk = d.viewkeys() |
| 445 | >>> vk |
| 446 | dict_keys([0, 130, 10, ..., 250]) |
| 447 | >>> d[260] = '&' |
| 448 | >>> vk |
| 449 | dict_keys([0, 130, 260, 10, ..., 250]) |
| 450 | |
| 451 | However, note that you can't add or remove keys while you're iterating |
| 452 | over the view:: |
| 453 | |
| 454 | >>> for k in vk: |
| 455 | ... d[k*2] = k |
| 456 | ... |
| 457 | Traceback (most recent call last): |
| 458 | File "<stdin>", line 1, in <module> |
| 459 | RuntimeError: dictionary changed size during iteration |
| 460 | |
| 461 | You can use the view methods in Python 2.x code, and the 2to3 |
| 462 | converter will change them to the standard :meth:`keys`, |
| 463 | :meth:`values`, and :meth:`items` methods. |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 464 | |
| 465 | .. seealso:: |
| 466 | |
| 467 | :pep:`3106` - Revamping dict.keys(), .values() and .items() |
| 468 | PEP written by Guido van Rossum. |
| 469 | Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`. |
| 470 | |
| 471 | |
Andrew M. Kuchling | 9fbbd3b | 2010-05-01 12:06:51 +0000 | [diff] [blame] | 472 | PEP 3137: The memoryview Object |
| 473 | ==================================================== |
| 474 | |
| 475 | The :class:`memoryview` object provides a view of another object's |
| 476 | memory content that matches the :class:`bytes` type's interface. |
| 477 | |
| 478 | >>> import string |
| 479 | >>> m = memoryview(string.letters) |
| 480 | >>> m |
| 481 | <memory at 0x37f850> |
| 482 | >>> len(m) # Returns length of underlying object |
| 483 | 52 |
| 484 | >>> m[0], m[25], m[26] # Indexing returns one byte |
| 485 | ('a', 'z', 'A') |
| 486 | >>> m2 = m[0:26] # Slicing returns another memoryview |
| 487 | >>> m2 |
| 488 | <memory at 0x37f080> |
| 489 | |
| 490 | The content of the view can be converted to a string of bytes or to |
| 491 | a list of integers: |
| 492 | |
| 493 | >>> m2.tobytes() |
| 494 | 'abcdefghijklmnopqrstuvwxyz' |
| 495 | >>> m2.tolist() |
| 496 | [97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122] |
| 497 | >>> |
| 498 | |
| 499 | :class:`memoryview` objects allow modifying the underlying object if |
| 500 | it's a mutable object. |
| 501 | |
| 502 | >>> m2[0] = 75 |
| 503 | Traceback (most recent call last): |
| 504 | File "<stdin>", line 1, in <module> |
| 505 | TypeError: cannot modify read-only memory |
| 506 | >>> b = bytearray(string.letters) # Creating a mutable object |
| 507 | >>> b |
| 508 | bytearray(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') |
| 509 | >>> mb = memoryview(b) |
| 510 | >>> mb[0] = '*' # Assign to view, changing the bytearray. |
| 511 | >>> b[0:5] # The bytearray has been changed. |
| 512 | bytearray(b'*bcde') |
| 513 | >>> |
| 514 | |
| 515 | .. seealso:: |
| 516 | |
| 517 | :pep:`3137` - Immutable Bytes and Mutable Buffer |
| 518 | PEP written by Guido van Rossum. |
Antoine Pitrou | 5cace78 | 2010-05-01 12:16:39 +0000 | [diff] [blame] | 519 | Implemented by Travis Oliphant, Antoine Pitrou and others. |
Andrew M. Kuchling | 9fbbd3b | 2010-05-01 12:06:51 +0000 | [diff] [blame] | 520 | Backported to 2.7 by Antoine Pitrou; :issue:`2396`. |
| 521 | |
| 522 | |
| 523 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 524 | Other Language Changes |
| 525 | ====================== |
| 526 | |
| 527 | Some smaller changes made to the core Python language are: |
| 528 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 529 | * The syntax for set literals has been backported from Python 3.x. |
| 530 | Curly brackets are used to surround the contents of the resulting |
| 531 | mutable set; set literals are |
| 532 | distinguished from dictionaries by not containing colons and values. |
| 533 | ``{}`` continues to represent an empty dictionary; use |
| 534 | ``set()`` for an empty set. |
| 535 | |
| 536 | >>> {1,2,3,4,5} |
| 537 | set([1, 2, 3, 4, 5]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 538 | >>> set() # empty set |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 539 | set([]) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 540 | >>> {} # empty dict |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 541 | {} |
| 542 | |
| 543 | Backported by Alexandre Vassalotti; :issue:`2335`. |
| 544 | |
| 545 | * Dictionary and set comprehensions are another feature backported from |
| 546 | 3.x, generalizing list/generator comprehensions to use |
| 547 | the literal syntax for sets and dictionaries. |
| 548 | |
| 549 | >>> {x:x*x for x in range(6)} |
| 550 | {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25} |
| 551 | >>> {'a'*x for x in range(6)} |
| 552 | set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa']) |
| 553 | |
| 554 | Backported by Alexandre Vassalotti; :issue:`2333`. |
| 555 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 556 | * The :keyword:`with` statement can now use multiple context managers |
| 557 | in one statement. Context managers are processed from left to right |
| 558 | and each one is treated as beginning a new :keyword:`with` statement. |
| 559 | This means that:: |
| 560 | |
| 561 | with A() as a, B() as b: |
| 562 | ... suite of statements ... |
| 563 | |
| 564 | is equivalent to:: |
| 565 | |
| 566 | with A() as a: |
| 567 | with B() as b: |
| 568 | ... suite of statements ... |
| 569 | |
| 570 | The :func:`contextlib.nested` function provides a very similar |
| 571 | function, so it's no longer necessary and has been deprecated. |
| 572 | |
| 573 | (Proposed in http://codereview.appspot.com/53094; implemented by |
| 574 | Georg Brandl.) |
| 575 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 576 | * Conversions between floating-point numbers and strings are |
| 577 | now correctly rounded on most platforms. These conversions occur |
| 578 | in many different places: :func:`str` on |
| 579 | floats and complex numbers; the :class:`float` and :class:`complex` |
| 580 | constructors; |
| 581 | numeric formatting; serialization and |
| 582 | deserialization of floats and complex numbers using the |
| 583 | :mod:`marshal`, :mod:`pickle` |
| 584 | and :mod:`json` modules; |
| 585 | parsing of float and imaginary literals in Python code; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 586 | and :class:`~decimal.Decimal`-to-float conversion. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 587 | |
| 588 | Related to this, the :func:`repr` of a floating-point number *x* |
| 589 | now returns a result based on the shortest decimal string that's |
| 590 | guaranteed to round back to *x* under correct rounding (with |
| 591 | round-half-to-even rounding mode). Previously it gave a string |
| 592 | based on rounding x to 17 decimal digits. |
| 593 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 594 | .. maybe add an example? |
| 595 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 596 | The rounding library responsible for this improvement works on |
| 597 | Windows, and on Unix platforms using the gcc, icc, or suncc |
| 598 | compilers. There may be a small number of platforms where correct |
| 599 | 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] | 600 | used on such systems. You can find out which code is being used |
| 601 | by checking :data:`sys.float_repr_style`, which will be ``short`` |
| 602 | 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] | 603 | |
Mark Dickinson | bdd863d | 2010-01-07 09:28:29 +0000 | [diff] [blame] | 604 | Implemented by Eric Smith and Mark Dickinson, using David Gay's |
| 605 | :file:`dtoa.c` library; :issue:`7117`. |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 606 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 607 | * The :meth:`str.format` method now supports automatic numbering of the replacement |
Benjamin Peterson | aa0a0b9 | 2009-04-11 20:27:15 +0000 | [diff] [blame] | 608 | fields. This makes using :meth:`str.format` more closely resemble using |
| 609 | ``%s`` formatting:: |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 610 | |
| 611 | >>> '{}:{}:{}'.format(2009, 04, 'Sunday') |
| 612 | '2009:4:Sunday' |
| 613 | >>> '{}:{}:{day}'.format(2009, 4, day='Sunday') |
| 614 | '2009:4:Sunday' |
| 615 | |
Benjamin Peterson | aa0a0b9 | 2009-04-11 20:27:15 +0000 | [diff] [blame] | 616 | The auto-numbering takes the fields from left to right, so the first ``{...}`` |
| 617 | specifier will use the first argument to :meth:`str.format`, the next |
| 618 | specifier will use the next argument, and so on. You can't mix auto-numbering |
| 619 | and explicit numbering -- either number all of your specifier fields or none |
| 620 | 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] | 621 | example above. (Contributed by Eric Smith; :issue:`5237`.) |
| 622 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 623 | Complex numbers now correctly support usage with :func:`format`, |
| 624 | and default to being right-aligned. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 625 | Specifying a precision or comma-separation applies to both the real |
| 626 | and imaginary parts of the number, but a specified field width and |
| 627 | 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] | 628 | output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 629 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 630 | The 'F' format code now always formats its output using uppercase characters, |
| 631 | so it will now produce 'INF' and 'NAN'. |
| 632 | (Contributed by Eric Smith; :issue:`3382`.) |
| 633 | |
Andrew M. Kuchling | c4ae73e | 2010-04-30 13:47:34 +0000 | [diff] [blame] | 634 | A low-level change: the :meth:`object.__format__` method now triggers |
| 635 | a :exc:`PendingDeprecationWarning` if it's passed a format string, |
| 636 | because the :meth:`__format__` method for :class:`object` converts |
| 637 | the object to a string representation and formats that. The method |
| 638 | used to silently apply the format string to the string |
| 639 | representation, but that could hide mistakes in Python code. If |
| 640 | you're supplying formatting information such as an alignment or |
| 641 | precision, presumably you're expecting the formatting to be applied |
| 642 | in some object-specific way. (Fixed by Eric Smith; :issue:`7994`.) |
| 643 | |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 644 | * The :func:`int` and :func:`long` types gained a ``bit_length`` |
Georg Brandl | 64e1c75 | 2009-04-11 18:19:27 +0000 | [diff] [blame] | 645 | method that returns the number of bits necessary to represent |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 646 | its argument in binary:: |
| 647 | |
| 648 | >>> n = 37 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 649 | >>> bin(n) |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 650 | '0b100101' |
| 651 | >>> n.bit_length() |
| 652 | 6 |
| 653 | >>> n = 2**123-1 |
| 654 | >>> n.bit_length() |
| 655 | 123 |
| 656 | >>> (n+1).bit_length() |
| 657 | 124 |
| 658 | |
| 659 | (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.) |
| 660 | |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 661 | * Conversions from long integers and regular integers to floating |
| 662 | point now round differently, returning the floating-point number |
| 663 | closest to the number. This doesn't matter for small integers that |
| 664 | can be converted exactly, but for large numbers that will |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 665 | unavoidably lose precision, Python 2.7 now approximates more |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 666 | closely. For example, Python 2.6 computed the following:: |
| 667 | |
| 668 | >>> n = 295147905179352891391 |
| 669 | >>> float(n) |
| 670 | 2.9514790517935283e+20 |
| 671 | >>> n - long(float(n)) |
| 672 | 65535L |
| 673 | |
| 674 | Python 2.7's floating-point result is larger, but much closer to the |
| 675 | true value:: |
| 676 | |
| 677 | >>> n = 295147905179352891391 |
| 678 | >>> float(n) |
| 679 | 2.9514790517935289e+20 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 680 | >>> n - long(float(n)) |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 681 | -1L |
| 682 | |
| 683 | (Implemented by Mark Dickinson; :issue:`3166`.) |
| 684 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 685 | Integer division is also more accurate in its rounding behaviours. (Also |
| 686 | implemented by Mark Dickinson; :issue:`1811`.) |
| 687 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 688 | * It's now possible for a subclass of the built-in :class:`unicode` type |
| 689 | to override the :meth:`__unicode__` method. (Implemented by |
| 690 | Victor Stinner; :issue:`1583863`.) |
| 691 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 692 | * The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 693 | ``None`` as its first argument. (Fixed by Georg Brandl; |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 694 | :issue:`4759`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 695 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 696 | .. bytearray doesn't seem to be documented |
| 697 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 698 | * When using ``@classmethod`` and ``@staticmethod`` to wrap |
| 699 | methods as class or static methods, the wrapper object now |
| 700 | exposes the wrapped function as their :attr:`__func__` attribute. |
| 701 | (Contributed by Amaury Forgeot d'Arc, after a suggestion by |
| 702 | George Sakkis; :issue:`5982`.) |
| 703 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 704 | * When a restricted set of attributes were set using ``__slots__``, |
| 705 | deleting an unset attribute would not raise :exc:`AttributeError` |
| 706 | as you would expect. Fixed by Benjamin Peterson; :issue:`7604`.) |
| 707 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 708 | * A new encoding named "cp720", used primarily for Arabic text, is now |
| 709 | supported. (Contributed by Alexander Belchenko and Amaury Forgeot |
| 710 | d'Arc; :issue:`1616979`.) |
| 711 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 712 | * The :class:`file` object will now set the :attr:`filename` attribute |
| 713 | on the :exc:`IOError` exception when trying to open a directory |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 714 | on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and |
| 715 | now explicitly checks for and forbids writing to read-only file objects |
| 716 | instead of trusting the C library to catch and report the error |
| 717 | (fixed by Stefan Krah; :issue:`5677`). |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 718 | |
Benjamin Peterson | ae9a0a0 | 2009-12-31 16:49:37 +0000 | [diff] [blame] | 719 | * The Python tokenizer now translates line endings itself, so the |
| 720 | :func:`compile` built-in function can now accept code using any |
| 721 | line-ending convention. Additionally, it no longer requires that the |
| 722 | code end in a newline. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 723 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 724 | * Extra parentheses in function definitions are illegal in Python 3.x, |
| 725 | meaning that you get a syntax error from ``def f((x)): pass``. In |
| 726 | Python3-warning mode, Python 2.7 will now warn about this odd usage. |
| 727 | (Noted by James Lingard; :issue:`7362`.) |
| 728 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 729 | * It's now possible to create weak references to old-style class |
| 730 | objects. New-style classes were always weak-referenceable. (Fixed |
| 731 | by Antoine Pitrou; :issue:`8268`.) |
| 732 | |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 733 | * When a module object is garbage-collected, the module's dictionary is |
| 734 | now only cleared if no one else is holding a reference to the |
| 735 | dictionary (:issue:`7140`). |
| 736 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 737 | .. ====================================================================== |
| 738 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 739 | .. _new-27-interpreter: |
| 740 | |
| 741 | Interpreter Changes |
| 742 | ------------------------------- |
| 743 | |
| 744 | A new environment variable, :envvar:`PYTHONWARNINGS`, |
| 745 | allows controlling warnings. It should be set to a string |
| 746 | containing warning settings, equivalent to those |
| 747 | used with the :option:`-W` switch, separated by commas. |
| 748 | (Contributed by Brian Curtin; :issue:`7301`.) |
| 749 | |
| 750 | For example, the following setting will print warnings every time |
| 751 | they occur, but turn warnings from the :mod:`Cookie` module into an |
| 752 | error. (The exact syntax for setting an environment variable varies |
| 753 | across operating systems and shells, so it may be different for you.) |
| 754 | |
| 755 | :: |
| 756 | |
| 757 | export PYTHONWARNINGS=all,error:::Cookie:0 |
| 758 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 759 | When running a module using the interpreter's :option:`-m` switch, |
| 760 | ``sys.argv[0]`` will now be set to the string ``'-m'`` while the |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 761 | module is being located, while executing the :file:`__init__.py` files |
| 762 | for any parent packages of the module to be executed. |
| 763 | (Suggested by Michael Foord; implemented by Nick Coghlan; |
| 764 | :issue:`8202`.) |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 765 | |
| 766 | .. ====================================================================== |
| 767 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 768 | |
| 769 | Optimizations |
| 770 | ------------- |
| 771 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 772 | Several performance enhancements have been added: |
| 773 | |
| 774 | .. * A new :program:`configure` option, :option:`--with-computed-gotos`, |
| 775 | compiles the main bytecode interpreter loop using a new dispatch |
| 776 | mechanism that gives speedups of up to 20%, depending on the system |
| 777 | and benchmark. The new mechanism is only supported on certain |
| 778 | compilers, such as gcc, SunPro, and icc. |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 779 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 780 | * A new opcode was added to perform the initial setup for |
| 781 | :keyword:`with` statements, looking up the :meth:`__enter__` and |
| 782 | :meth:`__exit__` methods. (Contributed by Benjamin Peterson.) |
| 783 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 784 | * The garbage collector now performs better for one common usage |
| 785 | pattern: when many objects are being allocated without deallocating |
| 786 | any of them. This would previously take quadratic |
| 787 | time for garbage collection, but now the number of full garbage collections |
| 788 | is reduced as the number of objects on the heap grows. |
| 789 | The new logic is to only perform a full garbage collection pass when |
| 790 | the middle generation has been collected 10 times and when the |
| 791 | number of survivor objects from the middle generation exceeds 10% of |
| 792 | the number of objects in the oldest generation. (Suggested by Martin |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 793 | von Löwis and implemented by Antoine Pitrou; :issue:`4074`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 794 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 795 | * The garbage collector tries to avoid tracking simple containers |
| 796 | which can't be part of a cycle. In Python 2.7, this is now true for |
| 797 | tuples and dicts containing atomic types (such as ints, strings, |
| 798 | etc.). Transitively, a dict containing tuples of atomic types won't |
| 799 | be tracked either. This helps reduce the cost of each |
| 800 | garbage collection by decreasing the number of objects to be |
| 801 | considered and traversed by the collector. |
Antoine Pitrou | c18f6b0 | 2009-03-28 19:10:13 +0000 | [diff] [blame] | 802 | (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 803 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 804 | * 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] | 805 | 2**30, the base being determined at build time. Previously, they |
| 806 | were always stored in base 2**15. Using base 2**30 gives |
| 807 | significant performance improvements on 64-bit machines, but |
| 808 | benchmark results on 32-bit machines have been mixed. Therefore, |
| 809 | the default is to use base 2**30 on 64-bit machines and base 2**15 |
| 810 | on 32-bit machines; on Unix, there's a new configure option |
| 811 | :option:`--enable-big-digits` that can be used to override this default. |
| 812 | |
| 813 | Apart from the performance improvements this change should be |
| 814 | invisible to end users, with one exception: for testing and |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 815 | 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] | 816 | provides information about the internal format, giving the number of |
| 817 | bits per digit and the size in bytes of the C type used to store |
| 818 | each digit:: |
| 819 | |
| 820 | >>> import sys |
| 821 | >>> sys.long_info |
| 822 | sys.long_info(bits_per_digit=30, sizeof_digit=4) |
| 823 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 824 | (Contributed by Mark Dickinson; :issue:`4258`.) |
| 825 | |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 826 | 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] | 827 | smaller on 32-bit systems and 6 bytes on 64-bit. |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 828 | (Contributed by Mark Dickinson; :issue:`5260`.) |
| 829 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 830 | * The division algorithm for long integers has been made faster |
| 831 | by tightening the inner loop, doing shifts instead of multiplications, |
| 832 | and fixing an unnecessary extra iteration. |
| 833 | Various benchmarks show speedups of between 50% and 150% for long |
| 834 | integer divisions and modulo operations. |
| 835 | (Contributed by Mark Dickinson; :issue:`5512`.) |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 836 | Bitwise operations are also significantly faster (initial patch by |
| 837 | Gregory Smith; :issue:`1087418`). |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 838 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 839 | * The implementation of ``%`` checks for the left-side operand being |
| 840 | a Python string and special-cases it; this results in a 1-3% |
| 841 | performance increase for applications that frequently use ``%`` |
| 842 | with strings, such as templating libraries. |
| 843 | (Implemented by Collin Winter; :issue:`5176`.) |
| 844 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 845 | * List comprehensions with an ``if`` condition are compiled into |
| 846 | faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7 |
| 847 | by Jeffrey Yasskin; :issue:`4715`.) |
| 848 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 849 | * Converting an integer or long integer to a decimal string was made |
| 850 | faster by special-casing base 10 instead of using a generalized |
| 851 | conversion function that supports arbitrary bases. |
| 852 | (Patch by Gawain Bolton; :issue:`6713`.) |
| 853 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 854 | * The :meth:`split`, :meth:`replace`, :meth:`rindex`, |
| 855 | :meth:`rpartition`, and :meth:`rsplit` methods of string-like types |
| 856 | (strings, Unicode strings, and :class:`bytearray` objects) now use a |
| 857 | fast reverse-search algorithm instead of a character-by-character |
| 858 | scan. This is sometimes faster by a factor of 10. (Added by |
| 859 | Florent Xicluna; :issue:`7462` and :issue:`7622`.) |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 860 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 861 | * The :mod:`pickle` and :mod:`cPickle` modules now automatically |
| 862 | intern the strings used for attribute names, reducing memory usage |
| 863 | of the objects resulting from unpickling. (Contributed by Jake |
| 864 | McGuire; :issue:`5084`.) |
| 865 | |
| 866 | * The :mod:`cPickle` module now special-cases dictionaries, |
| 867 | nearly halving the time required to pickle them. |
| 868 | (Contributed by Collin Winter; :issue:`5670`.) |
| 869 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 870 | .. ====================================================================== |
| 871 | |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 872 | New and Improved Modules |
| 873 | ======================== |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 874 | |
| 875 | As in every release, Python's standard library received a number of |
| 876 | enhancements and bug fixes. Here's a partial list of the most notable |
| 877 | changes, sorted alphabetically by module name. Consult the |
| 878 | :file:`Misc/NEWS` file in the source tree for a more complete list of |
| 879 | changes, or look through the Subversion logs for all the details. |
| 880 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 881 | * The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 882 | gained a feature for skipping modules. The constructor |
| 883 | now takes an iterable containing glob-style patterns such as |
| 884 | ``django.*``; the debugger will not step into stack frames |
| 885 | from a module that matches one of these patterns. |
| 886 | (Contributed by Maru Newby after a suggestion by |
| 887 | Senthil Kumaran; :issue:`5142`.) |
| 888 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 889 | * The :mod:`binascii` module now supports the buffer API, so it can be |
| 890 | used with :class:`memoryview` instances and other similar buffer objects. |
| 891 | (Backported from 3.x by Florent Xicluna; :issue:`7703`.) |
| 892 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 893 | * Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9 |
| 894 | to version 4.8.4 of |
| 895 | `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__. |
| 896 | The new version features better Python 3.x compatibility, various bug fixes, |
| 897 | and adds several new BerkeleyDB flags and methods. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 898 | (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 899 | changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.) |
| 900 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 901 | * 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] | 902 | management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``. |
| 903 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
| 904 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 905 | * New class: the :class:`~collections.Counter` class in the :mod:`collections` |
| 906 | module is useful for tallying data. :class:`~collections.Counter` instances |
| 907 | behave mostly like dictionaries but return zero for missing keys instead of |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 908 | raising a :exc:`KeyError`: |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 909 | |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 910 | .. doctest:: |
| 911 | :options: +NORMALIZE_WHITESPACE |
| 912 | |
| 913 | >>> from collections import Counter |
| 914 | >>> c = Counter() |
| 915 | >>> for letter in 'here is a sample of english text': |
| 916 | ... c[letter] += 1 |
| 917 | ... |
| 918 | >>> c |
| 919 | Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2, |
| 920 | 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1, |
| 921 | 'p': 1, 'r': 1, 'x': 1}) |
| 922 | >>> c['e'] |
| 923 | 5 |
| 924 | >>> c['z'] |
| 925 | 0 |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 926 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 927 | There are three additional :class:`~collections.Counter` methods: |
| 928 | :meth:`~collections.Counter.most_common` returns the N most common |
| 929 | elements and their counts. :meth:`~collections.Counter.elements` |
| 930 | returns an iterator over the contained elements, repeating each |
| 931 | element as many times as its count. |
| 932 | :meth:`~collections.Counter.subtract` takes an iterable and |
| 933 | subtracts one for each element instead of adding; if the argument is |
| 934 | a dictionary or another :class:`Counter`, the counts are |
| 935 | subtracted. :: |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 936 | |
| 937 | >>> c.most_common(5) |
| 938 | [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)] |
| 939 | >>> c.elements() -> |
| 940 | 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ', |
| 941 | 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i', |
| 942 | 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's', |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 943 | 's', 's', 'r', 't', 't', 'x' |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 944 | >>> c['e'] |
| 945 | 5 |
| 946 | >>> c.subtract('very heavy on the letter e') |
| 947 | >>> c['e'] # Count is now lower |
| 948 | -1 |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 949 | |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 950 | Contributed by Raymond Hettinger; :issue:`1696199`. |
| 951 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 952 | .. revision 79660 |
| 953 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 954 | The new :class:`~collections.OrderedDict` class is described in the earlier |
| 955 | section :ref:`pep-0372`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 956 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 957 | The :class:`~collections.namedtuple` class now has an optional *rename* parameter. |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 958 | 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] | 959 | been repeated or that aren't legal Python identifiers will be |
| 960 | renamed to legal names that are derived from the field's |
| 961 | position within the list of fields: |
| 962 | |
Georg Brandl | f6dab95 | 2009-04-28 21:48:35 +0000 | [diff] [blame] | 963 | >>> from collections import namedtuple |
| 964 | >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True) |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 965 | >>> T._fields |
| 966 | ('field1', '_1', '_2', 'field2') |
| 967 | |
| 968 | (Added by Raymond Hettinger; :issue:`1818`.) |
| 969 | |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 970 | The :class:`~collections.deque` data type now has a |
| 971 | :meth:`~collections.deque.count` method that returns the number of |
| 972 | contained elements equal to the supplied argument *x*, and a |
| 973 | :meth:`~collections.deque.reverse` method that reverses the elements |
| 974 | of the deque in-place. :class:`deque` also exposes its maximum |
| 975 | length as the read-only :attr:`~collections.deque.maxlen` attribute. |
| 976 | (Both features added by Raymond Hettinger.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 977 | |
Andrew M. Kuchling | 02f7b99 | 2010-05-07 01:45:14 +0000 | [diff] [blame] | 978 | * Constructors for the parsing classes in the :mod:`ConfigParser` module now |
| 979 | take a *allow_no_value* parameter, defaulting to false; if true, |
| 980 | options without values will be allowed. For example:: |
| 981 | |
| 982 | >>> import ConfigParser, StringIO |
| 983 | >>> sample_config = """ |
| 984 | ... [mysqld] |
| 985 | ... user = mysql |
| 986 | ... pid-file = /var/run/mysqld/mysqld.pid |
| 987 | ... skip-bdb |
| 988 | ... """ |
| 989 | >>> config = ConfigParser.RawConfigParser(allow_no_value=True) |
| 990 | >>> config.readfp(StringIO.StringIO(sample_config)) |
| 991 | >>> config.get('mysqld', 'user') |
| 992 | 'mysql' |
| 993 | >>> print config.get('mysqld', 'skip-bdb') |
| 994 | None |
| 995 | >>> print config.get('mysqld', 'unknown') |
| 996 | Traceback (most recent call last): |
| 997 | ... |
| 998 | ConfigParser.NoOptionError: No option 'unknown' in section: 'mysqld' |
| 999 | |
| 1000 | (Contributed by Mats Kindahl; :issue:`7005`.) |
| 1001 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1002 | * Deprecated function: :func:`contextlib.nested`, which allows |
| 1003 | handling more than one context manager with a single :keyword:`with` |
| 1004 | statement, has been deprecated, because :keyword:`with` supports |
| 1005 | multiple context managers syntactically now. |
| 1006 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1007 | * The :mod:`copy` module's :func:`~copy.deepcopy` function will now |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1008 | correctly copy bound instance methods. (Implemented by |
| 1009 | Robert Collins; :issue:`1515`.) |
| 1010 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1011 | * The :mod:`ctypes` module now always converts ``None`` to a C NULL |
| 1012 | pointer for arguments declared as pointers. (Changed by Thomas |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1013 | Heller; :issue:`4606`.) The underlying `libffi library |
| 1014 | <http://sourceware.org/libffi/>`__ has been updated to version |
| 1015 | 3.0.9, containing various fixes for different platforms. (Updated |
| 1016 | by Matthias Klose; :issue:`8142`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1017 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1018 | * New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class |
| 1019 | gained a :meth:`~datetime.timedelta.total_seconds` method that returns the |
| 1020 | 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] | 1021 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1022 | * New method: the :class:`~decimal.Decimal` class gained a |
| 1023 | :meth:`~decimal.Decimal.from_float` class method that performs an exact |
| 1024 | conversion of a floating-point number to a :class:`~decimal.Decimal`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1025 | Note that this is an **exact** conversion that strives for the |
| 1026 | closest decimal approximation to the floating-point representation's value; |
| 1027 | the resulting decimal value will therefore still include the inaccuracy, |
| 1028 | if any. |
| 1029 | For example, ``Decimal.from_float(0.1)`` returns |
| 1030 | ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``. |
| 1031 | (Implemented by Raymond Hettinger; :issue:`4796`.) |
| 1032 | |
Andrew M. Kuchling | 04b99cc | 2010-05-04 01:24:22 +0000 | [diff] [blame] | 1033 | Comparing instances of :class:`Decimal` with floating-point |
| 1034 | numbers now produces sensible results based on the numeric values |
| 1035 | of the operands. Previously such comparisons would fall back to |
| 1036 | Python's default rules for comparing objects, which produced arbitrary |
| 1037 | results based on their type. Note that you still cannot combine |
| 1038 | :class:`Decimal` and floating-point in other operations such as addition, |
| 1039 | since you should be explicitly choosing how to convert between float and |
| 1040 | :class:`Decimal`. |
| 1041 | (Fixed by Mark Dickinson; :issue:`2531`.) |
| 1042 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1043 | Most of the methods of the :class:`~decimal.Context` class now accept integers |
| 1044 | as well as :class:`~decimal.Decimal` instances; the only exceptions are the |
| 1045 | :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical` |
| 1046 | methods. (Patch by Juan José Conti; :issue:`7633`.) |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1047 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1048 | The constructor for :class:`~decimal.Decimal` now accepts |
| 1049 | floating-point numbers (added by Raymond Hettinger; :issue:`8257`) |
| 1050 | and non-European Unicode characters such as Arabic-Indic digits |
| 1051 | (contributed by Mark Dickinson; :issue:`6595`). |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1052 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1053 | When using :class:`~decimal.Decimal` instances with a string's |
| 1054 | :meth:`~str.format` method, the default alignment was previously |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1055 | left-alignment. This has been changed to right-alignment, which seems |
| 1056 | more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.) |
| 1057 | |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1058 | Comparisons involving a signaling NaN value (or ``sNAN``) now signal |
| 1059 | :const:`InvalidOperation` instead of silently returning a true or |
| 1060 | false value depending on the comparison operator. Quiet NaN values |
| 1061 | (or ``NaN``) are now hashable. (Fixed by Mark Dickinson; |
| 1062 | :issue:`7279`.) |
| 1063 | |
Andrew M. Kuchling | 363dbcc | 2010-04-14 23:55:17 +0000 | [diff] [blame] | 1064 | * The :mod:`difflib` module now produces output that is more |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 1065 | compatible with modern :command:`diff`/:command:`patch` tools |
| 1066 | through one small change, using a tab character instead of spaces as |
| 1067 | a separator in the header giving the filename. (Fixed by Anatoly |
| 1068 | Techtonik; :issue:`7585`.) |
| 1069 | |
| 1070 | * The :mod:`doctest` module's :const:`IGNORE_EXCEPTION_DETAIL` flag |
| 1071 | will now ignore the name of the module containing the exception |
| 1072 | being tested. (Patch by Lennart Regebro; :issue:`7490`.) |
Andrew M. Kuchling | 363dbcc | 2010-04-14 23:55:17 +0000 | [diff] [blame] | 1073 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1074 | * The :class:`~fractions.Fraction` class now accepts a single float or |
| 1075 | :class:`~decimal.Decimal` instance, or two rational numbers, as |
| 1076 | arguments to its constructor. (Implemented by Mark Dickinson; |
| 1077 | rationals added in :issue:`5812`, and float/decimal in |
| 1078 | :issue:`8294`.) |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 1079 | |
Andrew M. Kuchling | ec6393f | 2010-04-11 01:40:30 +0000 | [diff] [blame] | 1080 | An oversight was fixed, making the :class:`Fraction` match the other |
| 1081 | numeric types; ordering comparisons (``<``, ``<=``, ``>``, ``>=``) between |
| 1082 | fractions and complex numbers now raise a :exc:`TypeError`. |
| 1083 | |
| 1084 | .. revision 79455 |
| 1085 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1086 | * New class: a new :class:`~ftplib.FTP_TLS` class in |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1087 | the :mod:`ftplib` module provides secure FTP |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1088 | connections using TLS encapsulation of authentication as well as |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1089 | subsequent control and data transfers. |
| 1090 | (Contributed by Giampaolo Rodola', :issue:`2054`.) |
| 1091 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1092 | 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] | 1093 | uploads thanks to an added *rest* parameter (patch by Pablo Mouzo; |
| 1094 | :issue:`6845`.) |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1095 | |
Andrew M. Kuchling | 6d7dfa2 | 2010-04-11 12:49:37 +0000 | [diff] [blame] | 1096 | * New class decorator: :func:`total_ordering` in the :mod:`functools` |
| 1097 | module takes a class that defines an :meth:`__eq__` method and one of |
| 1098 | :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`, |
| 1099 | and generates the missing comparison methods. Since the |
| 1100 | :meth:`__cmp__` method is being deprecated in Python 3.x, |
| 1101 | this decorator makes it easier to define ordered classes. |
| 1102 | (Added by Raymond Hettinger; :issue:`5479`.) |
| 1103 | |
| 1104 | New function: :func:`cmp_to_key` will take an old-style comparison |
| 1105 | function that expects two arguments and return a new callable that |
| 1106 | can be used as the *key* parameter to functions such as |
| 1107 | :func:`sorted`, :func:`min` and :func:`max`, etc. The primary |
| 1108 | intended use is to help with making code compatible with Python 3.x. |
| 1109 | (Added by Raymond Hettinger.) |
| 1110 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1111 | * 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] | 1112 | 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] | 1113 | otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 1114 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1115 | * 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] | 1116 | management protocol, so you can write ``with gzip.GzipFile(...) as f: ...`` |
| 1117 | (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements |
| 1118 | the :class:`io.BufferedIOBase` ABC, so you can wrap it with |
| 1119 | :class:`io.BufferedReader` for faster processing |
| 1120 | (contributed by Nir Aides; :issue:`7471`). |
| 1121 | It's also now possible to override the modification time |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1122 | recorded in a gzipped file by providing an optional timestamp to |
| 1123 | the constructor. (Contributed by Jacques Frechet; :issue:`4272`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1124 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1125 | Files in gzip format can be padded with trailing zero bytes; the |
| 1126 | :mod:`gzip` module will now consume these trailing bytes. (Fixed by |
| 1127 | Tadek Pietraszek and Brian Curtin; :issue:`2846`.) |
| 1128 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1129 | * 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] | 1130 | attribute containing a tuple naming the supported algorithms. |
| 1131 | In Python 2.7, ``hashlib.algorithms`` contains |
| 1132 | ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')`` |
| 1133 | (Contributed by Carl Chenet; :issue:`7418`.) |
| 1134 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1135 | * 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] | 1136 | supports buffering, resulting in much faster reading of HTTP responses. |
| 1137 | (Contributed by Kristjan Valur Jonsson; :issue:`4879`.) |
| 1138 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1139 | The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1140 | now support a *source_address* parameter, a ``(host, port)`` 2-tuple |
| 1141 | giving the source address that will be used for the connection. |
| 1142 | (Contributed by Eldon Ziegler; :issue:`3972`.) |
| 1143 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 1144 | * The :mod:`ihooks` module now supports relative imports. Note that |
| 1145 | :mod:`ihooks` is an older module used to support customizing imports, |
| 1146 | superseded by the :mod:`imputil` module added in Python 2.0. |
| 1147 | (Relative import support added by Neil Schemenauer.) |
| 1148 | |
| 1149 | .. revision 75423 |
| 1150 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1151 | * The :mod:`imaplib` module now supports IPv6 addresses. |
| 1152 | (Contributed by Derek Morr; :issue:`1655`.) |
| 1153 | |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 1154 | * New function: the :mod:`inspect` module's :func:`~inspect.getcallargs` |
| 1155 | takes a callable and its positional and keyword arguments, |
| 1156 | and figures out which of the callable's parameters will receive each argument, |
| 1157 | returning a dictionary mapping argument names to their values. For example:: |
| 1158 | |
| 1159 | >>> from inspect import getcallargs |
| 1160 | >>> def f(a, b=1, *pos, **named): |
| 1161 | ... pass |
| 1162 | >>> getcallargs(f, 1, 2, 3) |
| 1163 | {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} |
| 1164 | >>> getcallargs(f, a=2, x=4) |
| 1165 | {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} |
| 1166 | >>> getcallargs(f) |
| 1167 | Traceback (most recent call last): |
| 1168 | ... |
| 1169 | TypeError: f() takes at least 1 argument (0 given) |
| 1170 | |
| 1171 | Contributed by George Sakkis; :issue:`3135`. |
| 1172 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1173 | * 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] | 1174 | 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] | 1175 | 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] | 1176 | original Python version was renamed to the :mod:`_pyio` module. |
| 1177 | |
| 1178 | One minor resulting change: the :class:`io.TextIOBase` class now |
| 1179 | has an :attr:`errors` attribute giving the error setting |
| 1180 | used for encoding and decoding errors (one of ``'strict'``, ``'replace'``, |
| 1181 | ``'ignore'``). |
| 1182 | |
| 1183 | 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] | 1184 | an invalid file descriptor. (Implemented by Benjamin Peterson; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1185 | :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1186 | file position; previously it would change the file position to the |
| 1187 | end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.) |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1188 | |
Andrew M. Kuchling | 5a73ff8 | 2009-12-02 14:27:11 +0000 | [diff] [blame] | 1189 | * New function: ``itertools.compress(data, selectors)`` takes two |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1190 | iterators. Elements of *data* are returned if the corresponding |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1191 | value in *selectors* is true:: |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1192 | |
| 1193 | itertools.compress('ABCDEF', [1,0,1,0,1,1]) => |
| 1194 | A, C, E, F |
| 1195 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1196 | .. maybe here is better to use >>> list(itertools.compress(...)) instead |
| 1197 | |
Andrew M. Kuchling | 5a73ff8 | 2009-12-02 14:27:11 +0000 | [diff] [blame] | 1198 | New function: ``itertools.combinations_with_replacement(iter, r)`` |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1199 | returns all the possible *r*-length combinations of elements from the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1200 | iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1201 | can be repeated in the generated combinations:: |
| 1202 | |
| 1203 | itertools.combinations_with_replacement('abc', 2) => |
| 1204 | ('a', 'a'), ('a', 'b'), ('a', 'c'), |
| 1205 | ('b', 'b'), ('b', 'c'), ('c', 'c') |
| 1206 | |
| 1207 | Note that elements are treated as unique depending on their position |
| 1208 | in the input, not their actual values. |
| 1209 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1210 | The :func:`itertools.count` function now has a *step* argument that |
| 1211 | allows incrementing by values other than 1. :func:`~itertools.count` also |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1212 | now allows keyword arguments, and using non-integer values such as |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1213 | floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1214 | Hettinger; :issue:`5032`.) |
| 1215 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 1216 | :func:`itertools.combinations` and :func:`itertools.product` were |
| 1217 | previously raising :exc:`ValueError` for values of *r* larger than |
| 1218 | the input iterable. This was deemed a specification error, so they |
| 1219 | now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.) |
| 1220 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1221 | * 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] | 1222 | simplejson package, which includes a C extension that makes |
| 1223 | encoding and decoding faster. |
| 1224 | (Contributed by Bob Ippolito; :issue:`4136`.) |
| 1225 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1226 | To support the new :class:`collections.OrderedDict` type, :func:`json.load` |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1227 | now has an optional *object_pairs_hook* parameter that will be called |
| 1228 | with any object literal that decodes to a list of pairs. |
| 1229 | (Contributed by Raymond Hettinger; :issue:`5381`.) |
| 1230 | |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 1231 | * The :mod:`mailbox` module's :class:`Maildir` class now records the |
| 1232 | timestamp on the directories it reads, and only re-reads them if the |
| 1233 | modification time has subsequently changed. This improves |
| 1234 | performance by avoiding unneeded directory scans. (Fixed by |
| 1235 | A.M. Kuchling and Antoine Pitrou; :issue:`1607951`, :issue:`6896`.) |
| 1236 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1237 | * New functions: the :mod:`math` module gained |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1238 | :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function, |
| 1239 | :func:`~math.expm1` which computes ``e**x - 1`` with more precision than |
| 1240 | using :func:`~math.exp` and subtracting 1, |
| 1241 | :func:`~math.gamma` for the Gamma function, and |
| 1242 | :func:`~math.lgamma` for the natural log of the Gamma function. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1243 | (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.) |
| 1244 | |
Andrew M. Kuchling | 24520b4 | 2009-04-09 11:22:47 +0000 | [diff] [blame] | 1245 | * The :mod:`multiprocessing` module's :class:`Manager*` classes |
| 1246 | can now be passed a callable that will be called whenever |
| 1247 | a subprocess is started, along with a set of arguments that will be |
| 1248 | passed to the callable. |
| 1249 | (Contributed by lekma; :issue:`5585`.) |
| 1250 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1251 | 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] | 1252 | now has an optional *maxtasksperchild* parameter. Worker processes |
| 1253 | will perform the specified number of tasks and then exit, causing the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1254 | :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] | 1255 | memory or other resources, or if some tasks will cause the worker to |
| 1256 | become very large. |
| 1257 | (Contributed by Charles Cazabon; :issue:`6963`.) |
| 1258 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1259 | * The :mod:`nntplib` module now supports IPv6 addresses. |
| 1260 | (Contributed by Derek Morr; :issue:`1664`.) |
| 1261 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1262 | * New functions: the :mod:`os` module wraps the following POSIX system |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1263 | calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1264 | real, effective, and saved GIDs and UIDs; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1265 | :func:`~os.setresgid` and :func:`~os.setresuid`, which set |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1266 | real, effective, and saved GIDs and UIDs to new values; |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1267 | :func:`~os.initgroups`. (GID/UID functions |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1268 | contributed by Travis H.; :issue:`6508`. Support for initgroups added |
| 1269 | by Jean-Paul Calderone; :issue:`7333`.) |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1270 | |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1271 | The :func:`os.fork` function now re-initializes the import lock in |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1272 | 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] | 1273 | is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.) |
| 1274 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1275 | * In the :mod:`os.path` module, the :func:`~os.path.normpath` and |
| 1276 | :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] | 1277 | is a Unicode string, the return value is also a Unicode string. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1278 | (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`; |
| 1279 | :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.) |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1280 | |
Andrew M. Kuchling | 9cb4277 | 2009-01-21 02:15:43 +0000 | [diff] [blame] | 1281 | * The :mod:`pydoc` module now has help for the various symbols that Python |
| 1282 | uses. You can now do ``help('<<')`` or ``help('@')``, for example. |
| 1283 | (Contributed by David Laban; :issue:`4739`.) |
| 1284 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1285 | * 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] | 1286 | now accept an optional *flags* argument, for consistency with the |
| 1287 | other functions in the module. (Added by Gregory P. Smith.) |
| 1288 | |
Andrew M. Kuchling | ca48562 | 2010-05-06 01:10:56 +0000 | [diff] [blame] | 1289 | * New function: :func:`~runpy.run_path` in the :mod:`runpy` module |
| 1290 | will execute the code at a provided *path* argument. *path* can be |
| 1291 | the path of a Python source file (:file:`example.py`), a compiled |
| 1292 | bytecode file (:file:`example.pyc`), a directory |
| 1293 | (:file:`./package/'), or a zip archive (:file:`example.zip`). If a |
| 1294 | directory or zip path is provided, it will be added to the front of |
| 1295 | ``sys.path`` and the module :mod:`__main__` will be imported. It's |
| 1296 | expected that the directory or zip contains a :file:`__main__.py`; |
| 1297 | if it doesn't, some other :file:`__main__.py` might be imported from |
| 1298 | a location later in ``sys.path``. This makes some of the machinery |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 1299 | of :mod:`runpy` available to scripts that want to mimic the way |
| 1300 | Python's :option:`-m` processes an explicit path name. |
| 1301 | (Added by Nick Coghlan; :issue:`6816`.) |
Andrew M. Kuchling | ca48562 | 2010-05-06 01:10:56 +0000 | [diff] [blame] | 1302 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1303 | * New function: in the :mod:`shutil` module, :func:`~shutil.make_archive` |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1304 | takes a filename, archive type (zip or tar-format), and a directory |
| 1305 | path, and creates an archive containing the directory's contents. |
| 1306 | (Added by Tarek Ziadé.) |
| 1307 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1308 | :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree` |
| 1309 | functions now raise a :exc:`~shutil.SpecialFileError` exception when |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1310 | asked to copy a named pipe. Previously the code would treat |
| 1311 | named pipes like a regular file by opening them for reading, and |
| 1312 | this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.) |
| 1313 | |
| 1314 | * New functions: in the :mod:`site` module, three new functions |
| 1315 | return various site- and user-specific paths. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1316 | :func:`~site.getsitepackages` returns a list containing all |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1317 | global site-packages directories, and |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1318 | :func:`~site.getusersitepackages` returns the path of the user's |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1319 | site-packages directory. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1320 | :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1321 | environment variable, giving the path to a directory that can be used |
| 1322 | to store data. |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1323 | (Contributed by Tarek Ziadé; :issue:`6693`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1324 | |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1325 | The :mod:`site` module now reports exceptions occurring |
| 1326 | when the :mod:`sitecustomize` module is imported, and will no longer |
Florent Xicluna | ad59833 | 2010-03-31 21:40:32 +0000 | [diff] [blame] | 1327 | catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1328 | Victor Stinner; :issue:`3137`.) |
| 1329 | |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1330 | * The :func:`~socket.create_connection` function |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 1331 | gained a *source_address* parameter, a ``(host, port)`` 2-tuple |
| 1332 | giving the source address that will be used for the connection. |
| 1333 | (Contributed by Eldon Ziegler; :issue:`3972`.) |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1334 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1335 | The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into` |
| 1336 | 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] | 1337 | the :class:`bytearray` and :class:`memoryview` objects. (Implemented by |
| 1338 | Antoine Pitrou; :issue:`8104`.) |
| 1339 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1340 | * The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now |
| 1341 | has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1342 | The default value is False; if overridden to be True, |
| 1343 | new request connections will have the TCP_NODELAY option set to |
| 1344 | prevent buffering many small sends into a single TCP packet. |
| 1345 | (Contributed by Kristjan Valur Jonsson; :issue:`6192`.) |
| 1346 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1347 | * Updated module: the :mod:`sqlite3` module has been updated to |
Andrew M. Kuchling | fed1576 | 2010-03-08 12:00:39 +0000 | [diff] [blame] | 1348 | 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 |
| 1349 | the ability to load SQLite extensions from shared libraries. |
| 1350 | Call the ``enable_load_extension(True)`` method to enable extensions, |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1351 | 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] | 1352 | (Updated by Gerhard Häring.) |
| 1353 | |
Antoine Pitrou | d69e6ee | 2010-05-07 10:15:51 +0000 | [diff] [blame^] | 1354 | * The :mod:`ssl` module's :class:`ssl.SSLSocket` objects now support the |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1355 | buffer API, which fixed a test suite failure (fix by Antoine Pitrou; |
| 1356 | :issue:`7133`) and automatically set |
| 1357 | OpenSSL's :cmacro:`SSL_MODE_AUTO_RETRY`, which will prevent an error |
| 1358 | code being returned from :meth:`recv` operations that trigger an SSL |
| 1359 | renegotiation (fix by Antoine Pitrou; :issue:`8222`). |
| 1360 | |
Antoine Pitrou | d69e6ee | 2010-05-07 10:15:51 +0000 | [diff] [blame^] | 1361 | The :func:`ssl.wrap_socket` constructor function now takes a |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1362 | *ciphers* argument that's a string listing the encryption algorithms |
| 1363 | to be allowed; the format of the string is described |
Antoine Pitrou | d69e6ee | 2010-05-07 10:15:51 +0000 | [diff] [blame^] | 1364 | `in the OpenSSL documentation |
| 1365 | <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`__. |
| 1366 | (Added by Antoine Pitrou; :issue:`8322`.) |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1367 | |
| 1368 | Another change makes the extension load all of OpenSSL's ciphers and |
| 1369 | digest algorithms so that they're all available. Some SSL |
| 1370 | certificates couldn't be verified, reporting an 'unknown algorithm' |
| 1371 | error. (Reported by Beda Kosata, and fixed by Antoine Pitrou; |
| 1372 | :issue:`8484`.) |
| 1373 | |
| 1374 | The version of OpenSSL being used is now available as the module |
Antoine Pitrou | d69e6ee | 2010-05-07 10:15:51 +0000 | [diff] [blame^] | 1375 | attributes :data:`ssl.OPENSSL_VERSION` (a string), |
| 1376 | :data:`ssl.OPENSSL_VERSION_INFO` (a 5-tuple), and |
| 1377 | :data:`ssl.OPENSSL_VERSION_NUMBER` (an integer). (Added by Antoine |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1378 | Pitrou; :issue:`8321`.) |
| 1379 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1380 | * The :mod:`struct` module will no longer silently ignore overflow |
| 1381 | errors when a value is too large for a particular integer format |
| 1382 | code (one of ``bBhHiIlLqQ``); it now always raises a |
| 1383 | :exc:`struct.error` exception. (Changed by Mark Dickinson; |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1384 | :issue:`1523`.) The :func:`~struct.pack` function will also |
| 1385 | attempt to use :meth:`__index__` to convert and pack non-integers |
| 1386 | before trying the :meth:`__int__` method or reporting an error. |
| 1387 | (Changed by Mark Dickinson; :issue:`8300`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1388 | |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1389 | * New function: the :mod:`subprocess` module's |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1390 | :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] | 1391 | 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] | 1392 | error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise. |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 1393 | |
| 1394 | :: |
| 1395 | |
| 1396 | >>> subprocess.check_output(['df', '-h', '.']) |
| 1397 | 'Filesystem Size Used Avail Capacity Mounted on\n |
| 1398 | /dev/disk0s2 52G 49G 3.0G 94% /\n' |
| 1399 | |
| 1400 | >>> subprocess.check_output(['df', '-h', '/bogus']) |
| 1401 | ... |
| 1402 | subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1 |
| 1403 | |
| 1404 | (Contributed by Gregory P. Smith.) |
| 1405 | |
Andrew M. Kuchling | f91a679 | 2010-03-24 18:07:43 +0000 | [diff] [blame] | 1406 | The :mod:`subprocess` module will now retry its internal system calls |
| 1407 | on receiving an :const:`EINTR` signal. (Reported by several people; final |
| 1408 | patch by Gregory P. Smith in :issue:`1068268`.) |
| 1409 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1410 | * 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] | 1411 | returns true for variables that are explicitly declared to be global, |
| 1412 | false for ones that are implicitly global. |
| 1413 | (Contributed by Jeremy Hylton.) |
| 1414 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 1415 | * The :mod:`syslog` module will now use the value of ``sys.argv[0]`` as the |
| 1416 | identifier instead of the previous default value of ``'python'``. |
| 1417 | (Changed by Sean Reifschneider; :issue:`8451`.) |
| 1418 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1419 | * 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] | 1420 | named :attr:`major`, :attr:`minor`, :attr:`micro`, |
| 1421 | :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross |
| 1422 | Light; :issue:`4285`.) |
| 1423 | |
| 1424 | :func:`sys.getwindowsversion` also returns a named tuple, |
Andrew M. Kuchling | 9e483ef | 2010-02-08 01:35:35 +0000 | [diff] [blame] | 1425 | with attributes named :attr:`major`, :attr:`minor`, :attr:`build`, |
Ezio Melotti | 1247775 | 2010-02-08 22:22:41 +0000 | [diff] [blame] | 1426 | :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`, |
Eric Smith | b3c5488 | 2010-02-03 14:17:50 +0000 | [diff] [blame] | 1427 | :attr:`service_pack_minor`, :attr:`suite_mask`, and |
| 1428 | :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.) |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1429 | |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1430 | * The :mod:`tarfile` module's default error handling has changed, to |
| 1431 | no longer suppress fatal errors. The default error level was previously 0, |
| 1432 | which meant that errors would only result in a message being written to the |
| 1433 | debug log, but because the debug log is not activated by default, |
| 1434 | these errors go unnoticed. The default error level is now 1, |
| 1435 | which raises an exception if there's an error. |
| 1436 | (Changed by Lars Gustäbel; :issue:`7357`.) |
| 1437 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1438 | :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo` |
| 1439 | 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] | 1440 | instance, you may supply an optional *filter* argument |
| 1441 | that's a callable. The *filter* callable will be passed the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1442 | :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] | 1443 | If the callable returns ``None``, the file will be excluded from the |
| 1444 | resulting archive. This is more powerful than the existing |
| 1445 | *exclude* argument, which has therefore been deprecated. |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1446 | (Added by Lars Gustäbel; :issue:`6856`.) |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1447 | 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] | 1448 | (Added by Lars Gustäbel; :issue:`7232`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1449 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1450 | * The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class |
| 1451 | now returns the internal flag on exit. This means the method will usually |
| 1452 | 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] | 1453 | internal flag becomes true. The return value will only be false if |
| 1454 | a timeout was provided and the operation timed out. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1455 | (Contributed by Tim Lesher; :issue:`1674032`.) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1456 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1457 | * The Unicode database provided by the :mod:`unicodedata` module is |
| 1458 | now used internally to determine which characters are numeric, |
| 1459 | whitespace, or represent line breaks. The database also |
| 1460 | includes information from the :file:`Unihan.txt` data file (patch |
| 1461 | by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`) |
| 1462 | and has been updated to version 5.2.0 (updated by |
| 1463 | Florent Xicluna; :issue:`8024`). |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1464 | |
Andrew M. Kuchling | 04b99cc | 2010-05-04 01:24:22 +0000 | [diff] [blame] | 1465 | * The :mod:`urlparse` module's :func:`~urlparse.urlsplit` now handles |
| 1466 | unknown URL schemes in a fashion compliant with :rfc:`3986`: if the |
| 1467 | URL is of the form ``"<something>://..."``, the text before the |
| 1468 | ``://`` is treated as the scheme, even if it's a made-up scheme that |
| 1469 | the module doesn't know about. This change may break code that |
| 1470 | worked around the old behaviour. For example, Python 2.6.4 or 2.5 |
| 1471 | will return the following: |
| 1472 | |
| 1473 | >>> import urlparse |
| 1474 | >>> urlparse.urlsplit('invented://host/filename?query') |
| 1475 | ('invented', '', '//host/filename?query', '', '') |
| 1476 | |
| 1477 | Python 2.7 (and Python 2.6.5) will return: |
| 1478 | |
| 1479 | >>> import urlparse |
| 1480 | >>> urlparse.urlsplit('invented://host/filename?query') |
| 1481 | ('invented', 'host', '/filename?query', '', '') |
| 1482 | |
| 1483 | (Python 2.7 actually produces slightly different output, since it |
| 1484 | returns a named tuple instead of a standard tuple.) |
| 1485 | |
| 1486 | The :mod:`urlparse` module also supports IPv6 literal addresses as defined by |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1487 | :rfc:`2732` (contributed by Senthil Kumaran; :issue:`2987`). :: |
| 1488 | |
| 1489 | >>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo') |
| 1490 | ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]', |
| 1491 | path='/foo', params='', query='', fragment='') |
| 1492 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1493 | * 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] | 1494 | Benjamin Peterson.) |
| 1495 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 1496 | * New class: the :class:`~weakref.WeakSet` class in the :mod:`weakref` |
| 1497 | module is a set that only holds weak references to its elements; elements |
| 1498 | will be removed once there are no references pointing to them. |
| 1499 | (Originally implemented in Python 3.x by Raymond Hettinger, and backported |
| 1500 | to 2.7 by Michael Foord.) |
| 1501 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1502 | * The ElementTree library, :mod:`xml.etree`, no longer escapes |
| 1503 | ampersands and angle brackets when outputting an XML processing |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1504 | instruction (which looks like ``<?xml-stylesheet href="#style1"?>``) |
| 1505 | or comment (which looks like ``<!-- comment -->``). |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1506 | (Patch by Neil Muller; :issue:`2746`.) |
| 1507 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1508 | * 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] | 1509 | management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``. |
| 1510 | (Contributed by Brian Curtin; :issue:`5511`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1511 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1512 | :mod:`zipfile` now also supports archiving empty directories and |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1513 | extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.) |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1514 | Reading files out of an archive is faster, and interleaving |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1515 | :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] | 1516 | (Contributed by Nir Aides; :issue:`7610`.) |
Andrew M. Kuchling | 6c2633e | 2009-03-30 23:09:46 +0000 | [diff] [blame] | 1517 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1518 | The :func:`~zipfile.is_zipfile` function now |
Andrew M. Kuchling | 039c899 | 2010-02-01 02:04:26 +0000 | [diff] [blame] | 1519 | accepts a file object, in addition to the path names accepted in earlier |
| 1520 | versions. (Contributed by Gabriel Genellina; :issue:`4756`.) |
| 1521 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1522 | 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] | 1523 | that lets you override the default compression method specified in the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1524 | :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren; |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1525 | :issue:`6003`.) |
| 1526 | |
| 1527 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1528 | .. ====================================================================== |
| 1529 | .. whole new modules get described in subsections here |
| 1530 | |
| 1531 | |
| 1532 | .. _importlib-section: |
| 1533 | |
| 1534 | New module: importlib |
| 1535 | ------------------------------ |
| 1536 | |
| 1537 | Python 3.1 includes the :mod:`importlib` package, a re-implementation |
| 1538 | of the logic underlying Python's :keyword:`import` statement. |
| 1539 | :mod:`importlib` is useful for implementors of Python interpreters and |
| 1540 | to users who wish to write new importers that can participate in the |
| 1541 | import process. Python 2.7 doesn't contain the complete |
| 1542 | :mod:`importlib` package, but instead has a tiny subset that contains |
| 1543 | a single function, :func:`~importlib.import_module`. |
| 1544 | |
| 1545 | ``import_module(name, package=None)`` imports a module. *name* is |
| 1546 | a string containing the module or package's name. It's possible to do |
| 1547 | relative imports by providing a string that begins with a ``.`` |
| 1548 | character, such as ``..utils.errors``. For relative imports, the |
| 1549 | *package* argument must be provided and is the name of the package that |
| 1550 | will be used as the anchor for |
| 1551 | the relative import. :func:`~importlib.import_module` both inserts the imported |
| 1552 | module into ``sys.modules`` and returns the module object. |
| 1553 | |
| 1554 | Here are some examples:: |
| 1555 | |
| 1556 | >>> from importlib import import_module |
| 1557 | >>> anydbm = import_module('anydbm') # Standard absolute import |
| 1558 | >>> anydbm |
| 1559 | <module 'anydbm' from '/p/python/Lib/anydbm.py'> |
| 1560 | >>> # Relative import |
| 1561 | >>> sysconfig = import_module('..sysconfig', 'distutils.command') |
| 1562 | >>> sysconfig |
| 1563 | <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'> |
| 1564 | |
| 1565 | :mod:`importlib` was implemented by Brett Cannon and introduced in |
| 1566 | Python 3.1. |
| 1567 | |
| 1568 | |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1569 | New module: sysconfig |
| 1570 | --------------------------------- |
| 1571 | |
Andrew M. Kuchling | ca48562 | 2010-05-06 01:10:56 +0000 | [diff] [blame] | 1572 | The :mod:`sysconfig` module has been pulled out of the Distutils |
| 1573 | package, becoming a new top-level module in its own right. |
| 1574 | :mod:`sysconfig` provides functions for getting information about |
| 1575 | Python's build process: compiler switches, installation paths, the |
| 1576 | platform name, and whether Python is running from its source |
| 1577 | directory. |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1578 | |
Andrew M. Kuchling | ca48562 | 2010-05-06 01:10:56 +0000 | [diff] [blame] | 1579 | Some of the functions in the module are: |
| 1580 | |
| 1581 | * :func:`~sysconfig.get_config_var` returns variables from Python's |
| 1582 | Makefile and the :file:`pyconfig.h` file. |
| 1583 | * :func:`~sysconfig.get_config_vars` returns a dictionary containing |
| 1584 | all of the configuration variables. |
| 1585 | * :func:`~sysconfig.getpath` returns the configured path for |
| 1586 | a particular type of module: the standard library, |
| 1587 | site-specific modules, platform-specific modules, etc. |
| 1588 | * :func:`~sysconfig.is_python_build` returns true if you're running a |
| 1589 | binary from a Python source tree, and false otherwise. |
| 1590 | |
| 1591 | Consult the :mod:`sysconfig` documentation for more details and for |
| 1592 | a complete list of functions. |
| 1593 | |
| 1594 | The Distutils package and :mod:`sysconfig` are now maintained and |
| 1595 | renamed by Tarek Ziadé. |
| 1596 | |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 1597 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1598 | ttk: Themed Widgets for Tk |
| 1599 | -------------------------- |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1600 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1601 | Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk |
| 1602 | widgets but have a more customizable appearance and can therefore more |
| 1603 | closely resemble the native platform's widgets. This widget |
| 1604 | set was originally called Tile, but was renamed to Ttk (for "themed Tk") |
| 1605 | on being added to Tcl/Tck release 8.5. |
Andrew M. Kuchling | 46c2db5 | 2010-03-21 18:47:12 +0000 | [diff] [blame] | 1606 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1607 | XXX write a brief discussion and an example here. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1608 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1609 | The :mod:`ttk` module was written by Guilherme Polo and added in |
| 1610 | :issue:`2983`. An alternate version called ``Tile.py``, written by |
| 1611 | Martin Franklin and maintained by Kevin Walzer, was proposed for |
| 1612 | inclusion in :issue:`2618`, but the authors argued that Guilherme |
| 1613 | Polo's work was more comprehensive. |
| 1614 | |
| 1615 | |
| 1616 | .. _unittest-section: |
Tarek Ziadé | 2b21069 | 2010-02-02 23:39:40 +0000 | [diff] [blame] | 1617 | |
Andrew M. Kuchling | acab940 | 2010-05-06 17:27:57 +0000 | [diff] [blame] | 1618 | Updated module: unittest |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1619 | --------------------------------- |
| 1620 | |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1621 | The :mod:`unittest` module was greatly enhanced; many |
| 1622 | new features were added. Most of these features were implemented |
Andrew M. Kuchling | acab940 | 2010-05-06 17:27:57 +0000 | [diff] [blame] | 1623 | by Michael Foord, unless otherwise noted. The enhanced version of |
| 1624 | the module is downloadable separately for use with Python versions 2.4 to 2.6, |
| 1625 | packaged as the :mod:`unittest2` package, from |
| 1626 | http://pypi.python.org/pypi/unittest2. |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1627 | |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 1628 | When used from the command line, the module can automatically discover |
| 1629 | tests. It's not as fancy as `py.test <http://pytest.org>`__ or |
| 1630 | `nose <http://code.google.com/p/python-nose/>`__, but provides a simple way |
| 1631 | to run tests kept within a set of package directories. For example, |
| 1632 | the following command will search the :file:`test/` subdirectory for |
| 1633 | any importable test files named ``test*.py``:: |
| 1634 | |
| 1635 | python -m unittest discover -s test |
| 1636 | |
| 1637 | Consult the :mod:`unittest` module documentation for more details. |
| 1638 | (Developed in :issue:`6001`.) |
| 1639 | |
| 1640 | The :func:`main` function supports some other new options: |
| 1641 | |
| 1642 | * :option:`-b` or :option:`--buffer` will buffer the standard output |
| 1643 | and standard error streams during each test. If the test passes, |
| 1644 | any resulting output will be discard; on failure, the buffered |
| 1645 | output will be displayed. |
| 1646 | |
| 1647 | * :option:`-c` or :option:`--catch` will cause the control-C interrupt |
| 1648 | to be handled more gracefully. Instead of interrupting the test |
| 1649 | process immediately, the currently running test will be completed |
| 1650 | and then the resulting partial results will be reported. If you're |
| 1651 | impatient, a second press of control-C will cause an immediate |
| 1652 | interruption. |
| 1653 | |
| 1654 | This control-C handler tries to avoid interfering when the code |
| 1655 | being tested or the tests being run have defined a signal handler of |
| 1656 | their own, by noticing that a signal handler was already set and |
| 1657 | calling it. If this doesn't work for you, there's a |
| 1658 | :func:`removeHandler` decorator that can be used to mark tests that |
| 1659 | should have the control-C handling disabled. |
| 1660 | |
| 1661 | * :option:`-f` or :option:`--failfast` makes |
| 1662 | test execution stop immediately when a test fails instead of |
| 1663 | continuing to execute further tests. (Suggested by Cliff Dyer and |
| 1664 | implemented by Michael Foord; :issue:`8074`.) |
| 1665 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1666 | The progress messages now shows 'x' for expected failures |
| 1667 | and 'u' for unexpected successes when run in verbose mode. |
| 1668 | (Contributed by Benjamin Peterson.) |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1669 | |
| 1670 | Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a |
| 1671 | test. (:issue:`1034053`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1672 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1673 | The error messages for :meth:`~unittest.TestCase.assertEqual`, |
| 1674 | :meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse` |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1675 | failures now provide more information. If you set the |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1676 | :attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to |
| 1677 | True, both the standard error message and any additional message you |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1678 | provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.) |
| 1679 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1680 | The :meth:`~unittest.TestCase.assertRaises` method now |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1681 | return a context handler when called without providing a callable |
| 1682 | object to run. For example, you can write this:: |
| 1683 | |
| 1684 | with self.assertRaises(KeyError): |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1685 | {}['foo'] |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1686 | |
| 1687 | (Implemented by Antoine Pitrou; :issue:`4444`.) |
| 1688 | |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1689 | .. rev 78774 |
| 1690 | |
| 1691 | Module- and class-level setup and teardown fixtures are now supported. |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1692 | Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule` |
| 1693 | functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and |
| 1694 | :meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods |
| 1695 | (using ``@classmethod`` or equivalent). These functions and |
Andrew M. Kuchling | 4a0661b | 2010-03-25 01:35:51 +0000 | [diff] [blame] | 1696 | methods are invoked when the test runner switches to a test case in a |
| 1697 | different module or class. |
| 1698 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1699 | The methods :meth:`~unittest.TestCase.addCleanup` and |
| 1700 | :meth:`~unittest.TestCase.doCleanups` were added. |
| 1701 | :meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that |
| 1702 | will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if |
| 1703 | :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] | 1704 | for much simpler resource allocation and deallocation during tests |
| 1705 | (:issue:`5679`). |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1706 | |
| 1707 | A number of new methods were added that provide more specialized |
| 1708 | tests. Many of these methods were written by Google engineers |
| 1709 | for use in their test suites; Gregory P. Smith, Michael Foord, and |
| 1710 | GvR worked on merging them into Python's version of :mod:`unittest`. |
| 1711 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1712 | * :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1713 | expression and verify that the result is or is not ``None``. |
| 1714 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1715 | * :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot` |
| 1716 | 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] | 1717 | (Added by Michael Foord; :issue:`2578`.) |
| 1718 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1719 | * :meth:`~unittest.TestCase.assertIsInstance` and |
| 1720 | :meth:`~unittest.TestCase.assertNotIsInstance` check whether |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1721 | the resulting object is an instance of a particular class, or of |
| 1722 | one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.) |
| 1723 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1724 | * :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`, |
| 1725 | :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1726 | two quantities. |
| 1727 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1728 | * :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1729 | not equal, displays a helpful comparison that highlights the |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1730 | differences in the two strings. This comparison is now used by |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1731 | default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1732 | |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1733 | * :meth:`~unittest.TestCase.assertRegexpMatches` and |
| 1734 | :meth:`~unittest.TestCase.assertNotRegexpMatches` checks whether the |
| 1735 | first argument is a string matching or not matching the regular |
| 1736 | expression provided as the second argument (:issue:`8038`). |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1737 | |
| 1738 | * :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1739 | is raised, and then also checks that the string representation of |
| 1740 | the exception matches the provided regular expression. |
| 1741 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1742 | * :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn` |
| 1743 | tests whether *first* is or is not in *second*. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1744 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1745 | * :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1746 | contain the same elements. |
| 1747 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1748 | * :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1749 | only reports the differences between the sets in case of error. |
| 1750 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1751 | * Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual` |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1752 | compare the specified types and explain any differences without necessarily |
| 1753 | printing their full values; these methods are now used by default |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1754 | when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`. |
| 1755 | More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1756 | and can optionally check whether both sequences are of a |
| 1757 | particular type. |
| 1758 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1759 | * :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 1760 | differences; it's now used by default when you compare two dictionaries |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1761 | using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1762 | all of the key/value pairs in *first* are found in *second*. |
| 1763 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1764 | * :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1765 | whether *first* and *second* are approximately equal. This method |
| 1766 | can either round their difference to an optionally-specified number |
| 1767 | of *places* (the default is 7) and compare it to zero, or require |
| 1768 | the difference to be smaller than a supplied *delta* value. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1769 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1770 | * :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the |
| 1771 | :attr:`~unittest.TestLoader.suiteClass` attribute of |
| 1772 | the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1773 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1774 | * A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle |
| 1775 | 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] | 1776 | object and a function. The function will be used when both of the |
| 1777 | objects being compared are of the specified type. This function |
| 1778 | should compare the two objects and raise an exception if they don't |
| 1779 | match; it's a good idea for the function to provide additional |
| 1780 | information about why the two objects are matching, much as the new |
| 1781 | sequence comparison methods do. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1782 | |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1783 | :func:`unittest.main` now takes an optional ``exit`` argument. If |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1784 | 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] | 1785 | used from the interactive interpreter. (Contributed by J. Pablo |
| 1786 | Fernández; :issue:`3379`.) |
| 1787 | |
Ezio Melotti | 021f334 | 2010-04-06 03:26:49 +0000 | [diff] [blame] | 1788 | :class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and |
| 1789 | :meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before |
Andrew M. Kuchling | 9858f63 | 2010-03-23 18:39:24 +0000 | [diff] [blame] | 1790 | and after a test run. (Contributed by Robert Collins; :issue:`5728`.) |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1791 | |
| 1792 | With all these changes, the :file:`unittest.py` was becoming awkwardly |
| 1793 | large, so the module was turned into a package and the code split into |
| 1794 | several files (by Benjamin Peterson). This doesn't affect how the |
Andrew M. Kuchling | b2454b2 | 2010-04-29 01:45:41 +0000 | [diff] [blame] | 1795 | module is imported or used. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1796 | |
| 1797 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1798 | .. _elementtree-section: |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1799 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1800 | Updated module: ElementTree 1.3 |
| 1801 | --------------------------------- |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1802 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1803 | The version of the ElementTree library included with Python was updated to |
| 1804 | version 1.3. Some of the new features in ElementTree 1.3 are: |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1805 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1806 | * The various parsing functions now take a *parser* keyword argument |
| 1807 | that can be used to provide an :class:`XMLParser` instance that will |
| 1808 | be used. This makes it possible to override the file's internal encoding: |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1809 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1810 | p = ET.XMLParser(encoding='utf-8') |
| 1811 | t = ET.XML("""<root/>""", parser=p) |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1812 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1813 | Errors in parsing XML now raise a :exc:`ParseError` exception. |
| 1814 | Instances of :exc:`ParseError` have a :attr:`position` attribute |
| 1815 | containing a (*line*, *column*) tuple giving the location of the problem. |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1816 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1817 | * ElementTree's code for converting trees to a string has been |
| 1818 | significantly reworked, making it roughly twice as fast in many |
| 1819 | cases. The :class:`ElementTree` :meth:`write` and :class:`Element` |
| 1820 | :meth:`write` methods now have a *method* parameter that can be |
| 1821 | "xml" (the default), "html", or "text". HTML mode will output empty |
| 1822 | elements as ``<empty></empty>`` instead of ``<empty/>``, and text |
| 1823 | mode will skip over elements and only output the text chunks. If |
| 1824 | you set the :attr:`tag` attribute of an element to ``None`` but |
| 1825 | leaves its children in place, the element will be omitted when the |
| 1826 | tree is written out, so you don't need to do more extensive rearrangement |
| 1827 | to remove a single element. |
Andrew M. Kuchling | 2c130b6 | 2009-04-11 16:12:23 +0000 | [diff] [blame] | 1828 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1829 | Namespace aspects have also been improved. All the ``xmlns:<whatever>`` |
| 1830 | declarations are now put on the root element and not scattered throughout |
| 1831 | the resulting output. You can set the default namespace for a tree |
| 1832 | by setting the :attr:`default_namespace` attribute and can |
| 1833 | register new prefixes with :meth:`regsiter_namespace`. In XML mode, |
| 1834 | you can use the true/false *xml_declaration* parameter to suppress the |
| 1835 | XML declaration. |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 1836 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1837 | * New :class:`Element` method: :meth:`extend` appends the items from a |
| 1838 | sequence to the element's children. Elements themselves behave like |
| 1839 | sequences, so it's easy to move children from one element to |
| 1840 | another:: |
Andrew M. Kuchling | a17cd4a | 2009-01-31 02:50:09 +0000 | [diff] [blame] | 1841 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1842 | from xml.etree import ElementTree as ET |
Andrew M. Kuchling | a17cd4a | 2009-01-31 02:50:09 +0000 | [diff] [blame] | 1843 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1844 | t = ET.XML("""<list> |
| 1845 | <item>1</item> <item>2</item> <item>3</item> |
| 1846 | </list>""") |
| 1847 | new = ET.XML('<root/>') |
| 1848 | new.extend(t) |
Andrew M. Kuchling | a17cd4a | 2009-01-31 02:50:09 +0000 | [diff] [blame] | 1849 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1850 | # Outputs <root><item>1</item>...</root> |
| 1851 | print ET.tostring(new) |
Andrew M. Kuchling | a17cd4a | 2009-01-31 02:50:09 +0000 | [diff] [blame] | 1852 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1853 | * New :class:`Element` method: :meth:`iter` yields the children of the |
| 1854 | element as a generator. It's also possible to write ``for child in |
| 1855 | elem: ...`` to loop over an element's children. The existing method |
| 1856 | :meth:`getiterator` is now deprecated. :meth:`getchildren` is |
| 1857 | another similar method that constructs and returns a list of |
| 1858 | children; it's also deprecated. |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 1859 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1860 | * New :class:`Element` method: :meth:`itertext` yields all chunks of |
| 1861 | text that are descendants of the element. For example:: |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 1862 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 1863 | t = ET.XML("""<list> |
| 1864 | <item>1</item> <item>2</item> <item>3</item> |
| 1865 | </list>""") |
| 1866 | |
| 1867 | # Outputs ['\n ', '1', ' ', '2', ' ', '3', '\n'] |
| 1868 | print list(t.itertext()) |
| 1869 | |
| 1870 | * Deprecated: using an element as a Boolean (i.e., ``if elem: ...``) |
| 1871 | would return true if the element had any children, or false if |
| 1872 | there were no children. This behaviour will eventually change or be removed |
| 1873 | because it's confusing (``None`` is false, but so is a childless element?), |
| 1874 | so it will now trigger a :exc:`FutureWarning`. In your code, |
| 1875 | you should be explicit: write ``len(elem) != 0`` if you're interested in |
| 1876 | the number of children, or ``elem is not None`` Instead, |
| 1877 | |
| 1878 | Fredrik Lundh develops ElementTree and produced the 1.3 version; |
| 1879 | you can read his article describing 1.3 at |
| 1880 | http://effbot.org/zone/elementtree-13-intro.htm. |
| 1881 | Florent Xicluna updated the version included with |
| 1882 | Python, after discussions on python-dev and in :issue:`6472`.) |
Georg Brandl | 0516f81 | 2009-11-18 18:52:35 +0000 | [diff] [blame] | 1883 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 1884 | .. ====================================================================== |
| 1885 | |
| 1886 | |
| 1887 | Build and C API Changes |
| 1888 | ======================= |
| 1889 | |
| 1890 | Changes to Python's build process and to the C API include: |
| 1891 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 1892 | * The latest release of the GNU Debugger, GDB 7, can be `scripted |
| 1893 | using Python |
| 1894 | <http://sourceware.org/gdb/current/onlinedocs/gdb/Python.html>`__. |
| 1895 | When you begin debugging an executable program P, GDB will look for |
| 1896 | a file named ``P-gdb.py`` and automatically read it. Dave Malcolm |
| 1897 | contributed a :file:`python-gdb.py` that adds a number of useful |
| 1898 | commands when debugging Python itself. For example, there are |
| 1899 | ``py-up`` and ``py-down`` that go up or down one Python stack frame, |
| 1900 | which usually corresponds to several C stack frames. ``py-print`` |
| 1901 | prints the value of a Python variable, and ``py-bt`` prints the |
| 1902 | Python stack trace. (Added as a result of :issue:`8032`.) |
| 1903 | |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 1904 | * If you use the :file:`.gdbinit` file provided with Python, |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1905 | the "pyo" macro in the 2.7 version now works correctly when the thread being |
| 1906 | 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] | 1907 | (Contributed by Victor Stinner; :issue:`3632`.) |
| 1908 | |
Andrew M. Kuchling | 9a4b94c | 2009-04-03 21:43:00 +0000 | [diff] [blame] | 1909 | * :cfunc:`Py_AddPendingCall` is now thread-safe, letting any |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 1910 | worker thread submit notifications to the main Python thread. This |
| 1911 | is particularly useful for asynchronous IO operations. |
| 1912 | (Contributed by Kristjan Valur Jonsson; :issue:`4293`.) |
| 1913 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1914 | * New function: :cfunc:`PyCode_NewEmpty` creates an empty code object; |
| 1915 | only the filename, function name, and first line number are required. |
| 1916 | This is useful to extension modules that are attempting to |
| 1917 | construct a more useful traceback stack. Previously such |
| 1918 | extensions needed to call :cfunc:`PyCode_New`, which had many |
| 1919 | more arguments. (Added by Jeffrey Yasskin.) |
| 1920 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 1921 | * New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new |
| 1922 | exception class, just as the existing :cfunc:`PyErr_NewException` does, |
| 1923 | but takes an extra ``char *`` argument containing the docstring for the |
| 1924 | new exception class. (Added by the 'lekma' user on the Python bug tracker; |
| 1925 | :issue:`7033`.) |
| 1926 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1927 | * New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object |
| 1928 | and returns the line number that the frame is currently executing. |
| 1929 | Previously code would need to get the index of the bytecode |
| 1930 | instruction currently executing, and then look up the line number |
| 1931 | corresponding to that address. (Added by Jeffrey Yasskin.) |
| 1932 | |
Andrew M. Kuchling | 17ae2ba | 2010-02-03 02:19:14 +0000 | [diff] [blame] | 1933 | * New functions: :cfunc:`PyLong_AsLongAndOverflow` and |
| 1934 | :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long |
| 1935 | integer as a C :ctype:`long` or :ctype:`long long`. |
| 1936 | If the number is too large to fit into |
| 1937 | the output type, an *overflow* flag is set and returned to the caller. |
| 1938 | (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.) |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1939 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 1940 | * New function: stemming from the rewrite of string-to-float conversion, |
| 1941 | a new :cfunc:`PyOS_string_to_double` function was added. The old |
| 1942 | :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions |
| 1943 | are now deprecated. |
| 1944 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1945 | * New macros: the Python header files now define the following macros: |
| 1946 | :cmacro:`Py_ISALNUM`, |
| 1947 | :cmacro:`Py_ISALPHA`, |
| 1948 | :cmacro:`Py_ISDIGIT`, |
| 1949 | :cmacro:`Py_ISLOWER`, |
| 1950 | :cmacro:`Py_ISSPACE`, |
| 1951 | :cmacro:`Py_ISUPPER`, |
| 1952 | :cmacro:`Py_ISXDIGIT`, |
| 1953 | and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`. |
| 1954 | All of these functions are analogous to the C |
| 1955 | standard macros for classifying characters, but ignore the current |
| 1956 | locale setting, because in |
| 1957 | several places Python needs to analyze characters in a |
| 1958 | locale-independent way. (Added by Eric Smith; |
| 1959 | :issue:`5793`.) |
| 1960 | |
| 1961 | .. XXX these macros don't seem to be described in the c-api docs. |
| 1962 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 1963 | * Removed function: :cmacro:`PyEval_CallObject` is now only available |
| 1964 | as a macro. A function version was being kept around to preserve |
| 1965 | ABI linking compatibility, but that was in 1997; it can certainly be |
| 1966 | deleted. (Removed by Antoine Pitrou; :issue:`8276`.) |
| 1967 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 1968 | * New format codes: the :cfunc:`PyFormat_FromString`, |
| 1969 | :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now |
| 1970 | accepts ``%lld`` and ``%llu`` format codes for displaying values of |
| 1971 | C's :ctype:`long long` types. |
| 1972 | (Contributed by Mark Dickinson; :issue:`7228`.) |
| 1973 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 1974 | * The complicated interaction between threads and process forking has |
| 1975 | been changed. Previously, the child process created by |
| 1976 | :func:`os.fork` might fail because the child is created with only a |
| 1977 | single thread running, the thread performing the :func:`os.fork`. |
| 1978 | If other threads were holding a lock, such as Python's import lock, |
| 1979 | when the fork was performed, the lock would still be marked as |
| 1980 | "held" in the new process. But in the child process nothing would |
| 1981 | ever release the lock, since the other threads weren't replicated, |
| 1982 | and the child process would no longer be able to perform imports. |
| 1983 | |
| 1984 | Python 2.7 now acquires the import lock before performing an |
| 1985 | :func:`os.fork`, and will also clean up any locks created using the |
| 1986 | :mod:`threading` module. C extension modules that have internal |
| 1987 | locks, or that call :cfunc:`fork()` themselves, will not benefit |
| 1988 | from this clean-up. |
| 1989 | |
| 1990 | (Fixed by Thomas Wouters; :issue:`1590864`.) |
| 1991 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 1992 | * The :cfunc:`Py_Finalize` function now calls the internal |
| 1993 | :func:`threading._shutdown` function; this prevents some exceptions from |
| 1994 | being raised when an interpreter shuts down. |
| 1995 | (Patch by Adam Olsen; :issue:`1722344`.) |
| 1996 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 1997 | * When using the :ctype:`PyMemberDef` structure to define attributes |
| 1998 | of a type, Python will no longer let you try to delete or set a |
| 1999 | :const:`T_STRING_INPLACE` attribute. |
| 2000 | |
| 2001 | .. rev 79644 |
| 2002 | |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 2003 | * Global symbols defined by the :mod:`ctypes` module are now prefixed |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2004 | with ``Py``, or with ``_ctypes``. (Implemented by Thomas |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 2005 | Heller; :issue:`3102`.) |
| 2006 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 2007 | * New configure option: the :option:`--with-system-expat` switch allows |
| 2008 | building the :mod:`pyexpat` module to use the system Expat library. |
| 2009 | (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.) |
| 2010 | |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 2011 | * New configure option: compiling Python with the |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 2012 | :option:`--with-valgrind` option will now disable the pymalloc |
Andrew M. Kuchling | ce69052 | 2010-04-13 01:32:51 +0000 | [diff] [blame] | 2013 | allocator, which is difficult for the Valgrind memory-error detector |
| 2014 | to analyze correctly. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 2015 | Valgrind will therefore be better at detecting memory leaks and |
| 2016 | overruns. (Contributed by James Henstridge; :issue:`2422`.) |
| 2017 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 2018 | * New configure option: you can now supply no arguments to |
| 2019 | :option:`--with-dbmliborder=` in order to build none of the various |
| 2020 | DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis; |
| 2021 | :issue:`6491`.) |
| 2022 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 2023 | * The :program:`configure` script now checks for floating-point rounding bugs |
| 2024 | on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING` |
| 2025 | preprocessor definition. No code currently uses this definition, |
| 2026 | but it's available if anyone wishes to use it. |
| 2027 | (Added by Mark Dickinson; :issue:`2937`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2028 | |
Andrew M. Kuchling | 4515f0d | 2010-04-11 20:40:09 +0000 | [diff] [blame] | 2029 | :program:`configure` also now sets a :envvar:`LDCXXSHARED` Makefile |
| 2030 | variable for supporting C++ linking. (Contributed by Arfrever |
| 2031 | Frehtes Taifersar Arahesis; :issue:`1222585`.) |
| 2032 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2033 | * The build process now creates the necessary files for pkg-config |
| 2034 | support. (Contributed by Clinton Roy; :issue:`3585`.) |
| 2035 | |
| 2036 | * The build process now supports Subversion 1.7. (Contributed by |
| 2037 | Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.) |
| 2038 | |
Andrew M. Kuchling | b4a4f51 | 2009-12-29 20:10:16 +0000 | [diff] [blame] | 2039 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2040 | .. ====================================================================== |
| 2041 | |
| 2042 | Port-Specific Changes: Windows |
| 2043 | ----------------------------------- |
| 2044 | |
Andrew M. Kuchling | 10b1ec9 | 2009-01-02 21:00:35 +0000 | [diff] [blame] | 2045 | * The :mod:`msvcrt` module now contains some constants from |
| 2046 | the :file:`crtassem.h` header file: |
| 2047 | :data:`CRT_ASSEMBLY_VERSION`, |
| 2048 | :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`, |
| 2049 | and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`. |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 2050 | (Contributed by David Cournapeau; :issue:`4365`.) |
| 2051 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 2052 | * The :mod:`_winreg` module for accessing the registry now implements |
| 2053 | the :func:`CreateKeyEx` and :func:`DeleteKeyEx` functions, extended |
| 2054 | versions of previously-supported functions that take several extra |
| 2055 | arguments. The :func:`DisableReflectionKey`, |
| 2056 | :func:`EnableReflectionKey`, and :func:`QueryReflectionKey` were also |
| 2057 | tested and documented. |
| 2058 | (Implemented by Brian Curtin: :issue:`7347`.) |
| 2059 | |
Andrew M. Kuchling | 466bd9d | 2009-01-24 03:28:18 +0000 | [diff] [blame] | 2060 | * The new :cfunc:`_beginthreadex` API is used to start threads, and |
| 2061 | the native thread-local storage functions are now used. |
| 2062 | (Contributed by Kristjan Valur Jonsson; :issue:`3582`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2063 | |
Andrew M. Kuchling | 363dbcc | 2010-04-14 23:55:17 +0000 | [diff] [blame] | 2064 | * The :func:`os.kill` function now works on Windows. The signal value |
| 2065 | can be the constants :const:`CTRL_C_EVENT`, |
| 2066 | :const:`CTRL_BREAK_EVENT`, or any integer. The Control-C and |
| 2067 | Control-Break keystroke events can be sent to subprocesses; any |
| 2068 | other value will use the :cfunc:`TerminateProcess` API. |
| 2069 | (Contributed by Miki Tebeka; :issue:`1220212`.) |
| 2070 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2071 | * The :func:`os.listdir` function now correctly fails |
| 2072 | for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.) |
| 2073 | |
Andrew M. Kuchling | 3c8a24e | 2009-12-29 23:41:04 +0000 | [diff] [blame] | 2074 | * The :mod:`mimelib` module will now read the MIME database from |
| 2075 | the Windows registry when initializing. |
| 2076 | (Patch by Gabriel Genellina; :issue:`4969`.) |
| 2077 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2078 | .. ====================================================================== |
| 2079 | |
| 2080 | Port-Specific Changes: Mac OS X |
| 2081 | ----------------------------------- |
| 2082 | |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2083 | * 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] | 2084 | ``sys.path``, in order to share added packages between the system |
| 2085 | installation and a user-installed copy of the same version. |
| 2086 | (Changed by Ronald Oussoren; :issue:`4865`.) |
| 2087 | |
Andrew M. Kuchling | 04b99cc | 2010-05-04 01:24:22 +0000 | [diff] [blame] | 2088 | Port-Specific Changes: FreeBSD |
| 2089 | ----------------------------------- |
| 2090 | |
| 2091 | * FreeBSD 7.1's :const:`SO_SETFIB` constant, used with |
| 2092 | :func:`~socket.getsockopt`/:func:`~socket.setsockopt` to select an |
| 2093 | alternate routing table, is now available in the :mod:`socket` |
| 2094 | module. (Added by Kyle VanderBeek; :issue:`8235`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2095 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 2096 | Other Changes and Fixes |
| 2097 | ======================= |
| 2098 | |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 2099 | * Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were |
| 2100 | added to the :file:`Tools` directory. :file:`iobench` measures the |
Antoine Pitrou | dde96e6 | 2010-02-08 20:25:47 +0000 | [diff] [blame] | 2101 | 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] | 2102 | while performing various operations, and :file:`ccbench` is a |
| 2103 | concurrency benchmark that tries to measure computing throughput, |
| 2104 | thread switching latency, and IO processing bandwidth when |
| 2105 | performing several tasks using a varying number of threads. |
Andrew M. Kuchling | 0e7123f | 2010-02-08 13:22:24 +0000 | [diff] [blame] | 2106 | |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 2107 | * When importing a module from a :file:`.pyc` or :file:`.pyo` file |
| 2108 | with an existing :file:`.py` counterpart, the :attr:`co_filename` |
Andrew M. Kuchling | 92b9700 | 2009-05-02 17:12:15 +0000 | [diff] [blame] | 2109 | attributes of the resulting code objects are overwritten when the |
| 2110 | original filename is obsolete. This can happen if the file has been |
| 2111 | renamed, moved, or is accessed through different paths. (Patch by |
| 2112 | Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.) |
Andrew M. Kuchling | 7706957 | 2009-03-31 01:21:01 +0000 | [diff] [blame] | 2113 | |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 2114 | * The :file:`regrtest.py` script now takes a :option:`--randseed=` |
| 2115 | switch that takes an integer that will be used as the random seed |
| 2116 | for the :option:`-r` option that executes tests in random order. |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2117 | The :option:`-r` option also reports the seed that was used |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 2118 | (Added by Collin Winter.) |
| 2119 | |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 2120 | * Another :file:`regrtest.py` switch is :option:`-j`, which |
| 2121 | takes an integer specifying how many tests run in parallel. This |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2122 | allows reducing the total runtime on multi-core machines. |
Antoine Pitrou | 4698d99 | 2009-05-31 14:20:14 +0000 | [diff] [blame] | 2123 | This option is compatible with several other options, including the |
| 2124 | :option:`-R` switch which is known to produce long runtimes. |
Andrew M. Kuchling | 91e0db8 | 2009-12-31 16:17:05 +0000 | [diff] [blame] | 2125 | (Added by Antoine Pitrou, :issue:`6152`.) This can also be used |
| 2126 | with a new :option:`-F` switch that runs selected tests in a loop |
| 2127 | until they fail. (Added by Antoine Pitrou; :issue:`7312`.) |
Andrew M. Kuchling | 71d5c28 | 2009-03-30 22:30:20 +0000 | [diff] [blame] | 2128 | |
Andrew M. Kuchling | 85f928a | 2010-04-15 01:42:27 +0000 | [diff] [blame] | 2129 | * When executed as a script, the :file:`py_compile.py` module now |
| 2130 | accepts ``'-'`` as an argument, which will read standard input for |
| 2131 | the list of filenames to be compiled. (Contributed by Piotr |
| 2132 | Ożarowski; :issue:`8233`.) |
| 2133 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2134 | .. ====================================================================== |
| 2135 | |
| 2136 | Porting to Python 2.7 |
| 2137 | ===================== |
| 2138 | |
| 2139 | This section lists previously described changes and other bugfixes |
| 2140 | that may require changes to your code: |
| 2141 | |
Andrew M. Kuchling | e86b7fe | 2010-05-06 14:14:09 +0000 | [diff] [blame] | 2142 | * The :func:`range` function processes its arguments more |
| 2143 | consistently; it will now call :meth:`__int__` on non-float, |
| 2144 | non-integer arguments that are supplied to it. (Fixed by Alexander |
| 2145 | Belopolsky; :issue:`1533`.) |
| 2146 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 2147 | * The string :meth:`format` method changed the default precision used |
| 2148 | for floating-point and complex numbers from 6 decimal |
Andrew M. Kuchling | 5a9c40b | 2009-10-05 22:30:22 +0000 | [diff] [blame] | 2149 | places to 12, which matches the precision used by :func:`str`. |
| 2150 | (Changed by Eric Smith; :issue:`5920`.) |
| 2151 | |
Amaury Forgeot d'Arc | 901f200 | 2009-06-09 23:08:13 +0000 | [diff] [blame] | 2152 | * Because of an optimization for the :keyword:`with` statement, the special |
| 2153 | methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's |
| 2154 | 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] | 2155 | 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] | 2156 | types. (:issue:`6101`.) |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2157 | |
Andrew M. Kuchling | 837a538 | 2010-05-06 17:21:59 +0000 | [diff] [blame] | 2158 | * Due to a bug in Python 2.6, the *exc_value* parameter to |
| 2159 | :meth:`__exit__` methods was often the string representation of the |
| 2160 | exception, not an instance. This was fixed in 2.7, so *exc_value* |
| 2161 | will be an instance as expected. (Fixed by Florent Xicluna; |
| 2162 | :issue:`7853`.) |
| 2163 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 2164 | * When a restricted set of attributes were set using ``__slots__``, |
| 2165 | deleting an unset attribute would not raise :exc:`AttributeError` |
| 2166 | as you would expect. Fixed by Benjamin Peterson; :issue:`7604`.) |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 2167 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 2168 | In the standard library: |
| 2169 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 2170 | * When using :class:`Decimal` instances with a string's |
| 2171 | :meth:`format` method, the default alignment was previously |
| 2172 | left-alignment. This has been changed to right-alignment, which might |
| 2173 | change the output of your programs. |
| 2174 | (Changed by Mark Dickinson; :issue:`6857`.) |
| 2175 | |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 2176 | Comparisons involving a signaling NaN value (or ``sNAN``) now signal |
| 2177 | :const:`InvalidOperation` instead of silently returning a true or |
| 2178 | false value depending on the comparison operator. Quiet NaN values |
| 2179 | (or ``NaN``) are now hashable. (Fixed by Mark Dickinson; |
| 2180 | :issue:`7279`.) |
| 2181 | |
Andrew M. Kuchling | e41e4db | 2010-02-18 14:16:48 +0000 | [diff] [blame] | 2182 | * The ElementTree library, :mod:`xml.etree`, no longer escapes |
| 2183 | ampersands and angle brackets when outputting an XML processing |
| 2184 | instruction (which looks like `<?xml-stylesheet href="#style1"?>`) |
| 2185 | or comment (which looks like `<!-- comment -->`). |
| 2186 | (Patch by Neil Muller; :issue:`2746`.) |
| 2187 | |
Andrew M. Kuchling | d3b6022 | 2010-05-01 01:19:16 +0000 | [diff] [blame] | 2188 | * The :meth:`readline` method of :class:`StringIO` objects now does |
| 2189 | nothing when a negative length is requested, as other file-like |
| 2190 | objects do. (:issue:`7348`). |
| 2191 | |
Andrew M. Kuchling | 15c82d2 | 2010-04-29 00:22:16 +0000 | [diff] [blame] | 2192 | * The :mod:`syslog` module will now use the value of ``sys.argv[0]`` as the |
| 2193 | identifier instead of the previous default value of ``'python'``. |
| 2194 | (Changed by Sean Reifschneider; :issue:`8451`.) |
| 2195 | |
Andrew M. Kuchling | 04b99cc | 2010-05-04 01:24:22 +0000 | [diff] [blame] | 2196 | * The :mod:`urlparse` module's :func:`~urlparse.urlsplit` now handles |
| 2197 | unknown URL schemes in a fashion compliant with :rfc:`3986`: if the |
| 2198 | URL is of the form ``"<something>://..."``, the text before the |
| 2199 | ``://`` is treated as the scheme, even if it's a made-up scheme that |
| 2200 | the module doesn't know about. This change may break code that |
| 2201 | worked around the old behaviour. For example, Python 2.6.4 or 2.5 |
| 2202 | will return the following: |
| 2203 | |
| 2204 | >>> import urlparse |
| 2205 | >>> urlparse.urlsplit('invented://host/filename?query') |
| 2206 | ('invented', '', '//host/filename?query', '', '') |
| 2207 | |
| 2208 | Python 2.7 (and Python 2.6.5) will return: |
| 2209 | |
| 2210 | >>> import urlparse |
| 2211 | >>> urlparse.urlsplit('invented://host/filename?query') |
| 2212 | ('invented', 'host', '/filename?query', '', '') |
| 2213 | |
| 2214 | (Python 2.7 actually produces slightly different output, since it |
| 2215 | returns a named tuple instead of a standard tuple.) |
| 2216 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 2217 | For C extensions: |
| 2218 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 2219 | * C extensions that use integer format codes with the ``PyArg_Parse*`` |
| 2220 | family of functions will now raise a :exc:`TypeError` exception |
| 2221 | instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`). |
| 2222 | |
Andrew M. Kuchling | a7f5947 | 2009-12-31 16:38:53 +0000 | [diff] [blame] | 2223 | * Use the new :cfunc:`PyOS_string_to_double` function instead of the old |
| 2224 | :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions, |
| 2225 | which are now deprecated. |
| 2226 | |
Andrew M. Kuchling | 7f8ebdb | 2010-01-03 01:15:21 +0000 | [diff] [blame] | 2227 | |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2228 | .. ====================================================================== |
| 2229 | |
| 2230 | |
| 2231 | .. _acks27: |
| 2232 | |
| 2233 | Acknowledgements |
| 2234 | ================ |
| 2235 | |
| 2236 | The author would like to thank the following people for offering |
| 2237 | suggestions, corrections and assistance with various drafts of this |
Andrew M. Kuchling | c121f13 | 2010-04-30 01:33:40 +0000 | [diff] [blame] | 2238 | article: Nick Coghlan, Ryan Lovett, R. David Murray, Hugh Secker-Walker. |
Andrew M. Kuchling | ce1882b | 2008-10-04 16:52:31 +0000 | [diff] [blame] | 2239 | |