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