blob: fbc1b3e1223f21e6d8f53fbcd363be5ddb9657e8 [file] [log] [blame]
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001****************************
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 Petersond69fe2a2010-02-03 02:59:43 +00009.. Fix accents on Kristjan Valur Jonsson, Fuerstenau
Benjamin Peterson1010bf32009-01-30 04:00:29 +000010
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000011.. $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
Benjamin Peterson9eea4802009-12-31 03:31:15 +000052This article explains the new features in Python 2.7. The final
53release of 2.7 is currently scheduled for June 2010; the detailed
54schedule is described in :pep:`373`.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000055
Benjamin Petersond69fe2a2010-02-03 02:59:43 +000056Python 2.7 is planned to be the last major release in the 2.x series.
57Though more major releases have not been absolutely ruled out, it's
58likely that the 2.7 release will have an extended period of
59maintenance compared to earlier 2.x versions.
60
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000061.. Compare with previous release in 2 - 3 sentences here.
62 add hyperlink when the documentation becomes available online.
63
Benjamin Petersonf6489f92009-11-25 17:46:26 +000064.. _whatsnew27-python31:
65
66Python 3.1 Features
67=======================
Benjamin Petersond23f8222009-04-05 19:13:16 +000068
69Much as Python 2.6 incorporated features from Python 3.0,
Benjamin Petersonf6489f92009-11-25 17:46:26 +000070version 2.7 incorporates some of the new features
71in Python 3.1. The 2.x series continues to provide tools
72for migrating to the 3.x series.
Benjamin Petersond23f8222009-04-05 19:13:16 +000073
Benjamin Petersonf6489f92009-11-25 17:46:26 +000074A partial list of 3.1 features that were backported to 2.7:
75
76* A version of the :mod:`io` library, rewritten in C for performance.
77* The ordered-dictionary type described in :ref:`pep-0372`.
Benjamin Peterson97dd9872009-12-13 01:23:39 +000078* The new format specifier described in :ref:`pep-0378`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000079* The :class:`memoryview` object.
80* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Benjamin Peterson9eea4802009-12-31 03:31:15 +000081* Float-to-string and string-to-float conversions now round their
82 results more correctly. And :func:`repr` of a floating-point
83 number *x* returns a result that's guaranteed to round back to the
84 same number when converted back to a string.
85* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Benjamin Petersond23f8222009-04-05 19:13:16 +000086
87One porting change: the :option:`-3` switch now automatically
88enables the :option:`-Qwarn` switch that causes warnings
89about using classic division with integers and long integers.
90
Benjamin Petersonf6489f92009-11-25 17:46:26 +000091Other new Python3-mode warnings include:
92
93* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
94 which are not supported in 3.x.
95
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000096.. ========================================================================
97.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000098.. ========================================================================
99
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000100.. _pep-0372:
101
Benjamin Petersond23f8222009-04-05 19:13:16 +0000102PEP 372: Adding an ordered dictionary to collections
103====================================================
104
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000105Regular Python dictionaries iterate over key/value pairs in arbitrary order.
106Over the years, a number of authors have written alternative implementations
107that remember the order that the keys were originally inserted. Based on
108the experiences from those implementations, a new
109:class:`collections.OrderedDict` class has been introduced.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000110
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000111The :class:`OrderedDict` API is substantially the same as regular dictionaries
112but will iterate over keys and values in a guaranteed order depending on
113when a key was first inserted::
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000114
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000115 >>> from collections import OrderedDict
116 >>> d = OrderedDict([('first', 1), ('second', 2),
117 ... ('third', 3)])
118 >>> d.items()
119 [('first', 1), ('second', 2), ('third', 3)]
120
121If a new entry overwrites an existing entry, the original insertion
122position is left unchanged::
123
124 >>> d['second'] = 4
125 >>> d.items()
126 [('first', 1), ('second', 4), ('third', 3)]
127
128Deleting an entry and reinserting it will move it to the end::
129
130 >>> del d['second']
131 >>> d['second'] = 5
132 >>> d.items()
133 [('first', 1), ('third', 3), ('second', 5)]
134
135The :meth:`popitem` method has an optional *last* argument
136that defaults to True. If *last* is True, the most recently
137added key is returned and removed; if it's False, the
138oldest key is selected::
139
140 >>> od = OrderedDict([(x,0) for x in range(20)])
141 >>> od.popitem()
142 (19, 0)
143 >>> od.popitem()
144 (18, 0)
145 >>> od.popitem(False)
146 (0, 0)
147 >>> od.popitem(False)
148 (1, 0)
149
150Comparing two ordered dictionaries checks both the keys and values,
151and requires that the insertion order was the same::
152
153 >>> od1 = OrderedDict([('first', 1), ('second', 2),
154 ... ('third', 3)])
155 >>> od2 = OrderedDict([('third', 3), ('first', 1),
156 ... ('second', 2)])
157 >>> od1==od2
158 False
159 >>> # Move 'third' key to the end
160 >>> del od2['third'] ; od2['third'] = 3
161 >>> od1==od2
162 True
163
164Comparing an :class:`OrderedDict` with a regular dictionary
165ignores the insertion order and just compares the keys and values.
166
167How does the :class:`OrderedDict` work? It maintains a doubly-linked
168list of keys, appending new keys to the list as they're inserted. A
169secondary dictionary maps keys to their corresponding list node, so
170deletion doesn't have to traverse the entire linked list and therefore
171remains O(1).
172
173.. XXX check O(1)-ness with Raymond
174
175The standard library now supports use of ordered dictionaries in several
176modules. The :mod:`configparser` module uses them by default. This lets
177configuration files be read, modified, and then written back in their original
178order. The *_asdict()* method for :func:`collections.namedtuple` now
179returns an ordered dictionary with the values appearing in the same order as
180the underlying tuple indicies. The :mod:`json` module is being built-out with
181an *object_pairs_hook* to allow OrderedDicts to be built by the decoder.
182Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
183
184.. seealso::
185
186 :pep:`372` - Adding an ordered dictionary to collections
187 PEP written by Armin Ronacher and Raymond Hettinger;
188 implemented by Raymond Hettinger.
189
190.. _pep-0378:
191
192PEP 378: Format Specifier for Thousands Separator
193====================================================
194
195To make program output more readable, it can be useful to add
196separators to large numbers and render them as
19718,446,744,073,709,551,616 instead of 18446744073709551616.
198
199The fully general solution for doing this is the :mod:`locale` module,
200which can use different separators ("," in North America, "." in
201Europe) and different grouping sizes, but :mod:`locale` is complicated
202to use and unsuitable for multi-threaded applications where different
203threads are producing output for different locales.
204
205Therefore, a simple comma-grouping mechanism has been added to the
206mini-language used by the string :meth:`format` method. When
207formatting a floating-point number, simply include a comma between the
208width and the precision::
209
210 >>> '{:20,.2}'.format(f)
211 '18,446,744,073,709,551,616.00'
212
213This mechanism is not adaptable at all; commas are always used as the
214separator and the grouping is always into three-digit groups. The
215comma-formatting mechanism isn't as general as the :mod:`locale`
216module, but it's easier to use.
217
218.. XXX "Format String Syntax" in string.rst could use many more examples.
219
220.. seealso::
221
222 :pep:`378` - Format Specifier for Thousands Separator
223 PEP written by Raymond Hettinger; implemented by Eric Smith.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000224
225Other Language Changes
226======================
227
228Some smaller changes made to the core Python language are:
229
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000230* The :keyword:`with` statement can now use multiple context managers
231 in one statement. Context managers are processed from left to right
232 and each one is treated as beginning a new :keyword:`with` statement.
233 This means that::
234
235 with A() as a, B() as b:
236 ... suite of statements ...
237
238 is equivalent to::
239
240 with A() as a:
241 with B() as b:
242 ... suite of statements ...
243
244 The :func:`contextlib.nested` function provides a very similar
245 function, so it's no longer necessary and has been deprecated.
246
247 (Proposed in http://codereview.appspot.com/53094; implemented by
248 Georg Brandl.)
249
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000250* Conversions between floating-point numbers and strings are
251 now correctly rounded on most platforms. These conversions occur
252 in many different places: :func:`str` on
253 floats and complex numbers; the :class:`float` and :class:`complex`
254 constructors;
255 numeric formatting; serialization and
256 deserialization of floats and complex numbers using the
257 :mod:`marshal`, :mod:`pickle`
258 and :mod:`json` modules;
259 parsing of float and imaginary literals in Python code;
260 and :class:`Decimal`-to-float conversion.
261
262 Related to this, the :func:`repr` of a floating-point number *x*
263 now returns a result based on the shortest decimal string that's
264 guaranteed to round back to *x* under correct rounding (with
265 round-half-to-even rounding mode). Previously it gave a string
266 based on rounding x to 17 decimal digits.
267
268 The rounding library responsible for this improvement works on
269 Windows, and on Unix platforms using the gcc, icc, or suncc
270 compilers. There may be a small number of platforms where correct
271 operation of this code cannot be guaranteed, so the code is not
Benjamin Petersona28e7022010-01-09 18:53:06 +0000272 used on such systems. You can find out which code is being used
273 by checking :data:`sys.float_repr_style`, which will be ``short``
274 if the new code is in use and ``legacy`` if it isn't.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000275
Mark Dickinson0bc8f902010-01-07 09:31:48 +0000276 Implemented by Eric Smith and Mark Dickinson, using David Gay's
277 :file:`dtoa.c` library; :issue:`7117`.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000278
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000279* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000280 fields. This makes using :meth:`str.format` more closely resemble using
281 ``%s`` formatting::
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000282
283 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
284 '2009:4:Sunday'
285 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
286 '2009:4:Sunday'
287
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000288 The auto-numbering takes the fields from left to right, so the first ``{...}``
289 specifier will use the first argument to :meth:`str.format`, the next
290 specifier will use the next argument, and so on. You can't mix auto-numbering
291 and explicit numbering -- either number all of your specifier fields or none
292 of them -- but you can mix auto-numbering and named fields, as in the second
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000293 example above. (Contributed by Eric Smith; :issue:`5237`.)
294
295 Complex numbers now correctly support usage with :func:`format`.
296 Specifying a precision or comma-separation applies to both the real
297 and imaginary parts of the number, but a specified field width and
298 alignment is applied to the whole of the resulting ``1.5+3j``
299 output. (Contributed by Eric Smith; :issue:`1588`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000300
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000301 The 'F' format code now always formats its output using uppercase characters,
302 so it will now produce 'INF' and 'NAN'.
303 (Contributed by Eric Smith; :issue:`3382`.)
304
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000305* The :func:`int` and :func:`long` types gained a ``bit_length``
306 method that returns the number of bits necessary to represent
307 its argument in binary::
308
309 >>> n = 37
310 >>> bin(37)
311 '0b100101'
312 >>> n.bit_length()
313 6
314 >>> n = 2**123-1
315 >>> n.bit_length()
316 123
317 >>> (n+1).bit_length()
318 124
319
320 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
321
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000322* Conversions from long integers and regular integers to floating
323 point now round differently, returning the floating-point number
324 closest to the number. This doesn't matter for small integers that
325 can be converted exactly, but for large numbers that will
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000326 unavoidably lose precision, Python 2.7 now approximates more
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000327 closely. For example, Python 2.6 computed the following::
328
329 >>> n = 295147905179352891391
330 >>> float(n)
331 2.9514790517935283e+20
332 >>> n - long(float(n))
333 65535L
334
335 Python 2.7's floating-point result is larger, but much closer to the
336 true value::
337
338 >>> n = 295147905179352891391
339 >>> float(n)
340 2.9514790517935289e+20
341 >>> n-long(float(n)
342 ... )
343 -1L
344
345 (Implemented by Mark Dickinson; :issue:`3166`.)
346
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000347 Integer division is also more accurate in its rounding behaviours. (Also
348 implemented by Mark Dickinson; :issue:`1811`.)
349
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000350* The :class:`bytearray` type's :meth:`translate` method now accepts
351 ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000352 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000353
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000354* When using ``@classmethod`` and ``@staticmethod`` to wrap
355 methods as class or static methods, the wrapper object now
356 exposes the wrapped function as their :attr:`__func__` attribute.
357 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
358 George Sakkis; :issue:`5982`.)
359
360* A new encoding named "cp720", used primarily for Arabic text, is now
361 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
362 d'Arc; :issue:`1616979`.)
363
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000364* The :class:`file` object will now set the :attr:`filename` attribute
365 on the :exc:`IOError` exception when trying to open a directory
366 on POSIX platforms. (Noted by Jan Kaliszewski; :issue:`4764`.)
367
Benjamin Petersona28e7022010-01-09 18:53:06 +0000368* The Python tokenizer now translates line endings itself, so the
369 :func:`compile` built-in function can now accept code using any
370 line-ending convention. Additionally, it no longer requires that the
371 code end in a newline.
372
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000373* Extra parentheses in function definitions are illegal in Python 3.x,
374 meaning that you get a syntax error from ``def f((x)): pass``. In
375 Python3-warning mode, Python 2.7 will now warn about this odd usage.
376 (Noted by James Lingard; :issue:`7362`.)
377
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000378.. ======================================================================
379
380
381Optimizations
382-------------
383
Benjamin Petersond23f8222009-04-05 19:13:16 +0000384Several performance enhancements have been added:
385
386.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
387 compiles the main bytecode interpreter loop using a new dispatch
388 mechanism that gives speedups of up to 20%, depending on the system
389 and benchmark. The new mechanism is only supported on certain
390 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000391
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000392* A new opcode was added to perform the initial setup for
393 :keyword:`with` statements, looking up the :meth:`__enter__` and
394 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
395
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000396* The garbage collector now performs better for one common usage
397 pattern: when many objects are being allocated without deallocating
398 any of them. This would previously take quadratic
399 time for garbage collection, but now the number of full garbage collections
400 is reduced as the number of objects on the heap grows.
401 The new logic is to only perform a full garbage collection pass when
402 the middle generation has been collected 10 times and when the
403 number of survivor objects from the middle generation exceeds 10% of
404 the number of objects in the oldest generation. (Suggested by Martin
405 von Loewis and implemented by Antoine Pitrou; :issue:`4074`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000406
Benjamin Petersond23f8222009-04-05 19:13:16 +0000407* The garbage collector tries to avoid tracking simple containers
408 which can't be part of a cycle. In Python 2.7, this is now true for
409 tuples and dicts containing atomic types (such as ints, strings,
410 etc.). Transitively, a dict containing tuples of atomic types won't
411 be tracked either. This helps reduce the cost of each
412 garbage collection by decreasing the number of objects to be
413 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000414 (Contributed by Antoine Pitrou; :issue:`4688`.)
415
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000416* Long integers are now stored internally either in base 2**15 or in base
Benjamin Petersond23f8222009-04-05 19:13:16 +0000417 2**30, the base being determined at build time. Previously, they
418 were always stored in base 2**15. Using base 2**30 gives
419 significant performance improvements on 64-bit machines, but
420 benchmark results on 32-bit machines have been mixed. Therefore,
421 the default is to use base 2**30 on 64-bit machines and base 2**15
422 on 32-bit machines; on Unix, there's a new configure option
423 :option:`--enable-big-digits` that can be used to override this default.
424
425 Apart from the performance improvements this change should be
426 invisible to end users, with one exception: for testing and
427 debugging purposes there's a new structseq ``sys.long_info`` that
428 provides information about the internal format, giving the number of
429 bits per digit and the size in bytes of the C type used to store
430 each digit::
431
432 >>> import sys
433 >>> sys.long_info
434 sys.long_info(bits_per_digit=30, sizeof_digit=4)
435
436 (Contributed by Mark Dickinson; :issue:`4258`.)
437
438 Another set of changes made long objects a few bytes smaller: 2 bytes
439 smaller on 32-bit systems and 6 bytes on 64-bit.
440 (Contributed by Mark Dickinson; :issue:`5260`.)
441
442* The division algorithm for long integers has been made faster
443 by tightening the inner loop, doing shifts instead of multiplications,
444 and fixing an unnecessary extra iteration.
445 Various benchmarks show speedups of between 50% and 150% for long
446 integer divisions and modulo operations.
447 (Contributed by Mark Dickinson; :issue:`5512`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000448 Bitwise operations are also significantly faster (initial patch by
449 Gregory Smith; :issue:`1087418`).
Benjamin Petersond23f8222009-04-05 19:13:16 +0000450
451* The implementation of ``%`` checks for the left-side operand being
452 a Python string and special-cases it; this results in a 1-3%
453 performance increase for applications that frequently use ``%``
454 with strings, such as templating libraries.
455 (Implemented by Collin Winter; :issue:`5176`.)
456
457* List comprehensions with an ``if`` condition are compiled into
458 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
459 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000460
Benjamin Petersona28e7022010-01-09 18:53:06 +0000461* Converting an integer or long integer to a decimal string was made
462 faster by special-casing base 10 instead of using a generalized
463 conversion function that supports arbitrary bases.
464 (Patch by Gawain Bolton; :issue:`6713`.)
465
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000466* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
467 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
468 (strings, Unicode strings, and :class:`bytearray` objects) now use a
469 fast reverse-search algorithm instead of a character-by-character
470 scan. This is sometimes faster by a factor of 10. (Added by
471 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000472
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000473* The :mod:`pickle` and :mod:`cPickle` modules now automatically
474 intern the strings used for attribute names, reducing memory usage
475 of the objects resulting from unpickling. (Contributed by Jake
476 McGuire; :issue:`5084`.)
477
478* The :mod:`cPickle` module now special-cases dictionaries,
479 nearly halving the time required to pickle them.
480 (Contributed by Collin Winter; :issue:`5670`.)
481
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000482.. ======================================================================
483
Georg Brandl4d131ee2009-11-18 18:53:14 +0000484New and Improved Modules
485========================
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000486
487As in every release, Python's standard library received a number of
488enhancements and bug fixes. Here's a partial list of the most notable
489changes, sorted alphabetically by module name. Consult the
490:file:`Misc/NEWS` file in the source tree for a more complete list of
491changes, or look through the Subversion logs for all the details.
492
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000493* The :mod:`bdb` module's base debugging class :class:`Bdb`
494 gained a feature for skipping modules. The constructor
495 now takes an iterable containing glob-style patterns such as
496 ``django.*``; the debugger will not step into stack frames
497 from a module that matches one of these patterns.
498 (Contributed by Maru Newby after a suggestion by
499 Senthil Kumaran; :issue:`5142`.)
500
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000501* The :mod:`bz2` module's :class:`BZ2File` now supports the context
502 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
503 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
504
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000505* New class: the :class:`Counter` class in the :mod:`collections` module is
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000506 useful for tallying data. :class:`Counter` instances behave mostly
507 like dictionaries but return zero for missing keys instead of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000508 raising a :exc:`KeyError`:
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000509
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000510 .. doctest::
511 :options: +NORMALIZE_WHITESPACE
512
513 >>> from collections import Counter
514 >>> c = Counter()
515 >>> for letter in 'here is a sample of english text':
516 ... c[letter] += 1
517 ...
518 >>> c
519 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
520 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
521 'p': 1, 'r': 1, 'x': 1})
522 >>> c['e']
523 5
524 >>> c['z']
525 0
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000526
527 There are two additional :class:`Counter` methods: :meth:`most_common`
528 returns the N most common elements and their counts, and :meth:`elements`
529 returns an iterator over the contained element, repeating each element
530 as many times as its count::
531
532 >>> c.most_common(5)
533 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
534 >>> c.elements() ->
535 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
536 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
537 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000538 's', 's', 'r', 't', 't', 'x'
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000539
540 Contributed by Raymond Hettinger; :issue:`1696199`.
541
Georg Brandlef871f62010-03-12 10:06:40 +0000542 The new `~collections.OrderedDict` class is described in the earlier section
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000543 :ref:`pep-0372`.
544
Benjamin Petersond23f8222009-04-05 19:13:16 +0000545 The :class:`namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000546 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000547 been repeated or that aren't legal Python identifiers will be
548 renamed to legal names that are derived from the field's
549 position within the list of fields:
550
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000551 >>> from collections import namedtuple
552 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000553 >>> T._fields
554 ('field1', '_1', '_2', 'field2')
555
556 (Added by Raymond Hettinger; :issue:`1818`.)
557
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000558 The :class:`deque` data type now exposes its maximum length as the
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000559 read-only :attr:`maxlen` attribute, and has a
560 :meth:`reverse` method that reverses the elements of the deque in-place.
561 (Added by Raymond Hettinger.)
562
563* The :mod:`copy` module's :func:`deepcopy` function will now
564 correctly copy bound instance methods. (Implemented by
565 Robert Collins; :issue:`1515`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000566
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000567* The :mod:`ctypes` module now always converts ``None`` to a C NULL
568 pointer for arguments declared as pointers. (Changed by Thomas
569 Heller; :issue:`4606`.)
570
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000571* New method: the :mod:`datetime` module's :class:`timedelta` class
572 gained a :meth:`total_seconds` method that returns the number of seconds
573 in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
574
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000575* New method: the :class:`Decimal` class gained a
576 :meth:`from_float` class method that performs an exact conversion
577 of a floating-point number to a :class:`Decimal`.
578 Note that this is an **exact** conversion that strives for the
579 closest decimal approximation to the floating-point representation's value;
580 the resulting decimal value will therefore still include the inaccuracy,
581 if any.
582 For example, ``Decimal.from_float(0.1)`` returns
583 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
584 (Implemented by Raymond Hettinger; :issue:`4796`.)
585
586 The constructor for :class:`Decimal` now accepts non-European
587 Unicode characters, such as Arabic-Indic digits. (Contributed by
588 Mark Dickinson; :issue:`6595`.)
589
590 When using :class:`Decimal` instances with a string's
591 :meth:`format` method, the default alignment was previously
592 left-alignment. This has been changed to right-alignment, which seems
593 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
594
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000595* Distutils is being more actively developed, thanks to Tarek Ziadé
596 who has taken over maintenance of the package, so there are a number
597 of fixes and improvments.
598
599 A new :file:`setup.py` subcommand, ``check``, will check that the
600 arguments being passed to the :func:`setup` function are complete
601 and correct (:issue:`5732`).
602
603 Byte-compilation by the ``install_lib`` subcommand is now only done
604 if the ``sys.dont_write_bytecode`` setting allows it (:issue:`7071`).
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000605
606 :func:`distutils.sdist.add_defaults` now uses
Benjamin Petersond23f8222009-04-05 19:13:16 +0000607 *package_dir* and *data_files* to create the MANIFEST file.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000608 :mod:`distutils.sysconfig` now reads the :envvar:`AR` and
609 :envvar:`ARFLAGS` environment variables.
610
611 .. ARFLAGS done in #5941
Benjamin Petersond23f8222009-04-05 19:13:16 +0000612
613 It is no longer mandatory to store clear-text passwords in the
614 :file:`.pypirc` file when registering and uploading packages to PyPI. As long
615 as the username is present in that file, the :mod:`distutils` package will
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000616 prompt for the password if not present. (Added by Tarek Ziadé,
Benjamin Petersond23f8222009-04-05 19:13:16 +0000617 based on an initial contribution by Nathan Van Gheem; :issue:`4394`.)
618
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000619 A Distutils setup can now specify that a C extension is optional by
620 setting the *optional* option setting to true. If this optional is
621 supplied, failure to build the extension will not abort the build
622 process, but instead simply not install the failing extension.
623 (Contributed by Georg Brandl; :issue:`5583`.)
624
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000625 The :class:`distutils.dist.DistributionMetadata` class'
626 :meth:`read_pkg_file` method will read the contents of a package's
Benjamin Peterson5e55b3e2010-02-03 02:35:45 +0000627 :file:`PKG-INFO` metadata file. For an example of its use, see
628 :ref:`reading-metadata`.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000629 (Contributed by Tarek Ziadé; :issue:`7457`.)
Tarek Ziadéb88a4962009-12-08 09:45:25 +0000630
Benjamin Petersona28e7022010-01-09 18:53:06 +0000631 :file:`setup.py` files will now accept a :option:`--no-user-cfg` switch
632 to skip reading the :file:`~/.pydistutils.cfg` file. (Suggested by
633 by Michael Hoffman, and implemented by Paul Winkler; :issue:`1180`.)
634
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000635* The :class:`Fraction` class now accepts two rational numbers
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000636 as arguments to its constructor.
637 (Implemented by Mark Dickinson; :issue:`5812`.)
638
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000639* The :mod:`ftplib` module gained the ability to establish secure FTP
640 connections using TLS encapsulation of authentication as well as
641 subsequent control and data transfers. This is provided by the new
642 :class:`ftplib.FTP_TLS` class.
643 (Contributed by Giampaolo Rodola', :issue:`2054`.) The :meth:`storbinary`
644 method for binary uploads can now restart uploads thanks to an added
645 *rest* parameter (patch by Pablo Mouzo; :issue:`6845`.)
646
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000647* New function: the :mod:`gc` module's :func:`is_tracked` returns
648 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000649 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
650
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000651* The :mod:`gzip` module's :class:`GzipFile` now supports the context
652 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``.
653 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000654 It's now possible to override the modification time
655 recorded in a gzipped file by providing an optional timestamp to
656 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000657
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000658 Files in gzip format can be padded with trailing zero bytes; the
659 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
660 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
661
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000662* The default :class:`HTTPResponse` class used by the :mod:`httplib` module now
663 supports buffering, resulting in much faster reading of HTTP responses.
664 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
665
666* The :mod:`imaplib` module now supports IPv6 addresses.
667 (Contributed by Derek Morr; :issue:`1655`.)
668
669* The :mod:`io` library has been upgraded to the version shipped with
670 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
671 and is 2 to 20 times faster depending on the task at hand. The
672 original Python version was renamed to the :mod:`_pyio` module.
673
674 One minor resulting change: the :class:`io.TextIOBase` class now
675 has an :attr:`errors` attribute giving the error setting
676 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
677 ``'ignore'``).
678
679 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000680 an invalid file descriptor. (Implemented by Benjamin Peterson;
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000681 :issue:`4991`.) The :meth:`truncate` method now preserves the
682 file position; previously it would change the file position to the
683 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000684
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000685* New function: ``itertools.compress(data, selectors)`` takes two
Benjamin Petersond23f8222009-04-05 19:13:16 +0000686 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000687 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000688
689 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
690 A, C, E, F
691
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000692 New function: ``itertools.combinations_with_replacement(iter, r)``
Benjamin Petersond23f8222009-04-05 19:13:16 +0000693 returns all the possible *r*-length combinations of elements from the
694 iterable *iter*. Unlike :func:`combinations`, individual elements
695 can be repeated in the generated combinations::
696
697 itertools.combinations_with_replacement('abc', 2) =>
698 ('a', 'a'), ('a', 'b'), ('a', 'c'),
699 ('b', 'b'), ('b', 'c'), ('c', 'c')
700
701 Note that elements are treated as unique depending on their position
702 in the input, not their actual values.
703
704 The :class:`itertools.count` function now has a *step* argument that
705 allows incrementing by values other than 1. :func:`count` also
706 now allows keyword arguments, and using non-integer values such as
707 floats or :class:`Decimal` instances. (Implemented by Raymond
708 Hettinger; :issue:`5032`.)
709
710 :func:`itertools.combinations` and :func:`itertools.product` were
711 previously raising :exc:`ValueError` for values of *r* larger than
712 the input iterable. This was deemed a specification error, so they
713 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
714
715* The :mod:`json` module was upgraded to version 2.0.9 of the
716 simplejson package, which includes a C extension that makes
717 encoding and decoding faster.
718 (Contributed by Bob Ippolito; :issue:`4136`.)
719
720 To support the new :class:`OrderedDict` type, :func:`json.load`
721 now has an optional *object_pairs_hook* parameter that will be called
722 with any object literal that decodes to a list of pairs.
723 (Contributed by Raymond Hettinger; :issue:`5381`.)
724
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000725* New functions: the :mod:`math` module gained
726 :func:`erf` and :func:`erfc` for the error function and the complementary error function,
727 :func:`expm1` which computes ``e**x - 1`` with more precision than
728 using :func:`exp` and subtracting 1,
729 :func:`gamma` for the Gamma function, and
730 :func:`lgamma` for the natural log of the Gamma function.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000731 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
732
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000733* The :mod:`multiprocessing` module's :class:`Manager*` classes
734 can now be passed a callable that will be called whenever
735 a subprocess is started, along with a set of arguments that will be
736 passed to the callable.
737 (Contributed by lekma; :issue:`5585`.)
738
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000739 The :class:`Pool` class, which controls a pool of worker processes,
740 now has an optional *maxtasksperchild* parameter. Worker processes
741 will perform the specified number of tasks and then exit, causing the
742 :class:`Pool` to start a new worker. This is useful if tasks may leak
743 memory or other resources, or if some tasks will cause the worker to
744 become very large.
745 (Contributed by Charles Cazabon; :issue:`6963`.)
746
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000747* The :mod:`nntplib` module now supports IPv6 addresses.
748 (Contributed by Derek Morr; :issue:`1664`.)
749
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000750* New functions: the :mod:`os` module wraps the following POSIX system
751 calls: :func:`getresgid` and :func:`getresuid`, which return the
752 real, effective, and saved GIDs and UIDs;
753 :func:`setresgid` and :func:`setresuid`, which set
754 real, effective, and saved GIDs and UIDs to new values;
755 :func:`initgroups`. (GID/UID functions
756 contributed by Travis H.; :issue:`6508`. Support for initgroups added
757 by Jean-Paul Calderone; :issue:`7333`.)
758
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000759 The :func:`normpath` function now preserves Unicode; if its input path
760 is a Unicode string, the return value is also a Unicode string.
761 (Fixed by Matt Giuca; :issue:`5827`.)
762
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000763* The :mod:`pydoc` module now has help for the various symbols that Python
764 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
765 (Contributed by David Laban; :issue:`4739`.)
766
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000767* The :mod:`re` module's :func:`split`, :func:`sub`, and :func:`subn`
768 now accept an optional *flags* argument, for consistency with the
769 other functions in the module. (Added by Gregory P. Smith.)
770
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000771* The :mod:`shutil` module's :func:`copyfile` and :func:`copytree`
772 functions now raises a :exc:`SpecialFileError` exception when
773 asked to copy a named pipe. Previously the code would treat
774 named pipes like a regular file by opening them for reading, and
775 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
776
777* New functions: in the :mod:`site` module, three new functions
778 return various site- and user-specific paths.
779 :func:`getsitepackages` returns a list containing all
780 global site-packages directories, and
781 :func:`getusersitepackages` returns the path of the user's
782 site-packages directory.
Ezio Melotti6e40e272010-01-04 09:29:10 +0000783 :func:`getuserbase` returns the value of the :envvar:`USER_BASE`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000784 environment variable, giving the path to a directory that can be used
785 to store data.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000786 (Contributed by Tarek Ziadé; :issue:`6693`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000787
Benjamin Petersona28e7022010-01-09 18:53:06 +0000788* The :mod:`socket` module's :class:`SSL` objects now support the
789 buffer API, which fixed a test suite failure. (Fixed by Antoine Pitrou;
790 :issue:`7133`.)
791
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000792* The :mod:`SocketServer` module's :class:`TCPServer` class now
793 has a :attr:`disable_nagle_algorithm` class attribute.
794 The default value is False; if overridden to be True,
795 new request connections will have the TCP_NODELAY option set to
796 prevent buffering many small sends into a single TCP packet.
797 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
798
799* The :mod:`struct` module will no longer silently ignore overflow
800 errors when a value is too large for a particular integer format
801 code (one of ``bBhHiIlLqQ``); it now always raises a
802 :exc:`struct.error` exception. (Changed by Mark Dickinson;
803 :issue:`1523`.)
804
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000805* New function: the :mod:`subprocess` module's
806 :func:`check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +0000807 and returns the command's output as a string when the command runs without
Georg Brandl1f01deb2009-01-03 22:47:39 +0000808 error, or raises a :exc:`CalledProcessError` exception otherwise.
809
810 ::
811
812 >>> subprocess.check_output(['df', '-h', '.'])
813 'Filesystem Size Used Avail Capacity Mounted on\n
814 /dev/disk0s2 52G 49G 3.0G 94% /\n'
815
816 >>> subprocess.check_output(['df', '-h', '/bogus'])
817 ...
818 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
819
820 (Contributed by Gregory P. Smith.)
821
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000822* New function: :func:`is_declared_global` in the :mod:`symtable` module
823 returns true for variables that are explicitly declared to be global,
824 false for ones that are implicitly global.
825 (Contributed by Jeremy Hylton.)
826
Benjamin Petersond23f8222009-04-05 19:13:16 +0000827* The ``sys.version_info`` value is now a named tuple, with attributes
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000828 named :attr:`major`, :attr:`minor`, :attr:`micro`,
829 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
830 Light; :issue:`4285`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000831
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000832 :func:`sys.getwindowsversion` also returns a named tuple,
Ezio Melotti0d85e412010-03-13 00:39:49 +0000833 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
834 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb0869402010-02-03 14:25:10 +0000835 :attr:`service_pack_minor`, :attr:`suite_mask`, and
836 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000837
838* The :mod:`tarfile` module's default error handling has changed, to
839 no longer suppress fatal errors. The default error level was previously 0,
840 which meant that errors would only result in a message being written to the
841 debug log, but because the debug log is not activated by default,
842 these errors go unnoticed. The default error level is now 1,
843 which raises an exception if there's an error.
844 (Changed by Lars Gustäbel; :issue:`7357`.)
845
846 :mod:`tarfile` now supports filtering the :class:`TarInfo`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000847 objects being added to a tar file. When you call :meth:`TarFile.add`,
848 instance, you may supply an optional *filter* argument
849 that's a callable. The *filter* callable will be passed the
850 :class:`TarInfo` for every file being added, and can modify and return it.
851 If the callable returns ``None``, the file will be excluded from the
852 resulting archive. This is more powerful than the existing
853 *exclude* argument, which has therefore been deprecated.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000854 (Added by Lars Gustäbel; :issue:`6856`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000855
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000856* The :mod:`threading` module's :meth:`Event.wait` method now returns
857 the internal flag on exit. This means the method will usually
858 return true because :meth:`wait` is supposed to block until the
859 internal flag becomes true. The return value will only be false if
860 a timeout was provided and the operation timed out.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000861 (Contributed by Tim Lesher; :issue:`1674032`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000862
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000863* The :class:`UserDict` class is now a new-style class. (Changed by
864 Benjamin Peterson.)
865
866* The :mod:`zipfile` module's :class:`ZipFile` now supports the context
867 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
868 (Contributed by Brian Curtin; :issue:`5511`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000869
Benjamin Petersond23f8222009-04-05 19:13:16 +0000870 :mod:`zipfile` now supports archiving empty directories and
871 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000872 Reading files out of an archive is now faster, and interleaving
873 :meth:`read` and :meth:`readline` now works correctly.
874 (Contributed by Nir Aides; :issue:`7610`.)
875
876 The :func:`is_zipfile` function in the module now
877 accepts a file object, in addition to the path names accepted in earlier
878 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000879
Tarek Ziadé396fad72010-02-23 05:30:31 +0000880* XXX the :mod:`shutil` module has now a :func:`make_archive` function
881 (see the module doc, contributed by Tarek)
882
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000883.. ======================================================================
884.. whole new modules get described in subsections here
885
Tarek Ziadéba0eacf2010-02-02 23:43:21 +0000886* XXX A new :mod:`sysconfig` module has been extracted from :mod:`distutils`
887 and put in the standard library.
888
889 The :mod:`sysconfig` module provides access to Python's configuration
890 information like the list of installation paths and the configuration
891 variables relevant for the current platform.
892
893
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000894Unit Testing Enhancements
895---------------------------------
896
897The :mod:`unittest` module was enhanced in several ways.
898The progress messages now shows 'x' for expected failures
899and 'u' for unexpected successes when run in verbose mode.
900(Contributed by Benjamin Peterson.)
901Test cases can raise the :exc:`SkipTest` exception to skip a test.
902(:issue:`1034053`.)
903
904.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
905
906The error messages for :meth:`assertEqual`,
907:meth:`assertTrue`, and :meth:`assertFalse`
908failures now provide more information. If you set the
909:attr:`longMessage` attribute of your :class:`TestCase` classes to
910true, both the standard error message and any additional message you
911provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
912
913The :meth:`assertRaises` and :meth:`failUnlessRaises` methods now
914return a context handler when called without providing a callable
915object to run. For example, you can write this::
916
917 with self.assertRaises(KeyError):
918 raise ValueError
919
920(Implemented by Antoine Pitrou; :issue:`4444`.)
921
922The methods :meth:`addCleanup` and :meth:`doCleanups` were added.
923:meth:`addCleanup` allows you to add cleanup functions that
924will be called unconditionally (after :meth:`setUp` if
925:meth:`setUp` fails, otherwise after :meth:`tearDown`). This allows
926for much simpler resource allocation and deallocation during tests.
927:issue:`5679`
928
929A number of new methods were added that provide more specialized
930tests. Many of these methods were written by Google engineers
931for use in their test suites; Gregory P. Smith, Michael Foord, and
932GvR worked on merging them into Python's version of :mod:`unittest`.
933
934* :meth:`assertIsNone` and :meth:`assertIsNotNone` take one
935 expression and verify that the result is or is not ``None``.
936
937* :meth:`assertIs` and :meth:`assertIsNot` take two values and check
938 whether the two values evaluate to the same object or not.
939 (Added by Michael Foord; :issue:`2578`.)
940
Benjamin Petersona28e7022010-01-09 18:53:06 +0000941* :meth:`assertIsInstance` and :meth:`assertNotIsInstance` check whether
942 the resulting object is an instance of a particular class, or of
943 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
944
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000945* :meth:`assertGreater`, :meth:`assertGreaterEqual`,
946 :meth:`assertLess`, and :meth:`assertLessEqual` compare
947 two quantities.
948
949* :meth:`assertMultiLineEqual` compares two strings, and if they're
950 not equal, displays a helpful comparison that highlights the
951 differences in the two strings.
952
953* :meth:`assertRegexpMatches` checks whether its first argument is a
954 string matching a regular expression provided as its second argument.
955
956* :meth:`assertRaisesRegexp` checks whether a particular exception
957 is raised, and then also checks that the string representation of
958 the exception matches the provided regular expression.
959
960* :meth:`assertIn` and :meth:`assertNotIn` tests whether
961 *first* is or is not in *second*.
962
963* :meth:`assertSameElements` tests whether two provided sequences
964 contain the same elements.
965
966* :meth:`assertSetEqual` compares whether two sets are equal, and
967 only reports the differences between the sets in case of error.
968
969* Similarly, :meth:`assertListEqual` and :meth:`assertTupleEqual`
970 compare the specified types and explain the differences.
971 More generally, :meth:`assertSequenceEqual` compares two sequences
972 and can optionally check whether both sequences are of a
973 particular type.
974
975* :meth:`assertDictEqual` compares two dictionaries and reports the
976 differences. :meth:`assertDictContainsSubset` checks whether
977 all of the key/value pairs in *first* are found in *second*.
978
979* :meth:`assertAlmostEqual` and :meth:`assertNotAlmostEqual` short-circuit
980 (automatically pass or fail without checking decimal places) if the objects
981 are equal.
982
983* :meth:`loadTestsFromName` properly honors the ``suiteClass`` attribute of
984 the :class:`TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
985
986* A new hook, :meth:`addTypeEqualityFunc` takes a type object and a
987 function. The :meth:`assertEqual` method will use the function
988 when both of the objects being compared are of the specified type.
989 This function should compare the two objects and raise an
990 exception if they don't match; it's a good idea for the function
991 to provide additional information about why the two objects are
992 matching, much as the new sequence comparison methods do.
993
994:func:`unittest.main` now takes an optional ``exit`` argument.
995If False ``main`` doesn't call :func:`sys.exit` allowing it to
996be used from the interactive interpreter. :issue:`3379`.
997
998:class:`TestResult` has new :meth:`startTestRun` and
999:meth:`stopTestRun` methods; called immediately before
1000and after a test run. :issue:`5728` by Robert Collins.
1001
1002With all these changes, the :file:`unittest.py` was becoming awkwardly
1003large, so the module was turned into a package and the code split into
1004several files (by Benjamin Peterson). This doesn't affect how the
1005module is imported.
1006
1007
1008.. _importlib-section:
1009
Benjamin Petersond23f8222009-04-05 19:13:16 +00001010importlib: Importing Modules
1011------------------------------
1012
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001013Python 3.1 includes the :mod:`importlib` package, a re-implementation
1014of the logic underlying Python's :keyword:`import` statement.
1015:mod:`importlib` is useful for implementors of Python interpreters and
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001016to users who wish to write new importers that can participate in the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001017import process. Python 2.7 doesn't contain the complete
1018:mod:`importlib` package, but instead has a tiny subset that contains
1019a single function, :func:`import_module`.
1020
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001021``import_module(name, package=None)`` imports a module. *name* is
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001022a string containing the module or package's name. It's possible to do
1023relative imports by providing a string that begins with a ``.``
1024character, such as ``..utils.errors``. For relative imports, the
1025*package* argument must be provided and is the name of the package that
1026will be used as the anchor for
1027the relative import. :func:`import_module` both inserts the imported
1028module into ``sys.modules`` and returns the module object.
1029
1030Here are some examples::
1031
1032 >>> from importlib import import_module
1033 >>> anydbm = import_module('anydbm') # Standard absolute import
1034 >>> anydbm
1035 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1036 >>> # Relative import
1037 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1038 >>> sysconfig
1039 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1040
1041:mod:`importlib` was implemented by Brett Cannon and introduced in
1042Python 3.1.
1043
Benjamin Petersond23f8222009-04-05 19:13:16 +00001044
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001045ttk: Themed Widgets for Tk
1046--------------------------
1047
1048Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1049widgets but have a more customizable appearance and can therefore more
1050closely resemble the native platform's widgets. This widget
1051set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1052on being added to Tcl/Tck release 8.5.
1053
1054XXX write a brief discussion and an example here.
1055
1056The :mod:`ttk` module was written by Guilherme Polo and added in
1057:issue:`2983`. An alternate version called ``Tile.py``, written by
1058Martin Franklin and maintained by Kevin Walzer, was proposed for
1059inclusion in :issue:`2618`, but the authors argued that Guilherme
1060Polo's work was more comprehensive.
1061
Georg Brandl4d131ee2009-11-18 18:53:14 +00001062
1063Deprecations and Removals
1064=========================
1065
1066* :func:`contextlib.nested`, which allows handling more than one context manager
1067 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1068 supports multiple context managers syntactically now.
1069
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001070.. ======================================================================
1071
1072
1073Build and C API Changes
1074=======================
1075
1076Changes to Python's build process and to the C API include:
1077
Georg Brandl1f01deb2009-01-03 22:47:39 +00001078* If you use the :file:`.gdbinit` file provided with Python,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001079 the "pyo" macro in the 2.7 version now works correctly when the thread being
1080 debugged doesn't hold the GIL; the macro now acquires it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001081 (Contributed by Victor Stinner; :issue:`3632`.)
1082
Benjamin Petersond23f8222009-04-05 19:13:16 +00001083* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001084 worker thread submit notifications to the main Python thread. This
1085 is particularly useful for asynchronous IO operations.
1086 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1087
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001088* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1089 only the filename, function name, and first line number are required.
1090 This is useful to extension modules that are attempting to
1091 construct a more useful traceback stack. Previously such
1092 extensions needed to call :cfunc:`PyCode_New`, which had many
1093 more arguments. (Added by Jeffrey Yasskin.)
1094
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001095* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1096 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1097 but takes an extra ``char *`` argument containing the docstring for the
1098 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1099 :issue:`7033`.)
1100
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001101* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1102 and returns the line number that the frame is currently executing.
1103 Previously code would need to get the index of the bytecode
1104 instruction currently executing, and then look up the line number
1105 corresponding to that address. (Added by Jeffrey Yasskin.)
1106
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001107* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1108 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1109 integer as a C :ctype:`long` or :ctype:`long long`.
1110 If the number is too large to fit into
1111 the output type, an *overflow* flag is set and returned to the caller.
1112 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001113
Benjamin Petersona28e7022010-01-09 18:53:06 +00001114* New function: stemming from the rewrite of string-to-float conversion,
1115 a new :cfunc:`PyOS_string_to_double` function was added. The old
1116 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1117 are now deprecated.
1118
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001119* New macros: the Python header files now define the following macros:
1120 :cmacro:`Py_ISALNUM`,
1121 :cmacro:`Py_ISALPHA`,
1122 :cmacro:`Py_ISDIGIT`,
1123 :cmacro:`Py_ISLOWER`,
1124 :cmacro:`Py_ISSPACE`,
1125 :cmacro:`Py_ISUPPER`,
1126 :cmacro:`Py_ISXDIGIT`,
1127 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1128 All of these functions are analogous to the C
1129 standard macros for classifying characters, but ignore the current
1130 locale setting, because in
1131 several places Python needs to analyze characters in a
1132 locale-independent way. (Added by Eric Smith;
1133 :issue:`5793`.)
1134
1135 .. XXX these macros don't seem to be described in the c-api docs.
1136
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001137* New format codes: the :cfunc:`PyFormat_FromString`,
1138 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1139 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1140 C's :ctype:`long long` types.
1141 (Contributed by Mark Dickinson; :issue:`7228`.)
1142
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001143* The complicated interaction between threads and process forking has
1144 been changed. Previously, the child process created by
1145 :func:`os.fork` might fail because the child is created with only a
1146 single thread running, the thread performing the :func:`os.fork`.
1147 If other threads were holding a lock, such as Python's import lock,
1148 when the fork was performed, the lock would still be marked as
1149 "held" in the new process. But in the child process nothing would
1150 ever release the lock, since the other threads weren't replicated,
1151 and the child process would no longer be able to perform imports.
1152
1153 Python 2.7 now acquires the import lock before performing an
1154 :func:`os.fork`, and will also clean up any locks created using the
1155 :mod:`threading` module. C extension modules that have internal
1156 locks, or that call :cfunc:`fork()` themselves, will not benefit
1157 from this clean-up.
1158
1159 (Fixed by Thomas Wouters; :issue:`1590864`.)
1160
Benjamin Petersona28e7022010-01-09 18:53:06 +00001161* The :cfunc:`Py_Finalize` function now calls the internal
1162 :func:`threading._shutdown` function; this prevents some exceptions from
1163 being raised when an interpreter shuts down.
1164 (Patch by Adam Olsen; :issue:`1722344`.)
1165
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001166* Global symbols defined by the :mod:`ctypes` module are now prefixed
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001167 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001168 Heller; :issue:`3102`.)
1169
Benjamin Petersona28e7022010-01-09 18:53:06 +00001170* New configure option: the :option:`--with-system-expat` switch allows
1171 building the :mod:`pyexpat` module to use the system Expat library.
1172 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1173
1174* New configure option: Compiling Python with the
1175 :option:`--with-valgrind` option will now disable the pymalloc
1176 allocator, which is difficult for the Valgrind to analyze correctly.
1177 Valgrind will therefore be better at detecting memory leaks and
1178 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1179
1180* New configure option: you can now supply no arguments to
1181 :option:`--with-dbmliborder=` in order to build none of the various
1182 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1183 :issue:`6491`.)
1184
Benjamin Petersond23f8222009-04-05 19:13:16 +00001185* The :program:`configure` script now checks for floating-point rounding bugs
1186 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1187 preprocessor definition. No code currently uses this definition,
1188 but it's available if anyone wishes to use it.
1189 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001190
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001191* The build process now creates the necessary files for pkg-config
1192 support. (Contributed by Clinton Roy; :issue:`3585`.)
1193
1194* The build process now supports Subversion 1.7. (Contributed by
1195 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1196
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001197
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001198.. ======================================================================
1199
1200Port-Specific Changes: Windows
1201-----------------------------------
1202
Georg Brandl1f01deb2009-01-03 22:47:39 +00001203* The :mod:`msvcrt` module now contains some constants from
1204 the :file:`crtassem.h` header file:
1205 :data:`CRT_ASSEMBLY_VERSION`,
1206 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1207 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001208 (Contributed by David Cournapeau; :issue:`4365`.)
1209
1210* The new :cfunc:`_beginthreadex` API is used to start threads, and
1211 the native thread-local storage functions are now used.
1212 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001213
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001214* The :func:`os.listdir` function now correctly fails
1215 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1216
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001217* The :mod:`mimelib` module will now read the MIME database from
1218 the Windows registry when initializing.
1219 (Patch by Gabriel Genellina; :issue:`4969`.)
1220
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001221.. ======================================================================
1222
1223Port-Specific Changes: Mac OS X
1224-----------------------------------
1225
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001226* The path ``/Library/Python/2.7/site-packages`` is now appended to
Benjamin Petersond23f8222009-04-05 19:13:16 +00001227 ``sys.path``, in order to share added packages between the system
1228 installation and a user-installed copy of the same version.
1229 (Changed by Ronald Oussoren; :issue:`4865`.)
1230
1231
1232Other Changes and Fixes
1233=======================
1234
1235* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1236 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001237 attributes of the resulting code objects are overwritten when the
1238 original filename is obsolete. This can happen if the file has been
1239 renamed, moved, or is accessed through different paths. (Patch by
1240 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001241
1242* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1243 switch that takes an integer that will be used as the random seed
1244 for the :option:`-r` option that executes tests in random order.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001245 The :option:`-r` option also reports the seed that was used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001246 (Added by Collin Winter.)
1247
Benjamin Petersona28e7022010-01-09 18:53:06 +00001248* Another :file:`regrtest.py` switch is :option:`-j`, which
1249 takes an integer specifying how many tests run in parallel. This
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001250 allows reducing the total runtime on multi-core machines.
Antoine Pitrou88909542009-06-29 13:54:42 +00001251 This option is compatible with several other options, including the
1252 :option:`-R` switch which is known to produce long runtimes.
Benjamin Petersona28e7022010-01-09 18:53:06 +00001253 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1254 with a new :option:`-F` switch that runs selected tests in a loop
1255 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001256
1257.. ======================================================================
1258
1259Porting to Python 2.7
1260=====================
1261
1262This section lists previously described changes and other bugfixes
1263that may require changes to your code:
1264
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001265* When using :class:`Decimal` instances with a string's
1266 :meth:`format` method, the default alignment was previously
1267 left-alignment. This has been changed to right-alignment, which might
1268 change the output of your programs.
1269 (Changed by Mark Dickinson; :issue:`6857`.)
1270
1271 Another :meth:`format`-related change: the default precision used
1272 for floating-point and complex numbers was changed from 6 decimal
1273 places to 12, which matches the precision used by :func:`str`.
1274 (Changed by Eric Smith; :issue:`5920`.)
1275
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001276* Because of an optimization for the :keyword:`with` statement, the special
1277 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1278 type, and cannot be directly attached to the object's instance. This
1279 affects new-style classes (derived from :class:`object`) and C extension
1280 types. (:issue:`6101`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001281
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001282* The :meth:`readline` method of :class:`StringIO` objects now does
1283 nothing when a negative length is requested, as other file-like
1284 objects do. (:issue:`7348`).
1285
Benjamin Petersona28e7022010-01-09 18:53:06 +00001286For C extensions:
1287
1288* C extensions that use integer format codes with the ``PyArg_Parse*``
1289 family of functions will now raise a :exc:`TypeError` exception
1290 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1291
1292* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1293 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1294 which are now deprecated.
1295
1296
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001297.. ======================================================================
1298
1299
1300.. _acks27:
1301
1302Acknowledgements
1303================
1304
1305The author would like to thank the following people for offering
1306suggestions, corrections and assistance with various drafts of this
Benjamin Peterson97dd9872009-12-13 01:23:39 +00001307article: Ryan Lovett, Hugh Secker-Walker.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001308