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 | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 9 | .. Fix accents on Kristjan Valur Jonsson, Fuerstenau, Tarek Ziade. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 11 | .. $Id$ |
| 12 | Rules for maintenance: |
| 13 | |
| 14 | * Anyone can add text to this document. Do not spend very much time |
| 15 | on the wording of your changes, because your text will probably |
| 16 | get rewritten to some degree. |
| 17 | |
| 18 | * The maintainer will go through Misc/NEWS periodically and add |
| 19 | changes; it's therefore more important to add your changes to |
| 20 | Misc/NEWS than to this file. |
| 21 | |
| 22 | * This is not a complete list of every single change; completeness |
| 23 | is the purpose of Misc/NEWS. Some changes I consider too small |
| 24 | or esoteric to include. If such a change is added to the text, |
| 25 | I'll just remove it. (This is another reason you shouldn't spend |
| 26 | too much time on writing your addition.) |
| 27 | |
| 28 | * If you want to draw your new text to the attention of the |
| 29 | maintainer, add 'XXX' to the beginning of the paragraph or |
| 30 | section. |
| 31 | |
| 32 | * It's OK to just add a fragmentary note about a change. For |
| 33 | example: "XXX Describe the transmogrify() function added to the |
| 34 | socket module." The maintainer will research the change and |
| 35 | write the necessary text. |
| 36 | |
| 37 | * You can comment out your additions if you like, but it's not |
| 38 | necessary (especially when a final release is some months away). |
| 39 | |
| 40 | * Credit the author of a patch or bugfix. Just the name is |
| 41 | sufficient; the e-mail address isn't necessary. |
| 42 | |
| 43 | * It's helpful to add the bug/patch number in a parenthetical comment. |
| 44 | |
| 45 | XXX Describe the transmogrify() function added to the socket |
| 46 | module. |
| 47 | (Contributed by P.Y. Developer; :issue:`12345`.) |
| 48 | |
| 49 | This saves the maintainer some effort going through the SVN logs |
| 50 | when researching a change. |
| 51 | |
| 52 | This article explains the new features in Python 2.7. |
| 53 | No release schedule has been decided yet for 2.7. |
| 54 | |
| 55 | .. Compare with previous release in 2 - 3 sentences here. |
| 56 | add hyperlink when the documentation becomes available online. |
| 57 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 58 | Python 3.1 |
| 59 | ================ |
| 60 | |
| 61 | Much as Python 2.6 incorporated features from Python 3.0, |
| 62 | version 2.7 is influenced by features from 3.1. |
| 63 | |
| 64 | XXX mention importlib; anything else? |
| 65 | |
| 66 | One porting change: the :option:`-3` switch now automatically |
| 67 | enables the :option:`-Qwarn` switch that causes warnings |
| 68 | about using classic division with integers and long integers. |
| 69 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 70 | .. ======================================================================== |
| 71 | .. Large, PEP-level features and changes should be described here. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 72 | .. ======================================================================== |
| 73 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 74 | PEP 372: Adding an ordered dictionary to collections |
| 75 | ==================================================== |
| 76 | |
| 77 | XXX write this |
| 78 | |
| 79 | Several modules will now use :class:`OrderedDict` by default. The |
| 80 | :mod:`ConfigParser` module uses :class:`OrderedDict` for the list |
| 81 | of sections and the options within a section. |
| 82 | The :meth:`namedtuple._asdict` method returns an :class:`OrderedDict` |
| 83 | as well. |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 84 | |
| 85 | |
| 86 | Other Language Changes |
| 87 | ====================== |
| 88 | |
| 89 | Some smaller changes made to the core Python language are: |
| 90 | |
Benjamin Peterson | 3f96a87 | 2009-04-11 20:58:12 +0000 | [diff] [blame] | 91 | * :meth:`str.format` method now supports automatic numbering of the replacement |
| 92 | fields. This makes using :meth:`str.format` more closely resemble using |
| 93 | ``%s`` formatting:: |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 94 | |
| 95 | >>> '{}:{}:{}'.format(2009, 04, 'Sunday') |
| 96 | '2009:4:Sunday' |
| 97 | >>> '{}:{}:{day}'.format(2009, 4, day='Sunday') |
| 98 | '2009:4:Sunday' |
| 99 | |
Benjamin Peterson | 3f96a87 | 2009-04-11 20:58:12 +0000 | [diff] [blame] | 100 | The auto-numbering takes the fields from left to right, so the first ``{...}`` |
| 101 | specifier will use the first argument to :meth:`str.format`, the next |
| 102 | specifier will use the next argument, and so on. You can't mix auto-numbering |
| 103 | and explicit numbering -- either number all of your specifier fields or none |
| 104 | of them -- but you can mix auto-numbering and named fields, as in the second |
Georg Brandl | 9078afe | 2009-04-27 16:38:14 +0000 | [diff] [blame] | 105 | example above. (Contributed by Eric Smith; :issue`5237`.) |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 106 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 107 | * The :func:`int` and :func:`long` types gained a ``bit_length`` |
| 108 | method that returns the number of bits necessary to represent |
| 109 | its argument in binary:: |
| 110 | |
| 111 | >>> n = 37 |
| 112 | >>> bin(37) |
| 113 | '0b100101' |
| 114 | >>> n.bit_length() |
| 115 | 6 |
| 116 | >>> n = 2**123-1 |
| 117 | >>> n.bit_length() |
| 118 | 123 |
| 119 | >>> (n+1).bit_length() |
| 120 | 124 |
| 121 | |
| 122 | (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.) |
| 123 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 124 | * Conversions from long integers and regular integers to floating |
| 125 | point now round differently, returning the floating-point number |
| 126 | closest to the number. This doesn't matter for small integers that |
| 127 | can be converted exactly, but for large numbers that will |
| 128 | unavoidably lose precision, Python 2.7 will now approximate more |
| 129 | closely. For example, Python 2.6 computed the following:: |
| 130 | |
| 131 | >>> n = 295147905179352891391 |
| 132 | >>> float(n) |
| 133 | 2.9514790517935283e+20 |
| 134 | >>> n - long(float(n)) |
| 135 | 65535L |
| 136 | |
| 137 | Python 2.7's floating-point result is larger, but much closer to the |
| 138 | true value:: |
| 139 | |
| 140 | >>> n = 295147905179352891391 |
| 141 | >>> float(n) |
| 142 | 2.9514790517935289e+20 |
| 143 | >>> n-long(float(n) |
| 144 | ... ) |
| 145 | -1L |
| 146 | |
| 147 | (Implemented by Mark Dickinson; :issue:`3166`.) |
| 148 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 149 | * The :class:`bytearray` type's :meth:`translate` method will |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 150 | now accept ``None`` as its first argument. (Fixed by Georg Brandl; |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 151 | :issue:`4759`.) |
Mark Dickinson | d72c7b6 | 2009-03-20 16:00:49 +0000 | [diff] [blame] | 152 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 153 | .. ====================================================================== |
| 154 | |
| 155 | |
| 156 | Optimizations |
| 157 | ------------- |
| 158 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 159 | Several performance enhancements have been added: |
| 160 | |
| 161 | .. * A new :program:`configure` option, :option:`--with-computed-gotos`, |
| 162 | compiles the main bytecode interpreter loop using a new dispatch |
| 163 | mechanism that gives speedups of up to 20%, depending on the system |
| 164 | and benchmark. The new mechanism is only supported on certain |
| 165 | compilers, such as gcc, SunPro, and icc. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 166 | |
| 167 | * The garbage collector now performs better when many objects are |
| 168 | being allocated without deallocating any. A full garbage collection |
| 169 | pass is only performed when the middle generation has been collected |
| 170 | 10 times and when the number of survivor objects from the middle |
| 171 | generation exceeds 10% of the number of objects in the oldest |
| 172 | generation. The second condition was added to reduce the number |
| 173 | of full garbage collections as the number of objects on the heap grows, |
| 174 | avoiding quadratic performance when allocating very many objects. |
| 175 | (Suggested by Martin von Loewis and implemented by Antoine Pitrou; |
| 176 | :issue:`4074`.) |
| 177 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 178 | * The garbage collector tries to avoid tracking simple containers |
| 179 | which can't be part of a cycle. In Python 2.7, this is now true for |
| 180 | tuples and dicts containing atomic types (such as ints, strings, |
| 181 | etc.). Transitively, a dict containing tuples of atomic types won't |
| 182 | be tracked either. This helps reduce the cost of each |
| 183 | garbage collection by decreasing the number of objects to be |
| 184 | considered and traversed by the collector. |
Antoine Pitrou | 9d81def | 2009-03-28 19:20:09 +0000 | [diff] [blame] | 185 | (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 186 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 187 | * Integers are now stored internally either in base 2**15 or in base |
| 188 | 2**30, the base being determined at build time. Previously, they |
| 189 | were always stored in base 2**15. Using base 2**30 gives |
| 190 | significant performance improvements on 64-bit machines, but |
| 191 | benchmark results on 32-bit machines have been mixed. Therefore, |
| 192 | the default is to use base 2**30 on 64-bit machines and base 2**15 |
| 193 | on 32-bit machines; on Unix, there's a new configure option |
| 194 | :option:`--enable-big-digits` that can be used to override this default. |
| 195 | |
| 196 | Apart from the performance improvements this change should be |
| 197 | invisible to end users, with one exception: for testing and |
| 198 | debugging purposes there's a new structseq ``sys.long_info`` that |
| 199 | provides information about the internal format, giving the number of |
| 200 | bits per digit and the size in bytes of the C type used to store |
| 201 | each digit:: |
| 202 | |
| 203 | >>> import sys |
| 204 | >>> sys.long_info |
| 205 | sys.long_info(bits_per_digit=30, sizeof_digit=4) |
| 206 | |
| 207 | (Contributed by Mark Dickinson; :issue:`4258`.) |
| 208 | |
| 209 | Another set of changes made long objects a few bytes smaller: 2 bytes |
| 210 | smaller on 32-bit systems and 6 bytes on 64-bit. |
| 211 | (Contributed by Mark Dickinson; :issue:`5260`.) |
| 212 | |
| 213 | * The division algorithm for long integers has been made faster |
| 214 | by tightening the inner loop, doing shifts instead of multiplications, |
| 215 | and fixing an unnecessary extra iteration. |
| 216 | Various benchmarks show speedups of between 50% and 150% for long |
| 217 | integer divisions and modulo operations. |
| 218 | (Contributed by Mark Dickinson; :issue:`5512`.) |
| 219 | |
| 220 | * The implementation of ``%`` checks for the left-side operand being |
| 221 | a Python string and special-cases it; this results in a 1-3% |
| 222 | performance increase for applications that frequently use ``%`` |
| 223 | with strings, such as templating libraries. |
| 224 | (Implemented by Collin Winter; :issue:`5176`.) |
| 225 | |
| 226 | * List comprehensions with an ``if`` condition are compiled into |
| 227 | faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7 |
| 228 | by Jeffrey Yasskin; :issue:`4715`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 229 | |
| 230 | .. ====================================================================== |
| 231 | |
| 232 | New, Improved, and Deprecated Modules |
| 233 | ===================================== |
| 234 | |
| 235 | As in every release, Python's standard library received a number of |
| 236 | enhancements and bug fixes. Here's a partial list of the most notable |
| 237 | changes, sorted alphabetically by module name. Consult the |
| 238 | :file:`Misc/NEWS` file in the source tree for a more complete list of |
| 239 | changes, or look through the Subversion logs for all the details. |
| 240 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 241 | * The :mod:`bz2` module's :class:`BZ2File` now supports the context |
| 242 | management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``. |
| 243 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
| 244 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 245 | * New class: the :class:`Counter` class in the :mod:`collections` module is |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 246 | useful for tallying data. :class:`Counter` instances behave mostly |
| 247 | like dictionaries but return zero for missing keys instead of |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 248 | raising a :exc:`KeyError`: |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 249 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 250 | .. doctest:: |
| 251 | :options: +NORMALIZE_WHITESPACE |
| 252 | |
| 253 | >>> from collections import Counter |
| 254 | >>> c = Counter() |
| 255 | >>> for letter in 'here is a sample of english text': |
| 256 | ... c[letter] += 1 |
| 257 | ... |
| 258 | >>> c |
| 259 | Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2, |
| 260 | 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1, |
| 261 | 'p': 1, 'r': 1, 'x': 1}) |
| 262 | >>> c['e'] |
| 263 | 5 |
| 264 | >>> c['z'] |
| 265 | 0 |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 266 | |
| 267 | There are two additional :class:`Counter` methods: :meth:`most_common` |
| 268 | returns the N most common elements and their counts, and :meth:`elements` |
| 269 | returns an iterator over the contained element, repeating each element |
| 270 | as many times as its count:: |
| 271 | |
| 272 | >>> c.most_common(5) |
| 273 | [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)] |
| 274 | >>> c.elements() -> |
| 275 | 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ', |
| 276 | 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i', |
| 277 | 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's', |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 278 | 's', 's', 'r', 't', 't', 'x' |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 279 | |
| 280 | Contributed by Raymond Hettinger; :issue:`1696199`. |
| 281 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 282 | The :class:`namedtuple` class now has an optional *rename* parameter. |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 283 | If *rename* is true, field names that are invalid because they've |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 284 | been repeated or that aren't legal Python identifiers will be |
| 285 | renamed to legal names that are derived from the field's |
| 286 | position within the list of fields: |
| 287 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 288 | >>> from collections import namedtuple |
| 289 | >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 290 | >>> T._fields |
| 291 | ('field1', '_1', '_2', 'field2') |
| 292 | |
| 293 | (Added by Raymond Hettinger; :issue:`1818`.) |
| 294 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 295 | The :class:`deque` data type now exposes its maximum length as the |
| 296 | read-only :attr:`maxlen` attribute. (Added by Raymond Hettinger.) |
| 297 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 298 | * In Distutils, :func:`distutils.sdist.add_defaults` now uses |
| 299 | *package_dir* and *data_files* to create the MANIFEST file. |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 300 | :mod:`distutils.sysconfig` will now read the :envvar:`AR` |
| 301 | environment variable. |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 302 | |
| 303 | It is no longer mandatory to store clear-text passwords in the |
| 304 | :file:`.pypirc` file when registering and uploading packages to PyPI. As long |
| 305 | as the username is present in that file, the :mod:`distutils` package will |
| 306 | prompt for the password if not present. (Added by Tarek Ziade, |
| 307 | based on an initial contribution by Nathan Van Gheem; :issue:`4394`.) |
| 308 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 309 | A Distutils setup can now specify that a C extension is optional by |
| 310 | setting the *optional* option setting to true. If this optional is |
| 311 | supplied, failure to build the extension will not abort the build |
| 312 | process, but instead simply not install the failing extension. |
| 313 | (Contributed by Georg Brandl; :issue:`5583`.) |
| 314 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 315 | * New method: the :class:`Decimal` class gained a |
| 316 | :meth:`from_float` class method that performs an exact conversion |
| 317 | of a floating-point number to a :class:`Decimal`. |
| 318 | Note that this is an **exact** conversion that strives for the |
| 319 | closest decimal approximation to the floating-point representation's value; |
| 320 | the resulting decimal value will therefore still include the inaccuracy, |
| 321 | if any. |
| 322 | For example, ``Decimal.from_float(0.1)`` returns |
| 323 | ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``. |
| 324 | (Implemented by Raymond Hettinger; :issue:`4796`.) |
| 325 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 326 | * The :class:`Fraction` class will now accept two rational numbers |
| 327 | as arguments to its constructor. |
| 328 | (Implemented by Mark Dickinson; :issue:`5812`.) |
| 329 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 330 | * New function: the :mod:`gc` module's :func:`is_tracked` returns |
| 331 | true if a given instance is tracked by the garbage collector, false |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 332 | otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.) |
| 333 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 334 | * The :mod:`gzip` module's :class:`GzipFile` now supports the context |
| 335 | management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``. |
| 336 | (Contributed by Hagen Fuerstenau; :issue:`3860`.) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 337 | It's now possible to override the modification time |
| 338 | recorded in a gzipped file by providing an optional timestamp to |
| 339 | the constructor. (Contributed by Jacques Frechet; :issue:`4272`.) |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 340 | |
| 341 | * The :class:`io.FileIO` class now raises an :exc:`OSError` when passed |
| 342 | an invalid file descriptor. (Implemented by Benjamin Peterson; |
| 343 | :issue:`4991`.) |
| 344 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 345 | * New function: ``itertools.compress(*data*, *selectors*)`` takes two |
| 346 | iterators. Elements of *data* are returned if the corresponding |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 347 | value in *selectors* is true:: |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 348 | |
| 349 | itertools.compress('ABCDEF', [1,0,1,0,1,1]) => |
| 350 | A, C, E, F |
| 351 | |
| 352 | New function: ``itertools.combinations_with_replacement(*iter*, *r*)`` |
| 353 | returns all the possible *r*-length combinations of elements from the |
| 354 | iterable *iter*. Unlike :func:`combinations`, individual elements |
| 355 | can be repeated in the generated combinations:: |
| 356 | |
| 357 | itertools.combinations_with_replacement('abc', 2) => |
| 358 | ('a', 'a'), ('a', 'b'), ('a', 'c'), |
| 359 | ('b', 'b'), ('b', 'c'), ('c', 'c') |
| 360 | |
| 361 | Note that elements are treated as unique depending on their position |
| 362 | in the input, not their actual values. |
| 363 | |
| 364 | The :class:`itertools.count` function now has a *step* argument that |
| 365 | allows incrementing by values other than 1. :func:`count` also |
| 366 | now allows keyword arguments, and using non-integer values such as |
| 367 | floats or :class:`Decimal` instances. (Implemented by Raymond |
| 368 | Hettinger; :issue:`5032`.) |
| 369 | |
| 370 | :func:`itertools.combinations` and :func:`itertools.product` were |
| 371 | previously raising :exc:`ValueError` for values of *r* larger than |
| 372 | the input iterable. This was deemed a specification error, so they |
| 373 | now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.) |
| 374 | |
| 375 | * The :mod:`json` module was upgraded to version 2.0.9 of the |
| 376 | simplejson package, which includes a C extension that makes |
| 377 | encoding and decoding faster. |
| 378 | (Contributed by Bob Ippolito; :issue:`4136`.) |
| 379 | |
| 380 | To support the new :class:`OrderedDict` type, :func:`json.load` |
| 381 | now has an optional *object_pairs_hook* parameter that will be called |
| 382 | with any object literal that decodes to a list of pairs. |
| 383 | (Contributed by Raymond Hettinger; :issue:`5381`.) |
| 384 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 385 | * The :mod:`multiprocessing` module's :class:`Manager*` classes |
| 386 | can now be passed a callable that will be called whenever |
| 387 | a subprocess is started, along with a set of arguments that will be |
| 388 | passed to the callable. |
| 389 | (Contributed by lekma; :issue:`5585`.) |
| 390 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 391 | * The :mod:`pydoc` module now has help for the various symbols that Python |
| 392 | uses. You can now do ``help('<<')`` or ``help('@')``, for example. |
| 393 | (Contributed by David Laban; :issue:`4739`.) |
| 394 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 395 | * The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn` |
| 396 | now accept an optional *flags* argument, for consistency with the |
| 397 | other functions in the module. (Added by Gregory P. Smith.) |
| 398 | |
| 399 | * New function: the :mod:`subprocess` module's |
| 400 | :func:`check_output` runs a command with a specified set of arguments |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 401 | and returns the command's output as a string when the command runs without |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 402 | error, or raises a :exc:`CalledProcessError` exception otherwise. |
| 403 | |
| 404 | :: |
| 405 | |
| 406 | >>> subprocess.check_output(['df', '-h', '.']) |
| 407 | 'Filesystem Size Used Avail Capacity Mounted on\n |
| 408 | /dev/disk0s2 52G 49G 3.0G 94% /\n' |
| 409 | |
| 410 | >>> subprocess.check_output(['df', '-h', '/bogus']) |
| 411 | ... |
| 412 | subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1 |
| 413 | |
| 414 | (Contributed by Gregory P. Smith.) |
| 415 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 416 | * New function: :func:`is_declared_global` in the :mod:`symtable` module |
| 417 | returns true for variables that are explicitly declared to be global, |
| 418 | false for ones that are implicitly global. |
| 419 | (Contributed by Jeremy Hylton.) |
| 420 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 421 | * The ``sys.version_info`` value is now a named tuple, with attributes |
| 422 | named ``major``, ``minor``, ``micro``, ``releaselevel``, and ``serial``. |
| 423 | (Contributed by Ross Light; :issue:`4285`.) |
| 424 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 425 | * The :mod:`threading` module's :meth:`Event.wait` method now returns |
| 426 | the internal flag on exit. This means the method will usually |
| 427 | return true because :meth:`wait` is supposed to block until the |
| 428 | internal flag becomes true. The return value will only be false if |
| 429 | a timeout was provided and the operation timed out. |
| 430 | (Contributed by XXX; :issue:`1674032`.) |
| 431 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 432 | * The :mod:`unittest` module was enhanced in several ways. |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 433 | The progress messages will now show 'x' for expected failures |
| 434 | and 'u' for unexpected successes when run in verbose mode. |
| 435 | (Contributed by Benjamin Peterson.) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 436 | Test cases can raise the :exc:`SkipTest` exception to skip a test. |
| 437 | (:issue:`1034053`.) |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 438 | |
| 439 | The error messages for :meth:`assertEqual`, |
| 440 | :meth:`assertTrue`, and :meth:`assertFalse` |
| 441 | failures now provide more information. If you set the |
| 442 | :attr:`longMessage` attribute of your :class:`TestCase` classes to |
| 443 | true, both the standard error message and any additional message you |
| 444 | provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 445 | |
| 446 | The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now |
| 447 | return a context handler when called without providing a callable |
| 448 | object to run. For example, you can write this:: |
| 449 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 450 | with self.assertRaises(KeyError): |
| 451 | raise ValueError |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 452 | |
| 453 | (Implemented by Antoine Pitrou; :issue:`4444`.) |
| 454 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 455 | The methods :meth:`addCleanup` and :meth:`doCleanups` were added. |
| 456 | :meth:`addCleanup` allows you to add cleanup functions that |
| 457 | will be called unconditionally (after :meth:`setUp` if |
| 458 | :meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows |
| 459 | for much simpler resource allocation and deallocation during tests. |
| 460 | :issue:`5679` |
| 461 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 462 | A number of new methods were added that provide more specialized |
| 463 | tests. Many of these methods were written by Google engineers |
| 464 | for use in their test suites; Gregory P. Smith, Michael Foord, and |
| 465 | GvR worked on merging them into Python's version of :mod:`unittest`. |
| 466 | |
| 467 | * :meth:`assertIsNone` and :meth:`assertIsNotNone` take one |
| 468 | expression and verify that the result is or is not ``None``. |
| 469 | |
| 470 | * :meth:`assertIs` and :meth:`assertIsNot` take two values and check |
| 471 | whether the two values evaluate to the same object or not. |
| 472 | (Added by Michael Foord; :issue:`2578`.) |
| 473 | |
| 474 | * :meth:`assertGreater`, :meth:`assertGreaterEqual`, |
| 475 | :meth:`assertLess`, and :meth:`assertLessEqual` compare |
| 476 | two quantities. |
| 477 | |
| 478 | * :meth:`assertMultiLineEqual` compares two strings, and if they're |
| 479 | not equal, displays a helpful comparison that highlights the |
| 480 | differences in the two strings. |
| 481 | |
| 482 | * :meth:`assertRegexpMatches` checks whether its first argument is a |
| 483 | string matching a regular expression provided as its second argument. |
| 484 | |
| 485 | * :meth:`assertRaisesRegexp` checks whether a particular exception |
| 486 | is raised, and then also checks that the string representation of |
| 487 | the exception matches the provided regular expression. |
| 488 | |
| 489 | * :meth:`assertIn` and :meth:`assertNotIn` tests whether |
| 490 | *first* is or is not in *second*. |
| 491 | |
| 492 | * :meth:`assertSameElements` tests whether two provided sequences |
| 493 | contain the same elements. |
| 494 | |
| 495 | * :meth:`assertSetEqual` compares whether two sets are equal, and |
| 496 | only reports the differences between the sets in case of error. |
| 497 | |
| 498 | * Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual` |
| 499 | compare the specified types and explain the differences. |
| 500 | More generally, :meth:`assertSequenceEqual` compares two sequences |
| 501 | and can optionally check whether both sequences are of a |
| 502 | particular type. |
| 503 | |
| 504 | * :meth:`assertDictEqual` compares two dictionaries and reports the |
| 505 | differences. :meth:`assertDictContainsSubset` checks whether |
| 506 | all of the key/value pairs in *first* are found in *second*. |
| 507 | |
| 508 | * A new hook, :meth:`addTypeEqualityFunc` takes a type object and a |
| 509 | function. The :meth:`assertEqual` method will use the function |
| 510 | when both of the objects being compared are of the specified type. |
| 511 | This function should compare the two objects and raise an |
| 512 | exception if they don't match; it's a good idea for the function |
| 513 | to provide additional information about why the two objects are |
| 514 | matching, much as the new sequence comparison methods do. |
| 515 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 516 | :func:`unittest.main` now takes an optional ``exit`` argument. |
| 517 | If False ``main`` doesn't call :func:`sys.exit` allowing it to |
| 518 | be used from the interactive interpreter. :issue:`3379`. |
| 519 | |
| 520 | :class:`TestResult` has new :meth:`startTestRun` and |
| 521 | :meth:`stopTestRun` methods; called immediately before |
| 522 | and after a test run. :issue:`5728` by Robert Collins. |
| 523 | |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 524 | * The :func:`is_zipfile` function in the :mod:`zipfile` module will now |
| 525 | accept a file object, in addition to the path names accepted in earlier |
| 526 | versions. (Contributed by Gabriel Genellina; :issue:`4756`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 527 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 528 | :mod:`zipfile` now supports archiving empty directories and |
| 529 | extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.) |
| 530 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 531 | .. ====================================================================== |
| 532 | .. whole new modules get described in subsections here |
| 533 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 534 | importlib: Importing Modules |
| 535 | ------------------------------ |
| 536 | |
Benjamin Peterson | f47ed4a | 2009-04-11 20:45:40 +0000 | [diff] [blame] | 537 | Python 3.1 includes the :mod:`importlib` package, a re-implementation |
| 538 | of the logic underlying Python's :keyword:`import` statement. |
| 539 | :mod:`importlib` is useful for implementors of Python interpreters and |
| 540 | to user who wish to write new importers that can participate in the |
| 541 | import process. Python 2.7 doesn't contain the complete |
| 542 | :mod:`importlib` package, but instead has a tiny subset that contains |
| 543 | a single function, :func:`import_module`. |
| 544 | |
| 545 | ``import_module(*name*, *package*=None)`` imports a module. *name* is |
| 546 | a string containing the module or package's name. It's possible to do |
| 547 | relative imports by providing a string that begins with a ``.`` |
| 548 | character, such as ``..utils.errors``. For relative imports, the |
| 549 | *package* argument must be provided and is the name of the package that |
| 550 | will be used as the anchor for |
| 551 | the relative import. :func:`import_module` both inserts the imported |
| 552 | module into ``sys.modules`` and returns the module object. |
| 553 | |
| 554 | Here are some examples:: |
| 555 | |
| 556 | >>> from importlib import import_module |
| 557 | >>> anydbm = import_module('anydbm') # Standard absolute import |
| 558 | >>> anydbm |
| 559 | <module 'anydbm' from '/p/python/Lib/anydbm.py'> |
| 560 | >>> # Relative import |
| 561 | >>> sysconfig = import_module('..sysconfig', 'distutils.command') |
| 562 | >>> sysconfig |
| 563 | <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'> |
| 564 | |
| 565 | :mod:`importlib` was implemented by Brett Cannon and introduced in |
| 566 | Python 3.1. |
| 567 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 568 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 569 | ttk: Themed Widgets for Tk |
| 570 | -------------------------- |
| 571 | |
| 572 | Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk |
| 573 | widgets but have a more customizable appearance and can therefore more |
| 574 | closely resemble the native platform's widgets. This widget |
| 575 | set was originally called Tile, but was renamed to Ttk (for "themed Tk") |
| 576 | on being added to Tcl/Tck release 8.5. |
| 577 | |
| 578 | XXX write a brief discussion and an example here. |
| 579 | |
| 580 | The :mod:`ttk` module was written by Guilherme Polo and added in |
| 581 | :issue:`2983`. An alternate version called ``Tile.py``, written by |
| 582 | Martin Franklin and maintained by Kevin Walzer, was proposed for |
| 583 | inclusion in :issue:`2618`, but the authors argued that Guilherme |
| 584 | Polo's work was more comprehensive. |
| 585 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 586 | .. ====================================================================== |
| 587 | |
| 588 | |
| 589 | Build and C API Changes |
| 590 | ======================= |
| 591 | |
| 592 | Changes to Python's build process and to the C API include: |
| 593 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 594 | * If you use the :file:`.gdbinit` file provided with Python, |
| 595 | the "pyo" macro in the 2.7 version will now work when the thread being |
| 596 | debugged doesn't hold the GIL; the macro will now acquire it before printing. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 597 | (Contributed by Victor Stinner; :issue:`3632`.) |
| 598 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 599 | * :cfunc:`Py_AddPendingCall` is now thread-safe, letting any |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 600 | worker thread submit notifications to the main Python thread. This |
| 601 | is particularly useful for asynchronous IO operations. |
| 602 | (Contributed by Kristjan Valur Jonsson; :issue:`4293`.) |
| 603 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 604 | * Global symbols defined by the :mod:`ctypes` module are now prefixed |
| 605 | with ``Py`, or with ``_ctypes``. (Implemented by Thomas |
| 606 | Heller; :issue:`3102`.) |
| 607 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 608 | * The :program:`configure` script now checks for floating-point rounding bugs |
| 609 | on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING` |
| 610 | preprocessor definition. No code currently uses this definition, |
| 611 | but it's available if anyone wishes to use it. |
| 612 | (Added by Mark Dickinson; :issue:`2937`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 613 | |
| 614 | .. ====================================================================== |
| 615 | |
| 616 | Port-Specific Changes: Windows |
| 617 | ----------------------------------- |
| 618 | |
Georg Brandl | 1f01deb | 2009-01-03 22:47:39 +0000 | [diff] [blame] | 619 | * The :mod:`msvcrt` module now contains some constants from |
| 620 | the :file:`crtassem.h` header file: |
| 621 | :data:`CRT_ASSEMBLY_VERSION`, |
| 622 | :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`, |
| 623 | and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`. |
Benjamin Peterson | 1010bf3 | 2009-01-30 04:00:29 +0000 | [diff] [blame] | 624 | (Contributed by David Cournapeau; :issue:`4365`.) |
| 625 | |
| 626 | * The new :cfunc:`_beginthreadex` API is used to start threads, and |
| 627 | the native thread-local storage functions are now used. |
| 628 | (Contributed by Kristjan Valur Jonsson; :issue:`3582`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 629 | |
| 630 | .. ====================================================================== |
| 631 | |
| 632 | Port-Specific Changes: Mac OS X |
| 633 | ----------------------------------- |
| 634 | |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 635 | * The ``/Library/Python/2.7/site-packages`` is now appended to |
| 636 | ``sys.path``, in order to share added packages between the system |
| 637 | installation and a user-installed copy of the same version. |
| 638 | (Changed by Ronald Oussoren; :issue:`4865`.) |
| 639 | |
| 640 | |
| 641 | Other Changes and Fixes |
| 642 | ======================= |
| 643 | |
| 644 | * When importing a module from a :file:`.pyc` or :file:`.pyo` file |
| 645 | with an existing :file:`.py` counterpart, the :attr:`co_filename` |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 646 | attributes of the resulting code objects are overwritten when the |
| 647 | original filename is obsolete. This can happen if the file has been |
| 648 | renamed, moved, or is accessed through different paths. (Patch by |
| 649 | Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.) |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 650 | |
| 651 | * The :file:`regrtest.py` script now takes a :option:`--randseed=` |
| 652 | switch that takes an integer that will be used as the random seed |
| 653 | for the :option:`-r` option that executes tests in random order. |
| 654 | The :option:`-r` option also now reports the seed that was used |
| 655 | (Added by Collin Winter.) |
| 656 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 657 | |
| 658 | .. ====================================================================== |
| 659 | |
| 660 | Porting to Python 2.7 |
| 661 | ===================== |
| 662 | |
| 663 | This section lists previously described changes and other bugfixes |
| 664 | that may require changes to your code: |
| 665 | |
Benjamin Peterson | 87c8d87 | 2009-06-11 22:54:11 +0000 | [diff] [blame^] | 666 | * Because of an optimization for the :keyword:`with` statement, the special |
| 667 | methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's |
| 668 | type, and cannot be directly attached to the object's instance. This |
| 669 | affects new-style classes (derived from :class:`object`) and C extension |
| 670 | types. (:issue:`6101`.) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 671 | |
| 672 | .. ====================================================================== |
| 673 | |
| 674 | |
| 675 | .. _acks27: |
| 676 | |
| 677 | Acknowledgements |
| 678 | ================ |
| 679 | |
| 680 | The author would like to thank the following people for offering |
| 681 | suggestions, corrections and assistance with various drafts of this |
| 682 | article: no one yet. |
| 683 | |