blob: 593c9fa22aa75ab810c4c11837c5bd7d835f2f6f [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
Ezio Melotti6c96ffe2010-04-07 04:27:14 +000011.. Big jobs: argparse, ElementTree 1.3, pep 391, 3106, sysconfig
12.. unittest test discovery
Benjamin Peterson08bf91c2010-04-11 16:12:57 +000013.. hyperlink all the methods & functions.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +000014
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000015.. $Id$
16 Rules for maintenance:
17
18 * Anyone can add text to this document. Do not spend very much time
19 on the wording of your changes, because your text will probably
20 get rewritten to some degree.
21
22 * The maintainer will go through Misc/NEWS periodically and add
23 changes; it's therefore more important to add your changes to
24 Misc/NEWS than to this file.
25
26 * This is not a complete list of every single change; completeness
27 is the purpose of Misc/NEWS. Some changes I consider too small
28 or esoteric to include. If such a change is added to the text,
29 I'll just remove it. (This is another reason you shouldn't spend
30 too much time on writing your addition.)
31
32 * If you want to draw your new text to the attention of the
33 maintainer, add 'XXX' to the beginning of the paragraph or
34 section.
35
36 * It's OK to just add a fragmentary note about a change. For
37 example: "XXX Describe the transmogrify() function added to the
38 socket module." The maintainer will research the change and
39 write the necessary text.
40
41 * You can comment out your additions if you like, but it's not
42 necessary (especially when a final release is some months away).
43
Ezio Melotti6c96ffe2010-04-07 04:27:14 +000044 * Credit the author of a patch or bugfix. Just the name is
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000045 sufficient; the e-mail address isn't necessary.
46
47 * It's helpful to add the bug/patch number in a parenthetical comment.
48
49 XXX Describe the transmogrify() function added to the socket
50 module.
51 (Contributed by P.Y. Developer; :issue:`12345`.)
52
53 This saves the maintainer some effort going through the SVN logs
54 when researching a change.
55
Benjamin Peterson9eea4802009-12-31 03:31:15 +000056This article explains the new features in Python 2.7. The final
57release of 2.7 is currently scheduled for June 2010; the detailed
58schedule is described in :pep:`373`.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000059
Benjamin Petersond69fe2a2010-02-03 02:59:43 +000060Python 2.7 is planned to be the last major release in the 2.x series.
61Though more major releases have not been absolutely ruled out, it's
62likely that the 2.7 release will have an extended period of
63maintenance compared to earlier 2.x versions.
64
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000065.. Compare with previous release in 2 - 3 sentences here.
66 add hyperlink when the documentation becomes available online.
67
Benjamin Petersonf6489f92009-11-25 17:46:26 +000068.. _whatsnew27-python31:
69
70Python 3.1 Features
71=======================
Benjamin Petersond23f8222009-04-05 19:13:16 +000072
73Much as Python 2.6 incorporated features from Python 3.0,
Benjamin Petersonf6489f92009-11-25 17:46:26 +000074version 2.7 incorporates some of the new features
75in Python 3.1. The 2.x series continues to provide tools
76for migrating to the 3.x series.
Benjamin Petersond23f8222009-04-05 19:13:16 +000077
Benjamin Petersonf6489f92009-11-25 17:46:26 +000078A partial list of 3.1 features that were backported to 2.7:
79
80* A version of the :mod:`io` library, rewritten in C for performance.
81* The ordered-dictionary type described in :ref:`pep-0372`.
Benjamin Peterson97dd9872009-12-13 01:23:39 +000082* The new format specifier described in :ref:`pep-0378`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000083* The :class:`memoryview` object.
84* A small subset of the :mod:`importlib` module `described below <#importlib-section>`__.
Benjamin Peterson9eea4802009-12-31 03:31:15 +000085* Float-to-string and string-to-float conversions now round their
86 results more correctly. And :func:`repr` of a floating-point
87 number *x* returns a result that's guaranteed to round back to the
88 same number when converted back to a string.
89* The :cfunc:`PyLong_AsLongAndOverflow` C API function.
Benjamin Petersond23f8222009-04-05 19:13:16 +000090
91One porting change: the :option:`-3` switch now automatically
92enables the :option:`-Qwarn` switch that causes warnings
93about using classic division with integers and long integers.
94
Benjamin Petersonf6489f92009-11-25 17:46:26 +000095Other new Python3-mode warnings include:
96
97* :func:`operator.isCallable` and :func:`operator.sequenceIncludes`,
98 which are not supported in 3.x.
99
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000100.. ========================================================================
101.. Large, PEP-level features and changes should be described here.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000102.. ========================================================================
103
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000104.. _pep-0372:
105
Benjamin Petersond23f8222009-04-05 19:13:16 +0000106PEP 372: Adding an ordered dictionary to collections
107====================================================
108
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000109Regular Python dictionaries iterate over key/value pairs in arbitrary order.
110Over the years, a number of authors have written alternative implementations
111that remember the order that the keys were originally inserted. Based on
112the experiences from those implementations, a new
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000113:class:`~collections.OrderedDict` class has been introduced in the
114:mod:`collections` module.
Benjamin Petersond23f8222009-04-05 19:13:16 +0000115
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000116The :class:`~collections.OrderedDict` API is substantially the same as regular
117dictionaries but will iterate over keys and values in a guaranteed order
118depending on when a key was first inserted::
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000119
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000120 >>> from collections import OrderedDict
121 >>> d = OrderedDict([('first', 1), ('second', 2),
122 ... ('third', 3)])
123 >>> d.items()
124 [('first', 1), ('second', 2), ('third', 3)]
125
126If a new entry overwrites an existing entry, the original insertion
127position is left unchanged::
128
129 >>> d['second'] = 4
130 >>> d.items()
131 [('first', 1), ('second', 4), ('third', 3)]
132
133Deleting an entry and reinserting it will move it to the end::
134
135 >>> del d['second']
136 >>> d['second'] = 5
137 >>> d.items()
138 [('first', 1), ('third', 3), ('second', 5)]
139
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000140The :meth:`~collections.OrderedDict.popitem` method has an optional *last*
141argument that defaults to True. If *last* is True, the most recently
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000142added key is returned and removed; if it's False, the
143oldest key is selected::
144
145 >>> od = OrderedDict([(x,0) for x in range(20)])
146 >>> od.popitem()
147 (19, 0)
148 >>> od.popitem()
149 (18, 0)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000150 >>> od.popitem(last=False)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000151 (0, 0)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000152 >>> od.popitem(last=False)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000153 (1, 0)
154
155Comparing two ordered dictionaries checks both the keys and values,
156and requires that the insertion order was the same::
157
158 >>> od1 = OrderedDict([('first', 1), ('second', 2),
159 ... ('third', 3)])
160 >>> od2 = OrderedDict([('third', 3), ('first', 1),
161 ... ('second', 2)])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000162 >>> od1 == od2
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000163 False
164 >>> # Move 'third' key to the end
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000165 >>> del od2['third']; od2['third'] = 3
166 >>> od1 == od2
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000167 True
168
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000169Comparing an :class:`~collections.OrderedDict` with a regular dictionary
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000170ignores the insertion order and just compares the keys and values.
171
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000172How does the :class:`~collections.OrderedDict` work? It maintains a
173doubly-linked list of keys, appending new keys to the list as they're inserted.
174A secondary dictionary maps keys to their corresponding list node, so
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000175deletion doesn't have to traverse the entire linked list and therefore
176remains O(1).
177
178.. XXX check O(1)-ness with Raymond
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000179.. Also check if the 'somenamedtuple' in the collection module should
180.. be replaced/removed in order to use
181.. :meth:`~collections.namedtuple._asdict()` (see below)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000182
183The standard library now supports use of ordered dictionaries in several
184modules. The :mod:`configparser` module uses them by default. This lets
185configuration files be read, modified, and then written back in their original
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000186order. The :meth:`~collections.somenamedtuple._asdict()` method for
187:func:`collections.namedtuple` now returns an ordered dictionary with the
188values appearing in the same order as the underlying tuple indices.
189The :mod:`json` module is being built-out with an *object_pairs_hook* to allow
190OrderedDicts to be built by the decoder.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000191Support was also added for third-party tools like `PyYAML <http://pyyaml.org/>`_.
192
193.. seealso::
194
195 :pep:`372` - Adding an ordered dictionary to collections
196 PEP written by Armin Ronacher and Raymond Hettinger;
197 implemented by Raymond Hettinger.
198
199.. _pep-0378:
200
201PEP 378: Format Specifier for Thousands Separator
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000202=================================================
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000203
204To make program output more readable, it can be useful to add
205separators to large numbers and render them as
20618,446,744,073,709,551,616 instead of 18446744073709551616.
207
208The fully general solution for doing this is the :mod:`locale` module,
209which can use different separators ("," in North America, "." in
210Europe) and different grouping sizes, but :mod:`locale` is complicated
211to use and unsuitable for multi-threaded applications where different
212threads are producing output for different locales.
213
214Therefore, a simple comma-grouping mechanism has been added to the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000215mini-language used by the :meth:`str.format` method. When
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000216formatting a floating-point number, simply include a comma between the
217width and the precision::
218
Eric Smith2b1a1162010-04-06 14:57:57 +0000219 >>> '{:20,.2f}'.format(18446744073709551616.0)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000220 '18,446,744,073,709,551,616.00'
221
Eric Smith21e85c72010-04-06 15:21:59 +0000222When formatting an integer, include the comma after the width:
223
224 >>> '{:20,d}'.format(18446744073709551616)
225 '18,446,744,073,709,551,616'
226
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000227This mechanism is not adaptable at all; commas are always used as the
228separator and the grouping is always into three-digit groups. The
229comma-formatting mechanism isn't as general as the :mod:`locale`
230module, but it's easier to use.
231
232.. XXX "Format String Syntax" in string.rst could use many more examples.
233
234.. seealso::
235
236 :pep:`378` - Format Specifier for Thousands Separator
237 PEP written by Raymond Hettinger; implemented by Eric Smith.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000238
Benjamin Peterson9895f912010-03-21 22:05:32 +0000239PEP 389: The argparse Module for Parsing Command Lines
240======================================================
241
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000242The :mod:`argparse` module for parsing command-line arguments was
243added, intended as a more powerful replacement for the
244:mod:`optparse` module.
245
246This means Python now supports three different modules for parsing
247command-line arguments: :mod:`getopt`, :mod:`optparse`, and
248:mod:`argparse`. The :mod:`getopt` module closely resembles the C
249:cfunc:`getopt` function, so it remains useful if you're writing a
250Python prototype that will eventually be rewritten in C.
251:mod:`optparse` becomes redundant, but there are no plans to remove it
252because there are many scripts still using it, and there's no
253automated way to update these scripts. (Making the :mod:`argparse`
254API consistent with :mod:`optparse`'s interface was discussed but
255rejected as too messy and difficult.)
256
257To summarize, if you're writing a new script and don't need to worry
258about compatibility with earlier versions of Python, use
259:mod:`argparse` instead of :mod:`optparse`.
260
261XXX need an example
Benjamin Peterson9895f912010-03-21 22:05:32 +0000262
263.. seealso::
264
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000265 `argparse module documentation <http://docs.python.org/dev/library/argparse.html>`__
266
267 `Upgrading optparse code to use argparse <http://docs.python.org/dev/library/argparse.html#upgrading-optparse-code>`__
268
Benjamin Peterson9895f912010-03-21 22:05:32 +0000269 :pep:`389` - argparse - New Command Line Parsing Module
270 PEP written and implemented by Steven Bethard.
271
272PEP 391: Dictionary-Based Configuration For Logging
273====================================================
274
275XXX write this section.
276
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000277Two smaller enhancements to the logging module are:
278
279.. rev79293
280
281* :class:`Logger` instances gained a :meth:`getChild` that retrieves a
282 descendant logger using a relative path. For example,
283 once you retrieve a logger by doing ``log = getLogger('app')``,
284 calling ``log.getChild('network.listen')`` is equivalent to
285 ``getLogger('app.network.listen')``.
286
287* The :class:`LoggerAdapter` class gained a :meth:`isEnabledFor` method
288 that takes a *level* and returns whether the underlying logger would
289 process a message of that level of importance.
290
Benjamin Peterson9895f912010-03-21 22:05:32 +0000291.. seealso::
292
293 :pep:`391` - Dictionary-Based Configuration For Logging
294 PEP written and implemented by Vinay Sajip.
295
296PEP 3106: Dictionary Views
297====================================================
298
299XXX write this section.
300
301.. seealso::
302
303 :pep:`3106` - Revamping dict.keys(), .values() and .items()
304 PEP written by Guido van Rossum.
305 Backported to 2.7 by Alexandre Vassalotti; :issue:`1967`.
306
307
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000308Other Language Changes
309======================
310
311Some smaller changes made to the core Python language are:
312
Benjamin Peterson9895f912010-03-21 22:05:32 +0000313* The syntax for set literals has been backported from Python 3.x.
314 Curly brackets are used to surround the contents of the resulting
315 mutable set; set literals are
316 distinguished from dictionaries by not containing colons and values.
317 ``{}`` continues to represent an empty dictionary; use
318 ``set()`` for an empty set.
319
320 >>> {1,2,3,4,5}
321 set([1, 2, 3, 4, 5])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000322 >>> set() # empty set
Benjamin Peterson9895f912010-03-21 22:05:32 +0000323 set([])
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000324 >>> {} # empty dict
Benjamin Peterson9895f912010-03-21 22:05:32 +0000325 {}
326
327 Backported by Alexandre Vassalotti; :issue:`2335`.
328
329* Dictionary and set comprehensions are another feature backported from
330 3.x, generalizing list/generator comprehensions to use
331 the literal syntax for sets and dictionaries.
332
333 >>> {x:x*x for x in range(6)}
334 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
335 >>> {'a'*x for x in range(6)}
336 set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])
337
338 Backported by Alexandre Vassalotti; :issue:`2333`.
339
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000340* The :keyword:`with` statement can now use multiple context managers
341 in one statement. Context managers are processed from left to right
342 and each one is treated as beginning a new :keyword:`with` statement.
343 This means that::
344
345 with A() as a, B() as b:
346 ... suite of statements ...
347
348 is equivalent to::
349
350 with A() as a:
351 with B() as b:
352 ... suite of statements ...
353
354 The :func:`contextlib.nested` function provides a very similar
355 function, so it's no longer necessary and has been deprecated.
356
357 (Proposed in http://codereview.appspot.com/53094; implemented by
358 Georg Brandl.)
359
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000360* Conversions between floating-point numbers and strings are
361 now correctly rounded on most platforms. These conversions occur
362 in many different places: :func:`str` on
363 floats and complex numbers; the :class:`float` and :class:`complex`
364 constructors;
365 numeric formatting; serialization and
366 deserialization of floats and complex numbers using the
367 :mod:`marshal`, :mod:`pickle`
368 and :mod:`json` modules;
369 parsing of float and imaginary literals in Python code;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000370 and :class:`~decimal.Decimal`-to-float conversion.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000371
372 Related to this, the :func:`repr` of a floating-point number *x*
373 now returns a result based on the shortest decimal string that's
374 guaranteed to round back to *x* under correct rounding (with
375 round-half-to-even rounding mode). Previously it gave a string
376 based on rounding x to 17 decimal digits.
377
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000378 .. maybe add an example?
379
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000380 The rounding library responsible for this improvement works on
381 Windows, and on Unix platforms using the gcc, icc, or suncc
382 compilers. There may be a small number of platforms where correct
383 operation of this code cannot be guaranteed, so the code is not
Benjamin Petersona28e7022010-01-09 18:53:06 +0000384 used on such systems. You can find out which code is being used
385 by checking :data:`sys.float_repr_style`, which will be ``short``
386 if the new code is in use and ``legacy`` if it isn't.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000387
Mark Dickinson0bc8f902010-01-07 09:31:48 +0000388 Implemented by Eric Smith and Mark Dickinson, using David Gay's
389 :file:`dtoa.c` library; :issue:`7117`.
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000390
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000391* The :meth:`str.format` method now supports automatic numbering of the replacement
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000392 fields. This makes using :meth:`str.format` more closely resemble using
393 ``%s`` formatting::
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000394
395 >>> '{}:{}:{}'.format(2009, 04, 'Sunday')
396 '2009:4:Sunday'
397 >>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
398 '2009:4:Sunday'
399
Benjamin Peterson3f96a872009-04-11 20:58:12 +0000400 The auto-numbering takes the fields from left to right, so the first ``{...}``
401 specifier will use the first argument to :meth:`str.format`, the next
402 specifier will use the next argument, and so on. You can't mix auto-numbering
403 and explicit numbering -- either number all of your specifier fields or none
404 of them -- but you can mix auto-numbering and named fields, as in the second
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000405 example above. (Contributed by Eric Smith; :issue:`5237`.)
406
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000407 Complex numbers now correctly support usage with :func:`format`,
408 and default to being right-aligned.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000409 Specifying a precision or comma-separation applies to both the real
410 and imaginary parts of the number, but a specified field width and
411 alignment is applied to the whole of the resulting ``1.5+3j``
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000412 output. (Contributed by Eric Smith; :issue:`1588` and :issue:`7988`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000413
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000414 The 'F' format code now always formats its output using uppercase characters,
415 so it will now produce 'INF' and 'NAN'.
416 (Contributed by Eric Smith; :issue:`3382`.)
417
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000418* The :func:`int` and :func:`long` types gained a ``bit_length``
419 method that returns the number of bits necessary to represent
420 its argument in binary::
421
422 >>> n = 37
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000423 >>> bin(n)
Mark Dickinson54bc1ec2008-12-17 16:19:07 +0000424 '0b100101'
425 >>> n.bit_length()
426 6
427 >>> n = 2**123-1
428 >>> n.bit_length()
429 123
430 >>> (n+1).bit_length()
431 124
432
433 (Contributed by Fredrik Johansson and Victor Stinner; :issue:`3439`.)
434
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000435* Conversions from long integers and regular integers to floating
436 point now round differently, returning the floating-point number
437 closest to the number. This doesn't matter for small integers that
438 can be converted exactly, but for large numbers that will
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000439 unavoidably lose precision, Python 2.7 now approximates more
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000440 closely. For example, Python 2.6 computed the following::
441
442 >>> n = 295147905179352891391
443 >>> float(n)
444 2.9514790517935283e+20
445 >>> n - long(float(n))
446 65535L
447
448 Python 2.7's floating-point result is larger, but much closer to the
449 true value::
450
451 >>> n = 295147905179352891391
452 >>> float(n)
453 2.9514790517935289e+20
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000454 >>> n - long(float(n))
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000455 -1L
456
457 (Implemented by Mark Dickinson; :issue:`3166`.)
458
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000459 Integer division is also more accurate in its rounding behaviours. (Also
460 implemented by Mark Dickinson; :issue:`1811`.)
461
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000462* It's now possible for a subclass of the built-in :class:`unicode` type
463 to override the :meth:`__unicode__` method. (Implemented by
464 Victor Stinner; :issue:`1583863`.)
465
466* The :class:`bytearray` type's :meth:`~bytearray.translate` method now accepts
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000467 ``None`` as its first argument. (Fixed by Georg Brandl;
Benjamin Petersond23f8222009-04-05 19:13:16 +0000468 :issue:`4759`.)
Mark Dickinsond72c7b62009-03-20 16:00:49 +0000469
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000470 .. bytearray doesn't seem to be documented
471
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000472* When using ``@classmethod`` and ``@staticmethod`` to wrap
473 methods as class or static methods, the wrapper object now
474 exposes the wrapped function as their :attr:`__func__` attribute.
475 (Contributed by Amaury Forgeot d'Arc, after a suggestion by
476 George Sakkis; :issue:`5982`.)
477
478* A new encoding named "cp720", used primarily for Arabic text, is now
479 supported. (Contributed by Alexander Belchenko and Amaury Forgeot
480 d'Arc; :issue:`1616979`.)
481
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000482* The :class:`file` object will now set the :attr:`filename` attribute
483 on the :exc:`IOError` exception when trying to open a directory
Benjamin Peterson9895f912010-03-21 22:05:32 +0000484 on POSIX platforms (noted by Jan Kaliszewski; :issue:`4764`), and
485 now explicitly checks for and forbids writing to read-only file objects
486 instead of trusting the C library to catch and report the error
487 (fixed by Stefan Krah; :issue:`5677`).
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000488
Benjamin Petersona28e7022010-01-09 18:53:06 +0000489* The Python tokenizer now translates line endings itself, so the
490 :func:`compile` built-in function can now accept code using any
491 line-ending convention. Additionally, it no longer requires that the
492 code end in a newline.
493
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000494* Extra parentheses in function definitions are illegal in Python 3.x,
495 meaning that you get a syntax error from ``def f((x)): pass``. In
496 Python3-warning mode, Python 2.7 will now warn about this odd usage.
497 (Noted by James Lingard; :issue:`7362`.)
498
Benjamin Peterson9895f912010-03-21 22:05:32 +0000499* When a module object is garbage-collected, the module's dictionary is
500 now only cleared if no one else is holding a reference to the
501 dictionary (:issue:`7140`).
502
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000503.. ======================================================================
504
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000505.. _new-27-interpreter:
506
507Interpreter Changes
508-------------------------------
509
510A new environment variable, :envvar:`PYTHONWARNINGS`,
511allows controlling warnings. It should be set to a string
512containing warning settings, equivalent to those
513used with the :option:`-W` switch, separated by commas.
514(Contributed by Brian Curtin; :issue:`7301`.)
515
516For example, the following setting will print warnings every time
517they occur, but turn warnings from the :mod:`Cookie` module into an
518error. (The exact syntax for setting an environment variable varies
519across operating systems and shells, so it may be different for you.)
520
521::
522
523 export PYTHONWARNINGS=all,error:::Cookie:0
524
525
526.. ======================================================================
527
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000528
529Optimizations
530-------------
531
Benjamin Petersond23f8222009-04-05 19:13:16 +0000532Several performance enhancements have been added:
533
534.. * A new :program:`configure` option, :option:`--with-computed-gotos`,
535 compiles the main bytecode interpreter loop using a new dispatch
536 mechanism that gives speedups of up to 20%, depending on the system
537 and benchmark. The new mechanism is only supported on certain
538 compilers, such as gcc, SunPro, and icc.
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000539
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000540* A new opcode was added to perform the initial setup for
541 :keyword:`with` statements, looking up the :meth:`__enter__` and
542 :meth:`__exit__` methods. (Contributed by Benjamin Peterson.)
543
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000544* The garbage collector now performs better for one common usage
545 pattern: when many objects are being allocated without deallocating
546 any of them. This would previously take quadratic
547 time for garbage collection, but now the number of full garbage collections
548 is reduced as the number of objects on the heap grows.
549 The new logic is to only perform a full garbage collection pass when
550 the middle generation has been collected 10 times and when the
551 number of survivor objects from the middle generation exceeds 10% of
552 the number of objects in the oldest generation. (Suggested by Martin
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000553 von Löwis and implemented by Antoine Pitrou; :issue:`4074`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000554
Benjamin Petersond23f8222009-04-05 19:13:16 +0000555* The garbage collector tries to avoid tracking simple containers
556 which can't be part of a cycle. In Python 2.7, this is now true for
557 tuples and dicts containing atomic types (such as ints, strings,
558 etc.). Transitively, a dict containing tuples of atomic types won't
559 be tracked either. This helps reduce the cost of each
560 garbage collection by decreasing the number of objects to be
561 considered and traversed by the collector.
Antoine Pitrou9d81def2009-03-28 19:20:09 +0000562 (Contributed by Antoine Pitrou; :issue:`4688`.)
563
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000564* Long integers are now stored internally either in base 2**15 or in base
Benjamin Petersond23f8222009-04-05 19:13:16 +0000565 2**30, the base being determined at build time. Previously, they
566 were always stored in base 2**15. Using base 2**30 gives
567 significant performance improvements on 64-bit machines, but
568 benchmark results on 32-bit machines have been mixed. Therefore,
569 the default is to use base 2**30 on 64-bit machines and base 2**15
570 on 32-bit machines; on Unix, there's a new configure option
571 :option:`--enable-big-digits` that can be used to override this default.
572
573 Apart from the performance improvements this change should be
574 invisible to end users, with one exception: for testing and
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000575 debugging purposes there's a new structseq :data:`sys.long_info` that
Benjamin Petersond23f8222009-04-05 19:13:16 +0000576 provides information about the internal format, giving the number of
577 bits per digit and the size in bytes of the C type used to store
578 each digit::
579
580 >>> import sys
581 >>> sys.long_info
582 sys.long_info(bits_per_digit=30, sizeof_digit=4)
583
584 (Contributed by Mark Dickinson; :issue:`4258`.)
585
586 Another set of changes made long objects a few bytes smaller: 2 bytes
587 smaller on 32-bit systems and 6 bytes on 64-bit.
588 (Contributed by Mark Dickinson; :issue:`5260`.)
589
590* The division algorithm for long integers has been made faster
591 by tightening the inner loop, doing shifts instead of multiplications,
592 and fixing an unnecessary extra iteration.
593 Various benchmarks show speedups of between 50% and 150% for long
594 integer divisions and modulo operations.
595 (Contributed by Mark Dickinson; :issue:`5512`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000596 Bitwise operations are also significantly faster (initial patch by
597 Gregory Smith; :issue:`1087418`).
Benjamin Petersond23f8222009-04-05 19:13:16 +0000598
599* The implementation of ``%`` checks for the left-side operand being
600 a Python string and special-cases it; this results in a 1-3%
601 performance increase for applications that frequently use ``%``
602 with strings, such as templating libraries.
603 (Implemented by Collin Winter; :issue:`5176`.)
604
605* List comprehensions with an ``if`` condition are compiled into
606 faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7
607 by Jeffrey Yasskin; :issue:`4715`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000608
Benjamin Petersona28e7022010-01-09 18:53:06 +0000609* Converting an integer or long integer to a decimal string was made
610 faster by special-casing base 10 instead of using a generalized
611 conversion function that supports arbitrary bases.
612 (Patch by Gawain Bolton; :issue:`6713`.)
613
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000614* The :meth:`split`, :meth:`replace`, :meth:`rindex`,
615 :meth:`rpartition`, and :meth:`rsplit` methods of string-like types
616 (strings, Unicode strings, and :class:`bytearray` objects) now use a
617 fast reverse-search algorithm instead of a character-by-character
618 scan. This is sometimes faster by a factor of 10. (Added by
619 Florent Xicluna; :issue:`7462` and :issue:`7622`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000620
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000621* The :mod:`pickle` and :mod:`cPickle` modules now automatically
622 intern the strings used for attribute names, reducing memory usage
623 of the objects resulting from unpickling. (Contributed by Jake
624 McGuire; :issue:`5084`.)
625
626* The :mod:`cPickle` module now special-cases dictionaries,
627 nearly halving the time required to pickle them.
628 (Contributed by Collin Winter; :issue:`5670`.)
629
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000630.. ======================================================================
631
Georg Brandl4d131ee2009-11-18 18:53:14 +0000632New and Improved Modules
633========================
Benjamin Petersonf10a79a2008-10-11 00:49:57 +0000634
635As in every release, Python's standard library received a number of
636enhancements and bug fixes. Here's a partial list of the most notable
637changes, sorted alphabetically by module name. Consult the
638:file:`Misc/NEWS` file in the source tree for a more complete list of
639changes, or look through the Subversion logs for all the details.
640
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000641* The :mod:`bdb` module's base debugging class :class:`~bdb.Bdb`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000642 gained a feature for skipping modules. The constructor
643 now takes an iterable containing glob-style patterns such as
644 ``django.*``; the debugger will not step into stack frames
645 from a module that matches one of these patterns.
646 (Contributed by Maru Newby after a suggestion by
647 Senthil Kumaran; :issue:`5142`.)
648
Benjamin Peterson9895f912010-03-21 22:05:32 +0000649* The :mod:`binascii` module now supports the buffer API, so it can be
650 used with :class:`memoryview` instances and other similar buffer objects.
651 (Backported from 3.x by Florent Xicluna; :issue:`7703`.)
652
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000653* Updated module: the :mod:`bsddb` module has been updated from 4.7.2devel9
654 to version 4.8.4 of
655 `the pybsddb package <http://www.jcea.es/programacion/pybsddb.htm>`__.
656 The new version features better Python 3.x compatibility, various bug fixes,
657 and adds several new BerkeleyDB flags and methods.
658 (Updated by Jesús Cea Avión; :issue:`8156`. The pybsddb
659 changelog can be browsed at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)
660
661* The :mod:`bz2` module's :class:`~bz2.BZ2File` now supports the context
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000662 management protocol, so you can write ``with bz2.BZ2File(...) as f: ...``.
663 (Contributed by Hagen Fuerstenau; :issue:`3860`.)
664
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000665* New class: the :class:`~collections.Counter` class in the :mod:`collections`
666 module is useful for tallying data. :class:`~collections.Counter` instances
667 behave mostly like dictionaries but return zero for missing keys instead of
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000668 raising a :exc:`KeyError`:
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000669
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000670 .. doctest::
671 :options: +NORMALIZE_WHITESPACE
672
673 >>> from collections import Counter
674 >>> c = Counter()
675 >>> for letter in 'here is a sample of english text':
676 ... c[letter] += 1
677 ...
678 >>> c
679 Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
680 'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
681 'p': 1, 'r': 1, 'x': 1})
682 >>> c['e']
683 5
684 >>> c['z']
685 0
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000686
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000687 There are two additional :class:`~collections.Counter` methods:
688 :meth:`~collections.Counter.most_common` returns the N most common elements
689 and their counts, and :meth:`~collections.Counter.elements`
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000690 returns an iterator over the contained element, repeating each element
691 as many times as its count::
692
693 >>> c.most_common(5)
694 [(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
695 >>> c.elements() ->
696 'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
697 'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
698 'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000699 's', 's', 'r', 't', 't', 'x'
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000700
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000701 .. maybe it's better to use list(c.elements()) here
702
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000703 Contributed by Raymond Hettinger; :issue:`1696199`.
704
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000705 The new :class:`~collections.OrderedDict` class is described in the earlier
706 section :ref:`pep-0372`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000707
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000708 The :class:`~collections.namedtuple` class now has an optional *rename* parameter.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000709 If *rename* is true, field names that are invalid because they've
Benjamin Petersond23f8222009-04-05 19:13:16 +0000710 been repeated or that aren't legal Python identifiers will be
711 renamed to legal names that are derived from the field's
712 position within the list of fields:
713
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000714 >>> from collections import namedtuple
715 >>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
Benjamin Petersond23f8222009-04-05 19:13:16 +0000716 >>> T._fields
717 ('field1', '_1', '_2', 'field2')
718
719 (Added by Raymond Hettinger; :issue:`1818`.)
720
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000721 The :class:`~collections.deque` data type now has a
722 :meth:`~collections.deque.count` method that returns the number of
723 contained elements equal to the supplied argument *x*, and a
724 :meth:`~collections.deque.reverse` method that reverses the elements
725 of the deque in-place. :class:`deque` also exposes its maximum
726 length as the read-only :attr:`~collections.deque.maxlen` attribute.
727 (Both features added by Raymond Hettinger.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000728
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000729* The :mod:`copy` module's :func:`~copy.deepcopy` function will now
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000730 correctly copy bound instance methods. (Implemented by
731 Robert Collins; :issue:`1515`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000732
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000733* The :mod:`ctypes` module now always converts ``None`` to a C NULL
734 pointer for arguments declared as pointers. (Changed by Thomas
Benjamin Peterson9895f912010-03-21 22:05:32 +0000735 Heller; :issue:`4606`.) The underlying `libffi library
736 <http://sourceware.org/libffi/>`__ has been updated to version
737 3.0.9, containing various fixes for different platforms. (Updated
738 by Matthias Klose; :issue:`8142`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000739
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000740* New method: the :mod:`datetime` module's :class:`~datetime.timedelta` class
741 gained a :meth:`~datetime.timedelta.total_seconds` method that returns the
742 number of seconds in the duration. (Contributed by Brian Quinlan; :issue:`5788`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000743
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000744* New method: the :class:`~decimal.Decimal` class gained a
745 :meth:`~decimal.Decimal.from_float` class method that performs an exact
746 conversion of a floating-point number to a :class:`~decimal.Decimal`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000747 Note that this is an **exact** conversion that strives for the
748 closest decimal approximation to the floating-point representation's value;
749 the resulting decimal value will therefore still include the inaccuracy,
750 if any.
751 For example, ``Decimal.from_float(0.1)`` returns
752 ``Decimal('0.1000000000000000055511151231257827021181583404541015625')``.
753 (Implemented by Raymond Hettinger; :issue:`4796`.)
754
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000755 Most of the methods of the :class:`~decimal.Context` class now accept integers
756 as well as :class:`~decimal.Decimal` instances; the only exceptions are the
757 :meth:`~decimal.Context.canonical` and :meth:`~decimal.Context.is_canonical`
758 methods. (Patch by Juan José Conti; :issue:`7633`.)
759
760 The constructor for :class:`~decimal.Decimal` now accepts non-European
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000761 Unicode characters, such as Arabic-Indic digits. (Contributed by
762 Mark Dickinson; :issue:`6595`.)
763
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000764 When using :class:`~decimal.Decimal` instances with a string's
765 :meth:`~str.format` method, the default alignment was previously
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000766 left-alignment. This has been changed to right-alignment, which seems
767 more sensible for numeric types. (Changed by Mark Dickinson; :issue:`6857`.)
768
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000769* The :class:`~fractions.Fraction` class now accepts two rational numbers
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000770 as arguments to its constructor.
771 (Implemented by Mark Dickinson; :issue:`5812`.)
772
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000773 An oversight was fixed, making the :class:`Fraction` match the other
774 numeric types; ordering comparisons (``<``, ``<=``, ``>``, ``>=``) between
775 fractions and complex numbers now raise a :exc:`TypeError`.
776
777 .. revision 79455
778
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000779* New class: a new :class:`~ftplib.FTP_TLS` class in
780 the :mod:`ftplib` module provides secure FTP
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000781 connections using TLS encapsulation of authentication as well as
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000782 subsequent control and data transfers.
783 (Contributed by Giampaolo Rodola', :issue:`2054`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000784
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000785 The :meth:`~ftplib.FTP.storbinary` method for binary uploads can now restart
786 uploads thanks to an added *rest* parameter (patch by Pablo Mouzo;
787 :issue:`6845`.)
788
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000789* New class decorator: :func:`total_ordering` in the :mod:`functools`
790 module takes a class that defines an :meth:`__eq__` method and one of
791 :meth:`__lt__`, :meth:`__le__`, :meth:`__gt__`, or :meth:`__ge__`,
792 and generates the missing comparison methods. Since the
793 :meth:`__cmp__` method is being deprecated in Python 3.x,
794 this decorator makes it easier to define ordered classes.
795 (Added by Raymond Hettinger; :issue:`5479`.)
796
797 New function: :func:`cmp_to_key` will take an old-style comparison
798 function that expects two arguments and return a new callable that
799 can be used as the *key* parameter to functions such as
800 :func:`sorted`, :func:`min` and :func:`max`, etc. The primary
801 intended use is to help with making code compatible with Python 3.x.
802 (Added by Raymond Hettinger.)
803
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000804* New function: the :mod:`gc` module's :func:`~gc.is_tracked` returns
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000805 true if a given instance is tracked by the garbage collector, false
Benjamin Petersond23f8222009-04-05 19:13:16 +0000806 otherwise. (Contributed by Antoine Pitrou; :issue:`4688`.)
807
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000808* The :mod:`gzip` module's :class:`~gzip.GzipFile` now supports the context
Benjamin Peterson9895f912010-03-21 22:05:32 +0000809 management protocol, so you can write ``with gzip.GzipFile(...) as f: ...``
810 (contributed by Hagen Fuerstenau; :issue:`3860`), and it now implements
811 the :class:`io.BufferedIOBase` ABC, so you can wrap it with
812 :class:`io.BufferedReader` for faster processing
813 (contributed by Nir Aides; :issue:`7471`).
814 It's also now possible to override the modification time
Benjamin Petersond23f8222009-04-05 19:13:16 +0000815 recorded in a gzipped file by providing an optional timestamp to
816 the constructor. (Contributed by Jacques Frechet; :issue:`4272`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000817
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000818 Files in gzip format can be padded with trailing zero bytes; the
819 :mod:`gzip` module will now consume these trailing bytes. (Fixed by
820 Tadek Pietraszek and Brian Curtin; :issue:`2846`.)
821
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000822* New attribute: the :mod:`hashlib` module now has an :attr:`~hashlib.hashlib.algorithms`
823 attribute containing a tuple naming the supported algorithms.
824 In Python 2.7, ``hashlib.algorithms`` contains
825 ``('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')``
826 (Contributed by Carl Chenet; :issue:`7418`.)
827
828* The default :class:`~httplib.HTTPResponse` class used by the :mod:`httplib` module now
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000829 supports buffering, resulting in much faster reading of HTTP responses.
830 (Contributed by Kristjan Valur Jonsson; :issue:`4879`.)
831
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000832 The :class:`~httplib.HTTPConnection` and :class:`~httplib.HTTPSConnection` classes
Benjamin Peterson9895f912010-03-21 22:05:32 +0000833 now support a *source_address* parameter, a ``(host, port)`` 2-tuple
834 giving the source address that will be used for the connection.
835 (Contributed by Eldon Ziegler; :issue:`3972`.)
836
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000837* The :mod:`imaplib` module now supports IPv6 addresses.
838 (Contributed by Derek Morr; :issue:`1655`.)
839
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000840* Updated module: The :mod:`io` library has been upgraded to the version shipped with
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000841 Python 3.1. For 3.1, the I/O library was entirely rewritten in C
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000842 and is 2 to 20 times faster depending on the task being performed. The
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000843 original Python version was renamed to the :mod:`_pyio` module.
844
845 One minor resulting change: the :class:`io.TextIOBase` class now
846 has an :attr:`errors` attribute giving the error setting
847 used for encoding and decoding errors (one of ``'strict'``, ``'replace'``,
848 ``'ignore'``).
849
850 The :class:`io.FileIO` class now raises an :exc:`OSError` when passed
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000851 an invalid file descriptor. (Implemented by Benjamin Peterson;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000852 :issue:`4991`.) The :meth:`~io.IOBase.truncate` method now preserves the
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000853 file position; previously it would change the file position to the
854 end of the new file. (Fixed by Pascal Chambon; :issue:`6939`.)
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000855
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000856* New function: ``itertools.compress(data, selectors)`` takes two
Benjamin Petersond23f8222009-04-05 19:13:16 +0000857 iterators. Elements of *data* are returned if the corresponding
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000858 value in *selectors* is true::
Benjamin Petersond23f8222009-04-05 19:13:16 +0000859
860 itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
861 A, C, E, F
862
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000863 .. maybe here is better to use >>> list(itertools.compress(...)) instead
864
Benjamin Peterson97dd9872009-12-13 01:23:39 +0000865 New function: ``itertools.combinations_with_replacement(iter, r)``
Benjamin Petersond23f8222009-04-05 19:13:16 +0000866 returns all the possible *r*-length combinations of elements from the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000867 iterable *iter*. Unlike :func:`~itertools.combinations`, individual elements
Benjamin Petersond23f8222009-04-05 19:13:16 +0000868 can be repeated in the generated combinations::
869
870 itertools.combinations_with_replacement('abc', 2) =>
871 ('a', 'a'), ('a', 'b'), ('a', 'c'),
872 ('b', 'b'), ('b', 'c'), ('c', 'c')
873
874 Note that elements are treated as unique depending on their position
875 in the input, not their actual values.
876
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000877 The :func:`itertools.count` function now has a *step* argument that
878 allows incrementing by values other than 1. :func:`~itertools.count` also
Benjamin Petersond23f8222009-04-05 19:13:16 +0000879 now allows keyword arguments, and using non-integer values such as
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000880 floats or :class:`~decimal.Decimal` instances. (Implemented by Raymond
Benjamin Petersond23f8222009-04-05 19:13:16 +0000881 Hettinger; :issue:`5032`.)
882
883 :func:`itertools.combinations` and :func:`itertools.product` were
884 previously raising :exc:`ValueError` for values of *r* larger than
885 the input iterable. This was deemed a specification error, so they
886 now return an empty iterator. (Fixed by Raymond Hettinger; :issue:`4816`.)
887
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000888* Updated module: The :mod:`json` module was upgraded to version 2.0.9 of the
Benjamin Petersond23f8222009-04-05 19:13:16 +0000889 simplejson package, which includes a C extension that makes
890 encoding and decoding faster.
891 (Contributed by Bob Ippolito; :issue:`4136`.)
892
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000893 To support the new :class:`collections.OrderedDict` type, :func:`json.load`
Benjamin Petersond23f8222009-04-05 19:13:16 +0000894 now has an optional *object_pairs_hook* parameter that will be called
895 with any object literal that decodes to a list of pairs.
896 (Contributed by Raymond Hettinger; :issue:`5381`.)
897
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000898* New functions: the :mod:`math` module gained
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000899 :func:`~math.erf` and :func:`~math.erfc` for the error function and the complementary error function,
900 :func:`~math.expm1` which computes ``e**x - 1`` with more precision than
901 using :func:`~math.exp` and subtracting 1,
902 :func:`~math.gamma` for the Gamma function, and
903 :func:`~math.lgamma` for the natural log of the Gamma function.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000904 (Contributed by Mark Dickinson and nirinA raseliarison; :issue:`3366`.)
905
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000906* The :mod:`multiprocessing` module's :class:`Manager*` classes
907 can now be passed a callable that will be called whenever
908 a subprocess is started, along with a set of arguments that will be
909 passed to the callable.
910 (Contributed by lekma; :issue:`5585`.)
911
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000912 The :class:`~multiprocessing.Pool` class, which controls a pool of worker processes,
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000913 now has an optional *maxtasksperchild* parameter. Worker processes
914 will perform the specified number of tasks and then exit, causing the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000915 :class:`~multiprocessing.Pool` to start a new worker. This is useful if tasks may leak
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000916 memory or other resources, or if some tasks will cause the worker to
917 become very large.
918 (Contributed by Charles Cazabon; :issue:`6963`.)
919
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000920* The :mod:`nntplib` module now supports IPv6 addresses.
921 (Contributed by Derek Morr; :issue:`1664`.)
922
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000923* New functions: the :mod:`os` module wraps the following POSIX system
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000924 calls: :func:`~os.getresgid` and :func:`~os.getresuid`, which return the
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000925 real, effective, and saved GIDs and UIDs;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000926 :func:`~os.setresgid` and :func:`~os.setresuid`, which set
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000927 real, effective, and saved GIDs and UIDs to new values;
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000928 :func:`~os.initgroups`. (GID/UID functions
Benjamin Peterson9eea4802009-12-31 03:31:15 +0000929 contributed by Travis H.; :issue:`6508`. Support for initgroups added
930 by Jean-Paul Calderone; :issue:`7333`.)
931
Benjamin Peterson9895f912010-03-21 22:05:32 +0000932 The :func:`os.fork` function now re-initializes the import lock in
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000933 the child process; this fixes problems on Solaris when :func:`~os.fork`
Benjamin Peterson9895f912010-03-21 22:05:32 +0000934 is called from a thread. (Fixed by Zsolt Cserna; :issue:`7242`.)
935
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000936* In the :mod:`os.path` module, the :func:`~os.path.normpath` and
937 :func:`~os.path.abspath` functions now preserve Unicode; if their input path
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000938 is a Unicode string, the return value is also a Unicode string.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000939 (:meth:`~os.path.normpath` fixed by Matt Giuca in :issue:`5827`;
940 :meth:`~os.path.abspath` fixed by Ezio Melotti in :issue:`3426`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000941
Benjamin Peterson1010bf32009-01-30 04:00:29 +0000942* The :mod:`pydoc` module now has help for the various symbols that Python
943 uses. You can now do ``help('<<')`` or ``help('@')``, for example.
944 (Contributed by David Laban; :issue:`4739`.)
945
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000946* The :mod:`re` module's :func:`~re.split`, :func:`~re.sub`, and :func:`~re.subn`
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +0000947 now accept an optional *flags* argument, for consistency with the
948 other functions in the module. (Added by Gregory P. Smith.)
949
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000950* New function: in the :mod:`shutil` module, :func:`~shutil.make_archive`
951 takes a filename, archive type (zip or tar-format), and a directory
952 path, and creates an archive containing the directory's contents.
953 (Added by Tarek Ziadé.)
954
955 :mod:`shutil`'s :func:`~shutil.copyfile` and :func:`~shutil.copytree`
956 functions now raise a :exc:`~shutil.SpecialFileError` exception when
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000957 asked to copy a named pipe. Previously the code would treat
958 named pipes like a regular file by opening them for reading, and
959 this would block indefinitely. (Fixed by Antoine Pitrou; :issue:`3002`.)
960
961* New functions: in the :mod:`site` module, three new functions
962 return various site- and user-specific paths.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000963 :func:`~site.getsitepackages` returns a list containing all
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000964 global site-packages directories, and
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000965 :func:`~site.getusersitepackages` returns the path of the user's
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000966 site-packages directory.
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000967 :func:`~site.getuserbase` returns the value of the :envvar:`USER_BASE`
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000968 environment variable, giving the path to a directory that can be used
969 to store data.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +0000970 (Contributed by Tarek Ziadé; :issue:`6693`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000971
Benjamin Peterson9895f912010-03-21 22:05:32 +0000972 The :mod:`site` module now reports exceptions occurring
973 when the :mod:`sitecustomize` module is imported, and will no longer
Florent Xicluna41fe6152010-04-02 18:52:12 +0000974 catch and swallow the :exc:`KeyboardInterrupt` exception. (Fixed by
Benjamin Peterson9895f912010-03-21 22:05:32 +0000975 Victor Stinner; :issue:`3137`.)
976
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000977* The :mod:`socket` module's :class:`~ssl.SSL` objects now support the
Benjamin Peterson9895f912010-03-21 22:05:32 +0000978 buffer API, which fixed a test suite failure. (Fixed by Antoine
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000979 Pitrou; :issue:`7133`.) The version of OpenSSL being used is
980 now available as the module attributes
981 :attr:`OPENSSL_VERSION` (a string),
982 :attr:`OPENSSL_VERSION_INFO` (a 5-tuple), and
983 :attr:`OPENSSL_VERSION_NUMBER` (an integer). (Added by Antoine Pitrou;
984 :issue:`8321`.)
Benjamin Peterson9895f912010-03-21 22:05:32 +0000985
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000986 The :func:`~socket.create_connection` function
Benjamin Peterson9895f912010-03-21 22:05:32 +0000987 gained a *source_address* parameter, a ``(host, port)`` 2-tuple
988 giving the source address that will be used for the connection.
989 (Contributed by Eldon Ziegler; :issue:`3972`.)
990
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000991 The :meth:`~socket.socket.recv_into` and :meth:`~socket.socket.recvfrom_into`
992 methods will now write into objects that support the buffer API, most usefully
Benjamin Peterson9895f912010-03-21 22:05:32 +0000993 the :class:`bytearray` and :class:`memoryview` objects. (Implemented by
994 Antoine Pitrou; :issue:`8104`.)
Benjamin Petersona28e7022010-01-09 18:53:06 +0000995
Ezio Melotti6c96ffe2010-04-07 04:27:14 +0000996* The :mod:`SocketServer` module's :class:`~SocketServer.TCPServer` class now
997 has a :attr:`~SocketServer.TCPServer.disable_nagle_algorithm` class attribute.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000998 The default value is False; if overridden to be True,
999 new request connections will have the TCP_NODELAY option set to
1000 prevent buffering many small sends into a single TCP packet.
1001 (Contributed by Kristjan Valur Jonsson; :issue:`6192`.)
1002
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001003* Updated module: the :mod:`sqlite3` module has been updated to
Benjamin Peterson9895f912010-03-21 22:05:32 +00001004 version 2.6.0 of the `pysqlite package <http://code.google.com/p/pysqlite/>`__. Version 2.6.0 includes a number of bugfixes, and adds
1005 the ability to load SQLite extensions from shared libraries.
1006 Call the ``enable_load_extension(True)`` method to enable extensions,
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001007 and then call :meth:`~sqlite3.Connection.load_extension` to load a particular shared library.
Benjamin Peterson9895f912010-03-21 22:05:32 +00001008 (Updated by Gerhard Häring.)
1009
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001010* The :mod:`struct` module will no longer silently ignore overflow
1011 errors when a value is too large for a particular integer format
1012 code (one of ``bBhHiIlLqQ``); it now always raises a
1013 :exc:`struct.error` exception. (Changed by Mark Dickinson;
1014 :issue:`1523`.)
1015
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001016* New function: the :mod:`subprocess` module's
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001017 :func:`~subprocess.check_output` runs a command with a specified set of arguments
Benjamin Petersond23f8222009-04-05 19:13:16 +00001018 and returns the command's output as a string when the command runs without
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001019 error, or raises a :exc:`~subprocess.CalledProcessError` exception otherwise.
Georg Brandl1f01deb2009-01-03 22:47:39 +00001020
1021 ::
1022
1023 >>> subprocess.check_output(['df', '-h', '.'])
1024 'Filesystem Size Used Avail Capacity Mounted on\n
1025 /dev/disk0s2 52G 49G 3.0G 94% /\n'
1026
1027 >>> subprocess.check_output(['df', '-h', '/bogus'])
1028 ...
1029 subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1
1030
1031 (Contributed by Gregory P. Smith.)
1032
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001033 The :mod:`subprocess` module will now retry its internal system calls
1034 on receiving an :const:`EINTR` signal. (Reported by several people; final
1035 patch by Gregory P. Smith in :issue:`1068268`.)
1036
1037* New function: :func:`~symtable.is_declared_global` in the :mod:`symtable` module
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001038 returns true for variables that are explicitly declared to be global,
1039 false for ones that are implicitly global.
1040 (Contributed by Jeremy Hylton.)
1041
Benjamin Petersond23f8222009-04-05 19:13:16 +00001042* The ``sys.version_info`` value is now a named tuple, with attributes
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001043 named :attr:`major`, :attr:`minor`, :attr:`micro`,
1044 :attr:`releaselevel`, and :attr:`serial`. (Contributed by Ross
1045 Light; :issue:`4285`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001046
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001047 :func:`sys.getwindowsversion` also returns a named tuple,
Ezio Melotti0d85e412010-03-13 00:39:49 +00001048 with attributes named :attr:`major`, :attr:`minor`, :attr:`build`,
1049 :attr:`platform`, :attr:`service_pack`, :attr:`service_pack_major`,
Eric Smithb0869402010-02-03 14:25:10 +00001050 :attr:`service_pack_minor`, :attr:`suite_mask`, and
1051 :attr:`product_type`. (Contributed by Brian Curtin; :issue:`7766`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001052
1053* The :mod:`tarfile` module's default error handling has changed, to
1054 no longer suppress fatal errors. The default error level was previously 0,
1055 which meant that errors would only result in a message being written to the
1056 debug log, but because the debug log is not activated by default,
1057 these errors go unnoticed. The default error level is now 1,
1058 which raises an exception if there's an error.
1059 (Changed by Lars Gustäbel; :issue:`7357`.)
1060
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001061 :mod:`tarfile` now supports filtering the :class:`~tarfile.TarInfo`
1062 objects being added to a tar file. When you call :meth:`~tarfile.TarFile.add`,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001063 instance, you may supply an optional *filter* argument
1064 that's a callable. The *filter* callable will be passed the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001065 :class:`~tarfile.TarInfo` for every file being added, and can modify and return it.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001066 If the callable returns ``None``, the file will be excluded from the
1067 resulting archive. This is more powerful than the existing
1068 *exclude* argument, which has therefore been deprecated.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001069 (Added by Lars Gustäbel; :issue:`6856`.)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001070 The :class:`~tarfile.TarFile` class also now supports the context manager protocol.
Benjamin Peterson9895f912010-03-21 22:05:32 +00001071 (Added by Lars Gustäbel; :issue:`7232`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001072
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001073* The :meth:`~threading.Event.wait` method of the :class:`threading.Event` class
1074 now returns the internal flag on exit. This means the method will usually
1075 return true because :meth:`~threading.Event.wait` is supposed to block until the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001076 internal flag becomes true. The return value will only be false if
1077 a timeout was provided and the operation timed out.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001078 (Contributed by Tim Lesher; :issue:`1674032`.)
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001079
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001080* The Unicode database provided by the :mod:`unicodedata` module is
1081 now used internally to determine which characters are numeric,
1082 whitespace, or represent line breaks. The database also
1083 includes information from the :file:`Unihan.txt` data file (patch
1084 by Anders Chrigström and Amaury Forgeot d'Arc; :issue:`1571184`)
1085 and has been updated to version 5.2.0 (updated by
1086 Florent Xicluna; :issue:`8024`).
Ezio Melotti4c5475d2010-03-22 23:16:42 +00001087
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001088* The :class:`~UserDict.UserDict` class is now a new-style class. (Changed by
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001089 Benjamin Peterson.)
1090
Benjamin Peterson9895f912010-03-21 22:05:32 +00001091* The ElementTree library, :mod:`xml.etree`, no longer escapes
1092 ampersands and angle brackets when outputting an XML processing
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001093 instruction (which looks like ``<?xml-stylesheet href="#style1"?>``)
1094 or comment (which looks like ``<!-- comment -->``).
Benjamin Peterson9895f912010-03-21 22:05:32 +00001095 (Patch by Neil Muller; :issue:`2746`.)
1096
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001097* The :mod:`zipfile` module's :class:`~zipfile.ZipFile` now supports the context
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001098 management protocol, so you can write ``with zipfile.ZipFile(...) as f: ...``.
1099 (Contributed by Brian Curtin; :issue:`5511`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001100
Benjamin Petersond23f8222009-04-05 19:13:16 +00001101 :mod:`zipfile` now supports archiving empty directories and
1102 extracts them correctly. (Fixed by Kuba Wieczorek; :issue:`4710`.)
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001103 Reading files out of an archive is now faster, and interleaving
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001104 :meth:`~zipfile.ZipFile.read` and :meth:`~zipfile.ZipFile.readline` now works correctly.
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001105 (Contributed by Nir Aides; :issue:`7610`.)
1106
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001107 The :func:`~zipfile.is_zipfile` function now
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001108 accepts a file object, in addition to the path names accepted in earlier
1109 versions. (Contributed by Gabriel Genellina; :issue:`4756`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001110
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001111 The :meth:`~zipfile.ZipFile.writestr` method now has an optional *compress_type* parameter
Benjamin Peterson9895f912010-03-21 22:05:32 +00001112 that lets you override the default compression method specified in the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001113 :class:`~zipfile.ZipFile` constructor. (Contributed by Ronald Oussoren;
Benjamin Peterson9895f912010-03-21 22:05:32 +00001114 :issue:`6003`.)
1115
Benjamin Peterson9895f912010-03-21 22:05:32 +00001116
1117New module: sysconfig
1118---------------------------------
1119
1120XXX A new :mod:`sysconfig` module has been extracted from
1121:mod:`distutils` and put in the standard library.
1122
1123The :mod:`sysconfig` module provides access to Python's configuration
1124information like the list of installation paths and the configuration
1125variables relevant for the current platform. (contributed by Tarek)
1126
1127Updated module: ElementTree 1.3
1128---------------------------------
1129
1130XXX write this.
Tarek Ziadé396fad72010-02-23 05:30:31 +00001131
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001132.. ======================================================================
1133.. whole new modules get described in subsections here
1134
Tarek Ziadéba0eacf2010-02-02 23:43:21 +00001135
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001136Unit Testing Enhancements
1137---------------------------------
1138
1139The :mod:`unittest` module was enhanced in several ways.
1140The progress messages now shows 'x' for expected failures
1141and 'u' for unexpected successes when run in verbose mode.
1142(Contributed by Benjamin Peterson.)
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001143Test cases can raise the :exc:`~unittest.SkipTest` exception to skip a test.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001144(:issue:`1034053`.)
1145
1146.. XXX describe test discovery (Contributed by Michael Foord; :issue:`6001`.)
1147
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001148The error messages for :meth:`~unittest.TestCase.assertEqual`,
1149:meth:`~unittest.TestCase.assertTrue`, and :meth:`~unittest.TestCase.assertFalse`
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001150failures now provide more information. If you set the
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001151:attr:`~unittest.TestCase.longMessage` attribute of your :class:`~unittest.TestCase` classes to
1152True, both the standard error message and any additional message you
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001153provide will be printed for failures. (Added by Michael Foord; :issue:`5663`.)
1154
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001155The :meth:`~unittest.TestCase.assertRaises` method now
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001156return a context handler when called without providing a callable
1157object to run. For example, you can write this::
1158
1159 with self.assertRaises(KeyError):
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001160 {}['foo']
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001161
1162(Implemented by Antoine Pitrou; :issue:`4444`.)
1163
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001164.. rev 78774
1165
1166Module- and class-level setup and teardown fixtures are now supported.
1167Modules can contain :func:`~unittest.setUpModule` and :func:`~unittest.tearDownModule`
1168functions. Classes can have :meth:`~unittest.TestCase.setUpClass` and
1169:meth:`~unittest.TestCase.tearDownClass` methods that must be defined as class methods
1170(using ``@classmethod`` or equivalent). These functions and
1171methods are invoked when the test runner switches to a test case in a
1172different module or class.
1173
1174The methods :meth:`~unittest.TestCase.addCleanup` and
1175:meth:`~unittest.TestCase.doCleanups` were added.
1176:meth:`~unittest.TestCase.addCleanup` allows you to add cleanup functions that
1177will be called unconditionally (after :meth:`~unittest.TestCase.setUp` if
1178:meth:`~unittest.TestCase.setUp` fails, otherwise after :meth:`~unittest.TestCase.tearDown`). This allows
1179for much simpler resource allocation and deallocation during tests
1180(:issue:`5679`).
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001181
1182A number of new methods were added that provide more specialized
1183tests. Many of these methods were written by Google engineers
1184for use in their test suites; Gregory P. Smith, Michael Foord, and
1185GvR worked on merging them into Python's version of :mod:`unittest`.
1186
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001187* :meth:`~unittest.TestCase.assertIsNone` and :meth:`~unittest.TestCase.assertIsNotNone` take one
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001188 expression and verify that the result is or is not ``None``.
1189
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001190* :meth:`~unittest.TestCase.assertIs` and :meth:`~unittest.TestCase.assertIsNot`
1191 take two values and check whether the two values evaluate to the same object or not.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001192 (Added by Michael Foord; :issue:`2578`.)
1193
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001194* :meth:`~unittest.TestCase.assertIsInstance` and
1195 :meth:`~unittest.TestCase.assertNotIsInstance` check whether
Benjamin Petersona28e7022010-01-09 18:53:06 +00001196 the resulting object is an instance of a particular class, or of
1197 one of a tuple of classes. (Added by Georg Brandl; :issue:`7031`.)
1198
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001199* :meth:`~unittest.TestCase.assertGreater`, :meth:`~unittest.TestCase.assertGreaterEqual`,
1200 :meth:`~unittest.TestCase.assertLess`, and :meth:`~unittest.TestCase.assertLessEqual` compare
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001201 two quantities.
1202
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001203* :meth:`~unittest.TestCase.assertMultiLineEqual` compares two strings, and if they're
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001204 not equal, displays a helpful comparison that highlights the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001205 differences in the two strings. This comparison is now used by
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001206 default when Unicode strings are compared with :meth:`~unittest.TestCase.assertEqual`.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001207
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001208* :meth:`~unittest.TestCase.assertRegexpMatches` checks whether its first argument is a
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001209 string matching a regular expression provided as its second argument.
1210
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001211 .. XXX add assertNotRegexpMatches see issue 8038
1212
1213* :meth:`~unittest.TestCase.assertRaisesRegexp` checks whether a particular exception
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001214 is raised, and then also checks that the string representation of
1215 the exception matches the provided regular expression.
1216
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001217* :meth:`~unittest.TestCase.assertIn` and :meth:`~unittest.TestCase.assertNotIn`
1218 tests whether *first* is or is not in *second*.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001219
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001220* :meth:`~unittest.TestCase.assertItemsEqual` tests whether two provided sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001221 contain the same elements.
1222
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001223* :meth:`~unittest.TestCase.assertSetEqual` compares whether two sets are equal, and
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001224 only reports the differences between the sets in case of error.
1225
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001226* Similarly, :meth:`~unittest.TestCase.assertListEqual` and :meth:`~unittest.TestCase.assertTupleEqual`
Benjamin Peterson9895f912010-03-21 22:05:32 +00001227 compare the specified types and explain any differences without necessarily
1228 printing their full values; these methods are now used by default
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001229 when comparing lists and tuples using :meth:`~unittest.TestCase.assertEqual`.
1230 More generally, :meth:`~unittest.TestCase.assertSequenceEqual` compares two sequences
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001231 and can optionally check whether both sequences are of a
1232 particular type.
1233
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001234* :meth:`~unittest.TestCase.assertDictEqual` compares two dictionaries and reports the
Benjamin Peterson9895f912010-03-21 22:05:32 +00001235 differences; it's now used by default when you compare two dictionaries
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001236 using :meth:`~unittest.TestCase.assertEqual`. :meth:`~unittest.TestCase.assertDictContainsSubset` checks whether
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001237 all of the key/value pairs in *first* are found in *second*.
1238
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001239* :meth:`~unittest.TestCase.assertAlmostEqual` and :meth:`~unittest.TestCase.assertNotAlmostEqual` test
Benjamin Peterson9895f912010-03-21 22:05:32 +00001240 whether *first* and *second* are approximately equal by computing
1241 their difference, rounding the result to an optionally-specified number
1242 of *places* (the default is 7), and comparing to zero.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001243
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001244* :meth:`~unittest.TestLoader.loadTestsFromName` properly honors the
1245 :attr:`~unittest.TestLoader.suiteClass` attribute of
1246 the :class:`~unittest.TestLoader`. (Fixed by Mark Roddy; :issue:`6866`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001247
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001248* A new hook lets you extend the :meth:`~unittest.TestCase.assertEqual` method to handle
1249 new data types. The :meth:`~unittest.TestCase.addTypeEqualityFunc` method takes a type
1250 object and a function. The function will be used when both of the
1251 objects being compared are of the specified type. This function
1252 should compare the two objects and raise an exception if they don't
1253 match; it's a good idea for the function to provide additional
1254 information about why the two objects are matching, much as the new
1255 sequence comparison methods do.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001256
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001257:func:`unittest.main` now takes an optional ``exit`` argument. If
1258False, :func:`~unittest.main` doesn't call :func:`sys.exit`, allowing it to be
1259used from the interactive interpreter. (Contributed by J. Pablo
1260Fernández; :issue:`3379`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001261
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001262A new command-line switch, :option:`-f` or :option:`--failfast`, makes
1263test execution stop immediately when a test fails instead of
1264continuing to execute further tests. (Suggested by Cliff Dyer and
1265implemented by Michael Foord; :issue:`8074`.)
1266
1267.. XXX document the other new switches
1268
1269:class:`~unittest.TestResult` has new :meth:`~unittest.TestResult.startTestRun` and
1270:meth:`~unittest.TestResult.stopTestRun` methods that are called immediately before
1271and after a test run. (Contributed by Robert Collins; :issue:`5728`.)
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001272
1273With all these changes, the :file:`unittest.py` was becoming awkwardly
1274large, so the module was turned into a package and the code split into
1275several files (by Benjamin Peterson). This doesn't affect how the
1276module is imported.
1277
1278
1279.. _importlib-section:
1280
Benjamin Petersond23f8222009-04-05 19:13:16 +00001281importlib: Importing Modules
1282------------------------------
1283
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001284Python 3.1 includes the :mod:`importlib` package, a re-implementation
1285of the logic underlying Python's :keyword:`import` statement.
1286:mod:`importlib` is useful for implementors of Python interpreters and
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001287to users who wish to write new importers that can participate in the
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001288import process. Python 2.7 doesn't contain the complete
1289:mod:`importlib` package, but instead has a tiny subset that contains
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001290a single function, :func:`~importlib.import_module`.
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001291
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001292``import_module(name, package=None)`` imports a module. *name* is
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001293a string containing the module or package's name. It's possible to do
1294relative imports by providing a string that begins with a ``.``
1295character, such as ``..utils.errors``. For relative imports, the
1296*package* argument must be provided and is the name of the package that
1297will be used as the anchor for
Ezio Melotti6c96ffe2010-04-07 04:27:14 +00001298the relative import. :func:`~importlib.import_module` both inserts the imported
Benjamin Petersonf47ed4a2009-04-11 20:45:40 +00001299module into ``sys.modules`` and returns the module object.
1300
1301Here are some examples::
1302
1303 >>> from importlib import import_module
1304 >>> anydbm = import_module('anydbm') # Standard absolute import
1305 >>> anydbm
1306 <module 'anydbm' from '/p/python/Lib/anydbm.py'>
1307 >>> # Relative import
1308 >>> sysconfig = import_module('..sysconfig', 'distutils.command')
1309 >>> sysconfig
1310 <module 'distutils.sysconfig' from '/p/python/Lib/distutils/sysconfig.pyc'>
1311
1312:mod:`importlib` was implemented by Brett Cannon and introduced in
1313Python 3.1.
1314
Benjamin Petersond23f8222009-04-05 19:13:16 +00001315
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00001316ttk: Themed Widgets for Tk
1317--------------------------
1318
1319Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk
1320widgets but have a more customizable appearance and can therefore more
1321closely resemble the native platform's widgets. This widget
1322set was originally called Tile, but was renamed to Ttk (for "themed Tk")
1323on being added to Tcl/Tck release 8.5.
1324
1325XXX write a brief discussion and an example here.
1326
1327The :mod:`ttk` module was written by Guilherme Polo and added in
1328:issue:`2983`. An alternate version called ``Tile.py``, written by
1329Martin Franklin and maintained by Kevin Walzer, was proposed for
1330inclusion in :issue:`2618`, but the authors argued that Guilherme
1331Polo's work was more comprehensive.
1332
Georg Brandl4d131ee2009-11-18 18:53:14 +00001333
1334Deprecations and Removals
1335=========================
1336
1337* :func:`contextlib.nested`, which allows handling more than one context manager
1338 with one :keyword:`with` statement, has been deprecated; :keyword:`with`
1339 supports multiple context managers syntactically now.
1340
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001341.. ======================================================================
1342
1343
1344Build and C API Changes
1345=======================
1346
1347Changes to Python's build process and to the C API include:
1348
Georg Brandl1f01deb2009-01-03 22:47:39 +00001349* If you use the :file:`.gdbinit` file provided with Python,
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001350 the "pyo" macro in the 2.7 version now works correctly when the thread being
1351 debugged doesn't hold the GIL; the macro now acquires it before printing.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001352 (Contributed by Victor Stinner; :issue:`3632`.)
1353
Benjamin Petersond23f8222009-04-05 19:13:16 +00001354* :cfunc:`Py_AddPendingCall` is now thread-safe, letting any
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001355 worker thread submit notifications to the main Python thread. This
1356 is particularly useful for asynchronous IO operations.
1357 (Contributed by Kristjan Valur Jonsson; :issue:`4293`.)
1358
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001359* New function: :cfunc:`PyCode_NewEmpty` creates an empty code object;
1360 only the filename, function name, and first line number are required.
1361 This is useful to extension modules that are attempting to
1362 construct a more useful traceback stack. Previously such
1363 extensions needed to call :cfunc:`PyCode_New`, which had many
1364 more arguments. (Added by Jeffrey Yasskin.)
1365
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001366* New function: :cfunc:`PyErr_NewExceptionWithDoc` creates a new
1367 exception class, just as the existing :cfunc:`PyErr_NewException` does,
1368 but takes an extra ``char *`` argument containing the docstring for the
1369 new exception class. (Added by the 'lekma' user on the Python bug tracker;
1370 :issue:`7033`.)
1371
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001372* New function: :cfunc:`PyFrame_GetLineNumber` takes a frame object
1373 and returns the line number that the frame is currently executing.
1374 Previously code would need to get the index of the bytecode
1375 instruction currently executing, and then look up the line number
1376 corresponding to that address. (Added by Jeffrey Yasskin.)
1377
Benjamin Petersond69fe2a2010-02-03 02:59:43 +00001378* New functions: :cfunc:`PyLong_AsLongAndOverflow` and
1379 :cfunc:`PyLong_AsLongLongAndOverflow` approximates a Python long
1380 integer as a C :ctype:`long` or :ctype:`long long`.
1381 If the number is too large to fit into
1382 the output type, an *overflow* flag is set and returned to the caller.
1383 (Contributed by Case Van Horsen; :issue:`7528` and :issue:`7767`.)
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001384
Benjamin Petersona28e7022010-01-09 18:53:06 +00001385* New function: stemming from the rewrite of string-to-float conversion,
1386 a new :cfunc:`PyOS_string_to_double` function was added. The old
1387 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions
1388 are now deprecated.
1389
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001390* New macros: the Python header files now define the following macros:
1391 :cmacro:`Py_ISALNUM`,
1392 :cmacro:`Py_ISALPHA`,
1393 :cmacro:`Py_ISDIGIT`,
1394 :cmacro:`Py_ISLOWER`,
1395 :cmacro:`Py_ISSPACE`,
1396 :cmacro:`Py_ISUPPER`,
1397 :cmacro:`Py_ISXDIGIT`,
1398 and :cmacro:`Py_TOLOWER`, :cmacro:`Py_TOUPPER`.
1399 All of these functions are analogous to the C
1400 standard macros for classifying characters, but ignore the current
1401 locale setting, because in
1402 several places Python needs to analyze characters in a
1403 locale-independent way. (Added by Eric Smith;
1404 :issue:`5793`.)
1405
1406 .. XXX these macros don't seem to be described in the c-api docs.
1407
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001408* New format codes: the :cfunc:`PyFormat_FromString`,
1409 :cfunc:`PyFormat_FromStringV`, and :cfunc:`PyErr_Format` now
1410 accepts ``%lld`` and ``%llu`` format codes for displaying values of
1411 C's :ctype:`long long` types.
1412 (Contributed by Mark Dickinson; :issue:`7228`.)
1413
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001414* The complicated interaction between threads and process forking has
1415 been changed. Previously, the child process created by
1416 :func:`os.fork` might fail because the child is created with only a
1417 single thread running, the thread performing the :func:`os.fork`.
1418 If other threads were holding a lock, such as Python's import lock,
1419 when the fork was performed, the lock would still be marked as
1420 "held" in the new process. But in the child process nothing would
1421 ever release the lock, since the other threads weren't replicated,
1422 and the child process would no longer be able to perform imports.
1423
1424 Python 2.7 now acquires the import lock before performing an
1425 :func:`os.fork`, and will also clean up any locks created using the
1426 :mod:`threading` module. C extension modules that have internal
1427 locks, or that call :cfunc:`fork()` themselves, will not benefit
1428 from this clean-up.
1429
1430 (Fixed by Thomas Wouters; :issue:`1590864`.)
1431
Benjamin Petersona28e7022010-01-09 18:53:06 +00001432* The :cfunc:`Py_Finalize` function now calls the internal
1433 :func:`threading._shutdown` function; this prevents some exceptions from
1434 being raised when an interpreter shuts down.
1435 (Patch by Adam Olsen; :issue:`1722344`.)
1436
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001437* Global symbols defined by the :mod:`ctypes` module are now prefixed
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001438 with ``Py``, or with ``_ctypes``. (Implemented by Thomas
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001439 Heller; :issue:`3102`.)
1440
Benjamin Petersona28e7022010-01-09 18:53:06 +00001441* New configure option: the :option:`--with-system-expat` switch allows
1442 building the :mod:`pyexpat` module to use the system Expat library.
1443 (Contributed by Arfrever Frehtes Taifersar Arahesis; :issue:`7609`.)
1444
1445* New configure option: Compiling Python with the
1446 :option:`--with-valgrind` option will now disable the pymalloc
1447 allocator, which is difficult for the Valgrind to analyze correctly.
1448 Valgrind will therefore be better at detecting memory leaks and
1449 overruns. (Contributed by James Henstridge; :issue:`2422`.)
1450
1451* New configure option: you can now supply no arguments to
1452 :option:`--with-dbmliborder=` in order to build none of the various
1453 DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis;
1454 :issue:`6491`.)
1455
Benjamin Petersond23f8222009-04-05 19:13:16 +00001456* The :program:`configure` script now checks for floating-point rounding bugs
1457 on certain 32-bit Intel chips and defines a :cmacro:`X87_DOUBLE_ROUNDING`
1458 preprocessor definition. No code currently uses this definition,
1459 but it's available if anyone wishes to use it.
1460 (Added by Mark Dickinson; :issue:`2937`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001461
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001462* The build process now creates the necessary files for pkg-config
1463 support. (Contributed by Clinton Roy; :issue:`3585`.)
1464
1465* The build process now supports Subversion 1.7. (Contributed by
1466 Arfrever Frehtes Taifersar Arahesis; :issue:`6094`.)
1467
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001468
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001469.. ======================================================================
1470
1471Port-Specific Changes: Windows
1472-----------------------------------
1473
Georg Brandl1f01deb2009-01-03 22:47:39 +00001474* The :mod:`msvcrt` module now contains some constants from
1475 the :file:`crtassem.h` header file:
1476 :data:`CRT_ASSEMBLY_VERSION`,
1477 :data:`VC_ASSEMBLY_PUBLICKEYTOKEN`,
1478 and :data:`LIBRARIES_ASSEMBLY_NAME_PREFIX`.
Benjamin Peterson1010bf32009-01-30 04:00:29 +00001479 (Contributed by David Cournapeau; :issue:`4365`.)
1480
1481* The new :cfunc:`_beginthreadex` API is used to start threads, and
1482 the native thread-local storage functions are now used.
1483 (Contributed by Kristjan Valur Jonsson; :issue:`3582`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001484
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001485* The :func:`os.listdir` function now correctly fails
1486 for an empty path. (Fixed by Hirokazu Yamamoto; :issue:`5913`.)
1487
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001488* The :mod:`mimelib` module will now read the MIME database from
1489 the Windows registry when initializing.
1490 (Patch by Gabriel Genellina; :issue:`4969`.)
1491
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001492.. ======================================================================
1493
1494Port-Specific Changes: Mac OS X
1495-----------------------------------
1496
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001497* The path ``/Library/Python/2.7/site-packages`` is now appended to
Benjamin Petersond23f8222009-04-05 19:13:16 +00001498 ``sys.path``, in order to share added packages between the system
1499 installation and a user-installed copy of the same version.
1500 (Changed by Ronald Oussoren; :issue:`4865`.)
1501
1502
1503Other Changes and Fixes
1504=======================
1505
Benjamin Peterson9895f912010-03-21 22:05:32 +00001506* Two benchmark scripts, :file:`iobench` and :file:`ccbench`, were
1507 added to the :file:`Tools` directory. :file:`iobench` measures the
1508 speed of built-in file I/O objects (as returned by :func:`open`)
1509 while performing various operations, and :file:`ccbench` is a
1510 concurrency benchmark that tries to measure computing throughput,
1511 thread switching latency, and IO processing bandwidth when
1512 performing several tasks using a varying number of threads.
1513
Benjamin Petersond23f8222009-04-05 19:13:16 +00001514* When importing a module from a :file:`.pyc` or :file:`.pyo` file
1515 with an existing :file:`.py` counterpart, the :attr:`co_filename`
Benjamin Peterson25c95f12009-05-08 20:42:26 +00001516 attributes of the resulting code objects are overwritten when the
1517 original filename is obsolete. This can happen if the file has been
1518 renamed, moved, or is accessed through different paths. (Patch by
1519 Ziga Seilnacht and Jean-Paul Calderone; :issue:`1180193`.)
Benjamin Petersond23f8222009-04-05 19:13:16 +00001520
1521* The :file:`regrtest.py` script now takes a :option:`--randseed=`
1522 switch that takes an integer that will be used as the random seed
1523 for the :option:`-r` option that executes tests in random order.
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001524 The :option:`-r` option also reports the seed that was used
Benjamin Petersond23f8222009-04-05 19:13:16 +00001525 (Added by Collin Winter.)
1526
Benjamin Petersona28e7022010-01-09 18:53:06 +00001527* Another :file:`regrtest.py` switch is :option:`-j`, which
1528 takes an integer specifying how many tests run in parallel. This
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001529 allows reducing the total runtime on multi-core machines.
Antoine Pitrou88909542009-06-29 13:54:42 +00001530 This option is compatible with several other options, including the
1531 :option:`-R` switch which is known to produce long runtimes.
Benjamin Petersona28e7022010-01-09 18:53:06 +00001532 (Added by Antoine Pitrou, :issue:`6152`.) This can also be used
1533 with a new :option:`-F` switch that runs selected tests in a loop
1534 until they fail. (Added by Antoine Pitrou; :issue:`7312`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001535
1536.. ======================================================================
1537
1538Porting to Python 2.7
1539=====================
1540
1541This section lists previously described changes and other bugfixes
1542that may require changes to your code:
1543
Benjamin Petersonf6489f92009-11-25 17:46:26 +00001544* When using :class:`Decimal` instances with a string's
1545 :meth:`format` method, the default alignment was previously
1546 left-alignment. This has been changed to right-alignment, which might
1547 change the output of your programs.
1548 (Changed by Mark Dickinson; :issue:`6857`.)
1549
1550 Another :meth:`format`-related change: the default precision used
1551 for floating-point and complex numbers was changed from 6 decimal
1552 places to 12, which matches the precision used by :func:`str`.
1553 (Changed by Eric Smith; :issue:`5920`.)
1554
Benjamin Peterson87c8d872009-06-11 22:54:11 +00001555* Because of an optimization for the :keyword:`with` statement, the special
1556 methods :meth:`__enter__` and :meth:`__exit__` must belong to the object's
1557 type, and cannot be directly attached to the object's instance. This
1558 affects new-style classes (derived from :class:`object`) and C extension
1559 types. (:issue:`6101`.)
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001560
Benjamin Peterson9eea4802009-12-31 03:31:15 +00001561* The :meth:`readline` method of :class:`StringIO` objects now does
1562 nothing when a negative length is requested, as other file-like
1563 objects do. (:issue:`7348`).
1564
Benjamin Peterson9895f912010-03-21 22:05:32 +00001565In the standard library:
1566
1567* The ElementTree library, :mod:`xml.etree`, no longer escapes
1568 ampersands and angle brackets when outputting an XML processing
1569 instruction (which looks like `<?xml-stylesheet href="#style1"?>`)
1570 or comment (which looks like `<!-- comment -->`).
1571 (Patch by Neil Muller; :issue:`2746`.)
1572
Benjamin Petersona28e7022010-01-09 18:53:06 +00001573For C extensions:
1574
1575* C extensions that use integer format codes with the ``PyArg_Parse*``
1576 family of functions will now raise a :exc:`TypeError` exception
1577 instead of triggering a :exc:`DeprecationWarning` (:issue:`5080`).
1578
1579* Use the new :cfunc:`PyOS_string_to_double` function instead of the old
1580 :cfunc:`PyOS_ascii_strtod` and :cfunc:`PyOS_ascii_atof` functions,
1581 which are now deprecated.
1582
1583
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001584.. ======================================================================
1585
1586
1587.. _acks27:
1588
1589Acknowledgements
1590================
1591
1592The author would like to thank the following people for offering
1593suggestions, corrections and assistance with various drafts of this
Benjamin Peterson97dd9872009-12-13 01:23:39 +00001594article: Ryan Lovett, Hugh Secker-Walker.
Benjamin Petersonf10a79a2008-10-11 00:49:57 +00001595