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