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